2 from wxPython
.wx
import *
3 from wxPython
.ogl
import *
9 #----------------------------------------------------------------------
11 class DiamondShape(wxPolygonShape
):
12 def __init__(self
, w
=0.0, h
=0.0):
13 wxPolygonShape
.__init
__(self
)
19 ## Either wxRealPoints or 2-tuples of floats works.
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),
26 points
= [ (0.0, -h
/2.0),
35 #----------------------------------------------------------------------
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)
43 #----------------------------------------------------------------------
45 class DividedShape(wxDividedShape
):
46 def __init__(self
, width
, height
, canvas
):
47 wxDividedShape
.__init
__(self
, width
, height
)
49 region1
= wxShapeRegion()
50 region1
.SetText('wxDividedShape')
51 region1
.SetProportions(0.0, 0.2)
52 region1
.SetFormatMode(FORMAT_CENTRE_HORIZ
)
53 self
.AddRegion(region1
)
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
)
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
)
68 self
.ReformatRegions(canvas
)
71 def ReformatRegions(self
, canvas
=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
)
82 def OnSizingEndDragLeft(self
, pt
, x
, y
, keys
, attch
):
84 self
.base_OnSizingEndDragLeft(pt
, x
, y
, keys
, attch
)
86 self
.ReformatRegions()
87 self
.GetCanvas().Refresh()
90 #----------------------------------------------------------------------
92 class MyEvtHandler(wxShapeEvtHandler
):
93 def __init__(self
, log
, frame
):
94 wxShapeEvtHandler
.__init
__(self
)
96 self
.statbarFrame
= frame
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
))
105 def OnLeftClick(self
, x
, y
, keys
= 0, attachment
= 0):
106 shape
= self
.GetShape()
107 print shape
.__class
__, shape
.GetClassName()
108 canvas
= shape
.GetCanvas()
109 dc
= wxClientDC(canvas
)
113 shape
.Select(False, dc
)
117 shapeList
= canvas
.GetDiagram().GetShapeList()
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...
126 shape
.Select(True, dc
)
133 self
.UpdateStatusBar(shape
)
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
)
144 def OnSizingEndDragLeft(self
, pt
, x
, y
, keys
, attch
):
145 self
.base_OnSizingEndDragLeft(pt
, x
, y
, keys
, attch
)
146 self
.UpdateStatusBar(self
.GetShape())
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())
154 def OnRightClick(self
, *dontcare
):
155 self
.log
.WriteText("%s\n" % self
.GetShape())
158 #----------------------------------------------------------------------
160 class TestWindow(wxShapeCanvas
):
161 def __init__(self
, parent
, log
, frame
):
162 wxShapeCanvas
.__init
__(self
, parent
)
166 self
.SetScrollbars(20, 20, maxWidth
/20, maxHeight
/20)
170 self
.SetBackgroundColour("LIGHT BLUE") #wxWHITE)
171 self
.diagram
= wxDiagram()
172 self
.SetDiagram(self
.diagram
)
173 self
.diagram
.SetCanvas(self
)
177 rRectBrush
= wxBrush("MEDIUM TURQUOISE", wxSOLID
)
178 dsBrush
= wxBrush("WHEAT", wxSOLID
)
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")
182 ds
= self
.MyAddShape(DividedShape(140, 150, self
), 495, 145, wxBLACK_PEN
, dsBrush
, '')
183 self
.MyAddShape(DiamondShape(90, 90), 345, 235, wxPen(wxBLUE
, 3, wxDOT
), wxRED_BRUSH
, "Polygon")
184 self
.MyAddShape(RoundedRectangleShape(95,70), 140, 255, wxPen(wxRED
, 2), rRectBrush
, "Rounded Rect")
186 bmp
= images
.getTest2Bitmap()
187 mask
= wxMaskColour(bmp
, wxBLUE
)
192 self
.MyAddShape(s
, 225, 150, None, None, "Bitmap")
194 dc
= wxClientDC(self
)
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]
201 toShape
= self
.shapes
[x
+1]
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
)
212 # for some reason, the shapes have to be moved for the line to show up...
213 fromShape
.Move(dc
, fromShape
.GetX(), fromShape
.GetY())
215 EVT_WINDOW_DESTROY(self
, self
.OnDestroy
)
218 def MyAddShape(self
, shape
, x
, y
, pen
, brush
, text
):
219 shape
.SetDraggable(True, True)
220 shape
.SetCanvas(self
)
223 if pen
: shape
.SetPen(pen
)
224 if brush
: shape
.SetBrush(brush
)
225 if text
: shape
.AddText(text
)
226 #shape.SetShadowMode(SHADOW_RIGHT)
227 self
.diagram
.AddShape(shape
)
230 evthandler
= MyEvtHandler(self
.log
, self
.frame
)
231 evthandler
.SetShape(shape
)
232 evthandler
.SetPreviousHandler(shape
.GetEventHandler())
233 shape
.SetEventHandler(evthandler
)
235 self
.shapes
.append(shape
)
239 def OnDestroy(self
, evt
):
241 for shape
in self
.diagram
.GetShapeList():
242 if shape
.GetParent() == None:
243 shape
.SetCanvas(None)
245 self
.diagram
.Destroy()
248 def OnBeginDragLeft(self
, x
, y
, keys
):
249 self
.log
.write("OnBeginDragLeft: %s, %s, %s\n" % (x
, y
, keys
))
251 def OnEndDragLeft(self
, x
, y
, keys
):
252 self
.log
.write("OnEndDragLeft: %s, %s, %s\n" % (x
, y
, keys
))
255 #----------------------------------------------------------------------
257 def runTest(frame
, nb
, log
):
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.
263 win
= TestWindow(nb
, log
, frame
)
266 #----------------------------------------------------------------------
269 cleanup
= wxOGLCleanUp
273 # when this module gets cleaned up then wxOGLCleanUp() will get called
284 The Object Graphics Library is a library supporting the creation and
285 manipulation of simple and complex graphic images on a canvas.
291 if __name__
== '__main__':
294 run
.main(['', os
.path
.basename(sys
.argv
[0])])