-
Notifications
You must be signed in to change notification settings - Fork 28
/
WebViewEdit.h
290 lines (216 loc) · 7.82 KB
/
WebViewEdit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/************************************************************************
**
** Copyright (C) 2019-2023 Kevin B. Hendricks Stratford, Ontario, Canada
**
** This file is part of PageEdit.
**
** PageEdit is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** PageEdit is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with PageEdit. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#ifndef WEBVIEWEDIT_H
#define WEBVIEWEDIT_H
#include <memory>
#include <QEvent>
#include <QContextMenuEvent>
#include <QtWebEngineWidgets>
#include <QWebEngineView>
#include "Utility.h"
#include "ClipEditorModel.h"
#include "WebPageEdit.h"
#include "Viewer.h"
class QSize;
class WebViewEdit : public QWebEngineView, public Viewer
{
Q_OBJECT
public:
/**
* Constructor.
*
* @param parent The object's parent.
*/
WebViewEdit(QWidget *parent = 0);
~WebViewEdit();
QSize sizeHint() const;
void CustomSetDocument(const QString &path, const QString &html);
bool IsLoadingFinished();
bool WasLoadOkay() { return m_LoadOkay; }
void SetZoomFactor(float factor);
void SetCurrentZoomFactor(float factor);
float GetZoomFactor() const;
void Zoom();
void UpdateDisplay();
/**
* Scrolls the editor to the top.
*/
void ScrollToTop();
/**
* Scrolls the editor to the specified fragment when the document is loaded.
*
* @param fragment The fragment ID to scroll to.
* It should have the "#" character as the first character.
*/
void ScrollToFragment(const QString &fragment);
void SelectPreviousChar();
/**
* Workaround for a crappy setFocus implementation in QtWebKit.
*/
void GrabFocus();
void SetDocumentEditable(bool newstate);
// inherited
QList<ElementIndex> GetCaretLocation();
// methods for working with and converting QWebPaths to ElementIndex Lists
QList<ElementIndex> ConvertQWebPathToHierarchy(const QString & webpath) const;
QString ConvertHierarchyToQWebPath(const QList<ElementIndex>& hierarchy);
// inherited
void StoreCaretLocationUpdate(const QList<ElementIndex> &hierarchy);
// inherited
bool ExecuteCaretUpdate();
/**
* Force a caret location update to the specified position.
*/
bool ExecuteCaretUpdate(const QString &caret_location);
QString GetCaretLocationUpdate();
void StoreCurrentCaretLocation();
void FormatBlock(const QString &element_name, bool preserve_attributes);
QString GetCaretElementName();
/**
* From the current cursor position, search for a parent tag element named in the tag list.
* When first if any matching tag found, return the value of the named attribute if exists.
*/
QString GetAncestorTagAttributeValue(const QString &attribute_name, const QStringList &tag_list);
bool InsertTagAttribute(const QString &element_name, const QString &attribute_name, const QString &attribute_value, const QStringList &tag_list, bool ignore_selection = false);
bool SetAncestorTagAttributeValue(const QString &attribute_name, const QString &attribute_value, const QStringList &tag_list);
public slots:
void PasteText(const QString &text);
QString GetHtml() const;
QString GetSelectedText();
void ApplyCaseChangeToSelection(const Utility::Casing &casing);
bool InsertHtml(const QString &html);
bool InsertId(const QString &id);
bool InsertHyperlink(const QString &href);
bool ExecCommand(const QString &command);
bool ExecCommand(const QString &command, const QString ¶meter);
bool QueryCommandState(const QString &command);
QString GetHoverUrl();
bool PasteClipNumber(int clip_number);
bool PasteClipEntries(const QList<ClipEditorModel::clipEntry*> &clips);
bool PasteClipEntry(ClipEditorModel::clipEntry* clip);
signals:
/**
* Emitted whenever the zoom factor changes.
*
* @param new_zoom_factor The new zoom factor of the View.
*/
void ZoomFactorChanged(float new_zoom_factor);
void LinkClicked(const QUrl &url);
void ShowStatusMessageRequest(const QString &message);
void DocumentLoaded();
void GoToPreviewLocationRequest();
protected:
void contextMenuEvent(QContextMenuEvent *event) override;
QString EscapeJSString(const QString &string);
/**
* Evaluates the provided javascript source code
* and returns the result.
*
* @param javascript The JavaScript source code to execute.
* @return The result from the last executed javascript statement.
*/
QVariant EvaluateJavascript(const QString &javascript);
/**
* run a javascript asynchronously with no need for return value
*/
void DoJavascript(const QString &javascript);
protected slots:
void UpdateFinishedState(bool okay);
void LoadingStarted();
void LoadingProgress(int progress);
void LinkHovered(const QString&url);
protected:
private slots:
/**
* Loads the required JavaScript on web page loads.
*/
void WebPageJavascriptOnLoad();
void executeCaretUpdateInternal() {
ExecuteCaretUpdate();
}
private:
/**
* Actually performs the scrolling, will only be invoked after the document has loaded.
*/
void ScrollToFragmentInternal(const QString &fragment);
/**
* Builds the element-selecting JavaScript code, ignoring the text nodes.
* Always just chains children() jQuery calls.
*
* @return The element-selecting JavaScript code.
*/
QString GetElementSelectingJS_NoTextNodes(const QList<ElementIndex> &hierarchy) const;
/**
* Builds the element-selecting JavaScript code, ignoring all the
* text nodes except the last one.
* Chains children() jQuery calls, and then the contents() function
* for the last element (the text node, naturally).
*
* @return The element-selecting JavaScript code.
*/
QString GetElementSelectingJS_WithTextNode(const QList<ElementIndex> &hierarchy) const;
/**
* Connects all the required signals to their respective slots.
*/
void ConnectSignalsToSlots();
///////////////////////////////
// PRIVATE MEMBER VARIABLES
///////////////////////////////
WebPageEdit *m_ViewWebPage;
float m_CurrentZoomFactor;
/**
* The javascript source code of the jQuery library.
*/
const QString c_jQuery;
/**
* The javascript source code of the jQuery
* ScrollTo extension library.
*/
const QString c_jQueryScrollTo;
/**
* The JavaScript source code used
* to get a hierarchy of elements from
* the caret element to the top of the document.
*/
const QString c_GetCaretLocation;
/**
* The JavaScript source code routines used
* to format block elements, ancestor attributes, etc
*/
const QString c_GetBlock;
const QString c_FormatBlock;
const QString c_GetAncestor;
const QString c_GetAncestorAttribute;
const QString c_SetAncestorAttribute;
/**
* Stores the JavaScript source code for the
* caret location update.
*/
QString m_CaretLocationUpdate;
bool m_CustomSetDocumentInProgress;
QString m_pendingScrollToFragment;
bool m_isLoadFinished;
bool m_LoadOkay;
QMenu * m_menu;
QStringList m_dictionaries;
QString m_hoverUrl;
};
#endif // WEBVIEWEDIT