laurent@566: #!/usr/bin/env python
laurent@566: # -*- coding: utf-8 -*-
laurent@566: 
laurent@566: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
laurent@566: #based on the plcopen standard. 
laurent@566: #
laurent@566: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
laurent@566: #
laurent@566: #See COPYING file for copyrights details.
laurent@566: #
laurent@566: #This library is free software; you can redistribute it and/or
laurent@566: #modify it under the terms of the GNU General Public
laurent@566: #License as published by the Free Software Foundation; either
laurent@566: #version 2.1 of the License, or (at your option) any later version.
laurent@566: #
laurent@566: #This library is distributed in the hope that it will be useful,
laurent@566: #but WITHOUT ANY WARRANTY; without even the implied warranty of
laurent@566: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
laurent@566: #General Public License for more details.
laurent@566: #
laurent@566: #You should have received a copy of the GNU General Public
laurent@566: #License along with this library; if not, write to the Free Software
laurent@566: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
laurent@566: 
laurent@566: import re
laurent@566: 
laurent@566: import wx
laurent@566: 
laurent@566: RE_ESCAPED_CHARACTERS = ".*+()[]?:|{}^$<>=-,"
laurent@566: 
laurent@566: def EscapeText(text):
laurent@566:     text = text.replace('\\', '\\\\')
laurent@566:     for c in RE_ESCAPED_CHARACTERS:
laurent@566:         text = text.replace(c, '\\' + c)
laurent@566:     return text
laurent@566: 
laurent@566: #-------------------------------------------------------------------------------
laurent@566: #                          Search In Project Dialog
laurent@566: #-------------------------------------------------------------------------------
laurent@566: 
laurent@566: def GetElementsChoices():
laurent@566:     _ = lambda x: x
laurent@566:     return [("datatype", _("Data Type")), 
laurent@566:             ("function", _("Function")), 
laurent@566:             ("functionBlock", _("Function Block")), 
laurent@566:             ("program", _("Program")), 
laurent@566:             ("configuration", _("Configuration"))]
laurent@566: 
laurent@566: [ID_SEARCHINPROJECTDIALOG, ID_SEARCHINPROJECTDIALOGPATTERNLABEL, 
laurent@566:  ID_SEARCHINPROJECTDIALOGPATTERN, ID_SEARCHINPROJECTDIALOGCASESENSITIVE,
laurent@566:  ID_SEARCHINPROJECTDIALOGREGULAREXPRESSION, ID_SEARCHINPROJECTDIALOGSCOPESTATICBOX, 
laurent@566:  ID_SEARCHINPROJECTDIALOGWHOLEPROJECT, ID_SEARCHINPROJECTDIALOGONLYELEMENTS,
laurent@566:  ID_SEARCHINPROJECTDIALOGELEMENTSLIST,
laurent@566: ] = [wx.NewId() for _init_ctrls in range(9)]
laurent@566: 
laurent@566: class SearchInProjectDialog(wx.Dialog):
laurent@566:     
laurent@566:     if wx.VERSION < (2, 6, 0):
laurent@566:         def Bind(self, event, function, id = None):
laurent@566:             if id is not None:
laurent@566:                 event(self, id, function)
laurent@566:             else:
laurent@566:                 event(self, function)
laurent@566:     
laurent@566:     def _init_coll_MainSizer_Items(self, parent):
laurent@566:         parent.AddSizer(self.PatternSizer, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
laurent@566:         parent.AddSizer(self.ScopeSizer, 0, border=20, flag=wx.GROW|wx.LEFT|wx.RIGHT)
laurent@566:         parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
laurent@566: 
laurent@566:     def _init_coll_MainSizer_Growables(self, parent):
laurent@566:         parent.AddGrowableCol(0)
laurent@566:         parent.AddGrowableRow(1)
laurent@566:     
laurent@566:     def _init_coll_PatternSizer_Items(self, parent):
laurent@566:         parent.AddWindow(self.PatternLabel, 0, border=0, flag=wx.ALIGN_BOTTOM)
laurent@566:         parent.AddWindow(self.CaseSensitive, 0, border=0, flag=wx.GROW)
laurent@566:         parent.AddWindow(self.Pattern, 0, border=0, flag=wx.GROW)
laurent@566:         parent.AddWindow(self.RegularExpression, 0, border=0, flag=wx.GROW)
laurent@566: 
laurent@566:     def _init_coll_PatternSizer_Growables(self, parent):
laurent@566:         parent.AddGrowableCol(0)
laurent@566:     
laurent@566:     def _init_coll_ScopeSizer_Items(self, parent):
laurent@566:         parent.AddSizer(self.ScopeSelectionSizer, 1, border=5, flag=wx.GROW|wx.TOP|wx.LEFT|wx.BOTTOM)
laurent@566:         parent.AddWindow(self.ElementsList, 1, border=5, flag=wx.GROW|wx.TOP|wx.RIGHT|wx.BOTTOM)
laurent@566:     
laurent@566:     def _init_coll_ScopeSelectionSizer_Items(self, parent):
laurent@566:         parent.AddWindow(self.WholeProject, 0, border=5, flag=wx.GROW|wx.BOTTOM)
laurent@566:         parent.AddWindow(self.OnlyElements, 0, border=0, flag=wx.GROW)
laurent@566:     
laurent@566:     def _init_sizers(self):
laurent@566:         self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
laurent@566:         self.PatternSizer = wx.FlexGridSizer(cols=2, hgap=5, rows=2, vgap=5)
laurent@566:         self.ScopeSizer = wx.StaticBoxSizer(self.ScopeStaticBox, wx.HORIZONTAL)
laurent@566:         self.ScopeSelectionSizer = wx.BoxSizer(wx.VERTICAL)
laurent@566:         
laurent@566:         self._init_coll_MainSizer_Items(self.MainSizer)
laurent@566:         self._init_coll_MainSizer_Growables(self.MainSizer)
laurent@566:         self._init_coll_PatternSizer_Items(self.PatternSizer)
laurent@566:         self._init_coll_PatternSizer_Growables(self.PatternSizer)
laurent@566:         self._init_coll_ScopeSizer_Items(self.ScopeSizer)
laurent@566:         self._init_coll_ScopeSelectionSizer_Items(self.ScopeSelectionSizer)
laurent@566:         
laurent@566:         self.SetSizer(self.MainSizer)
laurent@566:     
laurent@566:     def _init_ctrls(self, prnt):
laurent@566:         wx.Dialog.__init__(self, id=ID_SEARCHINPROJECTDIALOG,
laurent@566:               name='SearchInProjectDialog', parent=prnt,
laurent@566:               size=wx.Size(600, 300), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER,
laurent@566:               title=_('Search in Project'))
laurent@566:         
laurent@566:         self.PatternLabel = wx.StaticText(id=ID_SEARCHINPROJECTDIALOGPATTERNLABEL,
laurent@566:               label=_('Pattern to search:'), name='PatternLabel', parent=self,
laurent@566:               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
laurent@566: 
laurent@566:         self.Pattern = wx.TextCtrl(id=ID_SEARCHINPROJECTDIALOGPATTERN,
laurent@566:               name='Pattern', parent=self, pos=wx.Point(0, 0),
laurent@566:               size=wx.Size(0, 24), style=0)
laurent@566: 
laurent@566:         self.CaseSensitive = wx.CheckBox(id=ID_SEARCHINPROJECTDIALOGCASESENSITIVE,
laurent@566:               label=_('Case sensitive'), name='CaseSensitive', parent=self, 
laurent@566:               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
laurent@566:         
laurent@566:         self.RegularExpression = wx.CheckBox(id=ID_SEARCHINPROJECTDIALOGREGULAREXPRESSION,
laurent@566:               label=_('Regular expression'), name='RegularExpression', parent=self, 
laurent@566:               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
laurent@566:         
laurent@566:         self.ScopeStaticBox = wx.StaticBox(id=ID_SEARCHINPROJECTDIALOGSCOPESTATICBOX,
laurent@566:               label=_('Scope'), name='ScopeStaticBox', parent=self,
laurent@566:               pos=wx.Point(0, 0), size=wx.Size(0, 0), style=0)
laurent@566:         
laurent@566:         self.WholeProject = wx.RadioButton(id=ID_SEARCHINPROJECTDIALOGWHOLEPROJECT,
laurent@566:               label=_('Whole Project'), name='WholeProject', parent=self,
laurent@566:               pos=wx.Point(0, 0), size=wx.Size(0, 24), style=wx.RB_GROUP)
laurent@566:         self.Bind(wx.EVT_RADIOBUTTON, self.OnScopeChanged, id=ID_SEARCHINPROJECTDIALOGWHOLEPROJECT)
laurent@566:         self.WholeProject.SetValue(True)
laurent@566:         
laurent@566:         self.OnlyElements = wx.RadioButton(id=ID_SEARCHINPROJECTDIALOGONLYELEMENTS,
laurent@566:               label=_('Only Elements'), name='OnlyElements', parent=self,
laurent@566:               pos=wx.Point(0, 0), size=wx.Size(0, 24), style=0)
laurent@566:         self.Bind(wx.EVT_RADIOBUTTON, self.OnScopeChanged, id=ID_SEARCHINPROJECTDIALOGONLYELEMENTS)
laurent@566:         self.OnlyElements.SetValue(False)
laurent@566:         
laurent@566:         self.ElementsList = wx.CheckListBox(id=ID_SEARCHINPROJECTDIALOGELEMENTSLIST,
laurent@566:               name='ElementsList', parent=self, pos=wx.Point(0, 0), 
laurent@566:               size=wx.Size(0, 0), style=0)
laurent@566:         self.ElementsList.Enable(False)
laurent@566:         
laurent@566:         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
laurent@566:         if wx.VERSION >= (2, 5, 0):
laurent@566:             ok_button = self.ButtonSizer.GetAffirmativeButton()
laurent@566:         else:
laurent@566:             ok_button = self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow()
laurent@566:         ok_button.SetLabel(_('Search'))
laurent@566:         self.Bind(wx.EVT_BUTTON, self.OnOK, id=ok_button.GetId())
laurent@566:         
laurent@566:         self._init_sizers()
laurent@566: 
laurent@566:     def __init__(self, parent):
laurent@566:         self._init_ctrls(parent)
laurent@566: 
laurent@566:         for name, label in GetElementsChoices():
laurent@566:             self.ElementsList.Append(_(label))
laurent@577:         
laurent@577:         self.Pattern.SetFocus()
laurent@566: 
laurent@566:     def GetCriteria(self):
laurent@571:         raw_pattern = pattern = self.Pattern.GetValue()
laurent@566:         if not self.CaseSensitive.GetValue():
laurent@571:             pattern = pattern.upper()
laurent@566:         if not self.RegularExpression.GetValue():
laurent@571:             pattern = EscapeText(pattern)
laurent@566:         criteria = {
laurent@566:             "raw_pattern": raw_pattern, 
laurent@566:             "pattern": re.compile(pattern),
laurent@566:             "case_sensitive": self.CaseSensitive.GetValue(),
laurent@566:             "regular_expression": self.RegularExpression.GetValue(),
laurent@566:         }
laurent@566:         if self.WholeProject.GetValue():
laurent@566:             criteria["filter"] = "all"
laurent@566:         elif self.OnlyElements.GetValue():
laurent@566:             criteria["filter"] = []
laurent@566:             for index, (name, label) in enumerate(GetElementsChoices()):
laurent@566:                 if self.ElementsList.IsChecked(index):
laurent@566:                     criteria["filter"].append(name)
laurent@566:         return criteria
laurent@566:     
laurent@566:     def OnScopeChanged(self, event):
laurent@566:         self.ElementsList.Enable(self.OnlyElements.GetValue())
laurent@566:         event.Skip()
laurent@566:     
laurent@566:     def OnOK(self, event):
laurent@566:         if self.Pattern.GetValue() == "":
laurent@566:             message = wx.MessageDialog(self, _("Form isn't complete. Pattern to search must be filled!"), _("Error"), wx.OK|wx.ICON_ERROR)
laurent@566:             message.ShowModal()
laurent@566:             message.Destroy()
laurent@566:         else:
laurent@566:             wrong_pattern = False
laurent@566:             if self.RegularExpression.GetValue():
laurent@566:                 try:
laurent@566:                     re.compile(self.Pattern.GetValue())
laurent@566:                 except:
laurent@566:                     wrong_pattern = True
laurent@566:             if wrong_pattern:
laurent@566:                 message = wx.MessageDialog(self, _("Syntax error in regular expression of pattern to search!"), _("Error"), wx.OK|wx.ICON_ERROR)
laurent@566:                 message.ShowModal()
laurent@566:                 message.Destroy()
laurent@566:             else:
laurent@566:                 self.EndModal(wx.ID_OK)