Skip to content

Commit a844bfa

Browse files
author
Sandro Santilli
committed
Recompute rubberband extent on zoom/pan
Fix #12392 Includes testcase
1 parent 84d47c9 commit a844bfa

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/gui/qgsrubberband.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,12 @@ void QgsRubberBand::updateRect()
572572

573573
void QgsRubberBand::updatePosition( )
574574
{
575-
// nothing to do here...
575+
// re-compute rectangle
576+
// See http://hub.qgis.org/issues/12392
577+
// NOTE: could be optimized by saving map-extent
578+
// of rubberband and simply re-projecting
579+
// that to device-rectange on "updatePosition"
580+
updateRect();
576581
}
577582

578583
void QgsRubberBand::setTranslationOffset( double dx, double dy )

tests/src/gui/testqgsrubberband.cpp

+46-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class TestQgsRubberband : public QObject
4545
void cleanup(); // will be called after every testfunction.
4646

4747
void testAddSingleMultiGeometries(); //test for #7728
48+
void testBoundingRect(); //test for #12392
4849

4950
private:
5051
QgsMapCanvas* mCanvas;
@@ -107,10 +108,53 @@ void TestQgsRubberband::testAddSingleMultiGeometries()
107108
QVERIFY( mRubberband->numberOfVertices() == 15 );
108109
}
109110

110-
QTEST_MAIN( TestQgsRubberband )
111-
#include "testqgsrubberband.moc"
112111

112+
void TestQgsRubberband::testBoundingRect()
113+
{
114+
QSizeF mapSize = mCanvas->mapSettings().outputSize();
115+
116+
// Set extent to match canvas size.
117+
// This is to ensure a 1:1 scale
118+
mCanvas->setExtent( QgsRectangle( QRectF(
119+
QPointF(0,0), mapSize
120+
) ) );
121+
QCOMPARE( mCanvas->mapUnitsPerPixel (), 1.0 );
122+
123+
// Polygon extent is 10,10 to 30,30
124+
QSharedPointer<QgsGeometry> geom( QgsGeometry::fromWkt(
125+
"POLYGON((10 10,10 30,30 30,30 10,10 10))"
126+
) );
127+
mRubberband = new QgsRubberBand( mCanvas, mPolygonLayer->geometryType() );
128+
mRubberband->setIconSize( 5 ); // default, but better be explicit
129+
mRubberband->setWidth( 1 ); // default, but better be explicit
130+
mRubberband->addGeometry( geom.data(), mPolygonLayer );
131+
132+
// 20 pixels for the extent + 3 for pen & icon per side + 2 of padding
133+
QCOMPARE( mRubberband->boundingRect(), QRectF(QPointF(-1,-1),QSizeF(28,28)) );
134+
QCOMPARE( mRubberband->pos(), QPointF(
135+
// 10 for extent minx - 3 for pen & icon
136+
7,
137+
// 30 for extent maxy - 3 for pen & icon
138+
mapSize.height() - 30 - 3
139+
) );
140+
141+
mCanvas->setExtent( QgsRectangle( QRectF(
142+
QPointF(0,0), mapSize/2
143+
) ) );
144+
145+
// 40 pixels for the extent + 6 for pen & icon per side + 2 of padding
146+
QCOMPARE( mRubberband->boundingRect(), QRectF(QPointF(-1,-1),QSizeF(54,54)) );
147+
QCOMPARE( mRubberband->pos(), QPointF(
148+
// 10 for extent minx - 3 for pen & icon
149+
7 * 2,
150+
// 30 for extent maxy - 3 for pen & icon
151+
mapSize.height() - ( 30 + 3 ) * 2
152+
) );
153+
154+
}
113155

114156

157+
QTEST_MAIN( TestQgsRubberband )
158+
#include "testqgsrubberband.moc"
115159

116160

0 commit comments

Comments
 (0)