|
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) 2007: 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 re |
|
26 |
|
27 import wx |
|
28 |
|
29 RE_ESCAPED_CHARACTERS = ".*+()[]?:|{}^$<>=-," |
|
30 |
|
31 def EscapeText(text): |
|
32 text = text.replace('\\', '\\\\') |
|
33 for c in RE_ESCAPED_CHARACTERS: |
|
34 text = text.replace(c, '\\' + c) |
|
35 return text |
|
36 |
|
37 #------------------------------------------------------------------------------- |
|
38 # Search In Project Dialog |
|
39 #------------------------------------------------------------------------------- |
|
40 |
|
41 def GetElementsChoices(): |
|
42 _ = lambda x: x |
|
43 return [("datatype", _("Data Type")), |
|
44 ("function", _("Function")), |
|
45 ("functionBlock", _("Function Block")), |
|
46 ("program", _("Program")), |
|
47 ("configuration", _("Configuration"))] |
|
48 |
|
49 [ID_SEARCHINPROJECTDIALOG, ID_SEARCHINPROJECTDIALOGPATTERNLABEL, |
|
50 ID_SEARCHINPROJECTDIALOGPATTERN, ID_SEARCHINPROJECTDIALOGCASESENSITIVE, |
|
51 ID_SEARCHINPROJECTDIALOGREGULAREXPRESSION, ID_SEARCHINPROJECTDIALOGSCOPESTATICBOX, |
|
52 ID_SEARCHINPROJECTDIALOGWHOLEPROJECT, ID_SEARCHINPROJECTDIALOGONLYELEMENTS, |
|
53 ID_SEARCHINPROJECTDIALOGELEMENTSLIST, |
|
54 ] = [wx.NewId() for _init_ctrls in range(9)] |
|
55 |
|
56 class SearchInProjectDialog(wx.Dialog): |
|
57 |
|
58 if wx.VERSION < (2, 6, 0): |
|
59 def Bind(self, event, function, id = None): |
|
60 if id is not None: |
|
61 event(self, id, function) |
|
62 else: |
|
63 event(self, function) |
|
64 |
|
65 def _init_coll_MainSizer_Items(self, parent): |
|
66 parent.AddSizer(self.PatternSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) |
|
67 parent.AddSizer(self.ScopeSizer, 0, border=20, flag=wx.GROW|wx.LEFT|wx.RIGHT) |
|
68 parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
|
69 |
|
70 def _init_coll_MainSizer_Growables(self, parent): |
|
71 parent.AddGrowableCol(0) |
|
72 parent.AddGrowableRow(1) |
|
73 |
|
74 def _init_coll_PatternSizer_Items(self, parent): |
|
75 parent.AddWindow(self.PatternLabel, 0, border=0, flag=wx.ALIGN_BOTTOM) |
|
76 parent.AddWindow(self.CaseSensitive, 0, border=0, flag=wx.GROW) |
|
77 parent.AddWindow(self.Pattern, 0, border=0, flag=wx.GROW) |
|
78 parent.AddWindow(self.RegularExpression, 0, border=0, flag=wx.GROW) |
|
79 |
|
80 def _init_coll_PatternSizer_Growables(self, parent): |
|
81 parent.AddGrowableCol(0) |
|
82 |
|
83 def _init_coll_ScopeSizer_Items(self, parent): |
|
84 parent.AddSizer(self.ScopeSelectionSizer, 1, border=5, flag=wx.GROW|wx.TOP|wx.LEFT|wx.BOTTOM) |
|
85 parent.AddWindow(self.ElementsList, 1, border=5, flag=wx.GROW|wx.TOP|wx.RIGHT|wx.BOTTOM) |
|
86 |
|
87 def _init_coll_ScopeSelectionSizer_Items(self, parent): |
|
88 parent.AddWindow(self.WholeProject, 0, border=5, flag=wx.GROW|wx.BOTTOM) |
|
89 parent.AddWindow(self.OnlyElements, 0, border=0, flag=wx.GROW) |
|
90 |
|
91 def _init_sizers(self): |
|
92 self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) |
|
93 self.PatternSizer = wx.FlexGridSizer(cols=2, hgap=5, rows=2, vgap=5) |
|
94 self.ScopeSizer = wx.StaticBoxSizer(self.ScopeStaticBox, wx.HORIZONTAL) |
|
95 self.ScopeSelectionSizer = wx.BoxSizer(wx.VERTICAL) |
|
96 |
|
97 self._init_coll_MainSizer_Items(self.MainSizer) |
|
98 self._init_coll_MainSizer_Growables(self.MainSizer) |
|
99 self._init_coll_PatternSizer_Items(self.PatternSizer) |
|
100 self._init_coll_PatternSizer_Growables(self.PatternSizer) |
|
101 self._init_coll_ScopeSizer_Items(self.ScopeSizer) |
|
102 self._init_coll_ScopeSelectionSizer_Items(self.ScopeSelectionSizer) |
|
103 |
|
104 self.SetSizer(self.MainSizer) |
|
105 |
|
106 def _init_ctrls(self, prnt): |
|
107 wx.Dialog.__init__(self, id=ID_SEARCHINPROJECTDIALOG, |
|
108 name='SearchInProjectDialog', parent=prnt, |
|
109 size=wx.Size(600, 300), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER, |
|
110 title=_('Search in Project')) |
|
111 |
|
112 self.PatternLabel = wx.StaticText(id=ID_SEARCHINPROJECTDIALOGPATTERNLABEL, |
|
113 label=_('Pattern to search:'), name='PatternLabel', parent=self, |
|
114 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
115 |
|
116 self.Pattern = wx.TextCtrl(id=ID_SEARCHINPROJECTDIALOGPATTERN, |
|
117 name='Pattern', parent=self, pos=wx.Point(0, 0), |
|
118 size=wx.Size(0, 24), style=0) |
|
119 |
|
120 self.CaseSensitive = wx.CheckBox(id=ID_SEARCHINPROJECTDIALOGCASESENSITIVE, |
|
121 label=_('Case sensitive'), name='CaseSensitive', parent=self, |
|
122 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
123 |
|
124 self.RegularExpression = wx.CheckBox(id=ID_SEARCHINPROJECTDIALOGREGULAREXPRESSION, |
|
125 label=_('Regular expression'), name='RegularExpression', parent=self, |
|
126 pos=wx.Point(0, 0), size=wx.DefaultSize, style=0) |
|
127 |
|
128 self.ScopeStaticBox = wx.StaticBox(id=ID_SEARCHINPROJECTDIALOGSCOPESTATICBOX, |
|
129 label=_('Scope'), name='ScopeStaticBox', parent=self, |
|
130 pos=wx.Point(0, 0), size=wx.Size(0, 0), style=0) |
|
131 |
|
132 self.WholeProject = wx.RadioButton(id=ID_SEARCHINPROJECTDIALOGWHOLEPROJECT, |
|
133 label=_('Whole Project'), name='WholeProject', parent=self, |
|
134 pos=wx.Point(0, 0), size=wx.Size(0, 24), style=wx.RB_GROUP) |
|
135 self.Bind(wx.EVT_RADIOBUTTON, self.OnScopeChanged, id=ID_SEARCHINPROJECTDIALOGWHOLEPROJECT) |
|
136 self.WholeProject.SetValue(True) |
|
137 |
|
138 self.OnlyElements = wx.RadioButton(id=ID_SEARCHINPROJECTDIALOGONLYELEMENTS, |
|
139 label=_('Only Elements'), name='OnlyElements', parent=self, |
|
140 pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0) |
|
141 self.Bind(wx.EVT_RADIOBUTTON, self.OnScopeChanged, id=ID_SEARCHINPROJECTDIALOGONLYELEMENTS) |
|
142 self.OnlyElements.SetValue(False) |
|
143 |
|
144 self.ElementsList = wx.CheckListBox(id=ID_SEARCHINPROJECTDIALOGELEMENTSLIST, |
|
145 name='ElementsList', parent=self, pos=wx.Point(0, 0), |
|
146 size=wx.Size(0, 0), style=0) |
|
147 self.ElementsList.Enable(False) |
|
148 |
|
149 self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
|
150 if wx.VERSION >= (2, 5, 0): |
|
151 ok_button = self.ButtonSizer.GetAffirmativeButton() |
|
152 else: |
|
153 ok_button = self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow() |
|
154 ok_button.SetLabel(_('Search')) |
|
155 self.Bind(wx.EVT_BUTTON, self.OnOK, id=ok_button.GetId()) |
|
156 |
|
157 self._init_sizers() |
|
158 |
|
159 def __init__(self, parent): |
|
160 self._init_ctrls(parent) |
|
161 |
|
162 for name, label in GetElementsChoices(): |
|
163 self.ElementsList.Append(_(label)) |
|
164 |
|
165 def GetCriteria(self): |
|
166 raw_pattern = self.Pattern.GetValue() |
|
167 if not self.CaseSensitive.GetValue(): |
|
168 pattern = raw_pattern.upper() |
|
169 if not self.RegularExpression.GetValue(): |
|
170 pattern = EscapeText(raw_pattern) |
|
171 criteria = { |
|
172 "raw_pattern": raw_pattern, |
|
173 "pattern": re.compile(pattern), |
|
174 "case_sensitive": self.CaseSensitive.GetValue(), |
|
175 "regular_expression": self.RegularExpression.GetValue(), |
|
176 } |
|
177 if self.WholeProject.GetValue(): |
|
178 criteria["filter"] = "all" |
|
179 elif self.OnlyElements.GetValue(): |
|
180 criteria["filter"] = [] |
|
181 for index, (name, label) in enumerate(GetElementsChoices()): |
|
182 if self.ElementsList.IsChecked(index): |
|
183 criteria["filter"].append(name) |
|
184 return criteria |
|
185 |
|
186 def OnScopeChanged(self, event): |
|
187 self.ElementsList.Enable(self.OnlyElements.GetValue()) |
|
188 event.Skip() |
|
189 |
|
190 def OnOK(self, event): |
|
191 if self.Pattern.GetValue() == "": |
|
192 message = wx.MessageDialog(self, _("Form isn't complete. Pattern to search must be filled!"), _("Error"), wx.OK|wx.ICON_ERROR) |
|
193 message.ShowModal() |
|
194 message.Destroy() |
|
195 else: |
|
196 wrong_pattern = False |
|
197 if self.RegularExpression.GetValue(): |
|
198 try: |
|
199 re.compile(self.Pattern.GetValue()) |
|
200 except: |
|
201 wrong_pattern = True |
|
202 if wrong_pattern: |
|
203 message = wx.MessageDialog(self, _("Syntax error in regular expression of pattern to search!"), _("Error"), wx.OK|wx.ICON_ERROR) |
|
204 message.ShowModal() |
|
205 message.Destroy() |
|
206 else: |
|
207 self.EndModal(wx.ID_OK) |