Fact.cc 13.5 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

#include <QtQml>
18
#include <QQmlEngine>
Don Gagne's avatar
Don Gagne committed
19

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

    // Better sage than sorry on object ownership
    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
Don Gagne's avatar
Don Gagne committed
34 35
}

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

Don Gagne's avatar
Don Gagne committed
51 52 53 54
Fact::Fact(const Fact& other, QObject* parent)
    : QObject(parent)
{
    *this = other;
55
    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
Don Gagne's avatar
Don Gagne committed
56 57 58 59
}

const Fact& Fact::operator=(const Fact& other)
{
Don Gagne's avatar
Don Gagne committed
60 61 62 63 64 65 66
    _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
67 68 69 70 71 72 73
    if (_metaData && other._metaData) {
        *_metaData = *other._metaData;
    } else {
        _metaData = NULL;
    }
    
    return *this;
Don Gagne's avatar
Don Gagne committed
74 75
}

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

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

Don Gagne's avatar
Don Gagne committed
112
void Fact::setCookedValue(const QVariant& value)
113 114 115 116 117 118 119 120 121 122 123 124 125
{
    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
126
            setCookedValue(_metaData->enumValues()[index]);
127 128 129 130 131 132 133 134 135
        }
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

void Fact::setEnumIndex(int index)
{
    if (_metaData) {
Don Gagne's avatar
Don Gagne committed
136
        setCookedValue(_metaData->enumValues()[index]);
137 138 139 140 141
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

Don Gagne's avatar
Don Gagne committed
142
void Fact::_containerSetRawValue(const QVariant& value)
Don Gagne's avatar
Don Gagne committed
143
{
144
    _rawValue = value;
Don Gagne's avatar
Don Gagne committed
145
    _sendValueChangedSignal(cookedValue());
146
    emit vehicleUpdated(_rawValue);
147
    emit rawValueChanged(_rawValue);
Don Gagne's avatar
Don Gagne committed
148
}
149 150 151 152 153 154

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

155 156 157 158 159
int Fact::componentId(void) const
{
    return _componentId;
}

Don Gagne's avatar
Don Gagne committed
160
QVariant Fact::cookedValue(void) const
161
{
162 163 164 165 166 167 168 169
    if (_metaData) {
        return _metaData->rawTranslator()(_rawValue);
    } else {
        qWarning() << "Meta data pointer missing";
        return _rawValue;
    }
}

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

    return QString();
}

184
int Fact::enumIndex(void)
185 186 187
{
    if (_metaData) {
        int index = 0;
188

189
        foreach (QVariant enumValue, _metaData->enumValues()) {
Don Gagne's avatar
Don Gagne committed
190
            if (enumValue == rawValue()) {
191 192 193 194
                return index;
            }
            index ++;
        }
195 196 197 198 199 200

        // Current value is not in list, add it manually
        _metaData->addEnumInfo(QString("Unknown: %1").arg(rawValue().toString()), rawValue());
        emit enumStringsChanged();
        emit enumValuesChanged();
        return index;
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    } 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();
    }
226 227
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
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();
    }
}

248
QString Fact::_variantToString(const QVariant& variant, int decimalPlaces) const
249
{
250 251 252
    QString valueString;

    switch (type()) {
Don Gagne's avatar
Don Gagne committed
253 254 255 256 257 258
    case FactMetaData::valueTypeFloat:
    {
        float fValue = variant.toFloat();
        if (qIsNaN(fValue)) {
            valueString = QStringLiteral("--.--");
        } else {
259
            valueString = QString("%1").arg(fValue, 0, 'f', decimalPlaces);
Don Gagne's avatar
Don Gagne committed
260 261 262 263 264 265 266 267 268
        }
    }
        break;
    case FactMetaData::valueTypeDouble:
    {
        double dValue = variant.toDouble();
        if (qIsNaN(dValue)) {
            valueString = QStringLiteral("--.--");
        } else {
269
            valueString = QString("%1").arg(dValue, 0, 'f', decimalPlaces);
Don Gagne's avatar
Don Gagne committed
270 271 272
        }
    }
        break;
273 274 275 276 277 278 279 280 281 282 283 284
    case FactMetaData::valueTypeElapsedTimeInSeconds:
    {
        double dValue = variant.toDouble();
        if (qIsNaN(dValue)) {
            valueString = QStringLiteral("--:--:--");
        } else {
            QTime time(0, 0, 0, 0);
            time = time.addSecs(dValue);
            valueString = time.toString(QStringLiteral("hh:mm:ss"));
        }
    }
        break;
Don Gagne's avatar
Don Gagne committed
285 286 287
    default:
        valueString = variant.toString();
        break;
288 289 290
    }

    return valueString;
291 292
}

293 294 295 296 297 298
QString Fact::rawValueStringFullPrecision(void) const
{
    return _variantToString(rawValue(), 18);
}


299 300
QString Fact::rawValueString(void) const
{
301
    return _variantToString(rawValue(), decimalPlaces());
302 303 304
}

QString Fact::cookedValueString(void) const
305
{
306
    return _variantToString(cookedValue(), decimalPlaces());
307 308
}

309 310 311 312 313 314 315 316 317 318 319 320 321 322
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
323
{
Don Gagne's avatar
Don Gagne committed
324 325 326 327
    if (_metaData) {
        if (!_metaData->defaultValueAvailable()) {
            qDebug() << "Access to unavailable default value";
        }
328
        return _metaData->cookedDefaultValue();
Don Gagne's avatar
Don Gagne committed
329 330 331
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
332
    }
333 334
}

335
QString Fact::cookedDefaultValueString(void) const
336
{
337
    return _variantToString(cookedDefaultValue(), decimalPlaces());
338 339
}

340
FactMetaData::ValueType_t Fact::type(void) const
341
{
342
    return _type;
343 344
}

345
QString Fact::shortDescription(void) const
346
{
Don Gagne's avatar
Don Gagne committed
347 348 349 350 351 352
    if (_metaData) {
        return _metaData->shortDescription();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
353 354
}

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

365 366 367 368 369 370 371 372 373 374 375
QString Fact::rawUnits(void) const
{
    if (_metaData) {
        return _metaData->rawUnits();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
}

QString Fact::cookedUnits(void) const
376
{
Don Gagne's avatar
Don Gagne committed
377
    if (_metaData) {
378
        return _metaData->cookedUnits();
Don Gagne's avatar
Don Gagne committed
379 380 381 382
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
383 384
}

385
QVariant Fact::rawMin(void) const
386
{
Don Gagne's avatar
Don Gagne committed
387
    if (_metaData) {
388
        return _metaData->rawMin();
Don Gagne's avatar
Don Gagne committed
389 390 391 392
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
393 394
}

395
QVariant Fact::cookedMin(void) const
396
{
397 398 399 400 401 402
    if (_metaData) {
        return _metaData->cookedMin();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
403 404
}

405 406
QString Fact::cookedMinString(void) const
{
407
    return _variantToString(cookedMin(), decimalPlaces());
408 409 410
}

QVariant Fact::rawMax(void) const
411
{
Don Gagne's avatar
Don Gagne committed
412
    if (_metaData) {
413
        return _metaData->rawMax();
Don Gagne's avatar
Don Gagne committed
414 415 416 417 418 419
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
}

420
QVariant Fact::cookedMax(void) const
421
{
422 423 424 425 426 427 428 429 430 431
    if (_metaData) {
        return _metaData->cookedMax();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
}

QString Fact::cookedMaxString(void) const
{
432
    return _variantToString(cookedMax(), decimalPlaces());
433 434
}

435
bool Fact::minIsDefaultForType(void) const
Don Gagne's avatar
Don Gagne committed
436 437 438 439 440 441 442 443 444
{
    if (_metaData) {
        return _metaData->minIsDefaultForType();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
}

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

455 456 457 458 459 460 461 462 463 464
int Fact::decimalPlaces(void) const
{
    if (_metaData) {
        return _metaData->decimalPlaces();
    } else {
        qWarning() << "Meta data pointer missing";
        return FactMetaData::defaultDecimalPlaces;
    }
}

465
QString Fact::group(void) const
466
{
Don Gagne's avatar
Don Gagne committed
467 468 469 470 471 472
    if (_metaData) {
        return _metaData->group();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
473 474
}

475 476 477
void Fact::setMetaData(FactMetaData* metaData)
{
    _metaData = metaData;
Don Gagne's avatar
Don Gagne committed
478
    emit valueChanged(cookedValue());
479
}
480

481
bool Fact::valueEqualsDefault(void) const
482
{
Don Gagne's avatar
Don Gagne committed
483 484
    if (_metaData) {
        if (_metaData->defaultValueAvailable()) {
485
            return _metaData->rawDefaultValue() == rawValue();
Don Gagne's avatar
Don Gagne committed
486 487 488
        } else {
            return false;
        }
489
    } else {
Don Gagne's avatar
Don Gagne committed
490
        qWarning() << "Meta data pointer missing";
491 492 493 494
        return false;
    }
}

495
bool Fact::defaultValueAvailable(void) const
496
{
Don Gagne's avatar
Don Gagne committed
497 498 499 500 501 502 503 504
    if (_metaData) {
        return _metaData->defaultValueAvailable();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
}

505
QString Fact::validate(const QString& cookedValue, bool convertOnly)
Don Gagne's avatar
Don Gagne committed
506 507 508 509 510
{
    if (_metaData) {
        QVariant    typedValue;
        QString     errorString;
        
511
        _metaData->convertAndValidateCooked(cookedValue, convertOnly, typedValue, errorString);
Don Gagne's avatar
Don Gagne committed
512 513 514 515 516 517 518
        
        return errorString;
    } else {
        qWarning() << "Meta data pointer missing";
        return QString("Internal error: Meta data pointer missing");
    }
}
519 520 521 522 523 524 525 526 527 528

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
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554

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());
    }
}
555 556 557 558 559 560 561 562 563 564 565 566 567 568

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
569 570 571 572 573 574 575 576 577 578

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