Fix QDialogButtonBox connections in the edit profile dialog

KPageDialog already creates the accepted()/rejected() connections, since
the code here created another connection, the accept() slot was being
called twice for each OK button click, with e.g. the profile name empty,
clicking Ok:
- the first time isProfileNameValid() would return false, and the saving
  would abort, as it should
- the second time around, isProfileNameValid() would return true since
  the name has been reverted to the original value in the Name line edit

The same goes for the reject() slot.

Also use a lambda instead of apply(), it's a very small method and this
was the code is more readable, since we don't need to jump back and forth.
This commit is contained in:
Ahmad Samir
2021-04-06 12:52:07 +02:00
parent 1c8db8a3fc
commit c413d543c1
2 changed files with 12 additions and 29 deletions

View File

@@ -77,17 +77,16 @@ EditProfileDialog::EditProfileDialog(QWidget *parent)
_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply);
setButtonBox(_buttonBox);
QPushButton *okButton = _buttonBox->button(QDialogButtonBox::Ok);
okButton->setDefault(true);
connect(_buttonBox, &QDialogButtonBox::accepted, this, &Konsole::EditProfileDialog::accept);
connect(_buttonBox, &QDialogButtonBox::rejected, this, &Konsole::EditProfileDialog::reject);
_buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
// disable the apply button , since no modification has been made
_buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
connect(_buttonBox->button(QDialogButtonBox::Apply),
&QPushButton::clicked, this,
&Konsole::EditProfileDialog::apply);
auto *applyButton = _buttonBox->button(QDialogButtonBox::Apply);
// Disable it, since no modifications have been made yet
applyButton->setEnabled(false);
connect(applyButton, &QPushButton::clicked, this, [this]() {
if (isProfileNameValid()) {
save();
}
});
connect(_delayedPreviewTimer, &QTimer::timeout, this,
&Konsole::EditProfileDialog::delayedPreviewActivate);
@@ -259,25 +258,11 @@ void EditProfileDialog::reject()
void EditProfileDialog::accept()
{
// if the Apply button is disabled then no settings were changed
// or the changes have already been saved by apply()
if (_buttonBox->button(QDialogButtonBox::Apply)->isEnabled()) {
if (!isProfileNameValid()) {
return;
}
if (isProfileNameValid()) {
save();
unpreviewAll();
QDialog::accept();
}
unpreviewAll();
QDialog::accept();
}
void EditProfileDialog::apply()
{
if (!isProfileNameValid()) {
return;
}
save();
}
void EditProfileDialog::setMessageGeneralPage(const QString &msg)

View File

@@ -94,8 +94,6 @@ public Q_SLOTS:
// reimplemented
void reject() override;
void apply();
protected:
bool eventFilter(QObject *watched, QEvent *event) override;