Skip to content

Commit

Permalink
Add typedefs for QPointer QgsMapLayers
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 1, 2017
1 parent f720106 commit 6c928ef
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/core/annotations/qgsannotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class CORE_EXPORT QgsAnnotation : public QObject
QPointF mBalloonSegmentPoint2;

//! Associated layer (or nullptr if not attached to a layer)
QPointer<QgsMapLayer> mMapLayer;
QgsWeakMapLayerPointer mMapLayer;

//! Associated feature, or invalid feature if no feature associated
QgsFeature mFeature;
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ bool QgsComposerMap::writeXml( QDomElement& elem, QDomDocument & doc ) const

//layer set
QDomElement layerSetElem = doc.createElement( QStringLiteral( "LayerSet" ) );
Q_FOREACH ( const QPointer<QgsMapLayer>& layerPtr, mLayers )
Q_FOREACH ( const QgsWeakMapLayerPointer& layerPtr, mLayers )
{
QgsMapLayer* layer = layerPtr.data();
if ( !layer )
Expand Down Expand Up @@ -1557,7 +1557,7 @@ void QgsComposerMap::setLayerStyleOverrides( const QMap<QString, QString>& overr
void QgsComposerMap::storeCurrentLayerStyles()
{
mLayerStyleOverrides.clear();
Q_FOREACH ( const QPointer<QgsMapLayer>& layerPtr, mLayers )
Q_FOREACH ( const QgsWeakMapLayerPointer& layerPtr, mLayers )
{
if ( QgsMapLayer* layer = layerPtr.data() )
{
Expand Down
3 changes: 2 additions & 1 deletion src/core/composer/qgscomposermap.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "qgsrectangle.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsrendercontext.h"
#include "qgsmaplayer.h"
#include <QFont>
#include <QGraphicsRectItem>

Expand Down Expand Up @@ -542,7 +543,7 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
bool mKeepLayerSet;

//! Stored layer list (used if layer live-link mKeepLayerSet is disabled)
QList< QPointer<QgsMapLayer> > mLayers;
QgsWeakMapLayerPointerList mLayers;

bool mKeepLayerStyles;
//! Stored style names (value) to be used with particular layer IDs (key) instead of default style
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ static QgsExpression::Node* getNode( const QVariant& value, QgsExpression* paren

QgsVectorLayer* getVectorLayer( const QVariant& value, QgsExpression* )
{
QgsMapLayer* ml = value.value< QPointer<QgsMapLayer> >().data();
QgsMapLayer* ml = value.value< QgsWeakMapLayerPointer >().data();
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( ml );
if ( !vl )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsexpressioncontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ QgsExpressionContextScope* QgsExpressionContextUtils::layerScope( const QgsMapLa

scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_name" ), layer->name(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer_id" ), layer->id(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer" ), QVariant::fromValue<QPointer<QgsMapLayer> >( QPointer<QgsMapLayer>( const_cast<QgsMapLayer*>( layer ) ) ), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "layer" ), QVariant::fromValue<QgsWeakMapLayerPointer >( QgsWeakMapLayerPointer( const_cast<QgsMapLayer*>( layer ) ) ), true ) );

const QgsVectorLayer* vLayer = dynamic_cast< const QgsVectorLayer* >( layer );
if ( vLayer )
Expand Down
14 changes: 14 additions & 0 deletions src/core/qgsmaplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -920,4 +920,18 @@ class CORE_EXPORT QgsMapLayer : public QObject

Q_DECLARE_METATYPE( QgsMapLayer* )

/**
* Weak pointer for QgsMapLayer
* @note added in QGIS 3.0
* @note not available in Python bindings
*/
typedef QPointer< QgsMapLayer > QgsWeakMapLayerPointer;

/**
* A list of weak pointers to QgsMapLayers.
* @note added in QGIS 3.0
* @note not available in Python bindings
*/
typedef QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList;

#endif
14 changes: 7 additions & 7 deletions src/core/qgsmaplayerlistutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@

/// @cond PRIVATE

inline QList<QgsMapLayer*> _qgis_listQPointerToRaw( const QList< QPointer<QgsMapLayer> >& layers )
inline QList<QgsMapLayer*> _qgis_listQPointerToRaw( const QgsWeakMapLayerPointerList& layers )
{
QList<QgsMapLayer*> lst;
lst.reserve( layers.count() );
Q_FOREACH ( const QPointer<QgsMapLayer>& layerPtr, layers )
Q_FOREACH ( const QgsWeakMapLayerPointer& layerPtr, layers )
{
if ( layerPtr )
lst.append( layerPtr.data() );
}
return lst;
}

inline QList< QPointer<QgsMapLayer> > _qgis_listRawToQPointer( const QList<QgsMapLayer*>& layers )
inline QgsWeakMapLayerPointerList _qgis_listRawToQPointer( const QList<QgsMapLayer*>& layers )
{
QList< QPointer<QgsMapLayer> > lst;
QgsWeakMapLayerPointerList lst;
lst.reserve( layers.count() );
Q_FOREACH ( QgsMapLayer* layer, layers )
{
Expand All @@ -39,11 +39,11 @@ inline QList< QPointer<QgsMapLayer> > _qgis_listRawToQPointer( const QList<QgsMa
return lst;
}

inline QStringList _qgis_listQPointerToIDs( const QList< QPointer<QgsMapLayer> >& layers )
inline QStringList _qgis_listQPointerToIDs( const QgsWeakMapLayerPointerList& layers )
{
QStringList lst;
lst.reserve( layers.count() );
Q_FOREACH ( const QPointer<QgsMapLayer>& layerPtr, layers )
Q_FOREACH ( const QgsWeakMapLayerPointer& layerPtr, layers )
{
if ( layerPtr )
lst << layerPtr->id();
Expand Down Expand Up @@ -93,7 +93,7 @@ inline static QgsMapLayer* _qgis_findLayer( const QList< QgsMapLayer*> layers, c
}
}

inline uint qHash( const QPointer< QgsMapLayer >& key )
inline uint qHash( const QgsWeakMapLayerPointer& key )
{
return qHash( key ? key->id() : QString() );
}
Expand Down
16 changes: 8 additions & 8 deletions src/core/qgsmaprenderercache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void QgsMapRendererCache::clearInternal()
mScale = 0;

// make sure we are disconnected from all layers
Q_FOREACH ( const QPointer< QgsMapLayer >& layer, mConnectedLayers )
Q_FOREACH ( const QgsWeakMapLayerPointer& layer, mConnectedLayers )
{
if ( layer.data() )
{
Expand All @@ -48,9 +48,9 @@ void QgsMapRendererCache::clearInternal()

void QgsMapRendererCache::dropUnusedConnections()
{
QSet< QPointer< QgsMapLayer > > stillDepends = dependentLayers();
QSet< QPointer< QgsMapLayer > > disconnects = mConnectedLayers.subtract( stillDepends );
Q_FOREACH ( const QPointer< QgsMapLayer >& layer, disconnects )
QSet< QgsWeakMapLayerPointer > stillDepends = dependentLayers();
QSet< QgsWeakMapLayerPointer > disconnects = mConnectedLayers.subtract( stillDepends );
Q_FOREACH ( const QgsWeakMapLayerPointer& layer, disconnects )
{
if ( layer.data() )
{
Expand All @@ -61,13 +61,13 @@ void QgsMapRendererCache::dropUnusedConnections()
mConnectedLayers = stillDepends;
}

QSet<QPointer<QgsMapLayer> > QgsMapRendererCache::dependentLayers() const
QSet<QgsWeakMapLayerPointer > QgsMapRendererCache::dependentLayers() const
{
QSet< QPointer< QgsMapLayer > > result;
QSet< QgsWeakMapLayerPointer > result;
QMap<QString, CacheParameters>::const_iterator it = mCachedImages.constBegin();
for ( ; it != mCachedImages.constEnd(); ++it )
{
Q_FOREACH ( const QPointer< QgsMapLayer >& l, it.value().dependentLayers )
Q_FOREACH ( const QgsWeakMapLayerPointer& l, it.value().dependentLayers )
{
if ( l.data() )
result << l;
Expand Down Expand Up @@ -107,7 +107,7 @@ void QgsMapRendererCache::setCacheImage( const QString& cacheKey, const QImage&
if ( layer )
{
params.dependentLayers << layer;
if ( !mConnectedLayers.contains( QPointer< QgsMapLayer >( layer ) ) )
if ( !mConnectedLayers.contains( QgsWeakMapLayerPointer( layer ) ) )
{
connect( layer, &QgsMapLayer::repaintRequested, this, &QgsMapRendererCache::layerRequestedRepaint );
mConnectedLayers << layer;
Expand Down
6 changes: 3 additions & 3 deletions src/core/qgsmaprenderercache.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class CORE_EXPORT QgsMapRendererCache : public QObject
struct CacheParameters
{
QImage cachedImage;
QList< QPointer< QgsMapLayer > > dependentLayers;
QgsWeakMapLayerPointerList dependentLayers;
};

//! Invalidate cache contents (without locking)
Expand All @@ -108,7 +108,7 @@ class CORE_EXPORT QgsMapRendererCache : public QObject
//! Disconnects from layers we no longer care about
void dropUnusedConnections();

QSet< QPointer< QgsMapLayer > > dependentLayers() const;
QSet< QgsWeakMapLayerPointer > dependentLayers() const;

mutable QMutex mMutex;
QgsRectangle mExtent;
Expand All @@ -117,7 +117,7 @@ class CORE_EXPORT QgsMapRendererCache : public QObject
//! Map of cache key to cache parameters
QMap<QString, CacheParameters> mCachedImages;
//! List of all layers on which this cache is currently connected
QSet< QPointer< QgsMapLayer > > mConnectedLayers;
QSet< QgsWeakMapLayerPointer > mConnectedLayers;
};


Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmaprendererjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct LayerRenderJob
QPainter::CompositionMode blendMode;
double opacity;
bool cached; // if true, img already contains cached image from previous rendering
QPointer< QgsMapLayer > layer;
QgsWeakMapLayerPointer layer;
int renderingTime; //!< Time it took to render the layer in ms (it is -1 if not rendered or still rendering)
};

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmapsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ QgsRectangle QgsMapSettings::fullExtent() const
// iterate through the map layers and test each layers extent
// against the current min and max values
QgsDebugMsg( QString( "Layer count: %1" ).arg( mLayers.count() ) );
Q_FOREACH ( const QPointer<QgsMapLayer>& layerPtr, mLayers )
Q_FOREACH ( const QgsWeakMapLayerPointer& layerPtr, mLayers )
{
if ( QgsMapLayer* lyr = layerPtr.data() )
{
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsmapsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
#include "qgsrectangle.h"
#include "qgsscalecalculator.h"
#include "qgsexpressioncontext.h"
#include "qgsmaplayer.h"

class QPainter;

class QgsCoordinateTransform;
class QgsScaleCalculator;
class QgsMapRendererJob;
class QgsMapLayer;


/** \ingroup core
Expand Down Expand Up @@ -310,7 +310,7 @@ class CORE_EXPORT QgsMapSettings
double mMagnificationFactor;

//! list of layers to be rendered (stored as weak pointers)
QList< QPointer<QgsMapLayer> > mLayers;
QgsWeakMapLayerPointerList mLayers;
QMap<QString, QString> mLayerStyleOverrides;
QString mCustomRenderFlags;
QgsExpressionContext mExpressionContext;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmapthemecollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class CORE_EXPORT QgsMapThemeCollection : public QObject
QSet<QString> checkedLegendItems;
private:
//! Weak pointer to the layer
QPointer<QgsMapLayer> mLayer;
QgsWeakMapLayerPointer mLayer;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsmapsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void TestQgsMapSettings::testMapLayerListUtils()
l = _qgis_findLayer( listRawSource, QStringLiteral( "z" ) );
QCOMPARE( !l, true );

QList< QPointer<QgsMapLayer> > listQPointer = _qgis_listRawToQPointer( listRawSource );
QgsWeakMapLayerPointerList listQPointer = _qgis_listRawToQPointer( listRawSource );

QCOMPARE( listQPointer.count(), 2 );
QCOMPARE( listQPointer[0].data(), vlA );
Expand Down

0 comments on commit 6c928ef

Please sign in to comment.