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 QMENUBAR_H 41 #define QMENUBAR_H 42 43 #include <QtWidgets/qtwidgetsglobal.h> 44 #include <QtWidgets/qmenu.h> 45 46 QT_REQUIRE_CONFIG(menubar); 47 48 QT_BEGIN_NAMESPACE 49 50 class QMenuBarPrivate; 51 class QStyleOptionMenuItem; 52 class QWindowsStyle; 53 class QPlatformMenuBar; 54 55 class Q_WIDGETS_EXPORT QMenuBar : public QWidget 56 { 57 Q_OBJECT 58 59 Q_PROPERTY(bool defaultUp READ isDefaultUp WRITE setDefaultUp) 60 Q_PROPERTY(bool nativeMenuBar READ isNativeMenuBar WRITE setNativeMenuBar) 61 62 public: 63 explicit QMenuBar(QWidget *parent = nullptr); 64 ~QMenuBar(); 65 66 using QWidget::addAction; 67 QAction *addAction(const QString &text); 68 QAction *addAction(const QString &text, const QObject *receiver, const char* member); 69 70 #ifdef Q_CLANG_QDOC 71 template<typename Obj, typename PointerToMemberFunctionOrFunctor> 72 QAction *addAction(const QString &text, const Obj *receiver, PointerToMemberFunctionOrFunctor method); 73 template<typename Functor> 74 QAction *addAction(const QString &text, Functor functor); 75 #else 76 // addAction(QString): Connect to a QObject slot / functor or function pointer (with context) 77 template<typename Obj, typename Func1> 78 inline typename std::enable_if<!std::is_same<const char*, Func1>::value 79 && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type addAction(const QString & text,const Obj * object,Func1 slot)80 addAction(const QString &text, const Obj *object, Func1 slot) 81 { 82 QAction *result = addAction(text); 83 connect(result, &QAction::triggered, object, std::move(slot)); 84 return result; 85 } 86 // addAction(QString): Connect to a functor or function pointer (without context) 87 template <typename Func1> addAction(const QString & text,Func1 slot)88 inline QAction *addAction(const QString &text, Func1 slot) 89 { 90 QAction *result = addAction(text); 91 connect(result, &QAction::triggered, std::move(slot)); 92 return result; 93 } 94 #endif // !Q_CLANG_QDOC 95 96 QAction *addMenu(QMenu *menu); 97 QMenu *addMenu(const QString &title); 98 QMenu *addMenu(const QIcon &icon, const QString &title); 99 100 101 QAction *addSeparator(); 102 QAction *insertSeparator(QAction *before); 103 104 QAction *insertMenu(QAction *before, QMenu *menu); 105 106 void clear(); 107 108 QAction *activeAction() const; 109 void setActiveAction(QAction *action); 110 111 void setDefaultUp(bool); 112 bool isDefaultUp() const; 113 114 QSize sizeHint() const override; 115 QSize minimumSizeHint() const override; 116 int heightForWidth(int) const override; 117 118 QRect actionGeometry(QAction *) const; 119 QAction *actionAt(const QPoint &) const; 120 121 void setCornerWidget(QWidget *w, Qt::Corner corner = Qt::TopRightCorner); 122 QWidget *cornerWidget(Qt::Corner corner = Qt::TopRightCorner) const; 123 124 #if defined(Q_OS_MACOS) || defined(Q_CLANG_QDOC) 125 NSMenu* toNSMenu(); 126 #endif 127 128 bool isNativeMenuBar() const; 129 void setNativeMenuBar(bool nativeMenuBar); 130 QPlatformMenuBar *platformMenuBar(); 131 public Q_SLOTS: 132 void setVisible(bool visible) override; 133 134 Q_SIGNALS: 135 void triggered(QAction *action); 136 void hovered(QAction *action); 137 138 protected: 139 void changeEvent(QEvent *) override; 140 void keyPressEvent(QKeyEvent *) override; 141 void mouseReleaseEvent(QMouseEvent *) override; 142 void mousePressEvent(QMouseEvent *) override; 143 void mouseMoveEvent(QMouseEvent *) override; 144 void leaveEvent(QEvent *) override; 145 void paintEvent(QPaintEvent *) override; 146 void resizeEvent(QResizeEvent *) override; 147 void actionEvent(QActionEvent *) override; 148 void focusOutEvent(QFocusEvent *) override; 149 void focusInEvent(QFocusEvent *) override; 150 void timerEvent(QTimerEvent *) override; 151 bool eventFilter(QObject *, QEvent *) override; 152 bool event(QEvent *) override; 153 void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const; 154 155 private: 156 Q_DECLARE_PRIVATE(QMenuBar) 157 Q_DISABLE_COPY(QMenuBar) 158 Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered()) 159 Q_PRIVATE_SLOT(d_func(), void _q_actionHovered()) 160 Q_PRIVATE_SLOT(d_func(), void _q_internalShortcutActivated(int)) 161 Q_PRIVATE_SLOT(d_func(), void _q_updateLayout()) 162 163 friend class QMenu; 164 friend class QMenuPrivate; 165 friend class QWindowsStyle; 166 }; 167 168 QT_END_NAMESPACE 169 170 #endif // QMENUBAR_H 171