If picture does no exist, warn and exit
[cascardo/movie.git] / gzv.py
diff --git a/gzv.py b/gzv.py
index 752c117..ac4dce3 100644 (file)
--- a/gzv.py
+++ b/gzv.py
@@ -1,4 +1,5 @@
-# gzv.py - an user interface to generate-zooming-video
+# -*- coding: utf-8; -*-
+# gzv.py - an user interface to select people in a picture
 #
 # Copyright (C) 2008  Lincoln de Sousa <lincoln@minaslivre.org>
 #
@@ -22,14 +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, selected=False):
         self.position = position
         self.selected = selected
-        self.x = int(x)
-        self.y = int(y)
+        self.p = Point(int(x), int(y))
         self.radius = int(r)
         self.name = name
 
@@ -40,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):
@@ -138,6 +147,7 @@ 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.
@@ -162,11 +172,13 @@ class Gzv(GladeLoader):
         self.setup_treeview()
 
         self.new_ball = False
+        self.move_ball = None
 
         # drawing stuff
         self.start_x = -1
         self.start_y = -1
         self.last_x = -1
+        self.last_y = -1
         self.radius = Ball.DEFAULT_WIDTH
 
     def show(self):
@@ -222,7 +234,6 @@ class Gzv(GladeLoader):
             self.balls.save_to_file(self.project.focus_points_file)
         fc.destroy()
 
-
     def load_project(self, project):
         self.project = project
         self.balls = self.load_balls_from_file(project.focus_points_file)
@@ -245,12 +256,19 @@ class Gzv(GladeLoader):
 
         self.draw.set_from_pixbuf(pixbuf)
         self.load_balls_to_treeview()
+        self.set_widgets_sensitivity(True)
 
     def unload_project(self):
         self.project = None
         self.image = None
         self.balls = BallManager()
         self.draw.queue_draw()
+        self.set_widgets_sensitivity(False)
+
+    def set_widgets_sensitivity(self, sensitive):
+        for i in 'toolbutton1', 'toolbutton5', 'scrolledwindow1', \
+                'hbox2', 'imagemenuitem3':
+            self.wid(i).set_sensitive(sensitive)
 
     def load_balls_to_treeview(self):
         self.model.clear()
@@ -293,8 +311,29 @@ class Gzv(GladeLoader):
                 i.selected = False
             ball.selected = True
 
+            # available space to the image
+            w = self.evtbox.get_allocation().width
+            h = self.evtbox.get_allocation().height
+
+            # point begining from the left image border
+            wib = self.point_with_border(ball)
+
+            #self.wid('viewport').get_vadjustment().value = wib.x # + (w / 2)
+            #self.wid('viewport').get_hadjustment().value = wib.y # + (h / 2)
+
             self.draw.queue_draw()
 
+    def select_fp_from_image(self, ball):
+        selection = self.treeview.get_selection()
+        selection.select_path(str(ball.position))
+
+        # 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
 
@@ -316,6 +355,40 @@ class Gzv(GladeLoader):
 
         self.balls.save_to_file(self.project.focus_points_file)
 
+    def move_fp_up(self, *args):
+        selection = self.treeview.get_selection()
+        model, path = selection.get_selected()
+        if not path:
+            return
+
+        pos = model[path][0]
+        newpos = max(pos - 1, 0)
+        self.balls.insert(newpos, self.balls.pop(pos))
+
+        # normalizing the position of elements.
+        for index, item in enumerate(self.balls):
+            item.position = index
+
+        self.load_balls_to_treeview()
+        selection.select_path(str(newpos))
+
+    def move_fp_down(self, *args):
+        selection = self.treeview.get_selection()
+        model, path = selection.get_selected()
+        if not path:
+            return
+
+        pos = model[path][0]
+        newpos = min(pos + 1, len(self.balls))
+        self.balls.insert(newpos, self.balls.pop(pos))
+
+        # normalizing the position of elements.
+        for index, item in enumerate(self.balls):
+            item.position = index
+
+        self.load_balls_to_treeview()
+        selection.select_path(str(newpos))
+
     def expose_draw(self, draw, event):
         if not self.image:
             return
@@ -326,33 +399,70 @@ class Gzv(GladeLoader):
         if self.start_x < 0:
             return False
 
-        ball = Ball(self.start_x, self.start_y, self.radius)
-        self.draw_ball(ball)
+        if self.new_ball:
+            ball = Ball(self.start_x, self.start_y, self.radius)
+            self.draw_ball(ball)
 
         return False
 
+    def point_with_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.arc(self.point_with_border(ball).x,
+                self.point_with_border(ball).y,
+                ball.radius, 0, 64*math.pi)
         ctx.set_source_rgba(0.0, 0.0, 0.5, 0.4)
         ctx.fill()
 
         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.arc(self.point_with_border(ball).x,
+                    self.point_with_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.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.point_with_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.select_fp_from_image(i)
+
+                    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.new_ball = False
+        self.move_ball = None
 
         if event.button == 1:
             self.finish_drawing()
@@ -370,12 +480,23 @@ class Gzv(GladeLoader):
 
         self.last_x = event.x
 
+    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
+
+        self.draw.queue_draw()
+
     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)
+        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