Adding support for quickly changing variable and connection type
authorLaurent Bessard
Thu, 18 Oct 2012 16:30:12 +0200
changeset 857 9695969796d0
parent 856 b64e436f000e
child 858 daafaa8a28fd
Adding support for quickly changing variable and connection type
editors/Viewer.py
graphics/FBD_Objects.py
graphics/GraphicCommons.py
--- a/editors/Viewer.py	Thu Oct 18 12:05:45 2012 +0200
+++ b/editors/Viewer.py	Thu Oct 18 16:30:12 2012 +0200
@@ -1378,6 +1378,16 @@
             self.ReleaseDataValue(iec_path)
         return ReleaseVariableFunction
 
+    def GetChangeVariableTypeMenuFunction(self, type):
+        def ChangeVariableTypeMenu(event):
+            self.ChangeVariableType(self.SelectedElement, type)
+        return ChangeVariableTypeMenu
+
+    def GetChangeConnectionTypeMenuFunction(self, type):
+        def ChangeConnectionTypeMenu(event):
+            self.ChangeConnectionType(self.SelectedElement, type)
+        return ChangeConnectionTypeMenu
+
     def PopupForceMenu(self):
         iec_path = self.GetElementIECPath(self.SelectedElement)
         if iec_path is not None:
@@ -1405,6 +1415,37 @@
         self.Editor.PopupMenu(menu)
         menu.Destroy()
     
+    def PopupVariableMenu(self):
+        menu = wx.Menu(title='')
+        variable_type = self.SelectedElement.GetType()
+        for type_label, type in [(_("Input"), INPUT),
+                                 (_("Output"), OUTPUT),
+                                 (_("InOut"), INOUT)]:
+            new_id = wx.NewId()
+            AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_RADIO, text=type_label)
+            self.Bind(wx.EVT_MENU, self.GetChangeVariableTypeMenuFunction(type), id=new_id)
+            if type == variable_type:
+                menu.Check(new_id, True)
+        menu.AppendSeparator()
+        self.AddDefaultMenuItems(menu, block=True)
+        self.Editor.PopupMenu(menu)
+        menu.Destroy()
+    
+    def PopupConnectionMenu(self):
+        menu = wx.Menu(title='')
+        connection_type = self.SelectedElement.GetType()
+        for type_label, type in [(_("Connector"), CONNECTOR),
+                                 (_("Continuation"), CONTINUATION)]:
+            new_id = wx.NewId()
+            AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_RADIO, text=type_label)
+            self.Bind(wx.EVT_MENU, self.GetChangeConnectionTypeMenuFunction(type), id=new_id)
+            if type == connection_type:
+                menu.Check(new_id, True)
+        menu.AppendSeparator()
+        self.AddDefaultMenuItems(menu, block=True)
+        self.Editor.PopupMenu(menu)
+        menu.Destroy()
+    
     def PopupWireMenu(self, delete=True):
         menu = wx.Menu(title='')
         self.AddWireMenuItems(menu, delete)
@@ -1846,7 +1887,7 @@
                         else:
                             self.ParentWindow.OpenGraphicViewer(iec_path)
             elif event.ControlDown() and not event.ShiftDown():
-                if not isinstance(self.SelectedElement, Group_Element):
+                if not isinstance(self.SelectedElement, Graphic_Group):
                     instance_type = self.SelectedElement.GetType()
                     if self.IsBlock(self.SelectedElement) and instance_type in self.Controler.GetProjectPouNames(self.Debug):
                         self.ParentWindow.EditProjectElement(ITEM_POU, 
@@ -1855,9 +1896,9 @@
                         self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling)
             elif event.ControlDown() and event.ShiftDown():
                 movex, movey = self.SelectedElement.SetBestSize(self.Scaling)
-                self.SelectedElement.RefreshModel()
-                self.RefreshBuffer()
                 if movex != 0 or movey != 0:
+                    self.SelectedElement.RefreshModel()
+                    self.RefreshBuffer()
                     self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(movex, movey)), False)
             else:
                 self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling)
@@ -2704,6 +2745,20 @@
         infos["connectors"] = block.GetConnectors()
         self.Controler.SetEditedElementBlockInfos(self.TagName, blockid, infos)
     
+    def ChangeVariableType(self, variable, new_type):
+        old_type = variable.GetType()
+        rect = variable.GetRedrawRect(1, 1)
+        if old_type != new_type:
+            variable.SetType(new_type, variable.GetValueType())
+            id = variable.GetId()
+            self.Controler.RemoveEditedElementInstance(self.TagName, id)
+            self.Controler.AddEditedElementVariable(self.TagName, id, new_type)
+            self.RefreshVariableModel(variable)
+            self.RefreshBuffer()
+            self.RefreshVisibleElements()
+            self.RefreshScrollBars()
+            variable.Refresh(rect.Union(variable.GetRedrawRect()))
+    
     def RefreshVariableModel(self, variable):
         variableid = variable.GetId()
         infos = {}
