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