Adding support for:
authorlbessard
Thu, 09 Apr 2009 15:30:05 +0200
changeset 339 d4977f6d1621
parent 338 87e5015330ae
child 340 5a305b7c6735
Adding support for:
- change wire color to red while uncompatible types
- move element groups according to grid scaling
Bugs fixed
graphics/GraphicCommons.py
--- a/graphics/GraphicCommons.py	Tue Mar 24 17:31:42 2009 +0100
+++ b/graphics/GraphicCommons.py	Thu Apr 09 15:30:05 2009 +0200
@@ -472,10 +472,11 @@
         return rect
     
     def Refresh(self, rect = None):
-        if rect is not None:
-            self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False)
-        else:
-            self.Parent.RefreshRect(self.Parent.GetScrolledRect(self.GetRedrawRect()), False)
+        if self.Visible:
+            if rect is not None:
+                self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False)
+            else:
+                self.Parent.RefreshRect(self.Parent.GetScrolledRect(self.GetRedrawRect()), False)
     
     # Change the variable that indicates if this element is selected
     def SetSelected(self, selected):
@@ -639,7 +640,6 @@
                 scaling = (scaling[0] * width_fac, scaling[1] * height_fac)
             x = y = start_x = start_y = 0
             width, height = start_width, start_height = self.GetSize()
-            proportion = float(start_width) / float(start_height)
             if handle[0] == 1:
                 movex = max(-self.BoundingBox.x, movex)
                 if scaling is not None:
@@ -773,6 +773,7 @@
     def Clone(self, parent, pos = None):
         group = Graphic_Group(parent)
         connectors = {}
+        exclude_names = {}
         wires = []
         if pos is not None:
             dx, dy = pos.x - self.BoundingBox.x, pos.y - self.BoundingBox.y
@@ -785,10 +786,12 @@
                     new_pos = wx.Point(x + dx, y + dy)
                     newid = parent.GetNewId()
                     if parent.IsNamedElement(element):
-                        name = parent.GenerateNewName(element)
+                        name = parent.GenerateNewName(element, exclude_names)
+                        exclude_names[name.upper()] = True
                         new_element = element.Clone(parent, newid, name, pos = new_pos)
                     else:
                         new_element = element.Clone(parent, newid, pos = new_pos)
+                    new_element.AdjustToScaling(parent.Scaling)
                 else:
                     new_element = element.Clone(parent)
                 connectors.update(element.GetConnectorTranslation(new_element))
@@ -975,11 +978,40 @@
             movey_max = max(movey_max, abs(movey))
         return movex_max, movey_max
     
+    # Refreshes the group elements to move defined and handle selected
+    def ProcessDragging(self, movex, movey, event, scaling):
+        handle_type, handle = self.Handle
+        # If it is a move handle, Move this group elements
+        if handle_type == HANDLE_MOVE:
+            movex = max(-self.BoundingBox.x, movex)
+            movey = max(-self.BoundingBox.y, movey)
+            if scaling is not None:
+                movex = round_scaling(movex, scaling[0])
+                movey = round_scaling(movey, scaling[1])
+            if event.ControlDown():
+                self.CurrentDrag.x = self.CurrentDrag.x + movex
+                self.CurrentDrag.y = self.CurrentDrag.y + movey
+                if abs(self.CurrentDrag.x) > abs(self.CurrentDrag.y):
+                    movex = self.StartPos.x + self.CurrentDrag.x - self.Pos.x
+                    movey = self.StartPos.y - self.Pos.y
+                else:
+                    movex = self.StartPos.x - self.Pos.x
+                    movey = self.StartPos.y + self.CurrentDrag.y - self.Pos.y
+            self.Move(movex, movey)
+            return movex, movey
+        return 0, 0
+    
     # Change the variable that indicates if this element is highlighted
     def SetHighlighted(self, highlighted):
         for element in self.Elements:
             element.SetHighlighted(highlighted)
 
+    # Method called when a LeftDown event have been generated
+    def OnLeftDown(self, event, dc, scaling):
+        Graphic_Element.OnLeftDown(self, event, dc, scaling)
+        for element in self.Elements:
+            element.Handle = self.Handle
+
     # Change the variable that indicates if the elemente is selected
     def SetSelected(self, selected):
         for element in self.Elements:
@@ -1111,9 +1143,11 @@
     def RefreshValue(self):
         self.Value = self.ReceivingCurrent()
     
-    def MarkAsInvalid(self):
-        self.Valid = False
-    
+    def RefreshValid(self):
+        self.Valid = True
+        for wire, handle in self.Wires:
+            self.Valid &= wire.GetValid()
+        	
     def ReceivingCurrent(self):
         current = False
         for wire, handle in self.Wires:
@@ -1214,6 +1248,7 @@
         # If no wire defined, unconnect all wires
         if not wire:
             self.Wires = []
+        self.RefreshValid()
         self.ParentBlock.RefreshModel(False)
     
     # Returns if connector has one or more wire connected
@@ -1325,7 +1360,7 @@
     def AddError(self, infos, start, end):
         if len(infos) == 0:
             for wire, handle in self.Wires:
-                wire.MarkAsInvalid()
+                wire.SetValid(False)
         else:
             self.Errors[infos[0]] = (start, end)
     
@@ -1431,13 +1466,17 @@
     def CreateToolTip(self, pos):
         if self.Value is not None and self.Value != "undefined" and not isinstance(self.Value, BooleanType):
             if isinstance(self.Value, StringType):
-                self.ComputedValue = "\"%s\""%self.Value
+                computed_value = "\"%s\""%self.Value
             else:
