Commit fba4ceda authored by Marinus Schraal's avatar Marinus Schraal

disclistboxwidget: Simplify property usage

Use the short-form PyGObject property declaration and bind properties
directly to sub-class properties if possible.
parent 20ea7024
......@@ -195,7 +195,7 @@ class AlbumWidget(Gtk.EventBox):
@log
def _on_header_cancel_button_clicked(self, button):
"""Cancel selection mode callback."""
self._disc_listbox.set_selection_mode(False)
self._disc_listbox.props.selection_mode = False
self._header_bar.set_selection_mode(False)
self._header_bar.header_bar.title = self._album
......@@ -204,14 +204,14 @@ class AlbumWidget(Gtk.EventBox):
"""Selection mode button clicked callback."""
if button.get_active():
self._selection_mode = True
self._disc_listbox.set_selection_mode(True)
self._disc_listbox.props.selection_mode = True
self._header_bar.set_selection_mode(True)
self._parent_view.set_player_visible(False)
self._header_bar.header_bar.set_custom_title(
self._header_bar._selection_menu_button)
else:
self._selection_mode = False
self._disc_listbox.set_selection_mode(False)
self._disc_listbox.props.selection_mode = False
self._header_bar.set_selection_mode(False)
if self._player.get_playback_status() != Playback.STOPPED:
self._parent_view.set_player_visible(True)
......@@ -221,8 +221,10 @@ class AlbumWidget(Gtk.EventBox):
disc_box = DiscBox(self._model)
disc_box.set_songs(disc_songs)
disc_box.set_disc_number(disc_nr)
disc_box.set_columns(1)
disc_box.show_song_numbers(False)
disc_box.props.columns = 1
disc_box.props.show_durations = True
disc_box.props.show_favorites = True
disc_box.props.show_song_numbers = False
disc_box.connect('song-activated', self._song_activated)
disc_box.connect('selection-toggle', self._selection_mode_toggled)
......@@ -239,7 +241,7 @@ class AlbumWidget(Gtk.EventBox):
@log
def _song_activated(self, widget, song_widget):
if self._selection_mode:
song_widget.selected = not song_widget.selected
song_widget.props.selected = not song_widget.props.selected
return
self._player.stop()
......@@ -275,7 +277,7 @@ class AlbumWidget(Gtk.EventBox):
disc = self._create_disc_box(disc_nr, discs[disc_nr])
self._disc_listbox.add(disc)
if len(discs) == 1:
disc.show_disc_label(False)
disc.props.show_disc_label = False
if remaining == 0:
self._set_duration_label()
......@@ -306,13 +308,13 @@ class AlbumWidget(Gtk.EventBox):
self._duration += song.get_duration()
if (song == current_song):
song_widget.state = SongWidget.State.PLAYING
song_widget.props.state = SongWidget.State.PLAYING
song_passed = True
elif (song_passed):
# Counter intuitive, but this is due to call order.
song_widget.state = SongWidget.State.UNPLAYED
song_widget.props.state = SongWidget.State.UNPLAYED
else:
song_widget.state = SongWidget.State.PLAYED
song_widget.props.state = SongWidget.State.PLAYED
_iter = playlist.iter_next(_iter)
......
......@@ -147,13 +147,13 @@ class ArtistAlbumsWidget(Gtk.Box):
song_widget = song.song_widget
if (song == current_song):
song_widget.state = SongWidget.State.PLAYING
song_widget.props.state = SongWidget.State.PLAYING
song_passed = True
elif (song_passed):
# Counter intuitive, but this is due to call order.
song_widget.state = SongWidget.State.UNPLAYED
song_widget.props.state = SongWidget.State.UNPLAYED
else:
song_widget.state = SongWidget.State.PLAYED
song_widget.props.state = SongWidget.State.PLAYED
itr = playlist.iter_next(itr)
......@@ -166,7 +166,7 @@ class ArtistAlbumsWidget(Gtk.Box):
while itr:
song = self._model[itr][5]
song_widget = song.song_widget
song_widget.state = SongWidget.State.UNPLAYED
song_widget.props.state = SongWidget.State.UNPLAYED
itr = self._model.iter_next(itr)
......
......@@ -97,9 +97,10 @@ class ArtistAlbumWidget(Gtk.Box):
disc_box = DiscBox(self._model)
disc_box.set_songs(disc_songs)
disc_box.set_disc_number(disc_nr)
disc_box.set_columns(2)
disc_box.show_duration(False)
disc_box.show_favorites(False)
disc_box.props.columns = 2
disc_box.props.show_durations = False
disc_box.props.show_favorites = False
disc_box.props.show_song_numbers = True
disc_box.connect('song-activated', self._song_activated)
disc_box.connect('selection-toggle', self._selection_mode_toggled)
......@@ -121,14 +122,14 @@ class ArtistAlbumWidget(Gtk.Box):
"""Selection mode button clicked callback."""
if button.get_active():
self._selection_mode = True
self._disc_listbox.set_selection_mode(True)
self._disc_listbox.props.selection_mode = True
self._header_bar.set_selection_mode(True)
self._parent_view.set_player_visible(False)
self._header_bar.header_bar.set_custom_title(
self._header_bar._selection_menu_button)
else:
self._selection_mode = False
self._disc_listbox.set_selection_mode(False)
self._disc_listbox.props.selection_mode = False
self._header_bar.set_selection_mode(False)
if self._player.get_playback_status() != Playback.STOPPED:
self._parent_view.set_player_visible(True)
......@@ -151,7 +152,7 @@ class ArtistAlbumWidget(Gtk.Box):
disc = self.create_disc_box(disc_nr, discs[disc_nr])
self._disc_listbox.add(disc)
if len(discs) == 1:
disc.show_disc_label(False)
disc.props.show_disc_label = False
if remaining == 0:
self.emit("songs-loaded")
......@@ -174,4 +175,4 @@ class ArtistAlbumWidget(Gtk.Box):
return
self._selection_mode = selection_mode
self._disc_listbox.set_selection_mode(selection_mode)
self._disc_listbox.props.selection_mode = selection_mode
......@@ -53,8 +53,19 @@ class DiscSongsFlowBox(Gtk.FlowBox):
self._columns = columns
self.get_style_context().add_class('discsongsflowbox')
@GObject.Property(type=int, minimum=1, default=1)
@log
def set_columns(self, columns):
def columns(self):
"""Number of columns for the song list
:returns: The number of columns
:rtype: int
"""
return self._columns
@columns.setter
@log
def columns(self, columns):
"""Set the number of columns to use
:param int columns: The number of columns the widget uses
......@@ -86,6 +97,13 @@ class DiscBox(Gtk.Box):
'song-activated': (GObject.SignalFlags.RUN_FIRST, None, (Gtk.Widget,))
}
columns = GObject.Property(type=int, minimum=1, default=1)
selection_mode = GObject.Property(type=bool, default=False)
show_disc_label = GObject.Property(type=bool, default=False)
show_durations = GObject.Property(type=bool, default=False)
show_favorites = GObject.Property(type=bool, default=False)
show_song_numbers = GObject.Property(type=bool, default=False)
def __repr__(self):
return '<DiscBox>'
......@@ -107,21 +125,19 @@ class DiscBox(Gtk.Box):
self._label.set_no_show_all(True)
self._disc_songs_flowbox = builder.get_object('discsongsflowbox')
self._selection_mode = False
self.bind_property(
'columns', self._disc_songs_flowbox, 'columns',
GObject.BindingFlags.SYNC_CREATE)
self.bind_property(
'show-disc-label', self._label, 'visible',
GObject.BindingFlags.SYNC_CREATE)
self._selection_mode_allowed = True
self._selected_items = []
self._songs = []
self.pack_start(builder.get_object('disc'), True, True, 0)
@log
def set_columns(self, columns):
"""Set the number of columns used by the songs list
:param int columns: Number of columns to display
"""
self._disc_songs_flowbox.set_columns(columns)
@log
def set_disc_number(self, disc_number):
"""Set the dics number to display
......@@ -131,49 +147,6 @@ class DiscBox(Gtk.Box):
self._label.set_label(_("Disc {}").format(disc_number))
self._label.set_visible(True)
@log
def show_disc_label(self, show_header):
"""Wheter to show the disc number label
:param bool show_header: Display the disc number label
"""
self._label.set_visible(False)
self._label.hide()
@log
def show_duration(self, show_duration):
"""Wheter to show the song durations
:param bool show_duration: Display the song durations
"""
def child_show_duration(child):
child.get_child().show_duration = show_duration
self._disc_songs_flowbox.foreach(child_show_duration)
@log
def show_favorites(self, show_favorites):
"""Where to show the favorite switches
:param bool show_favorites: Display the favorite
switches
"""
def child_show_favorites(child):
child.get_child().show_favorite = show_favorites
self._disc_songs_flowbox.foreach(child_show_favorites)
@log
def show_song_numbers(self, show_song_number):
"""Whether to show the song numbers
:param bool show_song_number: Display the song number
"""
def child_show_song_number(child):
child.get_child().show_song_number = show_song_number
self._disc_songs_flowbox.foreach(child_show_song_number)
@log
def set_songs(self, songs):
"""Songs to display
......@@ -186,15 +159,6 @@ class DiscBox(Gtk.Box):
self._disc_songs_flowbox.insert(song_widget, -1)
song.song_widget = song_widget
@log
def set_selection_mode(self, selection_mode):
"""Set selection mode
:param bool selection_mode: Allow selection mode
"""
self._selection_mode = selection_mode
self._disc_songs_flowbox.foreach(self._toggle_widget_selection)
@log
def get_selected_items(self):
"""Return all selected items
......@@ -258,6 +222,19 @@ class DiscBox(Gtk.Box):
song_widget.connect('button-release-event', self._song_activated)
song_widget.connect('selection-changed', self._on_selection_changed)
self.bind_property(
'selection-mode', song_widget, 'selection-mode',
GObject.BindingFlags.SYNC_CREATE)
self.bind_property(
'show-durations', song_widget, 'show-duration',
GObject.BindingFlags.SYNC_CREATE)
self.bind_property(
'show-favorites', song_widget, 'show-favorite',
GObject.BindingFlags.SYNC_CREATE)
self.bind_property(
'show-song-numbers', song_widget, 'show-song-number',
GObject.BindingFlags.SYNC_CREATE)
return song_widget
@log
......@@ -269,7 +246,7 @@ class DiscBox(Gtk.Box):
@log
def _toggle_widget_selection(self, child):
song_widget = child.get_child()
song_widget.selection_mode = self._selection_mode
song_widget.props.selection_mode = self.props.selection_mode
@log
def _song_activated(self, widget, event):
......@@ -279,12 +256,12 @@ class DiscBox(Gtk.Box):
or (event.button == Gdk.BUTTON_PRIMARY
and event.state & Gdk.ModifierType.CONTROL_MASK)):
self.emit('song-activated', widget)
if self._selection_mode:
if self.props.selection_mode:
itr = widget.itr
self._model[itr][6] = not self._model[itr][6]
else:
self.emit('selection-toggle')
if self._selection_mode:
if self.props.selection_mode:
itr = widget.itr
self._model[itr][6] = True
......@@ -292,14 +269,14 @@ class DiscBox(Gtk.Box):
@log
def _model_row_changed(self, model, path, itr):
if (not self._selection_mode
if (not self.props.selection_mode
or not model[itr][5]):
return
song_widget = model[itr][5].song_widget
selected = model[itr][6]
if selected != song_widget.selected:
song_widget.selected = selected
if selected != song_widget.props.selected:
song_widget.props.selected = selected
return True
......@@ -332,6 +309,10 @@ class DiscListBox(Gtk.Box):
super().add(widget)
widget.connect('selection-changed', self._on_selection_changed)
self.bind_property(
'selection-mode', widget, 'selection-mode',
GObject.BindingFlags.SYNC_CREATE)
@log
def _on_selection_changed(self, widget):
self.emit('selection-changed')
......@@ -368,21 +349,27 @@ class DiscListBox(Gtk.Box):
self.foreach(child_select_none)
@GObject.Property(type=bool, default=False)
@log
def set_selection_mode(self, selection_mode):
"""Set selection mode
def selection_mode(self):
"""selection mode getter
:param bool selection_mode: Allow selection mode
:returns: If selection mode is active
:rtype: bool
"""
if not self._selection_mode_allowed:
return
return self._selection_mode
self._selection_mode = selection_mode
@selection_mode.setter
@log
def selection_mode(self, value):
"""selection-mode setter
def set_child_selection_mode(child):
child.set_selection_mode(self._selection_mode)
:param bool value: Activate selection mode
"""
if not self._selection_mode_allowed:
return
self.foreach(set_child_selection_mode)
self._selection_mode = value
@log
def set_selection_mode_allowed(self, allowed):
......
......@@ -94,7 +94,7 @@ class SongWidget(Gtk.EventBox):
time = utils.seconds_to_string(media.get_duration())
self._duration_label.set_text(time)
self._star_image.favorite = media.get_favourite()
self._star_image.props.favorite = media.get_favourite()
self._select_button.set_visible(False)
......@@ -131,7 +131,7 @@ class SongWidget(Gtk.EventBox):
return False
favorite = not self._star_image.favorite
self._star_image.favorite = favorite
self._star_image.props.favorite = favorite
# TODO: Rework and stop updating widgets from here directly.
grilo.set_favorite(self._media, favorite)
......@@ -142,12 +142,12 @@ class SongWidget(Gtk.EventBox):
@Gtk.Template.Callback()
@log
def _on_star_hover(self, widget, event):
self._star_image.hover = True
self._star_image.props.hover = True
@Gtk.Template.Callback()
@log
def _on_star_unhover(self, widget, event):
self._star_image.hover = False
self._star_image.props.hover = False
@GObject.Property(type=bool, default=False)
@log
......@@ -170,7 +170,7 @@ class SongWidget(Gtk.EventBox):
self._select_button.set_visible(value)
if not value:
self.selected = False
self.props.selected = False
@GObject.Property
@log
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment