Sorry for delay to put it in a vcs, some hours ago this
[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 NewProject(GladeLoader):
70     def __init__(self, parent=None):
71         super(NewProject, self).__init__('gzv.glade', 'new-project')
72         if parent:
73             self.wid('new-project').set_transient_for(parent)
74
75 class Gzv(GladeLoader):
76     def __init__(self):
77         super(Gzv, self).__init__('gzv.glade', 'main-window')
78         self.window = self.wid('main-window')
79         self.window.connect('delete-event', lambda *x: gtk.main_quit())
80
81         self.evtbox = self.wid('eventbox')
82         self.evtbox.connect('button-press-event', self.button_press)
83         self.evtbox.connect('button-release-event', self.button_release)
84         self.evtbox.connect('motion-notify-event', self.motion_notify)
85
86         self.model = gtk.ListStore(int, str)
87         self.treeview = self.wid('treeview')
88         self.treeview.set_model(self.model)
89
90         self.draw = self.wid('draw')
91         self.draw.connect('expose-event', self.expose_draw)
92
93         # FIXME: Hardcoded.
94         self.image = 'skol.jpg'
95         self.balls = self.load_balls_from_file('xxx')
96         self.load_balls_to_treeview()
97
98         # this *MUST* be called *AFTER* load_balls_to_treeview
99         self.setup_treeview()
100
101         self.ball_width = Ball.DEFAULT_WIDTH
102         self.selecting = False
103         self.start_x = -1
104         self.start_y = -1
105
106     def setup_treeview(self):
107         self.model.connect('rows-reordered', self.on_rows_reordered)
108
109         renderer = gtk.CellRendererText()
110         column = gtk.TreeViewColumn(_('Position'), renderer, text=0)
111         self.treeview.append_column(column)
112
113         renderer = gtk.CellRendererText()
114         renderer.connect('edited', self.on_cell_edited)
115         renderer.set_property('editable', True)
116         column = gtk.TreeViewColumn(_('Name'), renderer, text=1)
117         self.treeview.append_column(column)
118
119     def on_rows_reordered(self, *args):
120         print 
121
122     def on_cell_edited(self, *args):
123         print args
124
125     def new_project(self, button):
126         dialog = NewProject(self.window).wid('new-project')
127
128         # This '1' was defined in the glade file
129         if dialog.run() == 1:
130             pass
131         dialog.destroy()
132
133     def open_file_chooser(self, button):
134         fc = gtk.FileChooserDialog(_('Choose an image'), self,
135                                    buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
136                                             gtk.STOCK_OK, gtk.RESPONSE_OK))
137         if fc.run() == gtk.RESPONSE_OK:
138             self.image = fc.get_filename()
139
140         fc.destroy()
141
142     def load_balls_to_treeview(self):
143         model = self.treeview.get_model()
144         for i in self.balls:
145             model.append([i.position, i.name])
146
147     def load_balls_from_file(self, fname):
148         balls = BallManager()
149         for index, line in enumerate(file(fname)):
150             if not line:
151                 continue
152             pos, radios, name = line.split()
153             x, y = pos.split(',')
154             balls.append(Ball(int(x), int(y), int(radios), name, index))
155         return balls
156
157     def expose_draw(self, draw, event):
158         if not self.image:
159             return
160
161         # loading the picture image and getting some useful
162         # information to draw it in the widget's background
163         img = gtk.gdk.pixbuf_new_from_file(self.image)
164         pixels = img.get_pixels()
165         rowstride = img.get_rowstride()
166         width = img.get_width()
167         height = img.get_height()
168         gc = draw.style.black_gc
169
170         # sets the correct size of the eventbox, to show the scrollbar
171         # when needed.
172         self.evtbox.set_size_request(width, height)
173
174         # drawing the picture in the background of the drawing area,
175         # this is really important.
176         draw.window.draw_rgb_image(gc, 0, 0, width, height,
177                                    'normal', pixels, rowstride,
178                                    0, 0)
179
180         # this call makes the ball being drown be shown correctly.
181         self.draw_current_ball()
182
183         # drawing other balls stored in the self.balls list.
184         ctx = draw.window.cairo_create()
185         ctx.fill()
186
187         ctx.set_line_width(10.0)
188         ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
189
190         for i in self.balls:
191             ctx.arc(i.x, i.y, i.radios, 0, 64*math.pi)
192
193         ctx.fill()
194         ctx.stroke()
195
196     def draw_current_ball(self):
197         if self.start_x < 0:
198             return
199         ctx = self.draw.window.cairo_create()
200         ctx.arc(self.start_x, self.start_y, self.ball_width, 0, 64*math.pi)
201         ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
202         ctx.fill()
203
204     def button_press(self, widget, event):
205         if event.button == 1:
206             self.selecting = True
207             self.start_x = event.x
208             self.start_y = event.y
209             self.last_x = event.x
210
211     def button_release(self, widget, event):
212         if event.button == 1:
213             self.selecting = False
214             self.finish_drawing()
215
216     def motion_notify(self, widget, event):
217         self.draw.queue_draw()
218
219         if event.x > self.last_x:
220             self.ball_width += 2
221         else:
222             self.ball_width -= 2
223
224         self.last_x = event.x
225
226     def finish_drawing(self):
227         self.draw_current_ball()
228         self.ball_width = Ball.DEFAULT_WIDTH
229
230 if __name__ == '__main__':
231     Gzv().window.show_all()
232     gtk.main()