Viewer.py
changeset 437 59e33406eea8
parent 433 26bc7d5d4264
child 440 e183bffc05f0
--- 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)