getting enough data to start a new project
[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         # FIXME: Hardcoded.
110         self.image = 'skol.jpg'
111         self.balls = self.load_balls_from_file('xxx')
112         self.load_balls_to_treeview()
113
114         # this *MUST* be called *AFTER* load_balls_to_treeview
115         self.setup_treeview()
116
117         self.ball_width = Ball.DEFAULT_WIDTH
118         self.selecting = False
119         self.start_x = -1
120         self.start_y = -1
121
122     def setup_treeview(self):
123         self.model.connect('rows-reordered', self.on_rows_reordered)
124
125         renderer = gtk.CellRendererText()
126         column = gtk.TreeViewColumn(_('Position'), renderer, text=0)
127         self.treeview.append_column(column)
128
129         renderer = gtk.CellRendererText()
130         renderer.connect('edited', self.on_cell_edited)
131         renderer.set_property('editable', True)
132         column = gtk.TreeViewColumn(_('Name'), renderer, text=1)
133         self.treeview.append_column(column)
134
135     def on_rows_reordered(self, *args):
136         print 
137
138     def on_cell_edited(self, *args):
139         print args
140
141     def new_project(self, button):
142         proj = NewProject(self.window)
143
144         # This '1' was defined in the glade file
145         if proj.dialog.run() == 1:
146             self.load_new_project(proj.get_project())
147         proj.destroy()
148
149     def open_file_chooser(self, button):
150         fc = gtk.FileChooserDialog(_('Choose a gzv project'), self.window,
151                                    buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
152                                             gtk.STOCK_OK, gtk.RESPONSE_OK))
153         if fc.run() == gtk.RESPONSE_OK:
154             self.image = fc.get_filename()
155
156         fc.destroy()
157
158     def load_balls_to_treeview(self):
159         model = self.treeview.get_model()
160         for i in self.balls:
161             model.append([i.position, i.name])
162
163     def load_balls_from_file(self, fname):
164         balls = BallManager()
165         for index, line in enumerate(file(fname)):
166             if not line:
167                 continue
168             pos, radios, name = line.split()
169             x, y = pos.split(',')
170             balls.append(Ball(int(x), int(y), int(radios), name, index))
171         return balls
172
173     def expose_draw(self, draw, event):
174         if not self.image:
175             return
176
177         # loading the picture image and getting some useful
178         # information to draw it in the widget's background
179         img = gtk.gdk.pixbuf_new_from_file(self.image)
180         pixels = img.get_pixels()
181         rowstride = img.get_rowstride()
182         width = img.get_width()
183         height = img.get_height()
184         gc = draw.style.black_gc
185
186         # sets the correct size of the eventbox, to show the scrollbar
187         # when needed.
188         self.evtbox.set_size_request(width, height)
189
190         # drawing the picture in the background of the drawing area,
191         # this is really important.
192         draw.window.draw_rgb_image(gc, 0, 0, width, height,
193                                    'normal', pixels, rowstride,
194                                    0, 0)
195
196         # this call makes the ball being drown be shown correctly.
197         self.draw_current_ball()
198
199         # drawing other balls stored in the self.balls list.
200         ctx = draw.window.cairo_create()
201         ctx.fill()
202
203         ctx.set_line_width(10.0)
204         ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
205
206         for i in self.balls:
207             ctx.arc(i.x, i.y, i.radios, 0, 64*math.pi)
208
209         ctx.fill()
210         ctx.stroke()
211
212     def draw_current_ball(self):
213         if self.start_x < 0:
214             return
215         ctx = self.draw.window.cairo_create()
216         ctx.arc(self.start_x, self.start_y, self.ball_width, 0, 64*math.pi)
217         ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
218         ctx.fill()
219
220     def button_press(self, widget, event):
221         if event.button == 1:
222             self.selecting = True
223             self.start_x = event.x
224             self.start_y = event.y
225             self.last_x = event.x
226
227     def button_release(self, widget, event):
228         if event.button == 1:
229             self.selecting = False
230             self.finish_drawing()
231
232     def motion_notify(self, widget, event):
233         self.draw.queue_draw()
234
235         if event.x > self.last_x:
236             self.ball_width += 2
237         else:
238             self.ball_width -= 2
239
240         self.last_x = event.x
241
242     def finish_drawing(self):
243         self.draw_current_ball()
244         self.ball_width = Ball.DEFAULT_WIDTH
245
246 if __name__ == '__main__':
247     Gzv().window.show_all()
248     gtk.main()