Skip to content
Permalink
Browse files

Move getters and setters for layer opacity from QgsVectorLayer to Qgs…

…MapLayer

All the logic for rendering non-opaque layers is handled in a layer
type agnostic way already, so this just allows the existing handling
to be used for mesh/point cloud/vector tile/etc layers also
  • Loading branch information
nyalldawson committed Nov 13, 2020
1 parent 8c06a1a commit f3648ba3f6098adbf5001a720892addbd658e940
@@ -91,22 +91,6 @@ Returns a map of items contained in the layer, by unique item ID.

This map contains references to items owned by the layer, and ownership of these remains
with the layer.
%End

void setOpacity( double opacity );
%Docstring
Sets the ``opacity`` for the annotation layer, where ``opacity`` is a value between 0 (totally transparent)
and 1.0 (fully opaque).

.. seealso:: :py:func:`opacity`
%End

double opacity() const;
%Docstring
Returns the opacity for the annotation layer, where opacity is a value between 0 (totally transparent)
and 1.0 (fully opaque).

.. seealso:: :py:func:`setOpacity`
%End

virtual QgsAnnotationLayer *clone() const /Factory/;
@@ -448,6 +448,38 @@ Set the blending mode used for rendering a layer.
Returns the current blending mode for a layer.

.. seealso:: :py:func:`setBlendMode`
%End

void setOpacity( double opacity );
%Docstring
Sets the ``opacity`` for the layer, where ``opacity`` is a value between 0 (totally transparent)
and 1.0 (fully opaque).

.. seealso:: :py:func:`opacity`

.. seealso:: :py:func:`opacityChanged`

.. note::

Prior to QGIS 3.18, this method was available for vector layers only

.. versionadded:: 3.18
%End

double opacity() const;
%Docstring
Returns the opacity for the layer, where opacity is a value between 0 (totally transparent)
and 1.0 (fully opaque).

.. seealso:: :py:func:`setOpacity`

.. seealso:: :py:func:`opacityChanged`

.. note::

Prior to QGIS 3.18, this method was available for vector layers only

.. versionadded:: 3.18
%End

bool readOnly() const;
@@ -1488,6 +1520,22 @@ Data of layer changed
void blendModeChanged( QPainter::CompositionMode blendMode );
%Docstring
Signal emitted when the blend mode is changed, through :py:func:`QgsMapLayer.setBlendMode()`
%End

void opacityChanged( double opacity );
%Docstring
Emitted when the layer's opacity is changed, where ``opacity`` is a value between 0 (transparent)
and 1 (opaque).

.. seealso:: :py:func:`setOpacity`

.. seealso:: :py:func:`opacity`

.. note::

Prior to QGIS 3.18, this signal was available for vector layers only

.. versionadded:: 3.18
%End

void rendererChanged();
@@ -1746,6 +1794,7 @@ this method is now deprecated and always return ``False``, because circular depe




};

QFlags<QgsMapLayer::LayerFlag> operator|(QgsMapLayer::LayerFlag f1, QFlags<QgsMapLayer::LayerFlag> f2);
@@ -2406,30 +2406,6 @@ Sets the blending mode used for rendering each feature
QPainter::CompositionMode featureBlendMode() const;
%Docstring
Returns the current blending mode for features
%End

void setOpacity( double opacity );
%Docstring
Sets the ``opacity`` for the vector layer, where ``opacity`` is a value between 0 (totally transparent)
and 1.0 (fully opaque).

.. seealso:: :py:func:`opacity`

.. seealso:: :py:func:`opacityChanged`

.. versionadded:: 3.0
%End

double opacity() const;
%Docstring
Returns the opacity for the vector layer, where opacity is a value between 0 (totally transparent)
and 1.0 (fully opaque).

.. seealso:: :py:func:`setOpacity`

.. seealso:: :py:func:`opacityChanged`

.. versionadded:: 3.0
%End

virtual QString htmlMetadata() const ${SIP_FINAL};
@@ -2862,18 +2838,6 @@ Emitted when the font family defined for labeling layer is not found on system
void featureBlendModeChanged( QPainter::CompositionMode blendMode );
%Docstring
Signal emitted when :py:func:`~QgsVectorLayer.setFeatureBlendMode` is called
%End

void opacityChanged( double opacity );
%Docstring
Emitted when the layer's opacity is changed, where ``opacity`` is a value between 0 (transparent)
and 1 (opaque).

.. seealso:: :py:func:`setOpacity`

.. seealso:: :py:func:`opacity`

.. versionadded:: 3.0
%End

