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

Last change on this file since 1111 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 15.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
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 qt-info@nokia.com.
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();
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#ifndef QT_NO_WHEELEVENT
166 void wheelEvent(QWheelEvent *);
167#endif
168 void enterEvent(QEvent *);
169 void leaveEvent(QEvent *);
170 void hideEvent(QHideEvent *);
171 void paintEvent(QPaintEvent *);
172 void actionEvent(QActionEvent *);
173 void timerEvent(QTimerEvent *);
174 bool event(QEvent *);
175 bool focusNextPrevChild(bool next);
176 void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
177
178#ifdef Q_WS_WINCE
179 QAction* wceCommands(uint command);
180#endif
181
182private Q_SLOTS:
183 void internalSetSloppyAction();
184 void internalDelayedPopup();
185
186private:
187 Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
188 Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
189 Q_PRIVATE_SLOT(d_func(), void _q_overrideMenuActionDestroyed())
190
191#ifdef QT3_SUPPORT
192public:
193 //menudata
194 inline QT3_SUPPORT uint count() const { return actions().count(); }
195 inline QT3_SUPPORT int insertItem(const QString &text, const QObject *receiver, const char* member,
196 const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
197 return insertAny(0, &text, receiver, member, &shortcut, 0, id, index);
198 }
199 inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text,
200 const QObject *receiver, const char* member,
201 const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
202 return insertAny(&icon, &text, receiver, member, &shortcut, 0, id, index);
203 }
204 inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, const QObject *receiver, const char* member,
205 const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
206 QIcon icon(pixmap);
207 return insertAny(&icon, 0, receiver, member, &shortcut, 0, id, index);
208 }
209 inline QT3_SUPPORT int insertItem(const QString &text, int id=-1, int index=-1) {
210 return insertAny(0, &text, 0, 0, 0, 0, id, index);
211 }
212 inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, int id=-1, int index=-1) {
213 return insertAny(&icon, &text, 0, 0, 0, 0, id, index);
214 }
215 inline QT3_SUPPORT int insertItem(const QString &text, QMenu *popup, int id=-1, int index=-1) {
216 return insertAny(0, &text, 0, 0, 0, popup, id, index);
217 }
218 inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, QMenu *popup, int id=-1, int index=-1) {
219 return insertAny(&icon, &text, 0, 0, 0, popup, id, index);
220 }
221 inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, int id=-1, int index=-1) {
222 QIcon icon(pixmap);
223 return insertAny(&icon, 0, 0, 0, 0, 0, id, index);
224 }
225 inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, QMenu *popup, int id=-1, int index=-1) {
226 QIcon icon(pixmap);
227 return insertAny(&icon, 0, 0, 0, 0, popup, id, index);
228 }
229 QT3_SUPPORT int insertItem(QMenuItem *item, int id=-1, int index=-1);
230 QT3_SUPPORT int insertSeparator(int index=-1);
231 inline QT3_SUPPORT void removeItem(int id) {
232 if(QAction *act = findActionForId(id))
233 removeAction(act); }
234 inline QT3_SUPPORT void removeItemAt(int index) {
235 if(QAction *act = actions().value(index))
236 removeAction(act); }
237#ifndef QT_NO_SHORTCUT
238 inline QT3_SUPPORT QKeySequence accel(int id) const {
239 if(QAction *act = findActionForId(id))
240 return act->shortcut();
241 return QKeySequence(); }
242 inline QT3_SUPPORT void setAccel(const QKeySequence& key, int id) {
243 if(QAction *act = findActionForId(id))
244 act->setShortcut(key);
245 }
246#endif
247 inline QT3_SUPPORT QIcon iconSet(int id) const {
248 if(QAction *act = findActionForId(id))
249 return act->icon();
250 return QIcon(); }
251 inline QT3_SUPPORT QString text(int id) const {
252 if(QAction *act = findActionForId(id))
253 return act->text();
254 return QString(); }
255 inline QT3_SUPPORT QPixmap pixmap(int id) const {
256 if(QAction *act = findActionForId(id))
257 return act->icon().pixmap(QSize(22, 22));
258 return QPixmap(); }
259 inline QT3_SUPPORT void setWhatsThis(int id, const QString &w) {
260 if(QAction *act = findActionForId(id))
261 act->setWhatsThis(w); }
262 inline QT3_SUPPORT QString whatsThis(int id) const {
263 if(QAction *act = findActionForId(id))
264 return act->whatsThis();
265 return QString(); }
266
267 inline QT3_SUPPORT void changeItem(int id, const QString &text) {
268 if(QAction *act = findActionForId(id))
269 act->setText(text); }
270 inline QT3_SUPPORT void changeItem(int id, const QPixmap &pixmap) {
271 if(QAction *act = findActionForId(id))
272 act->setIcon(QIcon(pixmap)); }
273 inline QT3_SUPPORT void changeItem(int id, const QIcon &icon, const QString &text) {
274 if(QAction *act = findActionForId(id)) {
275 act->setIcon(icon);
276 act->setText(text);
277 }
278 }
279 inline QT3_SUPPORT void setActiveItem(int id) {
280 setActiveAction(findActionForId(id));
281 }
282 inline QT3_SUPPORT bool isItemActive(int id) const {
283 return findActionForId(id) == activeAction();
284 }
285 inline QT3_SUPPORT bool isItemEnabled(int id) const {
286 if(QAction *act = findActionForId(id))
287 return act->isEnabled();
288 return false; }
289 inline QT3_SUPPORT void setItemEnabled(int id, bool enable) {
290 if(QAction *act = findActionForId(id))
291 act->setEnabled(enable);
292 }
293 inline QT3_SUPPORT bool isItemChecked(int id) const {
294 if(QAction *act = findActionForId(id))
295 return act->isChecked();
296 return false;
297 }
298 inline QT3_SUPPORT void setItemChecked(int id, bool check) {
299 if(QAction *act = findActionForId(id)) {
300 act->setCheckable(true);
301 act->setChecked(check);
302 }
303 }
304 inline QT3_SUPPORT bool isItemVisible(int id) const {
305 if(QAction *act = findActionForId(id))
306 return act->isVisible();
307 return false;
308 }
309 inline QT3_SUPPORT void setItemVisible(int id, bool visible) {
310 if(QAction *act = findActionForId(id))
311 act->setVisible(visible);
312 }
313 inline QT3_SUPPORT QRect itemGeometry(int index) {
314 if(QAction *act = actions().value(index))
315 return actionGeometry(act);
316 return QRect();
317 }
318 inline QT3_SUPPORT QFont itemFont(int id) const {
319 if(QAction *act = findActionForId(id))
320 return act->font();
321 return QFont();
322 }
323 inline QT3_SUPPORT void setItemFont(int id, const QFont &font) {
324 if(QAction *act = findActionForId(id))
325 act->setFont(font);
326 }
327 inline QT3_SUPPORT int indexOf(int id) const {
328 return actions().indexOf(findActionForId(id));
329 }
330 inline QT3_SUPPORT int idAt(int index) const {
331 return findIdForAction(actions().value(index));
332 }
333 QT3_SUPPORT void setId (int index, int id);
334 inline QT3_SUPPORT void activateItemAt(int index) {
335 if(QAction *ret = actions().value(index))
336 ret->activate(QAction::Trigger);
337 }
338 inline QT3_SUPPORT bool connectItem(int id, const QObject *receiver, const char* member) {
339 if(QAction *act = findActionForId(id)) {
340 QObject::connect(act, SIGNAL(activated(int)), receiver, member);
341 return true;
342 }
343 return false;
344 }
345 inline QT3_SUPPORT bool disconnectItem(int id,const QObject *receiver, const char* member) {
346 if(QAction *act = findActionForId(id)) {
347 QObject::disconnect(act, SIGNAL(triggered()), receiver, member);
348 return true;
349 }
350 return false;
351 }
352 inline QT3_SUPPORT QMenuItem *findItem(int id) const {
353 return reinterpret_cast<QMenuItem*>(findActionForId(id));
354 }
355
356 inline QT3_SUPPORT void setCheckable(bool){}
357 inline QT3_SUPPORT bool isCheckable() const {return true;}
358
359 QT3_SUPPORT QMenuItem *findPopup( QMenu *popup, int *index );
360
361 QT3_SUPPORT bool setItemParameter(int id, int param);
362 QT3_SUPPORT int itemParameter(int id) const;
363
364 //frame
365 QT3_SUPPORT int frameWidth() const;
366
367 //popupmenu
368 inline QT3_SUPPORT void popup(const QPoint & pos, int indexAtPoint) { popup(pos, actions().value(indexAtPoint)); }
369 inline QT3_SUPPORT int insertTearOffHandle(int = 0, int = 0) {
370 setTearOffEnabled(true);
371 return -1;
372 }
373
374protected:
375 inline QT3_SUPPORT int itemAtPos(const QPoint &p, bool ignoreSeparator = true) {
376 QAction *ret = actionAt(p);
377 if(ignoreSeparator && ret && ret->isSeparator())
378 return -1;
379 return findIdForAction(ret);
380 }
381 inline QT3_SUPPORT int columns() const { return columnCount(); }
382 inline QT3_SUPPORT int itemHeight(int index) {
383 return actionGeometry(actions().value(index)).height();
384 }
385 inline QT3_SUPPORT int itemHeight(QMenuItem *mi) {
386 return actionGeometry(reinterpret_cast<QAction *>(mi)).height();
387 }
388
389Q_SIGNALS:
390 QT_MOC_COMPAT void activated(int itemId);
391 QT_MOC_COMPAT void highlighted(int itemId);
392
393private:
394 int insertAny(const QIcon *icon, const QString *text, const QObject *receiver, const char *member,
395 const QKeySequence *shorcut, const QMenu *popup, int id, int index);
396 QAction *findActionForId(int id) const;
397 int findIdForAction(QAction*) const;
398#endif
399
400protected:
401 QMenu(QMenuPrivate &dd, QWidget* parent = 0);
402
403private:
404 Q_DISABLE_COPY(QMenu)
405
406 friend class QMenuBar;
407 friend class QMenuBarPrivate;
408 friend class QTornOffMenu;
409 friend class Q3PopupMenu;
410 friend class QComboBox;
411 friend class QAction;
412 friend class QToolButtonPrivate;
413
414#ifdef Q_WS_MAC
415 friend void qt_mac_trayicon_activate_action(QMenu *, QAction *action);
416 friend bool qt_mac_watchingAboutToShow(QMenu *);
417 friend OSStatus qt_mac_menu_event(EventHandlerCallRef, EventRef, void *);
418 friend bool qt_mac_activate_action(OSMenuRef, uint, QAction::ActionEvent, bool);
419 friend void qt_mac_emit_menuSignals(QMenu *, bool);
420 friend void qt_mac_menu_emit_hovered(QMenu *menu, QAction *action);
421#endif
422};
423
424#endif // QT_NO_MENU
425
426QT_END_NAMESPACE
427
428QT_END_HEADER
429
430#endif // QMENU_H
Note: See TracBrowser for help on using the repository browser.