Skip to content

Commit

Permalink
Add a QgsTask subclass for proxying progress reports from a
Browse files Browse the repository at this point in the history
blocking operation via task manager

Allows use of the task manager progress reporting system from
operations which are blocking (and cannot be made background
tasks!), e.g. layout exporting, project loading.
  • Loading branch information
nyalldawson committed Aug 15, 2018
1 parent d8adad8 commit 21693bd
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 0 deletions.
61 changes: 61 additions & 0 deletions python/core/auto_generated/qgsproxyprogresstask.sip.in
@@ -0,0 +1,61 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsproxyprogresstask.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsProxyProgressTask : QgsTask
{
%Docstring

A QgsTask shell which proxies progress reports.

Simple task shell which runs until finalized and reports progress only.
This is usually used to expose a blocking operation's progress via
task manager.

.. versionadded:: 3.4
%End

%TypeHeaderCode
#include "qgsproxyprogresstask.h"
%End
public:

QgsProxyProgressTask( const QString &description );
%Docstring
Constructor for QgsProxyProgressTask, with the specified ``description``.
%End

void finalize( bool result );
%Docstring
Finalizes the task, with the specified ``result``.

This should be called when the operation being proxied has completed,
to remove this proxy task from the task manager.
%End

virtual bool run();


void setProxyProgress( double progress );
%Docstring
Sets the ``progress`` (from 0 to 100) for the proxied operation.

This method is safe to call from the main thread.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsproxyprogresstask.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -348,6 +348,7 @@
%Include auto_generated/qgspluginlayer.sip
%Include auto_generated/qgspointxy.sip
%Include auto_generated/qgsproject.sip
%Include auto_generated/qgsproxyprogresstask.sip
%Include auto_generated/qgsrelationmanager.sip
%Include auto_generated/qgsrelation.sip
%Include auto_generated/qgsrunprocess.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -276,6 +276,7 @@ SET(QGIS_CORE_SRCS
qgspropertytransformer.cpp
qgsprovidermetadata.cpp
qgsproviderregistry.cpp
qgsproxyprogresstask.cpp
qgspythonrunner.cpp
qgsreadwritecontext.cpp
qgsrelation.cpp
Expand Down Expand Up @@ -627,6 +628,7 @@ SET(QGIS_CORE_MOC_HDRS
qgspointxy.h
qgspointlocator.h
qgsproject.h
qgsproxyprogresstask.h
qgsrelationmanager.h
qgsrelation.h
qgsrunprocess.h
Expand Down
43 changes: 43 additions & 0 deletions src/core/qgsproxyprogresstask.cpp
@@ -0,0 +1,43 @@
/***************************************************************************
qgsproxyprogresstask.cpp
------------------------
begin : August 2018
copyright : (C) 2018 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsproxyprogresstask.h"

QgsProxyProgressTask::QgsProxyProgressTask( const QString &description )
: QgsTask( description, QgsTask::Flags() )
{
}

void QgsProxyProgressTask::finalize( bool result )
{
mResult = result;
mNotFinishedWaitCondition.wakeAll();
}

bool QgsProxyProgressTask::run()
{
mNotFinishedMutex.lock();
mNotFinishedWaitCondition.wait( &mNotFinishedMutex );
mNotFinishedMutex.unlock();

return mResult;
}

void QgsProxyProgressTask::setProxyProgress( double progress )
{
QMetaObject::invokeMethod( this, "setProgress", Qt::AutoConnection, Q_ARG( double, progress ) );
}
72 changes: 72 additions & 0 deletions src/core/qgsproxyprogresstask.h
@@ -0,0 +1,72 @@
/***************************************************************************
qgsproxyprogresstask.h
----------------------
begin : August 2018
copyright : (C) 2018 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSPROXYPROGRESSTASK_H
#define QGSPROXYPROGRESSTASK_H

#include "qgsvectorlayer.h"
#include "qgsvirtuallayerdefinition.h"
#include "qgstaskmanager.h"

/**
* \ingroup core
*
* A QgsTask shell which proxies progress reports.
*
* Simple task shell which runs until finalized and reports progress only.
* This is usually used to expose a blocking operation's progress via
* task manager.
*
* \since QGIS 3.4
*/
class CORE_EXPORT QgsProxyProgressTask : public QgsTask
{
Q_OBJECT

public:

/**
* Constructor for QgsProxyProgressTask, with the specified \a description.
*/
QgsProxyProgressTask( const QString &description );

/**
* Finalizes the task, with the specified \a result.
*
* This should be called when the operation being proxied has completed,
* to remove this proxy task from the task manager.
*/
void finalize( bool result );

bool run() override;

/**
* Sets the \a progress (from 0 to 100) for the proxied operation.
*
* This method is safe to call from the main thread.
*/
void setProxyProgress( double progress );

private:

QWaitCondition mNotFinishedWaitCondition;
QMutex mNotFinishedMutex;
bool mResult = true;

};

#endif // QGSPROXYPROGRESSTASK_H

0 comments on commit 21693bd

Please sign in to comment.