mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-28 00:17:30 -05:00
Some API improvements to QgsMapLayerRef, unit tests
This commit is contained in:
parent
6bb3fa61c5
commit
35410fd4b7
@ -54,7 +54,7 @@ class CORE_EXPORT QgsLayerTreeLayer : public QgsLayerTreeNode
|
|||||||
|
|
||||||
QString layerId() const { return mRef.layerId; }
|
QString layerId() const { return mRef.layerId; }
|
||||||
|
|
||||||
QgsMapLayer *layer() const { return mRef.layer.data(); }
|
QgsMapLayer *layer() const { return mRef.get(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the layer's name.
|
* Returns the layer's name.
|
||||||
|
@ -19,10 +19,7 @@
|
|||||||
#include <QPointer>
|
#include <QPointer>
|
||||||
|
|
||||||
#include "qgsmaplayer.h"
|
#include "qgsmaplayer.h"
|
||||||
#include "qgsvectorlayer.h"
|
#include "qgsdataprovider.h"
|
||||||
#include "qgsvectordataprovider.h"
|
|
||||||
#include "raster/qgsrasterlayer.h"
|
|
||||||
#include "raster/qgsrasterdataprovider.h"
|
|
||||||
|
|
||||||
/** Internal structure to keep weak pointer to QgsMapLayer or layerId
|
/** Internal structure to keep weak pointer to QgsMapLayer or layerId
|
||||||
* if the layer is not available yet.
|
* if the layer is not available yet.
|
||||||
@ -31,7 +28,19 @@
|
|||||||
template<typename TYPE>
|
template<typename TYPE>
|
||||||
struct _LayerRef
|
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,
|
* Constructor for a weak layer reference, using a combination of layer ID,
|
||||||
@ -45,7 +54,16 @@ struct _LayerRef
|
|||||||
, provider( provider )
|
, 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;
|
QPointer<TYPE> layer;
|
||||||
|
|
||||||
|
//! Original layer ID
|
||||||
QString layerId;
|
QString layerId;
|
||||||
|
|
||||||
//! Weak reference to layer public source
|
//! Weak reference to layer public source
|
||||||
|
@ -27,7 +27,7 @@ class CORE_EXPORT QgsVectorLayerJoinInfo
|
|||||||
//! Sets weak reference to the joined layer
|
//! Sets weak reference to the joined layer
|
||||||
void setJoinLayer( QgsVectorLayer *layer ) { mJoinLayerRef = QgsVectorLayerRef( 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)
|
//! 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
|
//! 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 ); }
|
void setJoinLayerId( const QString &layerId ) { mJoinLayerRef = QgsVectorLayerRef( layerId ); }
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#include <qgsvectorlayer.h>
|
#include <qgsvectorlayer.h>
|
||||||
#include <qgsapplication.h>
|
#include <qgsapplication.h>
|
||||||
#include <qgsproviderregistry.h>
|
#include <qgsproviderregistry.h>
|
||||||
|
#include "qgsvectorlayerref.h"
|
||||||
|
|
||||||
class TestSignalReceiver : public QObject
|
class TestSignalReceiver : public QObject
|
||||||
{
|
{
|
||||||
@ -68,9 +69,11 @@ class TestQgsMapLayer : public QObject
|
|||||||
void isInScaleRange_data();
|
void isInScaleRange_data();
|
||||||
void isInScaleRange();
|
void isInScaleRange();
|
||||||
|
|
||||||
|
void layerRef();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QgsMapLayer *mpLayer = nullptr;
|
QgsVectorLayer *mpLayer = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
void TestQgsMapLayer::initTestCase()
|
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 )
|
QGSTEST_MAIN( TestQgsMapLayer )
|
||||||
#include "testqgsmaplayer.moc"
|
#include "testqgsmaplayer.moc"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user