FactPanelController.cc 4.86 KB
Newer Older
1
/*=====================================================================
dogmaphobic's avatar
dogmaphobic committed
2

3
 QGroundControl Open Source Ground Control Station
dogmaphobic's avatar
dogmaphobic committed
4

5
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
dogmaphobic's avatar
dogmaphobic committed
6

7
 This file is part of the QGROUNDCONTROL project
dogmaphobic's avatar
dogmaphobic committed
8

9 10 11 12
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
dogmaphobic's avatar
dogmaphobic committed
13

14 15 16 17
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
dogmaphobic's avatar
dogmaphobic committed
18

19 20
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
dogmaphobic's avatar
dogmaphobic committed
21

22 23 24
 ======================================================================*/

#include "FactPanelController.h"
25
#include "MultiVehicleManager.h"
26
#include "UAS.h"
27
#include "QGCApplication.h"
28

Don Gagne's avatar
Don Gagne committed
29 30
#include <QQmlEngine>

31 32 33
/// @file
///     @author Don Gagne <don@thegagnes.com>

Don Gagne's avatar
Don Gagne committed
34 35
QGC_LOGGING_CATEGORY(FactPanelControllerLog, "FactPanelControllerLog")

36 37 38 39 40
FactPanelController::FactPanelController(void)
    : _vehicle(NULL)
    , _uas(NULL)
    , _autopilot(NULL)
    , _factPanel(NULL)
41
{
42
    _vehicle = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle();
dogmaphobic's avatar
dogmaphobic committed
43

44 45 46 47
    if (_vehicle) {
        _uas = _vehicle->uas();
        _autopilot = _vehicle->autopilotPlugin();
    }
dogmaphobic's avatar
dogmaphobic committed
48

49 50 51 52 53 54 55 56 57 58 59
    // 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
60
    // Once we finally have the _factPanel member set, send any
61
    // missing fact notices that were waiting to go out
dogmaphobic's avatar
dogmaphobic committed
62

63
    _factPanel = panel;
64
    foreach (const QString &missingParam, _delayedMissingParams) {
Don Gagne's avatar
Don Gagne committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78
        _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));
79 80 81
    }
}

Don Gagne's avatar
Don Gagne committed
82
void FactPanelController::_notifyPanelErrorMsg(const QString& errorMsg)
83
{
Don Gagne's avatar
Don Gagne committed
84 85
    if (_factPanel) {
        QVariant returnedValue;
86

Don Gagne's avatar
Don Gagne committed
87 88 89 90 91
        QMetaObject::invokeMethod(_factPanel,
                                  "showError",
                                  Q_RETURN_ARG(QVariant, returnedValue),
                                  Q_ARG(QVariant, errorMsg));
    }
92 93
}

Don Gagne's avatar
Don Gagne committed
94
void FactPanelController::_reportMissingParameter(int componentId, const QString& name)
95
{
Don Gagne's avatar
Don Gagne committed
96
    qgcApp()->reportMissingParameter(componentId, name);
dogmaphobic's avatar
dogmaphobic committed
97

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

Don Gagne's avatar
Don Gagne committed
100
    // If missing parameters a reported from the constructor of a derived class we
101 102 103 104
    // 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
105
        _notifyPanelMissingParameter(missingParam);
106
    } else {
Don Gagne's avatar
Don Gagne committed
107
        _delayedMissingParams += missingParam;
108 109 110
    }
}

Don Gagne's avatar
Don Gagne committed
111
bool FactPanelController::_allParametersExists(int componentId, QStringList names)
112 113
{
    bool noMissingFacts = true;
dogmaphobic's avatar
dogmaphobic committed
114

115
    foreach (const QString &name, names) {
116
        if (_autopilot && !_autopilot->parameterExists(componentId, name)) {
Don Gagne's avatar
Don Gagne committed
117
            _reportMissingParameter(componentId, name);
118 119 120
            noMissingFacts = false;
        }
    }
dogmaphobic's avatar
dogmaphobic committed
121

122 123 124 125 126 127
    return noMissingFacts;
}

void FactPanelController::_checkForMissingFactPanel(void)
{
    if (!_factPanel) {
Don Gagne's avatar
Don Gagne committed
128
        _showInternalError("Incorrect FactPanel Qml implementation. FactPanelController used without passing in factPanel.");
129
    }
Don Gagne's avatar
Don Gagne committed
130 131 132 133
}

Fact* FactPanelController::getParameterFact(int componentId, const QString& name)
{
134
    if (_autopilot && _autopilot->parameterExists(componentId, name)) {
Don Gagne's avatar
Don Gagne committed
135 136 137 138 139 140 141 142 143 144 145
        Fact* fact = _autopilot->getParameterFact(componentId, name);
        QQmlEngine::setObjectOwnership(fact, QQmlEngine::CppOwnership);
        return fact;
    } else {
        _reportMissingParameter(componentId, name);
        return NULL;
    }
}

bool FactPanelController::parameterExists(int componentId, const QString& name)
{
146
    return _autopilot ? _autopilot->parameterExists(componentId, name) : false;
Don Gagne's avatar
Don Gagne committed
147 148 149 150 151 152
}

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