Skip to content
Permalink
Browse files
Add QgsRubberBand::copyPointsFrom to quickly copy all points from
one rubber band to another
  • Loading branch information
nyalldawson committed Sep 1, 2021
1 parent 08d0e98 commit 26b536b068e1fc984d719dc33f044ae809ec667a
@@ -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 );
@@ -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() )
@@ -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.
@@ -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
@@ -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.

0 comments on commit 26b536b

Please sign in to comment.