comparison main/main.cpp @ 389:61111a6a229f

Merge Dan's initial macness improvements (rev 174ca518546f) into default branch
author Dan Stowell <dan.stowell@eecs.qmul.ac.uk>
date Tue, 11 Jan 2011 15:11:33 +0000
parents 3d1c69b2215e 174ca518546f
children a0d162a3f3a7 e1bde903c65c
comparison
equal deleted inserted replaced
388:5c4943eae166 389:61111a6a229f
34 #include <QSessionManager> 34 #include <QSessionManager>
35 #include <QDir> 35 #include <QDir>
36 #include <QSplashScreen> 36 #include <QSplashScreen>
37 #include <QTimer> 37 #include <QTimer>
38 #include <QPainter> 38 #include <QPainter>
39 #include <QFileOpenEvent>
39 40
40 #include "../version.h" 41 #include "../version.h"
41 42
42 #include <iostream> 43 #include <iostream>
43 #include <signal.h> 44 #include <signal.h>
186 class SVApplication : public QApplication 187 class SVApplication : public QApplication
187 { 188 {
188 public: 189 public:
189 SVApplication(int &argc, char **argv) : 190 SVApplication(int &argc, char **argv) :
190 QApplication(argc, argv), 191 QApplication(argc, argv),
191 m_mainWindow(0) { } 192 m_readyForFiles(false),
193 m_filepathQueue(QStringList()),
194 m_mainWindow(0)
195 { }
192 virtual ~SVApplication() { } 196 virtual ~SVApplication() { }
193 197
194 void setMainWindow(MainWindow *mw) { m_mainWindow = mw; } 198 void setMainWindow(MainWindow *mw) { m_mainWindow = mw; }
195 void releaseMainWindow() { m_mainWindow = 0; } 199 void releaseMainWindow() { m_mainWindow = 0; }
196 200
200 bool success = m_mainWindow->commitData(mayAskUser); 204 bool success = m_mainWindow->commitData(mayAskUser);
201 manager.release(); 205 manager.release();
202 if (!success) manager.cancel(); 206 if (!success) manager.cancel();
203 } 207 }
204 208
209 void handleFilepathArgument(QString path, QSplashScreen *splash);
210
211 bool m_readyForFiles;
212 QStringList m_filepathQueue;
213
205 protected: 214 protected:
206 MainWindow *m_mainWindow; 215 MainWindow *m_mainWindow;
216 bool event(QEvent *);
217
207 }; 218 };
208 219
209 int 220 int
210 main(int argc, char **argv) 221 main(int argc, char **argv)
211 { 222 {
345 // The MainWindow class seems to have trouble dealing with this if 356 // The MainWindow class seems to have trouble dealing with this if
346 // it tries to adapt to this preference before the constructor is 357 // it tries to adapt to this preference before the constructor is
347 // complete. As a lazy hack, apply it explicitly from here 358 // complete. As a lazy hack, apply it explicitly from here
348 gui->preferenceChanged("Property Box Layout"); 359 gui->preferenceChanged("Property Box Layout");
349 360
350 bool haveSession = false; 361 application.m_readyForFiles = true; // Ready to receive files from e.g. Apple Events
351 bool haveMainModel = false;
352 bool havePriorCommandLineModel = false;
353 362
354 for (QStringList::iterator i = args.begin(); i != args.end(); ++i) { 363 for (QStringList::iterator i = args.begin(); i != args.end(); ++i) {
355
356 MainWindow::FileOpenStatus status = MainWindow::FileOpenFailed;
357 364
358 if (i == args.begin()) continue; 365 if (i == args.begin()) continue;
359 if (i->startsWith('-')) continue; 366 if (i->startsWith('-')) continue;
360 367
361 QString path = *i; 368 QString path = *i;
362 369
363 if (path.endsWith("sv")) { 370 application.handleFilepathArgument(path, splash);
364 if (!haveSession) { 371 }
365 status = gui->openSessionFile(path); 372
366 if (status == MainWindow::FileOpenSucceeded) { 373 for (QStringList::iterator i = application.m_filepathQueue.begin(); i != application.m_filepathQueue.end(); ++i) {
367 haveSession = true; 374 QString path = *i;
368 haveMainModel = true; 375 application.handleFilepathArgument(path, splash);
369 }
370 } else {
371 std::cerr << "WARNING: Ignoring additional session file argument \"" << path.toStdString() << "\"" << std::endl;
372 status = MainWindow::FileOpenSucceeded;
373 }
374 }
375 if (status != MainWindow::FileOpenSucceeded) {
376 if (!haveMainModel) {
377 status = gui->open(path, MainWindow::ReplaceMainModel);
378 if (status == MainWindow::FileOpenSucceeded) {
379 haveMainModel = true;
380 }
381 } else {
382 if (haveSession && !havePriorCommandLineModel) {
383 status = gui->open(path, MainWindow::AskUser);
384 if (status == MainWindow::FileOpenSucceeded) {
385 havePriorCommandLineModel = true;
386 }
387 } else {
388 status = gui->open(path, MainWindow::CreateAdditionalModel);
389 }
390 }
391 }
392 if (status == MainWindow::FileOpenFailed) {
393 if (splash) splash->hide();
394 QMessageBox::critical
395 (gui, QMessageBox::tr("Failed to open file"),
396 QMessageBox::tr("File or URL \"%1\" could not be opened").arg(path));
397 } else if (status == MainWindow::FileOpenWrongMode) {
398 if (splash) splash->hide();
399 QMessageBox::critical
400 (gui, QMessageBox::tr("Failed to open file"),
401 QMessageBox::tr("<b>Audio required</b><p>Please load at least one audio file before importing annotation data"));
402 }
403 } 376 }
404 377
405 #ifdef HAVE_FFTW3F 378 #ifdef HAVE_FFTW3F
406 settings.beginGroup("FFTWisdom"); 379 settings.beginGroup("FFTWisdom");
407 QString wisdom = settings.value("wisdom").toString(); 380 QString wisdom = settings.value("wisdom").toString();
455 428
456 delete gui; 429 delete gui;
457 430
458 return rv; 431 return rv;
459 } 432 }
433
434 bool SVApplication::event(QEvent *event){
435 QString thePath;
436 switch (event->type()) {
437 case QEvent::FileOpen:
438 thePath = static_cast<QFileOpenEvent *>(event)->file();
439 if(m_readyForFiles)
440 handleFilepathArgument(thePath, NULL);
441 else
442 m_filepathQueue.append(thePath);
443 return true;
444 default:
445 return QApplication::event(event);
446 }
447 }
448
449 /** Application-global handler for filepaths passed in, e.g. as command-line arguments or apple events */
450 void SVApplication::handleFilepathArgument(QString path, QSplashScreen *splash){
451 static bool haveSession = false;
452 static bool haveMainModel = false;
453 static bool havePriorCommandLineModel = false;
454
455 MainWindow::FileOpenStatus status = MainWindow::FileOpenFailed;
456
457 if (path.endsWith("sv")) {
458 if (!haveSession) {
459 status = m_mainWindow->openSessionFile(path);
460 if (status == MainWindow::FileOpenSucceeded) {
461 haveSession = true;
462 haveMainModel = true;
463 }
464 } else {
465 std::cerr << "WARNING: Ignoring additional session file argument \"" << path.toStdString() << "\"" << std::endl;
466 status = MainWindow::FileOpenSucceeded;
467 }
468 }
469 if (status != MainWindow::FileOpenSucceeded) {
470 if (!haveMainModel) {
471 status = m_mainWindow->open(path, MainWindow::ReplaceMainModel);
472 if (status == MainWindow::FileOpenSucceeded) {
473 haveMainModel = true;
474 }
475 } else {
476 if (haveSession && !havePriorCommandLineModel) {
477 status = m_mainWindow->open(path, MainWindow::AskUser);
478 if (status == MainWindow::FileOpenSucceeded) {
479 havePriorCommandLineModel = true;
480 }
481 } else {
482 status = m_mainWindow->open(path, MainWindow::CreateAdditionalModel);
483 }
484 }
485 }
486 if (status == MainWindow::FileOpenFailed) {
487 if (splash) splash->hide();
488 QMessageBox::critical
489 (m_mainWindow, QMessageBox::tr("Failed to open file"),
490 QMessageBox::tr("File or URL \"%1\" could not be opened").arg(path));
491 } else if (status == MainWindow::FileOpenWrongMode) {
492 if (splash) splash->hide();
493 QMessageBox::critical
494 (m_mainWindow, QMessageBox::tr("Failed to open file"),
495 QMessageBox::tr("<b>Audio required</b><p>Please load at least one audio file before importing annotation data"));
496 }
497 }