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