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@0
|
7 This file copyright 2006 Chris Cannam.
|
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@0
|
23
|
Chris@0
|
24 #include <QMetaType>
|
Chris@0
|
25 #include <QApplication>
|
Chris@0
|
26 #include <QDesktopWidget>
|
Chris@0
|
27 #include <QMessageBox>
|
Chris@0
|
28 #include <QTranslator>
|
Chris@0
|
29 #include <QLocale>
|
Chris@5
|
30 #include <QSettings>
|
Chris@7
|
31 #include <QIcon>
|
Chris@11
|
32 #include <QSessionManager>
|
Chris@0
|
33
|
Chris@0
|
34 #include <iostream>
|
Chris@0
|
35 #include <signal.h>
|
Chris@0
|
36
|
Chris@0
|
37 //!!! catch trappable signals, cleanup temporary directory etc
|
Chris@0
|
38 //!!! check for crap left over from previous run
|
Chris@0
|
39
|
Chris@0
|
40 static QMutex cleanupMutex;
|
Chris@0
|
41
|
Chris@0
|
42 static void
|
Chris@0
|
43 signalHandler(int /* signal */)
|
Chris@0
|
44 {
|
Chris@0
|
45 // Avoid this happening more than once across threads
|
Chris@0
|
46
|
Chris@0
|
47 cleanupMutex.lock();
|
Chris@0
|
48 std::cerr << "signalHandler: cleaning up and exiting" << std::endl;
|
Chris@0
|
49 TempDirectory::getInstance()->cleanup();
|
Chris@0
|
50 exit(0); // without releasing mutex
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@11
|
53 class SVApplication : public QApplication
|
Chris@11
|
54 {
|
Chris@11
|
55 public:
|
Chris@11
|
56 SVApplication(int argc, char **argv) :
|
Chris@11
|
57 QApplication(argc, argv),
|
Chris@11
|
58 m_mainWindow(0) { }
|
Chris@11
|
59 virtual ~SVApplication() { }
|
Chris@11
|
60
|
Chris@11
|
61 void setMainWindow(MainWindow *mw) { m_mainWindow = mw; }
|
Chris@11
|
62 void releaseMainWindow() { m_mainWindow = 0; }
|
Chris@11
|
63
|
Chris@11
|
64 virtual void commitData(QSessionManager &manager) {
|
Chris@11
|
65 if (!m_mainWindow) return;
|
Chris@11
|
66 bool mayAskUser = manager.allowsInteraction();
|
Chris@11
|
67 bool success = m_mainWindow->commitData(mayAskUser);
|
Chris@11
|
68 manager.release();
|
Chris@11
|
69 if (!success) manager.cancel();
|
Chris@11
|
70 }
|
Chris@11
|
71
|
Chris@11
|
72 protected:
|
Chris@11
|
73 MainWindow *m_mainWindow;
|
Chris@11
|
74 };
|
Chris@11
|
75
|
Chris@0
|
76 int
|
Chris@0
|
77 main(int argc, char **argv)
|
Chris@0
|
78 {
|
Chris@11
|
79 SVApplication application(argc, argv);
|
Chris@0
|
80
|
Chris@46
|
81 QStringList args = application.arguments();
|
Chris@46
|
82
|
Chris@0
|
83 signal(SIGINT, signalHandler);
|
Chris@0
|
84 signal(SIGTERM, signalHandler);
|
Chris@0
|
85
|
Chris@0
|
86 #ifndef Q_WS_WIN32
|
Chris@0
|
87 signal(SIGHUP, signalHandler);
|
Chris@0
|
88 signal(SIGQUIT, signalHandler);
|
Chris@0
|
89 #endif
|
Chris@0
|
90
|
Chris@0
|
91 svSystemSpecificInitialisation();
|
Chris@0
|
92
|
Chris@46
|
93 bool audioOutput = true;
|
Chris@46
|
94 if (args.contains("--no-audio")) audioOutput = false;
|
Chris@46
|
95
|
Chris@6
|
96 QApplication::setOrganizationName("sonic-visualiser");
|
Chris@5
|
97 QApplication::setOrganizationDomain("sonicvisualiser.org");
|
Chris@6
|
98 QApplication::setApplicationName("sonic-visualiser");
|
Chris@5
|
99
|
Chris@7
|
100 QApplication::setWindowIcon(QIcon(":icons/waveform.png"));
|
Chris@7
|
101
|
Chris@0
|
102 QString language = QLocale::system().name();
|
Chris@0
|
103
|
Chris@0
|
104 QTranslator qtTranslator;
|
Chris@0
|
105 QString qtTrName = QString("qt_%1").arg(language);
|
Chris@0
|
106 std::cerr << "Loading " << qtTrName.toStdString() << "..." << std::endl;
|
Chris@0
|
107 qtTranslator.load(qtTrName);
|
Chris@0
|
108 application.installTranslator(&qtTranslator);
|
Chris@0
|
109
|
Chris@0
|
110 QTranslator svTranslator;
|
Chris@0
|
111 QString svTrName = QString("sonic-visualiser_%1").arg(language);
|
Chris@0
|
112 std::cerr << "Loading " << svTrName.toStdString() << "..." << std::endl;
|
Chris@0
|
113 svTranslator.load(svTrName, ":i18n");
|
Chris@0
|
114 application.installTranslator(&svTranslator);
|
Chris@0
|
115
|
Chris@0
|
116 // Permit size_t and PropertyName to be used as args in queued signal calls
|
Chris@0
|
117 qRegisterMetaType<size_t>("size_t");
|
Chris@0
|
118 qRegisterMetaType<PropertyContainer::PropertyName>("PropertyContainer::PropertyName");
|
Chris@0
|
119
|
Chris@46
|
120 MainWindow gui(audioOutput);
|
Chris@11
|
121 application.setMainWindow(&gui);
|
Chris@0
|
122
|
Chris@0
|
123 QDesktopWidget *desktop = QApplication::desktop();
|
Chris@0
|
124 QRect available = desktop->availableGeometry();
|
Chris@0
|
125
|
Chris@0
|
126 int width = available.width() * 2 / 3;
|
Chris@0
|
127 int height = available.height() / 2;
|
Chris@0
|
128 if (height < 450) height = available.height() * 2 / 3;
|
Chris@0
|
129 if (width > height * 2) width = height * 2;
|
Chris@0
|
130
|
Chris@5
|
131 QSettings settings;
|
Chris@5
|
132 settings.beginGroup("MainWindow");
|
Chris@5
|
133 QSize size = settings.value("size", QSize(width, height)).toSize();
|
Chris@5
|
134 gui.resize(size);
|
Chris@5
|
135 if (settings.contains("position")) {
|
Chris@5
|
136 gui.move(settings.value("position").toPoint());
|
Chris@5
|
137 }
|
Chris@5
|
138 settings.endGroup();
|
Chris@5
|
139
|
Chris@0
|
140 gui.show();
|
Chris@54
|
141 /*
|
Chris@54
|
142 QStringList pathList;
|
Chris@46
|
143 for (QStringList::iterator i = args.begin(); i != args.end(); ++i) {
|
Chris@46
|
144 if (i == args.begin()) continue;
|
Chris@46
|
145 if (!i->startsWith('-')) {
|
Chris@54
|
146 pathList.push_back(*i);
|
Chris@46
|
147 }
|
Chris@46
|
148 }
|
Chris@54
|
149 */
|
Chris@54
|
150 bool haveSession = false;
|
Chris@54
|
151 bool haveMainModel = false;
|
Chris@46
|
152
|
Chris@54
|
153 for (QStringList::iterator i = args.begin(); i != args.end(); ++i) {
|
Chris@54
|
154
|
Chris@54
|
155 bool success = false;
|
Chris@54
|
156
|
Chris@54
|
157 if (i == args.begin()) continue;
|
Chris@54
|
158 if (i->startsWith('-')) continue;
|
Chris@54
|
159
|
Chris@54
|
160 QString path = *i;
|
Chris@54
|
161
|
Chris@54
|
162 if (path.endsWith("sv")) {
|
Chris@54
|
163 if (!haveSession) {
|
Chris@54
|
164 success = gui.openSessionFile(path);
|
Chris@54
|
165 if (success) {
|
Chris@54
|
166 haveSession = true;
|
Chris@54
|
167 haveMainModel = true;
|
Chris@54
|
168 }
|
Chris@54
|
169 } else {
|
Chris@54
|
170 std::cerr << "WARNING: Ignoring additional session file argument \"" << path.toStdString() << "\"" << std::endl;
|
Chris@54
|
171 success = true;
|
Chris@54
|
172 }
|
Chris@54
|
173 }
|
Chris@54
|
174 if (!success) {
|
Chris@54
|
175 if (!haveMainModel) {
|
Chris@54
|
176 success = gui.openSomeFile(path, MainWindow::ReplaceMainModel);
|
Chris@54
|
177 if (success) haveMainModel = true;
|
Chris@54
|
178 } else {
|
Chris@54
|
179 success = gui.openSomeFile(path, MainWindow::CreateAdditionalModel);
|
Chris@54
|
180 }
|
Chris@54
|
181 }
|
Chris@54
|
182 if (!success) {
|
Chris@54
|
183 QMessageBox::critical
|
Chris@54
|
184 (&gui, QMessageBox::tr("Failed to open file"),
|
Chris@54
|
185 QMessageBox::tr("File \"%1\" could not be opened").arg(path));
|
Chris@54
|
186 }
|
Chris@54
|
187 }
|
Chris@54
|
188 /*
|
Chris@54
|
189
|
Chris@54
|
190 if (!pathList.isEmpty()) {
|
Chris@0
|
191 bool success = false;
|
Chris@0
|
192 if (path.endsWith(".sv")) {
|
Chris@0
|
193 success = gui.openSessionFile(path);
|
Chris@0
|
194 }
|
Chris@0
|
195 if (!success) {
|
Chris@0
|
196 success = gui.openSomeFile(path);
|
Chris@0
|
197 }
|
Chris@0
|
198 if (!success) {
|
Chris@0
|
199 QMessageBox::critical(&gui, QMessageBox::tr("Failed to open file"),
|
Chris@0
|
200 QMessageBox::tr("File \"%1\" could not be opened").arg(path));
|
Chris@0
|
201 }
|
Chris@0
|
202 }
|
Chris@54
|
203 */
|
Chris@0
|
204
|
Chris@0
|
205 int rv = application.exec();
|
Chris@0
|
206 std::cerr << "application.exec() returned " << rv << std::endl;
|
Chris@0
|
207
|
Chris@0
|
208 cleanupMutex.lock();
|
Chris@0
|
209 TempDirectory::getInstance()->cleanup();
|
Chris@11
|
210 application.releaseMainWindow();
|
Chris@5
|
211
|
Chris@0
|
212 return rv;
|
Chris@0
|
213 }
|