Add some methods for working with lists of map layer references

This commit is contained in:
Nyall Dawson 2017-04-18 15:18:33 +10:00
parent eb1e8205e1
commit dc3b2cdbd4
2 changed files with 66 additions and 0 deletions

View File

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

View File

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