Fixed Android build
[GPXSee.git] / src / GUI / app.cpp
blobf2e349fddf722d4e85c54cc7c1d0ab5d2ce762ab
1 #include <QtGlobal>
2 #include <QTranslator>
3 #include <QLocale>
4 #include <QFileOpenEvent>
5 #include <QNetworkProxyFactory>
6 #include <QNetworkAccessManager>
7 #include <QLibraryInfo>
8 #include <QSurfaceFormat>
9 #include <QImageReader>
10 #include <QFileInfo>
11 #ifdef Q_OS_ANDROID
12 #include <QCoreApplication>
13 #include <QJniObject>
14 #endif // Q_OS_ANDROID
15 #include "common/programpaths.h"
16 #include "common/config.h"
17 #include "common/downloader.h"
18 #include "map/ellipsoid.h"
19 #include "map/gcs.h"
20 #include "map/conversion.h"
21 #include "map/pcs.h"
22 #include "data/dem.h"
23 #include "data/waypoint.h"
24 #include "gui.h"
25 #include "mapaction.h"
26 #include "app.h"
29 App::App(int &argc, char **argv) : QApplication(argc, argv)
31 #if defined(Q_OS_WIN32) || defined(Q_OS_MAC)
32 setApplicationName(APP_NAME);
33 #else
34 setApplicationName(QString(APP_NAME).toLower());
35 #endif
36 setApplicationVersion(APP_VERSION);
38 QTranslator *app = new QTranslator(this);
39 if (app->load(QLocale::system(), "gpxsee", "_",
40 ProgramPaths::translationsDir()))
41 installTranslator(app);
43 QTranslator *qt = new QTranslator(this);
44 #if defined(Q_OS_WIN32) || defined(Q_OS_MAC)
45 if (qt->load(QLocale::system(), "qt", "_", ProgramPaths::translationsDir()))
46 #else // Q_OS_WIN32 || Q_OS_MAC
47 if (qt->load(QLocale::system(), "qt", "_", QLibraryInfo::location(
48 QLibraryInfo::TranslationsPath)))
49 #endif // Q_OS_WIN32 || Q_OS_MAC
50 installTranslator(qt);
52 #ifdef Q_OS_MAC
53 setAttribute(Qt::AA_DontShowIconsInMenus);
54 #endif // Q_OS_MAC
55 QNetworkProxyFactory::setUseSystemConfiguration(true);
56 /* The QNetworkAccessManager must be a child of QApplication, otherwise it
57 triggers the following warning on exit (and may probably crash):
58 "QThreadStorage: Thread X exited after QThreadStorage Y destroyed" */
59 Downloader::setNetworkManager(new QNetworkAccessManager(this));
60 DEM::setDir(ProgramPaths::demDir());
61 QSurfaceFormat fmt;
62 fmt.setStencilBufferSize(8);
63 fmt.setSamples(4);
64 QSurfaceFormat::setDefaultFormat(fmt);
65 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
66 QImageReader::setAllocationLimit(0);
67 #endif // QT6
69 loadDatums();
70 loadPCSs();
71 Waypoint::loadSymbolIcons(ProgramPaths::symbolsDir());
73 _gui = new GUI();
75 #ifdef Q_OS_ANDROID
76 connect(this, &App::applicationStateChanged, this, &App::appStateChanged);
77 #endif // Q_OS_ANDROID
80 App::~App()
82 delete _gui;
85 int App::run()
87 MapAction *lastReady = 0;
88 QStringList args(arguments());
89 int silent = 0;
90 int showError = (args.count() - 1 > 1) ? 2 : 1;
92 _gui->show();
94 for (int i = 1; i < args.count(); i++) {
95 if (!_gui->openFile(args.at(i), false, silent)) {
96 MapAction *a;
97 if (!_gui->loadMap(args.at(i), a, silent))
98 _gui->openFile(args.at(i), true, showError);
99 else {
100 if (a)
101 lastReady = a;
106 if (lastReady)
107 lastReady->trigger();
109 return exec();
112 #ifdef Q_OS_ANDROID
113 void App::appStateChanged(Qt::ApplicationState state)
115 if (state == Qt::ApplicationSuspended)
116 _gui->writeSettings();
117 else if (state == Qt::ApplicationActive) {
118 QJniObject activity = QNativeInterface::QAndroidApplication::context();
119 QString path(activity.callObjectMethod<jstring>("intentPath").toString());
120 if (!path.isEmpty()) {
121 int silent = 0;
122 int showError = 1;
124 if (!_gui->openFile(path, false, silent)) {
125 MapAction *a;
126 if (!_gui->loadMap(path, a, silent))
127 _gui->openFile(path, true, showError);
128 else {
129 if (a)
130 a->trigger();
136 #endif // Q_OS_ANDROID
138 bool App::event(QEvent *event)
140 int silent = 0;
141 int showError = 1;
143 if (event->type() == QEvent::FileOpen) {
144 QFileOpenEvent *e = static_cast<QFileOpenEvent *>(event);
146 if (!_gui->openFile(e->file(), false, silent)) {
147 MapAction *a;
148 if (!_gui->loadMap(e->file(), a, silent))
149 return _gui->openFile(e->file(), true, showError);
150 else {
151 if (a)
152 a->trigger();
153 return true;
155 } else
156 return true;
159 return QApplication::event(event);
162 void App::loadDatums()
164 QString ellipsoidsFile(ProgramPaths::ellipsoidsFile());
165 QString gcsFile(ProgramPaths::gcsFile());
167 if (!QFileInfo::exists(ellipsoidsFile)) {
168 qWarning("No ellipsoids file found.");
169 ellipsoidsFile = QString();
170 } if (!QFileInfo::exists(gcsFile)) {
171 qWarning("No GCS file found.");
172 gcsFile = QString();
175 if (!ellipsoidsFile.isNull() && !gcsFile.isNull()) {
176 Ellipsoid::loadList(ellipsoidsFile);
177 GCS::loadList(gcsFile);
178 } else
179 qWarning("Maps based on a datum different from WGS84 won't work.");
182 void App::loadPCSs()
184 QString projectionsFile(ProgramPaths::projectionsFile());
185 QString pcsFile(ProgramPaths::pcsFile());
187 if (!QFileInfo::exists(projectionsFile)) {
188 qWarning("No projections file found.");
189 projectionsFile = QString();
191 if (!QFileInfo::exists(pcsFile)) {
192 qWarning("No PCS file found.");
193 pcsFile = QString();
196 if (!projectionsFile.isNull() && !pcsFile.isNull()) {
197 Conversion::loadList(projectionsFile);
198 PCS::loadList(pcsFile);
199 } else
200 qWarning("Maps based on a projection different from EPSG:3857 won't work.");