From bfff174db8c626aecebb636e1f3ee130cefa1859 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Wed, 18 Nov 2015 22:27:46 +0100 Subject: [PATCH] Improved plugin initialization. Added delete feature. --- accommodation/accommodation.cpp | 2 -- accommodation/accommodationservice.cpp | 2 +- core/gridform.h | 44 +++++++++++++++++--------- core/igridform.cpp | 5 +++ core/igridform.h | 8 ++--- core/imetadataplugin.cpp | 7 ++++ core/iplugin.h | 1 + core/service.h | 14 ++++++++ 8 files changed, 60 insertions(+), 23 deletions(-) diff --git a/accommodation/accommodation.cpp b/accommodation/accommodation.cpp index b956a0e..f8d8795 100644 --- a/accommodation/accommodation.cpp +++ b/accommodation/accommodation.cpp @@ -19,9 +19,7 @@ void Accommodation::initServiceUi() AcForm *form = new AcForm(); grid->setForm(form); - grid->setPluginId(pluginId()); AccommodationService *service = new AccommodationService(); - service->setPluginId(pluginId()); m_service = service; m_ui = grid; } diff --git a/accommodation/accommodationservice.cpp b/accommodation/accommodationservice.cpp index 7051c5b..6febbe5 100644 --- a/accommodation/accommodationservice.cpp +++ b/accommodation/accommodationservice.cpp @@ -6,8 +6,8 @@ #include "accommodation-odb.hxx" AccommodationService::AccommodationService() + :Service("ACCOMMODATION") { - } AccommodationService::~AccommodationService() diff --git a/core/gridform.h b/core/gridform.h index 5431ee5..9874dce 100644 --- a/core/gridform.h +++ b/core/gridform.h @@ -2,6 +2,7 @@ #define GRIDFORM_H #include +#include #include "autoform.h" #include "autotablemodel.h" @@ -31,10 +32,16 @@ public: Q_ASSERT(m_form == NULL); m_form = form; - //m_form->setParent(this); - connect(m_form, SIGNAL(recordAdded()), this, SLOT(saveNew())); - connect(m_form, SIGNAL(recordUpdated()), this, SLOT(saveUpdate())); + connect(m_form, &IForm::recordAdded, [this](){ + service()->save(m_form->entity()); + m_tableModel->addRow(m_form->entity()); + emit dataChanged(); + }); + connect(m_form, &IForm::recordUpdated, [this](){ + service()->update(m_form->entity()); + emit dataChanged(); + }); } void setTableModel(AutoTableModel *tableModel) { @@ -59,18 +66,6 @@ public slots: tableView()->setModel(m_tableModel); } -protected slots: - void saveNew() { - service()->save(m_form->entity()); - m_tableModel->addRow(m_form->entity()); - emit dataChanged(); - } - - void saveUpdate() { - service()->update(m_form->entity()); - emit dataChanged(); - } - private: AutoForm *m_form; AutoTableModel *m_tableModel; @@ -119,6 +114,25 @@ protected: m_form->setNewRec(false); m_formHandler->showForm(m_form); } + + void handleDeleteRecord() override + { + if (m_form == NULL || m_tableModel == NULL || tableView()->currentIndex().row() < 0) + { + Q_ASSERT(false); + return; + } + + QMessageBox::StandardButton reply; + reply = QMessageBox::question(this, tr("Delete record"), tr("Realy delete this record?"), QMessageBox::Yes|QMessageBox::No); + if (reply == QMessageBox::Yes) + { + QSharedPointer entity = m_tableModel->itemFromIndex(tableView()->currentIndex()); + service()->erase(entity); + fillData(); + emit dataChanged(); + } + } }; #endif // GRIDFORM_H diff --git a/core/igridform.cpp b/core/igridform.cpp index f8befcc..0ffc3a9 100644 --- a/core/igridform.cpp +++ b/core/igridform.cpp @@ -38,3 +38,8 @@ void IGridForm::on_btnEdit_clicked() { handleEditRecord(); } + +void IGridForm::on_btnDelete_clicked() +{ + handleDeleteRecord(); +} diff --git a/core/igridform.h b/core/igridform.h index 5aa2e47..8ed7e45 100644 --- a/core/igridform.h +++ b/core/igridform.h @@ -30,19 +30,17 @@ signals: public slots: virtual void fillData() = 0; -protected slots: - virtual void saveNew() = 0; - virtual void saveUpdate() = 0; - protected: virtual void handleNewRecord() = 0; virtual void handleEditRecord() = 0; - //virtual void handleDeleteRecord() = 0; + virtual void handleDeleteRecord() = 0; private slots: void on_btnNew_clicked(); void on_btnEdit_clicked(); + void on_btnDelete_clicked(); + private: QString m_pluginId; IFormHandler *m_formHandler; diff --git a/core/imetadataplugin.cpp b/core/imetadataplugin.cpp index 290d0f4..c9e502a 100644 --- a/core/imetadataplugin.cpp +++ b/core/imetadataplugin.cpp @@ -5,6 +5,8 @@ #include #include +#include "igridform.h" + IMetaDataPlugin::IMetaDataPlugin() { } @@ -47,6 +49,11 @@ void IMetaDataPlugin::init(const QJsonObject &metaData) { parseMetaData(metaData); initServiceUi(); + + if (IGridForm *pluginUi = dynamic_cast(m_ui)) + { + pluginUi->setPluginId(pluginId()); + } } void IMetaDataPlugin::parseMetaData(const QJsonObject &metaData) diff --git a/core/iplugin.h b/core/iplugin.h index b9c8aa8..d50dd9c 100644 --- a/core/iplugin.h +++ b/core/iplugin.h @@ -15,6 +15,7 @@ class IPlugin public: IPlugin() { m_ui = NULL; + m_service = NULL; } virtual ~IPlugin() { } diff --git a/core/service.h b/core/service.h index cd15f16..f56207e 100644 --- a/core/service.h +++ b/core/service.h @@ -21,6 +21,10 @@ class Service public: Service() { } + explicit Service(const QString &pluginId) { + m_pluginId = pluginId; + } + QList > all() { odb::database *db = Context::instance().db(); @@ -69,6 +73,16 @@ public: return entity; } + void erase(QSharedPointer entity) { + odb::database *db = Context::instance().db(); + + Q_ASSERT(db); + + Transaction tx; + db->erase(entity); + tx.commit(); + } + void setPluginId(const QString &pluginId) { m_pluginId = pluginId; }