Skip to content

Commit

Permalink
[bugfix][backport] File downloader for identify dialog hyperlinks
Browse files Browse the repository at this point in the history
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 )
  • Loading branch information
elpaso committed Nov 10, 2016
1 parent afb4739 commit a676bde
Show file tree
Hide file tree
Showing 12 changed files with 845 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ci/travis/linux/qt4/script.sh
Expand Up @@ -24,4 +24,4 @@ if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then
chmod -R ugo-w ~/.ccache
fi

xvfb-run ctest -V -E 'qgis_openstreetmaptest|qgis_wcsprovidertest|qgis_ziplayertest' -S ./qgis-test-travis.ctest --output-on-failure
xvfb-run ctest -V -E 'qgis_filedownloader|qgis_openstreetmaptest|qgis_wcsprovidertest|qgis_ziplayertest' -S ./qgis-test-travis.ctest --output-on-failure
1 change: 1 addition & 0 deletions python/gui/gui.sip
Expand Up @@ -74,6 +74,7 @@
%Include qgsfieldvalidator.sip
%Include qgsfiledropedit.sip
%Include qgsfilewidget.sip
%Include qgsfiledownloader.sip
%Include qgsfilterlineedit.sip
%Include qgsformannotationitem.sip
%Include qgsgenericprojectionselector.sip
Expand Down
66 changes: 66 additions & 0 deletions python/gui/qgsfiledownloader.sip
@@ -0,0 +1,66 @@
/***************************************************************************
qgsfiledownloader.sip
--------------------------------------
Date : November 2016
Copyright : (C) 2016 by Alessandro Pasotti
Email : elpaso at itopen dot it
***************************************************************************
* *
* 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. *
* *
***************************************************************************/

/** \ingroup gui
* QgsFileDownloader is a utility class for downloading files.
*
* To use this class, it is necessary to pass the URL and an output file name as
* arguments to the constructor, the download will start immediately.
* The download is asynchronous and depending on the guiNotificationsEnabled
* parameter accepted by the constructor (default = true) the class will
* show a progress dialog and report all errors in a QMessageBox::warning dialog.
* If the guiNotificationsEnabled parameter is set to false, the class can still
* be used through the signals and slots mechanism.
* The object will destroy itself when the request completes, errors or is canceled.
*
* @note added in QGIS 2.18.1
*/
class QgsFileDownloader : public QObject
{
%TypeHeaderCode
#include <qgsfiledownloader.h>
%End
public:
/**
* QgsFileDownloader
* @param url the download url
* @param outputFileName file name where the downloaded content will be stored
* @param guiNotificationsEnabled if false, the downloader will not display any progress bar or error message
*/
QgsFileDownloader(QUrl url, QString outputFileName, bool guiNotificationsEnabled = true);

signals:
/** Emitted when the download has completed successfully */
void downloadCompleted();
/** Emitted always when the downloader exits */
void downloadExited();
/** Emitted when the download was canceled by the user */
void downloadCanceled();
/** Emitted when an error makes the download fail */
void downloadError( QStringList errorMessages );
/** Emitted when data ready to be processed */
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);

public slots:
/**
* Called when a download is canceled by the user
* this slot aborts the download and deletes the object
*/
void onDownloadCanceled();

private:
~QgsFileDownloader();

};
52 changes: 52 additions & 0 deletions src/app/qgsidentifyresultsdialog.cpp
Expand Up @@ -35,6 +35,7 @@
#include "qgsvectorlayer.h"
#include "qgswebview.h"
#include "qgswebframe.h"
#include "qgsfiledownloader.h"

#include <QCloseEvent>
#include <QLabel>
Expand All @@ -52,6 +53,11 @@
#include <QDesktopServices>
#include <QMessageBox>
#include <QComboBox>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QFileDialog>
#include <QFileInfo>
#include <QRegExp>

//graph
#include <qwt_plot.h>
Expand All @@ -66,13 +72,59 @@ QgsIdentifyResultsWebView::QgsIdentifyResultsWebView( QWidget *parent ) : QgsWeb
setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
// page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
page()->setForwardUnsupportedContent( true );
page()->setLinkDelegationPolicy( QWebPage::DontDelegateLinks );
settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
settings()->setAttribute( QWebSettings::JavascriptCanOpenWindows, true );
settings()->setAttribute( QWebSettings::PluginsEnabled, true );
#ifdef QGISDEBUG
settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
#endif
connect( page(), SIGNAL( downloadRequested( QNetworkRequest ) ), this, SLOT( downloadRequested( QNetworkRequest ) ) );
connect( page(), SIGNAL( unsupportedContent( QNetworkReply* ) ), this, SLOT( unsupportedContent( QNetworkReply* ) ) );
}


void QgsIdentifyResultsWebView::downloadRequested( const QNetworkRequest &request )
{
handleDownload( request.url() );
}

void QgsIdentifyResultsWebView::unsupportedContent( QNetworkReply * reply )
{
handleDownload( reply->url() );
}

void QgsIdentifyResultsWebView::handleDownload( QUrl url )
{
if ( ! url.isValid() )
{
QMessageBox::warning( this, tr( "Invalid URL" ), tr( "The download URL is not valid: %1" ).arg( url.toString( ) ) );
}
else
{
const QString DOWNLOADER_LAST_DIR_KEY( "Qgis/fileDownloaderLastDir" );
QSettings settings;
// Try to get some information from the URL
QFileInfo info( url.toString( ) );
QString savePath = settings.value( DOWNLOADER_LAST_DIR_KEY ).toString( );
QString fileName = info.fileName().replace( QRegExp( "[^A-z0-9\\-_\\.]" ), "_" );
if ( ! savePath.isEmpty() && ! fileName.isEmpty( ) )
{
savePath = QDir::cleanPath( savePath + QDir::separator() + fileName );
}
QString targetFile = QFileDialog::getSaveFileName( this,
tr( "Save as" ),
savePath,
info.suffix( ).isEmpty() ? QString( ) : "*." + info.suffix( )
);
if ( ! targetFile.isEmpty() )
{
settings.setValue( DOWNLOADER_LAST_DIR_KEY, QFileInfo( targetFile ).dir().absolutePath( ) );
// Start the download
new QgsFileDownloader( url, targetFile );
}
}
}

void QgsIdentifyResultsWebView::print( void )
Expand Down
7 changes: 7 additions & 0 deletions src/app/qgsidentifyresultsdialog.h
Expand Up @@ -31,6 +31,9 @@

#include <QWidget>
#include <QList>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>

class QCloseEvent;
class QTreeWidgetItem;
Expand All @@ -57,9 +60,13 @@ class APP_EXPORT QgsIdentifyResultsWebView : public QgsWebView
QSize sizeHint() const override;
public slots:
void print( void );
void downloadRequested( const QNetworkRequest &request );
void unsupportedContent( QNetworkReply *reply );
protected:
void contextMenuEvent( QContextMenuEvent* ) override;
QgsWebView *createWindow( QWebPage::WebWindowType type ) override;
private:
void handleDownload( QUrl url );
};

class APP_EXPORT QgsIdentifyResultsFeatureItem: public QTreeWidgetItem
Expand Down
3 changes: 3 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -272,6 +272,7 @@ SET(QGIS_GUI_SRCS
qgsuserinputdockwidget.cpp
qgsvariableeditorwidget.cpp
qgsvertexmarker.cpp
qgsfiledownloader.cpp
)

IF (WITH_QTWEBKIT)
Expand Down Expand Up @@ -403,6 +404,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsunitselectionwidget.h
qgsuserinputdockwidget.h
qgsvariableeditorwidget.h
qgsfiledownloader.h

raster/qgsmultibandcolorrendererwidget.h
raster/qgspalettedrendererwidget.h
Expand Down Expand Up @@ -579,6 +581,7 @@ SET(QGIS_GUI_HDRS
qgsuserinputdockwidget.h
qgsvectorlayertools.h
qgsvertexmarker.h
qgsfiledownloader.h

attributetable/qgsfeaturemodel.h

Expand Down

0 comments on commit a676bde

Please sign in to comment.