Implementing the "Open Project" feature, adding
authorLincoln de Sousa <lincoln@minaslivre.org>
Sat, 9 Aug 2008 19:51:44 +0000 (16:51 -0300)
committerLincoln de Sousa <lincoln@minaslivre.org>
Sat, 9 Aug 2008 19:51:44 +0000 (16:51 -0300)
Project.save_to_file, Project.parse_file, Gzv.load_project and changing Gzv.load_balls_from_file,
Gzv.open_project and Gzv.new_project

gzv.py

diff --git a/gzv.py b/gzv.py
index 2bee2ff..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):
@@ -146,7 +175,7 @@ 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_project(self, *args):
@@ -155,10 +184,14 @@ class Gzv(GladeLoader):
                                             gtk.STOCK_OK, gtk.RESPONSE_OK))
         if fc.run() == gtk.RESPONSE_OK:
             proj_file = fc.get_filename()
-            self.balls = self.load_balls_from_file(proj_file)
-
+            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:
@@ -166,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