Skip to content
Permalink
Browse files
Add some methods for working with lists of map layer references
  • Loading branch information
nyalldawson committed Apr 18, 2017
1 parent eb1e820 commit dc3b2cdbd4cc81fdf8c4238ec372d34a83496e79
Showing with 66 additions and 0 deletions.
  1. +35 −0 src/core/qgsmaplayerlistutils.h
  2. +31 −0 tests/src/core/testqgsmaplayer.cpp
@@ -13,6 +13,7 @@
#include <QPointer>

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

/// @cond PRIVATE

@@ -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;
@@ -26,6 +26,7 @@
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include "qgsvectorlayerref.h"
#include "qgsmaplayerlistutils.h"

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

void layerRef();
void layerRefListUtils();


private:
@@ -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.