Skip to content
Permalink
Browse files
Avoid styleChanged() signals being raised before new labeling
configuration is set when changing a vector layer's style preset

This causes the layer styling dock to update before the layer's
new labeling settings are set, so the dock shows the incorrect
label settings for the previously used style

Fixes #42310
  • Loading branch information
nyalldawson committed May 22, 2021
1 parent d06f1e6 commit f21ea8c54a4bb71217bb053d6cba1941094cce52
@@ -1611,6 +1611,11 @@ Signal emitted whenever a change affects the layer's style. Ie this may be trigg
by renderer changes, label style changes, or other style changes such as blend
mode or layer opacity changes.

.. warning::

This signal should never be manually emitted. Instead call the :py:func:`~QgsMapLayer.emitStyleChanged` method
to ensure that the signal is only emitted when appropriate.

.. seealso:: :py:func:`rendererChanged`

.. versionadded:: 2.16
@@ -1871,6 +1876,7 @@ this method is now deprecated and always return ``False``, because circular depe




};

QFlags<QgsMapLayer::LayerFlag> operator|(QgsMapLayer::LayerFlag f1, QFlags<QgsMapLayer::LayerFlag> f2);
@@ -644,5 +644,5 @@ void QgsPointCloudLayer::setRenderer( QgsPointCloudRenderer *renderer )

mRenderer.reset( renderer );
emit rendererChanged();
emit styleChanged();
emitStyleChanged();
}
@@ -89,39 +89,6 @@
// canonical project instance
QgsProject *QgsProject::sProject = nullptr;

///@cond PRIVATE
class ScopedIntIncrementor
{
public:

ScopedIntIncrementor( int *variable )
: mVariable( variable )
{
( *mVariable )++;
}

ScopedIntIncrementor( const ScopedIntIncrementor &other ) = delete;
ScopedIntIncrementor &operator=( const ScopedIntIncrementor &other ) = delete;

void release()
{
if ( mVariable )
( *mVariable )--;

mVariable = nullptr;
}

~ScopedIntIncrementor()
{
release();
}

private:
int *mVariable = nullptr;
};
///@endcond


/**
* Takes the given scope and key and convert them to a string list of key
* tokens that will be used to navigate through a Property hierarchy
@@ -821,6 +821,38 @@ void CORE_EXPORT qgsFree( void *ptr ) SIP_SKIP;
#define CONSTLATIN1STRING constexpr QLatin1String
#endif

///@cond PRIVATE
class ScopedIntIncrementor
{
public:

ScopedIntIncrementor( int *variable )
: mVariable( variable )
{
( *mVariable )++;
}

ScopedIntIncrementor( const ScopedIntIncrementor &other ) = delete;
ScopedIntIncrementor &operator=( const ScopedIntIncrementor &other ) = delete;

void release()
{
if ( mVariable )
( *mVariable )--;

mVariable = nullptr;
}

~ScopedIntIncrementor()
{
release();
}

private:
int *mVariable = nullptr;
};
///@endcond

/**
* Wkt string that represents a geographic coord sys
* \since QGIS GEOWkt
@@ -217,7 +217,7 @@ void QgsMapLayer::setBlendMode( const QPainter::CompositionMode blendMode )

mBlendMode = blendMode;
emit blendModeChanged( blendMode );
emit styleChanged();
emitStyleChanged();
}

QPainter::CompositionMode QgsMapLayer::blendMode() const
@@ -231,7 +231,7 @@ void QgsMapLayer::setOpacity( double opacity )
return;
mLayerOpacity = opacity;
emit opacityChanged( opacity );
emit styleChanged();
emitStyleChanged();
}

double QgsMapLayer::opacity() const
@@ -2001,7 +2001,8 @@ QDateTime QgsMapLayer::timestamp() const

void QgsMapLayer::emitStyleChanged()
{
emit styleChanged();
if ( !mBlockStyleChangedSignal )
emit styleChanged();
}

void QgsMapLayer::setExtent( const QgsRectangle &extent )
@@ -1429,6 +1429,10 @@ class CORE_EXPORT QgsMapLayer : public QObject
* Signal emitted whenever a change affects the layer's style. Ie this may be triggered
* by renderer changes, label style changes, or other style changes such as blend
* mode or layer opacity changes.
*
* \warning This signal should never be manually emitted. Instead call the emitStyleChanged() method
* to ensure that the signal is only emitted when appropriate.
*
* \see rendererChanged()
* \since QGIS 2.16
*/
@@ -1724,6 +1728,13 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
double mLayerOpacity = 1.0;

/**
* If non-zero, the styleChanged signal should not be emitted.
*
* \since QGIS 3.20
*/
int mBlockStyleChangedSignal = 0;

#ifndef SIP_RUN

/**
@@ -597,7 +597,7 @@ void QgsRasterLayer::setOpacity( double opacity )

mPipe.renderer()->setOpacity( opacity );
emit opacityChanged( opacity );
emit styleChanged();
emitStyleChanged();
}

double QgsRasterLayer::opacity() const
@@ -957,7 +957,7 @@ void QgsRasterLayer::setDataSourcePrivate( const QString &dataSource, const QStr
{
restoredStyle = true;
emit repaintRequested();
emit styleChanged();
emitStyleChanged();
emit rendererChanged();
}
}
@@ -1219,7 +1219,7 @@ void QgsRasterLayer::setContrastEnhancement( QgsContrastEnhancement::ContrastEnh
if ( rasterRenderer == renderer() )
{
emit repaintRequested();
emit styleChanged();
emitStyleChanged();
emit rendererChanged();
}
}
@@ -1328,7 +1328,7 @@ void QgsRasterLayer::refreshRenderer( QgsRasterRenderer *rasterRenderer, const Q
}

emit repaintRequested();
emit styleChanged();
emitStyleChanged();
emit rendererChanged();
return;
}
@@ -1364,7 +1364,7 @@ void QgsRasterLayer::refreshRenderer( QgsRasterRenderer *rasterRenderer, const Q
}
}

emit styleChanged();
emitStyleChanged();
emit rendererChanged();
}
}
@@ -1741,7 +1741,7 @@ void QgsRasterLayer::setRenderer( QgsRasterRenderer *renderer )

mPipe.set( renderer );
emit rendererChanged();
emit styleChanged();
emitStyleChanged();
}

void QgsRasterLayer::showStatusMessage( QString const &message )
@@ -1667,6 +1667,10 @@ void QgsVectorLayer::setDataSourcePrivate( const QString &dataSource, const QStr

bool defaultLoadedFlag = false;

// defer style changed signal until we've set the renderer, labeling, everything.
// we don't want multiple signals!
ScopedIntIncrementor styleChangedSignalBlocker( &mBlockStyleChangedSignal );

if ( loadDefaultStyleFlag && isSpatial() && mDataProvider->capabilities() & QgsVectorDataProvider::CreateRenderer )
{
// first try to create a renderer directly from the data provider
@@ -1708,6 +1712,9 @@ void QgsVectorLayer::setDataSourcePrivate( const QString &dataSource, const QStr
setLabelsEnabled( true );
}
}

styleChangedSignalBlocker.release();
emitStyleChanged();
}
}

@@ -2444,6 +2451,10 @@ bool QgsVectorLayer::readStyle( const QDomNode &node, QString &errorMessage,
// with broken sources
if ( isSpatial() || mWkbType == QgsWkbTypes::Unknown )
{
// defer style changed signal until we've set the renderer, labeling, everything.
// we don't want multiple signals!
ScopedIntIncrementor styleChangedSignalBlocker( &mBlockStyleChangedSignal );

// try renderer v2 first
if ( categories.testFlag( Symbology ) )
{
@@ -2631,6 +2642,9 @@ bool QgsVectorLayer::readStyle( const QDomNode &node, QString &errorMessage,
}
}
// end diagram

styleChangedSignalBlocker.release();
emitStyleChanged();
}
return result;
}
@@ -2924,10 +2938,17 @@ bool QgsVectorLayer::readSld( const QDomNode &node, QString &errorMessage )
if ( !r )
return false;

// defer style changed signal until we've set the renderer, labeling, everything.
// we don't want multiple signals!
ScopedIntIncrementor styleChangedSignalBlocker( &mBlockStyleChangedSignal );

setRenderer( r );

// labeling
readSldLabeling( node );

styleChangedSignalBlocker.release();
emitStyleChanged();
}
return true;
}
@@ -3715,7 +3736,7 @@ void QgsVectorLayer::setRenderer( QgsFeatureRenderer *r )
mSymbolFeatureIdMap.clear();

emit rendererChanged();
emit styleChanged();
emitStyleChanged();
}
}

@@ -4435,7 +4456,7 @@ void QgsVectorLayer::setFeatureBlendMode( QPainter::CompositionMode featureBlend

mFeatureBlendMode = featureBlendMode;
emit featureBlendModeChanged( featureBlendMode );
emit styleChanged();
emitStyleChanged();
}

QPainter::CompositionMode QgsVectorLayer::featureBlendMode() const

0 comments on commit f21ea8c

Please sign in to comment.