Bugs on Variable and Viewer DropTarget fixed
authorlbessard
Wed, 19 Sep 2007 13:32:24 +0200
changeset 97 28337cd092fd
parent 96 d178cfa9e77f
child 98 ec5d7af033d8
Bugs on Variable and Viewer DropTarget fixed
Added support for avoiding recursive block creation with Drag and Drop
PLCControler.py
PLCOpenEditor.py
Viewer.py
--- a/PLCControler.py	Wed Sep 19 13:28:04 2007 +0200
+++ b/PLCControler.py	Wed Sep 19 13:32:24 2007 +0200
@@ -368,15 +368,15 @@
 
     def GenerateProgram(self, filepath):
         if self.Project:
-            #try:
-            program = GenerateCurrentProgram(self.Project)
-            programfile = open(filepath, "w")
-            programfile.write(program)
-            programfile.close()
-            self.ProgramFilePath = filepath
-            return True
-            #except:
-            #    pass
+            try:
+                program = GenerateCurrentProgram(self.Project)
+                programfile = open(filepath, "w")
+                programfile.write(program)
+                programfile.close()
+                self.ProgramFilePath = filepath
+                return True
+            except:
+                pass
         return False
 
 #-------------------------------------------------------------------------------
@@ -1102,14 +1102,14 @@
     def RefreshCurrentElementEditing(self, index):
         self.CurrentElementEditing = index
 
-    # Return language in which current pou editing is written
+    # Return current pou editing name and type
     def GetCurrentElementEditingType(self):
         if self.CurrentElementEditing != None:
             name = self.ElementsOpened[self.CurrentElementEditing]
             words = name.split("::")
-            if words[0] == "P":
-                return self.GetPouType(words[1])
-        return None
+            if words[0] in ["P","T","A"]:
+                return words[1], self.GetPouType(words[1])
+        return None, None
 
     # Return language in which current pou editing is written
     def GetCurrentElementEditingBodyType(self):
--- a/PLCOpenEditor.py	Wed Sep 19 13:28:04 2007 +0200
+++ b/PLCOpenEditor.py	Wed Sep 19 13:32:24 2007 +0200
@@ -794,12 +794,13 @@
             self.Controler.RefreshCurrentElementEditing(selected)
             found = False
             name = self.TabsOpened.GetPageText(selected)
-            root = self.ProjectTree.GetRootItem()
-            item, root_cookie = self.ProjectTree.GetFirstChild(root)
-            while item.IsOk() and not found:
-                if self.ProjectTree.GetItemText(item) == name:
-                    self.ProjectTree.SelectItem(item)
-                item, root_cookie = self.ProjectTree.GetNextChild(root, root_cookie)
+            if self.ProjectTree:
+                root = self.ProjectTree.GetRootItem()
+                item, root_cookie = self.ProjectTree.GetFirstChild(root)
+                while item.IsOk() and not found:
+                    if self.ProjectTree.GetItemText(item) == name:
+                        self.ProjectTree.SelectItem(item)
+                    item, root_cookie = self.ProjectTree.GetNextChild(root, root_cookie)
             self.TabsOpened.GetPage(selected).RefreshView()
             self.RefreshFileMenu()
             self.RefreshEditMenu()
@@ -2198,7 +2199,7 @@
     
     def OnDropText(self, x, y, data):
         col = self.ParentWindow.VariablesGrid.XToCol(x)
-        row = self.ParentWindow.VariablesGrid.YToRow(y)
+        row = self.ParentWindow.VariablesGrid.YToRow(y - self.ParentWindow.VariablesGrid.GetColLabelSize())
         if col != wx.NOT_FOUND and row != wx.NOT_FOUND:
             if self.ParentWindow.Table.GetColLabelValue(col) != "Location":
                 return
@@ -2216,7 +2217,7 @@
                         location = "%Q" + values[0]
                     else:
                         location = "%M" + values[0]
-                    self.ParentWindow.Table.SetValue(row - 1, col, location)
+                    self.ParentWindow.Table.SetValue(row, col, location)
                     self.ParentWindow.Table.ResetView(self.ParentWindow.VariablesGrid)
                     self.ParentWindow.SaveValues()
                 dialog.Destroy()    
--- a/Viewer.py	Wed Sep 19 13:28:04 2007 +0200
+++ b/Viewer.py	Wed Sep 19 13:32:24 2007 +0200
@@ -53,17 +53,35 @@
     
     def OnDropText(self, x, y, data):
         values = eval(data)
