FactBinder.cc 2.96 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 25 26 27 28 29
/*=====================================================================
 
 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/>.
 
 ======================================================================*/

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

#include "FactBinder.h"
#include "UASManager.h"
#include "AutoPilotPluginManager.h"
30
#include <QDebug>
31 32 33

FactBinder::FactBinder(void) :
    _autopilotPlugin(NULL),
34 35
    _fact(NULL),
    _componentId(FactSystem::defaultComponentId)
36 37 38 39 40 41
{
    UASInterface* uas = UASManager::instance()->getActiveUAS();
    Q_ASSERT(uas);
    
    _autopilotPlugin = AutoPilotPluginManager::instance()->getInstanceForAutoPilotPlugin(uas);
    Q_ASSERT(_autopilotPlugin);
42
    Q_ASSERT(_autopilotPlugin->pluginReady());
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
}

QString FactBinder::name(void) const
{
    if (_fact) {
        return _fact->name();
    } else {
        return QString();
    }
}

void FactBinder::setName(const QString& name)
{
    if (_fact) {
        disconnect(_fact, &Fact::valueChanged, this, &FactBinder::valueChanged);
        _fact = NULL;
    }
    
    if (!name.isEmpty()) {
62 63
        if (_autopilotPlugin->factExists(FactSystem::ParameterProvider, _componentId, name)) {
            _fact = _autopilotPlugin->getFact(FactSystem::ParameterProvider, _componentId, name);
64
            connect(_fact, &Fact::valueChanged, this, &FactBinder::valueChanged);
65 66 67

            emit valueChanged();
            emit nameChanged();
68
        } else {
Don Gagne's avatar
Don Gagne committed
69 70
            qWarning() << "FAILED BINDING PARAM" << name << ": PARAM DOES NOT EXIST ON SYSTEM!";
            Q_ASSERT(false);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        }
    }
}

QVariant FactBinder::value(void) const
{
    if (_fact) {
        return _fact->value();
    } else {
        return QVariant(0);
    }
}

void FactBinder::setValue(const QVariant& value)
{
    if (_fact) {
        _fact->setValue(value);
    } else {
Don Gagne's avatar
Don Gagne committed
89 90
        qWarning() << "FAILED SETTING PARAM VALUE" << _fact->name() << ": PARAM DOES NOT EXIST ON SYSTEM!";
        Q_ASSERT(false);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    }
}

QString FactBinder::valueString(void) const
{
    if (_fact) {
        return _fact->valueString();
    } else {
        return QString();
    }
}

QString FactBinder::units(void) const
{
    if (_fact) {
        return _fact->units();
    } else {
        return QString();
    }
}