Matt G wrote:
Anyone have any clues on how to do something like this?
yes.
I have an image in a wxScrolledWindow. Currently, for a crop feature, I"m capturing mouse ups and downs and then cropping the image. That's all working just fine. What I want to add is a little bounding box when you click and drag. Either a static one, or one where you can grab the corners and reshape the box. Anything really, to show the box that you're going to be cropping. Much like the gimp or photoshop does while cropping.
The short version is to draw the rectangle to the window with a
wxCleintDC, using
dc.SetLogicalFunction(wxXOR)
when you want it to go away, re-draw the same rectangle, and the image
will abe bakc where it was.
Here's a snippet of code from my FloatCanvas, that draws a box as you
move the mouse over the screen. I use it so the user can draw a box to
zoom in on. With some adaption, you could give the box handles to drag
to change the size, location, etc.:
def LeftButtonEvent(self,event):
if self.GUIMode:
if self.GUIMode == "ZoomIn":
if event.LeftDown():
self.StartRBBox = (event.GetX(),event.GetY())
self.PrevRBBox = None
elif event.Dragging() and event.LeftIsDown() and
self.StartRBBox:
x0,y0 = self.StartRBBox
x1,y1 = event.GetX(),event.GetY()
# these caluclations make sure the box has the same
aspect ratio as the window
w, h = abs(x1-x0),abs(y1-y0)
w = max(w,int(h*self.AspectRatio))
h = int(w/self.AspectRatio)
x_c, y_c = (x0+x1)/2 , (y0+y1)/2
dc = wxClientDC(self.DrawPanel)
dc.BeginDrawing()
dc.SetPen(wxPen(wxNamedColour('WHITE'),
2,wxSHORT_DASH))
dc.SetBrush(wxTRANSPARENT_BRUSH)
dc.SetLogicalFunction(wxXOR)
# if a box exists, draw over it before drawing a new
one.
if self.PrevRBBox:
dc.DrawRectangle(*self.PrevRBBox)
dc.DrawRectangle(x_c-w/2,y_c-h/2,w,h)
self.PrevRBBox = (x_c-w/2,y_c-h/2,w,h)
dc.EndDrawing()
elif event.LeftUp() and self.StartRBBox :
EndRBBox = (event.GetX(),event.GetY())
StartRBBox = self.StartRBBox
# if mouse has moved less that ten pixels, don't use
the box.
if abs(StartRBBox[0] - EndRBBox[0]) > 10 and
abs(StartRBBox[1] - EndRBBox[1]) > 10:
EndRBBox = self.PixelToWorld(EndRBBox)
StartRBBox = self.PixelToWorld(StartRBBox)
BB = array(((min(EndRBBox[0],StartRBBox[0]),
min(EndRBBox[1],StartRBBox[1])),
(max(EndRBBox[0],StartRBBox[0]),
max(EndRBBox[1],StartRBBox[1]))),Float)
self.ZoomToBB(BB)
else:
Center = self.PixelToWorld(StartRBBox)
self.Zoom(1.5,Center)
self.StartRBBox = None
-- --
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov