Implementing the "Open Project" feature, adding
[cascardo/movie.git] / gzv.py
diff --git a/gzv.py b/gzv.py
index 318a0f8..81b88d9 100644 (file)
--- a/gzv.py
+++ b/gzv.py
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 
+import os
 import gtk
 import gtk.glade
 import math
 import cairo
+from ConfigParser import ConfigParser
 
 _ = lambda x:x
 
@@ -71,6 +73,33 @@ class Project(object):
         self.image = image
         self.width = width
         self.height = height
+        self.focus_points_file = None
+
+    def save_to_file(self, path):
+        bn = os.path.basename(path)
+        name = os.path.splitext(bn)[0]
+
+        cp = ConfigParser()
+        cp.set('Project', 'image', self.image)
+        cp.set('Project', 'width', self.width)
+        cp.set('Project', 'height', self.height)
+        
+        cp.write(open(path, 'w'))
+
+    @staticmethod
+    def parse_file(path):
+        cp = ConfigParser()
+        cp.read(path)
+
+        image = cp.get('Project', 'image')
+        width = cp.getint('Project', 'width')
+        height = cp.getint('Project', 'height')
+        x = cp.getint('Project', 'height')
+
+        proj = Project(image, width, height)
+        proj.focus_points_file = cp.get('Project', 'focus_points')
+
+        return proj
 
 class NewProject(GladeLoader):
     def __init__(self, parent=None):
@@ -106,14 +135,17 @@ class Gzv(GladeLoader):
         self.draw = self.wid('draw')
         self.draw.connect('expose-event', self.expose_draw)
 
-        # FIXME: Hardcoded.
-        self.image = 'skol.jpg'
-        self.balls = self.load_balls_from_file('xxx')
-        self.load_balls_to_treeview()
+        # Starting with an empty project with no image loaded
+        self.project = None
+        self.image = None
+
+        # This attr may be overriten, if so, call the method (load_balls_to_treeview)
+        self.balls = BallManager()
 
-        # this *MUST* be called *AFTER* load_balls_to_treeview
+        self.load_balls_to_treeview()
         self.setup_treeview()
 
+        # drawing stuff
         self.ball_width = Ball.DEFAULT_WIDTH
         self.selecting = False
         self.start_x = -1
@@ -143,18 +175,23 @@ class Gzv(GladeLoader):
 
         # This '1' was defined in the glade file
         if proj.dialog.run() == 1:
-            self.load_new_project(proj.get_project())
+            self.load_project(proj.get_project())
         proj.destroy()
 
-    def open_file_chooser(self, button):
+    def open_project(self, *args):
         fc = gtk.FileChooserDialog(_('Choose a gzv project'), self.window,
                                    buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                             gtk.STOCK_OK, gtk.RESPONSE_OK))
         if fc.run() == gtk.RESPONSE_OK:
-            self.image = fc.get_filename()
-
+            proj_file = fc.get_filename()
+            self.load_project(Project.parse_file(proj_file))
         fc.destroy()
 
+    def load_project(self, project):
+        self.balls = self.load_balls_from_file(project.focus_points_file)
+        self.image = project.image
+        self.load_balls_to_treeview()
+
     def load_balls_to_treeview(self):
         model = self.treeview.get_model()
         for i in self.balls:
@@ -162,6 +199,9 @@ class Gzv(GladeLoader):
 
     def load_balls_from_file(self, fname):
         balls = BallManager()
+        if not os.path.exists(fname):
+            return balls
+
         for index, line in enumerate(file(fname)):
             if not line:
                 continue