···
#------------------------------------------------------------
class GenericRectShape(ogl.RectangleShape):
def __init__(self, canvas, x=100, y=100):
ogl.RectangleShape.__init__(self,30,60)
self.SetX(x)
self.SetY(y)
self.canvas = canvas
self.SetPen(wx.Pen(wx.BLACK, 1, wx.SOLID))
self.SetBrush(wx.WHITE_BRUSH)
self.AddText("rect")
self.canvas.diagram.AddShape(self)
self.Show(True)
#----------------------------------------------------------------------
class TestWindow(ogl.ShapeCanvas):
def __init__(self, parent, log, frame):
ogl.ShapeCanvas.__init__(self, parent)
maxWidth = 1000
maxHeight = 1000
self.SetScrollbars(20, 20, maxWidth/20, maxHeight/20)
self.log = log
self.frame = frame
self.SetBackgroundColour("LIGHT BLUE") #wx.WHITE)
self.diagram = ogl.Diagram()
self.SetDiagram(self.diagram)
self.diagram.SetCanvas(self)
self.shapes = []
#self.save_gdi = []
#rRectBrush = wx.Brush("MEDIUM TURQUOISE", wx.SOLID)
#dsBrush = wx.Brush("WHEAT", wx.SOLID)
self.shapes.append(GenericCompoShape(self,300,120))
self.shapes.append(GenericRectShape(self,200,140))
dc = wx.ClientDC(self)
self.PrepareDC(dc)
for x in range(len(self.shapes)):
fromShape = self.shapes[x]
if x+1 == len(self.shapes):
toShape = self.shapes[0]
else:
toShape = self.shapes[x+1]
line = ogl.LineShape()
line.SetCanvas(self)
line.SetPen(wx.BLACK_PEN)
line.SetBrush(wx.BLACK_BRUSH)
line.AddArrow(ogl.ARROW_ARROW)
line.MakeLineControlPoints(2)
fromShape.AddLine(line, toShape)
self.diagram.AddShape(line)
line.Show(True)
# for some reason, the shapes have to be moved for the line to
show up...
fromShape.Move(dc, fromShape.GetX(), fromShape.GetY())
self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
def OnDestroy(self, evt):
# Do some cleanup
for shape in self.diagram.GetShapeList():
if shape.GetParent() == None:
shape.SetCanvas(None)
shape.Destroy()
self.diagram.Destroy()
def OnBeginDragLeft(self, x, y, keys):
self.log.write("OnBeginDragLeft: %s, %s, %s\n" % (x, y, keys))
def OnEndDragLeft(self, x, y, keys):
self.log.write("OnEndDragLeft: %s, %s, %s\n" % (x, y, keys))
def OnRightClick(self, *dontcare):
print "OnRightClick"
def OnLeftClick(self, *dontcare):
print "OnLeftClick"
#----------------------------------------------------------------------
# The OGL library holds some resources that need to be freed before
# the app shuts down.
class __Cleanup:
def __del__(self, cleanup=ogl.OGLCleanUp):
cleanup()
# When this module gets cleaned up by Python then __cu will be cleaned
# up and it's __dell__ is called, which will then call ogl.OGLCleanUp.
__cu = __Cleanup()
#---------------------------------------------------------------------------
-
class Log:
def WriteText(self, text):
if text[-1:] == '\n':
text = text[:-1]
wx.LogMessage(text)
write = WriteText
class RunDemoApp(wx.App):
def __init__(self, useShell):
self.useShell = useShell
wx.App.__init__(self, 0)
def runTest(self, frame, nb, log):
# This creates some pens and brushes that the OGL library uses.
# It should be called after the app object has been created, but
# before OGL is used.
ogl.OGLInitialize()
win = TestWindow(nb, log, frame)
return win
def OnInit(self):
wx.InitAllImageHandlers()
wx.Log_SetActiveTarget(wx.LogStderr())
self.SetAssertMode(assertMode)
frame = wx.Frame(None, -1, "Programma di prova OGL", pos=(50,50),
size=(200,100),
style=wx.NO_FULL_REPAINT_ON_RESIZE|wx.DEFAULT_FRAME_
STYLE)
frame.CreateStatusBar()
menuBar = wx.MenuBar()
menu = wx.Menu()
item = menu.Append(-1, "E&xit\tAlt-X", "Exit demo")
self.Bind(wx.EVT_MENU, self.OnButton, item)
menuBar.Append(menu, "&File")
ns = {}
ns['wx'] = wx
ns['app'] = self
ns['frame'] = frame
frame.SetMenuBar(menuBar)
frame.Show(True)
frame.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
win = self.runTest(frame, frame, Log())
# so set the frame to a good size for showing stuff
frame.SetSize((640, 480))
win.SetFocus()
self.window = win
ns['win'] = win
frect = frame.GetRect()
self.SetTopWindow(frame)
self.frame = frame
#wx.Log_SetActiveTarget(wx.LogStderr())
#wx.Log_SetTraceMask(wx.TraceMessages)
if self.useShell:
# Make a PyShell window, and position it below our test window
from wx import py
shell = py.shell.ShellFrame(None, locals=ns)
frect.OffsetXY(0, frect.height)
frect.height = 400
shell.SetRect(frect)
shell.Show()
# Hook the close event of the test window so that we close
# the shell at the same time
def CloseShell(evt):
if shell:
shell.Close()
evt.Skip()
frame.Bind(wx.EVT_CLOSE, CloseShell)
return True
def OnButton(self, evt):
self.frame.Close(True)
def OnCloseFrame(self, evt):
if hasattr(self, "window") and hasattr(self.window, "ShutdownDemo"):
self.window.ShutdownDemo()
evt.Skip()
#---------------------------------------------------------------------------
-
def main(argv):
useShell = False
for x in range(len(sys.argv)):
if sys.argv[x] in ['--shell', '-shell', '-s']:
useShell = True
del sys.argv[x]
break
app = RunDemoApp(useShell)
app.MainLoop()
if __name__ == "__main__":
main(sys.argv)