|
1 # |
|
2 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
3 # |
|
4 #See COPYING file for copyrights details. |
|
5 # |
|
6 #This library is free software; you can redistribute it and/or |
|
7 #modify it under the terms of the GNU General Public |
|
8 #License as published by the Free Software Foundation; either |
|
9 #version 2.1 of the License, or (at your option) any later version. |
|
10 # |
|
11 #This library is distributed in the hope that it will be useful, |
|
12 #but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 #General Public License for more details. |
|
15 # |
|
16 #You should have received a copy of the GNU General Public |
|
17 #License along with this library; if not, write to the Free Software |
|
18 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
19 |
|
20 import os |
|
21 |
|
22 import wx |
|
23 |
|
24 from plcopen.structures import LOCATIONDATATYPES |
|
25 from PLCControler import LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY |
|
26 from util.BitmapLibrary import GetBitmap |
|
27 |
|
28 #------------------------------------------------------------------------------- |
|
29 # Helpers |
|
30 #------------------------------------------------------------------------------- |
|
31 |
|
32 def GetDirFilterChoiceOptions(): |
|
33 _ = lambda x : x |
|
34 return [(_("All"), [LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY]), |
|
35 (_("Input"), [LOCATION_VAR_INPUT]), |
|
36 (_("Output"), [LOCATION_VAR_OUTPUT]), |
|
37 (_("Memory"), [LOCATION_VAR_MEMORY])] |
|
38 DIRFILTERCHOICE_OPTIONS = dict([(_(option), filter) for option, filter in GetDirFilterChoiceOptions()]) |
|
39 |
|
40 def GetTypeFilterChoiceOptions(): |
|
41 _ = lambda x : x |
|
42 return [_("All"), |
|
43 _("Type and derivated"), |
|
44 _("Type strict")] |
|
45 |
|
46 # turn LOCATIONDATATYPES inside-out |
|
47 LOCATION_SIZES = {} |
|
48 for size, types in LOCATIONDATATYPES.iteritems(): |
|
49 for type in types: |
|
50 LOCATION_SIZES[type] = size |
|
51 |
|
52 #------------------------------------------------------------------------------- |
|
53 # Browse Locations Dialog |
|
54 #------------------------------------------------------------------------------- |
|
55 |
|
56 class BrowseLocationsDialog(wx.Dialog): |
|
57 |
|
58 def __init__(self, parent, var_type, controller): |
|
59 wx.Dialog.__init__(self, parent, |
|
60 size=wx.Size(600, 400), title=_('Browse Locations'), |
|
61 style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER) |
|
62 |
|
63 main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) |
|
64 main_sizer.AddGrowableCol(0) |
|
65 main_sizer.AddGrowableRow(1) |
|
66 |
|
67 locations_label = wx.StaticText(self, label=_('Locations available:')) |
|
68 main_sizer.AddWindow(locations_label, border=20, |
|
69 flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW) |
|
70 |
|
71 self.LocationsTree = wx.TreeCtrl(self, |
|
72 style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER|wx.TR_HIDE_ROOT|wx.TR_LINES_AT_ROOT) |
|
73 self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnLocationsTreeItemActivated, |
|
74 self.LocationsTree) |
|
75 main_sizer.AddWindow(self.LocationsTree, border=20, |
|
76 flag=wx.LEFT|wx.RIGHT|wx.GROW) |
|
77 |
|
78 button_gridsizer = wx.FlexGridSizer(cols=5, hgap=5, rows=1, vgap=0) |
|
79 button_gridsizer.AddGrowableCol(1) |
|
80 button_gridsizer.AddGrowableCol(3) |
|
81 button_gridsizer.AddGrowableRow(0) |
|
82 main_sizer.AddSizer(button_gridsizer, border=20, |
|
83 flag=wx.BOTTOM|wx.LEFT|wx.RIGHT|wx.GROW) |
|
84 |
|
85 direction_label = wx.StaticText(self, label=_('Direction:')) |
|
86 button_gridsizer.AddWindow(direction_label, |
|
87 flag=wx.ALIGN_CENTER_VERTICAL) |
|
88 |
|
89 self.DirFilterChoice = wx.ComboBox(self, size=wx.Size(0, -1), style=wx.CB_READONLY) |
|
90 self.Bind(wx.EVT_COMBOBOX, self.OnFilterChoice, self.DirFilterChoice) |
|
91 button_gridsizer.AddWindow(self.DirFilterChoice, |
|
92 flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL) |
|
93 |
|
94 filter_label = wx.StaticText(self, label=_('Type:')) |
|
95 button_gridsizer.AddWindow(filter_label, |
|
96 flag=wx.ALIGN_CENTER_VERTICAL) |
|
97 |
|
98 self.TypeFilterChoice = wx.ComboBox(self, size=wx.Size(0, -1), style=wx.CB_READONLY) |
|
99 self.Bind(wx.EVT_COMBOBOX, self.OnFilterChoice, self.TypeFilterChoice) |
|
100 button_gridsizer.AddWindow(self.TypeFilterChoice, |
|
101 flag=wx.GROW|wx.ALIGN_CENTER_VERTICAL) |
|
102 |
|
103 button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
|
104 self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton()) |
|
105 button_gridsizer.AddSizer(button_sizer, flag=wx.ALIGN_RIGHT) |
|
106 |
|
107 self.SetSizer(main_sizer) |
|
108 |
|
109 self.Controller = controller |
|
110 self.VarType = var_type |
|
111 self.BaseVarType = self.Controller.GetBaseType(self.VarType) |
|
112 self.VarTypeSize = LOCATION_SIZES[self.BaseVarType] |
|
113 self.Locations = self.Controller.GetVariableLocationTree() |
|
114 |
|
115 # Define Tree item icon list |
|
116 self.TreeImageList = wx.ImageList(16, 16) |
|
117 self.TreeImageDict = {} |
|
118 |
|
119 # Icons for items |
|
120 for imgname, itemtype in [ |
|
121 ("CONFIGURATION", LOCATION_CONFNODE), |
|
122 ("RESOURCE", LOCATION_MODULE), |
|
123 ("PROGRAM", LOCATION_GROUP), |
|
124 ("VAR_INPUT", LOCATION_VAR_INPUT), |
|
125 ("VAR_OUTPUT", LOCATION_VAR_OUTPUT), |
|
126 ("VAR_LOCAL", LOCATION_VAR_MEMORY)]: |
|
127 self.TreeImageDict[itemtype]=self.TreeImageList.Add(GetBitmap(imgname)) |
|
128 |
|
129 # Assign icon list to TreeCtrls |
|
130 self.LocationsTree.SetImageList(self.TreeImageList) |
|
131 |
|
132 # Set a options for the choice |
|
133 for option, filter in GetDirFilterChoiceOptions(): |
|
134 self.DirFilterChoice.Append(_(option)) |
|
135 self.DirFilterChoice.SetStringSelection(_("All")) |
|
136 for option in GetTypeFilterChoiceOptions(): |
|
137 self.TypeFilterChoice.Append(_(option)) |
|
138 self.TypeFilterChoice.SetStringSelection(_("All")) |
|
139 self.RefreshFilters() |
|
140 |
|
141 self.RefreshLocationsTree() |
|
142 |
|
143 def RefreshFilters(self): |
|
144 self.DirFilter = DIRFILTERCHOICE_OPTIONS[self.DirFilterChoice.GetStringSelection()] |
|
145 self.TypeFilter = self.TypeFilterChoice.GetSelection() |
|
146 |
|
147 def RefreshLocationsTree(self): |
|
148 root = self.LocationsTree.GetRootItem() |
|
149 if not root.IsOk(): |
|
150 root = self.LocationsTree.AddRoot("") |
|
151 self.GenerateLocationsTreeBranch(root, self.Locations) |
|
152 |
|
153 def FilterType(self, location_type, location_size): |
|
154 if self.TypeFilter == 0: |
|
155 return True |
|
156 |
|
157 if location_size != self.VarTypeSize: |
|
158 return False |
|
159 |
|
160 if self.TypeFilter == 1: |
|
161 return self.Controller.IsOfType(location_type, self.BaseVarType) |
|
162 elif self.TypeFilter == 2: |
|
163 return location_type == self.VarType |
|
164 |
|
165 return True |
|
166 |
|
167 def GenerateLocationsTreeBranch(self, root, locations): |
|
168 to_delete = [] |
|
169 item, root_cookie = self.LocationsTree.GetFirstChild(root) |
|
170 for loc_infos in locations: |
|
171 infos = loc_infos.copy() |
|
172 if infos["type"] in [LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP] or\ |
|
173 infos["type"] in self.DirFilter and self.FilterType(infos["IEC_type"], infos["size"]): |
|
174 children = [child for child in infos.pop("children")] |
|
175 if not item.IsOk(): |
|
176 item = self.LocationsTree.AppendItem(root, infos["name"]) |
|
177 if wx.Platform != '__WXMSW__': |
|
178 item, root_cookie = self.LocationsTree.GetNextChild(root, root_cookie) |
|
179 else: |
|
180 self.LocationsTree.SetItemText(item, infos["name"]) |
|
181 self.LocationsTree.SetPyData(item, infos) |
|
182 self.LocationsTree.SetItemImage(item, self.TreeImageDict[infos["type"]]) |
|
183 self.GenerateLocationsTreeBranch(item, children) |
|
184 item, root_cookie = self.LocationsTree.GetNextChild(root, root_cookie) |
|
185 while item.IsOk(): |
|
186 to_delete.append(item) |
|
187 item, root_cookie = self.LocationsTree.GetNextChild(root, root_cookie) |
|
188 for item in to_delete: |
|
189 self.LocationsTree.Delete(item) |
|
190 |
|
191 def OnLocationsTreeItemActivated(self, event): |
|
192 infos = self.LocationsTree.GetPyData(event.GetItem()) |
|
193 if infos["type"] not in [LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP]: |
|
194 wx.CallAfter(self.EndModal, wx.ID_OK) |
|
195 event.Skip() |
|
196 |
|
197 def OnFilterChoice(self, event): |
|
198 self.RefreshFilters() |
|
199 self.RefreshLocationsTree() |
|
200 |
|
201 def GetValues(self): |
|
202 selected = self.LocationsTree.GetSelection() |
|
203 return self.LocationsTree.GetPyData(selected) |
|
204 |
|
205 def OnOK(self, event): |
|
206 selected = self.LocationsTree.GetSelection() |
|
207 var_infos = None |
|
208 if selected.IsOk(): |
|
209 var_infos = self.LocationsTree.GetPyData(selected) |
|
210 if var_infos is None or var_infos["type"] in [LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP]: |
|
211 dialog = wx.MessageDialog(self, _("A location must be selected!"), _("Error"), wx.OK|wx.ICON_ERROR) |
|
212 dialog.ShowModal() |
|
213 dialog.Destroy() |
|
214 else: |
|
215 self.EndModal(wx.ID_OK) |