Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
authorlaurent
Thu, 24 Sep 2009 18:20:07 +0200
changeset 437 59e33406eea8
parent 436 f3bb091f803f
child 438 eebcf66f2d5a
Adding support for dropping Beremiz plugin variable locations into PLCOpenEditor viewers and adding corresponding variable into POU interface
TextViewer.py
Viewer.py
--- a/TextViewer.py	Thu Sep 24 18:18:04 2009 +0200
+++ b/TextViewer.py	Thu Sep 24 18:20:07 2009 +0200
@@ -216,20 +216,41 @@
         except:
             values = event.GetDragText()
         if isinstance(values, tuple):
-            if values[1] in ["functionBlock", "program", "location", "debug"]:
+            message = None
+            if values[1] in ["functionBlock", "program", "debug"]:
                 event.SetDragText("")
             elif values[1] == "function":
                 event.SetDragText(values[0])
-            elif values[1] != "location":
-                if values[3] == self.TagName:
-                    self.ResetBuffer()
-                    event.SetDragText(values[0])
-                    wx.CallAfter(self.RefreshModel)
+            elif values[1] == "location":
+                pou_name, pou_type = self.Controler.GetEditedElementType(self.TagName, self.Debug)
+                if len(values) > 2 and pou_type == "program":
+                    var_name = values[3]
+                    if var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]:
+                        message = _("\"%s\" pou already exists!")%blockname
+                    elif var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]:
+                        message = _("\"%s\" element for this pou already exists!")%blockname
+                    else:
+                        if values[2] is not None:
+                            var_type = values[2]
+                        else:
+                            var_type = LOCATIONDATATYPES.get(values[0][2], ["BOOL"])[0]
+                        self.Controler.AddEditedElementPouVar(self.TagName, var_type, var_name, values[0], values[4])
+                        self.ParentWindow.RefreshVariablePanel(self.TagName)
+                        self.RefreshVariableTree()
+                        event.SetDragText(var_name)
                 else:
                     event.SetDragText("")
-                    message = wx.MessageDialog(self.ParentWindow, _("Variable don't belong to this POU!"), _("Error"), wx.OK|wx.ICON_ERROR)
-                    message.ShowModal()
-                    message.Destroy()
+            elif values[3] == self.TagName:
+                self.ResetBuffer()
+                event.SetDragText(values[0])
+                wx.CallAfter(self.RefreshModel)
+            else:
+                message = _("Variable don't belong to this POU!")
+            if message is not None:
+                dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR)
+                dialog.ShowModal()
+                dialog.Destroy()
+                event.SetDragText("")
         event.Skip()
     
     def SetTextSyntax(self, syntax):
@@ -278,11 +299,7 @@
         self.EmptyUndoBuffer()
         self.DisableEvents = False
         
-        words = self.TagName.split("::")
-        
-        self.Variables = self.GenerateVariableTree([(variable["Name"], variable["Type"], variable["Tree"]) for variable in self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)])
-        if self.Controler.GetEditedElementType(self.TagName, self.Debug)[1] == "function" or words[0] == "T" and self.TextSyntax == "IL":
-            self.Variables[words[-1].upper()] = {}
+        self.RefreshVariableTree()
         
         self.Functions = []
         for category in self.Controler.GetBlockTypes(self.TagName, self.Debug):
@@ -296,6 +313,12 @@
         
         self.Colourise(0, -1)
     
+    def RefreshVariableTree(self):
+        words = self.TagName.split("::")
+        self.Variables = self.GenerateVariableTree([(variable["Name"], variable["Type"], variable["Tree"]) for variable in self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)])
+        if self.Controler.GetEditedElementType(self.TagName, self.Debug)[1] == "function" or words[0] == "T" and self.TextSyntax == "IL":
+            self.Variables[words[-1].upper()] = {}
+    
     def GenerateVariableTree(self, list):
         tree = {}
         for var_name, var_type, (var_tree, var_dimension) in list:
--- a/Viewer.py	Thu Sep 24 18:18:04 2009 +0200
+++ b/Viewer.py	Thu Sep 24 18:20:07 2009 +0200
@@ -201,6 +201,8 @@
         self.ParentWindow = parent
     
     def OnDropText(self, x, y, data):
+        tagname = self.ParentWindow.GetTagName()
+        pou_name, pou_type = self.ParentWindow.Controler.GetEditedElementType(tagname, self.ParentWindow.Debug)
         x, y = self.ParentWindow.CalcUnscrolledPosition(x, y)
         x = int(x / self.ParentWindow.ViewScale[0])
         y = int(y / self.ParentWindow.ViewScale[1])
@@ -219,17 +221,16 @@
                 pass
             elif values[1] == "program":
                 message = _("Programs can't be used by other POUs!")
-            elif values[1] in ["function", "functionBlock", "program"]:
-                name, type = self.ParentWindow.Controler.GetEditedElementType(self.ParentWindow.GetTagName(), self.ParentWindow.Debug)
-                words = self.ParentWindow.TagName.split("::")
-                if name == values[0]:
-                    message = _("\"%s\" can't use itself!")%name
-                elif type == "function" and values[1] != "function":
+            elif values[1] in ["function", "functionBlock"]:
+                words = tagname.split("::")
+                if pou_name == values[0]:
+                    message = _("\"%s\" can't use itself!")%pou_name
+                elif pou_type == "function" and values[1] != "function":
                     message = _("Function Blocks can't be used in Functions!")
                 elif words[0] == "T" and values[1] != "function":
                     message = _("Function Blocks can't be used in Transitions!")
-                elif self.ParentWindow.Controler.PouIsUsedBy(name, values[0], self.ParentWindow.Debug):
-                    message = _("\"%s\" is already used by \"%s\"!")%(name, values[0])
+                elif self.ParentWindow.Controler.PouIsUsedBy(pou_name, values[0], self.ParentWindow.Debug):
+                    message = _("\"%s\" is already used by \"%s\"!")%(pou_name, values[0])
                 else:
                     blockname = values[2]
                     if len(values) > 3:
@@ -245,7 +246,7 @@
                         dialog.Destroy()
                     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(self.ParentWindow.GetTagName(), self.ParentWindow.Debug)]:
+                    elif blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(tagname, self.ParentWindow.Debug)]:
                         message = _("\"%s\" element for this pou already exists!")%blockname
                     else:
                         id = self.ParentWindow.GetNewId()
@@ -259,34 +260,67 @@
                         block.SetPosition(x, y)
                         block.SetSize(width, height)
                         self.ParentWindow.AddBlock(block)
-                        self.ParentWindow.Controler.AddEditedElementBlock(self.ParentWindow.GetTagName(), id, values[0], blockname)
+                        self.ParentWindow.Controler.AddEditedElementBlock(tagname, id, values[0], blockname)
                         self.ParentWindow.RefreshBlockModel(block)
                         self.ParentWindow.RefreshBuffer()
                         self.ParentWindow.RefreshScrollBars()
                         self.ParentWindow.RefreshVisibleElements()
-                        self.ParentWindow.ParentWindow.RefreshVariablePanel(self.ParentWindow.GetTagName())
+                        self.ParentWindow.ParentWindow.RefreshVariablePanel(tagname)
                         self.ParentWindow.Refresh(False)
-            elif values[1] != "location":
-                tagname = self.ParentWindow.GetTagName()
-                if values[3] == tagname:
-                    if values[1] == "Output":
-                        var_class = OUTPUT
-                    elif values[1] == "InOut":
-                        var_class = INPUT
+            elif values[1] == "location":
+                if len(values) > 2 and pou_type == "program":
+                    var_name = values[3]
+                    if var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames(self.ParentWindow.Debug)]:
+                        message = _("\"%s\" pou already exists!")%blockname
+                    elif var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(tagname, self.ParentWindow.Debug)]:
+                        message = _("\"%s\" element for this pou already exists!")%blockname
                     else:
-                        var_class = INPUT
-                    tree = dict([(var["Name"], var["Tree"]) for var in self.ParentWindow.Controler.GetEditedElementInterfaceVars(tagname, self.ParentWindow.Debug)]).get(values[0], None)
-                    if tree is not None:
-                        if len(tree[0]) > 0:
-                            menu = wx.Menu(title='')
-                            self.GenerateTreeMenu(x, y, scaling, menu, "", var_class, [(values[0], values[2], tree)])
-                            self.ParentWindow.PopupMenuXY(menu)
+                        if values[0][1] == "Q":
+                            var_class = OUTPUT
                         else:
-                            self.AddParentVariableBlock(x, y, scaling, var_class, values[0], values[2])
+                            var_class = INPUT
+                        if values[2] is not None:
+                            var_type = values[2]
+                        else:
+                            var_type = LOCATIONDATATYPES.get(values[0][2], ["BOOL"])[0]
+                        id = self.ParentWindow.GetNewId()
+                        variable = FBD_Variable(self.ParentWindow, var_class, var_name, var_type, id)
+                        width, height = variable.GetMinSize()
+                        if scaling is not None:
+                            x = round(float(x) / float(scaling[0])) * scaling[0]
+                            y = round(float(y) / float(scaling[1])) * scaling[1]
+                            width = round(float(width) / float(scaling[0]) + 0.5) * scaling[0]
+                            height = round(float(height) / float(scaling[1]) + 0.5) * scaling[1]
+                        variable.SetPosition(x, y)
+                        variable.SetSize(width, height)
+                        self.ParentWindow.AddBlock(variable)
+                        self.ParentWindow.Controler.AddEditedElementPouVar(tagname, var_type, var_name, values[0], values[4])
+                        self.ParentWindow.Controler.AddEditedElementVariable(tagname, id, var_class)
+                        self.ParentWindow.RefreshVariableModel(variable)
+                        self.ParentWindow.RefreshBuffer()
+                        self.ParentWindow.RefreshScrollBars()
+                        self.ParentWindow.RefreshVisibleElements()
+                        self.ParentWindow.ParentWindow.RefreshVariablePanel(tagname)
+                        self.ParentWindow.Refresh(False)
+            elif values[3] == tagname:
+                if values[1] == "Output":
+                    var_class = OUTPUT
+                elif values[1] == "InOut":
+                    var_class = INPUT
+                else:
+                    var_class = INPUT
+                tree = dict([(var["Name"], var["Tree"]) for var in self.ParentWindow.Controler.GetEditedElementInterfaceVars(tagname, self.ParentWindow.Debug)]).get(values[0], None)
+                if tree is not None:
+                    if len(tree[0]) > 0:
+                        menu = wx.Menu(title='')
+                        self.GenerateTreeMenu(x, y, scaling, menu, "", var_class, [(values[0], values[2], tree)])
+                        self.ParentWindow.PopupMenuXY(menu)
                     else:
-                        message = _("Unknown variable \"%s\" for this POU!") % values[0]
+                        self.AddParentVariableBlock(x, y, scaling, var_class, values[0], values[2])
                 else:
-                    message = _("Variable don't belong to this POU!")
+                    message = _("Unknown variable \"%s\" for this POU!") % values[0]
+            else:
+                message = _("Variable don't belong to this POU!")
         if message is not None:
             wx.CallAfter(self.ShowMessage, message)