Browse Source

added automatic c++ variant type conversion

for inputs, as the qml (js) conversion can conflict with the cpp one.
Now everything is converted in cpp, preventing such problems
pull/2/head
Skycoder42 7 years ago
parent
commit
6c0cdd5eac
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 1
      examples/mvvmquick/SampleQuick/TabView.qml
  2. 6
      src/imports/mvvmcore/plugins.qmltypes
  3. 5
      src/imports/mvvmcore/qqmlcoreapp.cpp
  4. 1
      src/imports/mvvmcore/qqmlcoreapp.h
  5. 10
      src/imports/mvvmquick/DateEdit.qml
  6. 1
      src/imports/mvvmquick/DialogPresenter11.qml
  7. 1
      src/imports/mvvmquick/PresentingStackView.qml
  8. 1
      src/imports/mvvmquick/PresentingStackView11.qml
  9. 2
      src/imports/mvvmquick/TimeEdit.qml
  10. 10
      src/imports/mvvmquick/settingsentrymodel.cpp
  11. 2
      src/imports/mvvmquick/settingsentrymodel.h
  12. 49
      src/mvvmcore/coreapp.cpp
  13. 11
      src/mvvmcore/coreapp.h
  14. 3
      src/mvvmcore/coreapp_p.h
  15. 5
      src/mvvmcore/message.cpp
  16. 11
      src/mvvmquick/BoolDelegate.qml
  17. 13
      src/mvvmquick/DateEdit.qml
  18. 24
      src/mvvmquick/DateTimeEdit.qml
  19. 11
      src/mvvmquick/SwitchDelegate.qml
  20. 13
      src/mvvmquick/TimeEdit.qml
  21. 1
      src/mvvmquick/inputviewfactory.cpp
  22. 2
      src/mvvmquick/inputviewfactory.h
  23. 2
      src/mvvmwidgets/inputwidgetfactory.cpp
  24. 10
      src/mvvmwidgets/settingsdialog.cpp
  25. 2
      src/mvvmwidgets/settingsdialog_p.h
  26. 1
      src/mvvmwidgets/widgetspresenter.cpp

1
examples/mvvmquick/SampleQuick/TabView.qml

