Skip to content

Commit

Permalink
Fix setting rubber band to point geometry, add tests
Browse files Browse the repository at this point in the history
Fixes #44934
  • Loading branch information
nyalldawson committed Sep 5, 2021
1 parent 98a902b commit 6b78485
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/gui/qgsrubberband.cpp
Expand Up @@ -335,15 +335,13 @@ void QgsRubberBand::addGeometry( const QgsGeometry &geometry, const QgsCoordinat
{
QgsPointXY pt = geom.asPoint();
addPoint( pt, false, idx );
removeLastPoint( idx, false );
}
else if ( QgsWkbTypes::geometryType( geomType ) == QgsWkbTypes::PointGeometry && QgsWkbTypes::isMultiType( geomType ) )
{
const QgsMultiPointXY mpt = geom.asMultiPoint();
for ( const QgsPointXY &pt : mpt )
{
addPoint( pt, false, idx );
removeLastPoint( idx, false );
idx++;
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/src/gui/testqgsrubberband.cpp
Expand Up @@ -44,6 +44,7 @@ class TestQgsRubberband : public QObject

void testAddSingleMultiGeometries(); //test for #7728
void pointGeometryAddPoints();
void pointGeometrySetGeometry();
void lineGeometryAddPoints();
void copyPointsFrom();
void testBoundingRect(); //test for #12392
Expand Down Expand Up @@ -147,6 +148,29 @@ void TestQgsRubberband::pointGeometryAddPoints()
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((1 2))" ) );
}

void TestQgsRubberband::pointGeometrySetGeometry()
{
// point geometry, set using setToGeometry
std::unique_ptr< QgsMapCanvas > canvas = std::make_unique< QgsMapCanvas >();
QgsRubberBand r1( canvas.get(), QgsWkbTypes::PointGeometry );
QVERIFY( r1.asGeometry().isEmpty() );
r1.setToGeometry( QgsGeometry::fromPointXY( QgsPointXY( 1, 2 ) ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((1 2))" ) );
r1.setToGeometry( QgsGeometry::fromPointXY( QgsPointXY( 2, 3 ) ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((2 3))" ) );
r1.addGeometry( QgsGeometry::fromPointXY( QgsPointXY( 5, 6 ) ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((2 3),(5 6))" ) );
r1.setToGeometry( QgsGeometry::fromMultiPointXY( {QgsPointXY( 1, 2 ), QgsPointXY( 3, 4 ) } ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((1 2),(3 4))" ) );
r1.addGeometry( QgsGeometry::fromPointXY( QgsPointXY( 5, 7 ) ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((1 2),(3 4),(5 7))" ) );
r1.addGeometry( QgsGeometry::fromMultiPointXY( { QgsPointXY( 7, 8 ), QgsPointXY( 9, 10 )} ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((1 2),(3 4),(5 7),(7 8),(9 10))" ) );
r1.reset( QgsWkbTypes::PointGeometry );
r1.addGeometry( QgsGeometry::fromMultiPointXY( { QgsPointXY( 7, 8 ), QgsPointXY( 9, 10 )} ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((7 8),(9 10))" ) );
}

void TestQgsRubberband::lineGeometryAddPoints()
{
std::unique_ptr< QgsMapCanvas > canvas = std::make_unique< QgsMapCanvas >();
Expand Down

0 comments on commit 6b78485

Please sign in to comment.