diff options
author | Dan Williams <dcbw@redhat.com> | 2006-05-21 04:29:20 (GMT) |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2006-05-21 04:29:20 (GMT) |
commit | 1c54ef5002071ceb408b25a47de25c1458f7aaee (patch) | |
tree | c03d0d559ea952cac62dbb383050ee5b50247444 /sugar/chat/sketchpad | |
parent | 1a2f5cc3f73f029aab715873b32e544777ff3884 (diff) |
Make colors work in Sketch
Diffstat (limited to 'sugar/chat/sketchpad')
-rw-r--r-- | sugar/chat/sketchpad/Sketch.py | 13 | ||||
-rw-r--r-- | sugar/chat/sketchpad/SketchPad.py | 21 | ||||
-rw-r--r-- | sugar/chat/sketchpad/Toolbox.py | 10 |
3 files changed, 28 insertions, 16 deletions
diff --git a/sugar/chat/sketchpad/Sketch.py b/sugar/chat/sketchpad/Sketch.py index 8c70f8d..3504b83 100644 --- a/sugar/chat/sketchpad/Sketch.py +++ b/sugar/chat/sketchpad/Sketch.py @@ -1,25 +1,27 @@ from SVGdraw import path class Sketch: - def __init__(self): + def __init__(self, rgb): self._points = [] + self._rgb = (float(rgb[0]), float(rgb[1]), float(rgb[2])) def add_point(self, x, y): - self._points.append([x, y]) + self._points.append((x, y)) def draw(self, ctx): start = True - for [x, y] in self._points: + for (x, y) in self._points: if start: ctx.move_to(x, y) start = False else: ctx.line_to(x, y) + ctx.set_source_rgb(self._rgb[0], self._rgb[1], self._rgb[2]) ctx.stroke() def draw_to_svg(self): i = 0 - for [x, y] in self._points: + for (x, y) in self._points: coords = str(x) + ' ' + str(y) + ' ' if i == 0: path_data = 'M ' + coords @@ -28,4 +30,5 @@ class Sketch: else: path_data += coords i += 1 - return path(path_data, fill = 'none', stroke = '#000000') + color = "#%02X%02X%02X" % (255 * self._rgb[0], 255 * self._rgb[1], 255 * self._rgb[2]) + return path(path_data, fill = 'none', stroke = color) diff --git a/sugar/chat/sketchpad/SketchPad.py b/sugar/chat/sketchpad/SketchPad.py index 9319378..150b6fb 100644 --- a/sugar/chat/sketchpad/SketchPad.py +++ b/sugar/chat/sketchpad/SketchPad.py @@ -13,6 +13,7 @@ class SketchPad(gtk.DrawingArea): gtk.DrawingArea.__init__(self) self._active_sketch = None + self._rgb = (0.0, 0.0, 0.0) self._sketches = [] self.add_events(gtk.gdk.BUTTON_PRESS_MASK | @@ -23,6 +24,7 @@ class SketchPad(gtk.DrawingArea): self.connect('expose_event', self.expose) def expose(self, widget, event): + """Draw the background of the sketchpad.""" rect = self.get_allocation() ctx = widget.window.cairo_create() @@ -38,27 +40,28 @@ class SketchPad(gtk.DrawingArea): return False + def set_color(self, color): + """Sets the current drawing color of the sketchpad. + color agument should be 3-item tuple of rgb values between 0 and 1.""" + self._rgb = color + def add_sketch(self, sketch): self._sketches.append(sketch) - def add_point(self, event): - if self._active_sketch: - self._active_sketch.add_point(event.x, event.y) - self.window.invalidate_rect(None, False) - def __button_press_cb(self, widget, event): - self._active_sketch = Sketch() + self._active_sketch = Sketch(self._rgb) self.add_sketch(self._active_sketch) - self.add_point(event) def __button_release_cb(self, widget, event): - self.add_point(event) self._active_sketch = None def __motion_notify_cb(self, widget, event): - self.add_point(event) + if self._active_sketch: + self._active_sketch.add_point(event.x, event.y) + self.window.invalidate_rect(None, False) def to_svg(self): + """Return a string containing an SVG representation of this sketch.""" d = drawing() s = svg() for sketch in self._sketches: diff --git a/sugar/chat/sketchpad/Toolbox.py b/sugar/chat/sketchpad/Toolbox.py index bf56ccc..7ed814a 100644 --- a/sugar/chat/sketchpad/Toolbox.py +++ b/sugar/chat/sketchpad/Toolbox.py @@ -18,6 +18,9 @@ class ColorButton(gtk.RadioButton): self.add(drawing_area) drawing_area.show() + def color(self): + return self._rgb + def expose(self, widget, event): rect = widget.get_allocation() ctx = widget.window.cairo_create() @@ -31,7 +34,9 @@ class ColorButton(gtk.RadioButton): class Toolbox(gtk.VBox): __gsignals__ = { 'tool-selected': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, - ([gobject.TYPE_STRING])) + ([gobject.TYPE_STRING])), + 'color-selected': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, + ([gobject.TYPE_PYOBJECT])) } def __init__(self): @@ -96,4 +101,5 @@ class Toolbox(gtk.VBox): self.emit("tool-selected", tool_id) def __color_clicked_cb(self, button, rgb): - pass + self.emit("color-selected", button.color()) + |