Skip to content
Permalink
Browse files

New class QgsWebPage

QgsWebPage is a QWebPage subclass which automatically handles
redirecting JavaScript console output to the QGIS message log.

This makes it easier to debug JS errors and scripts used inside
composer HTML and label items.

Sponsored by North Road
  • Loading branch information
nyalldawson committed May 9, 2016
1 parent cda387c commit 8126d460db71f0efc424937bdf06f131cd73cbf0
@@ -484,6 +484,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsvectorlayer.h
qgsvectorlayerjoinbuffer.h
qgsvisibilitypresetcollection.h
qgswebpage.h
qgswebview.h

auth/qgsauthmanager.h
@@ -547,7 +548,6 @@ SET(QGIS_CORE_MOC_HDRS

IF (NOT WITH_QTWEBKIT)
SET(QGIS_CORE_MOC_HDRS ${QGIS_CORE_MOC_HDRS}
qgswebpage.h
qgswebframe.h
)
ENDIF(NOT WITH_QTWEBKIT)
@@ -51,7 +51,8 @@ QgsComposerHtml::QgsComposerHtml( QgsComposition* c, bool createUndoCommands )
{
mDistanceArea = new QgsDistanceArea();
mHtmlUnitsToMM = htmlUnitsToMM();
mWebPage = new QWebPage();
mWebPage = new QgsWebPage();
mWebPage->setIdentifier( tr( "Composer HTML item" ) );
mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );

@@ -20,7 +20,7 @@
#include "qgsfeature.h"
#include <QUrl>

class QWebPage;
class QgsWebPage;
class QImage;
class QgsVectorLayer;
class QgsNetworkContentFetcher;
@@ -228,7 +228,7 @@ class CORE_EXPORT QgsComposerHtml: public QgsComposerMultiFrame
private:
ContentMode mContentMode;
QUrl mUrl;
QWebPage* mWebPage;
QgsWebPage* mWebPage;
QString mHtml;
QString mFetchedHtml;
QString mLastFetchedUrl;
@@ -80,7 +80,8 @@ QgsComposerLabel::QgsComposerLabel( QgsComposition *composition )
connect( &mComposition->atlasComposition(), SIGNAL( featureChanged( QgsFeature* ) ), this, SLOT( refreshExpressionContext() ) );
}

mWebPage = new QWebPage( this );
mWebPage = new QgsWebPage( this );
mWebPage->setIdentifier( tr( "Composer label item" ) );
mWebPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() );

//This makes the background transparent. Found on http://blog.qt.digia.com/blog/2009/06/30/transparent-qwebview-or-qwebpage/
@@ -23,7 +23,7 @@
class QgsVectorLayer;
class QgsFeature;
class QgsDistanceArea;
class QWebPage;
class QgsWebPage;

/** \ingroup MapComposer
* A label that can be placed onto a map composition.
@@ -216,7 +216,7 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
QMap<QString, QVariant> mSubstitutions;
QgsDistanceArea* mDistanceArea;

QWebPage* mWebPage;
QgsWebPage* mWebPage;
};

#endif
@@ -16,6 +16,7 @@
#ifndef QGSWEBPAGE_H
#define QGSWEBPAGE_H

#include "qgsmessagelog.h"
#include <QObject>

#ifdef WITH_QTWEBKIT
@@ -178,11 +179,65 @@ class CORE_EXPORT QWebPage : public QObject

public slots:

protected:

virtual void javaScriptConsoleMessage( const QString , int, const QString& ) {};

private:
QWebSettings* mSettings;
QWebFrame* mFrame;
/// @endcond
};
#endif

/** \ingroup core
* \class QgsWebPage
* \brief QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log.
* \note Added in version 2.16
* \note Not available in Python bindings
*/
class CORE_EXPORT QgsWebPage : public QWebPage
{
Q_OBJECT

public:

/** Constructor for QgsWebPage.
* @param parent parent object
*/
explicit QgsWebPage( QObject* parent = 0 )
: QWebPage( parent )
{}

/** Sets an identifier for the QgsWebPage. The page's identifier is included in messages written to the
* log, and should be set to a user-friendly string so that users can identify which QgsWebPage has
* logged the message.
* @param identifier identifier string
* @see identifier()
*/
void setIdentifier( const QString& identifier ) { mIdentifier = identifier; }

/** Returns the QgsWebPage's identifier. The page's identifier is included in messages written to the
* log so that users can identify which QgsWebPage has logged the message.
* @see setIdentifier()
*/
QString identifier() const { return mIdentifier; }

protected:

virtual void javaScriptConsoleMessage( const QString& message, int lineNumber, const QString& ) override
{
if ( mIdentifier.isEmpty() )
QgsMessageLog::logMessage( tr( "Line %1: %2" ).arg( lineNumber ).arg( message ), tr( "Javascript" ) );
else
QgsMessageLog::logMessage( tr( "%1 (line %2): %3" ).arg( mIdentifier ).arg( lineNumber ).arg( message ), tr( "Javascript" ) );
}

private:

QString mIdentifier;

};

#endif // QGSWEBPAGE_H

0 comments on commit 8126d46

Please sign in to comment.
You can’t perform that action at this time.