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 QtWidgets 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 QMENU_H
41#define QMENU_H
42
43#include <QtWidgets/qtwidgetsglobal.h>
44#include <QtWidgets/qwidget.h>
45#include <QtCore/qstring.h>
46#include <QtGui/qicon.h>
47#include <QtWidgets/qaction.h>
48
49#if defined(Q_OS_MACOS) || defined(Q_CLANG_QDOC)
50Q_FORWARD_DECLARE_OBJC_CLASS(NSMenu);
51#endif
52
53QT_REQUIRE_CONFIG(menu);
54
55QT_BEGIN_NAMESPACE
56
57class QMenuPrivate;
58class QStyleOptionMenuItem;
59class QPlatformMenu;
60
61class Q_WIDGETS_EXPORT QMenu : public QWidget
62{
63private:
64 Q_OBJECT
65 Q_DECLARE_PRIVATE(QMenu)
66
67 Q_PROPERTY(bool tearOffEnabled READ isTearOffEnabled WRITE setTearOffEnabled)
68 Q_PROPERTY(QString title READ title WRITE setTitle)
69 Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
70 Q_PROPERTY(bool separatorsCollapsible READ separatorsCollapsible WRITE setSeparatorsCollapsible)
71 Q_PROPERTY(bool toolTipsVisible READ toolTipsVisible WRITE setToolTipsVisible)
72
73public:
74 explicit QMenu(QWidget *parent = nullptr);
75 explicit QMenu(const QString &title, QWidget *parent = nullptr);
76 ~QMenu();
77
78 using QWidget::addAction;
79 QAction *addAction(const QString &text);
80 QAction *addAction(const QIcon &icon, const QString &text);
81 QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
82 QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
83
84#ifdef Q_CLANG_QDOC
85 template<typename PointerToMemberFunction>
86 QAction *addAction(const QString &text, const QObject *receiver, PointerToMemberFunction method, const QKeySequence &shortcut = 0);
87 template<typename Functor>
88 QAction *addAction(const QString &text, Functor functor, const QKeySequence &shortcut = 0);
89 template<typename Functor>
90 QAction *addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0);
91 template<typename PointerToMemberFunction>
92 QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, PointerToMemberFunction method, const QKeySequence &shortcut = 0);
93 template<typename Functor>
94 QAction *addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut = 0);
95 template<typename Functor>
96 QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0);
97#else
98 // addAction(QString): Connect to a QObject slot / functor or function pointer (with context)
99 template<class Obj, typename Func1>
100 inline typename std::enable_if<!std::is_same<const char*, Func1>::value
101 && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
102 addAction(const QString &text, const Obj *object, Func1 slot, const QKeySequence &shortcut = 0)
103 {
104 QAction *result = addAction(text);
105#ifdef QT_NO_SHORTCUT
106 Q_UNUSED(shortcut)
107#else
108 result->setShortcut(shortcut);
109#endif
110 connect(result, &QAction::triggered, object, std::move(slot));
111 return result;
112 }
113 // addAction(QString): Connect to a functor or function pointer (without context)
114 template <typename Func1>
115 inline QAction *addAction(const QString &text, Func1 slot, const QKeySequence &shortcut = 0)
116 {
117 QAction *result = addAction(text);
118#ifdef QT_NO_SHORTCUT
119 Q_UNUSED(shortcut)
120#else
121 result->setShortcut(shortcut);
122#endif
123 connect(result, &QAction::triggered, std::move(slot));
124 return result;
125 }
126 // addAction(QIcon, QString): Connect to a QObject slot / functor or function pointer (with context)
127 template<class Obj, typename Func1>
128 inline typename std::enable_if<!std::is_same<const char*, Func1>::value
129 && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
130 addAction(const QIcon &actionIcon, const QString &text, const Obj *object, Func1 slot, const QKeySequence &shortcut = 0)
131 {
132 QAction *result = addAction(actionIcon, text);
133#ifdef QT_NO_SHORTCUT
134 Q_UNUSED(shortcut)
135#else
136 result->setShortcut(shortcut);
137#endif
138 connect(result, &QAction::triggered, object, std::move(slot));
139 return result;
140 }
141 // addAction(QIcon, QString): Connect to a functor or function pointer (without context)
142 template <typename Func1>
143 inline QAction *addAction(const QIcon &actionIcon, const QString &text, Func1 slot, const QKeySequence &shortcut = 0)
144 {
145 QAction *result = addAction(actionIcon, text);
146#ifdef QT_NO_SHORTCUT
147 Q_UNUSED(shortcut)
148#else
149 result->setShortcut(shortcut);
150#endif
151 connect(result, &QAction::triggered, std::move(slot));
152 return result;
153 }
154#endif // !Q_CLANG_QDOC
155
156 QAction *addMenu(QMenu *menu);
157 QMenu *addMenu(const QString &title);
158 QMenu *addMenu(const QIcon &icon, const QString &title);
159
160 QAction *addSeparator();
161
162 QAction *addSection(const QString &text);
163 QAction *addSection(const QIcon &icon, const QString &text);
164
165 QAction *insertMenu(QAction *before, QMenu *menu);
166 QAction *insertSeparator(QAction *before);
167 QAction *insertSection(QAction *before, const QString &text);
168 QAction *insertSection(QAction *before, const QIcon &icon, const QString &text);
169
170 bool isEmpty() const;
171 void clear();
172
173 void setTearOffEnabled(bool);
174 bool isTearOffEnabled() const;
175
176 bool isTearOffMenuVisible() const;
177 void showTearOffMenu();
178 void showTearOffMenu(const QPoint &pos);
179 void hideTearOffMenu();
180
181 void setDefaultAction(QAction *);
182 QAction *defaultAction() const;
183
184 void setActiveAction(QAction *act);
185 QAction *activeAction() const;
186
187 void popup(const QPoint &pos, QAction *at = nullptr);
188 QAction *exec();
189 QAction *exec(const QPoint &pos, QAction *at = nullptr);
190
191#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
192 static QAction *exec(const QList<QAction *> &actions, const QPoint &pos, QAction *at = nullptr, QWidget *parent = nullptr);
193#else
194 static QAction *exec(QList<QAction*> actions, const QPoint &pos, QAction *at = nullptr, QWidget *parent = nullptr);
195#endif
196
197 QSize sizeHint() const override;
198
199 QRect actionGeometry(QAction *) const;
200 QAction *actionAt(const QPoint &) const;
201
202 QAction *menuAction() const;
203
204 QString title() const;
205 void setTitle(const QString &title);
206
207 QIcon icon() const;
208 void setIcon(const QIcon &icon);
209
210 void setNoReplayFor(QWidget *widget);
211 QPlatformMenu *platformMenu();
212 void setPlatformMenu(QPlatformMenu *platformMenu);
213
214#if defined(Q_OS_MACOS) || defined(Q_CLANG_QDOC)
215 NSMenu* toNSMenu();
216 void setAsDockMenu();
217#endif
218
219 bool separatorsCollapsible() const;
220 void setSeparatorsCollapsible(bool collapse);
221
222 bool toolTipsVisible() const;
223 void setToolTipsVisible(bool visible);
224
225Q_SIGNALS:
226 void aboutToShow();
227 void aboutToHide();
228 void triggered(QAction *action);
229 void hovered(QAction *action);
230
231protected:
232 int columnCount() const;
233
234 void changeEvent(QEvent *) override;
235 void keyPressEvent(QKeyEvent *) override;
236 void mouseReleaseEvent(QMouseEvent *) override;
237 void mousePressEvent(QMouseEvent *) override;
238 void mouseMoveEvent(QMouseEvent *) override;
239#if QT_CONFIG(wheelevent)
240 void wheelEvent(QWheelEvent *) override;
241#endif
242 void enterEvent(QEvent *) override;
243 void leaveEvent(QEvent *) override;
244 void hideEvent(QHideEvent *) override;
245 void paintEvent(QPaintEvent *) override;
246 void actionEvent(QActionEvent *) override;
247 void timerEvent(QTimerEvent *) override;
248 bool event(QEvent *) override;
249 bool focusNextPrevChild(bool next) override;
250 void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
251
252private Q_SLOTS:
253 void internalDelayedPopup();
254
255private:
256 Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
257 Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
258 Q_PRIVATE_SLOT(d_func(), void _q_overrideMenuActionDestroyed())
259 Q_PRIVATE_SLOT(d_func(), void _q_platformMenuAboutToShow())
260
261protected:
262 QMenu(QMenuPrivate &dd, QWidget* parent = nullptr);
263
264private:
265 Q_DISABLE_COPY(QMenu)
266
267 friend class QMenuBar;
268 friend class QMenuBarPrivate;
269 friend class QTornOffMenu;
270 friend class QComboBox;
271 friend class QAction;
272 friend class QToolButtonPrivate;
273 friend void qt_mac_emit_menuSignals(QMenu *menu, bool show);
274 friend void qt_mac_menu_emit_hovered(QMenu *menu, QAction *action);
275};
276
277#ifdef Q_OS_OSX
278// ### Qt 4 compatibility; remove in Qt 6
279inline QT_DEPRECATED void qt_mac_set_dock_menu(QMenu *menu) { menu->setAsDockMenu(); }
280#endif
281
282QT_END_NAMESPACE
283
284#endif // QMENU_H
285