Viewer.py
changeset 563 3f92a5e18804
parent 550 cfa295862d55
child 566 6014ef82a98a
equal deleted inserted replaced
562:0ce12552cf36 563:3f92a5e18804
    63 if wx.Platform == '__WXMSW__':
    63 if wx.Platform == '__WXMSW__':
    64     faces = { 'times': 'Times New Roman',
    64     faces = { 'times': 'Times New Roman',
    65               'mono' : 'Courier New',
    65               'mono' : 'Courier New',
    66               'helv' : 'Arial',
    66               'helv' : 'Arial',
    67               'other': 'Comic Sans MS',
    67               'other': 'Comic Sans MS',
    68               'size' : 20,
    68               'size' : 10,
    69              }
    69              }
    70 else:
    70 else:
    71     faces = { 'times': 'Times',
    71     faces = { 'times': 'Times',
    72               'mono' : 'Courier',
    72               'mono' : 'Courier',
    73               'helv' : 'Helvetica',
    73               'helv' : 'Helvetica',
    74               'other': 'new century schoolbook',
    74               'other': 'new century schoolbook',
    75               'size' : 20,
    75               'size' : 12,
    76              }
    76              }
    77 
    77 
    78 ZOOM_FACTORS = [math.sqrt(2) ** x for x in xrange(-6, 7)]
    78 ZOOM_FACTORS = [math.sqrt(2) ** x for x in xrange(-6, 7)]
    79 
    79 
    80 
    80 
   755                 height = max(2, int(scaling[1] * self.ViewScale[1]))
   755                 height = max(2, int(scaling[1] * self.ViewScale[1]))
   756                 bitmap = wx.EmptyBitmap(width, height)
   756                 bitmap = wx.EmptyBitmap(width, height)
   757                 dc = wx.MemoryDC(bitmap)
   757                 dc = wx.MemoryDC(bitmap)
   758                 dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
   758                 dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
   759                 dc.Clear()
   759                 dc.Clear()
   760                 dc.SetPen(wx.Pen(wx.Colour(180, 180, 180)))
   760                 dc.SetPen(MiterPen(wx.Colour(180, 180, 180)))
   761                 dc.DrawPoint(0, 0)
   761                 dc.DrawPoint(0, 0)
   762                 self.GridBrush = wx.BrushFromBitmap(bitmap)
   762                 self.GridBrush = wx.BrushFromBitmap(bitmap)
   763             else:
   763             else:
   764                 self.GridBrush = wx.TRANSPARENT_BRUSH
   764                 self.GridBrush = wx.TRANSPARENT_BRUSH
   765         else:
   765         else:
   766             self.Scaling = None
   766             self.Scaling = None
   767             self.GridBrush = wx.TRANSPARENT_BRUSH
   767             self.GridBrush = wx.TRANSPARENT_BRUSH
   768         page_size = properties["pageSize"]
   768         page_size = properties["pageSize"]
   769         if page_size != (0, 0):
   769         if page_size != (0, 0):
   770             self.PageSize = map(int, page_size)
   770             self.PageSize = map(int, page_size)
   771             self.PagePen = wx.Pen(wx.Colour(180, 180, 180))
   771             self.PagePen = MiterPen(wx.Colour(180, 180, 180))
   772         else:
   772         else:
   773             self.PageSize = None
   773             self.PageSize = None
   774             self.PagePen = wx.TRANSPARENT_PEN
   774             self.PagePen = wx.TRANSPARENT_PEN
   775         if refresh:
   775         if refresh:
   776             self.RefreshVisibleElements()
   776             self.RefreshVisibleElements()
  1014 
  1014 
  1015 #-------------------------------------------------------------------------------
  1015 #-------------------------------------------------------------------------------
  1016 #                          Search Element functions
  1016 #                          Search Element functions
  1017 #-------------------------------------------------------------------------------
  1017 #-------------------------------------------------------------------------------
  1018 
  1018 
  1019     def FindBlock(self, pos):
  1019     def FindBlock(self, event):
       
  1020         dc = self.GetLogicalDC()
       
  1021         pos = event.GetLogicalPosition(dc)
  1020         for block in self.Blocks.itervalues():
  1022         for block in self.Blocks.itervalues():
  1021             if block.HitTest(pos) or block.TestHandle(pos) != (0, 0):
  1023             if block.HitTest(pos) or block.TestHandle(event) != (0, 0):
  1022                 return block
  1024                 return block
  1023         return None
  1025         return None
  1024     
  1026     
  1025     def FindWire(self, pos):
  1027     def FindWire(self, event):
       
  1028         dc = self.GetLogicalDC()
       
  1029         pos = event.GetLogicalPosition(dc)
  1026         for wire in self.Wires:
  1030         for wire in self.Wires:
  1027             if wire.HitTest(pos) or wire.TestHandle(pos) != (0, 0):
  1031             if wire.HitTest(pos) or wire.TestHandle(event) != (0, 0):
  1028                 return wire
  1032                 return wire
  1029         return None
  1033         return None
  1030     
  1034     
  1031     def FindElement(self, pos, exclude_group = False):
  1035     def FindElement(self, event, exclude_group = False):
       
  1036         dc = self.GetLogicalDC()
       
  1037         pos = event.GetLogicalPosition(dc)
  1032         if self.SelectedElement and not (exclude_group and isinstance(self.SelectedElement, Graphic_Group)):
  1038         if self.SelectedElement and not (exclude_group and isinstance(self.SelectedElement, Graphic_Group)):
  1033             if self.SelectedElement.HitTest(pos) or self.SelectedElement.TestHandle(pos) != (0, 0):
  1039             if self.SelectedElement.HitTest(pos) or self.SelectedElement.TestHandle(event) != (0, 0):
  1034                 return self.SelectedElement
  1040                 return self.SelectedElement
  1035         for element in self.GetElements():
  1041         for element in self.GetElements():
  1036             if element.HitTest(pos) or element.TestHandle(pos) != (0, 0):
  1042             if element.HitTest(pos) or element.TestHandle(event) != (0, 0):
  1037                 return element
  1043                 return element
  1038         return None
  1044         return None
  1039     
  1045     
  1040     def FindBlockConnector(self, pos, direction = None, exclude = None):
  1046     def FindBlockConnector(self, pos, direction = None, exclude = None):
  1041         for block in self.Blocks.itervalues():
  1047         for block in self.Blocks.itervalues():
  1316                         self.SelectedElement = element
  1322                         self.SelectedElement = element
  1317                     elif len(elements) == 1:
  1323                     elif len(elements) == 1:
  1318                         self.SelectedElement = elements[0]
  1324                         self.SelectedElement = elements[0]
  1319                     self.SelectedElement.SetSelected(True)
  1325                     self.SelectedElement.SetSelected(True)
  1320             else:
  1326             else:
  1321                 element = self.FindElement(pos)
  1327                 element = self.FindElement(event)
  1322                 if not self.Debug and (element is None or element.TestHandle(pos) == (0, 0)):
  1328                 if not self.Debug and (element is None or element.TestHandle(event) == (0, 0)):
  1323                     connector = self.FindBlockConnector(pos)
  1329                     connector = self.FindBlockConnector(pos)
  1324                 else:
  1330                 else:
  1325                     connector = None
  1331                     connector = None
  1326                 if not self.Debug and self.DrawingWire:
  1332                 if not self.Debug and self.DrawingWire:
  1327                     self.DrawingWire = False
  1333                     self.DrawingWire = False
  1328                     if self.SelectedElement is not None:
  1334                     if self.SelectedElement is not None:
  1329                         if element is None or element.TestHandle(pos) == (0, 0):
  1335                         if element is None or element.TestHandle(event) == (0, 0):
  1330                             connector = self.FindBlockConnector(pos, self.SelectedElement.GetConnectionDirection())
  1336                             connector = self.FindBlockConnector(pos, self.SelectedElement.GetConnectionDirection())
  1331                         if connector is not None:
  1337                         if connector is not None:
  1332                             event.Dragging = lambda : True
  1338                             event.Dragging = lambda : True
  1333                             self.SelectedElement.OnMotion(event, self.GetLogicalDC(), self.Scaling)
  1339                             self.SelectedElement.OnMotion(event, dc, self.Scaling)
  1334                         if self.SelectedElement.EndConnected is not None:
  1340                         if self.SelectedElement.EndConnected is not None:
  1335                             self.SelectedElement.ResetPoints()
  1341                             self.SelectedElement.ResetPoints()
  1336                             self.SelectedElement.GeneratePoints()
  1342                             self.SelectedElement.GeneratePoints()
  1337                             self.SelectedElement.RefreshModel()
  1343                             self.SelectedElement.RefreshModel()
  1338                             self.SelectedElement.SetSelected(True)
  1344                             self.SelectedElement.SetSelected(True)
  1344                             self.SelectedElement = None
  1350                             self.SelectedElement = None
  1345                             element = None
  1351                             element = None
  1346                             self.RefreshRect(self.GetScrolledRect(rect), False)
  1352                             self.RefreshRect(self.GetScrolledRect(rect), False)
  1347                 elif not self.Debug and connector is not None:
  1353                 elif not self.Debug and connector is not None:
  1348                     self.DrawingWire = True
  1354                     self.DrawingWire = True
  1349                     scaled_pos = GetScaledEventPosition(event, self.GetLogicalDC(), self.Scaling)
  1355                     scaled_pos = GetScaledEventPosition(event, dc, self.Scaling)
  1350                     if (connector.GetDirection() == EAST):
  1356                     if (connector.GetDirection() == EAST):
  1351                         wire = Wire(self, [wx.Point(pos.x, pos.y), EAST], [wx.Point(scaled_pos.x, scaled_pos.y), WEST])
  1357                         wire = Wire(self, [wx.Point(pos.x, pos.y), EAST], [wx.Point(scaled_pos.x, scaled_pos.y), WEST])
  1352                     else:
  1358                     else:
  1353                         wire = Wire(self, [wx.Point(pos.x, pos.y), WEST], [wx.Point(scaled_pos.x, scaled_pos.y), EAST])
  1359                         wire = Wire(self, [wx.Point(pos.x, pos.y), WEST], [wx.Point(scaled_pos.x, scaled_pos.y), EAST])
  1354                     wire.oldPos = scaled_pos
  1360                     wire.oldPos = scaled_pos
  1471         self.StartScreenPos = None
  1477         self.StartScreenPos = None
  1472         event.Skip()
  1478         event.Skip()
  1473     
  1479     
  1474     def OnViewerRightDown(self, event):
  1480     def OnViewerRightDown(self, event):
  1475         if self.Mode == MODE_SELECTION:
  1481         if self.Mode == MODE_SELECTION:
  1476             dc = self.GetLogicalDC()
  1482             element = self.FindElement(event)
  1477             pos = event.GetLogicalPosition(dc)
       
  1478             element = self.FindElement(pos)
       
  1479             if self.SelectedElement is not None and self.SelectedElement != element:
  1483             if self.SelectedElement is not None and self.SelectedElement != element:
  1480                 self.SelectedElement.SetSelected(False)
  1484                 self.SelectedElement.SetSelected(False)
  1481                 self.SelectedElement = None
  1485                 self.SelectedElement = None
  1482             if element:
  1486             if element:
  1483                 self.SelectedElement = element
  1487                 self.SelectedElement = element
  1484                 if self.Debug:
  1488                 if self.Debug:
  1485                     Graphic_Element.OnRightDown(self.SelectedElement, event, dc, self.Scaling)
  1489                     Graphic_Element.OnRightDown(self.SelectedElement, event, self.GetLogicalDC(), self.Scaling)
  1486                 else:
  1490                 else:
  1487                     self.SelectedElement.OnRightDown(event, dc, self.Scaling)
  1491                     self.SelectedElement.OnRightDown(event, self.GetLogicalDC(), self.Scaling)
  1488                 self.SelectedElement.Refresh()
  1492                 self.SelectedElement.Refresh()
  1489         event.Skip()
  1493         event.Skip()
  1490     
  1494     
  1491     def OnViewerRightUp(self, event):
  1495     def OnViewerRightUp(self, event):
  1492         dc = self.GetLogicalDC()
       
  1493         if self.SelectedElement is not None:
  1496         if self.SelectedElement is not None:
  1494             if self.Debug:
  1497             if self.Debug:
  1495                 Graphic_Element.OnRightUp(self.SelectedElement, event, dc, self.Scaling)
  1498                 Graphic_Element.OnRightUp(self.SelectedElement, event, self.GetLogicalDC(), self.Scaling)
  1496             else:
  1499             else:
  1497                 self.SelectedElement.OnRightUp(event, dc, self.Scaling)
  1500                 self.SelectedElement.OnRightUp(event, self.GetLogicalDC(), self.Scaling)
  1498             wx.CallAfter(self.SetCurrentCursor, 0)
  1501             wx.CallAfter(self.SetCurrentCursor, 0)
  1499         elif not self.Debug:
  1502         elif not self.Debug:
  1500             self.PopupDefaultMenu(False)
  1503             self.PopupDefaultMenu(False)
  1501         event.Skip()
  1504         event.Skip()
  1502     
  1505     
  1550             if not event.Dragging():
  1553             if not event.Dragging():
  1551                 if self.Debug:
  1554                 if self.Debug:
  1552                     tooltip_pos = self.ClientToScreen(event.GetPosition())
  1555                     tooltip_pos = self.ClientToScreen(event.GetPosition())
  1553                     tooltip_pos.x += 10
  1556                     tooltip_pos.x += 10
  1554                     tooltip_pos.y += 10
  1557                     tooltip_pos.y += 10
  1555                 highlighted = self.FindElement(pos) 
  1558                 highlighted = self.FindElement(event) 
  1556                 if self.HighlightedElement is not None and self.HighlightedElement != highlighted:
  1559                 if self.HighlightedElement is not None and self.HighlightedElement != highlighted:
  1557                     if self.Debug and isinstance(self.HighlightedElement, Wire):
  1560                     if self.Debug and isinstance(self.HighlightedElement, Wire):
  1558                         self.HighlightedElement.ClearToolTip()
  1561                         self.HighlightedElement.ClearToolTip()
  1559                     self.HighlightedElement.SetHighlighted(False)
  1562                     self.HighlightedElement.SetHighlighted(False)
  1560                     self.HighlightedElement = None
  1563                     self.HighlightedElement = None