move guts of the set layer style to prepareAlgorithm() to keep it

threaded
This commit is contained in:
Alexander Bruy 2019-12-10 09:41:57 +02:00
parent bd72e02e55
commit 3a206a6bba
2 changed files with 15 additions and 12 deletions

View File

@ -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();
@ -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 ) );
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;
}

View File

@ -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;
@ -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