New class QgsScopedProxyProgressTask, which makes it easy

to create proxy progress tasks and have their lifetime
managed automatically.
This commit is contained in:
Nyall Dawson 2018-08-17 19:56:41 +10:00
parent 54f4eefc45
commit 2a12c6feeb
4 changed files with 83 additions and 5 deletions

View File

@ -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 *
* *

View File

@ -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() );
}

View File

@ -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 );
}

View File

@ -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