FactPanelController.cc 4.6 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(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
    if (_vehicle) {
        _uas = _vehicle->uas();
        _autopilot = _vehicle->autopilotPlugin();
34 35
    } else {
        _vehicle = qgcApp()->toolbox()->multiVehicleManager()->offlineEditingVehicle();
36
    }
dogmaphobic's avatar
dogmaphobic committed
37

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

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

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

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

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

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

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

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

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

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

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

113 114 115 116 117 118
    return noMissingFacts;
}

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

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

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

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