Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

PyApp.cxx

Go to the documentation of this file.
00001 
00014 #ifdef _MSC_VER
00015 #include "msdevstudio/MSconfig.h"
00016 #endif
00017 
00018 #include "PyApp.h" 
00019 
00020 // with Python 2.3, include before Qt headers to avoid conflict
00021 // with symbol `slots'
00022 #include <boost/python.hpp>
00023 
00024 #include "HdThread.h" 
00025 #include "PyCanvas.h" 
00026 
00027 #include "qt/CanvasWindow.h"
00028 #include "qt/QtApp.h"
00029 
00030 using namespace boost::python;
00031 
00032 namespace hippodraw {
00033 namespace Python {
00034 
00035 void 
00036 export_HDApp()
00037 {
00038         class_ < PyApp > ( "HDApp",
00039                            "The HippoDraw application class.\n" )
00040 
00041           .def ( init < int >
00042                  ( "HDApp () -> HDApp\n"
00043                    "HDApp ( value ) -> HDApp\n"
00044                    "\n"
00045                    "Using the constructor with no arguments creates\n"
00046                    "a HippodDraw application that runs in a separate\n"
00047                    "thread.\n" 
00048                    "Using the constructor with one argument creates\n"
00049                    "a HippoDraw application in same thread. Use\n"
00050                    "app.exec_loop() to start it." ) )
00051 
00052           .def ( "instance", &PyApp::instance,
00053                  return_value_policy < reference_existing_object > (),
00054                  "instance () -> HDApp\n"
00055                  "\n"
00056                  "Returns a singleton instance of the HippoDraw \n"
00057                  "application object" )
00058 
00059           .staticmethod ( "instance" )
00060 
00061           .def ( "canvas", &PyApp::currentCanvas, 
00062                  return_value_policy < reference_existing_object > (),
00063                  "canvas () -> Canvas\n"
00064                  "\n"
00065                  "Returns the current canvas window." )
00066 
00067           .def ( "exec_loop", &PyApp::exec,
00068                  "exec_loop () -> value\n"
00069                  "\n"
00070                  "Starts the application object.  Returns the value returned\n"
00071                  "by the application object upon exiting." )
00072 
00073           .def ( "quit", &PyApp::quit,
00074                  "quit () -> None\n"
00075                  "\n"
00076                  "Closes all Canvas windows and Terminates the application." )
00077 
00078           .def ( "openDocument", &PyApp::openDocument,
00079                  "openDocument ( string ) -> None\n"
00080                  "\n"
00081                  "Opens new canvas window from saved document file." )
00082 
00083           .def ( "lock", &PyApp::lock,
00084                  "lock () -> None\n"
00085                  "\n"
00086                  "Lock the Qt library Mutex.   Will no return no other\n"
00087                  "thread has the lock." )
00088 
00089           .def ( "unlock", &PyApp::unlock,
00090                  "unlock () -> None\n"
00091                  "\n"
00092                  "Unlocks the application thread." )
00093 
00094           ;
00095 }
00096 
00097 } // namesapce Python
00098 } // namesapce hippodraw
00099 
00100 PyApp * PyApp::s_instance = 0;
00101 
00102 PyApp::PyApp ( )
00103 {
00104   HdThread * m_thread = new HdThread ( );
00105   m_thread->start ();
00106 
00107   // Wait for application to initialize itself.  It is not sufficient
00108   // to wait just for the thread to run as it may cause segmentation
00109   // fault when we try to use the appliclication.
00110   while ( QtApp::startingUp () == true  ) {
00111     m_thread->wait ( 100 ); // wait for thread to run
00112   }
00113   m_app = QtApp::instance ();
00114 
00115 #ifdef _MSC_VER
00116   // The folowing works under Windows, but under Linux leads to
00117   // occasional X Windows async errors
00118   while ( m_app->currentCanvas () == 0 ) {
00119     m_thread->wait ( 100 ); // wait for thread to create first window
00120   }
00121 #else
00122   // The following seems to work better for Linux but not at all for
00123   // Windows ...
00124   while ( m_app->hasPendingEvents () == true ) {
00125     m_thread->wait ( 100 );
00126   }
00127 #endif
00128   s_instance = this;
00129 }
00130 
00131 PyApp::PyApp ( int i )
00132 {
00133   static int argc = 1;
00134   static char * argv[1];
00135   argv[0] = "HippoDraw";
00136 
00137   m_app = new QtApp ( argc, argv );
00138 }
00139 
00140 PyApp::~PyApp ( )
00141 {
00142   //  m_app->closeAllWindows();
00143 }
00144 
00145 PyApp * PyApp::instance ()
00146 {
00147   if ( s_instance == 0 ) {
00148     s_instance = new PyApp ();
00149   }
00150 
00151   return s_instance;
00152 }
00153 
00154 int PyApp::exec ()
00155 {
00156   return m_app->exec();
00157 }
00158 
00159 void
00160 PyApp::
00161 quit ()
00162 {
00163   m_app -> closeAllWindows ();
00164 }
00165 
00166 PyCanvas * PyApp::currentCanvas ()
00167 {
00168   m_app->lock();
00169   CanvasWindow * canvas = m_app->currentCanvas();
00170   assert ( canvas );
00171 
00172   PyCanvas * pycanvas = new PyCanvas ( canvas );
00173   m_app->unlock ();
00174   return pycanvas;
00175 }
00176 
00177 void PyApp::openDocument ( const std::string & filename )
00178 {
00179   m_app->lock (); // definitely need it else documents.py crashes
00180 
00181   CanvasWindow * window = new CanvasWindow ();
00182   window -> initFromFile ( filename );
00183 
00184   m_app->unlock ();
00185 }
00186 
00187 void PyApp::lock ()
00188 {
00189   m_app->lock ();
00190 }
00191 void PyApp::unlock ()
00192 {
00193   m_app->unlock ();
00194 }
00195 

Generated for HippoDraw-1.14.8.5 by doxygen 1.4.3