@ -64,7 +64,6 @@ Page {
} }
function presentTab(item) { function presentTab(item) {
console.log("here");
tabBar.insertItem(tabBar.count - 1, _newTab.createObject(tabBar, {viewModel: item.viewModel})); tabBar.insertItem(tabBar.count - 1, _newTab.createObject(tabBar, {viewModel: item.viewModel}));
item.parent = swipe; item.parent = swipe;
swipe.addItem(item); swipe.addItem(item);

6
src/imports/mvvmcore/plugins.qmltypes

@ -119,6 +119,12 @@ Module {
type: "QtMvvm::MessageResult*" type: "QtMvvm::MessageResult*"
Parameter { name: "config"; type: "QtMvvm::MessageConfig" } Parameter { name: "config"; type: "QtMvvm::MessageConfig" }
} }
Method {
name: "safeCastInputType"
type: "QVariant"
Parameter { name: "type"; type: "string" }
Parameter { name: "value"; type: "QVariant" }
}
} }
Component { Component {
name: "QtMvvm::QQmlMvvmBinding" name: "QtMvvm::QQmlMvvmBinding"

5
src/imports/mvvmcore/qqmlcoreapp.cpp

@ -13,6 +13,11 @@ MessageResult *QQmlCoreApp::showDialog(const MessageConfig &config)
return CoreApp::showDialog(config); return CoreApp::showDialog(config);
} }
QVariant QQmlCoreApp::safeCastInputType(const QString &type, const QVariant &value)
{
return CoreApp::safeCastInputType(type.toUtf8(), value);
}
void QQmlCoreApp::show(const QString &viewModelName, const QVariantHash &params, ViewModel *parentViewModel) void QQmlCoreApp::show(const QString &viewModelName, const QVariantHash &params, ViewModel *parentViewModel)
{ {
CoreApp::show(qUtf8Printable(viewModelName), params, parentViewModel); CoreApp::show(qUtf8Printable(viewModelName), params, parentViewModel);

1
src/imports/mvvmcore/qqmlcoreapp.h

@ -15,6 +15,7 @@ public:
explicit QQmlCoreApp(QObject *parent = nullptr); explicit QQmlCoreApp(QObject *parent = nullptr);
Q_INVOKABLE QtMvvm::MessageResult *showDialog(const QtMvvm::MessageConfig &config); Q_INVOKABLE QtMvvm::MessageResult *showDialog(const QtMvvm::MessageConfig &config);
Q_INVOKABLE QVariant safeCastInputType(const QString &type, const QVariant &value);
public Q_SLOTS: public Q_SLOTS:
void show(const QString &viewModelName, const QVariantHash &params = {}, QtMvvm::ViewModel *parentViewModel = nullptr); void show(const QString &viewModelName, const QVariantHash &params = {}, QtMvvm::ViewModel *parentViewModel = nullptr);

10
src/imports/mvvmquick/DateEdit.qml

@ -9,7 +9,7 @@ ListView {
property date firstDate: new Date(1970, 0, 1) property date firstDate: new Date(1970, 0, 1)
property date lastDate: new Date(9999, 11, 31) property date lastDate: new Date(9999, 11, 31)
property date currentDate: today(); property date date: today();
function today() { function today() {
var cDate = new Date(); var cDate = new Date();
@ -26,14 +26,14 @@ ListView {
if(_skipNextFocus) if(_skipNextFocus)
_skipNextFocus = false; _skipNextFocus = false;
else { else {
_calenderList.currentIndex = calendarModel.indexOf(_calenderList.currentDate); _calenderList.currentIndex = calendarModel.indexOf(_calenderList.date);
_calenderList.positionViewAtIndex(_calenderList.currentIndex, ListView.SnapPosition); _calenderList.positionViewAtIndex(_calenderList.currentIndex, ListView.SnapPosition);
} }
} }
} }
Component.onCompleted: _p.focusDate() Component.onCompleted: _p.focusDate()
onCurrentDateChanged: _p.focusDate() onDateChanged: _p.focusDate()
implicitWidth: 300 implicitWidth: 300
implicitHeight: 200 implicitHeight: 200
@ -120,7 +120,7 @@ ListView {
id: helper id: helper
} }
readonly property bool isCurrent: model.day === _calenderList.currentDate.getDate() && model.month === _calenderList.currentDate.getMonth() readonly property bool isCurrent: model.day === _calenderList.date.getDate() && model.month === _calenderList.date.getMonth()
readonly property alias highlightColor: helper.highlight readonly property alias highlightColor: helper.highlight
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
@ -160,7 +160,7 @@ ListView {
_calenderList.incrementCurrentIndex(); _calenderList.incrementCurrentIndex();
_p._skipNextFocus = true; _p._skipNextFocus = true;
} }
_calenderList.currentDate = model.date; _calenderList.date = model.date;
} }
} }
} }

1
src/imports/mvvmquick/DialogPresenter11.qml

@ -209,7 +209,6 @@ QtObject {
var props = config.viewProperties; var props = config.viewProperties;
props["msgConfig"] = config; props["msgConfig"] = config;
props["msgResult"] = result; props["msgResult"] = result;
console.log(config.buttons);
var incubator = _inputComponent.incubateObject(rootItem, props, Qt.Synchronous); var incubator = _inputComponent.incubateObject(rootItem, props, Qt.Synchronous);
return incubator.status !== Component.Error; return incubator.status !== Component.Error;
} }

1
src/imports/mvvmquick/PresentingStackView.qml

@ -160,7 +160,6 @@ StackView {
*/ */
function safeReplace(target, item, properties, operation) { function safeReplace(target, item, properties, operation) {
var items = []; var items = [];
console.log("current depth: ", depth)
for(var i = depth -1; i >= 0; i--) { for(var i = depth -1; i >= 0; i--) {
var cItem = get(i, StackView.ForceLoad); var cItem = get(i, StackView.ForceLoad);
_clearItems.push(cItem); _clearItems.push(cItem);

1
src/imports/mvvmquick/PresentingStackView11.qml

@ -156,7 +156,6 @@ StackView {
*/ */
function safeReplace(target, item, properties, operation) { function safeReplace(target, item, properties, operation) {
var items = []; var items = [];
console.log("current depth: ", depth)
for(var i = depth -1; i >= 0; i--) { for(var i = depth -1; i >= 0; i--) {
var cItem = get(i, StackView.ForceLoad); var cItem = get(i, StackView.ForceLoad);
_clearItems.push(cItem); _clearItems.push(cItem);

2
src/imports/mvvmquick/TimeEdit.qml

@ -12,7 +12,7 @@ RowLayout {
_forceTime = time _forceTime = time
} }
property var _forceTime: new Date() property date _forceTime: new Date()
property bool showHours: true property bool showHours: true
property bool showMinutes: true property bool showMinutes: true

10
src/imports/mvvmquick/settingsentrymodel.cpp

@ -1,6 +1,7 @@
#include "settingsentrymodel.h" #include "settingsentrymodel.h"
#include <QtCore/QRegularExpression> #include <QtCore/QRegularExpression>
#include <QtMvvmCore/CoreApp>
using namespace QtMvvm; using namespace QtMvvm;
@ -77,7 +78,7 @@ QVariant SettingsEntryModel::data(const QModelIndex &index, int role) const
case DelegateUrlRole: case DelegateUrlRole:
return entry.delegateUrl; return entry.delegateUrl;
case SettingsValueRole: case SettingsValueRole:
return _viewModel->loadValue(entry.key, entry.defaultValue); return readValue(entry);
case PropertiesRole: case PropertiesRole:
return entry.properties; return entry.properties;
case GroupRole: case GroupRole:
@ -92,7 +93,7 @@ QVariant SettingsEntryModel::data(const QModelIndex &index, int role) const
else if(preview.type() == QVariant::String) { else if(preview.type() == QVariant::String) {
return _factory->format(entry.type, return _factory->format(entry.type,
preview.toString(), preview.toString(),
_viewModel->loadValue(entry.key, entry.defaultValue), readValue(entry),
entry.properties); entry.properties);
} }
} }
@ -154,6 +155,11 @@ void SettingsEntryModel::entryChanged(const QString &key)
} }
} }
QVariant SettingsEntryModel::readValue(const SettingsElements::Entry &entry) const
{
return CoreApp::safeCastInputType(entry.type, _viewModel->loadValue(entry.key, entry.defaultValue));
}
SettingsEntryModel::EntryInfo::EntryInfo(SettingsElements::Entry entry, QUrl delegateUrl, SettingsElements::Group group) : SettingsEntryModel::EntryInfo::EntryInfo(SettingsElements::Entry entry, QUrl delegateUrl, SettingsElements::Group group) :

2
src/imports/mvvmquick/settingsentrymodel.h

@ -57,6 +57,8 @@ private:
SettingsViewModel *_viewModel = nullptr; SettingsViewModel *_viewModel = nullptr;
InputViewFactory *_factory = nullptr; InputViewFactory *_factory = nullptr;
QList<EntryInfo> _entries; QList<EntryInfo> _entries;
QVariant readValue(const SettingsElements::Entry &entry) const;
}; };
} }

49
src/mvvmcore/coreapp.cpp

@ -97,6 +97,38 @@ MessageResult *CoreApp::showDialog(const MessageConfig &config)
return result; return result;
} }
QVariant CoreApp::safeCastInputType(const QByteArray &type, const QVariant &value)
{
// get the target type, either explicitly or from the name
auto mType = CoreAppPrivate::dInstance()->inputTypeMapping.value(type, QMetaType::UnknownType);
if(mType == QMetaType::UnknownType) {
mType = QMetaType::type(type.constData());
if(mType == QMetaType::UnknownType) { // no type found -> do nothing but warn
logWarning() << "Input type" << type << "has no know C++ type to convert it to. Use QtMvvm::CoreApp::registerInputTypeMapping to add it."
<< "To surpress this warning without performing a conversion, register it as `CoreApp::registerInputTypeMapping<QVariant>(" << type << ");`";
return value;
}
}
// target type is QVariant -> return without converting
if(mType == QMetaType::QVariant)
return value;
auto convValue = value;
if(!convValue.convert(mType)) {
logWarning() << "Failed to convert data for input type" << type
<< "from given type" << value.typeName()
<< "to the target type" << QMetaType::typeName(mType);
return value;
} else
return convValue;
}
void CoreApp::registerInputTypeMapping(const QByteArray &type, int targetType)
{
CoreAppPrivate::dInstance()->inputTypeMapping.insert(type, targetType);
}
void CoreApp::bootApp() void CoreApp::bootApp()
{ {
if(!d->presenter) { if(!d->presenter) {
@ -181,6 +213,23 @@ void CoreApp::showImp(const QMetaObject *metaObject, const QVariantHash &params,
bool CoreAppPrivate::bootEnabled = true; bool CoreAppPrivate::bootEnabled = true;
QPointer<CoreApp> CoreAppPrivate::instance = nullptr; QPointer<CoreApp> CoreAppPrivate::instance = nullptr;
CoreAppPrivate::CoreAppPrivate() :
inputTypeMapping{
{"switch", QMetaType::Bool},
{"string", QMetaType::QString},
{"number", QMetaType::Double},
{"range", QMetaType::Int},
{"date", QMetaType::QDateTime},
{"color", QMetaType::QColor},
{"url", QMetaType::QUrl},
{"var", QMetaType::QVariant},
{"variant", QMetaType::QVariant},
{"selection", QMetaType::QVariant},
{"list", QMetaType::QVariant},
{"radiolist", QMetaType::QVariant},
}
{}
QScopedPointer<CoreAppPrivate> &CoreAppPrivate::dInstance() QScopedPointer<CoreAppPrivate> &CoreAppPrivate::dInstance()
{ {
return instance->d; return instance->d;

11
src/mvvmcore/coreapp.h

@ -47,6 +47,11 @@ public:
//! Show a basic dialog //! Show a basic dialog
static MessageResult *showDialog(const MessageConfig &config); static MessageResult *showDialog(const MessageConfig &config);
static QVariant safeCastInputType(const QByteArray &type, const QVariant &value);
static void registerInputTypeMapping(const QByteArray &type, int targetType);
template <typename T>
static void registerInputTypeMapping(const QByteArray &type);
public Q_SLOTS: public Q_SLOTS:
//! Boots up the app and starts the mvvm presenting //! Boots up the app and starts the mvvm presenting
void bootApp(); void bootApp();
@ -81,6 +86,12 @@ inline void CoreApp::show(const QVariantHash &params, QPointer<ViewModel> parent
showImp(&TViewModel::staticMetaObject, params, std::move(parentViewModel)); showImp(&TViewModel::staticMetaObject, params, std::move(parentViewModel));
} }
template<typename T>
void CoreApp::registerInputTypeMapping(const QByteArray &type)
{
registerInputTypeMapping(type, qMetaTypeId<T>());
}
} }
//! Registers you custom CoreApp class as CoreApp to be used //! Registers you custom CoreApp class as CoreApp to be used

3
src/mvvmcore/coreapp_p.h

@ -14,6 +14,8 @@ class Q_MVVMCORE_EXPORT CoreAppPrivate : public QObject
friend class QtMvvm::CoreApp; friend class QtMvvm::CoreApp;
public: public:
CoreAppPrivate();
static QScopedPointer<CoreAppPrivate> &dInstance(); static QScopedPointer<CoreAppPrivate> &dInstance();
public Q_SLOTS: public Q_SLOTS:
@ -28,6 +30,7 @@ private:
static QPointer<CoreApp> instance; static QPointer<CoreApp> instance;
IPresenter *presenter = nullptr; IPresenter *presenter = nullptr;
QHash<QByteArray, int> inputTypeMapping;
QHash<const QMetaObject*, QPointer<ViewModel>> singleInstances; QHash<const QMetaObject*, QPointer<ViewModel>> singleInstances;
bool isSingleton(const QMetaObject *metaObject) const; bool isSingleton(const QMetaObject *metaObject) const;

5
src/mvvmcore/message.cpp

@ -76,7 +76,10 @@ QHash<MessageConfig::StandardButton, QString> MessageConfig::buttonTexts() const
QVariant MessageConfig::defaultValue() const QVariant MessageConfig::defaultValue() const
{ {
return d->defaultValue; if(d->type == MessageConfig::TypeInputDialog)
return CoreApp::safeCastInputType(d->subType, d->defaultValue);
else
return d->defaultValue;
} }
QVariantMap MessageConfig::viewProperties() const QVariantMap MessageConfig::viewProperties() const

11
src/mvvmquick/BoolDelegate.qml

@ -8,16 +8,9 @@ CheckDelegate {
text: title text: title
function asBool(value) { checked: inputValue
if(typeof value == "string")
return value === "true";
else
return Boolean(value);
}
checked: asBool(inputValue)
onCheckedChanged: { onCheckedChanged: {
if(asBool(inputValue) !== checked) if(inputValue !== checked)
inputValue = checked; inputValue = checked;
} }

13
src/mvvmquick/DateEdit.qml

@ -3,16 +3,5 @@ import de.skycoder42.QtMvvm.Quick 1.1 as QtMvvm
QtMvvm.DateEdit { QtMvvm.DateEdit {
id: _edit id: _edit
property alias inputValue: _edit.date
property var inputValue: new Date()
onInputValueChanged: {
var realDate = typeof inputValue == "string" ? new Date(inputValue + "T00:00:00") : inputValue
if(realDate.getDate() !== currentDate.getDate() ||
realDate.getMonth() !== currentDate.getMonth() ||
realDate.getYear() !== currentDate.getYear()) {
currentDate = realDate
}
}
onCurrentDateChanged: inputValue = currentDate
} }

24
src/mvvmquick/DateTimeEdit.qml

@ -10,23 +10,23 @@ ColumnLayout {
property var inputValue: new Date() property var inputValue: new Date()
readonly property date combinedDate: new Date(_dateEdit.currentDate.getFullYear(), readonly property date combinedDate: new Date(_dateEdit.date.getFullYear(),
_dateEdit.currentDate.getMonth(), _dateEdit.date.getMonth(),
_dateEdit.currentDate.getDate(), _dateEdit.date.getDate(),
_timeEdit.time.getHours(), _timeEdit.time.getHours(),
_timeEdit.time.getMinutes(), _timeEdit.time.getMinutes(),
_timeEdit.time.getSeconds()) _timeEdit.time.getSeconds())
onInputValueChanged: { onInputValueChanged: {
var realDate = typeof inputValue == "string" ? new Date(inputValue) : inputValue var newDate = inputValue;
if(realDate.getYear() !== combinedDate.getYear() || if(newDate.getYear() !== combinedDate.getYear() ||
realDate.getMonth() !== combinedDate.getMonth() || newDate.getMonth() !== combinedDate.getMonth() ||
realDate.getDate() !== combinedDate.getDate() || newDate.getDate() !== combinedDate.getDate() ||
realDate.getHours() !== combinedDate.getHours() || newDate.getHours() !== combinedDate.getHours() ||
realDate.getMinutes() !== combinedDate.getMinutes() || newDate.getMinutes() !== combinedDate.getMinutes() ||
realDate.getSeconds() !== combinedDate.getSeconds()) { newDate.getSeconds() !== combinedDate.getSeconds()) {
_dateEdit.currentDate = realDate; _dateEdit.date = newDate;
_timeEdit.time = realDate; _timeEdit.time = newDate;
} }
} }
onCombinedDateChanged: { onCombinedDateChanged: {

11
src/mvvmquick/SwitchDelegate.qml

@ -8,16 +8,9 @@ Controls.SwitchDelegate {
text: title text: title
function asBool(value) { checked: inputValue
if(typeof value == "string")
return value === "true";
else
return Boolean(value);
}
checked: asBool(inputValue)
onCheckedChanged: { onCheckedChanged: {
if(asBool(inputValue) !== checked) if(inputValue !== checked)
inputValue = checked; inputValue = checked;
} }

13
src/mvvmquick/TimeEdit.qml

@ -3,16 +3,5 @@ import de.skycoder42.QtMvvm.Quick 1.1 as QtMvvm
QtMvvm.TimeEdit { QtMvvm.TimeEdit {
id: _edit id: _edit
property alias inputValue: _edit.time
property var inputValue: new Date()
onInputValueChanged: {
var realTime = typeof inputValue == "string" ? new Date("2000-01-01T" + inputValue) : inputValue
if(realTime.getHours() !== time.getHours() ||
realTime.getMinutes() !== time.getMinutes() ||
realTime.getSeconds() !== time.getSeconds()) {
time = realTime
}
}
onTimeChanged: inputValue = time
} }

1
src/mvvmquick/inputviewfactory.cpp

@ -123,6 +123,7 @@ InputViewFactoryPrivate::InputViewFactoryPrivate() :
{"color", QStringLiteral("qrc:/qtmvvm/inputs/ColorEdit.qml")}, {"color", QStringLiteral("qrc:/qtmvvm/inputs/ColorEdit.qml")},
{QMetaType::typeName(QMetaType::QFont), QStringLiteral("qrc:/qtmvvm/inputs/FontEdit.qml")}, {QMetaType::typeName(QMetaType::QFont), QStringLiteral("qrc:/qtmvvm/inputs/FontEdit.qml")},
{QMetaType::typeName(QMetaType::QUrl), QStringLiteral("qrc:/qtmvvm/inputs/UrlField.qml")}, {QMetaType::typeName(QMetaType::QUrl), QStringLiteral("qrc:/qtmvvm/inputs/UrlField.qml")},
{"url", QStringLiteral("qrc:/qtmvvm/inputs/UrlField.qml")},
{"selection", QStringLiteral("qrc:/qtmvvm/inputs/ListEdit.qml")}, {"selection", QStringLiteral("qrc:/qtmvvm/inputs/ListEdit.qml")},
{"list", QStringLiteral("qrc:/qtmvvm/inputs/ListEdit.qml")}, {"list", QStringLiteral("qrc:/qtmvvm/inputs/ListEdit.qml")},
{"radiolist", QStringLiteral("qrc:/qtmvvm/inputs/RadioListEdit.qml")} {"radiolist", QStringLiteral("qrc:/qtmvvm/inputs/RadioListEdit.qml")}

2
src/mvvmquick/inputviewfactory.h

@ -68,7 +68,7 @@ public:
template <typename TType> template <typename TType>
inline void addFormatter(Formatter *formatter); inline void addFormatter(Formatter *formatter);
void addFormatter(const QByteArray &type, Formatter *formatter); void addFormatter(const QByteArray &type, Formatter *formatter); //MAJOR make virtual
//! Adds a type name alias for views //! Adds a type name alias for views
template <typename TAliasType, typename TTargetType> template <typename TAliasType, typename TTargetType>

2
src/mvvmwidgets/inputwidgetfactory.cpp

@ -79,7 +79,7 @@ QWidget *InputWidgetFactory::createInput(const QByteArray &type, QWidget *parent
} else if(type == "selection" || type == "list") } else if(type == "selection" || type == "list")
widget = new SelectComboBox(parent); widget = new SelectComboBox(parent);
else else
throw PresenterException("Unable to find an input view for type" + type); throw PresenterException("Unable to find an input view for type: " + type);
for(auto it = viewProperties.constBegin(); it != viewProperties.constEnd(); it++) for(auto it = viewProperties.constBegin(); it != viewProperties.constEnd(); it++)
widget->setProperty(qUtf8Printable(it.key()), it.value()); widget->setProperty(qUtf8Printable(it.key()), it.value());

10
src/mvvmwidgets/settingsdialog.cpp

@ -11,6 +11,7 @@
#include <QtWidgets/QPushButton> #include <QtWidgets/QPushButton>
#include <QtWidgets/QScrollArea> #include <QtWidgets/QScrollArea>
#include <QtMvvmCore/CoreApp>
#include <QtMvvmCore/private/qtmvvm_logging_p.h> #include <QtMvvmCore/private/qtmvvm_logging_p.h>
using namespace QtMvvm; using namespace QtMvvm;
@ -115,7 +116,7 @@ void SettingsDialogPrivate::entryChanged(const QString &key)
if(!content) if(!content)
return; return;
auto info = entryMap.value(content); auto info = entryMap.value(content);
info.second.write(content, viewModel->loadValue(info.first.key, info.first.defaultValue)); info.second.write(content, readValue(info.first));
} }
void SettingsDialogPrivate::createCategory(const SettingsElements::Category &category) void SettingsDialogPrivate::createCategory(const SettingsElements::Category &category)
@ -200,7 +201,7 @@ void SettingsDialogPrivate::createEntry(const SettingsElements::Entry &entry, QW
auto widgetFactory = WidgetsPresenterPrivate::currentPresenter()->inputWidgetFactory(); auto widgetFactory = WidgetsPresenterPrivate::currentPresenter()->inputWidgetFactory();
content = widgetFactory->createInput(entry.type, sectionWidget, entry.properties); content = widgetFactory->createInput(entry.type, sectionWidget, entry.properties);
auto property = content->metaObject()->userProperty(); auto property = content->metaObject()->userProperty();
property.write(content, viewModel->loadValue(entry.key, entry.defaultValue)); property.write(content, readValue(entry));
if(property.hasNotifySignal()) { if(property.hasNotifySignal()) {
auto changedSlot = metaObject()->method(metaObject()->indexOfSlot("propertyChanged()")); auto changedSlot = metaObject()->method(metaObject()->indexOfSlot("propertyChanged()"));
connect(content, property.notifySignal(), connect(content, property.notifySignal(),
@ -416,6 +417,11 @@ bool SettingsDialogPrivate::searchInEntry(const QRegularExpression &regex, QLabe
return false; return false;
} }
QVariant SettingsDialogPrivate::readValue(const SettingsElements::Entry &entry) const
{
return CoreApp::safeCastInputType(entry.type, viewModel->loadValue(entry.key, entry.defaultValue));
}
void SettingsDialogPrivate::propertyChanged() void SettingsDialogPrivate::propertyChanged()
{ {
auto widget = qobject_cast<QWidget*>(sender()); auto widget = qobject_cast<QWidget*>(sender());

2
src/mvvmwidgets/settingsdialog_p.h

@ -81,6 +81,8 @@ public:
bool searchInGroup(const QRegularExpression &regex, QGroupBox *groupWidget); bool searchInGroup(const QRegularExpression &regex, QGroupBox *groupWidget);
bool searchInEntry(const QRegularExpression &regex, QLabel *label, QWidget *content); bool searchInEntry(const QRegularExpression &regex, QLabel *label, QWidget *content);
QVariant readValue(const SettingsElements::Entry &entry) const;
public Q_SLOTS: public Q_SLOTS:
void createUi(); void createUi();
void entryChanged(const QString &key); void entryChanged(const QString &key);

1
src/mvvmwidgets/widgetspresenter.cpp

@ -20,6 +20,7 @@
#include <QtWidgets/QLabel> #include <QtWidgets/QLabel>
#include <QtWidgets/QColorDialog> #include <QtWidgets/QColorDialog>
#include <QtMvvmCore/CoreApp>
#include <QtMvvmCore/private/qtmvvm_logging_p.h> #include <QtMvvmCore/private/qtmvvm_logging_p.h>
#include <dialogmaster.h> #include <dialogmaster.h>

Loading…
Cancel
Save