author | Laurent Bessard |
Mon, 11 Jun 2012 01:25:32 +0200 | |
changeset 709 | 2507f2736a3e |
parent 701 | 25fbbb005a30 |
child 714 | 131ea7f237b9 |
permissions | -rw-r--r-- |
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 |
|
701
25fbbb005a30
Fix bug in POUinstanceVariablesPanel when no tagname defined
Laurent Bessard
parents:
692
diff
changeset
|
135 |
if self.Controller is not None and self.PouTagName is not None: |
684 | 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) |
|
692 | 182 |
panel_sizer.Layout() |
183 |
||
684 | 184 |
else: |
185 |
panel.Destroy() |
|
186 |
panel = None |
|
187 |
||
188 |
item = self.VariablesList.AppendItem(root, text, wnd=panel) |
|
189 |
self.VariablesList.SetItemImage(item, self.ParentWindow.GetTreeImage(var_infos["class"])) |
|
190 |
self.VariablesList.SetPyData(item, var_infos) |
|
191 |
||
192 |
instances = self.Controller.SearchPouInstances(self.PouTagName, self.Debug) |
|
193 |
for instance in instances: |
|
194 |
self.InstanceChoice.Append(instance) |
|
687 | 195 |
if len(instances) == 1: |
196 |
self.PouInstance = instances[0] |
|
684 | 197 |
if self.PouInfos["class"] in [ITEM_CONFIGURATION, ITEM_RESOURCE]: |
198 |
self.PouInstance = None |
|
199 |
self.InstanceChoice.SetSelection(0) |
|
200 |
elif self.PouInstance in instances: |
|
201 |
self.InstanceChoice.SetStringSelection(self.PouInstance) |
|
202 |
else: |
|
203 |
self.PouInstance = None |
|
204 |
self.InstanceChoice.SetValue(_("Select an instance")) |
|
205 |
||
206 |
self.RefreshButtons() |
|
207 |
||
208 |
def RefreshButtons(self): |
|
209 |
enabled = self.InstanceChoice.GetSelection() != -1 |
|
210 |
self.ParentButton.Enable(enabled and self.PouInfos["class"] != ITEM_CONFIGURATION) |
|
211 |
self.DebugButton.Enable(enabled and self.PouInfos["debug"] and self.Debug) |
|
212 |
||
213 |
root = self.VariablesList.GetRootItem() |
|
214 |
if root is not None and root.IsOk(): |
|
215 |
item, item_cookie = self.VariablesList.GetFirstChild(root) |
|
216 |
while item is not None and item.IsOk(): |
|
217 |
panel = self.VariablesList.GetItemWindow(item) |
|
218 |
if panel is not None: |
|
219 |
for child in panel.GetChildren(): |
|
220 |
if child.GetName() != "edit": |
|
221 |
child.Enable(enabled) |
|
222 |
item, item_cookie = self.VariablesList.GetNextChild(root, item_cookie) |
|
223 |
||
224 |
def GenEditButtonCallback(self, infos): |
|
225 |
def EditButtonCallback(event): |
|
226 |
var_class = infos["class"] |
|
227 |
if var_class == ITEM_RESOURCE: |
|
228 |
tagname = self.Controller.ComputeConfigurationResourceName( |
|
229 |
self.InstanceChoice.GetStringSelection(), |
|
230 |
infos["name"]) |
|
231 |
else: |
|
232 |
var_class = ITEM_POU |
|
233 |
tagname = self.Controller.ComputePouName(infos["type"]) |
|
234 |
self.ParentWindow.EditProjectElement(var_class, tagname) |
|
235 |
event.Skip() |
|
236 |
return EditButtonCallback |
|
237 |
||
238 |
def GenDebugButtonCallback(self, infos): |
|
239 |
def DebugButtonCallback(event): |
|
240 |
if self.InstanceChoice.GetSelection() != -1: |
|
241 |
var_class = infos["class"] |
|
242 |
var_path = "%s.%s" % (self.InstanceChoice.GetStringSelection(), |
|
243 |
infos["name"]) |
|
244 |
if var_class in ITEMS_VARIABLE: |
|
245 |
self.ParentWindow.AddDebugVariable(var_path, force=True) |
|
246 |
else: |
|
247 |
self.ParentWindow.OpenDebugViewer( |
|
248 |
infos["class"], |
|
249 |
var_path, |
|
250 |
self.Controller.ComputePouName(infos["type"])) |
|
251 |
event.Skip() |
|
252 |
return DebugButtonCallback |
|
253 |
||
254 |
def GenGraphButtonCallback(self, infos): |
|
255 |
def GraphButtonCallback(event): |
|
256 |
if self.InstanceChoice.GetSelection() != -1: |
|
257 |
if infos["class"] in ITEMS_VARIABLE: |
|
258 |
var_path = "%s.%s" % (self.InstanceChoice.GetStringSelection(), |
|
259 |
infos["name"]) |
|
260 |
self.ParentWindow.OpenGraphicViewer(var_path) |
|
261 |
event.Skip() |
|
262 |
return GraphButtonCallback |
|
263 |
||
687 | 264 |
def ShowInstanceChoicePopup(self): |
265 |
self.InstanceChoice.SetFocusFromKbd() |
|
266 |
size = self.InstanceChoice.GetSize() |
|
267 |
event = wx.MouseEvent(wx.EVT_LEFT_DOWN._getEvtType()) |
|
268 |
event.m_x = size.width / 2 |
|
269 |
event.m_y = size.height / 2 |
|
270 |
event.SetEventObject(self.InstanceChoice) |
|
271 |
#event = wx.KeyEvent(wx.EVT_KEY_DOWN._getEvtType()) |
|
272 |
#event.m_keyCode = wx.WXK_SPACE |
|
273 |
self.InstanceChoice.GetEventHandler().ProcessEvent(event) |
|
274 |
||
684 | 275 |
def OnParentButtonClick(self, event): |
276 |
if self.InstanceChoice.GetSelection() != -1: |
|
277 |
parent_path = self.InstanceChoice.GetStringSelection().rsplit(".", 1)[0] |
|
687 | 278 |
tagname = self.Controller.GetPouInstanceTagName(parent_path, self.Debug) |
684 | 279 |
if tagname is not None: |
280 |
wx.CallAfter(self.SetPouType, tagname, parent_path) |
|
281 |
wx.CallAfter(self.ParentWindow.SelectProjectTreeItem, tagname) |
|
282 |
event.Skip() |
|
283 |
||
284 |
def OnInstanceChoiceChanged(self, event): |
|
285 |
self.RefreshButtons() |
|
286 |
event.Skip() |
|
287 |
||
288 |
def OnDebugButtonClick(self, event): |
|
289 |
if self.InstanceChoice.GetSelection() != -1: |
|
290 |
self.ParentWindow.OpenDebugViewer( |
|
291 |
self.PouInfos["class"], |
|
292 |
self.InstanceChoice.GetStringSelection(), |
|
293 |
self.PouTagName) |
|
294 |
event.Skip() |
|
295 |
||
296 |
def OnVariablesListItemActivated(self, event): |
|
297 |
if self.InstanceChoice.GetSelection() != -1: |
|
298 |
instance_path = self.InstanceChoice.GetStringSelection() |
|
686 | 299 |
selected_item = event.GetItem() |
684 | 300 |
if selected_item is not None and selected_item.IsOk(): |
301 |
item_infos = self.VariablesList.GetPyData(selected_item) |
|
687 | 302 |
if item_infos is not None and item_infos["class"] not in ITEMS_VARIABLE: |
684 | 303 |
if item_infos["class"] == ITEM_RESOURCE: |
304 |
tagname = self.Controller.ComputeConfigurationResourceName( |
|
305 |
instance_path, |
|
306 |
item_infos["name"]) |
|
307 |
else: |
|
308 |
tagname = self.Controller.ComputePouName(item_infos["type"]) |
|
309 |
item_path = "%s.%s" % (instance_path, item_infos["name"]) |
|
310 |
wx.CallAfter(self.SetPouType, tagname, item_path) |
|
311 |
wx.CallAfter(self.ParentWindow.SelectProjectTreeItem, tagname) |
|
312 |
event.Skip() |
|
313 |
||
687 | 314 |
def OnVariablesListLeftDown(self, event): |
315 |
if self.InstanceChoice.GetSelection() == -1: |
|
316 |
wx.CallAfter(self.ShowInstanceChoicePopup) |
|
317 |
event.Skip() |
|
318 |
||
319 |
def OnInstanceChoiceLeftDown(self, event): |
|
320 |
event.Skip() |