Improved plugin initialization. Added delete feature.

print
Josef Rokos 9 years ago
parent 02c5bbe788
commit bfff174db8

@ -19,9 +19,7 @@ void Accommodation::initServiceUi()
AcForm *form = new AcForm(); AcForm *form = new AcForm();
grid->setForm(form); grid->setForm(form);
grid->setPluginId(pluginId());
AccommodationService *service = new AccommodationService(); AccommodationService *service = new AccommodationService();
service->setPluginId(pluginId());
m_service = service; m_service = service;
m_ui = grid; m_ui = grid;
} }

@ -6,8 +6,8 @@
#include "accommodation-odb.hxx" #include "accommodation-odb.hxx"
AccommodationService::AccommodationService() AccommodationService::AccommodationService()
:Service<Person>("ACCOMMODATION")
{ {
} }
AccommodationService::~AccommodationService() AccommodationService::~AccommodationService()

@ -2,6 +2,7 @@
#define GRIDFORM_H #define GRIDFORM_H
#include <QWidget> #include <QWidget>
#include <QMessageBox>
#include "autoform.h" #include "autoform.h"
#include "autotablemodel.h" #include "autotablemodel.h"
@ -31,10 +32,16 @@ public:
Q_ASSERT(m_form == NULL); Q_ASSERT(m_form == NULL);
m_form = form; m_form = form;
//m_form->setParent(this);
connect(m_form, SIGNAL(recordAdded()), this, SLOT(saveNew())); connect(m_form, &IForm::recordAdded, [this](){
connect(m_form, SIGNAL(recordUpdated()), this, SLOT(saveUpdate())); 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<T> *tableModel) { void setTableModel(AutoTableModel<T> *tableModel) {
@ -59,18 +66,6 @@ public slots:
tableView()->setModel(m_tableModel); 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: private:
AutoForm<T> *m_form; AutoForm<T> *m_form;
AutoTableModel<T> *m_tableModel; AutoTableModel<T> *m_tableModel;
@ -119,6 +114,25 @@ protected:
m_form->setNewRec(false); m_form->setNewRec(false);
m_formHandler->showForm(m_form); 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<T> entity = m_tableModel->itemFromIndex(tableView()->currentIndex());
service()->erase(entity);
fillData();
emit dataChanged();
}
}
}; };
#endif // GRIDFORM_H #endif // GRIDFORM_H

@ -38,3 +38,8 @@ void IGridForm::on_btnEdit_clicked()
{ {
handleEditRecord(); handleEditRecord();
} }
void IGridForm::on_btnDelete_clicked()
{
handleDeleteRecord();
}

@ -30,19 +30,17 @@ signals:
public slots: public slots:
virtual void fillData() = 0; virtual void fillData() = 0;
protected slots:
virtual void saveNew() = 0;
virtual void saveUpdate() = 0;
protected: protected:
virtual void handleNewRecord() = 0; virtual void handleNewRecord() = 0;
virtual void handleEditRecord() = 0; virtual void handleEditRecord() = 0;
//virtual void handleDeleteRecord() = 0; virtual void handleDeleteRecord() = 0;
private slots: private slots:
void on_btnNew_clicked(); void on_btnNew_clicked();
void on_btnEdit_clicked(); void on_btnEdit_clicked();
void on_btnDelete_clicked();
private: private:
QString m_pluginId; QString m_pluginId;
IFormHandler *m_formHandler; IFormHandler *m_formHandler;

@ -5,6 +5,8 @@
#include <QLocale> #include <QLocale>
#include <QDebug> #include <QDebug>
#include "igridform.h"
IMetaDataPlugin::IMetaDataPlugin() IMetaDataPlugin::IMetaDataPlugin()
{ {
} }
@ -47,6 +49,11 @@ void IMetaDataPlugin::init(const QJsonObject &metaData)
{ {
parseMetaData(metaData); parseMetaData(metaData);
initServiceUi(); initServiceUi();
if (IGridForm *pluginUi = dynamic_cast<IGridForm*>(m_ui))
{
pluginUi->setPluginId(pluginId());
}
} }
void IMetaDataPlugin::parseMetaData(const QJsonObject &metaData) void IMetaDataPlugin::parseMetaData(const QJsonObject &metaData)

@ -15,6 +15,7 @@ class IPlugin
public: public:
IPlugin() { IPlugin() {
m_ui = NULL; m_ui = NULL;
m_service = NULL;
} }
virtual ~IPlugin() { } virtual ~IPlugin() { }

@ -21,6 +21,10 @@ class Service
public: public:
Service() { } Service() { }
explicit Service(const QString &pluginId) {
m_pluginId = pluginId;
}
QList<QSharedPointer<T> > all() { QList<QSharedPointer<T> > all() {
odb::database *db = Context::instance().db(); odb::database *db = Context::instance().db();
@ -69,6 +73,16 @@ public:
return entity; return entity;
} }
void erase(QSharedPointer<T> entity) {
odb::database *db = Context::instance().db();
Q_ASSERT(db);
Transaction tx;
db->erase(entity);
tx.commit();
}
void setPluginId(const QString &pluginId) { void setPluginId(const QString &pluginId) {
m_pluginId = pluginId; m_pluginId = pluginId;
} }

Loading…
Cancel
Save