Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
authorlaurent
Wed, 30 Mar 2011 17:52:07 +0200
changeset 510 e7327ea490b4
parent 509 6f72513bc074
child 511 38fdcbc8ebd7
Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
LDViewer.py
Viewer.py
--- a/LDViewer.py	Wed Mar 30 16:31:18 2011 +0200
+++ b/LDViewer.py	Wed Mar 30 17:52:07 2011 +0200
@@ -448,6 +448,46 @@
                 event.Skip()
 
 #-------------------------------------------------------------------------------
+#                  Model adding functions from Drop Target
+#-------------------------------------------------------------------------------
+
+    def AddVariableBlock(self, x, y, scaling, var_class, var_name, var_type):
+        if var_type == "BOOL":
+            id = self.GetNewId()
+            if var_class == INPUT:
+                contact = LD_Contact(self, CONTACT_NORMAL, var_name, id)
+                width, height = contact.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]
+                contact.SetPosition(x, y)
+                contact.SetSize(width, height)
+                self.AddBlock(contact)
+                self.Controler.AddEditedElementContact(self.GetTagName(), id)
+                self.RefreshContactModel(contact)
+            else:
+                coil = LD_Coil(self, COIL_NORMAL, var_name, id)
+                width, height = coil.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]
+                coil.SetPosition(x, y)
+                coil.SetSize(width, height)
+                self.AddBlock(coil)
+                self.Controler.AddEditedElementCoil(self.GetTagName(), id)
+                self.RefreshCoilModel(coil)
+            self.RefreshBuffer()
+            self.RefreshScrollBars()
+            self.RefreshVisibleElements()
+            self.Refresh(False)
+        else:
+            Viewer.AddVariableBlock(self, x, y, scaling, var_class, var_name, var_type)
+
+#-------------------------------------------------------------------------------
 #                          Adding element functions
 #-------------------------------------------------------------------------------
 
--- a/Viewer.py	Wed Mar 30 16:31:18 2011 +0200
+++ b/Viewer.py	Wed Mar 30 17:52:07 2011 +0200
@@ -281,26 +281,9 @@
                             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)
                         if not var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(tagname, self.ParentWindow.Debug)]:
                             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)
+                        self.ParentWindow.AddVariableBlock(x, y, scaling, var_class, var_name, var_type)
             elif values[3] == tagname:
                 if values[1] == "Output":
                     var_class = OUTPUT
@@ -315,7 +298,7 @@
                         self.GenerateTreeMenu(x, y, scaling, menu, "", var_class, [(values[0], values[2], tree)])
                         self.ParentWindow.PopupMenuXY(menu)
                     else:
-                        self.AddParentVariableBlock(x, y, scaling, var_class, values[0], values[2])
+                        self.ParentWindow.AddVariableBlock(x, y, scaling, var_class, values[0], values[2])
                 else:
                     message = _("Unknown variable \"%s\" for this POU!") % values[0]
             else:
@@ -343,28 +326,9 @@
             
     def GetAddVariableBlockFunction(self, x, y, scaling, var_class, var_name, var_type):
         def AddVariableFunction(event):
-            self.AddParentVariableBlock(x, y, scaling, var_class, var_name, var_type)
+            self.ParentWindow.AddVariableBlock(x, y, scaling, var_class, var_name, var_type)
         return AddVariableFunction
     
-    def AddParentVariableBlock(self, x, y, scaling, var_class, var_name, var_type):
-        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.AddEditedElementVariable(self.ParentWindow.GetTagName(), id, var_class)
-        self.ParentWindow.RefreshVariableModel(variable)
-        self.ParentWindow.RefreshBuffer()
-        self.ParentWindow.RefreshScrollBars()
-        self.ParentWindow.RefreshVisibleElements()
-        self.ParentWindow.Refresh(False)
-    
     def ShowMessage(self, message):
         message = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK|wx.ICON_ERROR)
         message.ShowModal()
@@ -1761,6 +1725,29 @@
             event.Skip()
 
 #-------------------------------------------------------------------------------
+#                  Model adding functions from Drop Target
+#-------------------------------------------------------------------------------
+
+    def AddVariableBlock(self, x, y, scaling, var_class, var_name, var_type):
+        id = self.GetNewId()
+        variable = FBD_Variable(self, 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.AddBlock(variable)
+        self.Controler.AddEditedElementVariable(self.GetTagName(), id, var_class)
+        self.RefreshVariableModel(variable)
+        self.RefreshBuffer()
+        self.RefreshScrollBars()
+        self.RefreshVisibleElements()
+        self.Refresh(False)
+
+#-------------------------------------------------------------------------------
 #                          Model adding functions
 #-------------------------------------------------------------------------------