|
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_PLUGIN, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY |
|
26 |
|
27 CWD = os.path.split(os.path.split(os.path.realpath(__file__))[0])[0] |
|
28 |
|
29 def GetDirChoiceOptions(): |
|
30 _ = lambda x : x |
|
31 return [(_("All"), [LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY]), |
|
32 (_("Input"), [LOCATION_VAR_INPUT]), |
|
33 (_("Output"), [LOCATION_VAR_OUTPUT]), |
|
34 (_("Memory"), [LOCATION_VAR_MEMORY])] |
|
35 DIRCHOICE_OPTIONS_FILTER = dict([(_(option), filter) for option, filter in GetDirChoiceOptions()]) |
|
36 |
|
37 # turn LOCATIONDATATYPES inside-out |
|
38 LOCATION_SIZES = {} |
|
39 for size, types in LOCATIONDATATYPES.iteritems(): |
|
40 for type in types: |
|
41 LOCATION_SIZES[type] = size |
|
42 |
|
43 [ID_BROWSELOCATIONSDIALOG, ID_BROWSELOCATIONSDIALOGLOCATIONSTREE, |
|
44 ID_BROWSELOCATIONSDIALOGDIRCHOICE, ID_BROWSELOCATIONSDIALOGSTATICTEXT1, |
|
45 ID_BROWSELOCATIONSDIALOGSTATICTEXT2, |
|
46 ] = [wx.NewId() for _init_ctrls in range(5)] |
|
47 |
|
48 class BrowseLocationsDialog(wx.Dialog): |
|
49 |
|
50 if wx.VERSION < (2, 6, 0): |
|
51 def Bind(self, event, function, id = None): |
|
52 if id is not None: |
|
53 event(self, id, function) |
|
54 else: |
|
55 event(self, function) |
|
56 |
|
57 def _init_coll_MainSizer_Items(self, parent): |
|
58 parent.AddWindow(self.staticText1, 0, border=20, flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW) |
|
59 parent.AddWindow(self.LocationsTree, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.GROW) |
|
60 parent.AddSizer(self.ButtonGridSizer, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.GROW) |
|
61 |
|
62 def _init_coll_MainSizer_Growables(self, parent): |
|
63 parent.AddGrowableCol(0) |
|
64 parent.AddGrowableRow(1) |
|
65 |
|
66 def _init_coll_ButtonGridSizer_Items(self, parent): |
|
67 parent.AddWindow(self.staticText2, 0, border=0, flag=wx.ALIGN_CENTER_VERTICAL) |
|
68 parent.AddWindow(self.DirChoice, 0, border=0, flag=wx.ALIGN_CENTER_VERTICAL) |
|
69 parent.AddSizer(self.ButtonSizer, 0, border=0, flag=wx.ALIGN_RIGHT) |
|
70 |
|
71 def _init_coll_ButtonGridSizer_Growables(self, parent): |
|
72 parent.AddGrowableCol(2) |
|
73 parent.AddGrowableRow(0) |
|
74 |
|
75 def _init_sizers(self): |
|
76 self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) |
|
77 self.ButtonGridSizer = wx.FlexGridSizer(cols=3, hgap=5, rows=1, vgap=0) |
|
78 |
|
79 self._init_coll_MainSizer_Items(self.MainSizer) |
|
80 self._init_coll_MainSizer_Growables(self.MainSizer) |
|
81 self._init_coll_ButtonGridSizer_Items(self.ButtonGridSizer) |
|
82 self._init_coll_ButtonGridSizer_Growables(self.ButtonGridSizer) |
|
83 |
|
84 self.SetSizer(self.MainSizer) |
|
85 |
|
86 def _init_ctrls(self, prnt): |
|
87 wx.Dialog.__init__(self, id=ID_BROWSELOCATIONSDIALOG, |
|
88 name='BrowseLocationsDialog', parent=prnt, |
|
89 size=wx.Size(600, 400), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER, |
|
90 title=_('Browse Locations')) |
|
91 |
|
92 self.staticText1 = wx.StaticText(id=ID_BROWSELOCATIONSDIALOGSTATICTEXT1, |
|
93 label=_('Locations available:'), name='staticText1', parent=self, |
|
94 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
95 |
|
96 if wx.Platform == '__WXMSW__': |
|
97 treestyle = wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER |
|
98 else: |
|
99 treestyle = wx.TR_HAS_BUTTONS|wx.TR_HIDE_ROOT|wx.TR_SINGLE|wx.SUNKEN_BORDER |
|
100 self.LocationsTree = wx.TreeCtrl(id=ID_BROWSELOCATIONSDIALOGLOCATIONSTREE, |
|
101 name='LocationsTree', parent=self, pos=wx.Point(0, 0), |
|
102 size=wx.Size(0, 0), style=treestyle) |
|
103 self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnLocationsTreeItemActivated, |
|
104 id=ID_BROWSELOCATIONSDIALOGLOCATIONSTREE) |
|
105 |
|
106 self.staticText2 = wx.StaticText(id=ID_BROWSELOCATIONSDIALOGSTATICTEXT2, |
|
107 label=_('Direction:'), name='staticText2', parent=self, |
|
108 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
109 |
|
110 self.DirChoice = wx.ComboBox(id=ID_BROWSELOCATIONSDIALOGDIRCHOICE, |
|
111 name='DirChoice', parent=self, pos=wx.Point(0, 0), |
|
112 size=wx.DefaultSize, style=wx.CB_READONLY) |
|
113 self.Bind(wx.EVT_COMBOBOX, self.OnDirChoice, id=ID_BROWSELOCATIONSDIALOGDIRCHOICE) |
|
114 |
|
115 self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
|
116 if wx.VERSION >= (2, 5, 0): |
|
117 self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId()) |
|
118 else: |
|
119 self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId()) |
|
120 |
|
121 self._init_sizers() |
|
122 |
|
123 def __init__(self, parent, var_type, locations): |
|
124 self._init_ctrls(parent) |
|
125 self.VarType = var_type |
|
126 self.Locations = locations |
|
127 |
|
128 # Define Tree item icon list |
|
129 self.TreeImageList = wx.ImageList(16, 16) |
|
130 self.TreeImageDict = {} |
|
131 |
|
132 # Icons for items |
|
133 for imgname, itemtype in [ |
|
134 ("CONFIGURATION", LOCATION_PLUGIN), |
|
135 ("RESOURCE", LOCATION_MODULE), |
|
136 ("PROGRAM", LOCATION_GROUP), |
|
137 ("VAR_INPUT", LOCATION_VAR_INPUT), |
|
138 ("VAR_OUTPUT", LOCATION_VAR_OUTPUT), |
|
139 ("VAR_LOCAL", LOCATION_VAR_MEMORY)]: |
|
140 self.TreeImageDict[itemtype]=self.TreeImageList.Add(wx.Bitmap(os.path.join(CWD, 'Images', '%s.png'%imgname))) |
|
141 |
|
142 # Assign icon list to TreeCtrls |
|
143 self.LocationsTree.SetImageList(self.TreeImageList) |
|
144 |
|
145 # Set a options for the choice |
|
146 for option, filter in GetDirChoiceOptions(): |
|
147 self.DirChoice.Append(_(option)) |
|
148 self.DirChoice.SetStringSelection(_("All")) |
|
149 self.RefreshFilter() |
|
150 |
|
151 self.RefreshLocationsTree() |
|
152 |
|
153 def RefreshFilter(self): |
|
154 self.Filter = DIRCHOICE_OPTIONS_FILTER[self.DirChoice.GetStringSelection()] |
|
155 |
|
156 def RefreshLocationsTree(self): |
|
157 root = self.LocationsTree.GetRootItem() |
|
158 if not root.IsOk(): |
|
159 if wx.Platform == '__WXMSW__': |
|
160 root = self.LocationsTree.AddRoot(_('Plugins')) |
|
161 else: |
|
162 root = self.LocationsTree.AddRoot("") |
|
163 self.GenerateLocationsTreeBranch(root, self.Locations) |
|
164 self.LocationsTree.Expand(root) |
|
165 |
|
166 def GenerateLocationsTreeBranch(self, root, locations): |
|
167 to_delete = [] |
|
168 if wx.VERSION >= (2, 6, 0): |
|
169 item, root_cookie = self.LocationsTree.GetFirstChild(root) |
|
170 else: |
|
171 item, root_cookie = self.LocationsTree.GetFirstChild(root, 0) |
|
172 for loc_infos in locations: |
|
173 infos = loc_infos.copy() |
|
174 if infos["type"] in [LOCATION_PLUGIN, LOCATION_MODULE, LOCATION_GROUP] or\ |
|
175 infos["type"] in self.Filter and (infos["IEC_type"] == self.VarType or |
|
176 infos["IEC_type"] is None and LOCATION_SIZES[self.VarType] == infos["size"]): |
|
177 children = [child for child in infos.pop("children")] |
|
178 if not item.IsOk(): |
|
179 item = self.LocationsTree.AppendItem(root, infos["name"]) |
|
180 if wx.Platform != '__WXMSW__': |
|
181 item, root_cookie = self.LocationsTree.GetNextChild(root, root_cookie) |
|
182 else: |
|
183 self.LocationsTree.SetItemText(item, infos["name"]) |
|
184 self.LocationsTree.SetPyData(item, infos) |
|
185 self.LocationsTree.SetItemImage(item, self.TreeImageDict[infos["type"]]) |
|
186 self.GenerateLocationsTreeBranch(item, children) |
|
187 item, root_cookie = self.LocationsTree.GetNextChild(root, root_cookie) |
|
188 while item.IsOk(): |
|
189 to_delete.append(item) |
|
190 item, root_cookie = self.LocationsTree.GetNextChild(root, root_cookie) |
|
191 for item in to_delete: |
|
192 self.LocationsTree.Delete(item) |
|
193 |
|
194 def OnLocationsTreeItemActivated(self, event): |
|
195 infos = self.LocationsTree.GetPyData(event.GetItem()) |
|
196 if infos["type"] not in [LOCATION_PLUGIN, LOCATION_MODULE, LOCATION_GROUP]: |
|
197 wx.CallAfter(self.EndModal, wx.ID_OK) |
|
198 event.Skip() |
|
199 |
|
200 def OnDirChoice(self, event): |
|
201 self.RefreshFilter() |
|
202 self.RefreshLocationsTree() |
|
203 |
|
204 def GetValues(self): |
|
205 selected = self.LocationsTree.GetSelection() |
|
206 return self.LocationsTree.GetPyData(selected) |
|
207 |
|
208 def OnOK(self, event): |
|
209 selected = self.LocationsTree.GetSelection() |
|
210 var_infos = None |
|
211 if selected.IsOk(): |
|
212 var_infos = self.LocationsTree.GetPyData(selected) |
|
213 if var_infos is None or var_infos["type"] in [LOCATION_PLUGIN, LOCATION_MODULE, LOCATION_GROUP]: |
|
214 message = wx.MessageDialog(self, _("A location must be selected!"), _("Error"), wx.OK|wx.ICON_ERROR) |
|
215 message.ShowModal() |
|
216 message.Destroy() |
|
217 else: |
|
218 self.EndModal(wx.ID_OK) |