[hamster-applet] tag clicking is back. should check performance later



commit da2617c05647e733a3525c48bd63e9992154f57c
Author: Toms Bauģis <toms baugis gmail com>
Date:   Thu Apr 8 19:47:17 2010 +0100

    tag clicking is back. should check performance later

 src/hamster/widgets/facttree.py |   11 ++-
 src/hamster/widgets/tags.py     |  218 +++++++++++++--------------------------
 2 files changed, 79 insertions(+), 150 deletions(-)
---
diff --git a/src/hamster/widgets/facttree.py b/src/hamster/widgets/facttree.py
index a2ff24d..b284cb3 100644
--- a/src/hamster/widgets/facttree.py
+++ b/src/hamster/widgets/facttree.py
@@ -424,14 +424,16 @@ class FactCellRenderer(gtk.GenericCellRenderer):
                 cur_x, cur_y = start_x, start_y
 
                 for i, tag in enumerate(fact["tags"]):
-                    tag_w, tag_h = Tag.tag_size(tag, self.tag_layout)
+                    temp_tag = Tag(tag)
+                    tag_w, tag_h = temp_tag.width, temp_tag.height
 
                     if i > 0 and cur_x + tag_w >= cell_end:
                         cur_x = start_x
                         cur_y += tag_h + 4
 
-                    Tag(context, self.tag_layout, True, tag, None,
-                        gtk.gdk.Rectangle(cur_x, cur_y, cell_end - cur_x, height - cur_y))
+                    sprite = Tag(tag, False)
+                    sprite.x, sprite.y  = cur_x, cur_y
+                    sprite._draw(context)
 
                     cur_x += tag_w + 4
 
@@ -505,7 +507,8 @@ class FactCellRenderer(gtk.GenericCellRenderer):
             layout.set_font_description(self.tag_font)
 
             for i, tag in enumerate(fact["tags"]):
-                tag_w, tag_h = Tag.tag_size(tag, layout)
+                temp_tag = Tag(tag)
+                tag_w, tag_h = temp_tag.width, temp_tag.height
 
                 if i > 0 and cur_x + tag_w >= tag_cell_end:
                     cur_x = tag_cell_start
diff --git a/src/hamster/widgets/tags.py b/src/hamster/widgets/tags.py
index 87bea15..16c9e3e 100644
--- a/src/hamster/widgets/tags.py
+++ b/src/hamster/widgets/tags.py
@@ -229,178 +229,104 @@ class TagBox(graphics.Scene):
         self.font_size = 10 #override default font size
 
         if self.interactive:
-            self.connect("on-mouse-over", self.on_tag_hover)
+            self.connect("on-mouse-over", self.on_mouse_over)
+            self.connect("on-mouse-out", self.on_mouse_out)
             self.connect("on-click", self.on_tag_click)
 
         self.connect("on-enter-frame", self.on_enter_frame)
 
-    def on_tag_hover(self, widget, regions):
-        if regions:
-            self.hover_tag = regions[0]
-        else:
-            self.hover_tag = None
+    def on_mouse_over(self, area, targets):
+        tag = targets[0]
+        tag.tag.fill = tag.graphics.colors.darker(tag.tag.fill, -20)
 
-        self.redraw()
+    def on_mouse_out(self, area, targets):
+        tag = targets[0]
 
-    def on_tag_click(self, widget, regions):
-        tag = regions[0]
-        if tag in self.selected_tags:
-            #self.selected_tags.remove(tag)
-            self.emit("tag-unselected", tag)
+        if tag.text in self.selected_tags:
+            tag.tag.fill = (242, 229, 97)
         else:
-            #self.selected_tags.append(tag)
-            self.emit("tag-selected", tag)
+            tag.tag.fill = (241, 234, 170)
 
-        self.redraw()
 
-    def draw(self, tags):
-        """Draw chart with given data"""
-        self.tags = tags
-        self.show()
+    def on_tag_click(self, area, event, targets):
+        tag = targets[0]
+        if tag.text in self.selected_tags:
+            self.emit("tag-unselected", tag.text)
+        else:
+            self.emit("tag-selected", tag.text)
+        self.on_mouse_out(area, targets) #paint
         self.redraw()
 
-    def set_text(self, text):
-        # sets text and returns width and height of the layout
-        self.layout.set_text(text)
-        w, h = self.layout.get_pixel_size()
-        return w, h
-
-    def tag_size(self, label):
-        text_w, text_h = self.set_text(label)
-        w = text_w + 16 # padding (we have some diagonals to draw)
-        h = text_h + 2
-        return w, h
-
-    def count_height(self, width):
-        # tells you how much height in this width is needed to show all tags
-
-        if not self.tags:
-            return None
+    def draw(self, tags):
+        new_tags = [Tag(label) for label in tags]
 
-        pixmap = gtk.gdk.Pixmap(None, width, 500, 24)
-        context = pixmap.cairo_create()
+        for tag in self.tags:
+            self.sprites.remove(tag)
 
-        if not self.layout:
-            self.layout = context.create_layout()
-            default_font = pango.FontDescription(gtk.Style().font_desc.to_string())
-            default_font.set_size(pango.SCALE * 10)
-            self.layout.set_font_description(default_font)
+        self.add_child(*new_tags)
+        self.tags = new_tags
 
-        cur_x, cur_y = 4, 4
-        for tag in self.tags:
-            w, h = self.tag_size(tag)
-            if cur_x + w >= width - 5:  #if we do not fit, we wrap
-                cur_x = 5
-                cur_y += h + 6
+        self.show()
+        self.redraw()
 
-            cur_x += w + 8 #some padding too, please
-        return cur_y + h + 6
+    def count_height(self, width):
+        # reposition tags and see how much space we take up
+        self.width = width
+        w, h = self.on_enter_frame(None, None)
+        return h + 6
 
     def on_enter_frame(self, scene, context):
         cur_x, cur_y = 4, 4
+        tag = None
         for tag in self.tags:
-            w, h = self.tag_size(tag)
-            if cur_x + w >= self.width - 5:  #if we do not fit, we wrap
+            if cur_x + tag.width >= self.width - 5:  #if we do not fit, we wrap
                 cur_x = 5
-                cur_y += h + 6
-
-            if tag in self.selected_tags:
-                color = (242, 229, 97)
-            elif tag == self.hover_tag:
-                color = (252, 248, 204)
-            else:
-                color = (241, 234, 170)
-
-            Tag(context,
-                self.layout,
-                True,
-                tag,
-                color,
-                gtk.gdk.Rectangle(cur_x, cur_y, self.width - cur_x, self.height - cur_y))
-
-            if 1==2 and self.interactive:
-                self.register_mouse_region(cur_x, cur_y, cur_x + w, cur_y + h, tag)
-
-            cur_x += w + 6 #some padding too, please
-
+                cur_y += tag.height + 6
 
-class Tag(object):
-    def __init__(self, context, layout, render_now = False, label = None, color = None, rect = None):
-        if not context:
-            render_now = False
-        else:
-            self.context = context
-            self.layout = layout
+            tag.x = cur_x
+            tag.y = cur_y
 
-        self.x, self.y, self.width, self.height = rect.x, rect.y, rect.width, rect.height
+            cur_x += tag.width + 6 #some padding too, please
 
-        if not render_now:
-            return
+        if tag:
+            cur_y += tag.height + 2 # the last one
 
-        self.label = label
-        self.color = color or (241, 234, 170)
+        return cur_x, cur_y
 
-        self.context.save()
-        self.draw_tag()
-        self.context.restore()
+class Tag(graphics.Sprite):
+    def __init__(self, text, interactive = True, color = "#F1EAAA"):
+        graphics.Sprite.__init__(self, interactive = interactive)
 
-    def set_color(self, color, opacity = None):
-        if color[0] > 1 or color[1] > 0 or color[2] > 0:
-            color = [c / 255.0 for c in color]
+        self.text = text
+        label = graphics.Label(text, size = 8, color = (30, 30, 30), y = 2)
 
-        if opacity:
-            self.context.set_source_rgba(color[0], color[1], color[2], opacity)
-        elif len(color) == 3:
-            self.context.set_source_rgb(*color)
-        else:
-            self.context.set_source_rgba(*color)
-
-    def set_text(self, text):
-        # sets text and returns width and height of the layout
-        self.layout.set_text(text)
-        w, h = self.layout.get_pixel_size()
-        return w, h
-
-    @staticmethod
-    def tag_size(label, layout):
-        layout.set_text(label)
-        text_w, text_h = layout.get_pixel_size()
-        w = text_w + 16 # padding (we have some diagonals to draw)
-        h = text_h + 2
-        return w, h
-
-    def draw_tag(self):
-        self.context.set_line_width(1)
-        label, x, y, color = self.label, self.x, self.y, self.color
-        if x - round(x) != 0.5: x += 0.5
-        if y - round(y) != 0.5: y += 0.5
-
-        w, h = self.tag_size(label, self.layout)
+        w, h = label.width + 18, label.height + 3
         corner = h / 3
+        label.x = corner + 8
+
+        self.color = color
+
+        self.tag = graphics.Polygon([(0, corner),
+                                         (corner, 0),
+                                         (w, 0),
+                                         (w, h),
+                                         (corner, h),
+                                         (0, h - corner)],
+                                        x = 0.5, y = 0.5,
+                                        fill = self.color,
+                                        stroke = "#b4b4b4",
+                                        line_width = 1)
+
+        self.add_child(self.tag)
+        self.add_child(graphics.Circle(2, x = 5.5, y = h / 2 - 1.5,
+                                       fill = "#fff",
+                                       stroke = "#b4b4b4",
+                                       line_width = 1))
+
+        self.add_child(label)
+        self.width, self.height = w, h
+
+        self.graphics.set_color((0,0,0), 0)
+        self.graphics.rectangle(0, 0, w, h)
+        self.graphics.stroke()
 
-        self.context.move_to(x, y + corner)
-        self.context.line_to(x + corner, y)
-        self.context.line_to(x + w, y)
-        self.context.line_to(x + w, y + h)
-        self.context.line_to(x + corner, y + h)
-        self.context.line_to(x, y + h - corner)
-        self.context.line_to(x, y + corner)
-
-        self.set_color(color)
-        self.context.fill_preserve()
-        self.set_color((180, 180, 180))
-        self.context.stroke()
-
-        self.context.arc(x + 6, y + h / 2 + 1, 2, 0, 2 * pi)
-        self.set_color((255, 255, 255))
-        self.context.fill_preserve()
-        self.set_color((180, 180, 180))
-        self.context.stroke()
-
-        self.set_color((0, 0, 0))
-
-        #self.layout.set_width((self.width) * pango.SCALE)
-        self.context.move_to(x + 12,y)
-
-        self.set_color((30, 30, 30))
-        self.context.show_layout(self.layout)



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