Skip to content

Commit

Permalink
Most c++ algorithms can run safely in background threads
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 9, 2018
1 parent 70785f8 commit c14e6ca
Showing 92 changed files with 269 additions and 6 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/gui/AlgorithmDialog.py
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@
from pprint import pformat
import time

from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtCore import QCoreApplication, Qt
from qgis.PyQt.QtWidgets import QMessageBox, QPushButton, QSizePolicy, QDialogButtonBox
from qgis.PyQt.QtGui import QColor, QPalette

@@ -241,14 +241,14 @@ def on_complete(ok, results):
feedback.pushInfo('')

self.cancelButton().setEnabled(False)

self.finish(ok, results, context, feedback)

if self.algorithm().flags() & QgsProcessingAlgorithm.FlagCanRunInBackground:
task = QgsProcessingAlgRunnerTask(self.algorithm(), parameters, context, feedback)
task.executed.connect(on_complete)
QgsApplication.taskManager().addTask(task)
else:
self.setWindowModality(Qt.ApplicationModal)
ok, results = execute(self.algorithm(), parameters, context, feedback)
on_complete(ok, results)

5 changes: 5 additions & 0 deletions src/3d/processing/qgsalgorithmtessellate.cpp
Original file line number Diff line number Diff line change
@@ -31,6 +31,11 @@ QString QgsTessellateAlgorithm::displayName() const
return QObject::tr( "Tessellate" );
}

