FactPanelController.cc 4.4 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.
 *
 ****************************************************************************/
dogmaphobic's avatar
dogmaphobic committed
9

10 11

#include "FactPanelController.h"
12
#include "MultiVehicleManager.h"
13
#include "UAS.h"
14
#include "QGCApplication.h"
15

Don Gagne's avatar
Don Gagne committed
16 17
#include <QQmlEngine>

18 19 20
/// @file
///     @author Don Gagne <don@thegagnes.com>

Don Gagne's avatar
Don Gagne committed
21 22
QGC_LOGGING_CATEGORY(FactPanelControllerLog, "FactPanelControllerLog")

23
FactPanelController::FactPanelController(bool standaloneUnitTesting)
24 25 26 27
    : _vehicle(NULL)
    , _uas(NULL)
    , _autopilot(NULL)
    , _factPanel(NULL)
28
{
29
    _vehicle = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle();
dogmaphobic's avatar
dogmaphobic committed
30

31 32 33 34
    if (_vehicle) {
        _uas = _vehicle->uas();
        _autopilot = _vehicle->autopilotPlugin();
    }
dogmaphobic's avatar
dogmaphobic committed
35

36 37 38 39
    if (!standaloneUnitTesting) {
        // Do a delayed check for the _factPanel finally being set correctly from Qml
        QTimer::singleShot(1000, this, &FactPanelController::_checkForMissingFactPanel);
    }
40 41 42 43 44 45 46 47 48
}

QQuickItem* FactPanelController::factPanel(void)
{
    return _factPanel;
}

void FactPanelController::setFactPanel(QQuickItem* panel)
{
Don Gagne's avatar
Don Gagne committed
49
    // Once we finally have the _factPanel member set, send any
50
    // missing fact notices that were waiting to go out
dogmaphobic's avatar
dogmaphobic committed
51

52
    _factPanel = panel;
53
    foreach (const QString &missingParam, _delayedMissingParams) {
Don Gagne's avatar
Don Gagne committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67
        _notifyPanelMissingParameter(missingParam);
    }
    _delayedMissingParams.clear();
}

void FactPanelController::_notifyPanelMissingParameter(const QString& missingParam)
{
    if (_factPanel) {
        QVariant returnedValue;

        QMetaObject::invokeMethod(_factPanel,
                                  "showMissingParameterOverlay",
                                  Q_RETURN_ARG(QVariant, returnedValue),
                                  Q_ARG(QVariant, missingParam));
68 69 70
    }
}

Don Gagne's avatar
Don Gagne committed
71
void FactPanelController::_notifyPanelErrorMsg(const QString& errorMsg)
72
{
Don Gagne's avatar
Don Gagne committed
73 74
    if (_factPanel) {
        QVariant returnedValue;
75

Don Gagne's avatar
Don Gagne committed
76 77 78 79 80
        QMetaObject::invokeMethod(_factPanel,
                                  "showError",
                                  Q_RETURN_ARG(QVariant, returnedValue),
                                  Q_ARG(QVariant, errorMsg));
    }
81 82
}

Don Gagne's avatar
Don Gagne committed
83
void FactPanelController::_reportMissingParameter(int componentId, const QString& name)
84
{
Don Gagne's avatar
Don Gagne committed
85
    qgcApp()->reportMissingParameter(componentId, name);
dogmaphobic's avatar
dogmaphobic committed
86

Don Gagne's avatar
Don Gagne committed
87
    QString missingParam = QString("%1:%2").arg(componentId).arg(name);
dogmaphobic's avatar
dogmaphobic committed
88

Don Gagne's avatar
Don Gagne committed
89
    // If missing parameters a reported from the constructor of a derived class we
90 91 92 93
    // will not have access to _factPanel yet. Just record list of missing facts
    // in that case instead of notify. Once _factPanel is available they will be
    // send out for real.
    if (_factPanel) {
Don Gagne's avatar
Don Gagne committed
94
        _notifyPanelMissingParameter(missingParam);
95
    } else {
Don Gagne's avatar
Don Gagne committed
96
        _delayedMissingParams += missingParam;
97 98 99
    }
}

Don Gagne's avatar
Don Gagne committed
100
bool FactPanelController::_allParametersExists(int componentId, QStringList names)
101 102
{
    bool noMissingFacts = true;
dogmaphobic's avatar
dogmaphobic committed
103

104
    foreach (const QString &name, names) {
105
        if (_autopilot && !_autopilot->parameterExists(componentId, name)) {
Don Gagne's avatar
Don Gagne committed
106
            _reportMissingParameter(componentId, name);
107 108 109
            noMissingFacts = false;
        }
    }
dogmaphobic's avatar
dogmaphobic committed
110

111 112 113 114 115 116
    return noMissingFacts;
}

void FactPanelController::_checkForMissingFactPanel(void)
{
    if (!_factPanel) {
Don Gagne's avatar
Don Gagne committed
117
        _showInternalError("Incorrect FactPanel Qml implementation. FactPanelController used without passing in factPanel.");
118
    }
Don Gagne's avatar
Don Gagne committed
119 120
}

dogmaphobic's avatar
dogmaphobic committed
121
Fact* FactPanelController::getParameterFact(int componentId, const QString& name, bool reportMissing)
Don Gagne's avatar
Don Gagne committed
122
{
123
    if (_autopilot && _autopilot->parameterExists(componentId, name)) {
Don Gagne's avatar
Don Gagne committed
124 125 126 127
        Fact* fact = _autopilot->getParameterFact(componentId, name);
        QQmlEngine::setObjectOwnership(fact, QQmlEngine::CppOwnership);
        return fact;
    } else {
dogmaphobic's avatar
dogmaphobic committed
128 129
        if(reportMissing)
            _reportMissingParameter(componentId, name);
Don Gagne's avatar
Don Gagne committed
130 131 132 133 134 135
        return NULL;
    }
}

bool FactPanelController::parameterExists(int componentId, const QString& name)
{
136
    return _autopilot ? _autopilot->parameterExists(componentId, name) : false;
Don Gagne's avatar
Don Gagne committed
137 138 139 140 141 142
}

void FactPanelController::_showInternalError(const QString& errorMsg)
{
    _notifyPanelErrorMsg(QString("Internal Error: %1").arg(errorMsg));
    qCWarning(FactPanelControllerLog) << "Internal Error" << errorMsg;
143
    qgcApp()->showMessage(errorMsg);
dogmaphobic's avatar
dogmaphobic committed
144
}