Skip to content
Permalink
Browse files

Better thread safety in package layers alg

  • Loading branch information
nyalldawson committed May 14, 2019
1 parent 2d64437 commit 449c9ce86cdd7a112e9d1e5e00c5e744706dab6d
Showing with 19 additions and 6 deletions.
  1. +15 −6 src/analysis/processing/qgsalgorithmpackage.cpp
  2. +4 −0 src/analysis/processing/qgsalgorithmpackage.h
@@ -68,6 +68,16 @@ QgsPackageAlgorithm *QgsPackageAlgorithm::createInstance() const
return new QgsPackageAlgorithm();
}

bool QgsPackageAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );
for ( QgsMapLayer *layer : layers )
{
mLayers.emplace_back( layer->clone() );
}
return true;
}

QVariantMap QgsPackageAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
const bool overwrite = parameterAsBoolean( parameters, QStringLiteral( "OVERWRITE" ), context );
@@ -97,22 +107,19 @@ QVariantMap QgsPackageAlgorithm::processAlgorithm( const QVariantMap &parameters
throw QgsProcessingException( QObject::tr( "Creation of database failed (OGR error: %1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );

bool errored = false;
const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );

QgsProcessingMultiStepFeedback multiStepFeedback( layers.count(), feedback );
QgsProcessingMultiStepFeedback multiStepFeedback( mLayers.size(), feedback );

QStringList outputLayers;
int i = 0;
for ( QgsMapLayer *layer : layers )
for ( const auto &layer : mLayers )
{
if ( feedback->isCanceled() )
break;

multiStepFeedback.setCurrentStep( i );
i++;

feedback->pushInfo( QObject::tr( "Packaging layer %1/%2: %3" ).arg( i ).arg( layers.count() ).arg( layer ? layer->name() : QString() ) );

if ( !layer )
{
// don't throw immediately - instead do what we can and error out later
@@ -121,11 +128,13 @@ QVariantMap QgsPackageAlgorithm::processAlgorithm( const QVariantMap &parameters
continue;
}

feedback->pushInfo( QObject::tr( "Packaging layer %1/%2: %3" ).arg( i ).arg( mLayers.size() ).arg( layer ? layer->name() : QString() ) );

switch ( layer->type() )
{
case QgsMapLayerType::VectorLayer:
{
if ( !packageVectorLayer( qobject_cast< QgsVectorLayer * >( layer ), packagePath,
if ( !packageVectorLayer( qobject_cast< QgsVectorLayer * >( layer.get() ), packagePath,
context, &multiStepFeedback, saveStyles ) )
errored = true;
else
@@ -47,13 +47,17 @@ class QgsPackageAlgorithm : public QgsProcessingAlgorithm

protected:

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

private:

bool packageVectorLayer( QgsVectorLayer *layer, const QString &path, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool saveStyles );

std::vector< std::unique_ptr< QgsMapLayer> > mLayers;

};

///@endcond PRIVATE

0 comments on commit 449c9ce

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