|
|
|
#ifndef AUTOFORM_H
|
|
|
|
#define AUTOFORM_H
|
|
|
|
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QList>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QSharedPointer>
|
|
|
|
#include <QMetaMethod>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QComboBox>
|
|
|
|
|
|
|
|
#include "iform.h"
|
|
|
|
#include "service.h"
|
|
|
|
#include "ivalidator.h"
|
|
|
|
#include "combodata.h"
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class AutoForm : public IForm
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit AutoForm(QWidget *parent = 0) : IForm(parent) {
|
|
|
|
m_newRec = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~AutoForm() {
|
|
|
|
foreach (IValidator *val, m_validators) {
|
|
|
|
delete val;
|
|
|
|
}
|
|
|
|
m_validators.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setEntity(QSharedPointer<T> entity) {
|
|
|
|
m_entity = entity;
|
|
|
|
bindToUi();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSharedPointer<T> entity() {
|
|
|
|
return m_entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setNewRec(bool isNew) {
|
|
|
|
m_newRec = isNew;
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerBinding(QWidget *widget) {
|
|
|
|
if (!m_bindWidgets.contains(widget)) {
|
|
|
|
m_bindWidgets.append(widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerBinding(QComboBox *combo, const QList<ComboData> &values) {
|
|
|
|
m_bindCombos[combo] = values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerValidator(IValidator *validator) {
|
|
|
|
m_validators.append(validator);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void bindOtherToUi() {}
|
|
|
|
virtual bool bindOtherToData() { return true; }
|
|
|
|
virtual void registerCombos() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QSharedPointer<T> m_entity;
|
|
|
|
QList<QWidget*> m_bindWidgets;
|
|
|
|
QList<IValidator*> m_validators;
|
|
|
|
QHash<QComboBox*, QList<ComboData> > m_bindCombos;
|
|
|
|
bool m_newRec;
|
|
|
|
|
|
|
|
void bindToUi() {
|
|
|
|
registerCombos();
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
((QObject*)m_entity.data())->setProperty(widget->objectName().toStdString().c_str(), widget->property(prop));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (QComboBox *combo, m_bindCombos.keys()) {
|
|
|
|
((QObject*)m_entity.data())->setProperty(combo->objectName().toStdString().c_str(), combo->currentData());
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindOtherToData();
|
|
|
|
}
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
bool saveRecord() {
|
|
|
|
if (!bindToData())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_newRec) {
|
|
|
|
emit recordAdded();
|
|
|
|
} else {
|
|
|
|
emit recordUpdated();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // AUTOFORM_H
|