Adding Paste POU to project node contextual menu
authorLaurent Bessard
Fri, 15 Jun 2012 18:02:09 +0200
changeset 712 c11b54730a7b
parent 711 5f6a743dcde5
child 713 95a0a427f3ef
Adding Paste POU to project node contextual menu
PLCControler.py
PLCOpenEditor.py
--- a/PLCControler.py	Thu Jun 14 18:13:32 2012 +0200
+++ b/PLCControler.py	Fri Jun 15 18:02:09 2012 +0200
@@ -769,14 +769,6 @@
             new_pou.loadXMLTree(root)
 
             name = new_pou.getname()
-            orig_type = new_pou.getpouType()
-
-            # prevent violations of POU content restrictions:
-            # function blocks cannot be pasted as functions,
-            # programs cannot be pasted as functions or function blocks
-            if orig_type == 'functionBlock' and pou_type == 'function' or \
-               orig_type == 'program' and pou_type in ['function', 'functionBlock']:
-                return _('''%s "%s" can't be pasted as a %s.''') % (orig_type, name, pou_type)
             
             idx = 0
             new_name = name
@@ -789,10 +781,23 @@
                 
             # we've found a name that does not already exist, use it
             new_pou.setname(new_name)
-            new_pou.setpouType(pou_type)
+            
+            if pou_type is not None:
+                orig_type = new_pou.getpouType()
+    
+                # prevent violations of POU content restrictions:
+                # function blocks cannot be pasted as functions,
+                # programs cannot be pasted as functions or function blocks
+                if orig_type == 'functionBlock' and pou_type == 'function' or \
+                   orig_type == 'program' and pou_type in ['function', 'functionBlock']:
+                    return _('''%s "%s" can't be pasted as a %s.''') % (orig_type, name, pou_type)
+                
+                new_pou.setpouType(pou_type)
 
             self.Project.insertpou(-1, new_pou)
             self.BufferProject()
+            
+            return self.ComputePouName(new_name),
         else:
             return _("Couldn't paste non-POU object.")
 
--- a/PLCOpenEditor.py	Thu Jun 14 18:13:32 2012 +0200
+++ b/PLCOpenEditor.py	Fri Jun 15 18:02:09 2012 +0200
@@ -1924,8 +1924,11 @@
         item_infos = self.ProjectTree.GetPyData(item)
         
         menu = None
-        if item_infos["type"] in ITEMS_UNEDITABLE:
-            name = UNEDITABLE_NAMES_DICT[name]
+        if item_infos["type"] in ITEMS_UNEDITABLE + [ITEM_PROJECT]:
+            if item_infos["type"] == ITEM_PROJECT:
+                name = "Project"
+            else:
+                name = UNEDITABLE_NAMES_DICT[name]
             
             if name == "Data Types":
                 menu = wx.Menu(title='')
@@ -1933,12 +1936,13 @@
                 AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Add DataType"))
                 self.Bind(wx.EVT_MENU, self.OnAddDataTypeMenu, id=new_id)
             
-            elif name in ["Functions", "Function Blocks", "Programs"]:
+            elif name in ["Functions", "Function Blocks", "Programs", "Project"]:
                 menu = wx.Menu(title='')
-
-                new_id = wx.NewId()
-                AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Add POU"))
-                self.Bind(wx.EVT_MENU, self.GenerateAddPouFunction({"Functions" : "function", "Function Blocks" : "functionBlock", "Programs" : "program"}[name]), id=new_id)
+                
+                if name != "Project":
+                    new_id = wx.NewId()
+                    AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Add POU"))
+                    self.Bind(wx.EVT_MENU, self.GenerateAddPouFunction({"Functions" : "function", "Function Blocks" : "functionBlock", "Programs" : "program"}[name]), id=new_id)
 
                 new_id = wx.NewId()
                 AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Paste POU"))
@@ -2449,21 +2453,25 @@
 
     def OnPastePou(self, event):
         selected = self.ProjectTree.GetSelection()
-
-        pou_type = self.ProjectTree.GetItemText(selected)
-        pou_type = UNEDITABLE_NAMES_DICT[pou_type] # one of 'Functions', 'Function Blocks' or 'Programs'
-        pou_type = {'Functions': 'function', 'Function Blocks': 'functionBlock', 'Programs': 'program'}[pou_type]
-
+        
+        if self.ProjectTree.GetPyData(selected)["type"] != ITEM_PROJECT: 
+            pou_type = self.ProjectTree.GetItemText(selected)
+            pou_type = UNEDITABLE_NAMES_DICT[pou_type] # one of 'Functions', 'Function Blocks' or 'Programs'
+            pou_type = {'Functions': 'function', 'Function Blocks': 'functionBlock', 'Programs': 'program'}[pou_type]
+        else:
+            pou_type = None
+        
         pou_xml = self.GetCopyBuffer()
 
         result = self.Controler.PastePou(pou_type, pou_xml)
 
-        if result is not None:
+        if not isinstance(result, TupleType):
             message = wx.MessageDialog(self, result, _("Error"), wx.OK|wx.ICON_ERROR)
             message.ShowModal()
             message.Destroy()
         else:
             self._Refresh(TITLE, EDITORTOOLBAR, FILEMENU, EDITMENU, PROJECTTREE, LIBRARYTREE)
+            self.EditProjectElement(ITEM_POU, result[0])
 
 #-------------------------------------------------------------------------------
 #                        Remove Project Elements Functions