FactMetaData.cc 22.4 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 15 16

/// @file
///     @brief Object which exposes a FactMetaData
///
///     @author Don Gagne <don@thegagnes.com>

#include "FactMetaData.h"
17
#include "QGroundControlQmlGlobal.h"
Don Gagne's avatar
Don Gagne committed
18

19
#include <QDebug>
20
#include <QtMath>
21

Don Gagne's avatar
Don Gagne committed
22
#include <limits>
23 24
#include <cmath>

25 26 27 28 29 30 31 32 33 34 35
// Conversion Constants
// Time
const qreal FactMetaData::UnitConsts_s::secondsPerHour = 3600.0;

// Velocity
const qreal FactMetaData::UnitConsts_s::knotsToKPH = 1.852; // exact, hence weird base for knotsToMetersPerSecond

// Length
const qreal FactMetaData::UnitConsts_s::milesToMeters = 1609.344;
const qreal FactMetaData::UnitConsts_s::feetToMeters = 0.3048;

36 37
// Built in translations for all Facts
const FactMetaData::BuiltInTranslation_s FactMetaData::_rgBuiltInTranslations[] = {
38 39
    { "centi-degrees",  "deg",  FactMetaData::_centiDegreesToDegrees,   FactMetaData::_degreesToCentiDegrees },
    { "radians",        "deg",  FactMetaData::_radiansToDegrees,        FactMetaData::_degreesToRadians },
40
    { "norm",           "%",  FactMetaData::_normToPercent,           FactMetaData::_percentToNorm },
41
};
Don Gagne's avatar
Don Gagne committed
42

43 44 45 46 47 48 49 50 51 52 53 54 55
// Translations driven by app settings
const FactMetaData::AppSettingsTranslation_s FactMetaData::_rgAppSettingsTranslations[] = {
    { "m",      "m",        false,  QGroundControlQmlGlobal::DistanceUnitsMeters,           FactMetaData::_defaultTranslator,                   FactMetaData::_defaultTranslator },
    { "meters", "meters",   false,  QGroundControlQmlGlobal::DistanceUnitsMeters,           FactMetaData::_defaultTranslator,                   FactMetaData::_defaultTranslator },
    { "m/s",    "m/s",      true,   QGroundControlQmlGlobal::SpeedUnitsMetersPerSecond,     FactMetaData::_defaultTranslator,                   FactMetaData::_defaultTranslator },
    { "m",      "ft",       false,  QGroundControlQmlGlobal::DistanceUnitsFeet,             FactMetaData::_metersToFeet,                        FactMetaData::_feetToMeters },
    { "meters", "ft",       false,  QGroundControlQmlGlobal::DistanceUnitsFeet,             FactMetaData::_metersToFeet,                        FactMetaData::_feetToMeters },
    { "m/s",    "ft/s",     true,   QGroundControlQmlGlobal::SpeedUnitsFeetPerSecond,       FactMetaData::_metersToFeet,                        FactMetaData::_feetToMeters },
    { "m/s",    "mph",      true,   QGroundControlQmlGlobal::SpeedUnitsMilesPerHour,        FactMetaData::_metersPerSecondToMilesPerHour,       FactMetaData::_milesPerHourToMetersPerSecond },
    { "m/s",    "km/h",     true,   QGroundControlQmlGlobal::SpeedUnitsKilometersPerHour,   FactMetaData::_metersPerSecondToKilometersPerHour,  FactMetaData::_kilometersPerHourToMetersPerSecond },
    { "m/s",    "kn",       true,   QGroundControlQmlGlobal::SpeedUnitsKnots,               FactMetaData::_metersPerSecondToKnots,              FactMetaData::_knotsToMetersPerSecond },
};

56 57 58
FactMetaData::FactMetaData(QObject* parent)
    : QObject(parent)
    , _type(valueTypeInt32)
59
    , _decimalPlaces(unknownDecimalPlaces)
60
    , _rawDefaultValue(0)
61
    , _defaultValueAvailable(false)
62
    , _group("*Default Group")
63
    , _rawMax(_maxForType())
64
    , _maxIsDefaultForType(true)
65
    , _rawMin(_minForType())
66
    , _minIsDefaultForType(true)
67 68
    , _rawTranslator(_defaultTranslator)
    , _cookedTranslator(_defaultTranslator)
69
    , _rebootRequired(false)
Don Gagne's avatar
Don Gagne committed
70
    , _increment(std::numeric_limits<double>::quiet_NaN())
Don Gagne's avatar
Don Gagne committed
71 72 73 74
{

}

75 76 77
FactMetaData::FactMetaData(ValueType_t type, QObject* parent)
    : QObject(parent)
    , _type(type)
78
    , _decimalPlaces(unknownDecimalPlaces)
79
    , _rawDefaultValue(0)
80
    , _defaultValueAvailable(false)
81
    , _group("*Default Group")
82
    , _rawMax(_maxForType())
83
    , _maxIsDefaultForType(true)
84
    , _rawMin(_minForType())
85
    , _minIsDefaultForType(true)
86 87
    , _rawTranslator(_defaultTranslator)
    , _cookedTranslator(_defaultTranslator)
88
    , _rebootRequired(false)
Don Gagne's avatar
Don Gagne committed
89
    , _increment(std::numeric_limits<double>::quiet_NaN())
90 91 92 93
{

}

Don Gagne's avatar
Don Gagne committed
94 95 96 97 98 99 100 101
FactMetaData::FactMetaData(const FactMetaData& other, QObject* parent)
    : QObject(parent)
{
    *this = other;
}

const FactMetaData& FactMetaData::operator=(const FactMetaData& other)
{
102
    _decimalPlaces          = other._decimalPlaces;
103
    _rawDefaultValue        = other._rawDefaultValue;
Don Gagne's avatar
Don Gagne committed
104
    _defaultValueAvailable  = other._defaultValueAvailable;
105 106
    _bitmaskStrings         = other._bitmaskStrings;
    _bitmaskValues          = other._bitmaskValues;
107 108 109 110
    _enumStrings            = other._enumStrings;
    _enumValues             = other._enumValues;
    _group                  = other._group;
    _longDescription        = other._longDescription;
111
    _rawMax                 = other._rawMax;
Don Gagne's avatar
Don Gagne committed
112
    _maxIsDefaultForType    = other._maxIsDefaultForType;
113
    _rawMin                 = other._rawMin;
114 115 116 117
    _minIsDefaultForType    = other._minIsDefaultForType;
    _name                   = other._name;
    _shortDescription       = other._shortDescription;
    _type                   = other._type;
118 119
    _rawUnits               = other._rawUnits;
    _cookedUnits            = other._cookedUnits;
120 121
    _rawTranslator          = other._rawTranslator;
    _cookedTranslator       = other._cookedTranslator;
122
    _rebootRequired         = other._rebootRequired;
Don Gagne's avatar
Don Gagne committed
123
    _increment              = other._increment;
124

Don Gagne's avatar
Don Gagne committed
125 126 127
    return *this;
}

128
QVariant FactMetaData::rawDefaultValue(void) const
129 130
{
    if (_defaultValueAvailable) {
131
        return _rawDefaultValue;
132 133 134 135 136 137
    } else {
        qWarning() << "Attempt to access unavailable default value";
        return QVariant(0);
    }
}

138
void FactMetaData::setRawDefaultValue(const QVariant& rawDefaultValue)
139
{
140 141
    if (_rawMin <= rawDefaultValue && rawDefaultValue <= _rawMax) {
        _rawDefaultValue = rawDefaultValue;
142 143 144 145 146 147
        _defaultValueAvailable = true;
    } else {
        qWarning() << "Attempt to set default value which is outside min/max range";
    }
}

148
void FactMetaData::setRawMin(const QVariant& rawMin)
149
{
Don Gagne's avatar
Don Gagne committed
150
    if (rawMin >= _minForType()) {
151
        _rawMin = rawMin;
Don Gagne's avatar
Don Gagne committed
152
        _minIsDefaultForType = false;
153
    } else {
154
        qWarning() << "Attempt to set min below allowable value for fact: " << name()
155
                   << ", value attempted: " << rawMin
156
                   << ", type: " << type() << ", min for type: " << _minForType();
157
        _rawMin = _minForType();
158 159 160
    }
}

161
void FactMetaData::setRawMax(const QVariant& rawMax)
162
{
163
    if (rawMax > _maxForType()) {
164
        qWarning() << "Attempt to set max above allowable value";
165
        _rawMax = _maxForType();
166
    } else {
167
        _rawMax = rawMax;
Don Gagne's avatar
Don Gagne committed
168
        _maxIsDefaultForType = false;
169 170 171
    }
}

172
QVariant FactMetaData::_minForType(void) const
Don Gagne's avatar
Don Gagne committed
173
{
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    switch (_type) {
        case valueTypeUint8:
            return QVariant(std::numeric_limits<unsigned char>::min());
        case valueTypeInt8:
            return QVariant(std::numeric_limits<signed char>::min());
        case valueTypeUint16:
            return QVariant(std::numeric_limits<unsigned short int>::min());
        case valueTypeInt16:
            return QVariant(std::numeric_limits<short int>::min());
        case valueTypeUint32:
            return QVariant(std::numeric_limits<unsigned int>::min());
        case valueTypeInt32:
            return QVariant(std::numeric_limits<int>::min());
        case valueTypeFloat:
            return QVariant(-std::numeric_limits<float>::max());
        case valueTypeDouble:
            return QVariant(-std::numeric_limits<double>::max());
    }
Don Gagne's avatar
Don Gagne committed
192 193 194
    
    // Make windows compiler happy, even switch is full cased
    return QVariant();
Don Gagne's avatar
Don Gagne committed
195
}
Don Gagne's avatar
Don Gagne committed
196

197
QVariant FactMetaData::_maxForType(void) const
Don Gagne's avatar
Don Gagne committed
198
{
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    switch (_type) {
        case valueTypeUint8:
            return QVariant(std::numeric_limits<unsigned char>::max());
        case valueTypeInt8:
            return QVariant(std::numeric_limits<signed char>::max());
        case valueTypeUint16:
            return QVariant(std::numeric_limits<unsigned short int>::max());
        case valueTypeInt16:
            return QVariant(std::numeric_limits<short int>::max());
        case valueTypeUint32:
            return QVariant(std::numeric_limits<unsigned int>::max());
        case valueTypeInt32:
            return QVariant(std::numeric_limits<int>::max());
        case valueTypeFloat:
            return QVariant(std::numeric_limits<float>::max());
        case valueTypeDouble:
            return QVariant(std::numeric_limits<double>::max());
    }
Don Gagne's avatar
Don Gagne committed
217 218 219
    
    // Make windows compiler happy, even switch is full cased
    return QVariant();
Don Gagne's avatar
Don Gagne committed
220
}
Don Gagne's avatar
Don Gagne committed
221

222
bool FactMetaData::convertAndValidateRaw(const QVariant& rawValue, bool convertOnly, QVariant& typedValue, QString& errorString)
Don Gagne's avatar
Don Gagne committed
223
{
224
    bool convertOk = false;
Don Gagne's avatar
Don Gagne committed
225 226 227 228 229 230 231
    
    errorString.clear();
    
    switch (type()) {
        case FactMetaData::valueTypeInt8:
        case FactMetaData::valueTypeInt16:
        case FactMetaData::valueTypeInt32:
232
            typedValue = QVariant(rawValue.toInt(&convertOk));
Don Gagne's avatar
Don Gagne committed
233
            if (!convertOnly && convertOk) {
234 235
                if (rawMin() > typedValue || typedValue > rawMax()) {
                    errorString = QString("Value must be within %1 and %2").arg(cookedMin().toInt()).arg(cookedMax().toInt());
Don Gagne's avatar
Don Gagne committed
236 237 238 239 240 241 242
                }
            }
            break;
            
        case FactMetaData::valueTypeUint8:
        case FactMetaData::valueTypeUint16:
        case FactMetaData::valueTypeUint32:
243
            typedValue = QVariant(rawValue.toUInt(&convertOk));
Don Gagne's avatar
Don Gagne committed
244
            if (!convertOnly && convertOk) {
245 246
                if (rawMin() > typedValue || typedValue > rawMax()) {
                    errorString = QString("Value must be within %1 and %2").arg(cookedMin().toUInt()).arg(cookedMax().toUInt());
Don Gagne's avatar
Don Gagne committed
247 248 249 250 251
                }
            }
            break;
            
        case FactMetaData::valueTypeFloat:
252
            typedValue = QVariant(rawValue.toFloat(&convertOk));
Don Gagne's avatar
Don Gagne committed
253
            if (!convertOnly && convertOk) {
254 255
                if (rawMin() > typedValue || typedValue > rawMax()) {
                    errorString = QString("Value must be within %1 and %2").arg(cookedMin().toFloat()).arg(cookedMax().toFloat());
Don Gagne's avatar
Don Gagne committed
256 257 258 259 260
                }
            }
            break;
            
        case FactMetaData::valueTypeDouble:
261
            typedValue = QVariant(rawValue.toDouble(&convertOk));
Don Gagne's avatar
Don Gagne committed
262
            if (!convertOnly && convertOk) {
263 264
                if (rawMin() > typedValue || typedValue > rawMax()) {
                    errorString = QString("Value must be within %1 and %2").arg(cookedMin().toDouble()).arg(cookedMax().toDouble());
Don Gagne's avatar
Don Gagne committed
265 266 267 268 269 270
                }
            }
            break;
    }
    
    if (!convertOk) {
271
        errorString += "Invalid number";
Don Gagne's avatar
Don Gagne committed
272 273 274 275
    }
    
    return convertOk && errorString.isEmpty();
}
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
bool FactMetaData::convertAndValidateCooked(const QVariant& cookedValue, bool convertOnly, QVariant& typedValue, QString& errorString)
{
    bool convertOk = false;

    errorString.clear();

    switch (type()) {
        case FactMetaData::valueTypeInt8:
        case FactMetaData::valueTypeInt16:
        case FactMetaData::valueTypeInt32:
            typedValue = QVariant(cookedValue.toInt(&convertOk));
            if (!convertOnly && convertOk) {
                if (cookedMin() > typedValue || typedValue > cookedMax()) {
                    errorString = QString("Value must be within %1 and %2").arg(cookedMin().toInt()).arg(cookedMax().toInt());
                }
            }
            break;

        case FactMetaData::valueTypeUint8:
        case FactMetaData::valueTypeUint16:
        case FactMetaData::valueTypeUint32:
            typedValue = QVariant(cookedValue.toUInt(&convertOk));
            if (!convertOnly && convertOk) {
                if (cookedMin() > typedValue || typedValue > cookedMax()) {
                    errorString = QString("Value must be within %1 and %2").arg(cookedMin().toUInt()).arg(cookedMax().toUInt());
                }
            }
            break;

        case FactMetaData::valueTypeFloat:
            typedValue = QVariant(cookedValue.toFloat(&convertOk));
            if (!convertOnly && convertOk) {
                if (cookedMin() > typedValue || typedValue > cookedMax()) {
                    errorString = QString("Value must be within %1 and %2").arg(cookedMin().toFloat()).arg(cookedMax().toFloat());
                }
            }
            break;

        case FactMetaData::valueTypeDouble:
            typedValue = QVariant(cookedValue.toDouble(&convertOk));
            if (!convertOnly && convertOk) {
                if (cookedMin() > typedValue || typedValue > cookedMax()) {
                    errorString = QString("Value must be within %1 and %2").arg(cookedMin().toDouble()).arg(cookedMax().toDouble());
                }
            }
            break;
    }

    if (!convertOk) {
        errorString += "Invalid number";
    }

    return convertOk && errorString.isEmpty();
}

332 333 334 335 336 337 338 339 340
void FactMetaData::setBitmaskInfo(const QStringList& strings, const QVariantList& values)
{
    if (strings.count() != values.count()) {
        qWarning() << "Count mismatch strings:values" << strings.count() << values.count();
        return;
    }

    _bitmaskStrings = strings;
    _bitmaskValues = values;
341
    setBuiltInTranslator();
342 343 344 345 346 347 348 349
}

void FactMetaData::addBitmaskInfo(const QString& name, const QVariant& value)
{
    _bitmaskStrings << name;
    _bitmaskValues << value;
}

350 351 352 353 354 355 356 357 358
void FactMetaData::setEnumInfo(const QStringList& strings, const QVariantList& values)
{
    if (strings.count() != values.count()) {
        qWarning() << "Count mismatch strings:values" << strings.count() << values.count();
        return;
    }

    _enumStrings = strings;
    _enumValues = values;
359
    setBuiltInTranslator();
360 361
}

362 363 364 365 366 367
void FactMetaData::addEnumInfo(const QString& name, const QVariant& value)
{
    _enumStrings << name;
    _enumValues << value;
}

368 369 370 371 372
void FactMetaData::setTranslators(Translator rawTranslator, Translator cookedTranslator)
{
    _rawTranslator = rawTranslator;
    _cookedTranslator = cookedTranslator;
}
373

374
void FactMetaData::setBuiltInTranslator(void)
375 376 377 378 379 380
{
    if (_enumStrings.count()) {
        // No translation if enum
        setTranslators(_defaultTranslator, _defaultTranslator);
        _cookedUnits = _rawUnits;
    } else {
381 382
        for (size_t i=0; i<sizeof(_rgBuiltInTranslations)/sizeof(_rgBuiltInTranslations[0]); i++) {
            const BuiltInTranslation_s* pBuiltInTranslation = &_rgBuiltInTranslations[i];
383 384 385 386 387 388 389 390 391 392 393

            if (pBuiltInTranslation->rawUnits == _rawUnits.toLower()) {
                _cookedUnits = pBuiltInTranslation->cookedUnits;
                setTranslators(pBuiltInTranslation->rawTranslator, pBuiltInTranslation->cookedTranslator);
            }
        }
    }
}

QVariant FactMetaData::_degreesToRadians(const QVariant& degrees)
{
394
    return QVariant(qDegreesToRadians(degrees.toDouble()));
395 396 397 398
}

QVariant FactMetaData::_radiansToDegrees(const QVariant& radians)
{
399
    return QVariant(qRadiansToDegrees(radians.toDouble()));
400 401 402 403
}

QVariant FactMetaData::_centiDegreesToDegrees(const QVariant& centiDegrees)
{
404
    return QVariant(centiDegrees.toReal() / 100.0);
405 406 407 408
}

QVariant FactMetaData::_degreesToCentiDegrees(const QVariant& degrees)
{
409
    return QVariant(qRound(degrees.toReal() * 100.0));
410 411
}

412 413
QVariant FactMetaData::_metersToFeet(const QVariant& meters)
{
414
    return QVariant(meters.toDouble() * 1.0/constants.feetToMeters);
415 416 417 418
}

QVariant FactMetaData::_feetToMeters(const QVariant& feet)
{
419
    return QVariant(feet.toDouble() * constants.feetToMeters);
420 421 422 423
}

QVariant FactMetaData::_metersPerSecondToMilesPerHour(const QVariant& metersPerSecond)
{
424
    return QVariant((metersPerSecond.toDouble() * 1.0/constants.milesToMeters) * constants.secondsPerHour);
425 426 427 428
}

QVariant FactMetaData::_milesPerHourToMetersPerSecond(const QVariant& milesPerHour)
{
429
    return QVariant((milesPerHour.toDouble() * constants.milesToMeters) / constants.secondsPerHour);
430 431 432 433
}

QVariant FactMetaData::_metersPerSecondToKilometersPerHour(const QVariant& metersPerSecond)
{
434
    return QVariant((metersPerSecond.toDouble() / 1000.0) * constants.secondsPerHour);
435 436 437 438
}

QVariant FactMetaData::_kilometersPerHourToMetersPerSecond(const QVariant& kilometersPerHour)
{
439
    return QVariant((kilometersPerHour.toDouble() * 1000.0) / constants.secondsPerHour);
440 441 442 443
}

QVariant FactMetaData::_metersPerSecondToKnots(const QVariant& metersPerSecond)
{
444
    return QVariant(metersPerSecond.toDouble() * constants.secondsPerHour / (1000.0 * constants.knotsToKPH));
445 446 447 448
}

QVariant FactMetaData::_knotsToMetersPerSecond(const QVariant& knots)
{
449
    return QVariant(knots.toDouble() * (constants.knotsToKPH / (1000.0 * constants.secondsPerHour)));
450 451
}

452 453 454 455 456 457 458 459 460 461
QVariant FactMetaData::_percentToNorm(const QVariant& percent)
{
    return QVariant(percent.toDouble() / 100.0);
}

QVariant FactMetaData::_normToPercent(const QVariant& normalized)
{
    return QVariant(normalized.toDouble() * 100.0);
}

462 463 464 465 466
void FactMetaData::setRawUnits(const QString& rawUnits)
{
    _rawUnits = rawUnits;
    _cookedUnits = rawUnits;

467
    setBuiltInTranslator();
468
}
Don Gagne's avatar
Don Gagne committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

FactMetaData::ValueType_t FactMetaData::stringToType(const QString& typeString, bool& unknownType)
{
    QStringList         knownTypeStrings;
    QList<ValueType_t>  knownTypes;

    unknownType = false;

    knownTypeStrings << QStringLiteral("Uint8")
                        << QStringLiteral("Int8")
                        << QStringLiteral("Uint16")
                        << QStringLiteral("Int16")
                        << QStringLiteral("Uint32")
                        << QStringLiteral("Int32")
                        << QStringLiteral("Float")
                        << QStringLiteral("Double");

    knownTypes << valueTypeUint8
                << valueTypeInt8
                << valueTypeUint16
                << valueTypeInt16
                << valueTypeUint32
                << valueTypeInt32
                << valueTypeFloat
                << valueTypeDouble;

    for (int i=0; i<knownTypeStrings.count(); i++) {
        if (knownTypeStrings[i].compare(typeString, Qt::CaseInsensitive) == 0) {
            return knownTypes[i];
        }
    }

    unknownType = true;

    return valueTypeDouble;
}
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529

size_t FactMetaData::typeToSize(ValueType_t type)
{
    switch (type) {
        case valueTypeUint8:
        case valueTypeInt8:
            return 1;

        case valueTypeUint16:
        case valueTypeInt16:
            return 2;

        case valueTypeUint32:
        case valueTypeInt32:
        case valueTypeFloat:
            return 4;

        case valueTypeDouble:
            return 8;

        default:
            qWarning() << "Unsupported fact value type" << type;
            return 4;
    }
}
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

void FactMetaData::setAppSettingsTranslators(void)
{
    if (!_enumStrings.count()) {
        for (size_t i=0; i<sizeof(_rgAppSettingsTranslations)/sizeof(_rgAppSettingsTranslations[0]); i++) {
            const AppSettingsTranslation_s* pAppSettingsTranslation = &_rgAppSettingsTranslations[i];

            if (pAppSettingsTranslation->rawUnits == _rawUnits.toLower() &&
                    ((pAppSettingsTranslation->speed && pAppSettingsTranslation->speedOrDistanceUnits == QGroundControlQmlGlobal::speedUnits()->rawValue().toUInt()) ||
                     (!pAppSettingsTranslation->speed && pAppSettingsTranslation->speedOrDistanceUnits == QGroundControlQmlGlobal::distanceUnits()->rawValue().toUInt()))) {
                _cookedUnits = pAppSettingsTranslation->cookedUnits;
                setTranslators(pAppSettingsTranslation->rawTranslator, pAppSettingsTranslation->cookedTranslator);
                return;
            }
        }
    }
}
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590

const FactMetaData::AppSettingsTranslation_s* FactMetaData::_findAppSettingsDistanceUnitsTranslation(const QString& rawUnits)
{
    for (size_t i=0; i<sizeof(_rgAppSettingsTranslations)/sizeof(_rgAppSettingsTranslations[0]); i++) {
        const AppSettingsTranslation_s* pAppSettingsTranslation = &_rgAppSettingsTranslations[i];

        if (pAppSettingsTranslation->rawUnits == rawUnits &&
                 (!pAppSettingsTranslation->speed && pAppSettingsTranslation->speedOrDistanceUnits == QGroundControlQmlGlobal::distanceUnits()->rawValue().toUInt())) {
            return pAppSettingsTranslation;
        }
    }

    return NULL;
}

QVariant FactMetaData::metersToAppSettingsDistanceUnits(const QVariant& meters)
{
    const AppSettingsTranslation_s* pAppSettingsTranslation = _findAppSettingsDistanceUnitsTranslation("m");
    if (pAppSettingsTranslation) {
        return pAppSettingsTranslation->rawTranslator(meters);
    } else {
        return meters;
    }
}

QVariant FactMetaData::appSettingsDistanceUnitsToMeters(const QVariant& distance)
{
    const AppSettingsTranslation_s* pAppSettingsTranslation = _findAppSettingsDistanceUnitsTranslation("m");
    if (pAppSettingsTranslation) {
        return pAppSettingsTranslation->cookedTranslator(distance);
    } else {
        return distance;
    }
}

QString FactMetaData::appSettingsDistanceUnitsString(void)
{
    const AppSettingsTranslation_s* pAppSettingsTranslation = _findAppSettingsDistanceUnitsTranslation("m");
    if (pAppSettingsTranslation) {
        return pAppSettingsTranslation->cookedUnits;
    } else {
        return QStringLiteral("m");
    }
}
591 592 593 594 595 596 597

int FactMetaData::decimalPlaces(void) const
{
    int actualDecimalPlaces = defaultDecimalPlaces;
    int incrementDecimalPlaces = unknownDecimalPlaces;

    // First determine decimal places from increment
598
    double increment = _rawTranslator(this->increment()).toDouble();
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
    if (!qIsNaN(increment)) {
        double integralPart;

        // Get the fractional part only
        increment = fabs(modf(increment, &integralPart));
        if (increment == 0.0) {
            // No fractional part, so no decimal places
            incrementDecimalPlaces = 0;
        } else {
            incrementDecimalPlaces = -ceil(log10(increment));
        }
    }

    // Correct decimal places is the larger of the two, increment or meta data value
    if (incrementDecimalPlaces != unknownDecimalPlaces && _decimalPlaces == unknownDecimalPlaces) {
        actualDecimalPlaces = incrementDecimalPlaces;
    } else {
616 617
        // Adjust decimal places for cooked translation
        int settingsDecimalPlaces = _decimalPlaces == unknownDecimalPlaces ? defaultDecimalPlaces : _decimalPlaces;
618 619 620 621 622 623 624 625
        double ctest = _rawTranslator(1.0).toDouble();

        settingsDecimalPlaces += -log10(ctest);

        settingsDecimalPlaces = qMin(25, settingsDecimalPlaces);
        settingsDecimalPlaces = qMax(0, settingsDecimalPlaces);

        actualDecimalPlaces = qMax(settingsDecimalPlaces, incrementDecimalPlaces);
626 627 628 629
    }

    return actualDecimalPlaces;
}