[gnome-clocks] Have a single standalone instance



commit 4898e481d18b236b1ed67a6cd157ce0d35f0d5e2
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Nov 18 16:14:24 2012 +0100

    Have a single standalone instance
    
    World and Alarm now have just one "standalone" each and the standalone
    content is set dynamically instead of having one standalone for each
    clock/alarm

 gnomeclocks/alarm.py  |   55 +++++++++++++++++++++++-------------------------
 gnomeclocks/app.py    |    7 ++---
 gnomeclocks/clocks.py |    4 +-
 gnomeclocks/world.py  |   47 ++++++++++++++++++++---------------------
 4 files changed, 54 insertions(+), 59 deletions(-)
---
diff --git a/gnomeclocks/alarm.py b/gnomeclocks/alarm.py
index 1435708..20f4038 100644
--- a/gnomeclocks/alarm.py
+++ b/gnomeclocks/alarm.py
@@ -297,8 +297,7 @@ class AlarmDialog(Gtk.Dialog):
 
 
 class AlarmThumbnail():
-    def __init__(self, view, alarm):
-        self.view = view
+    def __init__(self, alarm):
         self.alarm = alarm
         timestr = alarm.get_time_as_string()
         repeat = alarm.get_alarm_repeat_string()
@@ -309,7 +308,6 @@ class AlarmThumbnail():
         else:
             img = os.path.join(Dirs.get_image_dir(), "cities", "night.png")
         self.drawing.render(timestr, img, is_light, repeat)
-        self.standalone = None
 
     def get_alarm(self):
         return self.alarm
@@ -317,19 +315,13 @@ class AlarmThumbnail():
     def get_pixbuf(self):
         return self.drawing.pixbuf
 
-    def get_standalone_widget(self):
-        if not self.standalone:
-            self.standalone = StandaloneAlarm(self.view, self.alarm)
-        return self.standalone
-
 
-class StandaloneAlarm(Gtk.EventBox):
-    def __init__(self, view, alarm):
+class AlarmStandalone(Gtk.EventBox):
+    def __init__(self, view):
         Gtk.EventBox.__init__(self)
         self.get_style_context().add_class('view')
         self.get_style_context().add_class('content-view')
         self.view = view
-        self.alarm = alarm
         self.can_edit = True
 
         self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
@@ -381,10 +373,14 @@ class StandaloneAlarm(Gtk.EventBox):
         self.vbox.pack_start(hbox, False, False, 0)
         self.vbox.pack_start(Gtk.Label(), True, True, 0)
 
-        self.update()
+        self.set_alarm(None)
 
-        self.show_all()
-        self.set_ringing(False)
+    def set_alarm(self, alarm, ringing=False):
+        self.alarm = alarm
+        if alarm:
+            self.update()
+            self.show_all()
+            self.buttons.set_visible(ringing)
 
     def _on_stop_clicked(self, button):
         self.alarm.stop()
@@ -396,16 +392,14 @@ class StandaloneAlarm(Gtk.EventBox):
         name = self.alarm.name
         return GLib.markup_escape_text(name)
 
-    def set_ringing(self, show):
-        self.buttons.set_visible(show)
-
     def update(self):
-        timestr = self.alarm.get_time_as_string()
-        repeat = self.alarm.get_alarm_repeat_string()
-        self.alarm_label.set_markup(
-            "<span size='72000' color='dimgray'><b>%s</b></span>" % timestr)
-        self.repeat_label.set_markup(
-            "<span size='large' color='dimgray'><b>%s</b></span>" % repeat)
+        if self.alarm:
+            timestr = self.alarm.get_time_as_string()
+            repeat = self.alarm.get_alarm_repeat_string()
+            self.alarm_label.set_markup(
+                "<span size='72000' color='dimgray'><b>%s</b></span>" % timestr)
+            self.repeat_label.set_markup(
+                "<span size='large' color='dimgray'><b>%s</b></span>" % repeat)
 
     def open_edit_dialog(self):
         window = AlarmDialog(self.get_toplevel(), self.alarm)
@@ -445,20 +439,23 @@ class Alarm(Clock):
         self.load_alarms()
         self.show_all()
 
+        self.standalone = AlarmStandalone(self)
+
         self.timeout_id = GLib.timeout_add(1000, self._check_alarms)
 
     def _check_alarms(self):
         for i in self.liststore:
             thumb = self.liststore.get_value(i.iter, 3)
-            if thumb.get_alarm().check_expired():
-                standalone = thumb.get_standalone_widget()
-                standalone.set_ringing(True)
-                self.emit("show-standalone", thumb)
+            alarm = thumb.get_alarm()
+            if alarm.check_expired():
+                self.standalone.set_alarm(alarm, True)
+                self.emit("show-standalone")
         return True
 
     def _on_item_activated(self, iconview, path):
         thumb = self.liststore[path][3]
-        self.emit("show-standalone", thumb)
+        self.standalone.set_alarm(thumb.get_alarm())
+        self.emit("show-standalone")
 
     def _on_selection_changed(self, iconview):
         self.emit("selection-changed")
@@ -490,7 +487,7 @@ class Alarm(Clock):
         self.show_all()
 
     def add_alarm_thumbnail(self, alarm):
