CameraSpec.cc 4.76 KB
Newer Older
1 2
/****************************************************************************
 *
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "CameraSpec.h"
#include "JsonHelper.h"

#include <QQmlEngine>

const char* CameraSpec::_sensorWidthName =          "SensorWidth";
const char* CameraSpec::_sensorHeightName =         "SensorHeight";
const char* CameraSpec::_imageWidthName =           "ImageWidth";
const char* CameraSpec::_imageHeightName =          "ImageHeight";
const char* CameraSpec::_focalLengthName =          "FocalLength";
const char* CameraSpec::_landscapeName =            "Landscape";
const char* CameraSpec::_fixedOrientationName =     "FixedOrientation";
const char* CameraSpec::_minTriggerIntervalName =   "MinTriggerInterval";

Don Gagne's avatar
Don Gagne committed
24
CameraSpec::CameraSpec(const QString& settingsGroup, QObject* parent)
25 26 27
    : QObject                   (parent)
    , _dirty                    (false)
    , _metaDataMap              (FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/CameraSpec.FactMetaData.json"), this))
Don Gagne's avatar
Don Gagne committed
28 29 30 31 32 33 34 35
    , _sensorWidthFact          (settingsGroup, _metaDataMap[_sensorWidthName])
    , _sensorHeightFact         (settingsGroup, _metaDataMap[_sensorHeightName])
    , _imageWidthFact           (settingsGroup, _metaDataMap[_imageWidthName])
    , _imageHeightFact          (settingsGroup, _metaDataMap[_imageHeightName])
    , _focalLengthFact          (settingsGroup, _metaDataMap[_focalLengthName])
    , _landscapeFact            (settingsGroup, _metaDataMap[_landscapeName])
    , _fixedOrientationFact     (settingsGroup, _metaDataMap[_fixedOrientationName])
    , _minTriggerIntervalFact   (settingsGroup, _metaDataMap[_minTriggerIntervalName])
36
{
Don Gagne's avatar
Don Gagne committed
37
    QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
}

const CameraSpec& CameraSpec::operator=(const CameraSpec& other)
{
    _sensorWidthFact.setRawValue        (other._sensorWidthFact.rawValue());
    _sensorHeightFact.setRawValue       (other._sensorHeightFact.rawValue());
    _imageWidthFact.setRawValue         (other._imageWidthFact.rawValue());
    _imageHeightFact.setRawValue        (other._imageHeightFact.rawValue());
    _focalLengthFact.setRawValue        (other._focalLengthFact.rawValue());
    _landscapeFact.setRawValue          (other._landscapeFact.rawValue());
    _fixedOrientationFact.setRawValue   (other._fixedOrientationFact.rawValue());
    _minTriggerIntervalFact.setRawValue (other._minTriggerIntervalFact.rawValue());

    return *this;
}

void CameraSpec::setDirty(bool dirty)
{
    if (_dirty != dirty) {
        _dirty = dirty;
        emit dirtyChanged(_dirty);
    }
}

void CameraSpec::save(QJsonObject& json) const
{
64 65 66 67 68 69 70 71
    json[_sensorWidthName] =        _sensorWidthFact.rawValue().toDouble();
    json[_sensorHeightName] =       _sensorHeightFact.rawValue().toDouble();
    json[_imageWidthName] =         _imageWidthFact.rawValue().toDouble();
    json[_imageHeightName] =        _imageHeightFact.rawValue().toDouble();
    json[_focalLengthName] =        _focalLengthFact.rawValue().toDouble();
    json[_landscapeName] =          _landscapeFact.rawValue().toBool();
    json[_fixedOrientationName] =   _fixedOrientationFact.rawValue().toBool();
    json[_minTriggerIntervalName] = _minTriggerIntervalFact.rawValue().toDouble();
72 73
}

74
bool CameraSpec::load(const QJsonObject& json, QString& errorString)
75
{
76 77 78 79 80 81 82 83 84
    QList<JsonHelper::KeyValidateInfo> keyInfoList = {
        { _sensorWidthName,         QJsonValue::Double, true },
        { _sensorHeightName,        QJsonValue::Double, true },
        { _imageWidthName,          QJsonValue::Double, true },
        { _imageHeightName,         QJsonValue::Double, true },
        { _focalLengthName,         QJsonValue::Double, true },
        { _landscapeName,           QJsonValue::Bool,   true },
        { _fixedOrientationName,    QJsonValue::Bool,   true },
        { _minTriggerIntervalName,  QJsonValue::Double, true },
85
    };
86
    if (!JsonHelper::validateKeys(json, keyInfoList, errorString)) {
87 88 89
        return false;
    }

90 91
    _sensorWidthFact.setRawValue        (json[_sensorWidthName].toDouble());
    _sensorHeightFact.setRawValue       (json[_sensorHeightName].toDouble());
92 93
    _imageWidthFact.setRawValue         (json[_imageWidthName].toInt());
    _imageHeightFact.setRawValue        (json[_imageHeightName].toInt());
94 95 96 97
    _focalLengthFact.setRawValue        (json[_focalLengthName].toDouble());
    _landscapeFact.setRawValue          (json[_landscapeName].toBool());
    _fixedOrientationFact.setRawValue   (json[_fixedOrientationName].toBool());
    _minTriggerIntervalFact.setRawValue (json[_minTriggerIntervalName].toDouble());
98 99 100

    return true;
}