Fact.cc 9.32 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
///     @author Don Gagne <don@thegagnes.com>

#include "Fact.h"

#include <QtQml>

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

Don Gagne's avatar
Don Gagne committed
42 43 44 45
Fact::Fact(int componentId, QString name, FactMetaData::ValueType_t type, QObject* parent)
    : QObject(parent)
    , _name(name)
    , _componentId(componentId)
46
    , _rawValue(0)
Don Gagne's avatar
Don Gagne committed
47 48
    , _type(type)
    , _metaData(NULL)
Don Gagne's avatar
Don Gagne committed
49
{
Don Gagne's avatar
Don Gagne committed
50 51 52
    FactMetaData* metaData = new FactMetaData(_type, this);
    setMetaData(metaData);
}
Don Gagne's avatar
Don Gagne committed
53

Don Gagne's avatar
Don Gagne committed
54 55 56 57 58 59 60 61 62 63
Fact::Fact(const Fact& other, QObject* parent)
    : QObject(parent)
{
    *this = other;
}

const Fact& Fact::operator=(const Fact& other)
{
    _name           = other._name;
    _componentId    = other._componentId;
64
    _rawValue          = other._rawValue;
Don Gagne's avatar
Don Gagne committed
65 66 67 68 69 70 71 72 73
    _type           = other._type;
    
    if (_metaData && other._metaData) {
        *_metaData = *other._metaData;
    } else {
        _metaData = NULL;
    }
    
    return *this;
Don Gagne's avatar
Don Gagne committed
74 75
}

