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
Show file tree
Hide file tree
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
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions src/3d/processing/qgsalgorithmtessellate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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( ',' );
Expand Down
1 change: 1 addition & 0 deletions src/3d/processing/qgsalgorithmtessellate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmaddincrementalfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

///@cond PRIVATE

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

QString QgsAddIncrementalFieldAlgorithm::name() const
{
return QStringLiteral( "addautoincrementalfield" );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmaddincrementalfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmassignprojection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

///@cond PRIVATE

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

QString QgsAssignProjectionAlgorithm::name() const
{
return QStringLiteral( "assignprojection" );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmassignprojection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmboundary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

///@cond PRIVATE

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

QString QgsBoundaryAlgorithm::name() const
{
return QStringLiteral( "boundary" );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmboundary.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmboundingbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

///@cond PRIVATE

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

QString QgsBoundingBoxAlgorithm::name() const
{
return QStringLiteral( "boundingboxes" );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmboundingbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/processing/qgsalgorithmbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmcentroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) ) );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmcentroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmclip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmclip.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmconvexhull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

///@cond PRIVATE

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

QString QgsConvexHullAlgorithm::name() const
{
return QStringLiteral( "convexhull" );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmconvexhull.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmdissolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmdissolve.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
};
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmdropgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

///@cond PRIVATE

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

QString QgsDropGeometryAlgorithm::name() const
{
return QStringLiteral( "dropgeometries" );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmdropgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmdropmzvalues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

///@cond PRIVATE

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

QString QgsDropMZValuesAlgorithm::name() const
{
return QStringLiteral( "dropmzvalues" );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmdropmzvalues.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmextenttolayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) ) );
Expand Down
1 change: 1 addition & 0 deletions src/analysis/processing/qgsalgorithmextenttolayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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" ); }
Expand Down
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmextractbyattribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ QString QgsExtractByAttributeAlgorithm::group() const
return QObject::tr( "Vector selection" );
}