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 <QBuffer>
15 #include <QFileInfo>
16 #include <QNetworkAccessManager>
17 #include <QNetworkProxy>
18 #include <QTimer>
19 #include <QWebEngineHistory>
20 #include <QWebEngineProfile>
21 #include <QWebEngineUrlRequestJob>
22 #include <QWebEngineUrlSchemeHandler>
23 #include <QWebEngineView>
24 
25 namespace
26 {
27 
28 class pqUrlSchemeHandler : public QWebEngineUrlSchemeHandler
29 {
30  typedef QWebEngineUrlSchemeHandler Superclass;
31 
32 public:
33  pqUrlSchemeHandler(QHelpEngineCore* engine)
34  : Engine(engine)
35  {
36  }
37  ~pqUrlSchemeHandler() = default;
38 
39  void requestStarted(QWebEngineUrlRequestJob* request) override
40  {
41  QMap<QString, QString> extension_type_map;
42  extension_type_map["jpg"] = "image/jpeg";
43  extension_type_map["jpeg"] = "image/jpeg";
44  extension_type_map["png"] = "image/png";
45  extension_type_map["gif"] = "image/gif";
46  extension_type_map["tiff"] = "image/tiff";
47  extension_type_map["htm"] = "text/html";
48  extension_type_map["html"] = "text/html";
49  extension_type_map["css"] = "text/css";
50  extension_type_map["xml"] = "text/xml";
51 
52  QUrl url = request->requestUrl();
53  QString extension = QFileInfo(url.path()).suffix().toLower();
54  QString content_type = extension_type_map.value(extension, "text/plain");
55 
56  QByteArray array = this->Engine->fileData(url);
57  QBuffer* buffer = new QBuffer;
58  buffer->setData(array);
59  buffer->open(QIODevice::ReadOnly);
60  connect(buffer, &QIODevice::aboutToClose, buffer, &QObject::deleteLater);
61  request->reply(content_type.toUtf8(), buffer);
62  }
63 
64 private:
65  QHelpEngineCore* Engine;
66 };
67 //----------------------------------------------------------------------------------
71 class pqWebView : public QWebEngineView
72 {
73  typedef QWebEngineView Superclass;
74 
75 public:
76  pqWebView(QWidget* parentObject)
77  : Superclass(parentObject)
78  {
79  }
80  ~pqWebView() = default;
81 
82  static pqWebView* newInstance(QHelpEngine* engine, pqHelpWindow* self)
83  {
84  pqWebView* instance = new pqWebView(self);
85  QWebEngineProfile* profile = QWebEngineProfile::defaultProfile();
86  profile->installUrlSchemeHandler("qthelp", new pqUrlSchemeHandler(engine));
87  self->connect(instance, &pqWebView::loadFinished, self, &pqHelpWindow::updateHistoryButtons);
88  return instance;
89  }
90 
91  QUrl url() { return this->history()->currentItem().url(); }
92 
93  QUrl goBackward()
94  {
95  this->history()->back();
96  return this->history()->currentItem().url();
97  }
98 
99  QUrl goForward()
100  {
101  this->history()->forward();
102  return this->history()->currentItem().url();
103  }
104 
105  bool canGoBackward() { return this->history()->canGoBack(); }
106 
107  bool canGoForward() { return this->history()->canGoForward(); }
108 
109 private:
110  Q_DISABLE_COPY(pqWebView)
111 };
112 
113 } // end of namespace
114 #endif
profile
virtual void updateHistoryButtons()
Update the state of buttons used to navigate through history.
pqHelpWindow provides a assistant-like window for showing help provided by a QHelpEngine.
Definition: pqHelpWindow.h:20
url
connect