QGIS API Documentation  3.6.0-Noosa (5873452)
qgsfiledownloaderdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfiledownloaderdialog.cpp
3  --------------------------------------
4  Date : November 2016
5  Copyright : (C) 2016 by Alessandro Pasotti
6  Email : apasotti at boundlessgeo dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 #include "qgsfiledownloader.h"
18 #include "qgsfileutils.h"
19 #include <QMessageBox>
20 
21 QgsFileDownloaderDialog::QgsFileDownloaderDialog( const QUrl &url, const QString &outputFileName, const QString &authcfg )
22  : mOutputFileName( outputFileName ),
23  mDownloader( new QgsFileDownloader( url, outputFileName, authcfg, true ) )
24 {
25  setWindowTitle( tr( "Download" ) );
26  setLabelText( tr( "Downloading %1." ).arg( outputFileName ) );
27  show();
28 
29  connect( this, &QProgressDialog::canceled, mDownloader, &QgsFileDownloader::cancelDownload );
30  connect( mDownloader, &QgsFileDownloader::downloadError, this, &QgsFileDownloaderDialog::onError );
31  connect( mDownloader, &QgsFileDownloader::downloadProgress, this, &QgsFileDownloaderDialog::onDownloadProgress );
32  connect( mDownloader, &QgsFileDownloader::downloadExited, this, &QgsFileDownloaderDialog::deleteLater );
33 
39  mDownloader->startDownload();
40 }
41 
42 void QgsFileDownloaderDialog::onError( const QStringList &errors )
43 {
44  QMessageBox::warning( nullptr, tr( "Download File" ), errors.join( QStringLiteral( "<br>" ) ) );
45 }
46 
47 void QgsFileDownloaderDialog::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
48 {
49  setMaximum( bytesTotal );
50  setValue( bytesReceived );
51  setLabelText( tr( "Downloading %1 of %2 %3." ).arg( QgsFileUtils::representFileSize( bytesReceived ), QgsFileUtils::representFileSize( bytesTotal ), mOutputFileName ) );
52 }
53 
void startDownload()
Called to start the download.
static QString representFileSize(qint64 bytes)
Returns the human size from bytes.
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when data are ready to be processed.
QgsFileDownloaderDialog(const QUrl &url, const QString &outputFileName, const QString &authcfg=QString())
QgsFileDownloader.
QgsFileDownloader is a utility class for downloading files.
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when data are ready to be processed.
void downloadExited()
Emitted always when the downloader exits.
void downloadCanceled()
Emitted when the download was canceled by the user.
void downloadCompleted()
Emitted when the download has completed successfully.
void downloadError(QStringList errorMessages)
Emitted when an error makes the download fail.
void downloadCompleted()
Emitted when the download has completed successfully.
void downloadCanceled()
Emitted when the download was canceled by the user.
void downloadError(QStringList errorMessages)
Emitted when an error makes the download fail.
void cancelDownload()
Call to abort the download and delete this object after the cancelation has been processed.
void downloadExited()
Emitted always when the downloader exits.