graphics/GraphicCommons.py
changeset 1173 ad09b4a755ce
parent 1170 074e46cdedbc
child 1176 f4b434672204
equal deleted inserted replaced
1172:dff0a4e40808 1173:ad09b4a755ce
   433             DEBUG_REFRESH_LOCK.release()
   433             DEBUG_REFRESH_LOCK.release()
   434             self.HasAcquiredLock = False
   434             self.HasAcquiredLock = False
   435         if gettime() - self.LastRefreshTime > REFRESH_PERIOD:
   435         if gettime() - self.LastRefreshTime > REFRESH_PERIOD:
   436             self.LastRefreshTime = gettime()
   436             self.LastRefreshTime = gettime()
   437         self.AccessLock.release()
   437         self.AccessLock.release()
   438 
       
   439 #-------------------------------------------------------------------------------
       
   440 #                               Viewer Rubberband
       
   441 #-------------------------------------------------------------------------------
       
   442 
       
   443 """
       
   444 Class that implements a rubberband
       
   445 """
       
   446 
       
   447 class RubberBand:
       
   448     
       
   449     # Create a rubberband by indicated on which window it must be drawn
       
   450     def __init__(self, viewer):
       
   451         self.Viewer = viewer
       
   452         self.drawingSurface = viewer.Editor
       
   453         self.Reset()
       
   454     
       
   455     # Method that initializes the internal attributes of the rubberband
       
   456     def Reset(self):
       
   457         self.startPoint = None
       
   458         self.currentBox = None
       
   459         self.lastBox = None
       
   460     
       
   461     # Method that return if a box is currently edited
       
   462     def IsShown(self):
       
   463         return self.currentBox != None
       
   464     
       
   465     # Method that returns the currently edited box
       
   466     def GetCurrentExtent(self):
       
   467         if self.currentBox is None:
       
   468             return self.lastBox
       
   469         return self.currentBox
       
   470     
       
   471     # Method called when a new box starts to be edited
       
   472     def OnLeftDown(self, event, dc, scaling):
       
   473         pos = GetScaledEventPosition(event, dc, scaling)
       
   474         # Save the point for calculate the box position and size
       
   475         self.startPoint = pos
       
   476         self.currentBox = wx.Rect(pos.x, pos.y, 0, 0)
       
   477         self.drawingSurface.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))
       
   478         self.Redraw()
       
   479     
       
   480     # Method called when dragging with a box edited
       
   481     def OnMotion(self, event, dc, scaling):
       
   482         pos = GetScaledEventPosition(event, dc, scaling)
       
   483         # Save the last position and size of the box for erasing it
       
   484         self.lastBox = wx.Rect(self.currentBox.x, self.currentBox.y, self.currentBox.width,
       
   485             self.currentBox.height)
       
   486         # Calculate new position and size of the box 
       
   487         if pos.x >= self.startPoint.x:
       
   488             self.currentBox.x = self.startPoint.x
       
   489             self.currentBox.width = pos.x - self.startPoint.x + 1
       
   490         else:
       
   491             self.currentBox.x = pos.x
       
   492             self.currentBox.width = self.startPoint.x - pos.x + 1
       
   493         if pos.y >= self.startPoint.y:
       
   494             self.currentBox.y = self.startPoint.y
       
   495             self.currentBox.height = pos.y - self.startPoint.y + 1
       
   496         else:
       
   497             self.currentBox.y = pos.y
       
   498             self.currentBox.height = self.startPoint.y - pos.y + 1
       
   499         self.Redraw()
       
   500     
       
   501     # Method called when dragging is stopped
       
   502     def OnLeftUp(self, event, dc, scaling):
       
   503         self.drawingSurface.SetCursor(wx.NullCursor)
       
   504         self.lastBox = self.currentBox
       
   505         self.currentBox = None
       
   506         self.Redraw()
       
   507 
       
   508     # Method that erase the last box and draw the new box
       
   509     def Redraw(self, dc = None):
       
   510         if dc is None:
       
   511             dc = self.Viewer.GetLogicalDC()
       
   512         scalex, scaley = dc.GetUserScale()
       
   513         dc.SetUserScale(1, 1)
       
   514         dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT))
       
   515         dc.SetBrush(wx.TRANSPARENT_BRUSH)
       
   516         dc.SetLogicalFunction(wx.XOR)
       
   517         if self.lastBox:
       
   518             # Erase last box
       
   519             dc.DrawRectangle(self.lastBox.x * scalex, self.lastBox.y * scaley, 
       
   520                              self.lastBox.width * scalex, self.lastBox.height * scaley)
       
   521         if self.currentBox:
       
   522             # Draw current box
       
   523             dc.DrawRectangle(self.currentBox.x * scalex, self.currentBox.y * scaley, 
       
   524                              self.currentBox.width * scalex, self.currentBox.height * scaley)
       
   525         dc.SetUserScale(scalex, scaley)
       
   526     
       
   527     # Erase last box
       
   528     def Erase(self, dc = None):
       
   529         if dc is None:
       
   530             dc = self.Viewer.GetLogicalDC()
       
   531         scalex, scaley = dc.GetUserScale()
       
   532         dc.SetUserScale(1, 1)
       
   533         dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT))
       
   534         dc.SetBrush(wx.TRANSPARENT_BRUSH)
       
   535         dc.SetLogicalFunction(wx.XOR)
       
   536         if self.lastBox:
       
   537             dc.DrawRectangle(self.lastBox.x * scalex, self.lastBox.y * scaley, 
       
   538                              self.lastBox.width * scalex, self.lastBox.height * scalex)
       
   539         dc.SetUserScale(scalex, scaley)
       
   540 
       
   541     # Draw current box
       
   542     def Draw(self, dc = None):
       
   543         if dc is None:
       
   544             dc = self.Viewer.GetLogicalDC()
       
   545         scalex, scaley = dc.GetUserScale()
       
   546         dc.SetUserScale(1, 1)
       
   547         dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT))
       
   548         dc.SetBrush(wx.TRANSPARENT_BRUSH)
       
   549         dc.SetLogicalFunction(wx.XOR)
       
   550         if self.currentBox:
       
   551             # Draw current box
       
   552             dc.DrawRectangle(self.currentBox.x * scalex, self.currentBox.y * scaley, 
       
   553                              self.currentBox.width * scalex, self.currentBox.height * scaley)
       
   554         dc.SetUserScale(scalex, scaley)
       
   555 
   438 
   556 #-------------------------------------------------------------------------------
   439 #-------------------------------------------------------------------------------
   557 #                    Helpers for highlighting text
   440 #                    Helpers for highlighting text
   558 #-------------------------------------------------------------------------------
   441 #-------------------------------------------------------------------------------
   559 
   442