Skip to content

Commit a491e90

Browse files
committedJan 25, 2019
Add QgsNetworkAccessManager signal for reply download progress
This signal is propagated to the main thread QgsNetworkAccessManager instance, so it is necessary only to connect to the main thread's signal in order to receive notifications about requests created in any thread. Also includes the original requestId to allow linked download progress to original request
1 parent df1d47b commit a491e90

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
 

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,22 @@ created in any thread.
220220

221221
.. seealso:: :py:func:`finished`
222222

223+
.. versionadded:: 3.6
224+
%End
225+
226+
void downloadProgress( int requestId, qint64 bytesReceived, qint64 bytesTotal );
227+
%Docstring
228+
Emitted when a network reply receives a progress report.
229+
230+
The ``requestId`` argument reflects the unique ID identifying the original request which the progress report relates to.
231+
232+
The ``bytesReceived`` parameter indicates the number of bytes received, while ``bytesTotal`` indicates the total number
233+
of bytes expected to be downloaded. If the number of bytes to be downloaded is not known, ``bytesTotal`` will be -1.
234+
235+
This signal is propagated to the main thread QgsNetworkAccessManager instance, so it is necessary
236+
only to connect to the main thread's signal in order to receive notifications about requests
237+
created in any thread.
238+
223239
.. versionadded:: 3.6
224240
%End
225241

‎src/core/qgsnetworkaccessmanager.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
224224
emit requestCreated( reply );
225225
Q_NOWARN_DEPRECATED_POP
226226

227+
connect( reply, &QNetworkReply::downloadProgress, this, &QgsNetworkAccessManager::onReplyDownloadProgress );
228+
227229
// The timer will call abortRequest slot to abort the connection if needed.
228230
// The timer is stopped by the finished signal and is restarted on downloadProgress and
229231
// uploadProgress.
@@ -262,6 +264,17 @@ void QgsNetworkAccessManager::onReplyFinished( QNetworkReply *reply )
262264
emit finished( QgsNetworkReplyContent( reply ) );
263265
}
264266

267+
void QgsNetworkAccessManager::onReplyDownloadProgress( qint64 bytesRecevied, qint64 bytesTotal )
268+
{
269+
if ( QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() ) )
270+
{
271+
bool ok = false;
272+
int requestId = reply->property( "requestId" ).toInt( &ok );
273+
if ( ok )
274+
emit downloadProgress( requestId, bytesRecevied, bytesTotal );
275+
}
276+
}
277+
265278
QString QgsNetworkAccessManager::cacheLoadControlName( QNetworkRequest::CacheLoadControl control )
266279
{
267280
switch ( control )
@@ -330,6 +343,8 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache( Qt::ConnectionType conn
330343
connect( this, qgis::overload< QgsNetworkReplyContent >::of( &QgsNetworkAccessManager::finished ),
331344
sMainNAM, qgis::overload< QgsNetworkReplyContent >::of( &QgsNetworkAccessManager::finished ) );
332345

346+
connect( this, &QgsNetworkAccessManager::downloadProgress, sMainNAM, &QgsNetworkAccessManager::downloadProgress );
347+
333348
#ifndef QT_NO_SSL
334349
connect( this, &QNetworkAccessManager::sslErrors,
335350
sMainNAM, &QNetworkAccessManager::sslErrors,

‎src/core/qgsnetworkaccessmanager.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,22 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
221221
*/
222222
void requestTimedOut( QgsNetworkRequestParameters request );
223223

224+
/**
225+
* Emitted when a network reply receives a progress report.
226+
*
227+
* The \a requestId argument reflects the unique ID identifying the original request which the progress report relates to.
228+
*
229+
* The \a bytesReceived parameter indicates the number of bytes received, while \a bytesTotal indicates the total number
230+
* of bytes expected to be downloaded. If the number of bytes to be downloaded is not known, \a bytesTotal will be -1.
231+
*
232+
* This signal is propagated to the main thread QgsNetworkAccessManager instance, so it is necessary
233+
* only to connect to the main thread's signal in order to receive notifications about requests
234+
* created in any thread.
235+
*
236+
* \since QGIS 3.6
237+
*/
238+
void downloadProgress( int requestId, qint64 bytesReceived, qint64 bytesTotal );
239+
224240
/**
225241
* \deprecated Use the thread-safe requestAboutToBeCreated( QgsNetworkRequestParameters ) signal instead.
226242
*/
@@ -233,6 +249,8 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
233249

234250
void onReplyFinished( QNetworkReply *reply );
235251

252+
void onReplyDownloadProgress( qint64 bytesRecevied, qint64 bytesTotal );
253+
236254
protected:
237255
QNetworkReply *createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData = nullptr ) override;
238256

0 commit comments

Comments
 (0)
Please sign in to comment.