Skip to content

Commit

Permalink
Fix selecting features by radius (fix #14748)
Browse files Browse the repository at this point in the history
(cherry-picked from 33a5ee7)
  • Loading branch information
nyalldawson committed Jul 1, 2016
1 parent 5305486 commit 68739c6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
8 changes: 8 additions & 0 deletions python/gui/qgsrubberband.sip
Expand Up @@ -122,6 +122,14 @@ class QgsRubberBand: QgsMapCanvasItem
*/
void addPoint( const QgsPoint & p, bool doUpdate = true, int geometryIndex = 0 );

/** Ensures that a polygon geometry is closed and that the last vertex equals the
* first vertex.
* @param doUpdate set to true to update the map canvas immediately
* @param geometryIndex index of the feature part (in case of multipart geometries)
* @note added in QGIS 2.16
*/
void closePoints( bool doUpdate = true, int geometryIndex = 0 );

/**
* Remove a vertex from the rubberband and (optionally) update canvas.
* @param index The index of the vertex/point to remove, negative indexes start at end
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsmaptoolselectradius.cpp
Expand Up @@ -109,6 +109,7 @@ void QgsMapToolSelectRadius::setRadiusRubberBand( QgsPoint & radiusEdge )
double theta = i * ( 2.0 * M_PI / RADIUS_SEGMENTS );
QgsPoint radiusPoint( mRadiusCenter.x() + r * cos( theta ),
mRadiusCenter.y() + r * sin( theta ) );
mRubberBand->addPoint( radiusPoint );
mRubberBand->addPoint( radiusPoint, false );
}
mRubberBand->closePoints( true );
}
20 changes: 20 additions & 0 deletions src/gui/qgsrubberband.cpp
Expand Up @@ -188,6 +188,26 @@ void QgsRubberBand::addPoint( const QgsPoint & p, bool doUpdate /* = true */, in
}
}

void QgsRubberBand::closePoints( bool doUpdate, int geometryIndex )
{
if ( geometryIndex < 0 || geometryIndex >= mPoints.size() )
{
return;
}

if ( mPoints.at( geometryIndex ).at( 0 ) != mPoints.at( geometryIndex ).at( mPoints.at( geometryIndex ).size() - 1 ) )
{
mPoints[geometryIndex] << mPoints.at( geometryIndex ).at( 0 );
}

if ( doUpdate )
{
setVisible( true );
updateRect();
update();
}
}


void QgsRubberBand::removePoint( int index, bool doUpdate/* = true*/, int geometryIndex/* = 0*/ )
{
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsrubberband.h
Expand Up @@ -149,6 +149,14 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
*/
void addPoint( const QgsPoint & p, bool doUpdate = true, int geometryIndex = 0 );

/** Ensures that a polygon geometry is closed and that the last vertex equals the
* first vertex.
* @param doUpdate set to true to update the map canvas immediately
* @param geometryIndex index of the feature part (in case of multipart geometries)
* @note added in QGIS 2.16
*/
void closePoints( bool doUpdate = true, int geometryIndex = 0 );

/**
* Remove a vertex from the rubberband and (optionally) update canvas.
* @param index The index of the vertex/point to remove, negative indexes start at end
Expand Down
29 changes: 29 additions & 0 deletions tests/src/gui/testqgsrubberband.cpp
Expand Up @@ -46,6 +46,7 @@ class TestQgsRubberband : public QObject
void testAddSingleMultiGeometries(); //test for #7728
void testBoundingRect(); //test for #12392
void testVisibility(); //test for 12486
void testClose(); //test closing geometry

private:
QgsMapCanvas* mCanvas;
Expand Down Expand Up @@ -193,6 +194,34 @@ void TestQgsRubberband::testVisibility()

}

void TestQgsRubberband::testClose()
{
QgsRubberBand r( mCanvas, QGis::Polygon );

// try closing empty rubber band, don't want to crash
r.closePoints();
QCOMPARE( r.partSize( 0 ), 0 );

r.addPoint( QgsPoint( 1, 2 ) );
r.addPoint( QgsPoint( 1, 3 ) );
r.addPoint( QgsPoint( 2, 3 ) );
QCOMPARE( r.partSize( 0 ), 3 );

// test with some bad geometry indexes - don't want to crash!
r.closePoints( true, -1 );
QCOMPARE( r.partSize( 0 ), 3 );
r.closePoints( true, 100 );
QCOMPARE( r.partSize( 0 ), 3 );

// valid close
r.closePoints();
QCOMPARE( r.partSize( 0 ), 4 );

// close already closed polygon, should be no change
r.closePoints();
QCOMPARE( r.partSize( 0 ), 4 );
}


QTEST_MAIN( TestQgsRubberband )
#include "testqgsrubberband.moc"
Expand Down

0 comments on commit 68739c6

Please sign in to comment.