Exemplo n.º 1
0
 def createAttribute(self, xy, text):
     gf = Graphics.Attribute(xy[0],
                             xy[1],
                             canvas=self.canvas,
                             zoom=self.getZoom(),
                             eventHandler=self.mainHandler,
                             text=text)
     self.appendGF(gf)
     self.undoStack.append((self.undo_create, [[gf]]))
     return gf
Exemplo n.º 2
0
    def open(self):
        """ Tries to open an existing graphical form in the AToM3 format and to reproduce it on canvas """

        fileName = os.path.normpath(
            os.path.join(self.modelPath, 'graph_' + self.className + '.py'))
        if (not os.path.exists(fileName)): return []

        nameClass = "graph_" + self.className
        dc = Tkinter.Canvas(self.root)

        # File is already loaded
        if nameClass in sys.modules.keys():
            del sys.modules[nameClass]

        # Make sure we can reach the path and import from it
        sys.path.append(self.modelPath)

        # Load it in memory
        try:
            exec "import graph_" + self.className + "\n"
            sys.path = sys.path[:-1]
        except SyntaxError:  #  class Name not valid
            sys.path = sys.path[:-1]
            print "Syntax Error, Could not open graphical file", self.className
            self.debug()
            return []
        except IOError:  # could not open file (?)
            sys.path = sys.path[:-1]
            print "IO Error, Could not open graphical file", self.className
            self.debug()
            return []
        except ImportError:  # could not open file...
            print "Import Error, Could not open graphical file", self.className
            self.debug()
            sys.path = sys.path[:-1]
            return []

        try:
            # obtain the class object
            className = eval('graph_' + self.className + '.graph_' +
                             self.className)
        except:
            print 'WARNING:', 'graph_'+self.className+'.graph_'+self.className, \
                  'not found'
            return []
        new_obj = className(0, 0)  # create an instance of the new class
        new_obj.DrawObject(dc)  # draw the object

        # Get the constraints
        constraintList = []
        for constraint in new_obj.constraintList:
            constraintList.append(constraint.getValue())
        self.scripting.setConstraintList(constraintList)
        self.scripting.setRunTimeChange(new_obj.ChangesAtRunTime)

        GFlist = []

        # List with the handles of all the shapes drawn
        handleList = []
        for handle in dc.find_withtag(new_obj.tag):
            if (not handle in handleList):
                handleList.append(handle)

        # Get the connectors in self.connectors
        for handle in new_obj.connectors:
            x0, y0, x1, y1 = dc.coords(handle)
            if (new_obj.namedConnectors.has_key(handle)):
                name = new_obj.namedConnectors[handle]
                gf = Graphics.NamedConnector(x0,
                                             y0,
                                             canvas=self.canvas,
                                             eventHandler=self.mainHandler,
                                             name=name)
            else:
                gf = Graphics.Connector(x0,
                                        y0,
                                        canvas=self.canvas,
                                        eventHandler=self.mainHandler)
            GFlist.append(gf)

        # Get the drawn semantic attributes...
        handleAttributeDict = dict()
        for attribute in new_obj.attr_display.keys():
            handleAttributeDict[new_obj.attr_display[attribute]] = attribute

        # Get the image dict, if any
        if (hasattr(new_obj, 'imageDict')):
            Graphics.Image.IMAGE_DICT = new_obj.imageDict

        # Get the GraphicalForm objects
        objectNumberPattern = re.compile('\Agf(\d*)\Z')
        for graphicalForm in new_obj.graphForms:

            handle = graphicalForm.getHandler()
            objectNumber = int(
                objectNumberPattern.search(graphicalForm.getName()).group(1))
            coords = dc.coords(handle)
            objectType = dc.type(handle)

            # Attribute  --- Special Text
            if (handleAttributeDict.has_key(handle)):
                attribute = handleAttributeDict[handle]

                fontObject = graphicalForm.getFont()
                if (fontObject):
                    if (fontObject.cget('weight') == 'bold'): bold = True
                    else: bold = False
                    if (fontObject.cget('slant') == 'bold'): italic = True
                    else: italic = False
                    GFlist.append(
                        Graphics.Attribute(
                            coords[0],
                            coords[1],
                            canvas=self.canvas,
                            eventHandler=self.mainHandler,
                            fill=dc.itemcget(handle, "fill"),
                            text=attribute,
                            anchor=dc.itemcget(handle, "anchor"),
                            family=fontObject.cget('family'),
                            size=int(float(fontObject.cget('size'))),
                            bold=bold,
                            savedNumber=objectNumber,
                            width=int(float(dc.itemcget(handle, "width"))),
                            italic=italic,
                            underline=int(float(
                                fontObject.cget('underline')))))

                # Backward compatibility with graphical appearences that don't have
                # a font object. NOTE: Font type & size info is neccessarily lost.
                else:
                    GFlist.append(
                        Graphics.Attribute(
                            coords[0],
                            coords[1],
                            canvas=self.canvas,
                            eventHandler=self.mainHandler,
                            fill=dc.itemcget(handle, "fill"),
                            text=attribute,
                            width=int(float(dc.itemcget(handle, "width"))),
                            anchor=dc.itemcget(handle, "anchor"),
                            savedNumber=objectNumber))

            elif (objectType == 'text'):

                fontObject = graphicalForm.getFont()
                if (fontObject):
                    if (fontObject.cget('weight') == 'bold'): bold = True
                    else: bold = False
                    if (fontObject.cget('slant') == 'bold'): italic = True
                    else: italic = False
                    GFlist.append(
                        Graphics.Text(
                            coords[0],
                            coords[1],
                            canvas=self.canvas,
                            eventHandler=self.mainHandler,
                            fill=dc.itemcget(handle, "fill"),
                            text=dc.itemcget(handle, "text"),
                            anchor=dc.itemcget(handle, "anchor"),
                            family=fontObject.cget('family'),
                            size=int(float(fontObject.cget('size'))),
                            bold=bold,
                            savedNumber=objectNumber,
                            width=int(float(dc.itemcget(handle, "width"))),
                            italic=italic,
                            underline=int(float(
                                fontObject.cget('underline')))))

                # Backward compatibility with graphical appearences that don't have
                # a font object. NOTE: Font type & size info is neccessarily lost.
                else:
                    GFlist.append(
                        Graphics.Text(coords[0],
                                      coords[1],
                                      canvas=self.canvas,
                                      eventHandler=self.mainHandler,
                                      fill=dc.itemcget(handle, "fill"),
                                      text=dc.itemcget(handle, "text"),
                                      width=int(
                                          float(dc.itemcget(handle, "width"))),
                                      anchor=dc.itemcget(handle, "anchor"),
                                      savedNumber=objectNumber))

            elif (objectType == 'line'):

                if (dc.itemcget(handle, "smooth") == 'bezier'): smooth = True
                else: smooth = False
                GFlist.append(
                    Graphics.Line(coords,
                                  canvas=self.canvas,
                                  eventHandler=self.mainHandler,
                                  fill=dc.itemcget(handle, "fill"),
                                  stipple=dc.itemcget(handle, "stipple"),
                                  arrow=dc.itemcget(handle, "arrow"),
                                  capstyle=dc.itemcget(handle, "capstyle"),
                                  joinstyle=dc.itemcget(handle, "joinstyle"),
                                  smooth=smooth,
                                  savedNumber=objectNumber,
                                  width=int(float(dc.itemcget(handle,
                                                              "width")))))

            elif (objectType == 'polygon'):

                if (dc.itemcget(handle, "smooth") == 'bezier'): smooth = True
                else: smooth = False
                GFlist.append(
                    Graphics.Polygon(coords,
                                     canvas=self.canvas,
                                     eventHandler=self.mainHandler,
                                     fill=dc.itemcget(handle, "fill"),
                                     stipple=dc.itemcget(handle, "stipple"),
                                     smooth=smooth,
                                     outline=dc.itemcget(handle, "outline"),
                                     savedNumber=objectNumber,
                                     width=int(
                                         float(dc.itemcget(handle, "width")))))

            elif (objectType == 'oval'):
                x0, y0, x1, y1 = coords
                GFlist.append(
                    Graphics.Oval(x0,
                                  y0,
                                  x1,
                                  y1,
                                  canvas=self.canvas,
                                  eventHandler=self.mainHandler,
                                  fill=dc.itemcget(handle, "fill"),
                                  outline=dc.itemcget(handle, "outline"),
                                  width=int(float(dc.itemcget(handle,
                                                              "width"))),
                                  savedNumber=objectNumber,
                                  stipple=dc.itemcget(handle, "stipple")))

            elif (objectType == 'rectangle'):

                x0, y0, x1, y1 = coords
                GFlist.append(
                    Graphics.Rectangle(x0,
                                       y0,
                                       x1,
                                       y1,
                                       canvas=self.canvas,
                                       eventHandler=self.mainHandler,
                                       fill=dc.itemcget(handle, "fill"),
                                       outline=dc.itemcget(handle, "outline"),
                                       width=int(
                                           float(dc.itemcget(handle,
                                                             "width"))),
                                       savedNumber=objectNumber,
                                       stipple=dc.itemcget(handle, "stipple")))

            elif (objectType == 'image'):

                fileName = graphicalForm.getImageFilename()
                if (not Graphics.Image.IMAGE_DICT.has_key(fileName)):
                    print "ERROR: could not load image " + fileName + "... SKIPPED"
                    continue
                '''
          pathName = ''
          for path in sys.path:
            if( not os.path.isdir( path ) ): continue
            if( pathName ): break
            for file in os.listdir( path ):
              if( file == fileName ):
                pathName = os.path.join( path, fileName )
                break
      
          if( not fileName ): 
            print "ERROR: could not load image " + fileName + "... SKIPPED"
            continue
          '''
                GFlist.append(
                    Graphics.Image(coords[0],
                                   coords[1],
                                   canvas=self.canvas,
                                   eventHandler=self.mainHandler,
                                   savedNumber=objectNumber,
                                   filename=fileName))

            else:
                print "WARNING: Attempted to load unsupported objectType: " + str(
                    objectType)

        #print GFlist
        dc.destroy()
        return GFlist