QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCMapCircle.cc
Go to the documentation of this file.
1#include "QGCMapCircle.h"
2#include "GeoJsonHelper.h"
3#include "JsonParsing.h"
4#include "ParameterManager.h"
5
7 : QObject (parent)
8 , _dirty (false)
9 , _interactive (false)
10 , _showRotation (false)
11 , _clockwiseRotation(true)
12{
13 _init();
14}
15
16QGCMapCircle::QGCMapCircle(const QGeoCoordinate& center, double radius, bool showRotation, bool clockwiseRotation, QObject* parent)
17 : QObject (parent)
18 , _dirty (false)
19 , _center (center)
20 , _radius (ParameterManager::defaultComponentId, _radiusFactName, FactMetaData::valueTypeDouble)
21 , _interactive (false)
22 , _showRotation (showRotation)
23 , _clockwiseRotation(clockwiseRotation)
24{
25 _radius.setRawValue(radius);
26 _init();
27}
28
29QGCMapCircle::QGCMapCircle(const QGCMapCircle& other, QObject* parent)
30 : QObject (parent)
31 , _dirty (false)
32 , _center (other._center)
33 , _radius (ParameterManager::defaultComponentId, _radiusFactName, FactMetaData::valueTypeDouble)
34 , _interactive (false)
35 , _showRotation (other._showRotation)
36 , _clockwiseRotation(other._clockwiseRotation)
37{
38 _radius.setRawValue(other._radius.rawValue());
39 _init();
40}
41
43{
44 setCenter(other._center);
45 _radius.setRawValue(other._radius.rawValue());
46 setDirty(true);
47
48 return *this;
49}
50
51void QGCMapCircle::_init(void)
52{
53 _nameToMetaDataMap = FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/QGCMapCircle.Facts.json"), this);
54 _radius.setMetaData(_nameToMetaDataMap[_radiusFactName]);
55
56 connect(this, &QGCMapCircle::centerChanged, this, &QGCMapCircle::_setDirty);
57 connect(&_radius, &Fact::rawValueChanged, this, &QGCMapCircle::_setDirty);
58}
59
60void QGCMapCircle::setDirty(bool dirty)
61{
62 if (_dirty != dirty) {
63 _dirty = dirty;
64 emit dirtyChanged(dirty);
65 }
66}
67
68void QGCMapCircle::saveToJson(QJsonObject& json)
69{
70 QJsonValue jsonValue;
71 QJsonObject circleObject;
72
73 GeoJsonHelper::saveGeoCoordinate(_center, false /* writeAltitude*/, jsonValue);
74 circleObject.insert(_jsonCenterKey, jsonValue);
75 circleObject.insert(_jsonRadiusKey, _radius.rawValue().toDouble());
76
77 json.insert(jsonCircleKey, circleObject);
78}
79
80bool QGCMapCircle::loadFromJson(const QJsonObject& json, QString& errorString)
81{
82 errorString.clear();
83
84 QList<JsonParsing::KeyValidateInfo> circleKeyInfo = {
85 { jsonCircleKey, QJsonValue::Object, true },
86 };
87 if (!JsonParsing::validateKeys(json, circleKeyInfo, errorString)) {
88 return false;
89 }
90
91 QJsonObject circleObject = json[jsonCircleKey].toObject();
92
93 QList<JsonParsing::KeyValidateInfo> circleObjectKeyInfo = {
94 { _jsonCenterKey, QJsonValue::Array, true },
95 { _jsonRadiusKey, QJsonValue::Double, true },
96 };
97 if (!JsonParsing::validateKeys(circleObject, circleObjectKeyInfo, errorString)) {
98 return false;
99 }
100
101 QGeoCoordinate center;
102 if (!GeoJsonHelper::loadGeoCoordinate(circleObject[_jsonCenterKey], false /* altitudeRequired */, center, errorString)) {
103 return false;
104 }
106 _radius.setRawValue(circleObject[_jsonRadiusKey].toDouble());
107
108 _interactive = false;
109 _showRotation = false;
110 _clockwiseRotation = true;
111
112 return true;
113}
114
115void QGCMapCircle::setCenter(QGeoCoordinate newCenter)
116{
117 if (newCenter != _center) {
118 _center = newCenter;
119 setDirty(true);
120 emit centerChanged(newCenter);
121 }
122}
123
124void QGCMapCircle::_setDirty(void)
125{
126 setDirty(true);
127}
128
129void QGCMapCircle::setInteractive(bool interactive)
130{
131 if (_interactive != interactive) {
132 _interactive = interactive;
134 }
135}
136
137void QGCMapCircle::setShowRotation(bool showRotation)
138{
139 if (showRotation != _showRotation) {
140 _showRotation = showRotation;
142 }
143}
144
145void QGCMapCircle::setClockwiseRotation(bool clockwiseRotation)
146{
147 if (clockwiseRotation != _clockwiseRotation) {
148 _clockwiseRotation = clockwiseRotation;
150 }
151}
QString errorString
Holds the meta data associated with a Fact.
static QMap< QString, FactMetaData * > createMapFromJsonFile(const QString &jsonFilename, QObject *metaDataParent)
void setMetaData(FactMetaData *metaData, bool setDefaultFromMetaData=false)
Definition Fact.cc:674
void rawValueChanged(const QVariant &value)
void setRawValue(const QVariant &value)
Definition Fact.cc:128
QVariant rawValue() const
Value after translation.
Definition Fact.h:85
The QGCMapCircle represents a circular area which can be displayed on a Map control.
void dirtyChanged(bool dirty)
void setInteractive(bool interactive)
void interactiveChanged(bool interactive)
bool clockwiseRotation(void) const
void setClockwiseRotation(bool clockwiseRotation)
QGCMapCircle(QObject *parent=nullptr)
static constexpr const char * jsonCircleKey
void clockwiseRotationChanged(bool clockwiseRotation)
bool interactive(void) const
void saveToJson(QJsonObject &json)
void setShowRotation(bool showRotation)
void centerChanged(QGeoCoordinate center)
bool showRotation(void) const
void showRotationChanged(bool showRotation)
void setDirty(bool dirty)
Fact * radius(void)
void setCenter(QGeoCoordinate newCenter)
const QGCMapCircle & operator=(const QGCMapCircle &other)
QGeoCoordinate center(void) const
bool loadFromJson(const QJsonObject &json, QString &errorString)
bool dirty(void) const
bool loadGeoCoordinate(const QJsonValue &jsonValue, bool altitudeRequired, QGeoCoordinate &coordinate, QString &errorString)
void saveGeoCoordinate(const QGeoCoordinate &coordinate, bool writeAltitude, QJsonValue &jsonValue)
Saves a QGeoCoordinate as [lat, lon, alt] array (QGC plan format).
bool validateKeys(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
Validates that all required keys are present and that listed keys have the expected type.