Skip to content

Commit

Permalink
Refine flash features API
Browse files Browse the repository at this point in the history
Use a QgsCoordinateReferenceSystem object instead of
QgsVectorLayer to make API more flexible
  • Loading branch information
nyalldawson committed Oct 2, 2017
1 parent dfe0cba commit 5b6f02e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 16 deletions.
6 changes: 3 additions & 3 deletions python/gui/qgsmapcanvas.sip
Expand Up @@ -252,14 +252,14 @@ Pan to the selected features of current (vector) layer keeping same extent.
.. seealso:: flashGeometries()
%End

void flashGeometries( QgsVectorLayer *layer, const QList< QgsGeometry > &geometries,
void flashGeometries( const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
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.
If ``crs`` is a valid coordinate reference system, the geometries will be automatically
transformed from this CRS to the canvas CRS.

The ``startColor`` and ``endColor`` can be specified, along with the number of
``flashes`` and ``duration`` of each flash (in milliseconds).
Expand Down
14 changes: 12 additions & 2 deletions python/gui/qgsrubberband.sip
Expand Up @@ -216,19 +216,29 @@ for tracking the mouse while drawing polylines or polygons.
\param rect rectangle in canvas coordinates
%End

void addGeometry( const QgsGeometry &geom, QgsVectorLayer *layer );
void addGeometry( const QgsGeometry &geometry, QgsVectorLayer *layer );
%Docstring
Adds the geometry of an existing feature to a rubberband
This is useful for multi feature highlighting.
As of 2.0, this method does not change the GeometryType any more. You need to set the GeometryType
of the rubberband explicitly by calling reset() or setToGeometry() with appropriate arguments.
setToGeometry() is also to be preferred for backwards-compatibility.

\param geom the geometry object. Will be treated as a collection of vertices.
\param geometry the geometry object. Will be treated as a collection of vertices.
\param layer the layer containing the feature, used for coord transformation to map
crs. In case of 0 pointer, the coordinates are not going to be transformed.
%End

void addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
%Docstring
Adds a ``geometry`` to the rubberband.

If ``crs`` is specified, the geometry will be automatically reprojected from ``crs``
to the canvas CRS.

.. versionadded:: 3.0
%End

void setTranslationOffset( double dx, double dy );
%Docstring
Adds translation to original coordinates (all in map coordinates)
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsselectbyformdialog.cpp
Expand Up @@ -135,7 +135,7 @@ void QgsSelectByFormDialog::flashFeatures( const QString &filter )
int timeout = settings.value( QStringLiteral( "qgis/messageTimeout" ), 5 ).toInt();
if ( !geoms.empty() )
{
mMapCanvas->flashGeometries( mLayer, geoms );
mMapCanvas->flashGeometries( geoms, mLayer->crs() );
}
else if ( mMessageBar )
{
Expand Down
6 changes: 3 additions & 3 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -1078,18 +1078,18 @@ void QgsMapCanvas::flashFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &
geoms << fet.geometry();
}

flashGeometries( layer, geoms, color1, color2, flashes, duration );
flashGeometries( geoms, layer->crs(), color1, color2, flashes, duration );
}

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

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

if ( geomType == QgsWkbTypes::LineGeometry || geomType == QgsWkbTypes::PointGeometry )
{
Expand Down
6 changes: 3 additions & 3 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -255,16 +255,16 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
/**
* 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.
* If \a crs is a valid coordinate reference system, the geometries will be automatically
* transformed from this CRS to the 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,
void flashGeometries( const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
int flashes = 3, int duration = 500 );

Expand Down
16 changes: 14 additions & 2 deletions src/gui/qgsrubberband.cpp
Expand Up @@ -231,6 +231,18 @@ void QgsRubberBand::setToGeometry( const QgsGeometry &geom, QgsVectorLayer *laye
}

void QgsRubberBand::addGeometry( const QgsGeometry &geometry, QgsVectorLayer *layer )
{
QgsGeometry geom = geometry;
if ( layer )
{
QgsCoordinateTransform ct = mMapCanvas->mapSettings().layerTransform( layer );
geom.transform( ct );
}

addGeometry( geom );
}

void QgsRubberBand::addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs )
{
if ( geometry.isEmpty() )
{
Expand All @@ -243,9 +255,9 @@ void QgsRubberBand::addGeometry( const QgsGeometry &geometry, QgsVectorLayer *la
int idx = mPoints.size();

QgsGeometry geom = geometry;
if ( layer )
if ( crs.isValid() )
{
QgsCoordinateTransform ct = ms.layerTransform( layer );
QgsCoordinateTransform ct( crs, ms.destinationCrs() );
geom.transform( ct );
}

Expand Down
14 changes: 12 additions & 2 deletions src/gui/qgsrubberband.h
Expand Up @@ -257,11 +257,21 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
* of the rubberband explicitly by calling reset() or setToGeometry() with appropriate arguments.
* setToGeometry() is also to be preferred for backwards-compatibility.
*
* \param geom the geometry object. Will be treated as a collection of vertices.
* \param geometry the geometry object. Will be treated as a collection of vertices.
* \param layer the layer containing the feature, used for coord transformation to map
* crs. In case of 0 pointer, the coordinates are not going to be transformed.
*/
void addGeometry( const QgsGeometry &geom, QgsVectorLayer *layer );
void addGeometry( const QgsGeometry &geometry, QgsVectorLayer *layer );

/**
* Adds a \a geometry to the rubberband.
*
* If \a crs is specified, the geometry will be automatically reprojected from \a crs
* to the canvas CRS.
*
* \since QGIS 3.0
*/
void addGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );

/**
* Adds translation to original coordinates (all in map coordinates)
Expand Down

0 comments on commit 5b6f02e

Please sign in to comment.