42
42
#include " qgswebframe.h"
43
43
#include " qgsstringutils.h"
44
44
#include " qgstreewidgetitem.h"
45
+ #include " qgsfiledownloader.h"
45
46
46
47
#include < QCloseEvent>
47
48
#include < QLabel>
59
60
#include < QMessageBox>
60
61
#include < QComboBox>
61
62
#include < QTextDocument>
63
+ #include < QNetworkRequest>
64
+ #include < QNetworkReply>
65
+ #include < QFileDialog>
66
+ #include < QFileInfo>
67
+ #include < QRegExp>
62
68
63
69
// graph
64
70
#include < qwt_plot.h>
@@ -72,13 +78,59 @@ QgsIdentifyResultsWebView::QgsIdentifyResultsWebView( QWidget *parent ) : QgsWeb
72
78
setSizePolicy ( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
73
79
page ()->setNetworkAccessManager ( QgsNetworkAccessManager::instance () );
74
80
// page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
81
+ page ()->setForwardUnsupportedContent ( true );
75
82
page ()->setLinkDelegationPolicy ( QWebPage::DontDelegateLinks );
76
83
settings ()->setAttribute ( QWebSettings::LocalContentCanAccessRemoteUrls, true );
77
84
settings ()->setAttribute ( QWebSettings::JavascriptCanOpenWindows, true );
78
85
settings ()->setAttribute ( QWebSettings::PluginsEnabled, true );
79
86
#ifdef QGISDEBUG
80
87
settings ()->setAttribute ( QWebSettings::DeveloperExtrasEnabled, true );
81
88
#endif
89
+ connect ( page (), SIGNAL ( downloadRequested ( QNetworkRequest ) ), this , SLOT ( downloadRequested ( QNetworkRequest ) ) );
90
+ connect ( page (), SIGNAL ( unsupportedContent ( QNetworkReply* ) ), this , SLOT ( unsupportedContent ( QNetworkReply* ) ) );
91
+ }
92
+
93
+
94
+ void QgsIdentifyResultsWebView::downloadRequested ( const QNetworkRequest &request )
95
+ {
96
+ handleDownload ( request.url () );
97
+ }
98
+
99
+ void QgsIdentifyResultsWebView::unsupportedContent ( QNetworkReply * reply )
100
+ {
101
+ handleDownload ( reply->url () );
102
+ }
103
+
104
+ void QgsIdentifyResultsWebView::handleDownload ( QUrl url )
105
+ {
106
+ if ( ! url.isValid () )
107
+ {
108
+ QMessageBox::warning ( this , tr ( " Invalid URL" ), tr ( " The download URL is not valid: %1" ).arg ( url.toString ( ) ) );
109
+ }
110
+ else
111
+ {
112
+ const QString DOWNLOADER_LAST_DIR_KEY ( " Qgis/fileDownloaderLastDir" );
113
+ QSettings settings;
114
+ // Try to get some information from the URL
115
+ QFileInfo info ( url.toString ( ) );
116
+ QString savePath = settings.value ( DOWNLOADER_LAST_DIR_KEY ).toString ( );
117
+ QString fileName = info.fileName ().replace ( QRegExp ( " [^A-z0-9\\ -_\\ .]" ), " _" );
118
+ if ( ! savePath.isEmpty () && ! fileName.isEmpty ( ) )
119
+ {
120
+ savePath = QDir::cleanPath ( savePath + QDir::separator () + fileName );
121
+ }
122
+ QString targetFile = QFileDialog::getSaveFileName ( this ,
123
+ tr ( " Save as" ),
124
+ savePath,
125
+ info.suffix ( ).isEmpty () ? QString ( ) : " *." + info.suffix ( )
126
+ );
127
+ if ( ! targetFile.isEmpty () )
128
+ {
129
+ settings.setValue ( DOWNLOADER_LAST_DIR_KEY, QFileInfo ( targetFile ).dir ().absolutePath ( ) );
130
+ // Start the download
131
+ new QgsFileDownloader ( url, targetFile );
132
+ }
133
+ }
82
134
}
83
135
84
136
void QgsIdentifyResultsWebView::print ()
0 commit comments