• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

KAction Class Reference

#include <kaction.h>

Inheritance diagram for KAction:

Inheritance graph
[legend]

List of all members.


Detailed Description

Class to encapsulate user-driven action or event.

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

For instance, a user may want to paste the contents of the clipboard, 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, and conforms to KDE's extended functionality requirements - including supporting multiple user-configurable shortcuts, and KDE named icons. Actions also improve accessibility.

Specifically, QAction (and thus KAction) encapsulates the various attributes of an event/action. For instance, an action might have an icon() that provides a visual representation (a clipboard for a "paste" action or scissors for a "cut" action). The action should also be described by some text(). It will certainly be connected to a method that actually executes the action! All these attributes are contained within the action 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 would 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 add the action to whatever GUI element you want. The KAction class will then take care of correctly defining the menu item (with icons, accelerators, text, etc), toolbar button, or other. 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 call 'cutAction->setEnabled(false)' and both 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 hovered() signal on mouseover, and the triggered(bool checked) signal on activation of a corresponding GUI element ( menu item, toolbar button, etc. )

If you are in the situation of wanting to map the triggered() signal of multiple action objects to one slot, with a special argument bound to each action, you have several options:

Using QActionGroup:

  • Create a QActionGroup and assign it to each of the actions with setActionGroup(), then
  • Connect the QActionGroup::triggered(QAction*) signal to your slot.
Using QSignalMapper:
 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( triggered(bool) ), desktopNumberMapper, SLOT( map() ) );
     desktopNumberMapper->setMapping( desktopAction, i );
 }

General Usage

The steps to using actions are roughly as follows:

  • Decide which attributes you want to associate with a given action (icons, text, keyboard shortcut, etc)
  • Create the action using KAction (or derived or super class).
  • Add the action into whatever GUI element you want. Typically, this will be a menu or toolbar.

General Usage

Local shortcuts are active in their context, global shortcus are active everywhere, usually even if another program has focus.

  • Active shortcuts trigger a KAction if activated.
  • Default shortcuts are what the active shortcuts revert to if the user chooses to reset shortcuts to default.

Detailed Example

Here is an example of enabling a "New [document]" action
 KAction *newAct = new KAction("filenew", i18n("&New"), actionCollection(), "new");
 newAct->setShortcut(KStandardShortcut::shortcut(KStandardShortcut::New));
 connect(newAct, SIGNAL(triggered(bool)), SLOT(fileNew()));

This section 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.

 QMenu *file = new QMenu;
 file->addAction(newAct);
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.

 toolBar()->addAction(newAct);
And this added 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.

Unlike with previous versions of KDE, the action can simply be deleted when you have finished with it - the destructor takes care of all of the cleanup.

Note:
calling QAction::setShortcut() on a KAction may lead to unexpected behavior. There is nothing we can do about it because QAction::setShortcut() is not virtual.

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 KStandardAction 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, you do not ever have to add your actions to containers manually. The framework does that for you.

See also:
KStandardAction

Definition at line 191 of file kaction.h.


Public Types

enum  ShortcutType { ActiveShortcut = 0x1, DefaultShortcut = 0x2 }
enum  GlobalShortcutLoading { Autoloading = 0x0, NoAutoloading = 0x4 }

Signals

void triggered (Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)

Public Member Functions

 KAction (QObject *parent)
 KAction (const QString &text, QObject *parent)
 KAction (const KIcon &icon, const QString &text, QObject *parent)
virtual ~KAction ()
KShortcut shortcut (ShortcutTypes types=ActiveShortcut) const
void setShortcut (const KShortcut &shortcut, ShortcutTypes type=ShortcutTypes(ActiveShortcut|DefaultShortcut))
void setShortcut (const QKeySequence &shortcut, ShortcutTypes type=ShortcutTypes(ActiveShortcut|DefaultShortcut))
void setShortcuts (const QList< QKeySequence > &shortcuts, ShortcutTypes type=ShortcutTypes(ActiveShortcut|DefaultShortcut))
bool isShortcutConfigurable () const
void setShortcutConfigurable (bool configurable)
const KShortcut & globalShortcut (ShortcutTypes type=ActiveShortcut) const
void setGlobalShortcut (const KShortcut &shortcut, ShortcutTypes type=ShortcutTypes(ActiveShortcut|DefaultShortcut), GlobalShortcutLoading loading=Autoloading)
bool globalShortcutAllowed () const
void setGlobalShortcutAllowed (bool allowed, GlobalShortcutLoading loading=Autoloading)
KShapeGesture shapeGesture (ShortcutTypes type=ActiveShortcut) const
KRockerGesture rockerGesture (ShortcutTypes type=ActiveShortcut) const
void setShapeGesture (const KShapeGesture &gest, ShortcutTypes type=ShortcutTypes(ActiveShortcut|DefaultShortcut))
void setRockerGesture (const KRockerGesture &gest, ShortcutTypes type=ShortcutTypes(ActiveShortcut|DefaultShortcut))

Properties

KShortcut shortcut
bool shortcutConfigurable
KShortcut globalShortcut
bool globalShortcutAllowed

Friends

class KGlobalAccelPrivate
class KActionPrivate

Member Enumeration Documentation

enum KAction::ShortcutType

An enumeration about the two types of shortcuts in a KAction.

Enumerator:
ActiveShortcut  The shortcut will immediately become active but may be reset to "default".
DefaultShortcut  The shortcut is a default shortcut - it becomes active when somebody decides to reset shortcuts to default.

Definition at line 205 of file kaction.h.

enum KAction::GlobalShortcutLoading

An enum about global shortcut setter semantics.

Enumerator:
Autoloading  Look up the action in global settings (using its main component's name and text()) and set the shortcut as saved there.

See also:
setGlobalShortcut()
NoAutoloading  Prevent autoloading of saved global shortcut for action.

Definition at line 219 of file kaction.h.


Constructor & Destructor Documentation

KAction::KAction ( QObject *  parent  )  [explicit]

Constructs an action.

Definition at line 60 of file kaction.cpp.

KAction::KAction ( const QString &  text,
QObject *  parent 
)

Constructs an action with the specified parent and visible text.

Parameters:
text The visible text for this action.
parent The parent for this action.

Definition at line 66 of file kaction.cpp.

KAction::KAction ( const KIcon &  icon,
const QString &  text,
QObject *  parent 
)

Constructs an action with text and icon; a shortcut may be specified by the ampersand character (e.g.

\"&amp;Option\" creates a shortcut with key O )

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

Parameters:
icon The icon to display.
text The text that will be displayed.
parent The parent for this action.

Definition at line 73 of file kaction.cpp.

KAction::~KAction (  )  [virtual]

Standard destructor.

Definition at line 81 of file kaction.cpp.


Member Function Documentation

KShortcut KAction::shortcut ( ShortcutTypes  types = ActiveShortcut  )  const

Get the shortcut for this action.

This is preferred over QAction::shortcut(), as it allows for multiple shortcuts per action. The first and second shortcut as reported by shortcuts() will be the primary and alternate shortcut of the shortcut returned.

Parameters:
types the type of shortcut to return. Should both be specified, only the active shortcut will be returned. Defaults to the active shortcut, if one exists.
See also:
shortcuts()

Definition at line 110 of file kaction.cpp.

void KAction::setShortcut ( const KShortcut &  shortcut,
ShortcutTypes  type = ShortcutTypes(ActiveShortcut | DefaultShortcut) 
)

Set the shortcut for this action.

This is preferred over QAction::setShortcut(), as it allows for multiple shortcuts per action.

Parameters:
shortcut shortcut(s) to use for this action in its specified shortcutContext()
type type of shortcut to be set: active shortcut, default shortcut, or both (the default).

Definition at line 125 of file kaction.cpp.

void KAction::setShortcut ( const QKeySequence &  shortcut,
ShortcutTypes  type = ShortcutTypes(ActiveShortcut | DefaultShortcut) 
)

Definition at line 139 of file kaction.cpp.

void KAction::setShortcuts ( const QList< QKeySequence > &  shortcuts,
ShortcutTypes  type = ShortcutTypes(ActiveShortcut | DefaultShortcut) 
)

Definition at line 151 of file kaction.cpp.

bool KAction::isShortcutConfigurable (  )  const

Returns true if this action's shortcut is configurable.

Definition at line 100 of file kaction.cpp.

void KAction::setShortcutConfigurable ( bool  configurable  ) 

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

Parameters:
configurable set to true if this shortcut may be configured by the user, otherwise false.

Definition at line 105 of file kaction.cpp.

const KShortcut & KAction::globalShortcut ( ShortcutTypes  type = ActiveShortcut  )  const

Get the global shortcut for this action, if one exists.

Global shortcuts allow your actions to respond to accellerators independently of the focused window. Unlike regular shortcuts, the application's window does not need focus for them to be activated.

Parameters:
type the type of shortcut to be returned. Should both be specified, only the active shortcut will be returned. Defaults to the active shortcut, if one exists.
See also:
KGlobalAccel

setGlobalShortcut()

Definition at line 164 of file kaction.cpp.

void KAction::setGlobalShortcut ( const KShortcut &  shortcut,
ShortcutTypes  type = ShortcutTypes(ActiveShortcut | DefaultShortcut),
GlobalShortcutLoading  loading = Autoloading 
)

Assign a global shortcut for this action.

Global shortcuts allow your actions to respond to keys independently of the focused window. Unlike regular shortcuts, the application's window does not need focus for them to be activated.

When an action, identified by main component name and text(), is assigned a global shortcut for the first time on a KDE installation the assignment will be saved. The shortcut will then be restored every time the action's globalShortcutAllowed flag becomes true. This includes calling setGlobalShortcut()! If you actually want to change the global shortcut you have to set loading to NoAutoloading. The new shortcut will be saved again.

Parameters:
shortcut global shortcut(s) to assign. Will be ignored unless loading is set to NoAutoloading.
type the type of shortcut to be set, whether the active shortcut, the default shortcut, or both (the default).
loading load the previous shortcut, which might have been changed by the user (Autoloading, the default) or really set
shortcut as the new shortcut (NoAutoloading).
See also:
KGlobalAccel

globalShortcut()

Definition at line 174 of file kaction.cpp.

bool KAction::globalShortcutAllowed (  )  const

Returns true if this action is permitted to have a global shortcut.

Defaults to false.

void KAction::setGlobalShortcutAllowed ( bool  allowed,
GlobalShortcutLoading  loading = Autoloading 
)

Indicate whether the programmer and/or user may define a global shortcut for this action.

Defaults to false. Note that calling setGlobalShortcut() turns this on automatically.

Parameters:
allowed set to true if this action may have a global shortcut, otherwise false.
loading (see setGlobalShortcut). Ignore this parameter.

Definition at line 209 of file kaction.cpp.

KShapeGesture KAction::shapeGesture ( ShortcutTypes  type = ActiveShortcut  )  const

Definition at line 218 of file kaction.cpp.

KRockerGesture KAction::rockerGesture ( ShortcutTypes  type = ActiveShortcut  )  const

Definition at line 227 of file kaction.cpp.

void KAction::setShapeGesture ( const KShapeGesture &  gest,
ShortcutTypes  type = ShortcutTypes(ActiveShortcut | DefaultShortcut) 
)

Definition at line 236 of file kaction.cpp.

void KAction::setRockerGesture ( const KRockerGesture &  gest,
ShortcutTypes  type = ShortcutTypes(ActiveShortcut | DefaultShortcut) 
)

Definition at line 254 of file kaction.cpp.

void KAction::triggered ( Qt::MouseButtons  buttons,
Qt::KeyboardModifiers  modifiers 
) [signal]

Emitted when the action is triggered.

Also provides the state of the keyboard modifiers and mouse buttons at the time.


Friends And Related Function Documentation

friend class KGlobalAccelPrivate [friend]

Definition at line 406 of file kaction.h.

friend class KActionPrivate [friend]

Definition at line 409 of file kaction.h.


Property Documentation

KShortcut KAction::shortcut [read, write]

Definition at line 195 of file kaction.h.

bool KAction::shortcutConfigurable [read, write]

Definition at line 196 of file kaction.h.

KShortcut KAction::globalShortcut [read, write]

Definition at line 197 of file kaction.h.

bool KAction::globalShortcutAllowed [read, write]

Definition at line 198 of file kaction.h.


The documentation for this class was generated from the following files:
  • kaction.h
  • kaction.cpp

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   WTF
  • KJSEmbed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  •   core
  • Phonon
  •   Backend
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal