[gnome-clocks] Factor out common code between World and Alarms



commit d1983ad8e562527b735093cd503e2fed0c195958
Author: Paolo Borelli <pborelli gnome org>
Date:   Mon Aug 27 11:47:05 2012 +0200

    Factor out common code between World and Alarms
    
    Create a ContentView widget that knows how to show the empty placeholder

 gnomeclocks/clocks.py  |   64 ++++++-----------------------------------------
 gnomeclocks/widgets.py |   31 +++++++++++++++++++++++
 2 files changed, 40 insertions(+), 55 deletions(-)
---
diff --git a/gnomeclocks/clocks.py b/gnomeclocks/clocks.py
index 6ae0121..a9a67ad 100644
--- a/gnomeclocks/clocks.py
+++ b/gnomeclocks/clocks.py
@@ -20,7 +20,7 @@ from gi.repository import Gtk, GObject, Gio
 from gi.repository.GdkPixbuf import Pixbuf
 
 from widgets import NewWorldClockDialog, AlarmDialog
-from widgets import DigitalClock, AlarmWidget, SelectableIconView, EmptyPlaceholder
+from widgets import DigitalClock, AlarmWidget, SelectableIconView, ContentView
 from widgets import TogglePixbufRenderer
 from storage import worldclockstorage
 from utils import SystemSettings, Alert
@@ -67,11 +67,6 @@ class World(Clock):
     def __init__(self):
         Clock.__init__(self, _("World"), True, True)
 
-        self.empty_view = EmptyPlaceholder(
-                "document-open-recent-symbolic",
-                 _("Select <b>New</b> to add a world clock"))
-        self.add(self.empty_view)
-
         self.liststore = Gtk.ListStore(bool,
                                        Pixbuf,
                                        str,
@@ -79,11 +74,11 @@ class World(Clock):
 
         self.iconview = SelectableIconView(self.liststore, 0, 1, 2)
 
-        self.scrolledwindow = Gtk.ScrolledWindow()
-        self.scrolledwindow.add(self.iconview)
+        contentview = ContentView(self.iconview,
+                "document-open-recent-symbolic",
+                 _("Select <b>New</b> to add a world clock"))
+        self.add(contentview)
 
-        self.liststore.connect("row-inserted", self._on_item_inserted)
-        self.liststore.connect("row-deleted", self._on_item_deleted)
         self.iconview.connect("item-activated", self._on_item_activated)
         self.iconview.connect("selection-changed", self._on_selection_changed)
 
@@ -91,12 +86,6 @@ class World(Clock):
         self.load_clocks()
         self.show_all()
 
-    def _on_item_inserted(self, model, path, treeiter):
-        self.update_empty_view()
-
-    def _on_item_deleted(self, model, path):
-        self.update_empty_view()
-
     def _on_item_activated(self, iconview, path):
         d = self.liststore[path][3]
         self.emit("show-clock", d)
@@ -155,18 +144,6 @@ class World(Clock):
         self.load_clocks()
         self.notify("can-select")
 
-    def update_empty_view(self):
-        if len(self.liststore) == 0:
-            if self.scrolledwindow in self.get_children():
-                self.remove(self.scrolledwindow)
-                self.add(self.empty_view)
-                self.show_all()
-        else:
-            if self.empty_view in self.get_children():
-                self.remove(self.empty_view)
-                self.add(self.scrolledwindow)
-                self.show_all()
-
     def open_new_dialog(self):
         window = NewWorldClockDialog(self.get_toplevel())
         window.connect("response", self.on_dialog_response)
@@ -183,11 +160,6 @@ class Alarm(Clock):
     def __init__(self):
         Clock.__init__(self, _("Alarm"), True, True)
 
-        self.empty_view = EmptyPlaceholder(
-                "alarm-symbolic",
-                _("Select <b>New</b> to add an alarm"))
-        self.add(self.empty_view)
-
         self.liststore = Gtk.ListStore(bool,
                                        Pixbuf,
                                        str,
@@ -197,11 +169,11 @@ class Alarm(Clock):
 
         self.iconview = SelectableIconView(self.liststore, 0, 1, 2)
 
-        self.scrolledwindow = Gtk.ScrolledWindow()
-        self.scrolledwindow.add(self.iconview)
+        contentview = ContentView(self.iconview,
+                "alarm-symbolic",
+                _("Select <b>New</b> to add an alarm"))
+        self.add(contentview)
 
-        self.liststore.connect("row-inserted", self._on_item_inserted)
-        self.liststore.connect("row-deleted", self._on_item_deleted)
         self.iconview.connect("item-activated", self._on_item_activated)
         self.iconview.connect("selection-changed", self._on_selection_changed)
 
@@ -223,12 +195,6 @@ class Alarm(Clock):
         win = self.get_toplevel()
         win.show_clock(self)
 
-    def _on_item_inserted(self, model, path, treeiter):
-        self.update_empty_view()
-
-    def _on_item_deleted(self, model, path):
-        self.update_empty_view()
-
     def _on_item_activated(self, iconview, path):
         alarm = self.liststore[path][-1]
         self.open_edit_dialog(alarm.get_vevent())
@@ -262,18 +228,6 @@ class Alarm(Clock):
             alarm.new_from_vevent(vevent)
             self.add_alarm_widget(alarm)
 
-    def update_empty_view(self):
-        if len(self.liststore) == 0:
-            if self.scrolledwindow in self.get_children():
-                self.remove(self.scrolledwindow)
-                self.add(self.empty_view)
-                self.show_all()
-        else:
-            if self.empty_view in self.get_children():
-                self.remove(self.empty_view)
-                self.add(self.scrolledwindow)
-                self.show_all()
-
     def add_alarm(self, alarm):
         handler = ICSHandler()
         handler.add_vevent(alarm.get_vevent())
diff --git a/gnomeclocks/widgets.py b/gnomeclocks/widgets.py
index 08eccb9..c0ad680 100644
--- a/gnomeclocks/widgets.py
+++ b/gnomeclocks/widgets.py
@@ -705,6 +705,37 @@ class SelectableIconView(Gtk.IconView):
         return False
 
 
+class ContentView(Gtk.Box):
+    def __init__(self, iconview, icon, emptymsg):
+        Gtk.Box.__init__(self)
+        self.iconview = iconview
+        self.scrolledwindow = Gtk.ScrolledWindow()
+        self.scrolledwindow.add(self.iconview)
+        self.emptypage = EmptyPlaceholder(icon, emptymsg)
+        self.pack_start(self.emptypage, True, True, 0)
+
+        model = self.iconview.get_model()
+        model.connect("row-inserted", self._on_item_inserted)
+        model.connect("row-deleted", self._on_item_deleted)
+
+    def _on_item_inserted(self, model, path, treeiter):
+        self._update_empty_view(model)
+
+    def _on_item_deleted(self, model, path):
+        self._update_empty_view(model)
+
+    def _update_empty_view(self, model):
+        if len(model) == 0:
+            if self.scrolledwindow in self.get_children():
+                self.remove(self.scrolledwindow)
+                self.pack_start(self.emptypage, True, True, 0)
+        else:
+            if self.emptypage in self.get_children():
+                self.remove(self.emptypage)
+                self.pack_start(self.scrolledwindow, True, True, 0)
+        self.show_all()
+
+
 class SelectionToolbar():
     DEFAULT_WIDTH = 300
 



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