pqHelpWindowWebEngine.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
2 // SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
3 // SPDX-License-Identifier: BSD-3-Clause
4 #ifndef pqHelpWindowWebEngine_h
5 #define pqHelpWindowWebEngine_h
6 
14 #include "vtkParaViewDeprecation.h" // for PARAVIEW_DEPRECATED_IN_5_12_0
15 
16 #include <QBuffer>
17 #include <QFileInfo>
18 #include <QNetworkAccessManager>
19 #include <QNetworkProxy>
20 #include <QTimer>
21 #include <QWebEngineHistory>
22 #include <QWebEngineProfile>
23 #include <QWebEngineUrlRequestJob>
24 #include <QWebEngineUrlSchemeHandler>
25 #include <QWebEngineView>
26 
27 namespace
28 {
29 
30 class pqUrlSchemeHandler : public QWebEngineUrlSchemeHandler
31 {
32  typedef QWebEngineUrlSchemeHandler Superclass;
33 
34 public:
35  pqUrlSchemeHandler(QHelpEngineCore* engine)
36  : Engine(engine)
37  {
38  }
39  ~pqUrlSchemeHandler() = default;
40 
41  void requestStarted(QWebEngineUrlRequestJob* request) override
42  {
43  QMap<QString, QString> extension_type_map;
44  extension_type_map["jpg"] = "image/jpeg";
45  extension_type_map["jpeg"] = "image/jpeg";
46  extension_type_map["png"] = "image/png";
47  extension_type_map["gif"] = "image/gif";
48  extension_type_map["tiff"] = "image/tiff";
49  extension_type_map["htm"] = "text/html";
50  extension_type_map["html"] = "text/html";
51  extension_type_map["css"] = "text/css";
52  extension_type_map["xml"] = "text/xml";
53 
54  QUrl url = request->requestUrl();
55  QString extension = QFileInfo(url.path()).suffix().toLower();
56  QString content_type = extension_type_map.value(extension, "text/plain");
57 
58  QByteArray array = this->Engine->fileData(url);
59  QBuffer* buffer = new QBuffer;
60  buffer->setData(array);
61  buffer->open(QIODevice::ReadOnly);
62  connect(buffer, &QIODevice::aboutToClose, buffer, &QObject::deleteLater);
63  request->reply(content_type.toUtf8(), buffer);
64  }
65 
66 private:
67  QHelpEngineCore* Engine;
68 };
69 //----------------------------------------------------------------------------------
73 class pqWebView : public QWebEngineView
74 {
75  typedef QWebEngineView Superclass;
76 
77 public:
78  pqWebView(QWidget* parentObject)
79  : Superclass(parentObject)
80  {
81  }
82  ~pqWebView() = default;
83 
84  static pqWebView* newInstance(QHelpEngine* engine, pqHelpWindow* self)
85  {
86  pqWebView* instance = new pqWebView(self);
87  QWebEngineProfile* profile = QWebEngineProfile::defaultProfile();
88  profile->installUrlSchemeHandler("qthelp", new pqUrlSchemeHandler(engine));
89  self->connect(instance, &pqWebView::loadFinished, self, &pqHelpWindow::updateHistoryButtons);
90  return instance;
91  }
92 
94  "This constructor is deprecated, please use the specialized one instead: pqWebView* "
95  "newInstance(QHelpEngine* engine, pqHelpWindow* self)")
96  static pqWebView* newInstance(QHelpEngine* engine, QWidget* parentObject)
97  {
98  pqWebView* instance = new pqWebView(parentObject);
99  QWebEngineProfile* profile = QWebEngineProfile::defaultProfile();
100  profile->installUrlSchemeHandler("qthelp", new pqUrlSchemeHandler(engine));
101  return instance;
102  }
103 
104  QUrl url() { return this->history()->currentItem().url(); }
105 
106  QUrl goBackward()
107  {
108  this->history()->back();
109  return this->history()->currentItem().url();
110  }
111 
112  QUrl goForward()
113  {
114  this->history()->forward();
115  return this->history()->currentItem().url();
116  }
117 
118  bool canGoBackward() { return this->history()->canGoBack(); }
119 
120  bool canGoForward() { return this->history()->canGoForward(); }
121 
122 private:
123  Q_DISABLE_COPY(pqWebView)
124 };
125 
126 } // end of namespace
127 #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:22
url
connect