WIP adding searching capabilities in python files. was done : search_in_CTN
authorEdouard Tisserant
Wed, 20 Mar 2019 11:34:41 +0100
branchsearch_in_CTN
changeset 2524 c80b0d864475
parent 2523 aa7f138648f3
child 2525 9812b332f350
WIP adding searching capabilities in python files. was done :
- fixed stub CTN search results so that they can be accepted by search result panel
- worked on search result panel so that it can display those CTN results
- made search result panel display correctly stub search result, for variables and body matches
- EditProjectElement now opens CTN node as well as PlcOpen project nodes
BeremizIDE.py
CodeFileTreeNode.py
ConfigTreeNode.py
IDEFrame.py
controls/SearchResultPanel.py
plcopen/plcopen.py
plcopen/types_enums.py
--- a/BeremizIDE.py	Fri Mar 15 14:11:49 2019 +0100
+++ b/BeremizIDE.py	Wed Mar 20 11:34:41 2019 +0100
@@ -1033,6 +1033,15 @@
             else:
                 IDEFrame.ProjectTreeItemSelect(self, select_item)
 
+    def GetProjectElementWindow(self, element, tagname):
+        print("BeremizIDE GetProjectElementWindo", element, tagname)
+        is_a_CTN_tagname = len(tagname.split("::"))==1
+        if is_a_CTN_tagname:
+            confnode = self.CTR.GetChildByName(tagname)
+            return confnode.GetView() 
+        else :
+            return IDEFrame.GetProjectElementWindow(self, element, tagname)
+
     def SelectProjectTreeItem(self, tagname):
         if self.ProjectTree is not None:
             root = self.ProjectTree.GetRootItem()
@@ -1050,6 +1059,7 @@
                 elif words[0] == "R":
                     return self.RecursiveProjectTreeItemSelection(root, [(words[2], ITEM_RESOURCE)])
                 elif not os.path.exists(words[0]):
+                    print(words[0])
                     IDEFrame.SelectProjectTreeItem(self, tagname)
 
     def GetAddConfNodeFunction(self, name, confnode=None):
--- a/CodeFileTreeNode.py	Fri Mar 15 14:11:49 2019 +0100
+++ b/CodeFileTreeNode.py	Wed Mar 20 11:34:41 2019 +0100
@@ -212,7 +212,8 @@
 
     def CTNSearch(self, criteria):
         # TODO really search
-        return [((self.CTNFullName()), (0,0),(0,3),"cow")]
+        return [((self.CTNFullName(),"variable",0), (0,2),(0,4),"a_cow"),
+                ((self.CTNFullName(),"body"), (1,12),(1,15),"Bitch I'm a cow !")]
 
 # -------------------------------------------------------------------------------
 #                      Current Buffering Management Functions
--- a/ConfigTreeNode.py	Fri Mar 15 14:11:49 2019 +0100
+++ b/ConfigTreeNode.py	Wed Mar 20 11:34:41 2019 +0100
@@ -471,20 +471,23 @@
     def GetContextualMenuItems(self):
         return None
 
+    def GetView(self):
+        if self._View is None and self.EditorType is not None:
+            app_frame = self.GetCTRoot().AppFrame
+            self._View = self.EditorType(app_frame.TabsOpened, self, app_frame)
+
+        return self._View
+
     def _OpenView(self, name=None, onlyopened=False):
-        if self.EditorType is not None:
+        view = self.GetView()
+
+        if view is not None:
+            if name is None:
+                name = self.CTNFullName()
             app_frame = self.GetCTRoot().AppFrame
-            if self._View is None and not onlyopened:
-
-                self._View = self.EditorType(app_frame.TabsOpened, self, app_frame)
-
-            if self._View is not None:
-                if name is None:
-                    name = self.CTNFullName()
-                app_frame.EditProjectElement(self._View, name)
-
-            return self._View
-        return None
+            app_frame.EditProjectElement(view, name)
+
+        return view
 
     def _CloseView(self, view):
         app_frame = self.GetCTRoot().AppFrame
--- a/IDEFrame.py	Fri Mar 15 14:11:49 2019 +0100
+++ b/IDEFrame.py	Wed Mar 20 11:34:41 2019 +0100
@@ -1587,6 +1587,7 @@
         "R": [ITEM_CONFIGURATION, ITEM_RESOURCE]}
 
     def SelectProjectTreeItem(self, tagname):
+        print("SelectProjectTreeItem", tagname)
         result = False
         if self.ProjectTree is not None:
             root = self.ProjectTree.GetRootItem()
@@ -1597,6 +1598,7 @@
         return result
 
     def RecursiveProjectTreeItemSelection(self, root, items):
+        print("RecursiveProjectTreeItemSelection", items)
         found = False
         item, root_cookie = self.ProjectTree.GetFirstChild(root)
         while item is not None and item.IsOk() and not found:
