From fdc2fd35bf3a24f64b3f86a1caa4d69257c9dfab Mon Sep 17 00:00:00 2001 From: jan Iversen Date: Sat, 4 Jan 2020 09:26:09 +0100 Subject: [PATCH] mobile-widgets: add pure interface for passing values to QML Add a header file that contains a duplicate of the enums, that are needed in QML in one class. the unit enums are added imidiatly, since they are needed or will be neede shortly in Settings and DivePlannerSettings This class will also contain Q_PROPERTY and signal/slot for variables used in QML. This is done to allow e.g. deco_mode qPrefUnits::planner_deco_mode() void qPrefUnits::set_planner_deco_mode(deco_mode) as strongly typed in C++ and DECO_MODE planner_deco_mode() void set_planner_deco_mode(DECO_MODE) as strongly typed in QML Remark: wrong assignments gives errors in QML The advantage over using strings or the value directly is that QML detects typos and flags them as errors/warnings. It is important to note that the class may only contain a) a function call to the implementation b) a reference to a global variable e.g. prefs. Added note to the original definitions of the enums that they have been duplicated. Signed-off-by: jan Iversen Signed-off-by: Dirk Hohndel --- core/pref.h | 1 + core/units.h | 1 + mobile-widgets/CMakeLists.txt | 1 + mobile-widgets/qmlinterface.cpp | 21 +++++++ mobile-widgets/qmlinterface.h | 95 +++++++++++++++++++++++++++++ packaging/ios/Subsurface-mobile.pro | 2 + 6 files changed, 121 insertions(+) create mode 100644 mobile-widgets/qmlinterface.cpp create mode 100644 mobile-widgets/qmlinterface.h diff --git a/core/pref.h b/core/pref.h index 6949b762c..cfad1abfe 100644 --- a/core/pref.h +++ b/core/pref.h @@ -59,6 +59,7 @@ typedef struct { const char *device_name; } dive_computer_prefs_t; +// NOTE: these enums are duplicated in mobile-widgets/qmlinterface.h enum unit_system_values { METRIC, IMPERIAL, diff --git a/core/units.h b/core/units.h index ff34991f7..8839dde1d 100644 --- a/core/units.h +++ b/core/units.h @@ -275,6 +275,7 @@ static inline int32_t pressure_to_altitude(int32_t pressure) // pressure in mbar * keeps track of those units. */ /* turns out in Win32 PASCAL is defined as a calling convention */ +/* NOTE: these enums are duplicated in mobile-widgets/qmlinterface.h */ struct units { enum LENGTH { METERS, diff --git a/mobile-widgets/CMakeLists.txt b/mobile-widgets/CMakeLists.txt index 1eecb7d3b..6c1747dbc 100644 --- a/mobile-widgets/CMakeLists.txt +++ b/mobile-widgets/CMakeLists.txt @@ -1,6 +1,7 @@ # mobile backend functions set(SUBSURFACE_MOBILE_SRCS + qmlinterface.cpp qmlmanager.cpp qml/kirigami/src/columnview.cpp qml/kirigami/src/delegaterecycler.cpp diff --git a/mobile-widgets/qmlinterface.cpp b/mobile-widgets/qmlinterface.cpp new file mode 100644 index 000000000..78aba6a5c --- /dev/null +++ b/mobile-widgets/qmlinterface.cpp @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "qmlinterface.h" + +#include + +QMLInterface *QMLInterface::instance() +{ + static QMLInterface *self = new QMLInterface; + return self; +} + +void QMLInterface::setup(QQmlContext *ct) +{ + // Register interface class + ct->setContextProperty("Backend", QMLInterface::instance()); + + // Make enums available as types + qmlRegisterUncreatableType("org.subsurfacedivelog.mobile",1,0,"Enums","Enum is not a type"); + + // relink signals to QML +} diff --git a/mobile-widgets/qmlinterface.h b/mobile-widgets/qmlinterface.h new file mode 100644 index 000000000..59981e0d1 --- /dev/null +++ b/mobile-widgets/qmlinterface.h @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef QMLINTERFACE_H +#define QMLINTERFACE_H +#include +#include +// This class is a pure interface class and may not contain any implementation code +// Allowed are: +// header +// Q_PROPERTY +// signal/slot for Q_PROPERTY functions +// the functions may contain either +// a) a function call to the implementation +// b) a reference to a global variable like e.g. prefs. +// Q_INVOCABLE functions +// the functions may contain +// a) a function call to the implementation +// source +// connect signal/signal to pass signals from implementation + + +class QMLInterface : public QObject { + Q_OBJECT + + // Q_PROPERTY used in QML + +public: + static QMLInterface *instance(); + + // function to do the needed setup and do connect of signal/signal + static void setup(QQmlContext *ct); + + // Duplicated enums, these enums are properly defined in the C/C++ structure + // but duplicated here to make them available to QML. + + // Duplicating the enums poses a slight risk for forgetting to update + // them if the proper enum is changed (e.g. assigning a new start value). + + // remark please do not use these enums outside the C++/QML interface. + enum UNIT_SYSTEM { + METRIC, + IMPERIAL, + PERSONALIZE + }; + Q_ENUM(UNIT_SYSTEM); + + enum LENGTH { + METERS, + FEET + }; + Q_ENUM(LENGTH); + + enum VOLUME { + LITER, + CUFT + }; + Q_ENUM(VOLUME); + + enum PRESSURE { + BAR, + PSI, + PASCALS + }; + Q_ENUM(PRESSURE); + + enum TEMPERATURE { + CELSIUS, + FAHRENHEIT, + KELVIN + }; + Q_ENUM(TEMPERATURE); + + enum WEIGHT { + KG, + LBS + }; + Q_ENUM(WEIGHT); + + enum TIME { + SECONDS, + MINUTES + }; + Q_ENUM(TIME); + + enum DURATION { + MIXED, + MINUTES_ONLY, + ALWAYS_HOURS + }; + Q_ENUM(DURATION); + +private: + QMLInterface() {} +}; +#endif // QMLINTERFACE_H + diff --git a/packaging/ios/Subsurface-mobile.pro b/packaging/ios/Subsurface-mobile.pro index 4b8aa079c..afe2e10c5 100644 --- a/packaging/ios/Subsurface-mobile.pro +++ b/packaging/ios/Subsurface-mobile.pro @@ -110,6 +110,7 @@ SOURCES += ../../subsurface-mobile-main.cpp \ ../../core/subsurface-qt/DiveListNotifier.cpp \ ../../backend-shared/exportfuncs.cpp \ ../../backend-shared/plannershared.cpp \ + ../../mobile-widgets/qmlinterface.cpp \ ../../mobile-widgets/qmlmanager.cpp \ ../../qt-models/divelistmodel.cpp \ ../../qt-models/diveplotdatamodel.cpp \ @@ -242,6 +243,7 @@ HEADERS += \ ../../core/subsurface-qt/DiveListNotifier.h \ ../../backend-shared/exportfuncs.h \ ../../backend-shared/plannershared.h \ + ../../mobile-widgets/qmlinterface.h \ ../../mobile-widgets/qmlmanager.h \ ../../map-widget/qmlmapwidgethelper.h \ ../../qt-models/divelistmodel.h \