-
Notifications
You must be signed in to change notification settings - Fork 105
/
main.cpp
210 lines (172 loc) · 6.24 KB
/
main.cpp
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
/*
* Copyright 2012, 2013 Thomas Schöps
* Copyright 2012-2019 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenOrienteering is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#include <clocale>
#include <memory>
// IWYU pragma: no_include <type_traits>
#include <Qt>
#include <QtGlobal>
#include <QtPlugin> // IWYU pragma: keep
#include <QApplication>
#include <QLatin1String>
#include <QList>
#include <QLocale>
#include <QObject>
#include <QPalette>
#include <QPointer>
#include <QSettings>
#include <QString>
#include <QStringList>
#include <QTranslator>
#include <QWidget>
#include <mapper_config.h>
#if defined(QT_NETWORK_LIB)
# define MAPPER_USE_QTSINGLEAPPLICATION 1
# include <QtSingleApplication>
# include <QFileInfo>
#else
# define MAPPER_USE_QTSINGLEAPPLICATION 0
#endif
#include "global.h"
#include "mapper_resource.h"
#include "gui/home_screen_controller.h"
#include "gui/main_window.h"
#include "gui/widgets/mapper_proxystyle.h"
#include "util/backports.h" // IWYU pragma: keep
#include "util/recording_translator.h" // IWYU pragma: keep
#include "util/translation_util.h"
// IWYU pragma: no_forward_declare QTranslator
using namespace OpenOrienteering;
#if defined(Q_OS_WIN32) && defined(MAPPER_USE_POWERSHELL_POSITION_PLUGIN)
Q_IMPORT_PLUGIN(PowershellPositionPlugin)
#endif
// From map.h
namespace OpenOrienteering {
extern QPointer<QTranslator> map_symbol_translator;
} // namespace OpenOrienteering
#if MAPPER_USE_QTSINGLEAPPLICATION
void resetActivationWindow()
{
auto app = qobject_cast<QtSingleApplication*>(qApp);
app->setActivationWindow(nullptr);
if (!app->closingDown())
{
const auto* old_window = app->activationWindow();
const auto top_level_widgets = app->topLevelWidgets();
for (auto* widget : top_level_widgets)
{
auto new_window = qobject_cast<MainWindow*>(widget);
if (new_window && new_window != old_window)
{
app->setActivationWindow(new_window);
QObject::connect(new_window, &QObject::destroyed, &resetActivationWindow);
QObject::connect(app, &QtSingleApplication::messageReceived, new_window, QOverload<const QString&>::of(&MainWindow::openPath));
break;
}
}
}
}
#endif
int main(int argc, char** argv)
{
#if MAPPER_USE_QTSINGLEAPPLICATION
// Create single-instance application.
// Use "oo-mapper" instead of the executable as identifier, in case we launch from different paths.
QtSingleApplication qapp(QString::fromLatin1("oo-mapper"), argc, argv);
if (qapp.isRunning()) {
// Send a message to activate the running app, and optionally open a file
QString filepath = {};
if (argc > 1)
filepath = QFileInfo(QString::fromLocal8Bit(argv[1])).absoluteFilePath();
qapp.sendMessage(filepath);
return 0;
}
#else
QApplication qapp(argc, argv);
#endif
#ifdef Q_OS_ANDROID
qputenv("QT_USE_ANDROID_NATIVE_STYLE", "1");
// Workaround for Mapper issue GH-1373, QTBUG-72408
/// \todo Remove QTBUG-72408 workaround once solved in Qt.
qputenv("QT_QPA_NO_TEXT_HANDLES", "1");
#endif
// Load resources
Q_INIT_RESOURCE(resources);
// QSettings on OS X benefits from using an internet domain here.
QApplication::setOrganizationName(QString::fromLatin1("OpenOrienteering.org"));
QApplication::setApplicationName(QString::fromLatin1("Mapper"));
qapp.setApplicationDisplayName(APP_NAME + QString::fromUtf8(" " APP_VERSION));
#ifdef WIN32
// Load plugins on Windows
qapp.addLibraryPath(QCoreApplication::applicationDirPath() + QLatin1String("/plugins"));
#endif
MapperResource::setSeachPaths();
// Localization
QSettings settings;
TranslationUtil::setBaseName(QLatin1String("OpenOrienteering"));
TranslationUtil translation(settings);
QLocale::setDefault(QLocale(translation.code()));
#if defined(Q_OS_MACOS)
// Normally this is done in Settings::apply() because it is too late here.
// But Mapper 0.6.2/0.6.3 accidentally wrote a string instead of a list. This
// error caused crashes when opening native dialogs (i.e. the open-file dialog!).
settings.setValue(QString::fromLatin1("AppleLanguages"), QStringList{ translation.code() });
#endif
#if defined(Mapper_DEBUG_TRANSLATIONS)
if (!translation.getAppTranslator().isEmpty())
{
// Debug translation only if there is a Mapper translation, i.e. not for English.
qapp.installTranslator(new RecordingTranslator());
}
#endif
qapp.installTranslator(&translation.getQtTranslator());
qapp.installTranslator(&translation.getAppTranslator());
map_symbol_translator = translation.load(QString::fromLatin1("map_symbols")).release();
if (map_symbol_translator)
map_symbol_translator->setParent(&qapp);
// Avoid numeric issues in libraries such as GDAL
setlocale(LC_NUMERIC, "C");
// Initialize static things like the file format registry.
doStaticInitializations();
auto const palette = QApplication::palette();
QApplication::setStyle(new MapperProxyStyle());
QApplication::setPalette(palette);
// Create first main window
auto first_window = new MainWindow();
Q_ASSERT(first_window->testAttribute(Qt::WA_DeleteOnClose));
first_window->setController(new HomeScreenController());
// Open given files later, i.e. after the initial home screen has been
// displayed. In this way, error messages for missing files will show on
// top of a regular main window (home screen or other file).
// Treat all program parameters as files to be opened
QStringList args(qapp.arguments());
args.removeFirst(); // the program name
for (const auto& arg : qAsConst(args))
{
first_window->openPathLater(arg);
}
first_window->applicationStateChanged();
#if MAPPER_USE_QTSINGLEAPPLICATION
resetActivationWindow();
#endif
// Let application run
first_window->setVisible(true);
first_window->raise();
return qapp.exec();
}