Commit 11ac18df authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Prevent accidental closing of dialogs when pressing Enter

Unset any default and auto-default buttons to prevent the accidental
closing of the dialogs when the user presses Enter while one of the search
fields or some other UI element has focus.

GnuPG-bug-id: 6095
parent ada89a7e
......@@ -14,6 +14,8 @@
#include "groupsconfigpage.h"
#include "utils/gui-helper.h"
#include <KConfigGroup>
#include <KGuiItem>
#include <KLocalizedString>
......@@ -100,6 +102,9 @@ GroupsConfigDialog::GroupsConfigDialog(QWidget *parent)
delete helpAction;
}
// prevent accidental closing of dialog when pressing Enter while the search field has focus
Kleo::unsetAutoDefaultButtons(this);
connect(buttonBox()->button(QDialogButtonBox::Reset), &QAbstractButton::clicked,
this, &GroupsConfigDialog::updateWidgets);
......@@ -118,6 +123,14 @@ QString GroupsConfigDialog::dialogName()
return QStringLiteral("Group Settings");
}
void GroupsConfigDialog::showEvent(QShowEvent *event)
{
KConfigDialog::showEvent(event);
// prevent accidental closing of dialog when pressing Enter while the search field has focus
Kleo::unsetDefaultButtons(buttonBox());
}
void GroupsConfigDialog::updateSettings()
{
d->configPage->save();
......
......@@ -23,6 +23,9 @@ public:
static QString dialogName();
protected:
void showEvent(QShowEvent *event) override;
private Q_SLOTS:
void updateSettings() override;
void updateWidgets() override;
......
......@@ -11,6 +11,7 @@
#include "editgroupdialog.h"
#include "commands/detailscommand.h"
#include "utils/gui-helper.h"
#include "view/keytreeview.h"
#include <settings.h>
......@@ -176,6 +177,9 @@ public:
okButton->setEnabled(false);
mainLayout->addWidget(ui.buttonBox);
// prevent accidental closing of dialog when pressing Enter while a search field has focus
Kleo::unsetAutoDefaultButtons(q);
connect(ui.groupNameEdit, &QLineEdit::textChanged,
q, [okButton] (const QString &text) {
okButton->setEnabled(!text.trimmed().isEmpty());
......@@ -328,3 +332,11 @@ std::vector<Key> EditGroupDialog::groupKeys() const
}
return keys;
}
void EditGroupDialog::showEvent(QShowEvent *event)
{
QDialog::showEvent(event);
// prevent accidental closing of dialog when pressing Enter while a search field has focus
Kleo::unsetDefaultButtons(d->ui.buttonBox);
}
......@@ -45,6 +45,9 @@ public:
void setGroupKeys(const std::vector<GpgME::Key> &keys);
std::vector<GpgME::Key> groupKeys() const;
protected:
void showEvent(QShowEvent *event) override;
private:
class Private;
const std::unique_ptr<Private> d;
......
......@@ -7,6 +7,9 @@
*/
#include "gui-helper.h"
#include <QDialog>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QWidget>
#ifdef Q_OS_WIN
......@@ -132,3 +135,25 @@ bool Kleo::focusFirstEnabledButton(const std::vector<QAbstractButton *> &buttons
return btn && btn->isEnabled();
});
}
void Kleo::unsetDefaultButtons(const QDialogButtonBox *buttonBox)
{
if (!buttonBox) {
return;
}
for (const auto buttons = buttonBox->buttons(); auto button : buttons) {
if (auto pushButton = qobject_cast<QPushButton *>(button)) {
pushButton->setDefault(false);
}
}
}
void Kleo::unsetAutoDefaultButtons(const QDialog *dialog)
{
if (!dialog) {
return;
}
for (const auto pushButtons = dialog->findChildren<QPushButton *>(); auto pushButton : pushButtons) {
pushButton->setAutoDefault(false);
}
}
......@@ -11,6 +11,8 @@
#include <QAbstractButton>
class QDialog;
class QDialogButtonBox;
class QWidget;
namespace Kleo
......@@ -66,4 +68,25 @@ bool focusFirstCheckedButton(const std::vector<QAbstractButton *> &buttons);
*/
bool focusFirstEnabledButton(const std::vector<QAbstractButton *> &buttons);
/**
* Unsets the default property of all push buttons in the button box.
*
* This function needs to be called after the button box received the show event
* because QDialogButtonBox automatically sets a default button when it is shown.
*
* \sa unsetAutoDefaultButtons
*/
void unsetDefaultButtons(const QDialogButtonBox *buttonBox);
/**
* Unsets the auto-default property of all push buttons in the dialog.
*
* This can be useful if you want to prevent the accidental closing of the dialog
* when the user presses Enter while another UI element, e.g. a text input field
* has focus.
*
* \sa unsetDefaultButtons
*/
void unsetAutoDefaultButtons(const QDialog *dialog);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment