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 | |
44 | QT_BEGIN_NAMESPACE |
45 | |
46 | 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 | |
55 | QWidgetPlatformMenu::() |
56 | { |
57 | } |
58 | |
59 | QMenu *QWidgetPlatformMenu::() const |
60 | { |
61 | return m_menu.data(); |
62 | } |
63 | |
64 | void QWidgetPlatformMenu::(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 | |
78 | void QWidgetPlatformMenu::(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 | |
88 | void QWidgetPlatformMenu::(QPlatformMenuItem *item) |
89 | { |
90 | Q_UNUSED(item); |
91 | } |
92 | |
93 | void QWidgetPlatformMenu::(bool enable) |
94 | { |
95 | m_menu->setSeparatorsCollapsible(enable); |
96 | } |
97 | |
98 | void QWidgetPlatformMenu::(const QString &text) |
99 | { |
100 | m_menu->setTitle(text); |
101 | } |
102 | |
103 | void QWidgetPlatformMenu::(const QIcon &icon) |
104 | { |
105 | m_menu->setIcon(icon); |
106 | } |
107 | |
108 | void QWidgetPlatformMenu::(bool enabled) |
109 | { |
110 | m_menu->menuAction()->setEnabled(enabled); |
111 | } |
112 | |
113 | bool QWidgetPlatformMenu::() const |
114 | { |
115 | return m_menu->menuAction()->isEnabled(); |
116 | } |
117 | |
118 | void QWidgetPlatformMenu::(bool visible) |
119 | { |
120 | m_menu->menuAction()->setVisible(visible); |
121 | } |
122 | |
123 | void QWidgetPlatformMenu::(int width) |
124 | { |
125 | if (width > 0) |
126 | m_menu->setMinimumWidth(width); |
127 | } |
128 | |
129 | void QWidgetPlatformMenu::(const QFont &font) |
130 | { |
131 | m_menu->setFont(font); |
132 | } |
133 | |
134 | void QWidgetPlatformMenu::(MenuType type) |
135 | { |
136 | Q_UNUSED(type); |
137 | } |
138 | |
139 | void QWidgetPlatformMenu::(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 | |
154 | void QWidgetPlatformMenu::() |
155 | { |
156 | m_menu->close(); |
157 | } |
158 | |
159 | QPlatformMenuItem *QWidgetPlatformMenu::(int position) const |
160 | { |
161 | return m_items.value(i: position); |
162 | } |
163 | |
164 | QPlatformMenuItem *QWidgetPlatformMenu::(quintptr tag) const |
165 | { |
166 | for (QWidgetPlatformMenuItem *item : m_items) { |
167 | if (item->tag() == tag) |
168 | return item; |
169 | } |
170 | return nullptr; |
171 | } |
172 | |
173 | QPlatformMenuItem *QWidgetPlatformMenu::() const |
174 | { |
175 | return new QWidgetPlatformMenuItem; |
176 | } |
177 | |
178 | QPlatformMenu *QWidgetPlatformMenu::() const |
179 | { |
180 | return new QWidgetPlatformMenu; |
181 | } |
182 | |
183 | QT_END_NAMESPACE |
184 | |