# __ File: GraphicalForm.py __________________________________________________________________________________________________ # Implements : class GraphicalForm # Author : Juan de Lara # Description : A wrapper to Canvas objects. It is used when posing graphical constraints # Modified : 23 Oct 2001 # Changes : # - 14 Nov 2001 : Added methods setFill() and setSmooth() (copied from the old version of ATOM3) # ____________________________________________________________________________________________________________________________ from Tkinter import * from line import * from oval import * from rectangle import * from polygon import * from text import * class GraphicalForm: # a useful constant set fillForms = ['line', 'text'] # These forms use fill instead outline def __init__(self, canvas, handler, name): "Constructor, takes a canvas and the handler to be wrapped as well as the name assigned to the object" self.handler = handler # The handler of the associated drawing self.canvas = canvas # The canvas in which the element is being drawn self.name = name # its name self.drawingHandler = None # The handler of the text self.elementType = self.canvas.type(self.handler) # store the element type self.hidden = 0 # flag that indicates if the element is hidden if self.elementType in self.fillForms: # store the element color self.Color = self.canvas.itemcget(self.handler, "fill") else: self.Color = self.canvas.itemcget(self.handler, "outline") self.antColor = self.Color # Remember last color for HighLighting def setCoords(self, coords): "sets the coordinates of the handler" arg = [self.handler] # Put all arguments in a list for c in coords: arg.append(c) apply(self.canvas.coords, arg) # apply the function def getCoords(self): "gets the coordinates of the handler" return self.canvas.coords(self.handler) def setFill(self, color): "sets the color of the shape (color is a string)" self.canvas.itemconfigure( self.handler, fill=color) self.Color = color def setSmooth(self, flag): "sets the color of the shape (color is a string)" self.canvas.itemconfigure( self.handler, smooth=flag) def setColor(self, color): "sets the color of the shape (color is a string)" if self.elementType in self.fillForms: self.canvas.itemconfigure( self.handler, fill=color) else: self.canvas.itemconfigure( self.handler, outline=color) self.Color = color def HighLight(self, flag = 1): "Highlights (flag = 1) or LowLights (flag = 0) the element if it is not hidden" if not self.hidden: # Change color only if item is visible if flag: # Highlights element (draws it with green color) self.antColor = self.Color # Store current color self.setColor('green') # set new color to green else: # Lowlights the element (back to previous color) self.setColor(self.antColor) def showName(self, event): "prints the name in the drawing. This is a callback function invoked when " if self.drawingHandler: self.canvas.delete(self.drawingHandler) self.drawingHandler = None else: self.drawingHandler = self.canvas.create_text(event.x, event.y, text = self.name, fill = 'Magenta') def setVisible(self, switch): "if switch is different from None, it makes the form visible, otherwise, it makes it invisible" if not switch: if self.elementType in self.fillForms: self.canvas.itemconfig(self.handler, fill = "") else: self.canvas.itemconfig(self.handler, outline = "") self.hidden = 1 else: self.setColor(self.Color) self.hidden = 0 def write2File(self, file, indent=" "): """ Writes the properties of this object to a file. Added 10-July-2002 """ itemType = self.canvas.type(self.handler) # get my type # set the coordinates coords = self.canvas.coords(self.handler) # get the coordinates file.write(indent+"self.UMLmodel.coords(new_obj."+self.name+".handler") for c in coords: file.write (","+str(c)) file.write(")\n") itemObject = eval(itemType)() # create an object to show its properties for attr in itemObject.generatedAttributes.keys(): # walk through all the properties and get the value value = self.canvas.itemcget(self.handler, attr) # get the value... # set the attribute value depending on its type... file.write(indent+"self.UMLmodel.itemconfig(new_obj."+self.name+".handler, "+attr+"='"+str(value)+"')\n")