QtApp.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 #include "msdevstudio/MSconfig.h"
14 #endif
15 
16 #include "QtApp.h"
17 
18 #include "CanvasWindow.h"
19 #if QT_VERSION < 0x040000
20 #include "FileOpenEvent.h"
21 #else
22 #include <QtGui/QFileOpenEvent>
23 #endif
24 #include "QtFileDialog.h"
25 #include "WindowController.h"
26 
27 #include "qdir.h"
28 #include <cassert>
29 #include <cstdlib>
30 
31 using std::string;
32 
33 using namespace hippodraw;
34 
36 
37 QtApp::QtApp ( int argc, char** argv)
38  : QApplication ( argc, argv )
39 {
40  init ();
41 }
42 
43 QtApp::QtApp ( int argc, char** argv, bool gui )
44  : QApplication ( argc, argv, gui )
45 {
46  init ();
47 
48 }
49 
50 void
51 QtApp::
52 init ()
53 {
54 
55  /* Create temp directory. Now it is used for only PNG files generated
56  from LaTex equation. It will be deleted when the application exit.
57  */
58  QDir current_dir = QDir();
59  current_dir.mkdir("temp_latex");
60 
61 
63 
64 #if QT_VERSION < 0x040000
65 #else
66  // Needed in order to use std::string as argument in signal/slot connection.
67  qRegisterMetaType < std::string > ( "std::string" );
68 #endif
69 
70  // The Apple event handling implementaton for Qt 3 taken from
71  // http://doc.trolltech.com/qq/qq12-mac-events.html
72 
73 #ifdef Q_OS_MACX
74 #if QT_VERSION < 0x040000
75  AEInstallEventHandler ( kCoreEventClass,
76  kAEOpenDocuments,
77  appleEventHandler, 0, false );
78 #else
79 #endif
80 #endif
81 
82  s_instance = this;
83 }
84 
86 {
87  /* It's not session safe here. Consider two copy of hippo are running. */
88  QDir current_dir = QDir();
89  system("rm -f temp_latex/*.*");
90  current_dir.rmdir("temp_latex");
91 
92 
94  controller -> closeAllWindows ( true );
95  delete controller;
96 
97 #ifdef Q_OS_MACX
98 #if QT_VERSION < 0x040000
99  AERemoveEventHandler ( kCoreEventClass,
100  kAEOpenDocuments,
101  appleEventHandler, false );
102 #else
103 #endif
104 #endif
105 
106  s_instance = 0;
107 }
108 
110 {
111  return s_instance;
112 }
113 
114 #ifdef Q_OS_MAC
115 
117 void
118 QtApp::
119 customEvent ( QCustomEvent * event )
120 {
121 // int type = event -> type ();
122 #if QT_VERSION < 0x040000
123 // if ( type == OpenEventType ) {
124  FileOpenEvent * oe = dynamic_cast < FileOpenEvent * > ( event );
125 #else
126  QFileOpenEvent * oe = dynamic_cast < QFileOpenEvent * > ( event );
127 #endif
128 // assert ( oe != 0 );
129  if ( oe != 0 ) {
130  QString fn = oe -> file ();
131  const string filename = fn.latin1();
132  tryOpenFile ( filename );
133  }
134 }
135 
136 #if QT_VERSION < 0x040000
137 OSErr
138 QtApp::
139 appleEventHandler ( const AppleEvent * event,
140  AppleEvent *,
141  long )
142 {
143  AEDescList docs;
144  if ( AEGetParamDesc ( event,
145  keyDirectObject,
146  typeAEList, & docs) == noErr) {
147  long cnt = 0;
148  AECountItems ( &docs, &cnt );
149  UInt8 strBuffer[256];
150  for ( int i = 0; i < cnt; i++ ) {
151  FSRef ref;
152  if ( AEGetNthPtr( & docs, i+1,
153  typeFSRef, 0, 0,
154  & ref, sizeof(ref), 0 ) != noErr ) continue;
155  if ( FSRefMakePath ( &ref, strBuffer, 256) == noErr ) {
156  QString fn ( QString::fromUtf8 ( reinterpret_cast<char * >
157  ( strBuffer ) ) );
158  FileOpenEvent event ( fn );
159  // bool yes =
160  QApplication::sendEvent ( s_instance, & event );
161  }
162  }
163  }
164  return noErr;
165 }
166 #endif
167 #endif
168 
170 {
171  bool hasWindow = false;
172 
174 #if QT_VERSION < 0x040000
175  int count = argc ();
176  char ** args = argv ();
177 #else
178  QStringList args = QCoreApplication::arguments();
179  int count = args.count();
180 #endif
181  // No argument.
182  if ( count == 1 ) {
183  wc -> setFirstWindow();
184  return;
185  }
186 
187  wc -> createInspector();
188 
189  QString qarg;
190  string arg;
191  // Process each argument
192  for ( int i = 1; i < count; i++ ) {
193 // const string arg ( args[i] );
194  qarg = args[i];
195 
196  hasWindow |= tryOpenFile ( qarg.latin1() );
197  }
198  // No window created ( no .hpo argument ), create the first window.
199  if ( !hasWindow ) wc->setFirstWindow();
200 }
201 
202 bool
203 QtApp::
204 tryOpenFile ( const std::string & arg )
205 {
206  string::size_type pos = arg.find_last_of ( '.' );
207  if ( pos == string::npos ) return false;
208 
209  string suffix = arg.substr ( pos );
210 
211  if ( QtFileDialog::isDocSuffix ( suffix ) ) {
212  CanvasWindow * window = new CanvasWindow ();
213  try {
214  window -> initFromFile ( arg );
215  }
216  catch ( ... ) {
217  }
218  return true;
219  }
220 
221  if ( QtFileDialog::isTextSuffix ( suffix ) ) {
223  return false;
224  }
225 
226  // Use a QtFileDialog object to call non-static methods.
227  QtFileDialog * qd = new QtFileDialog ();
228 
229  if ( QtFileDialog::isFitsSuffix ( suffix ) ) {
230  qd->openFitsTuple ( arg, 0 );
231  delete qd;
232  return false;
233  }
234 
235  if ( QtFileDialog::isRootSuffix ( suffix ) ) {
236  qd->openRootTuple ( arg, 0 );
237  delete qd;
238  return false;
239  }
240 
241  delete qd;
242  return false;
243 }
244 
245 CanvasWindow *
246 QtApp::
248 {
250 }

Generated for HippoDraw Class Library by doxygen