[hamster-applet] mouse events will now return just the topmost target instead of the whole list as the former is more



commit 64eff3c845fc9ffd751a16f78a670d0ede16a4a1
Author: Toms Bauģis <toms baugis gmail com>
Date:   Tue Jul 13 14:17:23 2010 +0100

    mouse events will now return just the topmost target instead of the whole list as the former is more practical

 src/hamster/charting.py     |   10 ++-----
 src/hamster/graphics.py     |   57 +++++++++++++++++++++----------------------
 src/hamster/widgets/tags.py |   15 +++--------
 3 files changed, 35 insertions(+), 47 deletions(-)
---
diff --git a/src/hamster/charting.py b/src/hamster/charting.py
index d4f0132..5eed01b 100644
--- a/src/hamster/charting.py
+++ b/src/hamster/charting.py
@@ -106,21 +106,17 @@ class Chart(graphics.Scene):
         self.label_color = self.colors.contrast(fg_color,  80)
 
 
-    def on_mouse_over(self, scene, targets):
-        bar = targets[0]
+    def on_mouse_over(self, scene, bar):
         if bar.key not in self.selected_keys:
             bar.fill = self.get_style().base[gtk.STATE_PRELIGHT].to_string()
 
-    def on_mouse_out(self, scene, targets):
-        bar = targets[0]
+    def on_mouse_out(self, scene, bar):
         if bar.key not in self.selected_keys:
             bar.fill = self.bar_color
 
-    def on_click(self, scene, event, targets):
-        clicked_bar = targets[0]
+    def on_click(self, scene, event, clicked_bar):
         self.emit("bar-clicked", clicked_bar.key)
 
-
     def plot(self, keys, data):
         self.data = data
 
diff --git a/src/hamster/graphics.py b/src/hamster/graphics.py
index bf232ae..fb7d8a2 100644
--- a/src/hamster/graphics.py
+++ b/src/hamster/graphics.py
@@ -549,6 +549,8 @@ class Sprite(gtk.Object):
 
     def _on_click(self, button_state):
         self.emit("on-click", button_state)
+        if self.parent and isinstance(self.parent, Sprite):
+            self.parent._on_click(button_state)
 
     def _on_mouse_over(self):
         # scene will call us when there is mouse
@@ -817,7 +819,7 @@ class Scene(gtk.DrawingArea):
 
 
         self._last_frame_time = None
-        self._mouse_sprites = set()
+        self._mouse_sprite = None
         self._mouse_drag = None
         self._drag_sprite = None
         self._button_press_time = None # to distinguish between click and drag
@@ -987,36 +989,31 @@ class Scene(gtk.DrawingArea):
 
 
         #check if we have a mouse over
-        over = set()
+        over = None
         for sprite in self.all_sprites():
             if sprite.interactive and self._check_hit(sprite, mouse_x, mouse_y):
-                if custom_mouse == False:
-                    if sprite.draggable:
-                        cursor = gtk.gdk.FLEUR
-                    else:
-                        cursor = gtk.gdk.HAND2
-
-                over.add(sprite)
+                over = sprite
 
+        if over:
+            if custom_mouse == False:
+                if over.draggable:
+                    cursor = gtk.gdk.FLEUR
+                else:
+                    cursor = gtk.gdk.HAND2
 
-        new_mouse_overs = over - self._mouse_sprites
-        if new_mouse_overs:
-            for sprite in new_mouse_overs:
-                sprite._on_mouse_over()
-
-            self.emit("on-mouse-over", list(new_mouse_overs))
-            self.redraw()
+            if over != self._mouse_sprite:
+                over._on_mouse_over()
 
+                self.emit("on-mouse-over", over)
+                self.redraw()
 
-        gone_mouse_overs = self._mouse_sprites - over
-        if gone_mouse_overs:
-            for sprite in gone_mouse_overs:
-                sprite._on_mouse_out()
-            self.emit("on-mouse-out", list(gone_mouse_overs))
+        if self._mouse_sprite and self._mouse_sprite != over:
+            self._mouse_sprite._on_mouse_out()
+            self.emit("on-mouse-out", self._mouse_sprite)
             self.redraw()
 
 
-        self._mouse_sprites = over
+        self._mouse_sprite = over
 
         if isinstance(cursor, gtk.gdk.Cursor):
             self.window.set_cursor(cursor)
@@ -1029,9 +1026,9 @@ class Scene(gtk.DrawingArea):
 
     def __on_mouse_leave(self, area, event):
         self._mouse_in = False
-        if self._mouse_sprites:
-            self.emit("on-mouse-out", list(self._mouse_sprites))
-            self._mouse_sprites = set()
+        if self._mouse_sprite:
+            self.emit("on-mouse-out", self._mouse_sprite)
+            self._mouse_sprite = None
             self.redraw()
 
     def _check_hit(self, sprite, x, y):
@@ -1082,11 +1079,13 @@ class Scene(gtk.DrawingArea):
         self._drag_sprite = None
 
         if click:
-            targets = []
+            target = None
             for sprite in self.all_sprites():
                 if sprite.interactive and self._check_hit(sprite, event.x, event.y):
-                    targets.append(sprite)
-                    sprite._on_click(event.state)
+                    target = sprite
+
+            if target:
+                target._on_click(event.state)
+                self.emit("on-click", event, target)
 
-            self.emit("on-click", event, targets)
         self.emit("on-mouse-up")
diff --git a/src/hamster/widgets/tags.py b/src/hamster/widgets/tags.py
index 321a182..4f18cd9 100644
--- a/src/hamster/widgets/tags.py
+++ b/src/hamster/widgets/tags.py
@@ -247,29 +247,22 @@ class TagBox(graphics.Scene):
 
         self.connect("on-enter-frame", self.on_enter_frame)
 
-    def on_mouse_over(self, area, targets):
-        tag = targets[0]
+    def on_mouse_over(self, area, tag):
         tag.tag.fill = tag.graphics.colors.darker(tag.tag.fill, -20)
 
-    def on_mouse_out(self, area, targets):
-        tag = targets[0]
-
+    def on_mouse_out(self, area, tag):
         if tag.text in self.selected_tags:
             tag.tag.fill = (242, 229, 97)
         else:
             tag.tag.fill = (241, 234, 170)
 
 
-    def on_tag_click(self, area, event, targets):
-        if not targets:
-            return
-
-        tag = targets[0]
+    def on_tag_click(self, area, event, tag):
         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.on_mouse_out(area, tag) #paint
         self.redraw()
 
     def draw(self, tags):



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