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
|
// Copyright (C) 2024 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 "qquickcontextmenu_p.h"
#include <QtCore/qpointer.h>
#include <QtCore/qloggingcategory.h>
#include <QtQml/qqmlcomponent.h>
#include <QtQml/qqmlinfo.h>
#include <QtQuick/qquickwindow.h>
#include <QtQuick/private/qquickitem_p.h>
#include <QtQuickTemplates2/private/qquickdeferredexecute_p_p.h>
#include <QtQuickTemplates2/private/qquickmenu_p.h>
#include <QtQuickTemplates2/private/qquickmenu_p_p.h>
#include <QtQuickTemplates2/private/qquicktextarea_p.h>
#include <QtQuickTemplates2/private/qquicktextfield_p.h>
QT_BEGIN_NAMESPACE
Q_STATIC_LOGGING_CATEGORY(lcContextMenu, "qt.quick.controls.contextmenu")
/*!
\qmltype ContextMenu
\brief The ContextMenu attached type provides a way to open a context menu
in a platform-appropriate manner.
\inqmlmodule QtQuick.Controls
\ingroup qtquickcontrols-menus
\since 6.9
ContextMenu can be attached to any \l {QQuickItem}{item} in order to show a context menu
upon a platform-specific event, such as a right click or the context menu
key.
\snippet qtquickcontrols-contextmenu.qml root
\section1 Sharing context menus
It's possible to share a \l Menu amongst several attached context menu
objects. This allows reusing a single Menu when the items that need
context menus have data in common. For example:
\snippet qtquickcontrols-contextmenu-shared.qml file
\section1 Performance
ContextMenu lazily creates its \c Menu only when it's requested. If it
wasn't for this optimization, the \c Menu would be created when the
containing component is being loaded, which is typically at application
startup.
It is recommended not to give the \c Menu assigned to ContextMenu's \l menu
property an id when it's defined where it's assigned. Doing so prevents
this optimization. For example:
\snippet qtquickcontrols-contextmenu-id.qml root
The example in the \l {Sharing context menus} section works because the
\c Menu is defined separately from its assignment.
\section1 Interaction with other menus
If a \c Menu is opened via e.g. a \l TapHandler or other means, ContextMenu
will not open at the same time. This allows legacy applications that were
written before ContextMenu was introduced to continue working as expected.
*/
/*!
\qmlsignal QtQuick.Controls::ContextMenu::requested(point position)
This signal is emitted when a context menu is requested.
If it was requested by a right mouse button click, \a position gives the
position of the click relative to the parent.
The example below shows how to programmatically open a context menu:
\snippet qtquickcontrols-contextmenu-onrequested.qml buttonAndMenu
If no menu is set, but this signal is connected, the context menu event
will be accepted and will not propagate.
\sa QContextMenuEvent::pos()
*/
class QQuickContextMenuPrivate : public QObjectPrivate
{
public:
Q_DECLARE_PUBLIC(QQuickContextMenu)
static QQuickContextMenuPrivate *get(QQuickContextMenu *attachedObject)
{
return attachedObject->d_func();
}
void cancelMenu();
void executeMenu(bool complete = false);
bool isRequestedSignalConnected();
QQuickDeferredPointer<QQuickMenu> menu;
bool complete = false;
};
static const QString menuPropertyName = QStringLiteral("menu");
void QQuickContextMenuPrivate::cancelMenu()
{
Q_Q(QQuickContextMenu);
quickCancelDeferred(q, menuPropertyName);
}
void QQuickContextMenuPrivate::executeMenu(bool complete)
{
Q_Q(QQuickContextMenu);
if (menu.wasExecuted())
return;
QQmlEngine *engine = nullptr;
auto *parentItem = qobject_cast<QQuickItem *>(q->parent());
if (parentItem) {
engine = qmlEngine(parentItem);
// In most cases, the above line will work, but if it doesn't,
// it could be because we're attached to the contentItem of the window.
// In that case, we'll be created before the contentItem has a context
// and hence an engine. However, the window will have one, so use that.
if (!engine)
engine = qmlEngine(parentItem->window());
}
if (!menu || complete)
quickBeginAttachedDeferred(q, menuPropertyName, menu, engine);
if (complete)
quickCompleteAttachedDeferred(q, menuPropertyName, menu, engine);
}
bool QQuickContextMenuPrivate::isRequestedSignalConnected()
{
Q_Q(QQuickContextMenu);
IS_SIGNAL_CONNECTED(q, QQuickContextMenu, requested, (QPointF));
}
QQuickContextMenu::QQuickContextMenu(QObject *parent)
: QObject(*(new QQuickContextMenuPrivate), parent)
{
Q_ASSERT(parent);
if (parent->isQuickItemType()) {
auto *itemPriv = QQuickItemPrivate::get(static_cast<QQuickItem *>(parent));
Q_ASSERT(itemPriv);
if (QObject *oldMenu = itemPriv->setContextMenu(this))
qCWarning(lcContextMenu) << this << "replaced" << oldMenu << "on" << parent;
} else {
qmlWarning(parent) << "ContextMenu must be attached to an Item";
}
}
QQuickContextMenu *QQuickContextMenu::qmlAttachedProperties(QObject *object)
{
return new QQuickContextMenu(object);
}
/*!
\qmlproperty Menu QtQuick.Controls::ContextMenu::menu
This property holds the context menu that will be opened. It can be set to
any \l Menu object.
\note The \l Menu assigned to this property cannot be given an id. See
\l {Sharing context menus} for more information.
*/
QQuickMenu *QQuickContextMenu::menu() const
{
auto *d = const_cast<QQuickContextMenuPrivate *>(d_func());
if (!d->menu) {
qCDebug(lcContextMenu) << "creating menu via deferred execution"
<< "- is component complete:" << d->complete;
d->executeMenu(d->complete);
}
return d->menu;
}
void QQuickContextMenu::setMenu(QQuickMenu *menu)
{
Q_D(QQuickContextMenu);
if (!parent()->isQuickItemType())
return;
if (menu == d->menu)
return;
if (!d->menu.isExecuting())
d->cancelMenu();
d->menu = menu;
if (!d->menu.isExecuting())
emit menuChanged();
}
void QQuickContextMenu::classBegin()
{
}
void QQuickContextMenu::componentComplete()
{
Q_D(QQuickContextMenu);
d->complete = true;
}
bool QQuickContextMenu::event(QEvent *event)
{
Q_D(QQuickContextMenu);
switch (event->type()) {
case QEvent::ContextMenu: {
qCDebug(lcContextMenu) << this << "handling" << event << "on behalf of" << parent();
auto *attacheeItem = qobject_cast<QQuickItem *>(parent());
Q_ASSERT(attacheeItem);
const auto *contextMenuEvent = static_cast<QContextMenuEvent *>(event);
const QPoint posRelativeToParent(attacheeItem->mapFromScene(contextMenuEvent->pos()).toPoint());
const bool isRequestedSignalConnected = d->isRequestedSignalConnected();
if (isRequestedSignalConnected)
Q_EMIT requested(posRelativeToParent);
auto *menu = this->menu();
if (!menu) {
if (isRequestedSignalConnected) {
qCDebug(lcContextMenu) << this << "no menu instance but accepting event anyway"
<< "since requested signal has connections";
event->accept();
return true;
}
// No menu set and requested isn't connected; let the event propagate
// onwards and do nothing.
return QObject::event(event);
}
// On platforms like iOS, we want to use the native edit menu.
// TODO: test this on Android and desktop platforms (with popupType: Popup.Native)
if (menu && QQuickMenuPrivate::get(menu)->resolvedPopupType() == QQuickPopup::Native
&& (qobject_cast<const QQuickTextField *>(attacheeItem)
|| qobject_cast<const QQuickTextArea *>(attacheeItem))) {
QQuickMenuPrivate::get(menu)->makeEditMenu();
}
menu->setParentItem(attacheeItem);
qCDebug(lcContextMenu) << this << "showing" << menu << "at" << posRelativeToParent;
menu->popup(posRelativeToParent);
event->accept();
return true;
}
default:
break;
}
return QObject::event(event);
}
QT_END_NAMESPACE
#include "moc_qquickcontextmenu_p.cpp"
|