1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
"""
Part of the floatcanvas.Utilities package.
This module contains assorted GUI-related utilities that can be used
with FloatCanvas
So far, they are:
RubberBandBox: used to draw a RubberBand Box on the screen
"""
import numpy as np
import wx
from wx.lib.floatcanvas import FloatCanvas, GUIMode
class RubberBandBox(GUIMode.GUIBase):
"""
Class to provide a GUI Mode that makes a rubber band box that can be drawn on a Window
"""
def __init__(self, CallBack, Tol=5):
"""
To initialize:
RubberBandBox(CallBack, Tol=5)
CallBack: is the method you want called when the mouse is
released. That method will be called, passing in a rect
parameter, where rect is: (Point, WH) of the rect in
world coords.
Tol: The tolerance for the smallest rectangle allowed. defaults
to 5. In pixels
Attributes:
CallBack: The callback function.
"""
self.Canvas = None # this will be set when the mode is set on a Canvas
self.CallBack = CallBack
self.Tol = Tol
self.Drawing = False
self.RBRect = None
self.StartPointWorld = None
return None
def OnMove(self, event):
if self.Drawing:
x, y = self.StartPoint
Cornerx, Cornery = event.GetPosition()
w, h = ( Cornerx - x, Cornery - y)
if abs(w) > self.Tol and abs(h) > self.Tol:
# draw the RB box
dc = wx.ClientDC(self.Canvas)
dc.SetPen(wx.Pen('WHITE', 2, wx.SHORT_DASH))
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.SetLogicalFunction(wx.XOR)
if self.RBRect:
dc.DrawRectanglePointSize(*self.RBRect)
self.RBRect = ((x, y), (w, h) )
dc.DrawRectanglePointSize(*self.RBRect)
self.Canvas._RaiseMouseEvent(event,FloatCanvas.EVT_FC_MOTION)
def OnLeftDown(self, event):
# Start drawing
self.Drawing = True
self.StartPoint = event.GetPosition()
def OnLeftUp(self, event):
# Stop Drawing
if self.Drawing:
self.Drawing = False
if self.RBRect:
world_rect = (self.Canvas.PixelToWorld(self.RBRect[0]),
self.Canvas.ScalePixelToWorld(self.RBRect[1])
)
wx.CallAfter(self.CallBack, world_rect)
self.RBRect = None
|