Skip to content

Commit

Permalink
Add some methods for working with lists of map layer references
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 18, 2017
1 parent eb1e820 commit dc3b2cd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/core/qgsmaplayerlistutils.h
Expand Up @@ -13,6 +13,7 @@
#include <QPointer>

#include "qgsmaplayer.h"
#include "qgsmaplayerref.h"

/// @cond PRIVATE

Expand All @@ -39,6 +40,40 @@ inline QgsWeakMapLayerPointerList _qgis_listRawToQPointer( const QList<QgsMapLay
return lst;
}

inline QList<QgsMapLayer *> _qgis_listRefToRaw( const QList< QgsMapLayerRef > &layers )
{
QList<QgsMapLayer *> lst;
lst.reserve( layers.count() );
Q_FOREACH ( const QgsMapLayerRef &layer, layers )
{
if ( layer )
lst.append( &layer );
}
return lst;
}

inline QList< QgsMapLayerRef > _qgis_listRawToRef( const QList<QgsMapLayer *> &layers )
{
QList< QgsMapLayerRef > lst;
lst.reserve( layers.count() );
Q_FOREACH ( QgsMapLayer *layer, layers )
{
lst.append( QgsMapLayerRef( layer ) );
}
return lst;
}

inline void _qgis_removeLayers( QList< QgsMapLayerRef > &list, QList< QgsMapLayer *> layersToRemove )
{
QMutableListIterator<QgsMapLayerRef> it( list );
while ( it.hasNext() )
{
QgsMapLayerRef &ref = it.next();
if ( layersToRemove.contains( &ref ) )
it.remove();
}
}

inline QStringList _qgis_listQPointerToIDs( const QgsWeakMapLayerPointerList &layers )
{
QStringList lst;
Expand Down
31 changes: 31 additions & 0 deletions tests/src/core/testqgsmaplayer.cpp
Expand Up @@ -26,6 +26,7 @@
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include "qgsvectorlayerref.h"
#include "qgsmaplayerlistutils.h"

class TestSignalReceiver : public QObject
{
Expand Down Expand Up @@ -70,6 +71,7 @@ class TestQgsMapLayer : public QObject
void isInScaleRange();

void layerRef();
void layerRefListUtils();


private:
Expand Down Expand Up @@ -228,5 +230,34 @@ void TestQgsMapLayer::layerRef()
QVERIFY( !ref5.resolveWeakly( QgsProject::instance() ) );
}

void TestQgsMapLayer::layerRefListUtils()
{
// conversion utils
QgsVectorLayer *vlA = new QgsVectorLayer( "Point", "a", "memory" );
QgsVectorLayer *vlB = new QgsVectorLayer( "Point", "b", "memory" );

QList<QgsMapLayer *> listRawSource;
listRawSource << vlA << vlB;

QList< QgsMapLayerRef > refs = _qgis_listRawToRef( listRawSource );
QCOMPARE( &refs.at( 0 ), vlA );
QCOMPARE( &refs.at( 1 ), vlB );

QList<QgsMapLayer *> raw = _qgis_listRefToRaw( refs );
QCOMPARE( raw, QList< QgsMapLayer *>() << vlA << vlB );

//remove layers
QgsVectorLayer *vlC = new QgsVectorLayer( "Point", "c", "memory" );
QgsVectorLayer *vlD = new QgsVectorLayer( "Point", "d", "memory" );
refs << QgsMapLayerRef( vlC ) << QgsMapLayerRef( vlD );

_qgis_removeLayers( refs, QList< QgsMapLayer *>() << vlB << vlD );
QCOMPARE( refs.size(), 2 );
QCOMPARE( &refs.at( 0 ), vlA );
QCOMPARE( &refs.at( 1 ), vlC );


}

QGSTEST_MAIN( TestQgsMapLayer )
#include "testqgsmaplayer.moc"

0 comments on commit dc3b2cd

Please sign in to comment.