forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticlewebview.cc
214 lines (183 loc) · 5.54 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
/* 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"
#include <QMouseEvent>
#include <QWebEngineView>
#include <QApplication>
#include <QTimer>
#include <QDialog>
#include <QMainWindow>
#ifdef Q_OS_WIN32
#include <qt_windows.h>
#endif
ArticleWebView::ArticleWebView( QWidget *parent ):
QWebEngineView( parent ),
midButtonPressed( false ),
selectionBySingleClick( false )
{
}
ArticleWebView::~ArticleWebView()
{
}
void ArticleWebView::setUp( Config::Class * cfg )
{
this->cfg = cfg;
setZoomFactor(cfg->preferences.zoomFactor);
}
void ArticleWebView::triggerPageAction( QWebEnginePage::WebAction action, bool checked )
{
QWebEngineView::triggerPageAction( action, checked );
}
QWebEngineView * ArticleWebView::createWindow( QWebEnginePage::WebWindowType type )
{
if(type==QWebEnginePage::WebWindowType::WebDialog)
{
QMainWindow * dlg = new QMainWindow( this );
QWebEngineView * view = new QWebEngineView( dlg );
dlg->setCentralWidget(view);
dlg->resize(400,400);
dlg->show();
return view;
}
return QWebEngineView::createWindow(type);
}
bool ArticleWebView::event( QEvent * event )
{
if( event->type() == QEvent::ChildAdded )
{
QChildEvent * child_ev = static_cast< QChildEvent * >( event );
child_ev->child()->installEventFilter( this );
}
return QWebEngineView::event( event );
}
void ArticleWebView::linkClickedInHtml(QUrl const& ){
//disable single click to simulate dbclick action on the new loaded pages.
singleClickToDbClick=false;
}
bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev) {
if (ev->type() == QEvent::MouseButtonDblClick) {
QMouseEvent *pe = static_cast<QMouseEvent *>(ev);
if (Qt::MouseEventSynthesizedByApplication != pe->source()) {
singleClickToDbClick = false;
dbClicked = true;
}
}
if (ev->type()==QEvent::MouseMove) {
singleClickToDbClick=false;
}
if (ev->type() == QEvent::MouseButtonPress) {
QMouseEvent *pe = static_cast<QMouseEvent *>(ev);
if(pe->button() == Qt::LeftButton)
{
singleClickToDbClick = true;
dbClicked = false;
QTimer::singleShot(QApplication::doubleClickInterval(),this,[=](){
singleClickAction(pe);
});
}
mousePressEvent(pe);
}
if (ev->type() == QEvent::MouseButtonRelease) {
QMouseEvent *pe = static_cast<QMouseEvent *>(ev);
mouseReleaseEvent(pe);
if (dbClicked) {
//emit the signal after button release.emit earlier(in MouseButtonDblClick event) can not get selected text;
doubleClickAction(pe);
}
}
if (ev->type() == QEvent::Wheel) {
QWheelEvent *pe = static_cast<QWheelEvent *>(ev);
wheelEvent(pe);
if ( pe->modifiers().testFlag( Qt::ControlModifier ) )
{
return true;
}
}
if( ev->type() == QEvent::FocusIn )
{
QFocusEvent * pe = static_cast< QFocusEvent * >( ev );
focusInEvent( pe );
return true;
}
return QWebEngineView::eventFilter(obj, ev);
}
void ArticleWebView::mousePressEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::MiddleButton)
midButtonPressed = true;
}
void ArticleWebView::singleClickAction(QMouseEvent *event )
{
if(!singleClickToDbClick)
return;
if (selectionBySingleClick) {
findText(""); // clear the selection first, if any
//send dbl click event twice? send one time seems not work .weird really. need further investigate.
sendCustomMouseEvent( QEvent::MouseButtonDblClick);
sendCustomMouseEvent( QEvent::MouseButtonDblClick);
}
}
void ArticleWebView::sendCustomMouseEvent( QEvent::Type type) {
QPoint pt = mapFromGlobal(QCursor::pos());
QMouseEvent ev(type, pt, pt, QCursor::pos(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier,
Qt::MouseEventSynthesizedByApplication);
auto childrens = this->children();
for (auto child:childrens) {
QApplication::sendEvent(child, &ev);
}
}
void ArticleWebView::mouseReleaseEvent(QMouseEvent *event) {
bool noMidButton = !( event->buttons() & Qt::MiddleButton );
// if ( midButtonPressed & noMidButton )
// midButtonPressed = false;
}
void ArticleWebView::doubleClickAction(QMouseEvent *event) {
if (Qt::MouseEventSynthesizedByApplication != event->source()) {
emit doubleClicked(event->pos());
}
}
void ArticleWebView::focusInEvent( QFocusEvent * event )
{
QWebEngineView::focusInEvent( event );
switch( event->reason() )
{
case Qt::MouseFocusReason:
case Qt::TabFocusReason:
case Qt::BacktabFocusReason:
page()->runJavaScript("top.focus();");
break;
default:
break;
}
}
void ArticleWebView::wheelEvent( QWheelEvent *ev )
{
#ifdef Q_OS_WIN32
// Avoid wrong mouse wheel handling in QWebEngineView
// 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->angleDelta ().y () > 0 ? Qt::Key_PageUp : Qt::Key_PageDown,
Qt::NoModifier );
auto childrens = this->children();
for (auto child : childrens) {
QApplication::sendEvent(child, &kev);
}
ev->accept();
return;
}
}
#endif
if ( ev->modifiers().testFlag( Qt::ControlModifier ) )
{
ev->ignore();
}
else
{
QWebEngineView::wheelEvent( ev );
}
}