FactPanelController.cc 4.75 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8
 *
 * 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
{
64 65 66 67
    if (componentId == FactSystem::defaultComponentId) {
        componentId = _vehicle->defaultComponentId();
    }

Don Gagne's avatar
Don Gagne committed
68
    qgcApp()->reportMissingParameter(componentId, name);
dogmaphobic's avatar
dogmaphobic committed
69

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

72 73
    qCWarning(FactPanelControllerLog) << "Missing parameter:" << missingParam;

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

Don Gagne's avatar
Don Gagne committed
85
bool FactPanelController::_allParametersExists(int componentId, QStringList names)
86 87
{
    bool noMissingFacts = true;
dogmaphobic's avatar
dogmaphobic committed
88

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

96 97 98
    return noMissingFacts;
}

Don Gagne's avatar
Don Gagne committed
99

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

bool FactPanelController::parameterExists(int componentId, const QString& name)
{
116
    return _vehicle ? _vehicle->parameterManager()->parameterExists(componentId, name) : false;
Don Gagne's avatar
Don Gagne committed
117 118 119 120
}

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

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();
    }
}