Skip to content

Commit 84bc1fc

Browse files
authored
Merge pull request #3740 from elpaso/downloader_2_18
[bugfix] File downloader for identify dialog hyperlinks
2 parents bf3c0f1 + 57aa7fd commit 84bc1fc

13 files changed

+846
-2
lines changed

ci/travis/linux/qt4/script.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then
2424
chmod -R ugo-w ~/.ccache
2525
fi
2626

27-
xvfb-run ctest -V -E 'qgis_openstreetmaptest|qgis_wcsprovidertest|qgis_ziplayertest|PyQgsDBManagerGpkg' -S ./qgis-test-travis.ctest --output-on-failure
27+
xvfb-run ctest -V -E 'qgis_filedownloader|qgis_openstreetmaptest|qgis_wcsprovidertest|qgis_ziplayertest|PyQgsDBManagerGpkg' -S ./qgis-test-travis.ctest --output-on-failure

ci/travis/linux/qt5/script.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ fi
2525

2626
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2727

28-
xvfb-run ctest -V -E "qgis_openstreetmaptest|qgis_wcsprovidertest|qgis_ziplayertest|qgis_ogcutilstest|$(cat ${DIR}/blacklist.txt | paste -sd '|' -)" -S ./qgis-test-travis.ctest --output-on-failure
28+
xvfb-run ctest -V -E "qgis_filedownloader|qgis_openstreetmaptest|qgis_wcsprovidertest|qgis_ziplayertest|qgis_ogcutilstest|$(cat ${DIR}/blacklist.txt | paste -sd '|' -)" -S ./qgis-test-travis.ctest --output-on-failure
2929
# xvfb-run ctest -V -E "qgis_openstreetmaptest|qgis_wcsprovidertest" -S ./qgis-test-travis.ctest --output-on-failure

python/gui/gui.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
%Include qgsfieldvalidator.sip
8282
%Include qgsfiledropedit.sip
8383
%Include qgsfilewidget.sip
84+
%Include qgsfiledownloader.sip
8485
%Include qgsfilterlineedit.sip
8586
%Include qgsfocuswatcher.sip
8687
%Include qgsformannotationitem.sip

python/gui/qgsfiledownloader.sip

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/***************************************************************************
2+
qgsfiledownloader.sip
3+
--------------------------------------
4+
Date : November 2016
5+
Copyright : (C) 2016 by Alessandro Pasotti
6+
Email : elpaso at itopen dot it
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+
16+
/** \ingroup gui
17+
* QgsFileDownloader is a utility class for downloading files.
18+
*
19+
* To use this class, it is necessary to pass the URL and an output file name as
20+
* arguments to the constructor, the download will start immediately.
21+
* The download is asynchronous and depending on the guiNotificationsEnabled
22+
* parameter accepted by the constructor (default = true) the class will
23+
* show a progress dialog and report all errors in a QMessageBox::warning dialog.
24+
* If the guiNotificationsEnabled parameter is set to false, the class can still
25+
* be used through the signals and slots mechanism.
26+
* The object will destroy itself when the request completes, errors or is canceled.
27+
*
28+
* @note added in QGIS 2.18.1
29+
*/
30+
class QgsFileDownloader : public QObject
31+
{
32+
%TypeHeaderCode
33+
#include <qgsfiledownloader.h>
34+
%End
35+
public:
36+
/**
37+
* QgsFileDownloader
38+
* @param url the download url
39+
* @param outputFileName file name where the downloaded content will be stored
40+
* @param guiNotificationsEnabled if false, the downloader will not display any progress bar or error message
41+
*/
42+
QgsFileDownloader(QUrl url, QString outputFileName, bool guiNotificationsEnabled = true);
43+
44+
signals:
45+
/** Emitted when the download has completed successfully */
46+
void downloadCompleted();
47+
/** Emitted always when the downloader exits */
48+
void downloadExited();
49+
/** Emitted when the download was canceled by the user */
50+
void downloadCanceled();
51+
/** Emitted when an error makes the download fail */
52+
void downloadError( QStringList errorMessages );
53+
/** Emitted when data ready to be processed */
54+
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
55+
56+
public slots:
57+
/**
58+
* Called when a download is canceled by the user
59+
* this slot aborts the download and deletes the object
60+
*/
61+
void onDownloadCanceled();
62+
63+
private:
64+
~QgsFileDownloader();
65+
66+
};

src/app/qgsidentifyresultsdialog.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "qgswebview.h"
3939
#include "qgswebframe.h"
4040
#include "qgsstringutils.h"
41+
#include "qgsfiledownloader.h"
4142

4243
#include <QCloseEvent>
4344
#include <QLabel>
@@ -55,6 +56,11 @@
5556
#include <QMessageBox>
5657
#include <QComboBox>
5758
#include <QTextDocument>
59+
#include <QNetworkRequest>
60+
#include <QNetworkReply>
61+
#include <QFileDialog>
62+
#include <QFileInfo>
63+
#include <QRegExp>
5864

