?? qaction.cpp
字號:
/****************************************************************************** $Id: qt/src/widgets/qaction.cpp 2.3.12 edited 2005-10-27 $**** Implementation of QAction class**** Created : 000000**** Copyright (C) 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.************************************************************************/#include "qaction.h"#ifndef QT_NO_ACTION#include <qtoolbar.h>#include <qlist.h>#include <qpopupmenu.h>#include <qaccel.h>#include <qtoolbutton.h>#include <qtooltip.h>#include <qwhatsthis.h>#include <qstatusbar.h>#include <qobjectlist.h>/*! \class QAction qaction.h \brief The QAction class abstracts a user interface action that can appear both in menus and tool bars. There are two basic kind of user interface actions, command actions and options. QAction usually models a command action, for example "open file". When the actual action shall be performed, it emits the activated() signal. Options, for example the drawing tools in a paint program, are represented by toggle actions (see setToggleAction() ). A toggle action emits a toggled() signal whenever it changes state. Several toggle actions can be combined in a QActionGroup. To provide an action to the user, use addTo() to add it to either a menu or a tool bar, for example: \code QPopupMenu* popup = new QPopupMenu; QAction* myAction= new QAction; myAction->setText( "MyAction" ); myAction->addTo( popup ); \endcode You can add an action to an arbitrary number of menus and toolbars and remove it again with removeFrom(). Changing an action's properties, for example using setEnabled(), setOn() or setText(), immediately shows up in all representations. Other properties that define the way an action is presented to the user are iconSet(), menuText(), toolTip(), statusTip() and whatsThis(). An action may also be triggered by an accelerator key declared with setAccel(). Since accelerators are window specific, the application window has to be an ancestor of the action. Generally, it is therefore a good idea to always create actions as direct children of the main window.*/class QActionPrivate{public: QActionPrivate(); ~QActionPrivate(); QIconSet *iconset; QString text; QString menutext; QString tooltip; QString statustip; QString whatsthis; int key;#ifndef QT_NO_ACCEL QAccel* accel; int accelid;#endif uint enabled : 1; uint toggleaction :1; uint on : 1;#ifndef QT_NO_TOOLTIP QToolTipGroup* tipGroup;#endif struct MenuItem { MenuItem():popup(0),id(0){} QPopupMenu* popup; int id; }; QList<MenuItem> menuitems; QList<QToolButton> toolbuttons; enum Update { Everything, Icons, State }; // Everything means everything but icons and state void update( Update upd = Everything ); QString menuText() const; QString toolTip() const; QString statusTip() const;};QActionPrivate::QActionPrivate(){ iconset = 0;#ifndef QT_NO_ACCEL accel = 0; accelid = 0;#endif key = 0; enabled = 1; toggleaction = 0; on = 0; menuitems.setAutoDelete( TRUE );#ifndef QT_NO_TOOLTIP tipGroup = new QToolTipGroup( 0 );#endif}QActionPrivate::~QActionPrivate(){ QListIterator<QToolButton> ittb( toolbuttons ); QToolButton *tb; while ( ( tb = ittb.current() ) ) { ++ittb; QWidget* parent = tb->parentWidget(); delete tb;#ifndef QT_NO_TOOLBAR if ( parent->inherits( "QToolBar" ) ) { QToolBar* toolbar = (QToolBar*) parent; QObjectList* lst = toolbar->queryList( "QToolButton" ); if ( lst->isEmpty() ) delete toolbar; delete lst; }#endif } QListIterator<QActionPrivate::MenuItem> itmi( menuitems); QActionPrivate::MenuItem* mi; while ( ( mi = itmi.current() ) ) { ++itmi; QPopupMenu* menu = mi->popup; if ( menu->findItem( mi->id ) ) menu->removeItem( mi->id ); if ( !menu->count() ) { delete menu; } }#ifndef QT_NO_ACCEL delete accel;#endif delete iconset;#ifndef QT_NO_TOOLTIP delete tipGroup;#endif}void QActionPrivate::update( Update upd ){ for ( QListIterator<MenuItem> it( menuitems); it.current(); ++it ) { MenuItem* mi = it.current(); QString t = menuText();#ifndef QT_NO_ACCEL if ( key ) t += '\t' + QAccel::keyToString( key );#endif switch ( upd ) { case State: mi->popup->setItemEnabled( mi->id, enabled ); if ( toggleaction ) mi->popup->setItemChecked( mi->id, on ); break; case Icons: if ( iconset ) mi->popup->changeItem( mi->id, *iconset, t ); break; default: mi->popup->changeItem( mi->id, t ); if ( !whatsthis.isEmpty() ) mi->popup->setWhatsThis( mi->id, whatsthis ); if ( toggleaction ) { mi->popup->setCheckable( TRUE ); mi->popup->setItemChecked( mi->id, on ); } } } for ( QListIterator<QToolButton> it2( toolbuttons); it2.current(); ++it2 ) { QToolButton* btn = it2.current(); switch ( upd ) { case State: btn->setEnabled( enabled ); if ( toggleaction ) btn->setOn( on ); break; case Icons: if ( iconset ) btn->setIconSet( *iconset ); break; default: btn->setToggleButton( toggleaction ); if ( !text.isEmpty() ) btn->setTextLabel( text, FALSE );#ifndef QT_NO_TOOLTIP QToolTip::remove( btn ); QToolTip::add( btn, toolTip(), tipGroup, statusTip() );#endif#ifndef QT_NO_WHATSTHIS QWhatsThis::remove( btn ); if ( !whatsthis.isEmpty() ) QWhatsThis::add( btn, whatsthis );#endif } }}QString QActionPrivate::menuText() const{ if ( menutext.isNull() ) return text; return menutext;}QString QActionPrivate::toolTip() const{ if ( tooltip.isNull() ) {#ifndef QT_NO_ACCEL if ( accel ) return text + " (" + QAccel::keyToString( accel->key( accelid )) + ")";#endif return text; } return tooltip;}QString QActionPrivate::statusTip() const{ if ( statustip.isNull() ) return toolTip(); return statustip;}/*! Constructs an action with parent \a parent and name \a name. If \a toggle is TRUE, the action becomes a toggle action. If the parent is a QActionGroup, the action automatically becomes a member of it. Note: for accelerators to work, the parent or one of its ancestors has to be the application window. */QAction::QAction( QObject* parent, const char* name, bool toggle ) : QObject( parent, name ){ init(); d->toggleaction = toggle;}/*!\overload Constructs an action with text \a text, icon \a icon, menu text \a menuText, a keyboard accelerator \a accel, a \a parent and name \a name. \a text will also show up in tooltips, unless you call setToolTip() with a different tip later. If the parent is a QActionGroup, the action automatically becomes a member of it. Note: for accelerators to work, the parent or one of its ancestors has to be the application window. */QAction::QAction( const QString& text, const QIconSet& icon, const QString& menuText, int accel, QObject* parent, const char* name, bool toggle ) : QObject( parent, name ){ init(); d->toggleaction = toggle; if ( !icon.pixmap().isNull() ) setIconSet( icon ); d->text = text; d->menutext = menuText; setAccel( accel );}/*!\overload Constructs an action with text \a text, menu text \a menuText, a keyboard accelerator \a accel, a \a parent and name \a name. \a text will also show up in tooltips, unless you call setToolTip() with a different tip later. If \a toggle is TRUE, the action becomes a toggle action. If the parent is a QActionGroup, the action automatically becomes a member of it. Note: for accelerators to work, the parent or one of its ancestors has to be the application window. */QAction::QAction( const QString& text, const QString& menuText, int accel, QObject* parent, const char* name, bool toggle ) : QObject( parent, name ){ init(); d->toggleaction = toggle; d->text = text; d->menutext = menuText; setAccel( accel );}void QAction::init(){ d = new QActionPrivate; if ( parent() && parent()->inherits("QActionGroup") ) { ((QActionGroup*) parent())->insert( this ); // insert into action group }}/*! Destroys the object and frees any allocated resources. */QAction::~QAction(){ delete d;}/*! Sets the icon set to \a icon. \sa iconSet(); */void QAction::setIconSet( const QIconSet& icon ){ register QIconSet *i = d->iconset; d->iconset = new QIconSet( icon ); delete i; d->update( QActionPrivate::Icons );}/*! Returns the icon set. \sa setIconSet(); */QIconSet QAction::iconSet() const{ if ( d->iconset ) return *d->iconset; return QIconSet();}/*! Sets the text to \a text. \sa setMenuText(), text() */void QAction::setText( const QString& text ){ d->text = text; d->update();}/*! Returns the current text. \sa setText(), menuText() */QString QAction::text() const{ return d->text;}/*! Sets a special text \a text for menu items. Use this to specify ellipses or keyboard shortcuts that should not show up in tooltips or as button text. \sa setText(), menuText() */void QAction::setMenuText( const QString& text ){ d->menutext = text; d->update();}/*! Returns the text used for menu items. If no menu text has been defined yet, this is the same as text(). \sa setMenuText(), text() */QString QAction::menuText() const{ return d->menuText();}/*! Sets the tool tip to \a tip. \sa toolTip() */void QAction::setToolTip( const QString& tip ){ d->tooltip = tip; d->update();}/*! Returns the current tool tip. If no tool tip has been defined yet, it returns text and a hotkey hint. \sa setToolTip(), text()*/QString QAction::toolTip() const{ return d->toolTip();}/*! Sets the status tip to \a tip. The tip will be displayed on all status bars the topmost parent of the action provides. \sa statusTip()*/void QAction::setStatusTip( const QString& tip ){ d->statustip = tip; d->update();}/*! Returns the current status tip. If not status tip has been defined yet, this is the same as toolTip() \sa setStatusTip(), toolTip()*/QString QAction::statusTip() const{ return d->statusTip();}/*! Sets What's This help to \a whatsThis. \sa whatsThis() */void QAction::setWhatsThis( const QString& whatsThis ){ d->whatsthis = whatsThis;#ifndef QT_NO_ACCEL if ( !d->whatsthis.isEmpty() && d->accel ) d->accel->setWhatsThis( d->accelid, d->whatsthis );#endif d->update();}/*! Returns the What's This help for this action. \sa setWhatsThis() */QString QAction::whatsThis() const{ return d->whatsthis;}/*! Sets the action's accelerator to \a key. Note: For accelerators to work, the parent or one of its ancestors
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -