QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCMapPolyline.cc
Go to the documentation of this file.
1#include "QGCMapPolyline.h"
2
3#include <QMetaMethod>
4
5#include "AppMessages.h"
6#include "GeoJsonHelper.h"
7#include "JsonParsing.h"
8#include "QGCApplication.h"
9#include "QGCGeo.h"
10#include "QGCLoggingCategory.h"
11#include "QGCPlanarGeometry.h"
12#include "QGCQGeoCoordinate.h"
13#include "ShapeFileHelper.h"
14
15QGC_LOGGING_CATEGORY(QGCMapPolylineLog, "QMLControls.QGCMapPolyline")
16
18 : QObject (parent)
19 , _dirty (false)
20 , _interactive (false)
21{
22 _init();
23}
24
25QGCMapPolyline::QGCMapPolyline(const QGCMapPolyline& other, QObject* parent)
26 : QObject (parent)
27 , _dirty (false)
28 , _interactive (false)
29{
30 *this = other;
31
32 _init();
33}
34
36{
37 qgcApp()->removeCompressedSignal(QMetaMethod::fromSignal(&QGCMapPolyline::pathChanged));
38}
39
41{
42 clear();
43
44 QVariantList vertices = other.path();
45 for (int i=0; i<vertices.count(); i++) {
46 appendVertex(vertices[i].value<QGeoCoordinate>());
47 }
48
49 setDirty(true);
50
51 return *this;
52}
53
54void QGCMapPolyline::_init(void)
55{
56 connect(&_polylineModel, &QmlObjectListModel::dirtyChanged, this, &QGCMapPolyline::_polylineModelDirtyChanged);
57 connect(&_polylineModel, &QmlObjectListModel::countChanged, this, &QGCMapPolyline::_polylineModelCountChanged);
58 connect(&_polylineModel, &QmlObjectListModel::modelReset, this, &QGCMapPolyline::pathChanged);
59
62
63 qgcApp()->addCompressedSignal(QMetaMethod::fromSignal(&QGCMapPolyline::pathChanged));
64}
65
67{
68 _polylinePath.clear();
69 emit pathChanged();
70
71 _polylineModel.clearAndDeleteContents();
72
73 emit cleared();
74
75 setDirty(true);
76}
77
78void QGCMapPolyline::adjustVertex(int vertexIndex, const QGeoCoordinate coordinate)
79{
80 _polylinePath[vertexIndex] = QVariant::fromValue(coordinate);
81 _polylineModel.value<QGCQGeoCoordinate*>(vertexIndex)->setCoordinate(coordinate);
82 if (!_deferredPathChanged) {
83 _deferredPathChanged = true;
84 if (_vertexDrag) {
85 // During vertex drag only emit the lightweight visual signal
86 QTimer::singleShot(0, this, [this]() {
87 emit dragPathChanged();
88 _deferredPathChanged = false;
89 });
90 } else {
91 QTimer::singleShot(0, this, [this]() {
92 emit pathChanged();
93 _deferredPathChanged = false;
94 });
95 }
96 }
97 setDirty(true);
98}
99
101{
102 if (_dirty != dirty) {
103 _dirty = dirty;
104 if (!dirty) {
105 _polylineModel.setDirty(false);
106 }
107 emit dirtyChanged(dirty);
108 }
109}
110QGeoCoordinate QGCMapPolyline::_coordFromPointF(const QPointF& point) const
111{
112 QGeoCoordinate coord;
113
114 if (_polylinePath.count() > 0) {
115 QGeoCoordinate tangentOrigin = _polylinePath[0].value<QGeoCoordinate>();
116 QGCGeo::convertNedToGeo(-point.y(), point.x(), 0, tangentOrigin, coord);
117 }
118
119 return coord;
120}
121
122QPointF QGCMapPolyline::_pointFFromCoord(const QGeoCoordinate& coordinate) const
123{
124 if (_polylinePath.count() > 0) {
125 double y, x, down;
126 QGeoCoordinate tangentOrigin = _polylinePath[0].value<QGeoCoordinate>();
127
128 QGCGeo::convertGeoToNed(coordinate, tangentOrigin, y, x, down);
129 return QPointF(x, -y);
130 }
131
132 return QPointF();
133}
134
135void QGCMapPolyline::setPath(const QList<QGeoCoordinate>& path)
136{
137 beginReset();
138
139 _polylinePath.clear();
140 _polylineModel.clearAndDeleteContents();
141 for (const QGeoCoordinate& coord: path) {
142 _polylinePath.append(QVariant::fromValue(coord));
143 _polylineModel.append(new QGCQGeoCoordinate(coord, this));
144 }
145
146 setDirty(true);
147
148 endReset();
149}
150
151void QGCMapPolyline::setPath(const QVariantList& path)
152{
153 beginReset();
154
155 _polylinePath = path;
156 _polylineModel.clearAndDeleteContents();
157 for (int i=0; i<_polylinePath.count(); i++) {
158 _polylineModel.append(new QGCQGeoCoordinate(_polylinePath[i].value<QGeoCoordinate>(), this));
159 }
160 setDirty(true);
161
162 endReset();
163}
164
165
166void QGCMapPolyline::saveToJson(QJsonObject& json)
167{
168 QJsonValue jsonValue;
169
170 GeoJsonHelper::saveGeoCoordinateArray(_polylinePath, false /* writeAltitude*/, jsonValue);
171 json.insert(jsonPolylineKey, jsonValue);
172 setDirty(false);
173}
174
175bool QGCMapPolyline::loadFromJson(const QJsonObject& json, bool required, QString& errorString)
176{
177 errorString.clear();
178 clear();
179
180 if (required) {
181 if (!JsonParsing::validateRequiredKeys(json, QStringList(jsonPolylineKey), errorString)) {
182 return false;
183 }
184 } else if (!json.contains(jsonPolylineKey)) {
185 return true;
186 }
187
188 if (!GeoJsonHelper::loadGeoCoordinateArray(json[jsonPolylineKey], false /* altitudeRequired */, _polylinePath, errorString)) {
189 return false;
190 }
191
192 for (int i=0; i<_polylinePath.count(); i++) {
193 _polylineModel.append(new QGCQGeoCoordinate(_polylinePath[i].value<QGeoCoordinate>(), this));
194 }
195
196 setDirty(false);
197 emit pathChanged();
198
199 return true;
200}
201
202QList<QGeoCoordinate> QGCMapPolyline::coordinateList(void) const
203{
204 QList<QGeoCoordinate> coords;
205
206 for (int i=0; i<_polylinePath.count(); i++) {
207 coords.append(_polylinePath[i].value<QGeoCoordinate>());
208 }
209
210 return coords;
211}
212
213void QGCMapPolyline::splitSegment(int vertexIndex)
214{
215 int nextIndex = vertexIndex + 1;
216 if (nextIndex > _polylinePath.length() - 1) {
217 return;
218 }
219
220 QGeoCoordinate firstVertex = _polylinePath[vertexIndex].value<QGeoCoordinate>();
221 QGeoCoordinate nextVertex = _polylinePath[nextIndex].value<QGeoCoordinate>();
222
223 double distance = firstVertex.distanceTo(nextVertex);
224 double azimuth = firstVertex.azimuthTo(nextVertex);
225 QGeoCoordinate newVertex = firstVertex.atDistanceAndAzimuth(distance / 2, azimuth);
226
227 if (nextIndex == 0) {
228 appendVertex(newVertex);
229 } else {
230 _polylineModel.insert(nextIndex, new QGCQGeoCoordinate(newVertex, this));
231 _polylinePath.insert(nextIndex, QVariant::fromValue(newVertex));
232 emit pathChanged();
233 }
234}
235
236void QGCMapPolyline::appendVertex(const QGeoCoordinate& coordinate)
237{
238 _polylinePath.append(QVariant::fromValue(coordinate));
239 _polylineModel.append(new QGCQGeoCoordinate(coordinate, this));
240 emit pathChanged();
241}
242
243void QGCMapPolyline::removeVertex(int vertexIndex)
244{
245 if (vertexIndex < 0 || vertexIndex > _polylinePath.length() - 1) {
246 qCWarning(QGCMapPolylineLog) << "Call to removeVertex with bad vertexIndex:count" << vertexIndex << _polylinePath.length();
247 return;
248 }
249
250 if (_polylinePath.length() <= 2) {
251 // Don't allow the user to trash the polyline
252 return;
253 }
254
255 QObject* coordObj = _polylineModel.removeAt(vertexIndex);
256 coordObj->deleteLater();
257 if(vertexIndex == _selectedVertexIndex) {
258 selectVertex(-1);
259 } else if (vertexIndex < _selectedVertexIndex) {
260 selectVertex(_selectedVertexIndex - 1);
261 } // else do nothing - keep current selected vertex
262
263 _polylinePath.removeAt(vertexIndex);
264 emit pathChanged();
265}
266
268{
269 if (_interactive != interactive) {
270 _interactive = interactive;
272 }
273}
274
276{
277 if (_vertexDrag != vertexDrag) {
278 _vertexDrag = vertexDrag;
279 if (!vertexDrag) {
280 // Drag ended - signal path changed so downstream can recalculate
281 emit pathChanged();
282 }
284 }
285}
286
287QGeoCoordinate QGCMapPolyline::vertexCoordinate(int vertex) const
288{
289 if (vertex >= 0 && vertex < _polylinePath.count()) {
290 return _polylinePath[vertex].value<QGeoCoordinate>();
291 } else {
292 qCWarning(QGCMapPolylineLog) << "QGCMapPolyline::vertexCoordinate bad vertex requested";
293 return QGeoCoordinate();
294 }
295}
296
297QList<QPointF> QGCMapPolyline::nedPolyline(void)
298{
299 QList<QPointF> nedPolyline;
300
301 if (count() > 0) {
302 QGeoCoordinate tangentOrigin = vertexCoordinate(0);
303
304 for (int i=0; i<_polylinePath.count(); i++) {
305 double y, x, down;
306 QGeoCoordinate vertex = vertexCoordinate(i);
307 if (i == 0) {
308 // This avoids a nan calculation that comes out of convertGeoToNed
309 x = y = 0;
310 } else {
311 QGCGeo::convertGeoToNed(vertex, tangentOrigin, y, x, down);
312 }
313 nedPolyline += QPointF(x, y);
314 }
315 }
316
317 return nedPolyline;
318}
319
320QList<QGeoCoordinate> QGCMapPolyline::offsetPolyline(double distance)
321{
322 const QList<QPointF> offsetPoints = QGCPlanarGeometry::offsetPolyline(nedPolyline(), distance);
323 if (offsetPoints.isEmpty()) {
324 return {};
325 }
326
327 const QGeoCoordinate tangentOrigin = vertexCoordinate(0);
328 QList<QGeoCoordinate> result;
329 result.reserve(offsetPoints.size());
330 for (const QPointF& point : offsetPoints) {
331 QGeoCoordinate coordinate;
332 QGCGeo::convertNedToGeo(point.y(), point.x(), 0.0, tangentOrigin, coordinate);
333 result.append(coordinate);
334 }
335 return result;
336}
337
338bool QGCMapPolyline::loadKMLOrSHPFile(const QString &file)
339{
340 QString errorString;
341 QList<QList<QGeoCoordinate>> polylines;
342 if (!ShapeFileHelper::loadPolylinesFromFile(file, polylines, errorString)) {
343 QGC::showAppMessage(errorString);
344 return false;
345 }
346 if (polylines.isEmpty()) {
347 QGC::showAppMessage(tr("No polylines found in file"));
348 return false;
349 }
350 const QList<QGeoCoordinate>& rgCoords = polylines.first();
351
352 beginReset();
353 clear();
354 appendVertices(rgCoords);
355 endReset();
356
357 return true;
358}
359
360void QGCMapPolyline::_polylineModelDirtyChanged(bool dirty)
361{
362 if (dirty) {
363 setDirty(true);
364 }
365}
366
367void QGCMapPolyline::_polylineModelCountChanged(int count)
368{
369 emit countChanged(count);
370}
371
372
373double QGCMapPolyline::length(void) const
374{
375 double length = 0;
376
377 for (int i=0; i<_polylinePath.count() - 1; i++) {
378 QGeoCoordinate from = _polylinePath[i].value<QGeoCoordinate>();
379 QGeoCoordinate to = _polylinePath[i+1].value<QGeoCoordinate>();
380 length += from.distanceTo(to);
381 }
382
383 return length;
384}
385
386void QGCMapPolyline::appendVertices(const QList<QGeoCoordinate>& coordinates)
387{
388 beginReset();
389
390 QList<QObject*> objects;
391 for (const QGeoCoordinate& coordinate: coordinates) {
392 objects.append(new QGCQGeoCoordinate(coordinate, this));
393 _polylinePath.append(QVariant::fromValue(coordinate));
394 }
395 _polylineModel.append(objects);
396
397 endReset();
398
399 emit pathChanged();
400}
401
403{
404 _polylineModel.beginResetModel();
405}
406
408{
409 _polylineModel.endResetModel();
410}
411
413{
414 if (traceMode != _traceMode) {
415 _traceMode = traceMode;
417 }
418}
419
421{
422 if(index == _selectedVertexIndex) return; // do nothing
423
424 if(-1 <= index && index < count()) {
425 _selectedVertexIndex = index;
426 } else {
427 qCWarning(QGCMapPolylineLog) << QStringLiteral("QGCMapPolyline: Selected vertex index (%1) is out of bounds! "
428 "Polyline vertices indexes range is [%2..%3].").arg(index).arg(0).arg(count()-1);
429 _selectedVertexIndex = -1; // deselect vertex
430 }
431
432 emit selectedVertexChanged(_selectedVertexIndex);
433}
#define qgcApp()
Geographic coordinate conversion utilities using GeographicLib.
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void dirtyChanged(bool dirty)
void countChanged(int count)
Q_INVOKABLE void beginReset(void)
QList< QGeoCoordinate > offsetPolyline(double distance)
void dirtyChanged(bool dirty)
static constexpr const char * jsonPolylineKey
QList< QPointF > nedPolyline(void)
Convert polyline to NED and return (D is ignored).
Q_INVOKABLE void clear(void)
Q_INVOKABLE bool loadKMLOrSHPFile(const QString &file)
void cleared(void)
Q_INVOKABLE void splitSegment(int vertexIndex)
Splits the line segment comprised of vertexIndex -> vertexIndex + 1.
void pathChanged(void)
Q_INVOKABLE void removeVertex(int vertexIndex)
void interactiveChanged(bool interactive)
void traceModeChanged(bool traceMode)
Q_INVOKABLE void adjustVertex(int vertexIndex, const QGeoCoordinate coordinate)
void dragPathChanged(void)
void setTraceMode(bool traceMode)
void isEmptyChanged(void)
bool dirty(void) const
Q_INVOKABLE QGeoCoordinate vertexCoordinate(int vertex) const
Returns the QGeoCoordinate for the vertex specified.
void selectVertex(int index)
void setPath(const QList< QGeoCoordinate > &path)
void setDirty(bool dirty)
bool interactive(void) const
Q_INVOKABLE void appendVertex(const QGeoCoordinate &coordinate)
int count(void) const
~QGCMapPolyline() override
void vertexDragChanged(bool vertexDrag)
bool vertexDrag(void) const
void saveToJson(QJsonObject &json)
QList< QGeoCoordinate > coordinateList(void) const
Returns the path in a list of QGeoCoordinate's format.
const QGCMapPolyline & operator=(const QGCMapPolyline &other)
Q_INVOKABLE void endReset(void)
Q_INVOKABLE void appendVertices(const QList< QGeoCoordinate > &coordinates)
QVariantList path(void) const
void isValidChanged(void)
bool loadFromJson(const QJsonObject &json, bool required, QString &errorString)
QGCMapPolyline(QObject *parent=nullptr)
double length(void) const
Returns the length of the polyline in meters.
void countChanged(int count)
void setVertexDrag(bool vertexDrag)
bool traceMode(void) const
void selectedVertexChanged(int index)
void setInteractive(bool interactive)
This is a QGeoCoordinate within a QObject such that it can be used on a QmlObjectListModel.
static bool loadPolylinesFromFile(const QString &file, QList< QList< QGeoCoordinate > > &polylines, QString &errorString, double filterMeters=kDefaultVertexFilterMeters)
bool loadGeoCoordinateArray(const QJsonValue &jsonValue, bool altitudeRequired, QVariantList &rgVarPoints, QString &errorString)
Loads a list of QGeoCoordinates (QGC plan format) from a json array.
void saveGeoCoordinateArray(const QVariantList &rgVarPoints, bool writeAltitude, QJsonValue &jsonValue)
Saves a list of QGeoCoordinates (QGC plan format) to a json array.
bool validateRequiredKeys(const QJsonObject &jsonObject, const QStringList &keys, QString &errorString)
Validates that all listed keys are present in the object.
void convertGeoToNed(const QGeoCoordinate &coord, const QGeoCoordinate &origin, double &x, double &y, double &z)
Definition QGCGeo.cc:36
void convertNedToGeo(double x, double y, double z, const QGeoCoordinate &origin, QGeoCoordinate &coord)
Definition QGCGeo.cc:58
QList< QPointF > offsetPolyline(const QList< QPointF > &polyline, double distance)
void showAppMessage(const QString &message, const QString &title)
Modal application message. Queued if the UI isn't ready yet.
Definition AppMessages.cc:9