diff options
-rw-r--r-- | Area.py | 228 | ||||
-rw-r--r-- | Desenho.py | 6 | ||||
-rw-r--r-- | OficinaActivity.py | 15 | ||||
-rw-r--r-- | toolbox.py | 649 |
4 files changed, 468 insertions, 430 deletions
@@ -95,8 +95,25 @@ class Area(gtk.DrawingArea): self.connect("button_release_event", self.mouseup) self.set_extension_events(gtk.gdk.EXTENSION_EVENTS_CURSOR) - ##Define which tool is been used - self.tool = None + ## Define which tool is been used. It is now described as a dictionnary, + ## with the following keys: + ## - 'name' : a string + ## - 'line size' : a integer + ## - 'fill color' : a gtk.gdk.Color object + ## - 'stroke color' : a gtk.gdk.Color object + ## - 'line shape' : a string - 'circle' or 'square', for now + ## - 'fill' : a Boolean value + ## - 'vertices' : a integer + ## All values migth be None, execept in 'name' key. + self.tool = { + 'name' : 'pencil', + 'line size' : 2, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : None + } self.desenha = False self.selmove = False self.sel_get_out = False @@ -121,8 +138,7 @@ class Area(gtk.DrawingArea): self.janela = janela self.d = Desenho(self) self.line_size = 2 - self.brush_shape = 'circle' - self.eraser_shape = 'circle' + self.line_shape = 'circle' self.last = -1, -1 self.rainbow_counter = 0 @@ -140,7 +156,7 @@ class Area(gtk.DrawingArea): self.undo_list=[] ##Number of sides for regular polygon - self.polygon_sides = 5 + self.vertices = 5 ##Shapes will be filled or not? self.fill = True @@ -194,6 +210,10 @@ class Area(gtk.DrawingArea): self.enableUndo(widget) + # Setting a initial tool + # If not set here, cursor icon can't be load + self.set_tool(self.tool) + return True def configure_line(self, size): @@ -240,7 +260,7 @@ class Area(gtk.DrawingArea): width, height = self.window.get_size() # text coords = int(event.x), int(event.y) - if self.tool is 'text': + if self.tool['name'] == 'text': self.d.text(widget,event) # This fixes a bug that made the text viewer get stuck in the canvas @@ -259,26 +279,26 @@ class Area(gtk.DrawingArea): self.estadoTexto = 0 self.janela._textview.hide() - if not self.selmove or self.tool != 'marquee-rectangular': + if not self.selmove or self.tool['name'] != 'marquee-rectangular': self.oldx, self.oldy = coords - if self.selmove and self.tool != 'marquee-rectangular': #get out of the func selection + if self.selmove and self.tool['name'] != 'marquee-rectangular': #get out of the func selection self.pixmap.draw_drawable(self.gc, self.pixmap_temp, 0,0,0,0, width, height) self.selmove = False self.pixbuf_sel = None self.enableUndo(widget) - if self.tool == 'eraser': + if self.tool['name'] == 'eraser': self.last = -1, -1 - self.d.eraser(widget, coords, self.last, self.line_size, self.eraser_shape) + self.d.eraser(widget, coords, self.last, self.line_size, self.tool['line shape']) self.last = coords - elif self.tool == 'brush': + elif self.tool['name'] == 'brush': self.last = -1, -1 - self.d.brush(widget, coords, self.last, self.line_size, self.brush_shape) + self.d.brush(widget, coords, self.last, self.line_size, self.tool['line shape']) self.last = coords - elif self.tool == 'rainbow': + elif self.tool['name'] == 'rainbow': self.last = -1, -1 - self.d.rainbow(widget, coords, self.last, self.rainbow_counter,self.line_size, self.brush_shape) + self.d.rainbow(widget, coords, self.last, self.rainbow_counter,self.line_size, self.tool['line shape']) self.last = coords - elif self.tool == 'polygon': + elif self.tool['name'] == 'polygon': self.configure_line(self.line_size) x , y, state = event.window.get_pointer() @@ -309,76 +329,76 @@ class Area(gtk.DrawingArea): coords = int(x), int(y) if state & gtk.gdk.BUTTON1_MASK and self.pixmap != None: - if self.tool == 'eraser': - self.d.eraser(widget, coords, self.last, self.line_size, self.eraser_shape) + if self.tool['name'] == 'eraser': + self.d.eraser(widget, coords, self.last, self.line_size, self.tool['line shape']) self.last = coords - elif self.tool == 'brush': - self.d.brush(widget, coords, self.last, self.line_size, self.brush_shape) + elif self.tool['name'] == 'brush': + self.d.brush(widget, coords, self.last, self.line_size, self.tool['line shape']) self.last = coords - elif self.tool == 'rainbow': - self.d.rainbow(widget, coords, self.last, self.rainbow_counter,self.line_size, self.brush_shape) + elif self.tool['name'] == 'rainbow': + self.d.rainbow(widget, coords, self.last, self.rainbow_counter,self.line_size, self.tool['line shape']) self.rainbow_counter += 1 if self.rainbow_counter > 11: self.rainbow_counter = 0 self.last = coords if self.desenha: - if self.tool == 'line': + if self.tool['name'] == 'line': self.configure_line(self.line_size) self.d.line(widget, coords) - elif self.tool == 'pencil': + elif self.tool['name'] == 'pencil': self.configure_line(self.line_size) self.d.pencil(widget, coords) - elif self.tool == 'ellipse': + elif self.tool['name'] == 'ellipse': self.configure_line(self.line_size) - self.d.circle(widget,coords,True,self.fill) + self.d.circle(widget,coords,True,self.tool['fill']) - elif self.tool == 'rectangle': + elif self.tool['name'] == 'rectangle': self.configure_line(self.line_size) - self.d.square(widget,coords,True,self.fill) + self.d.square(widget,coords,True,self.tool['fill']) - elif self.tool == 'marquee-rectangular' and not self.selmove: + elif self.tool['name'] == 'marquee-rectangular' and not self.selmove: x1, y1, x2, y2 = self.d.selection(widget,coords,True,False) self._set_selection_bounds(x1, y1, x2, y2) # selected - elif self.tool == 'marquee-rectangular' and self.selmove: + elif self.tool['name'] == 'marquee-rectangular' and self.selmove: if self.pixbuf_sel!=None: self.d.moveSelection(widget,coords,True,self.pixbuf_sel) else: self.d.moveSelection(widget,coords) - elif self.tool == 'polygon': + elif self.tool['name'] == 'polygon': self.configure_line(self.line_size) - self.d.polygon(widget,coords,True,self.fill) + self.d.polygon(widget,coords,True,self.tool['fill']) - elif self.tool == 'triangle': + elif self.tool['name'] == 'triangle': self.configure_line(self.line_size) - self.d.triangle(widget,coords,True,self.fill) + self.d.triangle(widget,coords,True,self.tool['fill']) - elif self.tool == 'trapezoid': + elif self.tool['name'] == 'trapezoid': self.configure_line(self.line_size) - self.d.trapezoid(widget,coords,True,self.fill) + self.d.trapezoid(widget,coords,True,self.tool['fill']) - elif self.tool == 'arrow': + elif self.tool['name'] == 'arrow': self.configure_line(self.line_size) - self.d.arrow(widget,coords,True,self.fill) + self.d.arrow(widget,coords,True,self.tool['fill']) - elif self.tool == 'parallelogram': + elif self.tool['name'] == 'parallelogram': self.configure_line(self.line_size) - self.d.parallelogram(widget,coords,True,self.fill) + self.d.parallelogram(widget,coords,True,self.tool['fill']) - elif self.tool == 'star': + elif self.tool['name'] == 'star': self.configure_line(self.line_size) - self.d.star(widget,coords,self.polygon_sides,True,self.fill) + self.d.star(widget,coords,self.tool['vertices'],True,self.tool['fill']) - elif self.tool == 'polygon_regular': + elif self.tool['name'] == 'polygon_regular': self.configure_line(self.line_size) - self.d.polygon_regular(widget,coords,self.polygon_sides,True,self.fill) + self.d.polygon_regular(widget,coords,self.tool['vertices'],True,self.tool['fill']) - elif self.tool == 'heart': + elif self.tool['name'] == 'heart': self.configure_line(self.line_size) - self.d.heart(widget,coords,True,self.fill) + self.d.heart(widget,coords,True,self.tool['fill']) def mouseup(self,widget,event): @@ -392,20 +412,20 @@ class Area(gtk.DrawingArea): coords = int(event.x), int(event.y) width, height = self.window.get_size() if self.desenha == True: - if self.tool == 'line': + if self.tool['name'] == 'line': self.pixmap.draw_line(self.gc_line,self.oldx,self.oldy, int (event.x), int(event.y)) widget.queue_draw() self.enableUndo(widget) - elif self.tool == 'ellipse': - self.d.circle(widget,coords,False,self.fill) + elif self.tool['name'] == 'ellipse': + self.d.circle(widget,coords,False,self.tool['fill']) self.enableUndo(widget) - elif self.tool == 'rectangle': - self.d.square(widget,coords,False,self.fill) + elif self.tool['name'] == 'rectangle': + self.d.square(widget,coords,False,self.tool['fill']) self.enableUndo(widget) - elif self.tool == 'marquee-rectangular': + elif self.tool['name'] == 'marquee-rectangular': if self.selmove == False: self.pixmap_temp.draw_drawable(self.gc,self.pixmap, 0,0,0,0, width, height) self.pixmap_sel.draw_drawable(self.gc,self.pixmap, 0,0,0,0, width, height) @@ -423,44 +443,44 @@ class Area(gtk.DrawingArea): self.enableUndo(widget) self.emit('selected') - elif self.tool == 'polygon': - self.d.polygon(widget, coords, False, self.fill) + elif self.tool['name'] == 'polygon': + self.d.polygon(widget, coords, False, self.tool['fill']) - elif self.tool == 'bucket': + elif self.tool['name'] == 'bucket': width, height = self.window.get_size() fill(self.pixmap, self.gc, coords[0], coords[1], width, height, self.gc_line.foreground.pixel) widget.queue_draw() self.enableUndo(widget) - elif self.tool == 'triangle': - self.d.triangle(widget,coords,False,self.fill) + elif self.tool['name'] == 'triangle': + self.d.triangle(widget,coords,False,self.tool['fill']) self.enableUndo(widget) - elif self.tool == 'trapezoid': - self.d.trapezoid(widget,coords,False,self.fill) + elif self.tool['name'] == 'trapezoid': + self.d.trapezoid(widget,coords,False,self.tool['fill']) self.enableUndo(widget) - elif self.tool == 'arrow': - self.d.arrow(widget,coords,False,self.fill) + elif self.tool['name'] == 'arrow': + self.d.arrow(widget,coords,False,self.tool['fill']) self.enableUndo(widget) - elif self.tool == 'parallelogram': - self.d.parallelogram(widget,coords,False,self.fill) + elif self.tool['name'] == 'parallelogram': + self.d.parallelogram(widget,coords,False,self.tool['fill']) self.enableUndo(widget) - elif self.tool == 'star': - self.d.star(widget,coords,self.polygon_sides,False,self.fill) + elif self.tool['name'] == 'star': + self.d.star(widget,coords,self.tool['vertices'],False,self.tool['fill']) self.enableUndo(widget) - elif self.tool == 'polygon_regular': - self.d.polygon_regular(widget,coords,self.polygon_sides,False,self.fill) + elif self.tool['name'] == 'polygon_regular': + self.d.polygon_regular(widget,coords,self.tool['vertices'],False,self.tool['fill']) self.enableUndo(widget) - elif self.tool == 'heart': - self.d.heart(widget,coords,False,self.fill) + elif self.tool['name'] == 'heart': + self.d.heart(widget,coords,False,self.tool['fill']) self.enableUndo(widget) - if self.tool == 'brush' or self.tool == 'eraser' or self.tool == 'rainbow' or self.tool== 'pencil' : + if self.tool['name'] == 'brush' or self.tool['name'] == 'eraser' or self.tool['name'] == 'rainbow' or self.tool['name'] == 'pencil' : self.last = -1, -1 widget.queue_draw() self.enableUndo(widget) @@ -498,7 +518,7 @@ class Area(gtk.DrawingArea): #special case for func polygon - if self.tool == 'polygon': + if self.tool['name'] == 'polygon': self.polygon_start = True #start the polygon again @@ -641,7 +661,7 @@ class Area(gtk.DrawingArea): self._set_selection_bounds(x1, y1, x2, y2) self.pixmap_sel.draw_rectangle(self.gc_selection, False ,0,0,size[0],size[1]) self.sx, self.sy = size - self.tool = 'marquee-rectangular' + self.tool['name'] = 'marquee-rectangular' self.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.FLEUR)) self.emit('selected') else: @@ -855,7 +875,7 @@ class Area(gtk.DrawingArea): self._set_selection_bounds(x0, y0, x1, y1) self.pixmap_sel.draw_rectangle(self.gc_selection, False ,0,0,size[0],size[1]) self.sx, self.sy = size - self.tool = 'marquee-rectangular' + self.tool['name'] = 'marquee-rectangular' self.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.FLEUR)) self.emit('selected') self.queue_draw() @@ -864,7 +884,8 @@ class Area(gtk.DrawingArea): """ Clear Canvas @param self -- Area instance """ - self.d.clear() + logging.debug('Area.clear') + self.d.clear(self) self.enableUndo(self) # Changing to public methods @@ -883,50 +904,51 @@ class Area(gtk.DrawingArea): @param - tool: a dictionary with the following keys: 'name': a string - 'size': a integer + 'line size': a integer 'fill color': a gtk.gdk.Color object 'stroke color': a gtk.gdk.Color object - 'shape': a string - 'circle' or 'square', for now + 'line shape': a string - 'circle' or 'square', for now 'fill': a Boolean value - 'sides': a integer (used when drawing regular polygons) - 'points': a integer (used when drawing stars) + 'vertices': a integer ''' logging.debug('Area.set_tool') #FIXME: self.tool should be a dict too. print tool - self.tool = tool['name'] + self.tool = tool - if tool['size'] is not None: - self.configure_line(tool['size']) + try: + if self.tool['line size'] is not None: + self.configure_line(self.tool['line size']) - if tool['fill color'] is not None: - self.set_fill_color(tool['fill color']) - else: - # use black - self.set_fill_color( gtk.gdk.Color(0,0,0) ) - - if tool['stroke color'] is not None: - self.set_stroke_color(tool['stroke color']) - else: - # use black - self.set_stroke_color( gtk.gdk.Color(0,0,0) ) + if self.tool['fill color'] is not None: + self.set_fill_color(self.tool['fill color']) + else: + # use black + self.set_fill_color( gtk.gdk.Color(0,0,0) ) + + if self.tool['stroke color'] is not None: + self.set_stroke_color(self.tool['stroke color']) + else: + # use black + self.set_stroke_color( gtk.gdk.Color(0,0,0) ) + + except AttributeError: + pass - #FIXME: this is ugly! - if tool['name'] is 'brush': - self.brush_shape = tool['shape'] - elif tool['name'] is 'eraser': - self.eraser_shape = tool['shape'] + if self.tool['line shape'] is not None: + self.line_shape = self.tool['line shape'] - self.fill = tool['fill'] + if self.tool['fill'] is not None: + self.fill = self.tool['fill'] - if tool['name'] is 'polygon_regular' and (tool['sides'] is not None): - self.polygon_sides = tool['sides'] - if tool['name'] is 'star' and (tool['points'] is not None): - self.polygon_sides = tool['points'] + if ( self.tool['name'] is 'polygon_regular' \ + or self.tool['name'] is 'star')\ + and (self.tool['vertices'] is not None): + self.vertices = self.tool['vertices'] - #TODO: set cursors (?) + # Setting the cursor try: pixbuf = gtk.gdk.pixbuf_new_from_file('./images/' + tool['name'] + '.png') cursor = gtk.gdk.Cursor(gtk.gdk.display_get_default() , pixbuf, 6, 21) @@ -934,3 +956,5 @@ class Area(gtk.DrawingArea): cursor = None self.window.set_cursor(cursor) + + @@ -499,13 +499,13 @@ class Desenho: widget.oldy = coords[1] widget.queue_draw() - def clear(self): + def clear(self, widget): """Clear the drawing. @param self -- Desenho.Desenho instance - + @param widget -- Area object (GtkDrawingArea) """ - width, height = self.d.window.get_size() + width, height = widget.window.get_size() widget.desenho = [] widget.textos = [] widget.pixmap.draw_rectangle(widget.get_style().white_gc, True,0, 0, width, height) diff --git a/OficinaActivity.py b/OficinaActivity.py index 3d54bb3..d5727e0 100644 --- a/OficinaActivity.py +++ b/OficinaActivity.py @@ -83,7 +83,7 @@ class OficinaActivity(activity.Activity): os.chdir(activity.get_bundle_path()) #print activity.get_bundle_path() - self._fixed = gtk.Fixed() + self._fixed = gtk.Fixed() self._area = Area(self) toolbox = Toolbox(self) @@ -123,8 +123,17 @@ class OficinaActivity(activity.Activity): # setting scrolledwindow as activity canvas... self.set_canvas(sw) - # Setting a default tool - self._area.tool = 'pencil' +# # Setting a default tool +# initial_tool = { +# 'name' : 'pencil', +# 'line size' : 2, +# 'fill color' : None, +# 'stroke color' : None, +# 'line shape' : 'circle', +# 'fill' : True, +# 'vertices' : None +# } +# self._area.set_tool(initial_tool) def read_file(self, file_path): '''Read file from Sugar Journal. @@ -63,7 +63,8 @@ from sugar.graphics.toggletoolbutton import ToggleToolButton from sugar.graphics.combobox import ComboBox from sugar.graphics.palette import Palette from sugar.graphics.menuitem import MenuItem -#Create toolbars for the activity + +##Create toolbars for the activity class Toolbox(ActivityToolbox): ## The Constructor def __init__(self, activity): @@ -163,6 +164,7 @@ class DrawEditToolbar(EditToolbar): def _on_signal_action_saved_cb(self, widget, data=None): self._verify_sensitive_buttons() + ##define when a button is active def _verify_sensitive_buttons(self): self.undo.set_sensitive( self._activity._area.can_undo() ) @@ -174,106 +176,98 @@ class DrawEditToolbar(EditToolbar): def _clear_all_cb(self, widget, data=None): self._activity._area.clear() + ##Determine Tools of the Toolbar class ToolsToolbar(gtk.Toolbar): #Tool default definitions _TOOL_PENCIL = { 'name' : 'pencil', - 'size' : 2, + 'line size' : 2, 'fill color' : None, 'stroke color' : None, - 'shape' : 'circle', + 'line shape' : 'circle', 'fill' : True, - 'sides' : None, - 'points' : None + 'vertices' : None } _TOOL_BRUSH = { 'name' : 'brush', - 'size' : 5, + 'line size' : 10, 'fill color' : None, 'stroke color' : None, - 'shape' : 'circle', + 'line shape' : 'circle', 'fill' : True, - 'sides' : None, - 'points' : None + 'vertices' : None } ##The Constructor _TOOL_ERASER = { 'name' : 'eraser', - 'size' : 20, + 'line size' : 20, 'fill color' : None, 'stroke color' : None, - 'shape' : 'circle', + 'line shape' : 'circle', 'fill' : True, - 'sides' : None, - 'points' : None + 'vertices' : None } _TOOL_POLYGON = { 'name' : 'polygon', - 'size' : 2, + 'line size' : 2, 'fill color' : None, 'stroke color' : None, - 'shape' : 'circle', + 'line shape' : 'circle', 'fill' : True, - 'sides' : None, - 'points' : None + 'vertices' : None } _TOOL_BUCKET = { 'name' : 'bucket', - 'size' : None, + 'line size' : None, 'fill color' : None, 'stroke color' : None, - 'shape' : None, + 'line shape' : None, 'fill' : None, - 'sides' : None, - 'points' : None + 'vertices' : None } _TOOL_MARQUEE_ELLIPTICAL = { 'name' : 'marquee-elliptical', - 'size' : None, + 'line size' : None, 'fill color' : None, 'stroke color' : None, - 'shape' : None, + 'line shape' : None, 'fill' : None, - 'sides' : None, - 'points' : None + 'vertices' : None } _TOOL_MARQUEE_FREEFORM = { 'name' : 'marquee-freeform', - 'size' : None, + 'line size' : None, 'fill color' : None, 'stroke color' : None, - 'shape' : None, + 'line shape' : None, 'fill' : None, - 'sides' : None, - 'points' : None + 'vertices' : None } _TOOL_MARQUEE_RECTANGULAR = { 'name' : 'marquee-rectangular', - 'size' : None, + 'line size' : None, 'fill color' : None, 'stroke color' : None, - 'shape' : None, + 'line shape' : None, 'fill' : None, - 'sides' : None, - 'points' : None + 'vertices' : None } _TOOL_MARQUEE_SMART = { 'name' : 'marquee-smart', - 'size' : None, + 'line size' : None, 'fill color' : None, 'stroke color' : None, - 'shape' : None, + 'line shape' : None, 'fill' : None, - 'sides' : None, - 'points' : None + 'vertices' : None } @@ -427,7 +421,7 @@ class ToolsToolbar(gtk.Toolbar): # This is where we set restrictions for size: # Initial value, minimum value, maximum value, step - adj = gtk.Adjustment(tool['size'], 1.0, 100.0, 1.0) + adj = gtk.Adjustment(tool['line size'], 1.0, 100.0, 1.0) size_spinbutton.set_adjustment(adj) size_spinbutton.set_numeric(True) @@ -445,22 +439,6 @@ class ToolsToolbar(gtk.Toolbar): tool['name'] is self._TOOL_ERASER['name']: # Changing to gtk.RadioButton -# item_1 = MenuItem(_('Square'), 'rectangle') -# item_2 = MenuItem(_('Circle'), 'ellipse') -# -# logging.debug('Menu Items created') -# -# for menu_item in palette.menu.get_children(): -# palette.menu.remove(menu_item) -# -# palette.menu.append(item_1) -# palette.menu.append(item_2) -# -# item_1.connect('activate', self.set_shape, tool, 'square') -# item_2.connect('activate', self.set_shape, tool, 'circle') -# -# item_1.show() -# item_2.show() # TODO: insert images to represent shapes item1 = gtk.RadioButton(None, _('Circle')) item1.show() @@ -481,11 +459,11 @@ class ToolsToolbar(gtk.Toolbar): vbox.pack_start(label) vbox.pack_start(item1) vbox.pack_start(item2) - #palette.action_bar.pack_start(vbox) + palette.set_content(vbox) separator = gtk.HSeparator() - vbox.pack_end(separator1) + vbox.pack_end(separator) separator.show() # User is able to fill or not a polygon, and its fill color @@ -523,7 +501,7 @@ class ToolsToolbar(gtk.Toolbar): @param shape -- Determine which shape Brush and Erase will use """ - tool['shape'] = shape + tool['line shape'] = shape self.set_tool(tool=tool) def set_tool(self, widget=None, tool=None): @@ -543,15 +521,7 @@ class ToolsToolbar(gtk.Toolbar): self._activity._area.set_tool(tool) - # Moving this to Area - #setting cursor -# try: -# pixbuf = gtk.gdk.pixbuf_new_from_file('./images/' + tool['name'] + '.png') -# cursor = gtk.gdk.Cursor(gtk.gdk.display_get_default() , pixbuf, 6, 21) -# except: -# cursor = None -# -# self._activity._area.window.set_cursor(cursor) + #setting cursor: Moved to Area def _on_icon_stroke_clicked(self, widget, data=None): self._stroke_color.clicked() @@ -584,137 +554,14 @@ class ToolsToolbar(gtk.Toolbar): def _on_value_changed(self, spinbutton, tool): size = spinbutton.get_value_as_int() - tool['size'] = size + tool['line size'] = size self.set_tool(tool=tool) def _on_toggled(self, radiobutton, tool, shape): if radiobutton.get_active(): self.set_shape(tool=tool, shape=shape) -''' -##Class to manage Fill colors -class ComboFillColors(ToolComboBox): - ## The Constructor - def __init__(self, activity): - ToolComboBox.__init__(self) - self._activity = activity - - self._fill_color = self.combo - self._fill_color.append_item(self.alloc_color('#000000'), 'Black') - self._fill_color.append_item(self.alloc_color('#ffffff'), 'White') - self._fill_color.append_item(self.alloc_color('#800000'), 'Maroon') - self._fill_color.append_item(self.alloc_color('#ff0000'), 'Red') - self._fill_color.append_item(self.alloc_color('#808000'), 'Olive') - self._fill_color.append_item(self.alloc_color('#ffff00'), 'Yellow') - self._fill_color.append_item(self.alloc_color('#008000'), 'Green') - self._fill_color.append_item(self.alloc_color('#00ff00'), 'Lime') - self._fill_color.append_item(self.alloc_color('#008080'), 'Teal') - self._fill_color.append_item(self.alloc_color('#00ffff'), 'Aqua') - self._fill_color.append_item(self.alloc_color('#000080'), 'Navy') - self._fill_color.append_item(self.alloc_color('#0000ff'), 'Blue') - self._fill_color.append_item(self.alloc_color('#800080'), 'Purple') - self._fill_color.append_item(self.alloc_color('#ff00ff'), 'Fuchsia') - - self._fill_color.set_active(0) - self._fill_color.connect('changed', self._combo_changed_cb) - - def alloc_color(self, color): - """Alloc new color. - @param self -- gtk.Toolbar - @param color -- hexadecimal number - - @return gdk.Color object - - """ - colormap = self.get_colormap() - _COLOR_ = colormap.alloc_color(color, True, True) - return _COLOR_ - - def _combo_changed_cb(self, combo): - color = self.get_color() - self.set_fill_color(color) - - def set_fill_color(self, color): - """Set the fill color in Area - @param self -- gtk.Toolbar - @param color -- a gdk.Color object - - """ - self._activity._area._set_fill_color(color) - - def get_color(self): - """Get the fill color from combobox - @param self -- gtk.Toolbar - - @return gdk.Color object - - """ - model = self.combo.get_model() - active = self.combo.get_active() - return model[active][0] - -##Class to manage Stroke colors -class ComboStrokeColors(ToolComboBox): - ##The Constructor - def __init__(self, activity): - - ToolComboBox.__init__(self) - self._activity = activity - - self._stroke_color = self.combo - self._stroke_color.append_item(self.alloc_color('#000000'), 'Black') - self._stroke_color.append_item(self.alloc_color('#ffffff'), 'White') - self._stroke_color.append_item(self.alloc_color('#800000'), 'Maroon') - self._stroke_color.append_item(self.alloc_color('#ff0000'), 'Red') - self._stroke_color.append_item(self.alloc_color('#808000'), 'Olive') - self._stroke_color.append_item(self.alloc_color('#ffff00'), 'Yellow') - self._stroke_color.append_item(self.alloc_color('#008000'), 'Green') - self._stroke_color.append_item(self.alloc_color('#00ff00'), 'Lime') - self._stroke_color.append_item(self.alloc_color('#008080'), 'Teal') - self._stroke_color.append_item(self.alloc_color('#00ffff'), 'Aqua') - self._stroke_color.append_item(self.alloc_color('#000080'), 'Navy') - self._stroke_color.append_item(self.alloc_color('#0000ff'), 'Blue') - self._stroke_color.append_item(self.alloc_color('#800080'), 'Purple') - self._stroke_color.append_item(self.alloc_color('#ff00ff'), 'Fuchsia') - - self._stroke_color.set_active(0) - self._stroke_color.connect('changed', self._combo_changed_cb) - - def alloc_color(self, color): - """Alloc new color. - @param self -- gtk.Toolbar - @param color -- hexadecimal number - - @return gdk.Color object - - """ - colormap = self.get_colormap() - _COLOR_ = colormap.alloc_color(color, True, True) - return _COLOR_ - - def _combo_changed_cb(self, combo): - color = self.get_color() - self.set_stroke_color(color) - - def get_color(self): - """Get the fill color from combobox - @param self -- gtk.Toolbar - - @return a gdk.Color object - - """ - model = self.combo.get_model() - active = self.combo.get_active() - return model[active][0] - - def set_stroke_color(self, color): - """Set the fill color in Area - @param self -- gtk.Toolbar - @param color -- a gdk.Color object - """ - self._activity._area._set_stroke_color(color) -''' ##Class to manage Stroke Size class ComboStrokeSize(ToolComboBox): @@ -818,18 +665,115 @@ class ButtonStrokeColor(gtk.ColorButton): ##Make the Shapes Toolbar class ShapesToolbar(gtk.Toolbar): - _SHAPE_ARROW = 'arrow' - _SHAPE_CURVE = 'curve' - _SHAPE_ELLIPSE = 'ellipse' - _SHAPE_FREEFORM = 'freeform' - _SHAPE_HEART = 'heart' - _SHAPE_LINE = 'line' - _SHAPE_PARALLELOGRAM = 'parallelogram' - _SHAPE_POLYGON = 'polygon_regular' - _SHAPE_RECTANGLE = 'rectangle' - _SHAPE_STAR = 'star' - _SHAPE_TRAPEZOID = 'trapezoid' - _SHAPE_TRIANGLE = 'triangle' + _SHAPE_ARROW = { + 'name' : 'arrow', + 'line size' : 5, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : 5 + } + _SHAPE_CURVE = { + 'name' : 'curve', + 'line size' : 2, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : None + } + _SHAPE_ELLIPSE = { + 'name' : 'ellipse', + 'line size' : 2, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : None + } + _SHAPE_FREEFORM = { + 'name' : 'freeform', + 'line size' : 2, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : None + } + _SHAPE_HEART = { + 'name' : 'heart', + 'line size' : 2, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : None + } + _SHAPE_LINE = { + 'name' : 'line', + 'line size' : 2, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : None + } + _SHAPE_PARALLELOGRAM = { + 'name' : 'parallelogram', + 'line size' : 2, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : None + } + _SHAPE_POLYGON = { + 'name' : 'polygon_regular', + 'line size' : 2, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : None + } + _SHAPE_RECTANGLE = { + 'name' : 'rectangle', + 'line size' : 2, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : None + } + _SHAPE_STAR = { + 'name' : 'star', + 'line size' : 2, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : 5 + } + _SHAPE_TRAPEZOID = { + 'name' : 'trapezoid', + 'line size' : 2, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : None + } + _SHAPE_TRIANGLE = { + 'name' : 'triangle', + 'line size' : 2, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : None + } + ##The Constructor def __init__(self, activity): gtk.Toolbar.__init__(self) @@ -861,10 +805,10 @@ class ShapesToolbar(gtk.Toolbar): self.insert(item, -1) item.show() - - self._stroke_size = ComboStrokeSize(activity) - self.insert(self._stroke_size, -1) - self._stroke_size.show() + # Line size is now choosen into tool's palette +# self._stroke_size = ComboStrokeSize(activity) +# self.insert(self._stroke_size, -1) +# self._stroke_size.show() separator = gtk.SeparatorToolItem() separator.set_draw(True) @@ -893,6 +837,10 @@ class ShapesToolbar(gtk.Toolbar): self.insert(self._shape_line, -1) self._shape_line.show() self._shape_line.set_tooltip(_('Line')) + try: + self._configure_palette_shape_line() + except: + logging.debug('Could not create palette for Shape Line') self._shape_polygon = ToolButton('tool-shape-polygon') self.insert(self._shape_polygon, -1) @@ -983,29 +931,20 @@ class ShapesToolbar(gtk.Toolbar): self._shape_trapezoid.connect('clicked', self.set_tool, self._SHAPE_TRAPEZOID) self._shape_triangle.connect('clicked', self.set_tool, self._SHAPE_TRIANGLE) - def set_tool(self, widget, tool): - - # setting tool - self._activity._area.tool = tool - - # setting size and color - size = self._stroke_size.get_size() - self._stroke_size.set_stroke_size(size) + def set_tool(self, widget=None, tool=None): + # New method to set tools; using dict + # Color must be allocated; if not, it will be displayed as black stroke_color = self._stroke_color.get_color() - self._stroke_color.set_stroke_color(stroke_color) + tool['stroke color'] = self._stroke_color.alloc_color(stroke_color) fill_color = self._fill_color.get_color() - self._fill_color.set_fill_color(fill_color) + tool['fill color'] = self._fill_color.alloc_color(fill_color) - #setting cursor - try: - pixbuf = gtk.gdk.pixbuf_new_from_file('./images/' + tool + '.png') - cursor = gtk.gdk.Cursor(gtk.gdk.display_get_default() , pixbuf, 6, 21) - except: - cursor = None + self._activity._area.set_tool(tool) + + #setting cursor: moved to Area - self._activity._area.window.set_cursor(cursor) def _on_icon_stroke_clicked(self, widget, data=None): self._stroke_color.clicked() @@ -1014,39 +953,39 @@ class ShapesToolbar(gtk.Toolbar): self._fill_color.clicked() - def _on_value_changed(self, spinbutton, data=None): - self._activity._area.polygon_sides = spinbutton.get_value_as_int() + def _on_vertices_value_changed(self, spinbutton, tool): + #self._activity._area.polygon_sides = spinbutton.get_value_as_int() + tool['vertices'] = spinbutton.get_value_as_int() + self.set_tool(tool=tool) - if data is self._SHAPE_POLYGON: - self.set_tool(self._shape_polygon, self._SHAPE_POLYGON) - elif data is self._SHAPE_STAR: - self.set_tool(self._shape_star, self._SHAPE_STAR) + def _on_line_size_value_changed(self, spinbutton, tool): + tool['line size'] = spinbutton.get_value_as_int() + self.set_tool(tool=tool) - def _on_fill_checkbutton_toggled(self, checkbutton, button=None): + def _on_fill_checkbutton_toggled(self, checkbutton, tool): logging.debug('Checkbutton is Active: %s', checkbutton.get_active() ) - self._activity._area.fill = checkbutton.get_active() - try: - button.emit('clicked') - except: - pass + #self._activity._area.fill = checkbutton.get_active() + tool['fill'] = checkbutton.get_active() + self.set_tool(tool=tool) def _configure_palette_shape_ellipse(self): logging.debug('Creating palette to shape ellipse') - self._create_simple_palette(self._shape_ellipse) + self._create_simple_palette(self._shape_ellipse, self._SHAPE_ELLIPSE) def _configure_palette_shape_rectangle(self): logging.debug('Creating palette to shape rectangle') - self._create_simple_palette(self._shape_rectangle) + self._create_simple_palette(self._shape_rectangle, self._SHAPE_RECTANGLE) def _configure_palette_shape_polygon(self): logging.debug('Creating palette to shape polygon') - self._create_simple_palette(self._shape_polygon) + # Enable 'Size' and 'Fill' option + self._create_simple_palette(self._shape_polygon, self._SHAPE_POLYGON) + # We want choose the number of sides to our polygon palette = self._shape_polygon.get_palette() - - + spin = gtk.SpinButton() spin.show() @@ -1054,13 +993,9 @@ class ShapesToolbar(gtk.Toolbar): black = gtk.gdk.Color(0,0,0) spin.modify_text(gtk.STATE_NORMAL, black) - # This is where we set restrictions for Regular Polygon: + # This is where we set restrictions for sides in Regular Polygon: # Initial value, minimum value, maximum value, step - try: - initial = float(self._activity._area.polygon_sides) - except: - initial = 5.0 - adj = gtk.Adjustment(initial, 3.0, 50.0, 1.0) + adj = gtk.Adjustment(self._SHAPE_POLYGON['vertices'], 3.0, 50.0, 1.0) spin.set_adjustment(adj) spin.set_numeric(True) @@ -1069,25 +1004,28 @@ class ShapesToolbar(gtk.Toolbar): palette.action_bar.pack_start(label) palette.action_bar.pack_start(spin) - spin.connect('value-changed', self._on_value_changed, self._SHAPE_POLYGON) + spin.connect('value-changed', self._on_vertices_value_changed, self._SHAPE_POLYGON) def _configure_palette_shape_heart(self): logging.debug('Creating palette to shape heart') - self._create_simple_palette(self._shape_heart) + self._create_simple_palette(self._shape_heart, self._SHAPE_HEART) def _configure_palette_shape_parallelogram(self): logging.debug('Creating palette to shape parallelogram') - self._create_simple_palette(self._shape_parallelogram) + self._create_simple_palette(self._shape_parallelogram, self._SHAPE_PARALLELOGRAM) def _configure_palette_shape_arrow(self): logging.debug('Creating palette to shape arrow') - self._create_simple_palette(self._shape_arrow) + self._create_simple_palette(self._shape_arrow, self._SHAPE_ARROW) def _configure_palette_shape_star(self): logging.debug('Creating palette to shape star') - self._create_simple_palette(self._shape_star) + # Enable 'Size' and 'Fill' option + self._create_simple_palette(self._shape_star, self._SHAPE_STAR) + + # We want choose the number of sides to our star palette = self._shape_star.get_palette() spin = gtk.SpinButton() @@ -1099,11 +1037,7 @@ class ShapesToolbar(gtk.Toolbar): # This is where we set restrictions for Star: # Initial value, minimum value, maximum value, step - try: - initial = float(self._activity._area.polygon_sides) - except: - initial = 5.0 - adj = gtk.Adjustment(initial, 3.0, 50.0, 1.0) + adj = gtk.Adjustment(self._SHAPE_STAR['vertices'], 3.0, 50.0, 1.0) spin.set_adjustment(adj) spin.set_numeric(True) @@ -1112,54 +1046,84 @@ class ShapesToolbar(gtk.Toolbar): palette.action_bar.pack_start(label) palette.action_bar.pack_start(spin) - # It is connected to the same method that Regular Polygon's Palette is because they use the same property in Area - spin.connect('value-changed', self._on_value_changed, self._SHAPE_STAR) + spin.connect('value-changed', self._on_vertices_value_changed, self._SHAPE_STAR) def _configure_palette_shape_trapezoid(self): logging.debug('Creating palette to shape trapezoid') - self._create_simple_palette(self._shape_trapezoid) + self._create_simple_palette(self._shape_trapezoid, self._SHAPE_TRAPEZOID) def _configure_palette_shape_triangle(self): logging.debug('Creating palette to shape triangle') - self._create_simple_palette(self._shape_triangle) + self._create_simple_palette(self._shape_triangle, self._SHAPE_TRIANGLE) - def _create_simple_palette(self, button): + def _create_simple_palette(self, button, tool, line_size_only=False): """ - Create a simple palette with an CheckButton named "Fill". Most tools use only this. - @param self -- gtk.Toolbar + Create a simple palette with an CheckButton named "Fill" and a SpinButton to represent the line size. Most tools use only this. + @param self -- toolbox.ShapesToolbar @param button -- a ToolButton to associate the palette. + @param tool -- a dictionnary describing tool's properties. + @param line_size_only -- indicates if palette should only display Line Size option. Default value is False. """ palette = button.get_palette() - fill_checkbutton = gtk.CheckButton(_('Fill')) - fill_checkbutton.show() - fill_checkbutton.set_active(self._activity._area.fill) + # Fill option + if not line_size_only: + fill_checkbutton = gtk.CheckButton(_('Fill')) + fill_checkbutton.show() + fill_checkbutton.set_active(tool['fill']) + + fill_checkbutton.connect('toggled', self._on_fill_checkbutton_toggled, tool) + + palette.set_content(fill_checkbutton) + + size_spinbutton = gtk.SpinButton() + size_spinbutton.show() - fill_checkbutton.connect('toggled', self._on_fill_checkbutton_toggled, button) + # When inserted in a Palette, a spinbutton does not display text in black + black = gtk.gdk.Color(0,0,0) + size_spinbutton.modify_text(gtk.STATE_NORMAL, black) - fill_checkbutton.connect('map', self._on_fill_checkbutton_map) -# widget.connect_after('clicked', self._on_fill_checkbutton_after_clicked, fill_checkbutton) + # This is where we set restrictions for size: + # Initial value, minimum value, maximum value, step + adj = gtk.Adjustment(tool['line size'], 1.0, 100.0, 1.0) + size_spinbutton.set_adjustment(adj) - palette.set_content(fill_checkbutton) - - def _on_fill_checkbutton_map(self, checkbutton, data=None): - """ - Update checkbutton condition to agree with Area.Area object; this prevents tools to have fill checked but be drawed not filled. + size_spinbutton.set_numeric(True) - @param self -- gtk.Toolbar - @param checkbutton - @param data - """ - self._activity._area.fill = checkbutton.get_active() + label = gtk.Label(_('Size: ')) + label.show() + + palette.action_bar.pack_start(label) + palette.action_bar.pack_start(size_spinbutton) + + size_spinbutton.connect('value-changed', self._on_line_size_value_changed, tool) -# def _on_fill_checkbutton_after_clicked(self, widget, checkbutton): -# # Trying to prevent same condition described at self._on_fill_checkbutton_map +# def _on_fill_checkbutton_map(self, checkbutton, data=None): +# """ +# Update checkbutton condition to agree with Area.Area object; this prevents tools to have fill checked but be drawed not filled. +# +# @param self -- gtk.Toolbar +# @param checkbutton +# @param data +# """ # self._activity._area.fill = checkbutton.get_active() + def _configure_palette_shape_line(self): + logging.debug('Creating palette to shape line') + self._create_simple_palette(self._shape_line, self._SHAPE_LINE, True) + ##Make the Text Toolbar class TextToolbar(gtk.Toolbar): - _ACTION_TEXT = 'text' + _ACTION_TEXT = { + 'name' : 'text', + 'line size' : None, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : None, + 'fill' : True, + 'vertices' : None + } ##The Constructor def __init__(self, activity): gtk.Toolbar.__init__(self) @@ -1219,16 +1183,20 @@ class TextToolbar(gtk.Toolbar): def set_tool(self, widget, tool): #FIXME: this callback must change as others buttons get enabled - self._activity._area.tool = tool +# self._activity._area.tool = tool +# +# color = self._text_color.get_color() +# self._text_color.set_fill_color(color) +# +# # setting cursor +# pixbuf = gtk.gdk.pixbuf_new_from_file('./images/text.png') +# cursor = gtk.gdk.Cursor(gtk.gdk.display_get_default() , pixbuf, 6, 21) +# self._activity._area.window.set_cursor(cursor) + new_color = self._text_color.get_color() + tool['fill color'] = self._text_color.alloc_color(new_color) + self._activity._area.set_tool(tool) - color = self._text_color.get_color() - self._text_color.set_fill_color(color) - # setting cursor - pixbuf = gtk.gdk.pixbuf_new_from_file('./images/text.png') - cursor = gtk.gdk.Cursor(gtk.gdk.display_get_default() , pixbuf, 6, 21) - self._activity._area.window.set_cursor(cursor) - ##Make the Images Toolbar class ImageToolbar(gtk.Toolbar): @@ -1357,7 +1325,18 @@ class ImageToolbar(gtk.Toolbar): class EffectsToolbar(gtk.Toolbar): _EFFECT_GRAYSCALE = 'grayscale' - _EFFECT_RAINBOW = 'rainbow' + # Rainbow acts as a tool in Area, and it has to be described as a dict + _EFFECT_RAINBOW = { + 'name' : 'rainbow', + 'line size' : 10, + 'fill color' : None, + 'stroke color' : None, + 'line shape' : 'circle', + 'fill' : True, + 'vertices' : None + } + + ##The Constructor def __init__(self, activity): gtk.Toolbar.__init__(self) @@ -1397,69 +1376,95 @@ class EffectsToolbar(gtk.Toolbar): """ self._effect_grayscale.connect('clicked', self.grayscale) self._effect_rainbow.connect('clicked', self.rainbow) + ##Make the colors be in grayscale def grayscale(self, widget): self._activity._area.grayscale(widget) + ##Like the brush, but change it color when painting def rainbow(self, widget): - self._activity._area.tool = self._EFFECT_RAINBOW + self._activity._area.set_tool(self._EFFECT_RAINBOW) - # setting cursor - try: - pixbuf = gtk.gdk.pixbuf_new_from_file('./images/rainbow.png') - cursor = gtk.gdk.Cursor(gtk.gdk.display_get_default() , pixbuf, 6, 21) - - except: - cursor = None - - self._activity._area.window.set_cursor(cursor) + # setting cursor: moved to Area - - def _configure_palette(self, widget, tool=None): + def _configure_palette(self, button, tool=None): """Set palette for a tool - @param self -- gtk.Toolbar - @param widget - the widget which Palette will be set - @param tool - the reference tool for Palette creation. Its values are restricted to Class constants + @param self -- toolbox.EffectsToolbar + @param widget - the ToolButton which Palette will be set + @param tool -- a dictionnary describing tool's properties. """ logging.debug('setting a palette for %s', tool) - palette = widget.get_palette() + palette = button.get_palette() if tool is None: return elif tool is self._EFFECT_RAINBOW: - spin = gtk.SpinButton() - spin.show() + # We can adjust 'Line Size' and 'Line Shape' here - black = gtk.gdk.Color(0,0,0) - #white = gtk.gdk.Color(255,255,255) + # Line Size + size_spinbutton = gtk.SpinButton() + size_spinbutton.show() - #spin.modify_base(gtk.STATE_NORMAL, black) - #spin.modify_base(gtk.STATE_ACTIVE, white) - spin.modify_text(gtk.STATE_NORMAL, black) + black = gtk.gdk.Color(0,0,0) + size_spinbutton.modify_text(gtk.STATE_NORMAL, black) # This is where we set restrictions for Rainbow: # Initial value, minimum value, maximum value, step - adj = gtk.Adjustment(5.0, 1.0, 100.0, 1.0) - spin.set_adjustment(adj) + adj = gtk.Adjustment(tool['line size'], 1.0, 100.0, 1.0) + size_spinbutton.set_adjustment(adj) - spin.set_numeric(True) + size_spinbutton.set_numeric(True) label = gtk.Label(_('Size: ')) label.show() palette.action_bar.pack_start(label) - palette.action_bar.pack_start(spin) + palette.action_bar.pack_start(size_spinbutton) - spin.connect('value-changed', self._on_value_changed) + size_spinbutton.connect('value-changed', self._on_size_value_changed, tool) - def _on_value_changed(self, spinbutton, data=None): - size = spinbutton.get_value_as_int() - self._activity._area.configure_line(size) + # Line Shape + # TODO: insert images to represent shapes + item1 = gtk.RadioButton(None, _('Circle')) + item1.show() + item1.set_active(True) + + item2 = gtk.RadioButton(item1, _('Square')) + item2.show() + + item1.connect('toggled', self._on_radio_toggled, tool, 'circle') + item2.connect('toggled', self._on_radio_toggled, tool, 'square') + + label = gtk.Label(_('Shape')) + label.show() + + vbox = gtk.VBox() + vbox.show() + + vbox.pack_start(label) + vbox.pack_start(item1) + vbox.pack_start(item2) + + separator = gtk.HSeparator() + vbox.pack_end(separator) + separator.show() + + palette.set_content(vbox) + + def _on_size_value_changed(self, spinbutton, tool): +# size = spinbutton.get_value_as_int() +# self._activity._area.configure_line(size) + tool['line size'] = spinbutton.get_value_as_int() self.rainbow(self._effect_rainbow) + def _on_radio_toggled(self, radiobutton, tool, shape): + if radiobutton.get_active(): + tool['line shape'] = shape + self.rainbow(self._effect_rainbow) + ##Make the View Toolbar class ViewToolbar(gtk.Toolbar): |