1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Controls module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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 https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QQUICKMENU_P_H
41#define QQUICKMENU_P_H
42
43#include "qquickmenuitem_p.h"
44
45#include <QtCore/qglobal.h>
46#include <QtCore/qvariant.h>
47#include <QtQml/qqml.h>
48#include <QtQml/qqmllist.h>
49#include <QtGui/QFont>
50
51QT_BEGIN_NAMESPACE
52
53class QPlatformMenu;
54class QQuickMenuPopupWindow1;
55class QQuickMenuItemContainer1;
56class QQuickWindow;
57class QQuickMenuBar1;
58
59typedef QQmlListProperty<QObject> QQuickMenuItems;
60
61class QQuickMenu1 : public QQuickMenuText1
62{
63 Q_OBJECT
64 Q_PROPERTY(QString title READ text WRITE setText NOTIFY titleChanged)
65 Q_PROPERTY(QQmlListProperty<QObject> items READ menuItems NOTIFY itemsChanged)
66 Q_CLASSINFO("DefaultProperty", "items")
67
68 Q_PROPERTY(int __selectedIndex READ selectedIndex WRITE setSelectedIndex NOTIFY __selectedIndexChanged)
69 Q_PROPERTY(bool __popupVisible READ popupVisible NOTIFY popupVisibleChanged)
70 Q_PROPERTY(QQuickItem *__contentItem READ menuContentItem WRITE setMenuContentItem NOTIFY menuContentItemChanged)
71 Q_PROPERTY(int __minimumWidth READ minimumWidth WRITE setMinimumWidth NOTIFY minimumWidthChanged)
72 Q_PROPERTY(QFont __font READ font WRITE setFont)
73 Q_PROPERTY(qreal __xOffset READ xOffset WRITE setXOffset)
74 Q_PROPERTY(qreal __yOffset READ yOffset WRITE setYOffset)
75 Q_PROPERTY(QQuickAction1 *__action READ action CONSTANT)
76 Q_PROPERTY(QRect __popupGeometry READ popupGeometry NOTIFY __popupGeometryChanged)
77 Q_PROPERTY(bool __isProxy READ isProxy WRITE setProxy NOTIFY __proxyChanged)
78 Q_ENUMS(MenuType)
79
80public:
81 // MenuType must stay in sync with QPlatformMenu::MenuType
82 enum MenuType { DefaultMenu = 0, EditMenu };
83
84 Q_INVOKABLE void popup();
85 Q_INVOKABLE QQuickMenuItem1 *addItem(const QString &);
86 Q_INVOKABLE QQuickMenuItem1 *insertItem(int, const QString &);
87 Q_INVOKABLE void addSeparator();
88 Q_INVOKABLE void insertSeparator(int);
89
90 Q_INVOKABLE void insertItem(int, QQuickMenuBase1 *);
91 Q_INVOKABLE void removeItem(QQuickMenuBase1 *);
92 Q_INVOKABLE void clear();
93
94 Q_INVOKABLE void __popup(const QRectF &targetRect, int atItemIndex = -1, MenuType menuType = DefaultMenu);
95
96public Q_SLOTS:
97 void __dismissMenu();
98
99 void __closeAndDestroy();
100 void __dismissAndDestroy();
101
102Q_SIGNALS:
103 void itemsChanged();
104 void titleChanged();
105
106 void __selectedIndexChanged();
107 void aboutToShow();
108 void aboutToHide();
109 void popupVisibleChanged();
110 void __menuPopupDestroyed();
111 void __popupGeometryChanged();
112 void menuContentItemChanged();
113 void minimumWidthChanged();
114 void __proxyChanged();
115
116public:
117 QQuickMenu1(QObject *parent = 0);
118 virtual ~QQuickMenu1();
119
120 void setVisible(bool) override;
121 void setEnabled(bool) override;
122
123 int selectedIndex() const { return m_selectedIndex; }
124 void setSelectedIndex(int index);
125
126 QQuickMenuItems menuItems();
127 QQuickMenuBase1 *menuItemAtIndex(int index) const;
128 bool contains(QQuickMenuBase1 *);
129 int indexOfMenuItem(QQuickMenuBase1 *) const;
130
131 QPlatformMenu *platformMenu() const { return m_platformMenu; }
132
133 int minimumWidth() const { return m_minimumWidth; }
134 void setMinimumWidth(int w);
135
136 void setFont(const QFont &font);
137 QFont font() const { return m_font; }
138
139 qreal xOffset() const { return m_xOffset; }
140 void setXOffset(qreal);
141 qreal yOffset() const { return m_yOffset; }
142 void setYOffset(qreal);
143
144 QQuickItem *menuContentItem() const { return m_menuContentItem; }
145 bool popupVisible() const { return m_popupVisible; }
146
147 bool isNative() override { return m_platformMenu != 0; }
148
149 QRect popupGeometry() const;
150
151 bool isProxy() const { return m_proxy; }
152 void setProxy(bool proxy) { if (m_proxy != proxy) { m_proxy = proxy; emit __proxyChanged(); } }
153
154 void prepareItemTrigger(QQuickMenuItem1 *);
155 void concludeItemTrigger(QQuickMenuItem1 *);
156 void destroyMenuPopup();
157 void destroyAllMenuPopups();
158
159 QQuickMenuBar1 *menuBar();
160
161protected Q_SLOTS:
162 void updateSelectedIndex();
163
164 void setMenuContentItem(QQuickItem *);
165 void setPopupVisible(bool);
166 void hideMenu();
167 void clearPopupWindow();
168
169 void updateText() override;
170 void windowVisibleChanged(bool);
171 void platformMenuWindowVisibleChanged(bool);
172
173private:
174 QQuickWindow *findParentWindow();
175 void syncParentMenuBar();
176 QQuickMenuPopupWindow1 *topMenuPopup() const;
177
178 int itemIndexForListIndex(int listIndex) const;
179 void itemIndexToListIndex(int, int *, int *) const;
180
181 struct MenuItemIterator
182 {
183 MenuItemIterator(): index(-1), containerIndex(-1) {}
184 int index, containerIndex;
185 };
186
187 QQuickMenuBase1 *nextMenuItem(MenuItemIterator *) const;
188
189 static void append_menuItems(QQuickMenuItems *list, QObject *o);
190 static int count_menuItems(QQuickMenuItems *list);
191 static QObject *at_menuItems(QQuickMenuItems *list, int index);
192 static void clear_menuItems(QQuickMenuItems *list);
193
194 void unparentItem(QQuickMenuBase1 *menuItem);
195 void setupMenuItem(QQuickMenuBase1 *item, int platformIndex = -1);
196
197 QPlatformMenu *m_platformMenu;
198 QList<QQuickMenuBase1 *> m_menuItems;
199 QHash<QObject *, QQuickMenuItemContainer1 *> m_containers;
200 int m_itemsCount;
201 int m_selectedIndex;
202 int m_highlightedIndex;
203 QQuickWindow *m_parentWindow;
204 int m_minimumWidth;
205 QQuickMenuPopupWindow1 *m_popupWindow;
206 QQuickItem * m_menuContentItem;
207 bool m_popupVisible;
208 int m_containersCount;
209 qreal m_xOffset;
210 qreal m_yOffset;
211 QFont m_font;
212 int m_triggerCount;
213 bool m_proxy;
214 QMetaObject::Connection m_windowConnection;
215};
216
217QT_END_NAMESPACE
218
219QML_DECLARE_TYPE(QQuickMenu1)
220
221#endif // QQUICKMENU_P_H
222

source code of qtquickcontrols/src/controls/qquickmenu_p.h