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
|
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "touchselectionmenuwidget_p.h"
#include "qwebengineview.h"
#include "touch_selection_menu_controller.h"
#include <QBoxLayout>
#include <QEvent>
namespace QtWebEngineWidgetUI {
namespace {
// The Widgets module is built with rtti in developer-build while Core is not.
// The MOC will throw "undefined reference to typeinfo..." errors if we connect slots
// from QtWebEngineCore via function pointers, because it expects rtti on them.
// To workaround this we use lambdas.
void connectButton(TouchSelectionMenuWidget::TouchButton *button, std::function<void()> callback)
{
QObject::connect(button, &QPushButton::clicked, std::move(callback));
}
} // namespace
TouchSelectionMenuWidget::TouchButton::TouchButton(QString name, QWidget *parent)
: QPushButton(name, parent)
{
setAttribute(Qt::WA_AcceptTouchEvents, true);
}
TouchSelectionMenuWidget::TouchButton::~TouchButton()
{
}
bool TouchSelectionMenuWidget::TouchButton::event(QEvent *ev)
{
switch (ev->type()) {
case QEvent::TouchBegin:
ev->accept();
return true;
case QEvent::TouchEnd:
Q_EMIT clicked();
ev->accept();
return true;
default:
break;
}
return QPushButton::event(ev);
}
TouchSelectionMenuWidget::TouchSelectionMenuWidget(
QWebEngineView *view, QtWebEngineCore::TouchSelectionMenuController *controller)
: QWidget(view,
Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint
| Qt::WindowDoesNotAcceptFocus)
{
setAttribute(Qt::WA_AcceptTouchEvents, true);
setAttribute(Qt::WA_TransparentForMouseEvents, true);
setAttribute(Qt::WA_DeleteOnClose, true);
bool cutEnabled =
controller->isCommandEnabled(QtWebEngineCore::TouchSelectionMenuController::Cut);
bool copyEnabled =
controller->isCommandEnabled(QtWebEngineCore::TouchSelectionMenuController::Copy);
bool pasteEnabled =
controller->isCommandEnabled(QtWebEngineCore::TouchSelectionMenuController::Paste);
QSizePolicy policy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QHBoxLayout *layout = new QHBoxLayout();
if (cutEnabled) {
TouchButton *button = new TouchButton(tr("Cut"), this);
button->setSizePolicy(policy);
layout->addWidget(button);
connectButton(button, [controller]() { controller->cut(); });
}
if (copyEnabled) {
TouchButton *button = new TouchButton(tr("Copy"), this);
button->setSizePolicy(policy);
layout->addWidget(button);
connectButton(button, [controller]() { controller->copy(); });
}
if (pasteEnabled) {
TouchButton *button = new TouchButton(tr("Paste"), this);
button->setSizePolicy(policy);
layout->addWidget(button);
connectButton(button, [controller]() { controller->paste(); });
}
TouchButton *button = new TouchButton(tr("..."), this);
button->setSizePolicy(policy);
layout->addWidget(button);
connectButton(button, [controller]() { controller->runContextMenu(); });
layout->setSpacing(2);
layout->setSizeConstraint(QLayout::SetMinimumSize);
layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout);
nativeParentWidget()->installEventFilter(this);
}
TouchSelectionMenuWidget::~TouchSelectionMenuWidget()
{
}
bool TouchSelectionMenuWidget::eventFilter(QObject *obj, QEvent *ev)
{
// Close the menu if the window is moved
if (ev->type() == QEvent::Move)
close();
return QWidget::eventFilter(obj, ev);
}
} // QtWebEngineWidgetUI
|