-        thumb = AlarmThumbnail(self, alarm)
+        thumb = AlarmThumbnail(alarm)
         label = GLib.markup_escape_text(alarm.name)
         view_iter = self.liststore.append([False,
                                            thumb.get_pixbuf(),
diff --git a/gnomeclocks/app.py b/gnomeclocks/app.py
index cabb29d..787009a 100644
--- a/gnomeclocks/app.py
+++ b/gnomeclocks/app.py
@@ -90,11 +90,10 @@ class Window(Gtk.ApplicationWindow):
     def show_clock(self, view):
         self.toolbar.activate_view(view)
 
-    def _on_show_standalone(self, widget, d):
+    def _on_show_standalone(self, view):
         def show_standalone_page():
-            widget = d.get_standalone_widget()
-            self.toolbar.show_standalone_toolbar(widget)
-            self.single_evbox.add(widget)
+            self.toolbar.show_standalone_toolbar(view.standalone)
+            self.single_evbox.add(view.standalone)
             self.notebook.set_current_page(-1)
 
         if self.notebook.get_current_page() != len(self.views):
diff --git a/gnomeclocks/clocks.py b/gnomeclocks/clocks.py
index b503713..8bc861b 100644
--- a/gnomeclocks/clocks.py
+++ b/gnomeclocks/clocks.py
@@ -36,8 +36,8 @@ class Clock(Gtk.EventBox):
         self.get_style_context().add_class('view')
         self.get_style_context().add_class('content-view')
 
-    @GObject.Signal(arg_types=(object,))
-    def show_standalone(self, arg):
+    @GObject.Signal
+    def show_standalone(self):
         pass
 
     @GObject.Signal
diff --git a/gnomeclocks/world.py b/gnomeclocks/world.py
index ecb60d8..0d52852 100644
--- a/gnomeclocks/world.py
+++ b/gnomeclocks/world.py
@@ -212,7 +212,6 @@ class ClockThumbnail():
     def __init__(self, clock):
         self.clock = clock
         self.drawing = DigitalClockDrawing()
-        self.standalone = None
         self.update()
 
     def update(self):
@@ -227,8 +226,6 @@ class ClockThumbnail():
             self.drawing.render(timestr, img, is_light)
         else:
             self.drawing.render(timestr, img, is_light, day)
-        if self.standalone:
-            self.standalone.update()
 
     def get_clock(self):
         return self.clock
@@ -236,23 +233,15 @@ class ClockThumbnail():
     def get_pixbuf(self):
         return self.drawing.pixbuf
 
-    def get_standalone_widget(self):
-        if not self.standalone:
-            self.standalone = StandaloneClock(self.clock)
-        return self.standalone
 
-
-class StandaloneClock(Gtk.EventBox):
-    def __init__(self, clock):
+class ClockStandalone(Gtk.EventBox):
+    def __init__(self):
         Gtk.EventBox.__init__(self)
         self.get_style_context().add_class('view')
         self.get_style_context().add_class('content-view')
-        self.clock = clock
         self.can_edit = False
         self.time_label = Gtk.Label()
 
-        self.clock_format = None
-
         self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
         self.add(self.vbox)
 
@@ -303,21 +292,27 @@ class StandaloneClock(Gtk.EventBox):
         hbox.pack_start(Gtk.Label(), True, True, 0)
         self.vbox.pack_end(hbox, False, False, 30)
 
-        self.update()
-        self.show_all()
+        self.set_clock(None)
+
+    def set_clock(self, clock):
+        self.clock = clock
+        if clock:
+            self.update()
+            self.show_all()
 
     def get_name(self):
         return GLib.markup_escape_text(self.clock.location.get_city_name())
 
     def update(self):
-        timestr = self.clock.get_time_as_string()
-        sunrisestr, sunsetstr = self.clock.get_sunrise_sunset_as_strings()
-        self.time_label.set_markup(
-            "<span size='72000' color='dimgray'><b>%s</b></span>" % timestr)
-        self.sunrise_time_label.set_markup(
-            "<span size ='large'>%s</span>" % sunrisestr)
-        self.sunset_time_label.set_markup(
-            "<span size ='large'>%s</span>" % sunsetstr)
+        if self.clock:
+            timestr = self.clock.get_time_as_string()
+            sunrisestr, sunsetstr = self.clock.get_sunrise_sunset_as_strings()
+            self.time_label.set_markup(
+                "<span size='72000' color='dimgray'><b>%s</b></span>" % timestr)
+            self.sunrise_time_label.set_markup(
+                "<span size ='large'>%s</span>" % sunrisestr)
+            self.sunset_time_label.set_markup(
+                "<span size ='large'>%s</span>" % sunsetstr)
 
 
 class World(Clock):
@@ -345,17 +340,21 @@ class World(Clock):
         self.load_clocks()
         self.show_all()
 
+        self.standalone = ClockStandalone()
+
         self.timeout_id = GLib.timeout_add(1000, self._update_clocks)
 
     def _update_clocks(self):
         for i in self.liststore:
             thumb = self.liststore.get_value(i.iter, 3)
             thumb.update()
+        self.standalone.update()
         return True
 
     def _on_item_activated(self, iconview, path):
         thumb = self.liststore[path][3]
-        self.emit("show-standalone", thumb)
+        self.standalone.set_clock(thumb.get_clock())
+        self.emit("show-standalone")
 
     def _on_selection_changed(self, iconview):
         self.emit("selection-changed")



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