[gnome-music/wip/jfelder/playlistdialog-listbox: 5/7] playlistdialog: Port to GtkListBox



commit 72352024b41317467ff180ccb4f3598a9e42a838
Author: Jean Felder <jfelder src gnome org>
Date:   Wed May 15 19:17:09 2019 +0200

    playlistdialog: Port to GtkListBox
    
    Replace the treeview with a listbox. This improves code readability
    and this is a necessary step for the future core rewrite.
    A PlaylistDialogRow class is also introduced to simplify the creation
    of the listbox rows.

 data/org.gnome.Music.gresource.xml      |  1 +
 data/ui/PlaylistDialog.ui               | 22 ++-----------
 data/ui/PlaylistDialogRow.ui            | 17 ++++++++++
 gnomemusic/widgets/playlistdialog.py    | 56 +++++++++++----------------------
 gnomemusic/widgets/playlistdialogrow.py | 50 +++++++++++++++++++++++++++++
 5 files changed, 90 insertions(+), 56 deletions(-)
---
diff --git a/data/org.gnome.Music.gresource.xml b/data/org.gnome.Music.gresource.xml
index 5d7f0e50..3992ffa4 100644
--- a/data/org.gnome.Music.gresource.xml
+++ b/data/org.gnome.Music.gresource.xml
@@ -19,6 +19,7 @@
     <file preprocess="xml-stripblanks">ui/PlaylistContextMenu.ui</file>
     <file preprocess="xml-stripblanks">ui/PlaylistControls.ui</file>
     <file preprocess="xml-stripblanks">ui/PlaylistDialog.ui</file>
+    <file preprocess="xml-stripblanks">ui/PlaylistDialogRow.ui</file>
     <file preprocess="xml-stripblanks">ui/SearchBar.ui</file>
     <file preprocess="xml-stripblanks">ui/SelectionBarMenuButton.ui</file>
     <file preprocess="xml-stripblanks">ui/SelectionToolbar.ui</file>
diff --git a/data/ui/PlaylistDialog.ui b/data/ui/PlaylistDialog.ui
index 6b341b06..8f8e8034 100644
--- a/data/ui/PlaylistDialog.ui
+++ b/data/ui/PlaylistDialog.ui
@@ -201,19 +201,11 @@
                     <property name="can_focus">True</property>
                     <property name="vexpand">True</property>
                     <child>
-                      <object class="GtkTreeView" id="_view">
+                      <object class="GtkListBox" id="_listbox">
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
-                        <property name="model">_model</property>
-                        <property name="headers_visible">False</property>
-                        <property name="search_column">0</property>
-                        <property name="activate_on_single_click">True</property>
-                        <signal name="row-activated" handler="_on_item_activated" swapped="no"/>
-                        <child internal-child="selection">
-                          <object class="GtkTreeSelection" id="_selection">
-                            <signal name="changed" handler="_on_selection_changed" swapped="no"/>
-                          </object>
-                        </child>
+                        <property name="selection_mode">single</property>
+                        <signal name="row-selected" handler="_on_row_selected" swapped="no"/>
                       </object>
                     </child>
                   </object>
@@ -279,12 +271,4 @@
       </packing>
     </child>
   </object>
-  <object class="GtkListStore" id="_model">
-    <columns>
-      <!-- column-name playlist-name -->
-      <column type="gchararray"/>
-      <!-- column-name item -->
-      <column type="GObject"/>
-    </columns>
-  </object>
 </interface>
diff --git a/data/ui/PlaylistDialogRow.ui b/data/ui/PlaylistDialogRow.ui
new file mode 100644
index 00000000..66eb8cf7
--- /dev/null
+++ b/data/ui/PlaylistDialogRow.ui
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="PlaylistDialogRow" parent="GtkListBoxRow">
+    <property name="visible">True</property>
+    <style>
+      <class name="playlistdialog-row"/>
+    </style>
+    <child>
+      <object class="GtkLabel" id="_label">
+       <property name="ellipsize">end</property>
+        <property name="margin">8</property>
+        <property name="visible">True</property>
+        <property name="xalign">0.0</property>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/gnomemusic/widgets/playlistdialog.py b/gnomemusic/widgets/playlistdialog.py
index b87ca5df..15f6d4b6 100644
--- a/gnomemusic/widgets/playlistdialog.py
+++ b/gnomemusic/widgets/playlistdialog.py
@@ -22,12 +22,12 @@
 # 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 gi.repository import Gtk, Pango
+from gi.repository import Gtk
 
 from gnomemusic import log
 from gnomemusic.grilo import grilo
 from gnomemusic.playlists import Playlists
-import gnomemusic.utils as utils
+from gnomemusic.widgets.playlistdialogrow import PlaylistDialogRow
 
 
 @Gtk.Template(resource_path="/org/gnome/Music/ui/PlaylistDialog.ui")
@@ -40,9 +40,7 @@ class PlaylistDialog(Gtk.Dialog):
     _normal_box = Gtk.Template.Child()
     _empty_box = Gtk.Template.Child()
     _title_bar = Gtk.Template.Child()
-    _view = Gtk.Template.Child()
-    _selection = Gtk.Template.Child()
-    _model = Gtk.Template.Child()
+    _listbox = Gtk.Template.Child()
     _cancel_button = Gtk.Template.Child()
     _select_button = Gtk.Template.Child()
     _new_playlist_button = Gtk.Template.Child()
@@ -62,7 +60,6 @@ class PlaylistDialog(Gtk.Dialog):
 
         self.props.transient_for = parent
         self.set_titlebar(self._title_bar)
-        self._add_list_renderers()
         self._populate()
 
         self._playlists_todelete_ids = playlists_todelete.keys()
@@ -74,20 +71,12 @@ class PlaylistDialog(Gtk.Dialog):
     @log
     def get_selected(self):
         """Get the selected playlist"""
-        _iter = self._selection.get_selected()[1]
+        selected_row = self._listbox.get_selected_row()
 
-        if not _iter:
+        if not selected_row:
             return None
 
-        return self._model[_iter][1]
-
-    @log
-    def _add_list_renderers(self):
-        type_renderer = Gtk.CellRendererText(
-            xpad=8, ypad=8, ellipsize=Pango.EllipsizeMode.END, xalign=0.0)
-
-        col = Gtk.TreeViewColumn("Name", type_renderer, text=0)
-        self._view.append_column(col)
+        return selected_row.props.playlist
 
     @log
     def _set_view(self):
@@ -109,12 +98,12 @@ class PlaylistDialog(Gtk.Dialog):
     @log
     def _add_item(self, source, param, item, remaining=0, data=None):
         if item:
-            self._add_item_to_model(item)
+            self._add_playlist_to_listbox(item)
         if remaining == 0:
             self._set_view()
 
     @log
-    def _add_item_to_model(self, item):
+    def _add_playlist_to_listbox(self, item):
         """Adds (non-smart only) playlists to the model"""
 
         # Hide playlists that are going to be deleted
@@ -122,10 +111,10 @@ class PlaylistDialog(Gtk.Dialog):
             return None
 
         self._user_playlists_available = True
-        new_iter = self._model.insert_with_valuesv(
-            -1, [0, 1], [utils.get_media_title(item), item])
+        row = PlaylistDialogRow(item)
+        self._listbox.insert(row, -1)
 
-        return new_iter
+        return row
 
     @Gtk.Template.Callback()
     @log
@@ -139,15 +128,10 @@ class PlaylistDialog(Gtk.Dialog):
 
     @Gtk.Template.Callback()
     @log
-    def _on_item_activated(self, view, path, column):
+    def _on_row_selected(self, listbox, row):
         self._add_playlist_entry.props.text = ""
         self._add_playlist_button.props.sensitive = False
-
-    @Gtk.Template.Callback()
-    @log
-    def _on_selection_changed(self, selection):
-        model, _iter = self._selection.get_selected()
-        self._select_button.props.sensitive = _iter is not None
+        self._select_button.props.sensitive = row is not None
 
     @Gtk.Template.Callback()
     @log
@@ -158,13 +142,11 @@ class PlaylistDialog(Gtk.Dialog):
 
     @log
     def _on_playlist_created(self, playlists, item):
-        new_iter = self._add_item_to_model(item)
-        if new_iter and self._view.get_columns():
-            col0 = self._view.get_columns()[0]
-            path = self._model.get_path(new_iter)
-            self._view.set_cursor(path, col0, False)
-            self._view.row_activated(path, col0)
-            self.response(Gtk.ResponseType.ACCEPT)
+        row = self._add_playlist_to_listbox(item)
+        if not row:
+            return
+        self._listbox.select_row(row)
+        self.response(Gtk.ResponseType.ACCEPT)
 
     @Gtk.Template.Callback()
     @log
@@ -177,4 +159,4 @@ class PlaylistDialog(Gtk.Dialog):
     @Gtk.Template.Callback()
     @log
     def _on_add_playlist_entry_focused(self, editable, data=None):
-        self._selection.unselect_all()
+        self._listbox.unselect_all()
diff --git a/gnomemusic/widgets/playlistdialogrow.py b/gnomemusic/widgets/playlistdialogrow.py
new file mode 100644
index 00000000..c6df26a8
--- /dev/null
+++ b/gnomemusic/widgets/playlistdialogrow.py
@@ -0,0 +1,50 @@
+# Copyright 2019 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 gi.repository import GObject, Grl, Gtk
+
+import gnomemusic.utils as utils
+
+
+@Gtk.Template(resource_path="/org/gnome/Music/ui/PlaylistDialogRow.ui")
+class PlaylistDialogRow(Gtk.ListBoxRow):
+
+    __gtype_name__ = "PlaylistDialogRow"
+
+    playlist = GObject.Property(type=Grl.Media, default=None)
+
+    _label = Gtk.Template.Child()
+
+    def __repr__(self):
+        return "PlaylistDialogRow"
+
+    def __init__(self, playlist):
+        """Create a row of the PlaylistDialog
+
+        :param Grl.Media playlist: the associated playlist
+        """
+        super().__init__()
+
+        self.props.playlist = playlist
+        self._label.props.label = utils.get_media_title(playlist)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]