renaming open_file_chooser to open_project and cleaning
[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 gtk
16 import gtk.glade
17 import math
18 import cairo
19
20 _ = lambda x:x
21
22 class Ball(object):
23     DEFAULT_WIDTH = 10
24
25     def __init__(self, x, y, r, name='', position=0):
26         self.position = position
27         self.x = x
28         self.y = y
29         self.radios = r
30         self.name = name
31
32 class BallManager(list):
33     def __init__(self, *args, **kwargs):
34         super(BallManager, self).__init__(*args, **kwargs)
35
36     def save_to_file(self, path):
37         target = open(path, 'w')
38         for i in self:
39             target.write('%d,%d %d %s\n' % (i.x, i.y, i.radios, i.name))
40         target.close()
41
42 class GladeLoader(object):
43     def __init__(self, fname, root=''):
44         self.ui = gtk.glade.XML(fname, root)
45         self.ui.signal_autoconnect(self)
46
47     def get_widget(self, wname):
48         return self.ui.get_widget(wname)
49
50     # little shortcut
51     wid = get_widget
52
53     # glade callbacks
54
55     def gtk_widget_show(self, widget, *args):
56         widget.show()
57         return True
58
59     def gtk_widget_hide(self, widget, *args):
60         widget.hide()
61         return True
62
63     def gtk_main_quit(self, *args):
64         gtk.main_quit()
65
66     def gtk_main(self, *args):
67         gtk.main()
68
69 class Project(object):
70     def __init__(self, image, width, height):
71         self.image = image
72         self.width = width
73         self.height = height
74
75 class NewProject(GladeLoader):
76     def __init__(self, parent=None):
77         super(NewProject, self).__init__('gzv.glade', 'new-project')
78         self.dialog = self.wid('new-project')
79         if parent:
80             self.dialog.set_transient_for(parent)
81
82     def get_project(self):
83         fname = self.wid('image').get_filename()
84         width = self.wid('width').get_text()
85         height = self.wid('height').get_text()
86         return Project(fname, width, height)
87
88     def destroy(self):
89         self.dialog.destroy()
90
91 class Gzv(GladeLoader):
92     def __init__(self):
93         super(Gzv, self).__init__('gzv.glade', 'main-window')
94         self.window = self.wid('main-window')
95         self.window.connect('delete-event', lambda *x: gtk.main_quit())
96
97         self.evtbox = self.wid('eventbox')
98         self.evtbox.connect('button-press-event', self.button_press)
99         self.evtbox.connect('button-release-event', self.button_release)
100         self.evtbox.connect('motion-notify-event', self.motion_notify)
101
102         self.model = gtk.ListStore(int, str)
103         self.treeview = self.wid('treeview')
104         self.treeview.set_model(self.model)
105
106         self.draw = self.wid('draw')
107         self.draw.connect('expose-event', self.expose_draw)
108
109         # Starting with an empty project with no image loaded
110         self.project = None
111         self.image = None
112
113         # This attr may be overriten, if so, call the method (load_balls_to_treeview)
114         self.balls = BallManager()
115
116         self.load_balls_to_treeview()
117         self.setup_treeview()
118
119         # drawing stuff
120         self.ball_width = Ball.DEFAULT_WIDTH
121         self.selecting = False
122         self.start_x = -1
123         self.start_y = -1
124
125     def setup_treeview(self):
126         self.model.connect('rows-reordered', self.on_rows_reordered)
127
128         renderer = gtk.CellRendererText()
129         column = gtk.TreeViewColumn(_('Position'), renderer, text=0)
130         self.treeview.append_column(column)
131
132         renderer = gtk.CellRendererText()
133         renderer.connect('edited', self.on_cell_edited)
134         renderer.set_property('editable', True)
135         column = gtk.TreeViewColumn(_('Name'), renderer, text=1)
136         self.treeview.append_column(column)
137
138     def on_rows_reordered(self, *args):
139         print 
140
141     def on_cell_edited(self, *args):
142         print args
143
144     def new_project(self, button):
145         proj = NewProject(self.window)
146
147         # This '1' was defined in the glade file
148         if proj.dialog.run() == 1:
149             self.load_new_project(proj.get_project())
150         proj.destroy()
151
152     def open_project(self, *args):
153         fc = gtk.FileChooserDialog(_('Choose a gzv project'), self.window,
154                                    buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
155                                             gtk.STOCK_OK, gtk.RESPONSE_OK))
156         if fc.run() == gtk.RESPONSE_OK:
157             proj_file = fc.get_filename()
158             self.balls = self.load_balls_from_file(proj_file)
159
160         fc.destroy()
161
162     def load_balls_to_treeview(self):
163         model = self.treeview.get_model()
164         for i in self.balls:
165             model.append([i.position, i.name])
166
167     def load_balls_from_file(self, fname):
168         balls = BallManager()
169         for index, line in enumerate(file(fname)):
170             if not line:
171                 continue
172             pos, radios, name = line.split()
173             x, y = pos.split(',')
174             balls.append(Ball(int(x), int(y), int(radios), name, index))
175         return balls
176
177     def expose_draw(self, draw, event):
178         if not self.image:
179             return
180
181         # loading the picture image and getting some useful
182         # information to draw it in the widget's background
183         img = gtk.gdk.pixbuf_new_from_file(self.image)
184         pixels = img.get_pixels()
185         rowstride = img.get_rowstride()
186         width = img.get_width()
187         height = img.get_height()
188         gc = draw.style.black_gc
189
190         # sets the correct size of the eventbox, to show the scrollbar
191         # when needed.
192         self.evtbox.set_size_request(width, height)
193
194         # drawing the picture in the background of the drawing area,
195         # this is really important.
196         draw.window.draw_rgb_image(gc, 0, 0, width, height,
197                                    'normal', pixels, rowstride,
198                                    0, 0)
199
200         # this call makes the ball being drown be shown correctly.
201         self.draw_current_ball()
202
203         # drawing other balls stored in the self.balls list.
204         ctx = draw.window.cairo_create()
205         ctx.fill()
206
207         ctx.set_line_width(10.0)
208         ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
209
210         for i in self.balls:
211             ctx.arc(i.x, i.y, i.radios, 0, 64*math.pi)
212
213         ctx.fill()
214         ctx.stroke()
215
216     def draw_current_ball(self):
217         if self.start_x < 0:
218             return
219         ctx = self.draw.window.cairo_create()
220         ctx.arc(self.start_x, self.start_y, self.ball_width, 0, 64*math.pi)
221         ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
222         ctx.fill()
223
224     def button_press(self, widget, event):
225         if event.button == 1:
226             self.selecting = True
227             self.start_x = event.x
228             self.start_y = event.y
229             self.last_x = event.x
230
231     def button_release(self, widget, event):
232         if event.button == 1:
233             self.selecting = False
234             self.finish_drawing()
235
236     def motion_notify(self, widget, event):
237         self.draw.queue_draw()
238
239         if event.x > self.last_x:
240             self.ball_width += 2
241         else:
242             self.ball_width -= 2
243
244         self.last_x = event.x
245
246     def finish_drawing(self):
247         self.draw_current_ball()
248         self.ball_width = Ball.DEFAULT_WIDTH
249
250 if __name__ == '__main__':
251     Gzv().window.show_all()
252     gtk.main()