Unverified Commit ff026e1f authored by Don Gagne's avatar Don Gagne Committed by GitHub

Merge pull request #8813 from airmap/vertex_select

QGCMapPolygon select vertexes functionality
parents 55dd50c1 c43a4967
......@@ -13,6 +13,7 @@
#include "QGCQGeoCoordinate.h"
#include "QGCApplication.h"
#include "ShapeFileHelper.h"
#include "QGCLoggingCategory.h"
#include <QGeoRectangle>
#include <QDebug>
......@@ -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);
}
......@@ -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
......@@ -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);
}
......@@ -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 {
......
......@@ -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);
}
......@@ -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;
};
......@@ -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);
}
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment