From 049314a2455df4748c569c9034bd0a6eb14b3ebb Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Wed, 13 Apr 2016 21:34:49 +0200 Subject: [PATCH] Added class ObjectBinder for common data binding. --- core/core.pro | 6 ++- core/formbinder.h | 88 +++++++--------------------------------- core/objectbinder.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++ core/objectbinder.h | 37 +++++++++++++++++ shop/shopservice.cpp | 6 +++ shop/shopservice.h | 11 +++++ 6 files changed, 165 insertions(+), 76 deletions(-) create mode 100644 core/objectbinder.cpp create mode 100644 core/objectbinder.h create mode 100644 shop/shopservice.cpp create mode 100644 shop/shopservice.h diff --git a/core/core.pro b/core/core.pro index 02834c2..c9a1656 100644 --- a/core/core.pro +++ b/core/core.pro @@ -51,7 +51,8 @@ SOURCES += \ settings/globalsettings.cpp \ settingsform.cpp \ settings/globalsettingsform.cpp \ - permissionevaluator.cpp + permissionevaluator.cpp \ + objectbinder.cpp HEADERS += core.h\ core_global.h \ @@ -102,7 +103,8 @@ HEADERS += core.h\ settingsform.h \ settings/globalsettingsform.h \ formbinder.h \ - permissionevaluator.h + permissionevaluator.h \ + objectbinder.h unix { target.path = /usr/lib diff --git a/core/formbinder.h b/core/formbinder.h index dcac7a9..79614fb 100644 --- a/core/formbinder.h +++ b/core/formbinder.h @@ -11,6 +11,7 @@ #include "combodata.h" #include "ivalidator.h" #include "iform.h" +#include "objectbinder.h" #include "../qdecimal/src/QDecDouble.hh" @@ -19,31 +20,31 @@ class FormBinder : public IForm { public: - explicit FormBinder(QWidget *parent = NULL) : IForm(parent) {} + explicit FormBinder(QWidget *parent = NULL) : IForm(parent) { + connect(&m_binder, &ObjectBinder::validationError, [this](QString msg){ + emit this->validationError(msg); + }); + } virtual ~FormBinder() { - foreach (IValidator *val, m_validators) { - delete val; - } - m_validators.clear(); + } void registerBinding(QWidget *widget) { - if (!m_bindWidgets.contains(widget)) { - m_bindWidgets.append(widget); - } + m_binder.registerBinding(widget); } void registerBinding(QComboBox *combo, const QList &values) { - m_bindCombos[combo] = values; + m_binder.registerBinding(combo, values); } void registerValidator(IValidator *validator) { - m_validators.append(validator); + m_binder.registerValidator(validator); } void setEntity(QSharedPointer entity) { m_entity = entity; + m_binder.setData(m_entity.data()); bindToUi(); } @@ -60,77 +61,16 @@ protected: void bindToUi() { registerCombos(); - foreach (QWidget *widget, m_bindWidgets) { - const char* prop = widget->metaObject()->userProperty().name(); - QVariant value = ((QObject*)m_entity.data())->property(widget->objectName().toStdString().c_str()); - if (value.canConvert()) - { - widget->setProperty(prop, value.value().toString()); - } - else - { - widget->setProperty(prop, value); - } - } - - foreach (QComboBox *combo, m_bindCombos.keys()) { - int idx = 0; - QVariant field = ((QObject*)m_entity.data())->property(combo->objectName().toStdString().c_str()); - - combo->clear(); - for (int i = 0; i < m_bindCombos[combo].size(); i++) { - ComboData data = m_bindCombos[combo][i]; - combo->addItem(data.label(), data.index()); - - if (data.index().canConvert()) { - ComboItem* ci = qobject_cast(data.index().value()); - ComboItem* ciField = qobject_cast(field.value()); - if (ci->eq(ciField)) { - idx = i; - } - } - else if (field == data.index()) { - idx = i; - } - } - - combo->setCurrentIndex(idx); - } - + m_binder.bindToUi(); bindOtherToUi(); } bool bindToData() { - foreach (IValidator *val, m_validators) { - if (!val->validate()) { - emit validationError(val->errMessage()); - return false; - } - } - - foreach (QWidget *widget, m_bindWidgets) { - const char* prop = widget->metaObject()->userProperty().name(); - - QVariant val = widget->property(prop); - if (((QObject*)m_entity.data())->property(widget->objectName().toStdString().c_str()).canConvert()) - { - QDecDouble dec(val.toDouble()); - val = QVariant::fromValue(dec); - } - ((QObject*)m_entity.data())->setProperty(widget->objectName().toStdString().c_str(), val); - } - - foreach (QComboBox *combo, m_bindCombos.keys()) { - ((QObject*)m_entity.data())->setProperty(combo->objectName().toStdString().c_str(), combo->currentData()); - } - - return bindOtherToData(); + return m_binder.bindToData() && bindOtherToData(); } private: - QList m_bindWidgets; - QHash > m_bindCombos; - QList m_validators; + ObjectBinder m_binder; }; #endif // FORMBINDER_H diff --git a/core/objectbinder.cpp b/core/objectbinder.cpp new file mode 100644 index 0000000..655df8b --- /dev/null +++ b/core/objectbinder.cpp @@ -0,0 +1,93 @@ +#include "objectbinder.h" +#include + +ObjectBinder::ObjectBinder(QObject *parent) + :QObject(parent) +{ + m_data = NULL; +} + +void ObjectBinder::registerBinding(QWidget *widget) { + if (!m_bindWidgets.contains(widget)) { + m_bindWidgets.append(widget); + } +} + +void ObjectBinder::registerBinding(QComboBox *combo, const QList &values) { + m_bindCombos[combo] = values; +} + +void ObjectBinder::registerValidator(IValidator *validator) { + m_validators.append(validator); +} + +void ObjectBinder::setData(QObject *data) +{ + m_data = data; +} + +void ObjectBinder::bindToUi() { + foreach (QWidget *widget, m_bindWidgets) { + const char* prop = widget->metaObject()->userProperty().name(); + QVariant value = m_data->property(widget->objectName().toStdString().c_str()); + if (value.canConvert()) + { + widget->setProperty(prop, value.value().toString()); + } + else + { + widget->setProperty(prop, value); + } + } + + foreach (QComboBox *combo, m_bindCombos.keys()) { + int idx = 0; + QVariant field = m_data->property(combo->objectName().toStdString().c_str()); + + combo->clear(); + for (int i = 0; i < m_bindCombos[combo].size(); i++) { + ComboData data = m_bindCombos[combo][i]; + combo->addItem(data.label(), data.index()); + + if (data.index().canConvert()) { + ComboItem* ci = qobject_cast(data.index().value()); + ComboItem* ciField = qobject_cast(field.value()); + if (ci->eq(ciField)) { + idx = i; + } + } + else if (field == data.index()) { + idx = i; + } + } + + combo->setCurrentIndex(idx); + } +} + +bool ObjectBinder::bindToData() { + foreach (IValidator *val, m_validators) { + if (!val->validate()) { + emit validationError(val->errMessage()); + return false; + } + } + + foreach (QWidget *widget, m_bindWidgets) { + const char* prop = widget->metaObject()->userProperty().name(); + + QVariant val = widget->property(prop); + if (m_data->property(widget->objectName().toStdString().c_str()).canConvert()) + { + QDecDouble dec(val.toDouble()); + val = QVariant::fromValue(dec); + } + m_data->setProperty(widget->objectName().toStdString().c_str(), val); + } + + foreach (QComboBox *combo, m_bindCombos.keys()) { + m_data->setProperty(combo->objectName().toStdString().c_str(), combo->currentData()); + } + + return true; +} diff --git a/core/objectbinder.h b/core/objectbinder.h new file mode 100644 index 0000000..e752909 --- /dev/null +++ b/core/objectbinder.h @@ -0,0 +1,37 @@ +#ifndef OBJECTBINDER_H +#define OBJECTBINDER_H + +#include +#include +#include +#include +#include +#include "ivalidator.h" +#include "combodata.h" +#include "core_global.h" + +class CORESHARED_EXPORT ObjectBinder : public QObject +{ + Q_OBJECT + +public: + explicit ObjectBinder(QObject *parent = NULL); + + void registerBinding(QWidget *widget); + void registerBinding(QComboBox *combo, const QList &values); + void registerValidator(IValidator *validator); + void setData(QObject *data); + void bindToUi(); + bool bindToData(); + +signals: + void validationError(QString msg); + +private: + QList m_bindWidgets; + QHash > m_bindCombos; + QList m_validators; + QObject *m_data; +}; + +#endif // OBJECTBINDER_H diff --git a/shop/shopservice.cpp b/shop/shopservice.cpp new file mode 100644 index 0000000..11a1d1f --- /dev/null +++ b/shop/shopservice.cpp @@ -0,0 +1,6 @@ +#include "shopservice.h" + +ShopService::ShopService() +{ + +} diff --git a/shop/shopservice.h b/shop/shopservice.h new file mode 100644 index 0000000..3d406e9 --- /dev/null +++ b/shop/shopservice.h @@ -0,0 +1,11 @@ +#ifndef SHOPSERVICE_H +#define SHOPSERVICE_H + + +class ShopService +{ +public: + ShopService(); +}; + +#endif // SHOPSERVICE_H \ No newline at end of file