You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
3.5 KiB
C++
152 lines
3.5 KiB
C++
#ifndef AUTOFORM_H
|
|
#define AUTOFORM_H
|
|
|
|
#include <QWidget>
|
|
#include <QList>
|
|
#include <QLineEdit>
|
|
#include <QSharedPointer>
|
|
#include <QMetaMethod>
|
|
#include <QDebug>
|
|
#include <QVariant>
|
|
#include <QMessageBox>
|
|
|
|
#include "iform.h"
|
|
#include "service.h"
|
|
#include "ivalidator.h"
|
|
#include "iservice.h"
|
|
#include "iplugin.h"
|
|
|
|
|
|
template <class T>
|
|
class AutoForm : public IForm
|
|
{
|
|
public:
|
|
explicit AutoForm(QWidget *parent = 0) : IForm(parent) {
|
|
m_newRec = false;
|
|
m_serviceConnected = false;
|
|
m_saved = 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 registerValidator(IValidator *validator) {
|
|
m_validators.append(validator);
|
|
}
|
|
|
|
protected:
|
|
virtual void bindOtherToUi() {}
|
|
virtual bool bindOtherToData() { return true; }
|
|
|
|
private:
|
|
QSharedPointer<T> m_entity;
|
|
QList<QWidget*> m_bindWidgets;
|
|
QList<IValidator*> m_validators;
|
|
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()));
|
|
}
|
|
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));
|
|
}
|
|
|
|
return bindOtherToData();
|
|
}
|
|
|
|
public slots:
|
|
bool saveRecord() {
|
|
if (!bindToData())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!m_serviceConnected)
|
|
{
|
|
connect(service(), &IService::dbError, [this](QString msg) {
|
|
QMessageBox::critical(this, tr("Database error"), tr(msg.toStdString().c_str()));
|
|
m_saved = false;
|
|
});
|
|
connect(service(), &IService::dataChanged, [this]() {
|
|
m_saved = true;
|
|
});
|
|
m_serviceConnected = true;
|
|
}
|
|
|
|
if (m_newRec) {
|
|
service()->save(entity());
|
|
if (m_saved)
|
|
{
|
|
emit recordAdded();
|
|
}
|
|
} else {
|
|
service()->update(entity());
|
|
if (m_saved)
|
|
{
|
|
emit recordUpdated();
|
|
}
|
|
}
|
|
|
|
return m_saved;
|
|
}
|
|
|
|
private:
|
|
Service<T> *service() {
|
|
IPlugin *plugin = Context::instance().plugin(pluginId());
|
|
if (plugin == NULL) {
|
|
Q_ASSERT(false);
|
|
return NULL;
|
|
}
|
|
|
|
Service<T> *service = plugin->service<T>();
|
|
if (service == NULL) {
|
|
Q_ASSERT(false);
|
|
return NULL;
|
|
}
|
|
|
|
return service;
|
|
}
|
|
|
|
bool m_serviceConnected;
|
|
bool m_saved;
|
|
};
|
|
|
|
#endif // AUTOFORM_H
|