[gnome-music/wip/jfelder/playlistdialog-gtk-template: 5/5] playlistdialog: Use Gtk Template



commit 6b52ef337f1e625c6bd5ccc38f0f20dffa57444e
Author: Jean Felder <jfelder src gnome org>
Date:   Thu May 17 11:59:20 2018 +0200

    playlistdialog: Use Gtk Template
    
    Change TreeView selection activation to single click and remove some
    spurious code.

 data/PlaylistDialog.ui               |  46 ++++++++----
 gnomemusic/widgets/playlistdialog.py | 137 ++++++++++++++---------------------
 2 files changed, 86 insertions(+), 97 deletions(-)
---
diff --git a/data/PlaylistDialog.ui b/data/PlaylistDialog.ui
index 9e802d56..d31c073a 100644
--- a/data/PlaylistDialog.ui
+++ b/data/PlaylistDialog.ui
@@ -2,7 +2,7 @@
 <!-- Generated with glade 3.20.0 -->
 <interface>
   <requires lib="gtk+" version="3.10"/>
-  <object class="GtkDialog" id="dialog">
+  <template class="PlaylistDialog" parent="GtkDialog">
     <property name="width_request">400</property>
     <property name="height_request">500</property>
     <property name="can_focus">False</property>
@@ -24,14 +24,14 @@
           </packing>
         </child>
         <child>
-          <object class="GtkStack" id="add_playlist_stack">
+          <object class="GtkStack" id="_add_playlist_stack">
             <property name="visible">True</property>
             <property name="can_focus">False</property>
             <property name="hexpand">True</property>
             <property name="vexpand">True</property>
             <property name="transition_duration">250</property>
             <child>
-              <object class="GtkBox" id="empty_state">
+              <object class="GtkBox" id="_empty_box">
                 <property name="visible">False</property>
                 <property name="can_focus">False</property>
                 <property name="orientation">vertical</property>
@@ -74,13 +74,16 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkEntry" id="first-playlist-entry">
+                      <object class="GtkEntry" id="_first_playlist_entry">
                         <property name="width_request">300</property>
                         <property name="height_request">10</property>
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
                         <property name="halign">center</property>
                         <property name="margin_bottom">16</property>
+                        <signal name="activate" handler="_on_editing_done" swapped="no"/>
+                        <signal name="changed" handler="_on_add_playlist_entry_changed" swapped="no"/>
+                        <signal name="focus-in-event" handler="_on_add_playlist_entry_focused" swapped="no"/>
                       </object>
                       <packing>
                         <property name="expand">False</property>
@@ -90,7 +93,7 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkButton" id="create-first-playlist-button">
+                      <object class="GtkButton" id="_first_playlist_button">
                         <property name="label" translatable="yes">C_reate</property>
                         <property name="use_underline">True</property>
                         <property name="width_request">120</property>
@@ -103,6 +106,7 @@
                         <property name="valign">center</property>
                         <property name="margin_top">10</property>
                         <property name="margin_bottom">20</property>
+                        <signal name="clicked" handler="_on_editing_done" swapped="no"/>
                       </object>
                       <packing>
                         <property name="expand">False</property>
@@ -131,7 +135,7 @@
               </object>
             </child>
             <child>
-              <object class="GtkBox" id="normal_state">
+              <object class="GtkBox" id="_normal_box">
                 <property name="visible">False</property>
                 <property name="can_focus">False</property>
                 <property name="orientation">vertical</property>
@@ -157,11 +161,14 @@
                         <property name="can_focus">False</property>
                         <property name="border_width">6</property>
                         <child>
-                          <object class="GtkEntry" id="new-playlist-entry">
+                          <object class="GtkEntry" id="_new_playlist_entry">
                             <property name="visible">True</property>
                             <property name="can_focus">True</property>
                             <property name="hexpand">True</property>
                             <property name="placeholder_text" translatable="yes">New Playlist…</property>
+                            <signal name="activate" handler="_on_editing_done" swapped="no"/>
+                            <signal name="changed" handler="_on_add_playlist_entry_changed" swapped="no"/>
+                            <signal name="focus-in-event" handler="_on_add_playlist_entry_focused" 
swapped="no"/>
                             <style>
                               <class name="linked"/>
                             </style>
@@ -173,12 +180,13 @@
                           </packing>
                         </child>
                         <child>
-                          <object class="GtkButton" id="new-playlist-button">
+                          <object class="GtkButton" id="_new_playlist_button">
                             <property name="label" translatable="yes">Add</property>
                             <property name="visible">True</property>
                             <property name="sensitive">False</property>
                             <property name="can_focus">True</property>
                             <property name="receives_default">False</property>
+                            <signal name="clicked" handler="_on_editing_done" swapped="no"/>
                             <style>
                               <class name="suggested-action"/>
                               <class name="linked"/>
@@ -214,15 +222,18 @@
                     <property name="can_focus">True</property>
                     <property name="vexpand">True</property>
                     <child>
-                      <object class="GtkTreeView" id="treeview">
+                      <object class="GtkTreeView" id="_view">
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
-                        <property name="model">liststore</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="treeview-selection"/>
+                          <object class="GtkTreeSelection" id="_selection">
+                            <signal name="changed" handler="_on_selection_changed" swapped="no"/>
+                          </object>
                         </child>
                         <style>
                           <class name="list-row"/>
@@ -261,30 +272,33 @@
         </child>
       </object>
     </child>
-  </object>
-  <object class="GtkHeaderBar" id="headerbar">
+  </template>
+  <object class="GtkHeaderBar" id="_title_bar">
     <property name="visible">True</property>
     <property name="can_focus">False</property>
     <property name="title" translatable="yes">Add to Playlist</property>
     <child>
-      <object class="GtkButton" id="cancel-button">
+      <object class="GtkButton" id="_cancel_button">
         <property name="label" translatable="yes">_Cancel</property>
         <property name="use_underline">True</property>
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="receives_default">False</property>
+        <signal name="clicked" handler="_on_cancel_button_clicked" swapped="no"/>
         <style>
           <class name="text-button"/>
         </style>
       </object>
     </child>
     <child>
-      <object class="GtkButton" id="select-button">
+      <object class="GtkButton" id="_select_button">
         <property name="label" translatable="yes">_Add</property>
+        <property name="sensitive">False</property>
         <property name="use_underline">True</property>
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="receives_default">False</property>
+        <signal name="clicked" handler="_on_selection" swapped="no"/>
         <style>
           <class name="suggested-action"/>
           <class name="text-button"/>
@@ -296,7 +310,7 @@
       </packing>
     </child>
   </object>
-  <object class="GtkListStore" id="liststore">
+  <object class="GtkListStore" id="_model">
     <columns>
       <!-- column-name playlist-name -->
       <column type="gchararray"/>
diff --git a/gnomemusic/widgets/playlistdialog.py b/gnomemusic/widgets/playlistdialog.py
index 944f2940..be7f93a6 100644
--- a/gnomemusic/widgets/playlistdialog.py
+++ b/gnomemusic/widgets/playlistdialog.py
@@ -30,59 +30,46 @@ from gnomemusic.playlists import Playlists
 import gnomemusic.utils as utils
 
 
-class PlaylistDialog():
+@Gtk.Template(resource_path="/org/gnome/Music/PlaylistDialog.ui")
+class PlaylistDialog(Gtk.Dialog):
     """Dialog for adding items to a playlist"""
 
+    __gtype_name__ = 'PlaylistDialog'
+
+    _add_playlist_stack = Gtk.Template.Child()
+    _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()
+    _cancel_button = Gtk.Template.Child()
+    _select_button = Gtk.Template.Child()
+    _new_playlist_button = Gtk.Template.Child()
+    _new_playlist_entry = Gtk.Template.Child()
+    _first_playlist_button = Gtk.Template.Child()
+    _first_playlist_entry = Gtk.Template.Child()
+
     def __repr__(self):
         return '<PlaylistDialog>'
 
     @log
     def __init__(self, parent, playlists_todelete):
-        self._ui = Gtk.Builder()
-        self._ui.add_from_resource('/org/gnome/Music/PlaylistDialog.ui')
+        super().__init__()
 
-        self._dialog_box = self._ui.get_object('dialog')
-        self._dialog_box.set_transient_for(parent)
+        self._add_playlist_button = None
+        self._add_playlist_entry = None
 
-        self._add_playlist_stack = self._ui.get_object('add_playlist_stack')
-        self._normal_state = self._ui.get_object('normal_state')
-        self._empty_state = self._ui.get_object('empty_state')
-        self._title_bar = self._ui.get_object('headerbar')
-        self._dialog_box.set_titlebar(self._title_bar)
-        self._setup_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()
 
-        self._playlist = Playlists.get_default()
         self._user_playlists_available = False
-
-    @log
-    def run(self):
-        """Run the playlist dialog"""
-        return self._dialog_box.run()
-
-    @log
-    def destroy(self):
-        """Destroy the playlist dialog"""
-        return self._dialog_box.destroy()
-
-    @log
-    def _setup_dialog(self):
-        self._view = self._ui.get_object('treeview')
-        self._view.set_activate_on_single_click(False)
-        self._selection = self._ui.get_object('treeview-selection')
-        self._selection.connect('changed', self._on_selection_changed)
-        self._add_list_renderers()
-        self._view.connect('row-activated', self._on_item_activated)
-
-        self._model = self._ui.get_object('liststore')
-        self._populate()
-
-        self._cancel_button = self._ui.get_object('cancel-button')
-        self._select_button = self._ui.get_object('select-button')
-        self._select_button.set_sensitive(False)
-        self._cancel_button.connect('clicked', self._on_cancel_button_clicked)
-        self._select_button.connect('clicked', self._on_selection)
+        self._playlist = Playlists.get_default()
+        self._playlist.connect('playlist-created', self._on_playlist_created)
 
     @log
     def get_selected(self):
