diff options
author | Daniel Francis <francis@sugarlabs.org> | 2012-06-09 23:56:37 (GMT) |
---|---|---|
committer | Daniel Francis <francis@sugarlabs.org> | 2012-06-09 23:56:37 (GMT) |
commit | 12673a2278305a837a61fbd650facc484f38f8d6 (patch) | |
tree | 8b67a7c773a9be1ca6d357a8e9a125e19f883264 /activity.py | |
parent | dad089c1c91b0543017a8ad545bcd740ee7be00a (diff) |
Cairo migrated
Diffstat (limited to 'activity.py')
-rw-r--r-- | activity.py | 163 |
1 files changed, 81 insertions, 82 deletions
diff --git a/activity.py b/activity.py index 4d3d1ab..13b8a03 100644 --- a/activity.py +++ b/activity.py @@ -219,24 +219,23 @@ class Graph(gtk.DrawingArea): return self.y_max - (y * (self.y_max - self.y_min) / self.canvas_height) def plot(self): - self.pix_map.draw_rectangle(self.get_style().white_gc, - True, - 0, - 0, - self.canvas_width, - self.canvas_height) + self.context.set_source_rgb(1, 1, 1) + self.context.rectangle(0, + 0, + self.canvas_width, + self.canvas_height) + self.context.fill() + self.context.set_source_rgb(0, 0, 0) if (self.scale_style == "cust"): #draw cross - self.pix_map.draw_lines(self.gc['black'], - [(int(round(self.canvas_x(0))), 0), - (int(round(self.canvas_x(0))), - self.canvas_height)]) - self.pix_map.draw_lines(self.gc['black'], - [(0, int(round(self.canvas_y(0)))), - (self.canvas_width, - int(round(self.canvas_y(0))))]) + self.context.move_to(int(round(self.canvas_x(0))), 0) + self.context.line_to(int(round(self.canvas_x(0))), + self.canvas_height) + self.context.move_to(0, int(round(self.canvas_y(0)))) + self.context.line_to(self.canvas_width, + int(round(self.canvas_y(0)))) # old style axis marks # pixel interval between marks iv = self.x_scale * self.canvas_width / (self.x_max - self.x_min) @@ -246,21 +245,19 @@ class Graph(gtk.DrawingArea): #multiples of iv, #cause adding of any error in iv, so keep iv as float # use round(), to get to closest pixel, int() to prevent warning - self.pix_map.draw_lines(self.gc['black'], - [(int(round(os + i * iv)), - int(round(self.canvas_y(0) - 5))), - (int(round(os + i * iv)), - int(round(self.canvas_y(0) + 5)))]) + self.context.move_to(int(round(os + i * iv)), + int(round(self.canvas_y(0) - 5))) + self.context.line_to(int(round(os + i * iv)), + int(round(self.canvas_y(0) + 5))) # and the y-axis iv = self.y_scale * self.canvas_height / (self.y_max - self.y_min) os = self.canvas_y(0) % iv for i in xrange(int(self.canvas_height / iv + 1)): - self.pix_map.draw_lines(self.gc['black'], - [(int(round(self.canvas_x(0) - 5)), - int(round(i * iv + os))), - (int(round(self.canvas_x(0) + 5)), - int(round(i * iv + os)))]) + self.context.move_to(int(round(self.canvas_x(0) - 5)), + int(round(i * iv + os))), + self.context.line_to(int(round(self.canvas_x(0) + 5)), + int(round(i * iv + os))) else: #new style @@ -289,12 +286,10 @@ class Graph(gtk.DrawingArea): numbers_y_pos = -10 # draw cross - self.pix_map.draw_lines(self.gc['black'], - [(center_x_pix, 0), - (center_x_pix, self.canvas_height)]) - self.pix_map.draw_lines(self.gc['black'], - [(0, center_y_pix), - (self.canvas_width, center_y_pix)]) + self.context.move_to(center_x_pix, 0) + self.context.line_to(center_x_pix, self.canvas_height) + self.context.move_to(0, center_y_pix) + self.context.line_to(self.canvas_width, center_y_pix) for i in marks(self.x_min / factor, self.x_max / factor): label = '%g' % i @@ -302,11 +297,10 @@ class Graph(gtk.DrawingArea): label += '\xCF\x80' i = i * factor - self.pix_map.draw_lines(self.gc['black'], - [(int(round(self.canvas_x(i))), - center_y_pix - 5), - (int(round(self.canvas_x(i))), - center_y_pix + 5)]) + self.context.move_to(int(round(self.canvas_x(i))), + center_y_pix - 5), + self.context.line_to(int(round(self.canvas_x(i))), + center_y_pix + 5) self.layout.set_text(label) extents = self.layout.get_pixel_extents()[1] @@ -314,19 +308,17 @@ class Graph(gtk.DrawingArea): adjust = extents[3] else: adjust = 0 - self.pix_map.draw_layout(self.gc['black'], - int(round(self.canvas_x(i))), - center_y_pix + numbers_y_pos - adjust, - self.layout) + self.context.move_to(int(round(self.canvas_x(i))), + center_y_pix + numbers_y_pos - adjust) + self.context.show_layout(self.layout) for i in marks(self.y_min, self.y_max): label = '%g' % i - self.pix_map.draw_lines(self.gc['black'], - [(center_x_pix - 5, - int(round(self.canvas_y(i)))), - (center_x_pix + 5, - int(round(self.canvas_y(i))))]) + self.context.move_to(center_x_pix - 5, + int(round(self.canvas_y(i)))) + self.context.line_to(center_x_pix + 5, + int(round(self.canvas_y(i)))) self.layout.set_text(label) extents = self.layout.get_pixel_extents()[1] @@ -334,27 +326,25 @@ class Graph(gtk.DrawingArea): adjust = extents[2] else: adjust = 0 - self.pix_map.draw_layout(self.gc['black'], - center_x_pix + numbers_x_pos - adjust, - int(round(self.canvas_y(i))), - self.layout) + self.context.move_to(center_x_pix + numbers_x_pos - adjust, + int(round(self.canvas_y(i)))) + self.context.show_layout(self.layout) # minor marks for i in marks(self.x_min / factor, self.x_max / factor, minor=10): i = i * factor - self.pix_map.draw_lines(self.gc['black'], - [(int(round(self.canvas_x(i))), - center_y_pix - 2), - (int(round(self.canvas_x(i))), - center_y_pix + 2)]) + self.context.move_to(int(round(self.canvas_x(i))), + center_y_pix - 2) + self.context.line_to(int(round(self.canvas_x(i))), + center_y_pix + 2) for i in marks(self.y_min, self.y_max, minor=10): label = '%g' % i - self.pix_map.draw_lines(self.gc['black'], - [(center_x_pix - 2, - int(round(self.canvas_y(i)))), - (center_x_pix + 2, - int(round(self.canvas_y(i))))]) + self.context.move_to(center_x_pix - 2, + int(round(self.canvas_y(i)))) + self.context.line_to(center_x_pix + 2, + int(round(self.canvas_y(i)))) + self.context.stroke() plots = [] # precompile the functions @@ -386,36 +376,37 @@ class Graph(gtk.DrawingArea): if y_c < 0 or y_c > self.canvas_height: raise ValueError + self.context.set_source_rgb(e[2][0], e[2][1], e[2][2]) if connect_points and self.prev_y[e[1]] is not None: - self.pix_map.draw_lines(e[2], - [(i, - self.prev_y[e[1]]), - (i + x_res, - y_c)]) + self.context.move_to(i, + self.prev_y[e[1]]) + self.context.line_to(i + x_res, + y_c) else: - self.pix_map.draw_points(e[2], [(i + x_res, y_c)]) + self.context.rectangle(i + x_res, y_c, 1, 1) + #self.pix_map.draw_points(e[2], [(i + x_res, y_c)]) + self.context.stroke() self.prev_y[e[1]] = y_c except Exception, exc: print exc #print "Error at %d: %s" % (x, sys.exc_value) self.prev_y[e[1]] = None - self.draw_drawable() + self.context.clip() + + #self.draw_drawable() def _configure_event_cb(self, widget, event): x, y, w, h = widget.get_allocation() - self.pix_map = gtk.gdk.Pixmap(widget.window, w, h) + self.context = widget.get_window().cairo_create() # make colors self.gc = dict() for name, color in (('black', (0, 0, 0)), - ('red', (32000, 0, 0)), - ('blue', (0, 0, 32000)), - ('green', (0, 32000, 0))): - self.gc[name] = self.pix_map.new_gc() - self.gc[name].set_rgb_fg_color(gtk.gdk.Color(red=color[0], - green=color[1], - blue=color[2])) + ('red', (1, 0, 0)), + ('blue', (0, 0, 1)), + ('green', (0, 1, 0))): + self.gc[name] = color self.layout = pango.Layout(widget.create_pango_context()) self.canvas_width = w self.canvas_height = h @@ -430,8 +421,11 @@ class Graph(gtk.DrawingArea): def _expose_event_cb(self, widget, event): x, y, w, h = event.area - widget.window.draw_drawable(widget.get_style().fg_gc[gtk.STATE_NORMAL], - self.pix_map, x, y, x, y, w, h) + self.context = widget.get_window().cairo_create() + self.plot() + + #widget.window.draw_drawable(widget.get_style().fg_gc[gtk.STATE_NORMAL], + # self.pix_map, x, y, x, y, w, h) return False def canvas_y(self, y): @@ -443,13 +437,13 @@ class Graph(gtk.DrawingArea): class GraphPlotter(activity.Activity): - def write_file(self, file_path): - x, y, w, h = self.graph.get_allocation() - pix_buffer = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, w, h) - pix_buffer.get_from_drawable(self.graph.pix_map, - self.graph.pix_map.get_colormap(), - 0, 0, 0, 0, w, h) - pix_buffer.save(file_path, "png") + #def write_file(self, file_path): + #x, y, w, h = self.graph.get_allocation() + #pix_buffer = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, w, h) + #pix_buffer.get_from_drawable(self.graph.pix_map, + #self.graph.pix_map.get_colormap(), + #0, 0, 0, 0, w, h) + #pix_buffer.save(file_path, "png") def parameter_entries_repopulate(self): # set text in entries for parameters @@ -601,6 +595,8 @@ class GraphPlotter(activity.Activity): y3 = self.y3_entry.get_text() self.graph.plot() + self.graph.hide() + self.graph.show() def toggle_connect(self, widget, event=None): """ @@ -625,6 +621,9 @@ Toggle between a graph that connects points with lines and one that does not self.scale_box.show() self.plot(None) + def read_file(self, path): + pass + def __init__(self, handle): activity.Activity.__init__(self, handle) self.y1 = y1 |