QGCMapCircle.cc 4.01 KB
Newer Older
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 31 32 33 34 35 36 37 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "QGCMapCircle.h"
#include "QGCGeo.h"
#include "JsonHelper.h"
#include "QGCQGeoCoordinate.h"

#include <QGeoRectangle>
#include <QDebug>
#include <QJsonArray>

const char* QGCMapCircle::jsonCircleKey =   "circle";
const char* QGCMapCircle::_jsonCenterKey =  "center";
const char* QGCMapCircle::_jsonRadiusKey =  "radius";
const char* QGCMapCircle::_radiusFactName = "Radius";

QGCMapCircle::QGCMapCircle(QObject* parent)
    : QObject       (parent)
    , _dirty        (false)
    , _interactive  (false)
{
    _init();
}

QGCMapCircle::QGCMapCircle(const QGeoCoordinate& center, double radius, QObject* parent)
    : QObject       (parent)
    , _dirty        (false)
    , _center       (center)
    , _radius       (FactSystem::defaultComponentId, _radiusFactName, FactMetaData::valueTypeDouble)
    , _interactive  (false)
{
    _radius.setRawValue(radius);
    _init();
}

QGCMapCircle::QGCMapCircle(const QGCMapCircle& other, QObject* parent)
    : QObject       (parent)
    , _dirty        (false)
    , _center       (other._center)
    , _radius       (FactSystem::defaultComponentId, _radiusFactName, FactMetaData::valueTypeDouble)
    , _interactive  (false)
{
    _radius.setRawValue(other._radius.rawValue());
    _init();
}

const QGCMapCircle& QGCMapCircle::operator=(const QGCMapCircle& other)
{
    setCenter(other._center);
    _radius.setRawValue(other._radius.rawValue());
    setDirty(true);

    return *this;
}

void QGCMapCircle::_init(void)
{
    _nameToMetaDataMap = FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/QGCMapCircle.Facts.json"), this);
    _radius.setMetaData(_nameToMetaDataMap[_radiusFactName]);

    connect(this,       &QGCMapCircle::centerChanged,   this, &QGCMapCircle::_setDirty);
    connect(&_radius,   &Fact::rawValueChanged,         this, &QGCMapCircle::_setDirty);
}

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

void QGCMapCircle::saveToJson(QJsonObject& json)
{
    QJsonValue jsonValue;
    QJsonObject circleObject;

    JsonHelper::saveGeoCoordinate(_center, false /* writeAltitude*/, jsonValue);
    circleObject.insert(_jsonCenterKey, jsonValue);
    circleObject.insert(_jsonRadiusKey, _radius.rawValue().toDouble());

    json.insert(jsonCircleKey, circleObject);
}

bool QGCMapCircle::loadFromJson(const QJsonObject& json, QString& errorString)
{
    errorString.clear();

    QList<JsonHelper::KeyValidateInfo> circleKeyInfo = {
        { jsonCircleKey, QJsonValue::Object, true },
    };
    if (!JsonHelper::validateKeys(json, circleKeyInfo, errorString)) {
        return false;
    }

    QJsonObject circleObject = json[jsonCircleKey].toObject();

    QList<JsonHelper::KeyValidateInfo> circleObjectKeyInfo = {
        { _jsonCenterKey, QJsonValue::Array,    true },
        { _jsonRadiusKey, QJsonValue::Double,   true },
    };
    if (!JsonHelper::validateKeys(circleObject, circleObjectKeyInfo, errorString)) {
        return false;
    }

    QGeoCoordinate center;
114
    if (!JsonHelper::loadGeoCoordinate(circleObject[_jsonCenterKey], false /* altitudeRequired */, center, errorString)) {
115 116 117
        return false;
    }
    setCenter(center);
118
    _radius.setRawValue(circleObject[_jsonRadiusKey].toDouble());
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

    return true;
}

void QGCMapCircle::setCenter(QGeoCoordinate newCenter)
{
    if (newCenter != _center) {
        _center = newCenter;
        setDirty(true);
        emit centerChanged(newCenter);
    }
}

void QGCMapCircle::_setDirty(void)
{
    setDirty(true);
}

void QGCMapCircle::setInteractive(bool interactive)
{
    if (_interactive != interactive) {
        _interactive = interactive;
        emit interactiveChanged(interactive);
    }
}