5965
//graph
6066
#include <qwt_plot.h>
@@ -68,13 +74,59 @@ QgsIdentifyResultsWebView::QgsIdentifyResultsWebView( QWidget *parent ) : QgsWeb
6874
setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
6975
page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
7076
// page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
77+
page()->setForwardUnsupportedContent( true );
7178
page()->setLinkDelegationPolicy( QWebPage::DontDelegateLinks );
7279
settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
7380
settings()->setAttribute( QWebSettings::JavascriptCanOpenWindows, true );
7481
settings()->setAttribute( QWebSettings::PluginsEnabled, true );
7582
#ifdef QGISDEBUG
7683
settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
7784
#endif
85+
connect( page(), SIGNAL( downloadRequested( QNetworkRequest ) ), this, SLOT( downloadRequested( QNetworkRequest ) ) );
86+
connect( page(), SIGNAL( unsupportedContent( QNetworkReply* ) ), this, SLOT( unsupportedContent( QNetworkReply* ) ) );
87+
}
88+
89+
90+
void QgsIdentifyResultsWebView::downloadRequested( const QNetworkRequest &request )
91+
{
92+
handleDownload( request.url() );
93+
}
94+
95+
void QgsIdentifyResultsWebView::unsupportedContent( QNetworkReply * reply )
96+
{
97+
handleDownload( reply->url() );
98+
}
99+
100+
void QgsIdentifyResultsWebView::handleDownload( QUrl url )
101+
{
102+
if ( ! url.isValid() )
103+
{
104+
QMessageBox::warning( this, tr( "Invalid URL" ), tr( "The download URL is not valid: %1" ).arg( url.toString( ) ) );
105+
}
106+
else
107+
{
108+
const QString DOWNLOADER_LAST_DIR_KEY( "Qgis/fileDownloaderLastDir" );
109+
QSettings settings;
110+
// Try to get some information from the URL
111+
QFileInfo info( url.toString( ) );
112+
QString savePath = settings.value( DOWNLOADER_LAST_DIR_KEY ).toString( );
113+
QString fileName = info.fileName().replace( QRegExp( "[^A-z0-9\\-_\\.]" ), "_" );
114+
if ( ! savePath.isEmpty() && ! fileName.isEmpty( ) )
115+
{
116+
savePath = QDir::cleanPath( savePath + QDir::separator() + fileName );
117+
}
118+
QString targetFile = QFileDialog::getSaveFileName( this,
119+
tr( "Save as" ),
120+
savePath,
121+
info.suffix( ).isEmpty() ? QString( ) : "*." + info.suffix( )
122+
);
123+
if ( ! targetFile.isEmpty() )
124+
{
125+
settings.setValue( DOWNLOADER_LAST_DIR_KEY, QFileInfo( targetFile ).dir().absolutePath( ) );
126+
// Start the download
127+
new QgsFileDownloader( url, targetFile );
128+
}
129+
}
78130
}
79131

80132
void QgsIdentifyResultsWebView::print()

src/app/qgsidentifyresultsdialog.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
#include <QWidget>
3333
#include <QList>
34+
#include <QNetworkRequest>
35+
#include <QNetworkReply>
36+
#include <QUrl>
3437

3538
class QCloseEvent;
3639
class QTreeWidgetItem;
@@ -57,9 +60,13 @@ class APP_EXPORT QgsIdentifyResultsWebView : public QgsWebView
5760
QSize sizeHint() const override;
5861
public slots:
5962
void print();
63+
void downloadRequested( const QNetworkRequest &request );
64+
void unsupportedContent( QNetworkReply *reply );
6065
protected:
6166
void contextMenuEvent( QContextMenuEvent* ) override;
6267
QgsWebView *createWindow( QWebPage::WebWindowType type ) override;
68+
private:
69+
void handleDownload( QUrl url );
6370
};
6471

6572
class APP_EXPORT QgsIdentifyResultsFeatureItem: public QTreeWidgetItem

src/gui/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ SET(QGIS_GUI_SRCS
307307
qgsuserinputdockwidget.cpp
308308
qgsvariableeditorwidget.cpp
309309
qgsvertexmarker.cpp
310+
qgsfiledownloader.cpp
310311
)
311312

312313
IF (WITH_QTWEBKIT)
@@ -454,6 +455,7 @@ SET(QGIS_GUI_MOC_HDRS
454455
qgsunitselectionwidget.h
455456
qgsuserinputdockwidget.h
456457
qgsvariableeditorwidget.h
458+
qgsfiledownloader.h
457459

458460
raster/qgsmultibandcolorrendererwidget.h
459461
raster/qgspalettedrendererwidget.h
@@ -648,6 +650,7 @@ SET(QGIS_GUI_HDRS
648650
qgsuserinputdockwidget.h
649651
qgsvectorlayertools.h
650652
qgsvertexmarker.h
653+
qgsfiledownloader.h
651654

652655
attributetable/qgsfeaturemodel.h
653656

0 commit comments

Comments
 (0)