adding the attribute last_x to the Gzv.__init__ method and
[cascardo/movie.git] / gzv.py
diff --git a/gzv.py b/gzv.py
index 59c04d5..f74432c 100644 (file)
--- a/gzv.py
+++ b/gzv.py
@@ -25,8 +25,9 @@ _ = lambda x:x
 class Ball(object):
     DEFAULT_WIDTH = 10
 
-    def __init__(self, x, y, r, name='', position=0):
+    def __init__(self, x, y, r, name='', position=0, selected=False):
         self.position = position
+        self.selected = selected
         self.x = int(x)
         self.y = int(y)
         self.radius = int(r)
@@ -115,7 +116,8 @@ class NewProject(GladeLoader):
             self.dialog.set_transient_for(parent)
 
     def get_project(self):
-        if not self.dialog.run():
+        # This '1' was defined in the glade file
+        if not self.dialog.run() == 1:
             return None
 
         fname = self.wid('image').get_filename()
@@ -137,9 +139,14 @@ class Gzv(GladeLoader):
         self.evtbox.connect('button-release-event', self.button_release)
         self.evtbox.connect('motion-notify-event', self.motion_notify)
 
+        # making it possible to grab motion events when the mouse is
+        # over the widget.
+        self.evtbox.set_events(gtk.gdk.POINTER_MOTION_MASK)
+
         self.model = gtk.ListStore(int, str)
         self.treeview = self.wid('treeview')
         self.treeview.set_model(self.model)
+        self.treeview.connect('button-press-event', self.select_fp)
 
         self.draw = self.wid('draw')
         self.draw.connect_after('expose-event', self.expose_draw)
@@ -157,8 +164,12 @@ class Gzv(GladeLoader):
         # drawing stuff
         self.start_x = -1
         self.start_y = -1
+        self.last_x = -1
         self.radius = Ball.DEFAULT_WIDTH
 
+    def show(self):
+        self.window.show_all()
+
     def setup_treeview(self):
         self.model.connect('rows-reordered', self.on_rows_reordered)
 
@@ -185,7 +196,6 @@ class Gzv(GladeLoader):
         project = proj.get_project()
         proj.destroy()
 
-        # This '1' was defined in the glade file
         if project:
             self.load_project(project)
 
@@ -216,10 +226,10 @@ class Gzv(GladeLoader):
         self.balls = self.load_balls_from_file(project.focus_points_file)
         self.image = project.image
 
-        # loading the picture image and getting some useful
-        # information to draw it in the widget's background
+        # I'm loading a pixbuf first because I need to get its
+        # dimensions this with a pixbuf is easier than with an image.
         try:
-            self.draw.set_from_file(project.image)
+            pixbuf = gtk.gdk.pixbuf_new_from_file(project.image)
         except gobject.GError:
             msg = _("Couldn't recognize the image file format.")
             dialog = gtk.MessageDialog(self.window,
@@ -231,6 +241,7 @@ class Gzv(GladeLoader):
             dialog.destroy()
             return self.unload_project()
 
+        self.draw.set_from_pixbuf(pixbuf)
         self.load_balls_to_treeview()
 
     def unload_project(self):
@@ -266,6 +277,21 @@ class Gzv(GladeLoader):
                 if i.position == int(position):
                     self.balls.remove(i)
             del model[path]
+            self.draw.queue_draw()
+
+    def select_fp(self, treeview, event):
+        path, column, x, y = \
+            self.treeview.get_path_at_pos(int(event.x), int(event.y))
+        if path:
+            model = self.treeview.get_model()
+            ball = self.balls[model[path][0]]
+
+            # making sure that only one ball is selected
+            for i in self.balls:
+                i.selected = False
+            ball.selected = True
+
+            self.draw.queue_draw()
 
     def save_fp_list(self, *args):
         assert self.project is not None
@@ -292,22 +318,28 @@ class Gzv(GladeLoader):
         if not self.image:
             return
 
-        self.draw_current_ball()
         for i in self.balls:
             self.draw_ball(i)
+
+        if self.start_x < 0:
+            return False
+
+        ball = Ball(self.start_x, self.start_y, self.radius)
+        self.draw_ball(ball)
+
         return False
 
     def draw_ball(self, ball):
         ctx = self.draw.window.cairo_create()
         ctx.arc(ball.x, ball.y, ball.radius, 0, 64*math.pi)
-        ctx.set_source_rgba(0.5, 0.0, 0.0, 0.4)
+        ctx.set_source_rgba(0.0, 0.0, 0.5, 0.4)
         ctx.fill()
 
-    def draw_current_ball(self):
-        if self.start_x < 0:
-            return
-        ball = Ball(self.start_x, self.start_y, self.radius)
-        self.draw_ball(ball)
+        if ball.selected:
+            ctx.set_source_rgba(0.0, 0.5, 0.0, 0.4)
+            ctx.set_line_width(5)
+            ctx.arc(ball.x, ball.y, ball.radius+1, 0, 64*math.pi)
+            ctx.stroke()
 
     def button_press(self, widget, event):
         if event.button == 1:
@@ -336,9 +368,11 @@ class Gzv(GladeLoader):
         self.model.append([position, ''])
         self.treeview.set_cursor(str(position), self.fpcolumn, True)
 
-        # returning to the standard radius
+        # reseting to the default coordenades
+        self.start_x = -1
+        self.start_y = -1
         self.radius = Ball.DEFAULT_WIDTH
 
 if __name__ == '__main__':
-    Gzv().window.show_all()
+    Gzv().show()
     gtk.main()