Skip to content

Commit

Permalink
Ask user before deleting custom plugin data and disable button if no …
Browse files Browse the repository at this point in the history
…data selected
  • Loading branch information
phoerious committed Feb 21, 2018
1 parent da52da3 commit f15088f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
24 changes: 23 additions & 1 deletion src/gui/EditWidgetProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "EditWidgetProperties.h"
#include "ui_EditWidgetProperties.h"
#include "MessageBox.h"

EditWidgetProperties::EditWidgetProperties(QWidget* parent)
: QWidget(parent)
Expand All @@ -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()));
}

Expand All @@ -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
Expand All @@ -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)) {
Expand All @@ -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);
}
2 changes: 2 additions & 0 deletions src/gui/EditWidgetProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define KEEPASSX_EDITWIDGETPROPERTIES_H

#include <QStandardItemModel>
#include <QItemSelection>
#include <QPointer>
#include <QWidget>

Expand All @@ -45,6 +46,7 @@ class EditWidgetProperties : public QWidget

private slots:
void removeSelectedPluginData();
void toggleRemoveButton(const QItemSelection& selected);

private:
void updateModel();
Expand Down
4 changes: 2 additions & 2 deletions src/gui/EditWidgetProperties.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit f15088f

Please sign in to comment.