Conflicts:
	core/core.pro
print
Zdenek Jonak 9 years ago
commit 1439502c20

@ -9,6 +9,7 @@
#include <QDebug>
#include "service.h"
#include "ivalidator.h"
template <class T>
class AutoForm : public QWidget
@ -18,7 +19,12 @@ public:
m_newRec = false;
}
virtual ~AutoForm() {}
virtual ~AutoForm() {
foreach (IValidator *val, m_validators) {
delete val;
m_validators.clear();
}
}
void setEntity(QSharedPointer<T> entity) {
m_entity = entity;
@ -31,9 +37,14 @@ public:
}
}
void registerValidator(IValidator *validator) {
m_validators.append(validator);
}
private:
QSharedPointer<T> m_entity;
QList<QWidget*> m_bindWidgets;
QList<IValidator*> m_validators;
bool m_newRec;
void bindToUi() {
@ -43,20 +54,34 @@ private:
}
}
void bindToData() {
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 true;
}
signals:
void recordAdded(QSharedPointer<T> entity);
void recordUpdated(QSharedPointer<T> entity);
void validationError(QString errMessage);
public slots:
void saveRecord() {
bindToData();
bool saveRecord() {
if (!bindToData())
{
return false;
}
Service<T> service;
service.save(m_entity);
@ -65,6 +90,8 @@ public slots:
} else {
emit recordUpdated(m_entity);
}
return true;
}
};

@ -16,6 +16,7 @@ SOURCES += \
context.cpp \
imetadataplugin.cpp \
transaction.cpp \
emptystringvalidator.cpp
data/role.cpp \
data/permission.cpp
@ -29,6 +30,8 @@ HEADERS += core.h\
autotablemodel.h \
autoform.h \
transaction.h \
ivalidator.h \
emptystringvalidator.h
data/role.h \
data/permission.h

@ -0,0 +1,22 @@
#include "emptystringvalidator.h"
#include <QMetaObject>
#include <QMetaProperty>
EmptyStringValidator::EmptyStringValidator(QWidget *widget, const QString &errMessage)
:IValidator(widget, errMessage)
{
}
bool EmptyStringValidator::validate()
{
QString data = m_widget->property(m_widget->metaObject()->userProperty().name()).toString();
if (data.isEmpty())
{
m_widget->setFocus();
return false;
}
return true;
}

@ -0,0 +1,17 @@
#ifndef EMPTYSTRINGVALIDATOR_H
#define EMPTYSTRINGVALIDATOR_H
#include <QString>
#include "ivalidator.h"
class EmptyStringValidator : public IValidator
{
public:
EmptyStringValidator(QWidget *widget, const QString &errMessage);
// IValidator interface
public:
bool validate();
};
#endif // EMPTYSTRINGVALIDATOR_H

@ -0,0 +1,31 @@
#ifndef IVALIDATOR_H
#define IVALIDATOR_H
#include <QWidget>
#include <QString>
class IValidator
{
public:
IValidator(QWidget *widget, const QString &errMessage)
{
m_widget = widget;
m_errMessage = errMessage;
}
virtual bool validate() = 0;
QString errMessage()
{
return m_errMessage;
}
protected:
QWidget *m_widget;
private:
QString m_errMessage;
};
#endif // IVALIDATOR_H
Loading…
Cancel
Save