Adding support for Project Tree selected item following current item edited.
authorlbessard
Mon, 19 Nov 2007 10:02:35 +0100
changeset 122 e6faee0c271b
parent 121 40b91ba978db
child 123 62f93b8df816
Adding support for Project Tree selected item following current item edited.
Some DnD bugs fixed
LDViewer.py
PLCOpenEditor.py
SFCViewer.py
TextViewer.py
Viewer.py
--- a/LDViewer.py	Fri Nov 16 17:43:32 2007 +0100
+++ b/LDViewer.py	Mon Nov 19 10:02:35 2007 +0100
@@ -1248,3 +1248,16 @@
         if self.GetDrawingMode() == FREEDRAWING_MODE:
             Viewer.EditPowerRailContent(self, powerrail)
 
+#-------------------------------------------------------------------------------
+#                          Model update functions
+#-------------------------------------------------------------------------------
+
+    def RefreshBlockModel(self, block):
+        blockid = block.GetId()
+        infos = {}
+        infos["type"] = block.GetType()
+        infos["name"] = block.GetName()
+        infos["x"], infos["y"] = block.GetPosition()
+        infos["width"], infos["height"] = block.GetSize()
+        infos["connectors"] = block.GetConnectors()
+        self.Controler.SetEditedElementBlockInfos(self.TagName, blockid, infos)
--- a/PLCOpenEditor.py	Fri Nov 16 17:43:32 2007 +0100
+++ b/PLCOpenEditor.py	Mon Nov 19 10:02:35 2007 +0100
@@ -352,8 +352,10 @@
             self.AUIManager.AddPane(self.ProjectTree, wx.aui.AuiPaneInfo().Caption("Project Tree").Left().Layer(1).BestSize(wx.Size(200, 500)).CloseButton(False))
         if wx.VERSION >= (2, 6, 0):
             self.ProjectTree.Bind(wx.EVT_RIGHT_UP, self.OnProjectTreeRightUp)
+            self.ProjectTree.Bind(wx.EVT_LEFT_UP, self.OnProjectTreeLeftUp)
         else:
             wx.EVT_RIGHT_UP(self.ProjectTree, self.OnProjectTreeRightUp)
+            wx.EVT_LEFT_UP(self.ProjectTree, self.OnProjectTreeLeftUp)
         self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnProjectTreeBeginDrag,
               id=ID_PLCOPENEDITORPROJECTTREE)
         self.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT, self.OnProjectTreeItemBeginEdit,
@@ -362,7 +364,7 @@
               id=ID_PLCOPENEDITORPROJECTTREE)
         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnProjectTreeItemActivated,
               id=ID_PLCOPENEDITORPROJECTTREE)
-        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnProjectTreeItemSelected,
+        self.Bind(wx.EVT_TREE_SEL_CHANGING, self.OnProjectTreeItemChanging,
               id=ID_PLCOPENEDITORPROJECTTREE)
         
         if wx.VERSION < (2, 8, 0):
@@ -435,6 +437,7 @@
         
         self.CurrentToolBar = []
         self.CurrentLanguage = ""
+        self.SelectedItem = None
         self.CopyBuffer = None
         self.DrawingMode = FREEDRAWING_MODE
         #self.DrawingMode = DRIVENDRAWING_MODE
@@ -447,6 +450,9 @@
         self.RefreshTitle()
         self.RefreshToolBar()
 
+    def ResetSelectedItem(self):
+        self.SelectedItem = None
+
     def GetPageCount(self):
         if wx.VERSION >= (2, 8, 0):
             notebook = self.GetNotebook()
@@ -940,18 +946,19 @@
                 else:
                     window = None
             if window:
-##                found = False
-##                words = window.GetTagName().split("::")
-##                if self.ProjectTree:
-##                    root = self.ProjectTree.GetRootItem()
-##                    if wx.VERSION >= (2, 6, 0):
-##                        item, root_cookie = self.ProjectTree.GetFirstChild(root)
-##                    else:
-##                        item, root_cookie = self.ProjectTree.GetFirstChild(root, 0)
-##                    while item.IsOk() and not found:
-##                        if self.ProjectTree.GetItemText(item) == words[1]:
-##                            self.ProjectTree.SelectItem(item)
-##                        item, root_cookie = self.ProjectTree.GetNextChild(root, root_cookie)
+                if self.ProjectTree:
+                    root = self.ProjectTree.GetRootItem()
+                    words = window.GetTagName().split("::")
+                    if words[0] == "P":
+                        self.SelectProjectTreeItem(root, [(words[1], ITEM_POU)])
+                    elif words[0] == "T":
+                        self.SelectProjectTreeItem(root, [(words[1], ITEM_POU), (words[2], ITEM_TRANSITION)])
+                    elif words[0] == "A":
+                        self.SelectProjectTreeItem(root, [(words[1], ITEM_POU), (words[2], ITEM_ACTION)])
+                    elif words[0] == "C":
+                        self.SelectProjectTreeItem(root, [(words[1], ITEM_CONFIGURATION)])
+                    elif words[0] == "R":
+                        self.SelectProjectTreeItem(root, [(words[1], ITEM_CONFIGURATION), (words[2], ITEM_RESOURCE)])
                 window.RefreshView()
                 self.VariablePanelIndexer.ChangeVariablePanel(window.GetTagName())
                 self.RefreshFileMenu()
@@ -959,16 +966,37 @@
                 self.RefreshToolBar()
         event.Skip()
 
+    def SelectProjectTreeItem(self, root, items):
+        found = False
+        if self.ProjectTree:
+            if wx.VERSION >= (2, 6, 0):
+                item, root_cookie = self.ProjectTree.GetFirstChild(root)
+            else:
+                item, root_cookie = self.ProjectTree.GetFirstChild(root, 0)
+            while item.IsOk() and not found:
+                if (self.ProjectTree.GetItemText(item), self.ProjectTree.GetPyData(item)) == items[0]:
+                    if len(items) == 1:
+                        self.SelectedItem = item
+                        self.ProjectTree.SelectItem(item)
+                        wx.CallAfter(self.ResetSelectedItem)
+                        return True
+                    else:
+                        found = self.SelectProjectTreeItem(item, items[1:])
+                else:
+                    found = self.SelectProjectTreeItem(item, items)
+                item, root_cookie = self.ProjectTree.GetNextChild(root, root_cookie)
+        return found
+
     def OnProjectTreeBeginDrag(self, event):
-        item = event.GetItem()
-        if self.ProjectTree.GetPyData(item) == ITEM_POU:
-            block_name = self.ProjectTree.GetItemText(item)
+        if self.SelectedItem is not None and self.ProjectTree.GetPyData(self.SelectedItem) == ITEM_POU:
+            block_name = self.ProjectTree.GetItemText(self.SelectedItem)
             block_type = self.Controler.GetPouType(block_name)
             if block_type != "program":
                 data = wx.TextDataObject(str((block_name, block_type, "")))
                 dragSource = wx.DropSource(self.ProjectTree)
                 dragSource.SetData(data)
                 dragSource.DoDragDrop()
+            self.ResetSelectedItem()
 
     def RefreshEditorNames(self, item_type, old_name, new_name):
         for i in xrange(self.GetPageCount()):
@@ -1132,35 +1160,51 @@
                 tagname = self.Controler.ComputePouActionName(pou_name, name)
             self.EditProjectElement(data, tagname)
     
-    def OnProjectTreeItemSelected(self, event):
-        selected = event.GetItem()
-        name = self.ProjectTree.GetItemText(selected)
-        data = self.ProjectTree.GetPyData(selected)
-        if data == ITEM_POU:
-            self.EditProjectElement(data, self.Controler.ComputePouName(name), True)
-        elif data == ITEM_CONFIGURATION:
-            self.EditProjectElement(data, self.Controler.ComputeConfigurationName(name), True)
-        elif data == ITEM_RESOURCE:
-            item = self.ProjectTree.GetItemParent(selected)
-            item_type = self.ProjectTree.GetPyData(item)
-            while item_type != ITEM_CONFIGURATION:
-                item = self.ProjectTree.GetItemParent(item)
+    def OnProjectTreeLeftUp(self, event):
+        if self.SelectedItem is not None:
+            print "LeftUp", self.ProjectTree.GetItemText(self.SelectedItem)
+            self.ProjectTree.SelectItem(self.SelectedItem)
+            name = self.ProjectTree.GetItemText(self.SelectedItem)
+            data = self.ProjectTree.GetPyData(self.SelectedItem)
+            if data == ITEM_POU:
+                self.EditProjectElement(data, self.Controler.ComputePouName(name), True)
+            elif data == ITEM_CONFIGURATION:
+                self.EditProjectElement(data, self.Controler.ComputeConfigurationName(name), True)
+            elif data == ITEM_RESOURCE:
+                item = self.ProjectTree.GetItemParent(self.SelectedItem)
                 item_type = self.ProjectTree.GetPyData(item)
-            config_name = self.ProjectTree.GetItemText(item)
-            self.EditProjectElement(data, self.Controler.ComputeConfigurationResourceName(config_name, name), True)
-        elif data in [ITEM_TRANSITION, ITEM_ACTION]:
-            item = self.ProjectTree.GetItemParent(selected)
-            item_type = self.ProjectTree.GetPyData(item)
-            while item_type != ITEM_POU:
-                item = self.ProjectTree.GetItemParent(item)
+                while item_type != ITEM_CONFIGURATION:
+                    item = self.ProjectTree.GetItemParent(item)
+                    item_type = self.ProjectTree.GetPyData(item)
+                config_name = self.ProjectTree.GetItemText(item)
+                self.EditProjectElement(data, self.Controler.ComputeConfigurationResourceName(config_name, name), True)
+            elif data in [ITEM_TRANSITION, ITEM_ACTION]:
+                item = self.ProjectTree.GetItemParent(self.SelectedItem)
                 item_type = self.ProjectTree.GetPyData(item)
-            pou_name = self.ProjectTree.GetItemText(item)
-            if data == ITEM_TRANSITION:
-                tagname = self.Controler.ComputePouTransitionName(pou_name, name)
-            elif data == ITEM_ACTION:
-                tagname = self.Controler.ComputePouActionName(pou_name, name)
-            self.EditProjectElement(data, tagname, True)
-        event.Skip()
+                while item_type != ITEM_POU:
+                    item = self.ProjectTree.GetItemParent(item)
+                    item_type = self.ProjectTree.GetPyData(item)
+                pou_name = self.ProjectTree.GetItemText(item)
+                if data == ITEM_TRANSITION:
+                    tagname = self.Controler.ComputePouTransitionName(pou_name, name)
+                elif data == ITEM_ACTION:
+                    tagname = self.Controler.ComputePouActionName(pou_name, name)
+                self.EditProjectElement(data, tagname, True)
+            wx.CallAfter(self.ResetSelectedItem)
+        else:
+            print "LeftUp", None
+        event.Skip()
+    
+    def OnProjectTreeItemChanging(self, event):
+        if self.SelectedItem is not None:
+            print "Changing", self.ProjectTree.GetItemText(event.GetItem()), self.ProjectTree.GetItemText(self.SelectedItem)
+        else:
+            print "Changing", None, self.ProjectTree.GetItemText(event.GetItem())
+        if self.ProjectTree.GetPyData(event.GetItem()) != ITEM_UNEDITABLE and self.SelectedItem is None:
+            self.SelectedItem = event.GetItem()
+            event.Veto()
+        else:
+            event.Skip()
     
     def IsOpened(self, tagname):
         for idx in xrange(self.GetPageCount()):
--- a/SFCViewer.py	Fri Nov 16 17:43:32 2007 +0100
+++ b/SFCViewer.py	Mon Nov 19 10:02:35 2007 +0100
@@ -1028,4 +1028,18 @@
     def DeleteWire(self, wire):
         if self.GetDrawingMode() == FREEDRAWING_MODE:
             Viewer.DeleteWire(self, wire)
-    
+
+#-------------------------------------------------------------------------------
+#                          Model update functions
+#-------------------------------------------------------------------------------
+
+    def RefreshBlockModel(self, block):
+        blockid = block.GetId()
+        infos = {}
+        infos["type"] = block.GetType()
+        infos["name"] = block.GetName()
+        infos["x"], infos["y"] = block.GetPosition()
+        infos["width"], infos["height"] = block.GetSize()
+        infos["connectors"] = block.GetConnectors()
+        self.Controler.SetEditedElementBlockInfos(self.TagName, blockid, infos)
+
--- a/TextViewer.py	Fri Nov 16 17:43:32 2007 +0100
+++ b/TextViewer.py	Mon Nov 19 10:02:35 2007 +0100
@@ -218,6 +218,8 @@
         if isinstance(values, tuple):
             if values[1] in ["functionBlock", "program", "location"]:
                 event.SetDragText("")
+            elif values[1] == "function":
+                event.SetDragText(values[0])
             elif values[1] != "location":
                 if values[3] == self.TagName:
                     event.SetDragText(values[0])
--- a/Viewer.py	Fri Nov 16 17:43:32 2007 +0100
+++ b/Viewer.py	Mon Nov 19 10:02:35 2007 +0100
@@ -67,7 +67,7 @@
             message.ShowModal()
             message.Destroy()
         elif values[1] in ["function", "functionBlock", "program"]:
-            name, type = self.ParentWindow.Controler.GetEditedElementType(self.TagName)
+            name, type = self.ParentWindow.Controler.GetEditedElementType(self.ParentWindow.GetTagName())
             if name == values[0]:
                 message = wx.MessageDialog(self.ParentWindow, "\"%s\" can't use itself!"%name, "Error", wx.OK|wx.ICON_ERROR)
                 message.ShowModal()
@@ -93,7 +93,7 @@
                 width, height = block.GetMinSize()
                 block.SetSize(width, height)
                 self.ParentWindow.AddBlock(block)
-                self.ParentWindow.Controler.AddEditedElementBlock(self.TagName, id, values[0], blockname)
+                self.ParentWindow.Controler.AddEditedElementBlock(self.ParentWindow.GetTagName(), id, values[0], blockname)
                 self.ParentWindow.RefreshBlockModel(block)
                 self.ParentWindow.RefreshBuffer()
                 self.ParentWindow.RefreshScrollBars()