source: subsurface/subsurface-mobile-helper.cpp

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

Add a messageHandler to take care of qDebug & friends on QML

all qDebug / qCDebug and friends now will be properly
logged into developer -> log, on QML.

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

  • Property mode set to 100644
File size: 4.2 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        ctxt->setContextProperty("logModel", MessageHandlerModel::self());
79
80        engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));
81        qqWindowObject = engine.rootObjects().value(0);
82        if (!qqWindowObject) {
83                fprintf(stderr, "can't create window object\n");
84                exit(1);
85        }
86        QQuickWindow *qml_window = qobject_cast<QQuickWindow *>(qqWindowObject);
87        qml_window->setIcon(QIcon(":/subsurface-mobile-icon"));
88        qqWindowObject->setProperty("messageText", QVariant("Subsurface-mobile startup"));
89        qDebug() << "qqwindow devicePixelRatio" << qml_window->devicePixelRatio() << qml_window->screen()->devicePixelRatio();
90        QScreen *screen = qml_window->screen();
91        QObject::connect(qml_window, &QQuickWindow::screenChanged, QMLManager::instance(), &QMLManager::screenChanged);
92        QMLManager *manager = QMLManager::instance();
93        manager->setDevicePixelRatio(qml_window->devicePixelRatio(), qml_window->screen());
94        manager->dlSortModel = sortModel;
95        manager->screenChanged(screen);
96        qDebug() << "qqwindow screen has ldpi/pdpi" << screen->logicalDotsPerInch() << screen->physicalDotsPerInch();
97#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
98        qml_window->setHeight(1200);
99        qml_window->setWidth(800);
100#endif
101        qml_window->show();
102        qApp->exec();
103}
104
105void exit_ui()
106{
107        delete qApp;
108        free((void *)existing_filename);
109}
110
111double get_screen_dpi()
112{
113        QDesktopWidget *mydesk = qApp->desktop();
114        return mydesk->physicalDpiX();
115}
116
117bool haveFilesOnCommandLine()
118{
119        return false;
120}
Note: See TracBrowser for help on using the repository browser.