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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
import wx
import images
import random
#---------------------------------------------------------------------------
W = 2000
H = 2000
SW = 150
SH = 150
SHAPE_COUNT = 2500
hitradius = 5
#---------------------------------------------------------------------------
colours = [
"BLACK",
"BLUE",
"BLUE VIOLET",
"BROWN",
"CYAN",
"DARK GREY",
"DARK GREEN",
"GOLD",
"GREY",
"GREEN",
"MAGENTA",
"NAVY",
"PINK",
"RED",
"SKY BLUE",
"VIOLET",
"YELLOW",
]
class MyCanvas(wx.ScrolledWindow):
def __init__(self, parent, id, log, size = wx.DefaultSize):
wx.ScrolledWindow.__init__(self, parent, id, (0, 0), size=size, style=wx.SUNKEN_BORDER)
self.lines = []
self.maxWidth = W
self.maxHeight = H
self.x = self.y = 0
self.curLine = []
self.drawing = False
self.SetBackgroundColour("WHITE")
bmp = images.Test2.GetBitmap()
mask = wx.Mask(bmp, wx.BLUE)
bmp.SetMask(mask)
self.bmp = bmp
self.SetVirtualSize((self.maxWidth, self.maxHeight))
self.SetScrollRate(20,20)
# create a PseudoDC to record our drawing
self.pdc = wx.PseudoDC()
self.pen_cache = {}
self.brush_cache = {}
self.DoDrawing(self.pdc)
log.write('Created PseudoDC draw list with %d operations!'%self.pdc.GetLen())
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, lambda x:None)
self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouse)
# vars for handling mouse clicks
self.dragid = -1
self.lastpos = (0,0)
def ConvertEventCoords(self, event):
xView, yView = self.GetViewStart()
xDelta, yDelta = self.GetScrollPixelsPerUnit()
return (event.GetX() + (xView * xDelta),
event.GetY() + (yView * yDelta))
def OffsetRect(self, r):
xView, yView = self.GetViewStart()
xDelta, yDelta = self.GetScrollPixelsPerUnit()
r.OffsetXY(-(xView*xDelta),-(yView*yDelta))
def OnMouse(self, event):
global hitradius
if event.LeftDown():
x,y = self.ConvertEventCoords(event)
#l = self.pdc.FindObjectsByBBox(x, y)
l = self.pdc.FindObjects(x, y, hitradius)
for id in l:
if not self.pdc.GetIdGreyedOut(id):
self.dragid = id
self.lastpos = (event.GetX(),event.GetY())
break
elif event.RightDown():
x,y = self.ConvertEventCoords(event)
#l = self.pdc.FindObjectsByBBox(x, y)
l = self.pdc.FindObjects(x, y, hitradius)
if l:
self.pdc.SetIdGreyedOut(l[0], not self.pdc.GetIdGreyedOut(l[0]))
r = self.pdc.GetIdBounds(l[0])
r.Inflate(4,4)
self.OffsetRect(r)
self.RefreshRect(r, False)
elif event.Dragging() or event.LeftUp():
if self.dragid != -1:
x,y = self.lastpos
dx = event.GetX() - x
dy = event.GetY() - y
r = self.pdc.GetIdBounds(self.dragid)
self.pdc.TranslateId(self.dragid, dx, dy)
r2 = self.pdc.GetIdBounds(self.dragid)
r = r.Union(r2)
r.Inflate(4,4)
self.OffsetRect(r)
self.RefreshRect(r, False)
self.lastpos = (event.GetX(),event.GetY())
if event.LeftUp():
self.dragid = -1
def RandomPen(self):
c = random.choice(colours)
t = random.randint(1, 4)
if not self.pen_cache.has_key( (c, t) ):
self.pen_cache[(c, t)] = wx.Pen(c, t)
return self.pen_cache[(c, t)]
def RandomBrush(self):
c = random.choice(colours)
if not self.brush_cache.has_key(c):
self.brush_cache[c] = wx.Brush(c)
return self.brush_cache[c]
def RandomColor(self):
return random.choice(colours)
def OnPaint(self, event):
# Create a buffered paint DC. It will create the real
# wx.PaintDC and then blit the bitmap to it when dc is
# deleted.
dc = wx.BufferedPaintDC(self)
# use PrepateDC to set position correctly
self.PrepareDC(dc)
# we need to clear the dc BEFORE calling PrepareDC
bg = wx.Brush(self.GetBackgroundColour())
dc.SetBackground(bg)
dc.Clear()
# create a clipping rect from our position and size
# and the Update Region
xv, yv = self.GetViewStart()
dx, dy = self.GetScrollPixelsPerUnit()
x, y = (xv * dx, yv * dy)
rgn = self.GetUpdateRegion()
rgn.Offset(x,y)
r = rgn.GetBox()
# draw to the dc using the calculated clipping rect
self.pdc.DrawToDCClipped(dc,r)
def DoDrawing(self, dc):
random.seed()
self.objids = []
self.boundsdict = {}
dc.BeginDrawing()
for i in range(SHAPE_COUNT):
id = wx.NewId()
dc.SetId(id)
choice = random.randint(0,8)
if choice in (0,1):
x = random.randint(0, W)
y = random.randint(0, H)
pen = self.RandomPen()
dc.SetPen(pen)
dc.DrawPoint(x,y)
r = wx.Rect(x,y,1,1)
r.Inflate(pen.GetWidth(),pen.GetWidth())
dc.SetIdBounds(id,r)
elif choice in (2,3):
x1 = random.randint(0, W-SW)
y1 = random.randint(0, H-SH)
x2 = random.randint(x1, x1+SW)
y2 = random.randint(y1, y1+SH)
pen = self.RandomPen()
dc.SetPen(pen)
dc.DrawLine(x1,y1,x2,y2)
r = wx.Rect(x1,y1,x2-x1,y2-y1)
r.Inflate(pen.GetWidth(),pen.GetWidth())
dc.SetIdBounds(id,r)
elif choice in (4,5):
w = random.randint(10, SW)
h = random.randint(10, SH)
x = random.randint(0, W - w)
y = random.randint(0, H - h)
pen = self.RandomPen()
dc.SetPen(pen)
dc.SetBrush(self.RandomBrush())
dc.DrawRectangle(x,y,w,h)
r = wx.Rect(x,y,w,h)
r.Inflate(pen.GetWidth(),pen.GetWidth())
dc.SetIdBounds(id,r)
self.objids.append(id)
elif choice == 6:
Np = 8 # number of characters in text
word = []
for i in range(Np):
c = chr( random.randint(48, 122) )
word.append( c )
word = "".join(word)
w,h = self.GetFullTextExtent(word)[0:2]
x = random.randint(0, W-w)
y = random.randint(0, H-h)
dc.SetFont(self.GetFont())
dc.SetTextForeground(self.RandomColor())
dc.SetTextBackground(self.RandomColor())
dc.DrawText(word, x, y)
r = wx.Rect(x,y,w,h)
r.Inflate(2,2)
dc.SetIdBounds(id, r)
self.objids.append(id)
elif choice == 7:
Np = 8 # number of points per polygon
poly = []
minx = SW
miny = SH
maxx = 0
maxy = 0
for i in range(Np):
x = random.randint(0, SW)
y = random.randint(0, SH)
if x < minx: minx = x
if x > maxx: maxx = x
if y < miny: miny = y
if y > maxy: maxy = y
poly.append(wx.Point(x,y))
x = random.randint(0, W-SW)
y = random.randint(0, H-SH)
pen = self.RandomPen()
dc.SetPen(pen)
dc.SetBrush(self.RandomBrush())
dc.DrawPolygon(poly, x,y)
r = wx.Rect(minx+x,miny+y,maxx-minx,maxy-miny)
r.Inflate(pen.GetWidth(),pen.GetWidth())
dc.SetIdBounds(id,r)
self.objids.append(id)
elif choice == 8:
w,h = self.bmp.GetSize()
x = random.randint(0, W-w)
y = random.randint(0, H-h)
dc.DrawBitmap(self.bmp,x,y,True)
dc.SetIdBounds(id,wx.Rect(x,y,w,h))
self.objids.append(id)
dc.EndDrawing()
class ControlPanel(wx.Panel):
def __init__(self, parent, id, pos=wx.DefaultPosition,
size=wx.DefaultSize, style = wx.TAB_TRAVERSAL):
wx.Panel.__init__(self,parent,id,pos,size,style)
lbl = wx.StaticText(self, wx.ID_ANY, "Hit Test Radius: ")
lbl2 = wx.StaticText(self, wx.ID_ANY, "Left Click to drag, Right Click to enable/disable")
sc = wx.SpinCtrl(self, wx.ID_ANY, "5")
sc.SetRange(0,100)
global hitradius
sc.SetValue(hitradius)
self.sc = sc
sz = wx.BoxSizer(wx.HORIZONTAL)
sz.Add(lbl,0,wx.EXPAND)
sz.Add(sc,0)
sz.Add(lbl2,0,wx.LEFT,5)
sz.Add((10,10),1,wx.EXPAND)
self.SetSizerAndFit(sz)
sc.Bind(wx.EVT_SPINCTRL,self.OnChange)
sc.Bind(wx.EVT_TEXT,self.OnChange)
def OnChange(self, event):
global hitradius
hitradius = self.sc.GetValue()
#---------------------------------------------------------------------------
def runTest(frame, nb, log):
pnl = wx.Panel(nb, wx.ID_ANY,size=(200,30))
pnl2 = ControlPanel(pnl,wx.ID_ANY)
win = MyCanvas(pnl, wx.ID_ANY, log)
sz = wx.BoxSizer(wx.VERTICAL)
sz.Add(pnl2,0,wx.EXPAND|wx.ALL,5)
sz.Add(win,1,wx.EXPAND)
pnl.SetSizerAndFit(sz)
return pnl
#---------------------------------------------------------------------------
overview = """
<html>
<body>
<h2>wx.PseudoDC</h2>
The wx.PseudoDC class provides a way to record operations on a DC and then
play them back later. The PseudoDC can be passed to a drawing routine as
if it were a real DC. All Drawing methods are supported except Blit but
GetXXX methods are not supported and none of the drawing methods return
a value. The PseudoDC records the drawing to an operation
list. The operations can be played back to a real DC using:<pre>
DrawToDC(dc)
</pre>
The operations can be tagged with an id in order to associated them with a
specific object. To do this use:<pre>
SetId(id)
</pre>
Every operation after this will be associated with id until SetId is called
again. The PseudoDC also supports object level clipping. To enable this use:<pre>
SetIdBounds(id,rect)
</pre>
for each object that should be clipped. Then use:<pre>
DrawToDCClipped(dc, clippingRect)
</pre>
To draw the PseudoDC to a real dc. This is useful for large scrolled windows
where many objects are offscreen.
Objects can be moved around without re-drawing using:<pre>
TranslateId(id, dx, dy)
</pre>
To re-draw an object use:<pre>
ClearId(id)
SetId(id)
</pre>
and then re-draw the object.
</body>
</html>
"""
if __name__ == '__main__':
import sys,os
import run
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|