annotate main/main.cpp @ 123:a45733cc3939 sv1-1.0pre2

* Remove tip dialog for now. I don't like it enough * Fixes to export image
author Chris Cannam
date Wed, 14 Mar 2007 14:39:39 +0000
parents 863a470df510
children fbd09fcda469
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 Sonic Visualiser
Chris@0 5 An audio file viewer and annotation editor.
Chris@0 6 Centre for Digital Music, Queen Mary, University of London.
Chris@77 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@0 8
Chris@0 9 This program is free software; you can redistribute it and/or
Chris@0 10 modify it under the terms of the GNU General Public License as
Chris@0 11 published by the Free Software Foundation; either version 2 of the
Chris@0 12 License, or (at your option) any later version. See the file
Chris@0 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "MainWindow.h"
Chris@0 17
Chris@1 18 #include "system/System.h"
Chris@1 19 #include "system/Init.h"
Chris@0 20 #include "base/TempDirectory.h"
Chris@0 21 #include "base/PropertyContainer.h"
Chris@0 22 #include "base/Preferences.h"
Chris@120 23 #include "widgets/TipDialog.h"
Chris@0 24
Chris@0 25 #include <QMetaType>
Chris@0 26 #include <QApplication>
Chris@0 27 #include <QDesktopWidget>
Chris@0 28 #include <QMessageBox>
Chris@0 29 #include <QTranslator>
Chris@0 30 #include <QLocale>
Chris@5 31 #include <QSettings>
Chris@7 32 #include <QIcon>
Chris@11 33 #include <QSessionManager>
Chris@0 34
Chris@0 35 #include <iostream>
Chris@0 36 #include <signal.h>
Chris@0 37
Chris@0 38 static QMutex cleanupMutex;
Chris@0 39
Chris@0 40 static void
Chris@0 41 signalHandler(int /* signal */)
Chris@0 42 {
Chris@0 43 // Avoid this happening more than once across threads
Chris@0 44
Chris@0 45 cleanupMutex.lock();
Chris@0 46 std::cerr << "signalHandler: cleaning up and exiting" << std::endl;
Chris@0 47 TempDirectory::getInstance()->cleanup();
Chris@0 48 exit(0); // without releasing mutex
Chris@0 49 }
Chris@0 50
Chris@11 51 class SVApplication : public QApplication
Chris@11 52 {
Chris@11 53 public:
Chris@11 54 SVApplication(int argc, char **argv) :
Chris@11 55 QApplication(argc, argv),
Chris@11 56 m_mainWindow(0) { }
Chris@11 57 virtual ~SVApplication() { }
Chris@11 58
Chris@11 59 void setMainWindow(MainWindow *mw) { m_mainWindow = mw; }
Chris@11 60 void releaseMainWindow() { m_mainWindow = 0; }
Chris@11 61
Chris@11 62 virtual void commitData(QSessionManager &manager) {
Chris@11 63 if (!m_mainWindow) return;
Chris@11 64 bool mayAskUser = manager.allowsInteraction();
Chris@11 65 bool success = m_mainWindow->commitData(mayAskUser);
Chris@11 66 manager.release();
Chris@11 67 if (!success) manager.cancel();
Chris@11 68 }
Chris@11 69
Chris@11 70 protected:
Chris@11 71 MainWindow *m_mainWindow;
Chris@11 72 };
Chris@11 73
Chris@0 74 int
Chris@0 75 main(int argc, char **argv)
Chris@0 76 {
Chris@11 77 SVApplication application(argc, argv);
Chris@0 78
Chris@46 79 QStringList args = application.arguments();
Chris@46 80
Chris@0 81 signal(SIGINT, signalHandler);
Chris@0 82 signal(SIGTERM, signalHandler);
Chris@0 83
Chris@0 84 #ifndef Q_WS_WIN32
Chris@0 85 signal(SIGHUP, signalHandler);
Chris@0 86 signal(SIGQUIT, signalHandler);
Chris@0 87 #endif
Chris@0 88
Chris@0 89 svSystemSpecificInitialisation();
Chris@0 90
Chris@46 91 bool audioOutput = true;
Chris@70 92 bool oscSupport = true;
Chris@70 93
Chris@70 94 if (args.contains("--help")) {
Chris@70 95 std::cerr << QApplication::tr(
Chris@70 96 "\nSonic Visualiser is a program for viewing and exploring audio data\nfor semantic music analysis and annotation.\n\nUsage:\n\n %1 [--no-audio] [--no-osc] [<file> ...]\n\n --no-audio: Do not attempt to open an audio output device\n --no-osc: Do not provide an Open Sound Control port for remote control\n <file>: One or more Sonic Visualiser (.sv) and audio files may be provided.\n").arg(argv[0]).toStdString() << std::endl;
Chris@70 97 exit(2);
Chris@70 98 }
Chris@70 99
Chris@46 100 if (args.contains("--no-audio")) audioOutput = false;
Chris@70 101 if (args.contains("--no-osc")) oscSupport = false;
Chris@46 102
Chris@6 103 QApplication::setOrganizationName("sonic-visualiser");
Chris@5 104 QApplication::setOrganizationDomain("sonicvisualiser.org");
Chris@6 105 QApplication::setApplicationName("sonic-visualiser");
Chris@5 106
Chris@89 107 QApplication::setWindowIcon(QIcon(":icons/svicon16.png"));
Chris@7 108
Chris@0 109 QString language = QLocale::system().name();
Chris@0 110
Chris@0 111 QTranslator qtTranslator;
Chris@0 112 QString qtTrName = QString("qt_%1").arg(language);
Chris@0 113 std::cerr << "Loading " << qtTrName.toStdString() << "..." << std::endl;
Chris@0 114 qtTranslator.load(qtTrName);
Chris@0 115 application.installTranslator(&qtTranslator);
Chris@0 116
Chris@0 117 QTranslator svTranslator;
Chris@0 118 QString svTrName = QString("sonic-visualiser_%1").arg(language);
Chris@0 119 std::cerr << "Loading " << svTrName.toStdString() << "..." << std::endl;
Chris@0 120 svTranslator.load(svTrName, ":i18n");
Chris@0 121 application.installTranslator(&svTranslator);
Chris@0 122
Chris@0 123 // Permit size_t and PropertyName to be used as args in queued signal calls
Chris@0 124 qRegisterMetaType<size_t>("size_t");
Chris@0 125 qRegisterMetaType<PropertyContainer::PropertyName>("PropertyContainer::PropertyName");
Chris@0 126
Chris@70 127 MainWindow gui(audioOutput, oscSupport);
Chris@11 128 application.setMainWindow(&gui);
Chris@0 129
Chris@0 130 QDesktopWidget *desktop = QApplication::desktop();
Chris@0 131 QRect available = desktop->availableGeometry();
Chris@0 132
Chris@0 133 int width = available.width() * 2 / 3;
Chris@0 134 int height = available.height() / 2;
Chris@0 135 if (height < 450) height = available.height() * 2 / 3;
Chris@0 136 if (width > height * 2) width = height * 2;
Chris@0 137
Chris@5 138 QSettings settings;
Chris@5 139 settings.beginGroup("MainWindow");
Chris@5 140 QSize size = settings.value("size", QSize(width, height)).toSize();
Chris@5 141 gui.resize(size);
Chris@5 142 if (settings.contains("position")) {
Chris@5 143 gui.move(settings.value("position").toPoint());
Chris@5 144 }
Chris@5 145 settings.endGroup();
Chris@5 146
Chris@0 147 gui.show();
Chris@64 148
Chris@118 149 // The MainWindow class seems to have trouble dealing with this if
Chris@118 150 // it tries to adapt to this preference before the constructor is
Chris@118 151 // complete. As a lazy hack, apply it explicitly from here
Chris@118 152 gui.preferenceChanged("Property Box Layout");
Chris@118 153
Chris@54 154 bool haveSession = false;
Chris@54 155 bool haveMainModel = false;
Chris@46 156
Chris@54 157 for (QStringList::iterator i = args.begin(); i != args.end(); ++i) {
Chris@54 158
Chris@83 159 MainWindow::FileOpenStatus status = MainWindow::FileOpenFailed;
Chris@54 160
Chris@54 161 if (i == args.begin()) continue;
Chris@54 162 if (i->startsWith('-')) continue;
Chris@54 163
Chris@85 164 if (i->startsWith("http:") || i->startsWith("ftp:")) {
Chris@85 165 status = gui.openURL(QUrl(*i));
Chris@85 166 continue;
Chris@85 167 }
Chris@85 168
Chris@54 169 QString path = *i;
Chris@54 170
Chris@54 171 if (path.endsWith("sv")) {
Chris@54 172 if (!haveSession) {
Chris@82 173 status = gui.openSessionFile(path);
Chris@82 174 if (status == MainWindow::FileOpenSucceeded) {
Chris@54 175 haveSession = true;
Chris@54 176 haveMainModel = true;
Chris@54 177 }
Chris@54 178 } else {
Chris@54 179 std::cerr << "WARNING: Ignoring additional session file argument \"" << path.toStdString() << "\"" << std::endl;
Chris@82 180 status = MainWindow::FileOpenSucceeded;
Chris@54 181 }
Chris@54 182 }
Chris@82 183 if (status != MainWindow::FileOpenSucceeded) {
Chris@54 184 if (!haveMainModel) {
Chris@82 185 status = gui.openSomeFile(path, MainWindow::ReplaceMainModel);
Chris@82 186 if (status == MainWindow::FileOpenSucceeded) haveMainModel = true;
Chris@54 187 } else {
Chris@82 188 status = gui.openSomeFile(path, MainWindow::CreateAdditionalModel);
Chris@54 189 }
Chris@54 190 }
Chris@82 191 if (status == MainWindow::FileOpenFailed) {
Chris@54 192 QMessageBox::critical
Chris@54 193 (&gui, QMessageBox::tr("Failed to open file"),
Chris@54 194 QMessageBox::tr("File \"%1\" could not be opened").arg(path));
Chris@54 195 }
Chris@54 196 }
Chris@123 197 /*
Chris@120 198 TipDialog tipDialog;
Chris@120 199 if (tipDialog.isOK()) {
Chris@120 200 tipDialog.exec();
Chris@120 201 }
Chris@123 202 */
Chris@0 203 int rv = application.exec();
Chris@0 204 std::cerr << "application.exec() returned " << rv << std::endl;
Chris@0 205
Chris@0 206 cleanupMutex.lock();
Chris@0 207 TempDirectory::getInstance()->cleanup();
Chris@11 208 application.releaseMainWindow();
Chris@5 209
Chris@0 210 return rv;
Chris@0 211 }