@@ -45,6 +45,7 @@ class TestQgsRubberband : public QObject
45
45
void cleanup (); // will be called after every testfunction.
46
46
47
47
void testAddSingleMultiGeometries (); // test for #7728
48
+ void testBoundingRect (); // test for #12392
48
49
49
50
private:
50
51
QgsMapCanvas* mCanvas ;
@@ -107,10 +108,53 @@ void TestQgsRubberband::testAddSingleMultiGeometries()
107
108
QVERIFY ( mRubberband ->numberOfVertices () == 15 );
108
109
}
109
110
110
- QTEST_MAIN ( TestQgsRubberband )
111
- #include " testqgsrubberband.moc"
112
111
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
+ }
113
155
114
156
157
+ QTEST_MAIN ( TestQgsRubberband )
158
+ #include " testqgsrubberband.moc"
115
159
116
160
0 commit comments