Fact.cc 12.7 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 81 82 83 84 85
        }
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

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

Don Gagne's avatar
Don Gagne committed
104
void Fact::setCookedValue(const QVariant& value)
105 106 107 108 109 110 111 112 113 114 115 116 117
{
    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
118
            setCookedValue(_metaData->enumValues()[index]);
119 120 121 122 123 124 125 126 127
        }
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

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

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

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

146 147 148 149 150
int Fact::componentId(void) const
{
    return _componentId;
}

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

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

    return QString();
}

175
int Fact::enumIndex(void)
176 177 178
{
    if (_metaData) {
        int index = 0;
179

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

        // Current value is not in list, add it manually
        _metaData->addEnumInfo(QString("Unknown: %1").arg(rawValue().toString()), rawValue());
        emit enumStringsChanged();
        emit enumValuesChanged();
        return index;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    } 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();
    }
217 218
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
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();
    }
}

239
QString Fact::_variantToString(const QVariant& variant, int decimalPlaces) const
240
{
241 242 243
    QString valueString;

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

    return valueString;
270 271
}

272 273 274 275 276 277
QString Fact::rawValueStringFullPrecision(void) const
{
    return _variantToString(rawValue(), 18);
}


278 279
QString Fact::rawValueString(void) const
{
280
    return _variantToString(rawValue(), decimalPlaces());
281 282 283
}

QString Fact::cookedValueString(void) const
284
{
285
    return _variantToString(cookedValue(), decimalPlaces());
286 287
}

288 289 290 291 292 293 294 295 296 297 298 299 300 301
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
302
{
Don Gagne's avatar
Don Gagne committed
303 304 305 306
    if (_metaData) {
        if (!_metaData->defaultValueAvailable()) {
            qDebug() << "Access to unavailable default value";
        }
307
        return _metaData->cookedDefaultValue();
Don Gagne's avatar
Don Gagne committed
308 309 310
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
311
    }
312 313
}

314
QString Fact::cookedDefaultValueString(void) const
315
{
316
    return _variantToString(cookedDefaultValue(), decimalPlaces());
317 318
}

319
FactMetaData::ValueType_t Fact::type(void) const
320
{
321
    return _type;
322 323
}

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

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

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

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

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

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

384 385
QString Fact::cookedMinString(void) const
{
386
    return _variantToString(cookedMin(), decimalPlaces());
387 388 389
}

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

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

QString Fact::cookedMaxString(void) const
{
411
    return _variantToString(cookedMax(), decimalPlaces());
412 413
}

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

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

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

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

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

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

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

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

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
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533

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());
    }
}
534 535 536 537 538 539 540 541 542 543 544 545 546 547

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
548 549 550 551 552 553 554 555 556 557

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