pqHelpWindowWebEngine.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ParaView
4  Module: pqHelpWindowWebEngine.h
5 
6  Copyright (c) 2005,2006 Sandia Corporation, Kitware Inc.
7  All rights reserved.
8 
9  ParaView is a free software; you can redistribute it and/or modify it
10  under the terms of the ParaView license version 1.2.
11 
12  See License_v1.2.txt for the full ParaView license.
13  A copy of this license can be obtained by contacting
14  Kitware Inc.
15  28 Corporate Drive
16  Clifton Park, NY 12065
17  USA
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 ========================================================================*/
32 #ifndef pqHelpWindowWebEngine_h
33 #define pqHelpWindowWebEngine_h
34 
42 #include "vtkParaViewDeprecation.h"
43 
44 #include <QBuffer>
45 #include <QFileInfo>
46 #include <QNetworkAccessManager>
47 #include <QNetworkProxy>
48 #include <QTimer>
49 #include <QWebEngineHistory>
50 #include <QWebEngineProfile>
51 #include <QWebEngineUrlRequestJob>
52 #include <QWebEngineUrlSchemeHandler>
53 #include <QWebEngineView>
54 
55 namespace
56 {
57 
58 class pqUrlSchemeHandler : public QWebEngineUrlSchemeHandler
59 {
60  typedef QWebEngineUrlSchemeHandler Superclass;
61 
62 public:
63  pqUrlSchemeHandler(QHelpEngineCore* engine)
64  : Engine(engine)
65  {
66  }
67  ~pqUrlSchemeHandler() = default;
68 
69  void requestStarted(QWebEngineUrlRequestJob* request) override
70  {
71  QMap<QString, QString> extension_type_map;
72  extension_type_map["jpg"] = "image/jpeg";
73  extension_type_map["jpeg"] = "image/jpeg";
74  extension_type_map["png"] = "image/png";
75  extension_type_map["gif"] = "image/gif";
76  extension_type_map["tiff"] = "image/tiff";
77  extension_type_map["htm"] = "text/html";
78  extension_type_map["html"] = "text/html";
79  extension_type_map["css"] = "text/css";
80  extension_type_map["xml"] = "text/xml";
81 
82  QUrl url = request->requestUrl();
83  QString extension = QFileInfo(url.path()).suffix().toLower();
84  QString content_type = extension_type_map.value(extension, "text/plain");
85 
86  QByteArray array = this->Engine->fileData(url);
87  QBuffer* buffer = new QBuffer;
88  buffer->setData(array);
89  buffer->open(QIODevice::ReadOnly);
90  connect(buffer, &QIODevice::aboutToClose, buffer, &QObject::deleteLater);
91  request->reply(content_type.toUtf8(), buffer);
92  }
93 
94 private:
95  QHelpEngineCore* Engine;
96 };
97 //----------------------------------------------------------------------------------
101 class pqWebView : public QWebEngineView
102 {
103  typedef QWebEngineView Superclass;
104 
105 public:
106  pqWebView(QWidget* parentObject)
107  : Superclass(parentObject)
108  {
109  }
110  ~pqWebView() = default;
111 
112  static pqWebView* newInstance(QHelpEngine* engine, pqHelpWindow* self)
113  {
114  pqWebView* instance = new pqWebView(self);
115  QWebEngineProfile* profile = QWebEngineProfile::defaultProfile();
116  profile->installUrlSchemeHandler("qthelp", new pqUrlSchemeHandler(engine));
117  self->connect(instance, &pqWebView::loadFinished, self, &pqHelpWindow::updateHistoryButtons);
118  return instance;
119  }
120 
122  "This constructor is deprecated, please use the specialized one instead: pqWebView* "
123  "newInstance(QHelpEngine* engine, pqHelpWindow* self)");
124  static pqWebView* newInstance(QHelpEngine* engine, QWidget* parentObject)
125  {
126  pqWebView* instance = new pqWebView(parentObject);
127  QWebEngineProfile* profile = QWebEngineProfile::defaultProfile();
128  profile->installUrlSchemeHandler("qthelp", new pqUrlSchemeHandler(engine));
129  return instance;
130  }
131 
132  QUrl url() { return this->history()->currentItem().url(); }
133 
134  QUrl goBackward()
135  {
136  this->history()->back();
137  return this->history()->currentItem().url();
138  }
139 
140  QUrl goForward()
141  {
142  this->history()->forward();
143  return this->history()->currentItem().url();
144  }
145 
146  bool canGoBackward() { return this->history()->canGoBack(); }
147 
148  bool canGoForward() { return this->history()->canGoForward(); }
149 
150 private:
151  Q_DISABLE_COPY(pqWebView)
152 };
153 
154 } // end of namespace
155 #endif
profile
virtual void updateHistoryButtons()
Update the state of buttons used to navigate through history.
#define PARAVIEW_DEPRECATED_IN_5_12_0(reason)
pqHelpWindow provides a assistant-like window for showing help provided by a QHelpEngine.
Definition: pqHelpWindow.h:50
url
connect