Debian Codegraph @

src/ApvlvWebViewWidget.h

raw
1 /*
2 * This file is part of the apvlv package
3 *
4 * Copyright (C) 2008 Alf.
5 *
6 * Contact: Alf <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2.0 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23 /* @CPPFILE ApvlvWebViewWidget.h
24 *
25 * Author: Alf <[email protected]>
26 */
27
28 #ifndef _APVLV_WEBVIEW_WIDGET_H_
29 #define _APVLV_WEBVIEW_WIDGET_H_
30
31 #include <memory>
32 #include <string>
33
34 #include <QBuffer>
35 #include <QByteArray>
36 #include <QMenu>
37 #include <QWebEngineFindTextResult>
38 #include <QWebEngineProfile>
39 #include <QWebEngineUrlRequestJob>
40 #include <QWebEngineUrlSchemeHandler>
41 #include <QWebEngineView>
42
43 #include "ApvlvFileWidget.h"
44
45 namespace apvlv
46 {
47 class File;
48 class ApvlvSchemeHandler : public QWebEngineUrlSchemeHandler
49 {
50 Q_OBJECT
51 public:
52 void
53 setFile (File *file)
54 {
55 mFile = file;
56 }
57 File *
58 file () const
59 {
60 return mFile;
61 }
62 void requestStarted (QWebEngineUrlRequestJob *job) override;
63
64 private:
65 File *mFile;
66 QByteArray mArray;
67 QBuffer mBuffer;
68
69 signals:
70 void webpageUpdated (const std::string &key);
71 };
72
73 class WebView : public QWebEngineView
74 {
75 Q_OBJECT
76 public:
77 WebView ();
78 void
79 setFile (File *file)
80 {
81 mSchemeHandler.setFile (file);
82 }
83
84 protected:
85 void contextMenuEvent (QContextMenuEvent *event) override;
86
87 private:
88 QWebEngineProfile mProfile;
89 std::unique_ptr<QWebEnginePage> mPage;
90 ApvlvSchemeHandler mSchemeHandler;
91 QMenu mMenu;
92
93 bool isScrolledToTop ();
94 bool isScrolledToBottom ();
95
96 [[nodiscard]] std::pair<int, int> getSelectionPosition () const;
97 void underLinePosition (int begin, int end);
98
99 QAction mCopyAction;
100 QAction mUnderlineAction;
101 QAction mCommentAction;
102
103 friend class WebViewWidget;
104
105 private slots:
106 void copy () const;
107 void underline ();
108 void comment ();
109 };
110
111 class WebViewWidget : public FileWidget
112 {
113 Q_OBJECT
114 public:
115 WebViewWidget ();
116
117 [[nodiscard]] QWidget *
118 widget () override
119 {
120 return &mWebView;
121 }
122
123 void
124 setFile (File *file) override
125 {
126 mFile = file;
127 mWebView.setFile (mFile);
128 }
129
130 void showPage (int pn, double s) override;
131 void showPage (int pn, const std::string &anchor) override;
132
133 void scroll (int times, int w, int h) override;
134 void scrollTo (double x, double y) override;
135
136 void scrollUp (int times) override;
137 void scrollDown (int times) override;
138 void scrollLeft (int times) override;
139 void scrollRight (int times) override;
140
141 void setSearchStr (const std::string &str) override;
142 void setSearchSelect (int select) override;
143
144 void setZoomrate (double zm) override;
145
146 void
147 setInternalScroll (bool internal)
148 {
149 mIsInternalScroll = internal;
150 }
151 bool
152 internalScroll ()
153 {
154 return mIsInternalScroll;
155 }
156
157 private:
158 WebView mWebView{};
159 bool mIsInternalScroll{ false };
160 bool mIsScrollUp{ false };
161 QWebEngineFindTextResult mSearchResult;
162
163 bool loadJavaScriptFromDir (const std::string &dir);
164
165 void setComment ();
166
167 signals:
168 void webpageUpdated (const std::string &msg);
169
170 private slots:
171 void
172 webviewUpdate (const std::string &msg)
173 {
174 emit webpageUpdated (msg);
175 };
176
177 void webviewLoadFinished (bool suc);
178 void webviewFindTextFinished (const QWebEngineFindTextResult &result);
179 };
180
181 }
182
183 #endif
184
185 /* Local Variables: */
186 /* mode: c++ */
187 /* End: */