QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgswebpage.h
Go to the documentation of this file.
1 /***************************************************************************
2 
3  ----------------------------------------------------
4  date : 19.5.2015
5  copyright : (C) 2015 by Matthias Kuhn
6  email : matthias (at) opengis.ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSWEBPAGE_H
17 #define QGSWEBPAGE_H
18 
19 #define SIP_NO_FILE
20 
21 #include "qgis_core.h"
22 #include "qgsmessagelog.h"
23 #include <QObject>
24 
25 #ifdef WITH_QTWEBKIT
26 #include <QWebPage>
27 #else
28 
29 #include "qgswebframe.h"
30 
31 #include <QMenu>
32 #include <QNetworkAccessManager>
33 #include <QPalette>
34 #include <QTextBrowser>
35 
36 
42 class CORE_EXPORT QWebSettings : public QObject
43 {
45  Q_OBJECT
46 
47  public:
48 
49  enum WebAttribute
50  {
51  AutoLoadImages,
52  JavascriptEnabled,
53  JavaEnabled,
54  PluginsEnabled,
55  PrivateBrowsingEnabled,
56  JavascriptCanOpenWindows,
57  JavascriptCanAccessClipboard,
58  DeveloperExtrasEnabled,
59  LinksIncludedInFocusChain,
60  ZoomTextOnly,
61  PrintElementBackgrounds,
62  OfflineStorageDatabaseEnabled,
63  OfflineWebApplicationCacheEnabled,
64  LocalStorageEnabled,
65  LocalContentCanAccessRemoteUrls,
66  DnsPrefetchEnabled,
67  XSSAuditingEnabled,
68  AcceleratedCompositingEnabled,
69  SpatialNavigationEnabled,
70  LocalContentCanAccessFileUrls,
71  TiledBackingStoreEnabled,
72  FrameFlatteningEnabled,
73  SiteSpecificQuirksEnabled,
74  JavascriptCanCloseWindows,
75  WebGLEnabled,
76  CSSRegionsEnabled,
77  HyperlinkAuditingEnabled,
78  CSSGridLayoutEnabled,
79  ScrollAnimatorEnabled,
80  CaretBrowsingEnabled,
81  NotificationsEnabled
82  };
83  explicit QWebSettings( QObject *parent = nullptr )
84  : QObject( parent )
85  {
86  }
87 
88  void setUserStyleSheetUrl( const QUrl & )
89  {
90  }
91 
92  void setAttribute( WebAttribute, bool )
93  {
94  }
96 };
97 
103 class CORE_EXPORT QWebPage : public QObject
104 {
106  Q_OBJECT
107 
108  public:
109 
110  enum LinkDelegationPolicy
111  {
112  DontDelegateLinks,
113  DelegateExternalLinks,
114  DelegateAllLinks
115  };
116 
117  enum WebWindowType
118  {
119  WebBrowserWindow,
120  WebModalDialog
121  };
122 
123  explicit QWebPage( QObject *parent = nullptr )
124  : QObject( parent )
125  , mSettings( new QWebSettings() )
126  , mFrame( new QWebFrame() )
127  {
128  connect( mFrame, &QWebFrame::loadFinished, this, &QWebPage::loadFinished );
129  }
130 
131  ~QWebPage()
132  {
133  delete mFrame;
134  delete mSettings;
135  }
136 
137  QPalette palette() const
138  {
139  return QPalette();
140  }
141 
142  void setPalette( const QPalette &palette )
143  {
144  Q_UNUSED( palette )
145  }
146 
147  void setViewportSize( const QSize &size ) const
148  {
149  Q_UNUSED( size )
150  }
151 
152  void setLinkDelegationPolicy( LinkDelegationPolicy linkDelegationPolicy )
153  {
154  if ( !parent() )
155  return;
156 
157  QTextBrowser *tb = qobject_cast<QTextBrowser *>( parent() );
158  if ( !tb )
159  return;
160 
161  tb->setOpenExternalLinks( linkDelegationPolicy != DontDelegateLinks );
162  }
163 
164  void setNetworkAccessManager( QNetworkAccessManager *networkAccessManager )
165  {
166  Q_UNUSED( networkAccessManager )
167  }
168 
169  QWebFrame *mainFrame() const
170  {
171  return mFrame;
172  }
173 
174  QWebSettings *settings() const
175  {
176  return mSettings;
177  }
178 
179  QSize viewportSize() const
180  {
181  return QSize();
182  }
183 
184  QMenu *createStandardContextMenu()
185  {
186  return new QMenu();
187  }
188 
189  signals:
190 
191  void loadFinished( bool ok );
192 
193  void downloadRequested( const QNetworkRequest &request );
194 
195  void unsupportedContent( QNetworkReply *reply );
196 
197  public slots:
198 
199  protected:
200 
201  virtual void javaScriptConsoleMessage( const QString &, int, const QString & ) {}
202 
203  private:
204  QWebSettings *mSettings = nullptr;
205  QWebFrame *mFrame = nullptr;
207 };
208 #endif
209 
217 class CORE_EXPORT QgsWebPage : public QWebPage
218 {
219  Q_OBJECT
220 
221  public:
222 
227  explicit QgsWebPage( QObject *parent = nullptr )
228  : QWebPage( parent )
229  {}
230 
238  void setIdentifier( const QString &identifier ) { mIdentifier = identifier; }
239 
245  QString identifier() const { return mIdentifier; }
246 
247  protected:
248 
249  void javaScriptConsoleMessage( const QString &message, int lineNumber, const QString & ) override
250  {
251  if ( mIdentifier.isEmpty() )
252  QgsMessageLog::logMessage( tr( "Line %1: %2" ).arg( lineNumber ).arg( message ), tr( "JavaScript" ) );
253  else
254  QgsMessageLog::logMessage( tr( "%1 (line %2): %3" ).arg( mIdentifier ).arg( lineNumber ).arg( message ), tr( "JavaScript" ) );
255  }
256 
257  private:
258 
259  QString mIdentifier;
260 
261 };
262 
263 #endif // QGSWEBPAGE_H
264 
The QWebFrame class is a collection of stubs to mimic the API of a QWebFrame on systems where QtWebki...
Definition: qgswebframe.h:38
The QWebPage class is a collection of stubs to mimic the API of a QWebPage on systems where QtWebkit ...
Definition: qgswebpage.h:104
The QWebSettings class is a collection of stubs to mimic the API of a QWebSettings on systems where Q...
Definition: qgswebpage.h:43
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log.
Definition: qgswebpage.h:218
QgsWebPage(QObject *parent=nullptr)
Constructor for QgsWebPage.
Definition: qgswebpage.h:227
void javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &) override
Definition: qgswebpage.h:249
void setIdentifier(const QString &identifier)
Sets an identifier for the QgsWebPage.
Definition: qgswebpage.h:238
QString identifier() const
Returns the QgsWebPage's identifier.
Definition: qgswebpage.h:245