QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgswebview.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 QGSWEBVIEW_H
17 #define QGSWEBVIEW_H
18 
19 
20 #include <QWidget>
21 #include <QPrinter>
22 
23 #ifdef WITH_QTWEBKIT
24 #include <QWebView>
25 #include <QDesktopWidget>
26 
29 class CORE_EXPORT QgsWebView : public QWebView
30 {
31  Q_OBJECT
32 
33  public:
34  explicit QgsWebView( QWidget* parent = nullptr )
35  : QWebView( parent )
36  {
37  QDesktopWidget desktop;
38  // Apply zoom factor for HiDPI screens
39  if ( desktop.physicalDpiX() > 96 )
40  {
41  setZoomFactor( desktop.physicalDpiX() / 96 );
42  }
43  }
44 };
45 #else
46 #include "qgswebpage.h"
47 #include <QTextBrowser>
48 
57 class CORE_EXPORT QgsWebView : public QTextBrowser
58 {
59 
61  Q_OBJECT
62  public:
63  explicit QgsWebView( QWidget *parent = 0 )
64  : QTextBrowser( parent )
65  , mSettings( new QWebSettings() )
66  , mPage( new QWebPage( this ) )
67  {
68  connect( this, SIGNAL( anchorClicked( const QUrl & ) ), this, SIGNAL( linkClicked( const QUrl & ) ) );
69  connect( this, SIGNAL( pageLoadFinished( bool ) ), mPage, SIGNAL( loadFinished( bool ) ) );
70  }
71 
72  ~QgsWebView()
73  {
74  delete mSettings;
75  delete mPage;
76  }
77 
78  void setUrl( const QUrl& url )
79  {
80  setSource( url );
81  }
82 
83  void load( const QUrl& url )
84  {
85  setSource( url );
86  }
87 
88  QWebPage* page() const
89  {
90  return mPage;
91  }
92 
93  QWebSettings* settings() const
94  {
95  return mSettings;
96  }
97 
98  virtual QgsWebView* createWindow( QWebPage::WebWindowType )
99  {
100  return new QgsWebView();
101  }
102 
103  void setContent( const QByteArray& data, const QString& contentType, const QUrl& )
104  {
105  QString text = QString::fromUtf8( data );
106  if ( contentType == "text/html" )
107  setHtml( text );
108  else
109  setPlainText( text );
110 
111  emit pageLoadFinished( true );
112  }
113 
114  void print( QPrinter* )
115  {
116  }
117 
118  signals:
119  void linkClicked( const QUrl &link );
120 
121  void pageLoadFinished( bool ok );
122 
123  private:
124  QWebSettings *mSettings;
125  QWebPage *mPage;
126 
128 };
129 #endif
130 
131 #endif // QGSWEBVIEW_H
int physicalDpiX() const
void setHtml(const QString &text)
QTextBrowser(QWidget *parent)
void print(QPrinter *printer) const
void setPlainText(const QString &text)
QString fromUtf8(const char *str, int size)
The QWebPage class is a collection of stubs to mimic the API of a QWebPage on systems where QtWebkit ...
Definition: qgswebpage.h:99
The QgsWebView class is a collection of stubs to mimic the API of QWebView on systems where the real ...
Definition: qgswebview.h:57
void anchorClicked(const QUrl &link)
virtual void setSource(const QUrl &name)
void setZoomFactor(qreal factor)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
The QWebSettings class is a collection of stubs to mimic the API of a QWebSettings on systems where Q...
Definition: qgswebpage.h:38