FactPanelController.cc 3.69 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
    }
Don Gagne's avatar
Don Gagne committed
32 33 34 35
}

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

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

Don Gagne's avatar
Don Gagne committed
58
void FactPanelController::_reportMissingParameter(int componentId, const QString& name)
59
{
Don Gagne's avatar
Don Gagne committed
60
    qgcApp()->reportMissingParameter(componentId, name);
dogmaphobic's avatar
dogmaphobic committed
61

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

64 65
    qCWarning(FactPanelControllerLog) << "Missing parameter:" << missingParam;

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

Don Gagne's avatar
Don Gagne committed
77
bool FactPanelController::_allParametersExists(int componentId, QStringList names)
78 79
{
    bool noMissingFacts = true;
dogmaphobic's avatar
dogmaphobic committed
80

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

88 89 90
    return noMissingFacts;
}

Don Gagne's avatar
Don Gagne committed
91

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

bool FactPanelController::parameterExists(int componentId, const QString& name)
{
108
    return _vehicle ? _vehicle->parameterManager()->parameterExists(componentId, name) : false;
Don Gagne's avatar
Don Gagne committed
109 110 111 112
}

void FactPanelController::_showInternalError(const QString& errorMsg)
{
113
    _notifyPanelErrorMsg(tr("Internal Error: %1").arg(errorMsg));
Don Gagne's avatar
Don Gagne committed
114
    qCWarning(FactPanelControllerLog) << "Internal Error" << errorMsg;
115
    qgcApp()->showMessage(errorMsg);
dogmaphobic's avatar
dogmaphobic committed
116
}