1 # gzv.py - an user interface to generate-zooming-video
3 # Copyright (C) 2008 Lincoln de Sousa <lincoln@minaslivre.org>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
21 from ConfigParser import ConfigParser
28 def __init__(self, x, y, r, name='', position=0):
29 self.position = position
35 class BallManager(list):
36 def __init__(self, *args, **kwargs):
37 super(BallManager, self).__init__(*args, **kwargs)
39 def save_to_file(self, path):
40 target = open(path, 'w')
42 target.write('%d,%d %d %s\n' % (i.x, i.y, i.radius, i.name))
45 class GladeLoader(object):
46 def __init__(self, fname, root=''):
47 self.ui = gtk.glade.XML(fname, root)
48 self.ui.signal_autoconnect(self)
50 def get_widget(self, wname):
51 return self.ui.get_widget(wname)
58 def gtk_widget_show(self, widget, *args):
62 def gtk_widget_hide(self, widget, *args):
66 def gtk_main_quit(self, *args):
69 def gtk_main(self, *args):
72 class Project(object):
73 def __init__(self, image, width, height):
77 self.focus_points_file = ''
79 def save_to_file(self, path):
80 bn = os.path.basename(path)
81 name = os.path.splitext(bn)[0]
84 cp.set('Project', 'image', self.image)
85 cp.set('Project', 'width', self.width)
86 cp.set('Project', 'height', self.height)
87 cp.set('Project', 'focus_points', self.focus_points_file)
89 cp.write(open(path, 'w'))
96 image = cp.get('Project', 'image')
97 width = cp.getint('Project', 'width')
98 height = cp.getint('Project', 'height')
99 x = cp.getint('Project', 'height')
101 proj = Project(image, width, height)
102 proj.focus_points_file = cp.get('Project', 'focus_points')
106 class NewProject(GladeLoader):
107 def __init__(self, parent=None):
108 super(NewProject, self).__init__('gzv.glade', 'new-project')
109 self.dialog = self.wid('new-project')
111 self.dialog.set_transient_for(parent)
113 def get_project(self):
114 fname = self.wid('image').get_filename()
115 width = self.wid('width').get_text()
116 height = self.wid('height').get_text()
117 return Project(fname, width, height)
120 self.dialog.destroy()
122 class Gzv(GladeLoader):
124 super(Gzv, self).__init__('gzv.glade', 'main-window')
125 self.window = self.wid('main-window')
126 self.window.connect('delete-event', lambda *x: gtk.main_quit())
128 self.evtbox = self.wid('eventbox')
129 self.evtbox.connect('button-press-event', self.button_press)
130 self.evtbox.connect('button-release-event', self.button_release)
131 self.evtbox.connect('motion-notify-event', self.motion_notify)
133 self.model = gtk.ListStore(int, str)
134 self.treeview = self.wid('treeview')
135 self.treeview.set_model(self.model)
137 self.draw = self.wid('draw')
138 self.draw.connect('expose-event', self.expose_draw)
140 # Starting with an empty project with no image loaded
144 # This attr may be overriten, if so, call the method (load_balls_to_treeview)
145 self.balls = BallManager()
147 self.load_balls_to_treeview()
148 self.setup_treeview()
151 self.selecting = False
154 self.radius = Ball.DEFAULT_WIDTH
156 def setup_treeview(self):
157 self.model.connect('rows-reordered', self.on_rows_reordered)
159 renderer = gtk.CellRendererText()
160 column = gtk.TreeViewColumn(_('Position'), renderer, text=0)
161 column.set_property('visible', False)
162 self.treeview.append_column(column)
164 renderer = gtk.CellRendererText()
165 renderer.connect('edited', self.on_cell_edited)
166 renderer.set_property('editable', True)
167 self.fpcolumn = gtk.TreeViewColumn(_('Name'), renderer, text=1)
168 self.treeview.append_column(self.fpcolumn)
170 def on_rows_reordered(self, *args):
173 def on_cell_edited(self, renderer, path, value):
174 self.balls[int(path)].name = value
175 self.load_balls_to_treeview()
176 self.draw.queue_draw()
178 def new_project(self, button):
179 proj = NewProject(self.window)
181 # This '1' was defined in the glade file
182 if proj.dialog.run() == 1:
183 self.load_project(proj.get_project())
186 def open_project(self, *args):
187 fc = gtk.FileChooserDialog(_('Choose a gzv project'), self.window,
188 buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
189 gtk.STOCK_OK, gtk.RESPONSE_OK))
190 if fc.run() == gtk.RESPONSE_OK:
191 proj_file = fc.get_filename()
192 self.load_project(Project.parse_file(proj_file))
195 def load_project(self, project):
196 self.project = project
197 self.balls = self.load_balls_from_file(project.focus_points_file)
198 self.image = project.image
199 self.load_balls_to_treeview()
200 self.draw.queue_draw()
202 def unload_project(self):
205 self.balls = BallManager()
206 self.draw.queue_draw()
208 def load_balls_to_treeview(self):
211 self.model.append([i.position, i.name])
213 def load_balls_from_file(self, fname):
214 balls = BallManager()
215 if not os.path.exists(fname):
218 for index, line in enumerate(file(fname)):
221 pos, radius, name = line.split(None, 2)
222 x, y = pos.split(',')
223 balls.append(Ball(x, y, radius, name.strip(), index))
226 def remove_fp(self, *args):
227 selection = self.treeview.get_selection()
228 model, path = selection.get_selected()
230 position = model[path][0]
232 if i.position == int(position):
235 self.draw.queue_draw()
237 def save_fp_list(self, *args):
238 assert self.project is not None
240 # if the project has no
241 if self.project and not self.project.focus_points_file:
242 fc = gtk.FileChooserDialog(_('Save the focus points file'),
244 action=gtk.FILE_CHOOSER_ACTION_SAVE,
245 buttons=(gtk.STOCK_CANCEL,
249 if fc.run() == gtk.RESPONSE_OK:
250 self.project.focus_points_file = fc.get_filename()
256 self.balls.save_to_file(self.project.focus_points_file)
258 def expose_draw(self, draw, event):
262 # loading the picture image and getting some useful
263 # information to draw it in the widget's background
265 img = gtk.gdk.pixbuf_new_from_file(self.image)
266 except gobject.GError:
267 msg = _("Couldn't recognize the image file format.")
268 dialog = gtk.MessageDialog(self.window,
272 dialog.set_markup(msg)
276 self.draw.stop_emission('expose-event')
277 return self.unload_project()
279 pixels = img.get_pixels()
280 rowstride = img.get_rowstride()
281 width = img.get_width()
282 height = img.get_height()
283 gc = draw.style.black_gc
285 # sets the correct size of the eventbox, to show the scrollbar
287 self.evtbox.set_size_request(width, height)
289 # drawing the picture in the background of the drawing area,
290 # this is really important.
291 draw.window.draw_rgb_image(gc, 0, 0, width, height,
292 'normal', pixels, rowstride,
295 # this call makes the ball being drown be shown correctly.
296 self.draw_current_ball()
298 # drawing other balls stored in the self.balls list.
299 ctx = draw.window.cairo_create()
302 ctx.set_line_width(10.0)
303 ctx.set_source_rgba(0.5, 0.0, 0.0, 0.4)
306 ctx.arc(i.x, i.y, i.radius, 0, 64*math.pi)
309 def draw_current_ball(self):
312 ctx = self.draw.window.cairo_create()
313 ctx.arc(self.start_x, self.start_y, self.radius, 0, 64*math.pi)
314 ctx.set_source_rgba(0.5, 0.0, 0.0, 0.4)
317 def button_press(self, widget, event):
318 if event.button == 1:
319 self.selecting = True
320 self.start_x = event.x
321 self.start_y = event.y
322 self.last_x = event.x
324 def button_release(self, widget, event):
325 if event.button == 1:
326 self.selecting = False
327 self.finish_drawing()
329 def motion_notify(self, widget, event):
330 self.draw.queue_draw()
332 if event.x > self.last_x:
337 self.last_x = event.x
339 def finish_drawing(self):
340 self.draw_current_ball()
341 self.ball_width = Ball.DEFAULT_WIDTH
343 position = len(self.balls)
344 ball = Ball(self.start_x, self.start_y, self.radius, '', position)
345 self.balls.append(ball)
346 self.model.append([position, ''])
347 self.treeview.set_cursor(str(position), self.fpcolumn, True)
349 if __name__ == '__main__':
350 Gzv().window.show_all()