QGCMessageBox.h 5.73 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1
/*=====================================================================
2

Don Gagne's avatar
Don Gagne committed
3
 QGroundControl Open Source Ground Control Station
4

Don Gagne's avatar
Don Gagne committed
5
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
6

Don Gagne's avatar
Don Gagne committed
7
 This file is part of the QGROUNDCONTROL project
8

Don Gagne's avatar
Don Gagne committed
9 10 11 12
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
13

Don Gagne's avatar
Don Gagne committed
14 15 16 17
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
18

Don Gagne's avatar
Don Gagne committed
19 20
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
21

Don Gagne's avatar
Don Gagne committed
22 23 24 25 26 27 28 29
 ======================================================================*/

#ifndef QGCMESSAGEBOX_H
#define QGCMESSAGEBOX_H

#include <QMessageBox>

#include "MainWindow.h"
Don Gagne's avatar
Don Gagne committed
30
#include "QGCApplication.h"
Don Gagne's avatar
Don Gagne committed
31
#ifdef QT_DEBUG
dogmaphobic's avatar
dogmaphobic committed
32
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
33
#include "UnitTest.h"
Don Gagne's avatar
Don Gagne committed
34
#endif
dogmaphobic's avatar
dogmaphobic committed
35
#endif
Don Gagne's avatar
Don Gagne committed
36 37 38 39 40 41 42 43 44

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

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

Don Gagne's avatar
Don Gagne committed
50 51
    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); }
52

Don Gagne's avatar
Don Gagne committed
53 54
    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); }
55

Don Gagne's avatar
Don Gagne committed
56 57
    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); }
58

Don Gagne's avatar
Don Gagne committed
59 60 61 62
private slots:
    /// @brief The exec slot is private becasue when only want QGCMessageBox users to use the static methods. Otherwise it will break
    ///         unit testing.
    int exec(void) { return QMessageBox::exec(); }
63

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

Don Gagne's avatar
Don Gagne committed
70 71
        // 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.
72

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

Don Gagne's avatar
Don Gagne committed
82 83 84 85 86 87
        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));
        }
88

Don Gagne's avatar
Don Gagne committed
89 90 91
        return (parent == NULL) ? MainWindow::instance() : parent;
    }

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

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

Don Gagne's avatar
Don Gagne committed
99
        parent = _validateParameters(buttons, &defaultButton, parent);
Don Gagne's avatar
Don Gagne committed
100 101 102

        if (MainWindow::instance()) {
            MainWindow::instance()->hideSplashScreen();
103 104 105
            if (parent == NULL) {
                parent = MainWindow::instance();
            }
Don Gagne's avatar
Don Gagne committed
106
        }
107

Don Gagne's avatar
Don Gagne committed
108
#ifdef QT_DEBUG
dogmaphobic's avatar
dogmaphobic committed
109
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
110
        if (qgcApp()->runningUnitTests()) {
Don Gagne's avatar
Don Gagne committed
111
            qDebug() << "QGCMessageBox (unit testing)" << title << text;
Don Gagne's avatar
Don Gagne committed
112 113
            return UnitTest::_messageBox(icon, title, text, buttons, defaultButton);
        } else
dogmaphobic's avatar
dogmaphobic committed
114
#endif
Don Gagne's avatar
Don Gagne committed
115
#endif
Don Gagne's avatar
Don Gagne committed
116
        {
Don Gagne's avatar
Don Gagne committed
117
#ifdef Q_OS_MAC
Don Gagne's avatar
Don Gagne committed
118 119 120 121
            QString emptyTitle;
            QMessageBox box(icon, emptyTitle, title, buttons, parent);
            box.setDefaultButton(defaultButton);
            box.setInformativeText(text);
122 123 124 125
            // 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
126
#else
Don Gagne's avatar
Don Gagne committed
127 128
            QMessageBox box(icon, title, text, buttons, parent);
            box.setDefaultButton(defaultButton);
Don Gagne's avatar
Don Gagne committed
129
#endif
Don Gagne's avatar
Don Gagne committed
130
            return static_cast<QMessageBox::StandardButton>(box.exec());
Don Gagne's avatar
Don Gagne committed
131 132 133 134 135
        }
    }
};

#endif