Added support for displaying value of unconnected block connectors in debug
authorLaurent Bessard
Fri, 17 May 2013 20:30:02 +0200
changeset 1166 2ed9675be08d
parent 1165 99972084890d
child 1167 a2f9b44c17c9
Added support for displaying value of unconnected block connectors in debug
editors/Viewer.py
graphics/GraphicCommons.py
--- a/editors/Viewer.py	Fri May 17 19:33:01 2013 +0200
+++ b/editors/Viewer.py	Fri May 17 20:30:02 2013 +0200
@@ -844,11 +844,13 @@
     def GetElementIECPath(self, element):
         iec_path = None
         instance_path = self.GetInstancePath(True)
-        if isinstance(element, Wire) and element.EndConnected is not None:
-            block = element.EndConnected.GetParentBlock()
+        if isinstance(element, (Wire, Connector)):
+            if isinstance(element, Wire):
+                element = element.EndConnected
+            block = element.GetParentBlock()
             if isinstance(block, FBD_Block):
                 blockname = block.GetName()
-                connectorname = element.EndConnected.GetName()
+                connectorname = element.GetName()
                 if blockname != "":
                     iec_path = "%s.%s.%s"%(instance_path, blockname, connectorname)
                 else:
@@ -1092,9 +1094,16 @@
         if self.Debug:
             for block in self.Blocks.itervalues():
                 block.SpreadCurrent()
-                iec_path = self.GetElementIECPath(block)
-                if iec_path is not None:
-                    self.AddDataConsumer(iec_path.upper(), block)
+                if isinstance(block, FBD_Block):
+                    for output_connector in block.GetConnectors()["outputs"]:
+                        if len(output_connector.GetWires()) == 0:
+                            iec_path = self.GetElementIECPath(output_connector)
+                            if iec_path is not None:
+                                self.AddDataConsumer(iec_path.upper(), output_connector)
+                else:
+                    iec_path = self.GetElementIECPath(block)
+                    if iec_path is not None:
+                        self.AddDataConsumer(iec_path.upper(), block)
 
         self.Inhibit(False)
         self.RefreshVisibleElements()
--- a/graphics/GraphicCommons.py	Fri May 17 19:33:01 2013 +0200
+++ b/graphics/GraphicCommons.py	Fri May 17 20:30:02 2013 +0200
@@ -1514,10 +1514,11 @@
 Class that implements a connector for any type of block
 """
 
-class Connector:
+class Connector(DebugDataConsumer):
     
     # Create a new connector
     def __init__(self, parent, name, type, position, direction, negated = False, edge = "none", onlyone = False):
+        DebugDataConsumer.__init__(self)
         self.ParentBlock = parent
         self.Name = name
         self.Type = type
@@ -1534,6 +1535,8 @@
         self.Valid = True
         self.Value = None
         self.Forced = False
+        self.ValueSize = None
+        self.ComputedValue = None
         self.Selected = False
         self.Highlights = []
         self.RefreshNameSize()
@@ -1557,7 +1560,18 @@
             height = 5
         else:
             height = CONNECTOR_SIZE
-        return wx.Rect(x - abs(movex), y - abs(movey), width + 2 * abs(movex), height + 2 * abs(movey))
+        rect =  wx.Rect(x - abs(movex), y - abs(movey), width + 2 * abs(movex), height + 2 * abs(movey))
+        if self.ValueSize is None and isinstance(self.ComputedValue, (StringType, UnicodeType)):
+            self.ValueSize = self.ParentBlock.Parent.GetMiniTextExtent(self.ComputedValue)
+        if self.ValueSize is not None:
+            width, height = self.ValueSize
+            rect = rect.Union(wx.Rect(
+                    parent_pos[0] + self.Pos.x + CONNECTOR_SIZE * self.Direction[0] + \
+                                    width * (self.Direction[0] - 1) / 2,
+                    parent_pos[1] + self.Pos.y + CONNECTOR_SIZE * self.Direction[1] + \
+                                    height * (self.Direction[1] - 1),
+                    width, height))
+        return rect
     
     # Change the connector selection
     def SetSelected(self, selected):
@@ -1621,6 +1635,31 @@
         self.Name = name
         self.RefreshNameSize()
 
+    def SetForced(self, forced):
+        if self.Forced != forced:
+            self.Forced = forced
+            if self.Visible:
+                self.Parent.ElementNeedRefresh(self)
+
+    def SetValue(self, value):
+        if self.Value != value:
+            self.Value = value
+            if value is not None and not isinstance(value, BooleanType):
+                connector_type = self.GetType()
+                if connector_type == "STRING":
+                    self.ComputedValue = "'%s'"%value
+                elif connector_type == "WSTRING":
+                    self.ComputedValue = "\"%s\""%value
+                else:
+                    self.ComputedValue = str(value)
+                #if self.ToolTip is not None:
+                #    self.ToolTip.SetTip(self.ComputedValue)
+                if len(self.ComputedValue) > 4:
+                    self.ComputedValue = self.ComputedValue[:4] + "..."
+            self.ValueSize = None
+            if self.ParentBlock.Visible:
+                self.ParentBlock.Parent.ElementNeedRefresh(self)
+    
     def RefreshForced(self):
         self.Forced = False
         for wire, handle in self.Wires:
@@ -1960,6 +1999,21 @@
         if not getattr(dc, "printing", False):
             DrawHighlightedText(dc, self.Name, self.Highlights, xtext, ytext)
 
+        if self.Value is not None and not isinstance(self.Value, BooleanType) and self.Value != "undefined":
+            dc.SetFont(self.ParentBlock.Parent.GetMiniFont())
+            dc.SetTextForeground(wx.NamedColour("purple"))
+            if self.ValueSize is None and isinstance(self.ComputedValue, (StringType, UnicodeType)):
+                self.ValueSize = self.ParentBlock.Parent.GetMiniTextExtent(self.ComputedValue)
+            if self.ValueSize is not None:
+                width, height = self.ValueSize
+                dc.DrawText(self.ComputedValue, 
+                    parent_pos[0] + self.Pos.x + CONNECTOR_SIZE * self.Direction[0] + \
+                                    width * (self.Direction[0] - 1) / 2,
+                    parent_pos[1] + self.Pos.y + CONNECTOR_SIZE * self.Direction[1] + \
+                                    height * (self.Direction[1] - 1))
+            dc.SetFont(self.ParentBlock.Parent.GetFont())
+            dc.SetTextForeground(wx.BLACK)
+
 #-------------------------------------------------------------------------------
 #                           Common Wire Element
 #-------------------------------------------------------------------------------