Skip to content

Commit

Permalink
Add QgsRubberBand::copyPointsFrom to quickly copy all points from
Browse files Browse the repository at this point in the history
one rubber band to another
  • Loading branch information
nyalldawson committed Sep 1, 2021
1 parent 08d0e98 commit 26b536b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgsrubberband.sip.in
Expand Up @@ -272,6 +272,13 @@ By default, no reprojection is done.
Sets this rubber band to a map canvas rectangle

:param rect: rectangle in canvas coordinates
%End

void copyPointsFrom( const QgsRubberBand *other );
%Docstring
Copies the points from another rubber band.

.. versionadded:: 3.22
%End

void addGeometry( const QgsGeometry &geometry, QgsVectorLayer *layer, bool doUpdate = true );
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsrubberband.cpp
Expand Up @@ -437,6 +437,14 @@ void QgsRubberBand::setToCanvasRectangle( QRect rect )
addPoint( ul, true );
}

void QgsRubberBand::copyPointsFrom( const QgsRubberBand *other )
{
reset( other->mGeometryType );
mPoints = other->mPoints;
updateRect();
update();
}

void QgsRubberBand::paint( QPainter *p )
{
if ( mPoints.isEmpty() )
Expand Down
7 changes: 7 additions & 0 deletions src/gui/qgsrubberband.h
Expand Up @@ -319,6 +319,13 @@ class GUI_EXPORT QgsRubberBand : public QgsMapCanvasItem
*/
void setToCanvasRectangle( QRect rect );

/**
* Copies the points from another rubber band.
*
* \since QGIS 3.22
*/
void copyPointsFrom( const QgsRubberBand *other );

/**
* Adds the geometry of an existing feature to a rubberband
* This is useful for multi feature highlighting.
Expand Down
24 changes: 24 additions & 0 deletions tests/src/gui/testqgsrubberband.cpp
Expand Up @@ -45,6 +45,7 @@ class TestQgsRubberband : public QObject
void testAddSingleMultiGeometries(); //test for #7728
void pointGeometryAddPoints();
void lineGeometryAddPoints();
void copyPointsFrom();
void testBoundingRect(); //test for #12392
void testVisibility(); //test for 12486
void testClose(); //test closing geometry
Expand Down Expand Up @@ -163,6 +164,29 @@ void TestQgsRubberband::lineGeometryAddPoints()
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "LineString (1 2, 1 2)" ) );
}

void TestQgsRubberband::copyPointsFrom()
{
std::unique_ptr< QgsMapCanvas > canvas = std::make_unique< QgsMapCanvas >();
QgsRubberBand r1( canvas.get(), QgsWkbTypes::PointGeometry );
r1.addPoint( QgsPointXY( 1, 2 ) );
r1.addPoint( QgsPointXY( 3, 4 ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((1 2),(3 4))" ) );

QgsRubberBand r2( canvas.get(), QgsWkbTypes::LineGeometry );
r2.copyPointsFrom( &r1 );
QCOMPARE( r2.asGeometry().asWkt(), QStringLiteral( "MultiPoint ((1 2),(3 4))" ) );

// line geometry band
r1.reset( QgsWkbTypes::LineGeometry );
r1.addPoint( QgsPointXY( 1, 2 ) );
r1.addPoint( QgsPointXY( 2, 3 ) );
r1.addPoint( QgsPointXY( 3, 4 ) );
QCOMPARE( r1.asGeometry().asWkt(), QStringLiteral( "LineString (1 2, 2 3, 3 4)" ) );

r2.copyPointsFrom( &r1 );
QCOMPARE( r2.asGeometry().asWkt(), QStringLiteral( "LineString (1 2, 2 3, 3 4)" ) );
}

void TestQgsRubberband::testBoundingRect()
{
// Set extent to match canvas size.
Expand Down

0 comments on commit 26b536b

Please sign in to comment.