[gnome-clocks] Use custom cell rederer for alarms and clocks



commit b2f59de9aaaba3d1e15b9e4886d96de036797b97
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Nov 25 15:45:40 2012 +0100

    Use custom cell rederer for alarms and clocks
    
    Use a custom cell renderer instead of using a fake widget to create a
    pixbuf and then using the standard pixbuf cell renderer.
    This also cleans up a few visible things: the two day and night images are
    now of the correct size and we avoid loading them over and over, the
    rounded rectangle over the night image is lighter and the
    selection cellrender has a bit of an offset also vertically.

 data/cities/day.png    |  Bin 271188 -> 34012 bytes
 data/cities/night.png  |  Bin 10746 -> 5169 bytes
 gnomeclocks/alarm.py   |   79 ++++++++----------
 gnomeclocks/widgets.py |  221 +++++++++++++++++++++---------------------------
 gnomeclocks/world.py   |   97 ++++++++-------------
 5 files changed, 169 insertions(+), 228 deletions(-)
---
diff --git a/data/cities/day.png b/data/cities/day.png
index ce13b3e..dbbdd94 100644
Binary files a/data/cities/day.png and b/data/cities/day.png differ
diff --git a/data/cities/night.png b/data/cities/night.png
index 6a74717..5215c5f 100644
Binary files a/data/cities/night.png and b/data/cities/night.png differ
diff --git a/gnomeclocks/alarm.py b/gnomeclocks/alarm.py
index 156e5b0..0e9526c 100644
--- a/gnomeclocks/alarm.py
+++ b/gnomeclocks/alarm.py
@@ -21,13 +21,13 @@ import errno
 import time
 import json
 from datetime import datetime, timedelta
-from gi.repository import GLib, GObject, Gtk, GdkPixbuf
+from gi.repository import GLib, GObject, Gdk, GdkPixbuf, Gtk
 from clocks import Clock
 from utils import Dirs, SystemSettings, LocalizedWeekdays, Alert
-from widgets import DigitalClockDrawing, SelectableIconView, ContentView
+from widgets import SelectableIconView, ContentView
 
 
-class AlarmsStorage():
+class AlarmsStorage:
     def __init__(self):
         self.filename = os.path.join(Dirs.get_user_data_dir(), "alarms.json")
 
@@ -286,26 +286,6 @@ class AlarmDialog(Gtk.Dialog):
         return alarm
 
 
-class AlarmThumbnail():
-    def __init__(self, alarm):
-        self.alarm = alarm
-        timestr = alarm.get_time_as_string()
-        repeat = alarm.get_alarm_repeat_string()
-        self.drawing = DigitalClockDrawing()
-        is_light = self.alarm.get_is_light()
-        if is_light:
-            img = os.path.join(Dirs.get_image_dir(), "cities", "day.png")
-        else:
-            img = os.path.join(Dirs.get_image_dir(), "cities", "night.png")
-        self.drawing.render(timestr, img, is_light, repeat)
-
-    def get_alarm(self):
-        return self.alarm
-
-    def get_pixbuf(self):
-        return self.drawing.pixbuf
-
-
 class AlarmStandalone(Gtk.EventBox):
     def __init__(self, view):
         Gtk.EventBox.__init__(self)
@@ -414,21 +394,21 @@ class Alarm(Clock):
         self.notebook.set_show_border(False)
         self.add(self.notebook)
 
-        self.liststore = Gtk.ListStore(bool,
-                                       GdkPixbuf.Pixbuf,
-                                       str,
-                                       GObject.TYPE_PYOBJECT)
+        f = os.path.join(Dirs.get_image_dir(), "cities", "day.png")
+        self.daypixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(f, 160, 160)
+        f = os.path.join(Dirs.get_image_dir(), "cities", "night.png")
+        self.nightpixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(f, 160, 160)
 
-        self.iconview = SelectableIconView(self.liststore, 0, 1, 2)
+        self.liststore = Gtk.ListStore(bool, str, object)
+        self.iconview = SelectableIconView(self.liststore, 0, 1, self._thumb_data_func)
+        self.iconview.connect("item-activated", self._on_item_activated)
+        self.iconview.connect("selection-changed", self._on_selection_changed)
 
         contentview = ContentView(self.iconview,
                                   "alarm-symbolic",
                                   _("Select <b>New</b> to add an alarm"))
         self.notebook.append_page(contentview, None)
 
-        self.iconview.connect("item-activated", self._on_item_activated)
-        self.iconview.connect("selection-changed", self._on_selection_changed)
-
         self.storage = AlarmsStorage()
 
         self.load_alarms()
@@ -439,6 +419,19 @@ class Alarm(Clock):
 
         self.timeout_id = GLib.timeout_add(1000, self._check_alarms)
 
+    def _thumb_data_func(self, view, cell, store, i, data):
+        alarm = store.get_value(i, 2)
+        cell.text = alarm.get_time_as_string()
+        cell.subtext = alarm.get_alarm_repeat_string()
+        if alarm.get_is_light():
+            cell.props.pixbuf = self.daypixbuf
+            cell.foreground = Gdk.RGBA(0.0, 0.0, 0.0, 1.0)
+            cell.background = Gdk.RGBA(1.0, 1.0, 1.0, 0.7)
+        else:
+            cell.props.pixbuf = self.nightpixbuf
+            cell.foreground = Gdk.RGBA(1.0, 1.0, 1.0, 1.0)
+            cell.background = Gdk.RGBA(1.0, 1.0, 1.0, 0.3)
+
     def set_mode(self, mode):
         self.mode = mode
         if mode is Clock.Mode.NORMAL:
@@ -455,17 +448,15 @@ class Alarm(Clock):
         self.set_mode(Clock.Mode.STANDALONE)
 
     def _check_alarms(self):
-        for i in self.liststore:
-            thumb = self.liststore.get_value(i.iter, 3)
-            alarm = thumb.get_alarm()
-            if alarm.check_expired():
-                self.standalone.set_alarm(alarm, True)
+        for a in self.alarms:
+            if a.check_expired():
+                self.standalone.set_alarm(a, True)
                 self.emit("alarm-ringing")
         return True
 
     def _on_item_activated(self, iconview, path):
-        thumb = self.liststore[path][3]
-        self.standalone.set_alarm(thumb.get_alarm())
+        alarm = self.liststore[path][2]
+        self.standalone.set_alarm(alarm)
         self.emit("item-activated")
 
     def _on_selection_changed(self, iconview):
@@ -480,28 +471,26 @@ class Alarm(Clock):
 
     def delete_selected(self):
         selection = self.get_selection()
-        alarms = [self.liststore[path][3].get_alarm() for path in selection]
+        alarms = [self.liststore[path][2] for path in selection]
         self.delete_alarms(alarms)
         self.emit("selection-changed")
 
     def load_alarms(self):
         self.alarms = self.storage.load()
         for alarm in self.alarms:
-            self.add_alarm_thumbnail(alarm)
+            self._add_alarm_item(alarm)
 
     def add_alarm(self, alarm):
         self.alarms.append(alarm)
         self.storage.save(self.alarms)
-        self.add_alarm_thumbnail(alarm)
+        self._add_alarm_item(alarm)
         self.show_all()
 
-    def add_alarm_thumbnail(self, alarm):
-        thumb = AlarmThumbnail(alarm)
+    def _add_alarm_item(self, alarm):
         label = GLib.markup_escape_text(alarm.name)
         view_iter = self.liststore.append([False,
-                                           thumb.get_pixbuf(),
                                            "<b>%s</b>" % label,
-                                           thumb])
+                                           alarm])
         self.notify("can-select")
 
     def update_alarm(self, old_alarm, new_alarm):
diff --git a/gnomeclocks/widgets.py b/gnomeclocks/widgets.py
index 86f3f8f..2e2dd82 100644
--- a/gnomeclocks/widgets.py
+++ b/gnomeclocks/widgets.py
@@ -21,106 +21,6 @@ from gi.repository import GObject, Gio, Gtk, Gdk, Pango, PangoCairo
 from gi.repository import Clutter, GtkClutter
 
 