-        if values[1] in ["function", "functionBlock", "program"]:
-            id = self.ParentWindow.GetNewId()
-            block = FBD_Block(self.ParentWindow, values[0], values[2], id)
-            block.SetPosition(x, y)
-            width, height = block.GetMinSize()
-            block.SetSize(width, height)
-            self.ParentWindow.AddBlock(block)
-            self.ParentWindow.Controler.AddCurrentElementEditingBlock(id,values[0], values[2])
-            self.ParentWindow.RefreshBlockModel(block)
-            self.ParentWindow.RefreshScrollBars()
-            self.ParentWindow.Refresh()
+        if values[1] == "program":
+            message = wx.MessageDialog(self.ParentWindow, "Programs can't be used by other POUs!", "Error", wx.OK|wx.ICON_ERROR)
+            message.ShowModal()
+            message.Destroy()
+        elif values[1] in ["function", "functionBlock", "program"]:
+            name, type = self.ParentWindow.Controler.GetCurrentElementEditingType()
+            if name == values[0]:
+                message = wx.MessageDialog(self.ParentWindow, "\"%s\" can't use itself!"%name, "Error", wx.OK|wx.ICON_ERROR)
+                message.ShowModal()
+                message.Destroy()
+            elif type == "function" and values[1] != "function":
+                message = wx.MessageDialog(self.ParentWindow, "Function Blocks can't be used by Functions!", "Error", wx.OK|wx.ICON_ERROR)
+                message.ShowModal()
+                message.Destroy()
+            elif self.ParentWindow.Controler.PouIsUsedBy(name, values[0]):
+                message = wx.MessageDialog(self.ParentWindow, "\"%s\" is already used by \"%s\"!"%(name, values[0]), "Error", wx.OK|wx.ICON_ERROR)
+                message.ShowModal()
+                message.Destroy()
+            else:
+                id = self.ParentWindow.GetNewId()
+                block = FBD_Block(self.ParentWindow, values[0], values[2], id)
+                block.SetPosition(x, y)
+                width, height = block.GetMinSize()
+                block.SetSize(width, height)
+                self.ParentWindow.AddBlock(block)
+                self.ParentWindow.Controler.AddCurrentElementEditingBlock(id, values[0], values[2])
+                self.ParentWindow.RefreshBlockModel(block)
+                self.ParentWindow.RefreshScrollBars()
+                self.ParentWindow.Refresh()
         elif values[1] != "location":
             id = self.ParentWindow.GetNewId()
             if values[1] == "Output":
@@ -976,7 +994,7 @@
             if event.ControlDown() and event.ShiftDown():
                 self.Scroll(0, ypos)
             elif event.ControlDown():
-                self.Scroll(max(0, xpos - 1), ypos)
+                event.Skip()
             elif self.SelectedElement:
                 self.SelectedElement.Move(-scaling[0], 0)
                 self.SelectedElement.RefreshModel()
@@ -987,7 +1005,7 @@
             if event.ControlDown() and event.ShiftDown():
                 self.Scroll(xmax, ypos)
             elif event.ControlDown():
-                self.Scroll(min(xpos + 1, xmax), ypos)
+                event.Skip()
             elif self.SelectedElement:
                 self.SelectedElement.Move(scaling[0], 0)
                 self.SelectedElement.RefreshModel()
@@ -998,7 +1016,7 @@
             if event.ControlDown() and event.ShiftDown():
                 self.Scroll(xpos, 0)
             elif event.ControlDown():
-                self.Scroll(xpos, max(0, ypos - 1))
+                event.Skip()
             elif self.SelectedElement:
                 self.SelectedElement.Move(0, -scaling[1])
                 self.SelectedElement.RefreshModel()
@@ -1009,13 +1027,15 @@
             if event.ControlDown() and event.ShiftDown():
                 self.Scroll(xpos, ymax)
             elif event.ControlDown():
-                self.Scroll(xpos, min(ypos + 1, ymax))
+                event.Skip()
             elif self.SelectedElement:
                 self.SelectedElement.Move(0, scaling[1])
                 self.SelectedElement.RefreshModel()
                 self.RefreshBuffer()
                 self.RefreshScrollBars()
                 self.Refresh(False)
+        else:
+            event.Skip()
 
 #-------------------------------------------------------------------------------
 #                          Model adding functions