From 13daa0d0f31a16948e207380941104bdced82467 Mon Sep 17 00:00:00 2001 From: Lincoln de Sousa Date: Tue, 12 Aug 2008 02:39:58 -0300 Subject: [PATCH] Using a gtk image instead of a pixbuf, it is really faster. The drawing area was replaced in glade file and in all references in the python code. Some other little things were made to make it work properly --- gzv.glade | 7 +++-- gzv.py | 92 ++++++++++++++++++++++--------------------------------- 2 files changed, 41 insertions(+), 58 deletions(-) diff --git a/gzv.glade b/gzv.glade index 58ae30b..b7951ed 100644 --- a/gzv.glade +++ b/gzv.glade @@ -1,6 +1,6 @@ - + Gzv @@ -344,8 +344,9 @@ True - + True + gtk-missing-image @@ -457,6 +458,7 @@ True True + 640 1 @@ -479,6 +481,7 @@ True True + 480 1 diff --git a/gzv.py b/gzv.py index 081e06a..14e0a2b 100644 --- a/gzv.py +++ b/gzv.py @@ -115,6 +115,9 @@ class NewProject(GladeLoader): self.dialog.set_transient_for(parent) def get_project(self): + if not self.dialog.run(): + return None + fname = self.wid('image').get_filename() width = self.wid('width').get_text() height = self.wid('height').get_text() @@ -139,7 +142,7 @@ class Gzv(GladeLoader): self.treeview.set_model(self.model) self.draw = self.wid('draw') - self.draw.connect('expose-event', self.expose_draw) + self.draw.connect_after('expose-event', self.expose_draw) # Starting with an empty project with no image loaded self.project = None @@ -177,15 +180,15 @@ class Gzv(GladeLoader): def on_cell_edited(self, renderer, path, value): self.balls[int(path)].name = value self.load_balls_to_treeview() - self.draw.queue_draw() def new_project(self, button): proj = NewProject(self.window) + project = proj.get_project() + proj.destroy() # This '1' was defined in the glade file - if proj.dialog.run() == 1: - self.load_project(proj.get_project()) - proj.destroy() + if project: + self.load_project(project) def open_project(self, *args): fc = gtk.FileChooserDialog(_('Choose a gzv project'), self.window, @@ -213,8 +216,23 @@ class Gzv(GladeLoader): self.project = project self.balls = self.load_balls_from_file(project.focus_points_file) self.image = project.image + + # loading the picture image and getting some useful + # information to draw it in the widget's background + try: + self.draw.set_from_file(project.image) + except gobject.GError: + msg = _("Couldn't recognize the image file format.") + dialog = gtk.MessageDialog(self.window, + gtk.DIALOG_MODAL, + gtk.MESSAGE_ERROR, + gtk.BUTTONS_CLOSE) + dialog.set_markup(msg) + dialog.run() + dialog.destroy() + return self.unload_project() + self.load_balls_to_treeview() - self.draw.queue_draw() def unload_project(self): self.project = None @@ -276,60 +294,22 @@ class Gzv(GladeLoader): if not self.image: return - # loading the picture image and getting some useful - # information to draw it in the widget's background - try: - img = gtk.gdk.pixbuf_new_from_file(self.image) - except gobject.GError: - msg = _("Couldn't recognize the image file format.") - dialog = gtk.MessageDialog(self.window, - gtk.DIALOG_MODAL, - gtk.MESSAGE_ERROR, - gtk.BUTTONS_CLOSE) - dialog.set_markup(msg) - dialog.run() - dialog.destroy() - - self.draw.stop_emission('expose-event') - return self.unload_project() - - pixels = img.get_pixels() - rowstride = img.get_rowstride() - width = img.get_width() - height = img.get_height() - gc = draw.style.black_gc - - # sets the correct size of the eventbox, to show the scrollbar - # when needed. - self.evtbox.set_size_request(width, height) - - # drawing the picture in the background of the drawing area, - # this is really important. - draw.window.draw_rgb_image(gc, 0, 0, width, height, - 'normal', pixels, rowstride, - 0, 0) - - # this call makes the ball being drown be shown correctly. self.draw_current_ball() + for i in self.balls: + self.draw_ball(i) + return False - # drawing other balls stored in the self.balls list. - ctx = draw.window.cairo_create() - ctx.fill() - - ctx.set_line_width(10.0) + def draw_ball(self, ball): + ctx = self.draw.window.cairo_create() + ctx.arc(ball.x, ball.y, ball.radius, 0, 64*math.pi) ctx.set_source_rgba(0.5, 0.0, 0.0, 0.4) - - for i in self.balls: - ctx.arc(i.x, i.y, i.radius, 0, 64*math.pi) - ctx.fill() + ctx.fill() def draw_current_ball(self): if self.start_x < 0: return - ctx = self.draw.window.cairo_create() - ctx.arc(self.start_x, self.start_y, self.radius, 0, 64*math.pi) - ctx.set_source_rgba(0.5, 0.0, 0.0, 0.4) - ctx.fill() + ball = Ball(self.start_x, self.start_y, self.radius) + self.draw_ball(ball) def button_press(self, widget, event): if event.button == 1: @@ -354,15 +334,15 @@ class Gzv(GladeLoader): self.last_x = event.x def finish_drawing(self): - self.draw_current_ball() - self.ball_width = Ball.DEFAULT_WIDTH - position = len(self.balls) ball = Ball(self.start_x, self.start_y, self.radius, '', position) self.balls.append(ball) self.model.append([position, '']) self.treeview.set_cursor(str(position), self.fpcolumn, True) + # returning to the standard radius + self.radius = Ball.DEFAULT_WIDTH + if __name__ == '__main__': Gzv().window.show_all() gtk.main() -- 2.20.1