diff --git a/python/core/auto_generated/qgsproxyprogresstask.sip.in b/python/core/auto_generated/qgsproxyprogresstask.sip.in index feab6d412c5..06a50e1fed8 100644 --- a/python/core/auto_generated/qgsproxyprogresstask.sip.in +++ b/python/core/auto_generated/qgsproxyprogresstask.sip.in @@ -52,6 +52,36 @@ This method is safe to call from the main thread. }; +class QgsScopedProxyProgressTask +{ +%Docstring + +Scoped :py:class:`QgsScopedProxyProgressTask`, which automatically adds the proxy task +to the application task manager on construction and finalizes the task +when it goes out of scope. + +.. versionadded:: 3.4 +%End + +%TypeHeaderCode +#include "qgsproxyprogresstask.h" +%End + public: + + QgsScopedProxyProgressTask( const QString &description ); +%Docstring +Constructor for QgsScopedProxyProgressTask, with the specified ``description``. +%End + + ~QgsScopedProxyProgressTask(); + + void setProgress( double progress ); +%Docstring +Sets the ``progress`` (from 0 to 100) for the proxied operation. +%End + +}; + /************************************************************************ * This file has been generated automatically from * * * diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index 68def1d77a9..758cf71c6f4 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -1712,8 +1712,7 @@ void QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList &lst ) // added all layers, and only emit the signal once for the final layer added mBlockActiveLayerChanged = true; - QgsProxyProgressTask *proxyTask = new QgsProxyProgressTask( tr( "Loading layers" ) ); - QgsApplication::taskManager()->addTask( proxyTask ); + QgsScopedProxyProgressTask task( tr( "Loading layers" ) ); // insert items in reverse order as each one is inserted on top of previous one int count = 0; @@ -1756,11 +1755,9 @@ void QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList &lst ) openFile( u.uri, QStringLiteral( "project" ) ); } - proxyTask->setProxyProgress( 100.0 * static_cast< double >( count ) / lst.size() ); + task.setProgress( 100.0 * static_cast< double >( count ) / lst.size() ); } - proxyTask->finalize( true ); - mBlockActiveLayerChanged = false; emit activeLayerChanged( activeLayer() ); } diff --git a/src/core/qgsproxyprogresstask.cpp b/src/core/qgsproxyprogresstask.cpp index e91d9dea60a..7a473b02b3e 100644 --- a/src/core/qgsproxyprogresstask.cpp +++ b/src/core/qgsproxyprogresstask.cpp @@ -41,3 +41,23 @@ void QgsProxyProgressTask::setProxyProgress( double progress ) { QMetaObject::invokeMethod( this, "setProgress", Qt::AutoConnection, Q_ARG( double, progress ) ); } + +// +// QgsScopedProxyProgressTask +// + +QgsScopedProxyProgressTask::QgsScopedProxyProgressTask( const QString &description ) + : mTask( new QgsProxyProgressTask( description ) ) +{ + QgsApplication::taskManager()->addTask( mTask ); +} + +QgsScopedProxyProgressTask::~QgsScopedProxyProgressTask() +{ + mTask->finalize( true ); +} + +void QgsScopedProxyProgressTask::setProgress( double progress ) +{ + mTask->setProxyProgress( progress ); +} diff --git a/src/core/qgsproxyprogresstask.h b/src/core/qgsproxyprogresstask.h index f64fe11d537..c14e130d69a 100644 --- a/src/core/qgsproxyprogresstask.h +++ b/src/core/qgsproxyprogresstask.h @@ -69,4 +69,35 @@ class CORE_EXPORT QgsProxyProgressTask : public QgsTask }; +/** + * \ingroup core + * + * Scoped QgsScopedProxyProgressTask, which automatically adds the proxy task + * to the application task manager on construction and finalizes the task + * when it goes out of scope. + * + * \since QGIS 3.4 + */ +class CORE_EXPORT QgsScopedProxyProgressTask +{ + public: + + /** + * Constructor for QgsScopedProxyProgressTask, with the specified \a description. + */ + QgsScopedProxyProgressTask( const QString &description ); + + ~QgsScopedProxyProgressTask(); + + /** + * Sets the \a progress (from 0 to 100) for the proxied operation. + */ + void setProgress( double progress ); + + private: + + QgsProxyProgressTask *mTask = nullptr; + +}; + #endif // QGSPROXYPROGRESSTASK_H