Skip to content

Commit

Permalink
New class QgsWebPage
Browse files Browse the repository at this point in the history
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 8126d46
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/core/CMakeLists.txt
Expand Up @@ -484,6 +484,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsvectorlayer.h
qgsvectorlayerjoinbuffer.h
qgsvisibilitypresetcollection.h
qgswebpage.h
qgswebview.h

auth/qgsauthmanager.h
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/core/composer/qgscomposerhtml.cpp
Expand Up @@ -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 );

Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposerhtml.h
Expand Up @@ -20,7 +20,7 @@
#include "qgsfeature.h"
#include <QUrl>

class QWebPage;
class QgsWebPage;
class QImage;
class QgsVectorLayer;
class QgsNetworkContentFetcher;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -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/
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposerlabel.h
Expand Up @@ -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.
Expand Down Expand Up @@ -216,7 +216,7 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
QMap<QString, QVariant> mSubstitutions;
QgsDistanceArea* mDistanceArea;

QWebPage* mWebPage;
QgsWebPage* mWebPage;
};

#endif
55 changes: 55 additions & 0 deletions src/core/qgswebpage.h
Expand Up @@ -16,6 +16,7 @@
#ifndef QGSWEBPAGE_H
#define QGSWEBPAGE_H

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

#ifdef WITH_QTWEBKIT
Expand Down Expand Up @@ -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.