Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
More efficient canvas features flash
  • Loading branch information
nyalldawson committed Oct 1, 2017
1 parent e54ea73 commit fc844c6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
21 changes: 21 additions & 0 deletions python/gui/qgsmapcanvas.sip
Expand Up @@ -244,7 +244,28 @@ Pan to the selected features of current (vector) layer keeping same extent.
The ``startColor`` and ``endColor`` can be specified, along with the number of
``flashes`` and ``duration`` of each flash (in milliseconds).

.. note::

If the features or geometries are already available, flashGeometries() is much more efficient.

.. versionadded:: 3.0
.. seealso:: flashGeometries()
%End

void flashGeometries( QgsVectorLayer *layer, const QList< QgsGeometry > &geometries,
const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
int flashes = 3, int duration = 500 );
%Docstring
Causes a set of ``geometries`` to flash within the canvas.

If ``layer`` is non-null, the geometries will be automatically transformed from the layer
CRS to canvas CRS.

The ``startColor`` and ``endColor`` can be specified, along with the number of
``flashes`` and ``duration`` of each flash (in milliseconds).

.. versionadded:: 3.0
.. seealso:: flashFeatureIds()
%End

void setMapTool( QgsMapTool *mapTool );
Expand Down
10 changes: 5 additions & 5 deletions src/app/qgsselectbyformdialog.cpp
Expand Up @@ -119,23 +119,23 @@ void QgsSelectByFormDialog::flashFeatures( const QString &filter )
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );

QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( filter )
.setFlags( QgsFeatureRequest::NoGeometry )
.setExpressionContext( context )
.setSubsetOfAttributes( QgsAttributeList() );

QgsFeatureIterator features = mLayer->getFeatures( request );
QgsFeature feat;
QgsFeatureIds ids;
QList< QgsGeometry > geoms;
while ( features.nextFeature( feat ) )
{
ids.insert( feat.id() );
if ( feat.hasGeometry() )
geoms << feat.geometry();
}

QgsSettings settings;
int timeout = settings.value( QStringLiteral( "qgis/messageTimeout" ), 5 ).toInt();
if ( !ids.empty() )
if ( !geoms.empty() )
{
mMapCanvas->flashFeatureIds( mLayer, ids );
mMapCanvas->flashGeometries( mLayer, geoms );
}
else if ( mMessageBar )
{
Expand Down
11 changes: 8 additions & 3 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -1078,12 +1078,17 @@ void QgsMapCanvas::flashFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &
geoms << fet.geometry();
}

if ( geoms.isEmpty() )
flashGeometries( layer, geoms, color1, color2, flashes, duration );
}

void QgsMapCanvas::flashGeometries( QgsVectorLayer *layer, const QList<QgsGeometry> &geometries, const QColor &color1, const QColor &color2, int flashes, int duration )
{
if ( geometries.isEmpty() )
return;

QgsWkbTypes::GeometryType geomType = QgsWkbTypes::geometryType( layer->wkbType() );
QgsWkbTypes::GeometryType geomType = QgsWkbTypes::geometryType( geometries.at( 0 ).wkbType() );
QgsRubberBand *rb = new QgsRubberBand( this, geomType );
for ( const QgsGeometry &geom : qgsAsConst( geoms ) )
for ( const QgsGeometry &geom : geometries )
rb->addGeometry( geom, layer );

if ( geomType == QgsWkbTypes::LineGeometry || geomType == QgsWkbTypes::PointGeometry )
Expand Down
19 changes: 19 additions & 0 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -243,12 +243,31 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
* The \a startColor and \a endColor can be specified, along with the number of
* \a flashes and \a duration of each flash (in milliseconds).
*
* \note If the features or geometries are already available, flashGeometries() is much more efficient.
*
* \since QGIS 3.0
* \see flashGeometries()
*/
void flashFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids,
const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
int flashes = 3, int duration = 500 );

/**
* Causes a set of \a geometries to flash within the canvas.
*
* If \a layer is non-null, the geometries will be automatically transformed from the layer
* CRS to canvas CRS.
*
* The \a startColor and \a endColor can be specified, along with the number of
* \a flashes and \a duration of each flash (in milliseconds).
*
* \since QGIS 3.0
* \see flashFeatureIds()
*/
void flashGeometries( QgsVectorLayer *layer, const QList< QgsGeometry > &geometries,
const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
int flashes = 3, int duration = 500 );

//! \brief Sets the map tool currently being used on the canvas
void setMapTool( QgsMapTool *mapTool );

Expand Down

0 comments on commit fc844c6

Please sign in to comment.