Skip to content

Commit

Permalink
Fix QgsRubberBand adds two points for first point added to a point
Browse files Browse the repository at this point in the history
geometry rubber band
  • Loading branch information
nyalldawson committed Sep 1, 2021
1 parent f19f554 commit 08d0e98
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/gui/qgsrubberband.cpp
Expand Up @@ -149,7 +149,8 @@ void QgsRubberBand::addPoint( const QgsPointXY &p, bool doUpdate /* = true */, i
if ( ringIndex == mPoints.at( geometryIndex ).size() )
{
mPoints[geometryIndex].append( QgsPolylineXY() );
mPoints[geometryIndex][ringIndex].append( p );
if ( mGeometryType != QgsWkbTypes::PointGeometry )
mPoints[geometryIndex][ringIndex].append( p );
}

if ( mPoints.at( geometryIndex ).at( ringIndex ).size() == 2 &&
Expand Down
36 changes: 36 additions & 0 deletions tests/src/gui/testqgsrubberband.cpp
Expand Up @@ -43,6 +43,8 @@ class TestQgsRubberband : public QObject
void cleanup(); // will be called after every testfunction.

void testAddSingleMultiGeometries(); //test for #7728
void pointGeometryAddPoints();
void lineGeometryAddPoints();
void testBoundingRect(); //test for #12392
void testVisibility(); //test for 12486
void testClose(); //test closing geometry
Expand Down Expand Up @@ -126,6 +128,40 @@ void TestQgsRubberband::testAddSingleMultiGeometries()
QVERIFY( mRubberband->numberOfVertices() == 15 );
}

void TestQgsRubberband::pointGeometryAddPoints()
{
// point geometry
std::unique_ptr< QgsMapCanvas > canvas = std::make_unique< QgsMapCanvas >();
QgsRubberBand r1( canvas.get(), QgsWkbTypes::PointGeometry );
QVERIFY( r1.asGeometry().isEmpty() );
r1.addPoint( QgsPointXY( 1, 2 ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((1 2))" ) );
r1.addPoint( QgsPointXY( 2, 3 ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((1 2),(2 3))" ) );
r1.addPoint( QgsPointXY( 3, 4 ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((1 2),(2 3),(3 4))" ) );
r1.reset( QgsWkbTypes::PointGeometry );
QVERIFY( r1.asGeometry().isEmpty() );
r1.addPoint( QgsPointXY( 1, 2 ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((1 2))" ) );
}

void TestQgsRubberband::lineGeometryAddPoints()
{
std::unique_ptr< QgsMapCanvas > canvas = std::make_unique< QgsMapCanvas >();
QgsRubberBand r1( canvas.get(), QgsWkbTypes::LineGeometry );
QVERIFY( r1.asGeometry().isEmpty() );
r1.addPoint( QgsPointXY( 1, 2 ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "LineString (1 2, 1 2)" ) );
r1.addPoint( QgsPointXY( 2, 3 ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "LineString (1 2, 2 3)" ) );
r1.addPoint( QgsPointXY( 3, 4 ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "LineString (1 2, 2 3, 3 4)" ) );
r1.reset( QgsWkbTypes::LineGeometry );
QVERIFY( r1.asGeometry().isEmpty() );
r1.addPoint( QgsPointXY( 1, 2 ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "LineString (1 2, 1 2)" ) );
}

void TestQgsRubberband::testBoundingRect()
{
Expand Down

0 comments on commit 08d0e98

Please sign in to comment.