]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
1 | # 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
2 | # | |
3 | # o Updated for wx namespace | |
4 | # | |
e91a9dfc | 5 | |
8fa876ca RD |
6 | import wx |
7 | import wx.ogl as ogl | |
e91a9dfc | 8 | |
8fa876ca | 9 | import images |
96bfd053 | 10 | |
8fa876ca | 11 | ##wx.Trap() |
e91a9dfc | 12 | |
8fa876ca RD |
13 | #---------------------------------------------------------------------- |
14 | ||
15 | class DiamondShape(ogl.PolygonShape): | |
e91a9dfc | 16 | def __init__(self, w=0.0, h=0.0): |
8fa876ca | 17 | ogl.PolygonShape.__init__(self) |
e91a9dfc RD |
18 | if w == 0.0: |
19 | w = 60.0 | |
20 | if h == 0.0: | |
21 | h = 60.0 | |
22 | ||
8fa876ca | 23 | # Either ogl.RealPoints or 2-tuples of floats works. |
e91a9dfc | 24 | |
8fa876ca RD |
25 | #points = [ ogl.RealPoint(0.0, -h/2.0), |
26 | # ogl.RealPoint(w/2.0, 0.0), | |
27 | # ogl.RealPoint(0.0, h/2.0), | |
28 | # ogl.RealPoint(-w/2.0, 0.0), | |
e91a9dfc RD |
29 | # ] |
30 | points = [ (0.0, -h/2.0), | |
31 | (w/2.0, 0.0), | |
32 | (0.0, h/2.0), | |
33 | (-w/2.0, 0.0), | |
34 | ] | |
35 | ||
36 | self.Create(points) | |
37 | ||
38 | ||
39 | #---------------------------------------------------------------------- | |
40 | ||
8fa876ca | 41 | class RoundedRectangleShape(ogl.RectangleShape): |
e91a9dfc | 42 | def __init__(self, w=0.0, h=0.0): |
8fa876ca | 43 | ogl.RectangleShape.__init__(self, w, h) |
e91a9dfc RD |
44 | self.SetCornerRadius(-0.3) |
45 | ||
46 | ||
1e4a197e RD |
47 | #---------------------------------------------------------------------- |
48 | ||
8fa876ca | 49 | class DividedShape(ogl.DividedShape): |
1e4a197e | 50 | def __init__(self, width, height, canvas): |
8fa876ca | 51 | ogl.DividedShape.__init__(self, width, height) |
1e4a197e | 52 | |
8fa876ca RD |
53 | region1 = ogl.ShapeRegion() |
54 | region1.SetText('DividedShape') | |
1e4a197e | 55 | region1.SetProportions(0.0, 0.2) |
8fa876ca | 56 | region1.SetFormatMode(ogl.FORMAT_CENTRE_HORIZ) |
1e4a197e RD |
57 | self.AddRegion(region1) |
58 | ||
8fa876ca | 59 | region2 = ogl.ShapeRegion() |
1e4a197e RD |
60 | region2.SetText('This is Region number two.') |
61 | region2.SetProportions(0.0, 0.3) | |
8fa876ca | 62 | region2.SetFormatMode(ogl.FORMAT_CENTRE_HORIZ|ogl.FORMAT_CENTRE_VERT) |
1e4a197e RD |
63 | self.AddRegion(region2) |
64 | ||
8fa876ca | 65 | region3 = ogl.ShapeRegion() |
1e4a197e RD |
66 | region3.SetText('Region 3\nwith embedded\nline breaks') |
67 | region3.SetProportions(0.0, 0.5) | |
8fa876ca | 68 | region3.SetFormatMode(ogl.FORMAT_NONE) |
1e4a197e RD |
69 | self.AddRegion(region3) |
70 | ||
71 | self.SetRegionSizes() | |
72 | self.ReformatRegions(canvas) | |
73 | ||
74 | ||
75 | def ReformatRegions(self, canvas=None): | |
76 | rnum = 0 | |
8fa876ca | 77 | |
1e4a197e RD |
78 | if canvas is None: |
79 | canvas = self.GetCanvas() | |
8fa876ca RD |
80 | |
81 | dc = wx.ClientDC(canvas) # used for measuring | |
82 | ||
1e4a197e RD |
83 | for region in self.GetRegions(): |
84 | text = region.GetText() | |
85 | self.FormatText(dc, text, rnum) | |
86 | rnum += 1 | |
87 | ||
88 | ||
89 | def OnSizingEndDragLeft(self, pt, x, y, keys, attch): | |
d14a1e28 | 90 | print "***", self |
1e4a197e RD |
91 | self.base_OnSizingEndDragLeft(pt, x, y, keys, attch) |
92 | self.SetRegionSizes() | |
93 | self.ReformatRegions() | |
94 | self.GetCanvas().Refresh() | |
95 | ||
96 | ||
e91a9dfc RD |
97 | #---------------------------------------------------------------------- |
98 | ||
8fa876ca | 99 | class MyEvtHandler(ogl.ShapeEvtHandler): |
e91a9dfc | 100 | def __init__(self, log, frame): |
8fa876ca | 101 | ogl.ShapeEvtHandler.__init__(self) |
e91a9dfc RD |
102 | self.log = log |
103 | self.statbarFrame = frame | |
104 | ||
e91a9dfc RD |
105 | def UpdateStatusBar(self, shape): |
106 | x,y = shape.GetX(), shape.GetY() | |
107 | width, height = shape.GetBoundingBoxMax() | |
108 | self.statbarFrame.SetStatusText("Pos: (%d,%d) Size: (%d, %d)" % | |
109 | (x, y, width, height)) | |
110 | ||
111 | ||
112 | def OnLeftClick(self, x, y, keys = 0, attachment = 0): | |
113 | shape = self.GetShape() | |
1fded56b | 114 | print shape.__class__, shape.GetClassName() |
e91a9dfc | 115 | canvas = shape.GetCanvas() |
8fa876ca | 116 | dc = wx.ClientDC(canvas) |
e91a9dfc RD |
117 | canvas.PrepareDC(dc) |
118 | ||
119 | if shape.Selected(): | |
1e4a197e | 120 | shape.Select(False, dc) |
e91a9dfc RD |
121 | canvas.Redraw(dc) |
122 | else: | |
1e4a197e | 123 | redraw = False |
e91a9dfc RD |
124 | shapeList = canvas.GetDiagram().GetShapeList() |
125 | toUnselect = [] | |
8fa876ca | 126 | |
e91a9dfc RD |
127 | for s in shapeList: |
128 | if s.Selected(): | |
129 | # If we unselect it now then some of the objects in | |
130 | # shapeList will become invalid (the control points are | |
131 | # shapes too!) and bad things will happen... | |
132 | toUnselect.append(s) | |
133 | ||
1e4a197e | 134 | shape.Select(True, dc) |
e91a9dfc RD |
135 | |
136 | if toUnselect: | |
137 | for s in toUnselect: | |
1e4a197e | 138 | s.Select(False, dc) |
8fa876ca | 139 | |
e91a9dfc RD |
140 | canvas.Redraw(dc) |
141 | ||
142 | self.UpdateStatusBar(shape) | |
143 | ||
144 | ||
145 | def OnEndDragLeft(self, x, y, keys = 0, attachment = 0): | |
146 | shape = self.GetShape() | |
147 | self.base_OnEndDragLeft(x, y, keys, attachment) | |
8fa876ca | 148 | |
e91a9dfc RD |
149 | if not shape.Selected(): |
150 | self.OnLeftClick(x, y, keys, attachment) | |
8fa876ca | 151 | |
e91a9dfc RD |
152 | self.UpdateStatusBar(shape) |
153 | ||
154 | ||
1e4a197e RD |
155 | def OnSizingEndDragLeft(self, pt, x, y, keys, attch): |
156 | self.base_OnSizingEndDragLeft(pt, x, y, keys, attch) | |
e91a9dfc RD |
157 | self.UpdateStatusBar(self.GetShape()) |
158 | ||
159 | ||
1e4a197e RD |
160 | def OnMovePost(self, dc, x, y, oldX, oldY, display): |
161 | self.base_OnMovePost(dc, x, y, oldX, oldY, display) | |
162 | self.UpdateStatusBar(self.GetShape()) | |
e91a9dfc RD |
163 | |
164 | ||
165 | def OnRightClick(self, *dontcare): | |
166 | self.log.WriteText("%s\n" % self.GetShape()) | |
167 | ||
168 | ||
169 | #---------------------------------------------------------------------- | |
170 | ||
8fa876ca | 171 | class TestWindow(ogl.ShapeCanvas): |
e91a9dfc | 172 | def __init__(self, parent, log, frame): |
8fa876ca | 173 | ogl.ShapeCanvas.__init__(self, parent) |
e91a9dfc | 174 | |
f6bcfd97 BP |
175 | maxWidth = 1000 |
176 | maxHeight = 1000 | |
177 | self.SetScrollbars(20, 20, maxWidth/20, maxHeight/20) | |
178 | ||
e91a9dfc RD |
179 | self.log = log |
180 | self.frame = frame | |
8fa876ca RD |
181 | self.SetBackgroundColour("LIGHT BLUE") #wx.WHITE) |
182 | self.diagram = ogl.Diagram() | |
e91a9dfc RD |
183 | self.SetDiagram(self.diagram) |
184 | self.diagram.SetCanvas(self) | |
185 | self.shapes = [] | |
f3d9dc1d | 186 | self.save_gdi = [] |
e91a9dfc | 187 | |
8fa876ca RD |
188 | rRectBrush = wx.Brush("MEDIUM TURQUOISE", wx.SOLID) |
189 | dsBrush = wx.Brush("WHEAT", wx.SOLID) | |
190 | ||
191 | self.MyAddShape( | |
192 | ogl.CircleShape(80), | |
193 | 100, 100, wx.Pen(wx.BLUE, 3), wx.GREEN_BRUSH, "Circle" | |
194 | ) | |
195 | ||
196 | self.MyAddShape( | |
197 | ogl.RectangleShape(85, 50), | |
198 | 305, 60, wx.BLACK_PEN, wx.LIGHT_GREY_BRUSH, "Rectangle" | |
199 | ) | |
200 | ||
201 | ds = self.MyAddShape( | |
202 | DividedShape(140, 150, self), | |
203 | 495, 145, wx.BLACK_PEN, dsBrush, '' | |
204 | ) | |
205 | ||
206 | self.MyAddShape( | |
207 | DiamondShape(90, 90), | |
208 | 345, 235, wx.Pen(wx.BLUE, 3, wx.DOT), wx.RED_BRUSH, "Polygon" | |
209 | ) | |
210 | ||
211 | self.MyAddShape( | |
212 | RoundedRectangleShape(95,70), | |
213 | 140, 255, wx.Pen(wx.RED, 2), rRectBrush, "Rounded Rect" | |
214 | ) | |
e91a9dfc | 215 | |
96bfd053 | 216 | bmp = images.getTest2Bitmap() |
8fa876ca | 217 | mask = wx.MaskColour(bmp, wx.BLUE) |
f3d9dc1d | 218 | bmp.SetMask(mask) |
f3d9dc1d | 219 | |
8fa876ca | 220 | s = ogl.BitmapShape() |
f3d9dc1d RD |
221 | s.SetBitmap(bmp) |
222 | self.MyAddShape(s, 225, 150, None, None, "Bitmap") | |
223 | ||
8fa876ca | 224 | dc = wx.ClientDC(self) |
e91a9dfc | 225 | self.PrepareDC(dc) |
8fa876ca | 226 | |
e91a9dfc RD |
227 | for x in range(len(self.shapes)): |
228 | fromShape = self.shapes[x] | |
229 | if x+1 == len(self.shapes): | |
230 | toShape = self.shapes[0] | |
231 | else: | |
232 | toShape = self.shapes[x+1] | |
8fa876ca RD |
233 | |
234 | line = ogl.LineShape() | |
e91a9dfc | 235 | line.SetCanvas(self) |
8fa876ca RD |
236 | line.SetPen(wx.BLACK_PEN) |
237 | line.SetBrush(wx.BLACK_BRUSH) | |
238 | line.AddArrow(ogl.ARROW_ARROW) | |
e91a9dfc RD |
239 | line.MakeLineControlPoints(2) |
240 | fromShape.AddLine(line, toShape) | |
241 | self.diagram.AddShape(line) | |
1e4a197e | 242 | line.Show(True) |
e91a9dfc RD |
243 | |
244 | # for some reason, the shapes have to be moved for the line to show up... | |
245 | fromShape.Move(dc, fromShape.GetX(), fromShape.GetY()) | |
246 | ||
c10ae510 | 247 | self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy) |
e91a9dfc RD |
248 | |
249 | ||
2f90df85 | 250 | def MyAddShape(self, shape, x, y, pen, brush, text): |
1e4a197e | 251 | shape.SetDraggable(True, True) |
e91a9dfc RD |
252 | shape.SetCanvas(self) |
253 | shape.SetX(x) | |
254 | shape.SetY(y) | |
f3d9dc1d RD |
255 | if pen: shape.SetPen(pen) |
256 | if brush: shape.SetBrush(brush) | |
257 | if text: shape.AddText(text) | |
8fa876ca | 258 | #shape.SetShadowMode(ogl.SHADOW_RIGHT) |
e91a9dfc | 259 | self.diagram.AddShape(shape) |
1e4a197e | 260 | shape.Show(True) |
e91a9dfc RD |
261 | |
262 | evthandler = MyEvtHandler(self.log, self.frame) | |
263 | evthandler.SetShape(shape) | |
264 | evthandler.SetPreviousHandler(shape.GetEventHandler()) | |
265 | shape.SetEventHandler(evthandler) | |
266 | ||
267 | self.shapes.append(shape) | |
1e4a197e | 268 | return shape |
e91a9dfc | 269 | |
f3d9dc1d | 270 | |
0b85cc38 RD |
271 | def OnDestroy(self, evt): |
272 | # Do some cleanup | |
e91a9dfc RD |
273 | for shape in self.diagram.GetShapeList(): |
274 | if shape.GetParent() == None: | |
275 | shape.SetCanvas(None) | |
276 | shape.Destroy() | |
8fa876ca | 277 | |
856e03b7 | 278 | self.diagram.Destroy() |
e91a9dfc RD |
279 | |
280 | ||
ecc08ead RD |
281 | def OnBeginDragLeft(self, x, y, keys): |
282 | self.log.write("OnBeginDragLeft: %s, %s, %s\n" % (x, y, keys)) | |
283 | ||
284 | def OnEndDragLeft(self, x, y, keys): | |
285 | self.log.write("OnEndDragLeft: %s, %s, %s\n" % (x, y, keys)) | |
286 | ||
287 | ||
e91a9dfc RD |
288 | #---------------------------------------------------------------------- |
289 | ||
290 | def runTest(frame, nb, log): | |
d14a1e28 RD |
291 | # This creates some pens and brushes that the OGL library uses. |
292 | # It should be called after the app object has been created, but | |
293 | # before OGL is used. | |
372bde9b | 294 | ogl.OGLInitialize() |
d14a1e28 | 295 | |
e91a9dfc RD |
296 | win = TestWindow(nb, log, frame) |
297 | return win | |
8fa876ca | 298 | |
e91a9dfc RD |
299 | #---------------------------------------------------------------------- |
300 | ||
49234978 RD |
301 | # The OGL library holds some resources that need to be freed before |
302 | # the app shuts down. | |
e91a9dfc | 303 | class __Cleanup: |
49234978 RD |
304 | def __del__(self, cleanup=ogl.OGLCleanUp): |
305 | cleanup() | |
e91a9dfc | 306 | |
49234978 RD |
307 | # When this module gets cleaned up by Python then __cu will be cleaned |
308 | # up and it's __dell__ is called, which will then call ogl.OGLCleanUp. | |
e91a9dfc RD |
309 | __cu = __Cleanup() |
310 | ||
311 | ||
e91a9dfc RD |
312 | overview = """\ |
313 | The Object Graphics Library is a library supporting the creation and | |
314 | manipulation of simple and complex graphic images on a canvas. | |
315 | ||
316 | """ | |
317 | ||
1e4a197e RD |
318 | if __name__ == '__main__': |
319 | import sys,os | |
320 | import run | |
321 | run.main(['', os.path.basename(sys.argv[0])]) | |
322 |