SearchResultPanel.py
author laurent
Sun, 01 Apr 2012 17:08:49 +0200
changeset 668 e858ff2f7862
parent 581 c2d96ea9c14a
child 714 131ea7f237b9
permissions -rw-r--r--
Fix cursor refresh when GraphicViewer canvas is resized
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
     1
#!/usr/bin/env python
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
     3
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
     5
#based on the plcopen standard. 
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
     6
#
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
     8
#
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
     9
#See COPYING file for copyrights details.
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    10
#
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    15
#
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    19
#General Public License for more details.
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    20
#
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    24
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    25
from types import TupleType
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    26
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    27
import wx
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    28
import wx.lib.agw.customtreectrl as CT
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    29
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    30
from PLCControler import *
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    31
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    32
CWD = os.path.split(os.path.realpath(__file__))[0]
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    33
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    34
def GenerateName(infos):
576
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
    35
    if infos[0] in ["input", "output", "value"]:
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    36
        return "%s %d:" % (infos[0], infos[1])
576
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
    37
    elif infos[0] == "range":
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
    38
        return "%s %d %s" % (infos[0], infos[1], infos[2])
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
    39
    elif infos[0] == "struct":
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
    40
        return "element %d %s" % (infos[1], infos[2])
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    41
    return "%s:" % infos[0]
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    42
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    43
#-------------------------------------------------------------------------------
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    44
#                            Search Result Panel
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    45
#-------------------------------------------------------------------------------
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    46
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    47
[ID_SEARCHRESULTPANEL, ID_SEARCHRESULTPANELHEADERLABEL,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    48
 ID_SEARCHRESULTPANELSEARCHRESULTSTREE, ID_SEARCHRESULTPANELRESETBUTTON, 
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    49
] = [wx.NewId() for _init_ctrls in range(4)]
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    50
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    51
class SearchResultPanel(wx.Panel):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    52
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    53
    if wx.VERSION < (2, 6, 0):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    54
        def Bind(self, event, function, id = None):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    55
            if id is not None:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    56
                event(self, id, function)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    57
            else:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    58
                event(self, function)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    59
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    60
    def _init_coll_MainSizer_Items(self, parent):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    61
        parent.AddSizer(self.HeaderSizer, 0, border=0, flag=wx.GROW)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    62
        parent.AddWindow(self.SearchResultsTree, 1, border=0, flag=wx.GROW)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    63
    
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    64
    def _init_coll_MainSizer_Growables(self, parent):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    65
        parent.AddGrowableCol(0)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    66
        parent.AddGrowableRow(1)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    67
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    68
    def _init_coll_HeaderSizer_Items(self, parent):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    69
        parent.AddWindow(self.HeaderLabel, 1, border=5, flag=wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER_VERTICAL)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    70
        parent.AddWindow(self.ResetButton, 0, border=0, flag=0)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    71
    
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    72
    def _init_coll_HeaderSizer_Growables(self, parent):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    73
        parent.AddGrowableCol(0)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    74
    
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    75
    def _init_sizers(self):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    76
        self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    77
        self.HeaderSizer = wx.BoxSizer(wx.HORIZONTAL)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    78
        
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    79
        self._init_coll_MainSizer_Items(self.MainSizer)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    80
        self._init_coll_MainSizer_Growables(self.MainSizer)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    81
        self._init_coll_HeaderSizer_Items(self.HeaderSizer)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    82
        
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    83
        self.SetSizer(self.MainSizer)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    84
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    85
    def _init_ctrls(self, prnt):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    86
        wx.Panel.__init__(self, id=ID_SEARCHRESULTPANEL,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    87
              name='SearchResultPanel', parent=prnt, pos=wx.Point(0, 0),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    88
              size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    89
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    90
        self.HeaderLabel = wx.StaticText(id=ID_SEARCHRESULTPANELHEADERLABEL,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    91
              name='HeaderLabel', parent=self,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    92
              pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
574
8c525217ef1f Fix bug with usage of CustomTreeCtrl method unavailable on wx older versions
laurent
parents: 572
diff changeset
    93
        
8c525217ef1f Fix bug with usage of CustomTreeCtrl method unavailable on wx older versions
laurent
parents: 572
diff changeset
    94
        search_results_tree_style = CT.TR_HAS_BUTTONS|CT.TR_NO_LINES|CT.TR_HAS_VARIABLE_ROW_HEIGHT
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    95
        self.SearchResultsTree = CT.CustomTreeCtrl(id=ID_SEARCHRESULTPANELSEARCHRESULTSTREE,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
    96
              name="SearchResultsTree", parent=self,
574
8c525217ef1f Fix bug with usage of CustomTreeCtrl method unavailable on wx older versions
laurent
parents: 572
diff changeset
    97
              pos=wx.Point(0, 0), style=search_results_tree_style)
8c525217ef1f Fix bug with usage of CustomTreeCtrl method unavailable on wx older versions
laurent
parents: 572
diff changeset
    98
        if wx.VERSION >= (2, 8, 11):
8c525217ef1f Fix bug with usage of CustomTreeCtrl method unavailable on wx older versions
laurent
parents: 572
diff changeset
    99
            self.SearchResultsTree.SetAGWWindowStyleFlag(search_results_tree_style)
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   100
        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnSearchResultsTreeItemActivated,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   101
              id=ID_SEARCHRESULTPANELSEARCHRESULTSTREE)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   102
        
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   103
        self.ResetButton = wx.Button(id=ID_SEARCHRESULTPANELRESETBUTTON, label=_('Reset'),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   104
              name='ResetButton', parent=self, pos=wx.Point(0, 0),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   105
              size=wx.DefaultSize, style=0)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   106
        self.Bind(wx.EVT_BUTTON, self.OnResetButton, 
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   107
              id=ID_SEARCHRESULTPANELRESETBUTTON)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   108
        
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   109
        self._init_sizers()
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   110
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   111
    def __init__(self, parent, window):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   112
        self._init_ctrls(parent)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   113
        self.ParentWindow = window
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   114
        
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   115
        # Define Tree item icon list
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   116
        self.TreeImageList = wx.ImageList(16, 16)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   117
        self.TreeImageDict = {}
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   118
        
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   119
        # Icons for other items
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   120
        for imgname, itemtype in [
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   121
            #editables
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   122
            ("PROJECT",        ITEM_PROJECT),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   123
            ("TRANSITION",     ITEM_TRANSITION),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   124
            ("ACTION",         ITEM_ACTION),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   125
            ("CONFIGURATION",  ITEM_CONFIGURATION),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   126
            ("RESOURCE",       ITEM_RESOURCE),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   127
            ("DATATYPE",       ITEM_DATATYPE),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   128
            ("ACTION",         "action_block"),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   129
            ("IL",             "IL"),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   130
            ("ST",             "ST")]:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   131
            self.TreeImageDict[itemtype]=self.TreeImageList.Add(wx.Bitmap(os.path.join(CWD, 'Images', '%s.png'%imgname)))
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   132
        
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   133
        for itemtype in ["function", "functionBlock", "program",
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   134
                         "comment", "block", "io_variable",
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   135
                         "connector", "contact", "coil",
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   136
                         "step", "transition", "jump", 
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   137
                         "var_local", "var_input", 
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   138
                         "var_inout", "var_output"]:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   139
            self.TreeImageDict[itemtype]=self.TreeImageList.Add(wx.Bitmap(os.path.join(CWD, 'Images', '%s.png'%itemtype.upper())))
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   140
        
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   141
        # Assign icon list to TreeCtrl
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   142
        self.SearchResultsTree.SetImageList(self.TreeImageList)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   143
        
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   144
        self.ResetSearchResults()
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   145
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   146
    def SetSearchResults(self, criteria, search_results):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   147
        self.Criteria = criteria
571
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   148
        self.SearchResults = {}
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   149
        self.ElementsOrder = []
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   150
        
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   151
        for infos, start, end, text in search_results:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   152
            if infos[0] not in self.ElementsOrder:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   153
                self.ElementsOrder.append(infos[0])
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   154
            
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   155
            results = self.SearchResults.setdefault(infos[0], [])
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   156
            results.append((infos, start, end, text))
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   157
        
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   158
        self.RefreshView()
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   159
    
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   160
    def ResetSearchResults(self):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   161
        self.Criteria = None
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   162
        self.ElementsOrder = []
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   163
        self.SearchResults = {}
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   164
        self.RefreshView()
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   165
    
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   166
    def RefreshView(self):
571
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   167
        self.SearchResultsTree.DeleteAllItems()
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   168
        if self.Criteria is None:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   169
            self.HeaderLabel.SetLabel(_("No search results available."))
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   170
            self.ResetButton.Enable(False)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   171
        else:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   172
            matches_number = 0
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   173
            search_results_tree_infos = {"name": _("Project '%s':") % self.ParentWindow.Controler.GetProjectName(),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   174
                                         "type": ITEM_PROJECT,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   175
                                         "data": None,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   176
                                         "text": None,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   177
                                         "matches": None,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   178
                                        }
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   179
            search_results_tree_children = search_results_tree_infos.setdefault("children", [])
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   180
            for tagname in self.ElementsOrder:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   181
                results = self.SearchResults.get(tagname, [])
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   182
                matches_number += len(results)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   183
                
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   184
                words = tagname.split("::")
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   185
                
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   186
                element_type = self.ParentWindow.Controler.GetElementType(tagname)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   187
                if element_type == ITEM_POU:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   188
                    element_type = self.ParentWindow.Controler.GetPouType(words[1])
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   189
                
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   190
                element_infos = {"name": words[-1],
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   191
                                 "type": element_type,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   192
                                 "data": tagname,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   193
                                 "text": None,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   194
                                 "matches": len(results)}
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   195
                
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   196
                children = element_infos.setdefault("children", [])
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   197
                for infos, start, end, text in results:
576
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   198
                    if element_type == ITEM_DATATYPE:
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   199
                        child_name = GenerateName(infos[1:])
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   200
                        child_type = element_type
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   201
                    else:
581
c2d96ea9c14a Fix bug while matching pattern in the definition of resource elements like task or POU instance
laurent
parents: 576
diff changeset
   202
                        if element_type == ITEM_RESOURCE:
c2d96ea9c14a Fix bug while matching pattern in the definition of resource elements like task or POU instance
laurent
parents: 576
diff changeset
   203
                            child_type = element_type
c2d96ea9c14a Fix bug while matching pattern in the definition of resource elements like task or POU instance
laurent
parents: 576
diff changeset
   204
                        else:
c2d96ea9c14a Fix bug while matching pattern in the definition of resource elements like task or POU instance
laurent
parents: 576
diff changeset
   205
                            child_type = infos[1]
576
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   206
                        if child_type == "body":
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   207
                            child_name = "body"
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   208
                            if element_type == ITEM_TRANSITION:
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   209
                                child_type = self.ParentWindow.Controler.GetTransitionBodyType(words[1], words[2])
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   210
                            elif element_type == ITEM_ACTION:
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   211
                                child_type = self.ParentWindow.Controler.GetActionBodyType(words[1], words[2])
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   212
                            else:
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   213
                                child_type = self.ParentWindow.Controler.GetPouBodyType(words[1])
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   214
                        else:
576
3f2024f30553 Adding support for searching pattern in datatype elements
laurent
parents: 574
diff changeset
   215
                            child_name = GenerateName(infos[3:])
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   216
                    child_infos = {"name": child_name,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   217
                                   "type": child_type,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   218
                                   "data": (infos, start, end ,None),
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   219
                                   "text": text,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   220
                                   "matches": 1,
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   221
                                   "children": [],
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   222
                                  }
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   223
                    children.append(child_infos)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   224
                
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   225
                if len(words) > 2:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   226
                    for _element_infos in search_results_tree_children:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   227
                        if _element_infos["name"] == words[1]:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   228
                            _element_infos["matches"] += len(children)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   229
                            _element_infos["children"].append(element_infos)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   230
                            break
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   231
                else:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   232
                    search_results_tree_children.append(element_infos)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   233
            
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   234
            if matches_number < 2:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   235
                header_format = _("'%s' - %d match in project")
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   236
            else:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   237
                header_format = _("'%s' - %d matches in project")
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   238
            
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   239
            self.HeaderLabel.SetLabel(header_format % (self.Criteria["raw_pattern"], matches_number))
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   240
            self.ResetButton.Enable(True)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   241
            
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   242
            if matches_number > 0:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   243
                root = self.SearchResultsTree.GetRootItem()
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   244
                if root is None:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   245
                    root = self.SearchResultsTree.AddRoot(search_results_tree_infos["name"])
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   246
                self.GenerateSearchResultsTreeBranch(root, search_results_tree_infos)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   247
                self.SearchResultsTree.Expand(root)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   248
    
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   249
    def GetTextCtrlClickFunction(self, item):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   250
        def OnTextCtrlClick(event):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   251
            self.SearchResultsTree.SelectItem(item)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   252
            event.Skip()
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   253
        return OnTextCtrlClick
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   254
    
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   255
    def GetTextCtrlDClickFunction(self, item):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   256
        def OnTextCtrlDClick(event):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   257
            self.ShowSearchResults(item)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   258
            event.Skip()
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   259
        return OnTextCtrlDClick
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   260
    
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   261
    def GenerateSearchResultsTreeBranch(self, root, infos):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   262
        to_delete = []
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   263
        if infos["name"] == "body":
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   264
            item_name = "%d:" % infos["data"][1][0]
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   265
        else:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   266
            item_name = infos["name"]
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   267
        
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   268
        self.SearchResultsTree.SetItemText(root, item_name)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   269
        self.SearchResultsTree.SetPyData(root, infos["data"])
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   270
        self.SearchResultsTree.SetItemBackgroundColour(root, wx.WHITE)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   271
        self.SearchResultsTree.SetItemTextColour(root, wx.BLACK)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   272
        if infos["type"] is not None:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   273
            if infos["type"] == ITEM_POU:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   274
                self.SearchResultsTree.SetItemImage(root, self.TreeImageDict[self.ParentWindow.Controler.GetPouType(infos["name"])])
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   275
            else:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   276
                self.SearchResultsTree.SetItemImage(root, self.TreeImageDict[infos["type"]])
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   277
        
571
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   278
        text = None
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   279
        if infos["text"] is not None:
571
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   280
            text = infos["text"]
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   281
            start, end = infos["data"][1:3]
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   282
            text_lines = infos["text"].splitlines()
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   283
            start_idx = start[1]
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   284
            end_idx = reduce(lambda x, y: x + y, map(lambda x: len(x) + 1, text_lines[:end[0] - start[0]]), end[1] + 1)
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   285
            style = wx.TextAttr(wx.BLACK, wx.Colour(206, 204, 247))
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   286
        elif infos["type"] is not None and infos["matches"] > 1:
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   287
            text = _("(%d matches)") % infos["matches"]
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   288
            start_idx, end_idx = 0, len(text)
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   289
            style = wx.TextAttr(wx.Colour(0, 127, 174))
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   290
        
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   291
        if text is not None:
572
1af3cc2b207c Fix search in project feature for latest version of wx on Windows
laurent
parents: 571
diff changeset
   292
            text_ctrl_style = wx.BORDER_NONE|wx.TE_READONLY|wx.TE_RICH2
1af3cc2b207c Fix search in project feature for latest version of wx on Windows
laurent
parents: 571
diff changeset
   293
            if wx.Platform != '__WXMSW__' or len(text.splitlines()) > 1:
1af3cc2b207c Fix search in project feature for latest version of wx on Windows
laurent
parents: 571
diff changeset
   294
                text_ctrl_style |= wx.TE_MULTILINE
1af3cc2b207c Fix search in project feature for latest version of wx on Windows
laurent
parents: 571
diff changeset
   295
            text_ctrl = wx.TextCtrl(id=-1, parent=self.SearchResultsTree, pos=wx.Point(0, 0), 
1af3cc2b207c Fix search in project feature for latest version of wx on Windows
laurent
parents: 571
diff changeset
   296
                    value=text, style=text_ctrl_style)
1af3cc2b207c Fix search in project feature for latest version of wx on Windows
laurent
parents: 571
diff changeset
   297
            width, height = text_ctrl.GetTextExtent(text)
1af3cc2b207c Fix search in project feature for latest version of wx on Windows
laurent
parents: 571
diff changeset
   298
            text_ctrl.SetClientSize(wx.Size(width + 1, height))
1af3cc2b207c Fix search in project feature for latest version of wx on Windows
laurent
parents: 571
diff changeset
   299
            text_ctrl.SetBackgroundColour(self.SearchResultsTree.GetBackgroundColour())
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   300
            text_ctrl.Bind(wx.EVT_LEFT_DOWN, self.GetTextCtrlClickFunction(root))
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   301
            text_ctrl.Bind(wx.EVT_LEFT_DCLICK, self.GetTextCtrlDClickFunction(root))
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   302
            text_ctrl.SetInsertionPoint(0)
571
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   303
            text_ctrl.SetStyle(start_idx, end_idx, style)
572
1af3cc2b207c Fix search in project feature for latest version of wx on Windows
laurent
parents: 571
diff changeset
   304
            self.SearchResultsTree.SetItemWindow(root, text_ctrl)
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   305
            
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   306
        if wx.VERSION >= (2, 6, 0):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   307
            item, root_cookie = self.SearchResultsTree.GetFirstChild(root)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   308
        else:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   309
            item, root_cookie = self.SearchResultsTree.GetFirstChild(root, 0)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   310
        for child in infos["children"]:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   311
            if item is None:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   312
                item = self.SearchResultsTree.AppendItem(root, "")
571
79af7b821233 Fixing search in project feature for Windows
laurent
parents: 566
diff changeset
   313
                item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie)
566
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   314
            self.GenerateSearchResultsTreeBranch(item, child)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   315
            item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   316
    
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   317
    def ShowSearchResults(self, item):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   318
        data = self.SearchResultsTree.GetPyData(item)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   319
        if isinstance(data, TupleType):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   320
            search_results = [data]
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   321
        else:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   322
            search_results = self.SearchResults.get(data, [])
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   323
        for infos, start, end, text in search_results:
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   324
            self.ParentWindow.ShowSearchResult(infos, start, end)
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   325
    
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   326
    def OnSearchResultsTreeItemActivated(self, event):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   327
        self.ShowSearchResults(event.GetItem())
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   328
        event.Skip()
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   329
    
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   330
    def OnResetButton(self, event):
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   331
        self.ResetSearchResults()
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   332
        self.ParentWindow.ClearSearchResults()
6014ef82a98a Adding support for searching text or regular expression in whole project
laurent
parents:
diff changeset
   333
        event.Skip()