FactMetaData.cc 14.5 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 28 29 30
/*=====================================================================
 
 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
///     @brief Object which exposes a FactMetaData
///
///     @author Don Gagne <don@thegagnes.com>

#include "FactMetaData.h"

31 32
#include <QDebug>

Don Gagne's avatar
Don Gagne committed
33
#include <limits>
34 35 36 37 38 39
#include <cmath>

const FactMetaData::BuiltInTranslation_s FactMetaData::_rgBuildInTranslations[] = {
    { "centi-degrees",  "degrees",  FactMetaData::_centiDegreesToDegrees,   FactMetaData::_degreesToCentiDegrees },
    { "radians",        "degrees",  FactMetaData::_radiansToDegrees,        FactMetaData::_degreesToRadians },
};
Don Gagne's avatar
Don Gagne committed
40

41 42 43
FactMetaData::FactMetaData(QObject* parent)
    : QObject(parent)
    , _type(valueTypeInt32)
44
    , _decimalPlaces(defaultDecimalPlaces)
45
    , _rawDefaultValue(0)
46
    , _defaultValueAvailable(false)
47
    , _group("*Default Group")
48
    , _rawMax(_maxForType())
49
    , _maxIsDefaultForType(true)
50
    , _rawMin(_minForType())
51
    , _minIsDefaultForType(true)
52 53
    , _rawTranslator(_defaultTranslator)
    , _cookedTranslator(_defaultTranslator)
54
    , _rebootRequired(false)
Don Gagne's avatar
Don Gagne committed
55 56 57 58
{

}

59 60 61
FactMetaData::FactMetaData(ValueType_t type, QObject* parent)
    : QObject(parent)
    , _type(type)
62
    , _decimalPlaces(defaultDecimalPlaces)
63
    , _rawDefaultValue(0)
64
    , _defaultValueAvailable(false)
65
    , _group("*Default Group")
66
    , _rawMax(_maxForType())
67
    , _maxIsDefaultForType(true)
68
    , _rawMin(_minForType())
69
    , _minIsDefaultForType(true)
70 71
    , _rawTranslator(_defaultTranslator)
    , _cookedTranslator(_defaultTranslator)
72
    , _rebootRequired(false)
73 74 75 76
{

}

Don Gagne's avatar
Don Gagne committed
77 78 79 80 81 82 83 84
FactMetaData::FactMetaData(const FactMetaData& other, QObject* parent)
    : QObject(parent)
{
    *this = other;
}

const FactMetaData& FactMetaData::operator=(const FactMetaData& other)
{
85
    _decimalPlaces          = other._decimalPlaces;
86
    _rawDefaultValue        = other._rawDefaultValue;
Don Gagne's avatar
Don Gagne committed
87
    _defaultValueAvailable  = other._defaultValueAvailable;
88 89
    _bitmaskStrings         = other._bitmaskStrings;
    _bitmaskValues          = other._bitmaskValues;
90 91 92 93
    _enumStrings            = other._enumStrings;
    _enumValues             = other._enumValues;
    _group                  = other._group;
    _longDescription        = other._longDescription;
94
    _rawMax                 = other._rawMax;
Don Gagne's avatar
Don Gagne committed
95
    _maxIsDefaultForType    = other._maxIsDefaultForType;
96
    _rawMin                 = other._rawMin;
97 98 99 100
    _minIsDefaultForType    = other._minIsDefaultForType;
    _name                   = other._name;
    _shortDescription       = other._shortDescription;
    _type                   = other._type;
101 102
    _rawUnits               = other._rawUnits;
    _cookedUnits            = other._cookedUnits;
103 104
    _rawTranslator          = other._rawTranslator;
    _cookedTranslator       = other._cookedTranslator;
105
    _rebootRequired         = other._rebootRequired;
106

Don Gagne's avatar
Don Gagne committed
107 108 109
    return *this;
}

110
QVariant FactMetaData::rawDefaultValue(void) const
111 112
{
    if (_defaultValueAvailable) {
113
        return _rawDefaultValue;
114 115 116 117 118 119
    } else {
        qWarning() << "Attempt to access unavailable default value";
        return QVariant(0);
    }
}

120
void FactMetaData::setRawDefaultValue(const QVariant& rawDefaultValue)
121
{
122 123
    if (_rawMin <= rawDefaultValue && rawDefaultValue <= _rawMax) {
        _rawDefaultValue = rawDefaultValue;
124 125 126 127 128 129
        _defaultValueAvailable = true;
    } else {
        qWarning() << "Attempt to set default value which is outside min/max range";
    }
}

130
void FactMetaData::setRawMin(const QVariant& rawMin)
131
{
132 133
    if (rawMin > _minForType()) {
        _rawMin = rawMin;
Don Gagne's avatar
Don Gagne committed
134
        _minIsDefaultForType = false;
135
    } else {
136
        qWarning() << "Attempt to set min below allowable value for fact: " << name()
137
                   << ", value attempted: " << rawMin
138
                   << ", type: " << type() << ", min for type: " << _minForType();
139
        _rawMin = _minForType();
140 141 142
    }
}

143
void FactMetaData::setRawMax(const QVariant& rawMax)
144
{
145
    if (rawMax > _maxForType()) {
146
        qWarning() << "Attempt to set max above allowable value";
147
        _rawMax = _maxForType();
148
    } else {
149
        _rawMax = rawMax;
Don Gagne's avatar
Don Gagne committed
150
        _maxIsDefaultForType = false;
151 152 153
    }
}

154
QVariant FactMetaData::_minForType(void) const
Don Gagne's avatar
Don Gagne committed
155
{
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    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
174 175 176
    
    // Make windows compiler happy, even switch is full cased
    return QVariant();
Don Gagne's avatar
Don Gagne committed
177
}
Don Gagne's avatar
Don Gagne committed
178

179
QVariant FactMetaData::_maxForType(void) const
Don Gagne's avatar
Don Gagne committed
180
{
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    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
199 200 201
    
    // Make windows compiler happy, even switch is full cased
    return QVariant();
Don Gagne's avatar
Don Gagne committed
202
}
Don Gagne's avatar
Don Gagne committed
203

204
bool FactMetaData::convertAndValidateRaw(const QVariant& rawValue, bool convertOnly, QVariant& typedValue, QString& errorString)
Don Gagne's avatar
Don Gagne committed
205
{
206
    bool convertOk = false;
Don Gagne's avatar
Don Gagne committed
207 208 209 210 211 212 213
    
    errorString.clear();
    
    switch (type()) {
        case FactMetaData::valueTypeInt8:
        case FactMetaData::valueTypeInt16:
        case FactMetaData::valueTypeInt32:
214
            typedValue = QVariant(rawValue.toInt(&convertOk));
Don Gagne's avatar
Don Gagne committed
215
            if (!convertOnly && convertOk) {
216 217
                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
218 219 220 221 222 223 224
                }
            }
            break;
            
        case FactMetaData::valueTypeUint8:
        case FactMetaData::valueTypeUint16:
        case FactMetaData::valueTypeUint32:
225
            typedValue = QVariant(rawValue.toUInt(&convertOk));
Don Gagne's avatar
Don Gagne committed
226
            if (!convertOnly && convertOk) {
227 228
                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
229 230 231 232 233
                }
            }
            break;
            
        case FactMetaData::valueTypeFloat:
234
            typedValue = QVariant(rawValue.toFloat(&convertOk));
Don Gagne's avatar
Don Gagne committed
235
            if (!convertOnly && convertOk) {
236 237
                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
238 239 240 241 242
                }
            }
            break;
            
        case FactMetaData::valueTypeDouble:
243
            typedValue = QVariant(rawValue.toDouble(&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().toDouble()).arg(cookedMax().toDouble());
Don Gagne's avatar
Don Gagne committed
247 248 249 250 251 252
                }
            }
            break;
    }
    
    if (!convertOk) {
253
        errorString += "Invalid number";
Don Gagne's avatar
Don Gagne committed
254 255 256 257
    }
    
    return convertOk && errorString.isEmpty();
}
258

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 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
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();
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
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;
    _setBuiltInTranslator();
}

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

332 333 334 335 336 337 338 339 340
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;
341
    _setBuiltInTranslator();
342 343
}

344 345 346 347 348 349
void FactMetaData::addEnumInfo(const QString& name, const QVariant& value)
{
    _enumStrings << name;
    _enumValues << value;
}

350 351 352 353 354
void FactMetaData::setTranslators(Translator rawTranslator, Translator cookedTranslator)
{
    _rawTranslator = rawTranslator;
    _cookedTranslator = cookedTranslator;
}
355

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
void FactMetaData::_setBuiltInTranslator(void)
{
    if (_enumStrings.count()) {
        // No translation if enum
        setTranslators(_defaultTranslator, _defaultTranslator);
        _cookedUnits = _rawUnits;
    } else {
        for (size_t i=0; i<sizeof(_rgBuildInTranslations)/sizeof(_rgBuildInTranslations[0]); i++) {
            const BuiltInTranslation_s* pBuiltInTranslation = &_rgBuildInTranslations[i];

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

QVariant FactMetaData::_degreesToRadians(const QVariant& degrees)
{
    return QVariant(degrees.toDouble() * (M_PI / 180.0));
}

QVariant FactMetaData::_radiansToDegrees(const QVariant& radians)
{
    return QVariant(radians.toDouble() * (180 / M_PI));
}

QVariant FactMetaData::_centiDegreesToDegrees(const QVariant& centiDegrees)
{
    return QVariant(centiDegrees.toFloat() / 100.0f);
}

QVariant FactMetaData::_degreesToCentiDegrees(const QVariant& degrees)
{
391
    return QVariant((unsigned int)(degrees.toFloat() * 100.0f));
392 393 394 395 396 397 398 399 400
}

void FactMetaData::setRawUnits(const QString& rawUnits)
{
    _rawUnits = rawUnits;
    _cookedUnits = rawUnits;

    _setBuiltInTranslator();
}
Don Gagne's avatar
Don Gagne committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436

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;
}