graphics/FBD_Objects.py
changeset 231 fc2d6cbb8b39
parent 213 4931959ea256
child 243 c5da8b706cde
--- a/graphics/FBD_Objects.py	Tue Aug 12 18:15:35 2008 +0200
+++ b/graphics/FBD_Objects.py	Tue Aug 12 18:16:09 2008 +0200
@@ -26,7 +26,6 @@
 
 from GraphicCommons import *
 from plcopen.structures import *
-from plcopen.structures import IsOfType
 
 #-------------------------------------------------------------------------------
 #                         Function Block Diagram Block
@@ -51,6 +50,7 @@
         self.Colour = wx.BLACK
         self.Pen = wx.BLACK_PEN
         self.SetType(type, extension, inputs, connectors)
+        self.Errors = {}
     
     # Make a clone of this FBD_Block
     def Clone(self, parent, id = None, name = "", pos = None):
@@ -183,13 +183,13 @@
             name = input.GetName()
             if input != connector and (name.startswith("IN") or name in ["MN", "MX"]):
                 inputtype = input.GetConnectedType()
-                if resulttype is None or inputtype is not None and IsOfType(inputtype, resulttype):
+                if resulttype is None or inputtype is not None and self.IsOfType(inputtype, resulttype):
                     resulttype = inputtype
         for output in self.Outputs:
             name = output.GetName()
-            if output != connector and name == "OUT" and not IsEndType(output.GetType()):
+            if output != connector and name == "OUT" and not self.IsEndType(output.GetType()):
                 outputtype = output.GetConnectedType()
-                if resulttype is None or outputtype is not None and IsOfType(outputtype, resulttype):
+                if resulttype is None or outputtype is not None and self.IsOfType(outputtype, resulttype):
                     resulttype = outputtype
         return resulttype
     
@@ -218,7 +218,7 @@
             self.Extension = extension
             # Find the block definition from type given and create the corresponding
             # inputs and outputs
-            blocktype = GetBlockType(type, inputs)
+            blocktype = self.Parent.GetBlockType(type, inputs)
             if blocktype:
                 self.Colour = wx.BLACK
                 inputs = [input for input in blocktype["inputs"]]
@@ -350,6 +350,14 @@
             for output in self.Outputs:
                 output.RefreshWires()
     
+    def AddError(self, infos, start, end):
+        if infos[0] in ["type", "name"] and start[0] == 0 and end[0] == 0:
+            self.Errors[infos[0]] = (start, end)
+        elif infos[0] == "input" and infos[1] < len(self.Inputs):
+            self.Inputs[infos[1]].AddError(infos[2:], start, end)
+        elif infos[0] == "output" and infos[1] < len(self.Outputs):
+            self.Outputs[infos[1]].AddError(infos[2:], start, end)
+    
     # Draws block
     def Draw(self, dc):
         Graphic_Element.Draw(self, dc)
@@ -369,10 +377,12 @@
         # Draw a rectangle with the block size
         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
         # Draw block name and block type
-        dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - name_size[0]) / 2,
-                self.Pos.y - (name_size[1] + 2))
-        dc.DrawText(self.Type, self.Pos.x + (self.Size[0] - type_size[0]) / 2,
-                self.Pos.y + 5)
+        name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
+                    self.Pos.y - (name_size[1] + 2))
+        type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2,
+                    self.Pos.y + 5)
+        dc.DrawText(self.Name, name_pos[0], name_pos[1])
+        dc.DrawText(self.Type, type_pos[0], type_pos[1])
         # Draw inputs and outputs connectors
         for input in self.Inputs:
             input.Draw(dc)
@@ -382,6 +392,10 @@
             # Draw block execution order
             dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0],
                     self.Pos.y + self.Size[1] + 2)
+        if "name" in self.Errors:
+            HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1])
+        if "type" in self.Errors:
+            HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1])    
         dc.SetTextForeground(wx.BLACK)
         
 
@@ -406,6 +420,7 @@
         self.Input = None
         self.Output = None
         self.SetType(type, value_type)
+        self.Errors = []
     
     # Make a clone of this FBD_Variable
     def Clone(self, parent, id = None, pos = None):
@@ -594,6 +609,10 @@
             if self.Output:
                 self.Output.RefreshWires()
     
+    def AddError(self, infos, start, end):
+        if infos[0] == "expression" and start[0] == 0 and end[0] == 0:
+            self.Errors.append((start[1], end[1]))
+    
     # Draws variable
     def Draw(self, dc):
         Graphic_Element.Draw(self, dc)
@@ -607,11 +626,12 @@
             name_size = self.NameSize
             executionorder_size = self.ExecutionOrderSize
         
+        text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, 
+                    self.Pos.y + (self.Size[1] - name_size[1]) / 2)
         # Draw a rectangle with the variable size
         dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
         # Draw variable name
-        dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - name_size[0]) / 2,
-                self.Pos.y + (self.Size[1] - name_size[1]) / 2)
+        dc.DrawText(self.Name, text_pos[0], text_pos[1])
         # Draw connectors
         if self.Input:
             self.Input.Draw(dc)
@@ -621,7 +641,10 @@
             # Draw variable execution order
             dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0],
                     self.Pos.y + self.Size[1] + 2)
-
+        for start, end in self.Errors:
+            offset = dc.GetTextExtent(self.Name[:start])
+            size = dc.GetTextExtent(self.Name[start:end + 1])
+            HighlightErrorZone(dc, text_pos[0] + offset[0], text_pos[1], size[0], size[1])
 
 #-------------------------------------------------------------------------------
 #                        Function Block Diagram Connector