diff --git a/accommodation/acform.h b/accommodation/acform.h index 441c767..634f3b0 100644 --- a/accommodation/acform.h +++ b/accommodation/acform.h @@ -5,6 +5,7 @@ #include #include "data/person.h" +#include "accommodation-odb.hxx" namespace Ui { class AcForm; diff --git a/accommodation/data/person.cpp b/accommodation/data/person.cpp index e4304b7..3c785a0 100644 --- a/accommodation/data/person.cpp +++ b/accommodation/data/person.cpp @@ -3,14 +3,14 @@ Person::Person() { } -int Person::getId() const +int Person::id() const { - return id; + return m_id; } void Person::setId(int value) { - id = value; + m_id = value; } QString Person::getFirstName() const { diff --git a/accommodation/data/person.h b/accommodation/data/person.h index 015b2f7..bf784fa 100644 --- a/accommodation/data/person.h +++ b/accommodation/data/person.h @@ -16,7 +16,7 @@ class Person : public QObject public: Person(); - int getId() const; + int id() const; void setId(int value); QString getFirstName() const; @@ -28,7 +28,7 @@ public: private: friend class odb::access; #pragma db id auto - int id; + int m_id; QString firstName; QString lastName; diff --git a/core/autoform.h b/core/autoform.h index 0ad1603..fd0bbf3 100644 --- a/core/autoform.h +++ b/core/autoform.h @@ -8,10 +8,13 @@ #include #include #include +#include #include "iform.h" #include "service.h" #include "ivalidator.h" +#include "iservice.h" +#include "iplugin.h" template @@ -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_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) { - emit recordAdded(); + service()->save(entity()); + if (m_saved) + { + emit recordAdded(); + } } else { - emit recordUpdated(); + service()->update(entity()); + if (m_saved) + { + emit recordUpdated(); + } } - return true; + return m_saved; } + +private: + Service *service() { + IPlugin *plugin = Context::instance().plugin(pluginId()); + if (plugin == NULL) { + Q_ASSERT(false); + return NULL; + } + + Service *service = plugin->service(); + if (service == NULL) { + Q_ASSERT(false); + return NULL; + } + + return service; + } + + bool m_serviceConnected; + bool m_saved; }; #endif // AUTOFORM_H diff --git a/core/context.cpp b/core/context.cpp index a28b97c..be095d2 100644 --- a/core/context.cpp +++ b/core/context.cpp @@ -130,6 +130,11 @@ void Context::setCurrentUser(const QSharedPointer ¤tUser) m_currentUser = currentUser; } +odb::session &Context::session() +{ + return m_session; +} + void Context::checkDb(const QString &path) { { diff --git a/core/context.h b/core/context.h index 17b9062..7d28143 100644 --- a/core/context.h +++ b/core/context.h @@ -37,6 +37,8 @@ public: QSharedPointer currentUser() const; void setCurrentUser(const QSharedPointer ¤tUser); + odb::session &session(); + private: Context(); QList m_plugins; diff --git a/core/formdialog.cpp b/core/formdialog.cpp index 731e506..5d36b6e 100644 --- a/core/formdialog.cpp +++ b/core/formdialog.cpp @@ -39,3 +39,9 @@ void FormDialog::accept() QDialog::accept(); } } + +void FormDialog::reject() +{ + m_form->refresh(); + QDialog::reject(); +} diff --git a/core/formdialog.h b/core/formdialog.h index 2cdf9b4..0eaa584 100644 --- a/core/formdialog.h +++ b/core/formdialog.h @@ -31,6 +31,10 @@ private slots: // QDialog interface public slots: void accept(); + + // QDialog interface +public slots: + void reject() override; }; #endif // FORMDIALOG_H diff --git a/core/gridform.h b/core/gridform.h index 953098f..cb65446 100644 --- a/core/gridform.h +++ b/core/gridform.h @@ -10,8 +10,8 @@ #include "autotablemodel.h" #include "context.h" #include "iplugin.h" - #include "igridform.h" +#include "iservice.h" template class GridForm : public IGridForm @@ -20,14 +20,15 @@ class GridForm : public IGridForm public: explicit GridForm(QWidget *parent = 0) : IGridForm(parent) -{ - m_form = NULL; - m_tableModel = NULL; - m_formHandler = new DefaultFormHandler(); + { + m_serviceConnected = false; + m_tableModel = NULL; + m_formHandler = new DefaultFormHandler(); + + m_filterUi = new FilterUi(this, new T); + filterWidget()->layout()->addWidget(m_filterUi); + } - m_filterUi = new FilterUi(this, new T); - filterWidget()->layout()->addWidget(m_filterUi); -} virtual ~GridForm() { if (m_form != NULL && m_form->parent() == NULL) @@ -43,20 +44,26 @@ public: delete m_formHandler; } - void setForm(AutoForm *form) { + void setForm(AutoForm *aForm) { Q_ASSERT(m_form == NULL); - m_form = form; + m_form = aForm; connect(m_form, &IForm::recordAdded, [this](){ - service()->save(m_form->entity()); - m_tableModel->addRow(m_form->entity()); + //service()->save(form()->entity()); + m_tableModel->addRow(form()->entity()); emit dataChanged(); }); connect(m_form, &IForm::recordUpdated, [this](){ - service()->update(m_form->entity()); + //service()->update(form()->entity()); emit dataChanged(); }); + connect(m_form, &IForm::refreshEntity, [this](){ + if (m_tableModel != NULL) { + m_tableModel->setItemToIndex(tableView()->currentIndex(), + service()->reload(m_tableModel->itemFromIndex(tableView()->currentIndex())->id())); + } + }); } void setTableModel(AutoTableModel *tableModel) { @@ -79,6 +86,8 @@ public slots: return; } + connectService(); + m_tableModel->setData(service()->all()); tableView()->setModel(m_tableModel); @@ -93,7 +102,6 @@ public slots: } private: - AutoForm *m_form; AutoTableModel *m_tableModel; IFormHandler *m_formHandler; @@ -113,6 +121,26 @@ private: return service; } + AutoForm *form() { + return (AutoForm*)m_form; + } + + void connectService() { + if (!m_serviceConnected) + { + connect(service(), &IService::dbErrorRead, [this](QString msg) { + QMessageBox::critical(this, tr("Database error"), tr(msg.toStdString().c_str())); + }); + connect(service(), &IService::dbErrorDelete, [this](QString msg) { + QMessageBox::critical(this, tr("Database error"), tr(msg.toStdString().c_str())); + }); + + m_serviceConnected = true; + } + } + + bool m_serviceConnected; + // IGridForm interface protected: void handleNewRecord() override @@ -123,8 +151,8 @@ protected: return; } - m_form->setEntity(QSharedPointer(new T())); - m_form->setNewRec(true); + form()->setEntity(QSharedPointer(new T())); + form()->setNewRec(true); m_formHandler->showForm(m_form); } @@ -136,13 +164,14 @@ protected: return; } - m_form->setEntity(m_tableModel->itemFromIndex(tableView()->currentIndex())); - m_form->setNewRec(false); + form()->setEntity(m_tableModel->itemFromIndex(tableView()->currentIndex())); + form()->setNewRec(false); m_formHandler->showForm(m_form); } void handleDeleteRecord() override { + connectService(); if (m_form == NULL || m_tableModel == NULL || tableView()->currentIndex().row() < 0) { Q_ASSERT(false); diff --git a/core/iform.cpp b/core/iform.cpp index 27d2b59..ded8846 100644 --- a/core/iform.cpp +++ b/core/iform.cpp @@ -8,3 +8,18 @@ IForm::~IForm() { } +QString IForm::pluginId() const +{ + return m_pluginId; +} + +void IForm::setPluginId(const QString &pluginId) +{ + m_pluginId = pluginId; +} + +void IForm::refresh() +{ + emit refreshEntity(); +} + diff --git a/core/iform.h b/core/iform.h index 5bee31f..7a99e96 100644 --- a/core/iform.h +++ b/core/iform.h @@ -2,6 +2,7 @@ #define IFORM_H #include +#include #include "core_global.h" @@ -13,13 +14,21 @@ public: explicit IForm(QWidget *parent = 0); virtual ~IForm(); + QString pluginId() const; + void setPluginId(const QString &pluginId); + signals: void recordAdded(); void recordUpdated(); void validationError(const QString &errMessage); + void refreshEntity(); public slots: virtual bool saveRecord() = 0; + void refresh(); + +private: + QString m_pluginId; }; #endif // IFORM_H diff --git a/core/igridform.cpp b/core/igridform.cpp index 986b03c..ef1d899 100644 --- a/core/igridform.cpp +++ b/core/igridform.cpp @@ -14,6 +14,7 @@ IGridForm::IGridForm(QWidget *parent) : ui->filterWidget->setVisible(false); m_contextMenu = new QMenu(this); m_contextMenu->addAction(ui->actionSelectColumns); + m_form = NULL; m_columnDialog = new ColumnDialog(this); connect(m_columnDialog, SIGNAL(accepted()), this, SLOT(columnsAccepted())); @@ -32,6 +33,11 @@ void IGridForm::setPluginId(const QString &pluginId) { m_filterUi->setPluginId(pluginId); } + + if (m_form != NULL) + { + m_form->setPluginId(pluginId); + } } QString IGridForm::pluginId() diff --git a/core/igridform.h b/core/igridform.h index 499dc6a..bf22dc6 100644 --- a/core/igridform.h +++ b/core/igridform.h @@ -60,6 +60,7 @@ private: protected: FilterUi *m_filterUi; + IForm *m_form; }; #endif // IGRIDFORM_H diff --git a/core/iservice.h b/core/iservice.h index eabc038..e7fde5d 100644 --- a/core/iservice.h +++ b/core/iservice.h @@ -16,8 +16,13 @@ public: signals: void dbError(QString errMsg); + void dbErrorRead(QString errMsg); + void dbErrorInsert(QString errMsg); + void dbErrorUpdate(QString errMsg); + void dbErrorDelete(QString errMsg); void dataChanged(); - void permissionDenied(); + void updateDenied(); + void readDenied(); public slots: }; diff --git a/core/service.h b/core/service.h index e6d1f48..9caa514 100644 --- a/core/service.h +++ b/core/service.h @@ -67,6 +67,7 @@ public: catch (const odb::exception &ex) { emit dbError(ex.what()); + emit dbErrorUpdate(ex.what()); return; } @@ -88,6 +89,8 @@ public: catch (const odb::exception &ex) { emit dbError(ex.what()); + emit dbErrorInsert(ex.what()); + return; } emit dataChanged(); @@ -109,11 +112,21 @@ public: catch (const odb::exception &ex) { emit dbError(ex.what()); + emit dbErrorRead(ex.what()); } return entity; } + QSharedPointer reload(int id) { + odb::database *db = Context::instance().db(); + + Q_ASSERT(db); + + Context::instance().session().cache_erase(*db, id); + return loadById(id); + } + void erase(QSharedPointer entity) { odb::database *db = Context::instance().db(); @@ -129,6 +142,7 @@ public: catch (const odb::exception &ex) { emit dbError(ex.what()); + emit dbErrorDelete(ex.what()); } }