Skip to content
QGCMessageBox.h 5.75 KiB
Newer Older
Don Gagne's avatar
Don Gagne committed
/*=====================================================================
Don Gagne's avatar
Don Gagne committed
 QGroundControl Open Source Ground Control Station
Don Gagne's avatar
Don Gagne committed
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
Don Gagne's avatar
Don Gagne committed
 This file is part of the QGROUNDCONTROL project
Don Gagne's avatar
Don Gagne committed
 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.
Don Gagne's avatar
Don Gagne committed
 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.
Don Gagne's avatar
Don Gagne committed
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
Don Gagne's avatar
Don Gagne committed
 ======================================================================*/

#ifndef QGCMESSAGEBOX_H
#define QGCMESSAGEBOX_H

#ifdef __mobile__
#error Should not be included in mobile builds
#endif

Don Gagne's avatar
Don Gagne committed
#include <QMessageBox>

#include "MainWindow.h"
Don Gagne's avatar
Don Gagne committed
#include "QGCApplication.h"
Don Gagne's avatar
Don Gagne committed
#ifdef QT_DEBUG
dogmaphobic's avatar
dogmaphobic committed
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
#include "UnitTest.h"
Don Gagne's avatar
Don Gagne committed
#endif
dogmaphobic's avatar
dogmaphobic committed
#endif
Don Gagne's avatar
Don Gagne committed

/// @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 {
Don Gagne's avatar
Don Gagne committed
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); }
Don Gagne's avatar
Don Gagne committed
    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); }
Don Gagne's avatar
Don Gagne committed
    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); }
Don Gagne's avatar
Don Gagne committed
    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); }
Don Gagne's avatar
Don Gagne committed
private slots:
nopeppermint's avatar
nopeppermint committed
    /// @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
    ///         unit testing.
    int exec(void) { return QMessageBox::exec(); }
Don Gagne's avatar
Don Gagne committed
private:
Don Gagne's avatar
Don Gagne committed
    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));
Don Gagne's avatar
Don Gagne committed
        // 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.
Don Gagne's avatar
Don Gagne committed
        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);
Don Gagne's avatar
Don Gagne committed
        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));
        }
Don Gagne's avatar
Don Gagne committed
        return (parent == NULL) ? MainWindow::instance() : parent;
    }

Don Gagne's avatar
Don Gagne committed
    static StandardButton _messageBox(Icon icon, const QString& title, const QString& text, StandardButtons buttons, StandardButton defaultButton, QWidget* parent)
    {
Don Gagne's avatar
Don Gagne committed
        // You can't use QGCMessageBox if QGCApplication is not created yet.
        Q_ASSERT(qgcApp());
        Q_ASSERT_X(QThread::currentThread() == qgcApp()->thread(), "Threading issue", "QGCMessageBox can only be called from main thread");
Don Gagne's avatar
Don Gagne committed
        parent = _validateParameters(buttons, &defaultButton, parent);
Don Gagne's avatar
Don Gagne committed

        if (MainWindow::instance()) {
            if (parent == NULL) {
                parent = MainWindow::instance();
            }
Don Gagne's avatar
Don Gagne committed
        }
Don Gagne's avatar
Don Gagne committed
        qDebug() << "QGCMessageBox (unit testing)" << title << text;

Don Gagne's avatar
Don Gagne committed
#ifdef QT_DEBUG
dogmaphobic's avatar
dogmaphobic committed
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
        if (qgcApp()->runningUnitTests()) {
            return UnitTest::_messageBox(icon, title, text, buttons, defaultButton);
        } else
dogmaphobic's avatar
dogmaphobic committed
#endif
Don Gagne's avatar
Don Gagne committed
#endif
Don Gagne's avatar
Don Gagne committed
        {
dogmaphobic's avatar
dogmaphobic committed
#ifdef __macos__
Don Gagne's avatar
Don Gagne committed
            QString emptyTitle;
            QMessageBox box(icon, emptyTitle, title, buttons, parent);
            box.setDefaultButton(defaultButton);
            box.setInformativeText(text);
            // 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
#else
Don Gagne's avatar
Don Gagne committed
            QMessageBox box(icon, title, text, buttons, parent);
            box.setDefaultButton(defaultButton);
Don Gagne's avatar
Don Gagne committed
#endif
Don Gagne's avatar
Don Gagne committed
            return static_cast<QMessageBox::StandardButton>(box.exec());