FactPanelController.cc 4.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 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.
 
 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.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

#include "FactPanelController.h"
25
#include "MultiVehicleManager.h"
26
#include "QGCMessageBox.h"
27
#include "UAS.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
FactPanelController::FactPanelController(void) :
    _factPanel(NULL)
{
39
    _vehicle = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle();
40
    Q_ASSERT(_vehicle);
Don Gagne's avatar
Don Gagne committed
41
    
42
    _uas = _vehicle->uas();
Don Gagne's avatar
Don Gagne committed
43
    Q_ASSERT(_uas);
44
    
45
    _autopilot = _vehicle->autopilotPlugin();
46
    Q_ASSERT(_autopilot);
47 48
    Q_ASSERT(_autopilot->parametersReady());
    Q_ASSERT(!_autopilot->missingParameters());
49 50 51 52 53 54 55 56 57 58 59 60
    
    // 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
61
    // Once we finally have the _factPanel member set, send any
62 63 64
    // missing fact notices that were waiting to go out
    
    _factPanel = panel;
Don Gagne's avatar
Don Gagne committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    foreach (QString missingParam, _delayedMissingParams) {
        _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));
80 81 82
    }
}

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

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

Don Gagne's avatar
Don Gagne committed
95
void FactPanelController::_reportMissingParameter(int componentId, const QString& name)
96
{
Don Gagne's avatar
Don Gagne committed
97 98 99
    qgcApp()->reportMissingParameter(componentId, name);
    
    QString missingParam = QString("%1:%2").arg(componentId).arg(name);
100
    
Don Gagne's avatar
Don Gagne committed
101
    // If missing parameters a reported from the constructor of a derived class we
102 103 104 105
    // 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
106
        _notifyPanelMissingParameter(missingParam);
107
    } else {
Don Gagne's avatar
Don Gagne committed
108
        _delayedMissingParams += missingParam;
109 110 111
    }
}

Don Gagne's avatar
Don Gagne committed
112
bool FactPanelController::_allParametersExists(int componentId, QStringList names)
113 114 115
{
    bool noMissingFacts = true;
    
Don Gagne's avatar
Don Gagne committed
116 117 118
    foreach (QString name, names) {
        if (!_autopilot->parameterExists(componentId, name)) {
            _reportMissingParameter(componentId, name);
119 120 121 122 123 124 125 126 127 128
            noMissingFacts = false;
        }
    }
    
    return noMissingFacts;
}

void FactPanelController::_checkForMissingFactPanel(void)
{
    if (!_factPanel) {
Don Gagne's avatar
Don Gagne committed
129
        _showInternalError("Incorrect FactPanel Qml implementation. FactPanelController used without passing in factPanel.");
130
    }
Don Gagne's avatar
Don Gagne committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}

Fact* FactPanelController::getParameterFact(int componentId, const QString& name)
{
    if (_autopilot->parameterExists(componentId, name)) {
        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)
{
    return _autopilot->parameterExists(componentId, name);
}

void FactPanelController::_showInternalError(const QString& errorMsg)
{
    _notifyPanelErrorMsg(QString("Internal Error: %1").arg(errorMsg));
    qCWarning(FactPanelControllerLog) << "Internal Error" << errorMsg;
    QGCMessageBox::critical("Internal Error", errorMsg);
155
}