kdeui Library API Documentation

KAction Class Reference

The KAction class (and derived and super classes) provides a way to easily encapsulate a "real" user-selected action or event in your program. Class to encapsulate user-driven action or event. More...

#include <kaction.h>

Inheritance diagram for KAction:

QObject KActionMenu KSelectAction KToggleAction KToolBarPopupAction KWidgetAction KListAction KRadioAction KToggleToolBarAction KRecentFilesAction List of all members.

Public Slots

virtual void setText (const QString &text)
 Sets the text associated with this action.

virtual bool setShortcut (const KShortcut &)
 Sets the keyboard shortcut associated with this action.

virtual void setWhatsThis (const QString &text)
 Sets the What's this text for the action.

virtual void setToolTip (const QString &)
 Sets the tooltip text for the action.

virtual void setIconSet (const QIconSet &iconSet)
 Sets the QIconSet from which the icons used to display this action will be chosen.

virtual void setEnabled (bool enable)
 Enables or disables this action.

virtual void setShortcutConfigurable (bool)
 Indicate whether the user may configure the action's shortcut.

virtual void activate ()
 Emulate user's interaction programmatically, by activating the action.


Public Methods

 KAction (const QString &text, const KShortcut &cut, const QObject *receiver, const char *slot, KActionCollection *parent, const char *name)
 Constructs an action with text, potential keyboard shortcut, and a SLOT to call when this action is invoked by the user.

 KAction (const QString &text, const QIconSet &pix, const KShortcut &cut, const QObject *receiver, const char *slot, KActionCollection *parent, const char *name)
 Constructs an action with text, icon, potential keyboard shortcut, and a SLOT to call when this action is invoked by the user.

 KAction (const QString &text, const QString &pix, const KShortcut &cut, const QObject *receiver, const char *slot, KActionCollection *parent, const char *name)
 Constructs an action with text, icon, potential keyboard shortcut, and a SLOT to call when this action is invoked by the user.

 KAction (const KGuiItem &item, const KShortcut &cut, const QObject *receiver, const char *slot, KActionCollection *parent, const char *name)
 The same as the above constructor, but with a KGuiItem providing the text and icon.

 KAction (const QString &text, const KShortcut &cut=KShortcut(), QObject *parent=0, const char *name=0)
 KAction (const QString &text, const KShortcut &cut, const QObject *receiver, const char *slot, QObject *parent, const char *name=0)
 KAction (const QString &text, const QIconSet &pix, const KShortcut &cut=KShortcut(), QObject *parent=0, const char *name=0)
 KAction (const QString &text, const QString &pix, const KShortcut &cut=KShortcut(), QObject *parent=0, const char *name=0)
 KAction (const QString &text, const QIconSet &pix, const KShortcut &cut, const QObject *receiver, const char *slot, QObject *parent, const char *name=0)
 KAction (const QString &text, const QString &pix, const KShortcut &cut, const QObject *receiver, const char *slot, QObject *parent, const char *name=0)
 KAction (QObject *parent=0, const char *name=0)
virtual ~KAction ()
 Standard destructor.

virtual int plug (QWidget *w, int index=-1)
 "Plug" or insert this action into a given widget.

virtual void plugAccel (KAccel *accel, bool configurable=true)
virtual void unplug (QWidget *w)
 "Unplug" or remove this action from a given widget.

virtual void unplugAccel ()
virtual bool isPlugged () const
 returns whether the action is plugged into any container widget or not.

bool isPlugged (const QWidget *container) const
 returns whether the action is plugged into the given container

virtual bool isPlugged (const QWidget *container, int id) const
 returns whether the action is plugged into the given container with the given, container specific, id (often menu or toolbar id ) .

virtual bool isPlugged (const QWidget *container, const QWidget *_representative) const
 returns whether the action is plugged into the given container with the given, container specific, representative container widget item.

uint kaccelCount () const
 
Since:
3.1


virtual QString text () const
 Get the text associated with this action.

virtual const KShortcut & shortcut () const
 Get the keyboard shortcut associated with this action.

virtual const KShortcut & shortcutDefault () const
 Get the default shortcut for this action.

virtual bool isEnabled () const
 Returns true if this action is enabled.

virtual bool isShortcutConfigurable () const
 Returns true if this action's shortcut is configurable.

virtual QString whatsThis () const
 Get the What's this text for the action.

virtual QString toolTip () const
 Get the tooltip text for the action.

virtual QIconSet iconSet (KIcon::Group group, int size=0) const
 Get the QIconSet from which the icons used to display this action will be chosen.

int accel () const
void setAccel (int key)
void setStatusText (const QString &text)
int menuId (int i)

Detailed Description

The KAction class (and derived and super classes) provides a way to easily encapsulate a "real" user-selected action or event in your program. Class to encapsulate user-driven action or event.

For instance, a user may want to paste the contents of the clipboard or scroll down a document or quit the application. These are all actions -- events that the user causes to happen. The KAction class allows the developer to deal with these actions in an easy and intuitive manner.

Specifically, the KAction class encapsulated the various attributes to an event/action. For instance, an action might have an icon that goes along with it (a clipboard for a "paste" action or scissors for a "cut" action). The action might have some text to describe the action. It will certainly have a method or function that actually executes the action! All these attributes are contained within the KAction object.

The advantage of dealing with Actions is that you can manipulate the Action without regard to the GUI representation of it. For instance, in the "normal" way of dealing with actions like "cut", you would manually insert a item for Cut into a menu and a button into a toolbar. If you want to disable the cut action for a moment (maybe nothing is selected), you woud have to hunt down the pointer to the menu item and the toolbar button and disable both individually. Setting the menu item and toolbar item up uses very similar code - but has to be done twice!

With the Action concept, you simply "plug" the Action into whatever GUI element you want. The KAction class will then take care of correctly defining the menu item (with icons, accelerators, text, etc) or toolbar button.. or whatever. From then on, if you manipulate the Action at all, the effect will propogate through all GUI representations of it. Back to the "cut" example: if you want to disable the Cut Action, you would simply do 'cutAction->setEnabled(false)' and the menuitem and button would instantly be disabled!

This is the biggest advantage to the Action concept -- there is a one-to-one relationship between the "real" action and all GUI representations of it.

KAction emits the activated() signal if the user activated the corresponding GUI element ( menu item, toolbar button, etc. )

If you are in the situation of wanting to map the activated() signal of multiple action objects to one slot, with a special argument bound to each action, then you might consider using QSignalMapper . A tiny example:

 QSignalMapper *desktopNumberMapper = new QSignalMapper( this );
 connect( desktopNumberMapper, SIGNAL( mapped( int ) ),
          this, SLOT( moveWindowToDesktop( int ) ) );

 for ( uint i = 0; i < numberOfDesktops; ++i ) {
     KAction *desktopAction = new KAction( i18n( "Move Window To Desktop i" ).arg( i ), ... );
     connect( desktopAction, SIGNAL( activated() ), desktopNumberMapper, SLOT( map() ) );
     desktopNumberMapper->setMapping( desktopAction, i );
 }
 

General Usage:

The steps to using actions are roughly as follows

Detailed Example:

Here is an example of enabling a "New [document]" action

 KAction *newAct = new KAction(i18n("&New"), "filenew",
                               KStdAccel::shortcut(KStdAccel::New),
                               this, SLOT(fileNew()),
                               actionCollection(), "new");
 
This line creates our action. It says that wherever this action is displayed, it will use "&New" as the text, the standard icon, and the standard shortcut. It further says that whenever this action is invoked, it will use the fileNew() slot to execute it.

 QPopupMenu *file = new QPopupMenu;
 newAct->plug(file);
 
That just inserted the action into the File menu. The point is, it's not important in which menu it is: all manipulation of the item is done through the newAct object.

 newAct->plug(toolBar());
 
And this inserted the Action into the main toolbar as a button.

That's it!

If you want to disable that action sometime later, you can do so with

 newAct->setEnabled(false)
 
and both the menuitem in File and the toolbar button will instantly be disabled.

Note: if you are using a "standard" action like "new", "paste", "quit", or any other action described in the KDE UI Standards, please use the methods in the KStdAction class rather than defining your own.

Usage Within the XML Framework:

If you are using KAction within the context of the XML menu and toolbar building framework, then there are a few tiny changes. The first is that you must insert your new action into an action collection. The action collection (a KActionCollection) is, logically enough, a central collection of all of the actions defined in your application. The XML UI framework code in KXMLGUI classes needs access to this collection in order to build up the GUI (it's how the builder code knows which actions are valid and which aren't).

Also, if you use the XML builder framework, then you do not ever have to plug your actions into containers manually. The framework does that for you.

See also:
KStdAction

Definition at line 191 of file kaction.h.


Constructor & Destructor Documentation

KAction::KAction const QString   text,
const KShortcut &    cut,
const QObject   receiver,
const char *    slot,
KActionCollection   parent,
const char *    name
 

Constructs an action with text, potential keyboard shortcut, and a SLOT to call when this action is invoked by the user.

If you do not want or have a keyboard shortcut, set the cut param to 0.

This is the most common KAction used when you do not have a corresponding icon (note that it won't appear in the current version of the "Edit ToolBar" dialog, because an action needs an icon to be plugged in a toolbar...).

Parameters:
text  The text that will be displayed.
cut  The corresponding keyboard shortcut.
receiver  The SLOT's parent.
slot  The SLOT to invoke to execute this action.
parent  This action's parent.
name  An internal name for this action.

Definition at line 169 of file kaction.cpp.

KAction::KAction const QString   text,
const QIconSet   pix,
const KShortcut &    cut,
const QObject   receiver,
const char *    slot,
KActionCollection   parent,
const char *    name
 

Constructs an action with text, icon, potential keyboard shortcut, and a SLOT to call when this action is invoked by the user.

If you do not want or have a keyboard shortcut, set the cut param to 0.

This is the other common KAction used. Use it when you do have a corresponding icon.

Parameters:
text  The text that will be displayed.
pix  The icon to display.
cut  The corresponding keyboard shortcut.
receiver  The SLOT's parent.
slot  The SLOT to invoke to execute this action.
parent  This action's parent.
name  An internal name for this action.

Definition at line 186 of file kaction.cpp.

KAction::KAction const QString   text,
const QString   pix,
const KShortcut &    cut,
const QObject   receiver,
const char *    slot,
KActionCollection   parent,
const char *    name
 

Constructs an action with text, icon, potential keyboard shortcut, and a SLOT to call when this action is invoked by the user.

The icon is loaded on demand later based on where it is plugged in.

If you do not want or have a keyboard shortcut, set the cut param to 0.

This is the other common KAction used. Use it when you do have a corresponding icon.

Parameters:
text  The text that will be displayed.
pix  The icon to display.
cut  The corresponding keyboard shortcut (shortcut).
receiver  The SLOT's parent.
slot  The SLOT to invoke to execute this action.
parent  This action's parent.
name  An internal name for this action.

Definition at line 177 of file kaction.cpp.

KAction::KAction const KGuiItem &    item,
const KShortcut &    cut,
const QObject   receiver,
const char *    slot,
KActionCollection   parent,
const char *    name
 

The same as the above constructor, but with a KGuiItem providing the text and icon.

Parameters:
item  The KGuiItem with the label and (optional) icon.

Definition at line 195 of file kaction.cpp.

References setToolTip(), and setWhatsThis().

KAction::KAction const QString   text,
const KShortcut &    cut = KShortcut(),
QObject   parent = 0,
const char *    name = 0
 

Deprecated:

KAction::KAction const QString   text,
const KShortcut &    cut,
const QObject   receiver,
const char *    slot,
QObject   parent,
const char *    name = 0
 

Deprecated:

KAction::KAction const QString   text,
const QIconSet   pix,
const KShortcut &    cut = KShortcut(),
QObject   parent = 0,
const char *    name = 0
 

Deprecated:

KAction::KAction const QString   text,
const QString   pix,
const KShortcut &    cut = KShortcut(),
QObject   parent = 0,
const char *    name = 0
 

Deprecated:

KAction::KAction const QString   text,
const QIconSet   pix,
const KShortcut &    cut,
const QObject   receiver,
const char *    slot,
QObject   parent,
const char *    name = 0
 

Deprecated:

KAction::KAction const QString   text,
const QString   pix,
const KShortcut &    cut,
const QObject   receiver,
const char *    slot,
QObject   parent,
const char *    name = 0
 

Deprecated:

KAction::KAction QObject   parent = 0,
const char *    name = 0
 

Deprecated:

KAction::~KAction   [virtual]
 

Standard destructor.

Definition at line 268 of file kaction.cpp.

References QObject::name(), KActionCollection::take(), and unplugAccel().


Member Function Documentation

int KAction::plug QWidget   w,
int    index = -1
[virtual]
 

"Plug" or insert this action into a given widget.

This will typically be a menu or a toolbar. From this point on, you will never need to directly manipulate the item in the menu or toolbar. You do all enabling/disabling/manipulation directly with your KAction object.

Parameters:
w  The GUI element to display this action

Reimplemented in KToggleAction, KSelectAction, KActionMenu, KToolBarPopupAction, KToggleToolBarAction, and KWidgetAction.

Definition at line 657 of file kaction.cpp.

References QToolTip::add(), QWhatsThis::add(), QObject::connect(), KActionCollection::connectHighlight(), QObject::destroyed(), KToolBar::getButton(), iconSet(), KToolBar::insertButton(), QPopupMenu::insertItem(), KGlobal::instance(), KActionCollection::instance(), KNotifyClient::instance(), QString::isEmpty(), QObject::name(), QPopupMenu::setItemEnabled(), and QPopupMenu::setWhatsThis().

Referenced by KToolBarPopupAction::plug(), KToggleAction::plug(), and KSystemTray::showEvent().

void KAction::plugAccel KAccel   accel,
bool    configurable = true
[virtual]
 

Deprecated:
. Shouldn't be used. No substitute available.

"Plug" or insert this action into a given KAccel.

Parameters:
accel  The KAccel collection which holds this accel
configurable  If the shortcut is configurable via the KAccel configuration dialog (this is somehow deprecated since there is now a KAction key configuration dialog).

Definition at line 795 of file kaction.cpp.

References QObject::connect(), QObject::destroyed(), isEnabled(), QObject::name(), and unplugAccel().

void KAction::unplug QWidget   w [virtual]
 

"Unplug" or remove this action from a given widget.

This will typically be a menu or a toolbar. This is rarely used in "normal" application. Typically, it would be used if your application has several views or modes, each with a completely different menu structure. If you simply want to disable an action for a given period, use setEnabled() instead.

Parameters:
w  Remove the action from this GUI element.

Reimplemented in KWidgetAction.

Definition at line 767 of file kaction.cpp.

References KActionCollection::disconnectHighlight(), QMenuBar::removeItem(), KToolBar::removeItem(), and QPopupMenu::removeItem().

Referenced by KWidgetAction::unplug().

void KAction::unplugAccel   [virtual]
 

Deprecated:
. Complement method to plugAccel(). Disconnect this action from the KAccel.

Definition at line 823 of file kaction.cpp.

References QObject::name().

Referenced by plugAccel(), and ~KAction().

bool KAction::isPlugged   [virtual]
 

returns whether the action is plugged into any container widget or not.

Since:
3.1

Definition at line 314 of file kaction.cpp.

Referenced by KDCOPActionProxy::processAction(), and KWidgetAction::unplug().

bool KAction::isPlugged const QWidget   container const
 

returns whether the action is plugged into the given container

Definition at line 319 of file kaction.cpp.

bool KAction::isPlugged const QWidget   container,
int    id
const [virtual]
 

returns whether the action is plugged into the given container with the given, container specific, id (often menu or toolbar id ) .

Definition at line 324 of file kaction.cpp.

bool KAction::isPlugged const QWidget   container,
const QWidget   _representative
const [virtual]
 

returns whether the action is plugged into the given container with the given, container specific, representative container widget item.

Definition at line 330 of file kaction.cpp.

uint KAction::kaccelCount  
 

Since:
3.1

Definition at line 1083 of file kaction.cpp.

QString KAction::text   [virtual]
 

Get the text associated with this action.

Definition at line 930 of file kaction.cpp.

Referenced by KActionMenu::plug(), and KSelectAction::plug().

const KShortcut & KAction::shortcut   [virtual]
 

Get the keyboard shortcut associated with this action.

Definition at line 581 of file kaction.cpp.

const KShortcut & KAction::shortcutDefault   [virtual]
 

Get the default shortcut for this action.

Definition at line 586 of file kaction.cpp.

bool KAction::isEnabled   [virtual]
 

Returns true if this action is enabled.

Definition at line 625 of file kaction.cpp.

Referenced by KCommandHistory::addCommand(), KToolBarPopupAction::plug(), KActionMenu::plug(), KSelectAction::plug(), plugAccel(), and KCommandHistory::redo().

bool KAction::isShortcutConfigurable   [virtual]
 

Returns true if this action's shortcut is configurable.

Definition at line 630 of file kaction.cpp.

QString KAction::whatsThis   [virtual]
 

Get the What's this text for the action.

Definition at line 1039 of file kaction.cpp.

Referenced by KToolBarPopupAction::plug(), KActionMenu::plug(), and KSelectAction::plug().

QString KAction::toolTip   [virtual]
 

Get the tooltip text for the action.

Definition at line 652 of file kaction.cpp.

Referenced by KSelectAction::plug().

QIconSet KAction::iconSet KIcon::Group    group,
int    size = 0
const [virtual]
 

Get the QIconSet from which the icons used to display this action will be chosen.

Definition at line 1001 of file kaction.cpp.

References KIcon::Group.

Referenced by KActionMenu::plug(), KSelectAction::plug(), and plug().

void KAction::setText const QString   text [virtual, slot]
 

Sets the text associated with this action.

The text is used for menu and toolbar labels etc.

Definition at line 888 of file kaction.cpp.

References QObject::name().

Referenced by KCommandHistory::addCommand(), KCommandHistory::clear(), KSystemTray::mousePressEvent(), KCommandHistory::redo(), and KCommandHistory::undo().

bool KAction::setShortcut const KShortcut &    [virtual, slot]
 

Sets the keyboard shortcut associated with this action.

Definition at line 451 of file kaction.cpp.

References QObject::name().

void KAction::setWhatsThis const QString   text [virtual, slot]
 

Sets the What's this text for the action.

This text will be displayed when a widget that has been created by plugging this action into a container is clicked on in What's this mode.

The What's this text can include QML markup as well as raw text.

Definition at line 1011 of file kaction.cpp.

Referenced by KAction().

void KAction::setToolTip const QString   [virtual, slot]
 

Sets the tooltip text for the action.

This will be used as a tooltip for a toolbar button, as a statusbar help-text for a menu item, and it also appears in the toolbar editor, to describe the action.

Definition at line 635 of file kaction.cpp.

Referenced by KAction(), and setStatusText().

void KAction::setIconSet const QIconSet   iconSet [virtual, slot]
 

Sets the QIconSet from which the icons used to display this action will be chosen.

Definition at line 970 of file kaction.cpp.

void KAction::setEnabled bool    enable [virtual, slot]
 

Enables or disables this action.

All uses of this action (eg. in menus or toolbars) will be updated to reflect the state of the action.

Definition at line 848 of file kaction.cpp.

References QObject::name().

Referenced by KCommandHistory::addCommand(), KCommandHistory::clear(), KToggleToolBarAction::plug(), KCommandHistory::redo(), KSelectAction::setItems(), KXMLGUIClient::stateChanged(), KCommandHistory::undo(), and KCommandHistory::updateActions().

void KAction::setShortcutConfigurable bool    [virtual, slot]
 

Indicate whether the user may configure the action's shortcut.

Definition at line 883 of file kaction.cpp.

void KAction::activate   [virtual, slot]
 

Emulate user's interaction programmatically, by activating the action.

The implementation simply emits activated().

Definition at line 1104 of file kaction.cpp.

Referenced by KDCOPActionProxy::processAction().

int KAction::accel  
 

Deprecated:
. Use shortcut(). Get the keyboard accelerator associated with this action.

Definition at line 601 of file kaction.cpp.

void KAction::setAccel int    key
 

Deprecated:
. Use setShortcut(). Sets the keyboard accelerator associated with this action.

void KAction::setStatusText const QString   text [inline]
 

Deprecated:
. Use setToolTip instead (they do the same thing now).

Definition at line 588 of file kaction.h.

References setToolTip().

int KAction::menuId int    i [inline]
 

Deprecated:
. for backwards compatibility.

Definition at line 594 of file kaction.h.


The documentation for this class was generated from the following files:
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:03 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001