Bug with unformated drop text fixed
authorlbessard
Tue, 01 Jul 2008 10:33:29 +0200
changeset 218 1b8e9bb83f25
parent 217 ddb5b2e499e2
child 219 ca1f1d3734f9
Bug with unformated drop text fixed
PLCOpenEditor.py
Viewer.py
--- a/PLCOpenEditor.py	Mon Jun 30 14:35:41 2008 +0200
+++ b/PLCOpenEditor.py	Tue Jul 01 10:33:29 2008 +0200
@@ -43,6 +43,8 @@
 sys.path.append(base_folder)
 from docutils import *
 
+from types import TupleType
+
 __version__ = "$Revision$"
 
 CWD = os.path.split(os.path.realpath(__file__))[0]
@@ -3420,8 +3422,12 @@
                 try:
                     values = eval(data)    
                 except:
+                    message = "Invalid value \"%s\" for location"%data
                     values = None
-                if values and values[1] == "location":
+                if not isinstance(values, TupleType):
+                    message = "Invalid value \"%s\" for location"%data
+                    values = None
+                if values is not None and values[1] == "location":
                     location = values[0]
                     variable_type = self.ParentWindow.Table.GetValueByName(row, "Type")
                     message = None
@@ -3453,10 +3459,13 @@
                                 self.ParentWindow.Table.ResetView(self.ParentWindow.VariablesGrid)
                                 self.ParentWindow.SaveValues()
                             dialog.Destroy()
-            if message is not None:
-                message = wx.MessageDialog(self.ParentWindow, message, "Error", wx.OK|wx.ICON_ERROR)
-                message.ShowModal()
-                message.Destroy()
+            wx.CallAfter(self.ShowMessage, message)
+            
+    def ShowMessage(self, message):
+        if message is not None:
+            message = wx.MessageDialog(self.ParentWindow, message, "Error", wx.OK|wx.ICON_ERROR)
+            message.ShowModal()
+            message.Destroy()
 
 [ID_VARIABLEEDITORPANEL, ID_VARIABLEEDITORPANELVARIABLESGRID, 
  ID_VARIABLEEDITORPANELRETURNTYPE, ID_VARIABLEEDITORPANELCLASSFILTER, 
--- a/Viewer.py	Mon Jun 30 14:35:41 2008 +0200
+++ b/Viewer.py	Tue Jul 01 10:33:29 2008 +0200
@@ -29,6 +29,8 @@
 
 from Dialogs import *
 
+from types import TupleType
+
 SCROLLBAR_UNIT = 10
 WINDOW_BORDER = 10
 SCROLL_ZONE = 10
@@ -84,87 +86,89 @@
     def OnDropText(self, x, y, data):
         x, y = self.ParentWindow.CalcUnscrolledPosition(x, y)
         scaling = self.ParentWindow.Scaling
-        values = eval(data)
-        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.GetEditedElementType(self.ParentWindow.GetTagName())
-            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:
-                blockname = values[2]
-                if values[1] != "function" and blockname == "":
-                    dialog = wx.TextEntryDialog(self.ParentWindow, "Block name", "Please enter a block name", "", wx.OK|wx.CANCEL|wx.CENTRE)
-                    if dialog.ShowModal():
-                        blockname = dialog.GetValue()
-                    dialog.Destroy()
-                if blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames()]:
-                    message = wx.MessageDialog(self.ParentWindow, "\"%s\" pou already exists!"%blockname, "Error", wx.OK|wx.ICON_ERROR)
-                    message.ShowModal()
-                    message.Destroy()
-                elif blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(self.ParentWindow.GetTagName())]:
-                    message = wx.MessageDialog(self.ParentWindow, "\"%s\" element for this pou already exists!"%blockname, "Error", wx.OK|wx.ICON_ERROR)
-                    message.ShowModal()
-                    message.Destroy()
+        message = None
+        try:
+            values = eval(data)
+        except:
+            message = "Invalid value \"%s\" for viewer block"%data
+            values = None
+        if not isinstance(values, TupleType):
+            message = "Invalid value \"%s\" for viewer block"%data
+            values = None
+        if values is not None:
+            if 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())
+                if name == values[0]:
+                    message = "\"%s\" can't use itself!"%name
+                elif type == "function" and values[1] != "function":
+                    message = "Function Blocks can't be used by Functions!"
+                elif self.ParentWindow.Controler.PouIsUsedBy(name, values[0]):
+                    message = "\"%s\" is already used by \"%s\"!"%(name, values[0])
                 else:
