Skip to content

Commit

Permalink
move guts of the set layer style to prepareAlgorithm() to keep it
Browse files Browse the repository at this point in the history
threaded
  • Loading branch information
alexbruy committed Dec 10, 2019
1 parent bd72e02 commit 3a206a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/analysis/processing/qgsalgorithmapplylayerstyle.cpp
Expand Up @@ -49,11 +49,6 @@ QString QgsApplyLayerStyleAlgorithm::shortHelpString() const
return QObject::tr( "Applies the style to a layer. The style must be defined as QML file." );
}

QgsProcessingAlgorithm::Flags QgsApplyLayerStyleAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagNoThreading;
}

QgsApplyLayerStyleAlgorithm *QgsApplyLayerStyleAlgorithm::createInstance() const
{
return new QgsApplyLayerStyleAlgorithm();
Expand All @@ -66,24 +61,29 @@ void QgsApplyLayerStyleAlgorithm::initAlgorithm( const QVariantMap & )
addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Styled" ) ) );
}

QVariantMap QgsApplyLayerStyleAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
bool QgsApplyLayerStyleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
mLayer.reset( parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context ) );

This comment has been minimized.

Copy link
@nyalldawson

nyalldawson Dec 10, 2019

Collaborator

We don't get ownership here, so it can't be stored in a unique ptr. I'd leave this as a local variable and only store the layer id as a QString member variable

QString style = parameterAsFile( parameters, QStringLiteral( "STYLE" ), context );

if ( !layer )
if ( !mLayer )
throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );

bool ok = false;
QString msg = layer->loadNamedStyle( style, ok );
QString msg = mLayer->loadNamedStyle( style, ok );
if ( !ok )
{
throw QgsProcessingException( QObject::tr( "Failed to apply style. Error: %1" ).arg( msg ) );
}
layer->triggerRepaint();
mLayer->triggerRepaint();

return true;
}

QVariantMap QgsApplyLayerStyleAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
QVariantMap results;
results.insert( QStringLiteral( "OUTPUT" ), layer->id() );
results.insert( QStringLiteral( "OUTPUT" ), mLayer->id() );
return results;
}

Expand Down
5 changes: 4 additions & 1 deletion src/analysis/processing/qgsalgorithmapplylayerstyle.h
Expand Up @@ -32,7 +32,6 @@ class QgsApplyLayerStyleAlgorithm : public QgsProcessingAlgorithm
{
public:
QgsApplyLayerStyleAlgorithm() = default;
Flags flags() const override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
Expand All @@ -44,9 +43,13 @@ class QgsApplyLayerStyleAlgorithm : public QgsProcessingAlgorithm

protected:

bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback * ) override;

private:

std::unique_ptr< QgsMapLayer > mLayer;
};

///@endcond PRIVATE
Expand Down

0 comments on commit 3a206a6

Please sign in to comment.