Added class ObjectBinder for common data binding.
This commit is contained in:
+4
-2
@@ -51,7 +51,8 @@ SOURCES += \
|
|||||||
settings/globalsettings.cpp \
|
settings/globalsettings.cpp \
|
||||||
settingsform.cpp \
|
settingsform.cpp \
|
||||||
settings/globalsettingsform.cpp \
|
settings/globalsettingsform.cpp \
|
||||||
permissionevaluator.cpp
|
permissionevaluator.cpp \
|
||||||
|
objectbinder.cpp
|
||||||
|
|
||||||
HEADERS += core.h\
|
HEADERS += core.h\
|
||||||
core_global.h \
|
core_global.h \
|
||||||
@@ -102,7 +103,8 @@ HEADERS += core.h\
|
|||||||
settingsform.h \
|
settingsform.h \
|
||||||
settings/globalsettingsform.h \
|
settings/globalsettingsform.h \
|
||||||
formbinder.h \
|
formbinder.h \
|
||||||
permissionevaluator.h
|
permissionevaluator.h \
|
||||||
|
objectbinder.h
|
||||||
|
|
||||||
unix {
|
unix {
|
||||||
target.path = /usr/lib
|
target.path = /usr/lib
|
||||||
|
|||||||
+14
-74
@@ -11,6 +11,7 @@
|
|||||||
#include "combodata.h"
|
#include "combodata.h"
|
||||||
#include "ivalidator.h"
|
#include "ivalidator.h"
|
||||||
#include "iform.h"
|
#include "iform.h"
|
||||||
|
#include "objectbinder.h"
|
||||||
|
|
||||||
#include "../qdecimal/src/QDecDouble.hh"
|
#include "../qdecimal/src/QDecDouble.hh"
|
||||||
|
|
||||||
@@ -19,31 +20,31 @@ class FormBinder : public IForm
|
|||||||
{
|
{
|
||||||
public:
|
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() {
|
virtual ~FormBinder() {
|
||||||
foreach (IValidator *val, m_validators) {
|
|
||||||
delete val;
|
|
||||||
}
|
|
||||||
m_validators.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void registerBinding(QWidget *widget) {
|
void registerBinding(QWidget *widget) {
|
||||||
if (!m_bindWidgets.contains(widget)) {
|
m_binder.registerBinding(widget);
|
||||||
m_bindWidgets.append(widget);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void registerBinding(QComboBox *combo, const QList<ComboData> &values) {
|
void registerBinding(QComboBox *combo, const QList<ComboData> &values) {
|
||||||
m_bindCombos[combo] = values;
|
m_binder.registerBinding(combo, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
void registerValidator(IValidator *validator) {
|
void registerValidator(IValidator *validator) {
|
||||||
m_validators.append(validator);
|
m_binder.registerValidator(validator);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setEntity(QSharedPointer<T> entity) {
|
void setEntity(QSharedPointer<T> entity) {
|
||||||
m_entity = entity;
|
m_entity = entity;
|
||||||
|
m_binder.setData(m_entity.data());
|
||||||
bindToUi();
|
bindToUi();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,77 +61,16 @@ protected:
|
|||||||
|
|
||||||
void bindToUi() {
|
void bindToUi() {
|
||||||
registerCombos();
|
registerCombos();
|
||||||
foreach (QWidget *widget, m_bindWidgets) {
|
m_binder.bindToUi();
|
||||||
const char* prop = widget->metaObject()->userProperty().name();
|
|
||||||
QVariant value = ((QObject*)m_entity.data())->property(widget->objectName().toStdString().c_str());
|
|
||||||
if (value.canConvert<QDecDouble>())
|
|
||||||
{
|
|
||||||
widget->setProperty(prop, value.value<QDecDouble>().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<QObject*>()) {
|
|
||||||
ComboItem* ci = qobject_cast<ComboItem*>(data.index().value<QObject*>());
|
|
||||||
ComboItem* ciField = qobject_cast<ComboItem*>(field.value<QObject*>());
|
|
||||||
if (ci->eq(ciField)) {
|
|
||||||
idx = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (field == data.index()) {
|
|
||||||
idx = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
combo->setCurrentIndex(idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
bindOtherToUi();
|
bindOtherToUi();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool bindToData() {
|
bool bindToData() {
|
||||||
foreach (IValidator *val, m_validators) {
|
return m_binder.bindToData() && bindOtherToData();
|
||||||
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>())
|
|
||||||
{
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QList<QWidget*> m_bindWidgets;
|
ObjectBinder m_binder;
|
||||||
QHash<QComboBox*, QList<ComboData> > m_bindCombos;
|
|
||||||
QList<IValidator*> m_validators;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // FORMBINDER_H
|
#endif // FORMBINDER_H
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
#include "objectbinder.h"
|
||||||
|
#include <QDecDouble.hh>
|
||||||
|
|
||||||
|
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<ComboData> &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<QDecDouble>())
|
||||||
|
{
|
||||||
|
widget->setProperty(prop, value.value<QDecDouble>().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<QObject*>()) {
|
||||||
|
ComboItem* ci = qobject_cast<ComboItem*>(data.index().value<QObject*>());
|
||||||
|
ComboItem* ciField = qobject_cast<ComboItem*>(field.value<QObject*>());
|
||||||
|
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>())
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef OBJECTBINDER_H
|
||||||
|
#define OBJECTBINDER_H
|
||||||
|
|
||||||
|
#include <QList>
|
||||||
|
#include <QWidget>
|
||||||
|
#include <QComboBox>
|
||||||
|
#include <QObject>
|
||||||
|
#include <QMetaProperty>
|
||||||
|
#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<ComboData> &values);
|
||||||
|
void registerValidator(IValidator *validator);
|
||||||
|
void setData(QObject *data);
|
||||||
|
void bindToUi();
|
||||||
|
bool bindToData();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void validationError(QString msg);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QList<QWidget*> m_bindWidgets;
|
||||||
|
QHash<QComboBox*, QList<ComboData> > m_bindCombos;
|
||||||
|
QList<IValidator*> m_validators;
|
||||||
|
QObject *m_data;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // OBJECTBINDER_H
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include "shopservice.h"
|
||||||
|
|
||||||
|
ShopService::ShopService()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef SHOPSERVICE_H
|
||||||
|
#define SHOPSERVICE_H
|
||||||
|
|
||||||
|
|
||||||
|
class ShopService
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ShopService();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SHOPSERVICE_H
|
||||||
Reference in New Issue
Block a user