1 # gzv.py - an user interface to generate-zooming-video
3 # Copyright (C) 2008 Lincoln de Sousa <lincoln@minaslivre.org>
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.
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.
25 def __init__(self, x, y, r, name='', position=0):
26 self.position = position
32 class BallManager(list):
33 def __init__(self, *args, **kwargs):
34 super(BallManager, self).__init__(*args, **kwargs)
36 def save_to_file(self, path):
37 target = open(path, 'w')
39 target.write('%d,%d %d %s\n' % (i.x, i.y, i.radios, i.name))
42 class GladeLoader(object):
43 def __init__(self, fname, root=''):
44 self.ui = gtk.glade.XML(fname, root)
45 self.ui.signal_autoconnect(self)
47 def get_widget(self, wname):
48 return self.ui.get_widget(wname)
55 def gtk_widget_show(self, widget, *args):
59 def gtk_widget_hide(self, widget, *args):
63 def gtk_main_quit(self, *args):
66 def gtk_main(self, *args):
69 class NewProject(GladeLoader):
70 def __init__(self, parent=None):
71 super(NewProject, self).__init__('gzv.glade', 'new-project')
73 self.wid('new-project').set_transient_for(parent)
75 class Gzv(GladeLoader):
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())
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)
86 self.model = gtk.ListStore(int, str)
87 self.treeview = self.wid('treeview')
88 self.treeview.set_model(self.model)
90 self.draw = self.wid('draw')
91 self.draw.connect('expose-event', self.expose_draw)
94 self.image = 'skol.jpg'
95 self.balls = self.load_balls_from_file('xxx')
96 self.load_balls_to_treeview()
98 # this *MUST* be called *AFTER* load_balls_to_treeview
101 self.ball_width = Ball.DEFAULT_WIDTH
102 self.selecting = False
106 def setup_treeview(self):
107 self.model.connect('rows-reordered', self.on_rows_reordered)
109 renderer = gtk.CellRendererText()
110 column = gtk.TreeViewColumn(_('Position'), renderer, text=0)
111 self.treeview.append_column(column)
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)
119 def on_rows_reordered(self, *args):
122 def on_cell_edited(self, *args):
125 def new_project(self, button):
126 dialog = NewProject(self.window).wid('new-project')
128 # This '1' was defined in the glade file
129 if dialog.run() == 1:
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()
142 def load_balls_to_treeview(self):
143 model = self.treeview.get_model()
145 model.append([i.position, i.name])
147 def load_balls_from_file(self, fname):
148 balls = BallManager()
149 for index, line in enumerate(file(fname)):
152 pos, radios, name = line.split()
153 x, y = pos.split(',')
154 balls.append(Ball(int(x), int(y), int(radios), name, index))
157 def expose_draw(self, draw, event):
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
170 # sets the correct size of the eventbox, to show the scrollbar
172 self.evtbox.set_size_request(width, height)
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,
180 # this call makes the ball being drown be shown correctly.
181 self.draw_current_ball()
183 # drawing other balls stored in the self.balls list.
184 ctx = draw.window.cairo_create()
187 ctx.set_line_width(10.0)
188 ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
191 ctx.arc(i.x, i.y, i.radios, 0, 64*math.pi)
196 def draw_current_ball(self):
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)
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
211 def button_release(self, widget, event):
212 if event.button == 1:
213 self.selecting = False
214 self.finish_drawing()
216 def motion_notify(self, widget, event):
217 self.draw.queue_draw()
219 if event.x > self.last_x:
224 self.last_x = event.x
226 def finish_drawing(self):
227 self.draw_current_ball()
228 self.ball_width = Ball.DEFAULT_WIDTH
230 if __name__ == '__main__':
231 Gzv().window.show_all()