12#if QT_CONFIG(shortcut)
13# include <private/qshortcutmap_p.h>
15#include <private/qguiapplication_p.h>
16#include <private/qdebug_p.h>
18#define QAPP_CHECK(functionName)
19 if (Q_UNLIKELY(!QCoreApplication::instance())) {
20 qWarning("QAction: Initialize Q(Gui)Application before calling '" functionName "'.");
26using namespace Qt::StringLiterals;
29
30
34 for (
int i = 0; i < s.size(); ++i) {
41QActionPrivate *QGuiApplicationPrivate::createActionPrivate()
const
43 return new QActionPrivate;
46QActionPrivate::QActionPrivate() :
47#if QT_CONFIG(shortcut)
50 enabled(1), explicitEnabled(0), explicitEnabledValue(1), visible(1), forceInvisible(0), checkable(0),
51 checked(0), separator(0), fontSet(
false),
52 iconVisibleInMenu(-1), shortcutVisibleInContextMenu(-1)
56#if QT_CONFIG(shortcut)
57static bool dummy(QObject *, Qt::ShortcutContext) {
return false; }
59QShortcutMap::ContextMatcher QActionPrivate::contextMatcher()
const
65QActionPrivate::~QActionPrivate() =
default;
67void QActionPrivate::destroy()
71void QActionPrivate::sendDataChanged()
74 QActionEvent e(QEvent::ActionChanged, q);
75 QCoreApplication::sendEvent(q, &e);
80#if QT_CONFIG(shortcut)
81void QActionPrivate::redoGrab(QShortcutMap &map)
84 for (
int id : std::as_const(shortcutIds)) {
86 map.removeShortcut(id, q);
90 for (
const QKeySequence &shortcut : std::as_const(shortcuts)) {
91 if (!shortcut.isEmpty())
92 shortcutIds.append(map.addShortcut(q, shortcut, shortcutContext, contextMatcher()));
94 shortcutIds.append(0);
97 for (
int id : std::as_const(shortcutIds)) {
99 map.setShortcutEnabled(
false, id, q);
103 for (
int id : std::as_const(shortcutIds)) {
105 map.setShortcutAutoRepeat(
false, id, q);
110void QActionPrivate::setShortcutEnabled(
bool enable, QShortcutMap &map)
113 for (
int id : std::as_const(shortcutIds)) {
115 map.setShortcutEnabled(enable, id, q);
120bool QActionPrivate::showStatusText(QObject *object,
const QString &str)
122 if (QObject *receiver = object ? object : parent) {
123 QStatusTipEvent tip(str);
124 QCoreApplication::sendEvent(receiver, &tip);
130void QActionPrivate::setMenu(QObject *)
134QObject *QActionPrivate::menu()
const
140
141
142
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
191
192
193
194
197
198
199
200
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
225
226
227
228
229
230QAction::QAction(QObject *parent)
231 : QAction(*QGuiApplicationPrivate::instance()->createActionPrivate(), parent)
236
237
238
239
240
241
242
243
244
245
246QAction::QAction(
const QString &text, QObject *parent)
254
255
256
257
258
259
260
261
262
263
264QAction::QAction(
const QIcon &icon,
const QString &text, QObject *parent)
265 : QAction(text, parent)
272
273
274QAction::QAction(QActionPrivate &dd, QObject *parent)
275 : QObject(dd, parent)
278 d->group = qobject_cast<QActionGroup *>(parent);
280 d->group->addAction(
this);
283#if QT_CONFIG(shortcut)
285
286
287
288
289
290
293
294
295
296
297void QAction::setShortcut(
const QKeySequence &shortcut)
299 if (shortcut.isEmpty())
302 setShortcuts({ shortcut });
306
307
308
309
310
311void QAction::setShortcuts(
const QList<QKeySequence> &shortcuts)
313 QAPP_CHECK(
"setShortcuts");
316 if (d->shortcuts == shortcuts)
319 d->shortcuts = shortcuts;
320 d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
321 d->sendDataChanged();
325
326
327
328
329
330
331
332void QAction::setShortcuts(QKeySequence::StandardKey key)
334 QList <QKeySequence> list = QKeySequence::keyBindings(key);
339
340
341
342
343QKeySequence QAction::shortcut()
const
346 if (d->shortcuts.isEmpty())
347 return QKeySequence();
348 return d->shortcuts.first();
352
353
354
355
356
357QList<QKeySequence> QAction::shortcuts()
const
364
365
366
367
368
369
370void QAction::setShortcutContext(Qt::ShortcutContext context)
373 if (d->shortcutContext == context)
375 QAPP_CHECK(
"setShortcutContext");
376 d->shortcutContext = context;
377 d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
378 d->sendDataChanged();
381Qt::ShortcutContext QAction::shortcutContext()
const
384 return d->shortcutContext;
388
389
390
391
392
393
394
395
396void QAction::setAutoRepeat(
bool on)
399 if (d->autorepeat == on)
401 QAPP_CHECK(
"setAutoRepeat");
403 d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
404 d->sendDataChanged();
407bool QAction::autoRepeat()
const
410 return d->autorepeat;
415
416
417
418
419
420
421
422
423
424
425
426void QAction::setFont(
const QFont &font)
434 d->sendDataChanged();
437QFont QAction::font()
const
445
446
454 d->group->removeAction(
this);
455#if QT_CONFIG(shortcut)
457 for (
int id : std::as_const(d->shortcutIds)) {
459 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(id,
this);
466
467
468
469
470
471
472
473void QAction::setActionGroup(QActionGroup *group)
476 if (group == d->group)
480 d->group->removeAction(
this);
483 group->addAction(
this);
484 d->sendDataChanged();
488
489
490
491
492
493QActionGroup *QAction::actionGroup()
const
500
501
502
503
504
505QList<QObject*> QAction::associatedObjects()
const
508 return d->associatedObjects;
512
513
514
515
516
519
520
521
522
523
524
525
528
529
530
531
532
533
534
537
538
539
540
541
542
543
544
545
546
547
548
549void QAction::setIcon(
const QIcon &icon)
553 d->sendDataChanged();
556QIcon QAction::icon()
const
563
564
565
566
567
568
569
570
571void QAction::setSeparator(
bool b)
574 if (d->separator == b)
578 d->sendDataChanged();
582
583
584
585
586
587bool QAction::isSeparator()
const
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613void QAction::setText(
const QString &text)
620 d->sendDataChanged();
623QString QAction::text()
const
629 s.replace(u'&',
"&&"_L1);
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653void QAction::setIconText(
const QString &text)
656 if (d->iconText == text)
660 d->sendDataChanged();
663QString QAction::iconText()
const
666 if (d->iconText.isEmpty())
667 return qt_strippedText(d->text);
672
673
674
675
676
677
678
679
680
681
682void QAction::setToolTip(
const QString &tooltip)
685 if (d->tooltip == tooltip)
688 d->tooltip = tooltip;
689 d->sendDataChanged();
692QString QAction::toolTip()
const
695 if (d->tooltip.isEmpty()) {
696 if (!d->text.isEmpty())
697 return qt_strippedText(d->text);
698 return qt_strippedText(d->iconText);
704
705
706
707
708
709
710
711
712
713
714void QAction::setStatusTip(
const QString &statustip)
717 if (d->statustip == statustip)
720 d->statustip = statustip;
721 d->sendDataChanged();
724QString QAction::statusTip()
const
731
732
733
734
735
736
737
738bool QAction::showStatusText(QObject *object)
741 return d->showStatusText(object, statusTip());
745
746
747
748
749
750
751
752
753
754void QAction::setWhatsThis(
const QString &whatsthis)
757 if (d->whatsthis == whatsthis)
760 d->whatsthis = whatsthis;
761 d->sendDataChanged();
764QString QAction::whatsThis()
const
771
772
773
774
775
776
777
778
779
780
781
782
783
784
788
789
790
791
792
793
794
795
796
797
798
799void QAction::setPriority(Priority priority)
802 if (d->priority == priority)
805 d->priority = priority;
806 d->sendDataChanged();
809QAction::Priority QAction::priority()
const
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834void QAction::setCheckable(
bool b)
837 if (d->checkable == b)
841 QPointer<QAction> guard(
this);
842 d->sendDataChanged();
844 emit checkableChanged(b);
845 if (guard && d->checked)
849bool QAction::isCheckable()
const
856
857
858
859
860
861void QAction::toggle()
864 setChecked(!d->checked);
868
869
870
871
872
873
874
875
876
877
878
879void QAction::setChecked(
bool b)
888 QPointer<QAction> guard(
this);
889 d->sendDataChanged();
894bool QAction::isChecked()
const
897 return d->checked && d->checkable;
901
902
903
904
905
906
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929void QAction::setEnabled(
bool b)
932 if (d->explicitEnabledValue == b && d->explicitEnabled)
934 d->explicitEnabledValue = b;
935 d->explicitEnabled =
true;
937 d->setEnabled(b,
false);
940bool QActionPrivate::setEnabled(
bool b,
bool byGroup)
945 if (b && !byGroup && (group && !group->isEnabled()))
947 if (b && byGroup && explicitEnabled)
948 b = explicitEnabledValue;
954#if QT_CONFIG(shortcut)
955 setShortcutEnabled(b, QGuiApplicationPrivate::instance()->shortcutMap);
960 emit q->enabledChanged(b);
964void QAction::resetEnabled()
967 if (!d->explicitEnabled)
969 d->explicitEnabled =
false;
970 d->setEnabled(
true,
false);
973bool QAction::isEnabled()
const
980
981
982
983
984
985
986
987
988
989
990
991
992void QAction::setVisible(
bool b)
995 if (b != d->forceInvisible)
997 d->forceInvisible = !b;
998 if (b && d->group && !d->group->isVisible())
1003void QActionPrivate::setVisible(
bool b)
1010 bool enable = visible;
1011 if (enable && explicitEnabled)
1012 enable = explicitEnabledValue;
1014 if (!setEnabled(enable,
false))
1017 emit q->visibleChanged();
1020bool QAction::isVisible()
const
1027
1028
1029bool QAction::event(QEvent *e)
1032 if (e->type() == QEvent::ActionChanged) {
1033 for (
auto object : std::as_const(d->associatedObjects))
1034 QCoreApplication::sendEvent(object, e);
1037#if QT_CONFIG(shortcut)
1038 if (e->type() == QEvent::Shortcut) {
1039 QShortcutEvent *se =
static_cast<QShortcutEvent *>(e);
1040 Q_ASSERT_X(d_func()->shortcutIds.contains(se->shortcutId()),
1042 "Received shortcut event from incorrect shortcut");
1043 if (se->isAmbiguous())
1044 qWarning(
"QAction::event: Ambiguous shortcut overload: %s", se->key().toString(QKeySequence::NativeText).toLatin1().constData());
1050 return QObject::event(e);
1054
1055
1056
1057
1058QVariant QAction::data()
const
1065
1066
1067
1068
1069void QAction::setData(
const QVariant &data)
1072 if (d->userData == data)
1075 d->sendDataChanged();
1079
1080
1081
1082
1083
1084void QAction::activate(ActionEvent event)
1087 if (event == Trigger) {
1089 if ((d->explicitEnabled && !d->explicitEnabledValue) || (d->group && !d->group->isEnabled()))
1091 QPointer<QObject> guard =
this;
1094 if (d->checked && (d->group
1095 && d->group->exclusionPolicy() == QActionGroup::ExclusionPolicy::Exclusive
1096 && d->group->checkedAction() ==
this)) {
1097 if (!guard.isNull())
1098 emit triggered(
true);
1101 setChecked(!d->checked);
1103 if (!guard.isNull())
1104 emit triggered(d->checked);
1105 }
else if (event == Hover) {
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1140
1141
1142
1143
1144
1145
1146
1147
1150
1151
1152
1153
1154
1155
1156
1157
1158
1161
1162
1163
1164
1165
1166
1167
1168
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182void QAction::setMenuRole(MenuRole menuRole)
1185 if (d->menuRole == menuRole)
1188 d->menuRole = menuRole;
1189 d->sendDataChanged();
1192QAction::MenuRole QAction::menuRole()
const
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208QObject* QAction::menuObject()
const
1215
1216
1217
1218
1219void QAction::setMenuObject(QObject *object)
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242void QAction::setIconVisibleInMenu(
bool visible)
1245 if (d->iconVisibleInMenu == -1 || visible !=
bool(d->iconVisibleInMenu)) {
1246 int oldValue = d->iconVisibleInMenu;
1247 d->iconVisibleInMenu = visible;
1250 || visible == !QCoreApplication::testAttribute(Qt::AA_DontShowIconsInMenus)) {
1251 d->sendDataChanged();
1256bool QAction::isIconVisibleInMenu()
const
1259 if (d->iconVisibleInMenu == -1) {
1260 return !QCoreApplication::testAttribute(Qt::AA_DontShowIconsInMenus);
1262 return d->iconVisibleInMenu;
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278void QAction::setShortcutVisibleInContextMenu(
bool visible)
1281 if (d->shortcutVisibleInContextMenu == -1 || visible !=
bool(d->shortcutVisibleInContextMenu)) {
1282 int oldValue = d->shortcutVisibleInContextMenu;
1283 d->shortcutVisibleInContextMenu = visible;
1286 || visible == !QCoreApplication::testAttribute(Qt::AA_DontShowShortcutsInContextMenus)) {
1287 d->sendDataChanged();
1292bool QAction::isShortcutVisibleInContextMenu()
const
1295 if (d->shortcutVisibleInContextMenu == -1)
1296 return !QCoreApplication::testAttribute(Qt::AA_DontShowShortcutsInContextMenus);
1297 return d->shortcutVisibleInContextMenu;
1300#ifndef QT_NO_DEBUG_STREAM
1301Q_GUI_EXPORT
QDebug operator<<(QDebug d,
const QAction *action)
1303 QDebugStateSaver saver(d);
1305 d <<
"QAction(" <<
static_cast<
const void *>(action);
1307 d <<
" text=" << action->text();
1308 if (!action->toolTip().isEmpty())
1309 d <<
" toolTip=" << action->toolTip();
1310 if (action->isCheckable())
1311 d <<
" checked=" << action->isChecked();
1312#if QT_CONFIG(shortcut)
1313 if (!action->shortcuts().isEmpty())
1314 d <<
" shortcuts=" << action->shortcuts();
1317 QtDebugUtils::formatQEnum(d, action->menuRole());
1318 d <<
" enabled=" << action->isEnabled();
1319 d <<
" visible=" << action->isVisible();
1330#include "moc_qaction.cpp"
#define QAPP_CHECK(functionName)
static QString qt_strippedText(QString s)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)