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 
29 #include <cstdlib>
30 #include <cassert>
31 
32 using std::string;
33 
34 using namespace hippodraw;
35 
37 
38 QtApp::QtApp ( int & argc, char** argv)
39  : QApplication ( argc, argv )
40 {
41  init ();
42 }
43 
44 QtApp::QtApp ( int & argc, char** argv, bool gui )
45  : QApplication ( argc, argv, gui )
46 {
47  init ();
48 
49 }
50 
51 void
52 QtApp::
53 init ()
54 {
55 
56  /* Create temp directory. Now it is used for only PNG files generated
57  from LaTex equation. It will be deleted when the application exit.
58  */
59  QDir current_dir = QDir();
60  char * tmp_dir(std::getenv("TMP"));
61  if (tmp_dir) {
62  m_tmpDirName = tmp_dir + std::string("/temp_latex");
63  } else {
64  m_tmpDirName = "temp_latex";
65  }
66  current_dir.mkdir(m_tmpDirName.c_str());
67 
69 
70 #if QT_VERSION < 0x040000
71 #else
72  // Needed in order to use std::string as argument in signal/slot connection.
73  qRegisterMetaType < std::string > ( "std::string" );
74 #endif
75 
76  // The Apple event handling implementaton for Qt 3 taken from
77  // http://doc.trolltech.com/qq/qq12-mac-events.html
78 
79 #ifdef Q_OS_MACX
80 #if QT_VERSION < 0x040000
81  AEInstallEventHandler ( kCoreEventClass,
82  kAEOpenDocuments,
83  appleEventHandler, 0, false );
84 #else
85 #endif
86 #endif
87 
88  s_instance = this;
89 }
90 
92 {
93  /* It's not session safe here. Consider two copy of hippo are running. */
94  QDir current_dir = QDir();
95  std::string command("rm -f " + m_tmpDirName + "/*.*");
96  system(command.c_str());
97  current_dir.rmdir(m_tmpDirName.c_str());
98 
99 
101  controller -> closeAllWindows ( true );
102  delete controller;
103 
104 #ifdef Q_OS_MACX
105 #if QT_VERSION < 0x040000
106  AERemoveEventHandler ( kCoreEventClass,
107  kAEOpenDocuments,
108  appleEventHandler, false );
109 #else
110 #endif
111 #endif
112 
113  s_instance = 0;
114 }
115 
117 {
118  return s_instance;
119 }
120 
121 #ifdef Q_OS_MAC
122 
124 void
125 QtApp::
126 customEvent ( QCustomEvent * event )
127 {
128 // int type = event -> type ();
129 #if QT_VERSION < 0x040000
130 // if ( type == OpenEventType ) {
131  FileOpenEvent * oe = dynamic_cast < FileOpenEvent * > ( event );
132 #else
133  QFileOpenEvent * oe = dynamic_cast < QFileOpenEvent * > ( event );
134 #endif
135 // assert ( oe != 0 );
136  if ( oe != 0 ) {
137  QString fn = oe -> file ();
138  const string filename = fn.latin1();
139  tryOpenFile ( filename );
140  }
141 }
142 
143 #if QT_VERSION < 0x040000
144 OSErr
145 QtApp::
146 appleEventHandler ( const AppleEvent * event,
147  AppleEvent *,
148  long )
149 {
150  AEDescList docs;
151  if ( AEGetParamDesc ( event,
152  keyDirectObject,
153  typeAEList, & docs) == noErr) {
154  long cnt = 0;
155  AECountItems ( &docs, &cnt );
156  UInt8 strBuffer[256];
157  for ( int i = 0; i < cnt; i++ ) {
158  FSRef ref;
159  if ( AEGetNthPtr( & docs, i+1,
160  typeFSRef, 0, 0,
161  & ref, sizeof(ref), 0 ) != noErr ) continue;
162  if ( FSRefMakePath ( &ref, strBuffer, 256) == noErr ) {
163  QString fn ( QString::fromUtf8 ( reinterpret_cast<char * >
164  ( strBuffer ) ) );
165  FileOpenEvent event ( fn );
166  // bool yes =
167  QApplication::sendEvent ( s_instance, & event );
168  }
169  }
170  }
171  return noErr;
172 }
173 #endif
174 #endif
175 
177 {
178  bool hasWindow = false;
179 
181 #if QT_VERSION < 0x040000
182  int count = argc ();
183  char ** args = argv ();
184 #else
185  QStringList args = QCoreApplication::arguments();
186  int count = args.count();
187 #endif
188  // No argument.
189  if ( count == 1 ) {
190  wc -> setFirstWindow();
191  return;
192  }
193 
194  wc -> createInspector();
195 
196  QString qarg;
197  string arg;
198  // Process each argument
199  for ( int i = 1; i < count; i++ ) {
200 // const string arg ( args[i] );
201  qarg = args[i];
202 
203  hasWindow |= tryOpenFile ( qarg.latin1() );
204  }
205  // No window created ( no .hpo argument ), create the first window.
206  if ( !hasWindow ) wc->setFirstWindow();
207 }
208 
209 bool
210 QtApp::
211 tryOpenFile ( const std::string & arg )
212 {
213  string::size_type pos = arg.find_last_of ( '.' );
214  if ( pos == string::npos ) return false;
215 
216  string suffix = arg.substr ( pos );
217 
218  if ( QtFileDialog::isDocSuffix ( suffix ) ) {
219  CanvasWindow * window = new CanvasWindow ();
220  try {
221  window -> initFromFile ( arg );
222  }
223  catch ( ... ) {
224  }
225  return true;
226  }
227 
228  if ( QtFileDialog::isTextSuffix ( suffix ) ) {
230  return false;
231  }
232 
233  // Use a QtFileDialog object to call non-static methods.
234  QtFileDialog * qd = new QtFileDialog ();
235 
236  if ( QtFileDialog::isFitsSuffix ( suffix ) ) {
237  qd->openFitsTuple ( arg, 0 );
238  delete qd;
239  return false;
240  }
241 
242  if ( QtFileDialog::isRootSuffix ( suffix ) ) {
243  qd->openRootTuple ( arg, 0 );
244  delete qd;
245  return false;
246  }
247 
248  delete qd;
249  return false;
250 }
251 
252 CanvasWindow *
253 QtApp::
255 {
257 }
hippodraw::QtFileDialog class interface
void openRootTuple(const std::string &filename, QWidget *parent)
Opens a ROOT DataSource file.
unsigned int i
hippodraw::CanvasWindow class interface.
void setFirstWindow()
Sets up the first CanvasWindow object.
A singleton class for keeping track the window components of HippoDraw.
static QtApp * s_instance
The instance of the application.
Definition: QtApp.h:44
static bool isFitsSuffix(const std::string &suffix)
Returns true if suffix is one corresponding to FITS DataSource file.
bool tryOpenFile(const std::string &name)
The Apple event handler.
Definition: QtApp.cxx:211
CanvasWindow * currentCanvas()
Returns a pointer to the current CanvasWindow.
Definition: QtApp.cxx:254
hippodraw::QtApp class interface
hippodraw::WindowController class interface
argc() const
argv() const
static void resetFontSize()
Resets the font size to be no larger than 10.
A derived class of QApplication that instantiates the components of HippoDraw.
Definition: QtApp.h:39
void init()
Method called by constructors to initialize the application.
Definition: QtApp.cxx:53
QString fn
latin1() const
QtApp(int &argc, char **argv)
A Constructor that always enables the GUI.
Definition: QtApp.cxx:38
fromUtf8(const char *utf8, int len=-1)
rmdir(const QString &dirName, bool acceptAbsPath=TRUE) const
static bool isTextSuffix(const std::string &suffix)
Returns true if suffix is one corresponding to text NTuple file.
static WindowController * instance()
Returns the application instance.
A class to handle various file types.
Definition: QtFileDialog.h:49
void openFitsTuple(const std::string &filename, QWidget *parent)
Opens a FITS DataSource file.
static bool isDocSuffix(const std::string &suffix)
Returns true if suffix is one corresponding to document file.
mkdir(const QString &dirName, bool acceptAbsPath=TRUE) const
void setFirstWindow()
Sets up the first application main window.
Definition: QtApp.cxx:176
hippodraw::FileOpenEvent class interface
static bool isRootSuffix(const std::string &suffix)
Returns true if suffix is one corresponding to ROOT DataSource files.
A concrete window class that contains the canvas and responds to menu item and tool bar events from t...
Definition: CanvasWindow.h:106
std::string m_tmpDirName
Definition: QtApp.h:69
sendEvent(QObject *receiver, QEvent *event)
~QtApp()
The destructor.
Definition: QtApp.cxx:91
static QtApp * instance()
Returns the application instance.
Definition: QtApp.cxx:116
static void openTextTuple(const std::string &filename)
Opens a text NTuple file.

Generated for HippoDraw Class Library by doxygen