Fact.cc 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Don Gagne's avatar
Don Gagne committed
10 11 12 13 14

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

#include "Fact.h"
Don Gagne's avatar
Don Gagne committed
15
#include "QGCMAVLink.h"
Don Gagne's avatar
Don Gagne committed
16 17 18

#include <QtQml>

Don Gagne's avatar
Don Gagne committed
19 20 21
Fact::Fact(QObject* parent)
    : QObject(parent)
    , _componentId(-1)
22
    , _rawValue(0)
Don Gagne's avatar
Don Gagne committed
23 24
    , _type(FactMetaData::valueTypeInt32)
    , _metaData(NULL)
Don Gagne's avatar
Don Gagne committed
25 26
    , _sendValueChangedSignals(true)
    , _deferredValueChangeSignal(false)
Don Gagne's avatar
Don Gagne committed
27 28 29 30 31
{    
    FactMetaData* metaData = new FactMetaData(_type, this);
    setMetaData(metaData);
}

Don Gagne's avatar
Don Gagne committed
32 33 34 35
Fact::Fact(int componentId, QString name, FactMetaData::ValueType_t type, QObject* parent)
    : QObject(parent)
    , _name(name)
    , _componentId(componentId)
36
    , _rawValue(0)
Don Gagne's avatar
Don Gagne committed
37 38
    , _type(type)
    , _metaData(NULL)
Don Gagne's avatar
Don Gagne committed
39 40
    , _sendValueChangedSignals(true)
    , _deferredValueChangeSignal(false)
Don Gagne's avatar
Don Gagne committed
41
{
Don Gagne's avatar
Don Gagne committed
42 43 44
    FactMetaData* metaData = new FactMetaData(_type, this);
    setMetaData(metaData);
}
Don Gagne's avatar
Don Gagne committed
45

Don Gagne's avatar
Don Gagne committed
46 47 48 49 50 51 52 53
Fact::Fact(const Fact& other, QObject* parent)
    : QObject(parent)
{
    *this = other;
}

const Fact& Fact::operator=(const Fact& other)
{
Don Gagne's avatar
Don Gagne committed
54 55 56 57 58 59 60
    _name                       = other._name;
    _componentId                = other._componentId;
    _rawValue                   = other._rawValue;
    _type                       = other._type;
    _sendValueChangedSignals    = other._sendValueChangedSignals;
    _deferredValueChangeSignal  = other._deferredValueChangeSignal;

Don Gagne's avatar
Don Gagne committed
61 62 63 64 65 66 67
    if (_metaData && other._metaData) {
        *_metaData = *other._metaData;
    } else {
        _metaData = NULL;
    }
    
    return *this;
Don Gagne's avatar
Don Gagne committed
68 69
}

Don Gagne's avatar
Don Gagne committed
70
void Fact::forceSetRawValue(const QVariant& value)
71 72 73 74 75
{
    if (_metaData) {
        QVariant    typedValue;
        QString     errorString;
        
76
        if (_metaData->convertAndValidateRaw(value, true /* convertOnly */, typedValue, errorString)) {
Don Gagne's avatar
Don Gagne committed
77
            _rawValue.setValue(typedValue);
Don Gagne's avatar
Don Gagne committed
78
            _sendValueChangedSignal(cookedValue());
Don Gagne's avatar
Don Gagne committed
79
            emit _containerRawValueChanged(rawValue());
80
            emit rawValueChanged(_rawValue);
81 82 83 84 85 86
        }
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

87
void Fact::setRawValue(const QVariant& value)
Don Gagne's avatar
Don Gagne committed
88
{
Don Gagne's avatar
Don Gagne committed
89 90 91 92
    if (_metaData) {
        QVariant    typedValue;
        QString     errorString;
        
93
        if (_metaData->convertAndValidateRaw(value, true /* convertOnly */, typedValue, errorString)) {
94 95
            if (typedValue != _rawValue) {
                _rawValue.setValue(typedValue);
Don Gagne's avatar
Don Gagne committed
96
                _sendValueChangedSignal(cookedValue());
Don Gagne's avatar
Don Gagne committed
97
                emit _containerRawValueChanged(rawValue());
98
                emit rawValueChanged(_rawValue);
Don Gagne's avatar
Don Gagne committed
99 100 101 102
            }
        }
    } else {
        qWarning() << "Meta data pointer missing";
103
    }
Don Gagne's avatar
Don Gagne committed
104 105
}

Don Gagne's avatar
Don Gagne committed
106
void Fact::setCookedValue(const QVariant& value)
107 108 109 110 111 112 113 114 115 116 117 118 119
{
    if (_metaData) {
        setRawValue(_metaData->cookedTranslator()(value));
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

void Fact::setEnumStringValue(const QString& value)
{
    if (_metaData) {
        int index = _metaData->enumStrings().indexOf(value);
        if (index != -1) {
Don Gagne's avatar
Don Gagne committed
120
            setCookedValue(_metaData->enumValues()[index]);
121 122 123 124 125 126 127 128 129
        }
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

void Fact::setEnumIndex(int index)
{
    if (_metaData) {
Don Gagne's avatar
Don Gagne committed
130
        setCookedValue(_metaData->enumValues()[index]);
131 132 133 134 135
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

Don Gagne's avatar
Don Gagne committed
136
void Fact::_containerSetRawValue(const QVariant& value)
Don Gagne's avatar
Don Gagne committed
137
{
138
    _rawValue = value;
Don Gagne's avatar
Don Gagne committed
139
    _sendValueChangedSignal(cookedValue());
140
    emit vehicleUpdated(_rawValue);
141
    emit rawValueChanged(_rawValue);
Don Gagne's avatar
Don Gagne committed
142
}
143 144 145 146 147 148

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

149 150 151 152 153
int Fact::componentId(void) const
{
    return _componentId;
}

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

164
QString Fact::enumStringValue(void)
165 166 167
{
    if (_metaData) {
        int enumIndex = this->enumIndex();
Don Gagne's avatar
Don Gagne committed
168
        if (enumIndex >= 0 && enumIndex < _metaData->enumStrings().count()) {
169 170 171 172 173 174 175 176 177
            return _metaData->enumStrings()[enumIndex];
        }
    } else {
        qWarning() << "Meta data pointer missing";
    }

    return QString();
}

178
int Fact::enumIndex(void)
179 180 181
{
    if (_metaData) {
        int index = 0;
182

183
        foreach (QVariant enumValue, _metaData->enumValues()) {
Don Gagne's avatar
Don Gagne committed
184
            if (enumValue == rawValue()) {
185 186 187 188
                return index;
            }
            index ++;
        }
189 190 191 192 193 194

        // Current value is not in list, add it manually
        _metaData->addEnumInfo(QString("Unknown: %1").arg(rawValue().toString()), rawValue());
        emit enumStringsChanged();
        emit enumValuesChanged();
        return index;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    } else {
        qWarning() << "Meta data pointer missing";
    }

    return -1;
}

QStringList Fact::enumStrings(void) const
{
    if (_metaData) {
        return _metaData->enumStrings();
    } else {
        qWarning() << "Meta data pointer missing";
        return QStringList();
    }
}

QVariantList Fact::enumValues(void) const
{
    if (_metaData) {
        return _metaData->enumValues();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariantList();
    }
220 221
}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
QStringList Fact::bitmaskStrings(void) const
{
    if (_metaData) {
        return _metaData->bitmaskStrings();
    } else {
        qWarning() << "Meta data pointer missing";
        return QStringList();
    }
}

QVariantList Fact::bitmaskValues(void) const
{
    if (_metaData) {
        return _metaData->bitmaskValues();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariantList();
    }
}

242
QString Fact::_variantToString(const QVariant& variant, int decimalPlaces) const
243
{
244 245 246
    QString valueString;

    switch (type()) {
Don Gagne's avatar
Don Gagne committed
247 248 249 250 251 252
    case FactMetaData::valueTypeFloat:
    {
        float fValue = variant.toFloat();
        if (qIsNaN(fValue)) {
            valueString = QStringLiteral("--.--");
        } else {
253
            valueString = QString("%1").arg(fValue, 0, 'f', decimalPlaces);
Don Gagne's avatar
Don Gagne committed
254 255 256 257 258 259 260 261 262
        }
    }
        break;
    case FactMetaData::valueTypeDouble:
    {
        double dValue = variant.toDouble();
        if (qIsNaN(dValue)) {
            valueString = QStringLiteral("--.--");
        } else {
263
            valueString = QString("%1").arg(dValue, 0, 'f', decimalPlaces);
Don Gagne's avatar
Don Gagne committed
264 265 266 267 268 269
        }
    }
        break;
    default:
        valueString = variant.toString();
        break;
270 271 272
    }

    return valueString;
273 274
}

275 276 277 278 279 280
QString Fact::rawValueStringFullPrecision(void) const
{
    return _variantToString(rawValue(), 18);
}


281 282
QString Fact::rawValueString(void) const
{
283
    return _variantToString(rawValue(), decimalPlaces());
284 285 286
}

QString Fact::cookedValueString(void) const
287
{
288
    return _variantToString(cookedValue(), decimalPlaces());
289 290
}

291 292 293 294 295 296 297 298 299 300 301 302 303 304
QVariant Fact::rawDefaultValue(void) const
{
    if (_metaData) {
        if (!_metaData->defaultValueAvailable()) {
            qDebug() << "Access to unavailable default value";
        }
        return _metaData->rawDefaultValue();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
}

QVariant Fact::cookedDefaultValue(void) const
305
{
Don Gagne's avatar
Don Gagne committed
306 307 308 309
    if (_metaData) {
        if (!_metaData->defaultValueAvailable()) {
            qDebug() << "Access to unavailable default value";
        }
310
        return _metaData->cookedDefaultValue();
Don Gagne's avatar
Don Gagne committed
311 312 313
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
314
    }
315 316
}

317
QString Fact::cookedDefaultValueString(void) const
318
{
319
    return _variantToString(cookedDefaultValue(), decimalPlaces());
320 321
}

322
FactMetaData::ValueType_t Fact::type(void) const
323
{
324
    return _type;
325 326
}

327
QString Fact::shortDescription(void) const
328
{
Don Gagne's avatar
Don Gagne committed
329 330 331 332 333 334
    if (_metaData) {
        return _metaData->shortDescription();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
335 336
}

337
QString Fact::longDescription(void) const
338
{
Don Gagne's avatar
Don Gagne committed
339 340 341 342 343 344
    if (_metaData) {
        return _metaData->longDescription();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
345 346
}

347 348 349 350 351 352 353 354 355 356 357
QString Fact::rawUnits(void) const
{
    if (_metaData) {
        return _metaData->rawUnits();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
}

QString Fact::cookedUnits(void) const
358
{
Don Gagne's avatar
Don Gagne committed
359
    if (_metaData) {
360
        return _metaData->cookedUnits();
Don Gagne's avatar
Don Gagne committed
361 362 363 364
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
365 366
}

367
QVariant Fact::rawMin(void) const
368
{
Don Gagne's avatar
Don Gagne committed
369
    if (_metaData) {
370
        return _metaData->rawMin();
Don Gagne's avatar
Don Gagne committed
371 372 373 374
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
375 376
}

377
QVariant Fact::cookedMin(void) const
378
{
379 380 381 382 383 384
    if (_metaData) {
        return _metaData->cookedMin();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
385 386
}

387 388
QString Fact::cookedMinString(void) const
{
389
    return _variantToString(cookedMin(), decimalPlaces());
390 391 392
}

QVariant Fact::rawMax(void) const
393
{
Don Gagne's avatar
Don Gagne committed
394
    if (_metaData) {
395
        return _metaData->rawMax();
Don Gagne's avatar
Don Gagne committed
396 397 398 399 400 401
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
}

402
QVariant Fact::cookedMax(void) const
403
{
404 405 406 407 408 409 410 411 412 413
    if (_metaData) {
        return _metaData->cookedMax();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
}

QString Fact::cookedMaxString(void) const
{
414
    return _variantToString(cookedMax(), decimalPlaces());
415 416
}

417
bool Fact::minIsDefaultForType(void) const
Don Gagne's avatar
Don Gagne committed
418 419 420 421 422 423 424 425 426
{
    if (_metaData) {
        return _metaData->minIsDefaultForType();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
}

427
bool Fact::maxIsDefaultForType(void) const
Don Gagne's avatar
Don Gagne committed
428 429 430 431 432 433 434
{
    if (_metaData) {
        return _metaData->maxIsDefaultForType();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
435 436
}

437 438 439 440 441 442 443 444 445 446
int Fact::decimalPlaces(void) const
{
    if (_metaData) {
        return _metaData->decimalPlaces();
    } else {
        qWarning() << "Meta data pointer missing";
        return FactMetaData::defaultDecimalPlaces;
    }
}

447
QString Fact::group(void) const
448
{
Don Gagne's avatar
Don Gagne committed
449 450 451 452 453 454
    if (_metaData) {
        return _metaData->group();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
455 456
}

457 458 459
void Fact::setMetaData(FactMetaData* metaData)
{
    _metaData = metaData;
Don Gagne's avatar
Don Gagne committed
460
    emit valueChanged(cookedValue());
461
}
462

463
bool Fact::valueEqualsDefault(void) const
464
{
Don Gagne's avatar
Don Gagne committed
465 466
    if (_metaData) {
        if (_metaData->defaultValueAvailable()) {
467
            return _metaData->rawDefaultValue() == rawValue();
Don Gagne's avatar
Don Gagne committed
468 469 470
        } else {
            return false;
        }
471
    } else {
Don Gagne's avatar
Don Gagne committed
472
        qWarning() << "Meta data pointer missing";
473 474 475 476
        return false;
    }
}

477
bool Fact::defaultValueAvailable(void) const
478
{
Don Gagne's avatar
Don Gagne committed
479 480 481 482 483 484 485 486
    if (_metaData) {
        return _metaData->defaultValueAvailable();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
}

487
QString Fact::validate(const QString& cookedValue, bool convertOnly)
Don Gagne's avatar
Don Gagne committed
488 489 490 491 492
{
    if (_metaData) {
        QVariant    typedValue;
        QString     errorString;
        
493
        _metaData->convertAndValidateCooked(cookedValue, convertOnly, typedValue, errorString);
Don Gagne's avatar
Don Gagne committed
494 495 496 497 498 499 500
        
        return errorString;
    } else {
        qWarning() << "Meta data pointer missing";
        return QString("Internal error: Meta data pointer missing");
    }
}
501 502 503 504 505 506 507 508 509 510

bool Fact::rebootRequired(void) const
{
    if (_metaData) {
        return _metaData->rebootRequired();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
}
Don Gagne's avatar
Don Gagne committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536

void Fact::setSendValueChangedSignals (bool sendValueChangedSignals)
{
    if (sendValueChangedSignals != _sendValueChangedSignals) {
        _sendValueChangedSignals = sendValueChangedSignals;
        emit sendValueChangedSignalsChanged(_sendValueChangedSignals);
    }
}

void Fact::_sendValueChangedSignal(QVariant value)
{
    if (_sendValueChangedSignals) {
        emit valueChanged(value);
        _deferredValueChangeSignal = false;
    } else {
        _deferredValueChangeSignal = true;
    }
}

void Fact::sendDeferredValueChangedSignal(void)
{
    if (_deferredValueChangeSignal) {
        _deferredValueChangeSignal = false;
        emit valueChanged(cookedValue());
    }
}
537 538 539 540 541 542 543 544 545 546 547 548 549 550

QString Fact::enumOrValueString(void)
{
    if (_metaData) {
        if (_metaData->enumStrings().count()) {
            return enumStringValue();
        } else {
            return cookedValueString();
        }
    } else {
        qWarning() << "Meta data pointer missing";
    }
    return QString();
}
Don Gagne's avatar
Don Gagne committed
551 552 553 554 555 556 557 558 559 560

double Fact::increment(void) const
{
    if (_metaData) {
        return _metaData->increment();
    } else {
        qWarning() << "Meta data pointer missing";
    }
    return std::numeric_limits<double>::quiet_NaN();
}