diff --git a/src/MissionManager/QGCMapPolygon.cc b/src/MissionManager/QGCMapPolygon.cc index e678099cec21b1727134751faea59166127fa5d5..17122057e1bdbbc315b8cb9bef0bee205dd74a8d 100644 --- a/src/MissionManager/QGCMapPolygon.cc +++ b/src/MissionManager/QGCMapPolygon.cc @@ -13,6 +13,7 @@ #include "QGCQGeoCoordinate.h" #include "QGCApplication.h" #include "ShapeFileHelper.h" +#include "QGCLoggingCategory.h" #include #include @@ -311,6 +312,11 @@ void QGCMapPolygon::removeVertex(int vertexIndex) QObject* coordObj = _polygonModel.removeAt(vertexIndex); coordObj->deleteLater(); + if(vertexIndex == _selectedVertexIndex) { + selectVertex(-1); + } else if (vertexIndex < _selectedVertexIndex) { + selectVertex(_selectedVertexIndex - 1); + } // else do nothing - keep current selected vertex _polygonPath.removeAt(vertexIndex); emit pathChanged(); @@ -627,3 +633,21 @@ void QGCMapPolygon::setShowAltColor(bool showAltColor){ emit showAltColorChanged(showAltColor); } } + +void QGCMapPolygon::selectVertex(int index) +{ + if(index == _selectedVertexIndex) return; // do nothing + + if(-1 <= index && index < count()) { + _selectedVertexIndex = index; + } else { + if (!qgcApp()->runningUnitTests()) { + qCWarning(ParameterManagerLog) + << QString("QGCMapPolygon: Selected vertex index (%1) is out of bounds! " + "Polygon vertices indexes range is [%2..%3].").arg(index).arg(0).arg(count()-1); + } + _selectedVertexIndex = -1; // deselect vertex + } + + emit selectedVertexChanged(_selectedVertexIndex); +} diff --git a/src/MissionManager/QGCMapPolygon.h b/src/MissionManager/QGCMapPolygon.h index 08612b5ae1872e3a048f95f27ea2cef3253dd73f..8ac0df731b3e744822c2102b331b3a7a68efd5ce 100644 --- a/src/MissionManager/QGCMapPolygon.h +++ b/src/MissionManager/QGCMapPolygon.h @@ -42,6 +42,7 @@ public: Q_PROPERTY(bool empty READ empty NOTIFY isEmptyChanged) Q_PROPERTY(bool traceMode READ traceMode WRITE setTraceMode NOTIFY traceModeChanged) Q_PROPERTY(bool showAltColor READ showAltColor WRITE setShowAltColor NOTIFY showAltColorChanged) + Q_PROPERTY(int selectedVertex READ selectedVertex WRITE selectVertex NOTIFY selectedVertexChanged) Q_INVOKABLE void clear(void); Q_INVOKABLE void appendVertex(const QGeoCoordinate& coordinate); @@ -111,6 +112,7 @@ public: bool empty (void) const { return _polygonModel.count() == 0; } bool traceMode (void) const { return _traceMode; } bool showAltColor(void) const { return _showAltColor; } + int selectedVertex() const { return _selectedVertexIndex; } QVariantList path (void) const { return _polygonPath; } QmlObjectListModel* qmlPathModel(void) { return &_polygonModel; } @@ -123,6 +125,7 @@ public: void setInteractive (bool interactive); void setTraceMode (bool traceMode); void setShowAltColor(bool showAltColor); + void selectVertex (int index); static const char* jsonPolygonKey; @@ -138,6 +141,7 @@ signals: bool isEmptyChanged (void); void traceModeChanged (bool traceMode); void showAltColorChanged(bool showAltColor); + void selectedVertexChanged(int index); private slots: void _polygonModelCountChanged(int count); @@ -162,6 +166,7 @@ private: bool _resetActive = false; bool _traceMode = false; bool _showAltColor = false; + int _selectedVertexIndex = -1; }; #endif diff --git a/src/MissionManager/QGCMapPolygonTest.cc b/src/MissionManager/QGCMapPolygonTest.cc index 91bc90a0dd68ca56aceee6fdb221f1ed492b8a65..a493cccccda46ea9b0d98cec16993e2ccb856c8a 100644 --- a/src/MissionManager/QGCMapPolygonTest.cc +++ b/src/MissionManager/QGCMapPolygonTest.cc @@ -219,3 +219,38 @@ void QGCMapPolygonTest::_testKMLLoad(void) QVERIFY(!_mapPolygon->loadKMLOrSHPFile(QStringLiteral(":/unittest/PolygonBadCoordinatesNode.kml"))); checkExpectedMessageBox(); } + +void QGCMapPolygonTest::_testSelectVertex(void) +{ + // Create polygon + foreach (auto vertex, _polyPoints) { + _mapPolygon->appendVertex(vertex); + } + + QVERIFY(_mapPolygon->selectedVertex() == -1); + QVERIFY(_mapPolygon->count() == _polyPoints.count()); + + // Test deselect + _mapPolygon->selectVertex(-1); + QVERIFY(_mapPolygon->selectedVertex() == -1); + // Test out of bounds + _mapPolygon->selectVertex(_polyPoints.count()); + QVERIFY(_mapPolygon->selectedVertex() == -1); + // Simple select test + _mapPolygon->selectVertex(_polyPoints.count() - 1); + QVERIFY(_mapPolygon->selectedVertex() == _polyPoints.count() - 1); + // Keep selected test + _mapPolygon->selectVertex(0); + _mapPolygon->removeVertex(_polyPoints.count() - 1); + QVERIFY(_mapPolygon->selectedVertex() == 0); + // Deselect if selected vertex removed + _mapPolygon->appendVertex(_polyPoints[_polyPoints.count() - 1]); + _mapPolygon->selectVertex(_polyPoints.count() - 1); + _mapPolygon->removeVertex(_polyPoints.count() - 1); + QVERIFY(_mapPolygon->selectedVertex() == -1); + // Shift selected index down if removed index < selected index + _mapPolygon->appendVertex(_polyPoints[_polyPoints.count() - 1]); + _mapPolygon->selectVertex(_polyPoints.count() - 1); + _mapPolygon->removeVertex(0); + QVERIFY(_mapPolygon->selectedVertex() == _mapPolygon->count() - 1); +} diff --git a/src/MissionManager/QGCMapPolygonTest.h b/src/MissionManager/QGCMapPolygonTest.h index a5a09886b7294af65f254656c27da5c41a1eca1f..835482b9bdd975356519c7f9428000857e6bfb8f 100644 --- a/src/MissionManager/QGCMapPolygonTest.h +++ b/src/MissionManager/QGCMapPolygonTest.h @@ -17,18 +17,19 @@ class QGCMapPolygonTest : public UnitTest { Q_OBJECT - + public: QGCMapPolygonTest(void); protected: void init(void) final; void cleanup(void) final; - + private slots: void _testDirty(void); void _testVertexManipulation(void); void _testKMLLoad(void); + void _testSelectVertex(void); private: enum { diff --git a/src/MissionManager/QGCMapPolyline.cc b/src/MissionManager/QGCMapPolyline.cc index 8c111381b2e92e80a33d714f1e6499d7b55bc567..ae38dd3da16ab8d474e617a8126d869de9d124e8 100644 --- a/src/MissionManager/QGCMapPolyline.cc +++ b/src/MissionManager/QGCMapPolyline.cc @@ -243,6 +243,11 @@ void QGCMapPolyline::removeVertex(int vertexIndex) QObject* coordObj = _polylineModel.removeAt(vertexIndex); coordObj->deleteLater(); + if(vertexIndex == _selectedVertexIndex) { + selectVertex(-1); + } else if (vertexIndex < _selectedVertexIndex) { + selectVertex(_selectedVertexIndex - 1); + } // else do nothing - keep current selected vertex _polylinePath.removeAt(vertexIndex); emit pathChanged(); @@ -444,3 +449,21 @@ void QGCMapPolyline::setTraceMode(bool traceMode) emit traceModeChanged(traceMode); } } + +void QGCMapPolyline::selectVertex(int index) +{ + if(index == _selectedVertexIndex) return; // do nothing + + if(-1 <= index && index < count()) { + _selectedVertexIndex = index; + } else { + if (!qgcApp()->runningUnitTests()) { + qCWarning(ParameterManagerLog) + << QString("QGCMapPolyline: Selected vertex index (%1) is out of bounds! " + "Polyline vertices indexes range is [%2..%3].").arg(index).arg(0).arg(count()-1); + } + _selectedVertexIndex = -1; // deselect vertex + } + + emit selectedVertexChanged(_selectedVertexIndex); +} diff --git a/src/MissionManager/QGCMapPolyline.h b/src/MissionManager/QGCMapPolyline.h index aff17cbc71ebe92780fd51363c4027c004437e3b..bd5cc8b542a18cd1273f53a0a20878718fe9dc42 100644 --- a/src/MissionManager/QGCMapPolyline.h +++ b/src/MissionManager/QGCMapPolyline.h @@ -33,6 +33,7 @@ public: Q_PROPERTY(bool isValid READ isValid NOTIFY isValidChanged) Q_PROPERTY(bool empty READ empty NOTIFY isEmptyChanged) Q_PROPERTY(bool traceMode READ traceMode WRITE setTraceMode NOTIFY traceModeChanged) + Q_PROPERTY(int selectedVertex READ selectedVertex WRITE selectVertex NOTIFY selectedVertexChanged) Q_INVOKABLE void clear(void); Q_INVOKABLE void appendVertex(const QGeoCoordinate& coordinate); @@ -90,6 +91,7 @@ public: bool isValid (void) const { return _polylineModel.count() >= 2; } bool empty (void) const { return _polylineModel.count() == 0; } bool traceMode (void) const { return _traceMode; } + int selectedVertex() const { return _selectedVertexIndex; } QmlObjectListModel* qmlPathModel(void) { return &_polylineModel; } QmlObjectListModel& pathModel (void) { return _polylineModel; } @@ -98,6 +100,7 @@ public: void setPath (const QVariantList& path); void setInteractive (bool interactive); void setTraceMode (bool traceMode); + void selectVertex (int index); static const char* jsonPolylineKey; @@ -110,6 +113,7 @@ signals: void isValidChanged (void); void isEmptyChanged (void); void traceModeChanged (bool traceMode); + void selectedVertexChanged(int index); private slots: void _polylineModelCountChanged(int count); @@ -128,4 +132,5 @@ private: bool _interactive; bool _resetActive; bool _traceMode = false; + int _selectedVertexIndex = -1; }; diff --git a/src/MissionManager/QGCMapPolylineTest.cc b/src/MissionManager/QGCMapPolylineTest.cc index 02bd2bdaf8c3c736416de7873ea89d1214a78c92..92afce337d89942c70fb5506a0f6cbf15c246580 100644 --- a/src/MissionManager/QGCMapPolylineTest.cc +++ b/src/MissionManager/QGCMapPolylineTest.cc @@ -200,3 +200,38 @@ void QGCMapPolylineTest::_testKMLLoad(void) checkExpectedMessageBox(); } #endif + +void QGCMapPolylineTest::_testSelectVertex(void) +{ + // Create polyline + foreach (auto vertex, _linePoints) { + _mapPolyline->appendVertex(vertex); + } + + QVERIFY(_mapPolyline->selectedVertex() == -1); + QVERIFY(_mapPolyline->count() == _linePoints.count()); + + // Test deselect + _mapPolyline->selectVertex(-1); + QVERIFY(_mapPolyline->selectedVertex() == -1); + // Test out of bounds + _mapPolyline->selectVertex(_linePoints.count()); + QVERIFY(_mapPolyline->selectedVertex() == -1); + // Simple select test + _mapPolyline->selectVertex(_linePoints.count() - 1); + QVERIFY(_mapPolyline->selectedVertex() == _linePoints.count() - 1); + // Keep selected test + _mapPolyline->selectVertex(0); + _mapPolyline->removeVertex(_linePoints.count() - 1); + QVERIFY(_mapPolyline->selectedVertex() == 0); + // Deselect if selected vertex removed + _mapPolyline->appendVertex(_linePoints[_linePoints.count() - 1]); + _mapPolyline->selectVertex(_linePoints.count() - 1); + _mapPolyline->removeVertex(_linePoints.count() - 1); + QVERIFY(_mapPolyline->selectedVertex() == -1); + // Shift selected index down if removed index < selected index + _mapPolyline->appendVertex(_linePoints[_linePoints.count() - 1]); + _mapPolyline->selectVertex(_linePoints.count() - 1); + _mapPolyline->removeVertex(0); + QVERIFY(_mapPolyline->selectedVertex() == _mapPolyline->count() - 1); +} diff --git a/src/MissionManager/QGCMapPolylineTest.h b/src/MissionManager/QGCMapPolylineTest.h index a2c8caa54f2ae13a4f403cb0757a96cde66ec26a..873c6d4cbc8f070cd1b65fa81e8df99bd9e63b8f 100644 --- a/src/MissionManager/QGCMapPolylineTest.h +++ b/src/MissionManager/QGCMapPolylineTest.h @@ -17,18 +17,19 @@ class QGCMapPolylineTest : public UnitTest { Q_OBJECT - + public: QGCMapPolylineTest(void); protected: void init(void) final; void cleanup(void) final; - + private slots: void _testDirty(void); void _testVertexManipulation(void); // void _testKMLLoad(void); + void _testSelectVertex(void); private: enum {