summaryrefslogtreecommitdiffabout
path: root/microkde/kdeui/kaction.h
Unidiff
Diffstat (limited to 'microkde/kdeui/kaction.h') (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kdeui/kaction.h624
1 files changed, 624 insertions, 0 deletions
diff --git a/microkde/kdeui/kaction.h b/microkde/kdeui/kaction.h
new file mode 100644
index 0000000..13e2e1e
--- a/dev/null
+++ b/microkde/kdeui/kaction.h
@@ -0,0 +1,624 @@
1/* This file is part of the KDE libraries
2 Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
3 (C) 1999 Simon Hausmann <hausmann@kde.org>
4 (C) 2000 Nicolas Hadacek <haadcek@kde.org>
5 (C) 2000 Kurt Granroth <granroth@kde.org>
6 (C) 2000 Michael Koch <koch@kde.org>
7 (C) 2001 Holger Freyther <freyther@kde.org>
8 (C) 2002 Ellis Whitehead <ellis@kde.org>
9
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public
12 License version 2 as published by the Free Software Foundation.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
18
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
23*/
24//$Id$
25
26#ifndef __kaction_h__
27#define __kaction_h__
28
29
30//US #include <qkeysequence.h>
31#include <qobject.h>
32#include <qvaluelist.h>
33#include <qguardedptr.h>
34#include <kguiitem.h>
35#include <kshortcut.h>
36#include <kstdaction.h>
37//US#include <kicontheme.h>
38
39//US added the following files
40#include <kiconloader.h>
41
42class QMenuBar;
43class QPopupMenu;
44//USclass QComboBox;
45//USclass QPoint;
46class QIconSet;
47class QString;
48class KToolBar;
49
50class KAccel;
51//USclass KAccelActions;
52//USclass KConfig;
53//USclass KConfigBase;
54//USclass KURL;
55//USclass KInstance;
56//USclass KToolBar;
57class KActionCollection;
58//USclass KPopupMenu;
59class KMainWindow;
60
61/**
62 * The KAction class (and derived and super classes) provides a way to
63 * easily encapsulate a "real" user-selected action or event in your
64 * program.
65 *
66 * For instance, a user may want to @p paste the contents of
67 * the clipboard or @p scroll @p down a document or @p quit the
68 * application. These are all @p actions -- events that the
69 * user causes to happen. The KAction class allows the developer to
70 * deal with these actions in an easy and intuitive manner.
71 *
72 * Specifically, the KAction class encapsulated the various attributes
73 * to an event/action. For instance, an action might have an icon
74 * that goes along with it (a clipboard for a "paste" action or
75 * scissors for a "cut" action). The action might have some text to
76 * describe the action. It will certainly have a method or function
77 * that actually @p executes the action! All these attributes
78 * are contained within the KAction object.
79 *
80 * The advantage of dealing with Actions is that you can manipulate
81 * the Action without regard to the GUI representation of it. For
82 * instance, in the "normal" way of dealing with actions like "cut",
83 * you would manually insert a item for Cut into a menu and a button
84 * into a toolbar. If you want to disable the cut action for a moment
85 * (maybe nothing is selected), you woud have to hunt down the pointer
86 * to the menu item and the toolbar button and disable both
87 * individually. Setting the menu item and toolbar item up uses very
88 * similar code - but has to be done twice!
89 *
90 * With the Action concept, you simply "plug" the Action into whatever
91 * GUI element you want. The KAction class will then take care of
92 * correctly defining the menu item (with icons, accelerators, text,
93 * etc) or toolbar button.. or whatever. From then on, if you
94 * manipulate the Action at all, the effect will propogate through all
95 * GUI representations of it. Back to the "cut" example: if you want
96 * to disable the Cut Action, you would simply do
97 * 'cutAction->setEnabled(false)' and the menuitem and button would
98 * instantly be disabled!
99 *
100 * This is the biggest advantage to the Action concept -- there is a
101 * one-to-one relationship between the "real" action and @p all
102 * GUI representations of it.
103 *
104 * KAction emits the activated() signal if the user activated the
105 * corresponding GUI element ( menu item, toolbar button, etc. )
106 *
107 * If you are in the situation of wanting to map the activated()
108 * signal of multiple action objects to one slot, with a special
109 * argument bound to each action, then you might consider using
110 * @ref QSignalMapper . A tiny example:
111 *
112 * <PRE>
113 * QSignalMapper *desktopNumberMapper = new QSignalMapper( this );
114 * connect( desktopNumberMapper, SIGNAL( mapped( int ) ),
115 * this, SLOT( moveWindowToDesktop( int ) ) );
116 *
117 * for ( uint i = 0; i < numberOfDesktops; ++i ) {
118 * KAction *desktopAction = new KAction( i18n( "Move Window to Desktop %i" ).arg( i ), ... );
119 * connect( desktopAction, SIGNAL( activated() ), desktopNumberMapper, SLOT( map() ) );
120 * desktopNumberMapper->setMapping( desktopAction, i );
121 * }
122 * </PRE>
123 *
124 * @sect General Usage:
125 *
126 * The steps to using actions are roughly as follows
127 *
128 * @li Decide which attributes you want to associate with a given
129 * action (icons, text, keyboard shortcut, etc)
130 * @li Create the action using KAction (or derived or super class).
131 * @li "Plug" the Action into whatever GUI element you want. Typically,
132 * this will be a menu or toolbar.
133 *
134 * @sect Detailed Example:
135 *
136 * Here is an example of enabling a "New [document]" action
137 * <PRE>
138 * KAction *newAct = new KAction(i18n("&New"), "filenew",
139 * KStdAccel::shortcut(KStdAccel::New),
140 * this, SLOT(fileNew()),
141 * actionCollection(), "new");
142 * </PRE>
143 * This line creates our action. It says that wherever this action is
144 * displayed, it will use "&New" as the text, the standard icon, and
145 * the standard shortcut. It further says that whenever this action
146 * is invoked, it will use the fileNew() slot to execute it.
147 *
148 * <PRE>
149 * QPopupMenu *file = new QPopupMenu;
150 * newAct->plug(file);
151 * </PRE>
152 * That just inserted the action into the File menu. The point is, it's not
153 * important in which menu it is: all manipulation of the item is
154 * done through the newAct object.
155 *
156 * <PRE>
157 * newAct->plug(toolBar());
158 * </PRE>
159 * And this inserted the Action into the main toolbar as a button.
160 *
161 * That's it!
162 *
163 * If you want to disable that action sometime later, you can do so
164 * with
165 * <PRE>
166 * newAct->setEnabled(false)
167 * </PRE>
168 * and both the menuitem in File and the toolbar button will instantly
169 * be disabled.
170 *
171 * Do not delete a KAction object without unplugging it from all its
172 * containers. The simplest way to do that is to use the unplugAll()
173 * as in the following example:
174 * <PRE>
175 * newAct->unplugAll();
176 * delete newAct;
177 * </PRE>
178 * Normally you will not need to do this as KActionCollection manages
179 * everything for you.
180 *
181 * Note: if you are using a "standard" action like "new", "paste",
182 * "quit", or any other action described in the KDE UI Standards,
183 * please use the methods in the @ref KStdAction class rather than
184 * defining your own.
185 *
186 * @sect Usage Within the XML Framework:
187 *
188 * If you are using KAction within the context of the XML menu and
189 * toolbar building framework, then there are a few tiny changes. The
190 * first is that you must insert your new action into an action
191 * collection. The action collection (a @ref KActionCollection) is,
192 * logically enough, a central collection of all of the actions
193 * defined in your application. The XML UI framework code in KXMLGUI
194 * classes needs access to this collection in order to build up the
195 * GUI (it's how the builder code knows which actions are valid and
196 * which aren't).
197 *
198 * Also, if you use the XML builder framework, then you do not ever
199 * have to plug your actions into containers manually. The framework
200 * does that for you.
201 *
202 * @see KStdAction
203 * @short Class to encapsulate user-driven action or event
204 */
205class KAction : public QObject
206{
207 friend class KActionCollection;
208 Q_OBJECT
209 Q_PROPERTY( int containerCount READ containerCount )
210 Q_PROPERTY( QString plainText READ plainText )
211 Q_PROPERTY( QString text READ text WRITE setText )
212 Q_PROPERTY( QString shortcut READ shortcutText WRITE setShortcutText )
213 Q_PROPERTY( bool enabled READ isEnabled WRITE setEnabled )
214 Q_PROPERTY( QString group READ group WRITE setGroup )
215 Q_PROPERTY( QString whatsThis READ whatsThis WRITE setWhatsThis )
216 Q_PROPERTY( QString toolTip READ toolTip WRITE setToolTip )
217 Q_PROPERTY( QString icon READ icon WRITE setIcon )
218public:
219 /**
220 * Constructs an action with text, potential keyboard
221 * shortcut, and a SLOT to call when this action is invoked by
222 * the user.
223 *
224 * If you do not want or have a keyboard shortcut,
225 * set the @p cut param to 0.
226 *
227 * This is the most common KAction used when you do not have a
228 * corresponding icon (note that it won't appear in the current version
229 * of the "Edit ToolBar" dialog, because an action needs an icon to be
230 * plugged in a toolbar...).
231 *
232 * @param text The text that will be displayed.
233 * @param cut The corresponding keyboard shortcut.
234 * @param receiver The SLOT's parent.
235 * @param slot The SLOT to invoke to execute this action.
236 * @param parent This action's parent.
237 * @param name An internal name for this action.
238 */
239 KAction( const QString& text, const KShortcut& cut,
240 const QObject* receiver, const char* slot,
241 KActionCollection* parent, const char* name );
242 /**
243 * Constructs an action with text, icon, potential keyboard
244 * shortcut, and a SLOT to call when this action is invoked by
245 * the user.
246 *
247 * If you do not want or have a keyboard shortcut, set the
248 * @p cut param to 0.
249 *
250 * This is the other common KAction used. Use it when you
251 * @p do have a corresponding icon.
252 *
253 * @param text The text that will be displayed.
254 * @param pix The icon to display.
255 * @param cut The corresponding keyboard shortcut.
256 * @param receiver The SLOT's parent.
257 * @param slot The SLOT to invoke to execute this action.
258 * @param parent This action's parent.
259 * @param name An internal name for this action.
260 */
261
262 KAction( const QString& text, const QIconSet& pix, const KShortcut& cut,
263 const QObject* receiver, const char* slot,
264 KActionCollection* parent, const char* name );
265
266 /**
267 * Constructs an action with text, icon, potential keyboard
268 * shortcut, and a SLOT to call when this action is invoked by
269 * the user. The icon is loaded on demand later based on where it
270 * is plugged in.
271 *
272 * If you do not want or have a keyboard shortcut, set the
273 * @p cut param to 0.
274 *
275 * This is the other common KAction used. Use it when you
276 * @p do have a corresponding icon.
277 *
278 * @param text The text that will be displayed.
279 * @param pix The icon to display.
280 * @param cut The corresponding keyboard shortcut (shortcut).
281 * @param receiver The SLOT's parent.
282 * @param slot The SLOT to invoke to execute this action.
283 * @param parent This action's parent.
284 * @param name An internal name for this action.
285 */
286 KAction( const QString& text, const QString& pix, const KShortcut& cut,
287 const QObject* receiver, const char* slot,
288 KActionCollection* parent, const char* name );
289
290 /**
291 * The same as the above constructor, but with a KGuiItem providing
292 * the text and icon.
293 *
294 * @param item The KGuiItem with the label and (optional) icon.
295 */
296 KAction( const KGuiItem& item, const KShortcut& cut,
297 const QObject* receiver, const char* slot,
298 KActionCollection* parent, const char* name );
299 /**
300 * @obsolete
301 */
302 KAction( const QString& text, const KShortcut& cut = KShortcut(), QObject* parent = 0, const char* name = 0 );
303 /**
304 * @obsolete
305 */
306 KAction( const QString& text, const KShortcut& cut,
307 const QObject* receiver, const char* slot, QObject* parent, const char* name = 0 );
308 /**
309 * @obsolete
310 */
311 KAction( const QString& text, const QIconSet& pix, const KShortcut& cut = KShortcut(),
312 QObject* parent = 0, const char* name = 0 );
313 /**
314 * @obsolete
315 */
316 KAction( const QString& text, const QString& pix, const KShortcut& cut = KShortcut(),
317 QObject* parent = 0, const char* name = 0 );
318 /**
319 * @obsolete
320 */
321 KAction( const QString& text, const QIconSet& pix, const KShortcut& cut,
322 const QObject* receiver, const char* slot, QObject* parent, const char* name = 0 );
323 /**
324 * @obsolete
325 */
326 KAction( const QString& text, const QString& pix, const KShortcut& cut,
327 const QObject* receiver, const char* slot, QObject* parent,
328 const char* name = 0 );
329 /**
330 * @obsolete
331 */
332 KAction( QObject* parent = 0, const char* name = 0 );
333
334 /**
335 * Standard destructor
336 */
337 virtual ~KAction();
338
339 /**
340 * "Plug" or insert this action into a given widget.
341 *
342 * This will
343 * typically be a menu or a toolbar. From this point on, you will
344 * never need to directly manipulate the item in the menu or
345 * toolbar. You do all enabling/disabling/manipulation directly
346 * with your KAction object.
347 *
348 * @param w The GUI element to display this action
349 */
350 virtual int plug( QWidget *w, int index = -1 );
351
352 /**
353 * @deprecated. Shouldn't be used. No substitute available.
354 *
355 * "Plug" or insert this action into a given KAccel.
356 *
357 * @param accel The KAccel collection which holds this accel
358 * @param configurable If the shortcut is configurable via
359 * the KAccel configuration dialog (this is somehow deprecated since
360 * there is now a KAction key configuration dialog).
361 */
362 virtual void plugAccel(KAccel *accel, bool configurable = true);
363
364 /**
365 * "Unplug" or remove this action from a given widget.
366 *
367 * This will typically be a menu or a toolbar. This is rarely
368 * used in "normal" application. Typically, it would be used if
369 * your application has several views or modes, each with a
370 * completely different menu structure. If you simply want to
371 * disable an action for a given period, use @ref setEnabled()
372 * instead.
373 *
374 * @param w Remove the action from this GUI element.
375 */
376 virtual void unplug( QWidget *w );
377
378 /**
379 * @deprecated. Complement method to plugAccel().
380 * Disconnect this action from the KAccel.
381 */
382 virtual void unplugAccel();
383
384 /**
385 * returns whether the action is plugged into any container widget or not.
386 * @since 3.1
387 */
388 virtual bool isPlugged() const;
389
390 /**
391 * returns whether the action is plugged into the given container
392 */
393 bool isPlugged( const QWidget *container ) const;
394
395 /**
396 * returns whether the action is plugged into the given container with the given, container specific, id (often
397 * menu or toolbar id ) .
398 */
399 virtual bool isPlugged( const QWidget *container, int id ) const;
400
401 /**
402 * returns whether the action is plugged into the given container with the given, container specific, representative
403 * container widget item.
404 */
405 virtual bool isPlugged( const QWidget *container, const QWidget *_representative ) const;
406
407 QWidget* container( int index ) const;
408 int itemId( int index ) const;
409 QWidget* representative( int index ) const;
410 int containerCount() const;
411 /// @since 3.1
412 uint kaccelCount() const;
413
414 virtual bool hasIcon() const;
415#ifndef KDE_NO_COMPAT
416 bool hasIconSet() const { return hasIcon(); }
417#endif
418 virtual QString plainText() const;
419
420 /**
421 * Get the text associated with this action.
422 */
423 virtual QString text() const;
424
425 /**
426 * Get the keyboard shortcut associated with this action.
427 */
428 virtual const KShortcut& shortcut() const;
429 /**
430 * Get the default shortcut for this action.
431 */
432 virtual const KShortcut& shortcutDefault() const;
433
434 // These two methods are for Q_PROPERTY
435 QString shortcutText() const;
436 void setShortcutText( const QString& );
437
438 /**
439 * Returns true if this action is enabled.
440 */
441 virtual bool isEnabled() const;
442
443 /**
444 * Returns true if this action's shortcut is configurable.
445 */
446 virtual bool isShortcutConfigurable() const;
447
448 virtual QString group() const;
449
450 /**
451 * Get the What's this text for the action.
452 */
453 virtual QString whatsThis() const;
454
455 /**
456 * Get the tooltip text for the action.
457 */
458 virtual QString toolTip() const;
459
460 /**
461 * Get the QIconSet from which the icons used to display this action will
462 * be chosen.
463 */
464 virtual QIconSet iconSet( KIcon::Group group, int size=0 ) const;
465
466#ifndef KDE_NO_COMPAT
467 QIconSet iconSet() const
468 {
469 return iconSet( KIcon::Small );
470 }
471#endif
472
473 virtual QString icon() const;
474
475 KActionCollection *parentCollection() const;
476
477 /**
478 * @internal
479 * Generate a toolbar button id. Made public for reimplementations.
480 */
481 static int getToolButtonID();
482
483
484 void unplugAll();
485
486public slots:
487 /**
488 * Sets the text associated with this action. The text is used for menu
489 * and toolbar labels etc.
490 */
491 virtual void setText(const QString &text);
492
493 /**
494 * Sets the keyboard shortcut associated with this action.
495 */
496 virtual bool setShortcut( const KShortcut& );
497
498 virtual void setGroup( const QString& );
499
500 /**
501 * Sets the What's this text for the action. This text will be displayed when
502 * a widget that has been created by plugging this action into a container
503 * is clicked on in What's this mode.
504 *
505 * The What's this text can include QML markup as well as raw text.
506 */
507 virtual void setWhatsThis( const QString& text );
508
509 /**
510 * Sets the tooltip text for the action.
511 * This will be used as a tooltip for a toolbar button, as a
512 * statusbar help-text for a menu item, and it also appears
513 * in the toolbar editor, to describe the action.
514 */
515 virtual void setToolTip( const QString& );
516
517 /**
518 * Sets the QIconSet from which the icons used to display this action will
519 * be chosen.
520 */
521 virtual void setIconSet( const QIconSet &iconSet );
522
523 virtual void setIcon( const QString& icon );
524
525 /**
526 * Enables or disables this action. All uses of this action (eg. in menus
527 * or toolbars) will be updated to reflect the state of the action.
528 */
529 virtual void setEnabled(bool enable);
530
531 /**
532 * Indicate whether the user may configure the action's shortcut.
533 */
534 virtual void setShortcutConfigurable( bool );
535
536 /**
537 * Emulate user's interaction programmatically, by activating the action.
538 * The implementation simply emits activated().
539 */
540 virtual void activate();
541
542protected slots:
543 virtual void slotDestroyed();
544 virtual void slotKeycodeChanged();
545 virtual void slotActivated();
546
547protected:
548 KToolBar* toolBar( int index ) const;
549 QPopupMenu* popupMenu( int index ) const;
550 void removeContainer( int index );
551 int findContainer( const QWidget* widget ) const;
552 void plugMainWindowAccel( QWidget *w );
553
554 void addContainer( QWidget* parent, int id );
555 void addContainer( QWidget* parent, QWidget* representative );
556
557 virtual void updateShortcut( int i );
558 virtual void updateShortcut( QPopupMenu* menu, int id );
559 virtual void updateGroup( int id );
560 virtual void updateText(int i );
561 virtual void updateEnabled(int i);
562 virtual void updateIconSet(int i);
563 virtual void updateIcon( int i);
564 virtual void updateToolTip( int id );
565 virtual void updateWhatsThis( int i );
566
567 KActionCollection *m_parentCollection;
568 QString whatsThisWithIcon() const;
569
570signals:
571 void activated();
572 void enabled( bool );
573
574private:
575 void initPrivate( const QString& text, const KShortcut& cut,
576 const QObject* receiver, const char* slot );
577 KAccel* kaccelCurrent();
578 bool initShortcut( const KShortcut& );
579 void plugShortcut();
580 bool updateKAccelShortcut( KAccel* kaccel );
581 void insertKAccel( KAccel* );
582 /** @internal To be used exclusively by KActionCollection::removeWidget(). */
583 void removeKAccel( KAccel* );
584
585#ifndef KDE_NO_COMPAT
586public:
587 /**
588 * @deprecated. Use shortcut().
589 * Get the keyboard accelerator associated with this action.
590 */
591 int accel() const;
592
593 QString statusText() const
594 { return toolTip(); }
595
596 /**
597 * @deprecated. Use setShortcut().
598 * Sets the keyboard accelerator associated with this action.
599 */
600 void setAccel( int key );
601
602 /**
603 * @deprecated. Use setToolTip instead (they do the same thing now).
604 */
605 void setStatusText( const QString &text )
606 { setToolTip( text ); }
607
608 /**
609 * @deprecated. for backwards compatibility.
610 */
611 int menuId( int i ) { return itemId( i ); }
612#endif // !KDE_NO_COMPAT
613
614protected:
615 virtual void virtual_hook( int id, void* data );
616private:
617 class KActionPrivate;
618 KActionPrivate *d;
619};
620
621#include <kactioncollection.h>
622#include <kactionclasses.h>
623
624#endif