graphics/LD_Objects.py
changeset 27 dae55dd9ee14
parent 8 7ceec5c40d77
child 28 fc23e1f415d8
--- a/graphics/LD_Objects.py	Sat Jul 07 11:35:17 2007 +0200
+++ b/graphics/LD_Objects.py	Mon Jul 09 11:10:14 2007 +0200
@@ -43,6 +43,9 @@
         Graphic_Element.__init__(self, parent)
         self.Type = type
         self.Id = id
+        self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2]
+        if len(connectors) < 1:
+            connectors = [True]
         # Create a connector or a blank according to 'connectors' and add it in
         # the connectors list
         self.Connectors = []
@@ -56,10 +59,14 @@
     
     # Forbids to change the power rail size
     def SetSize(self, width, height):
-        pass
+        if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
+            Graphic_Element.SetSize(self, width, height)
+            self.RefreshConnectors()
     
     # Forbids to select a power rail
     def HitTest(self, pt):
+        if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
+            return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, False) != None
         return False
     
     # Deletes this power rail by calling the appropriate method
@@ -75,17 +82,17 @@
     # Refresh the power rail bounding box
     def RefreshBoundingBox(self):
         dc = wxClientDC(self.Parent)
-        if self.Type == LEFTRAIL:
-            bbx_x = self.Pos.x
-        elif self.Type == RIGHTRAIL:
-            bbx_x = self.Pos.x - CONNECTOR_SIZE
-        self.BoundingBox = wxRect(bbx_x, self.Pos.y, self.Size[0] + CONNECTOR_SIZE + 1, self.Size[1] + 1)
+        self.BoundingBox = wxRect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1] + 1)
     
     # Refresh the power rail size
     def RefreshSize(self):
         self.Size = wxSize(2, LD_LINE_SIZE * len(self.Connectors))
         self.RefreshBoundingBox()
     
+    # Returns the block minimum size
+    def GetMinSize(self):
+        return 2, LD_LINE_SIZE * len(self.Connectors)
+    
     # Add a connector or a blank to this power rail at the last place
     def AddConnector(self, connector = True):
         self.InsertConnector(len(self.Connectors), connector)
@@ -103,6 +110,26 @@
         self.RefreshSize()
         self.RefreshConnectors()
     
+    # Moves the divergence connector given
+    def MoveConnector(self, connector, movey):
+        position = connector.GetRelPosition()
+        connector.SetPosition(wxPoint(position.x, position.y + movey))
+        miny = self.Size[1]
+        maxy = 0
+        for connect in self.Connectors:
+            connect_pos = connect.GetRelPosition()
+            miny = min(miny, connect_pos.y)
+            maxy = max(maxy, connect_pos.y)
+        min_pos = self.Pos.y + miny - self.Extensions[0]
+        self.Pos.y = min(min_pos, self.Pos.y)
+        if min_pos == self.Pos.y:
+            for connect in self.Connectors:
+                connect_pos = connect.GetRelPosition()
+                connect.SetPosition(wxPoint(connect_pos.x, connect_pos.y - miny + self.Extensions[0]))
+        self.Size[1] = max(maxy - miny + self.Extensions[0] + self.Extensions[1], self.Size[1])
+        connector.MoveConnected()
+        self.RefreshBoundingBox()
+    
     # Returns the index in connectors list for the connector given
     def GetConnectorIndex(self, connector):
         if connector in self.Connectors:
@@ -123,7 +150,7 @@
     
     # Refresh the positions of the power rail connectors
     def RefreshConnectors(self):
-        position = LD_LINE_SIZE / 2
+        position = self.Extensions[0]
         for connector in self.Connectors:
             if connector:
                 if self.Type == LEFTRAIL:
@@ -140,7 +167,13 @@
                 connector.MoveConnected(exclude)
     
     # Returns the power rail connector that starts with the point given if it exists 
-    def GetConnector(self, position):
+    def GetConnector(self, position, name = None):
+        # if a name is given
+        if name:
+            # Test each connector if it exists
+            for connector in self.Connectors:
+                if connector and name == connector.GetName():
+                    return connector
         for connector in self.Connectors:
             if connector:
                 connector_pos = connector.GetRelPosition()
@@ -163,6 +196,63 @@
     def GetType(self):
         return self.Type
     
