# *********************************************************************** # # # License: freeware, under the terms of the BSD-license # Copyright (C) 2007 # mailto: ... # Please let me know if it works or not under different conditions # # # Test Conditions: 1 # - orginal release # # Test Conditions # 1. WinXP-SP2, Python 2.4.3, wxPython wx-2.8-msw-ansi # *********************************************************************** """ # *********************************************************************** ToDo: - """ # *********************************************************************** import os import sys subdirs = [ '..\\P24_support' ] for subdir in subdirs: if not ( subdir in sys.path) : sys.path.append ( subdir ) # !!! import wx before any support library !!! # otherwise the IDE won't do auto-suggest import wx import PyLab_Works_Globals as PG from dialog_support import * from inifile_support import * import OGLlike as ogl import ogl_2D from numpy import arange, pi, sin, arctan, sqrt # *********************************************************************** # A simple form to test the control # *********************************************************************** class Simple_Test_Form ( wx.MiniFrame ): def __init__ ( self, ini = None ): # restore position and size pos = ( 50, 50 ) size = ( 500, 300 ) if ini : ini.Section = 'Test' pos = ini.Read ( 'Pos' , pos ) size = ini.Read ( 'Size' , size ) wx.MiniFrame.__init__( self, None, -1, 'Test PyLab Works GUI Control', size = size, pos = pos, style = wx.DEFAULT_FRAME_STYLE ) # Create the control to be tested self.Scene = tScene_2D ( self, None, ini = ini ) #PG.OGL_Orientation_Hor = True #self.Scene.Canvas.Orientation_Hor = True #self.shape = self.Scene.Create_Rectangle ( (0,0), (10,40) ) self.shape = self.Scene.Create_Circle ( 5 ) #def Create_Circle ( self, *args, **kwargs ) : #item = ogl_2D.Circle_Shape ( self.Scene.Canvas, *args, **kwargs ) List = [ 'Table 2', 'veld 1', 'veld 2', 'veld3'] item = ogl.DB_Table_Shape ( self.Scene.Canvas, List ) self.Scene.diagram.AddShape ( item ) List = [ 'TableName', 'field 1', 'field 2', 'field 3', 'field 4', 'field 5'] item = ogl.DB_Table_Shape ( self.Scene.Canvas, List ) self.Scene.diagram.AddShape ( item ) # the Timer must be bound to Dock self.Timer = wx.Timer ( self ) # the third parameter is essential to allow other timers self.Bind ( wx.EVT_TIMER, self.On_Timer, self.Timer) self.Timer.Start ( 10 ) def On_Timer ( self, event ) : ball = self.shape dt = 0.1 ball.x = ball.x + ball.vx * dt ball.vx *= 0.997 ball.y = ball.y - ball.vy * dt if ( ball.y <= 0 ) and ( ball.vy > 0 ) : ball.vy = -0.8 * ball.vy ball.y = 0 else : ball.vy = ball.vy + 9.8 * dt self.Scene.Canvas.Refresh() #print ball.GetX(),ball.GetY(),self.velocity,ball.GetY () + self.velocity*self.dt # *********************************************************************** # *********************************************************************** # *********************************************************************** class tScene_2D ( object ) : def __init__ ( self, Dock, # the frame/panel/... where we can put # the GUI controls that will catch events Brick, # the Brick, with its inputs and outputs ini = None, # inifile to store and reload settings Test = False ): # if True, testmode with buildin examples self.Dock = Dock self.Brick = Brick self.Test = Test self.Ini = ini self.code = '' #print Dock,Brick,Test,ini # create the wx-components here and self.Canvas = ogl.ShapeCanvas ( Dock, pos=(0,0), size=(500,500) ) #self.Canvas.SetBackgroundColour ( wx.RED ) Sizer = wx.BoxSizer ( wx.VERTICAL ) Dock.SetSizer ( Sizer ) Sizer.Add ( self.Canvas, 1, wx.EXPAND ) #self.Dock.SendSizeEvent () # Create a diagram and assign it to the Canvas self.diagram = ogl.Diagram () self.Canvas.SetDiagram ( self.diagram ) """ # uniform coordinate system with WH self.Set_Coordinates ( ( -100, -100 ), ( 200, None ) ) # uniform coordinate system with RT self.Set_Coordinates ( ( -100, -100 ), RT = ( 100, None ) ) # non-uniform coordinate system with WH self.Set_Coordinates ( ( -100, -100 ), ( 200, 200 ) ) """ """ self.Set_Coordinates ( ( -10, -10 ), RT = ( 100, None ) ) shape = self.Create_Axis ( 0, 0 ) shape.Line_Width = 2 shape = self.Create_Grid ( 20 ) shape = self.Create_Text ( 'test1', (30,20)) shape.Line_Width = 1 shape = self.Create_Text ( 'test2', (30,40)) shape.Line_Width = 3 shape.Color = wx.BLUE shape = self.Create_Text ( 'test2', (30,60)) shape.Line_Width = 5 shape.rot = pi / 4 """ """ self.Set_Coordinates ( ( -10, -10 ), RT = ( 100, None ) ) shape = self.Create_Axis ( 0, 0 ) shape.Color = wx.GREY_PEN.GetColour() shape.Line_Width = 3 shape = self.Create_Grid ( 10 ) shape = self.Create_Grid ( 10, arctan (0.2) ) shape.Color = wx.RED shape.Line_Width = 2 shape = self.Create_Arrow ( ( 0, 0), ( 70, 70 ) ) shape.Color = wx.BLUE """ """ self.Set_Coordinates ( ( -10, -10 ), RT = ( 100, None ) ) shape = self.Create_Grid () shape = self.Create_Axis () shape = self.Create_Free_Shape ( ( (0,0) , (30,0), (15, 30*sin(pi/6) ) ) ) shape.Color = wx.RED shape = self.Create_Free_Shape ( ( (0,0) , (30,0), (15, 30*sin(pi/6) ) ) ) shape.x = 30 shape.y = 40 shape.rot = pi """ """ self.Set_Coordinates ( ( -10, -10 ), RT = ( 100, None ) ) #shape = self.Create_Grid () #shape = self.Create_Axis () shape = self.Create_Text ( 'test2', (30,60)) shape.Line_Width = 5 shape.rot = pi / 4 """ """ #shape = self.Create_Function ( # '30 + 20 * sin ( pi * x / 10 )', # ( 10,50,1 ) ) x = arange ( 10, 50, 1 ) y = 30 + 20 * sin ( pi * x / 10 ) Points = [] for i, xi in enumerate ( x ) : Points.append ( (xi, y[i] ) ) shape = self.Create_Points (Points) shape.Line_Width = 4 """ #shape = self.Create_Button ( 5, (50,70)) #shape = self.Create_Button ( 5, (30,70)) #self.shape = ogl_2D.Circle_Shape ( (50,50), 10 ) #shape = self.Create_Function ( '20 * sin ( x )', range ( 10,50 ) ) """ self.Set_Coordinates ( ( -10, -10 ), RT = ( 100, None ) ) shape = self.Create_Rectangle ( (0,0) , (40,40) ) shape.Color = wx.RED shape = self.Create_Rectangle ( (20,30) , RT = (60,70) ) shape.Color = wx.BLUE shape = self.Create_Rectangle ( (50,10) , RPhi = ( sqrt(3200), pi/4 ) ) shape.Color = wx.Color ( 255, 255, 50 ) """ """ self.Set_Coordinates ( ( -100, -100 ), ( 200, 200 ) ) #self.Set_Coordinates ( # ( -100, -100 ), ( 200, None ) ) shape = self.Create_Axis ( 0, 0 ) shape = self.Create_Grid ( 10 ) shape = self.Create_Arrow ( ( 0, 0), ( 50, 50 ) ) shape.Line_Width = 8 shape = self.Create_Arrow ( ( 20, 0), RT = ( 70, 50 ) ) shape.Color = wx.BLUE shape = self.Create_Arrow ( ( 40, 0), RPhi = ( sqrt(5000), pi/4 ) ) shape.Color = wx.BLACK shape.Line_Width = 1 """ """ shape = self.Create_Line ( ( 0, 0), ( 50, 50 ) ) shape.Line_Width = 8 shape = self.Create_Line ( ( 20, 0), RT = ( 70, 50 ) ) shape.Color = wx.BLUE shape = self.Create_Line ( ( 40, 0), RPhi = ( sqrt(5000), pi/4 ) ) shape.Color = wx.BLACK shape.Line_Width = 1 """ self.Canvas.Refresh () # ********************************************************* # ********************************************************* def Set_Coordinates ( self, *args, **kwargs ) : self.Canvas.Set_Coordinates ( *args, **kwargs ) # ********************************************************* # ********************************************************* def Create_Axis ( self, *args, **kwargs ) : item = ogl_2D.Axis_Shape ( self.Canvas, *args, **kwargs ) self.diagram.AddShape ( item ) return item # ********************************************************* # ********************************************************* def Create_Grid ( self, *args, **kwargs ) : item = ogl_2D.Grid_Shape ( self.Canvas, *args, **kwargs ) self.diagram.AddShape ( item ) return item # ********************************************************* # ********************************************************* def Create_Text ( self, *args, **kwargs ) : item = ogl_2D.Text_Shape ( self.Canvas, *args, **kwargs ) self.diagram.AddShape ( item ) return item # ********************************************************* # ********************************************************* def Create_Points ( self, *args, **kwargs ) : item = ogl_2D.Points_Shape ( self.Canvas, *args, **kwargs ) self.diagram.AddShape ( item ) return item # ********************************************************* # ********************************************************* def Create_Function ( self, *args, **kwargs ) : item = ogl_2D.Function_Shape ( self.Canvas, *args, **kwargs ) self.diagram.AddShape ( item ) return item # ********************************************************* # ********************************************************* def Create_Line ( self, *args, **kwargs ) : item = ogl_2D.Line_Shape ( self.Canvas, *args, **kwargs ) #LB, WH ) self.diagram.AddShape ( item ) return item # ********************************************************* # ********************************************************* def Create_Arrow ( self, *args, **kwargs ) : item = ogl_2D.Arrow_Shape ( self.Canvas, *args, **kwargs ) #LB, WH ) self.diagram.AddShape ( item ) return item # ********************************************************* # ********************************************************* def Create_Circle ( self, *args, **kwargs ) : item = ogl_2D.Circle_Shape ( self.Canvas, *args, **kwargs ) self.diagram.AddShape ( item ) return item # ********************************************************* # ********************************************************* def Create_Button ( self, *args, **kwargs ) : item = ogl_2D.Button_Shape ( self.Canvas, *args, **kwargs ) self.diagram.AddShape ( item ) return item # ********************************************************* # ********************************************************* def Create_Rectangle ( self, *args, **kwargs ) : item = ogl_2D.Rectangle_Shape ( self.Canvas, *args, **kwargs ) self.diagram.AddShape ( item ) return item # ********************************************************* # ********************************************************* def Create_Free_Shape ( self, *args, **kwargs ) : item = ogl_2D.Free_Shape ( self.Canvas, *args, **kwargs ) self.diagram.AddShape ( item ) return item # ********************************************************* # ********************************************************* def Start_Script ( self, Code ) : init_code = '' #init_code += 'print "DIR1",dir()\n' # only \n is allowed !! (see "compile") Code = Code.replace('\r\n','\n') # remove line continuation, not accepted by exec-statement Code = Code.replace('\\\n','') # and last line must end with \n (see "compile") Code = Code + '\n' Code = Code.split ( '\n' ) #print 'INIT1',init_code # find the init procedure ( must be on top ) i = 0 init_found = False while ( i < len ( Code ) ) and not ( init_found ): line = Code [i] func = line.find ( 'def ') init = line.find ( ' init') if ( func == 0 ) and ( init > func ) : init_found = True i += 1 #print 'INIT2',init_code if init_found : init_code += 'self._Trail_Count = -1 \n' init_code += 'self.Trail_Count = 5\n' # ignore empty lines, collect all lines while indent > 0 while (len ( Code [i].strip() ) == 0 ) or \ (len ( Code [i] ) > len ( Code[i].lstrip() )) : init_code += Code [i].lstrip() + '\n' i += 1 try : #init_code += 'print "DIR2",dir()\n' init_code += '\n' #print 'INIT CODE',init_code exec ( init_code) except : print '******** SCENE Code Editor, ERROR: *********' self.code = '' self.code += 'self._Trail_Count += 1 \n' #self.code += 'print "XXX",self._Trail_Count,self.Trail_Count\n' self.code += 'if (self._Trail_Count % self.Trail_Count) == 0 :\n' self.code += ' self.Canvas.Add_Trail ()\n' while i < len ( Code ) : self.code += Code [i] + '\n' i += 1 self.code += 'self.Canvas.Refresh()\n' #self.code += 'self.Canvas.SendSizeEvent ()\n' #print '$$$' #print self.code #print '$$$' # ********************************************************* # ********************************************************* def Execute_Script ( self ) : if self.code : #print 'DIR4', dir() try : exec ( self.code ) except : print 'AAP' # *********************************************************************** # *********************************************************************** # *********************************************************************** class tSQL_Builder ( tScene_2D ) : def __init__ ( self, Dock, # the frame/panel/... where we can put # the GUI controls that will catch events Brick, # the Brick, with its inputs and outputs ini = None, # inifile to store and reload settings Test = False ): # if True, testmode with buildin examples tScene_2D.__init__ ( self, Dock, Brick, ini, Test ) #PG.OGL_Orientation_Hor = True self.Canvas.Orientation_Hor = True #self.shape = self.Scene.Create_Rectangle ( (0,0), (10,40) ) self.shape = self.Create_Circle ( 5 ) #def Create_Circle ( self, *args, **kwargs ) : #item = ogl_2D.Circle_Shape ( self.Scene.Canvas, *args, **kwargs ) List = [ 'Table 2', 'veld 1', 'veld 2', 'veld 3'] item = ogl.DB_Table_Shape ( self.Canvas, List ) self.diagram.AddShape ( item ) List = [ 'Table 2', 'veld 5', 'veld 2', 'veld 3'] item = ogl.DB_Table_Shape ( self.Canvas, List ) self.diagram.AddShape ( item ) def SetValue ( self, value ): print ' NEWWWWW' #,value # *********************************************************************** # *********************************************************************** # demo program # *********************************************************************** if __name__ == '__main__': import PyLab_Works_Globals as PG app = wx.PySimpleApp () ini = inifile ( 'test_My_New_GUI_Control.cfg' ) frame = Simple_Test_Form (ini = ini) frame.Show ( True ) app.MainLoop () ini.Close () # ***********************************************************************