source: trunk/src/gui/widgets/qmenu.h@ 642

Last change on this file since 642 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 15.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QMENU_H
43#define QMENU_H
44
45#include <QtGui/qwidget.h>
46#include <QtCore/qstring.h>
47#include <QtGui/qicon.h>
48#include <QtGui/qaction.h>
49
50#ifdef QT3_SUPPORT
51#include <QtGui/qpixmap.h>
52#endif
53
54QT_BEGIN_HEADER
55
56QT_BEGIN_NAMESPACE
57
58QT_MODULE(Gui)
59
60#ifndef QT_NO_MENU
61
62class QMenuPrivate;
63class QStyleOptionMenuItem;
64#ifdef QT3_SUPPORT
65class QMenuItem;
66#endif
67
68class Q_GUI_EXPORT QMenu : public QWidget
69{
70private:
71 Q_OBJECT
72 Q_DECLARE_PRIVATE(QMenu)
73
74 Q_PROPERTY(bool tearOffEnabled READ isTearOffEnabled WRITE setTearOffEnabled)
75 Q_PROPERTY(QString title READ title WRITE setTitle)
76 Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
77 Q_PROPERTY(bool separatorsCollapsible READ separatorsCollapsible WRITE setSeparatorsCollapsible)
78
79public:
80 explicit QMenu(QWidget *parent = 0);
81 explicit QMenu(const QString &title, QWidget *parent = 0);
82 ~QMenu();
83
84#ifdef Q_NO_USING_KEYWORD
85 inline void addAction(QAction *action) { QWidget::addAction(action); }
86#else
87 using QWidget::addAction;
88#endif
89 QAction *addAction(const QString &text);
90 QAction *addAction(const QIcon &icon, const QString &text);
91 QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
92 QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
93
94 QAction *addMenu(QMenu *menu);
95 QMenu *addMenu(const QString &title);
96 QMenu *addMenu(const QIcon &icon, const QString &title);
97
98 QAction *addSeparator();
99
100 QAction *insertMenu(QAction *before, QMenu *menu);
101 QAction *insertSeparator(QAction *before);
102
103 bool isEmpty() const;
104 void clear();
105
106 void setTearOffEnabled(bool);
107 bool isTearOffEnabled() const;
108
109 bool isTearOffMenuVisible() const;
110 void hideTearOffMenu();
111
112 void setDefaultAction(QAction *);
113 QAction *defaultAction() const;
114
115 void setActiveAction(QAction *act);
116 QAction *activeAction() const;
117
118 void popup(const QPoint &pos, QAction *at=0);
119 QAction *exec();
120 QAction *exec(const QPoint &pos, QAction *at=0);
121
122 // ### Qt 5: merge
123 static QAction *exec(QList<QAction*> actions, const QPoint &pos, QAction *at=0);
124 static QAction *exec(QList<QAction*> actions, const QPoint &pos, QAction *at, QWidget *parent);
125
126 QSize sizeHint() const;
127
128 QRect actionGeometry(QAction *) const;
129 QAction *actionAt(const QPoint &) const;
130
131 QAction *menuAction() const;
132
133 QString title() const;
134 void setTitle(const QString &title);
135
136 QIcon icon() const;
137 void setIcon(const QIcon &icon);
138
139 void setNoReplayFor(QWidget *widget);
140#ifdef Q_WS_MAC
141 OSMenuRef macMenu(OSMenuRef merge=0);
142#endif
143
144#ifdef Q_WS_WINCE
145 HMENU wceMenu(bool create = false);
146#endif
147
148 bool separatorsCollapsible() const;
149 void setSeparatorsCollapsible(bool collapse);
150
151Q_SIGNALS:
152 void aboutToShow();
153 void aboutToHide();
154 void triggered(QAction *action);
155 void hovered(QAction *action);
156
157protected:
158 int columnCount() const;
159
160 void changeEvent(QEvent *);
161 void keyPressEvent(QKeyEvent *);
162 void mouseReleaseEvent(QMouseEvent *);
163 void mousePressEvent(QMouseEvent *);
164 void mouseMoveEvent(QMouseEvent *);
165 void wheelEvent(QWheelEvent *);
166 void enterEvent(QEvent *);
167 void leaveEvent(QEvent *);
168 void hideEvent(QHideEvent *);
169 void paintEvent(QPaintEvent *);
170 void actionEvent(QActionEvent *);
171 void timerEvent(QTimerEvent *);
172 bool event(QEvent *);
173 bool focusNextPrevChild(bool next);
174 void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
175
176#ifdef Q_WS_WINCE
177 QAction* wceCommands(uint command);
178#endif
179
180private Q_SLOTS:
181 void internalSetSloppyAction();
182 void internalDelayedPopup();
183
184private:
185 Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
186 Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
187 Q_PRIVATE_SLOT(d_func(), void _q_overrideMenuActionDestroyed())
188
189#ifdef QT3_SUPPORT
190public:
191 //menudata
192 inline QT3_SUPPORT uint count() const { return actions().count(); }
193 inline QT3_SUPPORT int insertItem(const QString &text, const QObject *receiver, const char* member,
194 const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
195 return insertAny(0, &text, receiver, member, &shortcut, 0, id, index);
196 }
197 inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text,
198 const QObject *receiver, const char* member,
199 const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
200 return insertAny(&icon, &text, receiver, member, &shortcut, 0, id, index);
201 }
202 inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, const QObject *receiver, const char* member,
203 const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
204 QIcon icon(pixmap);
205 return insertAny(&icon, 0, receiver, member, &shortcut, 0, id, index);
206 }
207 inline QT3_SUPPORT int insertItem(const QString &text, int id=-1, int index=-1) {
208 return insertAny(0, &text, 0, 0, 0, 0, id, index);
209 }
210 inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, int id=-1, int index=-1) {
211 return insertAny(&icon, &text, 0, 0, 0, 0, id, index);
212 }
213 inline QT3_SUPPORT int insertItem(const QString &text, QMenu *popup, int id=-1, int index=-1) {
214 return insertAny(0, &text, 0, 0, 0, popup, id, index);
215 }
216 inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, QMenu *popup, int id=-1, int index=-1) {
217 return insertAny(&icon, &text, 0, 0, 0, popup, id, index);
218 }
219 inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, int id=-1, int index=-1) {
220 QIcon icon(pixmap);
221 return insertAny(&icon, 0, 0, 0, 0, 0, id, index);
222 }
223 inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, QMenu *popup, int id=-1, int index=-1) {
224 QIcon icon(pixmap);
225 return insertAny(&icon, 0, 0, 0, 0, popup, id, index);
226 }
227 QT3_SUPPORT int insertItem(QMenuItem *item, int id=-1, int index=-1);
228 QT3_SUPPORT int insertSeparator(int index=-1);
229 inline QT3_SUPPORT void removeItem(int id) {
230 if(QAction *act = findActionForId(id))
231 removeAction(act); }
232 inline QT3_SUPPORT void removeItemAt(int index) {
233 if(QAction *act = actions().value(index))
234 removeAction(act); }
235#ifndef QT_NO_SHORTCUT
236 inline QT3_SUPPORT QKeySequence accel(int id) const {
237 if(QAction *act = findActionForId(id))
238 return act->shortcut();
239 return QKeySequence(); }
240 inline QT3_SUPPORT void setAccel(const QKeySequence& key, int id) {
241 if(QAction *act = findActionForId(id))
242 act->setShortcut(key);
243 }
244#endif
245 inline QT3_SUPPORT QIcon iconSet(int id) const {
246 if(QAction *act = findActionForId(id))
247 return act->icon();
248 return QIcon(); }
249 inline QT3_SUPPORT QString text(int id) const {
250 if(QAction *act = findActionForId(id))
251 return act->text();
252 return QString(); }
253 inline QT3_SUPPORT QPixmap pixmap(int id) const {
254 if(QAction *act = findActionForId(id))
255 return act->icon().pixmap(QSize(22, 22));
256 return QPixmap(); }
257 inline QT3_SUPPORT void setWhatsThis(int id, const QString &w) {
258 if(QAction *act = findActionForId(id))
259 act->setWhatsThis(w); }
260 inline QT3_SUPPORT QString whatsThis(int id) const {
261 if(QAction *act = findActionForId(id))
262 return act->whatsThis();
263 return QString(); }
264
265 inline QT3_SUPPORT void changeItem(int id, const QString &text) {
266 if(QAction *act = findActionForId(id))
267 act->setText(text); }
268 inline QT3_SUPPORT void changeItem(int id, const QPixmap &pixmap) {
269 if(QAction *act = findActionForId(id))
270 act->setIcon(QIcon(pixmap)); }
271 inline QT3_SUPPORT void changeItem(int id, const QIcon &icon, const QString &text) {
272 if(QAction *act = findActionForId(id)) {
273 act->setIcon(icon);
274 act->setText(text);
275 }
276 }
277 inline QT3_SUPPORT void setActiveItem(int id) {
278 setActiveAction(findActionForId(id));
279 }