From ec3aac74a4812b18e483f524e991e9e0bbcd0bdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 8 Mar 2023 10:27:48 +0100 Subject: [PATCH] Plumb QMessageBox::setCheckBox() through QPlatformMessageDialogHelper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Without this plumbing we have no way of knowing if the QMessageBox has a checkbox set, and can't decide to skip the native dialog, leaving the user without the expected checkbox. As the suppression checkbox on macOS can be customized, we can use this plumbing to actually provide native dialog support for generic check boxes. This mechanism can also be used by QErrorMessage, which now matches behavior between native and non-native dialogs in terms of the label of the checkbox and its initial state. We might want to tweak this in the future, since user's might expect the suppression label and state to match the system default, but that's something we can expose from the platform theme if so, and should apply equally to the non-native dialog. Fixes: QTBUG-111803 Change-Id: Ied9fc34383fe79fbd8437592ad1c1993b9396178 Reviewed-by: Volker Hilsheimer (cherry picked from commit a0313c85a9b8ab6ba0ec273f0c8638e2f7b8cc18) Reviewed-by: Tor Arne Vestbø --- src/gui/kernel/qplatformdialoghelper.cpp | 17 +++++++--- src/gui/kernel/qplatformdialoghelper.h | 7 +++-- .../android/qandroidplatformdialoghelpers.cpp | 2 +- .../platforms/cocoa/qcocoamessagedialog.mm | 31 +++++++++++++++++-- .../platforms/ios/qiosmessagedialog.mm | 2 +- src/widgets/dialogs/qerrormessage.cpp | 8 ++--- src/widgets/dialogs/qmessagebox.cpp | 11 +++++++ 7 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/gui/kernel/qplatformdialoghelper.cpp b/src/gui/kernel/qplatformdialoghelper.cpp index de0d4f43d7a..c8510160f80 100644 --- a/src/gui/kernel/qplatformdialoghelper.cpp +++ b/src/gui/kernel/qplatformdialoghelper.cpp @@ -776,7 +776,8 @@ public: QList customButtons; int nextCustomButtonId; QPixmap iconPixmap; - bool enableSupressionCheckBox = false; + QString checkBoxLabel; + Qt::CheckState checkBoxState = Qt::Unchecked; }; QMessageDialogOptions::QMessageDialogOptions(QMessageDialogOptionsPrivate *dd) @@ -907,14 +908,20 @@ const QMessageDialogOptions::CustomButton *QMessageDialogOptions::customButton(i return (i < 0 ? nullptr : &d->customButtons.at(i)); } -void QMessageDialogOptions::setSupressionCheckBoxEnabled(bool enabled) +void QMessageDialogOptions::setCheckBox(const QString &label, Qt::CheckState state) { - d->enableSupressionCheckBox = enabled; + d->checkBoxLabel = label; + d->checkBoxState = state; } -bool QMessageDialogOptions::supressionCheckBoxEnabled() const +QString QMessageDialogOptions::checkBoxLabel() const { - return d->enableSupressionCheckBox; + return d->checkBoxLabel; +} + +Qt::CheckState QMessageDialogOptions::checkBoxState() const +{ + return d->checkBoxState; } QPlatformDialogHelper::ButtonRole QPlatformDialogHelper::buttonRole(QPlatformDialogHelper::StandardButton button) diff --git a/src/gui/kernel/qplatformdialoghelper.h b/src/gui/kernel/qplatformdialoghelper.h index b7aa02cc7ca..fc2e85a8d6e 100644 --- a/src/gui/kernel/qplatformdialoghelper.h +++ b/src/gui/kernel/qplatformdialoghelper.h @@ -451,8 +451,9 @@ public: const QList &customButtons(); const CustomButton *customButton(int id); - void setSupressionCheckBoxEnabled(bool enabled); - bool supressionCheckBoxEnabled() const; + void setCheckBox(const QString &label, Qt::CheckState state); + QString checkBoxLabel() const; + Qt::CheckState checkBoxState() const; private: QMessageDialogOptionsPrivate *d; @@ -467,7 +468,7 @@ public: Q_SIGNALS: void clicked(QPlatformDialogHelper::StandardButton button, QPlatformDialogHelper::ButtonRole role); - void supressionCheckBoxChanged(bool checked); + void checkBoxStateChanged(Qt::CheckState state); private: QSharedPointer m_options; diff --git a/src/plugins/platforms/android/qandroidplatformdialoghelpers.cpp b/src/plugins/platforms/android/qandroidplatformdialoghelpers.cpp index 05dd671122a..56b1ffd8309 100644 --- a/src/plugins/platforms/android/qandroidplatformdialoghelpers.cpp +++ b/src/plugins/platforms/android/qandroidplatformdialoghelpers.cpp @@ -54,7 +54,7 @@ bool QAndroidPlatformMessageDialogHelper::show(Qt::WindowFlags windowFlags, if (!opt.data()) return false; - if (opt->supressionCheckBoxEnabled()) + if (!opt->checkBoxLabel().isNull()) return false; // Can't support m_javaMessageDialog.callMethod("setIcon", "(I)V", opt->icon()); diff --git a/src/plugins/platforms/cocoa/qcocoamessagedialog.mm b/src/plugins/platforms/cocoa/qcocoamessagedialog.mm index 8ba204752ba..e4e22602994 100644 --- a/src/plugins/platforms/cocoa/qcocoamessagedialog.mm +++ b/src/plugins/platforms/cocoa/qcocoamessagedialog.mm @@ -46,6 +46,16 @@ static QString toPlainText(const QString &text) return textDocument.toPlainText(); } +static NSControlStateValue controlStateFor(Qt::CheckState state) +{ + switch (state) { + case Qt::Checked: return NSControlStateValueOn; + case Qt::Unchecked: return NSControlStateValueOff; + case Qt::PartiallyChecked: return NSControlStateValueMixed; + } + Q_UNREACHABLE(); +} + /* Called from QDialogPrivate::setNativeDialogVisible() when the message box is ready to be shown. @@ -185,7 +195,14 @@ bool QCocoaMessageDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality w StandardButton::Ok, ButtonRole::AcceptRole); } - m_alert.showsSuppressionButton = options()->supressionCheckBoxEnabled(); + if (auto checkBoxLabel = options()->checkBoxLabel(); !checkBoxLabel.isNull()) { + checkBoxLabel = QPlatformTheme::removeMnemonics(checkBoxLabel); + m_alert.suppressionButton.title = checkBoxLabel.toNSString(); + auto state = options()->checkBoxState(); + m_alert.suppressionButton.allowsMixedState = state == Qt::PartiallyChecked; + m_alert.suppressionButton.state = controlStateFor(state); + m_alert.showsSuppressionButton = YES; + } qCDebug(lcQpaDialogs) << "Showing" << m_alert; @@ -233,6 +250,16 @@ void QCocoaMessageDialog::exec() // Custom modal response code to record that the dialog was hidden by us static const NSInteger kModalResponseDialogHidden = NSAlertThirdButtonReturn + 1; +static Qt::CheckState checkStateFor(NSControlStateValue state) +{ + switch (state) { + case NSControlStateValueOn: return Qt::Checked; + case NSControlStateValueOff: return Qt::Unchecked; + case NSControlStateValueMixed: return Qt::PartiallyChecked; + } + Q_UNREACHABLE(); +} + void QCocoaMessageDialog::processResponse(NSModalResponse response) { qCDebug(lcQpaDialogs) << "Processing response" << response << "for" << m_alert; @@ -244,7 +271,7 @@ void QCocoaMessageDialog::processResponse(NSModalResponse response) [alert autorelease]; if (alert.showsSuppressionButton) - emit supressionCheckBoxChanged(alert.suppressionButton.state == NSControlStateValueOn); + emit checkBoxStateChanged(checkStateFor(alert.suppressionButton.state)); if (response >= NSAlertFirstButtonReturn) { // Safe range for user-defined modal responses diff --git a/src/plugins/platforms/ios/qiosmessagedialog.mm b/src/plugins/platforms/ios/qiosmessagedialog.mm index 1f39a94172f..b95cfada602 100644 --- a/src/plugins/platforms/ios/qiosmessagedialog.mm +++ b/src/plugins/platforms/ios/qiosmessagedialog.mm @@ -92,7 +92,7 @@ bool QIOSMessageDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality win || windowModality == Qt::NonModal) // We can only do modal dialogs return false; - if (options()->supressionCheckBoxEnabled()) + if (!options()->checkBoxLabel().isNull()) return false; // Can't support m_alertController = [[UIAlertController diff --git a/src/widgets/dialogs/qerrormessage.cpp b/src/widgets/dialogs/qerrormessage.cpp index 29706c2eadd..a00af9a8d14 100644 --- a/src/widgets/dialogs/qerrormessage.cpp +++ b/src/widgets/dialogs/qerrormessage.cpp @@ -63,9 +63,9 @@ void QErrorMessagePrivate::initHelper(QPlatformDialogHelper *helper) { Q_Q(QErrorMessage); auto *messageDialogHelper = static_cast(helper); - QObject::connect(messageDialogHelper, &QPlatformMessageDialogHelper::supressionCheckBoxChanged, q, - [this](bool supressionChecked) { - again->setChecked(!supressionChecked); + QObject::connect(messageDialogHelper, &QPlatformMessageDialogHelper::checkBoxStateChanged, q, + [this](Qt::CheckState state) { + again->setCheckState(state); } ); QObject::connect(messageDialogHelper, &QPlatformMessageDialogHelper::clicked, q, @@ -86,7 +86,7 @@ void QErrorMessagePrivate::helperPrepareShow(QPlatformDialogHelper *helper) options->setText(QErrorMessage::tr("An error occurred")); options->setInformativeText(currentMessage); options->setIcon(QMessageDialogOptions::Critical); - options->setSupressionCheckBoxEnabled(true); + options->setCheckBox(again->text(), again->checkState()); messageDialogHelper->setOptions(options); } diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index b695af0f3f2..c11bd458157 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -2682,6 +2682,15 @@ void QMessageBoxPrivate::initHelper(QPlatformDialogHelper *h) Q_Q(QMessageBox); QObject::connect(h, SIGNAL(clicked(QPlatformDialogHelper::StandardButton,QPlatformDialogHelper::ButtonRole)), q, SLOT(_q_clicked(QPlatformDialogHelper::StandardButton,QPlatformDialogHelper::ButtonRole))); + + auto *messageDialogHelper = static_cast(h); + QObject::connect(messageDialogHelper, &QPlatformMessageDialogHelper::checkBoxStateChanged, q, + [this](Qt::CheckState state) { + if (checkbox) + checkbox->setCheckState(state); + } + ); + static_cast(h)->setOptions(options); } @@ -2720,6 +2729,8 @@ void QMessageBoxPrivate::helperPrepareShow(QPlatformDialogHelper *) options->setIcon(helperIcon(q->icon())); options->setIconPixmap(q->iconPixmap()); options->setStandardButtons(helperStandardButtons(q)); + if (checkbox) + options->setCheckBox(checkbox->text(), checkbox->checkState()); } void QMessageBoxPrivate::helperDone(QDialog::DialogCode code, QPlatformDialogHelper *)