forked from qt-creator/qt-creator
Utils: Add async methods to CheckableMessageBox
Change-Id: I296cb34453f5c12f712afedfe35b8962be8d7eec Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -34,19 +34,16 @@ namespace Utils {
|
|||||||
|
|
||||||
static QtcSettings *theSettings;
|
static QtcSettings *theSettings;
|
||||||
|
|
||||||
static QMessageBox::StandardButton exec(
|
static void prepare(QMessageBox::Icon icon,
|
||||||
QWidget *parent,
|
const QString &title,
|
||||||
QMessageBox::Icon icon,
|
const QString &text,
|
||||||
const QString &title,
|
const CheckableDecider &decider,
|
||||||
const QString &text,
|
QMessageBox::StandardButtons buttons,
|
||||||
const CheckableDecider &decider,
|
QMessageBox::StandardButton defaultButton,
|
||||||
QMessageBox::StandardButtons buttons,
|
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides,
|
||||||
QMessageBox::StandardButton defaultButton,
|
const QString &msg,
|
||||||
QMessageBox::StandardButton acceptButton,
|
QMessageBox &msgBox)
|
||||||
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides,
|
|
||||||
const QString &msg)
|
|
||||||
{
|
{
|
||||||
QMessageBox msgBox(parent);
|
|
||||||
msgBox.setWindowTitle(title);
|
msgBox.setWindowTitle(title);
|
||||||
msgBox.setIcon(icon);
|
msgBox.setIcon(icon);
|
||||||
msgBox.setText(text);
|
msgBox.setText(text);
|
||||||
@@ -60,7 +57,7 @@ static QMessageBox::StandardButton exec(
|
|||||||
if (text.contains("<a "))
|
if (text.contains("<a "))
|
||||||
msgBox.setOptions(QMessageBox::Option::DontUseNativeDialog);
|
msgBox.setOptions(QMessageBox::Option::DontUseNativeDialog);
|
||||||
|
|
||||||
// Workaround for QTBUG-118241, fixed in Qt 6.6.1
|
// Workaround for QTBUG-118241, fixed in Qt 6.6.1
|
||||||
#if QT_VERSION < QT_VERSION_CHECK(6, 6, 1)
|
#if QT_VERSION < QT_VERSION_CHECK(6, 6, 1)
|
||||||
if (!buttonTextOverrides.isEmpty())
|
if (!buttonTextOverrides.isEmpty())
|
||||||
msgBox.setOptions(QMessageBox::Option::DontUseNativeDialog);
|
msgBox.setOptions(QMessageBox::Option::DontUseNativeDialog);
|
||||||
@@ -69,9 +66,6 @@ static QMessageBox::StandardButton exec(
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (decider.shouldAskAgain) {
|
if (decider.shouldAskAgain) {
|
||||||
if (!decider.shouldAskAgain())
|
|
||||||
return acceptButton;
|
|
||||||
|
|
||||||
msgBox.setCheckBox(new QCheckBox);
|
msgBox.setCheckBox(new QCheckBox);
|
||||||
msgBox.checkBox()->setChecked(false);
|
msgBox.checkBox()->setChecked(false);
|
||||||
msgBox.checkBox()->setText(msg);
|
msgBox.checkBox()->setText(msg);
|
||||||
@@ -81,6 +75,27 @@ static QMessageBox::StandardButton exec(
|
|||||||
msgBox.setDefaultButton(defaultButton);
|
msgBox.setDefaultButton(defaultButton);
|
||||||
for (auto it = buttonTextOverrides.constBegin(); it != buttonTextOverrides.constEnd(); ++it)
|
for (auto it = buttonTextOverrides.constBegin(); it != buttonTextOverrides.constEnd(); ++it)
|
||||||
msgBox.button(it.key())->setText(it.value());
|
msgBox.button(it.key())->setText(it.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
static QMessageBox::StandardButton exec(
|
||||||
|
QWidget *parent,
|
||||||
|
QMessageBox::Icon icon,
|
||||||
|
const QString &title,
|
||||||
|
const QString &text,
|
||||||
|
const CheckableDecider &decider,
|
||||||
|
QMessageBox::StandardButtons buttons,
|
||||||
|
QMessageBox::StandardButton defaultButton,
|
||||||
|
QMessageBox::StandardButton acceptButton,
|
||||||
|
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides,
|
||||||
|
const QString &msg)
|
||||||
|
{
|
||||||
|
if (decider.shouldAskAgain) {
|
||||||
|
if (!decider.shouldAskAgain())
|
||||||
|
return acceptButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
QMessageBox msgBox(parent);
|
||||||
|
prepare(icon, title, text, decider, buttons, defaultButton, buttonTextOverrides, msg, msgBox);
|
||||||
msgBox.exec();
|
msgBox.exec();
|
||||||
|
|
||||||
QMessageBox::StandardButton clickedBtn = msgBox.standardButton(msgBox.clickedButton());
|
QMessageBox::StandardButton clickedBtn = msgBox.standardButton(msgBox.clickedButton());
|
||||||
@@ -91,6 +106,45 @@ static QMessageBox::StandardButton exec(
|
|||||||
return clickedBtn;
|
return clickedBtn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void show(QWidget *parent,
|
||||||
|
QMessageBox::Icon icon,
|
||||||
|
const QString &title,
|
||||||
|
const QString &text,
|
||||||
|
CheckableDecider decider,
|
||||||
|
QMessageBox::StandardButtons buttons,
|
||||||
|
QMessageBox::StandardButton defaultButton,
|
||||||
|
QMessageBox::StandardButton acceptButton,
|
||||||
|
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides,
|
||||||
|
const QString &msg,
|
||||||
|
QObject *guard,
|
||||||
|
const std::function<void(QMessageBox::StandardButton choosenBtn)> &callback)
|
||||||
|
{
|
||||||
|
if (decider.shouldAskAgain && !decider.shouldAskAgain()) {
|
||||||
|
if (callback) {
|
||||||
|
QMetaObject::invokeMethod(
|
||||||
|
guard, [callback, acceptButton] { callback(acceptButton); }, Qt::QueuedConnection);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QMessageBox *msgBox = new QMessageBox(parent);
|
||||||
|
prepare(icon, title, text, decider, buttons, defaultButton, buttonTextOverrides, msg, *msgBox);
|
||||||
|
|
||||||
|
QObject::connect(msgBox, &QMessageBox::finished, guard, [msgBox, callback, decider, acceptButton] {
|
||||||
|
QMessageBox::StandardButton clickedBtn = msgBox->standardButton(msgBox->clickedButton());
|
||||||
|
|
||||||
|
if (decider.doNotAskAgain && msgBox->checkBox()->isChecked()
|
||||||
|
&& (acceptButton == QMessageBox::NoButton || clickedBtn == acceptButton))
|
||||||
|
decider.doNotAskAgain();
|
||||||
|
if (callback)
|
||||||
|
callback(clickedBtn);
|
||||||
|
|
||||||
|
msgBox->deleteLater();
|
||||||
|
});
|
||||||
|
|
||||||
|
msgBox->show();
|
||||||
|
}
|
||||||
|
|
||||||
CheckableDecider::CheckableDecider(const Key &settingsSubKey)
|
CheckableDecider::CheckableDecider(const Key &settingsSubKey)
|
||||||
{
|
{
|
||||||
QTC_ASSERT(theSettings, return);
|
QTC_ASSERT(theSettings, return);
|
||||||
@@ -100,7 +154,7 @@ CheckableDecider::CheckableDecider(const Key &settingsSubKey)
|
|||||||
theSettings->endGroup();
|
theSettings->endGroup();
|
||||||
return !shouldNotAsk;
|
return !shouldNotAsk;
|
||||||
};
|
};
|
||||||
doNotAskAgain = [settingsSubKey] {
|
doNotAskAgain = [settingsSubKey] {
|
||||||
theSettings->beginGroup(kDoNotAskAgainKey);
|
theSettings->beginGroup(kDoNotAskAgainKey);
|
||||||
theSettings->setValue(settingsSubKey, true);
|
theSettings->setValue(settingsSubKey, true);
|
||||||
theSettings->endGroup();
|
theSettings->endGroup();
|
||||||
@@ -136,6 +190,33 @@ QMessageBox::StandardButton CheckableMessageBox::question(
|
|||||||
msg.isEmpty() ? msgDoNotAskAgain() : msg);
|
msg.isEmpty() ? msgDoNotAskAgain() : msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheckableMessageBox::question_async(
|
||||||
|
QWidget *parent,
|
||||||
|
const QString &title,
|
||||||
|
const QString &question,
|
||||||
|
const CheckableDecider &decider,
|
||||||
|
QObject *guard,
|
||||||
|
std::function<void(QMessageBox::StandardButton choosenBtn)> callback,
|
||||||
|
QMessageBox::StandardButtons buttons,
|
||||||
|
QMessageBox::StandardButton defaultButton,
|
||||||
|
QMessageBox::StandardButton acceptButton,
|
||||||
|
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides,
|
||||||
|
const QString &msg)
|
||||||
|
{
|
||||||
|
show(parent,
|
||||||
|
QMessageBox::Question,
|
||||||
|
title,
|
||||||
|
question,
|
||||||
|
decider,
|
||||||
|
buttons,
|
||||||
|
defaultButton,
|
||||||
|
acceptButton,
|
||||||
|
buttonTextOverrides,
|
||||||
|
msg.isEmpty() ? msgDoNotAskAgain() : msg,
|
||||||
|
guard,
|
||||||
|
callback);
|
||||||
|
}
|
||||||
|
|
||||||
QMessageBox::StandardButton CheckableMessageBox::information(
|
QMessageBox::StandardButton CheckableMessageBox::information(
|
||||||
QWidget *parent,
|
QWidget *parent,
|
||||||
const QString &title,
|
const QString &title,
|
||||||
@@ -153,11 +234,37 @@ QMessageBox::StandardButton CheckableMessageBox::information(
|
|||||||
decider,
|
decider,
|
||||||
buttons,
|
buttons,
|
||||||
defaultButton,
|
defaultButton,
|
||||||
QMessageBox::NoButton,
|
defaultButton,
|
||||||
buttonTextOverrides,
|
buttonTextOverrides,
|
||||||
msg.isEmpty() ? msgDoNotShowAgain() : msg);
|
msg.isEmpty() ? msgDoNotShowAgain() : msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheckableMessageBox::information_async(
|
||||||
|
QWidget *parent,
|
||||||
|
const QString &title,
|
||||||
|
const QString &text,
|
||||||
|
const CheckableDecider &decider,
|
||||||
|
QObject *guard,
|
||||||
|
std::function<void(QMessageBox::StandardButton choosenBtn)> callback,
|
||||||
|
QMessageBox::StandardButtons buttons,
|
||||||
|
QMessageBox::StandardButton defaultButton,
|
||||||
|
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides,
|
||||||
|
const QString &msg)
|
||||||
|
{
|
||||||
|
show(parent,
|
||||||
|
QMessageBox::Information,
|
||||||
|
title,
|
||||||
|
text,
|
||||||
|
decider,
|
||||||
|
buttons,
|
||||||
|
defaultButton,
|
||||||
|
defaultButton,
|
||||||
|
buttonTextOverrides,
|
||||||
|
msg.isEmpty() ? msgDoNotShowAgain() : msg,
|
||||||
|
guard,
|
||||||
|
callback);
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Resets all suppression settings for doNotAskAgainQuestion()
|
Resets all suppression settings for doNotAskAgainQuestion()
|
||||||
so all these message boxes are shown again.
|
so all these message boxes are shown again.
|
||||||
|
@@ -41,6 +41,19 @@ public:
|
|||||||
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides = {},
|
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides = {},
|
||||||
const QString &msg = {});
|
const QString &msg = {});
|
||||||
|
|
||||||
|
static void question_async(
|
||||||
|
QWidget *parent,
|
||||||
|
const QString &title,
|
||||||
|
const QString &question,
|
||||||
|
const CheckableDecider &decider,
|
||||||
|
QObject *guard = nullptr,
|
||||||
|
std::function<void(QMessageBox::StandardButton choosenBtn)> callback = nullptr,
|
||||||
|
QMessageBox::StandardButtons buttons = QMessageBox::Yes | QMessageBox::No,
|
||||||
|
QMessageBox::StandardButton defaultButton = QMessageBox::No,
|
||||||
|
QMessageBox::StandardButton acceptButton = QMessageBox::Yes,
|
||||||
|
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides = {},
|
||||||
|
const QString &msg = {});
|
||||||
|
|
||||||
static QMessageBox::StandardButton information(
|
static QMessageBox::StandardButton information(
|
||||||
QWidget *parent,
|
QWidget *parent,
|
||||||
const QString &title,
|
const QString &title,
|
||||||
@@ -51,6 +64,18 @@ public:
|
|||||||
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides = {},
|
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides = {},
|
||||||
const QString &msg = {});
|
const QString &msg = {});
|
||||||
|
|
||||||
|
static void information_async(
|
||||||
|
QWidget *parent,
|
||||||
|
const QString &title,
|
||||||
|
const QString &text,
|
||||||
|
const CheckableDecider &decider,
|
||||||
|
QObject *guard = nullptr,
|
||||||
|
std::function<void(QMessageBox::StandardButton choosenBtn)> callback = nullptr,
|
||||||
|
QMessageBox::StandardButtons buttons = QMessageBox::Ok,
|
||||||
|
QMessageBox::StandardButton defaultButton = QMessageBox::NoButton,
|
||||||
|
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides = {},
|
||||||
|
const QString &msg = {});
|
||||||
|
|
||||||
// Conversion convenience
|
// Conversion convenience
|
||||||
static void resetAllDoNotAskAgainQuestions();
|
static void resetAllDoNotAskAgainQuestions();
|
||||||
static bool hasSuppressedQuestions();
|
static bool hasSuppressedQuestions();
|
||||||
|
Reference in New Issue
Block a user