From 181c0a8dfc5fd434a978498840586e3d2b3637ba Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Mon, 02 Sep 2013 14:31:37 +0000 Subject: Save and restore tools state in the metadata - SL #2708 Signed-off-by: Puneet Kaur Acked-by: Gonzalo Odiard --- diff --git a/Area.py b/Area.py index d0f4f72..80de9a2 100644 --- a/Area.py +++ b/Area.py @@ -1670,7 +1670,8 @@ class Area(Gtk.DrawingArea): hotspot_x, hotspot_y) except GObject.GError: cursor = None - self.get_window().set_cursor(cursor) + if self.get_window() is not None: + self.get_window().set_cursor(cursor) def getout(self, undo=False, clear_selection=True): """ diff --git a/Desenho.py b/Desenho.py index 7c734b1..fd5aea4 100644 --- a/Desenho.py +++ b/Desenho.py @@ -212,6 +212,7 @@ class Desenho: widget.desenha = False size = widget.tool['line size'] shape = widget.tool['line shape'] + if shape == 'circle': if last: widget.drawing_ctx.set_line_width(size) @@ -226,6 +227,9 @@ class Desenho: coords[1]) widget.drawing_ctx.arc(coords[0], coords[1], size / 2, 0., 2 * math.pi) + # when activity starts with rainbow tool, need this to + # not paint the background + widget.drawing_ctx.set_source_rgba(1.0, 1.0, 1.0, 0.0) widget.drawing_ctx.fill() elif shape == 'square': @@ -249,6 +253,9 @@ class Desenho: coords[1] - size / 2) widget.drawing_ctx.rectangle(coords[0] - size / 2, coords[1] - size / 2, size, size) + # when activity starts with rainbow tool, need this to + # not paint the background + widget.drawing_ctx.set_source_rgba(1.0, 1.0, 1.0, 0.0) widget.drawing_ctx.fill() if last: diff --git a/OficinaActivity.py b/OficinaActivity.py index b30b203..02fb13c 100644 --- a/OficinaActivity.py +++ b/OficinaActivity.py @@ -65,6 +65,7 @@ from gi.repository import Gtk from gi.repository import Gdk from gi.repository import GObject import logging +import json from sugar3.activity import activity from sugar3.graphics import style @@ -108,6 +109,8 @@ class OficinaActivity(activity.Activity): self._sw.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC) self.set_canvas(self._sw) + self.toolset_intialize_from_journal() + toolbar_box = DrawToolbarBox(self) toolbar_box.show_all() @@ -209,6 +212,8 @@ class OficinaActivity(activity.Activity): self.area.getout() self.area.drawing_canvas.write_to_png(file_path) self.metadata['mime_type'] = 'image/png' + self.metadata['state'] = json.dumps(self.area.tool) + logging.debug('Wrote metadata[\'state\']: %s', self.metadata['state']) def _get_area_displacement(self): """Return the point to use as top left corner in order to move @@ -231,3 +236,10 @@ class OficinaActivity(activity.Activity): def move_textview(self, dx, dy): x, y = self._get_area_displacement() self.fixed.move(self.textview, x + dx, y + dy) + + def toolset_intialize_from_journal(self): + try: + self.area.tool = json.loads(self.metadata['state']) + logging.debug('self.area.tool %s', self.area.tool) + except Exception as e: + logging.error("exception %s", e) diff --git a/toolbox.py b/toolbox.py index 0bd089c..645e81b 100644 --- a/toolbox.py +++ b/toolbox.py @@ -107,6 +107,23 @@ def add_menu(icon_name, tooltip, tool_name, button, activate_cb): class DrawToolbarBox(ToolbarBox): """Create toolbars for the activity""" + #dictionary - tool name : tool icon name + tool_icon_name = {'ellipse': 'tool-shape-ellipse', + 'rectangle': 'tool-shape-rectangle', + 'line': 'tool-shape-line', + 'freeform': 'tool-shape-freeform', + 'heart': 'tool-shape-heart', + 'parallelogram': 'tool-shape-parallelogram', + 'arrow': 'tool-shape-arrow', + 'star': 'tool-shape-star', + 'trapezoid': 'tool-shape-trapezoid', + 'triangle': 'tool-shape-triangle', + 'polygon_regular': 'tool-shape-polygon', + 'brush': 'tool-brush', + 'eraser': 'tool-eraser', + 'bucket': 'tool-bucket', + 'picker': 'tool-picket'} + def __init__(self, activity): self._activity = activity @@ -130,14 +147,17 @@ class DrawToolbarBox(ToolbarBox): self._activity.tool_group = None - tools_builder = ToolsToolbarBuilder(self.toolbar, self._activity, - self._fill_color_button) + self.tools_builder = ToolsToolbarBuilder(self.toolbar, self._activity, + self._fill_color_button) - shapes_button = DrawToolButton('shapes', - self._activity.tool_group, _('Shapes')) - self.toolbar.insert(shapes_button, -1) - shapes_builder = ShapesToolbarBuilder(self._activity, shapes_button, - self._fill_color_button) + self.shapes_button = DrawToolButton('shapes', + self._activity.tool_group, + _('Shapes')) + self.toolbar.insert(self.shapes_button, -1) + self.shapes_builder = ShapesToolbarBuilder(self._activity, + self.shapes_button, + self._fill_color_button) + self.initialize_brush_shape_tools() self.toolbar.insert(item_fill_color, -1) @@ -165,7 +185,7 @@ class DrawToolbarBox(ToolbarBox): # TODO: workaround # the BrushButton does not starts - self.brush_button = tools_builder._stroke_color.color_button + self.brush_button = self.tools_builder._stroke_color.color_button area = self._activity.area self.brush_button.set_brush_shape(area.tool['line shape']) self.brush_button.set_brush_size(area.tool['line size']) @@ -180,6 +200,35 @@ class DrawToolbarBox(ToolbarBox): stroke_color = Gdk.Color(red, green, blue) self.brush_button.set_color(stroke_color) + def initialize_brush_shape_tools(self): + tool_name = self._activity.area.tool['name'] + if tool_name in ('brush', 'eraser', 'bucket', 'picker'): + #make the brush tool group + self.tools_builder._tool_brush.set_active(True) + #set the icon + self.tools_builder._tool_brush.set_icon_name( + self.tool_icon_name[tool_name]) + self.tools_builder.properties['name'] = tool_name + self._fill_color_button.set_sensitive(False) + elif tool_name in ('ellipse', 'rectangle', 'line', 'freeform', 'heart', + 'parallelogram', 'arrow', 'star', 'trapezoid', + 'triangle', 'polygon_regular'): + #need to make the shapes tool group active + self.shapes_button.set_active(True) + #set the icon + self.shapes_builder._tool_button.set_icon_name( + self.tool_icon_name[tool_name]) + self.shapes_builder._tool_name = tool_name + self.shapes_builder.properties['name'] = tool_name + self._fill_color_button.set_sensitive(True) + + #setting the fill color + cairo_fill_color = self._activity.area.tool['cairo_fill_color'] + red = cairo_fill_color[0] * 65535 + green = cairo_fill_color[1] * 65535 + blue = cairo_fill_color[2] * 65535 + self._fill_color_button.color = Gdk.Color(red, green, blue) + ##Make the Edit Toolbar class DrawEditToolbar(EditToolbar): diff --git a/widgets.py b/widgets.py index e2067ea..ab30df8 100644 --- a/widgets.py +++ b/widgets.py @@ -188,8 +188,8 @@ class ButtonStrokeColor(Gtk.ToolItem): # Replace it with a ColorButton self.color_button = BrushButton(has_invoker=False) self.add(self.color_button) - self.color_button.set_brush_size(2) - self.color_button.set_brush_shape('circle') + self.color_button.set_brush_size(self.properties['line size']) + self.color_button.set_brush_shape(self.properties['line shape']) self.color_button.set_stamp_size(20) # The following is so that the behaviour on the toolbar is correct. @@ -237,6 +237,7 @@ class ButtonStrokeColor(Gtk.ToolItem): # This is where we set restrictions for size: # Initial value, minimum value, maximum value, step + adj = Gtk.Adjustment(self.properties['line size'], 1.0, 100.0, 1.0) self.size_scale = Gtk.HScale() self.size_scale.set_adjustment(adj) @@ -268,12 +269,16 @@ class ButtonStrokeColor(Gtk.ToolItem): self.custom_box.pack_start(self.vbox_brush_options, True, True, 0) item1 = RadioToolButton() item1.set_icon_name('tool-shape-ellipse') - item1.set_active(True) item2 = RadioToolButton() item2.set_icon_name('tool-shape-rectangle') item2.props.group = item1 + if self.properties['line shape'] == 'circle': + item1.set_active(True) + else: + item2.set_active(True) + item1.connect('toggled', self._on_toggled, self.properties, 'circle') item2.connect('toggled', self._on_toggled, self.properties, 'square') -- cgit v0.9.1