-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
File downloader for identify dialog hyperlinks
fixes #14703 Include C++ and Python tests
- Loading branch information
Showing
11 changed files
with
815 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/*************************************************************************** | ||
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. | ||
*/ | ||
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(); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
#include "qgswebview.h" | ||
#include "qgswebframe.h" | ||
#include "qgsstringutils.h" | ||
#include "qgsfiledownloader.h" | ||
|
||
#include <QCloseEvent> | ||
#include <QLabel> | ||
|
@@ -55,6 +56,11 @@ | |
#include <QMessageBox> | ||
#include <QComboBox> | ||
#include <QTextDocument> | ||
#include <QNetworkRequest> | ||
#include <QNetworkReply> | ||
#include <QFileDialog> | ||
#include <QFileInfo> | ||
#include <QRegExp> | ||
|
||
//graph | ||
#include <qwt_plot.h> | ||
|
@@ -68,13 +74,63 @@ QgsIdentifyResultsWebView::QgsIdentifyResultsWebView( QWidget *parent ) : QgsWeb | |
setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum ); | ||
page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() ); | ||
// page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks ); | ||
page()->setForwardUnsupportedContent( true ); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
elpaso
Author
Contributor
|
||
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 ) | ||
{ | ||
qDebug() << "Download Requested: " << request.url(); | ||
handleDownload( request.url() ); | ||
} | ||
|
||
void QgsIdentifyResultsWebView::unsupportedContent( QNetworkReply * reply ) | ||
{ | ||
qDebug() << "Unsupported Content: " << reply->url(); | ||
handleDownload( reply->url() ); | ||
} | ||
|
||
void QgsIdentifyResultsWebView::handleDownload( QUrl url ) | ||
{ | ||
qDebug() << "Downloading: " << 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 | ||
qDebug() << "Start the download: " << url; | ||
new QgsFileDownloader( url, targetFile ); | ||
} | ||
} | ||
} | ||
|
||
void QgsIdentifyResultsWebView::print() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
While building, I get this error:
error: no member named 'setForwardUnsupportedContent' in 'QWebPage' page()->setForwardUnsupportedContent( true );