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.
20 from ConfigParser import ConfigParser
27 def __init__(self, x, y, r, name='', position=0):
28 self.position = position
34 class BallManager(list):
35 def __init__(self, *args, **kwargs):
36 super(BallManager, self).__init__(*args, **kwargs)
38 def save_to_file(self, path):
39 target = open(path, 'w')
41 target.write('%d,%d %d %s\n' % (i.x, i.y, i.radius, i.name))
44 class GladeLoader(object):
45 def __init__(self, fname, root=''):
46 self.ui = gtk.glade.XML(fname, root)
47 self.ui.signal_autoconnect(self)
49 def get_widget(self, wname):
50 return self.ui.get_widget(wname)
57 def gtk_widget_show(self, widget, *args):
61 def gtk_widget_hide(self, widget, *args):
65 def gtk_main_quit(self, *args):
68 def gtk_main(self, *args):
71 class Project(object):
72 def __init__(self, image, width, height):
76 self.focus_points_file = ''
78 def save_to_file(self, path):
79 bn = os.path.basename(path)
80 name = os.path.splitext(bn)[0]
83 cp.set('Project', 'image', self.image)
84 cp.set('Project', 'width', self.width)
85 cp.set('Project', 'height', self.height)
86 cp.set('Project', 'focus_points', self.focus_points_file)
88 cp.write(open(path, 'w'))
95 image = cp.get('Project', 'image')
96 width = cp.getint('Project', 'width')
97 height = cp.getint('Project', 'height')
98 x = cp.getint('Project', 'height')
100 proj = Project(image, width, height)
101 proj.focus_points_file = cp.get('Project', 'focus_points')
105 class NewProject(GladeLoader):
106 def __init__(self, parent=None):
107 super(NewProject, self).__init__('gzv.glade', 'new-project')
108 self.dialog = self.wid('new-project')
110 self.dialog.set_transient_for(parent)
112 def get_project(self):
113 fname = self.wid('image').get_filename()
114 width = self.wid('width').get_text()
115 height = self.wid('height').get_text()
116 return Project(fname, width, height)
119 self.dialog.destroy()
121 class Gzv(GladeLoader):
123 super(Gzv, self).__init__('gzv.glade', 'main-window')
124 self.window = self.wid('main-window')
125 self.window.connect('delete-event', lambda *x: gtk.main_quit())
127 self.evtbox = self.wid('eventbox')
128 self.evtbox.connect('button-press-event', self.button_press)
129 self.evtbox.connect('button-release-event', self.button_release)
130 self.evtbox.connect('motion-notify-event', self.motion_notify)
132 self.model = gtk.ListStore(int, str)
133 self.treeview = self.wid('treeview')
134 self.treeview.set_model(self.model)
136 self.draw = self.wid('draw')
137 self.draw.connect('expose-event', self.expose_draw)
139 # Starting with an empty project with no image loaded
143 # This attr may be overriten, if so, call the method (load_balls_to_treeview)
144 self.balls = BallManager()
146 self.load_balls_to_treeview()
147 self.setup_treeview()
150 self.selecting = False
153 self.radius = Ball.DEFAULT_WIDTH
155 def setup_treeview(self):
156 self.model.connect('rows-reordered', self.on_rows_reordered)
158 renderer = gtk.CellRendererText()
159 column = gtk.TreeViewColumn(_('Position'), renderer, text=0)
160 column.set_property('visible', False)
161 self.treeview.append_column(column)
163 renderer = gtk.CellRendererText()
164 renderer.connect('edited', self.on_cell_edited)
165 renderer.set_property('editable', True)
166 self.fpcolumn = gtk.TreeViewColumn(_('Name'), renderer, text=1)
167 self.treeview.append_column(self.fpcolumn)
169 def on_rows_reordered(self, *args):
172 def on_cell_edited(self, renderer, path, value):
173 ball = Ball(self.start_x, self.start_y, self.radius, value, int(path))
174 self.balls.append(ball)
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 load_balls_to_treeview(self):
203 model = self.treeview.get_model()
205 model.append([i.position, i.name])
207 def load_balls_from_file(self, fname):
208 balls = BallManager()
209 if not os.path.exists(fname):
212 for index, line in enumerate(file(fname)):
215 pos, radius, name = line.split()
216 x, y = pos.split(',')
217 balls.append(Ball(int(x), int(y), int(radius), name, index))
220 def remove_fp(self, *args):
221 selection = self.treeview.get_selection()
222 model, path = selection.get_selected()
224 position = model[path][0]
226 if i.position == int(position):
229 self.draw.queue_draw()
231 def save_fp_list(self, *args):
232 assert self.project is not None
234 # if the project has no
235 if self.project and not self.project.focus_points_file:
236 fc = gtk.FileChooserDialog(_('Save the focus points file'),
238 action=gtk.FILE_CHOOSER_ACTION_SAVE,
239 buttons=(gtk.STOCK_CANCEL,
243 if fc.run() == gtk.RESPONSE_OK:
244 self.project.focus_points_file = fc.get_filename()
250 self.balls.save_to_file(self.project.focus_points_file)
252 def expose_draw(self, draw, event):
256 # loading the picture image and getting some useful
257 # information to draw it in the widget's background
258 img = gtk.gdk.pixbuf_new_from_file(self.image)
259 pixels = img.get_pixels()
260 rowstride = img.get_rowstride()
261 width = img.get_width()
262 height = img.get_height()
263 gc = draw.style.black_gc
265 # sets the correct size of the eventbox, to show the scrollbar
267 self.evtbox.set_size_request(width, height)
269 # drawing the picture in the background of the drawing area,
270 # this is really important.
271 draw.window.draw_rgb_image(gc, 0, 0, width, height,
272 'normal', pixels, rowstride,
275 # this call makes the ball being drown be shown correctly.
276 self.draw_current_ball()
278 # drawing other balls stored in the self.balls list.
279 ctx = draw.window.cairo_create()
282 ctx.set_line_width(10.0)
283 ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
286 ctx.arc(i.x, i.y, i.radius, 0, 64*math.pi)
291 def draw_current_ball(self):
294 ctx = self.draw.window.cairo_create()
295 ctx.arc(self.start_x, self.start_y, self.radius, 0, 64*math.pi)
296 ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
299 def button_press(self, widget, event):
300 if event.button == 1:
301 self.selecting = True
302 self.start_x = event.x
303 self.start_y = event.y
304 self.last_x = event.x
306 def button_release(self, widget, event):
307 if event.button == 1:
308 self.selecting = False
309 self.finish_drawing()
311 def motion_notify(self, widget, event):
312 self.draw.queue_draw()
314 if event.x > self.last_x:
319 self.last_x = event.x
321 def finish_drawing(self):
322 self.draw_current_ball()
323 self.ball_width = Ball.DEFAULT_WIDTH
325 position = len(self.balls)
326 self.model.append([position, ''])
327 self.treeview.set_cursor(str(position), self.fpcolumn, True)
329 if __name__ == '__main__':
330 Gzv().window.show_all()