GraphicViewer.py
author laurent
Tue, 24 May 2011 19:37:14 +0200
changeset 540 82fa901a2160
parent 504 f88e0ebd8fe4
child 586 9aa96a36cf33
permissions -rw-r--r--
Fix bug when displaying errors when generating textual program in standalone mode
#!/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
from graphics.GraphicCommons import DebugViewer


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, DebugViewer):

    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.ALIGN_CENTER_VERTICAL|wx.ALL)
        parent.AddWindow(self.CanvasRange, 0, border=5, flag=wx.ALL)
        parent.AddWindow(self.staticText2, 0, border=5, flag=wx.ALIGN_CENTER_VERTICAL|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.DefaultSize, style=0)
        
        self.CanvasRange = wx.ComboBox(id=ID_GRAPHICVIEWERCANVASRANGE,
              name='CanvasRange', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(100, 28), 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.DefaultSize, 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, producer, instancepath = ""):
        self._init_ctrls(parent)
        DebugViewer.__init__(self, producer, True, False)
        
        self.ParentWindow = window
        self.InstancePath = instancepath

        self.Datas = []
        self.CurrentValue = 0
        self.CurrentRange = 25
        
        self.AddDataConsumer(self.InstancePath.upper(), self)
    
    def __del__(self):
        DebugViewer.__del__(self)
        self.RemoveDataConsumer(self)
    
    def ResetView(self):
        self.Datas = []
        self.CurrentValue = 0
        self.RefreshView()
    
    def RefreshNewData(self):
        self.RefreshView(False)
        DebugViewer.RefreshNewData(self)
    
    def RefreshView(self, force=True):
        self.Freeze()
        if force or self.CurrentValue + self.CurrentRange == len(self.Datas) or self.CurrentValue + len(self.Datas) < self.CurrentRange:
            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()
        self.Thaw()
    
    def SetMode(self, mode):
        pass
    
    def GetTagName(self):
        return ""
    
    def GetInstancePath(self):
        return self.InstancePath
    
    def IsViewing(self, tagname):
        return self.InstancePath == tagname
    
    def ResetBuffer(self):
        pass
    
    def RefreshScaling(self, refresh=True):
        pass
    
    def SelectAll(self):
        pass
    
    def ClearErrors(self):
        pass
    
    def NewValue(self, tick, value, forced=False):
        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.NewDataAvailable()
    
    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.ResetView()
        event.Skip()

    def OnCurrentButton(self, event):
        self.CurrentValue = max(0, len(self.Datas) - self.CurrentRange)
        self.RefreshView()
        event.Skip()