From 090bb9bba4742e5cd8d2c69a59295be39c604a9d Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sat, 19 Aug 2017 05:05:40 +1000 Subject: [PATCH] Use QgsFeedback instead of QProgressDialog in tin interpolator --- doc/api_break.dox | 5 +++ .../interpolation/NormVecDecorator.sip | 2 +- .../interpolation/qgstininterpolator.sip | 8 ++++- .../processing/algs/qgis/TinInterpolation.py | 2 +- .../interpolation/NormVecDecorator.cc | 23 ++++--------- src/analysis/interpolation/NormVecDecorator.h | 4 +-- .../interpolation/qgstininterpolator.cpp | 32 +++++-------------- .../interpolation/qgstininterpolator.h | 11 +++++-- 8 files changed, 39 insertions(+), 48 deletions(-) diff --git a/doc/api_break.dox b/doc/api_break.dox index 0ff3b52c16c3..fd63bf35ee54 100644 --- a/doc/api_break.dox +++ b/doc/api_break.dox @@ -2321,6 +2321,11 @@ QgsSymbolsListWidget {#qgis_api_break_3_0_QgsSymbolsListWidget} - expressionContext(), setExpressionContext(), setMapCanvas() and mapCanvas() have been removed in favor of setContext()/context() +QgsTINInterpolator {#qgis_api_break_3_0_QgsTINInterpolator} +------------------ + +- The constructor takes a QgsFeedback argument instead of using a QProgressDialog. + QgsTolerance {#qgis_api_break_3_0_QgsTolerance} ------------ diff --git a/python/analysis/interpolation/NormVecDecorator.sip b/python/analysis/interpolation/NormVecDecorator.sip index 17c9f24561b6..926c4a8bf0cc 100644 --- a/python/analysis/interpolation/NormVecDecorator.sip +++ b/python/analysis/interpolation/NormVecDecorator.sip @@ -59,7 +59,7 @@ Eliminates the horizontal triangles by swapping or by insertion of new points. I Estimates the first derivative a point. Return true in case of succes and false otherwise :rtype: bool %End - bool estimateFirstDerivatives( QProgressDialog *d = 0 ); + bool estimateFirstDerivatives( QgsFeedback *feedback = 0 ); %Docstring This method adds the functionality of estimating normals at the data points. Return true in the case of success and false otherwise :rtype: bool diff --git a/python/analysis/interpolation/qgstininterpolator.sip b/python/analysis/interpolation/qgstininterpolator.sip index 7c14fe56c106..390a1323f0f6 100644 --- a/python/analysis/interpolation/qgstininterpolator.sip +++ b/python/analysis/interpolation/qgstininterpolator.sip @@ -25,7 +25,13 @@ class QgsTINInterpolator: QgsInterpolator Linear, CloughTocher }; - QgsTINInterpolator( const QList &inputData, TINInterpolation interpolation = Linear, bool showProgressDialog = false ); + + QgsTINInterpolator( const QList &inputData, TINInterpolation interpolation = Linear, QgsFeedback *feedback = 0 ); +%Docstring + Constructor for QgsTINInterpolator. + The ``feedback`` object specifies an optional QgsFeedback object for progress reports and cancelation support. + Ownership of ``feedback`` is not transferred and callers must ensure that it exists for the lifetime of this object. +%End ~QgsTINInterpolator(); virtual int interpolatePoint( double x, double y, double &result ); diff --git a/python/plugins/processing/algs/qgis/TinInterpolation.py b/python/plugins/processing/algs/qgis/TinInterpolation.py index ebd8fe6fb9af..e5af69d2323b 100644 --- a/python/plugins/processing/algs/qgis/TinInterpolation.py +++ b/python/plugins/processing/algs/qgis/TinInterpolation.py @@ -190,7 +190,7 @@ def processAlgorithm(self, parameters, context, feedback): else: interpolationMethod = QgsTINInterpolator.CloughTocher - interpolator = QgsTINInterpolator(layerData, interpolationMethod) + interpolator = QgsTINInterpolator(layerData, interpolationMethod, feedback) if triangulation is not None and triangulation != '': interpolator.setExportTriangulationToFile(True) interpolator.setTriangulationFilePath(triangulation) diff --git a/src/analysis/interpolation/NormVecDecorator.cc b/src/analysis/interpolation/NormVecDecorator.cc index c4a44eec8e29..e9318d444d2f 100644 --- a/src/analysis/interpolation/NormVecDecorator.cc +++ b/src/analysis/interpolation/NormVecDecorator.cc @@ -15,9 +15,9 @@ ***************************************************************************/ #include "NormVecDecorator.h" +#include "qgsfeedback.h" #include "qgslogger.h" #include -#include NormVecDecorator::~NormVecDecorator() { @@ -511,29 +511,18 @@ bool NormVecDecorator::estimateFirstDerivative( int pointno ) } //weighted method of little -bool NormVecDecorator::estimateFirstDerivatives( QProgressDialog *d ) +bool NormVecDecorator::estimateFirstDerivatives( QgsFeedback *feedback ) { - if ( d ) + int numberPoints = getNumberOfPoints(); + for ( int i = 0; i < numberPoints; i++ ) { - d->setMinimum( 0 ); - d->setMaximum( getNumberOfPoints() ); - d->setCancelButton( nullptr ); //we cannot cancel derivative estimation - d->show(); - } - - for ( int i = 0; i < getNumberOfPoints(); i++ ) - { - if ( d ) + if ( feedback ) { - d->setValue( i ); + feedback->setProgress( 100.0 * static_cast< double >( i ) / numberPoints ); } estimateFirstDerivative( i ); } - if ( d ) - { - d->setValue( getNumberOfPoints() ); - } return true; } diff --git a/src/analysis/interpolation/NormVecDecorator.h b/src/analysis/interpolation/NormVecDecorator.h index 075163233b69..5d0e9ec85e9a 100644 --- a/src/analysis/interpolation/NormVecDecorator.h +++ b/src/analysis/interpolation/NormVecDecorator.h @@ -25,7 +25,7 @@ #include "qgslogger.h" #include "qgis_analysis.h" -class QProgressDialog; +class QgsFeedback; /** \ingroup analysis * Decorator class which adds the functionality of estimating normals at the data points*/ @@ -50,7 +50,7 @@ class ANALYSIS_EXPORT NormVecDecorator: public TriDecorator //! Estimates the first derivative a point. Return true in case of succes and false otherwise bool estimateFirstDerivative( int pointno ); //! This method adds the functionality of estimating normals at the data points. Return true in the case of success and false otherwise - bool estimateFirstDerivatives( QProgressDialog *d = nullptr ); + bool estimateFirstDerivatives( QgsFeedback *feedback = nullptr ); //! Returns a pointer to the normal vector for the point with the number n Vector3D *getNormal( int n ) const; //! Finds out, in which triangle a point with coordinates x and y is and assigns the triangle points to p1, p2, p3 and the estimated normals to v1, v2, v3. The vectors are normally taken from 'mNormVec', except if p1, p2 or p3 is a point on a breakline. In this case, the normal is calculated on-the-fly. Returns false, if something went wrong and true otherwise diff --git a/src/analysis/interpolation/qgstininterpolator.cpp b/src/analysis/interpolation/qgstininterpolator.cpp index d681bbc2f31b..332fb08f82ec 100644 --- a/src/analysis/interpolation/qgstininterpolator.cpp +++ b/src/analysis/interpolation/qgstininterpolator.cpp @@ -27,14 +27,15 @@ #include "qgsgeometry.h" #include "qgsvectorlayer.h" #include "qgswkbptr.h" +#include "qgsfeedback.h" #include -QgsTINInterpolator::QgsTINInterpolator( const QList &inputData, TINInterpolation interpolation, bool showProgressDialog ) +QgsTINInterpolator::QgsTINInterpolator( const QList &inputData, TINInterpolation interpolation, QgsFeedback *feedback ) : QgsInterpolator( inputData ) , mTriangulation( nullptr ) , mTriangleInterpolator( nullptr ) , mIsInitialized( false ) - , mShowProgressDialog( showProgressDialog ) + , mFeedback( feedback ) , mExportTriangulationToFile( false ) , mInterpolation( interpolation ) { @@ -84,7 +85,7 @@ void QgsTINInterpolator::initialize() //get number of features if we use a progress bar int nFeatures = 0; int nProcessedFeatures = 0; - if ( mShowProgressDialog ) + if ( mFeedback ) { Q_FOREACH ( const LayerData &layer, mLayerData ) { @@ -95,14 +96,6 @@ void QgsTINInterpolator::initialize() } } - QProgressDialog *progressDialog = nullptr; - if ( mShowProgressDialog ) - { - progressDialog = new QProgressDialog( QObject::tr( "Building triangulation..." ), QObject::tr( "Abort" ), 0, nFeatures, nullptr ); - progressDialog->setWindowModality( Qt::WindowModal ); - } - - QgsFeature f; Q_FOREACH ( const LayerData &layer, mLayerData ) { @@ -118,13 +111,13 @@ void QgsTINInterpolator::initialize() while ( fit.nextFeature( f ) ) { - if ( mShowProgressDialog ) + if ( mFeedback ) { - if ( progressDialog->wasCanceled() ) + if ( mFeedback->isCanceled() ) { break; } - progressDialog->setValue( nProcessedFeatures ); + mFeedback->setProgress( 100.0 * static_cast< double >( nProcessedFeatures ) / nFeatures ); } insertData( &f, layer.zCoordInterpolation, layer.interpolationAttribute, layer.mInputType ); ++nProcessedFeatures; @@ -132,22 +125,13 @@ void QgsTINInterpolator::initialize() } } - delete progressDialog; - if ( mInterpolation == CloughTocher ) { CloughTocherInterpolator *ctInterpolator = new CloughTocherInterpolator(); NormVecDecorator *dec = dynamic_cast( mTriangulation ); if ( dec ) { - QProgressDialog *progressDialog = nullptr; - if ( mShowProgressDialog ) //show a progress dialog because it can take a long time... - { - progressDialog = new QProgressDialog(); - progressDialog->setLabelText( QObject::tr( "Estimating normal derivatives..." ) ); - } - dec->estimateFirstDerivatives( progressDialog ); - delete progressDialog; + dec->estimateFirstDerivatives( mFeedback ); ctInterpolator->setTriangulation( dec ); dec->setTriangleInterpolator( ctInterpolator ); mTriangleInterpolator = ctInterpolator; diff --git a/src/analysis/interpolation/qgstininterpolator.h b/src/analysis/interpolation/qgstininterpolator.h index a298502a318f..4e1cc240b98d 100644 --- a/src/analysis/interpolation/qgstininterpolator.h +++ b/src/analysis/interpolation/qgstininterpolator.h @@ -25,6 +25,7 @@ class Triangulation; class TriangleInterpolator; class QgsFeature; +class QgsFeedback; /** \ingroup analysis * Interpolation in a triangular irregular network*/ @@ -37,7 +38,13 @@ class ANALYSIS_EXPORT QgsTINInterpolator: public QgsInterpolator Linear, CloughTocher }; - QgsTINInterpolator( const QList &inputData, TINInterpolation interpolation = Linear, bool showProgressDialog = false ); + + /** + * Constructor for QgsTINInterpolator. + * The \a feedback object specifies an optional QgsFeedback object for progress reports and cancelation support. + * Ownership of \a feedback is not transferred and callers must ensure that it exists for the lifetime of this object. + */ + QgsTINInterpolator( const QList &inputData, TINInterpolation interpolation = Linear, QgsFeedback *feedback = nullptr ); ~QgsTINInterpolator(); /** Calculates interpolation value for map coordinates x, y @@ -54,7 +61,7 @@ class ANALYSIS_EXPORT QgsTINInterpolator: public QgsInterpolator Triangulation *mTriangulation = nullptr; TriangleInterpolator *mTriangleInterpolator = nullptr; bool mIsInitialized; - bool mShowProgressDialog; + QgsFeedback *mFeedback = nullptr; //! If true: export triangulation to shapefile after initialization bool mExportTriangulationToFile; //! File path to export the triangulation