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
|
from wxPython.wx import *
from wxPython.ogl import *
import images
#----------------------------------------------------------------------
# This creates some pens and brushes that the OGL library uses.
wxOGLInitialize()
#----------------------------------------------------------------------
class DiamondShape(wxPolygonShape):
def __init__(self, w=0.0, h=0.0):
wxPolygonShape.__init__(self)
if w == 0.0:
w = 60.0
if h == 0.0:
h = 60.0
## Either wxRealPoints or 2-tuples of floats works.
#points = [ wxRealPoint(0.0, -h/2.0),
# wxRealPoint(w/2.0, 0.0),
# wxRealPoint(0.0, h/2.0),
# wxRealPoint(-w/2.0, 0.0),
# ]
points = [ (0.0, -h/2.0),
(w/2.0, 0.0),
(0.0, h/2.0),
(-w/2.0, 0.0),
]
self.Create(points)
#----------------------------------------------------------------------
class RoundedRectangleShape(wxRectangleShape):
def __init__(self, w=0.0, h=0.0):
wxRectangleShape.__init__(self, w, h)
self.SetCornerRadius(-0.3)
#----------------------------------------------------------------------
class DividedShape(wxDividedShape):
def __init__(self, width, height, canvas):
wxDividedShape.__init__(self, width, height)
region1 = wxShapeRegion()
region1.SetText('wxDividedShape')
region1.SetProportions(0.0, 0.2)
region1.SetFormatMode(FORMAT_CENTRE_HORIZ)
self.AddRegion(region1)
region2 = wxShapeRegion()
region2.SetText('This is Region number two.')
region2.SetProportions(0.0, 0.3)
region2.SetFormatMode(FORMAT_CENTRE_HORIZ|FORMAT_CENTRE_VERT)
self.AddRegion(region2)
region3 = wxShapeRegion()
region3.SetText('Region 3\nwith embedded\nline breaks')
region3.SetProportions(0.0, 0.5)
region3.SetFormatMode(FORMAT_NONE)
self.AddRegion(region3)
self.SetRegionSizes()
self.ReformatRegions(canvas)
def ReformatRegions(self, canvas=None):
rnum = 0
if canvas is None:
canvas = self.GetCanvas()
dc = wxClientDC(canvas) # used for measuring
for region in self.GetRegions():
text = region.GetText()
self.FormatText(dc, text, rnum)
rnum += 1
def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
self.base_OnSizingEndDragLeft(pt, x, y, keys, attch)
self.SetRegionSizes()
self.ReformatRegions()
self.GetCanvas().Refresh()
#----------------------------------------------------------------------
class MyEvtHandler(wxShapeEvtHandler):
def __init__(self, log, frame):
wxShapeEvtHandler.__init__(self)
self.log = log
self.statbarFrame = frame
def UpdateStatusBar(self, shape):
x,y = shape.GetX(), shape.GetY()
width, height = shape.GetBoundingBoxMax()
self.statbarFrame.SetStatusText("Pos: (%d,%d) Size: (%d, %d)" %
(x, y, width, height))
def OnLeftClick(self, x, y, keys = 0, attachment = 0):
shape = self.GetShape()
print shape.__class__, shape.GetClassName()
regions = shape.GetRegions()
if regions:
#print regions,
print regions[0].GetFormattedText()
canvas = shape.GetCanvas()
dc = wxClientDC(canvas)
canvas.PrepareDC(dc)
if shape.Selected():
shape.Select(False, dc)
canvas.Redraw(dc)
else:
redraw = False
shapeList = canvas.GetDiagram().GetShapeList()
toUnselect = []
for s in shapeList:
if s.Selected():
# If we unselect it now then some of the objects in
# shapeList will become invalid (the control points are
# shapes too!) and bad things will happen...
toUnselect.append(s)
shape.Select(True, dc)
if toUnselect:
for s in toUnselect:
s.Select(False, dc)
canvas.Redraw(dc)
self.UpdateStatusBar(shape)
def OnEndDragLeft(self, x, y, keys = 0, attachment = 0):
shape = self.GetShape()
self.base_OnEndDragLeft(x, y, keys, attachment)
if not shape.Selected():
self.OnLeftClick(x, y, keys, attachment)
self.UpdateStatusBar(shape)
def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
self.base_OnSizingEndDragLeft(pt, x, y, keys, attch)
self.UpdateStatusBar(self.GetShape())
def OnMovePost(self, dc, x, y, oldX, oldY, display):
self.base_OnMovePost(dc, x, y, oldX, oldY, display)
self.UpdateStatusBar(self.GetShape())
def OnRightClick(self, *dontcare):
self.log.WriteText("%s\n" % self.GetShape())
#----------------------------------------------------------------------
class TestWindow(wxShapeCanvas):
def __init__(self, parent, log, frame):
wxShapeCanvas.__init__(self, parent)
maxWidth = 1000
maxHeight = 1000
self.SetScrollbars(20, 20, maxWidth/20, maxHeight/20)
self.log = log
self.frame = frame
self.SetBackgroundColour("LIGHT BLUE") #wxWHITE)
self.diagram = wxDiagram()
self.SetDiagram(self.diagram)
self.diagram.SetCanvas(self)
self.shapes = []
self.save_gdi = []
rRectBrush = wxBrush("MEDIUM TURQUOISE", wxSOLID)
dsBrush = wxBrush("WHEAT", wxSOLID)
self.MyAddShape(wxCircleShape(80), 100, 100, wxPen(wxBLUE, 3), wxGREEN_BRUSH, "Circle")
self.MyAddShape(wxRectangleShape(85, 50), 305, 60, wxBLACK_PEN, wxLIGHT_GREY_BRUSH, "Rectangle")
ds = self.MyAddShape(DividedShape(140, 150, self), 495, 145, wxBLACK_PEN, dsBrush, '')
self.MyAddShape(DiamondShape(90, 90), 345, 235, wxPen(wxBLUE, 3, wxDOT), wxRED_BRUSH, "Polygon")
self.MyAddShape(RoundedRectangleShape(95,70), 140, 255, wxPen(wxRED, 2), rRectBrush, "Rounded Rect")
bmp = images.getTest2Bitmap()
mask = wxMaskColour(bmp, wxBLUE)
bmp.SetMask(mask)
s = wxBitmapShape()
s.SetBitmap(bmp)
self.MyAddShape(s, 225, 150, None, None, "Bitmap")
dc = wxClientDC(self)
self.PrepareDC(dc)
for x in range(len(self.shapes)):
fromShape = self.shapes[x]
if x+1 == len(self.shapes):
toShape = self.shapes[0]
else:
toShape = self.shapes[x+1]
line = wxLineShape()
line.SetCanvas(self)
line.SetPen(wxBLACK_PEN)
line.SetBrush(wxBLACK_BRUSH)
line.AddArrow(ARROW_ARROW)
line.MakeLineControlPoints(2)
fromShape.AddLine(line, toShape)
self.diagram.AddShape(line)
line.Show(True)
# for some reason, the shapes have to be moved for the line to show up...
fromShape.Move(dc, fromShape.GetX(), fromShape.GetY())
EVT_WINDOW_DESTROY(self, self.OnDestroy)
def MyAddShape(self, shape, x, y, pen, brush, text):
shape.SetDraggable(True, True)
shape.SetCanvas(self)
shape.SetX(x)
shape.SetY(y)
if pen: shape.SetPen(pen)
if brush: shape.SetBrush(brush)
if text: shape.AddText(text)
#shape.SetShadowMode(SHADOW_RIGHT)
self.diagram.AddShape(shape)
shape.Show(True)
evthandler = MyEvtHandler(self.log, self.frame)
evthandler.SetShape(shape)
evthandler.SetPreviousHandler(shape.GetEventHandler())
shape.SetEventHandler(evthandler)
self.shapes.append(shape)
return shape
def OnDestroy(self, evt):
# Do some cleanup
for shape in self.diagram.GetShapeList():
if shape.GetParent() == None:
shape.SetCanvas(None)
shape.Destroy()
self.diagram.Destroy()
def OnBeginDragLeft(self, x, y, keys):
self.log.write("OnBeginDragLeft: %s, %s, %s\n" % (x, y, keys))
def OnEndDragLeft(self, x, y, keys):
self.log.write("OnEndDragLeft: %s, %s, %s\n" % (x, y, keys))
#----------------------------------------------------------------------
def runTest(frame, nb, log):
win = TestWindow(nb, log, frame)
return win
#----------------------------------------------------------------------
class __Cleanup:
cleanup = wxOGLCleanUp
def __del__(self):
self.cleanup()
# when this module gets cleaned up then wxOGLCleanUp() will get called
__cu = __Cleanup()
overview = """\
The Object Graphics Library is a library supporting the creation and
manipulation of simple and complex graphic images on a canvas.
"""
if __name__ == '__main__':
import sys,os
import run
run.main(['', os.path.basename(sys.argv[0])])
|