-                self.ComputedValue = str(self.Value)
-            self.ToolTip = ToolTip(self.Parent, self.ComputedValue)
+                computed_value = str(self.Value)
+            self.ToolTip = ToolTip(self.Parent, computed_value)
             self.ToolTip.SetPosition(pos)
             self.ToolTip.Show()
     
+    def MoveToolTip(self, pos):
+        if self.ToolTip is not None:
+            self.ToolTip.SetPosition(pos)
+    
     def ClearToolTip(self):
         if self.ToolTip is not None:
             self.ToolTip.Destroy()
@@ -1606,7 +1645,7 @@
             if self.EndConnected:
                 self.EndConnected.RefreshValue()
             self.Refresh()
-            if isinstance(value, BooleanType):
+            if isinstance(value, BooleanType) and self.StartConnected is not None:
                 block = self.StartConnected.GetParentBlock()
                 block.SpreadCurrent()
     
@@ -1654,13 +1693,15 @@
         self.SelectedSegment = segment
         self.Refresh()
     
-    # Select a segment and not the whole wire. It's useful for Ladder Diagram
-    def MarkAsInvalid(self):
-        self.Valid = False
+    def SetValid(self, valid):
+        self.Valid = valid
         if self.StartConnected:
-            self.StartConnected.MarkAsInvalid()
+            self.StartConnected.RefreshValid()
         if self.EndConnected:
-            self.EndConnected.MarkAsInvalid()
+            self.EndConnected.RefreshValid()
+    
+    def GetValid(self):
+        return self.Valid
     
     # Reinitialize the wire points
     def ResetPoints(self):
@@ -2359,35 +2400,45 @@
             new_pos = wx.Point(self.Points[handle].x + movex, self.Points[handle].y + movey)
             connector = self.Parent.FindBlockConnector(new_pos, self.GetConnectionDirection())
             if connector:
-                if handle == 0 and self.EndConnected != connector and connector.IsCompatible(self.GetEndConnectedType()):
-                    connector.HighlightParentBlock(True)
-                    connector.Connect((self, handle))
-                    self.SetStartPointDirection(connector.GetDirection())
-                    self.ConnectStartPoint(connector.GetPosition(), connector)
-                    pos = connector.GetPosition()
-                    movex = pos.x - self.oldPos.x
-                    movey = pos.y - self.oldPos.y
-                    self.Dragging = False
-                elif handle != 0 and self.StartConnected != connector and connector.IsCompatible(self.GetStartConnectedType()):
-                    connector.HighlightParentBlock(True)
-                    connector.Connect((self, handle))
-                    self.SetEndPointDirection(connector.GetDirection())
-                    self.ConnectEndPoint(connector.GetPosition(), connector)
-                    pos = connector.GetPosition()
-                    movex = pos.x - self.oldPos.x
-                    movey = pos.y - self.oldPos.y
-                    self.Dragging = False
+                if handle == 0 and self.EndConnected != connector:
+                    if connector.IsCompatible(self.GetEndConnectedType()):
+                        connector.HighlightParentBlock(True)
+                        connector.Connect((self, handle))
+                        self.SetStartPointDirection(connector.GetDirection())
+                        self.ConnectStartPoint(connector.GetPosition(), connector)
+                        pos = connector.GetPosition()
+                        movex = pos.x - self.oldPos.x
+                        movey = pos.y - self.oldPos.y
+                        self.Dragging = False
+                    else:
+                        self.SetValid(False)
+                        self.MoveStartPoint(new_pos)
+                elif handle != 0 and self.StartConnected != connector:
+                    if connector.IsCompatible(self.GetStartConnectedType()):
+                        connector.HighlightParentBlock(True)
+                        connector.Connect((self, handle))
+                        self.SetEndPointDirection(connector.GetDirection())
+                        self.ConnectEndPoint(connector.GetPosition(), connector)
+                        pos = connector.GetPosition()
+                        movex = pos.x - self.oldPos.x
+                        movey = pos.y - self.oldPos.y
+                        self.Dragging = False
+                    else:
+                        self.SetValid(False)
+                        self.MoveEndPoint(new_pos)
                 elif handle == 0:
                     self.MoveStartPoint(new_pos)
                 else:
                     self.MoveEndPoint(new_pos)
             # If there is no connector, move the point
             elif handle == 0:
+                self.SetValid(True)
                 if self.StartConnected:
+                    self.StartConnected.HighlightParentBlock(False)
                     self.UnConnectStartPoint()
-                    self.StartConnected.HighlightParentBlock(False)
                 self.MoveStartPoint(new_pos)
             else:
+                self.SetValid(True)
                 if self.EndConnected:
                     self.EndConnected.HighlightParentBlock(False)
                     self.UnConnectEndPoint()
@@ -2436,13 +2487,16 @@
         Graphic_Element.Draw(self, dc)
         if not self.Valid:
             dc.SetPen(wx.RED_PEN)
+            dc.SetBrush(wx.RED_BRUSH)
         elif isinstance(self.Value, BooleanType) and self.Value:
             dc.SetPen(wx.GREEN_PEN)
+            dc.SetBrush(wx.GREEN_BRUSH)
         elif self.Value == "undefined":
             dc.SetPen(wx.Pen(wx.NamedColour("orange")))
+            dc.SetBrush(wx.Brush(wx.NamedColour("orange")))
         else:
             dc.SetPen(wx.BLACK_PEN)
-        dc.SetBrush(wx.BLACK_BRUSH)
+            dc.SetBrush(wx.BLACK_BRUSH)
         # Draw the start and end points if they are not connected or the mouse is over them
         if len(self.Points) > 0 and (not self.StartConnected or self.OverStart):
             dc.DrawCircle(self.Points[0].x, self.Points[0].y, POINT_RADIUS)