Fact.cc 13.3 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
/*=====================================================================
 
 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"
Don Gagne's avatar
Don Gagne committed
28
#include "QGCMAVLink.h"
Don Gagne's avatar
Don Gagne committed
29 30 31

#include <QtQml>

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

Don Gagne's avatar
Don Gagne committed
45 46 47 48
Fact::Fact(int componentId, QString name, FactMetaData::ValueType_t type, QObject* parent)
    : QObject(parent)
    , _name(name)
    , _componentId(componentId)
49
    , _rawValue(0)
Don Gagne's avatar
Don Gagne committed
50 51
    , _type(type)
    , _metaData(NULL)
Don Gagne's avatar
Don Gagne committed
52 53
    , _sendValueChangedSignals(true)
    , _deferredValueChangeSignal(false)
Don Gagne's avatar
Don Gagne committed
54
{
Don Gagne's avatar
Don Gagne committed
55 56 57
    FactMetaData* metaData = new FactMetaData(_type, this);
    setMetaData(metaData);
}
Don Gagne's avatar
Don Gagne committed
58

Don Gagne's avatar
Don Gagne committed
59 60 61 62 63 64 65 66
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
67 68 69 70 71 72 73
    _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
74 75 76 77 78 79 80
    if (_metaData && other._metaData) {
        *_metaData = *other._metaData;
    } else {
        _metaData = NULL;
    }
    
    return *this;
Don Gagne's avatar
Don Gagne committed
81 82
}

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

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

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

void Fact::setEnumIndex(int index)
{
    if (_metaData) {
Don Gagne's avatar
Don Gagne committed
141
        setCookedValue(_metaData->enumValues()[index]);
142 143 144 145 146
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

Don Gagne's avatar
Don Gagne committed
147
void Fact::_containerSetRawValue(const QVariant& value)
Don Gagne's avatar
Don Gagne committed
148
{
149
    _rawValue = value;
Don Gagne's avatar
Don Gagne committed
150
    _sendValueChangedSignal(cookedValue());
151
    emit vehicleUpdated(_rawValue);
Don Gagne's avatar
Don Gagne committed
152
}
153 154 155 156 157 158

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

159 160 161 162 163
int Fact::componentId(void) const
{
    return _componentId;
}

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

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

    return QString();
}

188
int Fact::enumIndex(void)
189 190 191
{
    if (_metaData) {
        int index = 0;
192

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

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

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
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();
    }
}

252
QString Fact::_variantToString(const QVariant& variant, int decimalPlaces) const
253
{
254 255 256
    QString valueString;

    switch (type()) {
Don Gagne's avatar
Don Gagne committed
257 258 259 260 261 262
    case FactMetaData::valueTypeFloat:
    {
        float fValue = variant.toFloat();
        if (qIsNaN(fValue)) {
            valueString = QStringLiteral("--.--");
        } else {
263
            valueString = QString("%1").arg(fValue, 0, 'f', decimalPlaces);
Don Gagne's avatar
Don Gagne committed
264 265 266 267 268 269 270 271 272
        }
    }
        break;
    case FactMetaData::valueTypeDouble:
    {
        double dValue = variant.toDouble();
        if (qIsNaN(dValue)) {
            valueString = QStringLiteral("--.--");
        } else {
273
            valueString = QString("%1").arg(dValue, 0, 'f', decimalPlaces);
Don Gagne's avatar
Don Gagne committed
274 275 276 277 278 279
        }
    }
        break;
    default:
        valueString = variant.toString();
        break;
280 281 282
    }

    return valueString;
283 284
}

285 286 287 288 289 290
QString Fact::rawValueStringFullPrecision(void) const
{
    return _variantToString(rawValue(), 18);
}


291 292
QString Fact::rawValueString(void) const
{
293
    return _variantToString(rawValue(), decimalPlaces());
294 295 296
}

QString Fact::cookedValueString(void) const
297
{
298
    return _variantToString(cookedValue(), decimalPlaces());
299 300
}

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

327
QString Fact::cookedDefaultValueString(void) const
328
{
329
    return _variantToString(cookedDefaultValue(), decimalPlaces());
330 331
}

332
FactMetaData::ValueType_t Fact::type(void) const
333
{
334
    return _type;
335 336
}

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

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

357 358 359 360 361 362 363 364 365 366 367
QString Fact::rawUnits(void) const
{
    if (_metaData) {
        return _metaData->rawUnits();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
}

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

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

387
QVariant Fact::cookedMin(void) const
388
{
389 390 391 392 393 394
    if (_metaData) {
        return _metaData->cookedMin();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
395 396
}

397 398
QString Fact::cookedMinString(void) const
{
399
    return _variantToString(cookedMin(), decimalPlaces());
400 401 402
}

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

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

QString Fact::cookedMaxString(void) const
{
424
    return _variantToString(cookedMax(), decimalPlaces());
425 426
}

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

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

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

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

467 468 469
void Fact::setMetaData(FactMetaData* metaData)
{
    _metaData = metaData;
Don Gagne's avatar
Don Gagne committed
470
    emit valueChanged(cookedValue());
471
}
472

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

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

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

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
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

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());
    }
}
547 548 549 550 551 552 553 554 555 556 557 558 559 560

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
561 562 563 564 565 566 567 568 569 570

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