?? qmenudata.cpp
字號:
/****************************************************************************** $Id: qt/src/widgets/qmenudata.cpp 2.3.12 edited 2005-10-27 $**** Implementation of QMenuData class**** Created : 941128**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of the widgets module of the Qt GUI Toolkit.**** This file may be distributed under the terms of the Q Public License** as defined by Trolltech AS of Norway and appearing in the file** LICENSE.QPL included in the packaging of this file.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition** licenses may use this file in accordance with the Qt Commercial License** Agreement provided with the Software.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for** information about Qt Commercial License Agreements.** See http://www.trolltech.com/qpl/ for QPL licensing information.** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#define INCLUDE_MENUITEM_DEF#include "qmenudata.h"#ifndef QT_NO_MENUDATA#include "qpopupmenu.h"#include "qmenubar.h"#include "qapplication.h"#include "qguardedptr.h"class QMenuItemData {public: QCustomMenuItem *custom_item; // custom menu item};class QMenuDataData { // attention: also defined in qmenubar.cpp and qpopupmenu.cpppublic: QMenuDataData(); QGuardedPtr<QWidget> aWidget; int aInt;};QMenuDataData::QMenuDataData() : aInt(-1){}// NOT REVISED/*! \class QMenuData qmenudata.h \brief The QMenuData class is a base class for QMenuBar and QPopupMenu. \ingroup misc QMenuData has an internal list of menu items. A menu item is a text, pixmap or a separator, and may also have a popup menu (separators have no popup menus). The menu item sends out an activated() signal when it is selected, and a highlighted() signal when it receives the user input focus. Menu items can be accessed through identifiers. \sa QAccel*//***************************************************************************** QMenuItem member functions *****************************************************************************/QMenuItem::QMenuItem(){ ident = -1; is_separator = FALSE; is_checked = FALSE; is_enabled = TRUE; is_dirty = TRUE; iconset_data = 0; pixmap_data = 0; popup_menu = 0; widget_item = 0; accel_key = 0; signal_data = 0; d = 0;}QMenuItem::~QMenuItem(){ delete iconset_data; delete pixmap_data; delete signal_data; delete widget_item; if ( d ) delete d->custom_item; delete d;}/***************************************************************************** QMenuData member functions *****************************************************************************/QMenuItemData* QMenuItem::extra(){ if ( !d ) d = new QMenuItemData; return d;}QCustomMenuItem *QMenuItem::custom() const{ if ( !d ) return 0; return d->custom_item;}static int get_seq_id(){ static int seq_no = -2; return seq_no--;}/*! Constructs an empty list.*/QMenuData::QMenuData(){ actItem = -1; // no active menu item mitems = new QMenuItemList; // create list of menu items CHECK_PTR( mitems ); mitems->setAutoDelete( TRUE ); parentMenu = 0; // assume top level isPopupMenu = FALSE; isMenuBar = FALSE; mouseBtDn = FALSE; badSize = TRUE; avoid_circularity = 0; actItemDown = FALSE; d = new QMenuDataData;}/*! Removes all menu items and disconnects any signals that have been connected.*/QMenuData::~QMenuData(){ register QMenuItem *mi = mitems->first(); while ( mi ) { if ( mi->popup_menu ) // reset parent pointer for all mi->popup_menu->parentMenu = 0; // child menus mi = mitems->next(); } delete mitems; // delete menu item list delete d;}/*! Virtual function; notifies subclasses about an item that has been changed.*/void QMenuData::updateItem( int ) // reimplemented in subclass{}/*! Virtual function; notifies subclasses that one or more items have been inserted or removed.*/void QMenuData::menuContentsChanged() // reimplemented in subclass{}/*! Virtual function; notifies subclasses that one or more items have changed state (enabled/disabled or checked/unchecked).*/void QMenuData::menuStateChanged() // reimplemented in subclass{}/*! Virtual function; notifies subclasses that a popup menu item has been inserted.*/void QMenuData::menuInsPopup( QPopupMenu * ) // reimplemented in subclass{}/*! Virtual function; notifies subclasses that a popup menu item has been removed.*/void QMenuData::menuDelPopup( QPopupMenu * ) // reimplemented in subclass{}/*! Returns the number of items in the menu.*/uint QMenuData::count() const{ return mitems->count();} /*! Internal function that insert a menu item. Called by all insert() functions.*/int QMenuData::insertAny( const QString *text, const QPixmap *pixmap, QPopupMenu *popup, const QIconSet* iconset, int id, int index, QWidget* widget, QCustomMenuItem* custom ){ if ( index < 0 || index > (int) mitems->count() ) // append index = mitems->count(); if ( id < 0 ) // -2, -3 etc. id = get_seq_id(); register QMenuItem *mi = new QMenuItem; CHECK_PTR( mi ); mi->ident = id; if ( widget != 0 ) { mi->widget_item = widget; mi->is_separator = !widget->isFocusEnabled(); } else if ( custom != 0 ) { mi->extra()->custom_item = custom; mi->is_separator = custom->isSeparator(); } else if ( text == 0 && pixmap == 0 && popup == 0 ) { mi->is_separator = TRUE; // separator } else { mi->text_data = text?*text:QString::null; mi->accel_key = Qt::Key_unknown; if ( pixmap ) mi->pixmap_data = new QPixmap( *pixmap ); if ( (mi->popup_menu = popup) ) menuInsPopup( popup ); if ( iconset ) mi->iconset_data = new QIconSet( *iconset ); } mitems->insert( index, mi ); menuContentsChanged(); // menu data changed return mi->ident;}/*! Internal function that finds the menu item where \a popup is located, storing its index at \a index if \a index is not NULL.*/QMenuItem *QMenuData::findPopup( QPopupMenu *popup, int *index ){ int i = 0; QMenuItem *mi = mitems->first(); while ( mi ) { if ( mi->popup_menu == popup ) // found popup break; i++; mi = mitems->next(); } if ( index && mi ) *index = i; return mi;}void QMenuData::removePopup( QPopupMenu *popup ){ int index = 0; QMenuItem *mi = findPopup( popup, &index ); if ( mi ) removeItemAt( index );}/*! does nothing, but that virtual ### remove 3.0*/void QMenuData::setAllDirty( bool ){}/*! The family of insertItem() functions inserts menu items into a popup menu or a menu bar. A menu item is usually either a text string or a a pixmap, both with an optional icon or keyboard accelerator. As special cases it is also possible to insert custom items (see QCustomMenuItem) or even widgets into popup menus. Some insertItem() members take a popup menu as additional argument. Use these to insert submenus to existing menus or pulldown menus to a menu bar. The amount of insert functions may look confusing, but is actually quite handy to use. This default version inserts a menu item with a text, an accelerator key, an id and an optional index and connects it to an object/slot. Example: \code QMenuBar *mainMenu = new QMenuBar; QPopupMenu *fileMenu = new QPopupMenu; fileMenu->insertItem( "New", myView, SLOT(newFile()), CTRL+Key_N ); fileMenu->insertItem( "Open", myView, SLOT(open()), CTRL+Key_O ); mainMenu->insertItem( "File", fileMenu ); \endcode Not all insert functions take an object/slot parameter or an accelerator key. Use connectItem() and setAccel() on these items. If you will need to translate accelerators, use QAccel::stringToKey() to calculate the accelerator key: \code fileMenu->insertItem( tr("Open"), myView, SLOT(open()), QAccel::stringToKey( tr("Ctrl+O") ) ); \endcode In the example above, pressing CTRL+N or selecting "open" from the menu activates the myView->open() function. Some insert functions take a QIconSet parameter to specify the little menu item icon. Note that you can always pass a QPixmap object instead. The menu item is assigned the identifier \a id or an automatically generated identifier if \a id is < 0. The generated identifiers (negative integers) are guaranteed to be unique within the entire application. The \a index specifies the position in the menu. The menu item is appended at the end of the list if \a index is negative. Note that keyboard accelerators in Qt are not application global, but bound to a certain toplevel window. Accelerators in QPopupMenu items therefore only work for menus that are associated with a certain window. This is true for popup menus that live in a menu bar, for instance. In that case, the accelerator will be installed on the menu bar itself. It also works for stand-alone popup menus that have a toplevel widget in their parentWidget()- chain. The menu will then install its accelerator object on that toplevel widget. For all other cases, use an independent QAccel object. \warning Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. <tt>(QObject*)0</tt>. \sa removeItem(), changeItem(), setAccel(), connectItem(), QAccel, qnamespace.h*/int QMenuData::insertItem( const QString &text, const QObject *receiver, const char* member, int accel, int id, int index ){ int actualID = insertAny( &text, 0, 0, 0, id, index ); connectItem( actualID, receiver, member ); if ( accel ) setAccel( accel, actualID ); return actualID;}/*!\overload Inserts a menu item with an icon, a text, an accelerator key, an id and an optional index and connects it to an object/slot. The icon will be displayed to the left of the text in the item. \sa removeItem(), changeItem(), setAccel(), connectItem(), QAccel, qnamespace.h*/int QMenuData::insertItem( const QIconSet& icon, const QString &text, const QObject *receiver, const char* member, int accel, int id, int index ){ int actualID = insertAny( &text, 0, 0, &icon, id, index ); connectItem( actualID, receiver, member ); if ( accel ) setAccel( accel, actualID ); return actualID;}/*!\overload Inserts a menu item with a pixmap, an accelerator key, an id and an optional index and connects it to an object/slot. To look best when being highlighted as menu item, the pixmap should provide a mask, see QPixmap::mask(). Returns the menu item identifier. \sa removeItem(), changeItem(), setAccel(), connectItem()*/int QMenuData::insertItem( const QPixmap &pixmap, const QObject *receiver, const char* member, int accel, int id, int index ){ int actualID = insertAny( 0, &pixmap, 0, 0, id, index ); connectItem( actualID, receiver, member ); if ( accel ) setAccel( accel, actualID ); return actualID;}/*!\overload Inserts a menu item with an icon, a pixmap, an accelerator key, an id and an optional index and connects it to an object/slot. The icon will be displayed to the left of the pixmap in the item. To look best when being highlighted as menu item, the pixmap should provide a mask, see QPixmap::mask(). Returns the menu item identifier. \sa removeItem(), changeItem(), setAccel(), connectItem(), QAccel, qnamespace.h*/int QMenuData::insertItem( const QIconSet& icon, const QPixmap &pixmap, const QObject *receiver, const char* member, int accel, int id, int index ){ int actualID = insertAny( 0, &pixmap, 0, &icon, id, index ); connectItem( actualID, receiver, member );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -