Skip to content

Commit eb7ad7a

Browse files
committed
Swap final uses of QProgressDialog in analysis lib to QgsFeedback
1 parent 7cd5880 commit eb7ad7a

File tree

8 files changed

+51
-35
lines changed

8 files changed

+51
-35
lines changed

doc/api_break.dox

+8-1
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,8 @@ QgsRasterCalculator {#qgis_api_break_3_0_QgsRasterCalculator}
19081908
-------------------
19091909

19101910
- Cancelled (Result enum value) has been renamed to Canceled <!--#spellok-->
1911-
1911+
- processCalculation() now uses an optional QgsFeedback instead of QProgressDialog
1912+
for progress reports and cancelation.
19121913

19131914
QgsRasterDataProvider {#qgis_api_break_3_0_QgsRasterDataProvider}
19141915
---------------------
@@ -2350,6 +2351,12 @@ QgsTracer {#qgis_api_break_3_0_QgsTracer}
23502351

23512352
- hasCrsTransformEnabled() and setCrsTransformEnabled() were removed. CRS transformation is now always enabled when required.
23522353

2354+
QgsTransectSample {#qgis_api_break_3_0_QgsTransectSample}
2355+
-----------------
2356+
2357+
- createSample() now uses an optional QgsFeedback instead of QProgressDialog
2358+
for progress reports and cancelation.
2359+
23532360

23542361
QgsTreeWidgetItem {#qgis_api_break_3_0_QgsTreeWidgetItem}
23552362
-----------------

python/analysis/raster/qgsrastercalculator.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class QgsRasterCalculator
7171
.. versionadded:: 2.10
7272
%End
7373

74-
int processCalculation( QProgressDialog *p = 0 );
74+
int processCalculation( QgsFeedback *feedback = 0 );
7575
%Docstring
7676
:rtype: int
7777
%End

python/analysis/vector/qgstransectsample.sip

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ class QgsTransectSample
3030
const QString &baselineStrataId, const QString &outputPointLayer, const QString &outputLineLayer, const QString &usedBaselineLayer, double minTransectLength = 0.0,
3131
double baselineBufferDistance = -1.0, double baselineSimplificationTolerance = -1.0 );
3232

33-
int createSample( QProgressDialog *pd );
33+
int createSample( QgsFeedback *feedback = 0 );
3434
%Docstring
35+
Creates the sample.
36+
37+
The optional ``feedback`` argument can be used for progress reporting and cancelation support.
3538
:rtype: int
3639
%End
3740

src/analysis/raster/qgsrastercalculator.cpp

+8-13
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
#include "qgsrasterlayer.h"
2323
#include "qgsrastermatrix.h"
2424
#include "qgsrasterprojector.h"
25+
#include "qgsfeedback.h"
2526

26-
#include <QProgressDialog>
2727
#include <QFile>
2828

2929
#include <cpl_string.h>
@@ -56,7 +56,7 @@ QgsRasterCalculator::QgsRasterCalculator( const QString &formulaString, const QS
5656
{
5757
}
5858

59-
int QgsRasterCalculator::processCalculation( QProgressDialog *p )
59+
int QgsRasterCalculator::processCalculation( QgsFeedback *feedback )
6060
{
6161
//prepare search string / tree
6262
QString errorString;
@@ -117,23 +117,18 @@ int QgsRasterCalculator::processCalculation( QProgressDialog *p )
117117
float outputNodataValue = -FLT_MAX;
118118
GDALSetRasterNoDataValue( outputRasterBand, outputNodataValue );
119119

120-
if ( p )
121-
{
122-
p->setMaximum( mNumOutputRows );
123-
}
124-
125120
QgsRasterMatrix resultMatrix;
126121
resultMatrix.setNodataValue( outputNodataValue );
127122

128123
//read / write line by line
129124
for ( int i = 0; i < mNumOutputRows; ++i )
130125
{
131-
if ( p )
126+
if ( feedback )
132127
{
133-
p->setValue( i );
128+
feedback->setProgress( 100.0 * static_cast< double >( i ) / mNumOutputRows );
134129
}
135130

136-
if ( p && p->wasCanceled() )
131+
if ( feedback && feedback->isCanceled() )
137132
{
138133
break;
139134
}
@@ -159,17 +154,17 @@ int QgsRasterCalculator::processCalculation( QProgressDialog *p )
159154

160155
}
161156

162-
if ( p )
157+
if ( feedback )
163158
{
164-
p->setValue( mNumOutputRows );
159+
feedback->setProgress( 100.0 );
165160
}
166161

167162
//close datasets and release memory
168163
delete calcNode;
169164
qDeleteAll( inputBlocks );
170165
inputBlocks.clear();
171166

172-
if ( p && p->wasCanceled() )
167+
if ( feedback && feedback->isCanceled() )
173168
{
174169
//delete the dataset without closing (because it is faster)
175170
GDALDeleteDataset( outputDriver, mOutputFile.toUtf8().constData() );

src/analysis/raster/qgsrastercalculator.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "qgis_analysis.h"
2727

2828
class QgsRasterLayer;
29-
class QProgressDialog;
29+
class QgsFeedback;
3030

3131

3232
struct ANALYSIS_EXPORT QgsRasterCalculatorEntry
@@ -85,11 +85,13 @@ class ANALYSIS_EXPORT QgsRasterCalculator
8585
QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat,
8686
const QgsRectangle &outputExtent, const QgsCoordinateReferenceSystem &outputCrs, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry> &rasterEntries );
8787

88-
/** Starts the calculation and writes new raster
89-
\param p progress bar (or 0 if called from non-gui code)
90-
\returns 0 in case of success*/
88+
/** Starts the calculation and writes a new raster.
89+
*
90+
* The optional \a feedback argument can be used for progress reporting and cancelation support.
91+
* \returns 0 in case of success
92+
*/
9193
//TODO QGIS 3.0 - return QgsRasterCalculator::Result
92-
int processCalculation( QProgressDialog *p = nullptr );
94+
int processCalculation( QgsFeedback *feedback = nullptr );
9395

9496
private:
9597
//default constructor forbidden. We need formula, output file, output format and output raster resolution obligatory

src/analysis/vector/qgstransectsample.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
#include "qgsvectorfilewriter.h"
2121
#include "qgsvectorlayer.h"
2222
#include "qgsproject.h"
23-
#include <QProgressDialog>
23+
#include "qgsfeedback.h"
24+
2425
#include <QFileInfo>
2526
#ifndef _MSC_VER
2627
#include <cstdint>
@@ -60,10 +61,8 @@ QgsTransectSample::QgsTransectSample()
6061
{
6162
}
6263

63-
int QgsTransectSample::createSample( QProgressDialog *pd )
64+
int QgsTransectSample::createSample( QgsFeedback *feedback )
6465
{
65-
Q_UNUSED( pd );
66-
6766
if ( !mStrataLayer || !mStrataLayer->isValid() )
6867
{
6968
return 1;
@@ -147,19 +146,20 @@ int QgsTransectSample::createSample( QProgressDialog *pd )
147146
QgsFeature fet;
148147
int nTotalTransects = 0;
149148
int nFeatures = 0;
149+
int totalFeatures = 0;
150150

151-
if ( pd )
151+
if ( feedback )
152152
{
153-
pd->setMaximum( mStrataLayer->featureCount() );
153+
totalFeatures = mStrataLayer->featureCount();
154154
}
155155

156156
while ( strataIt.nextFeature( fet ) )
157157
{
158-
if ( pd )
158+
if ( feedback )
159159
{
160-
pd->setValue( nFeatures );
160+
feedback->setProgress( 100.0 * static_cast< double >( nFeatures ) / totalFeatures );
161161
}
162-
if ( pd && pd->wasCanceled() )
162+
if ( feedback && feedback->isCanceled() )
163163
{
164164
break;
165165
}
@@ -321,9 +321,9 @@ int QgsTransectSample::createSample( QProgressDialog *pd )
321321
++nFeatures;
322322
}
323323

324-
if ( pd )
324+
if ( feedback )
325325
{
326-
pd->setValue( mStrataLayer->featureCount() );
326+
feedback->setProgress( 100.0 );
327327
}
328328

329329
return 0;

src/analysis/vector/qgstransectsample.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class QgsGeometry;
2525
class QgsSpatialIndex;
2626
class QgsVectorLayer;
2727
class QgsPointXY;
28-
class QProgressDialog;
28+
class QgsFeedback;
2929

3030
/** \ingroup analysis
3131
* A class for the creation of transect sample lines based on a set of strata polygons and baselines*/
@@ -44,7 +44,12 @@ class ANALYSIS_EXPORT QgsTransectSample
4444
const QString &baselineStrataId, const QString &outputPointLayer, const QString &outputLineLayer, const QString &usedBaselineLayer, double minTransectLength = 0.0,
4545
double baselineBufferDistance = -1.0, double baselineSimplificationTolerance = -1.0 );
4646

47-
int createSample( QProgressDialog *pd );
47+
/**
48+
* Creates the sample.
49+
*
50+
* The optional \a feedback argument can be used for progress reporting and cancelation support.
51+
*/
52+
int createSample( QgsFeedback *feedback = nullptr );
4853

4954
private:
5055
QgsTransectSample(); //default constructor forbidden

src/app/qgisapp.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -5127,7 +5127,11 @@ void QgisApp::showRasterCalculator()
51275127

51285128
QProgressDialog p( tr( "Calculating..." ), tr( "Abort..." ), 0, 0 );
51295129
p.setWindowModality( Qt::WindowModal );
5130-
QgsRasterCalculator::Result res = static_cast< QgsRasterCalculator::Result >( rc.processCalculation( &p ) );
5130+
p.setMaximum( 100.0 );
5131+
QgsFeedback feedback;
5132+
connect( &feedback, &QgsFeedback::progressChanged, &p, &QProgressDialog::setValue );
5133+
connect( &feedback, &QgsFeedback::canceled, &p, &QProgressDialog::cancel );
5134+
QgsRasterCalculator::Result res = static_cast< QgsRasterCalculator::Result >( rc.processCalculation( &feedback ) );
51315135
switch ( res )
51325136
{
51335137
case QgsRasterCalculator::Success:

0 commit comments

Comments
 (0)