void editCommandStarted( const QString &text );
@@ -38,7 +38,7 @@ QgsAnnotationLayer::~QgsAnnotationLayer()

void QgsAnnotationLayer::reset()
{
mOpacity = 1.0;
setOpacity( 1.0 );
setCrs( QgsCoordinateReferenceSystem() );
setTransformContext( QgsCoordinateTransformContext() );
clear();
@@ -79,20 +79,12 @@ bool QgsAnnotationLayer::isEmpty() const
return mItems.empty();
}

void QgsAnnotationLayer::setOpacity( double opacity )
{
mOpacity = opacity;
triggerRepaint();
}

QgsAnnotationLayer *QgsAnnotationLayer::clone() const
{
QgsAnnotationLayer::LayerOptions options( mTransformContext );
std::unique_ptr< QgsAnnotationLayer > layer = qgis::make_unique< QgsAnnotationLayer >( name(), options );
QgsMapLayer::clone( layer.get() );

layer->setOpacity( opacity() );

for ( auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
{
layer->mItems.insert( it.key(), ( *it )->clone() );
@@ -117,20 +117,6 @@ class CORE_EXPORT QgsAnnotationLayer : public QgsMapLayer
*/
QMap<QString, QgsAnnotationItem *> items() const { return mItems; }

/**
* Sets the \a opacity for the annotation layer, where \a opacity is a value between 0 (totally transparent)
* and 1.0 (fully opaque).
* \see opacity()
*/
void setOpacity( double opacity );

/**
* Returns the opacity for the annotation layer, where opacity is a value between 0 (totally transparent)
* and 1.0 (fully opaque).
* \see setOpacity()
*/
double opacity() const { return mOpacity; }

QgsAnnotationLayer *clone() const override SIP_FACTORY;
QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) override SIP_FACTORY;
QgsRectangle extent() const override;
@@ -142,7 +128,6 @@ class CORE_EXPORT QgsAnnotationLayer : public QgsMapLayer

private:
QMap<QString, QgsAnnotationItem *> mItems;
double mOpacity = 1;
QgsCoordinateTransformContext mTransformContext;
};

@@ -623,8 +623,8 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC
double dotsPerMM = context->scaleFactor();

int opacity = 255;
if ( QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layerNode()->layer() ) )
opacity = static_cast<int >( std::round( 255 * vectorLayer->opacity() ) );
if ( QgsMapLayer *layer = layerNode()->layer() )
opacity = static_cast<int >( std::round( 255 * layer->opacity() ) );

QgsScopedQPainterState painterState( p );
context->setPainterFlagsUsingContext( p );
@@ -718,8 +718,8 @@ QJsonObject QgsSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &se
QImage img( pix.toImage().convertToFormat( QImage::Format_ARGB32_Premultiplied ) );

int opacity = 255;
if ( QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layerNode()->layer() ) )
opacity = ( 255 * vectorLayer->opacity() );
if ( QgsMapLayer *layer = layerNode()->layer() )
opacity = ( 255 * layer->opacity() );

if ( opacity != 255 )
{
@@ -124,6 +124,7 @@ void QgsMapLayer::clone( QgsMapLayer *layer ) const
layer->mShouldValidateCrs = mShouldValidateCrs;
layer->setCrs( crs() );
layer->setCustomProperties( mCustomProperties );
layer->setOpacity( mLayerOpacity );
}

QgsMapLayerType QgsMapLayer::type() const
@@ -214,6 +215,19 @@ QPainter::CompositionMode QgsMapLayer::blendMode() const
return mBlendMode;
}

void QgsMapLayer::setOpacity( double opacity )
{
if ( qgsDoubleNear( mLayerOpacity, opacity ) )
return;
mLayerOpacity = opacity;
emit opacityChanged( opacity );
emit styleChanged();
}

double QgsMapLayer::opacity() const
{
return mLayerOpacity;
}

bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteContext &context, QgsMapLayer::ReadFlags flags )
{
@@ -90,6 +90,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs NOTIFY crsChanged )
Q_PROPERTY( QgsMapLayerType type READ type CONSTANT )
Q_PROPERTY( bool isValid READ isValid NOTIFY isValidChanged )
Q_PROPERTY( double opacity READ opacity WRITE setOpacity NOTIFY opacityChanged )

#ifdef SIP_RUN
SIP_CONVERT_TO_SUBCLASS_CODE
@@ -474,6 +475,26 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
QPainter::CompositionMode blendMode() const;

