Skip to content

Commit

Permalink
display the downloaded size in QgsFileDownloaderDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Nov 30, 2017
1 parent ded892e commit 88054a3
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 20 deletions.
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@
%Include qgsfieldmodel.sip
%Include qgsfieldproxymodel.sip
%Include qgsfiledownloader.sip
%Include qgsfileutils.sip
%Include qgsfeaturefiltermodel.sip
%Include qgsgeometryvalidator.sip
%Include qgsgml.sip
Expand Down
38 changes: 38 additions & 0 deletions python/core/qgsfileutils.sip
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsfileutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsFileUtils
{
%Docstring
Class for file utilities.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsfileutils.h"
%End
public:

static QString representFileSize( qint64 bytes );
%Docstring
Return the human size from bytes
:rtype: str
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsfileutils.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
21 changes: 3 additions & 18 deletions src/analysis/processing/qgsalgorithmfiledownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "qgsalgorithmfiledownloader.h"
#include "qgsfiledownloader.h"
#include "qgsfileutils.h"
#include <QEventLoop>
#include <QFileInfo>
#include <QTimer>
Expand Down Expand Up @@ -110,30 +111,14 @@ void QgsFileDownloaderAlgorithm::sendProgressFeedback()

void QgsFileDownloaderAlgorithm::receiveProgressFromDownloader( qint64 bytesReceived, qint64 bytesTotal )
{
mReceived = humanSize( bytesReceived );
mReceived = QgsFileUtils::representFileSize( bytesReceived );
if ( bytesTotal > 0 )
{
if ( mTotal.isEmpty() )
mTotal = humanSize( bytesTotal );
mTotal = QgsFileUtils::representFileSize( bytesTotal );

mFeedback->setProgress( ( bytesReceived * 100 ) / bytesTotal );
}
}

QString QgsFileDownloaderAlgorithm::humanSize( qint64 bytes )
{
QStringList list;
list << "KB" << "MB" << "GB" << "TB";

QStringListIterator i( list );
QString unit( "bytes" );

while ( bytes >= 1024.0 && i.hasNext() )
{
unit = i.next();
bytes /= 1024.0;
}
return QString( "%1 %2" ).arg( QString::number( bytes ) ).arg( unit );
}

///@endcond
1 change: 0 additions & 1 deletion src/analysis/processing/qgsalgorithmfiledownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class QgsFileDownloaderAlgorithm : public QgsProcessingAlgorithm, public QObject
QString mLastReport;
void reportErrors( QStringList errors );
void receiveProgressFromDownloader( qint64 bytesReceived, qint64 bytesTotal );
static QString humanSize( qint64 bytes );
void sendProgressFeedback();
};

Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ SET(QGIS_CORE_SRCS
qgsfieldproxymodel.cpp
qgsfields.cpp
qgsfiledownloader.cpp
qgsfileutils.cpp
qgsfontutils.cpp
qgsgeometrysimplifier.cpp
qgsgeometryvalidator.cpp
Expand Down Expand Up @@ -601,6 +602,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsfieldmodel.h
qgsfieldproxymodel.h
qgsfiledownloader.h
qgsfileutils.h
qgsfeaturefiltermodel.h
qgsfeaturefiltermodel_p.h
qgsgeometryvalidator.h
Expand Down
18 changes: 18 additions & 0 deletions src/core/qgsfileutils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "qgsfileutils.h"
#include <QObject>

QString QgsFileUtils::representFileSize( qint64 bytes )
{
QStringList list;
list << QObject::tr( "KB" ) << QObject::tr( "MB" ) << QObject::tr( "GB" ) << QObject::tr( "TB" );

QStringListIterator i( list );
QString unit = QObject::tr( "bytes" );

while ( bytes >= 1024.0 && i.hasNext() )
{
unit = i.next();
bytes /= 1024.0;
}
return QString( "%1 %2" ).arg( QString::number( bytes ), unit );
}
40 changes: 40 additions & 0 deletions src/core/qgsfileutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/***************************************************************************
qgsfileutils.h
---------------------------
begin : November 2017
copyright : (C) 2017 by Etienne Trimaille
email : etienne dot trimaille at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSFILEUTILS_H
#define QGSFILEUTILS_H

#include "qgis.h"

/**
* \ingroup core
* \class QgsFileUtils
* \brief Class for file utilities.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsFileUtils
{
public:

/**
* Return the human size from bytes
*/
static QString representFileSize( qint64 bytes );

};

#endif // QGSFILEUTILS_H
5 changes: 4 additions & 1 deletion src/gui/qgsfiledownloaderdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@

#include "qgsfiledownloaderdialog.h"
#include "qgsfiledownloader.h"
#include "qgsfileutils.h"
#include <QMessageBox>

QgsFileDownloaderDialog::QgsFileDownloaderDialog( const QUrl &url, const QString &outputFileName, const QString &authcfg )
: mDownloader( new QgsFileDownloader( url, outputFileName, authcfg, true ) )
: mOutputFileName( outputFileName ),
mDownloader( new QgsFileDownloader( url, outputFileName, authcfg, true ) )
{
setWindowTitle( tr( "Download" ) );
setLabelText( tr( "Downloading %1." ).arg( outputFileName ) );
Expand Down Expand Up @@ -46,5 +48,6 @@ void QgsFileDownloaderDialog::onDownloadProgress( qint64 bytesReceived, qint64 b
{
setMaximum( bytesTotal );
setValue( bytesReceived );
setLabelText( tr( "Downloading %1 of %2 %3." ).arg( QgsFileUtils::representFileSize( bytesReceived ), QgsFileUtils::representFileSize( bytesTotal ), mOutputFileName ) );
}

1 change: 1 addition & 0 deletions src/gui/qgsfiledownloaderdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class GUI_EXPORT QgsFileDownloaderDialog : public QProgressDialog

private:

QString mOutputFileName;
QgsFileDownloader *mDownloader = nullptr;

};
Expand Down

0 comments on commit 88054a3

Please sign in to comment.