+    # Method called when a LeftDown event have been generated
+    def OnLeftDown(self, event, dc, scaling):
+        pos = GetScaledEventPosition(event, dc, scaling)
+        # Test if a connector have been handled
+        connector = self.TestConnector(pos, False)
+        if connector:
+            self.Handle = (HANDLE_CONNECTOR, connector)
+            self.Parent.SetCursor(wxStockCursor(wxCURSOR_HAND))
+            self.Selected = False
+            # Initializes the last position
+            self.oldPos = GetScaledEventPosition(event, dc, scaling)
+        else:
+            self.RealConnectors = {"Inputs":[],"Outputs":[]}
+            for input in self.Inputs:
+                position = input.GetRelPosition()
+                self.RealConnectors["Inputs"].append(float(position.x)/float(self.Size[0]))
+            for output in self.Outputs:
+                position = output.GetRelPosition()
+                self.RealConnectors["Outputs"].append(float(position.x)/float(self.Size[0]))
+            Graphic_Element.OnLeftDown(self, event, dc, scaling)
+    
+    # Method called when a LeftUp event have been generated
+    def OnLeftUp(self, event, dc, scaling):
+        self.RealConnectors = None
+        handle_type, handle = self.Handle
+        if handle_type == HANDLE_CONNECTOR:
+            wires = handle.GetWires()
+            if len(wires) != 1:
+                return
+            if handle == wires[0][0].StartConnected:
+                block = wires[0][0].EndConnected.GetParentBlock()
+            else:
+                block = wires[0][0].StartConnected.GetParentBlock()
+            block.RefreshModel(False)
+        Graphic_Element.OnLeftUp(self, event, dc, scaling)
+    
+    # Method called when a RightUp event have been generated
+    def OnRightUp(self, event, dc, scaling):
+        pos = GetScaledEventPosition(event, dc, scaling)
+        # Popup the menu with special items for a block and a connector if one is handled
+        connector = self.TestConnector(pos, False)
+        if connector:
+            self.Handle = (HANDLE_CONNECTOR, connector)
+        #    self.Parent.PopupDivergenceMenu(True)
+        #else:
+            # Popup the divergence menu without delete branch
+        #    self.Parent.PopupDivergenceMenu(False)
+    
+    # Refreshes the divergence state according to move defined and handle selected
+    def ProcessDragging(self, movex, movey):
+        handle_type, handle = self.Handle
+        # A connector has been handled
+        if handle_type == HANDLE_CONNECTOR:
+            self.MoveConnector(handle, movey)
+        else:
+            Graphic_Element.ProcessDragging(self, movex, movey)
+    
     # Refreshes the power rail model
     def RefreshModel(self, move=True):
         self.Parent.RefreshPowerRailModel(self)
@@ -214,7 +304,9 @@
     
     # Forbids to change the contact size
     def SetSize(self, width, height):
-        pass
+        if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
+            Graphic_Element.SetSize(self, width, height)
+            self.RefreshConnectors()
     
     # Delete this contact by calling the appropriate method
     def Delete(self):
@@ -243,13 +335,24 @@
             bbx_height = self.Size[1]
         self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
     
+    # Returns the block minimum size
+    def GetMinSize(self):
+        return LD_ELEMENT_SIZE
+    
     # Refresh the position of wire connected to contact
     def RefreshConnected(self, exclude = []):
         self.Input.MoveConnected(exclude)
         self.Output.MoveConnected(exclude)
     
     # Returns the contact connector that starts with the point given if it exists 
-    def GetConnector(self, position):
+    def GetConnector(self, position, name = None):
+        # if a name is given
+        if name:
+            # Test input and output connector
+            if name == self.Input.GetName():
+                return self.Input
+            if name == self.Output.GetName():
+                return self.Output
         # Test input connector
         input_pos = self.Input.GetRelPosition()
         if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
@@ -274,6 +377,12 @@
             return self.Output
         return None
 
+    # Refresh the positions of the block connectors
+    def RefreshConnectors(self):
+        self.Input.SetPosition(wxPoint(0, self.Size[1] / 2 + 1))
+        self.Output.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2 + 1))
+        self.RefreshConnected()
+
     # Changes the contact name
     def SetName(self, name):
         self.Name = name
@@ -291,7 +400,7 @@
         return self.Type
     
     # Method called when a LeftDClick event have been generated
-    def OnLeftDClick(self, event, scaling):
+    def OnLeftDClick(self, event, dc, scaling):
         # Edit the contact properties
         self.Parent.EditContactContent(self)
     
@@ -359,7 +468,9 @@
     
     # Forbids to change the contact size
     def SetSize(self, width, height):
-        pass
+        if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
+            Graphic_Element.SetSize(self, width, height)
+            self.RefreshConnectors()
     
     # Delete this coil by calling the appropriate method
     def Delete(self):
@@ -387,6 +498,10 @@
             bbx_y = self.Pos.y
             bbx_height = self.Size[1]
         self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
+        
+    # Returns the block minimum size
+    def GetMinSize(self):
+        return LD_ELEMENT_SIZE
     
     # Refresh the position of wire connected to coil
     def RefreshConnected(self, exclude = []):
@@ -394,7 +509,14 @@
         self.Output.MoveConnected(exclude)
     
     # Returns the coil connector that starts with the point given if it exists 
-    def GetConnector(self, position):
+    def GetConnector(self, position, name = None):
+        # if a name is given
+        if name:
+            # Test input and output connector
+            if self.Input and name == self.Input.GetName():
+                return self.Input
+            if self.Output and name == self.Output.GetName():
+                return self.Output
         # Test input connector
         input_pos = self.Input.GetRelPosition()
         if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
@@ -419,6 +541,12 @@
             return self.Output
         return None
     
+    # Refresh the positions of the block connectors
+    def RefreshConnectors(self):
+        self.Input.SetPosition(wxPoint(0, self.Size[1] / 2 + 1))
+        self.Output.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2 + 1))
+        self.RefreshConnected()
+    
     # Changes the coil name
     def SetName(self, name):
         self.Name = name
@@ -436,7 +564,7 @@
         return self.Type
     
     # Method called when a LeftDClick event have been generated
-    def OnLeftDClick(self, event, scaling):
+    def OnLeftDClick(self, event, dc, scaling):
         # Edit the coil properties
         self.Parent.EditCoilContent(self)