Skip to content

Commit

Permalink
New class QgsScopedProxyProgressTask, which makes it easy
Browse files Browse the repository at this point in the history
to create proxy progress tasks and have their lifetime
managed automatically.
  • Loading branch information
nyalldawson committed Aug 17, 2018
1 parent 54f4eef commit 2a12c6f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
30 changes: 30 additions & 0 deletions python/core/auto_generated/qgsproxyprogresstask.sip.in
Expand Up @@ -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 *
* *
Expand Down
7 changes: 2 additions & 5 deletions src/app/qgisapp.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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() );
}
Expand Down
20 changes: 20 additions & 0 deletions src/core/qgsproxyprogresstask.cpp
Expand Up @@ -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 );
}
31 changes: 31 additions & 0 deletions src/core/qgsproxyprogresstask.h
Expand Up @@ -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

0 comments on commit 2a12c6f

Please sign in to comment.