Skip to content

Commit 19b062c

Browse files
authored
Merge pull request #5766 from Gustry/file_downloader
Display the downloaded size in QgsFileDownloaderDialog
2 parents b22825a + 88054a3 commit 19b062c

File tree

9 files changed

+107
-20
lines changed

9 files changed

+107
-20
lines changed

python/core/core_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@
313313
%Include qgsfieldmodel.sip
314314
%Include qgsfieldproxymodel.sip
315315
%Include qgsfiledownloader.sip
316+
%Include qgsfileutils.sip
316317
%Include qgsfeaturefiltermodel.sip
317318
%Include qgsgeometryvalidator.sip
318319
%Include qgsgml.sip

python/core/qgsfileutils.sip

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsfileutils.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsFileUtils
13+
{
14+
%Docstring
15+
Class for file utilities.
16+
.. versionadded:: 3.0
17+
%End
18+
19+
%TypeHeaderCode
20+
#include "qgsfileutils.h"
21+
%End
22+
public:
23+
24+
static QString representFileSize( qint64 bytes );
25+
%Docstring
26+
Return the human size from bytes
27+
:rtype: str
28+
%End
29+
30+
};
31+
32+
/************************************************************************
33+
* This file has been generated automatically from *
34+
* *
35+
* src/core/qgsfileutils.h *
36+
* *
37+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
38+
************************************************************************/

src/analysis/processing/qgsalgorithmfiledownloader.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgsalgorithmfiledownloader.h"
1919
#include "qgsfiledownloader.h"
20+
#include "qgsfileutils.h"
2021
#include <QEventLoop>
2122
#include <QFileInfo>
2223
#include <QTimer>
@@ -110,30 +111,14 @@ void QgsFileDownloaderAlgorithm::sendProgressFeedback()
110111

111112
void QgsFileDownloaderAlgorithm::receiveProgressFromDownloader( qint64 bytesReceived, qint64 bytesTotal )
112113
{
113-
mReceived = humanSize( bytesReceived );
114+
mReceived = QgsFileUtils::representFileSize( bytesReceived );
114115
if ( bytesTotal > 0 )
115116
{
116117
if ( mTotal.isEmpty() )
117-
mTotal = humanSize( bytesTotal );
118+
mTotal = QgsFileUtils::representFileSize( bytesTotal );
118119

119120
mFeedback->setProgress( ( bytesReceived * 100 ) / bytesTotal );
120121
}
121122
}
122123

123-
QString QgsFileDownloaderAlgorithm::humanSize( qint64 bytes )
124-
{
125-
QStringList list;
126-
list << "KB" << "MB" << "GB" << "TB";
127-
128-
QStringListIterator i( list );
129-
QString unit( "bytes" );
130-
131-
while ( bytes >= 1024.0 && i.hasNext() )
132-
{
133-
unit = i.next();
134-
bytes /= 1024.0;
135-
}
136-
return QString( "%1 %2" ).arg( QString::number( bytes ) ).arg( unit );
137-
}
138-
139124
///@endcond

src/analysis/processing/qgsalgorithmfiledownloader.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class QgsFileDownloaderAlgorithm : public QgsProcessingAlgorithm, public QObject
5454
QString mLastReport;
5555
void reportErrors( QStringList errors );
5656
void receiveProgressFromDownloader( qint64 bytesReceived, qint64 bytesTotal );
57-
static QString humanSize( qint64 bytes );
5857
void sendProgressFeedback();
5958
};
6059

src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ SET(QGIS_CORE_SRCS
186186
qgsfieldproxymodel.cpp
187187
qgsfields.cpp
188188
qgsfiledownloader.cpp
189+
qgsfileutils.cpp
189190
qgsfontutils.cpp
190191
qgsgeometrysimplifier.cpp
191192
qgsgeometryvalidator.cpp
@@ -610,6 +611,7 @@ SET(QGIS_CORE_MOC_HDRS
610611
qgsfieldmodel.h
611612
qgsfieldproxymodel.h
612613
qgsfiledownloader.h
614+
qgsfileutils.h
613615
qgsfeaturefiltermodel.h
614616
qgsfeaturefiltermodel_p.h
615617
qgsgeometryvalidator.h

src/core/qgsfileutils.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "qgsfileutils.h"
2+
#include <QObject>
3+
4+
QString QgsFileUtils::representFileSize( qint64 bytes )
5+
{
6+
QStringList list;
7+
list << QObject::tr( "KB" ) << QObject::tr( "MB" ) << QObject::tr( "GB" ) << QObject::tr( "TB" );
8+
9+
QStringListIterator i( list );
10+
QString unit = QObject::tr( "bytes" );
11+
12+
while ( bytes >= 1024.0 && i.hasNext() )
13+
{
14+
unit = i.next();
15+
bytes /= 1024.0;
16+
}
17+
return QString( "%1 %2" ).arg( QString::number( bytes ), unit );
18+
}

src/core/qgsfileutils.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/***************************************************************************
2+
qgsfileutils.h
3+
---------------------------
4+
begin : November 2017
5+
copyright : (C) 2017 by Etienne Trimaille
6+
email : etienne dot trimaille 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 QGSFILEUTILS_H
19+
#define QGSFILEUTILS_H
20+
21+
#include "qgis.h"
22+
23+
/**
24+
* \ingroup core
25+
* \class QgsFileUtils
26+
* \brief Class for file utilities.
27+
* \since QGIS 3.0
28+
*/
29+
class CORE_EXPORT QgsFileUtils
30+
{
31+
public:
32+
33+
/**
34+
* Return the human size from bytes
35+
*/
36+
static QString representFileSize( qint64 bytes );
37+
38+
};
39+
40+
#endif // QGSFILEUTILS_H

src/gui/qgsfiledownloaderdialog.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515

1616
#include "qgsfiledownloaderdialog.h"
1717
#include "qgsfiledownloader.h"
18+
#include "qgsfileutils.h"
1819
#include <QMessageBox>
1920

2021
QgsFileDownloaderDialog::QgsFileDownloaderDialog( const QUrl &url, const QString &outputFileName, const QString &authcfg )
21-
: mDownloader( new QgsFileDownloader( url, outputFileName, authcfg, true ) )
22+
: mOutputFileName( outputFileName ),
23+
mDownloader( new QgsFileDownloader( url, outputFileName, authcfg, true ) )
2224
{
2325
setWindowTitle( tr( "Download" ) );
2426
setLabelText( tr( "Downloading %1." ).arg( outputFileName ) );
@@ -46,5 +48,6 @@ void QgsFileDownloaderDialog::onDownloadProgress( qint64 bytesReceived, qint64 b
4648
{
4749
setMaximum( bytesTotal );
4850
setValue( bytesReceived );
51+
setLabelText( tr( "Downloading %1 of %2 %3." ).arg( QgsFileUtils::representFileSize( bytesReceived ), QgsFileUtils::representFileSize( bytesTotal ), mOutputFileName ) );
4952
}
5053

src/gui/qgsfiledownloaderdialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class GUI_EXPORT QgsFileDownloaderDialog : public QProgressDialog
6666

6767
private:
6868

69+
QString mOutputFileName;
6970
QgsFileDownloader *mDownloader = nullptr;
7071

7172
};

0 commit comments

Comments
 (0)