2018-07-11 09:34:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <QQmlEngine>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QQuickItem>
|
|
|
|
|
2018-07-11 10:17:06 +00:00
|
|
|
#include "map-widget/qmlmapwidgethelper.h"
|
|
|
|
#include "qt-models/maplocationmodel.h"
|
2018-07-11 09:34:33 +00:00
|
|
|
#include "core/qt-gui.h"
|
|
|
|
#include "core/settings/qPref.h"
|
2018-07-11 10:17:06 +00:00
|
|
|
#include "core/ssrf.h"
|
|
|
|
|
2018-07-11 09:34:33 +00:00
|
|
|
#ifdef SUBSURFACE_MOBILE
|
2019-09-28 19:04:52 +00:00
|
|
|
#include <QApplication>
|
2018-07-11 10:17:06 +00:00
|
|
|
#include <QQmlApplicationEngine>
|
|
|
|
#include <QQmlContext>
|
2020-01-13 13:45:32 +00:00
|
|
|
#include "mobile-widgets/themeinterface.h"
|
2018-07-11 09:34:33 +00:00
|
|
|
#include "mobile-widgets/qmlmanager.h"
|
2020-01-04 08:47:10 +00:00
|
|
|
#include "mobile-widgets/qmlinterface.h"
|
2018-07-11 09:34:33 +00:00
|
|
|
#include "qt-models/divelistmodel.h"
|
2020-02-08 11:06:57 +00:00
|
|
|
#include "qt-models/divesummarymodel.h"
|
2018-07-11 09:34:33 +00:00
|
|
|
#include "qt-models/gpslistmodel.h"
|
2018-07-11 10:17:06 +00:00
|
|
|
#include "qt-models/messagehandlermodel.h"
|
2019-11-12 05:35:20 +00:00
|
|
|
#include "qt-models/mobilelistmodel.h"
|
2018-07-11 09:34:33 +00:00
|
|
|
#include "profile-widget/qmlprofile.h"
|
|
|
|
#include "core/downloadfromdcthread.h"
|
|
|
|
#include "qt-models/diveimportedmodel.h"
|
2018-07-11 10:17:06 +00:00
|
|
|
#include "mobile-widgets/qml/kirigami/src/kirigamiplugin.h"
|
|
|
|
#else
|
|
|
|
#include "desktop-widgets/mainwindow.h"
|
2018-07-11 10:09:36 +00:00
|
|
|
#endif
|
2018-07-11 10:17:06 +00:00
|
|
|
|
2019-09-19 00:39:52 +00:00
|
|
|
#if defined(Q_OS_ANDROID)
|
|
|
|
QString getAndroidHWInfo(); // from android.cpp
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QFontDatabase>
|
|
|
|
#endif /* Q_OS_ANDROID */
|
|
|
|
|
2018-07-12 16:22:56 +00:00
|
|
|
#ifndef SUBSURFACE_TEST_DATA
|
2018-07-11 10:17:06 +00:00
|
|
|
QObject *qqWindowObject = NULL;
|
|
|
|
|
2018-09-12 10:22:11 +00:00
|
|
|
// Forward declaration
|
|
|
|
static void register_qml_types(QQmlEngine *);
|
2018-07-15 15:56:18 +00:00
|
|
|
static void register_meta_types();
|
2018-09-12 10:22:11 +00:00
|
|
|
|
2018-07-11 10:17:06 +00:00
|
|
|
void init_ui()
|
|
|
|
{
|
|
|
|
init_qt_late();
|
2018-07-15 15:56:18 +00:00
|
|
|
register_meta_types();
|
2018-07-11 10:17:06 +00:00
|
|
|
#ifndef SUBSURFACE_MOBILE
|
2018-09-12 10:22:11 +00:00
|
|
|
register_qml_types(NULL);
|
2018-07-11 10:17:06 +00:00
|
|
|
|
|
|
|
MainWindow *window = new MainWindow();
|
|
|
|
window->setTitle();
|
2018-07-12 16:22:56 +00:00
|
|
|
#endif // SUBSURFACE_MOBILE
|
2018-07-11 10:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void exit_ui()
|
|
|
|
{
|
|
|
|
#ifndef SUBSURFACE_MOBILE
|
|
|
|
delete MainWindow::instance();
|
2018-07-12 16:22:56 +00:00
|
|
|
#endif // SUBSURFACE_MOBILE
|
2018-07-11 10:17:06 +00:00
|
|
|
free((void *)existing_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_ui()
|
|
|
|
{
|
|
|
|
#ifdef SUBSURFACE_MOBILE
|
2019-09-28 19:03:55 +00:00
|
|
|
#if defined(Q_OS_ANDROID)
|
|
|
|
if (getAndroidHWInfo().contains("/OnePlus/")) {
|
|
|
|
QFontDatabase db;
|
|
|
|
int id = QFontDatabase::addApplicationFont(":/fonts/Roboto-Regular.ttf");
|
|
|
|
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
|
|
|
|
QFont newDefaultFont;
|
|
|
|
newDefaultFont.setFamily(family);
|
|
|
|
(static_cast<QApplication *>(QCoreApplication::instance()))->setFont(newDefaultFont);
|
|
|
|
qDebug() << "Detected OnePlus device, trying to force bundled font" << family;
|
|
|
|
QFont defaultFont = (static_cast<QApplication *>(QCoreApplication::instance()))->font();
|
|
|
|
qDebug() << "Qt reports default font is set as" << defaultFont.family();
|
|
|
|
}
|
|
|
|
#endif
|
2019-09-28 19:04:52 +00:00
|
|
|
QScreen *appScreen = QApplication::screens().at(0);
|
|
|
|
int availableScreenWidth = appScreen->availableSize().width();
|
2018-07-11 10:17:06 +00:00
|
|
|
QQmlApplicationEngine engine;
|
2018-09-04 09:18:43 +00:00
|
|
|
register_qml_types(&engine);
|
2018-07-11 10:17:06 +00:00
|
|
|
KirigamiPlugin::getInstance().registerTypes();
|
|
|
|
#if defined(__APPLE__) && !defined(Q_OS_IOS)
|
|
|
|
// when running the QML UI on a Mac the deployment of the QML Components seems
|
|
|
|
// to fail and the search path for the components is rather odd - simply the
|
|
|
|
// same directory the executable was started from <bundle>/Contents/MacOS/
|
|
|
|
// To work around this we need to manually copy the components at install time
|
|
|
|
// to Contents/Frameworks/qml and make sure that we add the correct import path
|
2019-04-01 20:15:19 +00:00
|
|
|
const QStringList importPathList = engine.importPathList();
|
|
|
|
for (QString importPath: importPathList) {
|
2018-07-11 10:17:06 +00:00
|
|
|
if (importPath.contains("MacOS"))
|
|
|
|
engine.addImportPath(importPath.replace("MacOS", "Frameworks"));
|
|
|
|
}
|
|
|
|
qDebug() << "QML import path" << engine.importPathList();
|
2018-07-12 16:22:56 +00:00
|
|
|
#endif // __APPLE__ not Q_OS_IOS
|
2018-07-11 10:17:06 +00:00
|
|
|
engine.addImportPath("qrc://imports");
|
2019-09-28 19:04:52 +00:00
|
|
|
QSortFilterProxyModel *gpsSortModel = new QSortFilterProxyModel(nullptr);
|
2019-09-28 21:23:57 +00:00
|
|
|
gpsSortModel->setSourceModel(GpsListModel::instance());
|
2018-07-11 10:17:06 +00:00
|
|
|
gpsSortModel->setDynamicSortFilter(true);
|
|
|
|
gpsSortModel->setSortRole(GpsListModel::GpsWhenRole);
|
|
|
|
gpsSortModel->sort(0, Qt::DescendingOrder);
|
|
|
|
QQmlContext *ctxt = engine.rootContext();
|
2019-11-12 05:35:20 +00:00
|
|
|
MobileListModel *mlm(MobileListModel::instance());
|
2019-12-05 21:36:09 +00:00
|
|
|
ctxt->setContextProperty("diveModel", mlm);
|
2018-07-11 10:17:06 +00:00
|
|
|
ctxt->setContextProperty("gpsModel", gpsSortModel);
|
|
|
|
ctxt->setContextProperty("vendorList", vendorList);
|
|
|
|
set_non_bt_addresses();
|
|
|
|
|
|
|
|
ctxt->setContextProperty("connectionListModel", &connectionListModel);
|
|
|
|
ctxt->setContextProperty("logModel", MessageHandlerModel::self());
|
|
|
|
|
2019-11-19 18:54:15 +00:00
|
|
|
qmlRegisterUncreatableType<QMLManager>("org.subsurfacedivelog.mobile",1,0,"ExportType","Enum is not a type");
|
|
|
|
|
2019-11-23 12:09:25 +00:00
|
|
|
#ifdef SUBSURFACE_MOBILE_DESKTOP
|
|
|
|
if (testqml) {
|
|
|
|
QString fileLoad(testqml);
|
|
|
|
fileLoad += "/main.qml";
|
|
|
|
engine.load(QUrl(fileLoad));
|
|
|
|
} else {
|
|
|
|
engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));
|
|
|
|
}
|
|
|
|
#else
|
2018-07-11 10:17:06 +00:00
|
|
|
engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));
|
2019-11-23 12:09:25 +00:00
|
|
|
#endif
|
2019-09-28 19:04:52 +00:00
|
|
|
qDebug() << "loaded main.qml";
|
2018-07-11 10:17:06 +00:00
|
|
|
qqWindowObject = engine.rootObjects().value(0);
|
|
|
|
if (!qqWindowObject) {
|
|
|
|
fprintf(stderr, "can't create window object\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
QQuickWindow *qml_window = qobject_cast<QQuickWindow *>(qqWindowObject);
|
|
|
|
qml_window->setIcon(QIcon(":subsurface-mobile-icon"));
|
|
|
|
qDebug() << "qqwindow devicePixelRatio" << qml_window->devicePixelRatio() << qml_window->screen()->devicePixelRatio();
|
|
|
|
QScreen *screen = qml_window->screen();
|
2019-09-28 19:04:52 +00:00
|
|
|
int qmlWW = qml_window->width();
|
|
|
|
int qmlSW = screen->size().width();
|
|
|
|
qDebug() << "qml_window reports width as" << qmlWW << "associated screen width" << qmlSW << "Qt screen reports width as" << availableScreenWidth;
|
2018-07-11 10:17:06 +00:00
|
|
|
QObject::connect(qml_window, &QQuickWindow::screenChanged, QMLManager::instance(), &QMLManager::screenChanged);
|
|
|
|
QMLManager *manager = QMLManager::instance();
|
|
|
|
|
|
|
|
manager->setDevicePixelRatio(qml_window->devicePixelRatio(), qml_window->screen());
|
2018-10-20 15:53:36 +00:00
|
|
|
manager->qmlWindow = qqWindowObject;
|
2018-07-11 10:17:06 +00:00
|
|
|
manager->screenChanged(screen);
|
|
|
|
qDebug() << "qqwindow screen has ldpi/pdpi" << screen->logicalDotsPerInch() << screen->physicalDotsPerInch();
|
|
|
|
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
|
2019-09-13 19:41:42 +00:00
|
|
|
int width = 800;
|
2020-02-12 06:15:32 +00:00
|
|
|
int height = 1200;
|
2019-09-13 19:41:42 +00:00
|
|
|
if (qEnvironmentVariableIsSet("SUBSURFACE_MOBILE_WIDTH")) {
|
|
|
|
bool ok;
|
|
|
|
int width_override = qEnvironmentVariableIntValue("SUBSURFACE_MOBILE_WIDTH", &ok);
|
|
|
|
if (ok) {
|
|
|
|
width = width_override;
|
|
|
|
qDebug() << "overriding window width:" << width;
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 06:15:32 +00:00
|
|
|
if (qEnvironmentVariableIsSet("SUBSURFACE_MOBILE_HEIGHT")) {
|
|
|
|
bool ok;
|
|
|
|
int height_override = qEnvironmentVariableIntValue("SUBSURFACE_MOBILE_HEIGHT", &ok);
|
|
|
|
if (ok) {
|
|
|
|
height = height_override;
|
|
|
|
qDebug() << "overriding window height:" << height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qml_window->setHeight(height);
|
2019-09-13 19:41:42 +00:00
|
|
|
qml_window->setWidth(width);
|
2018-07-12 16:22:56 +00:00
|
|
|
#endif // not Q_OS_ANDROID and not Q_OS_IOS
|
2018-07-11 10:17:06 +00:00
|
|
|
qml_window->show();
|
|
|
|
#else
|
|
|
|
MainWindow::instance()->show();
|
2018-07-12 16:22:56 +00:00
|
|
|
#endif // SUBSURFACE_MOBILE
|
2018-07-11 10:17:06 +00:00
|
|
|
qApp->exec();
|
|
|
|
}
|
2018-07-15 15:56:18 +00:00
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(duration_t)
|
|
|
|
static void register_meta_types()
|
|
|
|
{
|
|
|
|
qRegisterMetaType<duration_t>();
|
|
|
|
}
|
2018-07-12 16:22:56 +00:00
|
|
|
#endif // not SUBSURFACE_TEST_DATA
|
|
|
|
|
2018-07-28 17:24:55 +00:00
|
|
|
#define REGISTER_TYPE(useClass, useQML) \
|
|
|
|
rc = qmlRegisterType<useClass>("org.subsurfacedivelog.mobile", 1, 0, useQML); \
|
|
|
|
if (rc < 0) \
|
|
|
|
qWarning() << "ERROR: Cannot register " << useQML << ", QML will not work!!";
|
|
|
|
|
2018-09-04 09:18:43 +00:00
|
|
|
void register_qml_types(QQmlEngine *engine)
|
2018-07-12 16:22:56 +00:00
|
|
|
{
|
2018-08-31 09:57:28 +00:00
|
|
|
// register qPref*
|
2019-04-03 21:28:26 +00:00
|
|
|
qPref::registerQML(engine);
|
2018-07-12 16:22:56 +00:00
|
|
|
|
|
|
|
#ifndef SUBSURFACE_TEST_DATA
|
2018-09-04 09:18:43 +00:00
|
|
|
int rc;
|
|
|
|
|
2018-07-12 16:22:56 +00:00
|
|
|
#ifdef SUBSURFACE_MOBILE
|
2019-12-08 19:26:45 +00:00
|
|
|
if (engine != NULL) {
|
|
|
|
QQmlContext *ct = engine->rootContext();
|
|
|
|
|
2020-01-28 14:04:22 +00:00
|
|
|
// Register qml interface classes
|
2020-01-04 08:47:10 +00:00
|
|
|
QMLInterface::setup(ct);
|
2020-01-28 14:04:22 +00:00
|
|
|
themeInterface::setup(ct);
|
2019-12-08 19:26:45 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 17:24:55 +00:00
|
|
|
REGISTER_TYPE(QMLManager, "QMLManager");
|
|
|
|
REGISTER_TYPE(QMLProfile, "QMLProfile");
|
|
|
|
REGISTER_TYPE(DiveImportedModel, "DCImportModel");
|
2020-02-08 11:06:57 +00:00
|
|
|
REGISTER_TYPE(DiveSummaryModel, "DiveSummaryModel");
|
2018-07-12 16:22:56 +00:00
|
|
|
#endif // not SUBSURFACE_MOBILE
|
|
|
|
|
2018-07-28 17:24:55 +00:00
|
|
|
REGISTER_TYPE(MapWidgetHelper, "MapWidgetHelper");
|
|
|
|
REGISTER_TYPE(MapLocationModel, "MapLocationModel");
|
2018-07-12 16:22:56 +00:00
|
|
|
#endif // not SUBSURFACE_TEST_DATA
|
|
|
|
}
|