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
00030 using std::string;
00031
00032 using namespace hippodraw;
00033
00034 QtApp * QtApp::s_instance = 0;
00035
00036 QtApp::QtApp ( int argc, char** argv)
00037 : QApplication ( argc, argv )
00038 {
00039 init ();
00040 }
00041
00042 QtApp::QtApp ( int argc, char** argv, bool gui )
00043 : QApplication ( argc, argv, gui )
00044 {
00045 init ();
00046
00047 }
00048
00049 void
00050 QtApp::
00051 init ()
00052 {
00053
00054
00055
00056
00057 QDir current_dir = QDir();
00058 current_dir.mkdir("temp_latex");
00059
00060
00061 CanvasWindow::resetFontSize ();
00062
00063 #if QT_VERSION < 0x040000
00064 #else
00065
00066 qRegisterMetaType < std::string > ( "std::string" );
00067 #endif
00068
00069
00070
00071
00072 #ifdef Q_OS_MACX
00073 #if QT_VERSION < 0x040000
00074 AEInstallEventHandler ( kCoreEventClass,
00075 kAEOpenDocuments,
00076 appleEventHandler, 0, false );
00077 #else
00078 #endif
00079 #endif
00080
00081 s_instance = this;
00082 }
00083
00084 QtApp::~QtApp ()
00085 {
00086
00087 QDir current_dir = QDir();
00088 system("rm -f temp_latex/*.*");
00089 current_dir.rmdir("temp_latex");
00090
00091
00092 WindowController * controller = WindowController::instance ();
00093 controller -> closeAllWindows ( true );
00094 delete controller;
00095
00096 #ifdef Q_OS_MACX
00097 #if QT_VERSION < 0x040000
00098 AERemoveEventHandler ( kCoreEventClass,
00099 kAEOpenDocuments,
00100 appleEventHandler, false );
00101 #else
00102 #endif
00103 #endif
00104
00105 s_instance = 0;
00106 }
00107
00108 QtApp * QtApp::instance ()
00109 {
00110 return s_instance;
00111 }
00112
00113 #ifdef Q_OS_MAC
00114
00116 void
00117 QtApp::
00118 customEvent ( QCustomEvent * event )
00119 {
00120
00121 #if QT_VERSION < 0x040000
00122
00123 FileOpenEvent * oe = dynamic_cast < FileOpenEvent * > ( event );
00124 #else
00125 QFileOpenEvent * oe = dynamic_cast < QFileOpenEvent * > ( event );
00126 #endif
00127
00128 if ( oe != 0 ) {
00129 QString fn = oe -> file ();
00130 const string filename = fn.latin1();
00131 tryOpenFile ( filename );
00132 }
00133 }
00134
00135 #if QT_VERSION < 0x040000
00136 OSErr
00137 QtApp::
00138 appleEventHandler ( const AppleEvent * event,
00139 AppleEvent *,
00140 long )
00141 {
00142 AEDescList docs;
00143 if ( AEGetParamDesc ( event,
00144 keyDirectObject,
00145 typeAEList, & docs) == noErr) {
00146 long cnt = 0;
00147 AECountItems ( &docs, &cnt );
00148 UInt8 strBuffer[256];
00149 for ( int i = 0; i < cnt; i++ ) {
00150 FSRef ref;
00151 if ( AEGetNthPtr( & docs, i+1,
00152 typeFSRef, 0, 0,
00153 & ref, sizeof(ref), 0 ) != noErr ) continue;
00154 if ( FSRefMakePath ( &ref, strBuffer, 256) == noErr ) {
00155 QString fn ( QString::fromUtf8 ( reinterpret_cast<char * >
00156 ( strBuffer ) ) );
00157 FileOpenEvent event ( fn );
00158
00159 QApplication::sendEvent ( s_instance, & event );
00160 }
00161 }
00162 }
00163 return noErr;
00164 }
00165 #endif
00166 #endif
00167
00168 void QtApp::setFirstWindow ()
00169 {
00170 bool hasWindow = false;
00171
00172 WindowController * wc = WindowController::instance ();
00173 #if QT_VERSION < 0x040000
00174 int count = argc ();
00175 char ** args = argv ();
00176 #else
00177 QStringList args = QCoreApplication::arguments();
00178 int count = args.count();
00179 #endif
00180
00181 if ( count == 1 ) {
00182 wc -> setFirstWindow();
00183 return;
00184 }
00185
00186 wc -> createInspector();
00187
00188 QString qarg;
00189 string arg;
00190
00191 for ( int i = 1; i < count; i++ ) {
00192
00193 qarg = args[i];
00194
00195 hasWindow |= tryOpenFile ( qarg.latin1() );
00196 }
00197
00198 if ( !hasWindow ) wc->setFirstWindow();
00199 }
00200
00201 bool
00202 QtApp::
00203 tryOpenFile ( const std::string & arg )
00204 {
00205 string::size_type pos = arg.find_last_of ( '.' );
00206 if ( pos == string::npos ) return false;
00207
00208 string suffix = arg.substr ( pos );
00209
00210 if ( QtFileDialog::isDocSuffix ( suffix ) ) {
00211 CanvasWindow * window = new CanvasWindow ();
00212 try {
00213 window -> initFromFile ( arg );
00214 }
00215 catch ( ... ) {
00216 }
00217 return true;
00218 }
00219
00220 if ( QtFileDialog::isTextSuffix ( suffix ) ) {
00221 QtFileDialog::openTextTuple ( arg );
00222 return false;
00223 }
00224
00225
00226 QtFileDialog * qd = new QtFileDialog ();
00227
00228 if ( QtFileDialog::isFitsSuffix ( suffix ) ) {
00229 qd->openFitsTuple ( arg, 0 );
00230 delete qd;
00231 return false;
00232 }
00233
00234 if ( QtFileDialog::isRootSuffix ( suffix ) ) {
00235 qd->openRootTuple ( arg, 0 );
00236 delete qd;
00237 return false;
00238 }
00239
00240 delete qd;
00241 return false;
00242 }
00243
00244 CanvasWindow *
00245 QtApp::
00246 currentCanvas ()
00247 {
00248 return WindowController::instance() -> currentCanvas();
00249 }