X-Git-Url: http://git.cascardo.info/?a=blobdiff_plain;f=gzv.py;h=ca94af99cadf32f988d20e80bd1470fd997fd14e;hb=7c88a521007395559dff443beec71f4c1fcc28c2;hp=2d5c926d5be59d8c214d88a09d853cfb01540652;hpb=616c4a99ce887e5b48427e71cb60d676fe147446;p=cascardo%2Fmovie.git diff --git a/gzv.py b/gzv.py index 2d5c926..ca94af9 100644 --- a/gzv.py +++ b/gzv.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8; -*- # gzv.py - an user interface to generate-zooming-video # # Copyright (C) 2008 Lincoln de Sousa @@ -15,20 +16,30 @@ import os import gtk import gtk.glade +import gobject import math import cairo from ConfigParser import ConfigParser _ = lambda x:x +class Point(object): + def __init__(self, x, y): + self.x = x + self.y = y + + @staticmethod + def pythagorean(p1, p2): + return math.sqrt((p2.x - p1.x)**2 + (p2.y - p1.y)**2) + class Ball(object): DEFAULT_WIDTH = 10 - def __init__(self, x, y, r, name='', position=0): + def __init__(self, x, y, r, name='', position=0, selected=False): self.position = position - self.x = x - self.y = y - self.radios = r + self.selected = selected + self.p = Point(int(x), int(y)) + self.radius = int(r) self.name = name class BallManager(list): @@ -38,7 +49,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.p.x, i.p.y, i.radius, i.name)) target.close() class GladeLoader(object): @@ -76,10 +87,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) @@ -110,6 +125,10 @@ class NewProject(GladeLoader): self.dialog.set_transient_for(parent) def get_project(self): + # This '1' was defined in the glade file + if not self.dialog.run() == 1: + return None + fname = self.wid('image').get_filename() width = self.wid('width').get_text() height = self.wid('height').get_text() @@ -128,13 +147,19 @@ class Gzv(GladeLoader): self.evtbox.connect('button-press-event', self.button_press) self.evtbox.connect('button-release-event', self.button_release) self.evtbox.connect('motion-notify-event', self.motion_notify) + self.evtbox.connect('motion-notify-event', self.ball_motion) + + # making it possible to grab motion events when the mouse is + # over the widget. + self.evtbox.set_events(gtk.gdk.POINTER_MOTION_MASK) self.model = gtk.ListStore(int, str) self.treeview = self.wid('treeview') self.treeview.set_model(self.model) + self.treeview.connect('button-press-event', self.select_fp) self.draw = self.wid('draw') - self.draw.connect('expose-event', self.expose_draw) + self.draw.connect_after('expose-event', self.expose_draw) # Starting with an empty project with no image loaded self.project = None @@ -146,11 +171,18 @@ class Gzv(GladeLoader): self.load_balls_to_treeview() self.setup_treeview() + self.new_ball = False + self.move_ball = None + # drawing stuff - self.ball_width = Ball.DEFAULT_WIDTH - self.selecting = False self.start_x = -1 self.start_y = -1 + self.last_x = -1 + self.last_y = -1 + self.radius = Ball.DEFAULT_WIDTH + + def show(self): + self.window.show_all() def setup_treeview(self): self.model.connect('rows-reordered', self.on_rows_reordered) @@ -163,23 +195,24 @@ class Gzv(GladeLoader): 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() def new_project(self, button): proj = NewProject(self.window) - - # This '1' was defined in the glade file - if proj.dialog.run() == 1: - self.load_project(proj.get_project()) + project = proj.get_project() proj.destroy() + if project: + self.load_project(project) + def open_project(self, *args): fc = gtk.FileChooserDialog(_('Choose a gzv project'), self.window, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, @@ -189,17 +222,52 @@ 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) self.image = project.image + + # I'm loading a pixbuf first because I need to get its + # dimensions this with a pixbuf is easier than with an image. + try: + pixbuf = gtk.gdk.pixbuf_new_from_file(project.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() + return self.unload_project() + + self.draw.set_from_pixbuf(pixbuf) self.load_balls_to_treeview() + + 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() @@ -209,9 +277,9 @@ 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): @@ -223,6 +291,21 @@ class Gzv(GladeLoader): if i.position == int(position): self.balls.remove(i) del model[path] + self.draw.queue_draw() + + def select_fp(self, treeview, event): + path, column, x, y = \ + self.treeview.get_path_at_pos(int(event.x), int(event.y)) + if path: + model = self.treeview.get_model() + ball = self.balls[model[path][0]] + + # making sure that only one ball is selected + for i in self.balls: + i.selected = False + ball.selected = True + + self.draw.queue_draw() def save_fp_list(self, *args): assert self.project is not None @@ -249,75 +332,96 @@ class Gzv(GladeLoader): 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) - pixels = img.get_pixels() - rowstride = img.get_rowstride() - width = img.get_width() - height = img.get_height() - gc = draw.style.black_gc - - # sets the correct size of the eventbox, to show the scrollbar - # when needed. - self.evtbox.set_size_request(width, height) - - # drawing the picture in the background of the drawing area, - # this is really important. - draw.window.draw_rgb_image(gc, 0, 0, width, height, - 'normal', pixels, rowstride, - 0, 0) - - # this call makes the ball being drown be shown correctly. - self.draw_current_ball() - - # drawing other balls stored in the self.balls list. - ctx = draw.window.cairo_create() - ctx.fill() + for i in self.balls: + self.draw_ball(i) - ctx.set_line_width(10.0) - ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4) + if self.start_x < 0: + return False - for i in self.balls: - ctx.arc(i.x, i.y, i.radios, 0, 64*math.pi) + if self.new_ball: + ball = Ball(self.start_x, self.start_y, self.radius) + self.draw_ball(ball) - ctx.fill() - ctx.stroke() + return False - def draw_current_ball(self): - if self.start_x < 0: - return + def draw_ball(self, ball): 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(ball.p.x, ball.p.y, ball.radius, 0, 64*math.pi) + ctx.set_source_rgba(0.0, 0.0, 0.5, 0.4) ctx.fill() + if ball.selected: + ctx.set_source_rgba(0.0, 0.5, 0.0, 0.4) + ctx.set_line_width(5) + ctx.arc(ball.p.x, ball.p.y, ball.radius+1, 0, 64*math.pi) + ctx.stroke() + def button_press(self, widget, event): + self.new_ball = True + + self.last_x = event.x + self.last_y = event.y + if event.button == 1: - self.selecting = True + for i in self.balls: + p1 = Point(event.x, event.y) + p2 = Point(i.p.x, i.p.y) + if Point.pythagorean(p1, p2) < i.radius: + self.last_x = event.x - i.p.x + self.last_y = event.y - i.p.y + + self.new_ball = False + self.move_ball = i + break + self.start_x = event.x self.start_y = event.y - self.last_x = event.x def button_release(self, widget, event): + self.move_ball = None + if event.button == 1: - self.selecting = False self.finish_drawing() def motion_notify(self, widget, event): + if not self.new_ball: + return + self.draw.queue_draw() if event.x > self.last_x: - self.ball_width += 3 + self.radius += 3 else: - self.ball_width -= 3 + self.radius -= 3 self.last_x = event.x + def ball_motion(self, widget, event): + if not self.move_ball: + return + + self.move_ball.p.x += (event.x - self.move_ball.p.x) + self.move_ball.p.y += (event.y - self.move_ball.p.y) + + self.draw.queue_draw() + + self.last_x = event.x - self.move_ball.p.x + self.last_y = event.y - self.move_ball.p.y + def finish_drawing(self): - self.draw_current_ball() - self.ball_width = Ball.DEFAULT_WIDTH + if self.new_ball: + 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) + self.new_ball = False + + # reseting to the default coordenades + self.start_x = -1 + self.start_y = -1 + self.radius = Ball.DEFAULT_WIDTH if __name__ == '__main__': - Gzv().window.show_all() + Gzv().show() gtk.main()