/**
* Sets the \a opacity for the layer, where \a opacity is a value between 0 (totally transparent)
* and 1.0 (fully opaque).
* \see opacity()
* \see opacityChanged()
* \note Prior to QGIS 3.18, this method was available for vector layers only
* \since QGIS 3.18
*/
void setOpacity( double opacity );

/**
* Returns the opacity for the layer, where opacity is a value between 0 (totally transparent)
* and 1.0 (fully opaque).
* \see setOpacity()
* \see opacityChanged()
* \note Prior to QGIS 3.18, this method was available for vector layers only
* \since QGIS 3.18
*/
double opacity() const;

//! Returns if this layer is read only.
bool readOnly() const { return isReadOnly(); }

@@ -1337,6 +1358,16 @@ class CORE_EXPORT QgsMapLayer : public QObject
//! Signal emitted when the blend mode is changed, through QgsMapLayer::setBlendMode()
void blendModeChanged( QPainter::CompositionMode blendMode );

/**
* Emitted when the layer's opacity is changed, where \a opacity is a value between 0 (transparent)
* and 1 (opaque).
* \see setOpacity()
* \see opacity()
* \note Prior to QGIS 3.18, this signal was available for vector layers only
* \since QGIS 3.18
*/
void opacityChanged( double opacity );

/**
* Signal emitted when renderer is changed.
* \see styleChanged()
@@ -1606,6 +1637,13 @@ class CORE_EXPORT QgsMapLayer : public QObject
*/
bool mShouldValidateCrs = true;

/**
* Layer opacity.
*
* \since QGIS 3.18
*/
double mLayerOpacity = 1.0;

private:

virtual QString baseURI( PropertyType type ) const;
@@ -78,7 +78,7 @@ QHash<QgsMapLayer *, int> QgsMapRendererJob::perLayerRenderingTime() const
QHash<QgsMapLayer *, int> result;
for ( auto it = mPerLayerRenderingTime.constBegin(); it != mPerLayerRenderingTime.constEnd(); ++it )
{
if ( auto &&lKey = it.key() )
if ( auto && lKey = it.key() )
result.insert( lKey, it.value() );
}
return result;
@@ -395,15 +395,7 @@ LayerRenderJobs QgsMapRendererJob::prepareJobs( QPainter *painter, QgsLabelingEn
styleOverride.setOverrideStyle( mSettings.layerStyleOverrides().value( ml->id() ) );

job.blendMode = ml->blendMode();
job.opacity = 1.0;
if ( vl )
{
job.opacity = vl->opacity();
}
else if ( QgsAnnotationLayer *al = qobject_cast<QgsAnnotationLayer *>( ml ) )
{
job.opacity = al->opacity();
}
job.opacity = ml->opacity();

// if we can use the cache, let's do it and avoid rendering!
if ( mCache && mCache->hasCacheImage( ml->id() ) )
@@ -964,13 +956,10 @@ bool QgsMapRendererJob::needTemporaryImage( QgsMapLayer *ml )
case QgsMapLayerType::PointCloudLayer:
case QgsMapLayerType::VectorTileLayer:
case QgsMapLayerType::PluginLayer:
break;

case QgsMapLayerType::AnnotationLayer:
{
QgsAnnotationLayer *al = qobject_cast<QgsAnnotationLayer *>( ml );
if ( mSettings.testFlag( QgsMapSettings::UseAdvancedEffects ) &&
( !qgsDoubleNear( al->opacity(), 1.0 ) ) )
( !qgsDoubleNear( ml->opacity(), 1.0 ) ) )
{
//layer properties require rasterization
return true;
@@ -276,7 +276,6 @@ QgsVectorLayer *QgsVectorLayer::clone() const
layer->selectByIds( selectedFeatureIds() );
layer->setAttributeTableConfig( attributeTableConfig() );
layer->setFeatureBlendMode( featureBlendMode() );
layer->setOpacity( opacity() );
layer->setReadExtentFromXml( readExtentFromXml() );

const auto constActions = actions()->actions();
@@ -4342,22 +4341,6 @@ QPainter::CompositionMode QgsVectorLayer::featureBlendMode() const
return mFeatureBlendMode;
}

void QgsVectorLayer::setOpacity( double opacity )
{
if ( qgsDoubleNear( mLayerOpacity, opacity ) )
return;
mLayerOpacity = opacity;
emit opacityChanged( opacity );
emit styleChanged();
}

double QgsVectorLayer::opacity() const
{
return mLayerOpacity;
}



void QgsVectorLayer::readSldLabeling( const QDomNode &node )
{
setLabeling( nullptr ); // start with no labeling

0 comments on commit f3648ba

Please sign in to comment.
You can’t perform that action at this time.