-class DigitalClockDrawing(Gtk.DrawingArea):
-    width = 160
-    height = 160
-
-    def __init__(self):
-        Gtk.DrawingArea.__init__(self)
-        self.pango_context = None
-        self.ctx = None
-        self.pixbuf = None
-        self.surface = None
-        self.show_all()
-
-    def render(self, text, img, isDay, sub_text=None):
-        self.surface = cairo.ImageSurface.create_from_png(img)
-        ctx = cairo.Context(self.surface)
-        ctx.scale(1.0, 1.0)
-        ctx.set_source_surface(self.surface, 0, 0)
-        ctx.paint()
-
-        width = 136
-        height = 72
-        radius = 10
-        degrees = 0.017453293
-
-        x = (self.width - width) / 2
-        y = (self.height - height) / 2
-
-        # has to be before the drawing of the rectangle so the rectangle
-        # takes the right size if we have subtexts
-        self.pango_layout = self.create_pango_layout(text)
-        self.pango_layout.set_markup(
-            "<span size='xx-large'><b>%s</b></span>" % text, -1)
-        if sub_text:
-            self.pango_layout_subtext = self.create_pango_layout(sub_text)
-            self.pango_layout_subtext.set_markup(
-                "<span size='medium'>%s</span>" % sub_text, -1)
-            self.pango_layout_subtext.set_width(width * Pango.SCALE)
-            subtext_is_wrapped = self.pango_layout_subtext.is_wrapped()
-            if subtext_is_wrapped:
-                self.pango_layout_subtext.set_alignment(Pango.Alignment.CENTER)
-
-        if not isDay:
-            ctx.set_source_rgba(0.0, 0.0, 0.0, 0.7)
-        else:
-            ctx.set_source_rgba(1.0, 1.0, 1.0, 0.7)
-
-        ctx.move_to(x, y)
-        ctx.arc(x + width - radius, y + radius, radius, -90 * degrees,
-                0 * degrees)
-        if sub_text and subtext_is_wrapped:
-            ctx.arc(x + width - radius, y + height - radius + 25, radius,
-                    0 * degrees, 90 * degrees)
-            ctx.arc(x + radius, y + height - radius + 25, radius,
-                    90 * degrees, 180 * degrees)
-        elif sub_text and not subtext_is_wrapped:
-            ctx.arc(x + width - radius, y + height - radius + 10, radius,
-                    0 * degrees, 90 * degrees)
-            ctx.arc(x + radius, y + height - radius + 10, radius,
-                    90 * degrees, 180 * degrees)
-        else:
-            ctx.arc(x + width - radius, y + height - radius, radius,
-                    0 * degrees, 90 * degrees)
-            ctx.arc(x + radius, y + height - radius, radius,
-                    90 * degrees, 180 * degrees)
-        ctx.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees)
-        ctx.close_path()
-        ctx.fill()
-
-        if not isDay:
-            ctx.set_source_rgb(1.0, 1.0, 1.0)
-        else:
-            ctx.set_source_rgb(0.0, 0.0, 0.0)
-
-        text_width, text_height = self.pango_layout.get_pixel_size()
-        ctx.move_to(x + (width - text_width) / 2,
-                    y + (height - text_height) / 2)
-        PangoCairo.show_layout(ctx, self.pango_layout)
-
-        if sub_text:
-            sub_text_width, sub_text_height =\
-                self.pango_layout_subtext.get_pixel_size()
-            # centered on x axis, 5 pixels below main text on y axis
-            # for some reason setting the alignment adds an extra frame
-            # around it, slight change to allow for this
-            if subtext_is_wrapped:
-                ctx.move_to(x + (width - sub_text_width) / 2 - 10,
-                            y + (height - text_height) / 2 +
-                            sub_text_height - 10)
-            else:
-                ctx.move_to(x + (width - sub_text_width) / 2,
-                            y + (height - text_height) / 2 +
-                            sub_text_height + 10)
-            PangoCairo.show_layout(ctx, self.pango_layout_subtext)
-
-        pixbuf = Gdk.pixbuf_get_from_surface(self.surface, 0, 0, self.width,
-                                             self.height)
-        self.pixbuf = pixbuf
-        return self.pixbuf
-
-
 class Spinner(Gtk.SpinButton):
     def __init__(self, min_value, max_value):
         super(Spinner, self).__init__()
