FactPanelController.cc 4.33 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 24 25 26 27
FactPanelController::FactPanelController(void)
    : _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 40 41 42 43 44 45 46
    // Do a delayed check for the _factPanel finally being set correctly from Qml
    QTimer::singleShot(1000, this, &FactPanelController::_checkForMissingFactPanel);
}

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

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

50
    _factPanel = panel;
51
    foreach (const QString &missingParam, _delayedMissingParams) {
Don Gagne's avatar
Don Gagne committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65
        _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));
66 67 68
    }
}

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

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

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

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

Don Gagne's avatar
Don Gagne committed
87
    // If missing parameters a reported from the constructor of a derived class we
88 89 90 91
    // 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
92
        _notifyPanelMissingParameter(missingParam);
93
    } else {
Don Gagne's avatar
Don Gagne committed
94
        _delayedMissingParams += missingParam;
95 96 97
    }
}

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

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

109 110 111 112 113 114
    return noMissingFacts;
}

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

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

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

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