graphics/GraphicCommons.py
changeset 42 4a8400732001
parent 28 fc23e1f415d8
child 56 7187e1c00975
--- a/graphics/GraphicCommons.py	Tue Jul 17 12:25:50 2007 +0200
+++ b/graphics/GraphicCommons.py	Wed Jul 18 11:51:30 2007 +0200
@@ -321,6 +321,10 @@
         rect = self.BoundingBox
         return rect.InsideXY(pt.x, pt.y)
     
+    # Returns if the point given is in the bounding box
+    def IsInSelection(self, rect):
+        return rect.InsideXY(self.BoundingBox.x, self.BoundingBox.y) and rect.InsideXY(self.BoundingBox.x + self.BoundingBox.width, self.BoundingBox.y + self.BoundingBox.height)
+    
     # Override this method for refreshing the bounding box
     def RefreshBoundingBox(self):
         pass
@@ -515,12 +519,23 @@
     def __init__(self, parent):
         Graphic_Element.__init__(self, parent)
         self.Elements = []
+        self.RefreshWireExclusion()
         self.RefreshBoundingBox()
     
     # Destructor
     def __del__(self):
         self.Elements = []
     
+    # Refresh the list of wire excluded
+    def RefreshWireExclusion(self):
+        self.WireExcluded = []
+        for element in self.Elements:
+            if isinstance(element, Wire):
+                startblock = element.StartConnected.GetParentBlock()
+                endblock = element.EndConnected.GetParentBlock()
+                if startblock in self.Elements and endblock in self.Elements:
+                    self.WireExcluded.append(element)
+    
     # Make a clone of this group
     def Clone(self):
         clone = Graphic_Group(self.Parent)
@@ -542,6 +557,7 @@
         # Delete all the elements of the group
         for element in self.Elements:
             element.Delete()
+        self.WireExcluded = []
     
     # Returns if the point given is in the bounding box of one of the elements of this group
     def HitTest(self, pt):
@@ -557,6 +573,7 @@
     # Change the elements of the group
     def SetElements(self, elements):
         self.Elements = elements
+        self.RefreshWireExclusion()
         self.RefreshBoundingBox()
     
     # Returns the elements of the group
@@ -569,20 +586,17 @@
             self.Elements.remove(element)
         else:
             self.Elements.append(element)
+        self.RefreshWireExclusion()
         self.RefreshBoundingBox()
     
     # Move this group of elements
     def Move(self, movex, movey):
-        exclude = []
-        for element in self.Elements:
-            if isinstance(element, Wire):
-                exclude.append(element)
         # Move all the elements of the group
         for element in self.Elements:
-            if isinstance(element, Wire):
+            if not isinstance(element, Wire):
+                element.Move(movex, movey, self.WireExcluded)
+            elif element in self.WireExcluded:
                 element.Move(movex, movey, True)
-            else:
-                element.Move(movex, movey, exclude)
         self.RefreshBoundingBox()
     
     # Refreshes the bounding box of this group of elements
@@ -651,6 +665,7 @@
         self.Edge = edge
         self.OneConnected = onlyone
         self.Pen = wxBLACK_PEN
+        self.RefreshNameSize()
     
     # Change the connector pen
     def SetPen(self, pen):
@@ -658,7 +673,7 @@
     
     # Make a clone of the connector
     def Clone(self):
-        return Connector(self.Parent, self.Name, self.Type, wxPoint(self.Pos[0], self.Pos[1]),
+        return Connector(self.ParentBlock, self.Name, self.Type, wxPoint(self.Pos[0], self.Pos[1]),
                 self.Direction, self.Negated)
     
     # Returns the connector parent block
@@ -680,6 +695,19 @@
     # Changes the connector name
     def SetName(self, name):
         self.Name = name
+        self.RefreshNameSize()
+    
+    # Changes the connector name size
+    def RefreshNameSize(self):
+        if self.Name != "":
+            dc = wxClientDC(self.ParentBlock.Parent)
+            self.NameSize = dc.GetTextExtent(self.Name)
+        else:
+            self.NameSize = 0, 0
+    
+    # Returns the connector name size
+    def GetNameSize(self):
+        return self.NameSize
     
     # Returns the wires connected to the connector
     def GetWires(self):
@@ -856,20 +884,18 @@
             xend = xstart + CONNECTOR_SIZE * self.Direction[0]
             yend = ystart + CONNECTOR_SIZE * self.Direction[1]
             dc.DrawLine(xstart + self.Direction[0], ystart + self.Direction[1], xend, yend)
-        # Calculate the position of the text
-        text_size = dc.GetTextExtent(self.Name)
         if self.Direction[0] != 0:
-            ytext = parent_pos[1] + self.Pos.y - text_size[1] / 2
+            ytext = parent_pos[1] + self.Pos.y - self.NameSize[1] / 2
             if self.Direction[0] < 0:
                 xtext = parent_pos[0] + self.Pos.x + 5
             else:
-                xtext = parent_pos[0] + self.Pos.x - (text_size[0] + 5)
+                xtext = parent_pos[0] + self.Pos.x - (self.NameSize[0] + 5)
         if self.Direction[1] != 0:
-            xtext = parent_pos[0] + self.Pos.x - text_size[0] / 2
+            xtext = parent_pos[0] + self.Pos.x - self.NameSize[0] / 2
             if self.Direction[1] < 0:
                 ytext = parent_pos[1] + self.Pos.y + 5
             else:
-                ytext = parent_pos[1] + self.Pos.y - (text_size[1] + 5)
+                ytext = parent_pos[1] + self.Pos.y - (self.NameSize[1] + 5)
         # Draw the text
         dc.DrawText(self.Name, xtext, ytext)
 
@@ -1139,7 +1165,7 @@
         if index == 0 and self.StartConnected:
             return self.StartConnected.GetBlockId(), self.StartConnected.GetName()
         elif index == -1 and self.EndConnected:
-            return self.EndConnected.GetBlockId(), self.StartConnected.GetName()
+            return self.EndConnected.GetBlockId(), self.EndConnected.GetName()
         return None
     
     # Update the wire points position by keeping at most possible the current positions