summaryrefslogtreecommitdiffstats
path: root/src/libs/installer/messageboxhandler.cpp
blob: 286009e2122f7bfcc2e9d6c6ea56872770b83348 (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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/**************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
**************************************************************************/

#include "messageboxhandler.h"

#include <QtCore/QDebug>

#include <QApplication>
#include <QDialogButtonBox>
#include <QPushButton>

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::MessageBoxHandler
    \brief The MessageBoxHandler class provides a modal dialog for informing the user or asking the
    user a question and receiving an answer.


    \badcode
    var result = QMessageBox.question("quit.question", "Installer", "Do you want to quit the installer?",
                                      QMessageBox.Yes | QMessageBox.No);
    if (result == QMessageBox.Yes) {
       // ...
    }
    \endcode

    \section2 Buttons in Message Boxes

    \c QMessageBox defines a list of common buttons:
    \list
        \li \l{QMessageBox::Ok}{OK}
        \li \l{QMessageBox::Open}{Open}
        \li \l{QMessageBox::Save}{Save}
        \li \l{QMessageBox::Cancel}{Cancel}
        \li \l{QMessageBox::Close}{Close}
        \li \l{QMessageBox::Discard}{Discard}
        \li \l{QMessageBox::Apply}{Apply}
        \li \l{QMessageBox::Reset}{Reset}
        \li \l{QMessageBox::RestoreDefaults}{RestoreDefaults}
        \li \l{QMessageBox::Help}{Help}
        \li \l{QMessageBox::SaveAll}{SaveAll}
        \li \l{QMessageBox::Yes}{Yes}
        \li \l{QMessageBox::YesToAll}{YesToAll}
        \li \l{QMessageBox::No}{No}
        \li \l{QMessageBox::NoToAll}{NoToAll}
        \li \l{QMessageBox::Abort}{Abort}
        \li \l{QMessageBox::Retry}{Retry}
        \li \l{QMessageBox::Ignore}{Ignore}
        \li \l{QMessageBox::NoButton}{NoButton}
    \endlist
*/

using namespace QInstaller;

// -- MessageBoxHandler

/*!
    \enum MessageBoxHandler::DefaultAction

    This enum value holds the default action for the message box handler:

    \value  AskUser
            Ask the end user for confirmation.
    \value  Accept
            Accept the message box.
    \value  Reject
            Reject the message box.
*/

/*!
    \enum MessageBoxHandler::MessageType

    This enum value holds the severity level of the message displayed in the message box:

    \value  criticalType
            Reports critical errors.
    \value  informationType
            Reports information about normal operations.
    \value  questionType
            Asks a question during normal operations.
    \value  warningType
            Reports non-critical errors.
*/

MessageBoxHandler *MessageBoxHandler::m_instance = nullptr;

MessageBoxHandler::MessageBoxHandler(QObject *parent)
    : QObject(parent)
    , m_defaultAction(MessageBoxHandler::AskUser)
{
}

/*!
    Returns a message box handler instance.
*/
MessageBoxHandler *MessageBoxHandler::instance()
{
    if (m_instance == nullptr)
        m_instance = new MessageBoxHandler(qApp);
    return m_instance;
}

/*!
    Returns the widget or window that is most suitable to become the parent of the message box.
    Returns \c 0 if an application cannot be found.
*/
QWidget *MessageBoxHandler::currentBestSuitParent()
{
    if (qobject_cast<QApplication*> (qApp) == nullptr)
        return nullptr;

    if (qApp->activeModalWidget())
        return qApp->activeModalWidget();

    return qApp->activeWindow();
}

/*!
    Returns an ordered list of buttons to display in the message box.
*/
QList<QMessageBox::Button> MessageBoxHandler::orderedButtons()
{
    static QList<QMessageBox::Button> buttons;
    if (!buttons.isEmpty())
        return buttons;
    buttons << QMessageBox::YesToAll << QMessageBox::Yes << QMessageBox::Ok << QMessageBox::Apply
        << QMessageBox::SaveAll << QMessageBox::Save <<QMessageBox::Retry << QMessageBox::Ignore
        << QMessageBox::Help << QMessageBox::RestoreDefaults << QMessageBox::Reset << QMessageBox::Open
        << QMessageBox::Cancel << QMessageBox::Close << QMessageBox::Abort << QMessageBox::Discard
        << QMessageBox::No << QMessageBox::NoToAll;
    return buttons;
}

/*!
    Sets the default action for the message box to \a defaultAction.
*/
void MessageBoxHandler::setDefaultAction(DefaultAction defaultAction)
{
    if (m_defaultAction == defaultAction)
        return;
    m_defaultAction = defaultAction;

    m_buttonOrder.clear();
    if (m_defaultAction != AskUser) {
        m_buttonOrder = orderedButtons();
    }

    if (m_defaultAction == Reject) {
        // If we want to reject everything, we need the lowest button. For example, if Cancel is existing it
        // could use Cancel, but if Close is existing it will use Close.
        std::reverse(m_buttonOrder.begin(), m_buttonOrder.end());
    }
}

/*!
    Sets the button that is used as the default button and that is automatically invoked if a
    message box is shown in automatic mode. The string \a identifier is used to identify the
    message box that the default button \a answer is associated to.
*/
void MessageBoxHandler::setAutomaticAnswer(const QString &identifier, QMessageBox::StandardButton answer)
{
    m_automaticAnswers.insert(identifier, answer);
}

// -- static

/*!
    Opens a critical message box with the parent \a parent, identifier \a identifier,
    title \a title, and text \a text.

    The standard buttons specified by \a buttons are added to the message box. \a button specifies
    the button that is used when \key Enter is pressed. \a button must refer to a button that is
    specified in \a buttons. If \a button is QMessageBox::NoButton, a suitable default is chosen
    automatically.

    Returns the identity of the standard button that was clicked. However, if \key Esc was pressed,
    returns the escape button.
*/
QMessageBox::StandardButton MessageBoxHandler::critical(QWidget *parent, const QString &identifier,
    const QString &title, const QString &text, QMessageBox::StandardButtons buttons,
    QMessageBox::StandardButton button)
{
    return instance()->showMessageBox(criticalType, parent, identifier, title, text, buttons, button);
}

/*!
    Opens an information message box with the parent \a parent, identifier \a identifier,
    title \a title, and text \a text.

    The standard buttons specified by \a buttons are added to the message box. \a button specifies
    the button that is used when \key Enter is pressed. \a button must refer to a button that is
    specified in \a buttons. If \a button is QMessageBox::NoButton, a suitable default is chosen
    automatically.

    Returns the identity of the standard button that was clicked. However, if \key Esc was pressed,
    returns the escape button.
*/
QMessageBox::StandardButton MessageBoxHandler::information(QWidget *parent, const QString &identifier,
    const QString &title, const QString &text, QMessageBox::StandardButtons buttons,
    QMessageBox::StandardButton button)
{
    return instance()->showMessageBox(informationType, parent, identifier, title, text, buttons, button);
}

/*!
    Opens a question message box with the parent \a parent, identifier \a identifier,
    title \a title, and text \a text.

    The standard buttons specified by \a buttons are added to the message box. \a button specifies
    the button that is used when \key Enter is pressed. \a button must refer to a button that is
    specified in \a buttons. If \a button is QMessageBox::NoButton, a suitable default is chosen
    automatically.

    Returns the identity of the standard button that was clicked. However, if \key Esc was pressed,
    returns the escape button.
*/
QMessageBox::StandardButton MessageBoxHandler::question(QWidget *parent, const QString &identifier,
    const QString &title, const QString &text, QMessageBox::StandardButtons buttons,
    QMessageBox::StandardButton button)
{
    return instance()->showMessageBox(questionType, parent, identifier, title, text, buttons, button);
}

/*!
    Opens a warning message box with the parent \a parent, identifier \a identifier,
    title \a title, and text \a text.

    The standard buttons specified by \a buttons are added to the message box. \a button specifies
    the button that is used when \key Enter is pressed. \a button must refer to a button that is
    specified in \a buttons. If \a button is QMessageBox::NoButton, a suitable default is chosen
    automatically.

    Returns the identity of the standard button that was clicked. However, if \key Esc was pressed,
    returns the escape button.
*/
QMessageBox::StandardButton MessageBoxHandler::warning(QWidget *parent, const QString &identifier,
    const QString &title, const QString &text, QMessageBox::StandardButtons buttons,
    QMessageBox::StandardButton button)
{
    return instance()->showMessageBox(warningType, parent, identifier, title, text, buttons, button);
}

// -- invokable

/*!
    Opens a critical message box with the identifier \a identifier, title \a title, and text
    \a text.

    The standard buttons specified by \a buttons are added to the message box. \a button specifies
    the button that is used when \key Enter is pressed. \a button must refer to a button that is
    specified in \a buttons. If \a button is QMessageBox::NoButton, a suitable default is chosen
    automatically.

    Returns the identity of the standard button that was clicked. However, if \key Esc was pressed,
    returns the escape button.
*/
int MessageBoxHandler::critical(const QString &identifier, const QString &title,
    const QString &text, int buttons, int button) const
{
    return showMessageBox(criticalType, currentBestSuitParent(), identifier, title, text,
        QMessageBox::StandardButtons(buttons), QMessageBox::StandardButton(button));
}

/*!
    Opens an information message box with the identifier \a identifier, title \a title, and text
    \a text.

    The standard buttons specified by \a buttons are added to the message box. \a button specifies
    the button that is used when \key Enter is pressed. \a button must refer to a button that is
    specified in \a buttons. If \a button is QMessageBox::NoButton, a suitable default is chosen
    automatically.

    Returns the identity of the standard button that was clicked. However, if \key Esc was pressed,
    returns the escape button.
*/
int MessageBoxHandler::information(const QString &identifier, const QString &title,
    const QString &text, int buttons, int button) const
{
    return showMessageBox(informationType, currentBestSuitParent(), identifier, title, text,
        QMessageBox::StandardButtons(buttons), QMessageBox::StandardButton(button));
}

/*!
    Opens a question message box with the identifier \a identifier, title \a title, and text
    \a text.

    The standard buttons specified by \a buttons are added to the message box. \a button specifies
    the button that is used when \key Enter is pressed. \a button must refer to a button that is
    specified in \a buttons. If \a button is QMessageBox::NoButton, a suitable default is chosen
    automatically.

    Returns the identity of the standard button that was clicked. However, if \key Esc was pressed,
    returns the escape button.
*/
int MessageBoxHandler::question(const QString &identifier, const QString &title,
    const QString &text, int buttons, int button) const
{
    return showMessageBox(questionType, currentBestSuitParent(), identifier, title, text,
        QMessageBox::StandardButtons(buttons), QMessageBox::StandardButton(button));
}

/*!
    Opens a warning message box with the identifier \a identifier, title \a title, and text \a text.

    The standard buttons specified by \a buttons are added to the message box. \a button specifies
    the button that is used when \key Enter is pressed. \a button must refer to a button that is
    specified in \a buttons. If \a button is QMessageBox::NoButton, a suitable default is chosen
    automatically.

    Returns the identity of the standard button that was clicked. However, if \key Esc was pressed,
    returns the escape button.
*/
int MessageBoxHandler::warning(const QString &identifier, const QString &title, const QString &text,
    int buttons, int button) const
{
    return showMessageBox(warningType, currentBestSuitParent(), identifier, title, text,
        QMessageBox::StandardButtons(buttons), QMessageBox::StandardButton(button));
}

// -- private

QMessageBox::StandardButton MessageBoxHandler::autoReply(QMessageBox::StandardButtons buttons) const
{
    if (buttons == QMessageBox::NoButton)
        return QMessageBox::NoButton;

    foreach (const QMessageBox::StandardButton &currentButton, m_buttonOrder) {
        if ((buttons & currentButton) != 0)
            return currentButton;
    }
    Q_ASSERT(!"the list must have all possible buttons");
    return QMessageBox::NoButton;
}

static QMessageBox::StandardButton showNewMessageBox(QWidget *parent, QMessageBox::Icon icon,
    const QString &title, const QString &text, QMessageBox::StandardButtons buttons,
    QMessageBox::StandardButton defaultButton)
{
    QMessageBox msgBox(icon, title, text, QMessageBox::NoButton, parent);
    QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox *>();
    Q_ASSERT(buttonBox != nullptr);

    uint mask = QMessageBox::FirstButton;
    while (mask <= QMessageBox::LastButton) {
        uint sb = buttons & mask;
        mask <<= 1;
        if (!sb)
            continue;
        QPushButton *button = msgBox.addButton((QMessageBox::StandardButton)sb);
        // Choose the first accept role as the default
        if (msgBox.defaultButton())
            continue;
        if ((defaultButton == QMessageBox::NoButton
            && buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
            || (defaultButton != QMessageBox::NoButton && sb == uint(defaultButton))) {
                msgBox.setDefaultButton(button);
        }
    }
#if defined(Q_OS_OSX)
    msgBox.setWindowModality(Qt::WindowModal);
#endif
    if (msgBox.exec() == -1)
        return QMessageBox::Cancel;
    return msgBox.standardButton(msgBox.clickedButton());
}

QMessageBox::StandardButton MessageBoxHandler::showMessageBox(MessageType messageType, QWidget *parent,
    const QString &identifier, const QString &title, const QString &text, QMessageBox::StandardButtons buttons,
    QMessageBox::StandardButton defaultButton) const
{
    static QHash<MessageType, QString> messageTypeHash;
    if (messageTypeHash.isEmpty()) {
        messageTypeHash.insert(criticalType, QLatin1String("critical"));
        messageTypeHash.insert(informationType, QLatin1String("information"));
        messageTypeHash.insert(questionType, QLatin1String("question"));
        messageTypeHash.insert(warningType, QLatin1String("warning"));
    };

    qDebug().nospace() << "Created " << messageTypeHash.value(messageType).toUtf8().constData()
                       << " message box " << identifier << ": " << title << ", " << text;

    if (qobject_cast<QApplication*> (qApp) == nullptr)
        return defaultButton;

    if (m_automaticAnswers.contains(identifier))
        return m_automaticAnswers.value(identifier);

    if (m_defaultAction == AskUser) {
        switch (messageType) {
            case criticalType:
                return showNewMessageBox(parent, QMessageBox::Critical, title, text, buttons, defaultButton);
            case informationType:
                return showNewMessageBox(parent, QMessageBox::Information, title, text, buttons, defaultButton);
            case questionType:
                return showNewMessageBox(parent, QMessageBox::Question, title, text, buttons, defaultButton);
            case warningType:
                return showNewMessageBox(parent, QMessageBox::Warning, title, text, buttons, defaultButton);
        }
    } else {
        return autoReply(buttons);
    }

    Q_ASSERT_X(false, Q_FUNC_INFO, "Something went really wrong.");
    return defaultButton;
}