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 Project(object):
70 def __init__(self, image, width, height):
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')
80 self.dialog.set_transient_for(parent)
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)
91 class Gzv(GladeLoader):
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())
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)
102 self.model = gtk.ListStore(int, str)
103 self.treeview = self.wid('treeview')
104 self.treeview.set_model(self.model)
106 self.draw = self.wid('draw')
107 self.draw.connect('expose-event', self.expose_draw)
109 # Starting with an empty project with no image loaded
113 # This attr may be overriten, if so, call the method (load_balls_to_treeview)
114 self.balls = BallManager()
116 self.load_balls_to_treeview()
117 self.setup_treeview()
120 self.ball_width = Ball.DEFAULT_WIDTH
121 self.selecting = False
125 def setup_treeview(self):
126 self.model.connect('rows-reordered', self.on_rows_reordered)
128 renderer = gtk.CellRendererText()
129 column = gtk.TreeViewColumn(_('Position'), renderer, text=0)
130 self.treeview.append_column(column)
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)
138 def on_rows_reordered(self, *args):
141 def on_cell_edited(self, *args):
144 def new_project(self, button):
145 proj = NewProject(self.window)
147 # This '1' was defined in the glade file
148 if proj.dialog.run() == 1:
149 self.load_new_project(proj.get_project())
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)
162 def load_balls_to_treeview(self):
163 model = self.treeview.get_model()
165 model.append([i.position, i.name])
167 def load_balls_from_file(self, fname):
168 balls = BallManager()
169 for index, line in enumerate(file(fname)):
172 pos, radios, name = line.split()
173 x, y = pos.split(',')
174 balls.append(Ball(int(x), int(y), int(radios), name, index))
177 def expose_draw(self, draw, event):
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
190 # sets the correct size of the eventbox, to show the scrollbar
192 self.evtbox.set_size_request(width, height)
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,
200 # this call makes the ball being drown be shown correctly.
201 self.draw_current_ball()
203 # drawing other balls stored in the self.balls list.
204 ctx = draw.window.cairo_create()
207 ctx.set_line_width(10.0)
208 ctx.set_source_rgba (0.5, 0.0, 0.0, 0.4)
211 ctx.arc(i.x, i.y, i.radios, 0, 64*math.pi)
216 def draw_current_ball(self):
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)
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
231 def button_release(self, widget, event):
232 if event.button == 1:
233 self.selecting = False
234 self.finish_drawing()
236 def motion_notify(self, widget, event):
237 self.draw.queue_draw()
239 if event.x > self.last_x:
244 self.last_x = event.x
246 def finish_drawing(self):
247 self.draw_current_ball()
248 self.ball_width = Ball.DEFAULT_WIDTH
250 if __name__ == '__main__':
251 Gzv().window.show_all()