From e6bbae3eaf021a4d326d4ad5b310429ebec6cf15 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Tue, 10 Nov 2015 20:16:30 +0100 Subject: [PATCH] Added AutoForm class for automatic binding. --- accommodation/accform.cpp | 14 ++++++++ core/autoform.h | 71 +++++++++++++++++++++++++++++++++++++++ core/core.pro | 3 +- 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 accommodation/accform.cpp create mode 100644 core/autoform.h diff --git a/accommodation/accform.cpp b/accommodation/accform.cpp new file mode 100644 index 0000000..66e90de --- /dev/null +++ b/accommodation/accform.cpp @@ -0,0 +1,14 @@ +#include "accform.h" +#include "ui_accform.h" + +AccForm::AccForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::AccForm) +{ + ui->setupUi(this); +} + +AccForm::~AccForm() +{ + delete ui; +} diff --git a/core/autoform.h b/core/autoform.h new file mode 100644 index 0000000..c985615 --- /dev/null +++ b/core/autoform.h @@ -0,0 +1,71 @@ +#ifndef AUTOFORM_H +#define AUTOFORM_H + +#include +#include +#include +#include +#include +#include + +#include "service.h" + +template +class AutoForm : public QWidget +{ +public: + explicit AutoForm(QWidget *parent = 0) : QWidget(parent) { + m_newRec = false; + } + + virtual ~AutoForm() {} + + void setEntity(QSharedPointer entity) { + m_entity = entity; + bindToUi(); + } + + void registerBinding(QWidget *widget) { + if (!m_bindWidgets.contains(widget)) { + m_bindWidgets.append(widget); + } + } + +private: + QSharedPointer m_entity; + QList m_bindWidgets; + bool m_newRec; + + void bindToUi() { + foreach (QWidget *widget, m_bindWidgets) { + const char* prop = widget->metaObject()->userProperty().name(); + widget->setProperty(prop, ((QObject*)m_entity.data())->property(widget->objectName().toStdString().c_str())); + } + } + + void bindToData() { + foreach (QWidget *widget, m_bindWidgets) { + const char* prop = widget->metaObject()->userProperty().name(); + ((QObject*)m_entity.data())->setProperty(widget->objectName().toStdString().c_str(), widget->property(prop)); + } + } + +signals: + void recordAdded(QSharedPointer entity); + void recordUpdated(QSharedPointer entity); + +public slots: + void saveRecord() { + bindToData(); + Service service; + service.save(m_entity); + + if (m_newRec) { + emit recordAdded(m_entity); + } else { + emit recordUpdated(m_entity); + } + } +}; + +#endif // AUTOFORM_H diff --git a/core/core.pro b/core/core.pro index 514611c..ff3294f 100644 --- a/core/core.pro +++ b/core/core.pro @@ -23,7 +23,8 @@ HEADERS += core.h\ data/user.h \ context.h \ imetadataplugin.h \ - autotablemodel.h + autotablemodel.h \ + autoform.h unix { target.path = /usr/lib