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