aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/checkablemessagebox.cpp
blob: d3ede7d4e7e02863cd4ea461dc3e2d05df432132 (plain)
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
// 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 "hostosinfo.h"
#include "qtcassert.h"
#include "utilstr.h"

#include <QApplication>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QSettings>
#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 QSettings *theSettings;

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)
{
    QMessageBox msgBox(parent);
    msgBox.setWindowTitle(title);
    msgBox.setIcon(icon);
    msgBox.setText(text);
    msgBox.setTextFormat(Qt::RichText);
    msgBox.setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);

    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 ")) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
            msgBox.setOptions(QMessageBox::Option::DontUseNativeDialog);
#endif
        }
    }

    if (decider.shouldAskAgain) {
        if (!decider.shouldAskAgain())
            return acceptButton;

        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());
    msgBox.exec();

    QMessageBox::StandardButton clickedBtn = msgBox.standardButton(msgBox.clickedButton());

    if (decider.doNotAskAgain && msgBox.checkBox()->isChecked()
        && (acceptButton == QMessageBox::NoButton || clickedBtn == acceptButton))
        decider.doNotAskAgain();
    return clickedBtn;
}

CheckableDecider::CheckableDecider(const QString &settingsSubKey)
{
    QTC_ASSERT(theSettings, return);
    shouldAskAgain = [settingsSubKey] {
        theSettings->beginGroup(QLatin1String(kDoNotAskAgainKey));
        bool shouldNotAsk = theSettings->value(settingsSubKey, false).toBool();
        theSettings->endGroup();
        return !shouldNotAsk;
    };
    doNotAskAgain =  [settingsSubKey] {
        theSettings->beginGroup(QLatin1String(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);
}

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,
                QMessageBox::NoButton,
                buttonTextOverrides,
                msg.isEmpty() ? msgDoNotShowAgain() : msg);
}

/*!
    Resets all suppression settings for doNotAskAgainQuestion()
    so all these message boxes are shown again.
 */
void CheckableMessageBox::resetAllDoNotAskAgainQuestions()
{
    QTC_ASSERT(theSettings, return);
    theSettings->beginGroup(QLatin1String(kDoNotAskAgainKey));
    theSettings->remove(QString());
    theSettings->endGroup();
}

/*!
    Returns whether any message boxes from doNotAskAgainQuestion() are suppressed
    in the settings.
*/
bool CheckableMessageBox::hasSuppressedQuestions()
{
    QTC_ASSERT(theSettings, return false);
    theSettings->beginGroup(QLatin1String(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(QSettings *settings)
{
    theSettings = settings;
}

} // namespace Utils