Saving balls added in the image and making it possible
[cascardo/movie.git] / gzv.py
diff --git a/gzv.py b/gzv.py
index 81b88d9..47cd87e 100644 (file)
--- a/gzv.py
+++ b/gzv.py
@@ -26,9 +26,9 @@ class Ball(object):
 
     def __init__(self, x, y, r, name='', position=0):
         self.position = position
-        self.x = x
-        self.y = y
-        self.radios = r
+        self.x = int(x)
+        self.y = int(y)
+        self.radius = int(r)
         self.name = name
 
 class BallManager(list):
@@ -38,7 +38,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.radios, i.name))
+            target.write('%d,%d %d %s\n' % (i.x, i.y, i.radius, i.name))
         target.close()
 
 class GladeLoader(object):
@@ -73,7 +73,7 @@ class Project(object):
         self.image = image
         self.width = width
         self.height = height
-        self.focus_points_file = None
+        self.focus_points_file = ''
 
     def save_to_file(self, path):
         bn = os.path.basename(path)
@@ -83,6 +83,7 @@ class Project(object):
         cp.set('Project', 'image', self.image)
         cp.set('Project', 'width', self.width)
         cp.set('Project', 'height', self.height)
+        cp.set('Project', 'focus_points', self.focus_points_file)
         
         cp.write(open(path, 'w'))
 
@@ -146,29 +147,32 @@ class Gzv(GladeLoader):
         self.setup_treeview()
 
         # drawing stuff
-        self.ball_width = Ball.DEFAULT_WIDTH
         self.selecting = False
         self.start_x = -1
         self.start_y = -1
+        self.radius = Ball.DEFAULT_WIDTH
 
     def setup_treeview(self):
         self.model.connect('rows-reordered', self.on_rows_reordered)
 
         renderer = gtk.CellRendererText()
         column = gtk.TreeViewColumn(_('Position'), renderer, text=0)
+        column.set_property('visible', False)
         self.treeview.append_column(column)
 
         renderer = gtk.CellRendererText()
         renderer.connect('edited', self.on_cell_edited)
         renderer.set_property('editable', True)
-        column = gtk.TreeViewColumn(_('Name'), renderer, text=1)
-        self.treeview.append_column(column)
+        self.fpcolumn = gtk.TreeViewColumn(_('Name'), renderer, text=1)
+        self.treeview.append_column(self.fpcolumn)
 
     def on_rows_reordered(self, *args):
         print 
 
-    def on_cell_edited(self, *args):
-        print args
+    def on_cell_edited(self, renderer, path, value):
+        self.balls[int(path)].name = value
+        self.load_balls_to_treeview()
+        self.draw.queue_draw()
 
     def new_project(self, button):
         proj = NewProject(self.window)
@@ -188,14 +192,16 @@ class Gzv(GladeLoader):
         fc.destroy()
 
     def load_project(self, project):
+        self.project = project
         self.balls = self.load_balls_from_file(project.focus_points_file)
         self.image = project.image
         self.load_balls_to_treeview()
+        self.draw.queue_draw()
 
     def load_balls_to_treeview(self):
-        model = self.treeview.get_model()
+        self.model.clear()
         for i in self.balls:
-            model.append([i.position, i.name])
+            self.model.append([i.position, i.name])
 
     def load_balls_from_file(self, fname):
         balls = BallManager()
@@ -205,11 +211,43 @@ class Gzv(GladeLoader):
         for index, line in enumerate(file(fname)):
             if not line:
                 continue
-            pos, radios, name = line.split()
+            pos, radius, name = line.split(None, 2)
             x, y = pos.split(',')
-            balls.append(Ball(int(x), int(y), int(radios), name, index))
+            balls.append(Ball(x, y, radius, name.strip(), index))
         return balls
 
+    def remove_fp(self, *args):
+        selection = self.treeview.get_selection()
+        model, path = selection.get_selected()
+        if path:
+            position = model[path][0]
+            for i in self.balls:
+                if i.position == int(position):
+                    self.balls.remove(i)
+            del model[path]
+            self.draw.queue_draw()
+
+    def save_fp_list(self, *args):
+        assert self.project is not None
+
+        # if the project has no
+        if self.project and not self.project.focus_points_file:
+            fc = gtk.FileChooserDialog(_('Save the focus points file'),
+                                       self.window,
+                                       action=gtk.FILE_CHOOSER_ACTION_SAVE,
+                                       buttons=(gtk.STOCK_CANCEL,
+                                                gtk.RESPONSE_CANCEL,
+                                                gtk.STOCK_SAVE,
+                                                gtk.RESPONSE_OK))
+            if fc.run() == gtk.RESPONSE_OK:
+                self.project.focus_points_file = fc.get_filename()
+                fc.destroy()
+            else:
+                fc.destroy()
+                return
+
+        self.balls.save_to_file(self.project.focus_points_file)
+
     def expose_draw(self, draw, event):
         if not self.image:
             return
@@ -244,7 +282,7 @@ class Gzv(GladeLoader):
         ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
 
         for i in self.balls:
-            ctx.arc(i.x, i.y, i.radios, 0, 64*math.pi)
+            ctx.arc(i.x, i.y, i.radius, 0, 64*math.pi)
 
         ctx.fill()
         ctx.stroke()
@@ -253,7 +291,7 @@ class Gzv(GladeLoader):
         if self.start_x < 0:
             return
         ctx = self.draw.window.cairo_create()
-        ctx.arc(self.start_x, self.start_y, self.ball_width, 0, 64*math.pi)
+        ctx.arc(self.start_x, self.start_y, self.radius, 0, 64*math.pi)
         ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
         ctx.fill()
 
@@ -273,9 +311,9 @@ class Gzv(GladeLoader):
         self.draw.queue_draw()
 
         if event.x > self.last_x:
-            self.ball_width += 2
+            self.radius += 3
         else:
-            self.ball_width -= 2
+            self.radius -= 3
 
         self.last_x = event.x
 
@@ -283,6 +321,12 @@ class Gzv(GladeLoader):
         self.draw_current_ball()
         self.ball_width = Ball.DEFAULT_WIDTH
 
+        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 __name__ == '__main__':
     Gzv().window.show_all()
     gtk.main()