From cf9abd2bc6dbf7ed57e9b03e5162fb65f81644f7 Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Tue, 20 Feb 2018 00:59:04 +0100 Subject: [PATCH] Added input view factory with basic views --- src/imports/mvvmquick/plugins.qmltypes | 24 ++++++ src/imports/mvvmquick/qqmlquickpresenter.cpp | 7 ++ src/imports/mvvmquick/qqmlquickpresenter.h | 7 ++ src/imports/mvvmquick/qtmvvmquick_plugin.cpp | 3 + src/mvvmquick/CheckBox.qml | 7 ++ src/mvvmquick/DoubleSpinBox.qml | 38 +++++++++ src/mvvmquick/FontEdit.qml | 10 +++ src/mvvmquick/ListEdit.qml | 19 +++++ src/mvvmquick/SpinBox.qml | 8 ++ src/mvvmquick/TextField.qml | 30 ++++++++ src/mvvmquick/inputviewfactory.cpp | 77 +++++++++++++++++++ src/mvvmquick/inputviewfactory.h | 41 ++++++++++ .../inputviewfactory_p.h | 4 +- src/mvvmquick/mvvmquick.pro | 10 ++- src/mvvmquick/qtmvvmquick_module.qrc | 10 +++ src/mvvmquick/quickpresenter.cpp | 15 +++- src/mvvmquick/quickpresenter.h | 11 +++ src/mvvmquick/quickpresenter_p.h | 1 + ...viewfactory.cpp => inputwidgetfactory.cpp} | 16 ++-- ...nputviewfactory.h => inputwidgetfactory.h} | 18 ++--- src/mvvmwidgets/inputwidgetfactory_p.h | 19 +++++ src/mvvmwidgets/mvvmwidgets.pro | 16 ++-- src/mvvmwidgets/selectcombobox_p.h | 2 - src/mvvmwidgets/widgetspresenter.cpp | 9 ++- src/mvvmwidgets/widgetspresenter.h | 11 ++- src/mvvmwidgets/widgetspresenter_p.h | 2 +- 26 files changed, 375 insertions(+), 40 deletions(-) create mode 100644 src/mvvmquick/CheckBox.qml create mode 100644 src/mvvmquick/DoubleSpinBox.qml create mode 100644 src/mvvmquick/FontEdit.qml create mode 100644 src/mvvmquick/ListEdit.qml create mode 100644 src/mvvmquick/SpinBox.qml create mode 100644 src/mvvmquick/TextField.qml create mode 100644 src/mvvmquick/inputviewfactory.cpp create mode 100644 src/mvvmquick/inputviewfactory.h rename src/{mvvmwidgets => mvvmquick}/inputviewfactory_p.h (69%) create mode 100644 src/mvvmquick/qtmvvmquick_module.qrc rename src/mvvmwidgets/{inputviewfactory.cpp => inputwidgetfactory.cpp} (85%) rename src/mvvmwidgets/{inputviewfactory.h => inputwidgetfactory.h} (67%) create mode 100644 src/mvvmwidgets/inputwidgetfactory_p.h diff --git a/src/imports/mvvmquick/plugins.qmltypes b/src/imports/mvvmquick/plugins.qmltypes index db07cb3..5c4f600 100644 --- a/src/imports/mvvmquick/plugins.qmltypes +++ b/src/imports/mvvmquick/plugins.qmltypes @@ -8,6 +8,25 @@ import QtQuick.tooling 1.2 Module { dependencies: ["QtQuick 2.8"] + Component { + name: "QtMvvm::InputViewFactory" + prototype: "QObject" + exports: ["de.skycoder42.QtMvvm.Quick/InputViewFactory 1.0"] + isCreatable: false + exportMetaObjectRevisions: [0] + Method { + name: "getInputUrl" + type: "QUrl" + Parameter { name: "type"; type: "QByteArray" } + Parameter { name: "viewProperties"; type: "QVariantMap" } + } + Method { + name: "addSimpleView" + type: "bool" + Parameter { name: "type"; type: "QByteArray" } + Parameter { name: "qmlFileUrl"; type: "QUrl" } + } + } Component { name: "QtMvvm::QQmlQuickPresenter" prototype: "QObject" @@ -16,6 +35,7 @@ Module { isSingleton: true exportMetaObjectRevisions: [0] Property { name: "currentStyle"; type: "string"; isReadonly: true } + Property { name: "inputViewFactory"; type: "InputViewFactory"; isReadonly: true; isPointer: true } Property { name: "qmlPresenter"; type: "QObject"; isPointer: true } Property { name: "viewLoading"; type: "bool"; isReadonly: true } Property { name: "loadingProgress"; type: "double"; isReadonly: true } @@ -31,5 +51,9 @@ Module { name: "loadingProgressChanged" Parameter { name: "loadingProgress"; type: "double" } } + Signal { + name: "inputViewFactoryChanged" + Parameter { name: "inputViewFactory"; type: "InputViewFactory"; isPointer: true } + } } } diff --git a/src/imports/mvvmquick/qqmlquickpresenter.cpp b/src/imports/mvvmquick/qqmlquickpresenter.cpp index 25edc03..075732a 100644 --- a/src/imports/mvvmquick/qqmlquickpresenter.cpp +++ b/src/imports/mvvmquick/qqmlquickpresenter.cpp @@ -18,6 +18,8 @@ QQmlQuickPresenter::QQmlQuickPresenter(QQmlEngine *engine) : _loadCache() { QuickPresenterPrivate::setQmlPresenter(this); + connect(QuickPresenterPrivate::currentPresenter(), &QuickPresenter::inputViewFactoryChanged, + this, &QQmlQuickPresenter::inputViewFactoryChanged); } QString QQmlQuickPresenter::currentStyle() const @@ -25,6 +27,11 @@ QString QQmlQuickPresenter::currentStyle() const return QQuickStyle::name(); } +InputViewFactory *QQmlQuickPresenter::inputViewFactory() const +{ + return QuickPresenterPrivate::currentPresenter()->inputViewFactory(); +} + bool QQmlQuickPresenter::isViewLoading() const { return _latestComponent; diff --git a/src/imports/mvvmquick/qqmlquickpresenter.h b/src/imports/mvvmquick/qqmlquickpresenter.h index e940028..258e30b 100644 --- a/src/imports/mvvmquick/qqmlquickpresenter.h +++ b/src/imports/mvvmquick/qqmlquickpresenter.h @@ -14,6 +14,8 @@ #include #include +#include + namespace QtMvvm { class QQmlQuickPresenter : public QObject @@ -21,6 +23,7 @@ class QQmlQuickPresenter : public QObject Q_OBJECT Q_PROPERTY(QString currentStyle READ currentStyle CONSTANT) + Q_PROPERTY(InputViewFactory* inputViewFactory READ inputViewFactory NOTIFY inputViewFactoryChanged) Q_PROPERTY(QObject* qmlPresenter MEMBER _qmlPresenter NOTIFY qmlPresenterChanged) Q_PROPERTY(bool viewLoading READ isViewLoading NOTIFY viewLoadingChanged) @@ -30,15 +33,19 @@ public: explicit QQmlQuickPresenter(QQmlEngine *engine); QString currentStyle() const; + InputViewFactory* inputViewFactory() const; bool isViewLoading() const; qreal loadingProgress() const; + Q_SIGNALS: void qmlPresenterChanged(QObject* qmlPresenter); void viewLoadingChanged(bool viewLoading); void loadingProgressChanged(qreal loadingProgress); + void inputViewFactoryChanged(InputViewFactory* inputViewFactory); + private Q_SLOTS: void present(QtMvvm::ViewModel *viewModel, const QVariantHash ¶ms, const QUrl &viewUrl, QPointer parent); void showDialog(const QtMvvm::MessageConfig &config, QtMvvm::MessageResult *result); diff --git a/src/imports/mvvmquick/qtmvvmquick_plugin.cpp b/src/imports/mvvmquick/qtmvvmquick_plugin.cpp index bd88956..da57db5 100644 --- a/src/imports/mvvmquick/qtmvvmquick_plugin.cpp +++ b/src/imports/mvvmquick/qtmvvmquick_plugin.cpp @@ -2,6 +2,8 @@ #include +#include + #include "qqmlquickpresenter.h" #include "svgimageprovider.h" @@ -31,6 +33,7 @@ void QtMvvmQuickDeclarativeModule::registerTypes(const char *uri) Q_ASSERT(qstrcmp(uri, "de.skycoder42.QtMvvm.Quick") == 0); //Version 1.0 + qmlRegisterUncreatableType(uri, 1, 0, "InputViewFactory", QStringLiteral("InputViewFactories can only be created from C++ via the QuickPresenter")); qmlRegisterSingletonType(uri, 1, 0, "QuickPresenter", createQuickPresenterQmlSingleton); // Check to make shure no module update is forgotten diff --git a/src/mvvmquick/CheckBox.qml b/src/mvvmquick/CheckBox.qml new file mode 100644 index 0000000..58865ca --- /dev/null +++ b/src/mvvmquick/CheckBox.qml @@ -0,0 +1,7 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 + +CheckBox { + id: _edit + property alias inputValue: _edit.checked +} diff --git a/src/mvvmquick/DoubleSpinBox.qml b/src/mvvmquick/DoubleSpinBox.qml new file mode 100644 index 0000000..e8b1220 --- /dev/null +++ b/src/mvvmquick/DoubleSpinBox.qml @@ -0,0 +1,38 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 + +SpinBox { + id: _edit + property alias inputValue: _edit.dValue + editable: true + + //double spinbox code + property int decimals: 2 + property double dFrom: 0.0 + property double dTo: 100.0 + property double dValue: 0.0 + property double dStepSize: 0.1 + + readonly property int factor: Math.pow(10, _edit.decimals) + + stepSize: _edit.dStepSize * _edit.factor + from: _edit.dFrom * _edit.factor + to: _edit.dTo * _edit.factor + value: _edit.dValue * _edit.factor + + validator: DoubleValidator { + bottom: _edit.dFrom + top: _edit.dTo + } + + textFromValue: function(value, locale) { + return Number(value / _edit.factor).toLocaleString(locale, 'f', _edit.decimals); + } + + valueFromText: function(text, locale) { + return Number.fromLocaleString(locale, text) * _edit.factor; + } + + onDValueChanged: _edit.value = _edit.dValue * _edit.factor + onValueChanged: _edit.dValue = _edit.value / _edit.factor +} diff --git a/src/mvvmquick/FontEdit.qml b/src/mvvmquick/FontEdit.qml new file mode 100644 index 0000000..9cccfe0 --- /dev/null +++ b/src/mvvmquick/FontEdit.qml @@ -0,0 +1,10 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 + +ComboBox { + id: _edit + property alias inputValue: _edit.currentText + + model: Qt.fontFamilies() + editable: false +} diff --git a/src/mvvmquick/ListEdit.qml b/src/mvvmquick/ListEdit.qml new file mode 100644 index 0000000..0f2cb00 --- /dev/null +++ b/src/mvvmquick/ListEdit.qml @@ -0,0 +1,19 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 + +ComboBox { + id: _edit + property alias inputValue: _edit.currentValue + property var currentValue: getCurrentValue() + property alias listElements: _edit.model + + textRole: "name" + + function getCurrentValue() { + var value = _edit.model[_edit.currentIndex].value; + if(typeof value !== "undefined") + return value; + else + return _edit.displayText; + } +} diff --git a/src/mvvmquick/SpinBox.qml b/src/mvvmquick/SpinBox.qml new file mode 100644 index 0000000..f4ff7e7 --- /dev/null +++ b/src/mvvmquick/SpinBox.qml @@ -0,0 +1,8 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 + +SpinBox { + id: _edit + property alias inputValue: _edit.value + editable: true +} diff --git a/src/mvvmquick/TextField.qml b/src/mvvmquick/TextField.qml new file mode 100644 index 0000000..56c4064 --- /dev/null +++ b/src/mvvmquick/TextField.qml @@ -0,0 +1,30 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 + +TextField { + id: _edit + property alias inputValue: _edit.text + property string regexp: "" + property string patternOptions + + selectByMouse: true + validator: regexp != "" ? _validator.createObject(_edit) : null + + Component { + id: _validator + RegExpValidator { + regExp: new RegExp(_edit.regexp, optionsAsString(_edit.patternOptions)) + + function optionsAsString(options) { + var resStr; + if((options & 0x0001) != 0) //QRegularExpression::CaseInsensitiveOption + resStr += "i"; + if((options & 0x0004) != 0) //QRegularExpression::MultilineOption + resStr += "m"; + if((options & 0x0040) != 0) //QRegularExpression::UseUnicodePropertiesOption + resStr += "u"; + return resStr; + } + } + } +} diff --git a/src/mvvmquick/inputviewfactory.cpp b/src/mvvmquick/inputviewfactory.cpp new file mode 100644 index 0000000..079d3cd --- /dev/null +++ b/src/mvvmquick/inputviewfactory.cpp @@ -0,0 +1,77 @@ +#include "inputviewfactory.h" +#include "inputviewfactory_p.h" + +#include + +#include +using namespace QtMvvm; + +static void initResources() +{ +#ifdef QT_STATIC + Q_INIT_RESOURCE(qtmvvmquick_module); +#endif +} + +InputViewFactory::InputViewFactory() : + QObject(nullptr), + d(new InputViewFactoryPrivate()) +{ + initResources(); +} + +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->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 == QMetaType::typeName(QMetaType::QString) || type == "string") + return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/TextField.qml"); + else if(type == QMetaType::typeName(QMetaType::Int)) + return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/SpinBox.qml"); + else if(type == QMetaType::typeName(QMetaType::Double)) + return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/DoubleSpinBox.qml"); +// else if(type == QMetaType::typeName(QMetaType::QDate)) +// return QUrl(); +// else if(type == QMetaType::typeName(QMetaType::QTime)) +// return QUrl(); +// else if(type == QMetaType::typeName(QMetaType::QDateTime) || type == "date") +// return QUrl(); + else if(type == QMetaType::typeName(QMetaType::QFont)) + return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/FontEdit.qml"); +// else if(type == QMetaType::typeName(QMetaType::QUrl) || type == "url") +// return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/UrlField.qml"); + else if(type == "selection" || type == "list") + return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/ListEdit.qml"); + else { + logCritical() << "Failed to find any input view for input type:" << type; + return QUrl(); + } +} + +bool InputViewFactory::addSimpleView(const QByteArray &type, const QUrl &qmlFileUrl) +{ + d->simpleViews.insert(type, qmlFileUrl); + return true; +} + +// ------------- Private Implementation ------------- + +InputViewFactoryPrivate::InputViewFactoryPrivate() : + simpleViews() +{} + diff --git a/src/mvvmquick/inputviewfactory.h b/src/mvvmquick/inputviewfactory.h new file mode 100644 index 0000000..7e5dfae --- /dev/null +++ b/src/mvvmquick/inputviewfactory.h @@ -0,0 +1,41 @@ +#ifndef QTMVVM_INPUTVIEWFACTORY_H +#define QTMVVM_INPUTVIEWFACTORY_H + +#include +#include +#include +#include + +#include "QtMvvmQuick/qtmvvmquick_global.h" + +namespace QtMvvm { + +class InputViewFactoryPrivate; +class Q_MVVMQUICK_EXPORT InputViewFactory : public QObject +{ + Q_OBJECT + +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); + +private: + QScopedPointer d; +}; + +template +bool InputViewFactory::addSimpleView(const QUrl &qmlFileUrl) +{ + return addSimpleView(QMetaType::typeName(qMetaTypeId()), qmlFileUrl); +} + +} + +#endif // QTMVVM_INPUTVIEWFACTORY_H diff --git a/src/mvvmwidgets/inputviewfactory_p.h b/src/mvvmquick/inputviewfactory_p.h similarity index 69% rename from src/mvvmwidgets/inputviewfactory_p.h rename to src/mvvmquick/inputviewfactory_p.h index 866a4b1..23f1b26 100644 --- a/src/mvvmwidgets/inputviewfactory_p.h +++ b/src/mvvmquick/inputviewfactory_p.h @@ -1,7 +1,7 @@ #ifndef QTMVVM_INPUTVIEWFACTORY_P_H #define QTMVVM_INPUTVIEWFACTORY_P_H -#include "qtmvvmwidgets_global.h" +#include "qtmvvmquick_global.h" #include "inputviewfactory.h" namespace QtMvvm { @@ -11,7 +11,7 @@ class InputViewFactoryPrivate public: InputViewFactoryPrivate(); - QHash> simpleWidgets; + QHash simpleViews; }; } diff --git a/src/mvvmquick/mvvmquick.pro b/src/mvvmquick/mvvmquick.pro index 350ed4e..33f4365 100644 --- a/src/mvvmquick/mvvmquick.pro +++ b/src/mvvmquick/mvvmquick.pro @@ -5,10 +5,13 @@ QT = core gui widgets mvvmcore mvvmcore-private HEADERS += \ qtmvvmquick_global.h \ quickpresenter.h \ - quickpresenter_p.h + quickpresenter_p.h \ + inputviewfactory.h \ + inputviewfactory_p.h SOURCES += \ - quickpresenter.cpp + quickpresenter.cpp \ + inputviewfactory.cpp TRANSLATIONS += \ translations/qtmvvmquick_de.ts \ @@ -35,3 +38,6 @@ else: include($$OUT_PWD/qpmx_generated.pri) qpmx_ts_target.files -= $$OUT_PWD/$$QPMX_WORKINGDIR/qtmvvmquick_template.qm qpmx_ts_target.files += translations/qtmvvmquick_template.ts + +RESOURCES += \ + qtmvvmquick_module.qrc diff --git a/src/mvvmquick/qtmvvmquick_module.qrc b/src/mvvmquick/qtmvvmquick_module.qrc new file mode 100644 index 0000000..d574450 --- /dev/null +++ b/src/mvvmquick/qtmvvmquick_module.qrc @@ -0,0 +1,10 @@ + + + ListEdit.qml + TextField.qml + SpinBox.qml + CheckBox.qml + DoubleSpinBox.qml + FontEdit.qml + + diff --git a/src/mvvmquick/quickpresenter.cpp b/src/mvvmquick/quickpresenter.cpp index 58941c3..4dec0aa 100644 --- a/src/mvvmquick/quickpresenter.cpp +++ b/src/mvvmquick/quickpresenter.cpp @@ -67,6 +67,17 @@ bool QuickPresenter::presentToQml(QObject *qmlPresenter, QObject *viewObject) return presented.toBool(); } +InputViewFactory *QuickPresenter::inputViewFactory() const +{ + return d->inputViewFactory.data(); +} + +void QuickPresenter::setInputViewFactory(InputViewFactory *inputViewFactory) +{ + d->inputViewFactory.reset(inputViewFactory); + emit inputViewFactoryChanged(inputViewFactory); +} + QUrl QuickPresenter::findViewUrl(const QMetaObject *viewModelType) { auto currentMeta = viewModelType; @@ -141,7 +152,9 @@ bool QuickPresenter::nameOrClassContains(const QObject *obj, const QString &cont QuickPresenterPrivate::QuickPresenterPrivate() : explicitMappings(), - searchDirs({QStringLiteral(":/qtmvvm/views")}) + searchDirs({QStringLiteral(":/qtmvvm/views")}), + qmlPresenter(nullptr), + inputViewFactory(new InputViewFactory()) {} QuickPresenter *QuickPresenterPrivate::currentPresenter() diff --git a/src/mvvmquick/quickpresenter.h b/src/mvvmquick/quickpresenter.h index ea986af..334c908 100644 --- a/src/mvvmquick/quickpresenter.h +++ b/src/mvvmquick/quickpresenter.h @@ -10,6 +10,7 @@ #include #include "QtMvvmQuick/qtmvvmquick_global.h" +#include "QtMvvmQuick/inputviewfactory.h" namespace QtMvvm { @@ -19,6 +20,8 @@ class Q_MVVMQUICK_EXPORT QuickPresenter : public QObject, public IPresenter Q_OBJECT Q_INTERFACES(QtMvvm::IPresenter) + Q_PROPERTY(InputViewFactory* inputViewFactory READ inputViewFactory WRITE setInputViewFactory NOTIFY inputViewFactoryChanged) + public: explicit QuickPresenter(QObject *parent = nullptr); ~QuickPresenter(); @@ -36,6 +39,14 @@ public: virtual bool presentToQml(QObject *qmlPresenter, QObject *viewObject); + InputViewFactory* inputViewFactory() const; + +public Q_SLOTS: + void setInputViewFactory(InputViewFactory* inputViewFactory); + +Q_SIGNALS: + void inputViewFactoryChanged(InputViewFactory* inputViewFactory); + protected: virtual QUrl findViewUrl(const QMetaObject *viewModelType); virtual int presentMethodIndex(const QMetaObject *presenterMetaObject, QObject *viewObject); diff --git a/src/mvvmquick/quickpresenter_p.h b/src/mvvmquick/quickpresenter_p.h index a1dc996..2084ba4 100644 --- a/src/mvvmquick/quickpresenter_p.h +++ b/src/mvvmquick/quickpresenter_p.h @@ -24,6 +24,7 @@ private: QStringList searchDirs; QPointer qmlPresenter; + QScopedPointer inputViewFactory; }; } diff --git a/src/mvvmwidgets/inputviewfactory.cpp b/src/mvvmwidgets/inputwidgetfactory.cpp similarity index 85% rename from src/mvvmwidgets/inputviewfactory.cpp rename to src/mvvmwidgets/inputwidgetfactory.cpp index ec79a57..02e9e9a 100644 --- a/src/mvvmwidgets/inputviewfactory.cpp +++ b/src/mvvmwidgets/inputwidgetfactory.cpp @@ -1,5 +1,5 @@ -#include "inputviewfactory.h" -#include "inputviewfactory_p.h" +#include "inputwidgetfactory.h" +#include "inputwidgetfactory_p.h" #include #include @@ -15,13 +15,13 @@ using namespace QtMvvm; -InputViewFactory::InputViewFactory() : - d(new InputViewFactoryPrivate()) +InputWidgetFactory::InputWidgetFactory() : + d(new InputWidgetFactoryPrivate()) {} -InputViewFactory::~InputViewFactory() {} +InputWidgetFactory::~InputWidgetFactory() {} -QWidget *InputViewFactory::createInput(const QByteArray &type, QWidget *parent, const QVariantMap &viewProperties) +QWidget *InputWidgetFactory::createInput(const QByteArray &type, QWidget *parent, const QVariantMap &viewProperties) { QWidget *widget = nullptr; if(d->simpleWidgets.contains(type)) @@ -72,7 +72,7 @@ QWidget *InputViewFactory::createInput(const QByteArray &type, QWidget *parent, return widget; } -void InputViewFactory::addSimpleWidget(const QByteArray &type, const std::function &creator) +void InputWidgetFactory::addSimpleWidget(const QByteArray &type, const std::function &creator) { Q_ASSERT_X(creator, Q_FUNC_INFO, "The passed creation function must be valid"); d->simpleWidgets.insert(type, creator); @@ -80,6 +80,6 @@ void InputViewFactory::addSimpleWidget(const QByteArray &type, const std::functi // ------------- Private Implementation ------------- -InputViewFactoryPrivate::InputViewFactoryPrivate() : +InputWidgetFactoryPrivate::InputWidgetFactoryPrivate() : simpleWidgets() {} diff --git a/src/mvvmwidgets/inputviewfactory.h b/src/mvvmwidgets/inputwidgetfactory.h similarity index 67% rename from src/mvvmwidgets/inputviewfactory.h rename to src/mvvmwidgets/inputwidgetfactory.h index 7324bc9..d6486de 100644 --- a/src/mvvmwidgets/inputviewfactory.h +++ b/src/mvvmwidgets/inputwidgetfactory.h @@ -1,5 +1,5 @@ -#ifndef QTMVVM_INPUTVIEWFACTORY_H -#define QTMVVM_INPUTVIEWFACTORY_H +#ifndef QTMVVM_INPUTWIDGETFACTORY_H +#define QTMVVM_INPUTWIDGETFACTORY_H #include @@ -11,12 +11,12 @@ namespace QtMvvm { -class InputViewFactoryPrivate; -class InputViewFactory +class InputWidgetFactoryPrivate; +class InputWidgetFactory { public: - InputViewFactory(); - virtual ~InputViewFactory(); + InputWidgetFactory(); + virtual ~InputWidgetFactory(); virtual QWidget *createInput(const QByteArray &type, QWidget *parent, const QVariantMap &viewProperties); @@ -25,11 +25,11 @@ public: void addSimpleWidget(); private: - QScopedPointer d; + QScopedPointer d; }; template -void InputViewFactory::addSimpleWidget() +void InputWidgetFactory::addSimpleWidget() { addSimpleWidget(QMetaType::typeName(qMetaTypeId()), [](QWidget *parent){ return new TWidget(parent); @@ -38,4 +38,4 @@ void InputViewFactory::addSimpleWidget() } -#endif // QTMVVM_INPUTVIEWFACTORY_H +#endif // QTMVVM_INPUTWIDGETFACTORY_H diff --git a/src/mvvmwidgets/inputwidgetfactory_p.h b/src/mvvmwidgets/inputwidgetfactory_p.h new file mode 100644 index 0000000..e015f82 --- /dev/null +++ b/src/mvvmwidgets/inputwidgetfactory_p.h @@ -0,0 +1,19 @@ +#ifndef QTMVVM_INPUTWIDGETFACTORY_P_H +#define QTMVVM_INPUTWIDGETFACTORY_P_H + +#include "qtmvvmwidgets_global.h" +#include "inputwidgetfactory.h" + +namespace QtMvvm { + +class InputWidgetFactoryPrivate +{ +public: + InputWidgetFactoryPrivate(); + + QHash> simpleWidgets; +}; + +} + +#endif // QTMVVM_INPUTWIDGETFACTORY_P_H diff --git a/src/mvvmwidgets/mvvmwidgets.pro b/src/mvvmwidgets/mvvmwidgets.pro index 49da072..38bfb59 100644 --- a/src/mvvmwidgets/mvvmwidgets.pro +++ b/src/mvvmwidgets/mvvmwidgets.pro @@ -7,16 +7,16 @@ HEADERS += \ widgetspresenter.h \ ipresentingview.h \ widgetspresenter_p.h \ - inputviewfactory.h \ - inputviewfactory_p.h \ - fontcombobox_p.h \ - selectcombobox_p.h + fontcombobox_p.h \ + selectcombobox_p.h \ + inputwidgetfactory.h \ + inputwidgetfactory_p.h SOURCES += \ widgetspresenter.cpp \ - inputviewfactory.cpp \ - fontcombobox.cpp \ - selectcombobox.cpp + fontcombobox.cpp \ + selectcombobox.cpp \ + inputwidgetfactory.cpp TRANSLATIONS += \ translations/qtmvvmwidgets_de.ts \ @@ -43,3 +43,5 @@ else: include($$OUT_PWD/qpmx_generated.pri) qpmx_ts_target.files -= $$OUT_PWD/$$QPMX_WORKINGDIR/qtmvvmwidgets_template.qm qpmx_ts_target.files += translations/qtmvvmwidgets_template.ts + +mingw: LIBS_PRIVATE += -lQt5Widgets -lQt5Gui -lQt5Core diff --git a/src/mvvmwidgets/selectcombobox_p.h b/src/mvvmwidgets/selectcombobox_p.h index d43123b..5939d75 100644 --- a/src/mvvmwidgets/selectcombobox_p.h +++ b/src/mvvmwidgets/selectcombobox_p.h @@ -16,8 +16,6 @@ class SelectComboBox : public QComboBox Q_PROPERTY(QVariant currentValue READ currentValue WRITE setCurrentValue NOTIFY currentValueChanged USER true) Q_PROPERTY(QVariantList listElements READ listElements WRITE setListElements NOTIFY listElementsChanged) - QVariantMap m_listElements; - public: explicit SelectComboBox(QWidget *parent = nullptr); diff --git a/src/mvvmwidgets/widgetspresenter.cpp b/src/mvvmwidgets/widgetspresenter.cpp index d90271a..f3c361a 100644 --- a/src/mvvmwidgets/widgetspresenter.cpp +++ b/src/mvvmwidgets/widgetspresenter.cpp @@ -99,14 +99,15 @@ void WidgetsPresenter::showDialog(const MessageConfig &config, MessageResult *re presentOtherDialog(config, result); } -InputViewFactory *WidgetsPresenter::inputViewFactory() const +InputWidgetFactory *WidgetsPresenter::inputWidgetFactory() const { return d->inputViewFactory.data(); } -void WidgetsPresenter::setInputViewFactory(InputViewFactory *inputViewFactory) +void WidgetsPresenter::setInputWidgetFactory(InputWidgetFactory *inputWidgetFactory) { - d->inputViewFactory.reset(inputViewFactory); + d->inputViewFactory.reset(inputWidgetFactory); + emit inputWidgetFactoryChanged(inputWidgetFactory); } const QMetaObject *WidgetsPresenter::findWidgetMetaObject(const QMetaObject *viewModelMetaObject) @@ -395,7 +396,7 @@ void WidgetsPresenter::presentOtherDialog(const MessageConfig &config, QPointer< // ------------- Private Implementation ------------- WidgetsPresenterPrivate::WidgetsPresenterPrivate() : - inputViewFactory(new InputViewFactory()), + inputViewFactory(new InputWidgetFactory()), implicitMappings(), explicitMappings() {} diff --git a/src/mvvmwidgets/widgetspresenter.h b/src/mvvmwidgets/widgetspresenter.h index 1262c77..615b118 100644 --- a/src/mvvmwidgets/widgetspresenter.h +++ b/src/mvvmwidgets/widgetspresenter.h @@ -10,7 +10,7 @@ #include #include "QtMvvmWidgets/qtmvvmwidgets_global.h" -#include "QtMvvmWidgets/inputviewfactory.h" +#include "QtMvvmWidgets/inputwidgetfactory.h" namespace QtMvvm { @@ -20,7 +20,7 @@ class Q_MVVMWIDGETS_EXPORT WidgetsPresenter : public QObject, public IPresenter Q_OBJECT Q_INTERFACES(QtMvvm::IPresenter) - Q_PROPERTY(InputViewFactory* inputViewFactory READ inputViewFactory WRITE setInputViewFactory) + Q_PROPERTY(InputWidgetFactory* inputWidgetFactory READ inputWidgetFactory WRITE setInputWidgetFactory NOTIFY inputWidgetFactoryChanged) public: explicit WidgetsPresenter(QObject *parent = nullptr); @@ -40,10 +40,13 @@ public: void present(ViewModel *viewModel, const QVariantHash ¶ms, QPointer parent) override; void showDialog(const MessageConfig &config, MessageResult *result) override; - InputViewFactory* inputViewFactory() const; + InputWidgetFactory* inputWidgetFactory() const; public Q_SLOTS: - void setInputViewFactory(InputViewFactory* inputViewFactory); + void setInputWidgetFactory(InputWidgetFactory* inputWidgetFactory); + +Q_SIGNALS: + void inputWidgetFactoryChanged(InputWidgetFactory* inputWidgetFactory); protected: virtual const QMetaObject *findWidgetMetaObject(const QMetaObject *viewModelMetaObject); diff --git a/src/mvvmwidgets/widgetspresenter_p.h b/src/mvvmwidgets/widgetspresenter_p.h index d884c4e..11b6518 100644 --- a/src/mvvmwidgets/widgetspresenter_p.h +++ b/src/mvvmwidgets/widgetspresenter_p.h @@ -16,7 +16,7 @@ public: static WidgetsPresenter *currentPresenter(); - QScopedPointer inputViewFactory; + QScopedPointer inputViewFactory; QSet implicitMappings; QHash explicitMappings; };