@@ -164,8 +64,12 @@ class TogglePixbufRenderer(Gtk.CellRendererPixbuf):
     active = GObject.Property(type=bool, default=False)
     toggle_visible = GObject.Property(type=bool, default=False)
 
-    def __init__(self):
-        Gtk.CellRendererPixbuf.__init__(self)
+    def __init__(self, **kwds):
+        Gtk.CellRendererPixbuf.__init__(self, **kwds)
+
+        # FIXME: currently broken with g-i
+        # icon_size = widget.style_get_property("check-icon-size")
+        self.icon_size = 40
 
     def do_render(self, cr, widget, background_area, cell_area, flags):
         Gtk.CellRendererPixbuf.do_render(self, cr, widget, background_area, cell_area, flags)
@@ -176,17 +80,13 @@ class TogglePixbufRenderer(Gtk.CellRendererPixbuf):
         xpad, ypad = self.get_padding()
         direction = widget.get_direction()
 
-        # FIXME: currently broken with g-i
-        # icon_size = widget.style_get_property("check-icon-size")
-        icon_size = 40
-
         if direction == Gtk.TextDirection.RTL:
             x_offset = xpad
         else:
-            x_offset = cell_area.width - icon_size - xpad
+            x_offset = cell_area.width - self.icon_size - xpad
 
         check_x = cell_area.x + x_offset
-        check_y = cell_area.y + cell_area.height - icon_size - ypad
+        check_y = cell_area.y + cell_area.height - self.icon_size - ypad
 
         context = widget.get_style_context()
         context.save()
@@ -195,39 +95,112 @@ class TogglePixbufRenderer(Gtk.CellRendererPixbuf):
         if self.active:
             context.set_state(Gtk.StateFlags.ACTIVE)
 
-        Gtk.render_check(context, cr, check_x, check_y, icon_size, icon_size)
+        Gtk.render_check(context, cr, check_x, check_y, self.icon_size, self.icon_size)
 
         context.restore()
 
     def do_get_size(self, widget, cell_area):
+        x_offset, y_offset, width, height = Gtk.CellRendererPixbuf.do_get_size(self, widget, cell_area)
+        width += self.icon_size // 4
+        height += self.icon_size // 4
+        return (x_offset, y_offset, width, height)
 
-        # FIXME: currently broken with g-i
-        # icon_size = widget.style_get_property("check-icon-size")
-        icon_size = 40
 
-        x_offset, y_offset, width, height = Gtk.CellRendererPixbuf.do_get_size(self, widget, cell_area)
+class DigitalClockRenderer(TogglePixbufRenderer):
+    foreground = GObject.Property(type=Gdk.RGBA)
+    background = GObject.Property(type=Gdk.RGBA)
+    text = GObject.Property(type=str)
+    subtext = GObject.Property(type=str)
 
-        width += icon_size // 4
+    def __init__(self):
+        TogglePixbufRenderer.__init__(self)#, width=160, height=160, xpad=10, ypad=10)
 
