Skip to content
Permalink
Browse files
Some API improvements to QgsMapLayerRef, unit tests
  • Loading branch information
nyalldawson committed Apr 18, 2017
1 parent 6bb3fa6 commit 35410fd4b7eba423b20fa49163cd10b8e17500ce
Showing with 56 additions and 8 deletions.
  1. +1 −1 src/core/layertree/qgslayertreelayer.h
  2. +23 −5 src/core/qgsmaplayerref.h
  3. +1 −1 src/core/qgsvectorlayerjoininfo.h
  4. +31 −1 tests/src/core/testqgsmaplayer.cpp
@@ -54,7 +54,7 @@ class CORE_EXPORT QgsLayerTreeLayer : public QgsLayerTreeNode

QString layerId() const { return mRef.layerId; }

QgsMapLayer *layer() const { return mRef.layer.data(); }
QgsMapLayer *layer() const { return mRef.get(); }

/**
* Returns the layer's name.
@@ -19,10 +19,7 @@
#include <QPointer>

#include "qgsmaplayer.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "raster/qgsrasterlayer.h"
#include "raster/qgsrasterdataprovider.h"
#include "qgsdataprovider.h"

/** Internal structure to keep weak pointer to QgsMapLayer or layerId
* if the layer is not available yet.
@@ -31,7 +28,19 @@
template<typename TYPE>
struct _LayerRef
{
_LayerRef( TYPE *l = nullptr ): layer( l ), layerId( l ? l->id() : QString() ) {}

/**
* Constructor for a layer reference from an existing map layer.
* The layerId, source, name and provider members will automatically
* be populated from this layer.
*/
_LayerRef( TYPE *l = nullptr )
: layer( l )
, layerId( l ? l->id() : QString() )
, source( l ? l->publicSource() : QString() )
, name( l ? l->name() : QString() )
, provider( l && l->dataProvider() ? l->dataProvider()->name() : QString() )
{}

/**
* Constructor for a weak layer reference, using a combination of layer ID,
@@ -45,7 +54,16 @@ struct _LayerRef
, provider( provider )
{}

/**
* Returns a pointer to the layer, or nullptr if the reference has not yet been matched
* to a layer.
*/
TYPE *get() const { return layer.data(); }

//! Weak pointer to map layer
QPointer<TYPE> layer;

//! Original layer ID
QString layerId;

//! Weak reference to layer public source
@@ -27,7 +27,7 @@ class CORE_EXPORT QgsVectorLayerJoinInfo
//! Sets weak reference to the joined layer
void setJoinLayer( QgsVectorLayer *layer ) { mJoinLayerRef = QgsVectorLayerRef( layer ); }
//! Returns joined layer (may be null if the reference was set by layer ID and not resolved yet)
QgsVectorLayer *joinLayer() const { return mJoinLayerRef.layer.data(); }
QgsVectorLayer *joinLayer() const { return mJoinLayerRef.get(); }

//! Sets ID of the joined layer. It will need to be overwritten by setJoinLayer() to a reference to real layer
void setJoinLayerId( const QString &layerId ) { mJoinLayerRef = QgsVectorLayerRef( layerId ); }
@@ -25,6 +25,7 @@
#include <qgsvectorlayer.h>
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include "qgsvectorlayerref.h"

class TestSignalReceiver : public QObject
{
@@ -68,9 +69,11 @@ class TestQgsMapLayer : public QObject
void isInScaleRange_data();
void isInScaleRange();

void layerRef();


private:
QgsMapLayer *mpLayer = nullptr;
QgsVectorLayer *mpLayer = nullptr;
};

void TestQgsMapLayer::initTestCase()
@@ -152,5 +155,32 @@ void TestQgsMapLayer::isInScaleRange()

}

void TestQgsMapLayer::layerRef()
{
// construct from layer
QgsVectorLayerRef ref( mpLayer );
QCOMPARE( ref.get(), mpLayer );
QCOMPARE( ref.layer.data(), mpLayer );
QCOMPARE( ref.layerId, mpLayer->id() );
QCOMPARE( ref.name, QStringLiteral( "points" ) );
QCOMPARE( ref.source, mpLayer->publicSource() );
QCOMPARE( ref.provider, QStringLiteral( "ogr" ) );

// verify that layer matches layer
QVERIFY( ref.layerMatchesSource( mpLayer ) );

// create a weak reference
QgsVectorLayerRef ref2( mpLayer->id(), QStringLiteral( "points" ), mpLayer->publicSource(), QStringLiteral( "ogr" ) );
QVERIFY( !ref2.get() );
QVERIFY( !ref2.layer.data() );
QCOMPARE( ref2.layerId, mpLayer->id() );
QCOMPARE( ref2.name, QStringLiteral( "points" ) );
QCOMPARE( ref2.source, mpLayer->publicSource() );
QCOMPARE( ref2.provider, QStringLiteral( "ogr" ) );

// verify that weak reference matches layer
QVERIFY( ref2.layerMatchesSource( mpLayer ) );
}

QGSTEST_MAIN( TestQgsMapLayer )
#include "testqgsmaplayer.moc"

0 comments on commit 35410fd

Please sign in to comment.