Fact.cc 13.1 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 273 274 275
        }
    }
        break;
    default:
        valueString = variant.toString();
        break;
276 277 278
    }

    return valueString;
279 280
}

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


287 288
QString Fact::rawValueString(void) const
{
289
    return _variantToString(rawValue(), decimalPlaces());
290 291 292
}

QString Fact::cookedValueString(void) const
293
{
294
    return _variantToString(cookedValue(), decimalPlaces());
295 296
}

297 298 299 300 301 302 303 304 305 306 307 308 309 310
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
311
{
Don Gagne's avatar
Don Gagne committed
312 313 314 315
    if (_metaData) {
        if (!_metaData->defaultValueAvailable()) {
            qDebug() << "Access to unavailable default value";
        }
316
        return _metaData->cookedDefaultValue();
Don Gagne's avatar
Don Gagne committed
317 318 319
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
320
    }
321 322
}

323
QString Fact::cookedDefaultValueString(void) const
324
{
325
    return _variantToString(cookedDefaultValue(), decimalPlaces());
326 327
}

328
FactMetaData::ValueType_t Fact::type(void) const
329
{
330
    return _type;
331 332
}

333
QString Fact::shortDescription(void) const
334
{
Don Gagne's avatar
Don Gagne committed
335 336 337 338 339 340
    if (_metaData) {
        return _metaData->shortDescription();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
341 342
}

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

353 354 355 356 357 358 359 360 361 362 363
QString Fact::rawUnits(void) const
{
    if (_metaData) {
        return _metaData->rawUnits();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
}

QString Fact::cookedUnits(void) const
364
{
Don Gagne's avatar
Don Gagne committed
365
    if (_metaData) {
366
        return _metaData->cookedUnits();
Don Gagne's avatar
Don Gagne committed
367 368 369 370
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
371 372
}

373
QVariant Fact::rawMin(void) const
374
{
Don Gagne's avatar
Don Gagne committed
375
    if (_metaData) {
376
        return _metaData->rawMin();
Don Gagne's avatar
Don Gagne committed
377 378 379 380
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
381 382
}

383
QVariant Fact::cookedMin(void) const
384
{
385 386 387 388 389 390
    if (_metaData) {
        return _metaData->cookedMin();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
391 392
}

393 394
QString Fact::cookedMinString(void) const
{
395
    return _variantToString(cookedMin(), decimalPlaces());
396 397 398
}

QVariant Fact::rawMax(void) const
399
{
Don Gagne's avatar
Don Gagne committed
400
    if (_metaData) {
401
        return _metaData->rawMax();
Don Gagne's avatar
Don Gagne committed
402 403 404 405 406 407
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
}

408
QVariant Fact::cookedMax(void) const
409
{
410 411 412 413 414 415 416 417 418 419
    if (_metaData) {
        return _metaData->cookedMax();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
}

QString Fact::cookedMaxString(void) const
{
420
    return _variantToString(cookedMax(), decimalPlaces());
421 422
}

423
bool Fact::minIsDefaultForType(void) const
Don Gagne's avatar
Don Gagne committed
424 425 426 427 428 429 430 431 432
{
    if (_metaData) {
        return _metaData->minIsDefaultForType();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
}

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

443 444 445 446 447 448 449 450 451 452
int Fact::decimalPlaces(void) const
{
    if (_metaData) {
        return _metaData->decimalPlaces();
    } else {
        qWarning() << "Meta data pointer missing";
        return FactMetaData::defaultDecimalPlaces;
    }
}

453
QString Fact::group(void) const
454
{
Don Gagne's avatar
Don Gagne committed
455 456 457 458 459 460
    if (_metaData) {
        return _metaData->group();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
461 462
}

463 464 465
void Fact::setMetaData(FactMetaData* metaData)
{
    _metaData = metaData;
Don Gagne's avatar
Don Gagne committed
466
    emit valueChanged(cookedValue());
467
}
468

469
bool Fact::valueEqualsDefault(void) const
470
{
Don Gagne's avatar
Don Gagne committed
471 472
    if (_metaData) {
        if (_metaData->defaultValueAvailable()) {
473
            return _metaData->rawDefaultValue() == rawValue();
Don Gagne's avatar
Don Gagne committed
474 475 476
        } else {
            return false;
        }
477
    } else {
Don Gagne's avatar
Don Gagne committed
478
        qWarning() << "Meta data pointer missing";
479 480 481 482
        return false;
    }
}

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

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

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
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542

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());
    }
}
543 544 545 546 547 548 549 550 551 552 553 554 555 556

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
557 558 559 560 561 562 563 564 565 566

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