QGCMessageBox.h 5.14 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

23 24
#ifdef UNITTEST_BUILD
    #include "UnitTest.h"
dogmaphobic's avatar
dogmaphobic committed
25
#endif
Don Gagne's avatar
Don Gagne committed
26 27 28 29 30 31 32 33 34

/// @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 {
35

Don Gagne's avatar
Don Gagne committed
36 37 38
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); }
39

Don Gagne's avatar
Don Gagne committed
40 41
    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); }
42

Don Gagne's avatar
Don Gagne committed
43 44
    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); }
45

Don Gagne's avatar
Don Gagne committed
46 47
    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); }
48

Don Gagne's avatar
Don Gagne committed
49
private slots:
nopeppermint's avatar
nopeppermint committed
50
    /// @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
51 52
    ///         unit testing.
    int exec(void) { return QMessageBox::exec(); }
53

Don Gagne's avatar
Don Gagne committed
54
private:
Don Gagne's avatar
Don Gagne committed
55 56 57 58
    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));
59

Don Gagne's avatar
Don Gagne committed
60 61
        // 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.
62

Don Gagne's avatar
Don Gagne committed
63 64 65 66 67 68 69 70
        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);
71

Don Gagne's avatar
Don Gagne committed
72 73 74 75 76 77
        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));
        }
78

Don Gagne's avatar
Don Gagne committed
79 80 81
        return (parent == NULL) ? MainWindow::instance() : parent;
    }

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

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

Don Gagne's avatar
Don Gagne committed
89
        parent = _validateParameters(buttons, &defaultButton, parent);
Don Gagne's avatar
Don Gagne committed
90 91

        if (MainWindow::instance()) {
92 93 94
            if (parent == NULL) {
                parent = MainWindow::instance();
            }
Don Gagne's avatar
Don Gagne committed
95
        }
96

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

99
#ifdef UNITTEST_BUILD
Don Gagne's avatar
Don Gagne committed
100 101 102
        if (qgcApp()->runningUnitTests()) {
            return UnitTest::_messageBox(icon, title, text, buttons, defaultButton);
        } else
Don Gagne's avatar
Don Gagne committed
103
#endif
Don Gagne's avatar
Don Gagne committed
104
        {
105
#ifdef Q_OS_MAC
Don Gagne's avatar
Don Gagne committed
106 107 108 109
            QString emptyTitle;
            QMessageBox box(icon, emptyTitle, title, buttons, parent);
            box.setDefaultButton(defaultButton);
            box.setInformativeText(text);
110 111 112 113
            // 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
114
#else
Don Gagne's avatar
Don Gagne committed
115 116
            QMessageBox box(icon, title, text, buttons, parent);
            box.setDefaultButton(defaultButton);
Don Gagne's avatar
Don Gagne committed
117
#endif
Don Gagne's avatar
Don Gagne committed
118
            return static_cast<QMessageBox::StandardButton>(box.exec());
Don Gagne's avatar
Don Gagne committed
119 120 121 122 123
        }
    }
};

#endif