From de8ea82e95b932557624ae890c98f70a831f9f38 Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Mon, 18 Jun 2018 21:09:49 +0200 Subject: [PATCH] bump version 1.1, add qml service registry class --- .qmake.conf | 4 +- src/imports/imports.pro | 5 ++ src/imports/mvvmcore/mvvmcore.pro | 6 ++- src/imports/mvvmcore/plugins.qmltypes | 38 ++++++++++++++- src/imports/mvvmcore/qqmlserviceregistry.cpp | 46 +++++++++++++++++++ src/imports/mvvmcore/qqmlserviceregistry.h | 41 +++++++++++++++++ src/imports/mvvmcore/qtmvvmcore_plugin.cpp | 14 +++++- src/imports/mvvmdatasynccore/plugins.qmltypes | 2 +- .../qtmvvmdatasynccore_plugin.cpp | 5 +- .../mvvmdatasyncquick/plugins.qmltypes | 2 +- .../qtmvvmdatasyncquick_plugin.cpp | 6 ++- src/imports/mvvmquick/plugins.qmltypes | 2 +- src/imports/mvvmquick/qtmvvmquick_plugin.cpp | 5 +- src/mvvmcore/serviceregistry.h | 2 +- 14 files changed, 164 insertions(+), 14 deletions(-) create mode 100644 src/imports/mvvmcore/qqmlserviceregistry.cpp create mode 100644 src/imports/mvvmcore/qqmlserviceregistry.h diff --git a/.qmake.conf b/.qmake.conf index 2b3a44a..402bde8 100644 --- a/.qmake.conf +++ b/.qmake.conf @@ -6,7 +6,7 @@ win32:cross_compile: CONFIG += winrt DEFINES += QT_DEPRECATED_WARNINGS QT_ASCII_CAST_WARNINGS MODULE_VERSION_MAJOR = 1 -MODULE_VERSION_MINOR = 0 -MODULE_VERSION_PATCH = 2 +MODULE_VERSION_MINOR = 1 +MODULE_VERSION_PATCH = 0 MODULE_VERSION_IMPORT = $${MODULE_VERSION_MAJOR}.$${MODULE_VERSION_MINOR} MODULE_VERSION = $${MODULE_VERSION_MAJOR}.$${MODULE_VERSION_MINOR}.$${MODULE_VERSION_PATCH} diff --git a/src/imports/imports.pro b/src/imports/imports.pro index 844f148..2ff6a84 100644 --- a/src/imports/imports.pro +++ b/src/imports/imports.pro @@ -4,7 +4,12 @@ SUBDIRS += \ mvvmcore \ mvvmquick +mvvmquick.depends += mvvmcore + qtHaveModule(datasync) { SUBDIRS += mvvmdatasynccore \ mvvmdatasyncquick + + mvvmdatasynccore.depends += mvvmcore + mvvmdatasyncquick.depends += mvvmdatasynccore mvvmquick } diff --git a/src/imports/mvvmcore/mvvmcore.pro b/src/imports/mvvmcore/mvvmcore.pro index 30cfc77..ce2109a 100644 --- a/src/imports/mvvmcore/mvvmcore.pro +++ b/src/imports/mvvmcore/mvvmcore.pro @@ -9,12 +9,14 @@ DEFINES += "VERSION_MINOR=$$MODULE_VERSION_MINOR" HEADERS += \ qtmvvmcore_plugin.h \ qqmlmvvmbinding.h \ - qqmlmvvmmessage.h + qqmlmvvmmessage.h \ + qqmlserviceregistry.h SOURCES += \ qtmvvmcore_plugin.cpp \ qqmlmvvmbinding.cpp \ - qqmlmvvmmessage.cpp + qqmlmvvmmessage.cpp \ + qqmlserviceregistry.cpp OTHER_FILES += qmldir diff --git a/src/imports/mvvmcore/plugins.qmltypes b/src/imports/mvvmcore/plugins.qmltypes index 5acf010..5090605 100644 --- a/src/imports/mvvmcore/plugins.qmltypes +++ b/src/imports/mvvmcore/plugins.qmltypes @@ -4,7 +4,7 @@ import QtQuick.tooling 1.2 // It is used for QML tooling purposes only. // // This file was auto-generated by: -// 'qmlplugindump -nonrelocatable de.skycoder42.QtMvvm.Core 1.0' +// 'qmlplugindump -nonrelocatable de.skycoder42.QtMvvm.Core 1.1' Module { dependencies: ["QtQml 2.2"] @@ -429,6 +429,42 @@ Module { Parameter { name: "onResult"; type: "QJSValue" } } } + Component { + name: "QtMvvm::QQmlServiceRegistry" + prototype: "QObject" + exports: ["de.skycoder42.QtMvvm.Core/ServiceRegistry 1.1"] + isCreatable: false + isSingleton: true + exportMetaObjectRevisions: [0] + Enum { + name: "DestructionScope" + values: { + "DestroyOnAppQuit": 1, + "DestroyOnAppDestroy": 2, + "DestroyOnRegistryDestroy": 3, + "DestroyNever": 127 + } + } + Method { + name: "isRegistered" + type: "bool" + Parameter { name: "iid"; type: "string" } + } + Method { + name: "registerObject" + Parameter { name: "componentUrl"; type: "QUrl" } + Parameter { name: "weak"; type: "bool" } + } + Method { + name: "registerObject" + Parameter { name: "componentUrl"; type: "QUrl" } + } + Method { + name: "service" + type: "QObject*" + Parameter { name: "iid"; type: "string" } + } + } Component { name: "QtMvvm::SettingsViewModel" prototype: "QtMvvm::ViewModel" diff --git a/src/imports/mvvmcore/qqmlserviceregistry.cpp b/src/imports/mvvmcore/qqmlserviceregistry.cpp new file mode 100644 index 0000000..43ff206 --- /dev/null +++ b/src/imports/mvvmcore/qqmlserviceregistry.cpp @@ -0,0 +1,46 @@ +#include "qqmlserviceregistry.h" +#include +using namespace QtMvvm; + +QQmlServiceRegistry::QQmlServiceRegistry(QQmlEngine *parent) : + QObject{parent}, + _engine{parent} +{} + +bool QQmlServiceRegistry::isRegistered(const QString &iid) const +{ + return ServiceRegistry::instance()->isRegistered(iid.toUtf8()); +} + +void QQmlServiceRegistry::registerObject(const QUrl &componentUrl, bool weak) +{ + ServiceRegistry::instance()->registerService(componentUrl.toString().toUtf8(), [this, componentUrl](const QObjectList &) -> QObject* { + QQmlComponent component{_engine, componentUrl, QQmlComponent::PreferSynchronous}; + switch(component.status()) { + case QQmlComponent::Ready: + { + auto object = component.create(); + QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership); + object->setParent(nullptr); + return object; + } + case QQmlComponent::Null: + throw ServiceConstructionException{"No component was loaded for URL: " + + component.url().toString().toUtf8()}; + case QQmlComponent::Error: + throw ServiceConstructionException{"Failed to load componentfor URL \"" + component.url().toString().toUtf8() + + "\" with error: " + component.errorString().trimmed().toUtf8()}; + case QQmlComponent::Loading: + throw ServiceConstructionException{"Unable to construct service from asynchronously loaded URL: " + + component.url().toString().toUtf8()}; + default: + Q_UNREACHABLE(); + return nullptr; + } + }, {}, weak, ServiceRegistry::DestroyOnAppQuit); +} + +QObject *QQmlServiceRegistry::service(const QString &iid) +{ + return ServiceRegistry::instance()->serviceObj(iid.toUtf8()); +} diff --git a/src/imports/mvvmcore/qqmlserviceregistry.h b/src/imports/mvvmcore/qqmlserviceregistry.h new file mode 100644 index 0000000..0c5ff0a --- /dev/null +++ b/src/imports/mvvmcore/qqmlserviceregistry.h @@ -0,0 +1,41 @@ +#ifndef QTMVVM_QQMLSERVICEREGISTRY_H +#define QTMVVM_QQMLSERVICEREGISTRY_H + +#include + +#include + +#include +#include + +namespace QtMvvm { + +class QQmlServiceRegistry : public QObject +{ + Q_OBJECT + +public: + enum DestructionScope { + DestroyOnAppQuit = ServiceRegistry::DestroyOnAppQuit, + DestroyOnAppDestroy = ServiceRegistry::DestroyOnAppDestroy, + DestroyOnRegistryDestroy = ServiceRegistry::DestroyOnRegistryDestroy, + + DestroyNever = ServiceRegistry::DestroyNever + }; + Q_ENUM(DestructionScope) + + explicit QQmlServiceRegistry(QQmlEngine *parent = nullptr); + + Q_INVOKABLE bool isRegistered(const QString &iid) const; + + Q_INVOKABLE void registerObject(const QUrl &componentUrl, bool weak = false); + + Q_INVOKABLE QObject *service(const QString &iid); + +private: + QQmlEngine *_engine; +}; + +} + +#endif // QTMVVM_QQMLSERVICEREGISTRY_H diff --git a/src/imports/mvvmcore/qtmvvmcore_plugin.cpp b/src/imports/mvvmcore/qtmvvmcore_plugin.cpp index 6d9adf0..6043f01 100644 --- a/src/imports/mvvmcore/qtmvvmcore_plugin.cpp +++ b/src/imports/mvvmcore/qtmvvmcore_plugin.cpp @@ -8,10 +8,17 @@ #include "qqmlmvvmbinding.h" #include "qqmlmvvmmessage.h" +#include "qqmlserviceregistry.h" static QObject *createMessageSingleton(QQmlEngine *qmlEngine, QJSEngine *jsEngine) { - return new QtMvvm::QQmlMvvmMessage(jsEngine, qmlEngine); + return new QtMvvm::QQmlMvvmMessage{jsEngine, qmlEngine}; +} + +static QObject *createRegistrySingleton(QQmlEngine *qmlEngine, QJSEngine *jsEngine) +{ + Q_UNUSED(jsEngine) + return new QtMvvm::QQmlServiceRegistry{qmlEngine}; } QtMvvmCoreDeclarativeModule::QtMvvmCoreDeclarativeModule(QObject *parent) : @@ -32,6 +39,9 @@ void QtMvvmCoreDeclarativeModule::registerTypes(const char *uri) qmlRegisterSingletonType(uri, 1, 0, "Message", createMessageSingleton); + //Version 1.1 + qmlRegisterSingletonType(uri, 1, 1, "ServiceRegistry", createRegistrySingleton); + // Check to make shure no module update is forgotten - static_assert(VERSION_MAJOR == 1 && VERSION_MINOR == 0, "QML module version needs to be updated"); + static_assert(VERSION_MAJOR == 1 && VERSION_MINOR == 1, "QML module version needs to be updated"); } diff --git a/src/imports/mvvmdatasynccore/plugins.qmltypes b/src/imports/mvvmdatasynccore/plugins.qmltypes index cdfe0cc..568cb9e 100644 --- a/src/imports/mvvmdatasynccore/plugins.qmltypes +++ b/src/imports/mvvmdatasynccore/plugins.qmltypes @@ -4,7 +4,7 @@ import QtQuick.tooling 1.2 // It is used for QML tooling purposes only. // // This file was auto-generated by: -// 'qmlplugindump -nonrelocatable de.skycoder42.QtMvvm.DataSync.Core 1.0' +// 'qmlplugindump -nonrelocatable de.skycoder42.QtMvvm.DataSync.Core 1.1' Module { dependencies: [ diff --git a/src/imports/mvvmdatasynccore/qtmvvmdatasynccore_plugin.cpp b/src/imports/mvvmdatasynccore/qtmvvmdatasynccore_plugin.cpp index d1d248e..b317382 100644 --- a/src/imports/mvvmdatasynccore/qtmvvmdatasynccore_plugin.cpp +++ b/src/imports/mvvmdatasynccore/qtmvvmdatasynccore_plugin.cpp @@ -31,6 +31,9 @@ void QtMvvmDataSyncCoreDeclarativeModule::registerTypes(const char *uri) qmlRegisterUncreatableType(uri, 1, 0, "PChangeRemoteViewModel", QStringLiteral("ViewModels cannot be created from QML")); qmlRegisterUncreatableType(uri, 1, 0, "PExportSetupViewModel", QStringLiteral("ViewModels cannot be created from QML")); + //Version 1.1 + qmlRegisterModule(uri, 1, 1); + // Check to make shure no module update is forgotten - static_assert(VERSION_MAJOR == 1 && VERSION_MINOR == 0, "QML module version needs to be updated"); + static_assert(VERSION_MAJOR == 1 && VERSION_MINOR == 1, "QML module version needs to be updated"); } diff --git a/src/imports/mvvmdatasyncquick/plugins.qmltypes b/src/imports/mvvmdatasyncquick/plugins.qmltypes index 995bc13..a3271bc 100644 --- a/src/imports/mvvmdatasyncquick/plugins.qmltypes +++ b/src/imports/mvvmdatasyncquick/plugins.qmltypes @@ -4,7 +4,7 @@ import QtQuick.tooling 1.2 // It is used for QML tooling purposes only. // // This file was auto-generated by: -// 'qmlplugindump -nonrelocatable de.skycoder42.QtMvvm.DataSync.Quick 1.0' +// 'qmlplugindump -nonrelocatable de.skycoder42.QtMvvm.DataSync.Quick 1.1' Module { dependencies: [ diff --git a/src/imports/mvvmdatasyncquick/qtmvvmdatasyncquick_plugin.cpp b/src/imports/mvvmdatasyncquick/qtmvvmdatasyncquick_plugin.cpp index abf9ec6..f1fc7c0 100644 --- a/src/imports/mvvmdatasyncquick/qtmvvmdatasyncquick_plugin.cpp +++ b/src/imports/mvvmdatasyncquick/qtmvvmdatasyncquick_plugin.cpp @@ -21,7 +21,11 @@ void QtMvvmDataSyncQuickDeclarativeModule::registerTypes(const char *uri) //Version 1.0 //no c++ types. plugin is only needed for the resources + qmlRegisterModule(uri, 1, 0); + + //Version 1.1 + qmlRegisterModule(uri, 1, 1); // Check to make shure no module update is forgotten - static_assert(VERSION_MAJOR == 1 && VERSION_MINOR == 0, "QML module version needs to be updated"); + static_assert(VERSION_MAJOR == 1 && VERSION_MINOR == 1, "QML module version needs to be updated"); } diff --git a/src/imports/mvvmquick/plugins.qmltypes b/src/imports/mvvmquick/plugins.qmltypes index bd8e8e9..e79a96d 100644 --- a/src/imports/mvvmquick/plugins.qmltypes +++ b/src/imports/mvvmquick/plugins.qmltypes @@ -4,7 +4,7 @@ import QtQuick.tooling 1.2 // It is used for QML tooling purposes only. // // This file was auto-generated by: -// 'qmlplugindump -nonrelocatable de.skycoder42.QtMvvm.Quick 1.0' +// 'qmlplugindump -nonrelocatable de.skycoder42.QtMvvm.Quick 1.1' Module { dependencies: [ diff --git a/src/imports/mvvmquick/qtmvvmquick_plugin.cpp b/src/imports/mvvmquick/qtmvvmquick_plugin.cpp index a058aec..e77be57 100644 --- a/src/imports/mvvmquick/qtmvvmquick_plugin.cpp +++ b/src/imports/mvvmquick/qtmvvmquick_plugin.cpp @@ -48,6 +48,9 @@ void QtMvvmQuickDeclarativeModule::registerTypes(const char *uri) qmlRegisterType(QUrl(QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/qml/FolderDialog.qml")), uri, 1, 0, "FolderDialog"); #endif + //Version 1.1 + qmlRegisterModule(uri, 1, 1); + // Check to make shure no module update is forgotten - static_assert(VERSION_MAJOR == 1 && VERSION_MINOR == 0, "QML module version needs to be updated"); + static_assert(VERSION_MAJOR == 1 && VERSION_MINOR == 1, "QML module version needs to be updated"); } diff --git a/src/mvvmcore/serviceregistry.h b/src/mvvmcore/serviceregistry.h index b17bb35..f413e2e 100644 --- a/src/mvvmcore/serviceregistry.h +++ b/src/mvvmcore/serviceregistry.h @@ -14,7 +14,7 @@ namespace QtMvvm { class ServiceRegistryPrivate; //! A singleton to prepare services for dependency injection and to access them -class Q_MVVMCORE_EXPORT ServiceRegistry +class Q_MVVMCORE_EXPORT ServiceRegistry //MAJOR make a QObject for invokable methods and Q_ENUM { public: enum DestructionScope {