-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathmain.cpp
More file actions
241 lines (192 loc) · 6.69 KB
/
Copy pathmain.cpp
File metadata and controls
241 lines (192 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// NOTE: To run, it is recommended not to be in Compiz or Beryl, they have shown some instability.
#include <iostream>
#include <QTranslator>
#include <QDebug>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QOpenGLContext>
#include "MM.h"
#include "MainWindow.h"
#include "MainApplication.h"
#include "MetaObjectRegistry.h"
#include <stdlib.h>
MM_USE_NAMESPACE
// This class is just used to provide sleep functionalities in the main() method.
class I : public QThread
{
public:
static void sleep(unsigned long secs) {
QThread::sleep(secs);
}
static void msleep(unsigned long msecs) {
QThread::msleep(msecs);
}
static void usleep(unsigned long usecs) {
QThread::usleep(usecs);
}
};
void initRegistry()
{
MetaObjectRegistry& registry = MetaObjectRegistry::instance();
// Sources.
registry.add<Video>();
registry.add<Image>();
registry.add<Color>();
// Layers (formerly Mappings).
registry.add<TextureLayer>();
registry.add<ColorLayer>();
// Backward compatibility for old project files.
registry.addAlias("mmp::TextureMapping", &TextureLayer::staticMetaObject);
registry.addAlias("mmp::ColorMapping", &ColorLayer::staticMetaObject);
// Shapes.
registry.add<Quad>();
registry.add<Mesh>();
registry.add<MM_PREPEND_NAMESPACE(Ellipse)>();
registry.add<Triangle>();
}
// Intercept all logging message and display it in the console
void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
ConsoleWindow::console()->printMessage(type, context, msg);
}
int main(int argc, char *argv[])
{
// Enable shared OpenGL contexts so the two canvases share textures.
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
// Initialize meta-object registry.
initRegistry();
MainApplication app(argc, argv);
// Install message handler after QGuiApplication has been instantiated.
qInstallMessageHandler(logMessageHandler);
QCommandLineParser parser;
parser.setApplicationDescription("Video mapping editor");
// --help option
const QCommandLineOption helpOption = parser.addHelpOption();
// --version option
const QCommandLineOption versionOption = parser.addVersionOption();
// --fullscreen option
QCommandLineOption fullscreenOption(QStringList() << "F" << "fullscreen",
"Display the output window and make it fullscreen.");
parser.addOption(fullscreenOption);
// --file option
QCommandLineOption fileOption(QStringList() << "f" << "file",
"Load project from <file>.", "file", "");
parser.addOption(fileOption);
// --reset-settings option
QCommandLineOption resetSettingsOption(QStringList() << "R" << "reset-settings",
"Reset MapMap settings, such as GUI properties.");
parser.addOption(resetSettingsOption);
// --osc-port option
QCommandLineOption oscPortOption(QStringList() << "p" << "osc-port",
"Use OSC port number <osc-port>.", "osc-port", "");
parser.addOption(oscPortOption);
// --lang option
QCommandLineOption localeOption(QStringList() << "l" << "lang",
"Use language <lang>.", "lang", "");
parser.addOption(localeOption);
// --verbose option
QCommandLineOption verboseOption(QStringList() << "V" << "verbose",
"Enable verbose output, including OSC message logging.");
parser.addOption(verboseOption);
// --frame-rate option
QCommandLineOption frameRateOption(QStringList() << "r" << "frame-rate",
"Use a framerate of <frame-rate> per second.", "frame-rate", QString::number(MM::DEFAULT_FRAMES_PER_SECOND));
parser.addOption(frameRateOption);
// Positional argument: file
parser.addPositionalArgument("file", "Load project from that file.");
parser.process(app);
if (parser.isSet(versionOption) || parser.isSet(helpOption))
{
return 0;
}
if (parser.isSet(resetSettingsOption))
{
Util::eraseSettings();
}
// IMPORTANT: Translator must be set *before* the MainWindow is created for it to work.
QSettings settings;
// Get language from command line or user settings
QString lang = parser.value("lang").isEmpty()
? settings.value("language").toString()
: parser.value("lang");
QTranslator qtTranslator;
QTranslator appTranslator;
if (MM::SUPPORTED_LANGUAGES.contains(lang))
{
#ifdef Q_OS_WIN32
qtTranslator.load(QString("qt_%1").arg(lang),
QApplication::applicationDirPath().append("/translations"));
#else
(void)qtTranslator.load(QString("qtbase_%1").arg(lang),
QLibraryInfo::path(QLibraryInfo::TranslationsPath));
#endif
app.installTranslator(&qtTranslator);
(void)appTranslator.load(QString(":/translations_mapmap_%1").arg(lang));
app.installTranslator(&appTranslator);
}
else {
qWarning() << "Unrecognized/unsupported language: " << lang;
}
// Create splash screen.
QPixmap pixmap(":/mapmap-splash");
QSplashScreen splash(pixmap);
// Show splash.
splash.show();
splash.showMessage(" " + QObject::tr("Initiating program..."),
Qt::AlignLeft | Qt::AlignTop, MM::WHITE);
// Let splash for at least one second.
I::sleep(1);
// Create window.
MainWindow* win = MainWindow::window();
// Add custom font
int id = QFontDatabase::addApplicationFont(":/base-font");
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
app.setFont(QFont(family, 11, QFont::Normal));
// Load stylesheet.
QFile stylesheet(":/stylesheet");
(void)stylesheet.open(QFile::ReadOnly);
app.setStyleSheet(QLatin1String(stylesheet.readAll()));
// read positional argument:
const QStringList args = parser.positionalArguments();
QString projectFileValue = QString();
// there are two ways to specify the project file name.
// The 2nd overrides the first:
// read the file option value: (overrides the positional argument)
projectFileValue = parser.value("file");
// read the first positional argument:
if (! args.isEmpty())
{
projectFileValue = args.first();
}
// finally, load the project file.
if (projectFileValue != "")
{
win->loadFile(projectFileValue);
}
QString oscPortValue = parser.value("osc-port");
if (oscPortValue != "")
win->setOscPort(oscPortValue);
if (parser.isSet(verboseOption))
win->setVerbose(true);
bool optionOk;
qreal fps = parser.value("frame-rate").toDouble(&optionOk);
if (optionOk)
win->setFramesPerSecond(fps);
else
qFatal("Invalid option <frame-rate>.");
// Terminate splash.
splash.showMessage(" " + QObject::tr("Done."),
Qt::AlignLeft | Qt::AlignTop, MM::WHITE);
splash.finish(win);
splash.raise();
// Launch program.
win->show();
if (parser.isSet(fullscreenOption))
{
win->startFullScreen();
}
// Start app.
int result = app.exec();
delete win;
return result;
}