qmenu.h 10 KB

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