Ask user before deleting custom plugin data and disable button if no data selected

This commit is contained in:
Janek Bevendorff 2018-02-19 15:23:22 +01:00
parent da52da37b3
commit f15088f496
3 changed files with 27 additions and 3 deletions

View File

@ -17,6 +17,7 @@
#include "EditWidgetProperties.h"
#include "ui_EditWidgetProperties.h"
#include "MessageBox.h"
EditWidgetProperties::EditWidgetProperties(QWidget* parent)
: QWidget(parent)
@ -25,8 +26,11 @@ EditWidgetProperties::EditWidgetProperties(QWidget* parent)
, m_customDataModel(new QStandardItemModel(this))
{
m_ui->setupUi(this);
m_ui->removeCustomDataButton->setEnabled(false);
m_ui->customDataTable->setModel(m_customDataModel);
connect(m_ui->customDataTable->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
SLOT(toggleRemoveButton(QItemSelection)));
connect(m_ui->removeCustomDataButton, SIGNAL(clicked()), SLOT(removeSelectedPluginData()));
}
@ -51,7 +55,7 @@ void EditWidgetProperties::setCustomData(const CustomData* customData)
Q_ASSERT(customData);
m_customData->copyDataFrom(customData);
this->updateModel();
updateModel();
}
const CustomData* EditWidgetProperties::customData() const
@ -61,6 +65,14 @@ const CustomData* EditWidgetProperties::customData() const
void EditWidgetProperties::removeSelectedPluginData()
{
if (QMessageBox::Yes != MessageBox::question(this,
tr("Delete plugin data?"),
tr("Do you really want to delete the selected plugin data?\n"
"This may cause the affected plugins to malfunction."),
QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel)) {
return;
}
const QItemSelectionModel* itemSelectionModel = m_ui->customDataTable->selectionModel();
if (itemSelectionModel) {
for (const QModelIndex& index : itemSelectionModel->selectedRows(0)) {
@ -71,12 +83,22 @@ void EditWidgetProperties::removeSelectedPluginData()
}
}
void EditWidgetProperties::toggleRemoveButton(const QItemSelection& selected)
{
m_ui->removeCustomDataButton->setEnabled(!selected.isEmpty());
}
void EditWidgetProperties::updateModel()
{
m_customDataModel->clear();
m_customDataModel->setHorizontalHeaderLabels({tr("Key"), tr("Value")});
for (const QString& key : m_customData->keys()) {
m_customDataModel->appendRow(QList<QStandardItem*>()
<< new QStandardItem(key)
<< new QStandardItem(m_customData->value(key)));
}
m_ui->removeCustomDataButton->setEnabled(false);
}

View File

@ -19,6 +19,7 @@
#define KEEPASSX_EDITWIDGETPROPERTIES_H
#include <QStandardItemModel>
#include <QItemSelection>
#include <QPointer>
#include <QWidget>
@ -45,6 +46,7 @@ public:
private slots:
void removeSelectedPluginData();
void toggleRemoveButton(const QItemSelection& selected);
private:
void updateModel();

View File

@ -112,8 +112,8 @@
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<attribute name="horizontalHeaderVisible">
<bool>false</bool>
<attribute name="horizontalHeaderDefaultSectionSize">
<number>200</number>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>