forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharticlewebview.cc
278 lines (228 loc) · 7.58 KB
/
articlewebview.cc
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
/* This file is (c) 2008-2012 Konstantin Isakov <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "articlewebview.hh"
#ifdef USE_QTWEBKIT
#include "articleinspector.hh"
#include "qt4x5.hh"
#include <QMouseEvent>
#include <QApplication>
#include <QWebFrame>
#ifdef Q_OS_WIN32
#include <qt_windows.h>
#endif
ArticleWebView::ArticleWebView( QWidget *parent ):
QWebView( parent ),
inspector( NULL ),
midButtonPressed( false ),
selectionBySingleClick( false ),
showInspectorDirectly( true )
{
}
ArticleWebView::~ArticleWebView()
{
if ( inspector )
inspector->deleteLater();
}
void ArticleWebView::setUp( Config::Class * cfg )
{
this->cfg = cfg;
}
void ArticleWebView::saveConfigData() const
{
if( inspector )
cfg->inspectorGeometry = inspector->saveGeometry();
}
void ArticleWebView::triggerPageAction( QWebPage::WebAction action, bool checked )
{
if ( action == QWebPage::InspectElement )
{
// Get or create inspector instance for current view.
if ( !inspector )
{
inspector = new ArticleInspector( cfg );
inspector->setPage( page() );
connect( this, SIGNAL( destroyed() ), inspector, SLOT( beforeClosed() ) );
}
if ( showInspectorDirectly )
{
showInspectorDirectly = false;
// Bring up the inspector window and set focus
inspector->show();
inspector->activateWindow();
inspector->raise();
return;
}
}
QWebView::triggerPageAction( action, checked );
}
bool ArticleWebView::event( QEvent * event )
{
switch ( event->type() )
{
case QEvent::MouseButtonRelease:
case QEvent::MouseButtonDblClick:
showInspectorDirectly = true;
break;
case QEvent::ContextMenu:
showInspectorDirectly = false;
break;
default:
break;
}
return QWebView::event( event );
}
void ArticleWebView::mousePressEvent( QMouseEvent * event )
{
if ( event->buttons() & Qt4x5::middleButton() )
midButtonPressed = true;
QWebView::mousePressEvent( event );
if( selectionBySingleClick && ( event->buttons() & Qt::LeftButton ) && !isOnScrollBar( *event ) )
{
findText(""); // clear the selection first, if any
QMouseEvent ev( QEvent::MouseButtonDblClick, event->pos(), Qt::LeftButton, Qt::LeftButton, event->modifiers() );
QApplication::sendEvent( page(), &ev );
}
}
void ArticleWebView::mouseReleaseEvent( QMouseEvent * event )
{
QWebView::mouseReleaseEvent( event );
// QWebPage::linkClicked() signal is emitted during the above call to QWebView::mouseReleaseEvent(),
// so midButtonPressed has been used already and can be unset now.
if( midButtonPressed && !( event->buttons() & Qt4x5::middleButton() ) )
midButtonPressed = false;
}
void ArticleWebView::mouseDoubleClickEvent( QMouseEvent * event )
{
QWebView::mouseDoubleClickEvent( event );
// emit the signal only if we are not double-clicking on scrollbars
if( !isOnScrollBar( *event ) )
emit doubleClicked( event->pos() );
}
// TODO (Qt WebEngine): port if this code is useful in the Qt WebEngine version.
void ArticleWebView::focusInEvent( QFocusEvent * event )
{
QWebView::focusInEvent( event );
switch( event->reason() )
{
case Qt::MouseFocusReason:
case Qt::TabFocusReason:
case Qt::BacktabFocusReason:
page()->mainFrame()->evaluateJavaScript("top.focus();");
break;
default:
break;
}
}
void ArticleWebView::wheelEvent( QWheelEvent *ev )
{
#ifdef Q_OS_WIN32
// TODO (Qt WebEngine): port if this code is useful in the Qt WebEngine version.
// Avoid wrong mouse wheel handling in QWebView
// if system preferences is set to "scroll by page"
if( ev->modifiers() == Qt::NoModifier )
{
unsigned nLines;
SystemParametersInfo( SPI_GETWHEELSCROLLLINES, 0, &nLines, 0 );
if( nLines == WHEEL_PAGESCROLL )
{
QKeyEvent kev( QEvent::KeyPress, ev->delta() > 0 ? Qt::Key_PageUp : Qt::Key_PageDown,
Qt::NoModifier );
QApplication::sendEvent( this, &kev );
ev->accept();
return;
}
}
#endif
if ( ev->modifiers().testFlag( Qt::ControlModifier ) )
{
ev->ignore();
}
else
{
QWebView::wheelEvent( ev );
}
}
bool ArticleWebView::isOnScrollBar( QMouseEvent const & event ) const
{
int const scrollBarWidth = page()->mainFrame()->scrollBarGeometry( Qt::Vertical ).width();
int const scrollBarHeight = page()->mainFrame()->scrollBarGeometry( Qt::Horizontal ).height();
return event.x() >= width() - scrollBarWidth || event.y() >= height() - scrollBarHeight;
}
#else // USE_QTWEBKIT
#include "gddebug.hh"
#include <QChildEvent>
#include <QQuickWidget>
namespace {
QWidget * toChildWidget( QObject * object )
{
// We are interested only in the QQuickWidget child of QWebEngineView.
return qobject_cast< QQuickWidget * >( object );
}
}
void ArticleWebView::setEventFilter( QObject * filterObject )
{
eventFilterObject = filterObject;
}
bool ArticleWebView::isWatched( QObject * object ) const
{
return object->parent() == this && object->isWidgetType() && toChildWidget( object );
}
QCursor ArticleWebView::cursor() const
{
return childWidget ? childWidget->cursor() : QCursor{};
}
void ArticleWebView::setCursor( QCursor const & cursor )
{
if( childWidget )
childWidget->setCursor( cursor );
}
void ArticleWebView::unsetCursor()
{
if( childWidget )
childWidget->unsetCursor();
}
void ArticleWebView::childEvent( QChildEvent * event )
{
auto * const child = event->child();
if( child->isWidgetType() )
{
// Empirical observation: in Qt 5.15 QWebEngineView has a single QQuickWidget child, which is added
// soon after construction and is never removed. Remove the event filter from a removed child
// anyway in case this occurs under some circumstances or in a future Qt version.
switch( event->type() )
{
// Handle ChildPolished event, which occurs soon after ChildAdded event we are really interested in. Cannot handle
// the ChildAdded event directly, because child is not fully constructed and is only a QWidget then. ChildPolished
// event occurs before a lambda invocation queued in ChildAdded event, and thus is preferable to that alternative.
case QEvent::ChildPolished:
{
if( child == childWidget )
return; // repeated polishing => nothing to do
auto * const childAsWidget = toChildWidget( child );
if( !childAsWidget )
return; // this must be a non-quick widget child we are not interested in
// Theoretically eventFilterObject can be installed multiple times on the same quick widget child
// in case of multiple ChildPolished events and a childWidget replacement. This is not a problem,
// because QObject::installEventFilter() removes the event filter if present before installing it
// (guards against a single event filter object installed twice).
child->installEventFilter( eventFilterObject );
if( childWidget )
gdWarning( "A second quick widget child is added to an article web view. Replacing the first one." );
childWidget = childAsWidget;
break;
}
case QEvent::ChildRemoved:
// If child is being destroyed and is no longer a QQuickWidget, removing the event filter is unnecessary,
// because when an object is destroyed, all event filters installed on it are automatically removed.
if( toChildWidget( child ) )
child->removeEventFilter( eventFilterObject );
if( childWidget == child )
childWidget = nullptr;
break;
default:
break;
}
}
return QWebEngineView::childEvent( event );
}
#endif // USE_QTWEBKIT