diff -r 31e63e25b4cc -r 64beb9e9c749 controls/SearchResultPanel.py --- a/controls/SearchResultPanel.py Mon Aug 21 20:17:19 2017 +0000 +++ b/controls/SearchResultPanel.py Mon Aug 21 23:22:58 2017 +0300 @@ -31,6 +31,7 @@ from PLCControler import * from util.BitmapLibrary import GetBitmap + def GenerateName(infos): if infos[0] in ["input", "output", "value"]: return "%s %d:" % (infos[0], infos[1]) @@ -40,18 +41,22 @@ return "element %d %s" % (infos[1], infos[2]) return "%s:" % infos[0] -#------------------------------------------------------------------------------- + +# ------------------------------------------------------------------------------- # Search Result Panel -#------------------------------------------------------------------------------- - -[ID_SEARCHRESULTPANEL, ID_SEARCHRESULTPANELHEADERLABEL, - ID_SEARCHRESULTPANELSEARCHRESULTSTREE, ID_SEARCHRESULTPANELRESETBUTTON, +# ------------------------------------------------------------------------------- + + +[ + ID_SEARCHRESULTPANEL, ID_SEARCHRESULTPANELHEADERLABEL, + ID_SEARCHRESULTPANELSEARCHRESULTSTREE, ID_SEARCHRESULTPANELRESETBUTTON, ] = [wx.NewId() for _init_ctrls in range(4)] + class SearchResultPanel(wx.Panel): if wx.VERSION < (2, 6, 0): - def Bind(self, event, function, id = None): + def Bind(self, event, function, id=None): if id is not None: event(self, id, function) else: @@ -60,109 +65,110 @@ def _init_coll_MainSizer_Items(self, parent): parent.AddSizer(self.HeaderSizer, 0, border=0, flag=wx.GROW) parent.AddWindow(self.SearchResultsTree, 1, border=0, flag=wx.GROW) - + def _init_coll_MainSizer_Growables(self, parent): parent.AddGrowableCol(0) parent.AddGrowableRow(1) def _init_coll_HeaderSizer_Items(self, parent): - parent.AddWindow(self.HeaderLabel, 1, border=5, flag=wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER_VERTICAL) + parent.AddWindow(self.HeaderLabel, 1, border=5, flag=wx.LEFT | wx.RIGHT | wx.ALIGN_CENTER_VERTICAL) parent.AddWindow(self.ResetButton, 0, border=0, flag=0) - + def _init_coll_HeaderSizer_Growables(self, parent): parent.AddGrowableCol(0) - + def _init_sizers(self): self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) self.HeaderSizer = wx.BoxSizer(wx.HORIZONTAL) - + self._init_coll_MainSizer_Items(self.MainSizer) self._init_coll_MainSizer_Growables(self.MainSizer) self._init_coll_HeaderSizer_Items(self.HeaderSizer) - + self.SetSizer(self.MainSizer) def _init_ctrls(self, prnt): wx.Panel.__init__(self, id=ID_SEARCHRESULTPANEL, - name='SearchResultPanel', parent=prnt, pos=wx.Point(0, 0), - size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL) + name='SearchResultPanel', parent=prnt, pos=wx.Point(0, 0), + size=wx.Size(0, 0), style=wx.TAB_TRAVERSAL) self.HeaderLabel = wx.StaticText(id=ID_SEARCHRESULTPANELHEADERLABEL, - name='HeaderLabel', parent=self, - pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) - - search_results_tree_style = CT.TR_HAS_BUTTONS|CT.TR_NO_LINES|CT.TR_HAS_VARIABLE_ROW_HEIGHT + name='HeaderLabel', parent=self, + pos=wx.Point(0, 0), size=wx.Size(0, 17), style=0) + + search_results_tree_style = CT.TR_HAS_BUTTONS | CT.TR_NO_LINES | CT.TR_HAS_VARIABLE_ROW_HEIGHT self.SearchResultsTree = CT.CustomTreeCtrl(id=ID_SEARCHRESULTPANELSEARCHRESULTSTREE, - name="SearchResultsTree", parent=self, - pos=wx.Point(0, 0), style=search_results_tree_style) + name="SearchResultsTree", parent=self, + pos=wx.Point(0, 0), style=search_results_tree_style) if wx.VERSION >= (2, 8, 11): self.SearchResultsTree.SetAGWWindowStyleFlag(search_results_tree_style) self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnSearchResultsTreeItemActivated, - id=ID_SEARCHRESULTPANELSEARCHRESULTSTREE) - - self.ResetButton = wx.lib.buttons.GenBitmapButton(self, - bitmap=GetBitmap("reset"), size=wx.Size(28, 28), style=wx.NO_BORDER) + id=ID_SEARCHRESULTPANELSEARCHRESULTSTREE) + + self.ResetButton = wx.lib.buttons.GenBitmapButton( + self, bitmap=GetBitmap("reset"), + size=wx.Size(28, 28), style=wx.NO_BORDER) self.ResetButton.SetToolTipString(_("Reset search result")) self.Bind(wx.EVT_BUTTON, self.OnResetButton, self.ResetButton) - + self._init_sizers() def __init__(self, parent, window): self.ParentWindow = window - + self._init_ctrls(parent) - + # Define Tree item icon list self.TreeImageList = wx.ImageList(16, 16) self.TreeImageDict = {} - + # Icons for other items for imgname, itemtype in [ - #editables - ("PROJECT", ITEM_PROJECT), - ("TRANSITION", ITEM_TRANSITION), - ("ACTION", ITEM_ACTION), - ("CONFIGURATION", ITEM_CONFIGURATION), - ("RESOURCE", ITEM_RESOURCE), - ("DATATYPE", ITEM_DATATYPE), - ("ACTION", "action_block"), - ("IL", "IL"), - ("ST", "ST")]: + # editables + ("PROJECT", ITEM_PROJECT), + ("TRANSITION", ITEM_TRANSITION), + ("ACTION", ITEM_ACTION), + ("CONFIGURATION", ITEM_CONFIGURATION), + ("RESOURCE", ITEM_RESOURCE), + ("DATATYPE", ITEM_DATATYPE), + ("ACTION", "action_block"), + ("IL", "IL"), + ("ST", "ST")]: self.TreeImageDict[itemtype] = self.TreeImageList.Add(GetBitmap(imgname)) - + for itemtype in ["function", "functionBlock", "program", "comment", "block", "io_variable", "connector", "contact", "coil", - "step", "transition", "jump", - "var_local", "var_input", + "step", "transition", "jump", + "var_local", "var_input", "var_inout", "var_output"]: self.TreeImageDict[itemtype] = self.TreeImageList.Add(GetBitmap(itemtype.upper())) - + # Assign icon list to TreeCtrl self.SearchResultsTree.SetImageList(self.TreeImageList) - + self.ResetSearchResults() def SetSearchResults(self, criteria, search_results): self.Criteria = criteria self.SearchResults = {} self.ElementsOrder = [] - + for infos, start, end, text in search_results: if infos[0] not in self.ElementsOrder: self.ElementsOrder.append(infos[0]) - + results = self.SearchResults.setdefault(infos[0], []) results.append((infos, start, end, text)) - + self.RefreshView() - + def ResetSearchResults(self): self.Criteria = None self.ElementsOrder = [] self.SearchResults = {} self.RefreshView() - + def RefreshView(self): self.SearchResultsTree.DeleteAllItems() if self.Criteria is None: @@ -170,29 +176,30 @@ self.ResetButton.Enable(False) else: matches_number = 0 - search_results_tree_infos = {"name": _("Project '%s':") % self.ParentWindow.Controler.GetProjectName(), - "type": ITEM_PROJECT, - "data": None, - "text": None, - "matches": None, - } + search_results_tree_infos = { + "name": _("Project '%s':") % self.ParentWindow.Controler.GetProjectName(), + "type": ITEM_PROJECT, + "data": None, + "text": None, + "matches": None, + } search_results_tree_children = search_results_tree_infos.setdefault("children", []) for tagname in self.ElementsOrder: results = self.SearchResults.get(tagname, []) matches_number += len(results) - + words = tagname.split("::") - + element_type = self.ParentWindow.Controler.GetElementType(tagname) if element_type == ITEM_POU: element_type = self.ParentWindow.Controler.GetPouType(words[1]) - + element_infos = {"name": words[-1], "type": element_type, "data": tagname, "text": None, "matches": len(results)} - + children = element_infos.setdefault("children", []) for infos, start, end, text in results: if infos[1] == "name" or element_type == ITEM_DATATYPE: @@ -215,15 +222,16 @@ child_type = self.ParentWindow.Controler.GetPouBodyType(words[1]) else: child_name = GenerateName(infos[3:]) - child_infos = {"name": child_name, - "type": child_type, - "data": (infos, start, end ,None), - "text": text, - "matches": 1, - "children": [], - } + child_infos = { + "name": child_name, + "type": child_type, + "data": (infos, start, end, None), + "text": text, + "matches": 1, + "children": [], + } children.append(child_infos) - + if len(words) > 2: for _element_infos in search_results_tree_children: if _element_infos["name"] == words[1]: @@ -234,41 +242,41 @@ search_results_tree_children.append(element_infos) else: search_results_tree_children.append(element_infos) - + if matches_number < 2: header_format = _("'{a1}' - {a2} match in project") else: header_format = _("'{a1}' - {a2} matches in project") - - self.HeaderLabel.SetLabel(header_format.format(a1 = self.Criteria["find_pattern"], a2 = matches_number)) + + self.HeaderLabel.SetLabel(header_format.format(a1=self.Criteria["find_pattern"], a2=matches_number)) self.ResetButton.Enable(True) - + if matches_number > 0: root = self.SearchResultsTree.GetRootItem() if root is None: root = self.SearchResultsTree.AddRoot(search_results_tree_infos["name"]) self.GenerateSearchResultsTreeBranch(root, search_results_tree_infos) self.SearchResultsTree.Expand(root) - + def GetTextCtrlClickFunction(self, item): def OnTextCtrlClick(event): self.SearchResultsTree.SelectItem(item) event.Skip() return OnTextCtrlClick - + def GetTextCtrlDClickFunction(self, item): def OnTextCtrlDClick(event): self.ShowSearchResults(item) event.Skip() return OnTextCtrlDClick - + def GenerateSearchResultsTreeBranch(self, root, infos): to_delete = [] if infos["name"] == "body": item_name = "%d:" % infos["data"][1][0] else: item_name = infos["name"] - + self.SearchResultsTree.SetItemText(root, item_name) self.SearchResultsTree.SetPyData(root, infos["data"]) self.SearchResultsTree.SetItemBackgroundColour(root, wx.WHITE) @@ -278,7 +286,7 @@ self.SearchResultsTree.SetItemImage(root, self.TreeImageDict[self.ParentWindow.Controler.GetPouType(infos["name"])]) else: self.SearchResultsTree.SetItemImage(root, self.TreeImageDict[infos["type"]]) - + text = None if infos["text"] is not None: text = infos["text"] @@ -291,13 +299,13 @@ text = _("(%d matches)") % infos["matches"] start_idx, end_idx = 0, len(text) style = wx.TextAttr(wx.Colour(0, 127, 174)) - + if text is not None: - text_ctrl_style = wx.BORDER_NONE|wx.TE_READONLY|wx.TE_RICH2 + text_ctrl_style = wx.BORDER_NONE | wx.TE_READONLY | wx.TE_RICH2 if wx.Platform != '__WXMSW__' or len(text.splitlines()) > 1: text_ctrl_style |= wx.TE_MULTILINE - text_ctrl = wx.TextCtrl(id=-1, parent=self.SearchResultsTree, pos=wx.Point(0, 0), - value=text, style=text_ctrl_style) + text_ctrl = wx.TextCtrl(id=-1, parent=self.SearchResultsTree, pos=wx.Point(0, 0), + value=text, style=text_ctrl_style) width, height = text_ctrl.GetTextExtent(text) text_ctrl.SetClientSize(wx.Size(width + 1, height)) text_ctrl.SetBackgroundColour(self.SearchResultsTree.GetBackgroundColour()) @@ -306,7 +314,7 @@ text_ctrl.SetInsertionPoint(0) text_ctrl.SetStyle(start_idx, end_idx, style) self.SearchResultsTree.SetItemWindow(root, text_ctrl) - + if wx.VERSION >= (2, 6, 0): item, root_cookie = self.SearchResultsTree.GetFirstChild(root) else: @@ -317,7 +325,7 @@ item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie) self.GenerateSearchResultsTreeBranch(item, child) item, root_cookie = self.SearchResultsTree.GetNextChild(root, root_cookie) - + def ShowSearchResults(self, item): data = self.SearchResultsTree.GetPyData(item) if isinstance(data, TupleType): @@ -327,11 +335,11 @@ self.ParentWindow.ClearHighlights(SEARCH_RESULT_HIGHLIGHT) for infos, start, end, text in search_results: self.ParentWindow.ShowSearchResult(infos, start, end) - + def OnSearchResultsTreeItemActivated(self, event): self.ShowSearchResults(event.GetItem()) event.Skip() - + def OnResetButton(self, event): self.ResetSearchResults() self.ParentWindow.ClearSearchResults()