1 # -*- coding: utf-8; -*-
2 # gzv.py - an user interface to generate-zooming-video
4 # Copyright (C) 2008 Lincoln de Sousa <lincoln@minaslivre.org>
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
22 from ConfigParser import ConfigParser
27 def __init__(self, x, y):
32 def pythagorean(p1, p2):
33 return math.sqrt((p2.x - p1.x)**2 + (p2.y - p1.y)**2)
38 def __init__(self, x, y, r, name='', position=0, selected=False):
39 self.position = position
40 self.selected = selected
41 self.p = Point(int(x), int(y))
45 class BallManager(list):
46 def __init__(self, *args, **kwargs):
47 super(BallManager, self).__init__(*args, **kwargs)
49 def save_to_file(self, path):
50 target = open(path, 'w')
52 target.write('%d,%d %d %s\n' % (i.p.x, i.p.y, i.radius, i.name))
55 class GladeLoader(object):
56 def __init__(self, fname, root=''):
57 self.ui = gtk.glade.XML(fname, root)
58 self.ui.signal_autoconnect(self)
60 def get_widget(self, wname):
61 return self.ui.get_widget(wname)
68 def gtk_widget_show(self, widget, *args):
72 def gtk_widget_hide(self, widget, *args):
76 def gtk_main_quit(self, *args):
79 def gtk_main(self, *args):
82 class Project(object):
83 def __init__(self, image, width, height):
87 self.focus_points_file = ''
89 def save_to_file(self, path):
90 if not self.focus_points_file:
91 bn = os.path.basename(path)
92 name = os.path.splitext(bn)[0]
93 self.focus_points_file = \
94 os.path.join(os.path.dirname(path), name + '_fpf')
97 cp.add_section('Project')
98 cp.set('Project', 'image', self.image)
99 cp.set('Project', 'width', self.width)
100 cp.set('Project', 'height', self.height)
101 cp.set('Project', 'focus_points', self.focus_points_file)
103 cp.write(open(path, 'w'))
106 def parse_file(path):
110 image = cp.get('Project', 'image')
111 width = cp.getint('Project', 'width')
112 height = cp.getint('Project', 'height')
113 x = cp.getint('Project', 'height')
115 proj = Project(image, width, height)
116 proj.focus_points_file = cp.get('Project', 'focus_points')
120 class NewProject(GladeLoader):
121 def __init__(self, parent=None):
122 super(NewProject, self).__init__('gzv.glade', 'new-project')
123 self.dialog = self.wid('new-project')
125 self.dialog.set_transient_for(parent)
127 def get_project(self):
128 # This '1' was defined in the glade file
129 if not self.dialog.run() == 1:
132 fname = self.wid('image').get_filename()
133 width = self.wid('width').get_text()
134 height = self.wid('height').get_text()
135 return Project(fname, width, height)
138 self.dialog.destroy()
140 class Gzv(GladeLoader):
142 super(Gzv, self).__init__('gzv.glade', 'main-window')
143 self.window = self.wid('main-window')
144 self.window.connect('delete-event', lambda *x: gtk.main_quit())
146 self.evtbox = self.wid('eventbox')
147 self.evtbox.connect('button-press-event', self.button_press)
148 self.evtbox.connect('button-release-event', self.button_release)
149 self.evtbox.connect('motion-notify-event', self.motion_notify)
150 self.evtbox.connect('motion-notify-event', self.ball_motion)
152 # making it possible to grab motion events when the mouse is
154 self.evtbox.set_events(gtk.gdk.POINTER_MOTION_MASK)
156 self.model = gtk.ListStore(int, str)
157 self.treeview = self.wid('treeview')
158 self.treeview.set_model(self.model)
159 self.treeview.connect('button-press-event', self.select_fp)
161 self.draw = self.wid('draw')
162 self.draw.connect_after('expose-event', self.expose_draw)
164 # Starting with an empty project with no image loaded
168 # This attr may be overriten, if so, call the method (load_balls_to_treeview)
169 self.balls = BallManager()
171 self.load_balls_to_treeview()
172 self.setup_treeview()
174 self.new_ball = False
175 self.move_ball = None
182 self.radius = Ball.DEFAULT_WIDTH
185 self.window.show_all()
187 def setup_treeview(self):
188 self.model.connect('rows-reordered', self.on_rows_reordered)
190 renderer = gtk.CellRendererText()
191 column = gtk.TreeViewColumn(_('Position'), renderer, text=0)
192 column.set_property('visible', False)
193 self.treeview.append_column(column)
195 renderer = gtk.CellRendererText()
196 renderer.connect('edited', self.on_cell_edited)
197 renderer.set_property('editable', True)
198 self.fpcolumn = gtk.TreeViewColumn(_('Name'), renderer, text=1)
199 self.treeview.append_column(self.fpcolumn)
201 def on_rows_reordered(self, *args):
204 def on_cell_edited(self, renderer, path, value):
205 self.balls[int(path)].name = value
206 self.load_balls_to_treeview()
208 def new_project(self, button):
209 proj = NewProject(self.window)
210 project = proj.get_project()
214 self.load_project(project)
216 def open_project(self, *args):
217 fc = gtk.FileChooserDialog(_('Choose a gzv project'), self.window,
218 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
219 gtk.STOCK_OK, gtk.RESPONSE_OK))
220 if fc.run() == gtk.RESPONSE_OK:
221 proj_file = fc.get_filename()
222 self.load_project(Project.parse_file(proj_file))
225 def save_project(self, *args):
226 fc = gtk.FileChooserDialog(_('Save project'), self.window,
227 action=gtk.FILE_CHOOSER_ACTION_SAVE,
228 buttons=(gtk.STOCK_CANCEL,
232 if fc.run() == gtk.RESPONSE_OK:
233 self.project.save_to_file(fc.get_filename())
234 self.balls.save_to_file(self.project.focus_points_file)
238 def load_project(self, project):
239 self.project = project
240 self.balls = self.load_balls_from_file(project.focus_points_file)
241 self.image = project.image
243 # I'm loading a pixbuf first because I need to get its
244 # dimensions this with a pixbuf is easier than with an image.
246 pixbuf = gtk.gdk.pixbuf_new_from_file(project.image)
247 except gobject.GError:
248 msg = _("Couldn't recognize the image file format.")
249 dialog = gtk.MessageDialog(self.window,
253 dialog.set_markup(msg)
256 return self.unload_project()
258 self.draw.set_from_pixbuf(pixbuf)
259 self.load_balls_to_treeview()
261 def unload_project(self):
264 self.balls = BallManager()
265 self.draw.queue_draw()
267 def load_balls_to_treeview(self):
270 self.model.append([i.position, i.name])
272 def load_balls_from_file(self, fname):
273 balls = BallManager()
274 if not os.path.exists(fname):
277 for index, line in enumerate(file(fname)):
280 pos, radius, name = line.split(None, 2)
281 x, y = pos.split(',')
282 balls.append(Ball(x, y, radius, name.strip(), index))
285 def remove_fp(self, *args):
286 selection = self.treeview.get_selection()
287 model, path = selection.get_selected()
289 position = model[path][0]
291 if i.position == int(position):
294 self.draw.queue_draw()
296 def select_fp(self, treeview, event):
297 path, column, x, y = \
298 self.treeview.get_path_at_pos(int(event.x), int(event.y))
300 model = self.treeview.get_model()
301 ball = self.balls[model[path][0]]
303 # making sure that only one ball is selected
308 self.draw.queue_draw()
310 def select_fp_from_image(self, ball):
311 selection = self.treeview.get_selection()
312 selection.select_path(str(ball.position))
314 # making sure that only one ball is selected
319 self.draw.queue_draw()
321 def save_fp_list(self, *args):
322 assert self.project is not None
324 # if the project has no
325 if self.project and not self.project.focus_points_file:
326 fc = gtk.FileChooserDialog(_('Save the focus points file'),
328 action=gtk.FILE_CHOOSER_ACTION_SAVE,
329 buttons=(gtk.STOCK_CANCEL,
333 if fc.run() == gtk.RESPONSE_OK:
334 self.project.focus_points_file = fc.get_filename()
340 self.balls.save_to_file(self.project.focus_points_file)
342 def expose_draw(self, draw, event):
353 ball = Ball(self.start_x, self.start_y, self.radius)
358 def ball_width_border(self, ball):
359 iw, ih = self.draw.size_request()
360 w = self.draw.get_allocation().width
361 h = self.draw.get_allocation().height
363 x = ((w / 2) - (iw / 2)) + ball.p.x
364 y = ((h / 2) - (ih / 2)) + ball.p.y
367 def point_without_border(self, point):
368 iw, ih = self.draw.size_request()
369 w = self.draw.get_allocation().width
370 h = self.draw.get_allocation().height
372 x = point.x - ((w / 2) - (iw / 2))
373 y = point.y - ((h / 2) - (ih / 2))
376 def draw_ball(self, ball):
377 ctx = self.draw.window.cairo_create()
378 ctx.arc(self.ball_width_border(ball).x,
379 self.ball_width_border(ball).y,
380 ball.radius, 0, 64*math.pi)
381 ctx.set_source_rgba(0.0, 0.0, 0.5, 0.4)
385 ctx.set_source_rgba(0.0, 0.5, 0.0, 0.4)
386 ctx.set_line_width(5)
387 ctx.arc(self.ball_width_border(ball).x,
388 self.ball_width_border(ball).y,
389 ball.radius+1, 0, 64*math.pi)
392 def button_press(self, widget, event):
395 self.last_x = event.x
396 self.last_y = event.y
398 if event.button == 1:
400 p1 = Point(event.x, event.y)
401 p2 = self.ball_width_border(i)
402 if Point.pythagorean(p1, p2) < i.radius:
403 self.last_x = event.x - i.p.x
404 self.last_y = event.y - i.p.y
405 self.select_fp_from_image(i)
407 self.new_ball = False
411 self.start_x = self.point_without_border(event).x
412 self.start_y = self.point_without_border(event).y
414 def button_release(self, widget, event):
415 self.move_ball = None
417 if event.button == 1:
418 self.finish_drawing()
420 def motion_notify(self, widget, event):
421 if not self.new_ball:
424 self.draw.queue_draw()
426 if event.x > self.last_x:
431 self.last_x = event.x
433 def ball_motion(self, widget, event):
434 if not self.move_ball:
437 self.move_ball.p.x = self.point_without_border(event).x
438 self.move_ball.p.y = self.point_without_border(event).y
440 self.draw.queue_draw()
442 def finish_drawing(self):
444 position = len(self.balls)
445 ball = Ball(self.start_x, self.start_y, self.radius, '', position)
446 self.balls.append(ball)
447 self.model.append([position, ''])
448 self.treeview.set_cursor(str(position), self.fpcolumn, True)
449 self.new_ball = False
451 # reseting to the default coordenades
454 self.radius = Ball.DEFAULT_WIDTH
456 if __name__ == '__main__':