FactPanelController.cc 4.64 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
#include "FactPanelController.h"
11
#include "MultiVehicleManager.h"
12
#include "UAS.h"
13
#include "QGCApplication.h"
14
#include "ParameterManager.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()
24
{
25
    _vehicle = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle();
26 27 28
    if (_vehicle) {
        _uas = _vehicle->uas();
        _autopilot = _vehicle->autopilotPlugin();
29 30
    } else {
        _vehicle = qgcApp()->toolbox()->multiVehicleManager()->offlineEditingVehicle();
31
    }
DonLakeFlyer's avatar
DonLakeFlyer committed
32 33 34 35

    _missingParametersTimer.setInterval(500);
    _missingParametersTimer.setSingleShot(true);
    connect(&_missingParametersTimer, &QTimer::timeout, this, &FactPanelController::_checkForMissingParameters);
Don Gagne's avatar
Don Gagne committed
36 37 38 39
}

void FactPanelController::_notifyPanelMissingParameter(const QString& missingParam)
{
40
    if (qgcApp()->mainRootWindow()) {
Don Gagne's avatar
Don Gagne committed
41
        QVariant returnedValue;
42
        QMetaObject::invokeMethod(
43
            qgcApp()->mainRootWindow(),
44 45 46
            "showMissingParameterOverlay",
            Q_RETURN_ARG(QVariant, returnedValue),
            Q_ARG(QVariant, missingParam));
47 48 49
    }
}

Don Gagne's avatar
Don Gagne committed
50
void FactPanelController::_notifyPanelErrorMsg(const QString& errorMsg)
51
{
52
    if(qgcApp()->mainRootWindow()) {
Don Gagne's avatar
Don Gagne committed
53
        QVariant returnedValue;
54
        QMetaObject::invokeMethod(
55
            qgcApp()->mainRootWindow(),
56 57 58
            "showFactError",
            Q_RETURN_ARG(QVariant, returnedValue),
            Q_ARG(QVariant, errorMsg));
Don Gagne's avatar
Don Gagne committed
59
    }
60 61
}

Don Gagne's avatar
Don Gagne committed
62
void FactPanelController::_reportMissingParameter(int componentId, const QString& name)
63
{
Don Gagne's avatar
Don Gagne committed
64
    qgcApp()->reportMissingParameter(componentId, name);
dogmaphobic's avatar
dogmaphobic committed
65

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

68 69
    qCWarning(FactPanelControllerLog) << "Missing parameter:" << missingParam;

Don Gagne's avatar
Don Gagne committed
70
    // If missing parameters a reported from the constructor of a derived class we
71 72 73
    // 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.
74
    if (qgcApp()->mainRootWindow()) {
Don Gagne's avatar
Don Gagne committed
75
        _notifyPanelMissingParameter(missingParam);
76
    } else {
Don Gagne's avatar
Don Gagne committed
77
        _delayedMissingParams += missingParam;
78 79 80
    }
}

Don Gagne's avatar
Don Gagne committed
81
bool FactPanelController::_allParametersExists(int componentId, QStringList names)
82 83
{
    bool noMissingFacts = true;
dogmaphobic's avatar
dogmaphobic committed
84

85
    foreach (const QString &name, names) {
86
        if (_vehicle && !_vehicle->parameterManager()->parameterExists(componentId, name)) {
Don Gagne's avatar
Don Gagne committed
87
            _reportMissingParameter(componentId, name);
88 89 90
            noMissingFacts = false;
        }
    }
dogmaphobic's avatar
dogmaphobic committed
91

92 93 94
    return noMissingFacts;
}

Don Gagne's avatar
Don Gagne committed
95

dogmaphobic's avatar
dogmaphobic committed
96
Fact* FactPanelController::getParameterFact(int componentId, const QString& name, bool reportMissing)
Don Gagne's avatar
Don Gagne committed
97
{
98 99
    if (_vehicle && _vehicle->parameterManager()->parameterExists(componentId, name)) {
        Fact* fact = _vehicle->parameterManager()->getParameter(componentId, name);
Don Gagne's avatar
Don Gagne committed
100 101 102
        QQmlEngine::setObjectOwnership(fact, QQmlEngine::CppOwnership);
        return fact;
    } else {
103
        if (reportMissing) {
dogmaphobic's avatar
dogmaphobic committed
104
            _reportMissingParameter(componentId, name);
105
        }
106
        return nullptr;
Don Gagne's avatar
Don Gagne committed
107 108 109 110 111
    }
}

bool FactPanelController::parameterExists(int componentId, const QString& name)
{
112
    return _vehicle ? _vehicle->parameterManager()->parameterExists(componentId, name) : false;
Don Gagne's avatar
Don Gagne committed
113 114 115 116
}

void FactPanelController::_showInternalError(const QString& errorMsg)
{
117
    _notifyPanelErrorMsg(tr("Internal Error: %1").arg(errorMsg));
Don Gagne's avatar
Don Gagne committed
118
    qCWarning(FactPanelControllerLog) << "Internal Error" << errorMsg;
119
    qgcApp()->showMessage(errorMsg);
dogmaphobic's avatar
dogmaphobic committed
120
}
DonLakeFlyer's avatar
DonLakeFlyer committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

void FactPanelController::getMissingParameters(QStringList rgNames)
{
    for (const QString& name: rgNames) {
        _missingParameterWaitList.append(name);
        _vehicle->parameterManager()->refreshParameter(MAV_COMP_ID_AUTOPILOT1, name);
    }

    _missingParametersTimer.start();
}

void FactPanelController::_checkForMissingParameters(void)
{
    QStringList waitList = _missingParameterWaitList;
    for (const QString& name: waitList) {
        if (_vehicle->parameterManager()->parameterExists(MAV_COMP_ID_AUTOPILOT1, name)) {
            _missingParameterWaitList.removeOne(name);
        }
    }

    if (_missingParameterWaitList.isEmpty()) {
        emit missingParametersAvailable();
    } else {
        _missingParametersTimer.start();
    }
}