From 2180baaec58a4b1bc7e2e3d94493c41499778f15 Mon Sep 17 00:00:00 2001 From: nathalia Date: Mon, 13 Aug 2007 20:18:38 +0000 Subject: add regular polygon and rearranging functions --- diff --git a/Area.py b/Area.py index 3c36e1f..751a89d 100644..100755 --- a/Area.py +++ b/Area.py @@ -116,6 +116,7 @@ class Area(gtk.DrawingArea): self.newy_ = 0 self.color_dec = 0 self.polygon_start = True + self.points = [] self.gc = None self.gc_line = None self.gc_eraser = None @@ -307,27 +308,32 @@ class Area(gtk.DrawingArea): #polygon elif self.tool == 'polygon': self.configure_line(self.line_size) - self.d.polygon(widget, coords) + self.d.polygon(widget,coords,True,False) #triangle elif self.tool == 'triangle': self.configure_line(self.line_size) - self.d.triangle(widget,coords) + self.d.triangle(widget,coords,True) #trapezoid elif self.tool == 'trapezoid': self.configure_line(self.line_size) - self.d.trapezoid(widget,coords) + self.d.trapezoid(widget,coords,True) #arrow elif self.tool == 'arrow': self.configure_line(self.line_size) - self.d.arrow(widget,coords) + self.d.arrow(widget,coords,True) #parallelogram elif self.tool == 'parallelogram': self.configure_line(self.line_size) - self.d.parallelogram(widget,coords) + self.d.parallelogram(widget,coords,True) #star elif self.tool == 'star': self.configure_line(self.line_size) - self.d.star(widget,coords) + self.d.star(widget,coords,True) + #polygon regular + elif self.tool == 'polygon_regular': + self.configure_line(self.line_size) + n = 7 + self.d.polygon_regular(widget,coords,n,True) def mouseup(self,widget,event): """Make the Area object (GtkDrawingArea) recognize that the mouse was released. @@ -338,6 +344,7 @@ class Area(gtk.DrawingArea): event -- GdkEvent """ + coords = int(event.x), int(event.y) if self.desenha == True: # line if self.tool == 'line': @@ -374,70 +381,43 @@ class Area(gtk.DrawingArea): self.oldx = event.x self.oldy = event.y self.enableUndo(widget) - # polygon + # polygon elif self.tool == 'polygon': - if self.polygon_start: - self.enableUndo(widget) - self.pixmap.draw_line(self.gc_line,self.oldx,self.oldy, int (event.x), int( event.y )) - self.lastx = event.x - self.lasty = event.y - self.firstx = self.oldx - self.firsty = self.oldy - self.polygon_start = False - else: - self.dx = math.fabs(event.x - self.firstx) - self.dy = math.fabs(event.y - self.firsty) - if (self.dx < 20) & (self.dy < 20): - self.pixmap.draw_line(self.gc_line,int (self.firstx), int (self.firsty), int (self.lastx), int (self.lasty)) - self.polygon_start = True - self.undo_times -= 1#destroy the undo screen of polygon start - self.enableUndo(widget) - else: - self.pixmap.draw_line(self.gc_line,int (self.lastx),int (self.lasty), int (event.x), int( event.y )) - self.lastx = event.x - self.lasty = event.y - widget.queue_draw() - - elif self.tool == 'pencil': #to undo pencil + self.d.polygon(widget, coords, False, False) + #to undo pencil + elif self.tool == 'pencil': widget.queue_draw() self.enableUndo(widget) - #bucket elif self.tool == 'bucket': - # New algorithm. See Desenho.py width, height = self.window.get_size() fill(self.pixmap, self.gc, int(event.x), int(event.y), width, height, self.color_dec) widget.queue_draw() self.enableUndo(widget) - + #triangle elif self.tool == 'triangle': - self.pixmap.draw_polygon(self.gc, True, self.d.points) - self.pixmap.draw_polygon(self.gc_line, False, self.d.points) - widget.queue_draw() + self.d.triangle(widget,coords,False) self.enableUndo(widget) - + #trapezoid elif self.tool == 'trapezoid': - self.pixmap.draw_polygon(self.gc, True, self.d.points) - self.pixmap.draw_polygon(self.gc_line, False, self.d.points) - widget.queue_draw() + self.d.trapezoid(widget,coords,False) self.enableUndo(widget) - + #arrow elif self.tool == 'arrow': - self.pixmap.draw_polygon(self.gc, True, self.d.points) - self.pixmap.draw_polygon(self.gc_line, False, self.d.points) - widget.queue_draw() + self.d.arrow(widget,coords,False) self.enableUndo(widget) - + #parallelogram elif self.tool == 'parallelogram': - self.pixmap.draw_polygon(self.gc, True, self.d.points) - self.pixmap.draw_polygon(self.gc_line, False, self.d.points) - widget.queue_draw() + self.d.parallelogram(widget,coords,False) self.enableUndo(widget) - + #star elif self.tool == 'star': - self.pixmap.draw_polygon(self.gc, True, self.d.points) - self.pixmap.draw_polygon(self.gc_line, False, self.d.points) - widget.queue_draw() + self.d.star(widget,coords,False) + self.enableUndo(widget) + #polygon regular + elif self.tool == 'polygon_regular': + n = 7 + self.d.polygon_regular(widget,coords,n,False) self.enableUndo(widget) if self.tool == 'brush' or self.tool == 'eraser': diff --git a/Desenho.py b/Desenho.py index fe468fc..1049e27 100644..100755 --- a/Desenho.py +++ b/Desenho.py @@ -184,7 +184,7 @@ class Desenho: self.d.pixmap_temp.draw_rectangle(self.d.gc_line, False ,self.d.newx,self.d.newy,self.d.newx_,self.d.newy_) - def triangle(self, widget, coords): + def triangle(self, widget, coords, temp): """Draw a triangle. Keyword arguments: @@ -193,30 +193,21 @@ class Desenho: coords -- Two value tuple """ - widget.queue_draw() - if coords[0] > WIDTH: - coords0 = WIDTH + if temp == True: + pixmap = self.d.pixmap_temp else: - coords0 = coords[0] - - if coords [1] > HEIGHT: - coords1 = HEIGHT - else: - coords1 = coords[1] - - if coords0 < 0: - coords0 = 0 - - if coords1 < 0: - coords1 = 0 + pixmap = self.d.pixmap + width, height = self.d.window.get_size() + + points = [(self.d.oldx,self.d.oldy), (self.d.oldx+int((coords[0]-self.d.oldx)/2),coords[1]), (coords[0],self.d.oldy)] + pixmap.draw_drawable(self.d.gc, self.d.pixmap, 0, 0, 0, 0, width, height) + pixmap.draw_polygon(self.d.gc, True, points) + pixmap.draw_polygon(self.d.gc_line, False, points) + widget.queue_draw() - self.points = [(self.d.oldx, self.d.oldy), (self.d.oldx+int((coords0-self.d.oldx)/2), coords1), (coords0,self.d.oldy)] - self.d.pixmap_temp.draw_drawable(self.d.gc,self.d.pixmap, 0 , 0 ,0,0, WIDTH, HEIGHT) - self.d.pixmap_temp.draw_polygon(self.d.gc, True, self.points) - self.d.pixmap_temp.draw_polygon(self.d.gc_line, False, self.points) - def trapezoid(self, widget, coords): + def trapezoid(self, widget, coords, temp): """Draw a trapezoid. Keyword arguments: @@ -225,31 +216,22 @@ class Desenho: coords -- Two value tuple """ - widget.queue_draw() - if coords[0] > WIDTH: - coords0 = WIDTH + if temp == True: + pixmap = self.d.pixmap_temp else: - coords0 = coords[0] - - if coords[1] > HEIGHT: - coords1 = HEIGHT - else: - coords1 = coords[1] - - if coords0 < 0: - coords0 = 0 - - if coords1 < 0: - coords1 = 0 - - dif = int((coords0 - self.d.oldx)/4) - self.points = [(self.d.oldx, self.d.oldy), (self.d.oldx+dif, coords1), (coords0-dif, coords1) , (coords0,self.d.oldy)] - self.d.pixmap_temp.draw_drawable(self.d.gc,self.d.pixmap, 0 , 0 ,0,0, WIDTH, HEIGHT) - self.d.pixmap_temp.draw_polygon(self.d.gc, True, self.points) - self.d.pixmap_temp.draw_polygon(self.d.gc_line, False, self.points) + pixmap = self.d.pixmap + width, height = self.d.window.get_size() - def arrow(self, widget, coords): + dif = int((coords[0] - self.d.oldx)/4) + points = [(self.d.oldx, self.d.oldy), (self.d.oldx+dif, coords[1]), (coords[0]-dif, coords[1]) , (coords[0],self.d.oldy)] + pixmap.draw_drawable(self.d.gc,self.d.pixmap, 0 , 0 , 0, 0, width, height) + pixmap.draw_polygon(self.d.gc, True, points) + pixmap.draw_polygon(self.d.gc_line, False, points) + widget.queue_draw() + + + def arrow(self, widget, coords, temp): """Draw a arrow. Keyword arguments: @@ -258,31 +240,28 @@ class Desenho: coords -- Two value tuple """ - widget.queue_draw() - - if coords[0] > WIDTH: - coords0 = WIDTH + if temp == True: + pixmap = self.d.pixmap_temp else: - coords0 = coords[0] - - if coords [1] > HEIGHT: - coords1 = HEIGHT - else: - coords1 = coords[1] - - if coords0 < 0: - coords0 = 0 - - if coords1 < 0: - coords1 = 0 - self.width = coords0 - self.d.oldx - self.height = coords1 - self.d.oldy - self.points = [(self.d.oldx,self.d.oldy), (self.d.oldx+int(self.width/6),coords1), (self.d.oldx+int(self.width/6),self.d.oldy+int(self.height/3)), (coords0,self.d.oldy+int(self.height/3)), (coords0,self.d.oldy-int(self.height/3)), (self.d.oldx+int(self.width/6),self.d.oldy-int(self.height/3)), (self.d.oldx+int(self.width/6),self.d.oldy-self.height)] - self.d.pixmap_temp.draw_drawable(self.d.gc,self.d.pixmap, 0 , 0 ,0,0, WIDTH, HEIGHT) - self.d.pixmap_temp.draw_polygon(self.d.gc, True, self.points) - self.d.pixmap_temp.draw_polygon(self.d.gc_line, False, self.points) + pixmap = self.d.pixmap + width, height = self.d.window.get_size() - def parallelogram(self, widget, coords): + x = coords[0] - self.d.oldx + y = coords[1] - self.d.oldy + points = [(self.d.oldx,self.d.oldy),\ +(self.d.oldx+int(x/6),self.d.oldy+y),\ +(self.d.oldx+int(x/6),self.d.oldy+int(y/3)),\ +(self.d.oldx+x,self.d.oldy+int(y/3)),\ +(self.d.oldx+x,self.d.oldy-int(y/3)),\ +(self.d.oldx+int(x/6),self.d.oldy-int(y/3)),\ +(self.d.oldx+int(x/6),self.d.oldy-y)] + pixmap.draw_drawable(self.d.gc,self.d.pixmap, 0, 0, 0, 0, width, height) + pixmap.draw_polygon(self.d.gc, True, points) + pixmap.draw_polygon(self.d.gc_line, False, points) + widget.queue_draw() + + + def parallelogram(self, widget, coords, temp): """Draw a parallelogram. Keyword arguments: @@ -291,31 +270,21 @@ class Desenho: coords -- Two value tuple """ - widget.queue_draw() - - if coords[0] > WIDTH: - coords0 = WIDTH + if temp == True: + pixmap = self.d.pixmap_temp else: - coords0 = coords[0] - - if coords[1] > HEIGHT: - coords1 = HEIGHT - else: - coords1 = coords[1] - - if coords0 < 0: - coords0 = 0 + pixmap = self.d.pixmap + width, height = self.d.window.get_size() - if coords1 < 0: - coords1 = 0 + x = int((coords[0] - self.d.oldx)/4) + points = [(self.d.oldx,self.d.oldy), (coords[0]-x, self.d.oldy), (coords[0],coords[1]), (self.d.oldx+x,coords[1])] + pixmap.draw_drawable(self.d.gc,self.d.pixmap,0,0,0,0,width,height) + pixmap.draw_polygon(self.d.gc, True, points) + pixmap.draw_polygon(self.d.gc_line, False, points) + widget.queue_draw() - self.width = int((coords0 - self.d.oldx)/4) - self.points = [(self.d.oldx,self.d.oldy), (coords0-self.width, self.d.oldy), (coords0,coords1), (self.d.oldx+self.width,coords1)] - self.d.pixmap_temp.draw_drawable(self.d.gc,self.d.pixmap, 0 , 0 ,0,0, WIDTH, HEIGHT) - self.d.pixmap_temp.draw_polygon(self.d.gc, True, self.points) - self.d.pixmap_temp.draw_polygon(self.d.gc_line, False, self.points) - def star(self, widget, coords): + def star(self, widget, coords, temp): """Draw a arrow. Keyword arguments: @@ -324,38 +293,63 @@ class Desenho: coords -- Two value tuple """ - widget.queue_draw() - - if coords[0] > WIDTH: - coords0 = WIDTH - else: - coords0 = coords[0] - - if coords [1] > HEIGHT: - coords1 = HEIGHT + if temp == True: + pixmap = self.d.pixmap_temp else: - coords1 = coords[1] + pixmap = self.d.pixmap + width, height = self.d.window.get_size() + + x = coords[0] - self.d.oldx + y = coords[1] - self.d.oldy + + points = [(self.d.oldx,self.d.oldy),\ +(self.d.oldx+int(x*0.25), self.d.oldy+int(y*0.4)),\ +(self.d.oldx+int(x), self.d.oldy+int(y*0.4)),\ +(self.d.oldx+int(x*0.35), self.d.oldy+int(y*0.6)),\ +(self.d.oldx+int(x*0.6), self.d.oldy+y),\ +(self.d.oldx, self.d.oldy+int(y*0.75)),\ +(self.d.oldx-int(x*0.6), self.d.oldy+y),\ +(self.d.oldx-int(x*0.35), self.d.oldy+int(y*0.6)),\ +(self.d.oldx-int(x), self.d.oldy+int(y*0.4)),\ +(self.d.oldx-int(x*0.25), self.d.oldy+int(y*0.4))] + pixmap.draw_drawable(self.d.gc,self.d.pixmap, 0 , 0 ,0,0, width, height) + pixmap.draw_polygon(self.d.gc, True, points) + pixmap.draw_polygon(self.d.gc_line, False, points) + widget.queue_draw() - if coords0 < 0: - coords0 = 0 - - if coords1 < 0: - coords1 = 0 - self.width = coords0 - self.d.oldx - self.height = coords1 - self.d.oldy - self.points = [(self.d.oldx,self.d.oldy),\ -(self.d.oldx+int(self.width*0.25), self.d.oldy+int(self.height*0.4)),\ -(self.d.oldx+int(self.width),self.d.oldy+int(self.height*0.4)),\ -(self.d.oldx+int(self.width*0.35), self.d.oldy+int(self.height*0.6)),\ -(self.d.oldx+int(self.width*0.6), self.d.oldy+self.height),\ -(self.d.oldx, self.d.oldy+int(self.height*0.75)),\ -(self.d.oldx-int(self.width*0.6), self.d.oldy+self.height),\ -(self.d.oldx-int(self.width*0.35), self.d.oldy+int(self.height*0.6)),\ -(self.d.oldx-int(self.width),self.d.oldy+int(self.height*0.4)),\ -(self.d.oldx-int(self.width*0.25), self.d.oldy+int(self.height*0.4))] - self.d.pixmap_temp.draw_drawable(self.d.gc,self.d.pixmap, 0 , 0 ,0,0, WIDTH, HEIGHT) - self.d.pixmap_temp.draw_polygon(self.d.gc, True, self.points) - self.d.pixmap_temp.draw_polygon(self.d.gc_line, False, self.points) + + def polygon_regular(self, widget, coords, n, temp): + """Draw polygon with n sides. + + Keyword arguments: + self -- Desenho.Desenho instance + widget -- Area object (GtkDrawingArea) + coords -- Two value tuple + n -- number of sides + temp -- switch between pixmap and pixmap_temp + + """ + if temp == True: + pixmap = self.d.pixmap_temp + else: + pixmap = self.d.pixmap + width, height = self.d.window.get_size() + + x = coords[0] - self.d.oldx + y = coords[1] - self.d.oldy + A = math.atan2(y,x) + dA = 2*math.pi/n + r = math.hypot(y,x) + p = [(self.d.oldx+int(r*math.cos(A)),self.d.oldy+int(r*math.sin(A)))] + for i in range(n-1): + A = A+dA + p.append((self.d.oldx+int(r*math.cos(A)),self.d.oldy+int(r*math.sin(A)))) + tp = tuple(p) + + pixmap.draw_drawable(self.d.gc, self.d.pixmap, 0, 0, 0, 0, width, height) + pixmap.draw_polygon(self.d.gc, True, tp) + pixmap.draw_polygon(self.d.gc_line, False, tp) + widget.queue_draw() def circle(self, widget, coords): @@ -572,8 +566,9 @@ class Desenho: self.d.pixmap_sel.draw_rectangle(self.d.gc_selection1, False ,coords[0] - w/2-1, coords[1]- h/2-1, w+2, h+2) widget.queue_draw() - - def polygon(self, widget, coords): + + + def polygon(self, widget, coords, temp, fill): """Draw polygon. Keyword arguments: @@ -581,14 +576,48 @@ class Desenho: widget -- Area object (GtkDrawingArea) coords -- Two value tuple - """ - self.d.pixmap_temp.draw_drawable(self.d.gc,self.d.pixmap, 0 , 0 ,0,0, WIDTH, HEIGHT) - if self.d.polygon_start: - self.d.pixmap_temp.draw_line(self.d.gc_line,self.d.oldx,self.d.oldy,coords[0],coords[1]) + """ + + if temp == True: + pixmap = self.d.pixmap_temp else: - self.d.pixmap_temp.draw_line(self.d.gc_line,int (self.d.lastx), int (self.d.lasty),coords[0],coords[1]) - self.d.newx = coords[0] - self.d.newy = coords[1] + pixmap = self.d.pixmap + width, height = self.d.window.get_size() + + pixmap.draw_drawable(self.d.gc, self.d.pixmap, 0, 0, 0, 0, width, height) + + if self.d.polygon_start == True: # Starting a new polygon ? + if temp == True: + pixmap.draw_line(self.d.gc_line,self.d.oldx,self.d.oldy, coords[0], coords[1]) + else: + pixmap.draw_line(self.d.gc_line,self.d.oldx,self.d.oldy, coords[0], coords[1]) + self.d.enableUndo(widget) + self.d.lastx = coords[0] + self.d.lasty = coords[1] + self.d.firstx = self.d.oldx + self.d.firsty = self.d.oldy + self.d.polygon_start = False + self.d.points = [(self.d.oldx,self.d.oldy), (coords[0],coords[1])] + else: + if temp == True: + pixmap.draw_line(self.d.gc_line,self.d.lastx,self.d.lasty,coords[0],coords[1]) + else: + x = coords[0] - self.d.firstx + y = coords[1] - self.d.firsty + d = math.hypot(x,y) + if d > 20: # close the polygon ? + pixmap.draw_line(self.d.gc_line,self.d.lastx,self.d.lasty,coords[0],coords[1]) + self.d.lastx = coords[0] + self.d.lasty = coords[1] + self.d.points.append((coords[0],coords[1])) + else: + tp = tuple(self.d.points) + if fill == True: + pixmap.draw_polygon(self.d.gc, True, tp) + pixmap.draw_polygon(self.d.gc_line, False, tp) + self.d.polygon_start = True + self.d.undo_times -= 1#destroy the undo screen of polygon start + self.d.enableUndo(widget) widget.queue_draw() diff --git a/toolbox.py b/toolbox.py index 7b4d76a..cac41c0 100644..100755 --- a/toolbox.py +++ b/toolbox.py @@ -570,7 +570,7 @@ class ShapesToolbar(gtk.Toolbar): _TOOL_SHAPE_HEART = 'heart' _TOOL_SHAPE_LINE = 'line' _TOOL_SHAPE_PARALLELOGRAM = 'parallelogram' - _TOOL_SHAPE_POLYGON = 'polygon' + _TOOL_SHAPE_POLYGON = 'polygon_regular' _TOOL_SHAPE_RECTANGLE = 'rectangle' _TOOL_SHAPE_STAR = 'star' _TOOL_SHAPE_TRAPEZOID = 'trapezoid' @@ -641,12 +641,14 @@ class ShapesToolbar(gtk.Toolbar): self.insert(self._tool_shape_line, -1) self._tool_shape_line.show() self._tool_shape_line.set_tooltip(_('Line')) - """ + self._tool_shape_polygon = ToolButton('tool-shape-polygon') self.insert(self._tool_shape_polygon, -1) self._tool_shape_polygon.show() self._tool_shape_polygon.set_tooltip(_('Polygon')) + """ + self._tool_shape_freeform = ToolButton('tool-shape-freeform') self.insert(self._tool_shape_freeform, -1) self._tool_shape_freeform.show() @@ -695,7 +697,7 @@ class ShapesToolbar(gtk.Toolbar): #self._tool_shape_heart.connect('clicked', self.set_tool, self._TOOL_SHAPE_HEART) self._tool_shape_line.connect('clicked', self.set_tool, self._TOOL_SHAPE_LINE) self._tool_shape_parallelogram.connect('clicked', self.set_tool, self._TOOL_SHAPE_PARALLELOGRAM) - #self._tool_shape_polygon.connect('clicked', self.set_tool, self._TOOL_SHAPE_POLYGON) + self._tool_shape_polygon.connect('clicked', self.set_tool, self._TOOL_SHAPE_POLYGON) self._tool_shape_rectangle.connect('clicked', self.set_tool, self._TOOL_SHAPE_RECTANGLE) self._tool_shape_star.connect('clicked', self.set_tool, self._TOOL_SHAPE_STAR) self._tool_shape_trapezoid.connect('clicked', self.set_tool, self._TOOL_SHAPE_TRAPEZOID) -- cgit v0.9.1