author | Laurent Bessard |
Tue, 13 Nov 2012 11:36:04 +0100 | |
changeset 880 | 781ced3c0aef |
parent 878 | 37256069baed |
child 887 | d3c6c4ab8b28 |
permissions | -rw-r--r-- |
814 | 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) 2012: 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 |
from types import TupleType |
|
26 |
||
27 |
import wx |
|
28 |
import wx.lib.buttons |
|
29 |
||
30 |
from graphics import DebugDataConsumer, DebugViewer |
|
31 |
from controls import CustomGrid, CustomTable |
|
32 |
from dialogs.ForceVariableDialog import ForceVariableDialog |
|
33 |
from util.BitmapLibrary import GetBitmap |
|
34 |
||
35 |
def AppendMenu(parent, help, id, kind, text): |
|
36 |
parent.Append(help=help, id=id, kind=kind, text=text) |
|
37 |
||
38 |
def GetDebugVariablesTableColnames(): |
|
39 |
_ = lambda x : x |
|
40 |
return [_("Variable"), _("Value")] |
|
41 |
||
42 |
class VariableTableItem(DebugDataConsumer): |
|
43 |
||
44 |
def __init__(self, parent, variable, value): |
|
45 |
DebugDataConsumer.__init__(self) |
|
46 |
self.Parent = parent |
|
47 |
self.Variable = variable |
|
48 |
self.Value = value |
|
49 |
||
50 |
def __del__(self): |
|
51 |
self.Parent = None |
|
52 |
||
53 |
def SetVariable(self, variable): |
|
54 |
if self.Parent and self.Variable != variable: |
|
55 |
self.Variable = variable |
|
56 |
self.Parent.RefreshGrid() |
|
57 |
||
58 |
def GetVariable(self): |
|
59 |
return self.Variable |
|
60 |
||
61 |
def SetForced(self, forced): |
|
62 |
if self.Forced != forced: |
|
63 |
self.Forced = forced |
|
64 |
self.Parent.HasNewData = True |
|
65 |
||
66 |
def SetValue(self, value): |
|
67 |
if self.Value != value: |
|
68 |
self.Value = value |
|
69 |
self.Parent.HasNewData = True |
|
70 |
||
71 |
def GetValue(self): |
|
878
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
814
diff
changeset
|
72 |
variable_type = self.Parent.GetDataType(self.Variable.upper()) |
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
814
diff
changeset
|
73 |
if variable_type == "STRING": |
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
814
diff
changeset
|
74 |
return "'%s'" % self.Value |
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
814
diff
changeset
|
75 |
elif variable_type == "WSTRING": |
37256069baed
Fix display of string variables value in debug
Laurent Bessard
parents:
814
diff
changeset
|
76 |
return "\"%s\"" % self.Value |
814 | 77 |
return self.Value |
78 |
||
79 |
class DebugVariableTable(CustomTable): |
|
80 |
||
81 |
def GetValue(self, row, col): |
|
82 |
if row < self.GetNumberRows(): |
|
83 |
return self.GetValueByName(row, self.GetColLabelValue(col, False)) |
|
84 |
return "" |
|
85 |
||
86 |
def SetValue(self, row, col, value): |
|
87 |
if col < len(self.colnames): |
|
88 |
self.SetValueByName(row, self.GetColLabelValue(col, False), value) |
|
89 |
||
90 |
def GetValueByName(self, row, colname): |
|
91 |
if row < self.GetNumberRows(): |
|
92 |
if colname == "Variable": |
|
93 |
return self.data[row].GetVariable() |
|
94 |
elif colname == "Value": |
|
95 |
return self.data[row].GetValue() |
|
96 |
return "" |
|
97 |
||
98 |
def SetValueByName(self, row, colname, value): |
|
99 |
if row < self.GetNumberRows(): |
|
100 |
if colname == "Variable": |
|
101 |
self.data[row].SetVariable(value) |
|
102 |
elif colname == "Value": |
|
103 |
self.data[row].SetValue(value) |
|
104 |
||
105 |
def IsForced(self, row): |
|
106 |
if row < self.GetNumberRows(): |
|
107 |
return self.data[row].IsForced() |
|
108 |
return False |
|
109 |
||
110 |
def _updateColAttrs(self, grid): |
|
111 |
""" |
|
112 |
wx.grid.Grid -> update the column attributes to add the |
|
113 |
appropriate renderer given the column name. |
|
114 |
||
115 |
Otherwise default to the default renderer. |
|
116 |
""" |
|
117 |
||
118 |
for row in range(self.GetNumberRows()): |
|
119 |
for col in range(self.GetNumberCols()): |
|
120 |
if self.GetColLabelValue(col, False) == "Value": |
|
121 |
if self.IsForced(row): |
|
122 |
grid.SetCellTextColour(row, col, wx.BLUE) |
|
123 |
else: |
|
124 |
grid.SetCellTextColour(row, col, wx.BLACK) |
|
125 |
grid.SetReadOnly(row, col, True) |
|
126 |
self.ResizeRow(grid, row) |
|
127 |
||
128 |
def AppendItem(self, data): |
|
129 |
self.data.append(data) |
|
130 |
||
131 |
def InsertItem(self, idx, data): |
|
132 |
self.data.insert(idx, data) |
|
133 |
||
134 |
def RemoveItem(self, idx): |
|
135 |
self.data.pop(idx) |
|
136 |
||
137 |
def MoveItem(self, idx, new_idx): |
|
138 |
self.data.insert(new_idx, self.data.pop(idx)) |
|
139 |
||
140 |
def GetItem(self, idx): |
|
141 |
return self.data[idx] |
|
142 |
||
143 |
class DebugVariableDropTarget(wx.TextDropTarget): |
|
144 |
||
145 |
def __init__(self, parent): |
|
146 |
wx.TextDropTarget.__init__(self) |
|
147 |
self.ParentWindow = parent |
|
148 |
||
149 |
def OnDropText(self, x, y, data): |
|
150 |
x, y = self.ParentWindow.VariablesGrid.CalcUnscrolledPosition(x, y) |
|
151 |
row = self.ParentWindow.VariablesGrid.YToRow(y - self.ParentWindow.VariablesGrid.GetColLabelSize()) |
|
152 |
if row == wx.NOT_FOUND: |
|
153 |
row = self.ParentWindow.Table.GetNumberRows() |
|
154 |
message = None |
|
155 |
try: |
|
156 |
values = eval(data) |
|
157 |
except: |
|
158 |
message = _("Invalid value \"%s\" for debug variable")%data |
|
159 |
values = None |
|
160 |
if not isinstance(values, TupleType): |
|
161 |
message = _("Invalid value \"%s\" for debug variable")%data |
|
162 |
values = None |
|
163 |
if values is not None and values[1] == "debug": |
|
164 |
self.ParentWindow.InsertValue(values[0], row) |
|
165 |
if message is not None: |
|
166 |
wx.CallAfter(self.ShowMessage, message) |
|
167 |
||
168 |
def ShowMessage(self, message): |
|
169 |
dialog = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK|wx.ICON_ERROR) |
|
170 |
dialog.ShowModal() |
|
171 |
dialog.Destroy() |
|
172 |
||
173 |
class DebugVariablePanel(wx.Panel, DebugViewer): |
|
174 |
||
175 |
def __init__(self, parent, producer): |
|
176 |
wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL) |
|
177 |
DebugViewer.__init__(self, producer, True) |
|
178 |
||
179 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) |
|
180 |
main_sizer.AddGrowableCol(0) |
|
181 |
main_sizer.AddGrowableRow(1) |
|
182 |
||
183 |
button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
|
184 |
main_sizer.AddSizer(button_sizer, border=5, |
|
185 |
flag=wx.ALIGN_RIGHT|wx.ALL) |
|
186 |
||
187 |
for name, bitmap, help in [ |
|
188 |
("DeleteButton", "remove_element", _("Remove debug variable")), |
|
189 |
("UpButton", "up", _("Move debug variable up")), |
|
190 |
("DownButton", "down", _("Move debug variable down"))]: |
|
191 |
button = wx.lib.buttons.GenBitmapButton(self, bitmap=GetBitmap(bitmap), |
|
192 |
size=wx.Size(28, 28), style=wx.NO_BORDER) |
|
193 |
button.SetToolTipString(help) |
|
194 |
setattr(self, name, button) |
|
195 |
button_sizer.AddWindow(button, border=5, flag=wx.LEFT) |
|
196 |
||
197 |
self.VariablesGrid = CustomGrid(self, size=wx.Size(0, 150), style=wx.VSCROLL) |
|
198 |
self.VariablesGrid.SetDropTarget(DebugVariableDropTarget(self)) |
|
199 |
self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK, |
|
200 |
self.OnVariablesGridCellRightClick) |
|
201 |
main_sizer.AddWindow(self.VariablesGrid, flag=wx.GROW) |
|
202 |
||
203 |
self.SetSizer(main_sizer) |
|
204 |
||
205 |
self.HasNewData = False |
|
206 |
||
207 |
self.Table = DebugVariableTable(self, [], GetDebugVariablesTableColnames()) |
|
208 |
self.VariablesGrid.SetTable(self.Table) |
|
209 |
self.VariablesGrid.SetButtons({"Delete": self.DeleteButton, |
|
210 |
"Up": self.UpButton, |
|
211 |
"Down": self.DownButton}) |
|
212 |
||
213 |
def _AddVariable(new_row): |
|
214 |
return self.VariablesGrid.GetGridCursorRow() |
|
215 |
setattr(self.VariablesGrid, "_AddRow", _AddVariable) |
|
216 |
||
217 |
def _DeleteVariable(row): |
|
218 |
item = self.Table.GetItem(row) |
|
219 |
self.RemoveDataConsumer(item) |
|
220 |
self.Table.RemoveItem(row) |
|
221 |
self.RefreshGrid() |
|
222 |
setattr(self.VariablesGrid, "_DeleteRow", _DeleteVariable) |
|
223 |
||
224 |
def _MoveVariable(row, move): |
|
225 |
new_row = max(0, min(row + move, self.Table.GetNumberRows() - 1)) |
|
226 |
if new_row != row: |
|
227 |
self.Table.MoveItem(row, new_row) |
|
228 |
self.RefreshGrid() |
|
229 |
return new_row |
|
230 |
setattr(self.VariablesGrid, "_MoveRow", _MoveVariable) |
|
231 |
||
232 |
self.VariablesGrid.SetRowLabelSize(0) |
|
233 |
||
234 |
for col in range(self.Table.GetNumberCols()): |
|
235 |
attr = wx.grid.GridCellAttr() |
|
236 |
attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTER) |
|
237 |
self.VariablesGrid.SetColAttr(col, attr) |
|
238 |
self.VariablesGrid.SetColSize(col, 100) |
|
239 |
||
240 |
self.Table.ResetView(self.VariablesGrid) |
|
241 |
self.VariablesGrid.RefreshButtons() |
|
242 |
||
243 |
def RefreshNewData(self): |
|
244 |
if self.HasNewData: |
|
245 |
self.HasNewData = False |
|
246 |
self.RefreshGrid() |
|
247 |
DebugViewer.RefreshNewData(self) |
|
248 |
||
249 |
def RefreshGrid(self): |
|
250 |
self.Freeze() |
|
251 |
self.Table.ResetView(self.VariablesGrid) |
|
252 |
self.VariablesGrid.RefreshButtons() |
|
253 |
self.Thaw() |
|
254 |
||
255 |
def UnregisterObsoleteData(self): |
|
256 |
items = [(idx, item) for idx, item in enumerate(self.Table.GetData())] |
|
257 |
items.reverse() |
|
258 |
for idx, item in items: |
|
259 |
iec_path = item.GetVariable().upper() |
|
260 |
if self.GetDataType(iec_path) is None: |
|
261 |
self.RemoveDataConsumer(item) |
|
262 |
self.Table.RemoveItem(idx) |
|
263 |
else: |
|
264 |
self.AddDataConsumer(iec_path, item) |
|
265 |
self.Freeze() |
|
266 |
self.Table.ResetView(self.VariablesGrid) |
|
267 |
self.VariablesGrid.RefreshButtons() |
|
268 |
self.Thaw() |
|
269 |
||
270 |
def ResetGrid(self): |
|
271 |
self.DeleteDataConsumers() |
|
272 |
self.Table.Empty() |
|
273 |
self.Freeze() |
|
274 |
self.Table.ResetView(self.VariablesGrid) |
|
275 |
self.VariablesGrid.RefreshButtons() |
|
276 |
self.Thaw() |
|
277 |
||
278 |
def GetForceVariableMenuFunction(self, iec_path, item): |
|
279 |
iec_type = self.GetDataType(iec_path) |
|
280 |
def ForceVariableFunction(event): |
|
281 |
if iec_type is not None: |
|
282 |
dialog = ForceVariableDialog(self, iec_type, str(item.GetValue())) |
|
283 |
if dialog.ShowModal() == wx.ID_OK: |
|
284 |
self.ForceDataValue(iec_path, dialog.GetValue()) |
|
285 |
return ForceVariableFunction |
|
286 |
||
287 |
def GetReleaseVariableMenuFunction(self, iec_path): |
|
288 |
def ReleaseVariableFunction(event): |
|
289 |
self.ReleaseDataValue(iec_path) |
|
290 |
return ReleaseVariableFunction |
|
291 |
||
292 |
def OnVariablesGridCellRightClick(self, event): |
|
293 |
row, col = event.GetRow(), event.GetCol() |
|
294 |
if self.Table.GetColLabelValue(col, False) == "Value": |
|
295 |
iec_path = self.Table.GetValueByName(row, "Variable").upper() |
|
296 |
||
297 |
menu = wx.Menu(title='') |
|
298 |
||
299 |
new_id = wx.NewId() |
|
300 |
AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Force value")) |
|
301 |
self.Bind(wx.EVT_MENU, self.GetForceVariableMenuFunction(iec_path.upper(), self.Table.GetItem(row)), id=new_id) |
|
302 |
||
303 |
new_id = wx.NewId() |
|
304 |
AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Release value")) |
|
305 |
self.Bind(wx.EVT_MENU, self.GetReleaseVariableMenuFunction(iec_path.upper()), id=new_id) |
|
306 |
||
307 |
if self.Table.IsForced(row): |
|
308 |
menu.Enable(new_id, True) |
|
309 |
else: |
|
310 |
menu.Enable(new_id, False) |
|
311 |
||
312 |
self.PopupMenu(menu) |
|
313 |
||
314 |
menu.Destroy() |
|
315 |
event.Skip() |
|
316 |
||
317 |
def InsertValue(self, iec_path, idx = None, force=False): |
|
318 |
if idx is None: |
|
319 |
idx = self.Table.GetNumberRows() |
|
320 |
for item in self.Table.GetData(): |
|
321 |
if iec_path == item.GetVariable(): |
|
322 |
return |
|
323 |
item = VariableTableItem(self, iec_path, "") |
|
324 |
result = self.AddDataConsumer(iec_path.upper(), item) |
|
325 |
if result is not None or force: |
|
326 |
self.Table.InsertItem(idx, item) |
|
327 |
self.RefreshGrid() |
|
328 |
||
329 |
def GetDebugVariables(self): |
|
330 |
return [item.GetVariable() for item in self.Table.GetData()] |