+                    blockname = values[2]
+                    if values[1] != "function" and blockname == "":
+                        dialog = wx.TextEntryDialog(self.ParentWindow, "Block name", "Please enter a block name", "", wx.OK|wx.CANCEL|wx.CENTRE)
+                        if dialog.ShowModal():
+                            blockname = dialog.GetValue()
+                        dialog.Destroy()
+                    if blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames()]:
+                        message = "\"%s\" pou already exists!"%blockname
+                    elif blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(self.ParentWindow.GetTagName())]:
+                        message = "\"%s\" element for this pou already exists!"%blockname
+                    else:
+                        id = self.ParentWindow.GetNewId()
+                        block = FBD_Block(self.ParentWindow, values[0], blockname, id)
+                        width, height = block.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]
+                        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.RefreshBlockModel(block)
+                        self.ParentWindow.RefreshBuffer()
+                        self.ParentWindow.RefreshScrollBars()
+                        self.ParentWindow.ParentWindow.RefreshVariablePanel(self.ParentWindow.GetTagName())
+                        self.ParentWindow.Refresh(False)
+            elif values[1] != "location":
+                if values[3] == self.ParentWindow.GetTagName():
                     id = self.ParentWindow.GetNewId()
-                    block = FBD_Block(self.ParentWindow, values[0], blockname, id)
-                    width, height = block.GetMinSize()
+                    if values[1] == "Output":
+                        var_type = OUTPUT
+                    elif values[1] == "InOut":
+                        var_type = INPUT
+                    else:
+                        var_type = INPUT
+                    variable = FBD_Variable(self.ParentWindow, var_type, values[0], values[2], 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]
-                    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.RefreshBlockModel(block)
+                    variable.SetPosition(x, y)
+                    variable.SetSize(width, height)
+                    self.ParentWindow.AddBlock(variable)
+                    self.ParentWindow.Controler.AddEditedElementVariable(self.ParentWindow.GetTagName(), id, var_type)
+                    self.ParentWindow.RefreshVariableModel(variable)
                     self.ParentWindow.RefreshBuffer()
                     self.ParentWindow.RefreshScrollBars()
-                    self.ParentWindow.ParentWindow.RefreshVariablePanel(self.ParentWindow.GetTagName())
                     self.ParentWindow.Refresh(False)
-        elif values[1] != "location":
-            if values[3] == self.ParentWindow.GetTagName():
-                id = self.ParentWindow.GetNewId()
-                if values[1] == "Output":
-                    var_type = OUTPUT
-                elif values[1] == "InOut":
-                    var_type = INPUT
                 else:
-                    var_type = INPUT
-                variable = FBD_Variable(self.ParentWindow, var_type, values[0], values[2], 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_type)
-                self.ParentWindow.RefreshVariableModel(variable)
-                self.ParentWindow.RefreshBuffer()
-                self.ParentWindow.RefreshScrollBars()
-                self.ParentWindow.Refresh(False)
-            else:
-                message = wx.MessageDialog(self.ParentWindow, "Variable don't belong to this POU!", "Error", wx.OK|wx.ICON_ERROR)
-                message.ShowModal()
-                message.Destroy()
-            
+                    message = "Variable don't belong to this POU!"
+        wx.CallAfter(self.ShowMessage, message)
+
+    def ShowMessage(self, message):
+        if message is not None:
+            message = wx.MessageDialog(self.ParentWindow, message, "Error", wx.OK|wx.ICON_ERROR)
+            message.ShowModal()
+            message.Destroy()
+   
 if wx.VERSION >= (2, 8, 0):
     import wx.aui