graphics/FBD_Objects.py
changeset 3 86ccc89d7b0b
parent 2 93bc4c2cf376
child 5 f8652b073e84
--- a/graphics/FBD_Objects.py	Wed Feb 07 18:43:32 2007 +0100
+++ b/graphics/FBD_Objects.py	Thu Feb 08 17:41:41 2007 +0100
@@ -42,6 +42,7 @@
     # Create a new block
     def __init__(self, parent, type, name, id = None, extension = 0, inputs = [], outputs = []):
         Graphic_Element.__init__(self, parent)
+        self.Type = None
         self.Name = name
         self.Id = id
         self.Extension = extension
@@ -124,7 +125,7 @@
         return {"inputs" : self.Inputs, "outputs" : self.Outputs}
     
     # Test if point given is on one of the block connectors
-    def TestConnector(self, pt, exclude=True):
+    def TestConnector(self, pt, exclude = True):
         # Test each input connector
         for input in self.Inputs:
             if input.TestPoint(pt, exclude):
@@ -137,19 +138,40 @@
     
     # Changes the block type
     def SetType(self, type):
-        self.Type = type
-        # Find the block definition from type given and create the corresponding
-        # inputs and outputs
-        blocktype = GetBlockType(type)
-        if blocktype:
-            inputs = [input for input in blocktype["inputs"]]
-            outputs = [output for output in blocktype["outputs"]]
-            if blocktype["extensible"]:
-                start = int(inputs[-1][0].replace("IN", ""))
-                for i in xrange(self.Extension - len(blocktype["inputs"])):
-                    start += 1
-                    inputs.append(("IN%d"%start, inputs[-1][1], input[-1][2]))
-        self.SetConnectors(inputs, outputs)
+        if type != self.Type: 
+            self.Type = type
+            # Find the block definition from type given and create the corresponding
+            # inputs and outputs
+            blocktype = GetBlockType(type)
+            if blocktype:
+                inputs = [input for input in blocktype["inputs"]]
+                outputs = [output for output in blocktype["outputs"]]
+                if blocktype["extensible"]:
+                    start = int(inputs[-1][0].replace("IN", ""))
+                    for i in xrange(self.Extension - len(blocktype["inputs"])):
+                        start += 1
+                        inputs.append(("IN%d"%start, inputs[-1][1], input[-1][2]))
+            self.Clean()
+            # Extract the inputs properties and create the corresponding connector
+            self.Inputs = []
+            for input_name, input_type, input_modifier in inputs:
+                connector = Connector(self, input_name, input_type, wxPoint(0, 0), WEST)
+                if input_modifier == "negated":
+                    connector.SetNegated(True)
+                elif input_modifier != "none":
+                    connector.SetEdge(input_modifier)
+                self.Inputs.append(connector)
+            # Extract the outputs properties and create the corresponding connector
+            self.Outputs = []
+            for output_name, output_type, output_modifier in outputs:
+                connector = Connector(self, output_name, output_type, wxPoint(0, 0), EAST)
+                if output_modifier == "negated":
+                    connector.SetNegated(True)
+                elif output_modifier != "none":
+                    connector.SetEdge(output_modifier)
+                self.Outputs.append(connector)
+            self.RefreshConnectors()
+            self.RefreshBoundingBox()
     
     # Returns the block type
     def GetType(self):
@@ -189,30 +211,6 @@
         height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE
         return width, height
     
-    # Changes the block connectors
-    def SetConnectors(self, inputs, outputs):
-        self.Clean()
-        # Extract the inputs properties and create the corresponding connector
-        self.Inputs = []
-        for input_name, input_type, input_modifier in inputs:
-            connector = Connector(self, input_name, input_type, wxPoint(0, 0), WEST)
-            if input_modifier == "negated":
-                connector.SetNegated(True)
-            elif input_modifier != "none":
-                connector.SetEdge(input_modifier)
-            self.Inputs.append(connector)
-        # Extract the outputs properties and create the corresponding connector
-        self.Outputs = []
-        for output_name, output_type, output_modifier in outputs:
-            connector = Connector(self, output_name, output_type, wxPoint(0, 0), EAST)
-            if output_modifier == "negated":
-                connector.SetNegated(True)
-            elif output_modifier != "none":
-                connector.SetEdge(output_modifier)
-            self.Outputs.append(connector)
-        self.RefreshConnectors()
-        self.RefreshBoundingBox()
-    
     # Changes the negated property of the connector handled
     def SetConnectorNegated(self, negated):
         handle_type, handle = self.Handle
@@ -229,7 +227,7 @@
     
     # Method called when a LeftDClick event have been generated
     def OnLeftDClick(self, event, scaling):
-        # Edit the step properties
+        # Edit the block properties
         self.Parent.EditBlockContent(self)
     
     # Method called when a RightUp event have been generated
@@ -285,17 +283,13 @@
     # Create a new variable
     def __init__(self, parent, type, name, value_type, id = None):
         Graphic_Element.__init__(self, parent)
-        self.Type = type
+        self.Type = None
+        self.ValueType = None
         self.Name = name
         self.Id = id
         self.Input = None
         self.Output = None
-        # Create an input or output connector according to variable type
-        if self.Type != INPUT:
-            self.Input = Connector(self, "", value_type, wxPoint(0, 0), WEST)
-        if self.Type != OUTPUT:
-            self.Output = Connector(self, "", value_type, wxPoint(0, 0), EAST)
-        self.RefreshConnectors()
+        self.SetType(type, value_type)
     
     # Destructor
     def __del__(self):
@@ -305,9 +299,9 @@
     # Unconnect connector
     def Clean(self):
         if self.Input:
-            self.Input.UnConnect()
+            self.Input.UnConnect(delete = True)
         if self.Output:
-            self.Output.UnConnect()
+            self.Output.UnConnect(delete = True)
     
     # Delete this variable by calling the appropriate method
     def Delete(self):
@@ -374,6 +368,21 @@
             handle.SetNegated(negated)
             self.RefreshModel(False)
     
+    # Changes the variable type
+    def SetType(self, type, value_type):
+        if type != self.Type or value_type != self.ValueType:
+            self.Type = type
+            self.ValueType = value_type
+            self.Clean()
+            self.Input = None
+            self.Output = None
+            # Create an input or output connector according to variable type
+            if self.Type != INPUT:
+                self.Input = Connector(self, "", value_type, wxPoint(0, 0), WEST)
+            if self.Type != OUTPUT:
+                self.Output = Connector(self, "", value_type, wxPoint(0, 0), EAST)
+            self.RefreshConnectors()
+    
     # Returns the variable type
     def GetType(self):
         return self.Type
@@ -392,6 +401,11 @@
         text_width, text_height = dc.GetTextExtent(self.Name)
         return text_width + 10, text_height + 10
     
+    # Method called when a LeftDClick event have been generated
+    def OnLeftDClick(self, event, scaling):
+        # Edit the variable properties
+        self.Parent.EditVariableContent(self)
+    
     # Method called when a RightUp event have been generated
     def OnRightUp(self, event, scaling):
         pos = GetScaledEventPosition(event, scaling)