QtApp.cxx

Go to the documentation of this file.
00001 
00012 #ifdef _MSC_VER
00013 #include "msdevstudio/MSconfig.h"
00014 #endif
00015 
00016 #include "QtApp.h"
00017 
00018 #include "CanvasWindow.h"
00019 #if QT_VERSION < 0x040000
00020 #include "FileOpenEvent.h"
00021 #else
00022 #include <QtGui/QFileOpenEvent>
00023 #endif
00024 #include "QtFileDialog.h"
00025 #include "WindowController.h"
00026 
00027 #include "qdir.h"
00028 #include <cassert>
00029 #include <cstdlib>
00030 
00031 using std::string;
00032 
00033 using namespace hippodraw;
00034 
00035 QtApp * QtApp::s_instance = 0;
00036 
00037 QtApp::QtApp ( int argc, char** argv)
00038   : QApplication ( argc, argv )
00039 {
00040   init ();
00041 }
00042 
00043 QtApp::QtApp ( int argc, char** argv, bool gui )
00044   : QApplication ( argc, argv, gui )
00045 {
00046   init ();
00047 
00048 }
00049 
00050 void
00051 QtApp::
00052 init ()
00053 {
00054 
00055   /* Create temp directory. Now it is used for only PNG files generated
00056      from LaTex equation. It will be deleted when the application exit. 
00057   */
00058   QDir current_dir = QDir();
00059   current_dir.mkdir("temp_latex");
00060 
00061 
00062   CanvasWindow::resetFontSize ();
00063 
00064 #if QT_VERSION < 0x040000
00065 #else
00066   // Needed in order to use std::string as argument in signal/slot connection.
00067   qRegisterMetaType < std::string > ( "std::string" );
00068 #endif
00069 
00070   // The Apple event handling implementaton for Qt 3  taken from 
00071   // http://doc.trolltech.com/qq/qq12-mac-events.html
00072 
00073 #ifdef Q_OS_MACX
00074 #if QT_VERSION < 0x040000
00075   AEInstallEventHandler ( kCoreEventClass,
00076                           kAEOpenDocuments,
00077                           appleEventHandler, 0, false );
00078 #else
00079 #endif
00080 #endif
00081 
00082   s_instance = this;
00083 }
00084 
00085 QtApp::~QtApp ()
00086 {
00087   /* It's not session safe here. Consider two copy of hippo are running. */
00088   QDir current_dir = QDir();
00089   system("rm -f temp_latex/*.*"); 
00090   current_dir.rmdir("temp_latex");
00091 
00092 
00093   WindowController * controller = WindowController::instance ();
00094   controller -> closeAllWindows ( true );
00095   delete controller;
00096 
00097 #ifdef Q_OS_MACX
00098 #if QT_VERSION < 0x040000
00099   AERemoveEventHandler ( kCoreEventClass,
00100                          kAEOpenDocuments,
00101                          appleEventHandler, false );
00102 #else
00103 #endif
00104 #endif
00105 
00106   s_instance = 0;
00107 }
00108 
00109 QtApp * QtApp::instance ()
00110 {
00111   return s_instance;
00112 }
00113 
00114 #ifdef Q_OS_MAC
00115 
00117 void
00118 QtApp::
00119 customEvent ( QCustomEvent * event )
00120 {
00121 //   int type = event -> type ();
00122 #if QT_VERSION < 0x040000
00123 //   if ( type == OpenEventType ) {
00124   FileOpenEvent * oe = dynamic_cast < FileOpenEvent * > ( event );
00125 #else
00126   QFileOpenEvent * oe = dynamic_cast < QFileOpenEvent * > ( event );
00127 #endif
00128 //     assert ( oe != 0 );
00129   if ( oe != 0 ) {
00130     QString fn = oe -> file ();
00131     const string filename = fn.latin1();
00132     tryOpenFile ( filename );
00133   }
00134 }
00135 
00136 #if QT_VERSION < 0x040000
00137 OSErr
00138 QtApp::
00139 appleEventHandler ( const AppleEvent * event,
00140                     AppleEvent *,
00141                     long )
00142 {
00143   AEDescList docs;
00144   if ( AEGetParamDesc ( event,
00145                         keyDirectObject,
00146                         typeAEList, & docs) == noErr) {
00147     long cnt = 0;
00148     AECountItems ( &docs, &cnt );
00149     UInt8 strBuffer[256];
00150     for ( int i = 0; i < cnt; i++ ) {
00151       FSRef ref;
00152       if ( AEGetNthPtr( & docs, i+1,
00153                         typeFSRef, 0, 0,
00154                         & ref, sizeof(ref), 0 ) != noErr ) continue;
00155       if ( FSRefMakePath ( &ref, strBuffer, 256) == noErr ) {
00156        QString fn ( QString::fromUtf8 ( reinterpret_cast<char * >
00157                                         ( strBuffer ) ) );
00158        FileOpenEvent event ( fn );
00159        // bool yes = 
00160        QApplication::sendEvent ( s_instance, & event );
00161       }
00162     }
00163   }
00164   return noErr;
00165 }
00166 #endif
00167 #endif
00168 
00169 void QtApp::setFirstWindow ()
00170 {
00171   bool hasWindow = false;
00172   
00173   WindowController * wc = WindowController::instance ();
00174 #if QT_VERSION < 0x040000
00175   int count = argc ();
00176   char ** args = argv ();
00177 #else
00178   QStringList args = QCoreApplication::arguments();
00179    int count = args.count();
00180 #endif
00181   // No argument.
00182   if ( count == 1 ) {
00183     wc -> setFirstWindow();
00184     return;
00185   }
00186 
00187   wc -> createInspector();
00188 
00189   QString qarg;
00190   string arg;
00191   // Process each argument
00192   for ( int i = 1; i < count; i++ ) {
00193 //     const string arg ( args[i] );
00194     qarg =  args[i];
00195 
00196     hasWindow |= tryOpenFile ( qarg.latin1() );
00197   }
00198   // No window created ( no .hpo argument ), create the first window.
00199   if ( !hasWindow ) wc->setFirstWindow();
00200 }
00201 
00202 bool
00203 QtApp::
00204 tryOpenFile ( const std::string & arg )
00205 {
00206   string::size_type pos = arg.find_last_of ( '.' );
00207   if ( pos == string::npos ) return false;
00208 
00209   string suffix = arg.substr ( pos );
00210 
00211   if ( QtFileDialog::isDocSuffix ( suffix ) ) {
00212     CanvasWindow * window = new CanvasWindow ();
00213     try {
00214       window -> initFromFile ( arg );
00215     }
00216     catch ( ... ) {
00217     }
00218     return true;
00219   }
00220 
00221   if ( QtFileDialog::isTextSuffix ( suffix ) ) {
00222     QtFileDialog::openTextTuple ( arg );
00223     return false;
00224   }
00225 
00226   // Use a QtFileDialog object to call non-static methods.
00227   QtFileDialog * qd = new QtFileDialog ();
00228 
00229   if ( QtFileDialog::isFitsSuffix ( suffix ) ) {
00230     qd->openFitsTuple ( arg, 0 );
00231     delete qd;
00232     return false;
00233   }
00234 
00235   if ( QtFileDialog::isRootSuffix ( suffix ) ) {
00236     qd->openRootTuple ( arg, 0 );
00237     delete qd;
00238     return false;
00239   }
00240 
00241   delete qd;
00242   return false;
00243 }
00244 
00245 CanvasWindow *
00246 QtApp::
00247 currentCanvas ()
00248 {
00249   return WindowController::instance() -> currentCanvas();
00250 }

Generated for HippoDraw Class Library by doxygen