More efficient canvas features flash

This commit is contained in:
Nyall Dawson 2017-09-29 11:00:32 +10:00
parent e54ea73e0b
commit fc844c66d4
4 changed files with 53 additions and 8 deletions

View File

@ -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 );

View File

@ -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 )
{

View File

@ -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 )

View File

@ -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 );