source: subsurface/subsurface-mobile-helper.cpp

Last change on this file was 0b78b0c, checked in by Dirk Hohndel <dirk@…>, 2 months ago

QML UI: allow downloading from libdc simulator

Signed-off-by: Dirk Hohndel <dirk@…>

  • Property mode set to 100644
File size: 4.7 KB
Line 
1// SPDX-License-Identifier: GPL-2.0
2/* qt-gui.cpp */
3/* Qt UI implementation */
4#include "core/dive.h"
5#include "core/display.h"
6#include "core/helpers.h"
7
8#include <QApplication>
9#include <QDesktopWidget>
10#include <QNetworkProxy>
11#include <QLibraryInfo>
12
13#include "core/qt-gui.h"
14
15#include <QQuickWindow>
16#include <QScreen>
17#include <QQmlApplicationEngine>
18#include <QQmlContext>
19#include <QSortFilterProxyModel>
20#include "mobile-widgets/qmlmanager.h"
21#include "qt-models/divelistmodel.h"
22#include "qt-models/gpslistmodel.h"
23#include "mobile-widgets/qmlprofile.h"
24#include "core/downloadfromdcthread.h"
25#include "qt-models/diveimportedmodel.h"
26#include "qt-models/messagehandlermodel.h"
27
28#include "mobile-widgets/qml/kirigami/src/kirigamiplugin.h"
29
30QObject *qqWindowObject = NULL;
31
32void init_ui()
33{
34        init_qt_late();
35}
36
37void run_ui()
38{
39        qmlRegisterType<QMLManager>("org.subsurfacedivelog.mobile", 1, 0, "QMLManager");
40        qmlRegisterType<QMLProfile>("org.subsurfacedivelog.mobile", 1, 0, "QMLProfile");
41
42        qmlRegisterType<DCDeviceData>("org.subsurfacedivelog.mobile", 1, 0, "DCDeviceData");
43        qmlRegisterType<DownloadThread>("org.subsurfacedivelog.mobile", 1, 0, "DCDownloadThread");
44        qmlRegisterType<DiveImportedModel>("org.subsurfacedivelog.mobile", 1, 0, "DCImportModel");
45
46        QQmlApplicationEngine engine;
47        KirigamiPlugin::getInstance().registerTypes();
48#if __APPLE__
49        // when running the QML UI on a Mac the deployment of the QML Components seems
50        // to fail and the search path for the components is rather odd - simply the
51        // same directory the executable was started from <bundle>/Contents/MacOS/
52        // To work around this we need to manually copy the components at install time
53        // to Contents/Frameworks/qml and make sure that we add the correct import path
54        QStringList importPathList = engine.importPathList();
55        Q_FOREACH(QString importPath, importPathList) {
56                if (importPath.contains("MacOS"))
57                        engine.addImportPath(importPath.replace("MacOS", "Frameworks"));
58        }
59        qDebug() << "QML import path" << engine.importPathList();
60#endif
61        engine.addImportPath("qrc://imports");
62        DiveListModel diveListModel;
63        DiveListSortModel *sortModel = new DiveListSortModel(0);
64        sortModel->setSourceModel(&diveListModel);
65        sortModel->setDynamicSortFilter(true);
66        sortModel->setSortRole(DiveListModel::DiveDateRole);
67        sortModel->sort(0, Qt::DescendingOrder);
68        GpsListModel gpsListModel;
69        QSortFilterProxyModel *gpsSortModel = new QSortFilterProxyModel(0);
70        gpsSortModel->setSourceModel(&gpsListModel);
71        gpsSortModel->setDynamicSortFilter(true);
72        gpsSortModel->setSortRole(GpsListModel::GpsWhenRole);
73        gpsSortModel->sort(0, Qt::DescendingOrder);
74        QQmlContext *ctxt = engine.rootContext();
75        ctxt->setContextProperty("diveModel", sortModel);
76        ctxt->setContextProperty("gpsModel", gpsSortModel);
77        ctxt->setContextProperty("vendorList", vendorList);
78#if defined(Q_OS_ANDROID)
79        connectionListModel.addAddress("FTDI");
80#elif defined(Q_OS_LINUX) // since this is in the else, it does NOT include Android
81        connectionListModel.addAddress("/dev/ttyS0");
82        connectionListModel.addAddress("/dev/ttyS1");
83        connectionListModel.addAddress("/dev/ttyS2");
84        connectionListModel.addAddress("/dev/ttyS3");
85        // this makes debugging so much easier - use the simulator
86        connectionListModel.addAddress("/tmp/ttyS1");
87#endif
88        ctxt->setContextProperty("connectionListModel", &connectionListModel);
89        ctxt->setContextProperty("logModel", MessageHandlerModel::self());
90
91        engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));
92        qqWindowObject = engine.rootObjects().value(0);
93        if (!qqWindowObject) {
94                fprintf(stderr, "can't create window object\n");
95                exit(1);
96        }
97        QQuickWindow *qml_window = qobject_cast<QQuickWindow *>(qqWindowObject);
98        qml_window->setIcon(QIcon(":/subsurface-mobile-icon"));
99        qqWindowObject->setProperty("messageText", QVariant("Subsurface-mobile startup"));
100        qDebug() << "qqwindow devicePixelRatio" << qml_window->devicePixelRatio() << qml_window->screen()->devicePixelRatio();
101        QScreen *screen = qml_window->screen();
102        QObject::connect(qml_window, &QQuickWindow::screenChanged, QMLManager::instance(), &QMLManager::screenChanged);
103        QMLManager *manager = QMLManager::instance();
104        manager->setDevicePixelRatio(qml_window->devicePixelRatio(), qml_window->screen());
105        manager->dlSortModel = sortModel;
106        manager->screenChanged(screen);
107        qDebug() << "qqwindow screen has ldpi/pdpi" << screen->logicalDotsPerInch() << screen->physicalDotsPerInch();
108#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
109        qml_window->setHeight(1200);
110        qml_window->setWidth(800);
111#endif
112        qml_window->show();
113        qApp->exec();
114}
115
116void exit_ui()
117{
118        delete qApp;
119        free((void *)existing_filename);
120}
121
122double get_screen_dpi()
123{
124        QDesktopWidget *mydesk = qApp->desktop();
125        return mydesk->physicalDpiX();
126}
127
128bool haveFilesOnCommandLine()
129{
130        return false;
131}
Note: See TracBrowser for help on using the repository browser.