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

Last change on this file since 467 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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