SearchResultPanel.py
changeset 571 79af7b821233
parent 566 6014ef82a98a
child 572 1af3cc2b207c
equal deleted inserted replaced
570:37ba389e5c01 571:79af7b821233
    87               name='HeaderLabel', parent=self,
    87               name='HeaderLabel', parent=self,
    88               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
    88               pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0)
    89 
    89 
    90         self.SearchResultsTree = CT.CustomTreeCtrl(id=ID_SEARCHRESULTPANELSEARCHRESULTSTREE,
    90         self.SearchResultsTree = CT.CustomTreeCtrl(id=ID_SEARCHRESULTPANELSEARCHRESULTSTREE,
    91               name="SearchResultsTree", parent=self,
    91               name="SearchResultsTree", parent=self,
    92               pos=wx.Point(0, 0), style=0)
    92               pos=wx.Point(0, 0), style=CT.TR_HAS_BUTTONS|CT.TR_NO_LINES|CT.TR_HAS_VARIABLE_ROW_HEIGHT)
    93         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnSearchResultsTreeItemActivated,
    93         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnSearchResultsTreeItemActivated,
    94               id=ID_SEARCHRESULTPANELSEARCHRESULTSTREE)
    94               id=ID_SEARCHRESULTPANELSEARCHRESULTSTREE)
    95         
    95         
    96         self.ResetButton = wx.Button(id=ID_SEARCHRESULTPANELRESETBUTTON, label=_('Reset'),
    96         self.ResetButton = wx.Button(id=ID_SEARCHRESULTPANELRESETBUTTON, label=_('Reset'),
    97               name='ResetButton', parent=self, pos=wx.Point(0, 0),
    97               name='ResetButton', parent=self, pos=wx.Point(0, 0),
   136         
   136         
   137         self.ResetSearchResults()
   137         self.ResetSearchResults()
   138 
   138 
   139     def SetSearchResults(self, criteria, search_results):
   139     def SetSearchResults(self, criteria, search_results):
   140         self.Criteria = criteria
   140         self.Criteria = criteria
       
   141         self.SearchResults = {}
       
   142         self.ElementsOrder = []
   141         
   143         
   142         for infos, start, end, text in search_results:
   144         for infos, start, end, text in search_results:
   143             if infos[0] not in self.ElementsOrder:
   145             if infos[0] not in self.ElementsOrder:
   144                 self.ElementsOrder.append(infos[0])
   146                 self.ElementsOrder.append(infos[0])
   145             
   147             
   153         self.ElementsOrder = []
   155         self.ElementsOrder = []
   154         self.SearchResults = {}
   156         self.SearchResults = {}
   155         self.RefreshView()
   157         self.RefreshView()
   156     
   158     
   157     def RefreshView(self):
   159     def RefreshView(self):
       
   160         self.SearchResultsTree.DeleteAllItems()
   158         if self.Criteria is None:
   161         if self.Criteria is None:
   159             self.HeaderLabel.SetLabel(_("No search results available."))
   162             self.HeaderLabel.SetLabel(_("No search results available."))
   160             self.SearchResultsTree.DeleteAllItems()
       
   161             self.ResetButton.Enable(False)
   163             self.ResetButton.Enable(False)
   162         else:
   164         else:
   163             matches_number = 0
   165             matches_number = 0
   164             search_results_tree_infos = {"name": _("Project '%s':") % self.ParentWindow.Controler.GetProjectName(),
   166             search_results_tree_infos = {"name": _("Project '%s':") % self.ParentWindow.Controler.GetProjectName(),
   165                                          "type": ITEM_PROJECT,
   167                                          "type": ITEM_PROJECT,
   257             if infos["type"] == ITEM_POU:
   259             if infos["type"] == ITEM_POU:
   258                 self.SearchResultsTree.SetItemImage(root, self.TreeImageDict[self.ParentWindow.Controler.GetPouType(infos["name"])])
   260                 self.SearchResultsTree.SetItemImage(root, self.TreeImageDict[self.ParentWindow.Controler.GetPouType(infos["name"])])
   259             else:
   261             else:
   260                 self.SearchResultsTree.SetItemImage(root, self.TreeImageDict[infos["type"]])
   262                 self.SearchResultsTree.SetItemImage(root, self.TreeImageDict[infos["type"]])
   261         
   263         
       
   264         text = None
   262         if infos["text"] is not None:
   265         if infos["text"] is not None:
   263             text_ctrl = wx.TextCtrl(id=-1, parent=self.SearchResultsTree, pos=wx.Point(0, 0), 
   266             text = infos["text"]
   264                     value=infos["text"], style=wx.BORDER_NONE|wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
   267             start, end = infos["data"][1:3]
   265             width, height = wx.PaintDC(text_ctrl).GetTextExtent(infos["text"])
   268             text_lines = infos["text"].splitlines()
   266             text_ctrl.SetClientSize(wx.Size(width + 1, height))
   269             start_idx = start[1]
   267             text_ctrl.SetBackgroundColour(self.SearchResultsTree.GetBackgroundColour())
   270             end_idx = reduce(lambda x, y: x + y, map(lambda x: len(x) + 1, text_lines[:end[0] - start[0]]), end[1] + 1)
       
   271             style = wx.TextAttr(wx.BLACK, wx.Colour(206, 204, 247))
       
   272         elif infos["type"] is not None and infos["matches"] > 1:
       
   273             text = _("(%d matches)") % infos["matches"]
       
   274             start_idx, end_idx = 0, len(text)
       
   275             style = wx.TextAttr(wx.Colour(0, 127, 174))
       
   276         
       
   277         if text is not None:
       
   278             background_colour = self.SearchResultsTree.GetBackgroundColour()
       
   279             if wx.Platform != '__WXMSW__':
       
   280                 text_ctrl = wx.TextCtrl(id=-1, parent=self.SearchResultsTree, pos=wx.Point(0, 0), 
       
   281                         value=text, style=wx.BORDER_NONE|wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
       
   282                 width, height = text_ctrl.GetTextExtent(text)
       
   283                 text_ctrl.SetClientSize(wx.Size(width + 1, height))
       
   284                 self.SearchResultsTree.SetItemWindow(root, text_ctrl)
       
   285             else:
       
   286                 panel = wx.Panel(id=-1, parent=self.SearchResultsTree)
       
   287                 panel.SetBackgroundColour(background_colour)
       
   288                 sizer = wx.BoxSizer(wx.HORIZONTAL)
       
   289                 if len(text.splitlines()) > 1:
       
   290                     text_ctrl = wx.TextCtrl(id=-1, parent=panel, pos=wx.Point(0, 0), 
       
   291                           value=text, style=wx.BORDER_NONE|wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
       
   292                 else:
       
   293                     text_ctrl = wx.TextCtrl(id=-1, parent=panel, pos=wx.Point(0, 0), 
       
   294                            value=text, size=wx.Size(0, 13), style=wx.BORDER_NONE|wx.TE_READONLY|wx.TE_RICH2)
       
   295                 width, height = text_ctrl.GetTextExtent(text)
       
   296                 sizer.AddWindow(text_ctrl, 1, border=0, flags=wx.ALIGN_CENTER_VERTICAL)
       
   297                 panel.SetSizer(sizer)
       
   298                 panel.SetClientSize(wx.Size(width + 1, height + 5))
       
   299                 self.SearchResultsTree.SetItemWindow(root, panel)
       
   300             
       
   301             text_ctrl.SetBackgroundColour(background_colour)
   268             text_ctrl.Bind(wx.EVT_LEFT_DOWN, self.GetTextCtrlClickFunction(root))
   302             text_ctrl.Bind(wx.EVT_LEFT_DOWN, self.GetTextCtrlClickFunction(root))
   269             text_ctrl.Bind(wx.EVT_LEFT_DCLICK, self.GetTextCtrlDClickFunction(root))
   303             text_ctrl.Bind(wx.EVT_LEFT_DCLICK, self.GetTextCtrlDClickFunction(root))
   270             start, end = infos["data"][1:3]
       
   271             text_lines = infos["text"].splitlines()
       
   272             end_idx = reduce(lambda x, y: x + y, map(lambda x: len(x) + 1, text_lines[:end[0] - start[0]]), end[1] + 1)
       
   273             text_ctrl.SetInsertionPoint(0)
   304             text_ctrl.SetInsertionPoint(0)
   274             text_ctrl.SetStyle(start[1], end_idx, wx.TextAttr(wx.BLACK, wx.Colour(206, 204, 247)))
   305             text_ctrl.SetStyle(start_idx, end_idx, style)
   275             
       
   276             self.SearchResultsTree.SetItemWindow(root, text_ctrl)
       
   277         
       
   278         elif infos["type"] is not None and infos["matches"] > 1:
       
   279             text = _("(%d matches)") % infos["matches"]
       
   280             text_ctrl = wx.TextCtrl(id=-1, parent=self.SearchResultsTree, pos=wx.Point(0, 0), 
       
   281                     value=text, style=wx.BORDER_NONE|wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
       
   282             width, height = wx.PaintDC(text_ctrl).GetTextExtent(text)
       
   283             text_ctrl.SetClientSize(wx.Size(width + 1, height))
       
   284             text_ctrl.SetBackgroundColour(self.SearchResultsTree.GetBackgroundColour())
       
   285             text_ctrl.Bind(wx.EVT_LEFT_DOWN, self.GetTextCtrlClickFunction(root))
       
   286             text_ctrl.Bind(wx.EVT_LEFT_DCLICK, self.GetTextCtrlDClickFunction(root))
       
   287             
       
   288             text_ctrl.SetInsertionPoint(0)
       
   289             text_ctrl.SetStyle(0, len(text), wx.TextAttr(wx.Colour(0, 127, 174)))
       
   290             
       
   291             self.SearchResultsTree.SetItemWindow(root, text_ctrl)
       
   292             
       
   293             
   306             
   294         if wx.VERSION >= (2, 6, 0):
   307         if wx.VERSION >= (2, 6, 0):
   295             item, root_cookie = self.SearchResultsTree.GetFirstChild(root)
   308             item, root_cookie = self.SearchResultsTree.GetFirstChild(root)
   296         else:
   309         else:
   297             item, root_cookie = self.SearchResultsTree.GetFirstChild(root, 0)
   310             item, root_cookie = self.SearchResultsTree.GetFirstChild(root, 0)
   298         for child in infos["children"]:
   311         for child in infos["children"]:
   299             if item is None:
   312             if item is None:
   300                 item = self.SearchResultsTree.AppendItem(root, "")
   313                 item = self.SearchResultsTree.AppendItem(root, "")
   301                 if wx.Platform != '__WXMSW__':
   314                 item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie)
   302                     item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie)
       
   303             self.GenerateSearchResultsTreeBranch(item, child)
   315             self.GenerateSearchResultsTreeBranch(item, child)
   304             item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie)
   316             item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie)
   305         while item is not None:
       
   306             to_delete.append(item)
       
   307             item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie)
       
   308         for item in to_delete:
       
   309             self.SearchResultsTree.Delete(item)
       
   310     
   317     
   311     def ShowSearchResults(self, item):
   318     def ShowSearchResults(self, item):
   312         data = self.SearchResultsTree.GetPyData(item)
   319         data = self.SearchResultsTree.GetPyData(item)
   313         if isinstance(data, TupleType):
   320         if isinstance(data, TupleType):
   314             search_results = [data]
   321             search_results = [data]