Database error handling.
This commit is contained in:
+50
-5
@@ -8,10 +8,13 @@
|
||||
#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>
|
||||
@@ -20,6 +23,8 @@ class AutoForm : public IForm
|
||||
public:
|
||||
explicit AutoForm(QWidget *parent = 0) : IForm(parent) {
|
||||
m_newRec = false;
|
||||
m_serviceConnected = false;
|
||||
m_saved = false;
|
||||
}
|
||||
|
||||
virtual ~AutoForm() {
|
||||
@@ -93,14 +98,54 @@ public slots:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_newRec) {
|
||||
emit recordAdded();
|
||||
} else {
|
||||
emit recordUpdated();
|
||||
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;
|
||||
}
|
||||
|
||||
return 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
|
||||
|
||||
Reference in New Issue
Block a user