@@ -1819,7 +1821,50 @@
         else:
             event.Skip()
 
+    def GetProjectElementWindow(self, element, tagname):
+        print("GetProjectElementWindo", element, tagname)
+        new_window = None
+        if self.Controler.GetEditedElement(tagname) is not None:
+            new_window = None
+            if element == ITEM_CONFIGURATION:
+                new_window = ConfigurationEditor(self.TabsOpened, tagname, self, self.Controler)
+                new_window.SetIcon(GetBitmap("CONFIGURATION"))
+            elif element == ITEM_RESOURCE:
+                new_window = ResourceEditor(self.TabsOpened, tagname, self, self.Controler)
+                new_window.SetIcon(GetBitmap("RESOURCE"))
+            elif element in [ITEM_POU, ITEM_TRANSITION, ITEM_ACTION]:
+                bodytype = self.Controler.GetEditedElementBodyType(tagname)
+                if bodytype == "FBD":
+                    new_window = Viewer(self.TabsOpened, tagname, self, self.Controler)
+                    new_window.RefreshScaling(False)
+                elif bodytype == "LD":
+                    new_window = LD_Viewer(self.TabsOpened, tagname, self, self.Controler)
+                    new_window.RefreshScaling(False)
+                elif bodytype == "SFC":
+                    new_window = SFC_Viewer(self.TabsOpened, tagname, self, self.Controler)
+                    new_window.RefreshScaling(False)
+                else:
+                    new_window = TextViewer(self.TabsOpened, tagname, self, self.Controler)
+                    new_window.SetTextSyntax(bodytype)
+                    if bodytype == "IL":
+                        new_window.SetKeywords(IL_KEYWORDS)
+                    else:
+                        new_window.SetKeywords(ST_KEYWORDS)
+                if element == ITEM_POU:
+                    pou_type = self.Controler.GetEditedElementType(tagname)[1].upper()
+                    icon = GetBitmap(pou_type, bodytype)
+                elif element == ITEM_TRANSITION:
+                    icon = GetBitmap("TRANSITION", bodytype)
+                elif element == ITEM_ACTION:
+                    icon = GetBitmap("ACTION", bodytype)
+                new_window.SetIcon(icon)
+            elif element == ITEM_DATATYPE:
+                new_window = DataTypeEditor(self.TabsOpened, tagname, self, self.Controler)
+                new_window.SetIcon(GetBitmap("DATATYPE"))
+        return new_window
+
     def EditProjectElement(self, element, tagname, onlyopened=False):
+        print("EditProjectElement", element, tagname, onlyopened)
         openedidx = self.IsOpened(tagname)
         if openedidx is not None:
             old_selected = self.TabsOpened.GetSelection()
@@ -1831,49 +1876,11 @@
         elif not onlyopened:
             if isinstance(element, EditorPanel):
                 new_window = element
-                self.AddPage(element, "")
-            elif self.Controler.GetEditedElement(tagname) is not None:
-                new_window = None
-                if element == ITEM_CONFIGURATION:
-                    new_window = ConfigurationEditor(self.TabsOpened, tagname, self, self.Controler)
-                    new_window.SetIcon(GetBitmap("CONFIGURATION"))
-                    self.AddPage(new_window, "")
-                elif element == ITEM_RESOURCE:
-                    new_window = ResourceEditor(self.TabsOpened, tagname, self, self.Controler)
-                    new_window.SetIcon(GetBitmap("RESOURCE"))
-                    self.AddPage(new_window, "")
-                elif element in [ITEM_POU, ITEM_TRANSITION, ITEM_ACTION]:
-                    bodytype = self.Controler.GetEditedElementBodyType(tagname)
-                    if bodytype == "FBD":
-                        new_window = Viewer(self.TabsOpened, tagname, self, self.Controler)
-                        new_window.RefreshScaling(False)
-                    elif bodytype == "LD":
-                        new_window = LD_Viewer(self.TabsOpened, tagname, self, self.Controler)
-                        new_window.RefreshScaling(False)
-                    elif bodytype == "SFC":
-                        new_window = SFC_Viewer(self.TabsOpened, tagname, self, self.Controler)
-                        new_window.RefreshScaling(False)
-                    else:
-                        new_window = TextViewer(self.TabsOpened, tagname, self, self.Controler)
-                        new_window.SetTextSyntax(bodytype)
-                        if bodytype == "IL":
-                            new_window.SetKeywords(IL_KEYWORDS)
-                        else:
-                            new_window.SetKeywords(ST_KEYWORDS)
-                    if element == ITEM_POU:
-                        pou_type = self.Controler.GetEditedElementType(tagname)[1].upper()
-                        icon = GetBitmap(pou_type, bodytype)
-                    elif element == ITEM_TRANSITION:
-                        icon = GetBitmap("TRANSITION", bodytype)
-                    elif element == ITEM_ACTION:
-                        icon = GetBitmap("ACTION", bodytype)
-                    new_window.SetIcon(icon)
-                    self.AddPage(new_window, "")
-                elif element == ITEM_DATATYPE:
-                    new_window = DataTypeEditor(self.TabsOpened, tagname, self, self.Controler)
-                    new_window.SetIcon(GetBitmap("DATATYPE"))
-                    self.AddPage(new_window, "")
+            else:
+                new_window = self.GetProjectElementWindow(element, tagname)
+
             if new_window is not None:
