Skip to content

Commit 3f90dbb

Browse files
committedJul 16, 2014
[composer] Fetch html using QgsNetworkAccessManager so that reply can be manipulated prior to rendering. Sponsored by City of Uster, Switzerland.
1 parent 256999d commit 3f90dbb

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed
 

‎src/core/composer/qgscomposerhtml.cpp

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
#include "qgscomposition.h"
1919
#include "qgsaddremovemultiframecommand.h"
2020
#include "qgsnetworkaccessmanager.h"
21+
#include "qgsmessagelog.h"
2122

2223
#include <QCoreApplication>
2324
#include <QPainter>
2425
#include <QWebFrame>
2526
#include <QWebPage>
2627
#include <QImage>
28+
#include <QNetworkReply>
2729

2830
QgsComposerHtml::QgsComposerHtml( QgsComposition* c, bool createUndoCommands ): QgsComposerMultiFrame( c, createUndoCommands ),
2931
mContentMode( QgsComposerHtml::Url ),
@@ -78,26 +80,81 @@ void QgsComposerHtml::setHtml( const QString html )
7880
mHtml = html;
7981
}
8082

83+
QString QgsComposerHtml::fetchHtml( QUrl url )
84+
{
85+
QUrl nextUrlToFetch = url;
86+
QNetworkReply* reply = 0;
87+
88+
//loop until fetched valid html
89+
while ( 1 )
90+
{
91+
//set contents
92+
QNetworkRequest request( nextUrlToFetch );
93+
reply = QgsNetworkAccessManager::instance()->get( request );
94+
connect( reply, SIGNAL( finished() ), this, SLOT( frameLoaded() ) );
95+
//pause until HTML fetch
96+
mLoaded = false;
97+
while ( !mLoaded )
98+
{
99+
qApp->processEvents();
100+
}
101+
102+
if ( reply->error() != QNetworkReply::NoError )
103+
{
104+
QgsMessageLog::logMessage( tr( "HTML fetch %1 failed with error %2" ).arg( reply->url().toString() ).arg( reply->errorString() ) );
105+
reply->deleteLater();
106+
return QString();
107+
}
108+
109+
QVariant redirect = reply->attribute( QNetworkRequest::RedirectionTargetAttribute );
110+
if ( redirect.isNull() )
111+
{
112+
//no error or redirect, got target
113+
break;
114+
}
115+
116+
//redirect, so fetch redirect target
117+
nextUrlToFetch = redirect.toUrl();
118+
reply->deleteLater();
119+
}
120+
121+
QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
122+
if ( !status.isNull() && status.toInt() >= 400 )
123+
{
124+
QgsMessageLog::logMessage( tr( "HTML fetch %1 failed with error %2" ).arg( reply->url().toString() ).arg( status.toString() ) );
125+
reply->deleteLater();
126+
return QString();
127+
}
128+
129+
QByteArray array = reply->readAll();
130+
reply->deleteLater();
131+
return QString( array );
132+
}
133+
81134
void QgsComposerHtml::loadHtml()
82135
{
83136
if ( !mWebPage || ( mContentMode == QgsComposerHtml::Url && mUrl.isEmpty() ) )
84137
{
85138
return;
86139
}
87140

88-
mLoaded = false;
89-
//set contents
141+
QString loadedHtml;
90142
switch ( mContentMode )
91143
{
92144
case QgsComposerHtml::Url:
93-
mWebPage->mainFrame()->load( mUrl );
145+
{
146+
loadedHtml = fetchHtml( mUrl );
94147
break;
148+
}
95149
case QgsComposerHtml::ManualHtml:
96-
mWebPage->mainFrame()->setHtml( mHtml );
150+
loadedHtml = mHtml;
97151
break;
98152
}
99153

100-
//pause until HTML loaded
154+
mLoaded = false;
155+
//set html, using the specified url as base if in Url mode
156+
mWebPage->mainFrame()->setHtml( loadedHtml, mContentMode == QgsComposerHtml::Url ? QUrl( mUrl ) : QUrl() );
157+
101158
while ( !mLoaded )
102159
{
103160
qApp->processEvents();

‎src/core/composer/qgscomposerhtml.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
155155
void loadHtml();
156156

157157
private slots:
158-
void frameLoaded( bool ok );
158+
void frameLoaded( bool ok = true );
159159

160160
private:
161161
ContentMode mContentMode;
@@ -173,6 +173,9 @@ class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
173173

174174
//renders a snapshot of the page to a cached image
175175
void renderCachedImage();
176+
177+
//fetches html content from a url and returns it as a string
178+
QString fetchHtml( QUrl url );
176179
};
177180

178181
#endif // QGSCOMPOSERHTML_H

0 commit comments

Comments
 (0)
Please sign in to comment.