Skip to content

Commit e54ea73

Browse files
committed
Make rubber band creation a bit more efficient (avoid multiple transform creation)
1 parent 3e9fa72 commit e54ea73

File tree

1 file changed

+37
-72
lines changed

1 file changed

+37
-72
lines changed

src/gui/qgsrubberband.cpp

Lines changed: 37 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ void QgsRubberBand::setToGeometry( const QgsGeometry &geom, QgsVectorLayer *laye
230230
addGeometry( geom, layer );
231231
}
232232

233-
void QgsRubberBand::addGeometry( const QgsGeometry &geom, QgsVectorLayer *layer )
233+
void QgsRubberBand::addGeometry( const QgsGeometry &geometry, QgsVectorLayer *layer )
234234
{
235-
if ( geom.isNull() )
235+
if ( geometry.isEmpty() )
236236
{
237237
return;
238238
}
@@ -242,21 +242,20 @@ void QgsRubberBand::addGeometry( const QgsGeometry &geom, QgsVectorLayer *layer
242242

243243
int idx = mPoints.size();
244244

245+
QgsGeometry geom = geometry;
246+
if ( layer )
247+
{
248+
QgsCoordinateTransform ct = ms.layerTransform( layer );
249+
geom.transform( ct );
250+
}
251+
245252
switch ( geom.wkbType() )
246253
{
247254

248255
case QgsWkbTypes::Point:
249256
case QgsWkbTypes::Point25D:
250257
{
251-
QgsPointXY pt;
252-
if ( layer )
253-
{
254-
pt = ms.layerToMapCoordinates( layer, geom.asPoint() );
255-
}
256-
else
257-
{
258-
pt = geom.asPoint();
259-
}
258+
QgsPointXY pt = geom.asPoint();
260259
addPoint( pt, false, idx );
261260
removeLastPoint( idx, false );
262261
}
@@ -265,38 +264,23 @@ void QgsRubberBand::addGeometry( const QgsGeometry &geom, QgsVectorLayer *layer
265264
case QgsWkbTypes::MultiPoint:
266265
case QgsWkbTypes::MultiPoint25D:
267266
{
268-
QgsMultiPoint mpt = geom.asMultiPoint();
269-
for ( int i = 0; i < mpt.size(); ++i, ++idx )
267+
const QgsMultiPoint mpt = geom.asMultiPoint();
268+
for ( QgsPointXY pt : mpt )
270269
{
271-
QgsPointXY pt = mpt[i];
272-
if ( layer )
273-
{
274-
addPoint( ms.layerToMapCoordinates( layer, pt ), false, idx );
275-
removeLastPoint( idx, false );
276-
}
277-
else
278-
{
279-
addPoint( pt, false, idx );
280-
removeLastPoint( idx, false );
281-
}
270+
addPoint( pt, false, idx );
271+
removeLastPoint( idx, false );
272+
idx++;
282273
}
283274
}
284275
break;
285276

286277
case QgsWkbTypes::LineString:
287278
case QgsWkbTypes::LineString25D:
288279
{
289-
QgsPolyline line = geom.asPolyline();
290-
for ( int i = 0; i < line.count(); i++ )
280+
const QgsPolyline line = geom.asPolyline();
281+
for ( QgsPointXY pt : line )
291282
{
292-
if ( layer )
293-
{
294-
addPoint( ms.layerToMapCoordinates( layer, line[i] ), false, idx );
295-
}
296-
else
297-
{
298-
addPoint( line[i], false, idx );
299-
}
283+
addPoint( pt, false, idx );
300284
}
301285
}
302286
break;
@@ -305,46 +289,31 @@ void QgsRubberBand::addGeometry( const QgsGeometry &geom, QgsVectorLayer *layer
305289
case QgsWkbTypes::MultiLineString25D:
306290
{
307291

308-
QgsMultiPolyline mline = geom.asMultiPolyline();
309-
for ( int i = 0; i < mline.size(); ++i, ++idx )
292+
const QgsMultiPolyline mline = geom.asMultiPolyline();
293+
for ( const QgsPolyline &line : mline )
310294
{
311-
QgsPolyline line = mline[i];
312-
313295
if ( line.isEmpty() )
314296
{
315-
--idx;
297+
continue;
316298
}
317299

318-
for ( int j = 0; j < line.size(); ++j )
300+
for ( QgsPointXY pt : line )
319301
{
320-
if ( layer )
321-
{
322-
addPoint( ms.layerToMapCoordinates( layer, line[j] ), false, idx );
323-
}
324-
else
325-
{
326-
addPoint( line[j], false, idx );
327-
}
302+
addPoint( pt, false, idx );
328303
}
304+
idx++;
329305
}
330306
}
331307
break;
332308

333309
case QgsWkbTypes::Polygon:
334310
case QgsWkbTypes::Polygon25D:
335311
{
336-
QgsPolygon poly = geom.asPolygon();
337-
QgsPolyline line = poly[0];
338-
for ( int i = 0; i < line.count(); i++ )
312+
const QgsPolygon poly = geom.asPolygon();
313+
const QgsPolyline line = poly.at( 0 );
314+
for ( QgsPointXY pt : line )
339315
{
340-
if ( layer )
341-
{
342-
addPoint( ms.layerToMapCoordinates( layer, line[i] ), false, idx );
343-
}
344-
else
345-
{
346-
addPoint( line[i], false, idx );
347-
}
316+
addPoint( pt, false, idx );
348317
}
349318
}
350319
break;
@@ -353,22 +322,18 @@ void QgsRubberBand::addGeometry( const QgsGeometry &geom, QgsVectorLayer *layer
353322
case QgsWkbTypes::MultiPolygon25D:
354323
{
355324

356-
QgsMultiPolygon multipoly = geom.asMultiPolygon();
357-
for ( int i = 0; i < multipoly.size(); ++i, ++idx )
325+
const QgsMultiPolygon multipoly = geom.asMultiPolygon();
326+
for ( const QgsPolygon &poly : multipoly )
358327
{
359-
QgsPolygon poly = multipoly[i];
360-
QgsPolyline line = poly[0];
361-
for ( int j = 0; j < line.count(); ++j )
328+
if ( poly.empty() )
329+
continue;
330+
331+
const QgsPolyline line = poly.at( 0 );
332+
for ( QgsPointXY pt : line )
362333
{
363-
if ( layer )
364-
{
365-
addPoint( ms.layerToMapCoordinates( layer, line[j] ), false, idx );
366-
}
367-
else
368-
{
369-
addPoint( line[j], false, idx );
370-
}
334+
addPoint( pt, false, idx );
371335
}
336+
idx++;
372337
}
373338
}
374339
break;

0 commit comments

Comments
 (0)