-        return (x_offset, y_offset, width, height)
+    def do_render(self, cr, widget, background_area, cell_area, flags):
+        TogglePixbufRenderer.do_render(self, cr, widget, background_area, cell_area, flags)
+
+        cr.save();
+        Gdk.cairo_rectangle(cr, cell_area);
+        cr.clip();
+        cr.translate(cell_area.x, cell_area.y)
+
+        w = 136
+        h = 72
+        r = 10
+        degrees = 0.017453293
+
+        # has to be before the drawing of the rectangle so the rectangle
+        # takes the right size if we have subtexts
+        layout = widget.create_pango_layout("")
+        layout.set_markup(
+            "<span size='xx-large'><b>%s</b></span>" % self.text, -1)
+        if self.subtext:
+            layout_subtext = widget.create_pango_layout("")
+            layout_subtext.set_markup(
+                "<span size='medium'>%s</span>" % self.subtext, -1)
+            layout_subtext.set_width(w * Pango.SCALE)
+            subtext_is_wrapped = layout_subtext.is_wrapped()
+            if subtext_is_wrapped:
+                layout_subtext.set_alignment(Pango.Alignment.CENTER)
+
+        # draw inner rectangle background
+        Gdk.cairo_set_source_rgba(cr, self.background)
+
+        x = (cell_area.width - w) / 2
+        y = (cell_area.height - h) / 2
+
+        cr.move_to(x, y)
+        cr.arc(x + w - r, y + r, r, -90 * degrees, 0 * degrees)
+        if self.subtext and subtext_is_wrapped:
+            cr.arc(x + w - r, y + h - r + 25, r, 0 * degrees, 90 * degrees)
+            cr.arc(x + r, y + h - r + 25, r, 90 * degrees, 180 * degrees)
+        elif self.subtext:
+            cr.arc(x + w - r, y + h - r + 10, r, 0 * degrees, 90 * degrees)
+            cr.arc(x + r, y + h - r + 10, r, 90 * degrees, 180 * degrees)
+        else:
+            cr.arc(x + w - r, y + h - r, r, 0 * degrees, 90 * degrees)
+            cr.arc(x + r, y + h - r, r, 90 * degrees, 180 * degrees)
+        cr.arc(x + r, y + r, r, 180 * degrees, 270 * degrees)
+        cr.close_path()
+        cr.fill()
+
+        # draw text
+        Gdk.cairo_set_source_rgba(cr, self.foreground)
+
+        text_w, text_h = layout.get_pixel_size()
+        cr.move_to(x + (w - text_w) / 2, y + (h - text_h) / 2)
+        PangoCairo.show_layout(cr, layout)
+
+        if self.subtext:
+            subtext_w, subtext_h = layout_subtext.get_pixel_size()
+            # centered on x axis, 5 pixels below main text on y axis
+            # for some reason setting the alignment adds an extra frame
+            # around it, slight change to allow for this
+            if subtext_is_wrapped:
+                cr.move_to(x + (w - subtext_w) / 2 - 10,
+                           y + (h - text_h) / 2 + subtext_h - 10)
+            else:
+                cr.move_to(x + (w - subtext_w) / 2,
+                           y + (h - text_h) / 2 + subtext_h + 10)
+            PangoCairo.show_layout(cr, layout_subtext)
+
+        cr.restore()
 
 
 class SelectableIconView(Gtk.IconView):
-    def __init__(self, model, selection_col, pixbuf_col, text_col):
+    def __init__(self, model, selection_col, text_col, thumb_data_func):
         Gtk.IconView.__init__(self, model)
 
         self.selection_mode = False
-
         self.selection_col = selection_col
 
         self.set_spacing(3)
         self.get_style_context().add_class('content-view')
 
-        self.renderer_pixbuf = TogglePixbufRenderer()
-        self.renderer_pixbuf.set_alignment(0.5, 0.5)
-        self.pack_start(self.renderer_pixbuf, False)
-        self.add_attribute(self.renderer_pixbuf, "active", selection_col)
-        self.add_attribute(self.renderer_pixbuf, "pixbuf", pixbuf_col)
+        self.icon_renderer = DigitalClockRenderer()
+        self.icon_renderer.set_alignment(0.5, 0.5)
+        self.pack_start(self.icon_renderer, False)
+        self.add_attribute(self.icon_renderer, "active", selection_col)
+        self.set_cell_data_func(self.icon_renderer, thumb_data_func, None)
 
         renderer_text = Gtk.CellRendererText()
         renderer_text.set_alignment(0.5, 0.5)
@@ -246,7 +219,7 @@ class SelectableIconView(Gtk.IconView):
     def set_selection_mode(self, active):
         if self.selection_mode != active:
             self.selection_mode = active
-            self.renderer_pixbuf.set_property("toggle_visible", active)
+            self.icon_renderer.set_property("toggle-visible", active)
 
             # force redraw
             self.queue_draw()
