From 8e7609951a38c153907ba86f2fee997a5dbafc9c Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Tue, 20 Feb 2018 16:29:07 +0100 Subject: [PATCH] added alias support --- src/mvvmquick/Switch.qml | 7 +++++++ src/mvvmquick/inputviewfactory.cpp | 25 ++++++++++++------------- src/mvvmquick/inputviewfactory.h | 23 ++++++++++++++++------- src/mvvmquick/inputviewfactory_p.h | 1 + src/mvvmquick/qtmvvmquick_module.qrc | 1 + src/mvvmwidgets/inputwidgetfactory.cpp | 10 +++++++++- src/mvvmwidgets/inputwidgetfactory.h | 14 ++++++++++++-- src/mvvmwidgets/inputwidgetfactory_p.h | 1 + 8 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 src/mvvmquick/Switch.qml diff --git a/src/mvvmquick/Switch.qml b/src/mvvmquick/Switch.qml new file mode 100644 index 0000000..d192598 --- /dev/null +++ b/src/mvvmquick/Switch.qml @@ -0,0 +1,7 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 + +Switch { + id: _edit + property alias inputValue: _edit.checked +} diff --git a/src/mvvmquick/inputviewfactory.cpp b/src/mvvmquick/inputviewfactory.cpp index 2cb74ad..2f26de9 100644 --- a/src/mvvmquick/inputviewfactory.cpp +++ b/src/mvvmquick/inputviewfactory.cpp @@ -38,23 +38,17 @@ InputViewFactory::InputViewFactory() : InputViewFactory::~InputViewFactory() {} -//int InputViewFactory::metaTypeId(const QByteArray &type, const QVariantMap &properties) -//{ -// if(type == "string") -// return QMetaType::QString; -// else if(type == "list") -// return metaTypeId(properties.value(QStringLiteral("_list_data"), QByteArray("string")).toByteArray(), properties); -// else -// return QMetaType::type(type); -//} - QUrl InputViewFactory::getInputUrl(const QByteArray &type, const QVariantMap &viewProperties) { Q_UNUSED(viewProperties) + if(d->aliases.contains(type)) + return getInputUrl(d->aliases.value(type), viewProperties); if(d->simpleViews.contains(type)) return d->simpleViews.value(type); else if(type == QMetaType::typeName(QMetaType::Bool)) return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/CheckBox.qml"); + else if(type == "switch") + return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/Switch.qml"); else if(type == QMetaType::typeName(QMetaType::QString) || type == "string") return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/TextField.qml"); else if(type == QMetaType::typeName(QMetaType::Int)) @@ -79,15 +73,20 @@ QUrl InputViewFactory::getInputUrl(const QByteArray &type, const QVariantMap &vi } } -bool InputViewFactory::addSimpleView(const QByteArray &type, const QUrl &qmlFileUrl) +void InputViewFactory::addSimpleView(const QByteArray &type, const QUrl &qmlFileUrl) { d->simpleViews.insert(type, qmlFileUrl); - return true; +} + +void InputViewFactory::addAlias(const QByteArray &alias, const QByteArray &targetType) +{ + d->aliases.insert(alias, targetType); } // ------------- Private Implementation ------------- InputViewFactoryPrivate::InputViewFactoryPrivate() : - simpleViews() + simpleViews(), + aliases() {} diff --git a/src/mvvmquick/inputviewfactory.h b/src/mvvmquick/inputviewfactory.h index 7e5dfae..69fe369 100644 --- a/src/mvvmquick/inputviewfactory.h +++ b/src/mvvmquick/inputviewfactory.h @@ -19,21 +19,30 @@ public: InputViewFactory(); virtual ~InputViewFactory(); - //virtual int metaTypeId(const QByteArray &type, const QVariantMap &properties); Q_INVOKABLE virtual QUrl getInputUrl(const QByteArray &type, const QVariantMap &viewProperties); - Q_INVOKABLE virtual bool addSimpleView(const QByteArray &type, const QUrl &qmlFileUrl); - template - bool addSimpleView(const QUrl &qmlFileUrl); + Q_INVOKABLE virtual void addSimpleView(const QByteArray &type, const QUrl &qmlFileUrl); + template + inline void addSimpleView(const QUrl &qmlFileUrl); + + Q_INVOKABLE virtual void addAlias(const QByteArray &alias, const QByteArray &targetType); + template + inline void addAlias(); private: QScopedPointer d; }; -template -bool InputViewFactory::addSimpleView(const QUrl &qmlFileUrl) +template +inline void InputViewFactory::addSimpleView(const QUrl &qmlFileUrl) +{ + addSimpleView(QMetaType::typeName(qMetaTypeId()), qmlFileUrl); +} + +template +inline void InputViewFactory::addAlias() { - return addSimpleView(QMetaType::typeName(qMetaTypeId()), qmlFileUrl); + addAlias(QMetaType::typeName(qMetaTypeId()), QMetaType::typeName(qMetaTypeId())); } } diff --git a/src/mvvmquick/inputviewfactory_p.h b/src/mvvmquick/inputviewfactory_p.h index 23f1b26..7197085 100644 --- a/src/mvvmquick/inputviewfactory_p.h +++ b/src/mvvmquick/inputviewfactory_p.h @@ -12,6 +12,7 @@ public: InputViewFactoryPrivate(); QHash simpleViews; + QHash aliases; }; } diff --git a/src/mvvmquick/qtmvvmquick_module.qrc b/src/mvvmquick/qtmvvmquick_module.qrc index 21a3618..8b3f66b 100644 --- a/src/mvvmquick/qtmvvmquick_module.qrc +++ b/src/mvvmquick/qtmvvmquick_module.qrc @@ -7,5 +7,6 @@ DoubleSpinBox.qml FontEdit.qml UrlField.qml + Switch.qml diff --git a/src/mvvmwidgets/inputwidgetfactory.cpp b/src/mvvmwidgets/inputwidgetfactory.cpp index 02e9e9a..8ef4a6d 100644 --- a/src/mvvmwidgets/inputwidgetfactory.cpp +++ b/src/mvvmwidgets/inputwidgetfactory.cpp @@ -24,6 +24,8 @@ InputWidgetFactory::~InputWidgetFactory() {} QWidget *InputWidgetFactory::createInput(const QByteArray &type, QWidget *parent, const QVariantMap &viewProperties) { QWidget *widget = nullptr; + if(d->aliases.contains(type)) + return createInput(d->aliases.value(type), parent, viewProperties); if(d->simpleWidgets.contains(type)) widget = d->simpleWidgets.value(type)(parent); else if(type == QMetaType::typeName(QMetaType::Bool)) @@ -78,8 +80,14 @@ void InputWidgetFactory::addSimpleWidget(const QByteArray &type, const std::func d->simpleWidgets.insert(type, creator); } +void InputWidgetFactory::addAlias(const QByteArray &alias, const QByteArray &targetType) +{ + d->aliases.insert(alias, targetType); +} + // ------------- Private Implementation ------------- InputWidgetFactoryPrivate::InputWidgetFactoryPrivate() : - simpleWidgets() + simpleWidgets(), + aliases() {} diff --git a/src/mvvmwidgets/inputwidgetfactory.h b/src/mvvmwidgets/inputwidgetfactory.h index d6486de..5ac5aed 100644 --- a/src/mvvmwidgets/inputwidgetfactory.h +++ b/src/mvvmwidgets/inputwidgetfactory.h @@ -22,20 +22,30 @@ public: virtual void addSimpleWidget(const QByteArray &type, const std::function &creator); template - void addSimpleWidget(); + inline void addSimpleWidget(); + + virtual void addAlias(const QByteArray &alias, const QByteArray &targetType); + template + inline void addAlias(); private: QScopedPointer d; }; template -void InputWidgetFactory::addSimpleWidget() +inline void InputWidgetFactory::addSimpleWidget() { addSimpleWidget(QMetaType::typeName(qMetaTypeId()), [](QWidget *parent){ return new TWidget(parent); }); } +template +inline void InputWidgetFactory::addAlias() +{ + addAlias(QMetaType::typeName(qMetaTypeId()), QMetaType::typeName(qMetaTypeId())); +} + } #endif // QTMVVM_INPUTWIDGETFACTORY_H diff --git a/src/mvvmwidgets/inputwidgetfactory_p.h b/src/mvvmwidgets/inputwidgetfactory_p.h index e015f82..1bf18fa 100644 --- a/src/mvvmwidgets/inputwidgetfactory_p.h +++ b/src/mvvmwidgets/inputwidgetfactory_p.h @@ -12,6 +12,7 @@ public: InputWidgetFactoryPrivate(); QHash> simpleWidgets; + QHash aliases; }; }