From 2d600887c7612aff5be84e0b50b2df02c1fe3bf3 Mon Sep 17 00:00:00 2001 From: Manuel QuiƱones Date: Mon, 26 Mar 2012 07:07:45 +0000 Subject: Read and write image and metadata (stroke width for now) Signed-off-by: Manuel QuiƱones --- diff --git a/drawing.py b/drawing.py index 3e73f6f..a77f795 100644 --- a/drawing.py +++ b/drawing.py @@ -20,6 +20,7 @@ from gettext import gettext as _ import logging import gtk +import gobject import cairo @@ -28,6 +29,11 @@ STROKE_MAX_POINTS = 80 class Drawing(gtk.DrawingArea): + __gsignals__ = {'width-changed': (gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, + (gobject.TYPE_INT,)), + } + def __init__(self, parent): super(Drawing, self).__init__() @@ -63,15 +69,16 @@ class Drawing(gtk.DrawingArea): self.connect("motion-notify-event", self._motion_cb) self.connect("button-release-event", self._release_cb) - def setup(self, width, height): + def setup(self, width, height, clear=True): """Setup a blank canvas of specified size.""" logging.debug("drawing set up") - self._drawing_canvas = cairo.ImageSurface(cairo.FORMAT_ARGB32, - width, height) - context = cairo.Context(self._drawing_canvas) - context.rectangle(0, 0, width, height) - context.set_source_rgb(1.0, 1.0, 1.0) - context.fill() + if clear: + self._drawing_canvas = cairo.ImageSurface(cairo.FORMAT_ARGB32, + width, height) + context = cairo.Context(self._drawing_canvas) + context.rectangle(0, 0, width, height) + context.set_source_rgb(1.0, 1.0, 1.0) + context.fill() self.queue_draw() def set_sharing(self, share=True): @@ -93,6 +100,7 @@ class Drawing(gtk.DrawingArea): def set_stroke_width(self, width): self._settings['stroke width'] = width + self.emit('width-changed', width) def get_stroke_width(self): return self._settings['stroke width'] @@ -184,3 +192,14 @@ class Drawing(gtk.DrawingArea): # Get context from widget and paint: self._set_stroke_context(context) self._paint_stroke(context, self._stroke_points) + + def load_png(self, file_path): + logging.debug("reading from png") + self._drawing_canvas = cairo.ImageSurface.create_from_png(file_path) + self.setup(self._drawing_canvas.get_width(), + self._drawing_canvas.get_height(), + clear=False) + + def save_png(self, file_path): + logging.debug("writing to png") + self._drawing_canvas.write_to_png(file_path) diff --git a/paintwithme.py b/paintwithme.py index 6052cba..3a3f568 100644 --- a/paintwithme.py +++ b/paintwithme.py @@ -18,6 +18,7 @@ from gettext import gettext as _ import logging +import json from sugar.activity import activity @@ -29,6 +30,7 @@ from sugar.presence.tubeconn import TubeConnection from toolbar import PaintToolbar from drawing import Drawing +# FIXME use json from standard lib from utils import json_load, json_dump @@ -66,6 +68,26 @@ class PaintWithMeActivity(activity.Activity): self._setup_handle = self.canvas.connect('size_allocate', size_allocate_cb) + def read_file(self, file_path): + """Read from Sugar Journal.""" + self._drawing.load_png(file_path) + + state = json.loads(self.metadata['state']) + logging.debug("read_file") + logging.debug(state) + self._drawing.set_stroke_width(state['width']) + + def write_file(self, file_path): + """Write to Sugar Journal.""" + self.metadata['mime_type'] = 'image/png' + self._drawing.save_png(file_path) + + state = {} + state['width'] = self._drawing.get_stroke_width() + self.metadata['state'] = json.dumps(state) + logging.debug("write_file") + logging.debug(state) + # Collaboration-related methods below: def _setup_presence_service(self): diff --git a/toolbar.py b/toolbar.py index dfb3a82..8490ef5 100644 --- a/toolbar.py +++ b/toolbar.py @@ -80,11 +80,18 @@ class PaintToolbar(ToolbarBox): def set_drawing(self, drawing): self._drawing = drawing + self._drawing.connect('width-changed', self._width_changed_cb) self._update_buttons() + def _update_width_spin(self, width): + self._width_spin.props.value = width + def _update_buttons(self): width = self._drawing.get_stroke_width() - self._width_spin.props.value = width + self._update_width_spin(width) + + def _width_changed_cb(self, widget, width): + self._update_width_spin(width) def _width_spin_cb(self, width_spin): width = width_spin.get_value_as_int() -- cgit v0.9.1