Fact.cc 5.68 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
void Fact::forceSetValue(const QVariant& value)
{
    if (_metaData) {
        QVariant    typedValue;
        QString     errorString;
        
        if (_metaData->convertAndValidate(value, true /* convertOnly */, typedValue, errorString)) {
            _value.setValue(typedValue);
            emit valueChanged(_value);
            emit _containerValueChanged(_value);
        }
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

68
void Fact::setValue(const QVariant& value)
Don Gagne's avatar
Don Gagne committed
69
{
Don Gagne's avatar
Don Gagne committed
70 71 72 73 74 75 76 77 78 79 80 81 82
    if (_metaData) {
        QVariant    typedValue;
        QString     errorString;
        
        if (_metaData->convertAndValidate(value, true /* convertOnly */, typedValue, errorString)) {
            if (typedValue != _value) {
                _value.setValue(typedValue);
                emit valueChanged(_value);
                emit _containerValueChanged(_value);
            }
        }
    } else {
        qWarning() << "Meta data pointer missing";
83
    }
Don Gagne's avatar
Don Gagne committed
84 85
}

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

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

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

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

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

QVariant Fact::defaultValue(void)
{
Don Gagne's avatar
Don Gagne committed
115 116 117 118 119 120 121 122
    if (_metaData) {
        if (!_metaData->defaultValueAvailable()) {
            qDebug() << "Access to unavailable default value";
        }
        return _metaData->defaultValue();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
123
    }
124 125 126 127
}

FactMetaData::ValueType_t Fact::type(void)
{
128
    return _type;
129 130 131 132
}

QString Fact::shortDescription(void)
{
Don Gagne's avatar
Don Gagne committed
133 134 135 136 137 138
    if (_metaData) {
        return _metaData->shortDescription();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
139 140 141 142
}

QString Fact::longDescription(void)
{
Don Gagne's avatar
Don Gagne committed
143 144 145 146 147 148
    if (_metaData) {
        return _metaData->longDescription();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
149 150 151 152
}

QString Fact::units(void)
{
Don Gagne's avatar
Don Gagne committed
153 154 155 156 157 158
    if (_metaData) {
        return _metaData->units();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
159 160 161 162
}

QVariant Fact::min(void)
{
Don Gagne's avatar
Don Gagne committed
163 164 165 166 167 168
    if (_metaData) {
        return _metaData->min();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
169 170 171 172
}

QVariant Fact::max(void)
{
Don Gagne's avatar
Don Gagne committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    if (_metaData) {
        return _metaData->max();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
}

bool Fact::minIsDefaultForType(void)
{
    if (_metaData) {
        return _metaData->minIsDefaultForType();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
}

bool Fact::maxIsDefaultForType(void)
{
    if (_metaData) {
        return _metaData->maxIsDefaultForType();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
199 200
}

201 202
QString Fact::group(void)
{
Don Gagne's avatar
Don Gagne committed
203 204 205 206 207 208
    if (_metaData) {
        return _metaData->group();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
209 210
}

211 212 213 214
void Fact::setMetaData(FactMetaData* metaData)
{
    _metaData = metaData;
}
215 216 217

bool Fact::valueEqualsDefault(void)
{
Don Gagne's avatar
Don Gagne committed
218 219 220 221 222 223
    if (_metaData) {
        if (_metaData->defaultValueAvailable()) {
            return _metaData->defaultValue() == value();
        } else {
            return false;
        }
224
    } else {
Don Gagne's avatar
Don Gagne committed
225
        qWarning() << "Meta data pointer missing";
226 227 228 229 230 231
        return false;
    }
}

bool Fact::defaultValueAvailable(void)
{
Don Gagne's avatar
Don Gagne committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    if (_metaData) {
        return _metaData->defaultValueAvailable();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
}

QString Fact::validate(const QString& value, bool convertOnly)
{
    if (_metaData) {
        
        QVariant    typedValue;
        QString     errorString;
        
        _metaData->convertAndValidate(value, convertOnly, typedValue, errorString);
        
        return errorString;
    } else {
        qWarning() << "Meta data pointer missing";
        return QString("Internal error: Meta data pointer missing");
    }
}