Skip to content

Commit 2a12c6f

Browse files
committedAug 17, 2018
New class QgsScopedProxyProgressTask, which makes it easy
to create proxy progress tasks and have their lifetime managed automatically.
1 parent 54f4eef commit 2a12c6f

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed
 

‎python/core/auto_generated/qgsproxyprogresstask.sip.in

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ This method is safe to call from the main thread.
5252

5353
};
5454

55+
class QgsScopedProxyProgressTask
56+
{
57+
%Docstring
58+
59+
Scoped :py:class:`QgsScopedProxyProgressTask`, which automatically adds the proxy task
60+
to the application task manager on construction and finalizes the task
61+
when it goes out of scope.
62+
63+
.. versionadded:: 3.4
64+
%End
65+
66+
%TypeHeaderCode
67+
#include "qgsproxyprogresstask.h"
68+
%End
69+
public:
70+
71+
QgsScopedProxyProgressTask( const QString &description );
72+
%Docstring
73+
Constructor for QgsScopedProxyProgressTask, with the specified ``description``.
74+
%End
75+
76+
~QgsScopedProxyProgressTask();
77+
78+
void setProgress( double progress );
79+
%Docstring
80+
Sets the ``progress`` (from 0 to 100) for the proxied operation.
81+
%End
82+
83+
};
84+
5585
/************************************************************************
5686
* This file has been generated automatically from *
5787
* *

‎src/app/qgisapp.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,8 +1712,7 @@ void QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList &lst )
17121712
// added all layers, and only emit the signal once for the final layer added
17131713
mBlockActiveLayerChanged = true;
17141714

1715-
QgsProxyProgressTask *proxyTask = new QgsProxyProgressTask( tr( "Loading layers" ) );
1716-
QgsApplication::taskManager()->addTask( proxyTask );
1715+
QgsScopedProxyProgressTask task( tr( "Loading layers" ) );
17171716

17181717
// insert items in reverse order as each one is inserted on top of previous one
17191718
int count = 0;
@@ -1756,11 +1755,9 @@ void QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList &lst )
17561755
openFile( u.uri, QStringLiteral( "project" ) );
17571756
}
17581757

1759-
proxyTask->setProxyProgress( 100.0 * static_cast< double >( count ) / lst.size() );
1758+
task.setProgress( 100.0 * static_cast< double >( count ) / lst.size() );
17601759
}
17611760

1762-
proxyTask->finalize( true );
1763-
17641761
mBlockActiveLayerChanged = false;
17651762
emit activeLayerChanged( activeLayer() );
17661763
}

‎src/core/qgsproxyprogresstask.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,23 @@ void QgsProxyProgressTask::setProxyProgress( double progress )
4141
{
4242
QMetaObject::invokeMethod( this, "setProgress", Qt::AutoConnection, Q_ARG( double, progress ) );
4343
}
44+
45+
//
46+
// QgsScopedProxyProgressTask
47+
//
48+
49+
QgsScopedProxyProgressTask::QgsScopedProxyProgressTask( const QString &description )
50+
: mTask( new QgsProxyProgressTask( description ) )
51+
{
52+
QgsApplication::taskManager()->addTask( mTask );
53+
}
54+
55+
QgsScopedProxyProgressTask::~QgsScopedProxyProgressTask()
56+
{
57+
mTask->finalize( true );
58+
}
59+
60+
void QgsScopedProxyProgressTask::setProgress( double progress )
61+
{
62+
mTask->setProxyProgress( progress );
63+
}

‎src/core/qgsproxyprogresstask.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,35 @@ class CORE_EXPORT QgsProxyProgressTask : public QgsTask
6969

7070
};
7171

72+
/**
73+
* \ingroup core
74+
*
75+
* Scoped QgsScopedProxyProgressTask, which automatically adds the proxy task
76+
* to the application task manager on construction and finalizes the task
77+
* when it goes out of scope.
78+
*
79+
* \since QGIS 3.4
80+
*/
81+
class CORE_EXPORT QgsScopedProxyProgressTask
82+
{
83+
public:
84+
85+
/**
86+
* Constructor for QgsScopedProxyProgressTask, with the specified \a description.
87+
*/
88+
QgsScopedProxyProgressTask( const QString &description );
89+
90+
~QgsScopedProxyProgressTask();
91+
92+
/**
93+
* Sets the \a progress (from 0 to 100) for the proxied operation.
94+
*/
95+
void setProgress( double progress );
96+
97+
private:
98+
99+
QgsProxyProgressTask *mTask = nullptr;
100+
101+
};
102+
72103
#endif // QGSPROXYPROGRESSTASK_H

0 commit comments

Comments
 (0)
Please sign in to comment.