Skip to content

Commit 21693bd

Browse files
committed
Add a QgsTask subclass for proxying progress reports from a
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.
1 parent d8adad8 commit 21693bd

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsproxyprogresstask.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsProxyProgressTask : QgsTask
13+
{
14+
%Docstring
15+
16+
A QgsTask shell which proxies progress reports.
17+
18+
Simple task shell which runs until finalized and reports progress only.
19+
This is usually used to expose a blocking operation's progress via
20+
task manager.
21+
22+
.. versionadded:: 3.4
23+
%End
24+
25+
%TypeHeaderCode
26+
#include "qgsproxyprogresstask.h"
27+
%End
28+
public:
29+
30+
QgsProxyProgressTask( const QString &description );
31+
%Docstring
32+
Constructor for QgsProxyProgressTask, with the specified ``description``.
33+
%End
34+
35+
void finalize( bool result );
36+
%Docstring
37+
Finalizes the task, with the specified ``result``.
38+
39+
This should be called when the operation being proxied has completed,
40+
to remove this proxy task from the task manager.
41+
%End
42+
43+
virtual bool run();
44+
45+
46+
void setProxyProgress( double progress );
47+
%Docstring
48+
Sets the ``progress`` (from 0 to 100) for the proxied operation.
49+
50+
This method is safe to call from the main thread.
51+
%End
52+
53+
};
54+
55+
/************************************************************************
56+
* This file has been generated automatically from *
57+
* *
58+
* src/core/qgsproxyprogresstask.h *
59+
* *
60+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
61+
************************************************************************/

python/core/core_auto.sip

+1
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@
348348
%Include auto_generated/qgspluginlayer.sip
349349
%Include auto_generated/qgspointxy.sip
350350
%Include auto_generated/qgsproject.sip
351+
%Include auto_generated/qgsproxyprogresstask.sip
351352
%Include auto_generated/qgsrelationmanager.sip
352353
%Include auto_generated/qgsrelation.sip
353354
%Include auto_generated/qgsrunprocess.sip

src/core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ SET(QGIS_CORE_SRCS
276276
qgspropertytransformer.cpp
277277
qgsprovidermetadata.cpp
278278
qgsproviderregistry.cpp
279+
qgsproxyprogresstask.cpp
279280
qgspythonrunner.cpp
280281
qgsreadwritecontext.cpp
281282
qgsrelation.cpp
@@ -627,6 +628,7 @@ SET(QGIS_CORE_MOC_HDRS
627628
qgspointxy.h
628629
qgspointlocator.h
629630
qgsproject.h
631+
qgsproxyprogresstask.h
630632
qgsrelationmanager.h
631633
qgsrelation.h
632634
qgsrunprocess.h

src/core/qgsproxyprogresstask.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/***************************************************************************
2+
qgsproxyprogresstask.cpp
3+
------------------------
4+
begin : August 2018
5+
copyright : (C) 2018 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsproxyprogresstask.h"
19+
20+
QgsProxyProgressTask::QgsProxyProgressTask( const QString &description )
21+
: QgsTask( description, QgsTask::Flags() )
22+
{
23+
}
24+
25+
void QgsProxyProgressTask::finalize( bool result )
26+
{
27+
mResult = result;
28+
mNotFinishedWaitCondition.wakeAll();
29+
}
30+
31+
bool QgsProxyProgressTask::run()
32+
{
33+
mNotFinishedMutex.lock();
34+
mNotFinishedWaitCondition.wait( &mNotFinishedMutex );
35+
mNotFinishedMutex.unlock();
36+
37+
return mResult;
38+
}
39+
40+
void QgsProxyProgressTask::setProxyProgress( double progress )
41+
{
42+
QMetaObject::invokeMethod( this, "setProgress", Qt::AutoConnection, Q_ARG( double, progress ) );
43+
}

src/core/qgsproxyprogresstask.h

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/***************************************************************************
2+
qgsproxyprogresstask.h
3+
----------------------
4+
begin : August 2018
5+
copyright : (C) 2018 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGSPROXYPROGRESSTASK_H
19+
#define QGSPROXYPROGRESSTASK_H
20+
21+
#include "qgsvectorlayer.h"
22+
#include "qgsvirtuallayerdefinition.h"
23+
#include "qgstaskmanager.h"
24+
25+
/**
26+
* \ingroup core
27+
*
28+
* A QgsTask shell which proxies progress reports.
29+
*
30+
* Simple task shell which runs until finalized and reports progress only.
31+
* This is usually used to expose a blocking operation's progress via
32+
* task manager.
33+
*
34+
* \since QGIS 3.4
35+
*/
36+
class CORE_EXPORT QgsProxyProgressTask : public QgsTask
37+
{
38+
Q_OBJECT
39+
40+
public:
41+
42+
/**
43+
* Constructor for QgsProxyProgressTask, with the specified \a description.
44+
*/
45+
QgsProxyProgressTask( const QString &description );
46+
47+
/**
48+
* Finalizes the task, with the specified \a result.
49+
*
50+
* This should be called when the operation being proxied has completed,
51+
* to remove this proxy task from the task manager.
52+
*/
53+
void finalize( bool result );
54+
55+
bool run() override;
56+
57+
/**
58+
* Sets the \a progress (from 0 to 100) for the proxied operation.
59+
*
60+
* This method is safe to call from the main thread.
61+
*/
62+
void setProxyProgress( double progress );
63+
64+
private:
65+
66+
QWaitCondition mNotFinishedWaitCondition;
67+
QMutex mNotFinishedMutex;
68+
bool mResult = true;
69+
70+
};
71+
72+
#endif // QGSPROXYPROGRESSTASK_H

0 commit comments

Comments
 (0)