Skip to content

Commit

Permalink
Use QgsFeedback instead of QProgressDialog for QgsVectorLayerExporter…
Browse files Browse the repository at this point in the history
…::exportLayer

And add cancelation and progress reporting to background layer export
  • Loading branch information
nyalldawson committed May 11, 2017
1 parent 446db28 commit b6a5cc4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
3 changes: 2 additions & 1 deletion doc/api_break.dox
Expand Up @@ -2190,7 +2190,8 @@ pointers makes for more robust, safer code. Use an invalid (default constructed)
in code which previously passed a null pointer to QgsVectorLayerImport.
- importLayer was renamed to exportLayer
- The unused skipAttributeCreation in exportLayer() was removed
- The unused QProgressBar argument in the QgsVectorLayerImport was removed
- The QProgressDialog argument in exportLayer() was changed to a QgsFeedback object.
- The unused QProgressDialog argument in the QgsVectorLayerImport was removed
- ImportError was renamed to ExportError
- The unused enum value ErrDriverNotFound was removed
- hasError() was renamed to errorCode()
Expand Down
4 changes: 2 additions & 2 deletions python/core/qgsvectorlayerexporter.sip
Expand Up @@ -53,7 +53,7 @@ class QgsVectorLayerExporter : QgsFeatureSink
bool onlySelected = false,
QString *errorMessage /Out/ = 0,
QMap<QString, QVariant> *options = 0,
QProgressDialog *progress = 0
QgsFeedback *feedback = 0
);
%Docstring
Writes the contents of vector layer to a different datasource.
Expand All @@ -65,7 +65,7 @@ class QgsVectorLayerExporter : QgsFeatureSink
\param onlySelected set to true to export only selected features
\param errorMessage if non-null, will be set to any error messages
\param options optional provider dataset options
\param progress optional progress dialog to show progress of export
\param feedback optional feedback object to show progress and cancelation of export
:return: NoError for a successful export, or encountered error
:rtype: ExportError
%End
Expand Down
21 changes: 9 additions & 12 deletions src/core/qgsvectorlayerexporter.cpp
Expand Up @@ -232,7 +232,7 @@ QgsVectorLayerExporter::exportLayer( QgsVectorLayer *layer,
bool onlySelected,
QString *errorMessage,
QMap<QString, QVariant> *options,
QProgressDialog *progress )
QgsFeedback *feedback )
{
QgsCoordinateReferenceSystem outputCRS;
QgsCoordinateTransform ct;
Expand Down Expand Up @@ -339,29 +339,25 @@ QgsVectorLayerExporter::exportLayer( QgsVectorLayer *layer,
if ( !ct.isValid() )
shallTransform = false;

int n = 0;
long n = 0;
long approxTotal = onlySelected ? layer->selectedFeatureCount() : layer->featureCount();

if ( errorMessage )
{
*errorMessage = QObject::tr( "Feature write errors:" );
}

if ( progress )
{
progress->setRange( 0, layer->featureCount() );
}

bool canceled = false;

// write all features
while ( fit.nextFeature( fet ) )
{
if ( progress && progress->wasCanceled() )
if ( feedback && feedback->isCanceled() )
{
canceled = true;
if ( errorMessage )
{
*errorMessage += '\n' + QObject::tr( "Import was canceled at %1 of %2" ).arg( progress->value() ).arg( progress->maximum() );
*errorMessage += '\n' + QObject::tr( "Import was canceled at %1 of %2" ).arg( n ).arg( approxTotal );
}
break;
}
Expand Down Expand Up @@ -408,10 +404,11 @@ QgsVectorLayerExporter::exportLayer( QgsVectorLayer *layer,
}
n++;

if ( progress )
if ( feedback )
{
progress->setValue( n );
feedback->setProgress( 100.0 * static_cast< double >( n ) / approxTotal );
}

}

// flush the buffer to be sure that all features are written
Expand Down Expand Up @@ -495,7 +492,7 @@ bool QgsVectorLayerExporterTask::run()

mError = QgsVectorLayerExporter::exportLayer(
mLayer.data(), mDestUri, mDestProviderKey, mDestCrs, false, &mErrorMessage,
&mOptions );
&mOptions, mOwnedFeedback.get() );

if ( mOwnsLayer )
delete mLayer;
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsvectorlayerexporter.h
Expand Up @@ -75,7 +75,7 @@ class CORE_EXPORT QgsVectorLayerExporter : public QgsFeatureSink
* \param onlySelected set to true to export only selected features
* \param errorMessage if non-null, will be set to any error messages
* \param options optional provider dataset options
* \param progress optional progress dialog to show progress of export
* \param feedback optional feedback object to show progress and cancelation of export
* \returns NoError for a successful export, or encountered error
*/
static ExportError exportLayer( QgsVectorLayer *layer,
Expand All @@ -85,7 +85,7 @@ class CORE_EXPORT QgsVectorLayerExporter : public QgsFeatureSink
bool onlySelected = false,
QString *errorMessage SIP_OUT = 0,
QMap<QString, QVariant> *options = nullptr,
QProgressDialog *progress = nullptr
QgsFeedback *feedback = nullptr
);

/**
Expand Down

0 comments on commit b6a5cc4

Please sign in to comment.