e58ab82d061d9796e1f02c1d37ee899739c2964e
[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 = x
30         self.y = y
31         self.radios = 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.radios, 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         
87         cp.write(open(path, 'w'))
88
89     @staticmethod
90     def parse_file(path):
91         cp = ConfigParser()
92         cp.read(path)
93
94         image = cp.get('Project', 'image')
95         width = cp.getint('Project', 'width')
96         height = cp.getint('Project', 'height')
97         x = cp.getint('Project', 'height')
98
99         proj = Project(image, width, height)
100         proj.focus_points_file = cp.get('Project', 'focus_points')
101
102         return proj
103
104 class NewProject(GladeLoader):
105     def __init__(self, parent=None):
106         super(NewProject, self).__init__('gzv.glade', 'new-project')
107         self.dialog = self.wid('new-project')
108         if parent:
109             self.dialog.set_transient_for(parent)
110
111     def get_project(self):
112         fname = self.wid('image').get_filename()
113         width = self.wid('width').get_text()
114         height = self.wid('height').get_text()
115         return Project(fname, width, height)
116
117     def destroy(self):
118         self.dialog.destroy()
119
120 class Gzv(GladeLoader):
121     def __init__(self):
122         super(Gzv, self).__init__('gzv.glade', 'main-window')
123         self.window = self.wid('main-window')
124         self.window.connect('delete-event', lambda *x: gtk.main_quit())
125
126         self.evtbox = self.wid('eventbox')
127         self.evtbox.connect('button-press-event', self.button_press)
128         self.evtbox.connect('button-release-event', self.button_release)
129         self.evtbox.connect('motion-notify-event', self.motion_notify)
130
131         self.model = gtk.ListStore(int, str)
132         self.treeview = self.wid('treeview')
133         self.treeview.set_model(self.model)
134
135         self.draw = self.wid('draw')
136         self.draw.connect('expose-event', self.expose_draw)
137
138         # Starting with an empty project with no image loaded
139         self.project = None
140         self.image = None
141
142         # This attr may be overriten, if so, call the method (load_balls_to_treeview)
143         self.balls = BallManager()
144
145         self.load_balls_to_treeview()
146         self.setup_treeview()
147
148         # drawing stuff
149         self.ball_width = Ball.DEFAULT_WIDTH
150         self.selecting = False
151         self.start_x = -1
152         self.start_y = -1
153
154     def setup_treeview(self):
155         self.model.connect('rows-reordered', self.on_rows_reordered)
156
157         renderer = gtk.CellRendererText()
158         column = gtk.TreeViewColumn(_('Position'), renderer, text=0)
159         column.set_property('visible', False)
160         self.treeview.append_column(column)
161
162         renderer = gtk.CellRendererText()
163         renderer.connect('edited', self.on_cell_edited)
164         renderer.set_property('editable', True)
165         column = gtk.TreeViewColumn(_('Name'), renderer, text=1)
166         self.treeview.append_column(column)
167
168     def on_rows_reordered(self, *args):
169         print 
170
171     def on_cell_edited(self, *args):
172         print args
173
174     def new_project(self, button):
175         proj = NewProject(self.window)
176
177         # This '1' was defined in the glade file
178         if proj.dialog.run() == 1:
179             self.load_project(proj.get_project())
180         proj.destroy()
181
182     def open_project(self, *args):
183         fc = gtk.FileChooserDialog(_('Choose a gzv project'), self.window,
184                                    buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
185                                             gtk.STOCK_OK, gtk.RESPONSE_OK))
186         if fc.run() == gtk.RESPONSE_OK:
187             proj_file = fc.get_filename()
188             self.load_project(Project.parse_file(proj_file))
189         fc.destroy()
190
191     def load_project(self, project):
192         self.balls = self.load_balls_from_file(project.focus_points_file)
193         self.image = project.image
194         self.load_balls_to_treeview()
195         self.draw.queue_draw()
196
197     def load_balls_to_treeview(self):
198         model = self.treeview.get_model()
199         for i in self.balls:
200             model.append([i.position, i.name])
201
202     def load_balls_from_file(self, fname):
203         balls = BallManager()
204         if not os.path.exists(fname):
205             return balls
206
207         for index, line in enumerate(file(fname)):
208             if not line:
209                 continue
210             pos, radios, name = line.split()
211             x, y = pos.split(',')
212             balls.append(Ball(int(x), int(y), int(radios), name, index))
213         return balls
214
215     def expose_draw(self, draw, event):
216         if not self.image:
217             return
218
219         # loading the picture image and getting some useful
220         # information to draw it in the widget's background
221         img = gtk.gdk.pixbuf_new_from_file(self.image)
222         pixels = img.get_pixels()
223         rowstride = img.get_rowstride()
224         width = img.get_width()
225         height = img.get_height()
226         gc = draw.style.black_gc
227
228         # sets the correct size of the eventbox, to show the scrollbar
229         # when needed.
230         self.evtbox.set_size_request(width, height)
231
232         # drawing the picture in the background of the drawing area,
233         # this is really important.
234         draw.window.draw_rgb_image(gc, 0, 0, width, height,
235                                    'normal', pixels, rowstride,
236                                    0, 0)
237
238         # this call makes the ball being drown be shown correctly.
239         self.draw_current_ball()
240
241         # drawing other balls stored in the self.balls list.
242         ctx = draw.window.cairo_create()
243         ctx.fill()
244
245         ctx.set_line_width(10.0)
246         ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
247
248         for i in self.balls:
249             ctx.arc(i.x, i.y, i.radios, 0, 64*math.pi)
250
251         ctx.fill()
252         ctx.stroke()
253
254     def draw_current_ball(self):
255         if self.start_x < 0:
256             return
257         ctx = self.draw.window.cairo_create()
258         ctx.arc(self.start_x, self.start_y, self.ball_width, 0, 64*math.pi)
259         ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
260         ctx.fill()
261
262     def button_press(self, widget, event):
263         if event.button == 1:
264             self.selecting = True
265             self.start_x = event.x
266             self.start_y = event.y
267             self.last_x = event.x
268
269     def button_release(self, widget, event):
270         if event.button == 1:
271             self.selecting = False
272             self.finish_drawing()
273
274     def motion_notify(self, widget, event):
275         self.draw.queue_draw()
276
277         if event.x > self.last_x:
278             self.ball_width += 2
279         else:
280             self.ball_width -= 2
281
282         self.last_x = event.x
283
284     def finish_drawing(self):
285         self.draw_current_ball()
286         self.ball_width = Ball.DEFAULT_WIDTH
287
288 if __name__ == '__main__':
289     Gzv().window.show_all()
290     gtk.main()