Skip to content

Commit a676bde

Browse files
committedNov 10, 2016
[bugfix][backport] File downloader for identify dialog hyperlinks
fixes #14703 Include C++ and Python tests ( cherry-picked from commit bdc2e24 ) Try to convince Travis to behave like a normal mechanical being Travis won: ported all test cases to Python and disabled C++ companion test (still useful locally and for debugging) For the curious: QTemporaryFile is not working as expected ( cherry-picked from 57aa7fd )
1 parent afb4739 commit a676bde

File tree

12 files changed

+845
-2
lines changed

12 files changed

+845
-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' -S ./qgis-test-travis.ctest --output-on-failure
27+
xvfb-run ctest -V -E 'qgis_filedownloader|qgis_openstreetmaptest|qgis_wcsprovidertest|qgis_ziplayertest' -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
@@ -74,6 +74,7 @@
7474
%Include qgsfieldvalidator.sip
7575
%Include qgsfiledropedit.sip
7676
%Include qgsfilewidget.sip
77+
%Include qgsfiledownloader.sip
7778
%Include qgsfilterlineedit.sip
7879
%Include qgsformannotationitem.sip
7980
%Include qgsgenericprojectionselector.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
@@ -35,6 +35,7 @@
3535
#include "qgsvectorlayer.h"
3636
#include "qgswebview.h"
3737
#include "qgswebframe.h"
38+
#include "qgsfiledownloader.h"
3839

3940
#include <QCloseEvent>
4041
#include <QLabel>
@@ -52,6 +53,11 @@
5253
#include <QDesktopServices>
5354
#include <QMessageBox>
5455
#include <QComboBox>
56+
#include <QNetworkRequest>
57+
#include <QNetworkReply>
58+
#include <QFileDialog>
59+
#include <QFileInfo>
60+
#include <QRegExp>
5561

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

78130
void QgsIdentifyResultsWebView::print( void )

‎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( void );
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
@@ -272,6 +272,7 @@ SET(QGIS_GUI_SRCS
272272
qgsuserinputdockwidget.cpp
273273
qgsvariableeditorwidget.cpp
274274
qgsvertexmarker.cpp
275+
qgsfiledownloader.cpp
275276
)
276277

277278
IF (WITH_QTWEBKIT)
@@ -403,6 +404,7 @@ SET(QGIS_GUI_MOC_HDRS
403404
qgsunitselectionwidget.h
404405
qgsuserinputdockwidget.h
405406
qgsvariableeditorwidget.h
407+
qgsfiledownloader.h
406408

407409
raster/qgsmultibandcolorrendererwidget.h
408410
raster/qgspalettedrendererwidget.h
@@ -579,6 +581,7 @@ SET(QGIS_GUI_HDRS
579581
qgsuserinputdockwidget.h
580582
qgsvectorlayertools.h
581583
qgsvertexmarker.h
584+
qgsfiledownloader.h
582585

583586
attributetable/qgsfeaturemodel.h
584587

0 commit comments

Comments
 (0)
Please sign in to comment.