Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Use a proxy task for field calculator
- Loading branch information
Showing
with
25 additions
and
5 deletions.
-
+16
−5
src/app/qgsattributetabledialog.cpp
-
+9
−0
src/app/qgsfieldcalculator.cpp
|
@@ -53,6 +53,8 @@ |
|
|
#include "qgsgui.h" |
|
|
#include "qgsclipboard.h" |
|
|
#include "qgsfeaturestore.h" |
|
|
#include "qgsguiutils.h" |
|
|
#include "qgsproxyprogresstask.h" |
|
|
|
|
|
QgsExpressionContext QgsAttributeTableDialog::createExpressionContext() const |
|
|
{ |
|
@@ -503,10 +505,6 @@ void QgsAttributeTableDialog::formFilterSet( const QString &filter, QgsAttribute |
|
|
|
|
|
void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer *layer, const QString &fieldName, const QString &expression, const QgsFeatureIds &filteredIds ) |
|
|
{ |
|
|
QApplication::setOverrideCursor( Qt::WaitCursor ); |
|
|
|
|
|
mLayer->beginEditCommand( QStringLiteral( "Field calculator" ) ); |
|
|
|
|
|
int fieldindex = layer->fields().indexFromName( fieldName ); |
|
|
if ( fieldindex < 0 ) |
|
|
{ |
|
@@ -516,6 +514,9 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer *layer, const |
|
|
return; |
|
|
} |
|
|
|
|
|
QgsTemporaryCursorOverride cursorOverride( Qt::WaitCursor ); |
|
|
mLayer->beginEditCommand( QStringLiteral( "Field calculator" ) ); |
|
|
|
|
|
bool calculationSuccess = true; |
|
|
QString error; |
|
|
|
|
@@ -542,6 +543,12 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer *layer, const |
|
|
|
|
|
//go through all the features and change the new attributes |
|
|
QgsFeatureIterator fit = layer->getFeatures( request ); |
|
|
|
|
|
std::unique_ptr< QgsScopedProxyProgressTask > task = qgis::make_unique< QgsScopedProxyProgressTask >( tr( "Calculating field" ) ); |
|
|
|
|
|
long long count = !filteredIds.isEmpty() ? filteredIds.size() : layer->featureCount(); |
|
|
long long i = 0; |
|
|
|
|
|
QgsFeature feature; |
|
|
while ( fit.nextFeature( feature ) ) |
|
|
{ |
|
@@ -550,6 +557,9 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer *layer, const |
|
|
continue; |
|
|
} |
|
|
|
|
|
i++; |
|
|
task->setProgress( i / static_cast< double >( count ) * 100 ); |
|
|
|
|
|
context.setFeature( feature ); |
|
|
context.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "row_number" ), rownum, true ) ); |
|
|
|
|
@@ -571,7 +581,8 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer *layer, const |
|
|
rownum++; |
|
|
} |
|
|
|
|
|
QApplication::restoreOverrideCursor(); |
|
|
cursorOverride.release(); |
|
|
task.reset(); |
|
|
|
|
|
if ( !calculationSuccess ) |
|
|
{ |
|
|
|
@@ -27,6 +27,7 @@ |
|
|
#include "qgssettings.h" |
|
|
#include "qgsgui.h" |
|
|
#include "qgsguiutils.h" |
|
|
#include "qgsproxyprogresstask.h" |
|
|
|
|
|
#include <QMessageBox> |
|
|
|
|
@@ -289,8 +290,15 @@ void QgsFieldCalculator::accept() |
|
|
req.setFilterFids( mVectorLayer->selectedFeatureIds() ); |
|
|
} |
|
|
QgsFeatureIterator fit = mVectorLayer->getFeatures( req ); |
|
|
|
|
|
std::unique_ptr< QgsScopedProxyProgressTask > task = qgis::make_unique< QgsScopedProxyProgressTask >( tr( "Calculating field" ) ); |
|
|
long long count = mOnlyUpdateSelectedCheckBox->isChecked() ? mVectorLayer->selectedFeatureCount() : mVectorLayer->featureCount(); |
|
|
long long i = 0; |
|
|
while ( fit.nextFeature( feature ) ) |
|
|
{ |
|
|
i++; |
|
|
task->setProgress( i / static_cast< double >( count ) * 100 ); |
|
|
|
|
|
expContext.setFeature( feature ); |
|
|
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "row_number" ), rownum, true ) ); |
|
|
|
|
@@ -323,6 +331,7 @@ void QgsFieldCalculator::accept() |
|
|
if ( !calculationSuccess ) |
|
|
{ |
|
|
cursorOverride.release(); |
|
|
task.reset(); |
|
|
QMessageBox::critical( nullptr, tr( "Evaluation Error" ), tr( "An error occurred while evaluating the calculation string:\n%1" ).arg( error ) ); |
|
|
mVectorLayer->destroyEditCommand(); |
|
|
return; |
|
|