QtGui/qmenu.h Source File

qmenu.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
54 #ifdef Q_WS_WINCE
55 #include <windef.h> // for HMENU
56 #endif
57 
59 
61 
62 QT_MODULE(Gui)
63 
64 #ifndef QT_NO_MENU
65 
66 class QMenuPrivate;
68 #ifdef QT3_SUPPORT
69 class QMenuItem;
70 #endif
71 
72 class Q_GUI_EXPORT QMenu : public QWidget
73 {
74 private:
75  Q_OBJECT
76  Q_DECLARE_PRIVATE(QMenu)
77 
78  Q_PROPERTY(bool tearOffEnabled READ isTearOffEnabled WRITE setTearOffEnabled)
79  Q_PROPERTY(QString title READ title WRITE setTitle)
80  Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
81  Q_PROPERTY(bool separatorsCollapsible READ separatorsCollapsible WRITE setSeparatorsCollapsible)
82 
83 public:
84  explicit QMenu(QWidget *parent = 0);
85  explicit QMenu(const QString &title, QWidget *parent = 0);
86  ~QMenu();
87 
88 #ifdef Q_NO_USING_KEYWORD
89  inline void addAction(QAction *action) { QWidget::addAction(action); }
90 #else
91  using QWidget::addAction;
92 #endif
93  QAction *addAction(const QString &text);
94  QAction *addAction(const QIcon &icon, const QString &text);
95  QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
96  QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
97 
98  QAction *addMenu(QMenu *menu);
99  QMenu *addMenu(const QString &title);
100  QMenu *addMenu(const QIcon &icon, const QString &title);
101 
102  QAction *addSeparator();
103 
104  QAction *insertMenu(QAction *before, QMenu *menu);
105  QAction *insertSeparator(QAction *before);
106 
107  bool isEmpty() const;
108  void clear();
109 
110  void setTearOffEnabled(bool);
111  bool isTearOffEnabled() const;
112 
113  bool isTearOffMenuVisible() const;
114  void hideTearOffMenu();
115 
116  void setDefaultAction(QAction *);
117  QAction *defaultAction() const;
118 
119  void setActiveAction(QAction *act);
120  QAction *activeAction() const;
121 
122  void popup(const QPoint &pos, QAction *at=0);
123  QAction *exec();
124  QAction *exec(const QPoint &pos, QAction *at=0);
125 
126  // ### Qt 5: merge
127  static QAction *exec(QList<QAction*> actions, const QPoint &pos, QAction *at=0);
128  static QAction *exec(QList<QAction*> actions, const QPoint &pos, QAction *at, QWidget *parent);
129 
130  QSize sizeHint() const;
131 
132  QRect actionGeometry(QAction *) const;
133  QAction *actionAt(const QPoint &) const;
134 
135  QAction *menuAction() const;
136 
137  QString title() const;
138  void setTitle(const QString &title);
139 
140  QIcon icon() const;
141  void setIcon(const QIcon &icon);
142 
143  void setNoReplayFor(QWidget *widget);
144 #ifdef Q_WS_MAC
145  OSMenuRef macMenu(OSMenuRef merge=0);
146 #endif
147 
148 #ifdef Q_WS_WINCE
149  HMENU wceMenu();
150 #endif
151 
152  bool separatorsCollapsible() const;
153  void setSeparatorsCollapsible(bool collapse);
154 
155 Q_SIGNALS:
156  void aboutToShow();
157  void aboutToHide();
158  void triggered(QAction *action);
159  void hovered(QAction *action);
160 
161 protected:
162  int columnCount() const;
163 
164  void changeEvent(QEvent *);
165  void keyPressEvent(QKeyEvent *);
168  void mouseMoveEvent(QMouseEvent *);
169 #ifndef QT_NO_WHEELEVENT
170  void wheelEvent(QWheelEvent *);
171 #endif
172  void enterEvent(QEvent *);
173  void leaveEvent(QEvent *);
174  void hideEvent(QHideEvent *);
175  void paintEvent(QPaintEvent *);
176  void actionEvent(QActionEvent *);
177  void timerEvent(QTimerEvent *);
178  bool event(QEvent *);
179  bool focusNextPrevChild(bool next);
180  void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
181 
182 #ifdef Q_WS_WINCE
183  QAction* wceCommands(uint command);
184 #endif
185 
186 private Q_SLOTS:
187  void internalSetSloppyAction();
188  void internalDelayedPopup();
189 
190 private:
191  Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
192  Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
193  Q_PRIVATE_SLOT(d_func(), void _q_overrideMenuActionDestroyed())
194 
195 #ifdef QT3_SUPPORT
196 public:
197  //menudata
198  inline QT3_SUPPORT uint count() const { return actions().count(); }
199  inline QT3_SUPPORT int insertItem(const QString &text, const QObject *receiver, const char* member,
200  const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
201  return insertAny(0, &text, receiver, member, &shortcut, 0, id, index);
202  }
203  inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text,
204  const QObject *receiver, const char* member,
205  const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
206  return insertAny(&icon, &text, receiver, member, &shortcut, 0, id, index);
207  }
208  inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, const QObject *receiver, const char* member,
209  const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
210  QIcon icon(pixmap);
211  return insertAny(&icon, 0, receiver, member, &shortcut, 0, id, index);
212  }
213  inline QT3_SUPPORT int insertItem(const QString &text, int id=-1, int index=-1) {
214  return insertAny(0, &text, 0, 0, 0, 0, id, index);
215  }
216  inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, int id=-1, int index=-1) {
217  return insertAny(&icon, &text, 0, 0, 0, 0, id, index);
218  }
219  inline QT3_SUPPORT int insertItem(const QString &text, QMenu *popup, int id=-1, int index=-1) {
220  return insertAny(0, &text, 0, 0, 0, popup, id, index);
221  }
222  inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, QMenu *popup, int id=-1, int index=-1) {
223  return insertAny(&icon, &text, 0, 0, 0, popup, id, index);
224  }
225  inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, int id=-1, int index=-1) {
226  QIcon icon(pixmap);
227  return insertAny(&icon, 0, 0, 0, 0, 0, id, index);
228  }
229  inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, QMenu *popup, int id=-1, int index=-1) {
230  QIcon icon(pixmap);
231  return insertAny(&icon, 0, 0, 0, 0, popup, id, index);
232  }
233  QT3_SUPPORT int insertItem(QMenuItem *item, int id=-1, int index=-1);
234  QT3_SUPPORT int insertSeparator(int index=-1);
235  inline QT3_SUPPORT void removeItem(int id) {
236  if(QAction *act = findActionForId(id))
237  removeAction(act); }
238  inline QT3_SUPPORT void removeItemAt(int index) {
239  if(QAction *act = actions().value(index))
240  removeAction(act); }
241 #ifndef QT_NO_SHORTCUT
242  inline QT3_SUPPORT QKeySequence accel(int id) const {
243  if(QAction *act = findActionForId(id))
244  return act->shortcut();
245  return QKeySequence(); }
246  inline QT3_SUPPORT void setAccel(const QKeySequence& key, int id) {
247  if(QAction *act = findActionForId(id))
248  act->setShortcut(key);
249  }
250 #endif
251  inline QT3_SUPPORT QIcon iconSet(int id) const {
252  if(QAction *act = findActionForId(id))
253  return act->icon();
254  return QIcon(); }
255  inline QT3_SUPPORT QString text(int id) const {
256  if(QAction *act = findActionForId(id))
257  return act->text();
258  return QString(); }
259  inline QT3_SUPPORT QPixmap pixmap(int id) const {
260  if(QAction *act = findActionForId(id))
261  return act->icon().pixmap(QSize(22, 22));
262  return QPixmap(); }
263  inline QT3_SUPPORT void setWhatsThis(int id, const QString &w) {
264  if(QAction *act = findActionForId(id))
265  act->setWhatsThis(w); }
266  inline QT3_SUPPORT QString whatsThis(int id) const {
267  if(QAction *act = findActionForId(id))
268  return act->whatsThis();
269  return QString(); }
270 
271  inline QT3_SUPPORT void changeItem(int id, const QString &text) {
272  if(QAction *act = findActionForId(id))
273  act->setText(text); }
274  inline QT3_SUPPORT void changeItem(int id, const QPixmap &pixmap) {
275  if(QAction *act = findActionForId(id))
276  act->setIcon(QIcon(pixmap)); }
277  inline QT3_SUPPORT void changeItem(int id, const QIcon &icon, const QString &text) {
278  if(QAction *act = findActionForId(id)) {
279  act->setIcon(icon);
280  act->setText(text);
281  }
282  }
283  inline QT3_SUPPORT void setActiveItem(int id) {
284  setActiveAction(findActionForId(id));
285  }
286  inline QT3_SUPPORT bool isItemActive(int id) const {
287  return findActionForId(id) == activeAction();
288  }
289  inline QT3_SUPPORT bool isItemEnabled(int id) const {
290  if(QAction *act = findActionForId(id))
291  return act->isEnabled();
292  return false; }
293  inline QT3_SUPPORT void setItemEnabled(int id, bool enable) {
294  if(QAction *act = findActionForId(id))
295  act->setEnabled(enable);
296  }
297  inline QT3_SUPPORT bool isItemChecked(int id) const {
298  if(QAction *act = findActionForId(id))
299  return act->isChecked();
300  return false;
301  }
302  inline QT3_SUPPORT void setItemChecked(int id, bool check) {
303  if(QAction *act = findActionForId(id)) {
304  act->setCheckable(true);
305  act->setChecked(check);
306  }
307  }
308  inline QT3_SUPPORT bool isItemVisible(int id) const {
309  if(QAction *act = findActionForId(id))
310  return act->isVisible();
311  return false;
312  }
313  inline QT3_SUPPORT void setItemVisible(int id, bool visible) {
314  if(QAction *act = findActionForId(id))
315  act->setVisible(visible);
316  }
317  inline QT3_SUPPORT QRect itemGeometry(int index) {
318  if(QAction *act = actions().value(index))
319  return actionGeometry(act);
320  return QRect();
321  }
322  inline QT3_SUPPORT QFont itemFont(int id) const {
323  if(QAction *act = findActionForId(id))
324  return act->font();
325  return QFont();
326  }
327  inline QT3_SUPPORT void setItemFont(int id, const QFont &font) {
328  if(QAction *act = findActionForId(id))
329  act->setFont(font);
330  }
331  inline QT3_SUPPORT int indexOf(int id) const {
332  return actions().indexOf(findActionForId(id));
333  }
334  inline QT3_SUPPORT int idAt(int index) const {
335  return findIdForAction(actions().value(index));
336  }
337  QT3_SUPPORT void setId (int index, int id);
338  inline QT3_SUPPORT void activateItemAt(int index) {
339  if(QAction *ret = actions().value(index))
340  ret->activate(QAction::Trigger);
341  }
342  inline QT3_SUPPORT bool connectItem(int id, const QObject *receiver, const char* member) {
343  if(QAction *act = findActionForId(id)) {
344  QObject::connect(act, SIGNAL(activated(int)), receiver, member);
345  return true;
346  }
347  return false;
348  }
349  inline QT3_SUPPORT bool disconnectItem(int id,const QObject *receiver, const char* member) {
350  if(QAction *act = findActionForId(id)) {
351  QObject::disconnect(act, SIGNAL(triggered()), receiver, member);
352  return true;
353  }
354  return false;
355  }
356  inline QT3_SUPPORT QMenuItem *findItem(int id) const {
357  return reinterpret_cast<QMenuItem*>(findActionForId(id));
358  }
359 
360  inline QT3_SUPPORT void setCheckable(bool){}
361  inline QT3_SUPPORT bool isCheckable() const {return true;}
362 
363  QT3_SUPPORT QMenuItem *findPopup( QMenu *popup, int *index );
364 
365  QT3_SUPPORT bool setItemParameter(int id, int param);
366  QT3_SUPPORT int itemParameter(int id) const;
367 
368  //frame
369  QT3_SUPPORT int frameWidth() const;
370 
371  //popupmenu
372  inline QT3_SUPPORT void popup(const QPoint & pos, int indexAtPoint) { popup(pos, actions().value(indexAtPoint)); }
373  inline QT3_SUPPORT int insertTearOffHandle(int = 0, int = 0) {
374  setTearOffEnabled(true);
375  return -1;
376  }
377 
378 protected:
379  inline QT3_SUPPORT int itemAtPos(const QPoint &p, bool ignoreSeparator = true) {
380  QAction *ret = actionAt(p);
381  if(ignoreSeparator && ret && ret->isSeparator())
382  return -1;
383  return findIdForAction(ret);
384  }
385  inline QT3_SUPPORT int columns() const { return columnCount(); }
386  inline QT3_SUPPORT int itemHeight(int index) {
387  return actionGeometry(actions().value(index)).height();
388  }
389  inline QT3_SUPPORT int itemHeight(QMenuItem *mi) {
390  return actionGeometry(reinterpret_cast<QAction *>(mi)).height();
391  }
392 
393 Q_SIGNALS:
394  QT_MOC_COMPAT void activated(int itemId);
395  QT_MOC_COMPAT void highlighted(int itemId);
396 
397 private:
398  int insertAny(const QIcon *icon, const QString *text, const QObject *receiver, const char *member,
399  const QKeySequence *shorcut, const QMenu *popup, int id, int index);
400  QAction *findActionForId(int id) const;
401  int findIdForAction(QAction*) const;
402 #endif
403 
404 protected:
405  QMenu(QMenuPrivate &dd, QWidget* parent = 0);
406 
407 private:
408  Q_DISABLE_COPY(QMenu)
409 
410  friend class QMenuBar;
411  friend class QMenuBarPrivate;
412  friend class QTornOffMenu;
413  friend class Q3PopupMenu;
414  friend class QComboBox;
415  friend class QAction;
416  friend class QToolButtonPrivate;
417 
418 #ifdef Q_WS_MAC
419  friend void qt_mac_trayicon_activate_action(QMenu *, QAction *action);
420  friend bool qt_mac_watchingAboutToShow(QMenu *);
421  friend OSStatus qt_mac_menu_event(EventHandlerCallRef, EventRef, void *);
422  friend bool qt_mac_activate_action(OSMenuRef, uint, QAction::ActionEvent, bool);
423  friend void qt_mac_emit_menuSignals(QMenu *, bool);
424  friend void qt_mac_menu_emit_hovered(QMenu *menu, QAction *action);
425 #endif
426 };
427 
428 #endif // QT_NO_MENU
429 
431 
433 
434 #endif // QMENU_H
bool event(QEvent *)
virtual bool focusNextPrevChild(bool next)
#define QT_END_NAMESPACE
Definition: qglobal.h:128
#define Q_PROPERTY(text)
Definition: qobjectdefs.h:80
QString whatsThis() const
void removeAction(QAction *action)
#define QT_BEGIN_HEADER
Definition: qglobal.h:141
GLXFBConfig Pixmap pixmap
Definition: GLee.h:10237
bool isSeparator() const
virtual void wheelEvent(QWheelEvent *)
virtual void mouseMoveEvent(QMouseEvent *)
virtual void keyPressEvent(QKeyEvent *)
GLXDrawable GLXDrawable member
Definition: GLee.h:10556
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
#define Q_SLOTS
Definition: qobjectdefs.h:71
#define Q_SIGNALS
Definition: qobjectdefs.h:72
Definition: qicon.h:60
QList< QAction * > actions() const
Definition: qpoint.h:53
virtual void timerEvent(QTimerEvent *)
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
virtual void mouseReleaseEvent(QMouseEvent *)
Definition: qfont.h:64
Definition: qmenu.h:72
virtual QSize sizeHint() const
GLuint GLuint GLsizei count
Definition: GLee.h:872
signed long OSStatus
#define Q_OBJECT
Definition: qobjectdefs.h:157
struct OpaqueEventRef * EventRef
MenuRef OSMenuRef
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
GLsizei const GLfloat * value
Definition: GLee.h:1742
Definition: qrect.h:58
GLfloat GLfloat p
Definition: GLee.h:5416
void addAction(QAction *action)
Definition: qsize.h:53
virtual void paintEvent(QPaintEvent *)
virtual void hideEvent(QHideEvent *)
friend class QPixmap
Definition: qwidget.h:748
struct OpaqueEventHandlerCallRef * EventHandlerCallRef
virtual void mousePressEvent(QMouseEvent *)
GLubyte GLubyte GLubyte GLubyte w
Definition: GLee.h:1775
ActionEvent
Definition: qaction.h:176
#define QT_END_HEADER
Definition: qglobal.h:142
void setWhatsThis(const QString &)
GLfloat param
Definition: GLee.h:1364
virtual void leaveEvent(QEvent *)
GLuint index
Definition: GLee.h:1704
virtual void actionEvent(QActionEvent *)
#define SIGNAL(a)
Definition: qobjectdefs.h:227
virtual void changeEvent(QEvent *)
virtual void enterEvent(QEvent *)