1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qwidgetplatformmenu_p.h"
5#include "qwidgetplatformmenuitem_p.h"
6
7#include <QtGui/qaction.h>
8#include <QtGui/qwindow.h>
9#include <QtGui/private/qhighdpiscaling_p.h>
10#include <QtWidgets/qmenu.h>
11
12QT_BEGIN_NAMESPACE
13
14QWidgetPlatformMenu::QWidgetPlatformMenu(QObject *parent)
15 : m_menu(new QMenu)
16{
17 setParent(parent);
18
19 connect(sender: m_menu.data(), signal: &QMenu::aboutToShow, context: this, slot: &QPlatformMenu::aboutToShow);
20 connect(sender: m_menu.data(), signal: &QMenu::aboutToHide, context: this, slot: &QPlatformMenu::aboutToHide);
21}
22
23QWidgetPlatformMenu::~QWidgetPlatformMenu()
24{
25}
26
27QMenu *QWidgetPlatformMenu::menu() const
28{
29 return m_menu.data();
30}
31
32void QWidgetPlatformMenu::insertMenuItem(QPlatformMenuItem *item, QPlatformMenuItem *before)
33{
34 QWidgetPlatformMenuItem *widgetItem = qobject_cast<QWidgetPlatformMenuItem *>(object: item);
35 if (!widgetItem)
36 return;
37
38 QWidgetPlatformMenuItem *widgetBefore = qobject_cast<QWidgetPlatformMenuItem *>(object: before);
39 m_menu->insertAction(before: widgetBefore ? widgetBefore->action() : nullptr, action: widgetItem->action());
40 int index = m_items.indexOf(t: widgetBefore);
41 if (index < 0)
42 index = m_items.size();
43 m_items.insert(i: index, t: widgetItem);
44}
45
46void QWidgetPlatformMenu::removeMenuItem(QPlatformMenuItem *item)
47{
48 QWidgetPlatformMenuItem *widgetItem = qobject_cast<QWidgetPlatformMenuItem *>(object: item);
49 if (!widgetItem)
50 return;
51
52 m_items.removeOne(t: widgetItem);
53 m_menu->removeAction(action: widgetItem->action());
54}
55
56void QWidgetPlatformMenu::syncMenuItem(QPlatformMenuItem *item)
57{
58 Q_UNUSED(item);
59}
60
61void QWidgetPlatformMenu::syncSeparatorsCollapsible(bool enable)
62{
63 m_menu->setSeparatorsCollapsible(enable);
64}
65
66void QWidgetPlatformMenu::setText(const QString &text)
67{
68 m_menu->setTitle(text);
69}
70
71void QWidgetPlatformMenu::setIcon(const QIcon &icon)
72{
73 m_menu->setIcon(icon);
74}
75
76void QWidgetPlatformMenu::setEnabled(bool enabled)
77{
78 m_menu->menuAction()->setEnabled(enabled);
79}
80
81bool QWidgetPlatformMenu::isEnabled() const
82{
83 return m_menu->menuAction()->isEnabled();
84}
85
86void QWidgetPlatformMenu::setVisible(bool visible)
87{
88 m_menu->menuAction()->setVisible(visible);
89}
90
91void QWidgetPlatformMenu::setMinimumWidth(int width)
92{
93 if (width > 0)
94 m_menu->setMinimumWidth(width);
95}
96
97void QWidgetPlatformMenu::setFont(const QFont &font)
98{
99 m_menu->setFont(font);
100}
101
102void QWidgetPlatformMenu::setMenuType(MenuType type)
103{
104 Q_UNUSED(type);
105}
106
107void QWidgetPlatformMenu::showPopup(const QWindow *window, const QRect &targetRect, const QPlatformMenuItem *item)
108{
109 m_menu->createWinId();
110 QWindow *handle = m_menu->windowHandle();
111 Q_ASSERT(handle);
112 handle->setTransientParent(const_cast<QWindow *>(window));
113
114 QPoint targetPos = targetRect.bottomLeft();
115 if (window)
116 targetPos = window->mapToGlobal(pos: QHighDpi::fromNativeLocalPosition(value: targetPos, context: window));
117
118 const QWidgetPlatformMenuItem *widgetItem = qobject_cast<const QWidgetPlatformMenuItem *>(object: item);
119 m_menu->popup(pos: targetPos, at: widgetItem ? widgetItem->action() : nullptr);
120}
121
122void QWidgetPlatformMenu::dismiss()
123{
124 m_menu->close();
125}
126
127QPlatformMenuItem *QWidgetPlatformMenu::menuItemAt(int position) const
128{
129 return m_items.value(i: position);
130}
131
132QPlatformMenuItem *QWidgetPlatformMenu::menuItemForTag(quintptr tag) const
133{
134 for (QWidgetPlatformMenuItem *item : m_items) {
135 if (item->tag() == tag)
136 return item;
137 }
138 return nullptr;
139}
140
141QPlatformMenuItem *QWidgetPlatformMenu::createMenuItem() const
142{
143 return new QWidgetPlatformMenuItem;
144}
145
146QPlatformMenu *QWidgetPlatformMenu::createSubMenu() const
147{
148 return new QWidgetPlatformMenu;
149}
150
151QT_END_NAMESPACE
152
153#include "moc_qwidgetplatformmenu_p.cpp"
154

source code of qtdeclarative/src/labs/platform/widgets/qwidgetplatformmenu.cpp