@@ -299,7 +272,7 @@ class ContentView(Gtk.Box):
         self.show_all()
 
 
-class SelectionToolbar():
+class SelectionToolbar:
     DEFAULT_WIDTH = 300
 
     def __init__(self, parent_actor):
diff --git a/gnomeclocks/world.py b/gnomeclocks/world.py
index f29d5c7..9ebcc52 100644
--- a/gnomeclocks/world.py
+++ b/gnomeclocks/world.py
@@ -20,11 +20,11 @@ import os
 import errno
 import time
 import json
-from gi.repository import GLib, GObject, Gio, Gtk, GdkPixbuf
+from gi.repository import GLib, GObject, Gio, Gdk, GdkPixbuf, Gtk
 from gi.repository import GWeather
 from clocks import Clock
 from utils import Dirs, SystemSettings, TimeString
-from widgets import DigitalClockDrawing, SelectableIconView, ContentView
+from widgets import SelectableIconView, ContentView
 
 
 # keep the GWeather world around as a singletom, otherwise
@@ -32,7 +32,7 @@ from widgets import DigitalClockDrawing, SelectableIconView, ContentView
 gweather_world = GWeather.Location.new_world(True)
 
 
-class WorldClockStorage():
+class WorldClockStorage:
     def __init__(self):
         self.filename = os.path.join(Dirs.get_user_data_dir(), "clocks.json")
 
@@ -122,7 +122,7 @@ class NewWorldClockDialog(Gtk.Dialog):
             self.set_response_sensitive(1, False)
 
 
-class ClockItem():
+class ClockItem:
     def __init__(self, location):
         self.location = location
         self.sunrise = time.strptime("197007:00", "%Y%H:%M")
@@ -162,26 +162,24 @@ class ClockItem():
     def get_time_as_string(self):
         return TimeString.format_time(self.get_location_time())
 
-    def get_day(self):
+    def get_day_as_string(self):
         clock_time_day = self.get_location_time().tm_yday
         local_time_day = time.localtime().tm_yday
 
-        if clock_time_day == local_time_day:
-            return "Today"
         # if its 31st Dec here and 1st Jan there, clock_time_day = 1,
         # local_time_day = 365/366
         # if its 1st Jan here and 31st Dec there, clock_time_day = 365/366,
         # local_time_day = 1
-        elif clock_time_day > local_time_day:
+        if clock_time_day > local_time_day:
             if local_time_day == 1:
-                return "Yesterday"
+                return _("Yesterday")
             else:
-                return "Tomorrow"
+                return _("Tomorrow")
         elif clock_time_day < local_time_day:
             if clock_time_day == 1:
-                return "Tomorrow"
+                return _("Tomorrow")
             else:
-                return "Yesterday"
+                return _("Yesterday")
 
     def get_sunrise_sunset_as_strings(self):
         sunrise = TimeString.format_time(self.sunrise)
@@ -208,32 +206,6 @@ class ClockItem():
                 return False
 
 
-class ClockThumbnail():
-    def __init__(self, clock):
-        self.clock = clock
-        self.drawing = DigitalClockDrawing()
-        self.update()
-
-    def update(self):
-        timestr = self.clock.get_time_as_string()
-        is_light = self.clock.get_is_light()
-        if is_light:
-            img = os.path.join(Dirs.get_image_dir(), "cities", "day.png")
-        else:
-            img = os.path.join(Dirs.get_image_dir(), "cities", "night.png")
-        day = self.clock.get_day()
-        if day == "Today":
-            self.drawing.render(timestr, img, is_light)
-        else:
-            self.drawing.render(timestr, img, is_light, day)
-
-    def get_clock(self):
-        return self.clock
-
-    def get_pixbuf(self):
-        return self.drawing.pixbuf
-
-
 class ClockStandalone(Gtk.EventBox):
     def __init__(self):
         Gtk.EventBox.__init__(self)
@@ -325,21 +297,21 @@ class World(Clock):
         self.notebook.set_show_border(False)
         self.add(self.notebook)
 
-        self.liststore = Gtk.ListStore(bool,
-                                       GdkPixbuf.Pixbuf,
-                                       str,
-                                       GObject.TYPE_PYOBJECT)
+        f = os.path.join(Dirs.get_image_dir(), "cities", "day.png")
+        self.daypixbuf = GdkPixbuf.Pixbuf.new_from_file(f)
+        f = os.path.join(Dirs.get_image_dir(), "cities", "night.png")
+        self.nightpixbuf = GdkPixbuf.Pixbuf.new_from_file(f)
 
-        self.iconview = SelectableIconView(self.liststore, 0, 1, 2)
+        self.liststore = Gtk.ListStore(bool, str, object)
+        self.iconview = SelectableIconView(self.liststore, 0, 1, self._thumb_data_func)
+        self.iconview.connect("item-activated", self._on_item_activated)
+        self.iconview.connect("selection-changed", self._on_selection_changed)
 
         contentview = ContentView(self.iconview,
                                   "document-open-recent-symbolic",
                                   _("Select <b>New</b> to add a world clock"))
         self.notebook.append_page(contentview, None)
 
-        self.iconview.connect("item-activated", self._on_item_activated)
-        self.iconview.connect("selection-changed", self._on_selection_changed)
-
         self.storage = WorldClockStorage()
         self.clocks = []
         self.load_clocks()
@@ -350,6 +322,19 @@ class World(Clock):
 
         self.timeout_id = GLib.timeout_add(1000, self._update_clocks)
 
+    def _thumb_data_func(self, view, cell, store, i, data):
+        clock = store.get_value(i, 2)
+        cell.text = clock.get_time_as_string()
+        cell.subtext = clock.get_day_as_string()
+        if clock.get_is_light():
+            cell.props.pixbuf = self.daypixbuf
+            cell.foreground = Gdk.RGBA(0.0, 0.0, 0.0, 1.0)
+            cell.background = Gdk.RGBA(1.0, 1.0, 1.0, 0.7)
+        else:
+            cell.props.pixbuf = self.nightpixbuf
+            cell.foreground = Gdk.RGBA(1.0, 1.0, 1.0, 1.0)
+            cell.background = Gdk.RGBA(1.0, 1.0, 1.0, 0.3)
+
     def set_mode(self, mode):
         self.mode = mode
         if mode is Clock.Mode.NORMAL:
@@ -362,15 +347,12 @@ class World(Clock):
             self.iconview.set_selection_mode(True)
 
     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.standalone.set_clock(thumb.get_clock())
+        clock = self.liststore[path][2]
+        self.standalone.set_clock(clock)
         self.emit("item-activated")
 
     def _on_selection_changed(self, iconview):
@@ -385,14 +367,14 @@ class World(Clock):
 
     def delete_selected(self):
         selection = self.get_selection()
-        clocks = [self.liststore[path][3].get_clock() for path in selection]
+        clocks = [self.liststore[path][2] for path in selection]
         self.delete_clocks(clocks)
         self.emit("selection-changed")
 
     def load_clocks(self):
         self.clocks = self.storage.load()
         for clock in self.clocks:
-            self.add_clock_thumbnail(clock)
+            self._add_clock_item(clock)
 
     def add_clock(self, location):
         if location.get_code() in [c.location.get_code() for c in self.clocks]:
@@ -401,18 +383,15 @@ class World(Clock):
         clock = ClockItem(location)
         self.clocks.append(clock)
         self.storage.save(self.clocks)
-        self.add_clock_thumbnail(clock)
+        self._add_clock_item(clock)
         self.show_all()
 
-    def add_clock_thumbnail(self, clock):
+    def _add_clock_item(self, clock):
         name = clock.location.get_city_name()
-        thumb = ClockThumbnail(clock)
         label = GLib.markup_escape_text(name)
         view_iter = self.liststore.append([False,
-                                           thumb.get_pixbuf(),
                                            "<b>%s</b>" % label,
-                                           thumb])
-        path = self.liststore.get_path(view_iter)
+                                           clock])
         self.notify("can-select")
 
     def delete_clocks(self, clocks):



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