|
|
|
#ifndef GRIDFORM_H
|
|
|
|
#define GRIDFORM_H
|
|
|
|
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QHeaderView>
|
|
|
|
#include <QLayout>
|
|
|
|
|
|
|
|
#include "autoform.h"
|
|
|
|
#include "autotablemodel.h"
|
|
|
|
#include "context.h"
|
|
|
|
#include "iplugin.h"
|
|
|
|
|
|
|
|
#include "igridform.h"
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class GridForm : public IGridForm
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GridForm(QWidget *parent = 0) :
|
|
|
|
IGridForm(parent)
|
|
|
|
{
|
|
|
|
m_form = NULL;
|
|
|
|
m_tableModel = NULL;
|
|
|
|
m_formHandler = new DefaultFormHandler();
|
|
|
|
|
|
|
|
m_filterUi = new FilterUi(this, new T);
|
|
|
|
filterWidget()->layout()->addWidget(m_filterUi);
|
|
|
|
}
|
|
|
|
virtual ~GridForm()
|
|
|
|
{
|
|
|
|
if (m_form != NULL && m_form->parent() == NULL)
|
|
|
|
{
|
|
|
|
delete m_form;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_tableModel != NULL && m_tableModel->parent() == NULL)
|
|
|
|
{
|
|
|
|
delete m_tableModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete m_formHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setForm(AutoForm<T> *form) {
|
|
|
|
Q_ASSERT(m_form == NULL);
|
|
|
|
|
|
|
|
m_form = form;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
Q_ASSERT(m_tableModel == NULL);
|
|
|
|
|
|
|
|
m_tableModel = tableModel;
|
|
|
|
connect(m_filterUi, SIGNAL(applyFilter(QString)), m_tableModel, SLOT(filter(QString)));
|
|
|
|
connect(m_filterUi, SIGNAL(restoreData()), m_tableModel, SLOT(restore()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void setFormHandler(IFormHandler *handler) {
|
|
|
|
delete m_formHandler;
|
|
|
|
m_formHandler = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void fillData() {
|
|
|
|
if (m_tableModel == NULL) {
|
|
|
|
Q_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_tableModel->setData(service()->all());
|
|
|
|
tableView()->setModel(m_tableModel);
|
|
|
|
|
|
|
|
QList<QVariant> varList = Context::instance().settings()->value("grids/" + pluginId() + "/hide").toList();
|
|
|
|
QList<int> hide;
|
|
|
|
|
|
|
|
foreach (QVariant var, varList) {
|
|
|
|
hide.append(var.toInt());
|
|
|
|
}
|
|
|
|
|
|
|
|
hideColumns(hide);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AutoForm<T> *m_form;
|
|
|
|
AutoTableModel<T> *m_tableModel;
|
|
|
|
IFormHandler *m_formHandler;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IGridForm interface
|
|
|
|
protected:
|
|
|
|
void handleNewRecord() override
|
|
|
|
{
|
|
|
|
if (m_form == NULL)
|
|
|
|
{
|
|
|
|
Q_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_form->setEntity(QSharedPointer<T>(new T()));
|
|
|
|
m_form->setNewRec(true);
|
|
|
|
m_formHandler->showForm(m_form);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleEditRecord() override
|
|
|
|
{
|
|
|
|
if (m_form == NULL || m_tableModel == NULL || tableView()->currentIndex().row() < 0)
|
|
|
|
{
|
|
|
|
Q_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_form->setEntity(m_tableModel->itemFromIndex(tableView()->currentIndex()));
|
|
|
|
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);
|
|
|
|
m_tableModel->removeRowAt(tableView()->currentIndex());
|
|
|
|
emit dataChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // GRIDFORM_H
|