SFCViewer.py
changeset 42 4a8400732001
parent 28 fc23e1f415d8
child 44 c6e153273ea1
--- a/SFCViewer.py	Tue Jul 17 12:25:50 2007 +0200
+++ b/SFCViewer.py	Wed Jul 18 11:51:30 2007 +0200
@@ -41,8 +41,7 @@
         startpoint = [start.GetPosition(False), start.GetDirection()]
         endpoint = [end.GetPosition(False), end.GetDirection()]
         wire = Wire(self, startpoint, endpoint)
-        self.Wires.append(wire)
-        self.Elements.append(wire)
+        self.AddWire(wire)
         start.Connect((wire, 0), False)
         end.Connect((wire, -1), False)
         wire.ConnectStartPoint(None, start)
@@ -62,8 +61,7 @@
         else:
             previous.RefreshOutputPosition()
         wire.SetPoints([wxPoint(pos.x, pos.y + GetWireSize(previous)), wxPoint(pos.x, pos.y)])
-        self.Blocks.append(transition)
-        self.Elements.append(transition)
+        self.AddBlock(transition)
         self.Controler.AddCurrentElementEditingTransition(id)
         self.RefreshTransitionModel(transition)
         if next:
@@ -86,19 +84,16 @@
         input_wire = input_wires[0][0]
         previous = input_wire.EndConnected
         input_wire.Clean()
-        self.Wires.remove(input_wire)
-        self.Elements.remove(input_wire)
+        self.RemoveWire(input_wire)
         output_wires = connectors["output"].GetWires()
         if len(output_wires) != 1:
             return
         output_wire = output_wires[0][0]
         next = output_wire.StartConnected
         output_wire.Clean()
-        self.Wires.remove(output_wire)
-        self.Elements.remove(output_wire)
+        self.RemoveWire(output_wire)
         transition.Clean()
-        self.Blocks.remove(transition)
-        self.Elements.remove(transition)
+        self.RemoveBlock(transition)
         self.Controler.RemoveCurrentElementEditingInstance(transition.GetId())
         wire = self.ConnectConnectors(next, previous)
         return wire
@@ -120,8 +115,7 @@
         else:
             previous.RefreshOutputPosition()
         wire.SetPoints([wxPoint(pos.x, pos.y + GetWireSize(previous)), wxPoint(pos.x, pos.y)])
-        self.Blocks.append(step)
-        self.Elements.append(step)
+        self.AddBlock(step)
         self.Controler.AddCurrentElementEditingStep(id)
         self.RefreshStepModel(step)
         if next:
@@ -145,8 +139,7 @@
             input_wire = input_wires[0][0]
             previous = input_wire.EndConnected
             input_wire.Clean()
-            self.Wires.remove(input_wire)
-            self.Elements.remove(input_wire)
+            self.RemoveWire(input_wire)
         else:
             previous = None
         if connectors["output"]:
@@ -156,16 +149,14 @@
             output_wire = output_wires[0][0]
             next = output_wire.StartConnected
             output_wire.Clean()
-            self.Wires.remove(output_wire)
-            self.Elements.remove(output_wire)
+            self.RemoveWire(output_wire)
         else:
             next = None
         action = step.GetActionConnector()
         if action:
             self.DeleteActionBlock(action.GetParentBlock())
         step.Clean()
-        self.Blocks.remove(step)
-        self.Elements.remove(step)
+        self.RemoveBlock(step)
         self.Controler.RemoveCurrentElementEditingInstance(step.GetId())
         if next and previous:
             wire = self.ConnectConnectors(next, previous)
@@ -185,7 +176,7 @@
             pos = event.GetLogicalPosition(dc)
             if event.ControlDown():
                 element = self.FindElement(pos, True)
-                if element and element not in self.Wires:
+                if element and not self.IsWire(element):
                     if isinstance(self.SelectedElement, Graphic_Group):
                         self.SelectedElement.SelectElement(element)
                     else:
@@ -202,7 +193,7 @@
             else:
                 element = self.FindElement(pos)
                 if self.SelectedElement and self.SelectedElement != element:
