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