20 #include "msdevstudio/MSconfig.h"
27 #include <boost/python.hpp>
38 #include "qapplication.h"
39 #include "qwaitcondition.h"
45 using namespace boost::python;
53 class_ < PyApp > (
"HDApp",
54 "The HippoDraw application class.\n" )
57 (
"HDApp () -> HDApp\n"
58 "HDApp ( value ) -> HDApp\n"
60 "Using the constructor with no arguments creates\n"
61 "a HippoDraw application that runs in a separate\n"
63 "Using the constructor with one argument creates\n"
64 "a HippoDraw application in same thread. Use\n"
65 "app.exec_loop() to start it." ) )
67 .def ( init < char * >
68 (
"HDapp ( string ) -> HDapp" ) )
70 .def (
"instance", &PyApp::instance,
71 return_value_policy < reference_existing_object > (),
72 "instance () -> HDApp\n"
74 "Returns a singleton instance of the HippoDraw \n"
75 "application object" )
77 .staticmethod (
"instance" )
79 .def (
"canvas", &PyApp::currentCanvas,
80 return_value_policy < reference_existing_object > (),
81 "canvas () -> Canvas\n"
83 "Returns the current canvas window." )
85 .def (
"exec_loop", &PyApp::exec,
86 "exec_loop () -> value\n"
88 "Starts the application object. Returns the value returned\n"
89 "by the application object upon exiting." )
91 .def (
"quit", &PyApp::quit,
94 "Closes all Canvas windows and Terminates the application." )
96 .def (
"quitOnLastWindowClose", &PyApp::quitOnLastWindowClose,
97 "quitOnLastWindowClose ( Boolean ) -> None\n"
99 "If argument is True, allows application to quit when last\n"
100 "Canvas is closed." )
102 .def (
"hasTerminated", &PyApp::hasTerminated,
103 "hasTerminated () -> Boolean\n"
105 "Returns True if the application has terminated." )
107 .def (
"openDocument", &PyApp::openDocument,
108 "openDocument ( string ) -> None\n"
110 "Opens new canvas window from saved document file." )
112 .def (
"lock", &PyApp::lock,
115 "Lock the Qt library Mutex. Will not return until no\n"
116 "other thread has the lock." )
118 .def (
"unlock", &PyApp::unlock,
119 "unlock () -> None\n"
121 "Unlocks the application thread." )
129 using namespace hippodraw;
131 PyApp * PyApp::s_instance = 0;
132 QtApp * PyApp::s_app = 0;
139 static char * argv[1];
140 argv[0] =
const_cast< char *
> (
"Python" );
142 QtApp app ( argc, argv );
145 #endif // have qt app
155 #if QT_VERSION < 0x040200
164 while ( QtApp::startingUp () ==
true ) {
165 m_thread->wait ( 100 );
167 s_app = QtApp::instance ();
172 while ( s_app->currentCanvas () == 0 ) {
173 m_thread->wait ( 100 );
178 while ( s_app->hasPendingEvents () == true ) {
179 m_thread->wait ( 100 );
183 while ( s_app->currentCanvas () == 0 ) {
184 m_thread->wait ( 100 );
189 #endif // have qt app
190 #else // qt 4.2 or later
191 pthread_t * thread = ( pthread_t * ) malloc (
sizeof ( pthread_t ) );
193 perror (
"Malloc of thread failed" );
195 if ( pthread_create ( thread, NULL,
run, (
void * ) 0 ) ) {
196 perror (
"Failed to create thread");
199 while ( QtApp::startingUp() ==
true ) {
202 s_app = QtApp::instance ();
203 #endif // have qt app
210 PyApp::PyApp (
char * script )
213 pthread_t * thread = ( pthread_t * ) malloc (
sizeof ( pthread_t ) );
215 perror (
"Malloc of thread failed" );
217 if ( pthread_create ( thread, NULL,
run, (
void * ) 0 ) ) {
218 perror (
"Failed to create thread");
221 while ( QtApp::startingUp() ==
true ) {
224 s_app = QtApp::instance ();
234 static char * argv[1];
235 argv[0] = const_cast <
char * > (
"/HippoDraw" );
237 s_app =
new QtApp ( argc, argv,
true );
251 if ( s_instance == 0 ) {
252 s_instance =
new PyApp ();
261 return s_app->exec();
269 hasTerminated ()
const
283 s_app -> closeAllWindows ();
292 #if QT_VERSION < 0x040000
293 if ( m_thread -> running() ) {
299 while ( canvas == 0 ) {
305 canvas = s_app-> currentCanvas();
309 #if QT_VERSION < 0x040000
319 void PyApp::openDocument (
const std::string & filename )
325 window -> initFromFile ( filename );
333 #if QT_VERSION < 0x040000
344 bool PyApp::locked ()
347 #if QT_VERSION < 0x040000
350 yes = s_app->locked ();
357 void PyApp::unlock ()
359 #if QT_VERSION < 0x040000
375 yes = s_app -> hasPendingEvents ();
383 quitOnLastWindowClose (
bool yes )
388 controller -> quitOnLastWindowClose ( yes );