graphics/GraphicCommons.py
changeset 339 d4977f6d1621
parent 338 87e5015330ae
child 353 f2be2307d666
equal deleted inserted replaced
338:87e5015330ae 339:d4977f6d1621
   470         rect.width = self.BoundingBox.width + 2 * (HANDLE_SIZE + abs(movex)) + 4
   470         rect.width = self.BoundingBox.width + 2 * (HANDLE_SIZE + abs(movex)) + 4
   471         rect.height = self.BoundingBox.height + 2 * (HANDLE_SIZE + abs(movey)) + 4
   471         rect.height = self.BoundingBox.height + 2 * (HANDLE_SIZE + abs(movey)) + 4
   472         return rect
   472         return rect
   473     
   473     
   474     def Refresh(self, rect = None):
   474     def Refresh(self, rect = None):
   475         if rect is not None:
   475         if self.Visible:
   476             self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False)
   476             if rect is not None:
   477         else:
   477                 self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False)
   478             self.Parent.RefreshRect(self.Parent.GetScrolledRect(self.GetRedrawRect()), False)
   478             else:
       
   479                 self.Parent.RefreshRect(self.Parent.GetScrolledRect(self.GetRedrawRect()), False)
   479     
   480     
   480     # Change the variable that indicates if this element is selected
   481     # Change the variable that indicates if this element is selected
   481     def SetSelected(self, selected):
   482     def SetSelected(self, selected):
   482         self.Selected = selected
   483         self.Selected = selected
   483         self.Refresh()
   484         self.Refresh()
   637         if handle_type == HANDLE_RESIZE:
   638         if handle_type == HANDLE_RESIZE:
   638             if scaling is not None:
   639             if scaling is not None:
   639                 scaling = (scaling[0] * width_fac, scaling[1] * height_fac)
   640                 scaling = (scaling[0] * width_fac, scaling[1] * height_fac)
   640             x = y = start_x = start_y = 0
   641             x = y = start_x = start_y = 0
   641             width, height = start_width, start_height = self.GetSize()
   642             width, height = start_width, start_height = self.GetSize()
   642             proportion = float(start_width) / float(start_height)
       
   643             if handle[0] == 1:
   643             if handle[0] == 1:
   644                 movex = max(-self.BoundingBox.x, movex)
   644                 movex = max(-self.BoundingBox.x, movex)
   645                 if scaling is not None:
   645                 if scaling is not None:
   646                     movex = -(round_scaling(width - movex, scaling[0]) - width)
   646                     movex = -(round_scaling(width - movex, scaling[0]) - width)
   647                 x = movex
   647                 x = movex
   771     
   771     
   772     # Make a clone of this element
   772     # Make a clone of this element
   773     def Clone(self, parent, pos = None):
   773     def Clone(self, parent, pos = None):
   774         group = Graphic_Group(parent)
   774         group = Graphic_Group(parent)
   775         connectors = {}
   775         connectors = {}
       
   776         exclude_names = {}
   776         wires = []
   777         wires = []
   777         if pos is not None:
   778         if pos is not None:
   778             dx, dy = pos.x - self.BoundingBox.x, pos.y - self.BoundingBox.y
   779             dx, dy = pos.x - self.BoundingBox.x, pos.y - self.BoundingBox.y
   779         for element in self.Elements:
   780         for element in self.Elements:
   780             if isinstance(element, Wire):
   781             if isinstance(element, Wire):
   783                 if pos is not None:
   784                 if pos is not None:
   784                     x, y = element.GetPosition()
   785                     x, y = element.GetPosition()
   785                     new_pos = wx.Point(x + dx, y + dy)
   786                     new_pos = wx.Point(x + dx, y + dy)
   786                     newid = parent.GetNewId()
   787                     newid = parent.GetNewId()
   787                     if parent.IsNamedElement(element):
   788                     if parent.IsNamedElement(element):
   788                         name = parent.GenerateNewName(element)
   789                         name = parent.GenerateNewName(element, exclude_names)
       
   790                         exclude_names[name.upper()] = True
   789                         new_element = element.Clone(parent, newid, name, pos = new_pos)
   791                         new_element = element.Clone(parent, newid, name, pos = new_pos)
   790                     else:
   792                     else:
   791                         new_element = element.Clone(parent, newid, pos = new_pos)
   793                         new_element = element.Clone(parent, newid, pos = new_pos)
       
   794                     new_element.AdjustToScaling(parent.Scaling)
   792                 else:
   795                 else:
   793                     new_element = element.Clone(parent)
   796                     new_element = element.Clone(parent)
   794                 connectors.update(element.GetConnectorTranslation(new_element))
   797                 connectors.update(element.GetConnectorTranslation(new_element))
   795                 group.SelectElement(new_element)
   798                 group.SelectElement(new_element)
   796         for element in wires:
   799         for element in wires:
   973             movex, movey = element.AdjustToScaling(scaling)
   976             movex, movey = element.AdjustToScaling(scaling)
   974             movex_max = max(movex_max, abs(movex))
   977             movex_max = max(movex_max, abs(movex))
   975             movey_max = max(movey_max, abs(movey))
   978             movey_max = max(movey_max, abs(movey))
   976         return movex_max, movey_max
   979         return movex_max, movey_max
   977     
   980     
       
   981     # Refreshes the group elements to move defined and handle selected
       
   982     def ProcessDragging(self, movex, movey, event, scaling):
       
   983         handle_type, handle = self.Handle
       
   984         # If it is a move handle, Move this group elements
       
   985         if handle_type == HANDLE_MOVE:
       
   986             movex = max(-self.BoundingBox.x, movex)
       
   987             movey = max(-self.BoundingBox.y, movey)
       
   988             if scaling is not None:
       
   989                 movex = round_scaling(movex, scaling[0])
       
   990                 movey = round_scaling(movey, scaling[1])
       
   991             if event.ControlDown():
       
   992                 self.CurrentDrag.x = self.CurrentDrag.x + movex
       
   993                 self.CurrentDrag.y = self.CurrentDrag.y + movey
       
   994                 if abs(self.CurrentDrag.x) > abs(self.CurrentDrag.y):
       
   995                     movex = self.StartPos.x + self.CurrentDrag.x - self.Pos.x
       
   996                     movey = self.StartPos.y - self.Pos.y
       
   997                 else:
       
   998                     movex = self.StartPos.x - self.Pos.x
       
   999                     movey = self.StartPos.y + self.CurrentDrag.y - self.Pos.y
       
  1000             self.Move(movex, movey)
       
  1001             return movex, movey
       
  1002         return 0, 0
       
  1003     
   978     # Change the variable that indicates if this element is highlighted
  1004     # Change the variable that indicates if this element is highlighted
   979     def SetHighlighted(self, highlighted):
  1005     def SetHighlighted(self, highlighted):
   980         for element in self.Elements:
  1006         for element in self.Elements:
   981             element.SetHighlighted(highlighted)
  1007             element.SetHighlighted(highlighted)
       
  1008 
       
  1009     # Method called when a LeftDown event have been generated
       
  1010     def OnLeftDown(self, event, dc, scaling):
       
  1011         Graphic_Element.OnLeftDown(self, event, dc, scaling)
       
  1012         for element in self.Elements:
       
  1013             element.Handle = self.Handle
   982 
  1014 
   983     # Change the variable that indicates if the elemente is selected
  1015     # Change the variable that indicates if the elemente is selected
   984     def SetSelected(self, selected):
  1016     def SetSelected(self, selected):
   985         for element in self.Elements:
  1017         for element in self.Elements:
   986             element.SetSelected(selected)
  1018             element.SetSelected(selected)
  1109         self.RefreshNameSize()
  1141         self.RefreshNameSize()
  1110 
  1142 
  1111     def RefreshValue(self):
  1143     def RefreshValue(self):
  1112         self.Value = self.ReceivingCurrent()
  1144         self.Value = self.ReceivingCurrent()
  1113     
  1145     
  1114     def MarkAsInvalid(self):
  1146     def RefreshValid(self):
  1115         self.Valid = False
  1147         self.Valid = True
  1116     
  1148         for wire, handle in self.Wires:
       
  1149             self.Valid &= wire.GetValid()
       
  1150         	
  1117     def ReceivingCurrent(self):
  1151     def ReceivingCurrent(self):
  1118         current = False
  1152         current = False
  1119         for wire, handle in self.Wires:
  1153         for wire, handle in self.Wires:
  1120             value = wire.GetValue()
  1154             value = wire.GetValue()
  1121             if current != "undefined" and isinstance(value, BooleanType):
  1155             if current != "undefined" and isinstance(value, BooleanType):
  1212                     found = True
  1246                     found = True
  1213             i += 1
  1247             i += 1
  1214         # If no wire defined, unconnect all wires
  1248         # If no wire defined, unconnect all wires
  1215         if not wire:
  1249         if not wire:
  1216             self.Wires = []
  1250             self.Wires = []
       
  1251         self.RefreshValid()
  1217         self.ParentBlock.RefreshModel(False)
  1252         self.ParentBlock.RefreshModel(False)
  1218     
  1253     
  1219     # Returns if connector has one or more wire connected
  1254     # Returns if connector has one or more wire connected
  1220     def IsConnected(self):
  1255     def IsConnected(self):
  1221         return len(self.Wires) > 0
  1256         return len(self.Wires) > 0
  1323         dc.SetLogicalFunction(wx.COPY)
  1358         dc.SetLogicalFunction(wx.COPY)
  1324     
  1359     
  1325     def AddError(self, infos, start, end):
  1360     def AddError(self, infos, start, end):
  1326         if len(infos) == 0:
  1361         if len(infos) == 0:
  1327             for wire, handle in self.Wires:
  1362             for wire, handle in self.Wires:
  1328                 wire.MarkAsInvalid()
  1363                 wire.SetValid(False)
  1329         else:
  1364         else:
  1330             self.Errors[infos[0]] = (start, end)
  1365             self.Errors[infos[0]] = (start, end)
  1331     
  1366     
  1332     # Draws the connector
  1367     # Draws the connector
  1333     def Draw(self, dc):
  1368     def Draw(self, dc):
  1429         self.EndConnected = None
  1464         self.EndConnected = None
  1430     
  1465     
  1431     def CreateToolTip(self, pos):
  1466     def CreateToolTip(self, pos):
  1432         if self.Value is not None and self.Value != "undefined" and not isinstance(self.Value, BooleanType):
  1467         if self.Value is not None and self.Value != "undefined" and not isinstance(self.Value, BooleanType):
  1433             if isinstance(self.Value, StringType):
  1468             if isinstance(self.Value, StringType):
  1434                 self.ComputedValue = "\"%s\""%self.Value
  1469                 computed_value = "\"%s\""%self.Value
  1435             else:
  1470             else:
  1436                 self.ComputedValue = str(self.Value)
  1471                 computed_value = str(self.Value)
  1437             self.ToolTip = ToolTip(self.Parent, self.ComputedValue)
  1472             self.ToolTip = ToolTip(self.Parent, computed_value)
  1438             self.ToolTip.SetPosition(pos)
  1473             self.ToolTip.SetPosition(pos)
  1439             self.ToolTip.Show()
  1474             self.ToolTip.Show()
       
  1475     
       
  1476     def MoveToolTip(self, pos):
       
  1477         if self.ToolTip is not None:
       
  1478             self.ToolTip.SetPosition(pos)
  1440     
  1479     
  1441     def ClearToolTip(self):
  1480     def ClearToolTip(self):
  1442         if self.ToolTip is not None:
  1481         if self.ToolTip is not None:
  1443             self.ToolTip.Destroy()
  1482             self.ToolTip.Destroy()
  1444             self.ToolTip = None
  1483             self.ToolTip = None
  1604             if self.StartConnected:
  1643             if self.StartConnected:
  1605                 self.StartConnected.RefreshValue()
  1644                 self.StartConnected.RefreshValue()
  1606             if self.EndConnected:
  1645             if self.EndConnected:
  1607                 self.EndConnected.RefreshValue()
  1646                 self.EndConnected.RefreshValue()
  1608             self.Refresh()
  1647             self.Refresh()
  1609             if isinstance(value, BooleanType):
  1648             if isinstance(value, BooleanType) and self.StartConnected is not None:
  1610                 block = self.StartConnected.GetParentBlock()
  1649                 block = self.StartConnected.GetParentBlock()
  1611                 block.SpreadCurrent()
  1650                 block.SpreadCurrent()
  1612     
  1651     
  1613     def GetValue(self):
  1652     def GetValue(self):
  1614         return self.Value
  1653         return self.Value
  1652             if self.EndConnected:
  1691             if self.EndConnected:
  1653                 self.EndConnected.SetPen(wx.BLUE_PEN)
  1692                 self.EndConnected.SetPen(wx.BLUE_PEN)
  1654         self.SelectedSegment = segment
  1693         self.SelectedSegment = segment
  1655         self.Refresh()
  1694         self.Refresh()
  1656     
  1695     
  1657     # Select a segment and not the whole wire. It's useful for Ladder Diagram
  1696     def SetValid(self, valid):
  1658     def MarkAsInvalid(self):
  1697         self.Valid = valid
  1659         self.Valid = False
       
  1660         if self.StartConnected:
  1698         if self.StartConnected:
  1661             self.StartConnected.MarkAsInvalid()
  1699             self.StartConnected.RefreshValid()
  1662         if self.EndConnected:
  1700         if self.EndConnected:
  1663             self.EndConnected.MarkAsInvalid()
  1701             self.EndConnected.RefreshValid()
       
  1702     
       
  1703     def GetValid(self):
       
  1704         return self.Valid
  1664     
  1705     
  1665     # Reinitialize the wire points
  1706     # Reinitialize the wire points
  1666     def ResetPoints(self):
  1707     def ResetPoints(self):
  1667         if self.StartPoint and self.EndPoint:
  1708         if self.StartPoint and self.EndPoint:
  1668             self.Points = [self.StartPoint[0], self.EndPoint[0]]
  1709             self.Points = [self.StartPoint[0], self.EndPoint[0]]
  2357                 movey = round_scaling(self.Points[handle].y + movey, scaling[1]) - self.Points[handle].y
  2398                 movey = round_scaling(self.Points[handle].y + movey, scaling[1]) - self.Points[handle].y
  2358             # Try to connect point to a connector
  2399             # Try to connect point to a connector
  2359             new_pos = wx.Point(self.Points[handle].x + movex, self.Points[handle].y + movey)
  2400             new_pos = wx.Point(self.Points[handle].x + movex, self.Points[handle].y + movey)
  2360             connector = self.Parent.FindBlockConnector(new_pos, self.GetConnectionDirection())
  2401             connector = self.Parent.FindBlockConnector(new_pos, self.GetConnectionDirection())
  2361             if connector:
  2402             if connector:
  2362                 if handle == 0 and self.EndConnected != connector and connector.IsCompatible(self.GetEndConnectedType()):
  2403                 if handle == 0 and self.EndConnected != connector:
  2363                     connector.HighlightParentBlock(True)
  2404                     if connector.IsCompatible(self.GetEndConnectedType()):
  2364                     connector.Connect((self, handle))
  2405                         connector.HighlightParentBlock(True)
  2365                     self.SetStartPointDirection(connector.GetDirection())
  2406                         connector.Connect((self, handle))
  2366                     self.ConnectStartPoint(connector.GetPosition(), connector)
  2407                         self.SetStartPointDirection(connector.GetDirection())
  2367                     pos = connector.GetPosition()
  2408                         self.ConnectStartPoint(connector.GetPosition(), connector)
  2368                     movex = pos.x - self.oldPos.x
  2409                         pos = connector.GetPosition()
  2369                     movey = pos.y - self.oldPos.y
  2410                         movex = pos.x - self.oldPos.x
  2370                     self.Dragging = False
  2411                         movey = pos.y - self.oldPos.y
  2371                 elif handle != 0 and self.StartConnected != connector and connector.IsCompatible(self.GetStartConnectedType()):
  2412                         self.Dragging = False
  2372                     connector.HighlightParentBlock(True)
  2413                     else:
  2373                     connector.Connect((self, handle))
  2414                         self.SetValid(False)
  2374                     self.SetEndPointDirection(connector.GetDirection())
  2415                         self.MoveStartPoint(new_pos)
  2375                     self.ConnectEndPoint(connector.GetPosition(), connector)
  2416                 elif handle != 0 and self.StartConnected != connector:
  2376                     pos = connector.GetPosition()
  2417                     if connector.IsCompatible(self.GetStartConnectedType()):
  2377                     movex = pos.x - self.oldPos.x
  2418                         connector.HighlightParentBlock(True)
  2378                     movey = pos.y - self.oldPos.y
  2419                         connector.Connect((self, handle))
  2379                     self.Dragging = False
  2420                         self.SetEndPointDirection(connector.GetDirection())
       
  2421                         self.ConnectEndPoint(connector.GetPosition(), connector)
       
  2422                         pos = connector.GetPosition()
       
  2423                         movex = pos.x - self.oldPos.x
       
  2424                         movey = pos.y - self.oldPos.y
       
  2425                         self.Dragging = False
       
  2426                     else:
       
  2427                         self.SetValid(False)
       
  2428                         self.MoveEndPoint(new_pos)
  2380                 elif handle == 0:
  2429                 elif handle == 0:
  2381                     self.MoveStartPoint(new_pos)
  2430                     self.MoveStartPoint(new_pos)
  2382                 else:
  2431                 else:
  2383                     self.MoveEndPoint(new_pos)
  2432                     self.MoveEndPoint(new_pos)
  2384             # If there is no connector, move the point
  2433             # If there is no connector, move the point
  2385             elif handle == 0:
  2434             elif handle == 0:
       
  2435                 self.SetValid(True)
  2386                 if self.StartConnected:
  2436                 if self.StartConnected:
       
  2437                     self.StartConnected.HighlightParentBlock(False)
  2387                     self.UnConnectStartPoint()
  2438                     self.UnConnectStartPoint()
  2388                     self.StartConnected.HighlightParentBlock(False)
       
  2389                 self.MoveStartPoint(new_pos)
  2439                 self.MoveStartPoint(new_pos)
  2390             else:
  2440             else:
       
  2441                 self.SetValid(True)
  2391                 if self.EndConnected:
  2442                 if self.EndConnected:
  2392                     self.EndConnected.HighlightParentBlock(False)
  2443                     self.EndConnected.HighlightParentBlock(False)
  2393                     self.UnConnectEndPoint()
  2444                     self.UnConnectEndPoint()
  2394                 self.MoveEndPoint(new_pos)
  2445                 self.MoveEndPoint(new_pos)
  2395             return movex, movey
  2446             return movex, movey
  2434     # Draws the wire lines and points
  2485     # Draws the wire lines and points
  2435     def Draw(self, dc):
  2486     def Draw(self, dc):
  2436         Graphic_Element.Draw(self, dc)
  2487         Graphic_Element.Draw(self, dc)
  2437         if not self.Valid:
  2488         if not self.Valid:
  2438             dc.SetPen(wx.RED_PEN)
  2489             dc.SetPen(wx.RED_PEN)
       
  2490             dc.SetBrush(wx.RED_BRUSH)
  2439         elif isinstance(self.Value, BooleanType) and self.Value:
  2491         elif isinstance(self.Value, BooleanType) and self.Value:
  2440             dc.SetPen(wx.GREEN_PEN)
  2492             dc.SetPen(wx.GREEN_PEN)
       
  2493             dc.SetBrush(wx.GREEN_BRUSH)
  2441         elif self.Value == "undefined":
  2494         elif self.Value == "undefined":
  2442             dc.SetPen(wx.Pen(wx.NamedColour("orange")))
  2495             dc.SetPen(wx.Pen(wx.NamedColour("orange")))
       
  2496             dc.SetBrush(wx.Brush(wx.NamedColour("orange")))
  2443         else:
  2497         else:
  2444             dc.SetPen(wx.BLACK_PEN)
  2498             dc.SetPen(wx.BLACK_PEN)
  2445         dc.SetBrush(wx.BLACK_BRUSH)
  2499             dc.SetBrush(wx.BLACK_BRUSH)
  2446         # Draw the start and end points if they are not connected or the mouse is over them
  2500         # Draw the start and end points if they are not connected or the mouse is over them
  2447         if len(self.Points) > 0 and (not self.StartConnected or self.OverStart):
  2501         if len(self.Points) > 0 and (not self.StartConnected or self.OverStart):
  2448             dc.DrawCircle(self.Points[0].x, self.Points[0].y, POINT_RADIUS)
  2502             dc.DrawCircle(self.Points[0].x, self.Points[0].y, POINT_RADIUS)
  2449         if len(self.Points) > 1 and (not self.EndConnected or self.OverEnd):
  2503         if len(self.Points) > 1 and (not self.EndConnected or self.OverEnd):
  2450             dc.DrawCircle(self.Points[-1].x, self.Points[-1].y, POINT_RADIUS)
  2504             dc.DrawCircle(self.Points[-1].x, self.Points[-1].y, POINT_RADIUS)