76 77 78 79 80 81 82
void Fact::forceSetValue(const QVariant& value)
{
    if (_metaData) {
        QVariant    typedValue;
        QString     errorString;
        
        if (_metaData->convertAndValidate(value, true /* convertOnly */, typedValue, errorString)) {
83 84 85
            _rawValue.setValue(_metaData->cookedTranslator()(typedValue));
            emit valueChanged(typedValue);
            emit _containerValueChanged(typedValue);
86 87 88 89 90 91
        }
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

92
void Fact::setRawValue(const QVariant& value)
Don Gagne's avatar
Don Gagne committed
93
{
Don Gagne's avatar
Don Gagne committed
94 95 96 97 98
    if (_metaData) {
        QVariant    typedValue;
        QString     errorString;
        
        if (_metaData->convertAndValidate(value, true /* convertOnly */, typedValue, errorString)) {
99 100 101 102
            if (typedValue != _rawValue) {
                _rawValue.setValue(typedValue);
                emit valueChanged(this->value());
                emit _containerValueChanged(this->value());
Don Gagne's avatar
Don Gagne committed
103 104 105 106
            }
        }
    } else {
        qWarning() << "Meta data pointer missing";
107
    }
Don Gagne's avatar
Don Gagne committed
108 109
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
void Fact::setValue(const QVariant& value)
{
    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) {
            setValue(_metaData->enumValues()[index]);
        }
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

void Fact::setEnumIndex(int index)
{
    if (_metaData) {
        setValue(_metaData->enumValues()[index]);
    } else {
        qWarning() << "Meta data pointer missing";
    }
}

140
void Fact::_containerSetValue(const QVariant& value)
Don Gagne's avatar
Don Gagne committed
141
{
142 143 144
    _rawValue = value;
    emit valueChanged(_rawValue);
    emit vehicleUpdated(_rawValue);
Don Gagne's avatar
Don Gagne committed
145
}
146 147 148 149 150 151

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

152 153 154 155 156
int Fact::componentId(void) const
{
    return _componentId;
}

157 158
QVariant Fact::value(void) const
{
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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
    if (_metaData) {
        return _metaData->rawTranslator()(_rawValue);
    } else {
        qWarning() << "Meta data pointer missing";
        return _rawValue;
    }
}

QString Fact::enumStringValue(void) const
{
    if (_metaData) {
        int enumIndex = this->enumIndex();
        if (enumIndex > 0 && enumIndex < _metaData->enumStrings().count()) {
            return _metaData->enumStrings()[enumIndex];
        }
    } else {
        qWarning() << "Meta data pointer missing";
    }

    return QString();
}

int Fact::enumIndex(void) const
{
    if (_metaData) {
        int index = 0;
        foreach (QVariant enumValue, _metaData->enumValues()) {
            if (enumValue == value()) {
                return index;
            }
            index ++;
        }
    } 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();
    }
216 217
}

218
QString Fact::_variantToString(const QVariant& variant) const
219
{
220 221 222 223
    QString valueString;

    switch (type()) {
        case FactMetaData::valueTypeFloat:
224
            valueString = QString("%1").arg(variant.toFloat(), 0, 'f', decimalPlaces());
225 226
            break;
        case FactMetaData::valueTypeDouble:
227
            valueString = QString("%1").arg(variant.toDouble(), 0, 'f', decimalPlaces());
228 229
            break;
        default:
230
            valueString = variant.toString();
231 232 233 234
            break;
    }

    return valueString;
235 236
}

237 238 239 240 241
QString Fact::valueString(void) const
{
    return _variantToString(value());
}

242
QVariant Fact::defaultValue(void) const
243
{
Don Gagne's avatar
Don Gagne committed
244 245 246 247 248 249 250 251
    if (_metaData) {
        if (!_metaData->defaultValueAvailable()) {
            qDebug() << "Access to unavailable default value";
        }
        return _metaData->defaultValue();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
252
    }
253 254
}

255 256 257 258 259
QString Fact::defaultValueString(void) const
{
    return _variantToString(defaultValue());
}

260
FactMetaData::ValueType_t Fact::type(void) const
261
{
262
    return _type;
263 264
}

265
QString Fact::shortDescription(void) const
266
{
Don Gagne's avatar
Don Gagne committed
267 268 269 270 271 272
    if (_metaData) {
        return _metaData->shortDescription();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
273 274
}

275
QString Fact::longDescription(void) const
276
{
Don Gagne's avatar
Don Gagne committed
277 278 279 280 281 282
    if (_metaData) {
        return _metaData->longDescription();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
283 284
}

285
QString Fact::units(void) const
286
{
Don Gagne's avatar
Don Gagne committed
287 288 289 290 291 292
    if (_metaData) {
        return _metaData->units();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
293 294
}

295
QVariant Fact::min(void) const
296
{
Don Gagne's avatar
Don Gagne committed
297 298 299 300 301 302
    if (_metaData) {
        return _metaData->min();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
303 304
}

305 306 307 308 309
QString Fact::minString(void) const
{
    return _variantToString(min());
}

310
QVariant Fact::max(void) const
311
{
Don Gagne's avatar
Don Gagne committed
312 313 314 315 316 317 318 319
    if (_metaData) {
        return _metaData->max();
    } else {
        qWarning() << "Meta data pointer missing";
        return QVariant(0);
    }
}

320 321 322 323 324
QString Fact::maxString(void) const
{
    return _variantToString(max());
}

325
bool Fact::minIsDefaultForType(void) const
Don Gagne's avatar
Don Gagne committed
326 327 328 329 330 331 332 333 334
{
    if (_metaData) {
        return _metaData->minIsDefaultForType();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
}

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

345 346 347 348 349 350 351 352 353 354
int Fact::decimalPlaces(void) const
{
    if (_metaData) {
        return _metaData->decimalPlaces();
    } else {
        qWarning() << "Meta data pointer missing";
        return FactMetaData::defaultDecimalPlaces;
    }
}

355
QString Fact::group(void) const
356
{
Don Gagne's avatar
Don Gagne committed
357 358 359 360 361 362
    if (_metaData) {
        return _metaData->group();
    } else {
        qWarning() << "Meta data pointer missing";
        return QString();
    }
363 364
}

365 366 367
void Fact::setMetaData(FactMetaData* metaData)
{
    _metaData = metaData;
368
    emit valueChanged(value());
369
}
370

371
bool Fact::valueEqualsDefault(void) const
372
{
Don Gagne's avatar
Don Gagne committed
373 374 375 376 377 378
    if (_metaData) {
        if (_metaData->defaultValueAvailable()) {
            return _metaData->defaultValue() == value();
        } else {
            return false;
        }
379
    } else {
Don Gagne's avatar
Don Gagne committed
380
        qWarning() << "Meta data pointer missing";
381 382 383 384
        return false;
    }
}

385
bool Fact::defaultValueAvailable(void) const
386
{
Don Gagne's avatar
Don Gagne committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    if (_metaData) {
        return _metaData->defaultValueAvailable();
    } else {
        qWarning() << "Meta data pointer missing";
        return false;
    }
}

QString Fact::validate(const QString& value, bool convertOnly)
{
    if (_metaData) {
        
        QVariant    typedValue;
        QString     errorString;
        
        _metaData->convertAndValidate(value, convertOnly, typedValue, errorString);
        
        return errorString;
    } else {
        qWarning() << "Meta data pointer missing";
        return QString("Internal error: Meta data pointer missing");
    }
}