author | laurent |
Tue, 24 May 2011 19:37:14 +0200 | |
changeset 540 | 82fa901a2160 |
parent 504 | f88e0ebd8fe4 |
child 586 | 9aa96a36cf33 |
permissions | -rw-r--r-- |
301 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import wx |
|
26 |
import wx.lib.plot as plot |
|
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
27 |
from graphics.GraphicCommons import DebugViewer |
301 | 28 |
|
29 |
||
30 |
colours = ['blue', 'red', 'green', 'yellow', 'orange', 'purple', 'brown', 'cyan', |
|
31 |
'pink', 'grey'] |
|
32 |
markers = ['circle', 'dot', 'square', 'triangle', 'triangle_down', 'cross', 'plus', 'circle'] |
|
33 |
||
34 |
||
35 |
#------------------------------------------------------------------------------- |
|
36 |
# Debug Variable Graphic Viewer class |
|
37 |
#------------------------------------------------------------------------------- |
|
38 |
||
39 |
||
40 |
RANGE_VALUES = [str(25 * 2 ** i) for i in xrange(6)] |
|
41 |
||
42 |
[ID_GRAPHICVIEWER, ID_GRAPHICVIEWERCANVAS, |
|
43 |
ID_GRAPHICVIEWERCANVASRANGE, ID_GRAPHICVIEWERCANVASPOSITION, |
|
44 |
ID_GRAPHICVIEWERRESETBUTTON, ID_GRAPHICVIEWERCURRENTBUTTON, |
|
45 |
ID_GRAPHICVIEWERSTATICTEXT1, ID_GRAPHICVIEWERSTATICTEXT2, |
|
46 |
] = [wx.NewId() for _init_ctrls in range(8)] |
|
47 |
||
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
48 |
class GraphicViewer(wx.Panel, DebugViewer): |
301 | 49 |
|
50 |
def _init_coll_MainGridSizer_Items(self, parent): |
|
51 |
# generated method, don't edit |
|
52 |
parent.AddWindow(self.Canvas, 0, border=0, flag=wx.GROW) |
|
53 |
parent.AddSizer(self.RangeSizer, 0, border=0, flag=wx.GROW) |
|
54 |
||
55 |
def _init_coll_MainGridSizer_Growables(self, parent): |
|
56 |
# generated method, don't edit |
|
57 |
parent.AddGrowableCol(0) |
|
58 |
parent.AddGrowableRow(0) |
|
59 |
||
60 |
def _init_coll_RangeSizer_Items(self, parent): |
|
61 |
# generated method, don't edit |
|
391 | 62 |
parent.AddWindow(self.staticbox1, 0, border=5, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALL) |
301 | 63 |
parent.AddWindow(self.CanvasRange, 0, border=5, flag=wx.ALL) |
391 | 64 |
parent.AddWindow(self.staticText2, 0, border=5, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALL) |
301 | 65 |
parent.AddWindow(self.CanvasPosition, 0, border=5, flag=wx.GROW|wx.ALL) |
66 |
parent.AddWindow(self.ResetButton, 0, border=5, flag=wx.ALL) |
|
67 |
parent.AddWindow(self.CurrentButton, 0, border=5, flag=wx.ALL) |
|
68 |
||
69 |
def _init_coll_RangeSizer_Growables(self, parent): |
|
70 |
# generated method, don't edit |
|
71 |
parent.AddGrowableCol(3) |
|
72 |
parent.AddGrowableRow(0) |
|
73 |
||
74 |
def _init_sizers(self): |
|
75 |
# generated method, don't edit |
|
76 |
self.MainGridSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) |
|
77 |
self.RangeSizer = wx.FlexGridSizer(cols=6, hgap=0, rows=1, vgap=0) |
|
78 |
||
79 |
self._init_coll_MainGridSizer_Items(self.MainGridSizer) |
|
80 |
self._init_coll_MainGridSizer_Growables(self.MainGridSizer) |
|
81 |
self._init_coll_RangeSizer_Items(self.RangeSizer) |
|
82 |
self._init_coll_RangeSizer_Growables(self.RangeSizer) |
|
83 |
||
84 |
self.SetSizer(self.MainGridSizer) |
|
85 |
||
86 |
def _init_ctrls(self, prnt): |
|
87 |
wx.Panel.__init__(self, prnt, ID_GRAPHICVIEWER, wx.DefaultPosition, |
|
88 |
wx.DefaultSize, 0) |
|
89 |
||
90 |
self.Canvas = plot.PlotCanvas(id=ID_GRAPHICVIEWERCANVAS, |
|
91 |
name='Canvas', parent=self, pos=wx.Point(0, 0), |
|
92 |
size=wx.Size(0, 0), style=0) |
|
93 |
def _axisInterval(spec, lower, upper): |
|
94 |
if spec == 'border': |
|
95 |
if lower == upper: |
|
96 |
return lower - 0.5, upper + 0.5 |
|
97 |
else: |
|
98 |
border = (upper - lower) * 0.05 |
|
99 |
return lower - border, upper + border |
|
100 |
else: |
|
101 |
return plot.PlotCanvas._axisInterval(self.Canvas, spec, lower, upper) |
|
102 |
self.Canvas._axisInterval = _axisInterval |
|
103 |
self.Canvas.SetYSpec('border') |
|
104 |
||
105 |
self.staticbox1 = wx.StaticText(id=ID_GRAPHICVIEWERSTATICTEXT1, |
|
391 | 106 |
label=_('Range:'), name='staticText1', parent=self, |
107 |
pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
301 | 108 |
|
109 |
self.CanvasRange = wx.ComboBox(id=ID_GRAPHICVIEWERCANVASRANGE, |
|
110 |
name='CanvasRange', parent=self, pos=wx.Point(0, 0), |
|
313 | 111 |
size=wx.Size(100, 28), choices=RANGE_VALUES, style=0) |
301 | 112 |
self.CanvasRange.SetStringSelection("25") |
113 |
self.Bind(wx.EVT_COMBOBOX, self.OnRangeChanged, id=ID_GRAPHICVIEWERCANVASRANGE) |
|
114 |
self.Bind(wx.EVT_TEXT_ENTER, self.OnRangeChanged, id=ID_GRAPHICVIEWERCANVASRANGE) |
|
115 |
||
116 |
self.staticText2 = wx.StaticText(id=ID_GRAPHICVIEWERSTATICTEXT2, |
|
391 | 117 |
label=_('Position:'), name='staticText2', parent=self, |
118 |
pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
301 | 119 |
|
120 |
self.CanvasPosition = wx.ScrollBar(id=ID_GRAPHICVIEWERCANVASPOSITION, |
|
121 |
name='Position', parent=self, pos=wx.Point(0, 0), |
|
122 |
size=wx.Size(0, 16), style=wx.SB_HORIZONTAL) |
|
123 |
self.CanvasPosition.SetScrollbar(0, 10, 100, 10) |
|
124 |
self.CanvasPosition.Bind(wx.EVT_SCROLL_THUMBTRACK, self.OnPositionChanging, |
|
125 |
id = ID_GRAPHICVIEWERCANVASPOSITION) |
|
126 |
self.CanvasPosition.Bind(wx.EVT_SCROLL_LINEUP, self.OnPositionChanging, |
|
127 |
id = ID_GRAPHICVIEWERCANVASPOSITION) |
|
128 |
self.CanvasPosition.Bind(wx.EVT_SCROLL_LINEDOWN, self.OnPositionChanging, |
|
129 |
id = ID_GRAPHICVIEWERCANVASPOSITION) |
|
130 |
self.CanvasPosition.Bind(wx.EVT_SCROLL_PAGEUP, self.OnPositionChanging, |
|
131 |
id = ID_GRAPHICVIEWERCANVASPOSITION) |
|
132 |
self.CanvasPosition.Bind(wx.EVT_SCROLL_PAGEDOWN, self.OnPositionChanging, |
|
133 |
id = ID_GRAPHICVIEWERCANVASPOSITION) |
|
134 |
||
135 |
self.ResetButton = wx.Button(id=ID_GRAPHICVIEWERRESETBUTTON, label='Reset', |
|
136 |
name='ResetButton', parent=self, pos=wx.Point(0, 0), |
|
137 |
size=wx.Size(72, 24), style=0) |
|
138 |
self.Bind(wx.EVT_BUTTON, self.OnResetButton, id=ID_GRAPHICVIEWERRESETBUTTON) |
|
139 |
||
140 |
self.CurrentButton = wx.Button(id=ID_GRAPHICVIEWERCURRENTBUTTON, label='Current', |
|
141 |
name='CurrentButton', parent=self, pos=wx.Point(0, 0), |
|
142 |
size=wx.Size(72, 24), style=0) |
|
143 |
self.Bind(wx.EVT_BUTTON, self.OnCurrentButton, id=ID_GRAPHICVIEWERCURRENTBUTTON) |
|
144 |
||
145 |
self._init_sizers() |
|
146 |
||
415
d3d8f8f0b678
controler (PLCControler) and debug data producer (PluginsRoot) are no longer the same thing
b.taylor@willowglen.ca
parents:
374
diff
changeset
|
147 |
def __init__(self, parent, window, producer, instancepath = ""): |
301 | 148 |
self._init_ctrls(parent) |
415
d3d8f8f0b678
controler (PLCControler) and debug data producer (PluginsRoot) are no longer the same thing
b.taylor@willowglen.ca
parents:
374
diff
changeset
|
149 |
DebugViewer.__init__(self, producer, True, False) |
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
150 |
|
301 | 151 |
self.ParentWindow = window |
152 |
self.InstancePath = instancepath |
|
153 |
||
154 |
self.Datas = [] |
|
155 |
self.CurrentValue = 0 |
|
156 |
self.CurrentRange = 25 |
|
157 |
||
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
158 |
self.AddDataConsumer(self.InstancePath.upper(), self) |
301 | 159 |
|
338 | 160 |
def __del__(self): |
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
161 |
DebugViewer.__del__(self) |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
162 |
self.RemoveDataConsumer(self) |
338 | 163 |
|
164 |
def ResetView(self): |
|
165 |
self.Datas = [] |
|
166 |
self.CurrentValue = 0 |
|
167 |
self.RefreshView() |
|
168 |
||
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
169 |
def RefreshNewData(self): |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
170 |
self.RefreshView(False) |
374 | 171 |
DebugViewer.RefreshNewData(self) |
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
172 |
|
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
173 |
def RefreshView(self, force=True): |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
174 |
self.Freeze() |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
175 |
if force or self.CurrentValue + self.CurrentRange == len(self.Datas) or self.CurrentValue + len(self.Datas) < self.CurrentRange: |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
176 |
var_name = self.InstancePath.split(".")[-1] |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
177 |
|
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
178 |
self.VariableGraphic = plot.PolyLine(self.Datas[self.CurrentValue:self.CurrentValue + self.CurrentRange], |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
179 |
legend=var_name, colour=colours[0]) |
391 | 180 |
self.GraphicsObject = plot.PlotGraphics([self.VariableGraphic], _("%s Graphics") % var_name, _("Tick"), _("Values")) |
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
181 |
datas_length = len(self.Datas) |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
182 |
if datas_length > 1: |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
183 |
start = self.Datas[self.CurrentValue][0] |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
184 |
if self.CurrentValue + self.CurrentRange > datas_length: |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
185 |
end = start + (self.Datas[datas_length - 1][0] - start) * self.CurrentRange / (datas_length - self.CurrentValue - 1) |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
186 |
else: |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
187 |
end = self.Datas[self.CurrentValue + self.CurrentRange - 1][0] |
301 | 188 |
else: |
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
189 |
start = 0. |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
190 |
end = 25. |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
191 |
self.Canvas.Draw(self.GraphicsObject, xAxis=(start, end)) |
301 | 192 |
self.RefreshScrollBar() |
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
193 |
self.Thaw() |
326 | 194 |
|
195 |
def SetMode(self, mode): |
|
196 |
pass |
|
197 |
||
301 | 198 |
def GetTagName(self): |
199 |
return "" |
|
200 |
||
201 |
def GetInstancePath(self): |
|
202 |
return self.InstancePath |
|
203 |
||
326 | 204 |
def IsViewing(self, tagname): |
205 |
return self.InstancePath == tagname |
|
206 |
||
207 |
def ResetBuffer(self): |
|
208 |
pass |
|
209 |
||
210 |
def RefreshScaling(self, refresh=True): |
|
211 |
pass |
|
212 |
||
331
9106d66bd204
Bug with Scaling, MiddleButton, Wire modifications fixed.
lbessard
parents:
326
diff
changeset
|
213 |
def SelectAll(self): |
9106d66bd204
Bug with Scaling, MiddleButton, Wire modifications fixed.
lbessard
parents:
326
diff
changeset
|
214 |
pass |
9106d66bd204
Bug with Scaling, MiddleButton, Wire modifications fixed.
lbessard
parents:
326
diff
changeset
|
215 |
|
411 | 216 |
def ClearErrors(self): |
217 |
pass |
|
218 |
||
504 | 219 |
def NewValue(self, tick, value, forced=False): |
361
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
220 |
self.Datas.append((float(tick), {True:1., False:0.}.get(value, float(value)))) |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
221 |
if self.CurrentValue + self.CurrentRange == len(self.Datas) - 1: |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
222 |
self.CurrentValue += 1 |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
223 |
self.NewDataAvailable() |
62570186dad4
Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents:
344
diff
changeset
|
224 |
|
301 | 225 |
def RefreshScrollBar(self): |
226 |
self.CanvasPosition.SetScrollbar(self.CurrentValue, self.CurrentRange, len(self.Datas), self.CurrentRange) |
|
227 |
||
228 |
def OnRangeChanged(self, event): |
|
229 |
old_range = self.CurrentRange |
|
230 |
try: |
|
231 |
self.CurrentRange = int(self.CanvasRange.GetValue()) |
|
232 |
except ValueError, e: |
|
233 |
self.CanvasRange.SetValue(str(self.CurrentRange)) |
|
234 |
self.CurrentValue = max(0, min(self.CurrentValue + old_range - self.CurrentRange, |
|
235 |
len(self.Datas) - self.CurrentRange)) |
|
236 |
self.RefreshView() |
|
237 |
event.Skip() |
|
238 |
||
239 |
def OnPositionChanging(self, event): |
|
240 |
self.CurrentValue = event.GetPosition() |
|
241 |
self.RefreshView() |
|
242 |
event.Skip() |
|
243 |
||
244 |
def OnResetButton(self, event): |
|
338 | 245 |
self.ResetView() |
301 | 246 |
event.Skip() |
247 |
||
248 |
def OnCurrentButton(self, event): |
|
249 |
self.CurrentValue = max(0, len(self.Datas) - self.CurrentRange) |
|
250 |
self.RefreshView() |
|
251 |
event.Skip() |
|
252 |