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
This commit is contained in:
Nyall Dawson 2019-01-25 10:08:36 +10:00
parent df1d47b86a
commit a491e90a04
3 changed files with 49 additions and 0 deletions

View File

@ -220,6 +220,22 @@ created in any thread.
.. seealso:: :py:func:`finished`
.. versionadded:: 3.6
%End
void downloadProgress( int requestId, qint64 bytesReceived, qint64 bytesTotal );
%Docstring
Emitted when a network reply receives a progress report.
The ``requestId`` argument reflects the unique ID identifying the original request which the progress report relates to.
The ``bytesReceived`` parameter indicates the number of bytes received, while ``bytesTotal`` indicates the total number
of bytes expected to be downloaded. If the number of bytes to be downloaded is not known, ``bytesTotal`` will be -1.
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.
.. versionadded:: 3.6
%End

View File

@ -224,6 +224,8 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
emit requestCreated( reply );
Q_NOWARN_DEPRECATED_POP
connect( reply, &QNetworkReply::downloadProgress, this, &QgsNetworkAccessManager::onReplyDownloadProgress );
// The timer will call abortRequest slot to abort the connection if needed.
// The timer is stopped by the finished signal and is restarted on downloadProgress and
// uploadProgress.
@ -262,6 +264,17 @@ void QgsNetworkAccessManager::onReplyFinished( QNetworkReply *reply )
emit finished( QgsNetworkReplyContent( reply ) );
}
void QgsNetworkAccessManager::onReplyDownloadProgress( qint64 bytesRecevied, qint64 bytesTotal )
{
if ( QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() ) )
{
bool ok = false;
int requestId = reply->property( "requestId" ).toInt( &ok );
if ( ok )
emit downloadProgress( requestId, bytesRecevied, bytesTotal );
}
}
QString QgsNetworkAccessManager::cacheLoadControlName( QNetworkRequest::CacheLoadControl control )
{
switch ( control )
@ -330,6 +343,8 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache( Qt::ConnectionType conn
connect( this, qgis::overload< QgsNetworkReplyContent >::of( &QgsNetworkAccessManager::finished ),
sMainNAM, qgis::overload< QgsNetworkReplyContent >::of( &QgsNetworkAccessManager::finished ) );
connect( this, &QgsNetworkAccessManager::downloadProgress, sMainNAM, &QgsNetworkAccessManager::downloadProgress );
#ifndef QT_NO_SSL
connect( this, &QNetworkAccessManager::sslErrors,
sMainNAM, &QNetworkAccessManager::sslErrors,

View File

@ -221,6 +221,22 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
*/
void requestTimedOut( QgsNetworkRequestParameters request );
/**
* Emitted when a network reply receives a progress report.
*
* The \a requestId argument reflects the unique ID identifying the original request which the progress report relates to.
*
* The \a bytesReceived parameter indicates the number of bytes received, while \a bytesTotal indicates the total number
* of bytes expected to be downloaded. If the number of bytes to be downloaded is not known, \a bytesTotal will be -1.
*
* 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.
*
* \since QGIS 3.6
*/
void downloadProgress( int requestId, qint64 bytesReceived, qint64 bytesTotal );
/**
* \deprecated Use the thread-safe requestAboutToBeCreated( QgsNetworkRequestParameters ) signal instead.
*/
@ -233,6 +249,8 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
void onReplyFinished( QNetworkReply *reply );
void onReplyDownloadProgress( qint64 bytesRecevied, qint64 bytesTotal );
protected:
QNetworkReply *createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData = nullptr ) override;