Commit 0ccd89f6 authored by Marinus Schraal's avatar Marinus Schraal

views: Split up the views

parent a668832c
......@@ -52,6 +52,7 @@ AC_CONFIG_FILES([
help/Makefile
data/AboutDialog.ui
gnomemusic/Makefile
gnomemusic/views/Makefile
po/Makefile.in
libgd/Makefile
])
......
SUBDIRS = views
appdir = $(pythondir)/gnomemusic/
app_PYTHON = \
......@@ -8,7 +10,6 @@ app_PYTHON = \
mpris.py \
notification.py \
toolbar.py \
view.py \
grilo.py \
playlists.py\
utils.py \
......
This diff is collapsed.
appdir = $(pythondir)/gnomemusic/views/
app_PYTHON = \
__init__.py \
albumsview.py \
artistsview.py \
baseview.py \
emptysearchview.py \
initialstateview.py \
playlistview.py \
searchview.py \
songsview.py
# Copyright (c) 2016 The GNOME Music Developers
#
# GNOME Music is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# GNOME Music is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with GNOME Music; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# The GNOME Music authors hereby grant permission for non-GPL compatible
# GStreamer plugins to be used and distributed together with GStreamer
# and GNOME Music. This permission is above and beyond the permissions
# granted by the GPL license by which GNOME Music is covered. If you
# modify this code, you may extend this exception to your version of the
# code, but you are not obligated to do so. If you do not wish to do so,
# delete this exception statement from your version.
from gettext import gettext as _
from gi.repository import GLib, GObject, Gtk
from gnomemusic import log
from gnomemusic.albumartcache import ArtSize
from gnomemusic.grilo import grilo
from gnomemusic.toolbar import ToolbarState
from gnomemusic.views.baseview import BaseView
import gnomemusic.utils as utils
import gnomemusic.widgets as Widgets
class AlbumsView(BaseView):
def __repr__(self):
return '<AlbumsView>'
@log
def __init__(self, window, player):
BaseView.__init__(self, 'albums', _("Albums"), window, None)
self._albumWidget = Widgets.AlbumWidget(player, self)
self.player = player
self.add(self._albumWidget)
self.albums_selected = []
self.all_items = []
self.items_selected = []
self.items_selected_callback = None
self._add_list_renderers()
@log
def _on_changes_pending(self, data=None):
if (self._init and not self.header_bar._selectionMode):
self._offset = 0
self._init = True
GLib.idle_add(self.populate)
grilo.changes_pending['Albums'] = False
@log
def _on_selection_mode_changed(self, widget, data=None):
if (not self.header_bar._selectionMode
and grilo.changes_pending['Albums']):
self._on_changes_pending()
@log
def _setup_view(self, view_type):
self.view = Gtk.FlowBox(homogeneous=True,
hexpand=True,
halign=Gtk.Align.FILL,
valign=Gtk.Align.START,
selection_mode=Gtk.SelectionMode.NONE,
margin=18,
row_spacing=12,
column_spacing=6,
min_children_per_line=1,
max_children_per_line=25)
self.view.connect('child-activated', self._on_child_activated)
scrolledwin = Gtk.ScrolledWindow()
scrolledwin.add(self.view)
scrolledwin.show()
self._box.add(scrolledwin)
@log
def _back_button_clicked(self, widget, data=None):
self.header_bar.reset_header_title()
self.set_visible_child(self._grid)
@log
def _on_child_activated(self, widget, child, user_data=None):
item = child.media_item
if self.star_handler.star_renderer_click:
self.star_handler.star_renderer_click = False
return
# Toggle the selection when in selection mode
if self.selection_mode:
child.check.set_active(not child.check.get_active())
return
title = utils.get_media_title(item)
self._escaped_title = title
self._artist = utils.get_artist_name(item)
self._albumWidget.update(self._artist, title, item,
self.header_bar, self.selection_toolbar)
self.header_bar.set_state(ToolbarState.CHILD_VIEW)
self.header_bar.header_bar.set_title(self._escaped_title)
self.header_bar.header_bar.sub_title = self._artist
self.set_visible_child(self._albumWidget)
@log
def update_title(self):
self.header_bar.header_bar.set_title(self._escaped_title)
self.header_bar.header_bar.sub_title = self._artist
@log
def populate(self):
if grilo.tracker:
self.window._init_loading_notification()
GLib.idle_add(grilo.populate_albums, self._offset, self._add_item)
@log
def get_selected_tracks(self, callback):
if self.header_bar._state == ToolbarState.CHILD_VIEW:
items = []
for path in self._albumWidget.view.get_selection():
_iter = self._albumWidget.model.get_iter(path)
items.append(self._albumWidget.model.get_value(_iter, 5))
callback(items)
else:
self.items_selected = []
self.items_selected_callback = callback
self.albums_index = 0
if len(self.albums_selected):
self._get_selected_album_songs()
@log
def _add_item(self, source, param, item, remaining=0, data=None):
self.window.notification.set_timeout(0)
if item:
# Store all items to optimize 'Select All' action
self.all_items.append(item)
# Add to the flowbox
child = self._create_album_item(item)
self.view.add(child)
elif remaining == 0:
self.window.notification.dismiss()
self.view.show()
def _create_album_item(self, item):
artist = utils.get_artist_name(item)
title = utils.get_media_title(item)
builder = Gtk.Builder.new_from_resource('/org/gnome/Music/AlbumCover.ui')
child = Gtk.FlowBoxChild()
child.image = builder.get_object('image')
child.check = builder.get_object('check')
child.title = builder.get_object('title')
child.subtitle = builder.get_object('subtitle')
child.events = builder.get_object('events')
child.media_item = item
child.title.set_label(title)
child.subtitle.set_label(artist)
child.image.set_from_surface(self._loading_icon_surface)
# In the case of off-sized icons (eg. provided in the soundfile)
# keep the size request equal to all other icons to get proper
# alignment with GtkFlowBox.
child.image.set_property("width-request", ArtSize.medium.width)
child.image.set_property("height-request", ArtSize.medium.height)
child.events.connect('button-release-event',
self._on_album_event_triggered,
child)
child.check_handler_id = child.check.connect('notify::active',
self._on_child_toggled,
child)
child.check.bind_property('visible', self, 'selection_mode',
GObject.BindingFlags.BIDIRECTIONAL)
child.add(builder.get_object('main_box'))
child.show()
self.cache.lookup(item,
ArtSize.medium,
self._on_lookup_ready,
child)
return child
@log
def _on_album_event_triggered(self, evbox, event, child):
if event.button is 3:
self._on_selection_mode_request()
if self.selection_mode:
child.check.set_active(True)
def _on_lookup_ready(self, icon, child):
child.image.set_from_surface(icon)
@log
def _on_child_toggled(self, check, pspec, child):
if check.get_active() and not child.media_item in self.albums_selected:
self.albums_selected.append(child.media_item)
elif not check.get_active() and child.media_item in self.albums_selected:
self.albums_selected.remove(child.media_item)
self.update_header_from_selection(len(self.albums_selected))
@log
def _get_selected_album_songs(self):
grilo.populate_album_songs(
self.albums_selected[self.albums_index],
self._add_selected_item)
self.albums_index += 1
@log
def _add_selected_item(self, source, param, item, remaining=0, data=None):
if item:
self.items_selected.append(item)
if remaining == 0:
if self.albums_index < len(self.albums_selected):
self._get_selected_album_songs()
else:
self.items_selected_callback(self.items_selected)
def _toggle_all_selection(self, selected):
"""
Selects or unselects all items without sending the notify::active
signal for performance purposes.
"""
for child in self.view.get_children():
GObject.signal_handler_block(child.check, child.check_handler_id)
# Set the checkbutton state without emiting the signal
child.check.set_active(selected)
GObject.signal_handler_unblock(child.check, child.check_handler_id)
self.update_header_from_selection(len(self.albums_selected))
@log
def select_all(self):
self.albums_selected = list(self.all_items)
self._toggle_all_selection(True)
@log
def unselect_all(self):
self.albums_selected = []
self._toggle_all_selection(False)
# Copyright (c) 2016 The GNOME Music Developers
#
# GNOME Music is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# GNOME Music is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with GNOME Music; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# The GNOME Music authors hereby grant permission for non-GPL compatible
# GStreamer plugins to be used and distributed together with GStreamer
# and GNOME Music. This permission is above and beyond the permissions
# granted by the GPL license by which GNOME Music is covered. If you
# modify this code, you may extend this exception to your version of the
# code, but you are not obligated to do so. If you do not wish to do so,
# delete this exception statement from your version.
from gettext import gettext as _
from gi.repository import Gd, GLib, Gtk, Pango
from gnomemusic import log
from gnomemusic.grilo import grilo
from gnomemusic.views.baseview import BaseView
import gnomemusic.utils as utils
import gnomemusic.widgets as Widgets
class ArtistsView(BaseView):
def __repr__(self):
return '<ArtistsView>'
@log
def __init__(self, window, player):
BaseView.__init__(self, 'artists', _("Artists"), window,
Gd.MainViewType.LIST, True)
self.artists_counter = 0
self.player = player
self._artists = {}
self.albums_selected = []
self.items_selected = []
self.items_selected_callback = None
self.artistAlbumsStack = Gtk.Stack(
transition_type=Gtk.StackTransitionType.CROSSFADE,
)
self._artistAlbumsWidget = Gtk.Frame(
shadow_type=Gtk.ShadowType.NONE,
hexpand=True
)
self.artistAlbumsStack.add_named(self._artistAlbumsWidget, "sidebar")
self.artistAlbumsStack.set_visible_child_name("sidebar")
self.view.set_shadow_type(Gtk.ShadowType.IN)
self.view.get_style_context().add_class('side-panel')
self.view.set_hexpand(False)
self.view.get_generic_view().get_selection().set_mode(
Gtk.SelectionMode.SINGLE)
self._grid.attach(self.artistAlbumsStack, 2, 0, 2, 2)
self._add_list_renderers()
self.view.get_generic_view().get_style_context().remove_class('content-view')
self.show_all()
self.view.hide()
@log
def _on_changes_pending(self, data=None):
if (self._init and self.header_bar._selectionMode is False):
self.model.clear()
self._artists.clear()
self._offset = 0
GLib.idle_add(self._populate)
grilo.changes_pending['Artists'] = False
@log
def _populate(self, data=None):
self._init = True
self.populate()
@log
def _add_list_renderers(self):
list_widget = self.view.get_generic_view()
cols = list_widget.get_columns()
cells = cols[0].get_cells()
cells[1].set_visible(False)
cells[2].set_visible(False)
self.text_renderer = Gd.StyledTextRenderer(
xpad=16,
ypad=16,
ellipsize=Pango.EllipsizeMode.END,
xalign=0.0,
width=220
)
list_widget.add_renderer(self.text_renderer, lambda *args: None, None)
cols[0].clear_attributes(self.text_renderer)
cols[0].add_attribute(self.text_renderer, 'text', 2)
@log
def _on_item_activated(self, widget, item_id, path):
if self.star_handler.star_renderer_click:
self.star_handler.star_renderer_click = False
return
try:
_iter = self.model.get_iter(path)
except TypeError:
return
self._last_selection = _iter
artist = self.model.get_value(_iter, 2)
albums = self._artists[artist.casefold()]['albums']
widget = self._artists[artist.casefold()]['widget']
if widget:
if widget.model == self.player.running_playlist('Artist', widget.artist):
self._artistAlbumsWidget = widget.get_parent()
GLib.idle_add(self.artistAlbumsStack.set_visible_child,
self._artistAlbumsWidget)
return
elif widget.get_parent() == self._artistAlbumsWidget:
return
else:
widget.get_parent().destroy()
# Prepare a new artistAlbumsWidget here
new_artistAlbumsWidget = Gtk.Frame(
shadow_type=Gtk.ShadowType.NONE,
hexpand=True
)
self.artistAlbumsStack.add(new_artistAlbumsWidget)
artistAlbums = None
artistAlbums = Widgets.ArtistAlbums(
artist, albums, self.player,
self.header_bar, self.selection_toolbar, self.window
)
self._artists[artist.casefold()]['widget'] = artistAlbums
new_artistAlbumsWidget.add(artistAlbums)
new_artistAlbumsWidget.show()
# Replace previous widget
self._artistAlbumsWidget = new_artistAlbumsWidget
GLib.idle_add(self.artistAlbumsStack.set_visible_child, new_artistAlbumsWidget)
@log
def _add_item(self, source, param, item, remaining=0, data=None):
self.window.notification.set_timeout(0)
if item is None:
if remaining == 0:
self.view.set_model(self.model)
self.window.notification.dismiss()
self.view.show()
return
self._offset += 1
artist = utils.get_artist_name(item)
if not artist.casefold() in self._artists:
_iter = self.model.insert_with_valuesv(-1, [2], [artist])
self._artists[artist.casefold()] = {'iter': _iter, 'albums': [], 'widget': None}
self._artists[artist.casefold()]['albums'].append(item)
@log
def populate(self):
if grilo.tracker:
self.window._init_loading_notification()
GLib.idle_add(grilo.populate_artists, self._offset, self._add_item)
@log
def _on_header_bar_toggled(self, button):
BaseView._on_header_bar_toggled(self, button)
if button.get_active():
self.text_renderer.set_fixed_size(178, -1)
self._last_selection =\
self.view.get_generic_view().get_selection().get_selected()[1]
self.view.get_generic_view().get_selection().set_mode(
Gtk.SelectionMode.NONE)
else:
self.text_renderer.set_fixed_size(220, -1)
self.view.get_generic_view().get_selection().set_mode(
Gtk.SelectionMode.SINGLE)
if self._last_selection is not None:
self.view.get_generic_view().get_selection().select_iter(
self._last_selection)
@log
def _on_selection_mode_changed(self, widget, data=None):
self.artistAlbumsStack.set_sensitive(not self.header_bar._selectionMode)
if self.header_bar._selectionMode is False and grilo.changes_pending['Artists'] is True:
self._on_changes_pending()
@log
def get_selected_tracks(self, callback):
self.items_selected = []
self.items_selected_callback = callback
self.albums_index = 0
self.albums_selected = []
for path in self.view.get_selection():
_iter = self.model.get_iter(path)
artist = self.model.get_value(_iter, 2)
albums = self._artists[artist.casefold()]['albums']
self.albums_selected.extend(albums)
if len(self.albums_selected):
self._get_selected_album_songs()
@log
def _get_selected_album_songs(self):
grilo.populate_album_songs(
self.albums_selected[self.albums_index],
self._add_selected_item)
self.albums_index += 1
@log
def _add_selected_item(self, source, param, item, remaining=0, data=None):
if item:
self.items_selected.append(item)
if remaining == 0:
if self.albums_index < len(self.albums_selected):
self._get_selected_album_songs()
else:
self.items_selected_callback(self.items_selected)
# Copyright (c) 2016 The GNOME Music Developers
#
# GNOME Music is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# GNOME Music is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with GNOME Music; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# The GNOME Music authors hereby grant permission for non-GPL compatible
# GStreamer plugins to be used and distributed together with GStreamer
# and GNOME Music. This permission is above and beyond the permissions
# granted by the GPL license by which GNOME Music is covered. If you
# modify this code, you may extend this exception to your version of the
# code, but you are not obligated to do so. If you do not wish to do so,
# delete this exception statement from your version.
from gettext import gettext as _, ngettext
from gi.repository import Gd, Gdk, GdkPixbuf, GObject, Gtk
from gnomemusic import log
from gnomemusic.albumartcache import AlbumArtCache, DefaultIcon, ArtSize
from gnomemusic.grilo import grilo
import gnomemusic.widgets as Widgets
import gnomemusic.utils as utils
class BaseView(Gtk.Stack):
nowPlayingIconName = 'media-playback-start-symbolic'
errorIconName = 'dialog-error-symbolic'
selection_mode = GObject.Property(type=bool, default=False)
def __repr__(self):
return '<BaseView>'
@log
def __init__(self, name, title, window, view_type, use_sidebar=False, sidebar=None):
Gtk.Stack.__init__(self,
transition_type=Gtk.StackTransitionType.CROSSFADE)
self._grid = Gtk.Grid(orientation=Gtk.Orientation.HORIZONTAL)
self._offset = 0
self._adjustmentValueId = 0
self._adjustmentChangedId = 0
self._scrollbarVisibleId = 0
self.old_vsbl_range = None
self.model = Gtk.ListStore(
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_INT,
GObject.TYPE_BOOLEAN,
GObject.TYPE_INT
)
self._box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
# Setup the main view
self._setup_view(view_type)
if use_sidebar:
self.stack = Gtk.Stack(
transition_type=Gtk.StackTransitionType.SLIDE_RIGHT,
)
dummy = Gtk.Frame(visible=False)
self.stack.add_named(dummy, 'dummy')
if sidebar:
self.stack.add_named(sidebar, 'sidebar')
else:
self.stack.add_named(self._box, 'sidebar')
self.stack.set_visible_child_name('dummy')
self._grid.add(self.stack)
if not use_sidebar or sidebar:
self._grid.add(self._box)
self.star_handler = Widgets.StarHandler(self, 9)
self._cursor = None
self.window = window
self.header_bar = window.toolbar
self.selection_toolbar = window.selection_toolbar
self.header_bar._select_button.connect(
'toggled', self._on_header_bar_toggled)
self.header_bar._cancel_button.connect(
'clicked', self._on_cancel_button_clicked)
self.name = name
self.title = title
self.add(self._grid)
self.show_all()
self.view.hide()
self._items = []
scale = self.get_scale_factor()
self.cache = AlbumArtCache(scale)
self._loading_icon_surface = DefaultIcon(scale).get(
DefaultIcon.Type.loading,
ArtSize.medium)
self._init = False
grilo.connect('ready', self._on_grilo_ready)
self.selection_socket = None
self.header_bar.connect('selection-mode-changed',
self._on_selection_mode_changed)
self._discovering_urls = {}
grilo.connect('changes-pending', self._on_changes_pending)
@log
def _on_changes_pending(self, data=None):
pass
@log
def _setup_view(self, view_type):
self.view = Gd.MainView(shadow_type=Gtk.ShadowType.NONE)
self.view.set_view_type(view_type)
self.view.click_handler = self.view.connect('item-activated', self._on_item_activated)
self.view.connect('selection-mode-request', self._on_selection_mode_request)
self.view.bind_property('selection-mode', self, 'selection_mode',
GObject.BindingFlags.BIDIRECTIONAL)
self.view.connect('view-selection-changed', self._on_view_selection_changed)
self._box.pack_start(self.view, True, True, 0)
@log
def _on_header_bar_toggled(self, button):
self.selection_mode = button.get_active()
if self.selection_mode:
self.header_bar.set_selection_mode(True)
self.player.actionbar.set_visible(False)
self.selection_toolbar.actionbar.set_visible(True)
self.selection_toolbar._add_to_playlist_button.set_sensitive(False)
self.selection_toolbar._remove_from_playlist_button.set_sensitive(False)
else:
self.header_bar.set_selection_mode(False)
self.player.actionbar.set_visible(self.player.currentTrack is not None)
self.selection_toolbar.actionbar.set_visible(False)
self.unselect_all()
@log
def _on_cancel_button_clicked(self, button):
self.view.set_selection_mode(False)
self.header_bar.set_selection_mode(False)
@log
def _on_grilo_ready(self, data=None):
if (self.header_bar.get_stack().get_visible_child() == self and not self._init):
self._populate()
self.header_bar.get_stack().connect('notify::visible-child',
self._on_headerbar_visible)
@log
def _on_headerbar_visible(self, widget, param):
if self == widget.get_visible_child() and not self._init:
self._populate()
@log
def _on_view_selection_changed(self, widget):
if not self.selection_mode:
return
items = self.view.get_selection()