Adding support for Graphic for variable in Debug mode
authorlbessard
Wed, 14 Jan 2009 19:45:22 +0100
changeset 301 b5e564608b9e
parent 300 34d1402c0e24
child 302 43e10dccc16a
Adding support for Graphic for variable in Debug mode
GraphicViewer.py
PLCOpenEditor.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GraphicViewer.py	Wed Jan 14 19:45:22 2009 +0100
@@ -0,0 +1,222 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
+#based on the plcopen standard. 
+#
+#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
+#
+#See COPYING file for copyrights details.
+#
+#This library is free software; you can redistribute it and/or
+#modify it under the terms of the GNU General Public
+#License as published by the Free Software Foundation; either
+#version 2.1 of the License, or (at your option) any later version.
+#
+#This library is distributed in the hope that it will be useful,
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+#General Public License for more details.
+#
+#You should have received a copy of the GNU General Public
+#License along with this library; if not, write to the Free Software
+#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+import wx
+import wx.lib.plot as plot
+
+
+colours = ['blue', 'red', 'green', 'yellow', 'orange', 'purple', 'brown', 'cyan',
+           'pink', 'grey']
+markers = ['circle', 'dot', 'square', 'triangle', 'triangle_down', 'cross', 'plus', 'circle']
+
+
+#-------------------------------------------------------------------------------
+#                       Debug Variable Graphic Viewer class
+#-------------------------------------------------------------------------------
+
+
+RANGE_VALUES = [str(25 * 2 ** i) for i in xrange(6)]
+
+[ID_GRAPHICVIEWER, ID_GRAPHICVIEWERCANVAS,
+ ID_GRAPHICVIEWERCANVASRANGE, ID_GRAPHICVIEWERCANVASPOSITION,
+ ID_GRAPHICVIEWERRESETBUTTON, ID_GRAPHICVIEWERCURRENTBUTTON, 
+ ID_GRAPHICVIEWERSTATICTEXT1, ID_GRAPHICVIEWERSTATICTEXT2,
+] = [wx.NewId() for _init_ctrls in range(8)]
+
+class GraphicViewer(wx.Panel):
+
+    def _init_coll_MainGridSizer_Items(self, parent):
+        # generated method, don't edit
+        parent.AddWindow(self.Canvas, 0, border=0, flag=wx.GROW)
+        parent.AddSizer(self.RangeSizer, 0, border=0, flag=wx.GROW)
+    
+    def _init_coll_MainGridSizer_Growables(self, parent):
+        # generated method, don't edit
+        parent.AddGrowableCol(0)
+        parent.AddGrowableRow(0)
+
+    def _init_coll_RangeSizer_Items(self, parent):
+        # generated method, don't edit
+        parent.AddWindow(self.staticbox1, 0, border=5, flag=wx.ALL)
+        parent.AddWindow(self.CanvasRange, 0, border=5, flag=wx.ALL)
+        parent.AddWindow(self.staticText2, 0, border=5, flag=wx.ALL)
+        parent.AddWindow(self.CanvasPosition, 0, border=5, flag=wx.GROW|wx.ALL)
+        parent.AddWindow(self.ResetButton, 0, border=5, flag=wx.ALL)
+        parent.AddWindow(self.CurrentButton, 0, border=5, flag=wx.ALL)
+        
+    def _init_coll_RangeSizer_Growables(self, parent):
+        # generated method, don't edit
+        parent.AddGrowableCol(3)
+        parent.AddGrowableRow(0)
+        
+    def _init_sizers(self):
+        # generated method, don't edit
+        self.MainGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
+        self.RangeSizer = wx.FlexGridSizer(cols=6, hgap=0, rows=1, vgap=0)
+
+        self._init_coll_MainGridSizer_Items(self.MainGridSizer)
+        self._init_coll_MainGridSizer_Growables(self.MainGridSizer)
+        self._init_coll_RangeSizer_Items(self.RangeSizer)
+        self._init_coll_RangeSizer_Growables(self.RangeSizer)
+        
+        self.SetSizer(self.MainGridSizer)
+    
+    def _init_ctrls(self, prnt):
+        wx.Panel.__init__(self, prnt, ID_GRAPHICVIEWER, wx.DefaultPosition, 
+                 wx.DefaultSize, 0)
+        
+        self.Canvas = plot.PlotCanvas(id=ID_GRAPHICVIEWERCANVAS, 
+              name='Canvas', parent=self, pos=wx.Point(0, 0),
+              size=wx.Size(0, 0), style=0)
+        def _axisInterval(spec, lower, upper):
+            if spec == 'border':
+                if lower == upper:
+                    return lower - 0.5, upper + 0.5
+                else:
+                    border = (upper - lower) * 0.05
+                    return lower - border, upper + border
+            else:
+                return plot.PlotCanvas._axisInterval(self.Canvas, spec, lower, upper)
+        self.Canvas._axisInterval = _axisInterval
+        self.Canvas.SetYSpec('border')
+
+        self.staticbox1 = wx.StaticText(id=ID_GRAPHICVIEWERSTATICTEXT1,
+              label='Range:', name='staticText1', parent=self,
+              pos=wx.Point(0, 0), size=wx.Size(45, 17), style=0)
+        
+        self.CanvasRange = wx.ComboBox(id=ID_GRAPHICVIEWERCANVASRANGE,
+              name='CanvasRange', parent=self, pos=wx.Point(0, 0),
+              size=wx.Size(100, 24), choices=RANGE_VALUES, style=0)
+        self.CanvasRange.SetStringSelection("25")
+        self.Bind(wx.EVT_COMBOBOX, self.OnRangeChanged, id=ID_GRAPHICVIEWERCANVASRANGE)
+        self.Bind(wx.EVT_TEXT_ENTER, self.OnRangeChanged, id=ID_GRAPHICVIEWERCANVASRANGE)
+        
+        self.staticText2 = wx.StaticText(id=ID_GRAPHICVIEWERSTATICTEXT2,
+              label='Position:', name='staticText2', parent=self,
+              pos=wx.Point(0, 0), size=wx.Size(60, 17), style=0)
+
+        self.CanvasPosition = wx.ScrollBar(id=ID_GRAPHICVIEWERCANVASPOSITION,
+              name='Position', parent=self, pos=wx.Point(0, 0),
+              size=wx.Size(0, 16), style=wx.SB_HORIZONTAL)
+        self.CanvasPosition.SetScrollbar(0, 10, 100, 10)
+        self.CanvasPosition.Bind(wx.EVT_SCROLL_THUMBTRACK, self.OnPositionChanging, 
+              id = ID_GRAPHICVIEWERCANVASPOSITION)
+        self.CanvasPosition.Bind(wx.EVT_SCROLL_LINEUP, self.OnPositionChanging, 
+              id = ID_GRAPHICVIEWERCANVASPOSITION)
+        self.CanvasPosition.Bind(wx.EVT_SCROLL_LINEDOWN, self.OnPositionChanging, 
+              id = ID_GRAPHICVIEWERCANVASPOSITION)
+        self.CanvasPosition.Bind(wx.EVT_SCROLL_PAGEUP, self.OnPositionChanging, 
+              id = ID_GRAPHICVIEWERCANVASPOSITION)
+        self.CanvasPosition.Bind(wx.EVT_SCROLL_PAGEDOWN, self.OnPositionChanging, 
+              id = ID_GRAPHICVIEWERCANVASPOSITION)
+        
+        self.ResetButton = wx.Button(id=ID_GRAPHICVIEWERRESETBUTTON, label='Reset',
+              name='ResetButton', parent=self, pos=wx.Point(0, 0),
+              size=wx.Size(72, 24), style=0)
+        self.Bind(wx.EVT_BUTTON, self.OnResetButton, id=ID_GRAPHICVIEWERRESETBUTTON)
+        
+        self.CurrentButton = wx.Button(id=ID_GRAPHICVIEWERCURRENTBUTTON, label='Current',
+              name='CurrentButton', parent=self, pos=wx.Point(0, 0),
+              size=wx.Size(72, 24), style=0)
+        self.Bind(wx.EVT_BUTTON, self.OnCurrentButton, id=ID_GRAPHICVIEWERCURRENTBUTTON)
+        
+        self._init_sizers()
+
+    def __init__(self, parent, window, controler, instancepath = ""):
+        self._init_ctrls(parent)
+
+        self.ParentWindow = window
+        self.Controler = controler
+        self.InstancePath = instancepath
+
+        self.Datas = []
+        self.CurrentValue = 0
+        self.CurrentRange = 25
+        
+        self.Controler.SubscribeDebugIECVariable(self.InstancePath.upper(), self)
+    
+    def RefreshView(self):
+        var_name = self.InstancePath.split(".")[-1]
+        
+        self.VariableGraphic = plot.PolyLine(self.Datas[self.CurrentValue:self.CurrentValue + self.CurrentRange], 
+                                             legend=var_name, colour=colours[0])
+        self.GraphicsObject = plot.PlotGraphics([self.VariableGraphic], "%s Graphics" % var_name, "Tick", "Values")
+        datas_length = len(self.Datas)
+        if datas_length > 1:
+            start = self.Datas[self.CurrentValue][0]
+            if self.CurrentValue + self.CurrentRange > datas_length:
+                end = start + (self.Datas[datas_length - 1][0] - start) * self.CurrentRange / (datas_length - self.CurrentValue - 1)
+            else:
+                end = self.Datas[self.CurrentValue + self.CurrentRange - 1][0]
+        else:
+            start = 0.
+            end = 25.
+        self.Canvas.Draw(self.GraphicsObject, xAxis=(start, end))
+        
+        self.RefreshScrollBar()
+        
+    def GetTagName(self):
+        return ""
+    
+    def GetInstancePath(self):
+        return self.InstancePath
+    
+    def AddPoint(self, tick, value):
+        self.Datas.append((float(tick), {True:1., False:0.}.get(value, float(value))))
+        if self.CurrentValue + self.CurrentRange == len(self.Datas) - 1:
+            self.CurrentValue += 1
+            self.RefreshView()
+        elif len(self.Datas) < self.CurrentValue + self.CurrentRange:
+            self.RefreshView()
+            
+    def RefreshScrollBar(self):
+        self.CanvasPosition.SetScrollbar(self.CurrentValue, self.CurrentRange, len(self.Datas), self.CurrentRange)
+
+    def OnRangeChanged(self, event):
+        old_range = self.CurrentRange
+        try:
+            self.CurrentRange = int(self.CanvasRange.GetValue())
+        except ValueError, e:
+            self.CanvasRange.SetValue(str(self.CurrentRange))
+        self.CurrentValue = max(0, min(self.CurrentValue + old_range - self.CurrentRange, 
+                                       len(self.Datas) - self.CurrentRange))
+        self.RefreshView()
+        event.Skip()
+    
+    def OnPositionChanging(self, event):
+        self.CurrentValue = event.GetPosition()
+        self.RefreshView()
+        event.Skip()
+
+    def OnResetButton(self, event):
+        self.Datas = []
+        self.CurrentValue = 0
+        self.RefreshView()
+        event.Skip()
+
+    def OnCurrentButton(self, event):
+        self.CurrentValue = max(0, len(self.Datas) - self.CurrentRange)
+        self.RefreshView()
+        event.Skip()
+
--- a/PLCOpenEditor.py	Tue Jan 13 17:26:37 2009 +0100
+++ b/PLCOpenEditor.py	Wed Jan 14 19:45:22 2009 +0100
@@ -35,6 +35,7 @@
 from LDViewer import *
 from Viewer import *
 from TextViewer import *
+from GraphicViewer import *
 from RessourceEditor import *
 from DataTypeEditor import *
 from PLCControler import *
@@ -411,6 +412,10 @@
                   pos=wx.Point(0, 0), size=wx.Size(0, 0),
                   style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER)
         if self.Debug:
+            if wx.VERSION >= (2, 6, 0):
+                self.InstancesTree.Bind(wx.EVT_RIGHT_UP, self.OnInstancesTreeRightUp)
+            else:
+                wx.EVT_RIGHT_UP(self.InstancesTree, self.OnInstancesTreeRightUp)
             self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnInstancesTreeBeginDrag,
                   id=ID_PLCOPENEDITORINSTANCESTREE)
             self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnInstancesTreeItemActivated,
@@ -1820,7 +1825,59 @@
                 new_window.RefreshView()
                 new_window.SetFocus()
                 self.RefreshPageTitles()
-        event.Skip()
+        if selected_item is not None and selected_infos[0] in ITEMS_VARIABLE:
+            var_path, var_type = self.InstancesTree.GetItemText(selected_item).split(" (")
+            var_type = var_type.split(")")[0]
+            
+            if self.Controler.IsOfType(var_type, "ANY_NUM", self.Debug) or\
+               self.Controler.IsOfType(var_type, "ANY_BIT", self.Debug):
+                parent_item = self.InstancesTree.GetItemParent(selected_item)
+                while self.InstancesTree.GetPyData(parent_item)[0] != ITEM_PROJECT:
+                    parent_name = self.InstancesTree.GetItemText(parent_item).split(" (")[0]
+                    var_path = "%s.%s"%(parent_name, var_path)
+                    parent_item = self.InstancesTree.GetItemParent(parent_item)
+                
+                new_window = GraphicViewer(self.TabsOpened, self, self.Controler, var_path)
+                self.TabsOpened.AddPage(new_window, "")
+                new_window.SetFocus()
+                self.RefreshPageTitles()
+        event.Skip()
+
+    def OnInstancesTreeRightUp(self, event):
+        if wx.Platform == '__WXMSW__':
+            selected_item = event.GetItem()
+        else:
+            selected_item = self.InstancesTree.GetSelection()
+        selected_infos = self.InstancesTree.GetPyData(selected_item)
+        if selected_item is not None and selected_infos[0] in ITEMS_VARIABLE:
+            var_path, var_type = self.InstancesTree.GetItemText(selected_item).split(" (")
+            var_type = var_type.split(")")[0]
+            
+            if self.Controler.IsOfType(var_type, "ANY_NUM", self.Debug) or\
+               self.Controler.IsOfType(var_type, "ANY_BIT", self.Debug):
+                parent_item = self.InstancesTree.GetItemParent(selected_item)
+                while self.InstancesTree.GetPyData(parent_item)[0] != ITEM_PROJECT:
+                    parent_name = self.InstancesTree.GetItemText(parent_item).split(" (")[0]
+                    var_path = "%s.%s"%(parent_name, var_path)
+                    parent_item = self.InstancesTree.GetItemParent(parent_item)
+                
+                menu = wx.Menu(title='')
+                new_id = wx.NewId()
+                AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text="Graphic Panel")
+                self.Bind(wx.EVT_MENU, self.AddVariableGraphicFunction(var_path), id=new_id)
+                new_id = wx.NewId()
+                AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text="CSV Log")
+                self.PopupMenu(menu)
+        event.Skip()
+
+    def AddVariableGraphicFunction(self, iec_path):
+        def AddVariableGraphic(event):
+            new_window = GraphicViewer(self.TabsOpened, self, self.Controler, iec_path)
+            self.TabsOpened.AddPage(new_window, "")
+            new_window.SetFocus()
+            self.RefreshPageTitles()
+            event.Skip()
+        return AddVariableGraphic
 
     def SelectInstancesTreeItem(self, root, instancepath):
         found = False