fixing little bug when drawing more thant 2 balls and
[cascardo/movie.git] / gzv.py
1 # gzv.py - an user interface to generate-zooming-video
2 #
3 # Copyright (C) 2008  Lincoln de Sousa <lincoln@minaslivre.org>
4 #
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.
9 #
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.
14
15 import os
16 import gtk
17 import gtk.glade
18 import math
19 import cairo
20 from ConfigParser import ConfigParser
21
22 _ = lambda x:x
23
24 class Ball(object):
25     DEFAULT_WIDTH = 10
26
27     def __init__(self, x, y, r, name='', position=0):
28         self.position = position
29         self.x = int(x)
30         self.y = int(y)
31         self.radius = int(r)
32         self.name = name
33
34 class BallManager(list):
35     def __init__(self, *args, **kwargs):
36         super(BallManager, self).__init__(*args, **kwargs)
37
38     def save_to_file(self, path):
39         target = open(path, 'w')
40         for i in self:
41             target.write('%d,%d %d %s\n' % (i.x, i.y, i.radius, i.name))
42         target.close()
43
44 class GladeLoader(object):
45     def __init__(self, fname, root=''):
46         self.ui = gtk.glade.XML(fname, root)
47         self.ui.signal_autoconnect(self)
48
49     def get_widget(self, wname):
50         return self.ui.get_widget(wname)
51
52     # little shortcut
53     wid = get_widget
54
55     # glade callbacks
56
57     def gtk_widget_show(self, widget, *args):
58         widget.show()
59         return True
60
61     def gtk_widget_hide(self, widget, *args):
62         widget.hide()
63         return True
64
65     def gtk_main_quit(self, *args):
66         gtk.main_quit()
67
68     def gtk_main(self, *args):
69         gtk.main()
70
71 class Project(object):
72     def __init__(self, image, width, height):
73         self.image = image
74         self.width = width
75         self.height = height
76         self.focus_points_file = ''
77
78     def save_to_file(self, path):
79         bn = os.path.basename(path)
80         name = os.path.splitext(bn)[0]
81
82         cp = ConfigParser()
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)
87         
88         cp.write(open(path, 'w'))
89
90     @staticmethod
91     def parse_file(path):
92         cp = ConfigParser()
93         cp.read(path)
94
95         image = cp.get('Project', 'image')
96         width = cp.getint('Project', 'width')
97         height = cp.getint('Project', 'height')
98         x = cp.getint('Project', 'height')
99
100         proj = Project(image, width, height)
101         proj.focus_points_file = cp.get('Project', 'focus_points')
102
103         return proj
104
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')
109         if parent:
110             self.dialog.set_transient_for(parent)
111
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)
117
118     def destroy(self):
119         self.dialog.destroy()
120
121 class Gzv(GladeLoader):
122     def __init__(self):
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())
126
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)
131
132         self.model = gtk.ListStore(int, str)
133         self.treeview = self.wid('treeview')
134         self.treeview.set_model(self.model)
135
136         self.draw = self.wid('draw')
137         self.draw.connect('expose-event', self.expose_draw)
138
139         # Starting with an empty project with no image loaded
140         self.project = None
141         self.image = None
142
143         # This attr may be overriten, if so, call the method (load_balls_to_treeview)
144         self.balls = BallManager()
145
146         self.load_balls_to_treeview()
147         self.setup_treeview()
148
149         # drawing stuff
150         self.selecting = False
151         self.start_x = -1
152         self.start_y = -1
153         self.radius = Ball.DEFAULT_WIDTH
154
155     def setup_treeview(self):
156         self.model.connect('rows-reordered', self.on_rows_reordered)
157
158         renderer = gtk.CellRendererText()
159         column = gtk.TreeViewColumn(_('Position'), renderer, text=0)
160         column.set_property('visible', False)
161         self.treeview.append_column(column)
162
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)
168
169     def on_rows_reordered(self, *args):
170         print 
171
172     def on_cell_edited(self, renderer, path, value):
173         self.balls[int(path)].name = value
174         self.load_balls_to_treeview()
175         self.draw.queue_draw()
176
177     def new_project(self, button):
178         proj = NewProject(self.window)
179
180         # This '1' was defined in the glade file
181         if proj.dialog.run() == 1:
182             self.load_project(proj.get_project())
183         proj.destroy()
184
185     def open_project(self, *args):
186         fc = gtk.FileChooserDialog(_('Choose a gzv project'), self.window,
187                                    buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
188                                             gtk.STOCK_OK, gtk.RESPONSE_OK))
189         if fc.run() == gtk.RESPONSE_OK:
190             proj_file = fc.get_filename()
191             self.load_project(Project.parse_file(proj_file))
192         fc.destroy()
193
194     def load_project(self, project):
195         self.project = project
196         self.balls = self.load_balls_from_file(project.focus_points_file)
197         self.image = project.image
198         self.load_balls_to_treeview()
199         self.draw.queue_draw()
200
201     def load_balls_to_treeview(self):
202         self.model.clear()
203         for i in self.balls:
204             self.model.append([i.position, i.name])
205
206     def load_balls_from_file(self, fname):
207         balls = BallManager()
208         if not os.path.exists(fname):
209             return balls
210
211         for index, line in enumerate(file(fname)):
212             if not line:
213                 continue
214             pos, radius, name = line.split(None, 2)
215             x, y = pos.split(',')
216             balls.append(Ball(x, y, radius, name.strip(), index))
217         return balls
218
219     def remove_fp(self, *args):
220         selection = self.treeview.get_selection()
221         model, path = selection.get_selected()
222         if path:
223             position = model[path][0]
224             for i in self.balls:
225                 if i.position == int(position):
226                     self.balls.remove(i)
227             del model[path]
228             self.draw.queue_draw()
229
230     def save_fp_list(self, *args):
231         assert self.project is not None
232
233         # if the project has no
234         if self.project and not self.project.focus_points_file:
235             fc = gtk.FileChooserDialog(_('Save the focus points file'),
236                                        self.window,
237                                        action=gtk.FILE_CHOOSER_ACTION_SAVE,
238                                        buttons=(gtk.STOCK_CANCEL,
239                                                 gtk.RESPONSE_CANCEL,
240                                                 gtk.STOCK_SAVE,
241                                                 gtk.RESPONSE_OK))
242             if fc.run() == gtk.RESPONSE_OK:
243                 self.project.focus_points_file = fc.get_filename()
244                 fc.destroy()
245             else:
246                 fc.destroy()
247                 return
248
249         self.balls.save_to_file(self.project.focus_points_file)
250
251     def expose_draw(self, draw, event):
252         if not self.image:
253             return
254
255         # loading the picture image and getting some useful
256         # information to draw it in the widget's background
257         img = gtk.gdk.pixbuf_new_from_file(self.image)
258         pixels = img.get_pixels()
259         rowstride = img.get_rowstride()
260         width = img.get_width()
261         height = img.get_height()
262         gc = draw.style.black_gc
263
264         # sets the correct size of the eventbox, to show the scrollbar
265         # when needed.
266         self.evtbox.set_size_request(width, height)
267
268         # drawing the picture in the background of the drawing area,
269         # this is really important.
270         draw.window.draw_rgb_image(gc, 0, 0, width, height,
271                                    'normal', pixels, rowstride,
272                                    0, 0)
273
274         # this call makes the ball being drown be shown correctly.
275         self.draw_current_ball()
276
277         # drawing other balls stored in the self.balls list.
278         ctx = draw.window.cairo_create()
279         ctx.fill()
280
281         ctx.set_line_width(10.0)
282         ctx.set_source_rgba(0.5, 0.0, 0.0, 0.4)
283
284         for i in self.balls:
285             ctx.arc(i.x, i.y, i.radius, 0, 64*math.pi)
286             ctx.fill()
287
288     def draw_current_ball(self):
289         if self.start_x < 0:
290             return
291         ctx = self.draw.window.cairo_create()
292         ctx.arc(self.start_x, self.start_y, self.radius, 0, 64*math.pi)
293         ctx.set_source_rgba(0.5, 0.0, 0.0, 0.4)
294         ctx.fill()
295
296     def button_press(self, widget, event):
297         if event.button == 1:
298             self.selecting = True
299             self.start_x = event.x
300             self.start_y = event.y
301             self.last_x = event.x
302
303     def button_release(self, widget, event):
304         if event.button == 1:
305             self.selecting = False
306             self.finish_drawing()
307
308     def motion_notify(self, widget, event):
309         self.draw.queue_draw()
310
311         if event.x > self.last_x:
312             self.radius += 3
313         else:
314             self.radius -= 3
315
316         self.last_x = event.x
317
318     def finish_drawing(self):
319         self.draw_current_ball()
320         self.ball_width = Ball.DEFAULT_WIDTH
321
322         position = len(self.balls)
323         ball = Ball(self.start_x, self.start_y, self.radius, '', position)
324         self.balls.append(ball)
325         self.model.append([position, ''])
326         self.treeview.set_cursor(str(position), self.fpcolumn, True)
327
328 if __name__ == '__main__':
329     Gzv().window.show_all()
330     gtk.main()