QGCMessageBox.h 5.18 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9

Don Gagne's avatar
Don Gagne committed
10 11 12 13

#ifndef QGCMESSAGEBOX_H
#define QGCMESSAGEBOX_H

14 15 16 17
#ifdef __mobile__
#error Should not be included in mobile builds
#endif

Don Gagne's avatar
Don Gagne committed
18 19 20
#include <QMessageBox>

#include "MainWindow.h"
Don Gagne's avatar
Don Gagne committed
21
#include "QGCApplication.h"
22

Don Gagne's avatar
Don Gagne committed
23
#ifdef QT_DEBUG
dogmaphobic's avatar
dogmaphobic committed
24
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
25
#include "UnitTest.h"
Don Gagne's avatar
Don Gagne committed
26
#endif
27

dogmaphobic's avatar
dogmaphobic committed
28
#endif
Don Gagne's avatar
Don Gagne committed
29 30 31 32 33 34 35 36 37

/// @file
///     @brief Subclass of QMessageBox which re-implements the static public functions. There are two reasons for this:
///             1) The QMessageBox implementation on OSX does now show the title string. This leads to message
///             boxes which don't make much sense. So on OSX we set title to text and text to informative text.
///             2) If parent is NULL, we set parent to MainWindow::instance
///     @author Don Gagne <don@thegagnes.com>

class QGCMessageBox : public QMessageBox {
38

Don Gagne's avatar
Don Gagne committed
39 40 41
public:
    static StandardButton critical(const QString& title, const QString& text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton, QWidget* parent = NULL)
        { return _messageBox(QMessageBox::Critical, title, text, buttons, defaultButton, parent); }
42

Don Gagne's avatar
Don Gagne committed
43 44
    static StandardButton information(const QString & title, const QString& text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton, QWidget* parent = NULL)
        { return _messageBox(QMessageBox::Information, title, text, buttons, defaultButton, parent); }
45

Don Gagne's avatar
Don Gagne committed
46 47
    static StandardButton question(const QString& title, const QString& text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton, QWidget* parent = NULL)
        { return _messageBox(QMessageBox::Question, title, text, buttons, defaultButton, parent); }
48

Don Gagne's avatar
Don Gagne committed
49 50
    static StandardButton warning(const QString& title, const QString& text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton, QWidget* parent = NULL)
        { return _messageBox(QMessageBox::Warning, title, text, buttons, defaultButton, parent); }
51

Don Gagne's avatar
Don Gagne committed
52
private slots:
nopeppermint's avatar
nopeppermint committed
53
    /// @brief The exec slot is private because when only want QGCMessageBox users to use the static methods. Otherwise it will break
Don Gagne's avatar
Don Gagne committed
54 55
    ///         unit testing.
    int exec(void) { return QMessageBox::exec(); }
56

Don Gagne's avatar
Don Gagne committed
57
private:
Don Gagne's avatar
Don Gagne committed
58 59 60 61
    static QWidget* _validateParameters(StandardButtons buttons, StandardButton* defaultButton, QWidget* parent)
    {
        // This is an obsolete bit which unit tests use for signalling. It should not be used in regular code.
        Q_ASSERT(!(buttons & QMessageBox::Escape));
62

Don Gagne's avatar
Don Gagne committed
63 64
        // If there is more than one button displayed, make sure a default button is set. Without this unit test code
        // will not be able to respond to unexpected message boxes.
65

Don Gagne's avatar
Don Gagne committed
66 67 68 69 70 71 72 73
        unsigned int bits = static_cast<unsigned int>(buttons);
        int buttonCount = 0;
        for (size_t i=0; i<sizeof(bits)*8; i++) {
            if (bits & (1 << i)) {
                buttonCount++;
            }
        }
        Q_ASSERT(buttonCount != 0);
74

Don Gagne's avatar
Don Gagne committed
75 76 77 78 79 80
        if (buttonCount > 1) {
            Q_ASSERT(buttons & *defaultButton);
        } else {
            // Force default button to be set correctly for single button case to make unit test code simpler
            *defaultButton = static_cast<QMessageBox::StandardButton>(static_cast<int>(buttons));
        }
81

Don Gagne's avatar
Don Gagne committed
82 83 84
        return (parent == NULL) ? MainWindow::instance() : parent;
    }

Don Gagne's avatar
Don Gagne committed
85 86
    static StandardButton _messageBox(Icon icon, const QString& title, const QString& text, StandardButtons buttons, StandardButton defaultButton, QWidget* parent)
    {
Don Gagne's avatar
Don Gagne committed
87 88
        // You can't use QGCMessageBox if QGCApplication is not created yet.
        Q_ASSERT(qgcApp());
89

90
        Q_ASSERT_X(QThread::currentThread() == qgcApp()->thread(), "Threading issue", "QGCMessageBox can only be called from main thread");
91

Don Gagne's avatar
Don Gagne committed
92
        parent = _validateParameters(buttons, &defaultButton, parent);
Don Gagne's avatar
Don Gagne committed
93 94

        if (MainWindow::instance()) {
95 96 97
            if (parent == NULL) {
                parent = MainWindow::instance();
            }
Don Gagne's avatar
Don Gagne committed
98
        }
99

Don Gagne's avatar
Don Gagne committed
100 101
        qDebug() << "QGCMessageBox (unit testing)" << title << text;

Don Gagne's avatar
Don Gagne committed
102
#ifdef QT_DEBUG
dogmaphobic's avatar
dogmaphobic committed
103
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
104 105 106
        if (qgcApp()->runningUnitTests()) {
            return UnitTest::_messageBox(icon, title, text, buttons, defaultButton);
        } else
dogmaphobic's avatar
dogmaphobic committed
107
#endif
Don Gagne's avatar
Don Gagne committed
108
#endif
Don Gagne's avatar
Don Gagne committed
109
        {
dogmaphobic's avatar
dogmaphobic committed
110
#ifdef __macos__
Don Gagne's avatar
Don Gagne committed
111 112 113 114
            QString emptyTitle;
            QMessageBox box(icon, emptyTitle, title, buttons, parent);
            box.setDefaultButton(defaultButton);
            box.setInformativeText(text);
115 116 117 118
            // Get this thing off a proper size
            QSpacerItem* horizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
            QGridLayout* layout = (QGridLayout*)box.layout();
            layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
Don Gagne's avatar
Don Gagne committed
119
#else
Don Gagne's avatar
Don Gagne committed
120 121
            QMessageBox box(icon, title, text, buttons, parent);
            box.setDefaultButton(defaultButton);
Don Gagne's avatar
Don Gagne committed
122
#endif
Don Gagne's avatar
Don Gagne committed
123
            return static_cast<QMessageBox::StandardButton>(box.exec());
Don Gagne's avatar
Don Gagne committed
124 125 126 127 128
        }
    }
};

#endif