Browse Source

added more qml service creators

pull/2/head
Skycoder42 7 years ago
parent
commit
5f68d32db3
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 26
      src/imports/mvvmcore/plugins.qmltypes
  2. 20
      src/imports/mvvmcore/qqmlserviceregistry.cpp
  3. 2
      src/imports/mvvmcore/qqmlserviceregistry.h

26
src/imports/mvvmcore/plugins.qmltypes

@ -450,6 +450,32 @@ Module {
type: "bool" type: "bool"
Parameter { name: "iid"; type: "string" } Parameter { name: "iid"; type: "string" }
} }
Method {
name: "registerObject"
Parameter { name: "object"; type: "QObject"; isPointer: true }
Parameter { name: "scope"; type: "QtMvvm::QQmlServiceRegistry::DestructionScope" }
Parameter { name: "weak"; type: "bool" }
}
Method {
name: "registerObject"
Parameter { name: "object"; type: "QObject"; isPointer: true }
Parameter { name: "scope"; type: "QtMvvm::QQmlServiceRegistry::DestructionScope" }
}
Method {
name: "registerObject"
Parameter { name: "object"; type: "QObject"; isPointer: true }
}
Method {
name: "registerObject"
Parameter { name: "iid"; type: "string" }
Parameter { name: "function"; type: "QJSValue" }
Parameter { name: "weak"; type: "bool" }
}
Method {
name: "registerObject"
Parameter { name: "iid"; type: "string" }
Parameter { name: "function"; type: "QJSValue" }
}
Method { Method {
name: "registerObject" name: "registerObject"
Parameter { name: "componentUrl"; type: "QUrl" } Parameter { name: "componentUrl"; type: "QUrl" }

20
src/imports/mvvmcore/qqmlserviceregistry.cpp

@ -12,6 +12,26 @@ bool QQmlServiceRegistry::isRegistered(const QString &iid) const
return ServiceRegistry::instance()->isRegistered(iid.toUtf8()); return ServiceRegistry::instance()->isRegistered(iid.toUtf8());
} }
void QQmlServiceRegistry::registerObject(QObject *object, DestructionScope scope, bool weak)
{
ServiceRegistry::instance()->registerService(__helpertypes::InjectablePrefix + object->metaObject()->className(),
[object](const QObjectList &) {
return object;
}, {}, static_cast<ServiceRegistry::DestructionScope>(scope), weak);
}
void QQmlServiceRegistry::registerObject(const QString &iid, const QJSValue &function, bool weak)
{
if(!function.isCallable()) {
qmlWarning(this) << "function must be callable";
return;
}
ServiceRegistry::instance()->registerService(iid.toUtf8(), [function](const QObjectList &) {
auto fn = function;
return fn.call().toQObject();
}, {}, ServiceRegistry::DestroyOnAppQuit, weak);
}
void QQmlServiceRegistry::registerObject(const QUrl &componentUrl, bool weak) void QQmlServiceRegistry::registerObject(const QUrl &componentUrl, bool weak)
{ {
ServiceRegistry::instance()->registerService(componentUrl.toString().toUtf8(), [this, componentUrl](const QObjectList &) -> QObject* { ServiceRegistry::instance()->registerService(componentUrl.toString().toUtf8(), [this, componentUrl](const QObjectList &) -> QObject* {

2
src/imports/mvvmcore/qqmlserviceregistry.h

@ -28,6 +28,8 @@ public:
Q_INVOKABLE bool isRegistered(const QString &iid) const; Q_INVOKABLE bool isRegistered(const QString &iid) const;
Q_INVOKABLE void registerObject(QObject *object, QtMvvm::QQmlServiceRegistry::DestructionScope scope = DestroyOnAppQuit, bool weak = false);
Q_INVOKABLE void registerObject(const QString &iid, const QJSValue &function, bool weak = false);
Q_INVOKABLE void registerObject(const QUrl &componentUrl, bool weak = false); Q_INVOKABLE void registerObject(const QUrl &componentUrl, bool weak = false);
Q_INVOKABLE QObject *service(const QString &iid); Q_INVOKABLE QObject *service(const QString &iid);

Loading…
Cancel
Save