fixing issues when the image does not fill the whole allocated
[cascardo/movie.git] / gzv.py
diff --git a/gzv.py b/gzv.py
index 90e6bb8..6d591fc 100644 (file)
--- a/gzv.py
+++ b/gzv.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8; -*-
 # gzv.py - an user interface to generate-zooming-video
 #
 # Copyright (C) 2008  Lincoln de Sousa <lincoln@minaslivre.org>
@@ -22,13 +23,22 @@ from ConfigParser import ConfigParser
 
 _ = lambda x:x
 
+class Point(object):
+    def __init__(self, x, y):
+        self.x = x
+        self.y = y
+
+    @staticmethod
+    def pythagorean(p1, p2):
+        return math.sqrt((p2.x - p1.x)**2 + (p2.y - p1.y)**2)
+
 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.x = int(x)
-        self.y = int(y)
+        self.selected = selected
+        self.p = Point(int(x), int(y))
         self.radius = int(r)
         self.name = name
 
@@ -39,7 +49,7 @@ class BallManager(list):
     def save_to_file(self, path):
         target = open(path, 'w')
         for i in self:
-            target.write('%d,%d %d %s\n' % (i.x, i.y, i.radius, i.name))
+            target.write('%d,%d %d %s\n' % (i.p.x, i.p.y, i.radius, i.name))
         target.close()
 
 class GladeLoader(object):
@@ -115,7 +125,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()
@@ -136,10 +147,16 @@ class Gzv(GladeLoader):
         self.evtbox.connect('button-press-event', self.button_press)
         self.evtbox.connect('button-release-event', self.button_release)
         self.evtbox.connect('motion-notify-event', self.motion_notify)
+        self.evtbox.connect('motion-notify-event', self.ball_motion)
+
+        # 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)
@@ -154,12 +171,19 @@ class Gzv(GladeLoader):
         self.load_balls_to_treeview()
         self.setup_treeview()
 
+        self.new_ball = False
+        self.move_ball = None
+
         # drawing stuff
-        self.selecting = False
         self.start_x = -1
         self.start_y = -1
+        self.last_x = -1
+        self.last_y = -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)
 
@@ -186,7 +210,6 @@ class Gzv(GladeLoader):
         project = proj.get_project()
         proj.destroy()
 
-        # This '1' was defined in the glade file
         if project:
             self.load_project(project)
 
@@ -217,10 +240,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,
@@ -232,6 +255,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):
@@ -267,6 +291,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
@@ -293,36 +332,83 @@ 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
+
+        if self.new_ball:
+            ball = Ball(self.start_x, self.start_y, self.radius)
+            self.draw_ball(ball)
+
         return False
 
+    def ball_width_border(self, ball):
+        iw, ih = self.draw.size_request()
+        w = self.draw.get_allocation().width
+        h = self.draw.get_allocation().height
+
+        x = ((w / 2) - (iw / 2)) + ball.p.x
+        y = ((h / 2) - (ih / 2)) + ball.p.y
+        return Point(x, y)
+
+    def point_without_border(self, point):
+        iw, ih = self.draw.size_request()
+        w = self.draw.get_allocation().width
+        h = self.draw.get_allocation().height
+
+        x = point.x - ((w / 2) - (iw / 2))
+        y = point.y - ((h / 2) - (ih / 2))
+        return Point(x, y)
+
     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.arc(self.ball_width_border(ball).x,
+                self.ball_width_border(ball).y,
+                ball.radius, 0, 64*math.pi)
+        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(self.ball_width_border(ball).x,
+                    self.ball_width_border(ball).y,
+                    ball.radius+1, 0, 64*math.pi)
+            ctx.stroke()
 
     def button_press(self, widget, event):
+        self.new_ball = True
+
+        self.last_x = event.x
+        self.last_y = event.y
+
         if event.button == 1:
-            self.selecting = True
-            self.start_x = event.x
-            self.start_y = event.y
-            self.last_x = event.x
+            for i in self.balls:
+                p1 = Point(event.x, event.y)
+                p2 = self.ball_width_border(i)
+                if Point.pythagorean(p1, p2) < i.radius:
+                    self.last_x = event.x - i.p.x
+                    self.last_y = event.y - i.p.y
+
+                    self.new_ball = False
+                    self.move_ball = i
+                    break
+
+            self.start_x = self.point_without_border(event).x
+            self.start_y = self.point_without_border(event).y
 
     def button_release(self, widget, event):
+        self.move_ball = None
+
         if event.button == 1:
-            self.selecting = False
             self.finish_drawing()
 
     def motion_notify(self, widget, event):
+        if not self.new_ball:
+            return
+
         self.draw.queue_draw()
 
         if event.x > self.last_x:
@@ -332,16 +418,29 @@ class Gzv(GladeLoader):
 
         self.last_x = event.x
 
-    def finish_drawing(self):
-        position = len(self.balls)
-        ball = Ball(self.start_x, self.start_y, self.radius, '', position)
-        self.balls.append(ball)
-        self.model.append([position, ''])
-        self.treeview.set_cursor(str(position), self.fpcolumn, True)
+    def ball_motion(self, widget, event):
+        if not self.move_ball:
+            return
+
+        self.move_ball.p.x = self.point_without_border(event).x
+        self.move_ball.p.y = self.point_without_border(event).y
 
-        # returning to the standard radius
+        self.draw.queue_draw()
+
+    def finish_drawing(self):
+        if self.new_ball:
+            position = len(self.balls)
+            ball = Ball(self.start_x, self.start_y, self.radius, '', position)
+            self.balls.append(ball)
+            self.model.append([position, ''])
+            self.treeview.set_cursor(str(position), self.fpcolumn, True)
+            self.new_ball = False
+
+        # 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()