FactMetaData.cc 8.39 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 34
#include <limits>

35 36 37
FactMetaData::FactMetaData(QObject* parent)
    : QObject(parent)
    , _type(valueTypeInt32)
38
    , _decimalPlaces(defaultDecimalPlaces)
39 40
    , _defaultValue(0)
    , _defaultValueAvailable(false)
41
    , _group("*Default Group")
42 43
    , _max(_maxForType())
    , _maxIsDefaultForType(true)
44 45 46 47
    , _min(_minForType())
    , _minIsDefaultForType(true)
    , _rawTranslator(defaultTranslator)
    , _cookedTranslator(defaultTranslator)
Don Gagne's avatar
Don Gagne committed
48 49 50 51
{

}

52 53 54
FactMetaData::FactMetaData(ValueType_t type, QObject* parent)
    : QObject(parent)
    , _type(type)
55
    , _decimalPlaces(defaultDecimalPlaces)
56 57
    , _defaultValue(0)
    , _defaultValueAvailable(false)
58
    , _group("*Default Group")
59 60
    , _max(_maxForType())
    , _maxIsDefaultForType(true)
61 62 63 64
    , _min(_minForType())
    , _minIsDefaultForType(true)
    , _rawTranslator(defaultTranslator)
    , _cookedTranslator(defaultTranslator)
65 66 67 68
{

}

Don Gagne's avatar
Don Gagne committed
69 70 71 72 73 74 75 76
FactMetaData::FactMetaData(const FactMetaData& other, QObject* parent)
    : QObject(parent)
{
    *this = other;
}

const FactMetaData& FactMetaData::operator=(const FactMetaData& other)
{
77
    _decimalPlaces          = other._decimalPlaces;
Don Gagne's avatar
Don Gagne committed
78 79
    _defaultValue           = other._defaultValue;
    _defaultValueAvailable  = other._defaultValueAvailable;
80 81 82 83
    _enumStrings            = other._enumStrings;
    _enumValues             = other._enumValues;
    _group                  = other._group;
    _longDescription        = other._longDescription;
Don Gagne's avatar
Don Gagne committed
84 85
    _max                    = other._max;
    _maxIsDefaultForType    = other._maxIsDefaultForType;
86 87 88 89 90 91 92 93 94
    _min                    = other._min;
    _minIsDefaultForType    = other._minIsDefaultForType;
    _name                   = other._name;
    _shortDescription       = other._shortDescription;
    _type                   = other._type;
    _units                  = other._units;
    _rawTranslator          = other._rawTranslator;
    _cookedTranslator       = other._cookedTranslator;

Don Gagne's avatar
Don Gagne committed
95 96 97
    return *this;
}

98
QVariant FactMetaData::defaultValue(void) const
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
{
    if (_defaultValueAvailable) {
        return _defaultValue;
    } else {
        qWarning() << "Attempt to access unavailable default value";
        return QVariant(0);
    }
}

void FactMetaData::setDefaultValue(const QVariant& defaultValue)
{
    if (_min <= defaultValue && defaultValue <= _max) {
        _defaultValue = defaultValue;
        _defaultValueAvailable = true;
    } else {
        qWarning() << "Attempt to set default value which is outside min/max range";
    }
}

void FactMetaData::setMin(const QVariant& min)
{
    if (min > _minForType()) {
        _min = min;
Don Gagne's avatar
Don Gagne committed
122
        _minIsDefaultForType = false;
123 124 125 126 127 128 129 130 131 132 133 134 135
    } else {
        qWarning() << "Attempt to set min below allowable value";
        _min = _minForType();
    }
}

void FactMetaData::setMax(const QVariant& max)
{
    if (max > _maxForType()) {
        qWarning() << "Attempt to set max above allowable value";
        _max = _maxForType();
    } else {
        _max = max;
Don Gagne's avatar
Don Gagne committed
136
        _maxIsDefaultForType = false;
137 138 139
    }
}

140
QVariant FactMetaData::_minForType(void) const
Don Gagne's avatar
Don Gagne committed
141
{
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    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
160 161 162
    
    // Make windows compiler happy, even switch is full cased
    return QVariant();
Don Gagne's avatar
Don Gagne committed
163
}
Don Gagne's avatar
Don Gagne committed
164

165
QVariant FactMetaData::_maxForType(void) const
Don Gagne's avatar
Don Gagne committed
166
{
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    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
185 186 187
    
    // Make windows compiler happy, even switch is full cased
    return QVariant();
Don Gagne's avatar
Don Gagne committed
188
}
Don Gagne's avatar
Don Gagne committed
189 190 191 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

bool FactMetaData::convertAndValidate(const QVariant& value, bool convertOnly, QVariant& typedValue, QString& errorString)
{
    bool convertOk;
    
    errorString.clear();
    
    switch (type()) {
        case FactMetaData::valueTypeInt8:
        case FactMetaData::valueTypeInt16:
        case FactMetaData::valueTypeInt32:
            typedValue = QVariant(value.toInt(&convertOk));
            if (!convertOnly && convertOk) {
                if (min() > typedValue || typedValue > max()) {
                    errorString = QString("Value must be within %1 and %2").arg(min().toInt()).arg(max().toInt());
                }
            }
            break;
            
        case FactMetaData::valueTypeUint8:
        case FactMetaData::valueTypeUint16:
        case FactMetaData::valueTypeUint32:
            typedValue = QVariant(value.toUInt(&convertOk));
            if (!convertOnly && convertOk) {
                if (min() > typedValue || typedValue > max()) {
                    errorString = QString("Value must be within %1 and %2").arg(min().toUInt()).arg(max().toUInt());
                }
            }
            break;
            
        case FactMetaData::valueTypeFloat:
            typedValue = QVariant(value.toFloat(&convertOk));
            if (!convertOnly && convertOk) {
                if (min() > typedValue || typedValue > max()) {
                    errorString = QString("Value must be within %1 and %2").arg(min().toFloat()).arg(max().toFloat());
                }
            }
            break;
            
        case FactMetaData::valueTypeDouble:
            typedValue = QVariant(value.toDouble(&convertOk));
            if (!convertOnly && convertOk) {
                if (min() > typedValue || typedValue > max()) {
                    errorString = QString("Value must be within %1 and %2").arg(min().toDouble()).arg(max().toDouble());
                }
            }
            break;
    }
    
    if (!convertOk) {
        errorString = "Invalid number";
    }
    
    return convertOk && errorString.isEmpty();
}
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

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

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