Fact.cc 7.02 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
Fact::Fact(QObject* parent)
    : QObject(parent)
    , _componentId(-1)
    , _value(0)
    , _type(FactMetaData::valueTypeInt32)
    , _metaData(NULL)
Don Gagne's avatar
Don Gagne committed
37 38 39 40 41
{    
    FactMetaData* metaData = new FactMetaData(_type, this);
    setMetaData(metaData);
}

Don Gagne's avatar
Don Gagne committed
42 43 44 45 46 47 48
Fact::Fact(int componentId, QString name, FactMetaData::ValueType_t type, QObject* parent)
    : QObject(parent)
    , _name(name)
    , _componentId(componentId)
    , _value(0)
    , _type(type)
    , _metaData(NULL)
Don Gagne's avatar
Don Gagne committed
49
{
Don Gagne's avatar
Don Gagne committed
50 51 52
    FactMetaData* metaData = new FactMetaData(_type, this);
    setMetaData(metaData);
}
Don Gagne's avatar
Don Gagne committed
53

Don Gagne's avatar
Don Gagne committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
Fact::Fact(const Fact& other, QObject* parent)
    : QObject(parent)
{
    *this = other;
}

const Fact& Fact::operator=(const Fact& other)
{
    _name           = other._name;
    _componentId    = other._componentId;
    _value          = other._value;
    _type           = other._type;
    
    if (_metaData && other._metaData) {
        *_metaData = *other._metaData;
    } else {
        _metaData = NULL;
    }
    
    return *this;
Don Gagne's avatar
Don Gagne committed
74 75
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
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";
    }
}

92
void Fact::setValue(const QVariant& value)
Don Gagne's avatar
Don Gagne committed
93
{
Don Gagne's avatar
Don Gagne committed
94 95 96 97 98 99 100 101 102 103 104 105 106
    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";
107
    }
Don Gagne's avatar
Don Gagne committed
108 109
}

110
void Fact::_containerSetValue(const QVariant& value)
Don Gagne's avatar
Don Gagne committed
111 112
{
    _value = value;
113
    emit valueChanged(_value);
114
    emit vehicleUpdated(_value);
Don Gagne's avatar
Don Gagne committed
115
}
116 117 118 119 120 121

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

122 123 124 125 126
int Fact::componentId(void) const
{
    return _componentId;
}

127 128 129 130 131 132 133
QVariant Fact::value(void) const
{
    return _value;
}

QString Fact::valueString(void) const
{
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    QString valueString;

    switch (type()) {
        case FactMetaData::valueTypeFloat:
            qDebug() << name() << value() << decimalPlaces();
            valueString = QString("%1").arg(value().toFloat(), 0, 'g', decimalPlaces());
            break;
        case FactMetaData::valueTypeDouble:
            valueString = QString("%1").arg(value().toDouble(), 0, 'g', decimalPlaces());
            break;
        default:
            valueString = value().toString();
            break;
    }

    return valueString;
150 151
}

152
QVariant Fact::defaultValue(void) const
153
{
Don Gagne's avatar
Don Gagne committed
154 155 156 157 158 159 160 161
    if (_metaData) {
        if (!_metaData->defaultValueAvailable()) {
            qDebug() << "Access to unavailable default value";
        }
        return _metaData->defaultValue();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
162
    }
163 164
}

165
FactMetaData::ValueType_t Fact::type(void) const
166
{
167
    return _type;
168 169
}

170
QString Fact::shortDescription(void) const
171
{
Don Gagne's avatar
Don Gagne committed
172 173 174 175 176 177
    if (_metaData) {
        return _metaData->shortDescription();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
178 179
}

180
QString Fact::longDescription(void) const
181
{
Don Gagne's avatar
Don Gagne committed
182 183 184 185 186 187
    if (_metaData) {
        return _metaData->longDescription();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
188 189
}

190
QString Fact::units(void) const
191
{
Don Gagne's avatar
Don Gagne committed
192 193 194 195 196 197
    if (_metaData) {
        return _metaData->units();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
198 199
}

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

210
QVariant Fact::max(void) const
211
{
Don Gagne's avatar
Don Gagne committed
212 213 214 215 216 217 218 219
    if (_metaData) {
        return _metaData->max();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
}

220
bool Fact::minIsDefaultForType(void) const
Don Gagne's avatar
Don Gagne committed
221 222 223 224 225 226 227 228 229
{
    if (_metaData) {
        return _metaData->minIsDefaultForType();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
}

230
bool Fact::maxIsDefaultForType(void) const
Don Gagne's avatar
Don Gagne committed
231 232 233 234 235 236 237
{
    if (_metaData) {
        return _metaData->maxIsDefaultForType();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
238 239
}

240 241 242 243 244 245 246 247 248 249
int Fact::decimalPlaces(void) const
{
    if (_metaData) {
        return _metaData->decimalPlaces();
    } else {
        qWarning() << "Meta data pointer missing";
        return FactMetaData::defaultDecimalPlaces;
    }
}

250
QString Fact::group(void) const
251
{
Don Gagne's avatar
Don Gagne committed
252 253 254 255 256 257
    if (_metaData) {
        return _metaData->group();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
258 259
}

260 261 262 263
void Fact::setMetaData(FactMetaData* metaData)
{
    _metaData = metaData;
}
264

265
bool Fact::valueEqualsDefault(void) const
266
{
Don Gagne's avatar
Don Gagne committed
267 268 269 270 271 272
    if (_metaData) {
        if (_metaData->defaultValueAvailable()) {
            return _metaData->defaultValue() == value();
        } else {
            return false;
        }
273
    } else {
Don Gagne's avatar
Don Gagne committed
274
        qWarning() << "Meta data pointer missing";
275 276 277 278
        return false;
    }
}

279
bool Fact::defaultValueAvailable(void) const
280
{
Don Gagne's avatar
Don Gagne committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    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");
    }
}