Added AutoForm class for automatic binding.
parent
36f9c93e08
commit
e6bbae3eaf
@ -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;
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
#ifndef AUTOFORM_H
|
||||
#define AUTOFORM_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QList>
|
||||
#include <QLineEdit>
|
||||
#include <QSharedPointer>
|
||||
#include <QMetaMethod>
|
||||
#include <QDebug>
|
||||
|
||||
#include "service.h"
|
||||
|
||||
template <class T>
|
||||
class AutoForm : public QWidget
|
||||
{
|
||||
public:
|
||||
explicit AutoForm(QWidget *parent = 0) : QWidget(parent) {
|
||||
m_newRec = false;
|
||||
}
|
||||
|
||||
virtual ~AutoForm() {}
|
||||
|
||||
void setEntity(QSharedPointer<T> entity) {
|
||||
m_entity = entity;
|
||||
bindToUi();
|
||||
}
|
||||
|
||||
void registerBinding(QWidget *widget) {
|
||||
if (!m_bindWidgets.contains(widget)) {
|
||||
m_bindWidgets.append(widget);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
QSharedPointer<T> m_entity;
|
||||
QList<QWidget*> 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<T> entity);
|
||||
void recordUpdated(QSharedPointer<T> entity);
|
||||
|
||||
public slots:
|
||||
void saveRecord() {
|
||||
bindToData();
|
||||
Service<T> service;
|
||||
service.save(m_entity);
|
||||
|
||||
if (m_newRec) {
|
||||
emit recordAdded(m_entity);
|
||||
} else {
|
||||
emit recordUpdated(m_entity);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif // AUTOFORM_H
|
Loading…
Reference in New Issue