Fact.cc 3.87 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
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 30
/*=====================================================================
 
 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 "Fact.h"

#include <QtQml>

Don Gagne's avatar
Don Gagne committed
31 32 33 34 35 36 37 38 39 40
Fact::Fact(void) :
    _componentId(-1),
    _value(0),
    _type(FactMetaData::valueTypeInt32),
    _metaData(NULL)
{    
    FactMetaData* metaData = new FactMetaData(_type, this);
    setMetaData(metaData);
}

41
Fact::Fact(int componentId, QString name, FactMetaData::ValueType_t type, QObject* parent) :
Don Gagne's avatar
Don Gagne committed
42 43
    QObject(parent),
    _name(name),
44
    _componentId(componentId),
Don Gagne's avatar
Don Gagne committed
45
    _value(0),
46
    _type(type),
Don Gagne's avatar
Don Gagne committed
47
    _metaData(NULL)
Don Gagne's avatar
Don Gagne committed
48
{
Don Gagne's avatar
Don Gagne committed
49

Don Gagne's avatar
Don Gagne committed
50 51
}

52
void Fact::setValue(const QVariant& value)
Don Gagne's avatar
Don Gagne committed
53
{
54 55
    QVariant newValue;
    
56 57 58 59
    switch (type()) {
        case FactMetaData::valueTypeInt8:
        case FactMetaData::valueTypeInt16:
        case FactMetaData::valueTypeInt32:
60
            newValue = QVariant(value.toInt());
61 62 63 64 65
            break;

        case FactMetaData::valueTypeUint8:
        case FactMetaData::valueTypeUint16:
        case FactMetaData::valueTypeUint32:
66
            newValue = QVariant(value.toUInt());
67 68 69
            break;

        case FactMetaData::valueTypeFloat:
70
            newValue = QVariant(value.toFloat());
71 72 73
            break;

        case FactMetaData::valueTypeDouble:
74
            newValue = QVariant(value.toDouble());
75 76
            break;
    }
77 78 79 80 81 82
    
    if (newValue != _value) {
        _value.setValue(newValue);
        emit valueChanged(_value);
        emit _containerValueChanged(_value);
    }
Don Gagne's avatar
Don Gagne committed
83 84
}

85
void Fact::_containerSetValue(const QVariant& value)
Don Gagne's avatar
Don Gagne committed
86 87
{
    _value = value;
88
    emit valueChanged(_value);
Don Gagne's avatar
Don Gagne committed
89
}
90 91 92 93 94 95

QString Fact::name(void) const
{
    return _name;
}

96 97 98 99 100
int Fact::componentId(void) const
{
    return _componentId;
}

101 102 103 104 105 106 107 108 109 110 111 112
QVariant Fact::value(void) const
{
    return _value;
}

QString Fact::valueString(void) const
{
    return _value.toString();
}

QVariant Fact::defaultValue(void)
{
113
    Q_ASSERT(_metaData);
114 115 116 117
    if (!_metaData->defaultValueAvailable()) {
        qDebug() << "Access to unavailable default value";
    }
    return _metaData->defaultValue();
118 119 120 121
}

FactMetaData::ValueType_t Fact::type(void)
{
122
    return _type;
123 124 125 126
}

QString Fact::shortDescription(void)
{
127 128
    Q_ASSERT(_metaData);
    return _metaData->shortDescription();
129 130 131 132
}

QString Fact::longDescription(void)
{
133 134
    Q_ASSERT(_metaData);
    return _metaData->longDescription();
135 136 137 138
}

QString Fact::units(void)
{
139 140
    Q_ASSERT(_metaData);
    return _metaData->units();
141 142 143 144
}

QVariant Fact::min(void)
{
145
    Q_ASSERT(_metaData);
146
    return _metaData->min();
147 148 149 150
}

QVariant Fact::max(void)
{
151
    Q_ASSERT(_metaData);
152
    return _metaData->max();
153 154
}

155 156
QString Fact::group(void)
{
157 158
    Q_ASSERT(_metaData);
    return _metaData->group();
159 160
}

161 162 163 164
void Fact::setMetaData(FactMetaData* metaData)
{
    _metaData = metaData;
}
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

bool Fact::valueEqualsDefault(void)
{
    Q_ASSERT(_metaData);
    if (_metaData->defaultValueAvailable()) {
        return _metaData->defaultValue() == value();
    } else {
        return false;
    }
}

bool Fact::defaultValueAvailable(void)
{
    Q_ASSERT(_metaData);
    return _metaData->defaultValueAvailable();
}