Some API improvements to QgsMapLayerRef, unit tests

This commit is contained in:
Nyall Dawson 2017-04-18 13:21:42 +10:00
parent 6bb3fa61c5
commit 35410fd4b7
4 changed files with 56 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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