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