X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=gzv.py;h=081e06a181527f2dd877884ffa52be3a067635db;hb=bbfc3969346db1b11309f60cc72c979258c26bf3;hp=78f3929a80ca79281664280519e6dfad998cb185;hpb=2447321fd08b4a8dad396e3317f63f4614c147d6;p=cascardo%2Fmovie.git diff --git a/gzv.py b/gzv.py index 78f3929..081e06a 100644 --- a/gzv.py +++ b/gzv.py @@ -12,10 +12,13 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. +import os import gtk import gtk.glade +import gobject import math import cairo +from ConfigParser import ConfigParser _ = lambda x:x @@ -24,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.radios = r + self.x = int(x) + self.y = int(y) + self.radius = int(r) self.name = name class BallManager(list): @@ -36,7 +39,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): @@ -66,11 +69,59 @@ class GladeLoader(object): def gtk_main(self, *args): gtk.main() +class Project(object): + def __init__(self, image, width, height): + self.image = image + self.width = width + self.height = height + self.focus_points_file = '' + + def save_to_file(self, path): + 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) + cp.set('Project', 'focus_points', self.focus_points_file) + + 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): super(NewProject, self).__init__('gzv.glade', 'new-project') + self.dialog = self.wid('new-project') if parent: - self.wid('new-project').set_transient_for(parent) + self.dialog.set_transient_for(parent) + + def get_project(self): + fname = self.wid('image').get_filename() + width = self.wid('width').get_text() + height = self.wid('height').get_text() + return Project(fname, width, height) + + def destroy(self): + self.dialog.destroy() class Gzv(GladeLoader): def __init__(self): @@ -90,77 +141,158 @@ 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() - self.ball_width = Ball.DEFAULT_WIDTH + # drawing stuff 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): - dialog = NewProject(self.window).wid('new-project') + proj = NewProject(self.window) # This '1' was defined in the glade file - if dialog.run() == 1: - pass - dialog.destroy() + if proj.dialog.run() == 1: + 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 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) + self.image = project.image + 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() + if not os.path.exists(fname): + return balls + 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 # 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() @@ -185,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.radios, 0, 64*math.pi) - - ctx.fill() - ctx.stroke() + ctx.arc(i.x, i.y, i.radius, 0, 64*math.pi) + 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.ball_width, 0, 64*math.pi) - ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4) + 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() def button_press(self, widget, event): @@ -217,9 +347,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 @@ -227,6 +357,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()