1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "checkablemessagebox.h"
#include "guiutils.h"
#include "hostosinfo.h"
#include "qtcassert.h"
#include "qtcsettings.h"
#include "utilstr.h"
#include <QApplication>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QPointer>
#include <QPushButton>
#include <QStyle>
#include <QTextEdit>
/*!
\class Utils::CheckableMessageBox
\inmodule QtCreator
\brief The CheckableMessageBox class implements a message box suitable for
questions with a \uicontrol {Do not ask again} or \uicontrol {Do not show again}
checkbox.
Emulates the QMessageBox API with
static conveniences. The message label can open external URLs.
*/
static const char kDoNotAskAgainKey[] = "DoNotAskAgain";
namespace Utils {
static QtcSettings *theSettings;
static void prepare(QMessageBox::Icon icon,
const QString &title,
const QString &text,
const CheckableDecider &decider,
QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton,
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides,
const QString &msg,
QMessageBox &msgBox)
{
msgBox.setWindowTitle(title);
msgBox.setIcon(icon);
msgBox.setText(text);
msgBox.setTextFormat(Qt::RichText);
msgBox.setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
if (HostOsInfo::isMacHost()) {
// Message boxes on macOS cannot display links.
// If the message contains a link, we need to disable native dialogs.
if (text.contains("<a "))
msgBox.setOptions(QMessageBox::Option::DontUseNativeDialog);
// Workaround for QTBUG-118241, fixed in Qt 6.6.1
#if QT_VERSION < QT_VERSION_CHECK(6, 6, 1)
if (!buttonTextOverrides.isEmpty())
msgBox.setOptions(QMessageBox::Option::DontUseNativeDialog);
#endif // QT_VERSION < QT_VERSION_CHECK(6, 6, 1)
}
#endif
if (decider.shouldAskAgain) {
msgBox.setCheckBox(new QCheckBox);
msgBox.checkBox()->setChecked(false);
msgBox.checkBox()->setText(msg);
}
msgBox.setStandardButtons(buttons);
msgBox.setDefaultButton(defaultButton);
for (auto it = buttonTextOverrides.constBegin(); it != buttonTextOverrides.constEnd(); ++it)
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(dialogParent(parent));
prepare(icon, title, text, decider, buttons, defaultButton, buttonTextOverrides, msg, msgBox);
msgBox.exec();
QMessageBox::StandardButton clickedBtn = msgBox.standardButton(msgBox.clickedButton());
if (decider.doNotAskAgain && msgBox.checkBox()->isChecked()
&& (acceptButton == QMessageBox::NoButton || clickedBtn == acceptButton))
decider.doNotAskAgain();
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 ? guard : qApp,
[callback, acceptButton] { callback(acceptButton); },
Qt::QueuedConnection);
}
return;
}
QMessageBox *msgBox = new QMessageBox(dialogParent(parent));
prepare(icon, title, text, decider, buttons, defaultButton, buttonTextOverrides, msg, *msgBox);
std::optional<QPointer<QObject>> guardPtr;
if (guard)
guardPtr = guard;
QObject::connect(msgBox,
&QMessageBox::finished,
[guardPtr, 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 && (!guardPtr || *guardPtr))
callback(clickedBtn);
msgBox->deleteLater();
});
msgBox->show();
}
CheckableDecider::CheckableDecider(const Key &settingsSubKey)
{
QTC_ASSERT(theSettings, return);
shouldAskAgain = [settingsSubKey] {
theSettings->beginGroup(kDoNotAskAgainKey);
bool shouldNotAsk = theSettings->value(settingsSubKey, false).toBool();
theSettings->endGroup();
return !shouldNotAsk;
};
doNotAskAgain = [settingsSubKey] {
theSettings->beginGroup(kDoNotAskAgainKey);
theSettings->setValue(settingsSubKey, true);
theSettings->endGroup();
};
}
CheckableDecider::CheckableDecider(bool *storage)
{
shouldAskAgain = [storage] { return !*storage; };
doNotAskAgain = [storage] { *storage = true; };
}
QMessageBox::StandardButton CheckableMessageBox::question(
QWidget *parent,
const QString &title,
const QString &question,
const CheckableDecider &decider,
QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton,
QMessageBox::StandardButton acceptButton,
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides,
const QString &msg)
{
return exec(parent,
QMessageBox::Question,
title,
question,
decider,
buttons,
defaultButton,
acceptButton,
buttonTextOverrides,
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(
QWidget *parent,
const QString &title,
const QString &text,
const CheckableDecider &decider,
QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton,
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides,
const QString &msg)
{
return exec(parent,
QMessageBox::Information,
title,
text,
decider,
buttons,
defaultButton,
defaultButton,
buttonTextOverrides,
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()
so all these message boxes are shown again.
*/
void CheckableMessageBox::resetAllDoNotAskAgainQuestions()
{
QTC_ASSERT(theSettings, return);
theSettings->beginGroup(kDoNotAskAgainKey);
theSettings->remove(Key());
theSettings->endGroup();
}
/*!
Returns whether any message boxes from doNotAskAgainQuestion() are suppressed
in the settings.
*/
bool CheckableMessageBox::hasSuppressedQuestions()
{
QTC_ASSERT(theSettings, return false);
theSettings->beginGroup(kDoNotAskAgainKey);
const bool hasSuppressed = !theSettings->childKeys().isEmpty()
|| !theSettings->childGroups().isEmpty();
theSettings->endGroup();
return hasSuppressed;
}
/*!
Returns the standard \uicontrol {Do not ask again} check box text.
*/
QString CheckableMessageBox::msgDoNotAskAgain()
{
return Tr::tr("Do not &ask again");
}
/*!
Returns the standard \uicontrol {Do not show again} check box text.
*/
QString CheckableMessageBox::msgDoNotShowAgain()
{
return Tr::tr("Do not &show again");
}
void CheckableMessageBox::initialize(QtcSettings *settings)
{
theSettings = settings;
}
} // namespace Utils
|