@@ -2715,6 +2770,20 @@
         infos["connectors"] = variable.GetConnectors()
         self.Controler.SetEditedElementVariableInfos(self.TagName, variableid, infos)
 
+    def ChangeConnectionType(self, connection, new_type):
+        old_type = connection.GetType()
+        rect = connection.GetRedrawRect(1, 1)
+        if old_type != new_type:
+            connection.SetType(new_type)
+            id = connection.GetId()
+            self.Controler.RemoveEditedElementInstance(self.TagName, id)
+            self.Controler.AddEditedElementConnection(self.TagName, id, new_type)
+            self.RefreshConnectionModel(connection)
+            self.RefreshBuffer()
+            self.RefreshScrollBars()
+            self.RefreshVisibleElements()
+            connection.Refresh(rect.Union(connection.GetRedrawRect()))
+
     def RefreshConnectionModel(self, connection):
         connectionid = connection.GetId()
         infos = {}
--- a/graphics/FBD_Objects.py	Thu Oct 18 12:05:45 2012 +0200
+++ b/graphics/FBD_Objects.py	Thu Oct 18 16:30:12 2012 +0200
@@ -652,26 +652,34 @@
     def SetType(self, type, value_type):
         if type != self.Type:
             self.Type = type
-            self.Clean()
-            self.Input = None
-            self.Output = None
             # Create an input or output connector according to variable type
             if self.Type != INPUT:
-                self.Input = Connector(self, "", value_type, wx.Point(0, 0), WEST, onlyone = True)
+                if self.Input is None:
+                    self.Input = Connector(self, "", value_type, wx.Point(0, 0), WEST, onlyone = True)
+            elif self.Input:
+                self.Input.UnConnect(delete = True)
+                self.Input = None
             if self.Type != OUTPUT:
-                self.Output = Connector(self, "", value_type, wx.Point(0, 0), EAST)
+                if self.Output is None:
+                    self.Output = Connector(self, "", value_type, wx.Point(0, 0), EAST)
+            elif self.Output:
+                self.Output.UnConnect(delete = True)
+                self.Output = None
             self.RefreshConnectors()
         elif value_type != self.ValueType:
             if self.Input:
                 self.Input.SetType(value_type)
             if self.Output:
                 self.Output.SetType(value_type)            
-        self.RefreshConnectors()
-    
+        
     # Returns the variable type
     def GetType(self):
         return self.Type
     
+    # Returns the variable value type
+    def GetValueType(self):
+        return self.ValueType
+    
     # Changes the variable name
     def SetName(self, name):
         self.Name = name
@@ -705,12 +713,18 @@
     
     # Method called when a LeftDClick event have been generated
     def OnLeftDClick(self, event, dc, scaling):
-        # Edit the variable properties
-        self.Parent.EditVariableContent(self)
+        if event.ControlDown():
+            # Change variable type 
+            types = [INPUT, OUTPUT, INOUT]
+            self.Parent.ChangeVariableType(self,
+                types[(types.index(self.Type) + 1) % len(types)])
+        else:
+            # Edit the variable properties
+            self.Parent.EditVariableContent(self)
     
     # Method called when a RightUp event have been generated
     def OnRightUp(self, event, dc, scaling):
-        self.Parent.PopupDefaultMenu()
+        self.Parent.PopupVariableMenu()
     
     # Refreshes the variable model
     def RefreshModel(self, move=True):
@@ -927,13 +941,20 @@
     
     # Method called when a LeftDClick event have been generated
     def OnLeftDClick(self, event, dc, scaling):
-        # Edit the connection properties
-        self.Parent.EditConnectionContent(self)
+        if event.ControlDown():
+            # Change connection type 
+            if self.Type == CONNECTOR:
+                self.Parent.ChangeConnectionType(self, CONTINUATION)
+            else:
+                self.Parent.ChangeConnectionType(self, CONNECTOR)
+        else:
+            # Edit the connection properties
+            self.Parent.EditConnectionContent(self)
     
     # Method called when a RightUp event have been generated
     def OnRightUp(self, event, dc, scaling):
         # Popup the default menu
-        self.Parent.PopupDefaultMenu()
+        self.Parent.PopupConnectionMenu()
     
     # Refreshes the connection model
     def RefreshModel(self, move=True):
--- a/graphics/GraphicCommons.py	Thu Oct 18 12:05:45 2012 +0200
+++ b/graphics/GraphicCommons.py	Thu Oct 18 16:30:12 2012 +0200
@@ -1667,8 +1667,9 @@
         # If no wire defined, unconnect all wires
         if not wire:
             self.Wires = []
-        self.RefreshValid()
-        self.ParentBlock.RefreshModel(False)
+        if not delete:
+            self.RefreshValid()
+            self.ParentBlock.RefreshModel(False)
     
     # Returns if connector has one or more wire connected
     def IsConnected(self):