13#if QT_CONFIG(shortcut)
14# include <private/qshortcutmap_p.h>
16#include <private/qguiapplication_p.h>
17#include <private/qdebug_p.h>
19#define QAPP_CHECK(functionName)
20 if (Q_UNLIKELY(!QCoreApplication::instance())) {
21 qWarning("QAction: Initialize Q(Gui)Application before calling '" functionName "'.");
27using namespace Qt::StringLiterals;
30
31
34 for (qsizetype i = 0; i < s.size(); ++i) {
35 const QChar &c = s.at(i);
36 if (c == u'&' || c == u'\x2026')
38 else if (i + 2 < s.size() && c == u'.' && s.at(i + 1) == u'.' && s.at(i + 2) == u'.')
44QActionPrivate *QGuiApplicationPrivate::createActionPrivate()
const
46 return new QActionPrivate;
49QActionPrivate::QActionPrivate() :
50#if QT_CONFIG(shortcut)
53 enabled(1), explicitEnabled(0), explicitEnabledValue(1), visible(1), forceInvisible(0), checkable(0),
54 checked(0), separator(0), fontSet(
false),
55 iconVisibleInMenu(-1), shortcutVisibleInContextMenu(-1)
59#if QT_CONFIG(shortcut)
60static bool dummy(QObject *, Qt::ShortcutContext) {
return false; }
62QShortcutMap::ContextMatcher QActionPrivate::contextMatcher()
const
68QActionPrivate::~QActionPrivate() =
default;
70void QActionPrivate::destroy()
74void QActionPrivate::sendDataChanged()
77 QActionEvent e(QEvent::ActionChanged, q);
78 QCoreApplication::sendEvent(q, &e);
83#if QT_CONFIG(shortcut)
84void QActionPrivate::redoGrab(QShortcutMap &map)
87 for (
int id : std::as_const(shortcutIds)) {
89 map.removeShortcut(id, q);
93 for (
const QKeySequence &shortcut : std::as_const(shortcuts)) {
94 if (!shortcut.isEmpty())
95 shortcutIds.append(map.addShortcut(q, shortcut, shortcutContext, contextMatcher()));
97 shortcutIds.append(0);
100 for (
int id : std::as_const(shortcutIds)) {
102 map.setShortcutEnabled(
false, id, q);
106 for (
int id : std::as_const(shortcutIds)) {
108 map.setShortcutAutoRepeat(
false, id, q);
113void QActionPrivate::setShortcutEnabled(
bool enable, QShortcutMap &map)
116 for (
int id : std::as_const(shortcutIds)) {
118 map.setShortcutEnabled(enable, id, q);
123bool QActionPrivate::showStatusText(QObject *object,
const QString &str)
125 if (QObject *receiver = object ? object : parent) {
126 QStatusTipEvent tip(str);
127 QCoreApplication::sendEvent(receiver, &tip);
133void QActionPrivate::setMenu(QObject *)
137QObject *QActionPrivate::menu()
const
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
194
195
196
197
200
201
202
203
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
228
229
230
231
232
233QAction::QAction(QObject *parent)
234 : QAction(*QGuiApplicationPrivate::instance()->createActionPrivate(), parent)
239
240
241
242
243
244
245
246
247
248
249QAction::QAction(
const QString &text, QObject *parent)
257
258
259
260
261
262
263
264
265
266
267QAction::QAction(
const QIcon &icon,
const QString &text, QObject *parent)
268 : QAction(text, parent)
275
276
277QAction::QAction(QActionPrivate &dd, QObject *parent)
278 : QObject(dd, parent)
281 d->group = qobject_cast<QActionGroup *>(parent);
283 d->group->addAction(
this);
286#if QT_CONFIG(shortcut)
288
289
290
291
292
293
296
297
298
299
300void QAction::setShortcut(
const QKeySequence &shortcut)
302 if (shortcut.isEmpty())
305 setShortcuts({ shortcut });
309
310
311
312
313
314void QAction::setShortcuts(
const QList<QKeySequence> &shortcuts)
316 QAPP_CHECK(
"setShortcuts");
319 if (d->shortcuts == shortcuts)
322 d->shortcuts = shortcuts;
323 d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
324 d->sendDataChanged();
328
329
330
331
332
333
334
335void QAction::setShortcuts(QKeySequence::StandardKey key)
337 QList <QKeySequence> list = QKeySequence::keyBindings(key);
342
343
344
345
346QKeySequence QAction::shortcut()
const
349 if (d->shortcuts.isEmpty())
350 return QKeySequence();
351 return d->shortcuts.first();
355
356
357
358
359
360QList<QKeySequence> QAction::shortcuts()
const
367
368
369
370
371
372
373void QAction::setShortcutContext(Qt::ShortcutContext context)
376 if (d->shortcutContext == context)
378 QAPP_CHECK(
"setShortcutContext");
379 d->shortcutContext = context;
380 d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
381 d->sendDataChanged();
384Qt::ShortcutContext QAction::shortcutContext()
const
387 return d->shortcutContext;
391
392
393
394
395
396
397
398
399void QAction::setAutoRepeat(
bool on)
402 if (d->autorepeat == on)
404 QAPP_CHECK(
"setAutoRepeat");
406 d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
407 d->sendDataChanged();
410bool QAction::autoRepeat()
const
413 return d->autorepeat;
418
419
420
421
422
423
424
425
426
427
428
429void QAction::setFont(
const QFont &font)
437 d->sendDataChanged();
440QFont QAction::font()
const
448
449
457 d->group->removeAction(
this);
458#if QT_CONFIG(shortcut)
460 for (
int id : std::as_const(d->shortcutIds)) {
462 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(id,
this);
469
470
471
472
473
474
475
476void QAction::setActionGroup(QActionGroup *group)
479 if (group == d->group)
483 d->group->removeAction(
this);
486 group->addAction(
this);
487 d->sendDataChanged();
491
492
493
494
495
496QActionGroup *QAction::actionGroup()
const
503
504
505
506
507
508QList<QObject*> QAction::associatedObjects()
const
511 return d->associatedObjects;
515
516
517
518
519
522
523
524
525
526
527
528
531
532
533
534
535
536
537
540
541
542
543
544
545
546
547
548
549
550
551
552void QAction::setIcon(
const QIcon &icon)
556 d->sendDataChanged();
559QIcon QAction::icon()
const
566
567
568
569
570
571
572
573
574void QAction::setSeparator(
bool b)
577 if (d->separator == b)
581 d->sendDataChanged();
585
586
587
588
589
590bool QAction::isSeparator()
const
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616void QAction::setText(
const QString &text)
623 d->sendDataChanged();
626QString QAction::text()
const
632 s.replace(u'&',
"&&"_L1);
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656void QAction::setIconText(
const QString &text)
659 if (d->iconText == text)
663 d->sendDataChanged();
666QString QAction::iconText()
const
669 if (d->iconText.isEmpty())
670 return qt_strippedText(d->text);
675
676
677
678
679
680
681
682
683
684
685void QAction::setToolTip(
const QString &tooltip)
688 if (d->tooltip == tooltip)
691 d->tooltip = tooltip;
692 d->sendDataChanged();
695QString QAction::toolTip()
const
698 if (d->tooltip.isEmpty()) {
699 if (!d->text.isEmpty())
700 return qt_strippedText(d->text);
701 return qt_strippedText(d->iconText);
707
708
709
710
711
712
713
714
715
716
717void QAction::setStatusTip(
const QString &statustip)
720 if (d->statustip == statustip)
723 d->statustip = statustip;
724 d->sendDataChanged();
727QString QAction::statusTip()
const
734
735
736
737
738
739
740
741bool QAction::showStatusText(QObject *object)
744 return d->showStatusText(object, statusTip());
748
749
750
751
752
753
754
755
756
757void QAction::setWhatsThis(
const QString &whatsthis)
760 if (d->whatsthis == whatsthis)
763 d->whatsthis = whatsthis;
764 d->sendDataChanged();
767QString QAction::whatsThis()
const
774
775
776
777
778
779
780
781
782
783
784
785
786
787
791
792
793
794
795
796
797
798
799
800
801
802void QAction::setPriority(Priority priority)
805 if (d->priority == priority)
808 d->priority = priority;
809 d->sendDataChanged();
812QAction::Priority QAction::priority()
const
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837void QAction::setCheckable(
bool b)
840 if (d->checkable == b)
844 QPointer<QAction> guard(
this);
845 d->sendDataChanged();
847 emit checkableChanged(b);
848 if (guard && d->checked)
852bool QAction::isCheckable()
const
859
860
861
862
863
864void QAction::toggle()
867 setChecked(!d->checked);
871
872
873
874
875
876
877
878
879
880
881
882void QAction::setChecked(
bool b)
891 QPointer<QAction> guard(
this);
892 d->sendDataChanged();
897bool QAction::isChecked()
const
900 return d->checked && d->checkable;
904
905
906
907
908
909
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932void QAction::setEnabled(
bool b)
935 if (d->explicitEnabledValue == b && d->explicitEnabled)
937 d->explicitEnabledValue = b;
938 d->explicitEnabled =
true;
940 d->setEnabled(b,
false);
943bool QActionPrivate::setEnabled(
bool b,
bool byGroup)
948 if (b && !byGroup && (group && !group->isEnabled()))
950 if (b && byGroup && explicitEnabled)
951 b = explicitEnabledValue;
957#if QT_CONFIG(shortcut)
958 setShortcutEnabled(b, QGuiApplicationPrivate::instance()->shortcutMap);
963 emit q->enabledChanged(b);
967void QAction::resetEnabled()
970 if (!d->explicitEnabled)
972 d->explicitEnabled =
false;
973 d->setEnabled(
true,
false);
976bool QAction::isEnabled()
const
983
984
985
986
987
988
989
990
991
992
993
994
995void QAction::setVisible(
bool b)
998 if (b != d->forceInvisible)
1000 d->forceInvisible = !b;
1001 if (b && d->group && !d->group->isVisible())
1006void QActionPrivate::setVisible(
bool b)
1013 bool enable = visible;
1014 if (enable && explicitEnabled)
1015 enable = explicitEnabledValue;
1017 if (!setEnabled(enable,
false))
1020 emit q->visibleChanged();
1023bool QAction::isVisible()
const
1030
1031
1032bool QAction::event(QEvent *e)
1035 if (e->type() == QEvent::ActionChanged) {
1036 for (
auto object : std::as_const(d->associatedObjects))
1037 QCoreApplication::sendEvent(object, e);
1040#if QT_CONFIG(shortcut)
1041 if (e->type() == QEvent::Shortcut) {
1042 QShortcutEvent *se =
static_cast<QShortcutEvent *>(e);
1043 Q_ASSERT_X(d_func()->shortcutIds.contains(se->shortcutId()),
1045 "Received shortcut event from incorrect shortcut");
1046 if (se->isAmbiguous())
1047 qWarning(
"QAction::event: Ambiguous shortcut overload: %s", se->key().toString(QKeySequence::NativeText).toLatin1().constData());
1053 return QObject::event(e);
1057
1058
1059
1060
1061QVariant QAction::data()
const
1068
1069
1070
1071
1072void QAction::setData(
const QVariant &data)
1075 if (d->userData == data)
1078 d->sendDataChanged();
1082
1083
1084
1085
1086
1087void QAction::activate(ActionEvent event)
1090 if (event == Trigger) {
1092 if ((d->explicitEnabled && !d->explicitEnabledValue) || (d->group && !d->group->isEnabled()))
1094 QPointer<QObject> guard =
this;
1097 if (d->checked && (d->group
1098 && d->group->exclusionPolicy() == QActionGroup::ExclusionPolicy::Exclusive
1099 && d->group->checkedAction() ==
this)) {
1100 if (!guard.isNull())
1101 emit triggered(
true);
1104 setChecked(!d->checked);
1106 if (!guard.isNull())
1107 emit triggered(d->checked);
1108 }
else if (event == Hover) {
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1143
1144
1145
1146
1147
1148
1149
1150
1153
1154
1155
1156
1157
1158
1159
1160
1161
1164
1165
1166
1167
1168
1169
1170
1171
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185void QAction::setMenuRole(MenuRole menuRole)
1188 if (d->menuRole == menuRole)
1191 d->menuRole = menuRole;
1192 d->sendDataChanged();
1195QAction::MenuRole QAction::menuRole()
const
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211QObject* QAction::menuObject()
const
1218
1219
1220
1221
1222void QAction::setMenuObject(QObject *object)
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245void QAction::setIconVisibleInMenu(
bool visible)
1248 if (d->iconVisibleInMenu == -1 || visible !=
bool(d->iconVisibleInMenu)) {
1249 int oldValue = d->iconVisibleInMenu;
1250 d->iconVisibleInMenu = visible;
1253 || visible == !QCoreApplication::testAttribute(Qt::AA_DontShowIconsInMenus)) {
1254 d->sendDataChanged();
1259bool QAction::isIconVisibleInMenu()
const
1262 if (d->iconVisibleInMenu == -1) {
1263 return !QCoreApplication::testAttribute(Qt::AA_DontShowIconsInMenus);
1265 return d->iconVisibleInMenu;
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281void QAction::setShortcutVisibleInContextMenu(
bool visible)
1284 if (d->shortcutVisibleInContextMenu == -1 || visible !=
bool(d->shortcutVisibleInContextMenu)) {
1285 int oldValue = d->shortcutVisibleInContextMenu;
1286 d->shortcutVisibleInContextMenu = visible;
1289 || visible == !QCoreApplication::testAttribute(Qt::AA_DontShowShortcutsInContextMenus)) {
1290 d->sendDataChanged();
1295bool QAction::isShortcutVisibleInContextMenu()
const
1298 if (d->shortcutVisibleInContextMenu == -1)
1299 return !QCoreApplication::testAttribute(Qt::AA_DontShowShortcutsInContextMenus);
1300 return d->shortcutVisibleInContextMenu;
1303#ifndef QT_NO_DEBUG_STREAM
1304Q_GUI_EXPORT
QDebug operator<<(QDebug d,
const QAction *action)
1306 QDebugStateSaver saver(d);
1308 d <<
"QAction(" <<
static_cast<
const void *>(action);
1310 d <<
" text=" << action->text();
1311 if (!action->toolTip().isEmpty())
1312 d <<
" toolTip=" << action->toolTip();
1313 if (action->isCheckable())
1314 d <<
" checked=" << action->isChecked();
1315#if QT_CONFIG(shortcut)
1316 if (!action->shortcuts().isEmpty())
1317 d <<
" shortcuts=" << action->shortcuts();
1320 QtDebugUtils::formatQEnum(d, action->menuRole());
1321 d <<
" enabled=" << action->isEnabled();
1322 d <<
" visible=" << action->isVisible();
1333#include "moc_qaction.cpp"
Combined button and popup list for selecting options.
#define QAPP_CHECK(functionName)
static QString qt_strippedText(QString s)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)