Adding support for drag and drop wire, step and transition in debug mode
authorlbessard
Thu, 09 Apr 2009 15:39:45 +0200
changeset 343 dc8ff76b39fd
parent 342 25e093542b40
child 344 0acc2bd658f0
Adding support for drag and drop wire, step and transition in debug mode
Bug with new name generating while copying groups fixed
Viewer.py
--- a/Viewer.py	Thu Apr 09 15:36:37 2009 +0200
+++ b/Viewer.py	Thu Apr 09 15:39:45 2009 +0200
@@ -531,7 +531,34 @@
             wire.TestVisible(screen)
         for block in self.Blocks:
             block.TestVisible(screen)
-            
+    
+    def GetElementIECPath(self, element):
+        iec_path = None
+        if isinstance(element, Wire) and element.EndConnected is not None:
+            block = element.EndConnected.GetParentBlock()
+            if isinstance(block, FBD_Block):
+                blockname = block.GetName()
+                connectorname = element.EndConnected.GetName()
+                if blockname != "":
+                    iec_path = "%s.%s.%s"%(self.InstancePath, blockname, connectorname)
+                else:
+                    if connectorname == "":
+                        iec_path = "%s.%s%d"%(self.InstancePath, block.GetType(), block.GetId())
+                    else:
+                        iec_path = "%s.%s%d_%s"%(self.InstancePath, block.GetType(), block.GetId(), connectorname)
+            elif isinstance(block, FBD_Variable):
+                iec_path = "%s.%s"%(self.InstancePath, block.GetName())
+        elif isinstance(element, (LD_Contact, LD_Coil)):
+            iec_path = "%s.%s"%(self.InstancePath, element.GetName())
+        elif isinstance(element, SFC_Step):
+            iec_path = "%s.%s.X"%(self.InstancePath, element.GetName())
+        elif isinstance(element, SFC_Transition):
+            connectors = element.GetConnectors()
+            previous_steps = self.GetPreviousSteps(connectors["input"])
+            next_steps = self.GetNextSteps(connectors["output"])
+            iec_path = "%s.%s->%s"%(self.InstancePath, ",".join(previous_steps), ",".join(next_steps))
+        return iec_path
+       
 #-------------------------------------------------------------------------------
 #                              Reset functions
 #-------------------------------------------------------------------------------
@@ -636,7 +663,7 @@
         
         for wire in self.Wires:
             if not wire.IsConnectedCompatible():
-                wire.MarkAsInvalid()
+                wire.SetValid(False)
             if self.Debug:
                 block = wire.EndConnected.GetParentBlock()
                 if isinstance(block, LD_PowerRail):
@@ -645,7 +672,7 @@
                     blockname = block.GetName()
                     connectorname = wire.EndConnected.GetName()
                     if blockname != "":
-                        iec_path = "%s.%s.%s"%(self.InstancePath, block.GetName(), connectorname)
+                        iec_path = "%s.%s.%s"%(self.InstancePath, blockname, connectorname)
                     else:
                         if connectorname == "":
                             iec_path = "%s.%s%d"%(self.InstancePath, block.GetType(), block.GetId())
@@ -667,20 +694,10 @@
         if self.Debug:
             for block in self.Blocks.keys():
                 block.SpreadCurrent()
-                if isinstance(block, LD_Contact):
-                    iec_path = "%s.%s"%(self.InstancePath, block.GetName())
-                    if self.Controler.SubscribeDebugIECVariable(iec_path.upper(), block) is not None:
-                        self.Subscribed[block] = iec_path.upper()
-                elif isinstance(block, SFC_Step):
-                    iec_path = "%s.%s.X"%(self.InstancePath, block.GetName())
-                    if self.Controler.SubscribeDebugIECVariable(iec_path.upper(), block) is not None:
-                        self.Subscribed[block] = iec_path.upper()
-                elif isinstance(block, SFC_Transition):
-                    connectors = block.GetConnectors()
-                    previous_steps = self.GetPreviousSteps(connectors["input"])
-                    next_steps = self.GetNextSteps(connectors["output"])
-                    iec_path = "%s.%s->%s"%(self.InstancePath, ",".join(previous_steps), ",".join(next_steps))
-                    if self.Controler.SubscribeDebugIECVariable(iec_path.upper(), block) is not None:
+                iec_path = self.GetElementIECPath(block)
+                if iec_path is not None:
+                    result = self.Controler.SubscribeDebugIECVariable(iec_path.upper(), block)
+                    if result is not None:
                         self.Subscribed[block] = iec_path.upper()
                 
         self.RefreshVisibleElements()
@@ -1370,6 +1387,7 @@
                     if element is not None:
                         self.SelectedElement = element
                         if self.Debug:
+                            self.StartMousePos = event.GetPosition()
                             Graphic_Element.OnLeftDown(self.SelectedElement, event, dc, self.Scaling)
                         else:
                             self.SelectedElement.OnLeftDown(event, dc, self.Scaling)
@@ -1385,6 +1403,7 @@
         event.Skip()
 
     def OnViewerLeftUp(self, event):
+        self.StartMousePos = None
         if self.rubberBand.IsShown():
             if self.Mode == MODE_SELECTION:
                 elements = self.SearchElements(self.rubberBand.GetCurrentExtent())
@@ -1503,10 +1522,21 @@
     def OnViewerLeftDClick(self, event):
         if self.Mode == MODE_SELECTION and self.SelectedElement is not None:
             if self.Debug:
-                Graphic_Element.OnLeftDClick(self.SelectedElement, event, self.GetLogicalDC(), self.Scaling)
+                iec_path = self.GetElementIECPath(self.SelectedElement)
+                if iec_path is not None:
+                    if isinstance(self.SelectedElement, Wire):
+                        if self.SelectedElement.EndConnected is not None:
+                            var_type = self.SelectedElement.EndConnected.GetType()
+                            if self.Controler.IsOfType(var_type, "ANY_NUM", self.Debug) or\
+                               self.Controler.IsOfType(var_type, "ANY_BIT", self.Debug):
+                                self.ParentWindow.OpenGraphicViewer(iec_path)
+                    else:
+                        self.ParentWindow.OpenGraphicViewer(iec_path)
             elif event.ControlDown() and not event.ShiftDown():
                 if self.IsBlock(self.SelectedElement) and self.SelectedElement.GetType() in self.Controler.GetProjectPouNames(self.Debug):
                     self.ParentWindow.EditProjectElement(ITEM_POU, self.SelectedElement.GetType())
+                else:
+                    self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling)
             elif event.ControlDown() and event.ShiftDown():
                 movex, movey = self.SelectedElement.AdjustToScaling(self.Scaling)
                 self.SelectedElement.RefreshModel()
@@ -1537,19 +1567,22 @@
                 self.RefreshVisibleElements()
         else:
             if not event.Dragging():
+                if self.Debug:
+                    tooltip_pos = self.ClientToScreen(event.GetPosition())
+                    tooltip_pos.x += 10
+                    tooltip_pos.y += 10
                 highlighted = self.FindElement(pos) 
                 if self.HighlightedElement is not None and self.HighlightedElement != highlighted:
-                    if isinstance(self.HighlightedElement, Wire):
+                    if self.Debug and isinstance(self.HighlightedElement, Wire):
                         self.HighlightedElement.ClearToolTip()
                     self.HighlightedElement.SetHighlighted(False)
                     self.HighlightedElement = None
                 if highlighted is not None and self.HighlightedElement != highlighted:
-                    if isinstance(highlighted, Wire):
-                        pos = self.ClientToScreen(event.GetPosition())
-                        pos.x += 10
-                        pos.y += 10
-                        highlighted.CreateToolTip(pos)
+                    if self.Debug and isinstance(highlighted, Wire):
+                        highlighted.CreateToolTip(tooltip_pos)
                     highlighted.SetHighlighted(True)
+                elif self.Debug and highlighted is not None and isinstance(highlighted, Wire):
+                    highlighted.MoveToolTip(tooltip_pos)
                 self.HighlightedElement = highlighted
             if self.rubberBand.IsShown():
                 self.rubberBand.OnMotion(event, dc, self.Scaling)
@@ -1566,10 +1599,26 @@
                     movex, movey = self.SelectedElement.OnMotion(event, dc, self.Scaling)
                     if movex != 0 or movey != 0:
                         self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(movex, movey)), False)
+            elif self.Debug and self.StartMousePos is not None and event.Dragging():
+                pos = event.GetPosition()
+                if abs(self.StartMousePos.x - pos.x) > 5 or abs(self.StartMousePos.y - pos.y) > 5:
+                    iec_path = self.GetElementIECPath(self.SelectedElement)
+                    if iec_path is not None:
+                        self.StartMousePos = None
+                        if self.HighlightedElement is not None:
+                            if isinstance(self.HighlightedElement, Wire):
+                                self.HighlightedElement.ClearToolTip()
+                            self.HighlightedElement.SetHighlighted(False)
+                            self.HighlightedElement = None
+                        data = wx.TextDataObject(str((iec_path, "debug")))
+                        dragSource = wx.DropSource(self)
+                        dragSource.SetData(data)
+                        dragSource.DoDragDrop()
             self.UpdateScrollPos(event)
         event.Skip()
 
     def OnLeaveViewer(self, event):
+        self.StartMousePos = None
         if self.SelectedElement is not None and self.SelectedElement.GetDragging():
             event.Skip()
         elif self.HighlightedElement is not None:
@@ -2606,13 +2655,13 @@
             else:
                 new_pos = wx.Point(max(30, x), max(30, y))
             block = self.CopyBlock(element, new_pos)
+            self.RefreshVisibleElements()
             if self.SelectedElement is not None:
                 self.SelectedElement.SetSelected(False)
             self.SelectedElement = block
             self.SelectedElement.SetSelected(True)
             self.RefreshBuffer()
             self.RefreshScrollBars()
-            self.RefreshVisibleElements()
             self.ParentWindow.RefreshVariablePanel(self.TagName)
             self.ParentWindow.RefreshInstancesTree()
         else:
@@ -2631,15 +2680,16 @@
             return True
         return False
 
-    def GenerateNewName(self, element):
+    def GenerateNewName(self, element, exclude={}):
+        names = exclude.copy()
         if isinstance(element, FBD_Block):
-            names = [varname.upper() for varname in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]
+            names.update(dict([(varname.upper(), True) for varname in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]))
             format = "Block%d"
         elif isinstance(element, SFC_Step):
-            names = [block.GetName().upper() for block in self.Blocks if isinstance(block, SFC_Step)]
+            names.update(dict([block.GetName().upper() for block in self.Blocks if isinstance(block, SFC_Step)]))
             format = "Step%d"
         i = 1
-        while (format%i).upper() in names:
+        while names.get((format%i).upper(), False):
             i += 1
         return format%i