-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathview.py
471 lines (411 loc) · 18.4 KB
/
view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
from gi.repository import Gtk
class ViewContainer(Gtk.Stack):
def __init__(self, title, headerBar, selectionToolbar, useStack):
Gtk.Stack.__init__(self, transition_type=Gtk.StackTransitionType.CROSSFADE)
self._grid = Gtk.Grid(orientation=Gtk.Orientation.VERTICAL)
self._iconWidth = -1
self._iconHeight = 128
self._offset = 0
self._adjustmentValueId = 0
self._adjustmentChangedId = 0
self._scrollbarVisibleId = 0
self._model = Gtk.ListStore.new([
GObject.TYPE_STRING,
GObject.TYPE_STRING,
GObject.TYPE_STRING,
GObject.TYPE_STRING,
GdkPixbuf.Pixbuf,
GObject.TYPE_OBJECT,
GObject.TYPE_BOOLEAN,
GObject.TYPE_INT,
GObject.TYPE_STRING,
GObject.TYPE_BOOLEAN,
GObject.TYPE_BOOLEAN
])
self.view = Gd.MainView(
shadow_type=Gtk.ShadowType.NONE
)
self.view.set_view_type(Gd.MainViewType.ICON)
self.view.set_model(self._model)
self.selectionToolbar = selectionToolbar
box = Gtk.Box({orientation: Gtk.Orientation.VERTICAL})
box.pack_start(self.view, True, True, 0)
if useStack:
self.stack = Gd.Stack(
transition_type=Gd.StackTransitionType.SLIDE_RIGHT,
)
dummy = Gtk.Frame(visible=False)
self.stack.add_named(dummy, "dummy")
self.stack.add_named(_box, "artists")
self.stack.set_visible_child_name("dummy")
self._grid.add(self.stack)
else:
self._grid.add(box)
self._loadMore = Widgets.LoadMoreButton(self._getRemainingItemCount)
box.pack_end(self._loadMore.widget, False, False, 0)
self._loadMore.widget.connect("clicked", self.populate)
self.view.connect('item-activated', self._onItemActivated)
self._cursor = None
self.headerBar = headerBar
self.headerBar._selectButton.connect('toggled', self._onHeaderBarToggled)
headerBar._cancelButton.connect('clicked', self._onCancelButtonClicked)
self.title = title
self.add(self._grid)
self.show_all()
self._items = []
self._loadMore.widget.hide()
self._connectView()
self._symbolicIcon = albumArtCache.makeDefaultIcon(self._iconHeight, self._iconWidth)
self._init = False
grilo.connect('ready', _onGriloReady)
self.header_bar.connect('state-changed', self._onStateChanged)
self.view.connect('view-selection-changed', self._onViewSelectionChanged)
def _onHeaderBarToggled(self, button):
if button.get_active():
self.view.set_selection_mode(True)
self.header_bar.setSelectionMode(True)
self.selection_toolbar.eventbox.set_visible(True)
self.selection_toolbar._add_to_playlist_button.sensitive = False
else:
self.view.set_selection_mode(False)
self.header_bar.setSelectionMode(False)
self.selection_toolbar.eventbox.set_visible(False)
def _onCancelButtonClicked(self, button):
self.view.set_selection_mode(False)
headerBar.setSelectionMode(False)
def _onGriloRead(self):
if (self.headerBar.get_stack().get_visible_child() == self and self._init == False):
self._populate()
self.headerBar.get_stack().connect('notify::visible-child',
self._onHeaderBarVisible)
def _onHeaderBarVisible(self, widget, param):
if self == widget.get_visible_child() and self._init:
self._populate()
def _onViewSelectionChanged(self):
items = self.view.get_selection()
self.selectionToolbar._add_to_playlist_button.sensitive = items.length > 0
def _populate(self):
self._init = True
self.populate()
def _onStateChanged(self):
pass
def _connectView(self):
self._adjustmentValueId = self.view.vadjustment.connect(
'value-changed',
self._onScrolledWinChange)
self._adjustmentChangedId = self.view.vadjustment.connect(
'changed',
self._onScrolledWinChange)
self._scrollbarVisibleId = self.view.get_vscrollbar().connect(
'notify::visible',
self._onScrolledWinChange)
self._onScrolledWinChange()
def _onScrolledWinChange(self):
vScrollbar = self.view.get_vscrollbar()
adjustment = self.view.vadjustment
revealAreaHeight = 32
#if there's no vscrollbar, or if it's not visible, hide the button
if !vScrollbar or !vScrollbar.get_visible():
self._loadMore.setBlock(True)
return
value = adjustment.value
upper = adjustment.upper
page_size = adjustment.page_size
end = False
#special case self values which happen at construction
if (value == 0) && (upper == 1) && (page_size == 1):
end = False
else:
end = !(value < (upper - page_size - revealAreaHeight))
if self._getRemainingItemCount() <= 0:
end = False
self._loadMore.setBlock(!end)
def populate:
pass
def _addItem(self, source, param, item):
if item != None:
self._offset += 1
iter = self._model.append()
artist = "Unknown"
if item.get_author() != None;
artist = item.get_author()
if item.get_string(Grl.METADATA_KEY_ARTIST) != None;
artist = item.get_string(Grl.METADATA_KEY_ARTIST)
if (item.get_title() == None) and (item.get_url() != None):
item.set_title (extractFileName(item.get_url()))
try:
if item.get_url():
self.player.discoverer.discover_uri(item.get_url())
self._model.set(
iter,
[0, 1, 2, 3, 4, 5, 7, 8, 9, 10],
[toString(item.get_id()), "", item.get_title(), artist, self._symbolicIcon, item, -1, nowPlayingIconName, False, False]
)
except:
print("failed to discover url " + item.get_url())
self._model.set(
iter,
[0, 1, 2, 3, 4, 5, 7, 8, 9, 10],
[toString(item.get_id()), "", item.get_title(), artist, self._symbolicIcon, item, -1, errorIconName, False, True]
)
}
GLib.idle_add(300, lambda item,iter: self._updateAlbumArt, item, iter)
def _getRemainingItemCount(self):
count = -1
if self.countQuery != None:
cursor = tracker.query(self.countQuery, None)
if cursor != None and cursor.next(None):
count = cursor.get_integer(0)
return count - self._offset
def _updateAlbumArt(self,item, iter):
albumArtCache.lookupOrResolve(item, self._iconWidth, self._iconHeight, _albumArtCacheLookUp)
return False
def _albumArtCacheLookUp(self, icon):
if icon:
self._model.set_value(iter, 4, albumArtCache.makeIconFrame(icon))
else:
self._model.set_value(iter, 4, None)
self.emit("album-art-updated")
def _addListRenderers(self):
pass
def _onItemActivated(self, widget, id, path):
pass
#Class for the Empty View
class Empty(Gtk.Stack):
def __init__(self, headerBar, player):
Gtk.Stack.__init__(self, transition_type=Gtk.StackTransitionType.CROSSFADE)
builder = Gtk.Builder()
builder.add_from_resource('/org/gnome/music/NoMusic.ui')
widget = builder.get_object('container')
self.add(widget)
self.show_all()
class Albums(ViewContainer):
def __init__(self, HeaderBar, selectionToolbar, player):
ViewContainer.__init__("Albums", headerBar,selectionToolbar)
self.view.set_view_type(Gd.MainViewType.ICON)
self.countQuery = Query.album_count
self._albumWidget = Widgets.AlbumWidget(player)
self.add(self._albumWidget)
self.headerBar.setState (1)
def _onStateChanged(self, widget):
if self.headerBar.get_stack() != None and
self == self.headerBar.get_stack().get_visible_child())
self.visible_child = self._grid
def _onItemActivated(self, widget, id, path):
iter = self._model.get_iter (path)[1]
title = self._model.get_value (iter, 2)
artist = self._model.get_value (iter, 3)
item = self._model.get_value (iter, 5)
self._albumWidget.update (artist, title, item, self.headerBar,self.selectionToolbar)
self.headerBar.setState (0)
self.headerBar.header_bar.title = title
self.headerBar.header_bar.sub_title = artist
self.visible_child = self._albumWidget
def populate(self):
if grilo.tracker != None:
grilo.populateAlbums (self._offset, self._addItem, None)
class Songs(ViewContainer):
def __init__(self, headerBar, selectionToolbar, player):
ViewContainer.__init__("Songs", headerBar, selectionToolbar)
self.countQuery = Query.songs_count
self._items = {}
self.isStarred = None
self.view.set_view_type(Gd.MainViewType.LIST)
self.view.get_generic_view().get_style_context().add_class("songs-list")
self._iconHeight = 32
self._iconWidth = 32
self._symbolicIcon = albumArtCache.makeDefaultIcon(self._iconHeight, self._iconWidth)
self._addListRenderers()
self.player = player
self.player.connect('playlist-item-changed', Lang.bind(self, self.updateModel))
def _onItemActivated(self, widget, id, path):
iter = self._model.get_iter(path)[1]
if self._model.get_value(iter, 8) != errorIconName:
self.player.setPlaylist("Songs", None, self._model, iter, 5)
self.player.setPlaying(True)
def updateModel(self, player, playlist, currentIter):
if playlist != self._model:
return False
if self.iterToClean:
self._model.set_value(self.iterToClean, 10, False)
self._model.set_value(currentIter, 10, True)
self.iterToClean = currentIter.copy()
return False
def _addItem(self, source, param, item):
if item != None:
self._offset += 1
iter = self._model.append()
if (item.get_title() == None) && (item.get_url() != None):
item.set_title (extractFileName(item.get_url()))
try:
if item.get_url():
self.player.discoverer.discover_uri(item.get_url())
self._model.set(
iter,
[5, 8, 9, 10],
[item, nowPlayingIconName, false, false]
)
except:
print("failed to discover url " + item.get_url())
self._model.set(
iter,
[5, 8, 9, 10],
[item, errorIconName, False, True]
)
def _addListRenderers(self):
listWidget = self.view.get_generic_view()
cols = listWidget.get_columns()
cells = cols[0].get_cells()
cells[2].visible = False
nowPlayingSymbolRenderer = Gtk.CellRendererPixbuf()
columnNowPlaying = Gtk.TreeViewColumn()
nowPlayingSymbolRenderer.xalign = 1.0
columnNowPlaying.pack_start(nowPlayingSymbolRenderer, False)
columnNowPlaying.fixed_width = 24
columnNowPlaying.add_attribute(nowPlayingSymbolRenderer, "visible", 10)
columnNowPlaying.add_attribute(nowPlayingSymbolRenderer, "icon_name", 8)
listWidget.insert_column(columnNowPlaying, 0)
titleRenderer = Gtk.CellRendererText(xpad=0)
listWidget.add_renderer(titleRenderer, self._onListWidgetTitleRender)
starRenderer = Gtk.CellRendererPixbuf(xpad=32)
listWidget.add_renderer(starRenderer, self._onListWidgetStarRender)
durationRenderer = Gd.StyledTextRenderer(xpad=32)
durationRenderer.add_class('dim-label')
listWidget.add_renderer(durationRenderer, self._onListWidgetDurationRender)
artistRenderer = Gd.StyledTextRenderer(xpad=32)
artistRenderer.add_class('dim-label')
artistRenderer.ellipsize = Pango.EllipsizeMode.END
listWidget.add_renderer(artistRenderer, self._onListWidgetArtistRender)
typeRenderer = Gd.StyledTextRenderer(xpad: 32)
typeRenderer.add_class('dim-label')
typeRenderer.ellipsize = Pango.EllipsizeMode.END
listWidget.add_renderer(typeRenderer, self._onListWidgetTypeRender)
def _onListWidgetTitleRender(self, col, cell, model, iter):
item = model.get_value(iter,5)
titleRenderer.xalign = 0.0
titleRenderer.yalign = 0.5
titleRenderer.height = 48
titleRenderer.ellipsize = Pango.EllipsizeMode.END
titleRenderer.text = item.get_title()
def _onListWidgetStarRender(self, col, cell, model, iter):
showstar = model.get_value(iter, 9)
if(showstar):
starRenderer.icon_name = starIconName
else:
starRenderer.pixbuf = None
def _onListWidgetDurationRender(self, col, cell, model, iter) {
item = model.get_value(iter, 5)
if item:
duration = item.get_duration ()
minutes = parseInt(duration / 60)
seconds = duration % 60
time = None
if seconds < 10:
time = minutes + ":0" + seconds
else:
time = minutes + ":" + seconds
durationRenderer.xalign = 1.0
durationRenderer.text = time
def _onListWidgetArtistRender(self, col, cell, model, iter):
item = model.get_value(iter, 5)
if item:
artistRenderer.ellipsize = Pango.EllipsizeMode.END
artistRenderer.text = item.get_string(Grl.METADATA_KEY_ARTIST)
def _onListWidgetTypeRender(self, coll, cell, model, iter):
item = model.get_value(iter, 5)
if item:
typeRenderer.ellipsize = Pango.EllipsizeMode.END
typeRenderer.text = item.get_string(Grl.METADATA_KEY_ALBUM)
def populate(self):
if grilo.tracker != None:
grilo.populateSongs (self._offset, lambda: self._addItem, None)
class Playlist(ViewContainer):
def __init__(self, headerBar, selectionToolbar, player)
ViewContainer.__init__("Playlists", headerBar, selectionToolbar)
class Artists (ViewContainer):
def __init__(self, headerBar, selectionToolbar, player):
ViewContainer.__init__("Artists", headerBar, selectionToolbar, True)
self.player = player
self._artists = {}
self.countQuery = Query.artist_count
self._artistAlbumsWidget = Gtk.Frame(
shadow_type=Gtk.ShadowType.NONE
)
self.view.set_view_type(Gd.MainViewType.LIST)
self.view.set_hexpand(False)
self._artistAlbumsWidget.set_hexpand(True)
self.view.get_style_context().add_class("artist-panel")
self.view.get_generic_view().get_selection().set_mode(Gtk.SelectionMode.SINGLE)
self._grid.attach(Gtk.Separator({orientation: Gtk.Orientation.VERTICAL}), 1, 0, 1, 1)
self._grid.attach(self._artistAlbumsWidget, 2, 0, 2, 2)
self._addListRenderers()
if Gtk.Settings.get_default().gtk_application_prefer_dark_theme:
self.view.get_generic_view().get_style_context().add_class("artist-panel-dark")
else:
self.view.get_generic_view().get_style_context().add_class("artist-panel-white")
self.show_all()
def _populate(self, widget, param):
selection = self.view.get_generic_view().get_selection()
if !selection.get_selected()[0]:
self._allIter = self._model.append()
self._artists["All Artists".toLowerCase()] = {"iter": self._allIter, "albums": []}
self._model.set(
self._allIter,
[0, 1, 2, 3],
["All Artists", "All Artists", "All Artists", "All Artists"]
)
selection.select_path(self._model.get_path(self._allIter))
self.view.emit('item-activated', "0", self._model.get_path(self._allIter))
self._init = True
self.populate()
def _addListRenderers(self):
listWidget = self.view.get_generic_view()
cols = listWidget.get_columns()
cells = cols[0].get_cells()
cells[2].visible = False
typeRenderer = Gd.StyledTextRenderer(xpad: 0)
typeRenderer.ellipsize = 3
typeRenderer.xalign = 0.0
typeRenderer.yalign = 0.5
typeRenderer.height = 48
typeRenderer.width = 220
listWidget.add_renderer(typeRenderer, lambda col, cell, model, iter:
typeRenderer.text = model.get_value(iter, 0)
)
def _onItemActivated(self, widget, id, path):
children = self._artistAlbumsWidget.get_children()
for (i=0 i<children.length i++)
self._artistAlbumsWidget.remove(children[i])
iter = self._model.get_iter (path)[1]
artist = self._model.get_value (iter, 0)
albums = self._artists[artist.toLowerCase()]["albums"]
self.artistAlbums = None
if self._model.get_string_from_iter(iter) == self._model.get_string_from_iter(self._allIter):
self.artistAlbums = Widgets.AllArtistsAlbums(self.player)
else:
self.artistAlbums = Widgets.ArtistAlbums(artist, albums, self.player)
self._artistAlbumsWidget.add(self.artistAlbums)
def _addItem(self, source, param, item):
self._offset += 1
if :item == None:
return
artist = "Unknown"
if item.get_author() != None:
artist = item.get_author()
if item.get_string(Grl.METADATA_KEY_ARTIST) != None:
artist = item.get_string(Grl.METADATA_KEY_ARTIST)
if self._artists[artist.toLowerCase()] == undefined:
iter = self._model.append()
self._artists[artist.toLowerCase()] = {"iter": iter, "albums": []}
self._model.set(
iter,
[0, 1, 2, 3],
[artist, artist, artist, artist]
)
self._artists[artist.toLowerCase()]["albums"].append(item)
self.emit("artist-added")
},
def populate(self):
if grilo.tracker != None:
grilo.populateArtists(self._offset, lambda: self._addItem, None)
#FIXME: We're emitting self too early, need to wait for all artists to be filled in