684
|
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 |
import wx
|
|
26 |
import wx.lib.buttons
|
|
27 |
import wx.lib.agw.customtreectrl as CT
|
|
28 |
|
|
29 |
from PLCControler import ITEMS_VARIABLE, ITEM_CONFIGURATION, ITEM_RESOURCE, ITEM_POU
|
|
30 |
|
|
31 |
class PouInstanceVariablesPanel(wx.Panel):
|
|
32 |
|
|
33 |
def __init__(self, parent, window, controller, debug):
|
|
34 |
wx.Panel.__init__(self, name='PouInstanceTreePanel',
|
|
35 |
parent=parent, pos=wx.Point(0, 0),
|
|
36 |
size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)
|
|
37 |
|
|
38 |
self.ParentButton = wx.lib.buttons.GenBitmapButton(
|
|
39 |
name='ParentButton', parent=self,
|
|
40 |
bitmap=window.GenerateBitmap("up"),
|
|
41 |
pos=wx.Point(0, 0), size=wx.Size(28, 28),
|
|
42 |
style=wx.NO_BORDER)
|
|
43 |
self.Bind(wx.EVT_BUTTON, self.OnParentButtonClick,
|
|
44 |
self.ParentButton)
|
|
45 |
|
|
46 |
self.InstanceChoice = wx.ComboBox(name='InstanceChoice',
|
|
47 |
parent=self, pos=wx.Point(0, 0),
|
|
48 |
size=wx.Size(0, 28), style=wx.CB_READONLY)
|
|
49 |
self.Bind(wx.EVT_COMBOBOX, self.OnInstanceChoiceChanged,
|
|
50 |
self.InstanceChoice)
|
687
|
51 |
self.InstanceChoice.Bind(wx.EVT_LEFT_DOWN, self.OnInstanceChoiceLeftDown)
|
684
|
52 |
|
|
53 |
self.DebugButton = wx.lib.buttons.GenBitmapButton(
|
|
54 |
name='DebugButton', parent=self,
|
|
55 |
bitmap=window.GenerateBitmap("debug"),
|
|
56 |
pos=wx.Point(0, 0), size=wx.Size(28, 28),
|
|
57 |
style=wx.NO_BORDER)
|
|
58 |
self.Bind(wx.EVT_BUTTON, self.OnDebugButtonClick,
|
|
59 |
self.DebugButton)
|
687
|
60 |
|
684
|
61 |
self.VariablesList = CT.CustomTreeCtrl(
|
|
62 |
name='VariablesList', parent=self,
|
|
63 |
pos=wx.Point(0, 0), size=wx.Size(0, 0),
|
|
64 |
style=wx.SUNKEN_BORDER,
|
|
65 |
agwStyle=CT.TR_NO_BUTTONS|
|
|
66 |
CT.TR_SINGLE|
|
|
67 |
CT.TR_HAS_VARIABLE_ROW_HEIGHT|
|
|
68 |
CT.TR_HIDE_ROOT|
|
686
|
69 |
CT.TR_NO_LINES|
|
687
|
70 |
getattr(CT, "TR_ALIGN_WINDOWS_RIGHT", CT.TR_ALIGN_WINDOWS))
|
686
|
71 |
self.VariablesList.SetIndent(0)
|
|
72 |
self.VariablesList.SetSpacing(5)
|
|
73 |
self.VariablesList.DoSelectItem = lambda *x,**y:True
|
684
|
74 |
self.VariablesList.Bind(CT.EVT_TREE_ITEM_ACTIVATED,
|
|
75 |
self.OnVariablesListItemActivated)
|
687
|
76 |
self.VariablesList.Bind(wx.EVT_LEFT_DOWN, self.OnVariablesListLeftDown)
|
684
|
77 |
|
|
78 |
buttons_sizer = wx.FlexGridSizer(cols=3, hgap=0, rows=1, vgap=0)
|
|
79 |
buttons_sizer.AddWindow(self.ParentButton, 0, border=0, flag=0)
|
|
80 |
buttons_sizer.AddWindow(self.InstanceChoice, 0, border=0, flag=wx.GROW)
|
|
81 |
buttons_sizer.AddWindow(self.DebugButton, 0, border=0, flag=0)
|
|
82 |
buttons_sizer.AddGrowableCol(1)
|
|
83 |
buttons_sizer.AddGrowableRow(0)
|
|
84 |
|
|
85 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
|
|
86 |
main_sizer.AddSizer(buttons_sizer, 0, border=0, flag=wx.GROW)
|
|
87 |
main_sizer.AddWindow(self.VariablesList, 0, border=0, flag=wx.GROW)
|
|
88 |
main_sizer.AddGrowableCol(0)
|
|
89 |
main_sizer.AddGrowableRow(1)
|
|
90 |
|
|
91 |
self.SetSizer(main_sizer)
|
|
92 |
|
|
93 |
self.ParentWindow = window
|
|
94 |
self.Controller = controller
|
|
95 |
self.Debug = debug
|
|
96 |
if not self.Debug:
|
|
97 |
self.DebugButton.Hide()
|
|
98 |
|
|
99 |
self.PouTagName = None
|
|
100 |
self.PouInfos = None
|
|
101 |
self.PouInstance = None
|
|
102 |
|
|
103 |
def __del__(self):
|
|
104 |
self.Controller = None
|
|
105 |
|
|
106 |
def SetTreeImageList(self, tree_image_list):
|
|
107 |
self.VariablesList.SetImageList(tree_image_list)
|
|
108 |
|
|
109 |
def SetController(self, controller):
|
|
110 |
self.Controller = controller
|
687
|
111 |
|
|
112 |
self.RefreshView()
|
|
113 |
|
684
|
114 |
def SetPouType(self, tagname, pou_instance=None):
|
|
115 |
self.PouTagName = tagname
|
|
116 |
if pou_instance is not None:
|
|
117 |
self.PouInstance = pou_instance
|
|
118 |
|
|
119 |
self.RefreshView()
|
|
120 |
|
|
121 |
def ResetView(self):
|
687
|
122 |
self.Controller = None
|
|
123 |
|
684
|
124 |
self.PouTagName = None
|
|
125 |
self.PouInfos = None
|
|
126 |
self.PouInstance = None
|
|
127 |
|
|
128 |
self.RefreshView()
|
|
129 |
|
|
130 |
def RefreshView(self):
|
|
131 |
self.VariablesList.DeleteAllItems()
|
|
132 |
self.InstanceChoice.Clear()
|
687
|
133 |
self.InstanceChoice.SetValue("")
|
684
|
134 |
|
|
135 |
if self.PouTagName is not None:
|
|
136 |
self.PouInfos = self.Controller.GetPouVariables(self.PouTagName, self.Debug)
|
|
137 |
else:
|
|
138 |
self.PouInfos = None
|
|
139 |
if self.PouInfos is not None:
|
|
140 |
root = self.VariablesList.AddRoot("")
|
|
141 |
for var_infos in self.PouInfos["variables"]:
|
|
142 |
if var_infos.get("type", None) is not None:
|
|
143 |
text = "%(name)s (%(type)s)" % var_infos
|
|
144 |
else:
|
|
145 |
text = var_infos["name"]
|
|
146 |
|
|
147 |
panel = wx.Panel(self.VariablesList)
|
|
148 |
|
|
149 |
buttons = []
|
|
150 |
if var_infos["class"] in ITEMS_VARIABLE:
|
|
151 |
if (var_infos["debug"] and self.Debug and
|
|
152 |
(self.Controller.IsOfType(var_infos["type"], "ANY_NUM", True) or
|
|
153 |
self.Controller.IsOfType(var_infos["type"], "ANY_BIT", True))):
|
|
154 |
graph_button = wx.lib.buttons.GenBitmapButton(name="graph",
|
|
155 |
parent=panel, bitmap=self.ParentWindow.GenerateBitmap("graph"),
|
|
156 |
pos=wx.Point(0, 0), size=wx.Size(28, 28), style=wx.NO_BORDER)
|
|
157 |
self.Bind(wx.EVT_BUTTON, self.GenGraphButtonCallback(var_infos), graph_button)
|
|
158 |
buttons.append(graph_button)
|
|
159 |
elif var_infos["edit"]:
|
|
160 |
edit_button = wx.lib.buttons.GenBitmapButton(name="edit",
|
|
161 |
parent=panel, bitmap=self.ParentWindow.GenerateBitmap("edit"),
|
|
162 |
pos=wx.Point(0, 0), size=wx.Size(28, 28), style=wx.NO_BORDER)
|
|
163 |
self.Bind(wx.EVT_BUTTON, self.GenEditButtonCallback(var_infos), edit_button)
|
|
164 |
buttons.append(edit_button)
|
|
165 |
|
|
166 |
if var_infos["debug"] and self.Debug:
|
|
167 |
debug_button = wx.lib.buttons.GenBitmapButton(name="debug",
|
|
168 |
parent=panel, bitmap=self.ParentWindow.GenerateBitmap("debug"),
|
|
169 |
pos=wx.Point(0, 0), size=wx.Size(28, 28), style=wx.NO_BORDER)
|
|
170 |
self.Bind(wx.EVT_BUTTON, self.GenDebugButtonCallback(var_infos), debug_button)
|
|
171 |
buttons.append(debug_button)
|
|
172 |
|
|
173 |
button_num = len(buttons)
|
|
174 |
if button_num > 0:
|
|
175 |
panel.SetSize(wx.Size(button_num * 32, 28))
|
|
176 |
panel.SetBackgroundColour(self.VariablesList.GetBackgroundColour())
|
|
177 |
panel_sizer = wx.BoxSizer(wx.HORIZONTAL)
|
|
178 |
panel.SetSizer(panel_sizer)
|
|
179 |
|
|
180 |
for button in buttons:
|
|
181 |
panel_sizer.AddWindow(button, 0, border=4, flag=wx.LEFT)
|
|
182 |
else:
|
|
183 |
panel.Destroy()
|
|
184 |
panel = None
|
|
185 |
|
|
186 |
item = self.VariablesList.AppendItem(root, text, wnd=panel)
|
|
187 |
self.VariablesList.SetItemImage(item, self.ParentWindow.GetTreeImage(var_infos["class"]))
|
|
188 |
self.VariablesList.SetPyData(item, var_infos)
|
|
189 |
|
|
190 |
instances = self.Controller.SearchPouInstances(self.PouTagName, self.Debug)
|
|
191 |
for instance in instances:
|
|
192 |
self.InstanceChoice.Append(instance)
|
687
|
193 |
if len(instances) == 1:
|
|
194 |
self.PouInstance = instances[0]
|
684
|
195 |
if self.PouInfos["class"] in [ITEM_CONFIGURATION, ITEM_RESOURCE]:
|
|
196 |
self.PouInstance = None
|
|
197 |
self.InstanceChoice.SetSelection(0)
|
|
198 |
elif self.PouInstance in instances:
|
|
199 |
self.InstanceChoice.SetStringSelection(self.PouInstance)
|
|
200 |
else:
|
|
201 |
self.PouInstance = None
|
|
202 |
self.InstanceChoice.SetValue(_("Select an instance"))
|
|
203 |
|
|
204 |
self.RefreshButtons()
|
|
205 |
|
|
206 |
def RefreshButtons(self):
|
|
207 |
enabled = self.InstanceChoice.GetSelection() != -1
|
|
208 |
self.ParentButton.Enable(enabled and self.PouInfos["class"] != ITEM_CONFIGURATION)
|
|
209 |
self.DebugButton.Enable(enabled and self.PouInfos["debug"] and self.Debug)
|
|
210 |
|
|
211 |
root = self.VariablesList.GetRootItem()
|
|
212 |
if root is not None and root.IsOk():
|
|
213 |
item, item_cookie = self.VariablesList.GetFirstChild(root)
|
|
214 |
while item is not None and item.IsOk():
|
|
215 |
panel = self.VariablesList.GetItemWindow(item)
|
|
216 |
if panel is not None:
|
|
217 |
for child in panel.GetChildren():
|
|
218 |
if child.GetName() != "edit":
|
|
219 |
child.Enable(enabled)
|
|
220 |
item, item_cookie = self.VariablesList.GetNextChild(root, item_cookie)
|
|
221 |
|
|
222 |
def GenEditButtonCallback(self, infos):
|
|
223 |
def EditButtonCallback(event):
|
|
224 |
var_class = infos["class"]
|
|
225 |
if var_class == ITEM_RESOURCE:
|
|
226 |
tagname = self.Controller.ComputeConfigurationResourceName(
|
|
227 |
self.InstanceChoice.GetStringSelection(),
|
|
228 |
infos["name"])
|
|
229 |
else:
|
|
230 |
var_class = ITEM_POU
|
|
231 |
tagname = self.Controller.ComputePouName(infos["type"])
|
|
232 |
self.ParentWindow.EditProjectElement(var_class, tagname)
|
|
233 |
event.Skip()
|
|
234 |
return EditButtonCallback
|
|
235 |
|
|
236 |
def GenDebugButtonCallback(self, infos):
|
|
237 |
def DebugButtonCallback(event):
|
|
238 |
if self.InstanceChoice.GetSelection() != -1:
|
|
239 |
var_class = infos["class"]
|
|
240 |
var_path = "%s.%s" % (self.InstanceChoice.GetStringSelection(),
|
|
241 |
infos["name"])
|
|
242 |
if var_class in ITEMS_VARIABLE:
|
|
243 |
self.ParentWindow.AddDebugVariable(var_path, force=True)
|
|
244 |
else:
|
|
245 |
self.ParentWindow.OpenDebugViewer(
|
|
246 |
infos["class"],
|
|
247 |
var_path,
|
|
248 |
self.Controller.ComputePouName(infos["type"]))
|
|
249 |
event.Skip()
|
|
250 |
return DebugButtonCallback
|
|
251 |
|
|
252 |
def GenGraphButtonCallback(self, infos):
|
|
253 |
def GraphButtonCallback(event):
|
|
254 |
if self.InstanceChoice.GetSelection() != -1:
|
|
255 |
if infos["class"] in ITEMS_VARIABLE:
|
|
256 |
var_path = "%s.%s" % (self.InstanceChoice.GetStringSelection(),
|
|
257 |
infos["name"])
|
|
258 |
self.ParentWindow.OpenGraphicViewer(var_path)
|
|
259 |
event.Skip()
|
|
260 |
return GraphButtonCallback
|
|
261 |
|
687
|
262 |
def ShowInstanceChoicePopup(self):
|
|
263 |
self.InstanceChoice.SetFocusFromKbd()
|
|
264 |
size = self.InstanceChoice.GetSize()
|
|
265 |
event = wx.MouseEvent(wx.EVT_LEFT_DOWN._getEvtType())
|
|
266 |
event.m_x = size.width / 2
|
|
267 |
event.m_y = size.height / 2
|
|
268 |
event.SetEventObject(self.InstanceChoice)
|
|
269 |
#event = wx.KeyEvent(wx.EVT_KEY_DOWN._getEvtType())
|
|
270 |
#event.m_keyCode = wx.WXK_SPACE
|
|
271 |
self.InstanceChoice.GetEventHandler().ProcessEvent(event)
|
|
272 |
|
684
|
273 |
def OnParentButtonClick(self, event):
|
|
274 |
if self.InstanceChoice.GetSelection() != -1:
|
|
275 |
parent_path = self.InstanceChoice.GetStringSelection().rsplit(".", 1)[0]
|
687
|
276 |
tagname = self.Controller.GetPouInstanceTagName(parent_path, self.Debug)
|
684
|
277 |
if tagname is not None:
|
|
278 |
wx.CallAfter(self.SetPouType, tagname, parent_path)
|
|
279 |
wx.CallAfter(self.ParentWindow.SelectProjectTreeItem, tagname)
|
|
280 |
event.Skip()
|
|
281 |
|
|
282 |
def OnInstanceChoiceChanged(self, event):
|
|
283 |
self.RefreshButtons()
|
|
284 |
event.Skip()
|
|
285 |
|
|
286 |
def OnDebugButtonClick(self, event):
|
|
287 |
if self.InstanceChoice.GetSelection() != -1:
|
|
288 |
self.ParentWindow.OpenDebugViewer(
|
|
289 |
self.PouInfos["class"],
|
|
290 |
self.InstanceChoice.GetStringSelection(),
|
|
291 |
self.PouTagName)
|
|
292 |
event.Skip()
|
|
293 |
|
|
294 |
def OnVariablesListItemActivated(self, event):
|
|
295 |
if self.InstanceChoice.GetSelection() != -1:
|
|
296 |
instance_path = self.InstanceChoice.GetStringSelection()
|
686
|
297 |
selected_item = event.GetItem()
|
684
|
298 |
if selected_item is not None and selected_item.IsOk():
|
|
299 |
item_infos = self.VariablesList.GetPyData(selected_item)
|
687
|
300 |
if item_infos is not None and item_infos["class"] not in ITEMS_VARIABLE:
|
684
|
301 |
if item_infos["class"] == ITEM_RESOURCE:
|
|
302 |
tagname = self.Controller.ComputeConfigurationResourceName(
|
|
303 |
instance_path,
|
|
304 |
item_infos["name"])
|
|
305 |
else:
|
|
306 |
tagname = self.Controller.ComputePouName(item_infos["type"])
|
|
307 |
item_path = "%s.%s" % (instance_path, item_infos["name"])
|
|
308 |
wx.CallAfter(self.SetPouType, tagname, item_path)
|
|
309 |
wx.CallAfter(self.ParentWindow.SelectProjectTreeItem, tagname)
|
|
310 |
event.Skip()
|
|
311 |
|
687
|
312 |
def OnVariablesListLeftDown(self, event):
|
|
313 |
if self.InstanceChoice.GetSelection() == -1:
|
|
314 |
wx.CallAfter(self.ShowInstanceChoicePopup)
|
|
315 |
event.Skip()
|
|
316 |
|
|
317 |
def OnInstanceChoiceLeftDown(self, event):
|
|
318 |
event.Skip()
|