Viewer.py
changeset 297 e837b67cb184
parent 292 800e100038ae
child 299 15669fe26e56
--- a/Viewer.py	Fri Dec 19 15:08:54 2008 +0100
+++ b/Viewer.py	Fri Jan 09 17:09:59 2009 +0100
@@ -152,35 +152,72 @@
                         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()
+                tagname = self.ParentWindow.GetTagName()
+                if values[3] == tagname:
                     if values[1] == "Output":
-                        var_type = OUTPUT
+                        var_class = OUTPUT
                     elif values[1] == "InOut":
-                        var_type = INPUT
+                        var_class = 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.RefreshVisibleElements()
-                    self.ParentWindow.Refresh(False)
+                        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:
+                            menu = wx.Menu(title='')
+                            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])
+                    else:
+                        message = "Unknown variable \"%s\" this POU!" % values[0]
                 else:
                     message = "Variable don't belong to this POU!"
         if message is not None:
             wx.CallAfter(self.ShowMessage, message)
 
+    def GenerateTreeMenu(self, x, y, scaling, menu, base_path, var_class, tree):
+        for child_name, child_type, child_tree in tree:
+            if base_path:
+                child_path = "%s.%s" % (base_path, child_name)
+            else:
+                child_path = child_name
+            if len(child_tree) == 1 and isinstance(child_tree[0], ListType):
+                child_path += "[0]"
+                child_name += "[]"
+                child_tree = child_tree[0]
+            new_id = wx.NewId()
+            AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=child_name)
+            self.ParentWindow.Bind(wx.EVT_MENU, self.GetAddVariableBlockFunction(x, y, scaling, var_class, child_path, child_type), id=new_id)
+            if len(child_tree) > 0:
+                new_id = wx.NewId()
+                child_menu = wx.Menu(title='')
+                self.GenerateTreeMenu(x, y, scaling, child_menu, child_path, var_class, child_tree)
+                menu.AppendMenu(new_id, "%s." % child_name, child_menu)
+            
+    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)
+        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()