From 35696fbfea739da5dad696f04028e3ee180a5d5c Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Wed, 7 Jun 2017 15:51:33 +0200 Subject: [PATCH] Added methods for better permission checks in modules. Added method for returning selected entity in grid. Added polymorphic methods for delete record and fill grid. --- core/gridform.h | 72 +++++++++++++++++++++++++++++++++++++++------- core/igridform.cpp | 17 +++++++---- core/igridform.h | 1 + 3 files changed, 74 insertions(+), 16 deletions(-) diff --git a/core/gridform.h b/core/gridform.h index fd72813..2a1222e 100644 --- a/core/gridform.h +++ b/core/gridform.h @@ -99,7 +99,7 @@ public slots: m_permissionDenied = false; connectService(); - m_tableModel->setData(service()->all()); + m_tableModel->setData(listForGrid()); tableView()->setModel(m_tableModel); QList varList = Context::instance().settings()->value("grids/" + pluginId() + "/hide").toList(); @@ -174,10 +174,8 @@ private slots: protected: virtual void handleNewRecord() override { - PermissionEvaluator permEv; - if (!permEv.hasPermission(pluginId(), PERM_ADD)) + if (!checkPermAdd()) { - QMessageBox::critical(this, tr("Permission denied"), tr("You don't have permission to add new record.")); return; } @@ -194,10 +192,8 @@ protected: virtual void handleEditRecord() override { - PermissionEvaluator permEv; - if (!permEv.hasPermission(pluginId(), PERM_EDIT)) + if (!checkPermEdit()) { - QMessageBox::critical(this, tr("Permission denied"), tr("You don't have permission to edit record.")); return; } @@ -214,10 +210,8 @@ protected: void handleDeleteRecord() override { - PermissionEvaluator permEv; - if (!permEv.hasPermission(pluginId(), PERM_DELETE)) + if (!checkPermDelete()) { - QMessageBox::critical(this, tr("Permission denied"), tr("You don't have permission to delete record.")); return; } @@ -234,7 +228,7 @@ protected: if (reply == QMessageBox::Yes) { QSharedPointer entity = m_tableModel->itemFromIndex(tableView()->currentIndex()); - service()->erase(entity); + doDelete(entity); if (!m_permissionDenied) { @@ -244,6 +238,42 @@ protected: } } + bool checkPermAdd() + { + PermissionEvaluator permEv; + if (!permEv.hasPermission(pluginId(), PERM_ADD)) + { + QMessageBox::critical(this, tr("Permission denied"), tr("You don't have permission to add new record.")); + return false; + } + + return true; + } + + bool checkPermEdit() + { + PermissionEvaluator permEv; + if (!permEv.hasPermission(pluginId(), PERM_EDIT)) + { + QMessageBox::critical(this, tr("Permission denied"), tr("You don't have permission to edit record.")); + return false; + } + + return true; + } + + bool checkPermDelete() + { + PermissionEvaluator permEv; + if (!permEv.hasPermission(pluginId(), PERM_DELETE)) + { + QMessageBox::critical(this, tr("Permission denied"), tr("You don't have permission to delete record.")); + return false; + } + + return true; + } + virtual int currentRecordId() { if (tableView()->currentIndex().isValid()) @@ -254,6 +284,26 @@ protected: return 0; } + QSharedPointer currentEntity() + { + if (tableView()->currentIndex().isValid()) + { + return m_tableModel->itemFromIndex(tableView()->currentIndex()); + } + + return QSharedPointer(); + } + + virtual void doDelete(QSharedPointer entity) + { + service()->erase(entity); + } + + virtual QList > listForGrid() + { + return service()->all(); + } + void addRow(QSharedPointer entity) { m_tableModel->addRow(entity); diff --git a/core/igridform.cpp b/core/igridform.cpp index 319f195..66cce6a 100644 --- a/core/igridform.cpp +++ b/core/igridform.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "context.h" #include "filterui.h" @@ -23,6 +24,10 @@ IGridForm::IGridForm(QWidget *parent) : m_columnDialog = new ColumnDialog(this); connect(m_columnDialog, SIGNAL(accepted()), this, SLOT(columnsAccepted())); + connect(ui->tableView->selectionModel(), &QItemSelectionModel::currentRowChanged, [this](const QModelIndex ¤t, const QModelIndex &){ + currentIndexChanged(current); + }); + m_varFiller = new VariableFiller(); } @@ -91,6 +96,12 @@ QWidget *IGridForm::filterWidget() void IGridForm::enableButtons() { ui->btnNew->setEnabled(canAddRecord()); + + if (ui->tableView->currentIndex().isValid()) + { + ui->btnEdit->setEnabled(canEditRecord()); + ui->btnDelete->setEnabled(canDeleteRecord()); + } } @@ -163,11 +174,7 @@ void IGridForm::on_btnFilter_toggled(bool checked) void IGridForm::on_tableView_clicked(const QModelIndex &) { - if (ui->tableView->currentIndex().isValid()) - { - ui->btnEdit->setEnabled(canEditRecord()); - ui->btnDelete->setEnabled(canDeleteRecord()); - } + enableButtons(); } void IGridForm::on_btnPrint_clicked() diff --git a/core/igridform.h b/core/igridform.h index b733387..81f8863 100644 --- a/core/igridform.h +++ b/core/igridform.h @@ -47,6 +47,7 @@ protected: virtual bool canEditRecord() { return true; } virtual bool canDeleteRecord() { return true; } virtual int currentRecordId() = 0; + virtual void currentIndexChanged(const QModelIndex ¤t) { Q_UNUSED(current) } void hideColumns(const QList &cols); QWidget *filterWidget(); void enableButtons();