14 changed files with 164 additions and 14 deletions
@ -0,0 +1,46 @@ |
|||||
|
#include "qqmlserviceregistry.h" |
||||
|
#include <QtQml/QQmlInfo> |
||||
|
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()); |
||||
|
} |
@ -0,0 +1,41 @@ |
|||||
|
#ifndef QTMVVM_QQMLSERVICEREGISTRY_H |
||||
|
#define QTMVVM_QQMLSERVICEREGISTRY_H |
||||
|
|
||||
|
#include <QtCore/QObject> |
||||
|
|
||||
|
#include <QtMvvmCore/ServiceRegistry> |
||||
|
|
||||
|
#include <QtQml/QQmlEngine> |
||||
|
#include <QtQml/QQmlComponent> |
||||
|
|
||||
|
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
|
Loading…
Reference in new issue