Skip to content

Commit

Permalink
Ensure QgsMapLayer private QObject members are correctly parented
Browse files Browse the repository at this point in the history
to the layer
  • Loading branch information
nyalldawson committed Feb 15, 2018
1 parent 2defaff commit bc23f1d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
45 changes: 19 additions & 26 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -70,12 +70,13 @@ QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
const QString &lyrname,
const QString &source )
: mDataSource( source )
, mLayerName( lyrname )
, mLayerType( type )
, mUndoStack( new QUndoStack( this ) )
, mUndoStackStyles( new QUndoStack( this ) )
, mStyleManager( new QgsMapLayerStyleManager( this ) )
, mRefreshTimer( new QTimer( this ) )
{
// Set the display name = internal name
mLayerName = lyrname;

//mShortName.replace( QRegExp( "[\\W]" ), "_" );

// Generate the unique ID of this layer
Expand All @@ -91,13 +92,8 @@ QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
// there for the compiler, so the pattern is actually \W
mID.replace( QRegExp( "[\\W]" ), QStringLiteral( "_" ) );

//set some generous defaults for scale based visibility
mMinScale = 0;
mMaxScale = 100000000;
mScaleBasedVisibility = false;

connect( mStyleManager, &QgsMapLayerStyleManager::currentStyleChanged, this, &QgsMapLayer::styleChanged );
connect( &mRefreshTimer, &QTimer::timeout, this, [ = ] { triggerRepaint( true ); } );
connect( mRefreshTimer, &QTimer::timeout, this, [ = ] { triggerRepaint( true ); } );
}

QgsMapLayer::~QgsMapLayer()
Expand Down Expand Up @@ -586,8 +582,8 @@ bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &docume
layerElement.appendChild( QgsXmlUtils::writeRectangle( mExtent, document ) );
}

layerElement.setAttribute( QStringLiteral( "autoRefreshTime" ), QString::number( mRefreshTimer.interval() ) );
layerElement.setAttribute( QStringLiteral( "autoRefreshEnabled" ), mRefreshTimer.isActive() ? 1 : 0 );
layerElement.setAttribute( QStringLiteral( "autoRefreshTime" ), QString::number( mRefreshTimer->interval() ) );
layerElement.setAttribute( QStringLiteral( "autoRefreshEnabled" ), mRefreshTimer->isActive() ? 1 : 0 );
layerElement.setAttribute( QStringLiteral( "refreshOnNotifyEnabled" ), mIsRefreshOnNofifyEnabled ? 1 : 0 );
layerElement.setAttribute( QStringLiteral( "refreshOnNotifyMessage" ), mRefreshOnNofifyMessage );

Expand Down Expand Up @@ -913,36 +909,36 @@ bool QgsMapLayer::hasScaleBasedVisibility() const

bool QgsMapLayer::hasAutoRefreshEnabled() const
{
return mRefreshTimer.isActive();
return mRefreshTimer->isActive();
}

int QgsMapLayer::autoRefreshInterval() const
{
return mRefreshTimer.interval();
return mRefreshTimer->interval();
}

void QgsMapLayer::setAutoRefreshInterval( int interval )
{
if ( interval <= 0 )
{
mRefreshTimer.stop();
mRefreshTimer.setInterval( 0 );
mRefreshTimer->stop();
mRefreshTimer->setInterval( 0 );
}
else
{
mRefreshTimer.setInterval( interval );
mRefreshTimer->setInterval( interval );
}
emit autoRefreshIntervalChanged( mRefreshTimer.isActive() ? mRefreshTimer.interval() : 0 );
emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
}

void QgsMapLayer::setAutoRefreshEnabled( bool enabled )
{
if ( !enabled )
mRefreshTimer.stop();
else if ( mRefreshTimer.interval() > 0 )
mRefreshTimer.start();
mRefreshTimer->stop();
else if ( mRefreshTimer->interval() > 0 )
mRefreshTimer->start();

emit autoRefreshIntervalChanged( mRefreshTimer.isActive() ? mRefreshTimer.interval() : 0 );
emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
}

const QgsLayerMetadata &QgsMapLayer::metadata() const
Expand Down Expand Up @@ -1824,7 +1820,6 @@ void QgsMapLayer::writeCommonStyle( QDomElement &layerElement, QDomDocument &doc
}
}


void QgsMapLayer::readCommonStyle( const QDomElement &layerElement, const QgsReadWriteContext &context )
{
QgsAbstract3DRenderer *r3D = nullptr;
Expand All @@ -1841,18 +1836,16 @@ void QgsMapLayer::readCommonStyle( const QDomElement &layerElement, const QgsRea
setRenderer3D( r3D );
}


QUndoStack *QgsMapLayer::undoStack()
{
return &mUndoStack;
return mUndoStack;
}

QUndoStack *QgsMapLayer::undoStackStyles()
{
return &mUndoStackStyles;
return mUndoStackStyles;
}


QStringList QgsMapLayer::customPropertyKeys() const
{
return mCustomProperties.keys();
Expand Down
14 changes: 8 additions & 6 deletions src/core/qgsmaplayer.h
Expand Up @@ -1304,17 +1304,19 @@ class CORE_EXPORT QgsMapLayer : public QObject
//! Tag for embedding additional information
QString mTag;

//set some generous defaults for scale based visibility

//! Minimum scale denominator at which this layer should be displayed
double mMinScale;
double mMinScale = 0;
//! Maximum scale denominator at which this layer should be displayed
double mMaxScale;
double mMaxScale = 100000000;
//! A flag that tells us whether to use the above vars to restrict layer visibility
bool mScaleBasedVisibility;
bool mScaleBasedVisibility = false;

//! Collection of undoable operations for this layer. *
QUndoStack mUndoStack;
QUndoStack *mUndoStack = nullptr;

QUndoStack mUndoStackStyles;
QUndoStack *mUndoStackStyles = nullptr;

//! Layer's persistent storage of additional properties (may be used by plugins)
QgsObjectCustomProperties mCustomProperties;
Expand All @@ -1326,7 +1328,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
QgsMapLayerStyleManager *mStyleManager = nullptr;

//! Timer for triggering automatic refreshes of the layer
QTimer mRefreshTimer;
QTimer *mRefreshTimer = nullptr;

QgsLayerMetadata mMetadata;

Expand Down

0 comments on commit bc23f1d

Please sign in to comment.