Adding Function Block name test in Viewer Drop Target
authorlbessard
Thu, 03 Apr 2008 18:32:33 +0200
changeset 195 d9084f6eecfd
parent 194 1b3f8b4f8e04
child 196 54f5020531f6
Adding Function Block name test in Viewer Drop Target
Viewer.py
--- a/Viewer.py	Thu Apr 03 18:31:35 2008 +0200
+++ b/Viewer.py	Thu Apr 03 18:32:33 2008 +0200
@@ -110,23 +110,32 @@
                     if dialog.ShowModal():
                         blockname = dialog.GetValue()
                     dialog.Destroy()
-                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)
+                if blockname.upper() not 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() not 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()
+                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()
@@ -288,7 +297,7 @@
     # Create a new Viewer
     def __init__(self, parent, tagname, window, controler):
         wx.ScrolledWindow.__init__(self, parent, pos=wx.Point(0, 0), size=wx.Size(0, 0), 
-            style=wx.SUNKEN_BORDER | wx.HSCROLL | wx.VSCROLL)
+            style=wx.HSCROLL | wx.VSCROLL)
         self._init_menus()
         # Adding a rubberband to Viewer
         self.rubberBand = RubberBand(drawingSurface=self)
@@ -760,20 +769,22 @@
             self.AddBlock(block)
             connectors = block.GetConnectors()
             for i, input_connector in enumerate(instance["connectors"]["inputs"]):
-                connector = connectors["inputs"][i]
-                connector.SetPosition(wx.Point(*input_connector["position"]))
-                if input_connector["negated"]:
-                    connector.SetNegated(True)
-                if input_connector["edge"] != "none":
-                    connector.SetEdge(input_connector["edge"])
-                self.CreateWires(connector, input_connector["links"], ids)
+                if i < len(connectors["inputs"]):
+                    connector = connectors["inputs"][i]
+                    connector.SetPosition(wx.Point(*input_connector["position"]))
+                    if input_connector["negated"]:
+                        connector.SetNegated(True)
+                    if input_connector["edge"] != "none":
+                        connector.SetEdge(input_connector["edge"])
+                    self.CreateWires(connector, input_connector["links"], ids)
             for i, output_connector in enumerate(instance["connectors"]["outputs"]):
-                connector = connectors["outputs"][i]
-                if output_connector["negated"]:
-                    connector.SetNegated(True)
-                if output_connector["edge"] != "none":
-                    connector.SetEdge(output_connector["edge"])
-                connector.SetPosition(wx.Point(*output_connector["position"]))
+                if i < len(connectors["outputs"]):
+                    connector = connectors["outputs"][i]
+                    if output_connector["negated"]:
+                        connector.SetNegated(True)
+                    if output_connector["edge"] != "none":
+                        connector.SetEdge(output_connector["edge"])
+                    connector.SetPosition(wx.Point(*output_connector["position"]))
     
     def CreateWires(self, start_connector, links, ids):
         for link in links: