Skip to content
Permalink
Browse files

Create a QgsLayerStyleOverride and use it for the job renderer

  • Loading branch information
nirvn committed Jun 11, 2018
1 parent 5ba1e91 commit 5c267f22fbeeb0928fb2bf595556c16581f6512b
@@ -11,7 +11,6 @@




class QgsMapLayerStyle
{
%Docstring
@@ -224,6 +223,35 @@ Emitted when the current style has been changed

};


class QgsLayerStyleOverride
{
%Docstring
Restore overridden layer style on destruction.

.. versionadded:: 3.2
%End

%TypeHeaderCode
#include "qgsmaplayerstylemanager.h"
%End
public:

QgsLayerStyleOverride( QgsMapLayer *layer );
%Docstring
Construct a style override object associated with a map layer.
The overridden style will be restored upon object destruction.
%End

~QgsLayerStyleOverride();

void setOverrideStyle( const QString &style );
%Docstring
Temporarily apply a different style to the layer. The argument
can be either a style name or a full QML style definition.
%End

};
/************************************************************************
* This file has been generated automatically from *
* *
@@ -16,7 +16,6 @@
#include "qgsmaplayerstylemanager.h"

#include "qgslogger.h"
#include "qgsmaplayer.h"

#include <QDomElement>
#include <QTextStream>
@@ -16,16 +16,15 @@
#ifndef QGSMAPLAYERSTYLEMANAGER_H
#define QGSMAPLAYERSTYLEMANAGER_H


class QgsMapLayer;
#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgsmaplayer.h"

#include <QByteArray>
#include <QMap>
#include <QStringList>
#include <QObject>

#include "qgis_core.h"
#include "qgis_sip.h"

class QDomElement;

@@ -195,4 +194,47 @@ class CORE_EXPORT QgsMapLayerStyleManager : public QObject
QString defaultStyleName() const;
};


/**
* \ingroup core
* Restore overridden layer style on destruction.
*
* \since QGIS 3.2
*/
class CORE_EXPORT QgsLayerStyleOverride
{
public:

/**
* Construct a style override object associated with a map layer.
* The overridden style will be restored upon object destruction.
*/
QgsLayerStyleOverride( QgsMapLayer *layer )
: mLayer( layer )
{
}

~QgsLayerStyleOverride()
{
if ( mLayer && mStyleOverridden )
mLayer->styleManager()->restoreOverrideStyle();
}

/**
* Temporarily apply a different style to the layer. The argument
* can be either a style name or a full QML style definition.
*/
void setOverrideStyle( const QString &style )
{
if ( mLayer && mStyleOverridden )
mLayer->styleManager()->restoreOverrideStyle();
mLayer->styleManager()->setOverrideStyle( style );
mStyleOverridden = true;
}

private:

QgsMapLayer *mLayer = nullptr;
bool mStyleOverridden = false;
};
#endif // QGSMAPLAYERSTYLEMANAGER_H
@@ -216,8 +216,6 @@ bool QgsMapRendererJob::reprojectToLayerExtent( const QgsMapLayer *ml, const Qgs
return split;
}



LayerRenderJobs QgsMapRendererJob::prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2 )
{
LayerRenderJobs layerJobs;
@@ -298,9 +296,9 @@ LayerRenderJobs QgsMapRendererJob::prepareJobs( QPainter *painter, QgsLabelingEn
if ( mFeatureFilterProvider )
job.context.setFeatureFilterProvider( mFeatureFilterProvider );

bool hasStyleOverride = mSettings.layerStyleOverrides().contains( ml->id() );
if ( hasStyleOverride )
ml->styleManager()->setOverrideStyle( mSettings.layerStyleOverrides().value( ml->id() ) );
QgsLayerStyleOverride styleOverride( ml );
if ( mSettings.layerStyleOverrides().contains( ml->id() ) )
styleOverride.setOverrideStyle( mSettings.layerStyleOverrides().value( ml->id() ) );

job.blendMode = ml->blendMode();
job.opacity = 1.0;
@@ -312,9 +310,6 @@ LayerRenderJobs QgsMapRendererJob::prepareJobs( QPainter *painter, QgsLabelingEn
// if we can use the cache, let's do it and avoid rendering!
if ( mCache && mCache->hasCacheImage( ml->id() ) )
{
if ( hasStyleOverride )
ml->styleManager()->restoreOverrideStyle();

job.cached = true;
job.imageInitialized = true;
job.img = new QImage( mCache->cacheImage( ml->id() ) );
@@ -335,9 +330,6 @@ LayerRenderJobs QgsMapRendererJob::prepareJobs( QPainter *painter, QgsLabelingEn
mSettings.outputImageFormat() );
if ( mypFlattenedImage->isNull() )
{
if ( hasStyleOverride )
ml->styleManager()->restoreOverrideStyle();

mErrors.append( Error( ml->id(), tr( "Insufficient memory for image %1x%2" ).arg( mSettings.outputSize().width() ).arg( mSettings.outputSize().height() ) ) );
delete mypFlattenedImage;
layerJobs.removeLast();
@@ -354,9 +346,6 @@ LayerRenderJobs QgsMapRendererJob::prepareJobs( QPainter *painter, QgsLabelingEn
layerTime.start();
job.renderer = ml->createMapRenderer( job.context );
job.renderingTime = layerTime.elapsed(); // include job preparation time in layer rendering time

if ( hasStyleOverride )
ml->styleManager()->restoreOverrideStyle();
} // while (li.hasPrevious())

return layerJobs;

0 comments on commit 5c267f2

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