editors/Viewer.py
changeset 828 319dac4c4fd3
parent 823 c6ed7b933617
child 832 b2609a8e4cb6
equal deleted inserted replaced
827:a2ce084fb598 828:319dac4c4fd3
   368 """
   368 """
   369 Class that implements a Viewer based on a wx.ScrolledWindow for drawing and 
   369 Class that implements a Viewer based on a wx.ScrolledWindow for drawing and 
   370 manipulating graphic elements
   370 manipulating graphic elements
   371 """
   371 """
   372 
   372 
   373 class Viewer(EditorPanel, DebugViewer):
   373 class Viewer(EditorPanel, DebugViewer, DebugDataConsumer):
   374     
   374     
   375     if wx.VERSION < (2, 6, 0):
   375     if wx.VERSION < (2, 6, 0):
   376         def Bind(self, event, function, id = None):
   376         def Bind(self, event, function, id = None):
   377             if id is not None:
   377             if id is not None:
   378                 event(self, id, function)
   378                 event(self, id, function)
   548     def __init__(self, parent, tagname, window, controler, debug = False, instancepath = ""):
   548     def __init__(self, parent, tagname, window, controler, debug = False, instancepath = ""):
   549         self.VARIABLE_PANEL_TYPE = controler.GetPouType(tagname.split("::")[1])
   549         self.VARIABLE_PANEL_TYPE = controler.GetPouType(tagname.split("::")[1])
   550         
   550         
   551         EditorPanel.__init__(self, parent, tagname, window, controler, debug)
   551         EditorPanel.__init__(self, parent, tagname, window, controler, debug)
   552         DebugViewer.__init__(self, controler, debug)
   552         DebugViewer.__init__(self, controler, debug)
       
   553         DebugDataConsumer.__init__(self)
   553         
   554         
   554         # Adding a rubberband to Viewer
   555         # Adding a rubberband to Viewer
   555         self.rubberBand = RubberBand(viewer=self)
   556         self.rubberBand = RubberBand(viewer=self)
   556         self.Editor.SetBackgroundColour(wx.Colour(255,255,255))
   557         self.Editor.SetBackgroundColour(wx.Colour(255,255,255))
   557         self.Editor.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
   558         self.Editor.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
   659         return EditorPanel.GetTitle(self)
   660         return EditorPanel.GetTitle(self)
   660     
   661     
   661     def GetScaling(self):
   662     def GetScaling(self):
   662         return self.Scaling
   663         return self.Scaling
   663     
   664     
   664     def GetInstancePath(self):
   665     def GetInstancePath(self, variable_base=False):
       
   666         if variable_base:
       
   667             words = self.TagName.split("::")
       
   668             if words[0] in ["A", "T"]:
       
   669                 return ".".join(self.InstancePath.split(".")[:-1])
   665         return self.InstancePath
   670         return self.InstancePath
   666     
   671     
   667     def IsViewing(self, tagname):
   672     def IsViewing(self, tagname):
   668         if self.Debug:
   673         if self.Debug:
   669             return self.InstancePath == tagname
   674             return self.InstancePath == tagname
   838         for block in self.Blocks.itervalues():
   843         for block in self.Blocks.itervalues():
   839             block.TestVisible(screen)
   844             block.TestVisible(screen)
   840     
   845     
   841     def GetElementIECPath(self, element):
   846     def GetElementIECPath(self, element):
   842         iec_path = None
   847         iec_path = None
       
   848         instance_path = self.GetInstancePath(True)
   843         if isinstance(element, Wire) and element.EndConnected is not None:
   849         if isinstance(element, Wire) and element.EndConnected is not None:
   844             block = element.EndConnected.GetParentBlock()
   850             block = element.EndConnected.GetParentBlock()
   845             if isinstance(block, FBD_Block):
   851             if isinstance(block, FBD_Block):
   846                 blockname = block.GetName()
   852                 blockname = block.GetName()
   847                 connectorname = element.EndConnected.GetName()
   853                 connectorname = element.EndConnected.GetName()
   848                 if blockname != "":
   854                 if blockname != "":
   849                     iec_path = "%s.%s.%s"%(self.InstancePath, blockname, connectorname)
   855                     iec_path = "%s.%s.%s"%(instance_path, blockname, connectorname)
   850                 else:
   856                 else:
   851                     if connectorname == "":
   857                     if connectorname == "":
   852                         iec_path = "%s.%s%d"%(self.InstancePath, block.GetType(), block.GetId())
   858                         iec_path = "%s.%s%d"%(instance_path, block.GetType(), block.GetId())
   853                     else:
   859                     else:
   854                         iec_path = "%s.%s%d_%s"%(self.InstancePath, block.GetType(), block.GetId(), connectorname)
   860                         iec_path = "%s.%s%d_%s"%(instance_path, block.GetType(), block.GetId(), connectorname)
   855             elif isinstance(block, FBD_Variable):
   861             elif isinstance(block, FBD_Variable):
   856                 iec_path = "%s.%s"%(self.InstancePath, block.GetName())
   862                 iec_path = "%s.%s"%(instance_path, block.GetName())
   857             elif isinstance(block, FBD_Connector):
   863             elif isinstance(block, FBD_Connector):
   858                 connection = self.GetConnectorByName(block.GetName())
   864                 connection = self.GetConnectorByName(block.GetName())
   859                 if connection is not None:
   865                 if connection is not None:
   860                     connector = connection.GetConnector()
   866                     connector = connection.GetConnector()
   861                     if len(connector.Wires) == 1:
   867                     if len(connector.Wires) == 1:
   862                         iec_path = self.GetElementIECPath(connector.Wires[0][0])
   868                         iec_path = self.GetElementIECPath(connector.Wires[0][0])
   863         elif isinstance(element, LD_Contact):
   869         elif isinstance(element, LD_Contact):
   864             iec_path = "%s.%s"%(self.InstancePath, element.GetName())
   870             iec_path = "%s.%s"%(instance_path, element.GetName())
   865         elif isinstance(element, SFC_Step):
   871         elif isinstance(element, SFC_Step):
   866             iec_path = "%s.%s.X"%(self.InstancePath, element.GetName())
   872             iec_path = "%s.%s.X"%(instance_path, element.GetName())
   867         elif isinstance(element, SFC_Transition):
   873         elif isinstance(element, SFC_Transition):
   868             connectors = element.GetConnectors()
   874             connectors = element.GetConnectors()
   869             previous_steps = self.GetPreviousSteps(connectors["inputs"])
   875             previous_steps = self.GetPreviousSteps(connectors["inputs"])
   870             next_steps = self.GetNextSteps(connectors["outputs"])
   876             next_steps = self.GetNextSteps(connectors["outputs"])
   871             iec_path = "%s.%s->%s"%(self.InstancePath, ",".join(previous_steps), ",".join(next_steps))
   877             iec_path = "%s.%s->%s"%(instance_path, ",".join(previous_steps), ",".join(next_steps))
   872         return iec_path
   878         return iec_path
   873        
   879        
   874 #-------------------------------------------------------------------------------
   880 #-------------------------------------------------------------------------------
   875 #                              Reset functions
   881 #                              Reset functions
   876 #-------------------------------------------------------------------------------
   882 #-------------------------------------------------------------------------------
   993         
   999         
   994         
  1000         
   995 #-------------------------------------------------------------------------------
  1001 #-------------------------------------------------------------------------------
   996 #                          Refresh functions
  1002 #                          Refresh functions
   997 #-------------------------------------------------------------------------------
  1003 #-------------------------------------------------------------------------------
   998 
  1004     
       
  1005     VALUE_TRANSLATION = {True: _("Active"), False: _("Inactive")}
       
  1006 
       
  1007     def SetValue(self, value):
       
  1008         if self.Value != value:
       
  1009             self.Value = value
       
  1010             
       
  1011             xstart, ystart = self.GetViewStart()
       
  1012             window_size = self.Editor.GetClientSize()
       
  1013             refresh_rect = self.GetRedrawRect()
       
  1014             if (xstart * SCROLLBAR_UNIT <= refresh_rect.x + refresh_rect.width and 
       
  1015                 xstart * SCROLLBAR_UNIT + window_size[0] >= refresh_rect.x and
       
  1016                 ystart * SCROLLBAR_UNIT <= refresh_rect.y + refresh_rect.height and 
       
  1017                 ystart * SCROLLBAR_UNIT + window_size[1] >= refresh_rect.y):
       
  1018                 self.ElementNeedRefresh(self)
       
  1019     
       
  1020     def GetRedrawRect(self):
       
  1021         dc = self.GetLogicalDC()
       
  1022         ipw, iph = dc.GetTextExtent(_("Debug: %s") % self.InstancePath)
       
  1023         vw, vh = 0, 0
       
  1024         for value in self.VALUE_TRANSLATION.itervalues():
       
  1025             w, h = dc.GetTextExtent("(%s)" % value)
       
  1026             vw = max(vw, w)
       
  1027             vh = max(vh, h)
       
  1028         return wx.Rect(ipw + 4, 2, vw, vh)
       
  1029     
   999     def ElementNeedRefresh(self, element):
  1030     def ElementNeedRefresh(self, element):
  1000         self.ElementRefreshList_lock.acquire()
  1031         self.ElementRefreshList_lock.acquire()
  1001         self.ElementRefreshList.append(element)
  1032         self.ElementRefreshList.append(element)
  1002         self.ElementRefreshList_lock.release()
  1033         self.ElementRefreshList_lock.release()
  1003         
  1034         
  1018             DebugViewer.RefreshNewData(self)
  1049             DebugViewer.RefreshNewData(self)
  1019         
  1050         
  1020     # Refresh Viewer elements
  1051     # Refresh Viewer elements
  1021     def RefreshView(self, variablepanel=True, selection=None):
  1052     def RefreshView(self, variablepanel=True, selection=None):
  1022         EditorPanel.RefreshView(self, variablepanel)
  1053         EditorPanel.RefreshView(self, variablepanel)
       
  1054         
       
  1055         if self.TagName.split("::")[0] == "A":
       
  1056             self.AddDataConsumer("%s.Q" % self.InstancePath.upper(), self)
  1023         
  1057         
  1024         if self.ToolTipElement is not None:
  1058         if self.ToolTipElement is not None:
  1025             self.ToolTipElement.ClearToolTip()
  1059             self.ToolTipElement.ClearToolTip()
  1026             self.ToolTipElement = None
  1060             self.ToolTipElement = None
  1027         
  1061         
  1787                         "program": ITEM_PROGRAM,
  1821                         "program": ITEM_PROGRAM,
  1788                         "functionBlock": ITEM_FUNCTIONBLOCK,
  1822                         "functionBlock": ITEM_FUNCTIONBLOCK,
  1789                     }.get(self.Controler.GetPouType(instance_type))
  1823                     }.get(self.Controler.GetPouType(instance_type))
  1790                     if pou_type is not None and instance_type in self.Controler.GetProjectPouNames(self.Debug):
  1824                     if pou_type is not None and instance_type in self.Controler.GetProjectPouNames(self.Debug):
  1791                         self.ParentWindow.OpenDebugViewer(pou_type, 
  1825                         self.ParentWindow.OpenDebugViewer(pou_type, 
  1792                             "%s.%s"%(self.InstancePath, self.SelectedElement.GetName()),
  1826                             "%s.%s"%(self.GetInstancePath(True), self.SelectedElement.GetName()),
  1793                             self.Controler.ComputePouName(instance_type))
  1827                             self.Controler.ComputePouName(instance_type))
  1794                 else:
  1828                 else:
  1795                     iec_path = self.GetElementIECPath(self.SelectedElement)
  1829                     iec_path = self.GetElementIECPath(self.SelectedElement)
  1796                     if iec_path is not None:
  1830                     if iec_path is not None:
  1797                         if isinstance(self.SelectedElement, Wire):
  1831                         if isinstance(self.SelectedElement, Wire):
  3214         if self.SelectedElement is not None and (self.SelectedElement.IsVisible() or printing):
  3248         if self.SelectedElement is not None and (self.SelectedElement.IsVisible() or printing):
  3215             self.SelectedElement.Draw(dc)
  3249             self.SelectedElement.Draw(dc)
  3216         
  3250         
  3217         if not printing:
  3251         if not printing:
  3218             if self.Debug:
  3252             if self.Debug:
  3219                 xstart, ystart = self.GetViewStart()
  3253                 is_action = self.TagName.split("::")[0] == "A"
  3220                 dc.DrawText(_("Debug: %s") % self.InstancePath, 2, 2)
  3254                 text = _("Debug: %s") % self.InstancePath
       
  3255                 if is_action and self.Value is not None:
       
  3256                     text += " ("
       
  3257                 dc.DrawText(text, 2, 2)
       
  3258                 if is_action and self.Value is not None:
       
  3259                     value_text = self.VALUE_TRANSLATION[self.Value]
       
  3260                     tw, th = dc.GetTextExtent(text)
       
  3261                     if self.Value:
       
  3262                         dc.SetTextForeground(wx.GREEN)
       
  3263                     dc.DrawText(value_text, tw + 2, 2)
       
  3264                     if self.Value:
       
  3265                         dc.SetTextForeground(wx.BLACK)
       
  3266                     vw, vh = dc.GetTextExtent(value_text)
       
  3267                     dc.DrawText(")", tw + vw + 4, 2)
  3221             if self.rubberBand.IsShown():
  3268             if self.rubberBand.IsShown():
  3222                 self.rubberBand.Draw(dc)
  3269                 self.rubberBand.Draw(dc)
  3223             dc.EndDrawing()
  3270             dc.EndDrawing()
  3224 
  3271 
  3225     def OnPaint(self, event):
  3272     def OnPaint(self, event):