+                self.AddPage(new_window, "")
                 openedidx = self.IsOpened(tagname)
                 old_selected = self.TabsOpened.GetSelection()
                 if old_selected != openedidx:
@@ -2554,6 +2561,7 @@
     # -------------------------------------------------------------------------------
 
     def ShowHighlight(self, infos, start, end, highlight_type):
+        print(infos)
         self.SelectProjectTreeItem(infos[0])
         if infos[1] == "name":
             self.Highlights[infos[0]] = highlight_type
--- a/controls/SearchResultPanel.py	Fri Mar 15 14:11:49 2019 +0100
+++ b/controls/SearchResultPanel.py	Wed Mar 20 11:34:41 2019 +0100
@@ -29,6 +29,7 @@
 import wx
 import wx.lib.buttons
 import wx.lib.agw.customtreectrl as CT
+from pprint import pprint
 
 from PLCControler import *
 from util.BitmapLibrary import GetBitmap
@@ -130,7 +131,9 @@
                 ("DATATYPE",       ITEM_DATATYPE),
                 ("ACTION",         "action_block"),
                 ("IL",             "IL"),
-                ("ST",             "ST")]:
+                ("ST",             "ST"),
+                ("FILE",           ITEM_CONFNODE),
+                ]:
             self.TreeImageDict[itemtype] = self.TreeImageList.Add(GetBitmap(imgname))
 
         for itemtype in ["function", "functionBlock", "program",
@@ -202,7 +205,12 @@
 
                 children = element_infos.setdefault("children", [])
                 for infos, start, end, text in results:
-                    if infos[1] == "name" or element_type == ITEM_DATATYPE:
+                    if len(words) == 1:  # CTN match
+                        child_name = {"body":str(start[0])+":",
+                                      "variable":_("Variable:")}[infos[1]]
+                        child_type = {"body":ITEM_CONFNODE,
+                                      "variable":"var_inout"}[infos[1]]
+                    elif infos[1] == "name" or element_type == ITEM_DATATYPE:
                         child_name = GenerateName(infos[1:])
                         child_type = element_type
                     else:
@@ -232,6 +240,7 @@
                     }
                     children.append(child_infos)
 
+                # not Project node
                 if len(words) > 2:
                     for _element_infos in search_results_tree_children:
                         if _element_infos["name"] == words[1]:
@@ -240,7 +249,7 @@
                             break
                     if element_type == ITEM_RESOURCE:
                         search_results_tree_children.append(element_infos)
-                else:
+                else:  # Project node or CTN
                     search_results_tree_children.append(element_infos)
 
             if matches_number < 2:
@@ -287,6 +296,8 @@
                 self.SearchResultsTree.SetItemImage(root, self.TreeImageDict[infos["type"]])
 
         text = None
+        print("XXXXXXXXXXXXXX")
+        pprint(infos)
         if infos["text"] is not None:
             text = infos["text"]
             start, end = infos["data"][1:3]
@@ -302,7 +313,7 @@
         if text is not None:
             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_style |= wx.TE_MULTILINE | wx.TE_NO_VSCROLL
             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)
--- a/plcopen/plcopen.py	Fri Mar 15 14:11:49 2019 +0100
+++ b/plcopen/plcopen.py	Wed Mar 20 11:34:41 2019 +0100
@@ -856,6 +856,7 @@
     setattr(cls, "removeVariableByFilter", _removeConfigurationResourceVariableByFilter)
 
     def Search(self, criteria, parent_infos=None):
+        # FIXME  : two next lines are incompatible [][-1] raises exception !
         parent_infos = [] if parent_infos is None else parent_infos
         parent_infos = parent_infos[:-1] + ["R::%s::%s" % (parent_infos[-1].split("::")[1], self.getname())]
         search_result = _SearchInConfigurationResource(self, criteria, parent_infos)
--- a/plcopen/types_enums.py	Fri Mar 15 14:11:49 2019 +0100
+++ b/plcopen/types_enums.py	Wed Mar 20 11:34:41 2019 +0100
@@ -117,6 +117,9 @@
 
 def GetElementType(tagname):
     words = tagname.split("::")
+    print("GetElementType",tagname, len(words))
+    if len(words) == 1:
+        return ITEM_CONFNODE
     return {
         "D": ITEM_DATATYPE,
         "P": ITEM_POU,