source: subsurface/subsurface-mobile-helper.cpp

Last change on this file was 01d091fb, checked in by Dirk Hohndel <dirk@…>, 3 weeks ago

QML UI: better handling of device pixel ratio

In order to make sure we don't render the initial profiles with the
wrong scale on devices, we need to seed the device pixel ratio with the
device default and then update it once the window has been created.

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

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