|
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 class SearchInProjectDialog(wx.Dialog): |
|
50 |
|
51 def __init__(self, parent): |
|
52 wx.Dialog.__init__(self, parent, title=_('Search in Project'), |
|
53 size=wx.Size(600, 350)) |
|
54 |
|
55 main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10) |
|
56 main_sizer.AddGrowableCol(0) |
|
57 main_sizer.AddGrowableRow(1) |
|
58 |
|
59 pattern_sizer = wx.FlexGridSizer(cols=2, hgap=5, rows=2, vgap=5) |
|
60 pattern_sizer.AddGrowableCol(0) |
|
61 main_sizer.AddSizer(pattern_sizer, border=20, |
|
62 flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT) |
|
63 |
|
64 pattern_label = wx.StaticText(self, label=_('Pattern to search:')) |
|
65 pattern_sizer.AddWindow(pattern_label, flag=wx.ALIGN_BOTTOM) |
|
66 |
|
67 self.CaseSensitive = wx.CheckBox(self, label=_('Case sensitive')) |
|
68 pattern_sizer.AddWindow(self.CaseSensitive, flag=wx.GROW) |
|
69 |
|
70 self.Pattern = wx.TextCtrl(self) |
|
71 pattern_sizer.AddWindow(self.Pattern, flag=wx.GROW) |
|
72 |
|
73 self.RegularExpression = wx.CheckBox(self, label=_('Regular expression')) |
|
74 pattern_sizer.AddWindow(self.RegularExpression, flag=wx.GROW) |
|
75 |
|
76 scope_staticbox = wx.StaticBox(self, label=_('Scope')) |
|
77 scope_sizer = wx.StaticBoxSizer(scope_staticbox, wx.HORIZONTAL) |
|
78 main_sizer.AddSizer(scope_sizer, border=20, |
|
79 flag=wx.GROW|wx.LEFT|wx.RIGHT) |
|
80 |
|
81 scope_selection_sizer = wx.BoxSizer(wx.VERTICAL) |
|
82 scope_sizer.AddSizer(scope_selection_sizer, 1, border=5, |
|
83 flag=wx.GROW|wx.TOP|wx.LEFT|wx.BOTTOM) |
|
84 |
|
85 self.WholeProject = wx.RadioButton(self, label=_('Whole Project'), |
|
86 size=wx.Size(0, 24), style=wx.RB_GROUP) |
|
87 self.WholeProject.SetValue(True) |
|
88 self.Bind(wx.EVT_RADIOBUTTON, self.OnScopeChanged, self.WholeProject) |
|
89 scope_selection_sizer.AddWindow(self.WholeProject, border=5, |
|
90 flag=wx.GROW|wx.BOTTOM) |
|
91 |
|
92 self.OnlyElements = wx.RadioButton(self, |
|
93 label=_('Only Elements'), size=wx.Size(0, 24)) |
|
94 self.Bind(wx.EVT_RADIOBUTTON, self.OnScopeChanged, self.OnlyElements) |
|
95 self.OnlyElements.SetValue(False) |
|
96 scope_selection_sizer.AddWindow(self.OnlyElements, flag=wx.GROW) |
|
97 |
|
98 self.ElementsList = wx.CheckListBox(self) |
|
99 self.ElementsList.Enable(False) |
|
100 scope_sizer.AddWindow(self.ElementsList, 1, border=5, |
|
101 flag=wx.GROW|wx.TOP|wx.RIGHT|wx.BOTTOM) |
|
102 |
|
103 self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE) |
|
104 ok_button = self.ButtonSizer.GetAffirmativeButton() |
|
105 ok_button.SetLabel(_('Search')) |
|
106 self.Bind(wx.EVT_BUTTON, self.OnOK, ok_button) |
|
107 main_sizer.AddSizer(self.ButtonSizer, border=20, |
|
108 flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT) |
|
109 |
|
110 self.SetSizer(main_sizer) |
|
111 |
|
112 for name, label in GetElementsChoices(): |
|
113 self.ElementsList.Append(_(label)) |
|
114 |
|
115 self.Pattern.SetFocus() |
|
116 |
|
117 def GetCriteria(self): |
|
118 raw_pattern = pattern = self.Pattern.GetValue() |
|
119 if not self.CaseSensitive.GetValue(): |
|
120 pattern = pattern.upper() |
|
121 if not self.RegularExpression.GetValue(): |
|
122 pattern = EscapeText(pattern) |
|
123 criteria = { |
|
124 "raw_pattern": raw_pattern, |
|
125 "pattern": re.compile(pattern), |
|
126 "case_sensitive": self.CaseSensitive.GetValue(), |
|
127 "regular_expression": self.RegularExpression.GetValue(), |
|
128 } |
|
129 if self.WholeProject.GetValue(): |
|
130 criteria["filter"] = "all" |
|
131 elif self.OnlyElements.GetValue(): |
|
132 criteria["filter"] = [] |
|
133 for index, (name, label) in enumerate(GetElementsChoices()): |
|
134 if self.ElementsList.IsChecked(index): |
|
135 criteria["filter"].append(name) |
|
136 return criteria |
|
137 |
|
138 def OnScopeChanged(self, event): |
|
139 self.ElementsList.Enable(self.OnlyElements.GetValue()) |
|
140 event.Skip() |
|
141 |
|
142 def OnOK(self, event): |
|
143 message = None |
|
144 if self.Pattern.GetValue() == "": |
|
145 message = _("Form isn't complete. Pattern to search must be filled!") |
|
146 else: |
|
147 wrong_pattern = False |
|
148 if self.RegularExpression.GetValue(): |
|
149 try: |
|
150 re.compile(self.Pattern.GetValue()) |
|
151 except: |
|
152 wrong_pattern = True |
|
153 if wrong_pattern: |
|
154 message = _("Syntax error in regular expression of pattern to search!") |
|
155 |
|
156 if message is not None: |
|
157 dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) |
|
158 dialog.ShowModal() |
|
159 dialog.Destroy() |
|
160 else: |
|
161 self.EndModal(wx.ID_OK) |