Skip to content

Commit

Permalink
Add method to seek atlas directly to a QgsFeature
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 7450c78 commit 39ae0ee
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
13 changes: 13 additions & 0 deletions python/core/layout/qgslayoutatlas.sip
Expand Up @@ -332,6 +332,19 @@ Seeks to the specified ``feature`` number.

.. seealso:: :py:func:`next()`

.. seealso:: :py:func:`last()`
%End

bool seekTo( const QgsFeature &feature );
%Docstring
Seeks to the specified ``feature``.

.. seealso:: :py:func:`first()`

.. seealso:: :py:func:`previous()`

.. seealso:: :py:func:`next()`

.. seealso:: :py:func:`last()`
%End

Expand Down
22 changes: 22 additions & 0 deletions src/core/layout/qgslayoutatlas.cpp
Expand Up @@ -387,6 +387,28 @@ bool QgsLayoutAtlas::seekTo( int feature )
return prepareForFeature( feature );
}

bool QgsLayoutAtlas::seekTo( const QgsFeature &feature )
{
int i = -1;
auto it = mFeatureIds.constBegin();
for ( int currentIdx = 0; it != mFeatureIds.constEnd(); ++it, ++currentIdx )
{
if ( ( *it ).first == feature.id() )
{
i = currentIdx;
break;
}
}

if ( i < 0 )
{
//feature not found
return false;
}

return seekTo( i );
}

void QgsLayoutAtlas::refreshCurrentFeature()
{
prepareForFeature( mCurrentFeatureNo );
Expand Down
9 changes: 9 additions & 0 deletions src/core/layout/qgslayoutatlas.h
Expand Up @@ -289,6 +289,15 @@ class CORE_EXPORT QgsLayoutAtlas : public QObject, public QgsAbstractLayoutItera
*/
bool seekTo( int feature );

/**
* Seeks to the specified \a feature.
* \see first()
* \see previous()
* \see next()
* \see last()
*/
bool seekTo( const QgsFeature &feature );

/**
* Refreshes the current atlas feature, by refetching its attributes from the vector layer provider
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/src/python/test_qgslayoutatlas.py
Expand Up @@ -20,6 +20,7 @@
import glob

from qgis.core import (QgsUnitTypes,
QgsFeature,
QgsLayout,
QgsPrintLayout,
QgsLayoutAtlas,
Expand Down Expand Up @@ -216,24 +217,28 @@ def testIteration(self):
self.assertEqual(atlas.currentFeatureNumber(), 0)
self.assertEqual(l.reportContext().feature()[4], 'Basse-Normandie')
self.assertEqual(l.reportContext().layer(), vector_layer)
f1 = l.reportContext().feature()

self.assertTrue(atlas.next())
self.assertEqual(len(atlas_feature_changed_spy), 2)
self.assertEqual(len(context_changed_spy), 2)
self.assertEqual(atlas.currentFeatureNumber(), 1)
self.assertEqual(l.reportContext().feature()[4], 'Bretagne')
f2 = l.reportContext().feature()

self.assertTrue(atlas.next())
self.assertEqual(len(atlas_feature_changed_spy), 3)
self.assertEqual(len(context_changed_spy), 3)
self.assertEqual(atlas.currentFeatureNumber(), 2)
self.assertEqual(l.reportContext().feature()[4], 'Pays de la Loire')
f3 = l.reportContext().feature()

self.assertTrue(atlas.next())
self.assertEqual(len(atlas_feature_changed_spy), 4)
self.assertEqual(len(context_changed_spy), 4)
self.assertEqual(atlas.currentFeatureNumber(), 3)
self.assertEqual(l.reportContext().feature()[4], 'Centre')
f4 = l.reportContext().feature()

self.assertFalse(atlas.next())
self.assertTrue(atlas.seekTo(2))
Expand Down Expand Up @@ -263,6 +268,16 @@ def testIteration(self):
self.assertTrue(atlas.endRender())
self.assertEqual(len(atlas_feature_changed_spy), 10)

self.assertTrue(atlas.seekTo(f1))
self.assertEqual(l.reportContext().feature()[4], 'Basse-Normandie')
self.assertTrue(atlas.seekTo(f4))
self.assertEqual(l.reportContext().feature()[4], 'Centre')
self.assertTrue(atlas.seekTo(f3))
self.assertEqual(l.reportContext().feature()[4], 'Pays de la Loire')
self.assertTrue(atlas.seekTo(f2))
self.assertEqual(l.reportContext().feature()[4], 'Bretagne')
self.assertFalse(atlas.seekTo(QgsFeature(5)))

def testUpdateFeature(self):
p = QgsProject()
vectorFileInfo = QFileInfo(unitTestDataPath() + "/france_parts.shp")
Expand Down

0 comments on commit 39ae0ee

Please sign in to comment.