-                    if self.SelectedElement in self.Wires:
+                    if self.IsWire(self.SelectedElement):
                         self.SelectedElement.SetSelectedSegment(None)
                     else:
                         self.SelectedElement.SetSelected(False)
@@ -225,8 +216,7 @@
             wire.Handle = (HANDLE_POINT, 0)
             wire.ProcessDragging(0, 0)
             wire.Handle = (HANDLE_POINT, 1)
-            self.Wires.append(wire)
-            self.Elements.append(wire)
+            self.AddWire(wire)
             if self.SelectedElement:
                 self.SelectedElement.SetSelected(False)
             self.SelectedElement = wire
@@ -252,7 +242,7 @@
         elif self.Mode == MODE_INITIALSTEP:
             wxCallAfter(self.AddInitialStep, GetScaledEventPosition(event, self.GetLogicalDC(), self.Scaling))
         elif self.Mode == MODE_SELECTION and self.SelectedElement:
-            if self.SelectedElement in self.Wires:
+            if self.IsWire(self.SelectedElement):
                 self.SelectedElement.SetSelectedSegment(0)
             else:
                 self.SelectedElement.OnLeftUp(event, self.GetLogicalDC(), self.Scaling)
@@ -279,7 +269,7 @@
                 if self.SelectedElement and self.SelectedElement != element:
                     self.SelectedElement.SetSelected(False)
                 self.SelectedElement = element
-                if self.SelectedElement in self.Wires:
+                if self.IsWire(self.SelectedElement):
                     self.SelectedElement.SetSelectedSegment(0)
                 else:
                     self.SelectedElement.SetSelected(True)
@@ -303,7 +293,7 @@
         elif self.rubberBand.IsShown():
             self.rubberBand.OnMotion(event, self.GetLogicalDC(), self.Scaling)
         elif self.Mode == MODE_SELECTION and self.SelectedElement:
-            if self.SelectedElement not in self.Wires:
+            if not self.IsWire(self.SelectedElement):
                 self.SelectedElement.OnMotion(event, self.GetLogicalDC(), self.Scaling)
             self.Refresh()
         elif self.Mode == MODE_WIRE and self.SelectedElement:
@@ -327,16 +317,35 @@
         if keycode == WXK_DELETE and self.SelectedElement:
             self.SelectedElement.Delete()
             self.SelectedElement = None
-        elif keycode == WXK_LEFT and self.SelectedElement:
-            self.SelectedElement.Move(-scaling[0], 0)
-        elif keycode == WXK_RIGHT and self.SelectedElement:
-            self.SelectedElement.Move(scaling[0], 0)
-        elif keycode == WXK_UP and self.SelectedElement:
-            self.SelectedElement.Move(0, -scaling[1])
-        elif keycode == WXK_DOWN and self.SelectedElement:
-            self.SelectedElement.Move(0, scaling[1])
+        elif keycode == WXK_LEFT:
+            if event.ControlDown() and event.ShiftDown():
+                self.Scroll(0, ypos)
+            elif event.ControlDown():
+                self.Scroll(max(0, xpos - 1), ypos)
+            elif self.SelectedElement:
+                self.SelectedElement.Move(-scaling[0], 0)
+        elif keycode == WXK_RIGHT:
+            if event.ControlDown() and event.ShiftDown():
+                self.Scroll(xmax, ypos)
+            elif event.ControlDown():
+                self.Scroll(min(xpos + 1, xmax), ypos)
+            elif self.SelectedElement:
+                self.SelectedElement.Move(scaling[0], 0)
+        elif keycode == WXK_UP:
+            if event.ControlDown() and event.ShiftDown():
+                self.Scroll(xpos, 0)
+            elif event.ControlDown():
+                self.Scroll(xpos, max(0, ypos - 1))
+            elif self.SelectedElement:
+                self.SelectedElement.Move(0, -scaling[1])
+        elif keycode == WXK_DOWN:
+            if event.ControlDown() and event.ShiftDown():
+                self.Scroll(xpos, ymax)
+            elif event.ControlDown():
+                self.Scroll(xpos, min(ypos + 1, ymax))
+            elif self.SelectedElement:
+                self.SelectedElement.Move(0, scaling[1])
         self.Refresh()
-        event.Skip()
         
 #-------------------------------------------------------------------------------
 #                          Adding element functions
@@ -355,8 +364,7 @@
             step.SetPosition(pos.x, pos.y)
             width, height = step.GetSize()
             step.SetSize(max(min_width, width), max(min_height, height))
-            self.Blocks.append(step)
-            self.Elements.append(step)
+            self.AddBlock(step)
             self.Controler.AddCurrentElementEditingStep(id)
             self.RefreshStepModel(step)
             self.Parent.RefreshProjectTree()
@@ -371,13 +379,12 @@
             dialog.SetStepNames([block.GetName() for block in self.Blocks if isinstance(block, SFC_Step)])
             if dialog.ShowModal() == wxID_OK:
                 name = dialog.GetValue()
-                if self.SelectedElement in self.Wires:
+                if self.IsWire(self.SelectedElement):
                     self.SelectedElement.SetSelectedSegment(None)
                     previous = self.SelectedElement.EndConnected
                     next = self.SelectedElement.StartConnected
                     self.SelectedElement.Clean()
-                    self.Wires.remove(self.SelectedElement)
-                    self.Elements.remove(self.SelectedElement)
+                    self.RemoveWire(self.SelectedElement)
                 else:
                     connectors = self.SelectedElement.GetConnectors()
                     if connectors["output"]:
@@ -388,8 +395,7 @@
                         wire = wires[0][0]
                         next = wire.StartConnected
                         wire.Clean()
-                        self.Wires.remove(wire)
-                        self.Elements.remove(wire)
+                        self.RemoveWire(wire)
                     else:
                         self.SelectedElement.AddOutput()
                         connectors = self.SelectedElement.GetConnectors()
@@ -407,7 +413,7 @@
                     step.RefreshModel()
                     step_connectors = step.GetConnectors()
                     transition = self.CreateTransition(step_connectors["output"], next)
-                if self.SelectedElement in self.Wires:
+                if self.IsWire(self.SelectedElement):
                     self.SelectedElement = wire
                     self.SelectedElement.SetSelectedSegment(0)
                 else:
@@ -439,8 +445,7 @@
                     wire = self.ConnectConnectors(actionblock_connector, connectors["action"])
                     wire.SetPoints([wxPoint(pos.x + SFC_WIRE_MIN_SIZE, pos.y), wxPoint(pos.x, pos.y)])
                     actionblock.SetActions(actions)
-                    self.Blocks.append(actionblock)
-                    self.Elements.append(actionblock)
+                    self.AddBlock(actionblock)
                     self.Controler.AddCurrentElementEditingActionBlock(id)
                     self.RefreshActionBlockModel(actionblock)
                     self.Refresh()
@@ -457,8 +462,7 @@
                         previous = self.SelectedElement.EndConnected
                         next = self.SelectedElement.StartConnected
                         self.SelectedElement.Clean()
-                        self.Wires.remove(self.SelectedElement)
-                        self.Elements.remove(self.SelectedElement)
+                        self.RemoveWire(self.SelectedElement)
                         self.SelectedElement = None
                     elif isinstance(self.SelectedElement, SFC_Step):
                         connectors = self.SelectedElement.GetConnectors()
@@ -470,8 +474,7 @@
                             wire = wires[0][0]
                             next = wire.StartConnected
                             wire.Clean()
-                            self.Wires.remove(wire)
-                            self.Elements.remove(wire)
+                            self.RemoveWire(wire)
                         else:
                             self.SelectedElement.AddOutput()
                             connectors = self.SelectedElement.GetConnectors()
@@ -490,8 +493,7 @@
                     wire = self.ConnectConnectors(divergence_connectors["inputs"][0], previous)
                     previous_block.RefreshOutputPosition()
                     wire.SetPoints([wxPoint(pos.x, pos.y + wire_size), wxPoint(pos.x, pos.y)])
-                    self.Blocks.append(divergence)
-                    self.Elements.append(divergence)
+                    self.AddBlock(divergence)
                     self.Controler.AddCurrentElementEditingDivergence(id, value["type"])
                     self.RefreshDivergenceModel(divergence)
                     for index, connector in enumerate(divergence_connectors["outputs"]):
@@ -515,8 +517,7 @@
                         previous = self.SelectedElement.EndConnected
                         next = self.SelectedElement.StartConnected
                         self.SelectedElement.Clean()
-                        self.Wires.remove(self.SelectedElement)
-                        self.Elements.remove(self.SelectedElement)
+                        self.RemoveWire(self.SelectedElement)
                         self.SelectedElement = None
                     elif isinstance(self.SelectedElement, SFC_Step):
                         connectors = self.SelectedElement.GetConnectors()
@@ -528,8 +529,7 @@
                             wire = wires[0][0]
                             next = wire.StartConnected
                             wire.Clean()
-                            self.Wires.remove(wire)
-                            self.Elements.remove(wire)
+                            self.RemoveWire(wire)
                         else:
                             self.SelectedElement.AddOutput()
                             connectors = self.SelectedElement.GetConnectors()
@@ -551,8 +551,7 @@
                     wire = self.ConnectConnectors(divergence_connectors["inputs"][0], previous)
                     previous_block.RefreshOutputPosition()
                     wire.SetPoints([wxPoint(pos.x, pos.y + wire_size), wxPoint(pos.x, pos.y)])
-                    self.Blocks.append(divergence)
-                    self.Elements.append(divergence)
+                    self.AddBlock(divergence)
                     self.Controler.AddCurrentElementEditingDivergence(id, value["type"])
                     self.RefreshDivergenceModel(divergence)
                     for index, connector in enumerate(divergence_connectors["outputs"]):
@@ -587,8 +586,7 @@
                             wire = wires[0][0]
                             next = wire.StartConnected
                             wire.Clean()
-                            self.Wires.remove(wire)
-                            self.Elements.remove(wire)
+                            self.RemoveWire(wire)
                     inputs = []
                     for input in self.SelectedElement.GetElements():
                         input_connectors = input.GetConnectors()
@@ -624,8 +622,7 @@
                     divergence.RefreshPosition()
                     pos = divergence_connectors["outputs"][0].GetRelPosition()
                     divergence.MoveConnector(divergence_connectors["outputs"][0], - pos.x)
-                    self.Blocks.append(divergence)
-                    self.Elements.append(divergence)
+                    self.AddBlock(divergence)
                     self.Controler.AddCurrentElementEditingDivergence(id, value["type"])
                     self.RefreshDivergenceModel(divergence)
                     if next:
@@ -681,8 +678,7 @@
                 jump = SFC_Jump(self, value, id)
                 pos = transition_connectors["output"].GetPosition(False)
                 jump.SetPosition(pos.x, pos.y + SFC_WIRE_MIN_SIZE)
-                self.Blocks.append(jump)
-                self.Elements.append(jump)
+                self.AddBlock(jump)
                 self.Controler.AddCurrentElementEditingJump(id)
                 jump_connector = jump.GetConnector()
                 wire = self.ConnectConnectors(jump_connector, transition_connectors["output"])
@@ -701,7 +697,7 @@
             comment.SetPosition(bbox.x, bbox.y)
             min_width, min_height = comment.GetMinSize()
             comment.SetSize(max(min_width,bbox.width),max(min_height,bbox.height))
-            self.Elements.append(comment)
+            self.AddComment(comment)
             self.Controler.AddCurrentElementEditingComment(id)
             self.RefreshCommentModel(comment)
             self.Refresh()
@@ -801,8 +797,7 @@
                 if next_block:
                     if isinstance(next_block, SFC_Divergence) and next_block.GetType() == SIMULTANEOUS_CONVERGENCE and isinstance(previous_block, SFC_Divergence) and previous_block.GetType() == SIMULTANEOUS_DIVERGENCE:
                         wire.Clean()
-                        self.Wires.remove(wire)
-                        self.Elements.remove(wire)
+                        self.RemoveWire(wire)
                         next_block.RemoveBranch(next)
                         if next_block.GetBranchNumber() < 2:
                             self.DeleteDivergence(next_block)
@@ -850,18 +845,15 @@
                     return
                 wire = wires[0][0]
                 wire.Clean()