QgsProcessingAlgorithm::Flags QgsTessellateAlgorithm::flags() const
{
return QgsProcessingFeatureBasedAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

QStringList QgsTessellateAlgorithm::tags() const
{
return QObject::tr( "3d,triangle" ).split( ',' );
1 change: 1 addition & 0 deletions src/3d/processing/qgsalgorithmtessellate.h
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ class QgsTessellateAlgorithm : public QgsProcessingFeatureBasedAlgorithm
QgsTessellateAlgorithm() = default;
QString name() const override;
QString displayName() const override;
Flags flags() const override;
QStringList tags() const override;
QString group() const override;
QString shortHelpString() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmaddincrementalfield.cpp
Original file line number Diff line number Diff line change
@@ -19,6 +19,11 @@

///@cond PRIVATE

QgsProcessingAlgorithm::Flags QgsAddIncrementalFieldAlgorithm::flags() const
{
return QgsProcessingFeatureBasedAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

QString QgsAddIncrementalFieldAlgorithm::name() const
{
return QStringLiteral( "addautoincrementalfield" );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmaddincrementalfield.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class QgsAddIncrementalFieldAlgorithm : public QgsProcessingFeatureBasedAlgorith
public:

QgsAddIncrementalFieldAlgorithm() = default;
Flags flags() const override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmassignprojection.cpp
Original file line number Diff line number Diff line change
@@ -19,6 +19,11 @@

///@cond PRIVATE

QgsProcessingAlgorithm::Flags QgsAssignProjectionAlgorithm::flags() const
{
return QgsProcessingFeatureBasedAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

QString QgsAssignProjectionAlgorithm::name() const
{
return QStringLiteral( "assignprojection" );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmassignprojection.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class QgsAssignProjectionAlgorithm : public QgsProcessingFeatureBasedAlgorithm
public:

QgsAssignProjectionAlgorithm() = default;
Flags flags() const override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmboundary.cpp
Original file line number Diff line number Diff line change
@@ -19,6 +19,11 @@

///@cond PRIVATE

QgsProcessingAlgorithm::Flags QgsBoundaryAlgorithm::flags() const
{
return QgsProcessingFeatureBasedAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

QString QgsBoundaryAlgorithm::name() const
{
return QStringLiteral( "boundary" );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmboundary.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class QgsBoundaryAlgorithm : public QgsProcessingFeatureBasedAlgorithm
public:

QgsBoundaryAlgorithm() = default;
Flags flags() const override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmboundingbox.cpp
Original file line number Diff line number Diff line change
@@ -19,6 +19,11 @@

///@cond PRIVATE

QgsProcessingAlgorithm::Flags QgsBoundingBoxAlgorithm::flags() const
{
return QgsProcessingFeatureBasedAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

QString QgsBoundingBoxAlgorithm::name() const
{
return QStringLiteral( "boundingboxes" );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmboundingbox.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class QgsBoundingBoxAlgorithm : public QgsProcessingFeatureBasedAlgorithm
public:

QgsBoundingBoxAlgorithm() = default;
Flags flags() const override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmbuffer.cpp
Original file line number Diff line number Diff line change
@@ -63,6 +63,11 @@ void QgsBufferAlgorithm::initAlgorithm( const QVariantMap & )
addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Buffered" ), QgsProcessing::TypeVectorPolygon ) );
}

QgsProcessingAlgorithm::Flags QgsBufferAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

QString QgsBufferAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm computes a buffer area for all the features in an input layer, using a fixed or dynamic distance.\n\n"
2 changes: 1 addition & 1 deletion src/analysis/processing/qgsalgorithmbuffer.h
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ class QgsBufferAlgorithm : public QgsProcessingAlgorithm

QgsBufferAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;

Flags flags() const override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmcentroid.cpp
Original file line number Diff line number Diff line change
@@ -49,6 +49,11 @@ QString QgsCentroidAlgorithm::outputName() const
return QObject::tr( "Centroids" );
}

QgsProcessingAlgorithm::Flags QgsCentroidAlgorithm::flags() const
{
return QgsProcessingFeatureBasedAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

void QgsCentroidAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmcentroid.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class QgsCentroidAlgorithm : public QgsProcessingFeatureBasedAlgorithm
public:

QgsCentroidAlgorithm() = default;
Flags flags() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmclip.cpp
Original file line number Diff line number Diff line change
@@ -40,6 +40,11 @@ QString QgsClipAlgorithm::group() const
return QObject::tr( "Vector overlay" );
}

QgsProcessingAlgorithm::Flags QgsClipAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

QString QgsClipAlgorithm::groupId() const
{
return QStringLiteral( "vectoroverlay" );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmclip.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class QgsClipAlgorithm : public QgsProcessingAlgorithm
public:

QgsClipAlgorithm() = default;
Flags flags() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmconvexhull.cpp
Original file line number Diff line number Diff line change
@@ -19,6 +19,11 @@

///@cond PRIVATE

QgsProcessingAlgorithm::Flags QgsConvexHullAlgorithm::flags() const
{
return QgsProcessingFeatureBasedAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

QString QgsConvexHullAlgorithm::name() const
{
return QStringLiteral( "convexhull" );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmconvexhull.h
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ class QgsConvexHullAlgorithm : public QgsProcessingFeatureBasedAlgorithm
public:

QgsConvexHullAlgorithm() = default;
Flags flags() const override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmdissolve.cpp
Original file line number Diff line number Diff line change
@@ -23,6 +23,11 @@
// QgsCollectorAlgorithm
//

QgsProcessingAlgorithm::Flags QgsCollectorAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

QVariantMap QgsCollectorAlgorithm::processCollection( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback,
const std::function<QgsGeometry( const QVector< QgsGeometry >& )> &collector, int maxQueueLength )
{
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmdissolve.h
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ class QgsCollectorAlgorithm : public QgsProcessingAlgorithm
{
protected:

Flags flags() const override;
QVariantMap processCollection( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback,
const std::function<QgsGeometry( const QVector<QgsGeometry>& )> &collector, int maxQueueLength = 0 );
};
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmdropgeometry.cpp
Original file line number Diff line number Diff line change
@@ -19,6 +19,11 @@

///@cond PRIVATE

QgsProcessingAlgorithm::Flags QgsDropGeometryAlgorithm::flags() const
{
return QgsProcessingFeatureBasedAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

QString QgsDropGeometryAlgorithm::name() const
{
return QStringLiteral( "dropgeometries" );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmdropgeometry.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class QgsDropGeometryAlgorithm : public QgsProcessingFeatureBasedAlgorithm
public:

QgsDropGeometryAlgorithm() = default;
Flags flags() const override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmdropmzvalues.cpp
Original file line number Diff line number Diff line change
@@ -19,6 +19,11 @@

///@cond PRIVATE

QgsProcessingAlgorithm::Flags QgsDropMZValuesAlgorithm::flags() const
{
return QgsProcessingFeatureBasedAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

QString QgsDropMZValuesAlgorithm::name() const
{
return QStringLiteral( "dropmzvalues" );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmdropmzvalues.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class QgsDropMZValuesAlgorithm : public QgsProcessingFeatureBasedAlgorithm
public:

QgsDropMZValuesAlgorithm() = default;
Flags flags() const override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmextenttolayer.cpp
Original file line number Diff line number Diff line change
@@ -24,6 +24,11 @@ QString QgsExtentToLayerAlgorithm::name() const
return QStringLiteral( "extenttolayer" );
}

QgsProcessingAlgorithm::Flags QgsExtentToLayerAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

void QgsExtentToLayerAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterExtent( QStringLiteral( "INPUT" ), QObject::tr( "Extent" ) ) );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmextenttolayer.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class QgsExtentToLayerAlgorithm : public QgsProcessingAlgorithm
public:

QgsExtentToLayerAlgorithm() = default;
Flags flags() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override { return QObject::tr( "Create layer from extent" ); }
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmextractbyattribute.cpp
Original file line number Diff line number Diff line change
@@ -39,6 +39,11 @@ QString QgsExtractByAttributeAlgorithm::group() const
return QObject::tr( "Vector selection" );
}

QgsProcessingAlgorithm::Flags QgsExtractByAttributeAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

QString QgsExtractByAttributeAlgorithm::groupId() const
{
return QStringLiteral( "vectorselection" );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmextractbyattribute.h
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ class QgsExtractByAttributeAlgorithm : public QgsProcessingAlgorithm
};

QgsExtractByAttributeAlgorithm() = default;
Flags flags() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmextractbyexpression.cpp
Original file line number Diff line number Diff line change
@@ -44,6 +44,11 @@ QString QgsExtractByExpressionAlgorithm::groupId() const
return QStringLiteral( "vectorselection" );
}

QgsProcessingAlgorithm::Flags QgsExtractByExpressionAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

void QgsExtractByExpressionAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmextractbyexpression.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class QgsExtractByExpressionAlgorithm : public QgsProcessingAlgorithm
public:

QgsExtractByExpressionAlgorithm() = default;
Flags flags() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmextractbyextent.cpp
Original file line number Diff line number Diff line change
@@ -44,6 +44,11 @@ QString QgsExtractByExtentAlgorithm::groupId() const
return QStringLiteral( "vectoroverlay" );
}

QgsProcessingAlgorithm::Flags QgsExtractByExtentAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

void QgsExtractByExtentAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmextractbyextent.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class QgsExtractByExtentAlgorithm : public QgsProcessingAlgorithm
public:

QgsExtractByExtentAlgorithm() = default;
Flags flags() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmextractbylocation.cpp
Original file line number Diff line number Diff line change
@@ -286,6 +286,11 @@ QVariantMap QgsSelectByLocationAlgorithm::processAlgorithm( const QVariantMap &p
// QgsExtractByLocationAlgorithm
//

QgsProcessingAlgorithm::Flags QgsExtractByLocationAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

void QgsExtractByLocationAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Extract features from" ),
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmextractbylocation.h
Original file line number Diff line number Diff line change
@@ -87,6 +87,7 @@ class QgsExtractByLocationAlgorithm : public QgsLocationBasedAlgorithm
public:

QgsExtractByLocationAlgorithm() = default;
Flags flags() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmextractnodes.cpp
Original file line number Diff line number Diff line change
@@ -59,6 +59,11 @@ QgsExtractNodesAlgorithm *QgsExtractNodesAlgorithm::createInstance() const
return new QgsExtractNodesAlgorithm();
}

QgsProcessingAlgorithm::Flags QgsExtractNodesAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

void QgsExtractNodesAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmextractnodes.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class QgsExtractNodesAlgorithm : public QgsProcessingAlgorithm
public:

QgsExtractNodesAlgorithm() = default;
Flags flags() const override;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmfiledownloader.cpp
Original file line number Diff line number Diff line change
@@ -60,6 +60,11 @@ QgsFileDownloaderAlgorithm *QgsFileDownloaderAlgorithm::createInstance() const
return new QgsFileDownloaderAlgorithm();
}

QgsProcessingAlgorithm::Flags QgsFileDownloaderAlgorithm::flags() const
{
return QgsProcessingAlgorithm::flags() | QgsProcessingAlgorithm::FlagCanRunInBackground;
}

void QgsFileDownloaderAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterString( QStringLiteral( "URL" ), tr( "URL" ), QVariant(), false, false ) );
Loading

0 comments on commit c14e6ca

Please sign in to comment.