Replacing dialog asking for a name when drag'n drop function block to viewer by automatically generated name
authorLaurent Bessard
Wed, 06 Jun 2012 16:57:58 +0200
changeset 704 aca0c83ed82e
parent 703 1a14560e10ed
child 705 1a343b239e9c
Replacing dialog asking for a name when drag'n drop function block to viewer by automatically generated name
PLCControler.py
TextViewer.py
Viewer.py
--- a/PLCControler.py	Mon Jun 04 02:04:29 2012 +0200
+++ b/PLCControler.py	Wed Jun 06 16:57:58 2012 +0200
@@ -1964,8 +1964,8 @@
             for instance in element.getinstances():
                 if isinstance(instance, (plcopen.sfcObjects_step, plcopen.commonObjects_connector, plcopen.commonObjects_continuation)):
                     names[instance.getname()] = True
-        i = 1
-        while names.get(name.upper(), False):
+        i = 0
+        while name is None or names.get(name.upper(), False):
             name = (format%i)
             i += 1
         return name
@@ -2020,7 +2020,7 @@
                                         blocktype = instance.gettypeName()
                                         if element_type == "function":
                                             return _("FunctionBlock \"%s\" can't be pasted in a Function!!!")%blocktype
-                                        blockname = self.GenerateNewName(tagname, blockname, "Block%d", debug=debug)
+                                        blockname = self.GenerateNewName(tagname, blockname, "%s%%d"%blocktype, debug=debug)
                                         exclude[blockname] = True
                                         instance.setinstanceName(blockname)
                                         self.AddEditedElementPouVar(tagname, blocktype, blockname)
--- a/TextViewer.py	Mon Jun 04 02:04:29 2012 +0200
+++ b/TextViewer.py	Wed Jun 06 16:57:58 2012 +0200
@@ -248,7 +248,7 @@
         return {"cursor_pos": self.Editor.GetCurrentPos()}
     
     def SetState(self, state):
-        if self:
+        if self and state.has_key("cursor_pos"):
             self.Editor.GotoPos(state.get("cursor_pos", 0))
         
     def OnModification(self, event):
--- a/Viewer.py	Mon Jun 04 02:04:29 2012 +0200
+++ b/Viewer.py	Wed Jun 06 16:57:58 2012 +0200
@@ -235,12 +235,7 @@
                     else:
                         blockinputs = None
                     if values[1] != "function" and blockname == "":
-                        dialog = wx.TextEntryDialog(self.ParentWindow.ParentWindow, "Block name", "Please enter a block name", "", wx.OK|wx.CANCEL|wx.CENTRE)
-                        if dialog.ShowModal() == wx.ID_OK:
-                            blockname = dialog.GetValue()
-                        else:
-                            return
-                        dialog.Destroy()
+                        blockname = self.ParentWindow.GenerateNewName(blocktype=values[0])
                     if blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames(self.ParentWindow.Debug)]:
                         message = _("\"%s\" pou already exists!")%blockname
                     elif blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(tagname, self.ParentWindow.Debug)]:
@@ -693,8 +688,10 @@
 
     def SetState(self, state):
         if self:
-            self.SetScale(state["zoom"])
-            self.Scroll(*state["position"])
+            if state.has_key("zoom"):
+                self.SetScale(state["zoom"])
+            if state.has_key("position"):
+                self.Scroll(*state["position"])
             self.RefreshVisibleElements()
         
     def GetLogicalDC(self, buffered=False):
@@ -2888,12 +2885,16 @@
             return True
         return False
 
-    def GenerateNewName(self, element, exclude={}):
-        if isinstance(element, SFC_Step):
+    def GenerateNewName(self, element=None, blocktype=None, exclude={}):
+        if element is not None and isinstance(element, SFC_Step):
             format = "Step%d"
         else:
-            format = "Block%d"    
-        return self.Controler.GenerateNewName(self.TagName, element.GetName(), format, exclude, self.Debug)
+            if element is not None:
+                blocktype = element.GetType()
+            if blocktype is None:
+                blocktype = "Block"
+            format = "%s%%d" % blocktype
+        return self.Controler.GenerateNewName(self.TagName, None, format, exclude, self.Debug)
 
     def IsNamedElement(self, element):
         return isinstance(element, FBD_Block) and element.GetName() != "" or isinstance(element, SFC_Step)