2 from wxPython
.wx
import *
3 from wxPython
.ogl
import *
7 #----------------------------------------------------------------------
8 # This creates some pens and brushes that the OGL library uses.
12 #----------------------------------------------------------------------
14 class DiamondShape(wxPolygonShape
):
15 def __init__(self
, w
=0.0, h
=0.0):
16 wxPolygonShape
.__init
__(self
)
22 ## Either wxRealPoints or 2-tuples of floats works.
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),
29 points
= [ (0.0, -h
/2.0),
38 #----------------------------------------------------------------------
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)
46 #----------------------------------------------------------------------
48 class DividedShape(wxDividedShape
):
49 def __init__(self
, width
, height
, canvas
):
50 wxDividedShape
.__init
__(self
, width
, height
)
52 region1
= wxShapeRegion()
53 region1
.SetText('wxDividedShape')
54 region1
.SetProportions(0.0, 0.2)
55 region1
.SetFormatMode(FORMAT_CENTRE_HORIZ
)
56 self
.AddRegion(region1
)
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
)
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
)
71 self
.ReformatRegions(canvas
)
74 def ReformatRegions(self
, canvas
=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
)
85 def OnSizingEndDragLeft(self
, pt
, x
, y
, keys
, attch
):
86 self
.base_OnSizingEndDragLeft(pt
, x
, y
, keys
, attch
)
88 self
.ReformatRegions()
89 self
.GetCanvas().Refresh()
92 #----------------------------------------------------------------------
94 class MyEvtHandler(wxShapeEvtHandler
):
95 def __init__(self
, log
, frame
):
96 wxShapeEvtHandler
.__init
__(self
)
98 self
.statbarFrame
= frame
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
))
107 def OnLeftClick(self
, x
, y
, keys
= 0, attachment
= 0):
108 shape
= self
.GetShape()
109 print shape
.__class
__, shape
.GetClassName()
110 canvas
= shape
.GetCanvas()
111 dc
= wxClientDC(canvas
)
115 shape
.Select(False, dc
)
119 shapeList
= canvas
.GetDiagram().GetShapeList()
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...
128 shape
.Select(True, dc
)
135 self
.UpdateStatusBar(shape
)
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
)
146 def OnSizingEndDragLeft(self
, pt
, x
, y
, keys
, attch
):
147 self
.base_OnSizingEndDragLeft(pt
, x
, y
, keys
, attch
)
148 self
.UpdateStatusBar(self
.GetShape())
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())
156 def OnRightClick(self
, *dontcare
):
157 self
.log
.WriteText("%s\n" % self
.GetShape())
160 #----------------------------------------------------------------------
162 class TestWindow(wxShapeCanvas
):
163 def __init__(self
, parent
, log
, frame
):
164 wxShapeCanvas
.__init
__(self
, parent
)
168 self
.SetScrollbars(20, 20, maxWidth
/20, maxHeight
/20)
172 self
.SetBackgroundColour("LIGHT BLUE") #wxWHITE)
173 self
.diagram
= wxDiagram()
174 self
.SetDiagram(self
.diagram
)
175 self
.diagram
.SetCanvas(self
)
179 rRectBrush
= wxBrush("MEDIUM TURQUOISE", wxSOLID
)
180 dsBrush
= wxBrush("WHEAT", wxSOLID
)
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")
184 ds
= self
.MyAddShape(DividedShape(140, 150, self
), 495, 145, wxBLACK_PEN
, dsBrush
, '')
185 self
.MyAddShape(DiamondShape(90, 90), 345, 235, wxPen(wxBLUE
, 3, wxDOT
), wxRED_BRUSH
, "Polygon")
186 self
.MyAddShape(RoundedRectangleShape(95,70), 140, 255, wxPen(wxRED
, 2), rRectBrush
, "Rounded Rect")
188 bmp
= images
.getTest2Bitmap()
189 mask
= wxMaskColour(bmp
, wxBLUE
)
194 self
.MyAddShape(s
, 225, 150, None, None, "Bitmap")
196 dc
= wxClientDC(self
)
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]
203 toShape
= self
.shapes
[x
+1]
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
)
214 # for some reason, the shapes have to be moved for the line to show up...
215 fromShape
.Move(dc
, fromShape
.GetX(), fromShape
.GetY())
217 EVT_WINDOW_DESTROY(self
, self
.OnDestroy
)
220 def MyAddShape(self
, shape
, x
, y
, pen
, brush
, text
):
221 shape
.SetDraggable(True, True)
222 shape
.SetCanvas(self
)
225 if pen
: shape
.SetPen(pen
)
226 if brush
: shape
.SetBrush(brush
)
227 if text
: shape
.AddText(text
)
228 #shape.SetShadowMode(SHADOW_RIGHT)
229 self
.diagram
.AddShape(shape
)
232 evthandler
= MyEvtHandler(self
.log
, self
.frame
)
233 evthandler
.SetShape(shape
)
234 evthandler
.SetPreviousHandler(shape
.GetEventHandler())
235 shape
.SetEventHandler(evthandler
)
237 self
.shapes
.append(shape
)
241 def OnDestroy(self
, evt
):
243 for shape
in self
.diagram
.GetShapeList():
244 if shape
.GetParent() == None:
245 shape
.SetCanvas(None)
247 self
.diagram
.Destroy()
250 def OnBeginDragLeft(self
, x
, y
, keys
):
251 self
.log
.write("OnBeginDragLeft: %s, %s, %s\n" % (x
, y
, keys
))
253 def OnEndDragLeft(self
, x
, y
, keys
):
254 self
.log
.write("OnEndDragLeft: %s, %s, %s\n" % (x
, y
, keys
))
257 #----------------------------------------------------------------------
259 def runTest(frame
, nb
, log
):
260 win
= TestWindow(nb
, log
, frame
)
263 #----------------------------------------------------------------------
266 cleanup
= wxOGLCleanUp
270 # when this module gets cleaned up then wxOGLCleanUp() will get called
281 The Object Graphics Library is a library supporting the creation and
282 manipulation of simple and complex graphic images on a canvas.
288 if __name__
== '__main__':
291 run
.main(['', os
.path
.basename(sys
.argv
[0])])