[gnome-clocks/editmode: 2/2] First cut at selection mode



commit 3a0f90418f508a3c69e8e5dedda3d2f322f67223
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Aug 19 10:06:50 2012 +0200

    First cut at selection mode
    
    I did not import libgd, since it would be painful (switching build
    system, compile C, add introspection etc) and since it does more than we
    need (switching list view and icon view etc). Fow now I somply ported
    the selection cell renderer to python, it is just ~100 lines of code.

 gnomeclocks/app.py     |    6 +++
 gnomeclocks/clocks.py  |   40 ++++++++++++++++++++--
 gnomeclocks/widgets.py |   85 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 127 insertions(+), 4 deletions(-)
---
diff --git a/gnomeclocks/app.py b/gnomeclocks/app.py
index fc53d89..d9566bd 100644
--- a/gnomeclocks/app.py
+++ b/gnomeclocks/app.py
@@ -322,6 +322,12 @@ class ClocksToolbar(Gtk.Toolbar):
         self.selection_toolbar.set_visible(selection_mode)
         self.set_visible(not selection_mode)
 
+        active_view = None
+        for view in self.views:
+            if view.button.get_active():
+                active_view = view
+        active_view.set_selection_mode(selection_mode)
+
     def _delete_clock(self, button):
         pass
 
diff --git a/gnomeclocks/clocks.py b/gnomeclocks/clocks.py
index f2aa9fd..9da88e0 100644
--- a/gnomeclocks/clocks.py
+++ b/gnomeclocks/clocks.py
@@ -21,6 +21,7 @@ from gi.repository.GdkPixbuf import Pixbuf
 
 from widgets import NewWorldClockDialog, AlarmDialog
 from widgets import DigitalClock, AlarmWidget, EmptyPlaceholder
+from widgets import TogglePixbufRenderer
 from storage import worldclockstorage
 from utils import SystemSettings, Alert
 
@@ -85,8 +86,10 @@ class World(Clock):
         Clock.__init__(self, _("World"), True, True)
         self.addButton = None
 
-        self.liststore = liststore = Gtk.ListStore(Pixbuf, str,
-                                                   GObject.TYPE_PYOBJECT)
+        self.liststore = liststore = Gtk.ListStore(Pixbuf,
+                                                   str,
+                                                   GObject.TYPE_PYOBJECT,
+                                                   bool)
         self.iconview = iconview = Gtk.IconView.new()
 
         self.empty_view = EmptyPlaceholder(
@@ -95,9 +98,16 @@ class World(Clock):
 
         iconview.set_model(liststore)
         iconview.set_spacing(3)
-        iconview.set_pixbuf_column(0)
         iconview.get_style_context().add_class('content-view')
 
+        # FIXME: factor out a SelectableIconview widget to share
+        # between world and alarms
+        self.renderer_pixbuf = TogglePixbufRenderer();
+        self.renderer_pixbuf.set_alignment(0.5, 0.5)
+        iconview.pack_start(self.renderer_pixbuf, False)
+        iconview.add_attribute(self.renderer_pixbuf, "pixbuf", 0)
+        iconview.add_attribute(self.renderer_pixbuf, "active", 3)
+
         renderer_text = Gtk.CellRendererText()
         renderer_text.set_alignment(0.5, 0.5)
         iconview.pack_start(renderer_text, True)
@@ -106,12 +116,31 @@ class World(Clock):
         self.scrolledwindow = scrolledwindow = Gtk.ScrolledWindow()
         scrolledwindow.add(iconview)
 
+        # FIXME: do press and release like documents to check the click
+        # is on the same item
+        iconview.connect("button-press-event", self._on_button_press_event)
         iconview.connect("selection-changed", self._on_selection_changed)
 
         self.clocks = []
         self.load_clocks()
         self.show_all()
 
+    def _on_button_press_event (self, iconview, event):
+        path = iconview.get_path_at_pos(event.x, event.y);
+
+        if path:
+            i = self.liststore.get_iter(path)
+            if i:
+                selected = self.liststore.get_value(i, 3)
+                self.liststore.set_value(i, 3, not selected)
+
+#                g_signal_emit (self, signals[VIEW_SELECTION_CHANGED], 0);
+
+        return False
+
+    def set_selection_mode(self, active):
+        self.renderer_pixbuf.set_property("toggle_visible", active)
+
     def unselect_all(self):
         self.iconview.unselect_all()
 
@@ -149,7 +178,7 @@ class World(Clock):
         name = d.location.get_city_name()
         view_iter = self.liststore.append([d.drawing.pixbuf,
                                            "<b>" + name + "</b>",
-                                           d])
+                                           d, False])
         d.set_iter(self.liststore, view_iter)
 
     def delete_clock(self, d):
@@ -218,6 +247,9 @@ class Alarm(Clock):
         self.load_alarms()
         self.show_all()
 
+    def set_selection_mode(self, active):
+        pass
+
     def _on_selection_changed(self, iconview):
         items = iconview.get_selected_items()
         if items:
diff --git a/gnomeclocks/widgets.py b/gnomeclocks/widgets.py
index 372a6e2..e32be10 100644
--- a/gnomeclocks/widgets.py
+++ b/gnomeclocks/widgets.py
@@ -589,3 +589,88 @@ class EmptyPlaceholder(Gtk.Box):
         self.pack_start(text, False, False, 6)
         self.pack_start(Gtk.Label(), True, True, 0)
         self.show_all()
+
+
+# Python version of the gd-toggle-pixbuf-renderer of gnome-documents
+# we should use those widgets directly at some point, but for now
+# it is easier to just reimplement this renderer than include and build
+# a C library
+class TogglePixbufRenderer(Gtk.CellRendererPixbuf):
+
+    __gproperties__ = {
+         "active" : (GObject.TYPE_BOOLEAN,
+                    "Active",
+                    "Whether the cell renderer is active",
+                    False,
+                    GObject.PARAM_READWRITE),
+         "toggle-visible" : (GObject.TYPE_BOOLEAN,
+                             "Toggle visible",
+                             "Whether to draw the toggle indicator",
+                             False,
+                             GObject.PARAM_READWRITE)
+    }
+
+    def __init__(self):
+        Gtk.CellRendererPixbuf.__init__(self)
+        self._active = False
+        self._toggle_visible = False
+
+    def do_render(self, cr, widget, background_area, cell_area, flags):
+        Gtk.CellRendererPixbuf.do_render(self, cr, widget, background_area, cell_area, flags)
+
+        if not self._toggle_visible:
+            return
+
+        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
+
+        check_x = cell_area.x + x_offset
+        check_y = cell_area.y + cell_area.height - icon_size - ypad
+
+        context = widget.get_style_context()
+        context.save()
+        context.add_class(Gtk.STYLE_CLASS_CHECK)
+
+        if self._active:
+            context.set_state(Gtk.StateFlags.ACTIVE)
+
+        Gtk.render_check(context, cr, check_x, check_y, icon_size, icon_size)
+
+        context.restore()
+
+    def do_get_size(self, widget, cell_area):
+
+        # 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)
+
+        width += icon_size / 4
+
+        return (x_offset, y_offset, width, height)
+
+    def do_get_property(self, prop):
+        if prop.name == "active":
+            return self._active
+        elif prop.name == "toggle-visible":
+            return self._toggle_visible
+        else:
+            raise AttributeError, 'unknown property %s' % prop.name
+
+    def do_set_property(self, prop, value):
+        if prop.name == "active":
+            self._active = value
+        elif prop.name == "toggle-visible":
+            self._toggle_visible = value
+        else:
+            raise AttributeError, 'unknown property %s' % prop.name



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