source: trunk/src/gui/kernel/qaction.cpp@ 580

Last change on this file since 580 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 39.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qaction.h"
43#include "qactiongroup.h"
44
45#ifndef QT_NO_ACTION
46#include "qaction_p.h"
47#include "qapplication.h"
48#include "qevent.h"
49#include "qlist.h"
50#include "qdebug.h"
51#include <private/qshortcutmap_p.h>
52#include <private/qapplication_p.h>
53#include <private/qmenu_p.h>
54
55#define QAPP_CHECK(functionName) \
56 if (!qApp) { \
57 qWarning("QAction: Initialize QApplication before calling '" functionName "'."); \
58 return; \
59 }
60
61QT_BEGIN_NAMESPACE
62
63/*
64 internal: guesses a descriptive text from a text suited for a menu entry
65 */
66static QString qt_strippedText(QString s)
67{
68 s.remove( QString::fromLatin1("...") );
69 int i = 0;
70 while (i < s.size()) {
71 ++i;
72 if (s.at(i-1) != QLatin1Char('&'))
73 continue;
74 if (i < s.size() && s.at(i) == QLatin1Char('&'))
75 ++i;
76 s.remove(i-1,1);
77 }
78 return s.trimmed();
79}
80
81
82QActionPrivate::QActionPrivate() : group(0), enabled(1), forceDisabled(0),
83 visible(1), forceInvisible(0), checkable(0), checked(0), separator(0), fontSet(false),
84 menuRole(QAction::TextHeuristicRole), softKeyRole(QAction::NoSoftKey),
85 priority(QAction::NormalPriority), iconVisibleInMenu(-1)
86{
87#ifdef QT3_SUPPORT
88 static int qt_static_action_id = -1;
89 param = id = --qt_static_action_id;
90 act_signal = 0;
91#endif
92#ifndef QT_NO_SHORTCUT
93 shortcutId = 0;
94 shortcutContext = Qt::WindowShortcut;
95 autorepeat = true;
96#endif
97}
98
99QActionPrivate::~QActionPrivate()
100{
101}
102
103bool QActionPrivate::showStatusText(QWidget *widget, const QString &str)
104{
105#ifdef QT_NO_STATUSTIP
106 Q_UNUSED(widget);
107 Q_UNUSED(str);
108#else
109 if(QObject *object = widget ? widget : parent) {
110 QStatusTipEvent tip(str);
111 QApplication::sendEvent(object, &tip);
112 return true;
113 }
114#endif
115 return false;
116}
117
118void QActionPrivate::sendDataChanged()
119{
120 Q_Q(QAction);
121 QActionEvent e(QEvent::ActionChanged, q);
122 for (int i = 0; i < widgets.size(); ++i) {
123 QWidget *w = widgets.at(i);
124 QApplication::sendEvent(w, &e);
125 }
126#ifndef QT_NO_GRAPHICSVIEW
127 for (int i = 0; i < graphicsWidgets.size(); ++i) {
128 QGraphicsWidget *w = graphicsWidgets.at(i);
129 QApplication::sendEvent(w, &e);
130 }
131#endif
132 QApplication::sendEvent(q, &e);
133
134 emit q->changed();
135}
136
137#ifndef QT_NO_SHORTCUT
138void QActionPrivate::redoGrab(QShortcutMap &map)
139{
140 Q_Q(QAction);
141 if (shortcutId)
142 map.removeShortcut(shortcutId, q);
143 if (shortcut.isEmpty())
144 return;
145 shortcutId = map.addShortcut(q, shortcut, shortcutContext);
146 if (!enabled)
147 map.setShortcutEnabled(false, shortcutId, q);
148 if (!autorepeat)
149 map.setShortcutAutoRepeat(false, shortcutId, q);
150}
151
152void QActionPrivate::redoGrabAlternate(QShortcutMap &map)
153{
154 Q_Q(QAction);
155 for(int i = 0; i < alternateShortcutIds.count(); ++i) {
156 if (const int id = alternateShortcutIds.at(i))
157 map.removeShortcut(id, q);
158 }
159 alternateShortcutIds.clear();
160 if (alternateShortcuts.isEmpty())
161 return;
162 for(int i = 0; i < alternateShortcuts.count(); ++i) {
163 const QKeySequence& alternate = alternateShortcuts.at(i);
164 if (!alternate.isEmpty())
165 alternateShortcutIds.append(map.addShortcut(q, alternate, shortcutContext));
166 else
167 alternateShortcutIds.append(0);
168 }
169 if (!enabled) {
170 for(int i = 0; i < alternateShortcutIds.count(); ++i) {
171 const int id = alternateShortcutIds.at(i);
172 map.setShortcutEnabled(false, id, q);
173 }
174 }
175 if (!autorepeat) {
176 for(int i = 0; i < alternateShortcutIds.count(); ++i) {
177 const int id = alternateShortcutIds.at(i);
178 map.setShortcutAutoRepeat(false, id, q);
179 }
180 }
181}
182
183void QActionPrivate::setShortcutEnabled(bool enable, QShortcutMap &map)
184{
185 Q_Q(QAction);
186 if (shortcutId)
187 map.setShortcutEnabled(enable, shortcutId, q);
188 for(int i = 0; i < alternateShortcutIds.count(); ++i) {
189 if (const int id = alternateShortcutIds.at(i))
190 map.setShortcutEnabled(enable, id, q);
191 }
192}
193#endif // QT_NO_SHORTCUT
194
195
196/*!
197 \class QAction
198 \brief The QAction class provides an abstract user interface
199 action that can be inserted into widgets.
200
201 \ingroup mainwindow-classes
202
203
204 \omit
205 * parent and widget are different
206 * parent does not define context
207 \endomit
208
209 In applications many common commands can be invoked via menus,
210 toolbar buttons, and keyboard shortcuts. Since the user expects
211 each command to be performed in the same way, regardless of the
212 user interface used, it is useful to represent each command as
213 an \e action.
214
215 Actions can be added to menus and toolbars, and will
216 automatically keep them in sync. For example, in a word processor,
217 if the user presses a Bold toolbar button, the Bold menu item
218 will automatically be checked.
219
220 Actions can be created as independent objects, but they may
221 also be created during the construction of menus; the QMenu class
222 contains convenience functions for creating actions suitable for
223 use as menu items.
224
225 A QAction may contain an icon, menu text, a shortcut, status text,
226 "What's This?" text, and a tooltip. Most of these can be set in
227 the constructor. They can also be set independently with
228 setIcon(), setText(), setIconText(), setShortcut(),
229 setStatusTip(), setWhatsThis(), and setToolTip(). For menu items,
230 it is possible to set an individual font with setFont().
231
232 Actions are added to widgets using QWidget::addAction() or
233 QGraphicsWidget::addAction(). Note that an action must be added to a
234 widget before it can be used; this is also true when the shortcut should
235 be global (i.e., Qt::ApplicationShortcut as Qt::ShortcutContext).
236
237 Once a QAction has been created it should be added to the relevant
238 menu and toolbar, then connected to the slot which will perform
239 the action. For example:
240
241 \snippet examples/mainwindows/application/mainwindow.cpp 19
242 \codeline
243 \snippet examples/mainwindows/application/mainwindow.cpp 28
244 \snippet examples/mainwindows/application/mainwindow.cpp 31
245
246 We recommend that actions are created as children of the window
247 they are used in. In most cases actions will be children of
248 the application's main window.
249
250 \sa QMenu, QToolBar, {Application Example}
251*/
252
253/*!
254 \fn void QAction::trigger()
255
256 This is a convenience slot that calls activate(Trigger).
257*/
258
259/*!
260 \fn void QAction::hover()
261
262 This is a convenience slot that calls activate(Hover).
263*/
264
265/*!
266 \enum QAction::MenuRole
267
268 This enum describes how an action should be moved into the application menu on Mac OS X.
269
270 \value NoRole This action should not be put into the application menu
271 \value TextHeuristicRole This action should be put in the application menu based on the action's text
272 as described in the QMenuBar documentation.
273 \value ApplicationSpecificRole This action should be put in the application menu with an application specific role
274 \value AboutQtRole This action matches handles the "About Qt" menu item.
275 \value AboutRole This action should be placed where the "About" menu item is in the application menu. The text of
276 the menu item will be set to "About <application name>". The application name is fetched from the
277 \c{Info.plist} file in the application's bundle (See \l{Deploying an Application on Mac OS X}).
278 \value PreferencesRole This action should be placed where the "Preferences..." menu item is in the application menu.
279 \value QuitRole This action should be placed where the Quit menu item is in the application menu.
280
281 Setting this value only has effect on items that are in the immediate menus
282 of the menubar, not the submenus of those menus. For example, if you have
283 File menu in your menubar and the File menu has a submenu, setting the
284 MenuRole for the actions in that submenu have no effect. They will never be moved.
285*/
286
287/*! \since 4.6
288
289 \enum QAction::SoftKeyRole
290
291 This enum describes how an action should be placed in the softkey bar. Currently this enum only
292 has an effect on the Symbian platform.
293
294 \value NoSoftKey This action should not be used as a softkey
295 \value PositiveSoftKey This action is used to describe a softkey with a positive or non-destructive
296 role such as Ok, Select, or Options.
297 \value NegativeSoftKey This action is used to describe a soft ey with a negative or destructive role
298 role such as Cancel, Discard, or Close.
299 \value SelectSoftKey This action is used to describe a role that selects a particular item or widget
300 in the application.
301
302 Actions with a softkey role defined are only visible in the softkey bar when the widget containing
303 the action has focus. If no widget currently has focus, the softkey framework will traverse up the
304 widget parent hierarchy looking for a widget containing softkey actions.
305 */
306
307/*!
308 Constructs an action with \a parent. If \a parent is an action
309 group the action will be automatically inserted into the group.
310*/
311QAction::QAction(QObject* parent)
312 : QObject(*(new QActionPrivate), parent)
313{
314 Q_D(QAction);
315 d->group = qobject_cast<QActionGroup *>(parent);
316 if (d->group)
317 d->group->addAction(this);
318}
319
320
321/*!
322 Constructs an action with some \a text and \a parent. If \a
323 parent is an action group the action will be automatically
324 inserted into the group.
325
326 The action uses a stripped version of \a text (e.g. "\&Menu
327 Option..." becomes "Menu Option") as descriptive text for
328 tool buttons. You can override this by setting a specific
329 description with setText(). The same text will be used for
330 tooltips unless you specify a different text using
331 setToolTip().
332
333*/
334QAction::QAction(const QString &text, QObject* parent)
335 : QObject(*(new QActionPrivate), parent)
336{
337 Q_D(QAction);
338 d->text = text;
339 d->group = qobject_cast<QActionGroup *>(parent);
340 if (d->group)
341 d->group->addAction(this);
342}
343
344/*!
345 Constructs an action with an \a icon and some \a text and \a
346 parent. If \a parent is an action group the action will be
347 automatically inserted into the group.
348
349 The action uses a stripped version of \a text (e.g. "\&Menu
350 Option..." becomes "Menu Option") as descriptive text for
351 tool buttons. You can override this by setting a specific
352 description with setText(). The same text will be used for
353 tooltips unless you specify a different text using
354 setToolTip().
355*/
356QAction::QAction(const QIcon &icon, const QString &text, QObject* parent)
357 : QObject(*(new QActionPrivate), parent)
358{
359 Q_D(QAction);
360 d->icon = icon;
361 d->text = text;
362 d->group = qobject_cast<QActionGroup *>(parent);
363 if (d->group)
364 d->group->addAction(this);
365}
366
367/*!
368 \internal
369*/
370QAction::QAction(QActionPrivate &dd, QObject *parent)
371 : QObject(dd, parent)
372{
373 Q_D(QAction);
374 d->group = qobject_cast<QActionGroup *>(parent);
375 if (d->group)
376 d->group->addAction(this);
377}
378
379/*!
380 Returns the parent widget.
381*/
382QWidget *QAction::parentWidget() const
383{
384 QObject *ret = parent();
385 while (ret && !ret->isWidgetType())
386 ret = ret->parent();
387 return (QWidget*)ret;
388}
389
390/*!
391 \since 4.2
392 Returns a list of widgets this action has been added to.
393
394 \sa QWidget::addAction(), associatedGraphicsWidgets()
395*/
396QList<QWidget *> QAction::associatedWidgets() const
397{
398 Q_D(const QAction);
399 return d->widgets;
400}
401
402#ifndef QT_NO_GRAPHICSVIEW
403/*!
404 \since 4.5
405 Returns a list of widgets this action has been added to.
406
407 \sa QWidget::addAction(), associatedWidgets()
408*/
409QList<QGraphicsWidget *> QAction::associatedGraphicsWidgets() const
410{
411 Q_D(const QAction);
412 return d->graphicsWidgets;
413}
414#endif
415
416#ifndef QT_NO_SHORTCUT
417/*!
418 \property QAction::shortcut
419 \brief the action's primary shortcut key
420
421 Valid keycodes for this property can be found in \l Qt::Key and
422 \l Qt::Modifier. There is no default shortcut key.
423*/
424void QAction::setShortcut(const QKeySequence &shortcut)
425{
426 QAPP_CHECK("setShortcut");
427
428 Q_D(QAction);
429 if (d->shortcut == shortcut)
430 return;
431
432 d->shortcut = shortcut;
433 d->redoGrab(qApp->d_func()->shortcutMap);
434 d->sendDataChanged();
435}
436
437/*!
438 \since 4.2
439
440 Sets \a shortcuts as the list of shortcuts that trigger the
441 action. The first element of the list is the primary shortcut.
442
443 \sa shortcut
444*/
445void QAction::setShortcuts(const QList<QKeySequence> &shortcuts)
446{
447 Q_D(QAction);
448
449 QList <QKeySequence> listCopy = shortcuts;
450
451 QKeySequence primary;
452 if (!listCopy.isEmpty())
453 primary = listCopy.takeFirst();
454
455 if (d->shortcut == primary && d->alternateShortcuts == listCopy)
456 return;
457
458 QAPP_CHECK("setShortcuts");
459
460 d->shortcut = primary;
461 d->alternateShortcuts = listCopy;
462 d->redoGrab(qApp->d_func()->shortcutMap);
463 d->redoGrabAlternate(qApp->d_func()->shortcutMap);
464 d->sendDataChanged();
465}
466
467/*!
468 \since 4.2
469
470 Sets a platform dependent list of shortcuts based on the \a key.
471 The result of calling this function will depend on the currently running platform.
472 Note that more than one shortcut can assigned by this action.
473 If only the primary shortcut is required, use setShortcut instead.
474
475 \sa QKeySequence::keyBindings()
476*/
477void QAction::setShortcuts(QKeySequence::StandardKey key)
478{
479 QList <QKeySequence> list = QKeySequence::keyBindings(key);
480 setShortcuts(list);
481}
482
483/*!
484 Returns the primary shortcut.
485
486 \sa setShortcuts()
487*/
488QKeySequence QAction::shortcut() const
489{
490 Q_D(const QAction);
491 return d->shortcut;
492}
493
494/*!
495 \since 4.2
496
497 Returns the list of shortcuts, with the primary shortcut as
498 the first element of the list.
499
500 \sa setShortcuts()
501*/
502QList<QKeySequence> QAction::shortcuts() const
503{
504 Q_D(const QAction);
505 QList <QKeySequence> shortcuts;
506 if (!d->shortcut.isEmpty())
507 shortcuts << d->shortcut;
508 if (!d->alternateShortcuts.isEmpty())
509 shortcuts << d->alternateShortcuts;
510 return shortcuts;
511}
512
513/*!
514 \property QAction::shortcutContext
515 \brief the context for the action's shortcut
516
517 Valid values for this property can be found in \l Qt::ShortcutContext.
518 The default value is Qt::WindowShortcut.
519*/
520void QAction::setShortcutContext(Qt::ShortcutContext context)
521{
522 Q_D(QAction);
523 if (d->shortcutContext == context)
524 return;
525 QAPP_CHECK("setShortcutContext");
526 d->shortcutContext = context;
527 d->redoGrab(qApp->d_func()->shortcutMap);
528 d->redoGrabAlternate(qApp->d_func()->shortcutMap);
529 d->sendDataChanged();
530}
531
532Qt::ShortcutContext QAction::shortcutContext() const
533{
534 Q_D(const QAction);
535 return d->shortcutContext;
536}
537
538/*!
539 \property QAction::autoRepeat
540 \brief whether the action can auto repeat
541 \since 4.2
542
543 If true, the action will auto repeat when the keyboard shortcut
544 combination is held down, provided that keyboard auto repeat is
545 enabled on the system.
546 The default value is true.
547*/
548void QAction::setAutoRepeat(bool on)
549{
550 Q_D(QAction);
551 if (d->autorepeat == on)
552 return;
553 QAPP_CHECK("setAutoRepeat");
554 d->autorepeat = on;
555 d->redoGrab(qApp->d_func()->shortcutMap);
556 d->redoGrabAlternate(qApp->d_func()->shortcutMap);
557 d->sendDataChanged();
558}
559
560bool QAction::autoRepeat() const
561{
562 Q_D(const QAction);
563 return d->autorepeat;
564}
565#endif // QT_NO_SHORTCUT
566
567/*!
568 \property QAction::font
569 \brief the action's font
570
571 The font property is used to render the text set on the
572 QAction. The font will can be considered a hint as it will not be
573 consulted in all cases based upon application and style.
574
575 By default, this property contains the application's default font.
576
577 \sa QAction::setText() QStyle
578*/
579void QAction::setFont(const QFont &font)
580{
581 Q_D(QAction);
582 if (d->font == font)
583 return;
584
585 d->fontSet = true;
586 d->font = font;
587 d->sendDataChanged();
588}
589
590QFont QAction::font() const
591{
592 Q_D(const QAction);
593 return d->font;
594}
595
596#ifdef QT3_SUPPORT
597/*!
598 Use one of the QAction constructors that doesn't take a \a name
599 argument and call setObjectName() instead.
600*/
601QAction::QAction(QObject* parent, const char* name)
602 : QObject(*(new QActionPrivate), parent)
603{
604 Q_D(QAction);
605 setObjectName(QString::fromAscii(name));
606 d->group = qobject_cast<QActionGroup *>(parent);
607 if (d->group)
608 d->group->addAction(this);
609}
610
611
612/*!
613 Use one of the QAction constructors that doesn't take a \a name
614 argument and call setObjectName() instead.
615*/
616QAction::QAction(const QString &text, const QKeySequence &shortcut, QObject* parent, const char* name)
617 : QObject(*(new QActionPrivate), parent)
618{
619 Q_D(QAction);
620 setObjectName(QString::fromAscii(name));
621 d->text = text;
622 setShortcut(shortcut);
623 d->group = qobject_cast<QActionGroup *>(parent);
624 if (d->group)
625 d->group->addAction(this);
626}
627
628/*!
629 Use one of the QAction constructors that doesn't take a \a name
630 argument and call setObjectName() instead.
631*/
632QAction::QAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut,
633 QObject* parent, const char* name)
634 : QObject(*(new QActionPrivate), parent)
635{
636 Q_D(QAction);
637 setObjectName(QString::fromAscii(name));
638 d->text = text;
639 setShortcut(shortcut);
640 d->icon = icon;
641 d->group = qobject_cast<QActionGroup *>(parent);
642 if (d->group)
643 d->group->addAction(this);
644}
645#endif
646
647/*!
648 Destroys the object and frees allocated resources.
649*/
650QAction::~QAction()
651{
652 Q_D(QAction);
653 for (int i = d->widgets.size()-1; i >= 0; --i) {
654 QWidget *w = d->widgets.at(i);
655 w->removeAction(this);
656 }
657#ifndef QT_NO_GRAPHICSVIEW
658 for (int i = d->graphicsWidgets.size()-1; i >= 0; --i) {
659 QGraphicsWidget *w = d->graphicsWidgets.at(i);
660 w->removeAction(this);
661 }
662#endif
663 if (d->group)
664 d->group->removeAction(this);
665#ifndef QT_NO_SHORTCUT
666 if (d->shortcutId && qApp) {
667 qApp->d_func()->shortcutMap.removeShortcut(d->shortcutId, this);
668 for(int i = 0; i < d->alternateShortcutIds.count(); ++i) {
669 const int id = d->alternateShortcutIds.at(i);
670 qApp->d_func()->shortcutMap.removeShortcut(id, this);
671 }
672 }
673#endif
674}
675
676/*!
677 Sets this action group to \a group. The action will be automatically
678 added to the group's list of actions.
679
680 Actions within the group will be mutually exclusive.
681
682 \sa QActionGroup, QAction::actionGroup()
683*/
684void QAction::setActionGroup(QActionGroup *group)
685{
686 Q_D(QAction);
687 if(group == d->group)
688 return;
689
690 if(d->group)
691 d->group->removeAction(this);
692 d->group = group;
693 if(group)
694 group->addAction(this);
695}
696
697/*!
698 Returns the action group for this action. If no action group manages
699 this action then 0 will be returned.
700
701 \sa QActionGroup, QAction::setActionGroup()
702*/
703QActionGroup *QAction::actionGroup() const
704{
705 Q_D(const QAction);
706 return d->group;
707}
708
709
710/*!
711 \property QAction::icon
712 \brief the action's icon
713
714 In toolbars, the icon is used as the tool button icon; in menus,
715 it is displayed to the left of the menu text. There is no default
716 icon.
717
718 If a null icon (QIcon::isNull() is passed into this function,
719 the icon of the action is cleared.
720*/
721void QAction::setIcon(const QIcon &icon)
722{
723 Q_D(QAction);
724 d->icon = icon;
725 d->sendDataChanged();
726}
727
728QIcon QAction::icon() const
729{
730 Q_D(const QAction);
731 return d->icon;
732}
733
734#ifndef QT_NO_MENU
735/*!
736 Returns the menu contained by this action. Actions that contain
737 menus can be used to create menu items with submenus, or inserted
738 into toolbars to create buttons with popup menus.
739
740 \sa QMenu::addAction()
741*/
742QMenu *QAction::menu() const
743{
744 Q_D(const QAction);
745 return d->menu;
746}
747
748/*!
749 Sets the menu contained by this action to the specified \a menu.
750*/
751void QAction::setMenu(QMenu *menu)
752{
753 Q_D(QAction);
754 if (d->menu)
755 d->menu->d_func()->setOverrideMenuAction(0); //we reset the default action of any previous menu
756 d->menu = menu;
757 if (menu)
758 menu->d_func()->setOverrideMenuAction(this);
759 d->sendDataChanged();
760}
761#endif // QT_NO_MENU
762
763/*!
764 If \a b is true then this action will be considered a separator.
765
766 How a separator is represented depends on the widget it is inserted
767 into. Under most circumstances the text, submenu, and icon will be
768 ignored for separator actions.
769
770 \sa QAction::isSeparator()
771*/
772void QAction::setSeparator(bool b)
773{
774 Q_D(QAction);
775 if (d->separator == b)
776 return;
777
778 d->separator = b;
779 d->sendDataChanged();
780}
781
782/*!
783 Returns true if this action is a separator action; otherwise it
784 returns false.
785
786 \sa QAction::setSeparator()
787*/
788bool QAction::isSeparator() const
789{
790 Q_D(const QAction);
791 return d->separator;
792}
793
794/*!
795 \property QAction::text
796 \brief the action's descriptive text
797
798 If the action is added to a menu, the menu option will consist of
799 the icon (if there is one), the text, and the shortcut (if there
800 is one). If the text is not explicitly set in the constructor, or
801 by using setText(), the action's description icon text will be
802 used as text. There is no default text.
803
804 \sa iconText
805*/
806void QAction::setText(const QString &text)
807{
808 Q_D(QAction);
809 if (d->text == text)
810 return;
811
812 d->text = text;
813 d->sendDataChanged();
814}
815
816QString QAction::text() const
817{
818 Q_D(const QAction);
819 QString s = d->text;
820 if(s.isEmpty()) {
821 s = d->iconText;
822 s.replace(QLatin1Char('&'), QLatin1String("&&"));
823 }
824 return s;
825}
826
827
828
829
830
831/*!
832 \property QAction::iconText
833 \brief the action's descriptive icon text
834
835 If QToolBar::toolButtonStyle is set to a value that permits text to
836 be displayed, the text defined held in this property appears as a
837 label in the relevant tool button.
838
839 It also serves as the default text in menus and tooltips if the action
840 has not been defined with setText() or setToolTip(), and will
841 also be used in toolbar buttons if no icon has been defined using setIcon().
842
843 If the icon text is not explicitly set, the action's normal text will be
844 used for the icon text.
845
846 By default, this property contains an empty string.
847
848 \sa setToolTip(), setStatusTip()
849*/
850void QAction::setIconText(const QString &text)
851{
852 Q_D(QAction);
853 if (d->iconText == text)
854 return;
855
856 d->iconText = text;
857 d->sendDataChanged();
858}
859
860QString QAction::iconText() const
861{
862 Q_D(const QAction);
863 if (d->iconText.isEmpty())
864 return qt_strippedText(d->text);
865 return d->iconText;
866}
867
868/*!
869 \property QAction::toolTip
870 \brief the action's tooltip
871
872 This text is used for the tooltip. If no tooltip is specified,
873 the action's text is used.
874
875 By default, this property contains the action's text.
876
877 \sa setStatusTip() setShortcut()
878*/
879void QAction::setToolTip(const QString &tooltip)
880{
881 Q_D(QAction);
882 if (d->tooltip == tooltip)
883 return;
884
885 d->tooltip = tooltip;
886 d->sendDataChanged();
887}
888
889QString QAction::toolTip() const
890{
891 Q_D(const QAction);
892 if (d->tooltip.isEmpty()) {
893 if (!d->text.isEmpty())
894 return qt_strippedText(d->text);
895 return qt_strippedText(d->iconText);
896 }
897 return d->tooltip;
898}
899
900/*!
901 \property QAction::statusTip
902 \brief the action's status tip
903
904 The status tip is displayed on all status bars provided by the
905 action's top-level parent widget.
906
907 By default, this property contains an empty string.
908
909 \sa setToolTip() showStatusText()
910*/
911void QAction::setStatusTip(const QString &statustip)
912{
913 Q_D(QAction);
914 if (d->statustip == statustip)
915 return;
916
917 d->statustip = statustip;
918 d->sendDataChanged();
919}
920
921QString QAction::statusTip() const
922{
923 Q_D(const QAction);
924 return d->statustip;
925}
926
927/*!
928 \property QAction::whatsThis
929 \brief the action's "What's This?" help text
930
931 The "What's This?" text is used to provide a brief description of
932 the action. The text may contain rich text. There is no default
933 "What's This?" text.
934
935 \sa QWhatsThis Q3StyleSheet
936*/
937void QAction::setWhatsThis(const QString &whatsthis)
938{
939 Q_D(QAction);
940 if (d->whatsthis == whatsthis)
941 return;
942
943 d->whatsthis = whatsthis;
944 d->sendDataChanged();
945}
946
947QString QAction::whatsThis() const
948{
949 Q_D(const QAction);
950 return d->whatsthis;
951}
952
953/*!
954 \enum QAction::Priority
955 \since 4.6
956
957 This enum defines priorities for actions in user interface.
958
959 \value LowPriority The action should not be prioritized in
960 the user interface.
961
962 \value NormalPriority
963
964 \value HighPriority The action should be prioritized in
965 the user interface.
966
967 \sa priority
968*/
969
970
971/*!
972 \property QAction::priority
973 \since 4.6
974
975 \brief the actions's priority in the user interface.
976
977 This property can be set to indicate how the action should be prioritized
978 in the user interface.
979
980 For instance, when toolbars have the Qt::ToolButtonTextBesideIcon
981 mode set, then actions with LowPriority will not show the text
982 labels.
983*/
984void QAction::setPriority(Priority priority)
985{
986 Q_D(QAction);
987 if (d->priority == priority)
988 return;
989
990 d->priority = priority;
991 d->sendDataChanged();
992}
993
994QAction::Priority QAction::priority() const
995{
996 Q_D(const QAction);
997 return d->priority;
998}
999
1000/*!
1001 \property QAction::checkable
1002 \brief whether the action is a checkable action
1003
1004 A checkable action is one which has an on/off state. For example,
1005 in a word processor, a Bold toolbar button may be either on or
1006 off. An action which is not a toggle action is a command action;
1007 a command action is simply executed, e.g. file save.
1008 By default, this property is false.
1009
1010 In some situations, the state of one toggle action should depend
1011 on the state of others. For example, "Left Align", "Center" and
1012 "Right Align" toggle actions are mutually exclusive. To achieve
1013 exclusive toggling, add the relevant toggle actions to a
1014 QActionGroup with the QActionGroup::exclusive property set to
1015 true.
1016
1017 \sa QAction::setChecked()
1018*/
1019void QAction::setCheckable(bool b)
1020{
1021 Q_D(QAction);
1022 if (d->checkable == b)
1023 return;
1024
1025 d->checkable = b;
1026 d->checked = false;
1027 d->sendDataChanged();
1028}
1029
1030bool QAction::isCheckable() const
1031{
1032 Q_D(const QAction);
1033 return d->checkable;
1034}
1035
1036/*!
1037 \fn void QAction::toggle()
1038
1039 This is a convenience function for the \l checked property.
1040 Connect to it to change the checked state to its opposite state.
1041*/
1042void QAction::toggle()
1043{
1044 Q_D(QAction);
1045 setChecked(!d->checked);
1046}
1047
1048/*!
1049 \property QAction::checked
1050 \brief whether the action is checked.
1051
1052 Only checkable actions can be checked. By default, this is false
1053 (the action is unchecked).
1054
1055 \sa checkable
1056*/
1057void QAction::setChecked(bool b)
1058{
1059 Q_D(QAction);
1060 if (!d->checkable || d->checked == b)
1061 return;
1062
1063 QPointer<QAction> guard(this);
1064 d->checked = b;
1065 d->sendDataChanged();
1066 if (guard)
1067 emit toggled(b);
1068}
1069
1070bool QAction::isChecked() const
1071{
1072 Q_D(const QAction);
1073 return d->checked;
1074}
1075
1076/*!
1077 \fn void QAction::setDisabled(bool b)
1078
1079 This is a convenience function for the \l enabled property, that
1080 is useful for signals--slots connections. If \a b is true the
1081 action is disabled; otherwise it is enabled.
1082*/
1083
1084/*!
1085 \property QAction::enabled
1086 \brief whether the action is enabled
1087
1088 Disabled actions cannot be chosen by the user. They do not
1089 disappear from menus or toolbars, but they are displayed in a way
1090 which indicates that they are unavailable. For example, they might
1091 be displayed using only shades of gray.
1092
1093 \gui{What's This?} help on disabled actions is still available, provided
1094 that the QAction::whatsThis property is set.
1095
1096 An action will be disabled when all widgets to which it is added
1097 (with QWidget::addAction()) are disabled or not visible. When an
1098 action is disabled, it is not possible to trigger it through its
1099 shortcut.
1100
1101 By default, this property is true (actions are enabled).
1102
1103 \sa text
1104*/
1105void QAction::setEnabled(bool b)
1106{
1107 Q_D(QAction);
1108 if (b == d->enabled && b != d->forceDisabled)
1109 return;
1110 d->forceDisabled = !b;
1111 if (b && (!d->visible || (d->group && !d->group->isEnabled())))
1112 return;
1113 QAPP_CHECK("setEnabled");
1114 d->enabled = b;
1115#ifndef QT_NO_SHORTCUT
1116 d->setShortcutEnabled(b, qApp->d_func()->shortcutMap);
1117#endif
1118 d->sendDataChanged();
1119}
1120
1121bool QAction::isEnabled() const
1122{
1123 Q_D(const QAction);
1124 return d->enabled;
1125}
1126
1127/*!
1128 \property QAction::visible
1129 \brief whether the action can be seen (e.g. in menus and toolbars)
1130
1131 If \e visible is true the action can be seen (e.g. in menus and
1132 toolbars) and chosen by the user; if \e visible is false the
1133 action cannot be seen or chosen by the user.
1134
1135 Actions which are not visible are \e not grayed out; they do not
1136 appear at all.
1137
1138 By default, this property is true (actions are visible).
1139*/
1140void QAction::setVisible(bool b)
1141{
1142 Q_D(QAction);
1143 if (b == d->visible && b != d->forceInvisible)
1144 return;
1145 QAPP_CHECK("setVisible");
1146 d->forceInvisible = !b;
1147 d->visible = b;
1148 d->enabled = b && !d->forceDisabled && (!d->group || d->group->isEnabled()) ;
1149#ifndef QT_NO_SHORTCUT
1150 d->setShortcutEnabled(d->enabled, qApp->d_func()->shortcutMap);
1151#endif
1152 d->sendDataChanged();
1153}
1154
1155
1156bool QAction::isVisible() const
1157{
1158 Q_D(const QAction);
1159 return d->visible;
1160}
1161
1162/*!
1163 \reimp
1164*/
1165bool
1166QAction::event(QEvent *e)
1167{
1168#ifndef QT_NO_SHORTCUT
1169 if (e->type() == QEvent::Shortcut) {
1170 QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
1171 Q_ASSERT_X(se->key() == d_func()->shortcut || d_func()->alternateShortcuts.contains(se->key()),
1172 "QAction::event",
1173 "Received shortcut event from incorrect shortcut");
1174 if (se->isAmbiguous())
1175 qWarning("QAction::eventFilter: Ambiguous shortcut overload: %s", QString(se->key()).toLatin1().constData());
1176 else
1177 activate(Trigger);
1178 return true;
1179 }
1180#endif
1181 return QObject::event(e);
1182}
1183
1184/*!
1185 Returns the user data as set in QAction::setData.
1186
1187 \sa setData()
1188*/
1189QVariant
1190QAction::data() const
1191{
1192 Q_D(const QAction);
1193 return d->userData;
1194}
1195
1196/*!
1197 \fn void QAction::setData(const QVariant &userData)
1198
1199 Sets the action's internal data to the given \a userData.
1200
1201 \sa data()
1202*/
1203void
1204QAction::setData(const QVariant &data)
1205{
1206 Q_D(QAction);
1207 d->userData = data;
1208 d->sendDataChanged();
1209}
1210
1211
1212/*!
1213 Updates the relevant status bar for the \a widget specified by sending a
1214 QStatusTipEvent to its parent widget. Returns true if an event was sent;
1215 otherwise returns false.
1216
1217 If a null widget is specified, the event is sent to the action's parent.
1218
1219 \sa statusTip
1220*/
1221bool
1222QAction::showStatusText(QWidget *widget)
1223{
1224 return d_func()->showStatusText(widget, statusTip());
1225}
1226
1227/*!
1228 Sends the relevant signals for ActionEvent \a event.
1229
1230 Action based widgets use this API to cause the QAction
1231 to emit signals as well as emitting their own.
1232*/
1233void QAction::activate(ActionEvent event)
1234{
1235 Q_D(QAction);
1236 if(event == Trigger) {
1237 QObject *guard = this;
1238 QMetaObject::addGuard(&guard);
1239 if(d->checkable) {
1240 // the checked action of an exclusive group cannot be unchecked
1241 if (d->checked && (d->group && d->group->isExclusive()
1242 && d->group->checkedAction() == this)) {
1243 if (guard)
1244 emit triggered(true);
1245 QMetaObject::removeGuard(&guard);
1246 return;
1247 }
1248 setChecked(!d->checked);
1249 }
1250 if (guard)
1251 emit triggered(d->checked);
1252#ifdef QT3_SUPPORT
1253 if (guard)
1254 emit activated(d->param);
1255#endif
1256 QMetaObject::removeGuard(&guard);
1257 } else if(event == Hover) {
1258 emit hovered();
1259 }
1260}
1261
1262/*!
1263 \fn void QAction::triggered(bool checked)
1264
1265 This signal is emitted when an action is activated by the user;
1266 for example, when the user clicks a menu option, toolbar button,
1267 or presses an action's shortcut key combination, or when trigger()
1268 was called. Notably, it is \e not emitted when setChecked() or
1269 toggle() is called.
1270
1271 If the action is checkable, \a checked is true if the action is
1272 checked, or false if the action is unchecked.
1273
1274 \sa QAction::activate(), QAction::toggled(), checked
1275*/
1276
1277/*!
1278 \fn void QAction::toggled(bool checked)
1279
1280 This signal is emitted whenever a checkable action changes its
1281 isChecked() status. This can be the result of a user interaction,
1282 or because setChecked() was called.
1283
1284 \a checked is true if the action is checked, or false if the
1285 action is unchecked.
1286
1287 \sa QAction::activate(), QAction::triggered(), checked
1288*/
1289
1290/*!
1291 \fn void QAction::hovered()
1292
1293 This signal is emitted when an action is highlighted by the user;
1294 for example, when the user pauses with the cursor over a menu option,
1295 toolbar button, or presses an action's shortcut key combination.
1296
1297 \sa QAction::activate()
1298*/
1299
1300/*!
1301 \fn void QAction::changed()
1302
1303 This signal is emitted when an action has changed. If you
1304 are only interested in actions in a given widget, you can
1305 watch for QWidget::actionEvent() sent with an
1306 QEvent::ActionChanged.
1307
1308 \sa QWidget::actionEvent()
1309*/
1310
1311/*!
1312 \enum QAction::ActionEvent
1313
1314 This enum type is used when calling QAction::activate()
1315
1316 \value Trigger this will cause the QAction::triggered() signal to be emitted.
1317
1318 \value Hover this will cause the QAction::hovered() signal to be emitted.
1319*/
1320
1321/*!
1322 \fn void QAction::setMenuText(const QString &text)
1323
1324 Use setText() instead.
1325*/
1326
1327/*!
1328 \fn QString QAction::menuText() const
1329
1330 Use text() instead.
1331*/
1332
1333/*!
1334 \fn bool QAction::isOn() const
1335
1336 Use isChecked() instead.
1337*/
1338
1339/*!
1340 \fn void QAction::setOn(bool b)
1341
1342 Use setChecked() instead.
1343*/
1344
1345/*!
1346 \fn bool QAction::isToggleAction() const
1347
1348 Use isCheckable() instead.
1349*/
1350
1351/*!
1352 \fn void QAction::setToggleAction(bool b)
1353
1354 Use setCheckable() instead.
1355*/
1356
1357/*!
1358 \fn void QAction::setIconSet(const QIcon &i)
1359
1360 Use setIcon() instead.
1361*/
1362
1363/*!
1364 \fn bool QAction::addTo(QWidget *w)
1365
1366 Use QWidget::addAction() instead.
1367
1368 \oldcode
1369 action->addTo(widget);
1370 \newcode
1371 widget->addAction(action);
1372 \endcode
1373*/
1374
1375/*!
1376 \fn bool QAction::removeFrom(QWidget *w)
1377
1378 Use QWidget::removeAction() instead.
1379
1380 \oldcode
1381 action->removeFrom(widget);
1382 \newcode
1383 widget->removeAction(action);
1384 \endcode
1385*/
1386
1387/*!
1388 \fn void QAction::setAccel(const QKeySequence &shortcut)
1389
1390 Use setShortcut() instead.
1391*/
1392
1393/*!
1394 \fn QIcon QAction::iconSet() const
1395
1396 Use icon() instead.
1397*/
1398
1399/*!
1400 \fn QKeySequence QAction::accel() const
1401
1402 Use shortcut() instead.
1403*/
1404
1405/*!
1406 \fn void QAction::activated(int i);
1407
1408 Use triggered() instead.
1409*/
1410
1411
1412/*!
1413 \property QAction::menuRole
1414 \brief the action's menu role
1415 \since 4.2
1416
1417 This indicates what role the action serves in the application menu on Mac
1418 OS X. By default all action have the TextHeuristicRole, which means that
1419 the action is added based on its text (see QMenuBar for more information).
1420
1421 The menu role can only be changed before the actions are put into the menu
1422 bar in Mac OS X (usually just before the first application window is
1423 shown).
1424*/
1425void QAction::setMenuRole(MenuRole menuRole)
1426{
1427 Q_D(QAction);
1428 if (d->menuRole == menuRole)
1429 return;
1430
1431 d->menuRole = menuRole;
1432 d->sendDataChanged();
1433}
1434
1435QAction::MenuRole QAction::menuRole() const
1436{
1437 Q_D(const QAction);
1438 return d->menuRole;
1439}
1440
1441/*!
1442 \property QAction::softKeyRole
1443 \brief the action's softkey role
1444 \since 4.6
1445
1446 This indicates what type of role this action describes in the softkey framework
1447 on platforms where such a framework is supported. Currently this is only
1448 supported on the Symbian platform.
1449
1450 The softkey role can be changed any time.
1451*/
1452void QAction::setSoftKeyRole(SoftKeyRole softKeyRole)
1453{
1454 Q_D(QAction);
1455 if (d->softKeyRole == softKeyRole)
1456 return;
1457
1458 d->softKeyRole = softKeyRole;
1459 d->sendDataChanged();
1460}
1461
1462QAction::SoftKeyRole QAction::softKeyRole() const
1463{
1464 Q_D(const QAction);
1465 return d->softKeyRole;
1466}
1467
1468/*!
1469 \property QAction::iconVisibleInMenu
1470 \brief Whether or not an action should show an icon in a menu
1471 \since 4.4
1472
1473 In some applications, it may make sense to have actions with icons in the
1474 toolbar, but not in menus. If true, the icon (if valid) is shown in the menu, when it
1475 is false, it is not shown.
1476
1477 The default is to follow whether the Qt::AA_DontShowIconsInMenus attribute
1478 is set for the application. Explicitly settings this property overrides
1479 the presence (or abscence) of the attribute.
1480
1481 For example:
1482 \snippet doc/src/snippets/code/src_gui_kernel_qaction.cpp 0
1483
1484 \sa QAction::icon QApplication::setAttribute()
1485*/
1486void QAction::setIconVisibleInMenu(bool visible)
1487{
1488 Q_D(QAction);
1489 if (d->iconVisibleInMenu == -1 || visible != bool(d->iconVisibleInMenu)) {
1490 int oldValue = d->iconVisibleInMenu;
1491 d->iconVisibleInMenu = visible;
1492 // Only send data changed if we really need to.
1493 if (oldValue != -1
1494 || (oldValue == -1
1495 && visible == !QApplication::instance()->testAttribute(Qt::AA_DontShowIconsInMenus))) {
1496 d->sendDataChanged();
1497 }
1498 }
1499}
1500
1501bool QAction::isIconVisibleInMenu() const
1502{
1503 Q_D(const QAction);
1504 if (d->iconVisibleInMenu == -1) {
1505 return !QApplication::instance()->testAttribute(Qt::AA_DontShowIconsInMenus);
1506 }
1507 return d->iconVisibleInMenu;
1508}
1509
1510QT_END_NAMESPACE
1511
1512#include "moc_qaction.cpp"
1513
1514#endif // QT_NO_ACTION
Note: See TracBrowser for help on using the repository browser.