src/gui/widgets/qmenu.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the QtGui module of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #ifndef QMENU_H
00025 #define QMENU_H
00026 
00027 #include <QtGui/qwidget.h>
00028 #include <QtCore/qstring.h>
00029 #include <QtGui/qicon.h>
00030 #include <QtGui/qaction.h>
00031 
00032 QT_BEGIN_HEADER
00033 
00034 QT_MODULE(Gui)
00035 
00036 #ifndef QT_NO_MENU
00037 
00038 class QMenuPrivate;
00039 #ifdef QT3_SUPPORT
00040 class QMenuItem;
00041 #include <QtGui/qpixmap.h>
00042 #endif
00043 
00044 class Q_GUI_EXPORT QMenu : public QWidget
00045 {
00046 private:
00047     Q_OBJECT
00048     Q_DECLARE_PRIVATE(QMenu)
00049 
00050     Q_PROPERTY(bool tearOffEnabled READ isTearOffEnabled WRITE setTearOffEnabled)
00051     Q_PROPERTY(QString title READ title WRITE setTitle)
00052     Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
00053     Q_PROPERTY(bool separatorsCollapsible READ separatorsCollapsible WRITE setSeparatorsCollapsible)
00054 
00055 public:
00056     explicit QMenu(QWidget *parent = 0);
00057     explicit QMenu(const QString &title, QWidget *parent = 0);
00058     ~QMenu();
00059 
00060 #ifdef Q_NO_USING_KEYWORD
00061     inline void addAction(QAction *action) { QWidget::addAction(action); }
00062 #else
00063     using QWidget::addAction;
00064 #endif
00065     QAction *addAction(const QString &text);
00066     QAction *addAction(const QIcon &icon, const QString &text);
00067     QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
00068     QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
00069 
00070     QAction *addMenu(QMenu *menu);
00071     QMenu *addMenu(const QString &title);
00072     QMenu *addMenu(const QIcon &icon, const QString &title);
00073 
00074     QAction *addSeparator();
00075 
00076     QAction *insertMenu(QAction *before, QMenu *menu);
00077     QAction *insertSeparator(QAction *before);
00078 
00079     bool isEmpty() const;
00080     void clear();
00081 
00082     void setTearOffEnabled(bool);
00083     bool isTearOffEnabled() const;
00084 
00085     bool isTearOffMenuVisible() const;
00086     void hideTearOffMenu();
00087 
00088     void setDefaultAction(QAction *);
00089     QAction *defaultAction() const;
00090 
00091     void setActiveAction(QAction *act);
00092     QAction *activeAction() const;
00093 
00094     void popup(const QPoint &pos, QAction *at=0);
00095     QAction *exec();
00096     QAction *exec(const QPoint &pos, QAction *at=0);
00097     static QAction *exec(QList<QAction*> actions, const QPoint &pos, QAction *at=0);
00098 
00099     QSize sizeHint() const;
00100 
00101     QRect actionGeometry(QAction *) const;
00102     QAction *actionAt(const QPoint &) const;
00103 
00104     QAction *menuAction() const;
00105 
00106     QString title() const;
00107     void setTitle(const QString &title);
00108 
00109     QIcon icon() const;
00110     void setIcon(const QIcon &icon);
00111 
00112     void setNoReplayFor(QWidget *widget);
00113 #ifdef Q_WS_MAC
00114     MenuRef macMenu(MenuRef merge=0);
00115 #endif
00116 
00117     bool separatorsCollapsible() const;
00118     void setSeparatorsCollapsible(bool collapse);
00119 
00120 Q_SIGNALS:
00121     void aboutToShow();
00122     void aboutToHide();
00123     void triggered(QAction *action);
00124     void hovered(QAction *action);
00125 
00126 protected:
00127     int columnCount() const;
00128 
00129     void changeEvent(QEvent *);
00130     void keyPressEvent(QKeyEvent *);
00131     void mouseReleaseEvent(QMouseEvent *);
00132     void mousePressEvent(QMouseEvent *);
00133     void mouseMoveEvent(QMouseEvent *);
00134     void wheelEvent(QWheelEvent *);
00135     void enterEvent(QEvent *);
00136     void leaveEvent(QEvent *);
00137     void hideEvent(QHideEvent *);
00138     void paintEvent(QPaintEvent *);
00139     void actionEvent(QActionEvent *);
00140     void timerEvent(QTimerEvent *);
00141     bool event(QEvent *);
00142     bool focusNextPrevChild(bool next);
00143 
00144 private Q_SLOTS:
00145     void internalSetSloppyAction();
00146     void internalDelayedPopup();
00147 
00148 private:
00149     Q_PRIVATE_SLOT(d_func(), void _q_actionTriggered())
00150     Q_PRIVATE_SLOT(d_func(), void _q_actionHovered())
00151 
00152 #ifdef QT3_SUPPORT
00153 public:
00154     //menudata
00155     inline QT3_SUPPORT uint count() const { return actions().count(); }
00156     inline QT3_SUPPORT int insertItem(const QString &text, const QObject *receiver, const char* member,
00157                                     const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
00158         return insertAny(0, &text, receiver, member, &shortcut, 0, id, index);
00159     }
00160     inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text,
00161                                     const QObject *receiver, const char* member,
00162                                     const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
00163         return insertAny(&icon, &text, receiver, member, &shortcut, 0, id, index);
00164     }
00165     inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, const QObject *receiver, const char* member,
00166                                     const QKeySequence& shortcut = 0, int id = -1, int index = -1) {
00167         QIcon icon(pixmap);
00168         return insertAny(&icon, 0, receiver, member, &shortcut, 0, id, index);
00169     }
00170     inline QT3_SUPPORT int insertItem(const QString &text, int id=-1, int index=-1) {
00171         return insertAny(0, &text, 0, 0, 0, 0, id, index);
00172     }
00173     inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, int id=-1, int index=-1) {
00174         return insertAny(&icon, &text, 0, 0, 0, 0, id, index);
00175     }
00176     inline QT3_SUPPORT int insertItem(const QString &text, QMenu *popup, int id=-1, int index=-1) {
00177         return insertAny(0, &text, 0, 0, 0, popup, id, index);
00178     }
00179     inline QT3_SUPPORT int insertItem(const QIcon& icon, const QString &text, QMenu *popup, int id=-1, int index=-1) {
00180         return insertAny(&icon, &text, 0, 0, 0, popup, id, index);
00181     }
00182     inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, int id=-1, int index=-1) {
00183         QIcon icon(pixmap);
00184         return insertAny(&icon, 0, 0, 0, 0, 0, id, index);
00185     }
00186     inline QT3_SUPPORT int insertItem(const QPixmap &pixmap, QMenu *popup, int id=-1, int index=-1) {
00187         QIcon icon(pixmap);
00188         return insertAny(&icon, 0, 0, 0, 0, popup, id, index);
00189     }
00190     QT3_SUPPORT int insertItem(QMenuItem *item, int id=-1, int index=-1);
00191     QT3_SUPPORT int insertSeparator(int index=-1);
00192     inline QT3_SUPPORT void removeItem(int id) {
00193         if(QAction *act = findActionForId(id))
00194             removeAction(act); }
00195     inline QT3_SUPPORT void removeItemAt(int index) {
00196         if(QAction *act = actions().value(index))
00197             removeAction(act); }
00198 #ifndef QT_NO_SHORTCUT
00199     inline QT3_SUPPORT QKeySequence accel(int id) const {
00200         if(QAction *act = findActionForId(id))
00201             return act->shortcut();
00202         return QKeySequence(); }
00203     inline QT3_SUPPORT void setAccel(const QKeySequence& key, int id) {
00204         if(QAction *act = findActionForId(id))
00205             act->setShortcut(key);
00206     }
00207 #endif
00208     inline QT3_SUPPORT QIcon iconSet(int id) const {
00209         if(QAction *act = findActionForId(id))
00210             return act->icon();
00211         return QIcon(); }
00212     inline QT3_SUPPORT QString text(int id) const {
00213         if(QAction *act = findActionForId(id))
00214             return act->text();
00215         return QString(); }
00216     inline QT3_SUPPORT QPixmap pixmap(int id) const {
00217         if(QAction *act = findActionForId(id))
00218             return act->icon().pixmap(QSize(22, 22));
00219         return QPixmap(); }
00220     inline QT3_SUPPORT void setWhatsThis(int id, const QString &w) {
00221         if(QAction *act = findActionForId(id))
00222             act->setWhatsThis(w); }
00223     inline QT3_SUPPORT QString whatsThis(int id) const {
00224         if(QAction *act = findActionForId(id))
00225             return act->whatsThis();
00226         return QString(); }
00227 
00228     inline QT3_SUPPORT void changeItem(int id, const QString &text) {
00229         if(QAction *act = findActionForId(id))
00230             act->setText(text); }
00231     inline QT3_SUPPORT void changeItem(int id, const QPixmap &pixmap) {
00232         if(QAction *act = findActionForId(id))
00233             act->setIcon(QIcon(pixmap)); }
00234     inline QT3_SUPPORT void changeItem(int id, const QIcon &icon, const QString &text) {
00235         if(QAction *act = findActionForId(id)) {
00236             act->setIcon(icon);
00237             act->setText(text);
00238         }
00239     }
00240     inline QT3_SUPPORT void setActiveItem(int id) {
00241         setActiveAction(findActionForId(id));
00242     }
00243     inline QT3_SUPPORT bool isItemActive(int id) const {
00244         return findActionForId(id) == activeAction();
00245     }
00246     inline QT3_SUPPORT bool isItemEnabled(int id) const {
00247         if(QAction *act = findActionForId(id))
00248             return act->isEnabled();
00249         return false; }
00250     inline QT3_SUPPORT void setItemEnabled(int id, bool enable) {
00251         if(QAction *act = findActionForId(id))
00252             act->setEnabled(enable);
00253     }
00254     inline QT3_SUPPORT bool isItemChecked(int id) const {
00255         if(QAction *act = findActionForId(id))
00256             return act->isChecked();
00257         return false;
00258     }
00259     inline QT3_SUPPORT void setItemChecked(int id, bool check) {
00260         if(QAction *act = findActionForId(id)) {
00261             act->setCheckable(true);
00262             act->setChecked(check);
00263         }
00264     }
00265     inline QT3_SUPPORT bool isItemVisible(int id) const {
00266         if(QAction *act = findActionForId(id))
00267             return act->isVisible();
00268         return false;
00269     }
00270     inline QT3_SUPPORT void setItemVisible(int id, bool visible) {
00271         if(QAction *act = findActionForId(id))
00272             act->setVisible(visible);
00273     }
00274     inline QT3_SUPPORT QRect itemGeometry(int index) {
00275         if(QAction *act = actions().value(index))
00276             return actionGeometry(act);
00277         return QRect();
00278     }
00279     inline QT3_SUPPORT QFont itemFont(int id) const {
00280         if(QAction *act = findActionForId(id))
00281             return act->font();
00282         return QFont();
00283     }
00284     inline QT3_SUPPORT void setItemFont(int id, const QFont &font) {
00285         if(QAction *act = findActionForId(id))
00286             act->setFont(font);
00287     }
00288     inline QT3_SUPPORT int indexOf(int id) const {
00289         return actions().indexOf(findActionForId(id));
00290     }
00291     inline QT3_SUPPORT int idAt(int index) const {
00292         return findIdForAction(actions().value(index));
00293     }
00294     QT3_SUPPORT void setId (int index, int id);
00295     inline QT3_SUPPORT void activateItemAt(int index) {
00296         if(QAction *ret = actions().value(index))
00297             ret->activate(QAction::Trigger);
00298     }
00299     inline QT3_SUPPORT bool connectItem(int id, const QObject *receiver, const char* member) {
00300         if(QAction *act = findActionForId(id)) {
00301             QObject::connect(act, SIGNAL(activated(int)), receiver, member);
00302             return true;
00303         }
00304         return false;
00305     }
00306     inline QT3_SUPPORT bool disconnectItem(int id,const QObject *receiver, const char* member) {
00307         if(QAction *act = findActionForId(id)) {
00308             QObject::disconnect(act, SIGNAL(triggered()), receiver, member);
00309             return true;
00310         }
00311         return false;
00312     }
00313     inline QT3_SUPPORT QMenuItem *findItem(int id) const {
00314         return reinterpret_cast<QMenuItem*>(findActionForId(id));
00315     }
00316 
00317     inline QT3_SUPPORT void setCheckable(bool){}
00318     inline QT3_SUPPORT bool isCheckable() const {return true;}
00319 
00320     QT3_SUPPORT QMenuItem *findPopup( QMenu *popup, int *index );
00321 
00322     QT3_SUPPORT bool setItemParameter(int id, int param);
00323     QT3_SUPPORT int itemParameter(int id) const;
00324 
00325     //frame
00326     QT3_SUPPORT int frameWidth() const;
00327 
00328     //popupmenu
00329     inline QT3_SUPPORT void popup(const QPoint & pos, int indexAtPoint) { popup(pos, actions().value(indexAtPoint)); }
00330     inline QT3_SUPPORT int insertTearOffHandle(int = 0, int = 0) {
00331         setTearOffEnabled(true);
00332         return -1;
00333     }
00334 
00335 protected:
00336     inline QT3_SUPPORT int itemAtPos(const QPoint &p, bool ignoreSeparator = true) {
00337         QAction *ret = actionAt(p);
00338         if(ignoreSeparator && ret && ret->isSeparator())
00339             return -1;
00340         return findIdForAction(ret);
00341     }
00342     inline QT3_SUPPORT int columns() const { return columnCount(); }
00343     inline QT3_SUPPORT int itemHeight(int index) {
00344         return actionGeometry(actions().value(index)).height();
00345     }
00346     inline QT3_SUPPORT int itemHeight(QMenuItem *mi) {
00347         return actionGeometry(reinterpret_cast<QAction *>(mi)).height();
00348     }
00349 
00350 Q_SIGNALS:
00351     QT_MOC_COMPAT void activated(int itemId);
00352     QT_MOC_COMPAT void highlighted(int itemId);
00353 
00354 private:
00355     int insertAny(const QIcon *icon, const QString *text, const QObject *receiver, const char *member,
00356                   const QKeySequence *shorcut, const QMenu *popup, int id, int index);
00357     QAction *findActionForId(int id) const;
00358     int findIdForAction(QAction*) const;
00359 #endif
00360 
00361 private:
00362     Q_DISABLE_COPY(QMenu)
00363 
00364     friend class QMenuBar;
00365     friend class QMenuBarPrivate;
00366     friend class QTornOffMenu;
00367     friend class Q3PopupMenu;
00368     friend class QComboBox;
00369 
00370 #ifdef Q_WS_MAC
00371     friend void qt_mac_trayicon_activate_action(QMenu *, QAction *action);
00372     friend bool qt_mac_watchingAboutToShow(QMenu *);
00373     friend OSStatus qt_mac_menu_event(EventHandlerCallRef, EventRef, void *);
00374     friend bool qt_mac_activate_action(MenuRef, uint, QAction::ActionEvent, bool);
00375 #endif
00376 };
00377 
00378 #endif // QT_NO_MENU
00379 
00380 QT_END_HEADER
00381 
00382 #endif // QMENU_H

Generated on Thu Mar 15 11:57:03 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1