-                self.Wires.remove(wire)
-                self.Elements.remove(wire)
+                self.RemoveWire(wire)
                 wires = next.GetWires()
                 if len(wires) != 1:
                     return
                 wire = wires[0][0]
                 wire.Clean()
-                self.Wires.remove(wire)
-                self.Elements.remove(wire)
+                self.RemoveWire(wire)
                 transition.Clean()
-                self.Blocks.remove(transition)
-                self.Elements.remove(transition)
+                self.AddBlock(transition)
                 self.Controler.RemoveCurrentElementEditingInstance(transition.GetId())
                 previous_block.RemoveBranch(previous)
                 if previous_block.GetBranchNumber() < 2:
@@ -890,8 +882,7 @@
                     next_block = next.GetParentBlock()
                     wire = wires[0][0]
                     wire.Clean()
-                    self.Wires.remove(wire)
-                    self.Elements.remove(wire)
+                    self.RemoveWire(wire)
                 else:
                     next = None
                     next_block = None
@@ -901,8 +892,7 @@
                         wire = wires[0][0]
                         previous = wires[0][0].EndConnected
                         wire.Clean()
-                        self.Wires.remove(wire)
-                        self.Elements.remove(wire)
+                        self.RemoveWire(wire)
                     else:
                         if type == SELECTION_CONVERGENCE:
                             wires = connector.GetWires()
@@ -913,8 +903,7 @@
                         previous_connector = wire.EndConnected
                         previous_block = previous_connector.GetParentBlock()
                         wire.Clean()
-                        self.Wires.remove(wire)
-                        self.Elements.remove(wire)
+                        self.RemoveWire(wire)
                         if isinstance(previous_block, SFC_Step):
                             previous_block.RemoveOutput()
                             self.RefreshStepModel(previous_block)
@@ -928,8 +917,7 @@
                             else:
                                 self.DeleteDivergence(previous_block)
                 divergence.Clean()
-                self.Blocks.remove(divergence)
-                self.Elements.remove(divergence)
+                self.AddBlocks(divergence)
                 self.Controler.RemoveCurrentElementEditingInstance(divergence.GetId())
                 if next:
                     wire = self.ConnectConnectors(next, previous)
@@ -951,8 +939,7 @@
                 previous = wire.EndConnected
                 previous_block = previous.GetParentBlock()
                 wire.Clean()
-                self.Wires.remove(wire)
-                self.Elements.remove(wire)
+                self.RemoveWire(wire)
                 wires = connectors["outputs"][0].GetWires()
                 if len(wires) != 1:
                     return
@@ -960,11 +947,9 @@
                 next = wire.StartConnected
                 next_block = next.GetParentBlock()
                 wire.Clean()
-                self.Wires.remove(wire)
-                self.Elements.remove(wire)
+                self.RemoveWire(wire)
                 divergence.Clean()
-                self.Blocks.remove(divergence)
-                self.Elements.remove(divergence)
+                self.AddBlock(divergence)
                 self.Controler.RemoveCurrentElementEditingInstance(divergence.GetId())
                 wire = self.ConnectConnectors(next, previous)
                 previous_pos = previous.GetPosition(False)
@@ -996,11 +981,9 @@
                 return
             wire = wires[0][0]
             wire.Clean()
-            self.Wires.remove(wire)
-            self.Elements.remove(wire)
+            self.RemoveWire(wire)
             jump.Clean()
-            self.Blocks.remove(jump)
-            self.Elements.remove(jump)
+            self.AddBlocks(jump)
             self.Controler.RemoveCurrentElementEditingInstance(jump.GetId())
             if isinstance(previous_block, SFC_Step):
                 previous_block.RemoveOutput()
@@ -1027,11 +1010,9 @@
             wire = wires[0][0]
             step = wire.EndConnected.GetParentBlock()
             wire.Clean()
-            self.Wires.remove(wire)
-            self.Elements.remove(wire)
+            self.RemoveWire(wire)
             actionblock.Clean()
-            self.Blocks.remove(actionblock)
-            self.Elements.remove(actionblock)
+            self.AddBlock(actionblock)
             self.Controler.RemoveCurrentElementEditingInstance(actionblock.GetId())
             step.RemoveAction()
             self.RefreshStepModel(step)