Ticket #2708: save-restore-brush-properties.patch

File save-restore-brush-properties.patch, 16.0 KB (added by humitos, 11 years ago)

A very old patch that I was working on 10 months ago. Maybe it is useful to take some ideas or chunks of code

  • Area.py

    diff --git a/Area.py b/Area.py
    index d953005..eb346b1 100644
    a b class Area(gtk.DrawingArea): 
    917917            @param  color -- a gdk.Color object
    918918
    919919        """
     920
     921        logging.debug('Area.set_stroke_color called: %s' % color)
     922
    920923        self.gc_line.set_foreground(color)
    921924        self.gc_line.set_line_attributes(1, gtk.gdk.LINE_ON_OFF_DASH,
    922925            gtk.gdk.CAP_ROUND, gtk.gdk.JOIN_ROUND)
    class Area(gtk.DrawingArea): 
    12791282                       'fill': a Boolean value
    12801283                       'vertices': a integer
    12811284        '''
    1282         # logging.debug('Area.set_tool %s', tool)
    1283         self.tool = tool
    1284         try:
    1285             if self.tool['line size'] is not None:
    1286                 self.configure_line(self.tool['line size'])
    12871285
    1288             if self.tool['fill color'] is not None:
    1289                 self.set_fill_color(self.tool['fill color'])
    1290             else:
    1291                 # use black
    1292                 self.set_fill_color(self.black)
     1286        logging.debug('Area.set_tool %s', tool)
     1287        self.tool = tool
     1288        if self.tool['line size'] is not None:
     1289            self.configure_line(self.tool['line size'])
    12931290
    1294             if self.tool['stroke color'] is not None:
    1295                 self.set_stroke_color(self.tool['stroke color'])
    1296             else:
    1297                 # use black
    1298                 self.set_stroke_color(self.black)
     1291        if self.tool['fill color'] is not None:
     1292            self.set_fill_color(self.tool['fill color'])
     1293        else:
     1294            # use black
     1295            self.set_fill_color(self.black)
    12991296
    1300         except AttributeError:
    1301             pass
     1297        if self.tool['stroke color'] is not None:
     1298            self.set_stroke_color(self.tool['stroke color'])
     1299        else:
     1300            # use black
     1301            self.set_stroke_color(self.black)
    13021302
    13031303        self.set_tool_cursor()
    13041304
  • OficinaActivity.py

    diff --git a/OficinaActivity.py b/OficinaActivity.py
    index a74b7c1..ac594d5 100644
    a b Walter Bender (walter@laptop.org) 
    6464import gtk
    6565import logging
    6666
     67import simplejson
     68
    6769from sugar.activity import activity
    6870from sugar.graphics import style
    6971
    class OficinaActivity(activity.Activity): 
    157159            self.area.loadImageFromJournal(pixbuf)
    158160            self.center_area()
    159161
     162            # README: we need to modify:
     163            #   1. GUI
     164            #   2. Area
     165            # if not, the Are changes will not take effect
     166
     167            # MODIFY THE GUI
     168            # select the correct tool on the GUI
     169            name = self.state_restored['name']
     170            if name in ('brush', 'eraser', 'bucket'):  # 'stamp'
     171                tool = getattr(self.get_toolbar_box().tools_builder,
     172                               '_tool_' + name)
     173            elif name in ('ellipse', 'rectangle',
     174                          'line', 'freeform',
     175                          'polygon_regular', 'heart',
     176                          'parallelogram', 'arrow',
     177                          'star', 'trapezoid',
     178                          'triangle'):
     179                if name == 'polygon_regular':
     180                    name = 'polygon'
     181                tool = getattr(self.get_toolbar_box().shapes_button.props.page,
     182                               '_shape_' + name)
     183            elif name in ('text', ):
     184                tool = getattr(self.get_toolbar_box().fonts_button.props.page,
     185                               '_shape_' + name)
     186            logging.debug('self.tool_group: %s' % self.tool_group)
     187            tool.set_active(True)
     188            self.tool_group = tool
     189
     190            toolbar_box = self.get_toolbar_box()
     191            toolbar_box._stroke_color.size_spinbutton.set_value(\
     192                                        self.state_restored['line size'])
     193
     194            # brush shape
     195            toolbar_box.brush_button.set_brush_shape(\
     196                                        self.state_restored['line shape'])
     197            # brush size
     198            toolbar_box.brush_button.set_brush_size(\
     199                                        self.state_restored['line size'])
     200            toolbar_box.brush_button.set_stamp_size(\
     201                                        self.state_restored['stamp size'])
     202
     203            # brush color
     204            if self.state_restored['stroke color'] is not None:
     205                toolbar_box.brush_button.set_color(\
     206                            self.state_restored['stroke color'])
     207            # fill color
     208            if self.state_restored['fill color'] is not None:
     209                toolbar_box.shapes_button.props.page._fill_color.set_color(\
     210                            self.state_restored['fill color'])
     211
     212            toolbar_box.shapes_button.props.page._fill_color.spin_sides.set_value(self.state_restored['vertices'])
     213
     214            # brush checkbutton shape (circle, square)
     215            if self.state_restored['line shape'] == 'square':
     216                # toolbar_box._stroke_color.circle_radio_button.set_active(False)
     217                toolbar_box._stroke_color.square_radio_button.set_active(True)
     218            else:
     219                # toolbar_box._stroke_color.square_radio_button.set_active(False)
     220                toolbar_box._stroke_color.circle_radio_button.set_active(True)
     221
     222
     223            # TODO: check fonts
     224            #    Modify GUI using toolbox.py
     225            # TextToolbar (__bold_bt_cb, __italic_bt_cb)
     226
     227            # MODIFY THE AREA
     228            # set the stroke and fill color for the Area drawing
     229            colormap = self.area.get_colormap()
     230            if self.state_restored['stroke color'] is not None:
     231                stroke_color = colormap.alloc_color(\
     232                    self.state_restored['stroke color'].to_string(),
     233                                                    True, True)
     234                self.area.set_stroke_color(stroke_color)
     235            if self.state_restored['fill color'] is not None:
     236                fill_color = colormap.alloc_color(\
     237                self.state_restored['fill color'].to_string(),
     238                                                  True, True)
     239                self.area.set_fill_color(fill_color)
     240
     241            # TODO: check fonts
     242            #    Modify Area using toolbox.py
     243            # TextToolbar (__bold_bt_cb, __italic_bt_cb)
     244
     245
    160246        self.canvas.add_with_viewport(self.fixed)
    161247        # to remove the border, we need set the shadowtype
    162248        # in the viewport child of the scrolledwindow
    class OficinaActivity(activity.Activity): 
    176262                self.metadata['title'] = title
    177263            logging.error('title: %s', self.metadata['title'])
    178264
     265        self.state_restored = simplejson.loads(self.metadata['state'])
     266        for k in ('fill color', 'stroke color'):
     267            if self.state_restored[k] != None:
     268                self.state_restored[k] = \
     269                        gtk.gdk.color_parse(self.state_restored[k])
     270        logging.debug('OficinaActivity.read_file')
     271        self.area.tool = self.state_restored
     272
     273        # colormap = self.area.get_colormap()
     274        # colormap.alloc_color(color.red, color.green, color.blue)
     275        logging.debug('Loaded data state: %s' % self.state_restored)
     276
    179277    def write_file(self, file_path):
    180278        '''Save file on Sugar Journal. '''
    181279
    class OficinaActivity(activity.Activity): 
    193291        self.metadata['mime_type'] = 'image/png'
    194292        pixbuf.save(file_path, 'png', {})
    195293
     294        # TODO: here we should save brushes data, color, font, and all
     295        # those settings that we change
     296        state = {}
     297        for k in self.area.tool.keys():
     298            if k in ('fill color', 'stroke color'):
     299                if self.area.tool[k] != None:
     300                    state[k] = self.area.tool[k].to_string()
     301                else:
     302                    state[k] = None
     303            else:
     304                state[k] = self.area.tool[k]
     305        self.metadata['state'] = simplejson.dumps(state)
     306        logging.debug('Wrote data state: %s' % state)
     307
    196308    def _get_area_displacement(self):
    197309        """Return the point to use as top left corner in order to move
    198310        the drawing area and center it on the canvas.
  • toolbox.py

    diff --git a/toolbox.py b/toolbox.py
    index 25248ad..58dfb85 100644
    a b class DrawToolbarBox(ToolbarBox): 
    103103
    104104        self._activity.tool_group = None
    105105
    106         tools_builder = ToolsToolbarBuilder(self.toolbar, self._activity)
     106        self.tools_builder = ToolsToolbarBuilder(self.toolbar, self._activity)
    107107
    108         shapes_button = ToolbarButton()
    109         shapes_button.props.page = ShapesToolbar(self._activity)
    110         shapes_button.props.icon_name = 'shapes'
    111         shapes_button.props.label = _('Shapes')
    112         self.toolbar.insert(shapes_button, -1)
     108        self.shapes_button = ToolbarButton()
     109        self.shapes_button.props.page = ShapesToolbar(self._activity)
     110        self.shapes_button.props.icon_name = 'shapes'
     111        self.shapes_button.props.label = _('Shapes')
     112        self.toolbar.insert(self.shapes_button, -1)
    113113
    114         fonts_button = ToolbarButton()
    115         fonts_button.props.page = TextToolbar(self._activity)
    116         fonts_button.props.icon_name = 'format-text-size'
    117         fonts_button.props.label = _('Fonts')
    118         self.toolbar.insert(fonts_button, -1)
     114        self.fonts_button = ToolbarButton()
     115        self.fonts_button.props.page = TextToolbar(self._activity)
     116        self.fonts_button.props.icon_name = 'format-text-size'
     117        self.fonts_button.props.label = _('Fonts')
     118        self.toolbar.insert(self.fonts_button, -1)
    119119
    120120        image_button = ToolbarButton()
    121121        image_button.props.page = ImageToolbar(self._activity)
    class DrawToolbarBox(ToolbarBox): 
    134134
    135135        # TODO: workaround
    136136        # the BrushButton does not starts
    137         brush_button = tools_builder._stroke_color.color_button
    138         brush_button.set_brush_shape(self._activity.area.tool['line shape'])
    139         brush_button.set_brush_size(self._activity.area.tool['line size'])
    140         brush_button.set_stamp_size(self._activity.area.tool['stamp size'])
     137        self._stroke_color = self.tools_builder._stroke_color
     138        self.brush_button = self.tools_builder._stroke_color.color_button
     139        self.brush_button.set_brush_shape(self._activity.area.tool['line shape'])
     140        self.brush_button.set_brush_size(self._activity.area.tool['line size'])
     141        self.brush_button.set_stamp_size(self._activity.area.tool['stamp size'])
    141142        if self._activity.area.tool['stroke color'] is not None:
    142             brush_button.set_color(self._activity.area.tool['stroke color'])
     143            self.brush_button.set_color(self._activity.area.tool['stroke color'])
    143144
    144145
    145146##Make the Edit Toolbar
    class ToolsToolbarBuilder(): 
    243244        #self._stroke_color.set_icon_name('icon-stroke')
    244245        self._stroke_color.set_title(_('Brush properties'))
    245246        self._stroke_color.connect('notify::color', self._color_button_cb)
     247
    246248        item = gtk.ToolItem()
    247249        item.add(self._stroke_color)
    248250        toolbar.insert(item, -1)
    class ButtonFillColor(ColorToolButton): 
    357359        self.properties = self._activity.area.tool
    358360        self.connect('notify::color', self._color_button_cb)
    359361
     362        self.spin_sides = None
     363
    360364    def _color_button_cb(self, widget, pspec):
    361365        color = self.get_color()
    362366        self.set_fill_color(color)
    class ButtonFillColor(ColorToolButton): 
    392396        content_box.pack_start(keep_aspect_checkbutton)
    393397
    394398        # We want choose the number of sides to our polygon
    395         spin = gtk.SpinButton()
     399        self.spin_sides = gtk.SpinButton()
    396400
    397401        # This is where we set restrictions for sides in Regular Polygon:
    398402        # Initial value, minimum value, maximum value, step
    399403        adj = gtk.Adjustment(self.properties['vertices'], 3.0, 50.0, 1.0)
    400         spin.set_adjustment(adj)
    401         spin.set_numeric(True)
     404        self.spin_sides.set_adjustment(adj)
     405        self.spin_sides.set_numeric(True)
    402406
    403407        label = gtk.Label(_('Sides: '))
    404408        #For stars
    class ButtonFillColor(ColorToolButton): 
    407411        hbox = gtk.HBox()
    408412        hbox.show_all()
    409413        hbox.pack_start(label)
    410         hbox.pack_start(spin)
     414        hbox.pack_start(self.spin_sides)
    411415
    412416        content_box.pack_start(hbox)
    413417        hbox.show_all()
    414         spin.connect('value-changed', self._on_vertices_value_changed)
     418        self.spin_sides.connect('value-changed',
     419                                self._on_vertices_value_changed)
    415420
    416421        color_palette_hbox.pack_start(gtk.VSeparator(),
    417422                                     padding=style.DEFAULT_SPACING)
  • widgets.py

    diff --git a/widgets.py b/widgets.py
    index c1833fa..10dcb86 100644
    a b class ButtonStrokeColor(gtk.ToolItem): 
    187187        # Replace it with a ColorButton
    188188        self.color_button = BrushButton(has_invoker=False)
    189189        self.add(self.color_button)
     190
     191
     192
    190193        self.color_button.set_brush_size(2)
    191194        self.color_button.set_brush_shape('circle')
    192195        self.color_button.set_stamp_size(20)
    class ButtonStrokeColor(gtk.ToolItem): 
    221224
    222225    def alloc_color(self, color):
    223226        colormap = self._activity.area.get_colormap()
     227        # import logging
     228        # logging.debug('COLORMAP')
     229        # logging.debug(colormap)
     230        # logging.debug(color)
    224231        return colormap.alloc_color(color.red, color.green, color.blue)
    225232
    226233    def create_palette(self):
    class ButtonStrokeColor(gtk.ToolItem): 
    246253        self.size_spinbutton.connect('value-changed', self._on_value_changed)
    247254
    248255        # User is able to choose Shapes for 'Brush' and 'Eraser'
    249         item1 = gtk.RadioButton(None, _('Circle'))
    250         item1.set_active(True)
     256        self.circle_radio_button = gtk.RadioButton(None, _('Circle'))
     257        self.circle_radio_button.set_active(True)
    251258        image1 = gtk.Image()
    252259        pixbuf1 = gtk.gdk.pixbuf_new_from_file_at_size(
    253260                                './icons/tool-shape-ellipse.svg',
    254261                                style.SMALL_ICON_SIZE,
    255262                                style.SMALL_ICON_SIZE)
    256263        image1.set_from_pixbuf(pixbuf1)
    257         item1.set_image(image1)
     264        self.circle_radio_button.set_image(image1)
    258265
    259         item2 = gtk.RadioButton(item1, _('Square'))
     266        self.square_radio_button = gtk.RadioButton(self.circle_radio_button,
     267                                                   _('Square'))
    260268        image2 = gtk.Image()
    261269        pixbuf2 = gtk.gdk.pixbuf_new_from_file_at_size(
    262270                                './icons/tool-shape-rectangle.svg',
    263271                                style.SMALL_ICON_SIZE,
    264272                                style.SMALL_ICON_SIZE)
    265273        image2.set_from_pixbuf(pixbuf2)
    266         item2.set_image(image2)
     274        self.square_radio_button.set_image(image2)
    267275
    268         item1.connect('toggled', self._on_toggled, self.properties, 'circle')
    269         item2.connect('toggled', self._on_toggled, self.properties, 'square')
     276        self.circle_radio_button.connect('toggled', self._on_toggled,
     277                                    self.properties, 'circle')
     278        self.square_radio_button.connect('toggled', self._on_toggled,
     279                                         self.properties, 'square')
    270280
    271281        label = gtk.Label(_('Shape'))
    272282
    273283        self.vbox_brush_options.pack_start(label)
    274         self.vbox_brush_options.pack_start(item1)
    275         self.vbox_brush_options.pack_start(item2)
     284        self.vbox_brush_options.pack_start(self.circle_radio_button)
     285        self.vbox_brush_options.pack_start(self.square_radio_button)
    276286
    277287        keep_aspect_checkbutton = gtk.CheckButton(_('Keep aspect'))
    278288        ratio = self._activity.area.keep_aspect_ratio
    class ButtonStrokeColor(gtk.ToolItem): 
    330340        else:
    331341            self.properties['line size'] = size
    332342            self.color_button.set_brush_size(self.properties['line size'])
     343        import logging
     344        logging.debug(self.properties)
    333345        self._activity.area.set_tool(self.properties)
    334346
    335347    def _on_toggled(self, radiobutton, tool, shape):