1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Labs Platform module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qwidgetplatformmenu_p.h"
38#include "qwidgetplatformmenuitem_p.h"
39
40#include <QtGui/qwindow.h>
41#include <QtWidgets/qmenu.h>
42#include <QtWidgets/qaction.h>
43
44QT_BEGIN_NAMESPACE
45
46QWidgetPlatformMenu::QWidgetPlatformMenu(QObject *parent)
47 : m_menu(new QMenu)
48{
49 setParent(parent);
50
51 connect(sender: m_menu.data(), signal: &QMenu::aboutToShow, receiver: this, slot: &QPlatformMenu::aboutToShow);
52 connect(sender: m_menu.data(), signal: &QMenu::aboutToHide, receiver: this, slot: &QPlatformMenu::aboutToHide);
53}
54
55QWidgetPlatformMenu::~QWidgetPlatformMenu()
56{
57}
58
59QMenu *QWidgetPlatformMenu::menu() const
60{
61 return m_menu.data();
62}
63
64void QWidgetPlatformMenu::insertMenuItem(QPlatformMenuItem *item, QPlatformMenuItem *before)
65{
66 QWidgetPlatformMenuItem *widgetItem = qobject_cast<QWidgetPlatformMenuItem *>(object: item);
67 if (!widgetItem)
68 return;
69
70 QWidgetPlatformMenuItem *widgetBefore = qobject_cast<QWidgetPlatformMenuItem *>(object: before);
71 m_menu->insertAction(before: widgetBefore ? widgetBefore->action() : nullptr, action: widgetItem->action());
72 int index = m_items.indexOf(t: widgetBefore);
73 if (index < 0)
74 index = m_items.count();
75 m_items.insert(i: index, t: widgetItem);
76}
77
78void QWidgetPlatformMenu::removeMenuItem(QPlatformMenuItem *item)
79{
80 QWidgetPlatformMenuItem *widgetItem = qobject_cast<QWidgetPlatformMenuItem *>(object: item);
81 if (!widgetItem)
82 return;
83
84 m_items.removeOne(t: widgetItem);
85 m_menu->removeAction(action: widgetItem->action());
86}
87
88void QWidgetPlatformMenu::syncMenuItem(QPlatformMenuItem *item)
89{
90 Q_UNUSED(item);
91}
92
93void QWidgetPlatformMenu::syncSeparatorsCollapsible(bool enable)
94{
95 m_menu->setSeparatorsCollapsible(enable);
96}
97
98void QWidgetPlatformMenu::setText(const QString &text)
99{
100 m_menu->setTitle(text);
101}
102
103void QWidgetPlatformMenu::setIcon(const QIcon &icon)
104{
105 m_menu->setIcon(icon);
106}
107
108void QWidgetPlatformMenu::setEnabled(bool enabled)
109{
110 m_menu->menuAction()->setEnabled(enabled);
111}
112
113bool QWidgetPlatformMenu::isEnabled() const
114{
115 return m_menu->menuAction()->isEnabled();
116}
117
118void QWidgetPlatformMenu::setVisible(bool visible)
119{
120 m_menu->menuAction()->setVisible(visible);
121}
122
123void QWidgetPlatformMenu::setMinimumWidth(int width)
124{
125 if (width > 0)
126 m_menu->setMinimumWidth(width);
127}
128
129void QWidgetPlatformMenu::setFont(const QFont &font)
130{
131 m_menu->setFont(font);
132}
133
134void QWidgetPlatformMenu::setMenuType(MenuType type)
135{
136 Q_UNUSED(type);
137}
138
139void QWidgetPlatformMenu::showPopup(const QWindow *window, const QRect &targetRect, const QPlatformMenuItem *item)
140{
141 m_menu->createWinId();
142 QWindow *handle = m_menu->windowHandle();
143 Q_ASSERT(handle);
144 handle->setTransientParent(const_cast<QWindow *>(window));
145
146 QPoint targetPos = targetRect.bottomLeft();
147 if (window)
148 targetPos = window->mapToGlobal(pos: targetPos);
149
150 const QWidgetPlatformMenuItem *widgetItem = qobject_cast<const QWidgetPlatformMenuItem *>(object: item);
151 m_menu->popup(pos: targetPos, at: widgetItem ? widgetItem->action() : nullptr);
152}
153
154void QWidgetPlatformMenu::dismiss()
155{
156 m_menu->close();
157}
158
159QPlatformMenuItem *QWidgetPlatformMenu::menuItemAt(int position) const
160{
161 return m_items.value(i: position);
162}
163
164QPlatformMenuItem *QWidgetPlatformMenu::menuItemForTag(quintptr tag) const
165{
166 for (QWidgetPlatformMenuItem *item : m_items) {
167 if (item->tag() == tag)
168 return item;
169 }
170 return nullptr;
171}
172
173QPlatformMenuItem *QWidgetPlatformMenu::createMenuItem() const
174{
175 return new QWidgetPlatformMenuItem;
176}
177
178QPlatformMenu *QWidgetPlatformMenu::createSubMenu() const
179{
180 return new QWidgetPlatformMenu;
181}
182
183QT_END_NAMESPACE
184

source code of qtquickcontrols2/src/imports/platform/widgets/qwidgetplatformmenu.cpp