X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=gzv.py;h=081e06a181527f2dd877884ffa52be3a067635db;hb=bbfc3969346db1b11309f60cc72c979258c26bf3;hp=2149bd08f9070df158795ccdd616ac4cd81dbbec;hpb=03139b816432120f5ee9f133946c6bc24d02834f;p=cascardo%2Fmovie.git diff --git a/gzv.py b/gzv.py index 2149bd0..081e06a 100644 --- a/gzv.py +++ b/gzv.py @@ -15,6 +15,7 @@ import os import gtk import gtk.glade +import gobject import math import cairo from ConfigParser import ConfigParser @@ -26,9 +27,9 @@ class Ball(object): def __init__(self, x, y, r, name='', position=0): self.position = position - self.x = x - self.y = y - self.radius = r + self.x = int(x) + self.y = int(y) + self.radius = int(r) self.name = name class BallManager(list): @@ -76,10 +77,14 @@ class Project(object): self.focus_points_file = '' def save_to_file(self, path): - bn = os.path.basename(path) - name = os.path.splitext(bn)[0] + if not self.focus_points_file: + bn = os.path.basename(path) + name = os.path.splitext(bn)[0] + self.focus_points_file = \ + os.path.join(os.path.dirname(path), name + '_fpf') cp = ConfigParser() + cp.add_section('Project') cp.set('Project', 'image', self.image) cp.set('Project', 'width', self.width) cp.set('Project', 'height', self.height) @@ -170,8 +175,7 @@ class Gzv(GladeLoader): print def on_cell_edited(self, renderer, path, value): - ball = Ball(self.start_x, self.start_y, self.radius, value, int(path)) - self.balls.append(ball) + self.balls[int(path)].name = value self.load_balls_to_treeview() self.draw.queue_draw() @@ -192,6 +196,19 @@ class Gzv(GladeLoader): self.load_project(Project.parse_file(proj_file)) fc.destroy() + def save_project(self, *args): + fc = gtk.FileChooserDialog(_('Save project'), 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.save_to_file(fc.get_filename()) + 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) @@ -199,10 +216,16 @@ class Gzv(GladeLoader): self.load_balls_to_treeview() self.draw.queue_draw() + def unload_project(self): + self.project = None + self.image = None + self.balls = BallManager() + 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() @@ -212,9 +235,9 @@ class Gzv(GladeLoader): for index, line in enumerate(file(fname)): if not line: continue - pos, radius, name = line.split() + pos, radius, name = line.split(None, 2) x, y = pos.split(',') - balls.append(Ball(int(x), int(y), int(radius), name, index)) + balls.append(Ball(x, y, radius, name.strip(), index)) return balls def remove_fp(self, *args): @@ -255,7 +278,21 @@ class Gzv(GladeLoader): # loading the picture image and getting some useful # information to draw it in the widget's background - img = gtk.gdk.pixbuf_new_from_file(self.image) + try: + img = gtk.gdk.pixbuf_new_from_file(self.image) + except gobject.GError: + msg = _("Couldn't recognize the image file format.") + dialog = gtk.MessageDialog(self.window, + gtk.DIALOG_MODAL, + gtk.MESSAGE_ERROR, + gtk.BUTTONS_CLOSE) + dialog.set_markup(msg) + dialog.run() + dialog.destroy() + + self.draw.stop_emission('expose-event') + return self.unload_project() + pixels = img.get_pixels() rowstride = img.get_rowstride() width = img.get_width() @@ -280,20 +317,18 @@ class Gzv(GladeLoader): ctx.fill() ctx.set_line_width(10.0) - ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4) + ctx.set_source_rgba(0.5, 0.0, 0.0, 0.4) for i in self.balls: ctx.arc(i.x, i.y, i.radius, 0, 64*math.pi) - - ctx.fill() - ctx.stroke() + ctx.fill() def draw_current_ball(self): if self.start_x < 0: return ctx = self.draw.window.cairo_create() 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.set_source_rgba(0.5, 0.0, 0.0, 0.4) ctx.fill() def button_press(self, widget, event): @@ -323,6 +358,8 @@ class Gzv(GladeLoader): 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)