@@ -105,30 +92,15 @@ class PlaylistDialog():
     @log
     def _set_view(self):
         if self._user_playlists_available:
-            self._normal_state.show()
-            self._add_playlist_stack.set_visible_child(self._normal_state)
-            self._new_playlist_button = self._ui.get_object(
-                'new-playlist-button')
-            self._new_playlist_entry = self._ui.get_object(
-                'new-playlist-entry')
+            self._normal_box.show()
+            self._add_playlist_stack.props.visible_child = self._normal_box
+            self._add_playlist_button = self._new_playlist_button
+            self._add_playlist_entry = self._new_playlist_entry
         else:
-            self._empty_state.show()
-            self._add_playlist_stack.set_visible_child(self._empty_state)
-            self._new_playlist_button = self._ui.get_object(
-                'create-first-playlist-button')
-            self._new_playlist_entry = self._ui.get_object(
-                'first-playlist-entry')
-
-        self._new_playlist_button.set_sensitive(False)
-        self._new_playlist_button.connect('clicked', self._on_editing_done)
-
-        self._new_playlist_entry.connect(
-            'changed', self._on_new_playlist_entry_changed)
-        self._new_playlist_entry.connect('activate', self._on_editing_done)
-        self._new_playlist_entry.connect(
-            'focus-in-event', self._on_new_playlist_entry_focused)
-
-        self._playlist.connect('playlist-created', self._on_playlist_created)
+            self._empty_box.show()
+            self._add_playlist_stack.props.visible_child = self._empty_box
+            self._add_playlist_button = self._first_playlist_button
+            self._add_playlist_entry = self._first_playlist_entry
 
     @log
     def _populate(self):
@@ -155,33 +127,34 @@ class PlaylistDialog():
 
         return new_iter
 
+    @Gtk.Template.Callback()
     @log
     def _on_selection(self, select_button):
-        self._dialog_box.response(Gtk.ResponseType.ACCEPT)
+        self.response(Gtk.ResponseType.ACCEPT)
 
+    @Gtk.Template.Callback()
     @log
     def _on_cancel_button_clicked(self, cancel_button):
-        self._dialog_box.response(Gtk.ResponseType.REJECT)
+        self.response(Gtk.ResponseType.REJECT)
 
+    @Gtk.Template.Callback()
     @log
     def _on_item_activated(self, view, path, column):
-        self._new_playlist_entry.set_text("")
-        self._new_playlist_button.set_sensitive(False)
-        _iter = self._model.get_iter(path)
-        if self._model[_iter][1]:
-            self._view.set_cursor(path, column, True)
-        else:
-            self._dialog_box.response(Gtk.ResponseType.ACCEPT)
+        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.set_sensitive(_iter is not None)
+        self._select_button.props.sensitive = _iter is not None
 
+    @Gtk.Template.Callback()
     @log
     def _on_editing_done(self, sender, data=None):
-        if self._new_playlist_entry.get_text() != '':
-            self._playlist.create_playlist(self._new_playlist_entry.get_text())
+        text = self._add_playlist_entry.props.text
+        if text:
+            self._playlist.create_playlist(text)
 
     @log
     def _on_playlist_created(self, playlists, item):
@@ -191,15 +164,17 @@ class PlaylistDialog():
             path = self._model.get_path(new_iter)
             self._view.set_cursor(path, col0, False)
             self._view.row_activated(path, col0)
-            self._dialog_box.response(Gtk.ResponseType.ACCEPT)
+            self.response(Gtk.ResponseType.ACCEPT)
 
+    @Gtk.Template.Callback()
     @log
-    def _on_new_playlist_entry_changed(self, editable, data=None):
-        if editable.get_text() != '':
-            self._new_playlist_button.set_sensitive(True)
+    def _on_add_playlist_entry_changed(self, editable, data=None):
+        if editable.props.text:
+            self._add_playlist_button.props.sensitive = True
         else:
-            self._new_playlist_button.set_sensitive(False)
+            self._add_playlist_button.props.sensitive = False
 
+    @Gtk.Template.Callback()
     @log
-    def _on_new_playlist_entry_focused(self, editable, data=None):
+    def _on_add_playlist_entry_focused(self, editable, data=None):
         self._selection.unselect_all()


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