On 6/9/11 10:21 AM, Christopher Barker wrote:
I'd start by looking at the DrawObject base class,
I'd start be making your wx.Window a child of the FloatCanvas. Then in
the _Draw method you can set it's position and size and Show() it, to
see how that works.
OK -- I was thinking about this, s I figured I'd give it a try. It turns
out that it was almost trivial. All I had to do was set the Position,
and call .Refresh() -- that was pretty much it!
Enclosed is a sample that implements a new DrawObject: Widget -- in
theory, you could put an wxWindow in it and get a widget on the Canvas.
In practice, I've only tested the simplest example -- a basic wx.Button,
but it's working fine so far.
Please check it out, and hopefully add to it and test it more, and
report back here.
Once I'm a bit more comfortable that it works, I'll put in in the core
floatcanvas module.
HTH,
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
[email protected]
#!/usr/bin/env python
"""
A simple test and demo for putting Widgets on a Canvas
"""
import wx
import wx.lib.colourdb
## import the installed version
#from wx.lib.floatcanvas import NavCanvas, FloatCanvas
## import a local version
import sys
sys.path.append("../")
from floatcanvas import NavCanvas
from floatcanvas import FloatCanvas as FC
from floatcanvas.Utilities import BBox
import random
class Widget(FC.TextObjectMixin, FC.DrawObject):
"""
This class holds an arbitrary wx Widget (Control).
It will be placed at the given location, XY = (x, y) The "Position"
argument is a two character string, indicating where in relation
to the coordinates the widget should be oriented.
The first letter is: t, c, or b, for top, center and bottom The
second letter is: l, c, or r, for left, center and right The
position refers to the position relative to the text itself. It
defaults to "tl" (top left).
The size is fixed, and does not scale with the drawing.
"""
def __init__(self, Widget,
XY,
Position = 'tl',
InForeground = False):
FC.DrawObject.__init__(self,InForeground)
self.Widget = Widget
# Note the BB is just the point, as the size in World coordinates is
not fixed
self.BoundingBox = BBox.asBBox( (XY,XY) )
self.XY = XY
(self.Width, self.Height) = self.Widget.Size
self.ShiftFun = self.ShiftFunDict[Position]
def _Draw(self, dc , WorldToPixel, ScaleWorldToPixel, HTdc=None):
XY = WorldToPixel(self.XY)
XY = self.ShiftFun(XY[0], XY[1], self.Width, self.Height)
self.Widget.Position = XY
self.Widget.Refresh()
# note: Widgets are not hitable, as that should be taken care fo by wx
class DrawFrame(wx.Frame):
"""
A frame used for the FloatCanvas Demo
"""
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.CreateStatusBar()
## getting all the colors for random objects
wx.lib.colourdb.updateColourDB()
self.AllColors = wx.lib.colourdb.getColourList()
# Add the Canvas
Canvas = NavCanvas.NavCanvas(self,-1,
size = (500,500),
ProjectionFun = None,
Debug = 0,
BackgroundColor = "DARK SLATE BLUE",
).Canvas
self.Canvas = Canvas
FC.EVT_MOTION(self.Canvas, self.OnMove )
self.Rectangle = Canvas.AddRectangle((50, 20), (40,10),
FillColor="Red", LineStyle = None)
MyButton = wx.Button(self.Canvas, label="A Button")
MyButton.Bind(wx.EVT_BUTTON, self.OnButtonPress)
Canvas.AddObject(Widget(MyButton, (70, 25) ) )
self.Show()
Canvas.ZoomToBB()
def OnButtonPress(self, evt=None):
print "The button was pressed"
# Make the Rectangle and random color:
self.Rectangle.SetFillColor( random.choice(self.AllColors) )
self.Canvas.Draw(Force=True)
def OnMove(self, event):
"""
Updates the status bar with the world coordinates
"""
self.SetStatusText("%.2f, %.2f"%tuple(event.Coords))
app = wx.App(False)
F = DrawFrame(None, title="FloatCanvas Demo App", size=(700,700) )
app.MainLoop()
_______________________________________________
FloatCanvas mailing list
[email protected]
http://paulmcnett.com/cgi-bin/mailman/listinfo/floatcanvas