FactMetaData.cc 12.7 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)
Don Gagne's avatar
Don Gagne committed
54 55 56 57
{

}

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

}

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

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

Don Gagne's avatar
Don Gagne committed
102 103 104
    return *this;
}

105
QVariant FactMetaData::rawDefaultValue(void) const
106 107
{
    if (_defaultValueAvailable) {
108
        return _rawDefaultValue;
109 110 111 112 113 114
    } else {
        qWarning() << "Attempt to access unavailable default value";
        return QVariant(0);
    }
}

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

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

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

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

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

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

254 255 256 257 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
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();
}

309 310 311 312 313 314 315 316 317
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;
318
    _setBuiltInTranslator();
319 320 321 322 323 324 325
}

void FactMetaData::setTranslators(Translator rawTranslator, Translator cookedTranslator)
{
    _rawTranslator = rawTranslator;
    _cookedTranslator = cookedTranslator;
}
326

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
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);
            }
        }
    }
}

345 346 347 348 349
void FactMetaData::addEnumInfo(const QString& name, const QVariant& value)
{
    _enumStrings << name;
    _enumValues << value;
}
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

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)
{
368
    return QVariant((unsigned int)(degrees.toFloat() * 100.0f));
369 370 371 372 373 374 375 376 377
}

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

    _setBuiltInTranslator();
}