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();
grid->setForm(form);
grid->setPluginId(pluginId());
AccommodationService *service = new AccommodationService();
service->setPluginId(pluginId());
m_service = service;
m_ui = grid;
}

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

@ -2,6 +2,7 @@
#define GRIDFORM_H
#include <QWidget>
#include <QMessageBox>
#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<T> *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<T> *m_form;
AutoTableModel<T> *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<T> entity = m_tableModel->itemFromIndex(tableView()->currentIndex());
service()->erase(entity);
fillData();
emit dataChanged();
}
}
};
#endif // GRIDFORM_H

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

@ -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;

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

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

@ -21,6 +21,10 @@ class Service
public:
Service() { }
explicit Service(const QString &pluginId) {
m_pluginId = pluginId;
}
QList<QSharedPointer<T> > all() {
odb::database *db = Context::instance().db();
@ -69,6 +73,16 @@ public:
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) {
m_pluginId = pluginId;
}

Loading…
Cancel
Save