1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
|
/****************************************************************************
**
** Implementation of QAction class
**
** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the widgets module of the Qt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free Qt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at [email protected].
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.QPL
** included in the packaging of this file. Licensees holding valid Qt
** Commercial licenses may use this file in accordance with the Qt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/
#include "qaction.h"
#ifndef QT_NO_ACTION
#include "qtoolbar.h"
#include "qptrlist.h"
#include "qpopupmenu.h"
#include "qaccel.h"
#include "qtoolbutton.h"
#include "qcombobox.h"
#include "qtooltip.h"
#include "qwhatsthis.h"
#include "qstatusbar.h"
#include "qobjectlist.h"
/*!
\class QAction qaction.h
\brief The QAction class provides an abstract user interface
action that can appear both in menus and tool bars.
\ingroup basic
\ingroup application
\mainclass
In GUI applications many commands can be invoked via a menu
option, a toolbar button and a keyboard accelerator. Since the
same action must be performed regardless of how the action was
invoked, and since the menu and toolbar should be kept in sync, it
is useful to represent a command as an \e action. An action can be
added to a menu and a toolbar and will automatically keep them in
sync. For example, if the user presses a Bold toolbar button the
Bold menu item will automatically be checked.
A QAction may contain an icon, a menu text, an accelerator, a
status text, a whats this text and a tool tip. Most of these can
be set in the constructor. They can also be set independently with
setIconSet(), setText(), setMenuText(), setToolTip(),
setStatusTip(), setWhatsThis() and setAccel().
An action may be a toggle action e.g. a Bold toolbar button, or a
command action, e.g. 'Open File' to invoke an open file dialog.
Toggle actions emit the toggled() signal when their state changes.
Both command and toggle actions emit the activated() signal when
they are invoked. Use setToggleAction() to set an action's toggled
status. To see if an action is a toggle action use
isToggleAction(). A toggle action may be "on", isOn() returns
TRUE, or "off", isOn() returns FALSE.
Actions are added to widgets (menus or toolbars) using addTo(),
and removed using removeFrom().
Once a QAction has been created it should be added to the relevant
menu and toolbar and then connected to the slot which will perform
the action. For example:
\quotefile action/application.cpp
\skipto QPixmap( fileopen
\printuntil connect
We create a "File Save" action with a menu text of "&Save" and
\e{Ctrl+S} as the keyboard accelerator. We connect the
fileSaveAction's activated() signal to our own save() slot. Note
that at this point there is no menu or toolbar action, we'll add
them next:
\skipto new QToolBar
\printline
\skipto fileSaveAction->addTo
\printline
\skipto new QPopupMenu
\printuntil insertItem
\skipto fileSaveAction->addTo
\printline
We create a toolbar and add our fileSaveAction to it. Similarly we
create a menu, add a top-level menu item, and add our
fileSaveAction.
We recommend that actions are created as children of the window
that they are used in. In most cases actions will be children of
the application's main window.
To prevent recursion, don't create an action as a child of a
widget that the action is later added to.
*/
class QActionPrivate
{
public:
QActionPrivate(QAction *act);
~QActionPrivate();
QIconSet *iconset;
QString text;
QString menutext;
QString tooltip;
QString statustip;
QString whatsthis;
#ifndef QT_NO_ACCEL
QKeySequence key;
QAccel* accel;
int accelid;
#endif
uint enabled : 1;
uint visible : 1;
uint toggleaction : 1;
uint on : 1;
uint forceDisabled : 1;
uint forceInvisible : 1;
#ifndef QT_NO_TOOLTIP
QToolTipGroup tipGroup;
#endif
QActionGroupPrivate* d_group;
QAction *action;
struct MenuItem {
MenuItem():popup(0),id(0){}
QPopupMenu* popup;
int id;
};
// ComboItem is only necessary for actions that are
// in dropdown/exclusive actiongroups. The actiongroup
// will clean this up
struct ComboItem {
ComboItem():combo(0), id(0) {}
QComboBox *combo;
int id;
};
QPtrList<MenuItem> menuitems;
QPtrList<QToolButton> toolbuttons;
QPtrList<ComboItem> comboitems;
enum Update { Icons = 1, Visibility = 2, State = 4, EverythingElse = 8 };
void update( uint upd = EverythingElse );
QString menuText() const;
QString toolTip() const;
QString statusTip() const;
};
QActionPrivate::QActionPrivate(QAction *act)
: iconset( 0 ),
#ifndef QT_NO_ACCEL
key( 0 ), accel( 0 ), accelid( 0 ),
#endif
enabled( TRUE ), visible( TRUE ), toggleaction( FALSE ), on( FALSE ),
forceDisabled( FALSE ), forceInvisible( FALSE ),
#ifndef QT_NO_TOOLTIP
tipGroup( 0 ),
#endif
d_group( 0 ), action(act)
{
menuitems.setAutoDelete( TRUE );
comboitems.setAutoDelete( TRUE );
#ifndef QT_NO_TOOLTIP
tipGroup.setDelay( FALSE );
#endif
}
QActionPrivate::~QActionPrivate()
{
QPtrListIterator<QToolButton> ittb( toolbuttons );
QToolButton *tb;
while ( ( tb = ittb.current() ) ) {
++ittb;
delete tb;
}
QPtrListIterator<QActionPrivate::MenuItem> itmi( menuitems);
QActionPrivate::MenuItem* mi;
while ( ( mi = itmi.current() ) ) {
++itmi;
QPopupMenu* menu = mi->popup;
if ( menu->findItem( mi->id ) )
menu->removeItem( mi->id );
}
QPtrListIterator<QActionPrivate::ComboItem> itci(comboitems);
QActionPrivate::ComboItem* ci;
while ( ( ci = itci.current() ) ) {
++itci;
QComboBox* combo = ci->combo;
combo->clear();
QActionGroup *group = ::qt_cast<QActionGroup*>(action->parent());
QObjectList *siblings = group ? group->queryList("QAction") : 0;
if (siblings) {
QObjectListIt it(*siblings);
while (it.current()) {
QAction *sib = ::qt_cast<QAction*>(it.current());
++it;
sib->removeFrom(combo);
}
it = QObjectListIt(*siblings);
while (it.current()) {
QAction *sib = ::qt_cast<QAction*>(it.current());
++it;
if (sib == action)
continue;
sib->addTo(combo);
}
}
delete siblings;
}
#ifndef QT_NO_ACCEL
delete accel;
#endif
delete iconset;
}
class QActionGroupPrivate
{
public:
uint exclusive: 1;
uint dropdown: 1;
QPtrList<QAction> actions;
QAction* selected;
QAction* separatorAction;
struct MenuItem {
MenuItem():popup(0),id(0){}
QPopupMenu* popup;
int id;
};
QPtrList<QComboBox> comboboxes;
QPtrList<QToolButton> menubuttons;
QPtrList<MenuItem> menuitems;
QPtrList<QPopupMenu> popupmenus;
void update( const QActionGroup * );
};
void QActionPrivate::update( uint upd )
{
for ( QPtrListIterator<MenuItem> it( menuitems); it.current(); ++it ) {
MenuItem* mi = it.current();
QString t = menuText();
#ifndef QT_NO_ACCEL
if ( key )
t += '\t' + QAccel::keyToString( key );
#endif
if ( upd & State ) {
mi->popup->setItemEnabled( mi->id, enabled );
if ( toggleaction )
mi->popup->setItemChecked( mi->id, on );
}
if ( upd & Visibility )
mi->popup->setItemVisible( mi->id, visible );
if ( upd & Icons ) {
if ( iconset )
mi->popup->changeItem( mi->id, *iconset, t );
else
mi->popup->changeItem( mi->id, QIconSet(), t );
}
if ( upd & EverythingElse ) {
mi->popup->changeItem( mi->id, t );
if ( !whatsthis.isEmpty() )
mi->popup->setWhatsThis( mi->id, whatsthis );
if ( toggleaction ) {
mi->popup->setCheckable( TRUE );
mi->popup->setItemChecked( mi->id, on );
}
}
}
for ( QPtrListIterator<QToolButton> it2(toolbuttons); it2.current(); ++it2 ) {
QToolButton* btn = it2.current();
if ( upd & State ) {
btn->setEnabled( enabled );
if ( toggleaction )
btn->setOn( on );
}
if ( upd & Visibility )
visible ? btn->show() : btn->hide();
if ( upd & Icons ) {
if ( iconset )
btn->setIconSet( *iconset );
else
btn->setIconSet( QIconSet() );
}
if ( upd & EverythingElse ) {
btn->setToggleButton( toggleaction );
if ( !text.isEmpty() )
btn->setTextLabel( text, FALSE );
#ifndef QT_NO_TOOLTIP
QToolTip::remove( btn );
QToolTip::add( btn, toolTip(), &tipGroup, statusTip() );
#endif
#ifndef QT_NO_WHATSTHIS
QWhatsThis::remove( btn );
if ( !whatsthis.isEmpty() )
QWhatsThis::add( btn, whatsthis );
#endif
}
}
#ifndef QT_NO_ACCEL
if ( accel ) {
accel->setEnabled( enabled && visible );
if ( !whatsthis.isEmpty() )
accel->setWhatsThis( accelid, whatsthis );
}
#endif
// Only used by actiongroup
for ( QPtrListIterator<ComboItem> it3( comboitems ); it3.current(); ++it3 ) {
ComboItem *ci = it3.current();
if ( !ci->combo )
return;
if ( iconset )
ci->combo->changeItem( iconset->pixmap(), text, ci->id );
else
ci->combo->changeItem( text, ci->id );
}
}
QString QActionPrivate::menuText() const
{
if ( menutext.isNull() ) {
QString t(text);
t.replace('&', "&&");
return t;
}
return menutext;
}
QString QActionPrivate::toolTip() const
{
if ( tooltip.isNull() ) {
#ifndef QT_NO_ACCEL
if ( accel )
return text + " (" + QAccel::keyToString( accel->key( accelid )) + ")";
#endif
return text;
}
return tooltip;
}
QString QActionPrivate::statusTip() const
{
if ( statustip.isNull() )
return toolTip();
return statustip;
}
/*
internal: guesses a descriptive text from a menu text
*/
static QString qt_stripMenuText( QString s )
{
s.remove( QString::fromLatin1("...") );
s.remove( QChar('&' ) );
return s.stripWhiteSpace();
}
/*!
Constructs an action called \a name with parent \a parent.
If \a parent is a QActionGroup, the new action inserts itself into
\a parent.
For accelerators and status tips to work, \a parent must either be
a widget, or an action group whose parent is a widget.
\warning To prevent recursion, don't create an action as a child
of a widget that the action is later added to.
*/
QAction::QAction( QObject* parent, const char* name )
: QObject( parent, name )
{
d = new QActionPrivate(this);
init();
}
/*! \obsolete
Constructs an action called \a name with parent \a parent.
If \a toggle is TRUE the action will be a toggle action, otherwise
it will be a command action.
If \a parent is a QActionGroup, the new action inserts itself into
\a parent.
For accelerators and status tips to work, \a parent must either be
a widget, or an action group whose parent is a widget.
*/
QAction::QAction( QObject* parent, const char* name, bool toggle )
: QObject( parent, name )
{
d = new QActionPrivate(this);
d->toggleaction = toggle;
init();
}
#ifndef QT_NO_ACCEL
/*!
This constructor creates an action with the following properties:
the icon or iconset \a icon, the menu text \a menuText and
keyboard accelerator \a accel. It is a child of \a parent and
called \a name.
If \a parent is a QActionGroup, the action automatically becomes
a member of it.
For accelerators and status tips to work, \a parent must either be
a widget, or an action group whose parent is a widget.
The action uses a stripped version of \a menuText (e.g. "\&Menu
Option..." becomes "Menu Option") as descriptive text for
toolbuttons. You can override this by setting a specific
description with setText(). The same text and \a accel will be
used for tool tips and status tips unless you provide text for
these using setToolTip() and setStatusTip().
Call setToggleAction(TRUE) to make the action a toggle action.
\warning To prevent recursion, don't create an action as a child
of a widget that the action is later added to.
*/
QAction::QAction( const QIconSet& icon, const QString& menuText, QKeySequence accel,
QObject* parent, const char* name )
: QObject( parent, name )
{
d = new QActionPrivate(this);
if ( !icon.isNull() )
setIconSet( icon );
d->text = qt_stripMenuText( menuText );
d->menutext = menuText;
setAccel( accel );
init();
}
/*!
This constructor results in an icon-less action with the the menu
text \a menuText and keyboard accelerator \a accel. It is a child
of \a parent and called \a name.
If \a parent is a QActionGroup, the action automatically becomes
a member of it.
For accelerators and status tips to work, \a parent must either be
a widget, or an action group whose parent is a widget.
The action uses a stripped version of \a menuText (e.g. "\&Menu
Option..." becomes "Menu Option") as descriptive text for
toolbuttons. You can override this by setting a specific
description with setText(). The same text and \a accel will be
used for tool tips and status tips unless you provide text for
these using setToolTip() and setStatusTip().
Call setToggleAction(TRUE) to make the action a toggle action.
\warning To prevent recursion, don't create an action as a child
of a widget that the action is later added to.
*/
QAction::QAction( const QString& menuText, QKeySequence accel,
QObject* parent, const char* name )
: QObject( parent, name )
{
d = new QActionPrivate(this);
d->text = qt_stripMenuText( menuText );
d->menutext = menuText;
setAccel( accel );
init();
}
/*! \obsolete
This constructor creates an action with the following properties:
the description \a text, the icon or iconset \a icon, the menu
text \a menuText and keyboard accelerator \a accel. It is a child
of \a parent and called \a name. If \a toggle is TRUE the action
will be a toggle action, otherwise it will be a command action.
If \a parent is a QActionGroup, the action automatically becomes
a member of it.
For accelerators and status tips to work, \a parent must either be
a widget, or an action group whose parent is a widget.
The \a text and \a accel will be used for tool tips and status
tips unless you provide specific text for these using setToolTip()
and setStatusTip().
*/
QAction::QAction( const QString& text, const QIconSet& icon, const QString& menuText, QKeySequence accel, QObject* parent, const char* name, bool toggle )
: QObject( parent, name )
{
d = new QActionPrivate(this);
d->toggleaction = toggle;
if ( !icon.isNull() )
setIconSet( icon );
d->text = text;
d->menutext = menuText;
setAccel( accel );
init();
}
/*! \obsolete
This constructor results in an icon-less action with the
description \a text, the menu text \a menuText and the keyboard
accelerator \a accel. Its parent is \a parent and it is called \a
name. If \a toggle is TRUE the action will be a toggle action,
otherwise it will be a command action.
The action automatically becomes a member of \a parent if \a
parent is a QActionGroup.
For accelerators and status tips to work, \a parent must either be
a widget, or an action group whose parent is a widget.
The \a text and \a accel will be used for tool tips and status
tips unless you provide specific text for these using setToolTip()
and setStatusTip().
*/
QAction::QAction( const QString& text, const QString& menuText, QKeySequence accel, QObject* parent, const char* name, bool toggle )
: QObject( parent, name )
{
d = new QActionPrivate(this);
d->toggleaction = toggle;
d->text = text;
d->menutext = menuText;
setAccel( accel );
init();
}
#endif
/*!
\internal
*/
void QAction::init()
{
if ( ::qt_cast<QActionGroup*>(parent()) )
((QActionGroup*) parent())->add( this ); // insert into action group
}
/*!
Destroys the object and frees allocated resources.
*/
QAction::~QAction()
{
delete d;
}
/*!
\property QAction::iconSet
\brief the action's icon
The icon is used as the tool button icon and in the menu to the
left of the menu text. There is no default icon.
If a null icon (QIconSet::isNull() is passed into this function,
the icon of the action is cleared.
(See the action/toggleaction/toggleaction.cpp example.)
*/
void QAction::setIconSet( const QIconSet& icon )
{
QIconSet *i = d->iconset;
if ( !icon.isNull() )
d->iconset = new QIconSet( icon );
else
d->iconset = 0;
delete i;
d->update( QActionPrivate::Icons );
}
QIconSet QAction::iconSet() const
{
if ( d->iconset )
return *d->iconset;
return QIconSet();
}
/*!
\property QAction::text
\brief the action's descriptive text
If \l QMainWindow::usesTextLabel is TRUE, the text appears as a
label in the relevant tool button. It also serves as the default
text in menus and tool tips if these have not been specifically
defined. There is no default text.
\sa setMenuText() setToolTip() setStatusTip()
*/
void QAction::setText( const QString& text )
{
d->text = text;
d->update();
}
QString QAction::text() const
{
return d->text;
}
/*!
\property QAction::menuText
\brief the action's menu text
If the action is added to a menu the menu option will consist of
the icon (if there is one), the menu text and the accelerator (if
there is one). If the menu text is not explicitly set in the
constructor or by using setMenuText() the action's description
text will be used as the menu text. There is no default menu text.
\sa text
*/
void QAction::setMenuText( const QString& text )
{
if ( d->menutext == text )
return;
d->menutext = text;
d->update();
}
QString QAction::menuText() const
{
return d->menuText();
}
/*!
\property QAction::toolTip
\brief the action's tool tip
This text is used for the tool tip. If no status tip has been set
the tool tip will be used for the status tip.
If no tool tip is specified the action's text is used, and if that
hasn't been specified the description text is used as the tool tip
text.
There is no default tool tip text.
\sa setStatusTip() setAccel()
*/
void QAction::setToolTip( const QString& tip )
{
if ( d->tooltip == tip )
return;
d->tooltip = tip;
d->update();
}
QString QAction::toolTip() const
{
return d->toolTip();
}
/*!
\property QAction::statusTip
\brief the action's status tip
The statusTip is displayed on all status bars that this action's
toplevel parent widget provides.
If no status tip is defined, the action uses the tool tip text.
There is no default statusTip text.
\sa setStatusTip() setToolTip()
*/
//#### Please reimp for QActionGroup!
//#### For consistency reasons even action groups should show
//#### status tips (as they already do with tool tips)
//#### Please change QActionGroup class doc appropriately after
//#### reimplementation.
void QAction::setStatusTip( const QString& tip )
{
if ( d->statustip == tip )
return;
d->statustip = tip;
d->update();
}
QString QAction::statusTip() const
{
return d->statusTip();
}
/*!
\property QAction::whatsThis
\brief the action's "What's This?" help text
The whats this text is used to provide a brief description of the
action. The text may contain rich text (HTML-like tags -- see
QStyleSheet for the list of supported tags). There is no default
"What's This" text.
\sa QWhatsThis
*/
void QAction::setWhatsThis( const QString& whatsThis )
{
if ( d->whatsthis == whatsThis )
return;
d->whatsthis = whatsThis;
d->update();
}
QString QAction::whatsThis() const
{
return d->whatsthis;
}
#ifndef QT_NO_ACCEL
/*!
\property QAction::accel
\brief the action's accelerator key
The keycodes can be found in \l Qt::Key and \l Qt::Modifier. There
is no default accelerator key.
*/
//#### Please reimp for QActionGroup!
//#### For consistency reasons even QActionGroups should respond to
//#### their accelerators and e.g. open the relevant submenu.
//#### Please change appropriate QActionGroup class doc after
//#### reimplementation.
void QAction::setAccel( const QKeySequence& key )
{
if ( d->key == key )
return;
d->key = key;
delete d->accel;
d->accel = 0;
if ( !(int)key ) {
d->update();
return;
}
QObject* p = parent();
while ( p && !p->isWidgetType() ) {
p = p->parent();
}
if ( p ) {
d->accel = new QAccel( (QWidget*)p, this, "qt_action_accel" );
d->accelid = d->accel->insertItem( d->key );
d->accel->connectItem( d->accelid, this, SLOT( internalActivation() ) );
}
#if defined(QT_CHECK_STATE)
else
qWarning( "QAction::setAccel() (%s) requires widget in parent chain", name() );
#endif
d->update();
}
QKeySequence QAction::accel() const
{
return d->key;
}
#endif
/*!
\property QAction::toggleAction
\brief whether the action is a toggle action
A toggle action is one which has an on/off state. For example a
Bold toolbar button is either on or off. An action which is not a
toggle action is a command action; a command action is simply
executed, e.g. file save. This property's default is FALSE.
In some situations, the state of one toggle action should depend
on the state of others. For example, "Left Align", "Center" and
"Right Align" toggle actions are mutually exclusive. To achieve
exclusive toggling, add the relevant toggle actions to a
QActionGroup with the \l QActionGroup::exclusive property set to
TRUE.
*/
void QAction::setToggleAction( bool enable )
{
if ( enable == (bool)d->toggleaction )
return;
if ( !enable )
d->on = FALSE;
d->toggleaction = enable;
d->update();
}
bool QAction::isToggleAction() const
{
return d->toggleaction;
}
/*!
Activates the action and executes all connected slots.
This only works for actions that are not toggle action.
\sa toggle()
*/
void QAction::activate()
{
if ( isToggleAction() ) {
#if defined(QT_CHECK_STATE)
qWarning( "QAction::%s() (%s) Toggle actions "
"can not be activated", "activate", name() );
#endif
return;
}
emit activated();
}
/*!
Toggles the state of a toggle action.
\sa on, activate(), toggled(), isToggleAction()
*/
void QAction::toggle()
{
if ( !isToggleAction() ) {
#if defined(QT_CHECK_STATE)
qWarning( "QAction::%s() (%s) Only toggle actions "
"can be switched", "toggle", name() );
#endif
return;
}
setOn( !isOn() );
}
/*!
\property QAction::on
\brief whether a toggle action is on
This property is always on (TRUE) for command actions and
\l{QActionGroup}s; setOn() has no effect on them. For action's
where isToggleAction() is TRUE, this property's default value is
off (FALSE).
\sa toggleAction
*/
void QAction::setOn( bool enable )
{
if ( !isToggleAction() ) {
#if defined(QT_CHECK_STATE)
if ( enable )
qWarning( "QAction::%s() (%s) Only toggle actions "
"can be switched", "setOn", name() );
#endif
return;
}
if ( enable == (bool)d->on )
return;
d->on = enable;
d->update( QActionPrivate::State );
emit toggled( enable );
}
bool QAction::isOn() const
{
return d->on;
}
/*!
\property QAction::enabled
\brief whether the action is enabled
Disabled actions can't be chosen by the user. They don't disappear
from the menu/tool bar but are displayed in a way which indicates
that they are unavailable, e.g. they might be displayed grayed
out.
What's this? help on disabled actions is still available provided
the \l QAction::whatsThis property is set.
*/
void QAction::setEnabled( bool enable )
{
d->forceDisabled = !enable;
if ( (bool)d->enabled == enable )
return;
d->enabled = enable;
d->update( QActionPrivate::State );
}
bool QAction::isEnabled() const
{
return d->enabled;
}
/*!
Disables the action if \a disable is TRUE; otherwise
enables the action.
See the \l enabled documentation for more information.
*/
void QAction::setDisabled( bool disable )
{
setEnabled( !disable );
}
/*!
\property QAction::visible
\brief whether the action can be seen (e.g. in menus and toolbars)
If \e visible is TRUE the action can be seen (e.g. in menus and
toolbars) and chosen by the user; if \e visible is FALSE the
action cannot be seen or chosen by the user.
Actions which are not visible are \e not grayed out; they do not
appear at all.
*/
void QAction::setVisible( bool visible )
{
d->forceInvisible = !visible;
if ( (bool)d->visible == visible )
return;
d->visible = visible;
d->update( QActionPrivate::Visibility );
#if (QT_VERSION-0 >= 0x040000)
#error "QAction::setVisible function wants to be virtual. Also add virtual change() function"
#endif
if ( d->d_group ) //### this function wants to be virtual in 4.0
d->d_group->update( (QActionGroup*) this );
}
/*
Returns TRUE if the action is visible (e.g. in menus and
toolbars); otherwise returns FALSE.
*/
bool QAction::isVisible() const
{
return d->visible;
}
/*! \internal
*/
void QAction::internalActivation()
{
if ( isToggleAction() )
setOn( !isOn() );
emit activated();
}
/*! \internal
*/
void QAction::toolButtonToggled( bool on )
{
if ( !isToggleAction() )
return;
setOn( on );
}
/*!
Adds this action to widget \a w.
Currently actions may be added to QToolBar and QPopupMenu widgets.
An action added to a tool bar is automatically displayed as a tool
button; an action added to a pop up menu appears as a menu option.
addTo() returns TRUE if the action was added successfully and
FALSE otherwise. (If \a w is not a QToolBar or QPopupMenu the
action will not be added and FALSE will be returned.)
\sa removeFrom()
*/
bool QAction::addTo( QWidget* w )
{
#ifndef QT_NO_TOOLBAR
if ( ::qt_cast<QToolBar*>(w) ) {
if ( !qstrcmp( name(), "qt_separator_action" ) ) {
((QToolBar*)w)->addSeparator();
} else {
QCString bname = name() + QCString( "_action_button" );
QToolButton* btn = new QToolButton( (QToolBar*) w, bname );
addedTo( btn, w );
btn->setToggleButton( d->toggleaction );
d->toolbuttons.append( btn );
if ( d->iconset )
btn->setIconSet( *d->iconset );
d->update( QActionPrivate::State | QActionPrivate::Visibility | QActionPrivate::EverythingElse ) ;
connect( btn, SIGNAL( clicked() ), this, SIGNAL( activated() ) );
connect( btn, SIGNAL( toggled(bool) ), this, SLOT( toolButtonToggled(bool) ) );
connect( btn, SIGNAL( destroyed() ), this, SLOT( objectDestroyed() ) );
#ifndef QT_NO_TOOLTIP
connect( &(d->tipGroup), SIGNAL(showTip(const QString&)), this, SLOT(showStatusText(const QString&)) );
connect( &(d->tipGroup), SIGNAL(removeTip()), this, SLOT(clearStatusText()) );
#endif
}
} else
#endif
if ( ::qt_cast<QPopupMenu*>(w) ) {
QActionPrivate::MenuItem* mi = new QActionPrivate::MenuItem;
mi->popup = (QPopupMenu*) w;
QIconSet* diconset = d->iconset;
if ( !qstrcmp( name(), "qt_separator_action" ) )
mi->id = ((QPopupMenu*)w)->insertSeparator();
else if ( diconset )
mi->id = mi->popup->insertItem( *diconset, QString::fromLatin1("") );
else
mi->id = mi->popup->insertItem( QString::fromLatin1("") );
addedTo( mi->popup->indexOf( mi->id ), mi->popup );
mi->popup->connectItem( mi->id, this, SLOT(internalActivation()) );
d->menuitems.append( mi );
d->update( QActionPrivate::State | QActionPrivate::Visibility | QActionPrivate::EverythingElse ) ;
w->topLevelWidget()->className();
connect( mi->popup, SIGNAL(highlighted(int)), this, SLOT(menuStatusText(int)) );
connect( mi->popup, SIGNAL(aboutToHide()), this, SLOT(clearStatusText()) );
connect( mi->popup, SIGNAL( destroyed() ), this, SLOT( objectDestroyed() ) );
// Makes only sense when called by QActionGroup::addTo
} else if ( ::qt_cast<QComboBox*>(w) ) {
QActionPrivate::ComboItem *ci = new QActionPrivate::ComboItem;
ci->combo = (QComboBox*)w;
connect( ci->combo, SIGNAL( destroyed() ), this, SLOT( objectDestroyed() ) );
ci->id = ci->combo->count();
if ( qstrcmp( name(), "qt_separator_action" ) ) {
if ( d->iconset )
ci->combo->insertItem( d->iconset->pixmap(), text() );
else
ci->combo->insertItem( text() );
} else {
ci->id = -1;
}
d->comboitems.append( ci );
d->update( QActionPrivate::State | QActionPrivate::EverythingElse );
} else {
qWarning( "QAction::addTo(), unknown object" );
return FALSE;
}
return TRUE;
}
/*!
This function is called from the addTo() function when it has
created a widget (\a actionWidget) for the action in the \a
container.
*/
void QAction::addedTo( QWidget *actionWidget, QWidget *container )
{
Q_UNUSED( actionWidget );
Q_UNUSED( container );
}
/*!
\overload
This function is called from the addTo() function when it has
created a menu item at the index position \a index in the popup
menu \a menu.
*/
void QAction::addedTo( int index, QPopupMenu *menu )
{
Q_UNUSED( index );
Q_UNUSED( menu );
}
/*!
Sets the status message to \a text
*/
void QAction::showStatusText( const QString& text )
{
#ifndef QT_NO_STATUSBAR
// find out whether we are clearing the status bar by the popup that actually set the text
static QPopupMenu *lastmenu = 0;
QObject *s = (QObject*)sender();
if ( s ) {
QPopupMenu *menu = (QPopupMenu*)s->qt_cast( "QPopupMenu" );
if ( menu && !!text )
lastmenu = menu;
else if ( menu && text.isEmpty() ) {
if ( lastmenu && menu != lastmenu )
return;
lastmenu = 0;
}
}
QObject* par = parent();
QObject* lpar = 0;
QStatusBar *bar = 0;
while ( par && !bar ) {
lpar = par;
bar = (QStatusBar*)par->child( 0, "QStatusBar", FALSE );
par = par->parent();
}
if ( !bar && lpar ) {
QObjectList *l = lpar->queryList( "QStatusBar" );
if ( !l )
return;
// #### hopefully the last one is the one of the mainwindow...
bar = (QStatusBar*)l->last();
delete l;
}
if ( bar ) {
if ( text.isEmpty() )
bar->clear();
else
bar->message( text );
}
#endif
}
/*!
Sets the status message to the menu item's status text, or to the
tooltip, if there is no status text.
*/
void QAction::menuStatusText( int id )
{
static int lastId = 0;
QString text;
QPtrListIterator<QActionPrivate::MenuItem> it( d->menuitems);
QActionPrivate::MenuItem* mi;
while ( ( mi = it.current() ) ) {
++it;
if ( mi->id == id ) {
text = statusTip();
break;
}
}
if ( !text.isEmpty() )
showStatusText( text );
else if ( id != lastId )
clearStatusText();
lastId = id;
}
/*!
Clears the status text.
*/
void QAction::clearStatusText()
{
if (!statusTip().isEmpty())
showStatusText( QString::null );
}
/*!
Removes the action from widget \a w.
Returns TRUE if the action was removed successfully; otherwise
returns FALSE.
\sa addTo()
*/
bool QAction::removeFrom( QWidget* w )
{
#ifndef QT_NO_TOOLBAR
if ( ::qt_cast<QToolBar*>(w) ) {
QPtrListIterator<QToolButton> it( d->toolbuttons);
QToolButton* btn;
while ( ( btn = it.current() ) ) {
++it;
if ( btn->parentWidget() == w ) {
d->toolbuttons.removeRef( btn );
disconnect( btn, SIGNAL( destroyed() ), this, SLOT( objectDestroyed() ) );
delete btn;
// no need to disconnect from statusbar
}
}
} else
#endif
if ( ::qt_cast<QPopupMenu*>(w) ) {
QPtrListIterator<QActionPrivate::MenuItem> it( d->menuitems);
QActionPrivate::MenuItem* mi;
while ( ( mi = it.current() ) ) {
++it;
if ( mi->popup == w ) {
disconnect( mi->popup, SIGNAL(highlighted(int)), this, SLOT(menuStatusText(int)) );
disconnect( mi->popup, SIGNAL(aboutToHide()), this, SLOT(clearStatusText()) );
disconnect( mi->popup, SIGNAL( destroyed() ), this, SLOT( objectDestroyed() ) );
mi->popup->removeItem( mi->id );
d->menuitems.removeRef( mi );
}
}
} else if ( ::qt_cast<QComboBox*>(w) ) {
QPtrListIterator<QActionPrivate::ComboItem> it( d->comboitems );
QActionPrivate::ComboItem *ci;
while ( ( ci = it.current() ) ) {
++it;
if ( ci->combo == w ) {
disconnect( ci->combo, SIGNAL(destroyed()), this, SLOT(objectDestroyed()) );
d->comboitems.removeRef( ci );
}
}
} else {
qWarning( "QAction::removeFrom(), unknown object" );
return FALSE;
}
return TRUE;
}
/*!
\internal
*/
void QAction::objectDestroyed()
{
const QObject* obj = sender();
QPtrListIterator<QActionPrivate::MenuItem> it( d->menuitems );
QActionPrivate::MenuItem* mi;
while ( ( mi = it.current() ) ) {
++it;
if ( mi->popup == obj )
d->menuitems.removeRef( mi );
}
QActionPrivate::ComboItem *ci;
QPtrListIterator<QActionPrivate::ComboItem> it2( d->comboitems );
while ( ( ci = it2.current() ) ) {
++it2;
if ( ci->combo == obj )
d->comboitems.removeRef( ci );
}
d->toolbuttons.removeRef( (QToolButton*) obj );
}
/*!
\fn void QAction::activated()
This signal is emitted when an action is activated by the user,
e.g. when the user clicks a menu option or a toolbar button or
presses an action's accelerator key combination.
Connect to this signal for command actions. Connect to the
toggled() signal for toggle actions.
*/
/*!
\fn void QAction::toggled(bool on)
This signal is emitted when a toggle action changes state; command
actions and \l{QActionGroup}s don't emit toggled().
The \a on argument denotes the new state: If \a on is TRUE the
toggle action is switched on, and if \a on is FALSE the toggle
action is switched off.
To trigger a user command depending on whether a toggle action has
been switched on or off connect it to a slot that takes a bool to
indicate the state, e.g.
\quotefile action/toggleaction/toggleaction.cpp
\skipto QMainWindow * window
\printline QMainWindow * window
\skipto labelonoffaction
\printline labelonoffaction
\skipto connect
\printuntil setUsesTextLabel
\sa activated() setToggleAction() setOn()
*/
void QActionGroupPrivate::update( const QActionGroup* that )
{
for ( QPtrListIterator<QAction> it( actions ); it.current(); ++it ) {
if ( that->isEnabled() && !it.current()->d->forceDisabled ) {
it.current()->setEnabled( TRUE );
} else if ( !that->isEnabled() && it.current()->isEnabled() ) {
it.current()->setEnabled( FALSE );
it.current()->d->forceDisabled = FALSE;
}
if ( that->isVisible() && !it.current()->d->forceInvisible ) {
it.current()->setVisible( TRUE );
} else if ( !that->isVisible() && it.current()->isVisible() ) {
it.current()->setVisible( FALSE );
it.current()->d->forceInvisible = FALSE;
}
}
for ( QPtrListIterator<QComboBox> cb( comboboxes ); cb.current(); ++cb ) {
QComboBox *combobox = cb.current();
combobox->setEnabled( that->isEnabled() );
combobox->setShown( that->isVisible() );
#ifndef QT_NO_TOOLTIP
QToolTip::remove( combobox );
if ( !!that->toolTip() )
QToolTip::add( combobox, that->toolTip() );
#endif
#ifndef QT_NO_WHATSTHIS
QWhatsThis::remove( combobox );
if ( !!that->whatsThis() )
QWhatsThis::add( combobox, that->whatsThis() );
#endif
}
for ( QPtrListIterator<QToolButton> mb( menubuttons ); mb.current(); ++mb ) {
QToolButton *button = mb.current();
button->setEnabled( that->isEnabled() );
button->setShown( that->isVisible() );
if ( !that->text().isNull() )
button->setTextLabel( that->text() );
if ( !that->iconSet().isNull() )
button->setIconSet( that->iconSet() );
#ifndef QT_NO_TOOLTIP
QToolTip::remove( mb.current() );
if ( !!that->toolTip() )
QToolTip::add( button, that->toolTip() );
#endif
#ifndef QT_NO_WHATSTHIS
QWhatsThis::remove( button );
if ( !!that->whatsThis() )
QWhatsThis::add( button, that->whatsThis() );
#endif
}
for ( QPtrListIterator<QActionGroupPrivate::MenuItem> pu( menuitems ); pu.current(); ++pu ) {
QWidget* parent = pu.current()->popup->parentWidget();
if ( ::qt_cast<QPopupMenu*>(parent) ) {
QPopupMenu* ppopup = (QPopupMenu*)parent;
ppopup->setItemEnabled( pu.current()->id, that->isEnabled() );
ppopup->setItemVisible( pu.current()->id, that->isVisible() );
} else {
pu.current()->popup->setEnabled( that->isEnabled() );
}
}
for ( QPtrListIterator<QPopupMenu> pm( popupmenus ); pm.current(); ++pm ) {
QPopupMenu *popup = pm.current();
QPopupMenu *parent = ::qt_cast<QPopupMenu*>(popup->parentWidget());
if ( !parent )
continue;
int index;
parent->findPopup( popup, &index );
int id = parent->idAt( index );
if ( !that->iconSet().isNull() )
parent->changeItem( id, that->iconSet(), that->menuText() );
else
parent->changeItem( id, that->menuText() );
parent->setItemEnabled( id, that->isEnabled() );
#ifndef QT_NO_ACCEL
parent->setAccel( that->accel(), id );
#endif
}
}
/*!
\class QActionGroup qaction.h
\brief The QActionGroup class groups actions together.
\ingroup basic
\ingroup application
In some situations it is useful to group actions together. For
example, if you have a left justify action, a right justify action
and a center action, only one of these actions should be active at
any one time, and one simple way of achieving this is to group the
actions together in an action group.
An action group can also be added to a menu or a toolbar as a
single unit, with all the actions within the action group
appearing as separate menu options and toolbar buttons.
Here's an example from examples/textedit:
\quotefile textedit/textedit.cpp
\skipto QActionGroup
\printuntil connect
Here we create a new action group. Since the action group is exclusive
by default, only one of the actions in the group is ever active at any
one time. We then connect the group's selected() signal to our
textAlign() slot.
\printuntil actionAlignLeft->setToggleAction
We create a left align action, add it to the toolbar and the menu
and make it a toggle action. We create center and right align
actions in exactly the same way.
\omit
A QActionGroup emits an activated() signal when one of its actions
is activated.
\endomit
The actions in an action group emit their activated() (and for
toggle actions, toggled()) signals as usual.
The setExclusive() function is used to ensure that only one action
is active at any one time: it should be used with actions which
have their \c toggleAction set to TRUE.
Action group actions appear as individual menu options and toolbar
buttons. For exclusive action groups use setUsesDropDown() to
display the actions in a subwidget of any widget the action group
is added to. For example, the actions would appear in a combobox
in a toolbar or as a submenu in a menu.
Actions can be added to an action group using add(), but normally
they are added by creating the action with the action group as
parent. Actions can have separators dividing them using
addSeparator(). Action groups are added to widgets with addTo().
*/
/*!
Constructs an action group called \a name, with parent \a parent.
The action group is exclusive by default. Call setExclusive(FALSE) to make
the action group non-exclusive.
*/
QActionGroup::QActionGroup( QObject* parent, const char* name )
: QAction( parent, name )
{
d = new QActionGroupPrivate;
d->exclusive = TRUE;
d->dropdown = FALSE;
d->selected = 0;
d->separatorAction = 0;
QAction::d->d_group = d;
connect( this, SIGNAL(selected(QAction*)), SLOT(internalToggle(QAction*)) );
}
/*!
Constructs an action group called \a name, with parent \a parent.
If \a exclusive is TRUE only one toggle action in the group will
ever be active.
\sa exclusive
*/
QActionGroup::QActionGroup( QObject* parent, const char* name, bool exclusive )
: QAction( parent, name )
{
d = new QActionGroupPrivate;
d->exclusive = exclusive;
d->dropdown = FALSE;
d->selected = 0;
d->separatorAction = 0;
QAction::d->d_group = d;
connect( this, SIGNAL(selected(QAction*)), SLOT(internalToggle(QAction*)) );
}
/*!
Destroys the object and frees allocated resources.
*/
QActionGroup::~QActionGroup()
{
QPtrListIterator<QActionGroupPrivate::MenuItem> mit( d->menuitems );
while ( mit.current() ) {
QActionGroupPrivate::MenuItem *mi = mit.current();
++mit;
if ( mi->popup )
mi->popup->disconnect( SIGNAL(destroyed()), this, SLOT(objectDestroyed()) );
}
QPtrListIterator<QComboBox> cbit( d->comboboxes );
while ( cbit.current() ) {
QComboBox *cb = cbit.current();
++cbit;
cb->disconnect( SIGNAL(destroyed()), this, SLOT(objectDestroyed()) );
}
QPtrListIterator<QToolButton> mbit( d->menubuttons );
while ( mbit.current() ) {
QToolButton *mb = mbit.current();
++mbit;
mb->disconnect( SIGNAL(destroyed()), this, SLOT(objectDestroyed()) );
}
QPtrListIterator<QPopupMenu> pmit( d->popupmenus );
while ( pmit.current() ) {
QPopupMenu *pm = pmit.current();
++pmit;
pm->disconnect( SIGNAL(destroyed()), this, SLOT(objectDestroyed()) );
}
delete d->separatorAction;
d->menubuttons.setAutoDelete( TRUE );
d->comboboxes.setAutoDelete( TRUE );
d->menuitems.setAutoDelete( TRUE );
d->popupmenus.setAutoDelete( TRUE );
delete d;
}
/*!
\property QActionGroup::exclusive
\brief whether the action group does exclusive toggling
If exclusive is TRUE only one toggle action in the action group
can ever be active at any one time. If the user chooses another
toggle action in the group the one they chose becomes active and
the one that was active becomes inactive.
\sa QAction::toggleAction
*/
void QActionGroup::setExclusive( bool enable )
{
d->exclusive = enable;
}
bool QActionGroup::isExclusive() const
{
return d->exclusive;
}
/*!
\property QActionGroup::usesDropDown
\brief whether the group's actions are displayed in a subwidget of
the widgets the action group is added to
Exclusive action groups added to a toolbar display their actions
in a combobox with the action's \l QAction::text and \l
QAction::iconSet properties shown. Non-exclusive groups are
represented by a tool button showing their \l QAction::iconSet and
-- depending on \l QMainWindow::usesTextLabel() -- text()
property.
In a popup menu the member actions are displayed in a submenu.
Changing usesDropDown only affects \e subsequent calls to addTo().
Note that setting this property for actions in a combobox causes
calls to their \link QAction::setVisible()\endlink,
\link QAction::setEnabled()\endlink, and
\link QAction::setDisabled()\endlink functions to have no effect.
This property's default is FALSE.
*/
void QActionGroup::setUsesDropDown( bool enable )
{
d->dropdown = enable;
}
bool QActionGroup::usesDropDown() const
{
return d->dropdown;
}
/*!
Adds action \a action to this group.
Normally an action is added to a group by creating it with the
group as parent, so this function is not usually used.
\sa addTo()
*/
void QActionGroup::add( QAction* action )
{
if ( d->actions.containsRef( action ) )
return;
d->actions.append( action );
if ( action->whatsThis().isNull() )
action->setWhatsThis( whatsThis() );
if ( action->toolTip().isNull() )
action->setToolTip( toolTip() );
if (!action->d->forceDisabled)
action->d->enabled = isEnabled();
if (!action->d->forceInvisible)
action->d->visible = isVisible();
connect( action, SIGNAL( destroyed() ), this, SLOT( childDestroyed() ) );
connect( action, SIGNAL( activated() ), this, SIGNAL( activated() ) );
connect( action, SIGNAL( toggled(bool) ), this, SLOT( childToggled(bool) ) );
for ( QPtrListIterator<QComboBox> cb( d->comboboxes ); cb.current(); ++cb ) {
action->addTo( cb.current() );
}
for ( QPtrListIterator<QToolButton> mb( d->menubuttons ); mb.current(); ++mb ) {
QPopupMenu* popup = mb.current()->popup();
if ( !popup )
continue;
action->addTo( popup );
}
for ( QPtrListIterator<QActionGroupPrivate::MenuItem> mi( d->menuitems ); mi.current(); ++mi ) {
QPopupMenu* popup = mi.current()->popup;
if ( !popup )
continue;
action->addTo( popup );
}
}
/*!
Adds a separator to the group.
*/
void QActionGroup::addSeparator()
{
if ( !d->separatorAction )
d->separatorAction = new QAction( 0, "qt_separator_action" );
d->actions.append( d->separatorAction );
}
/*! \fn void QActionGroup::insert( QAction* a )
\obsolete
Use add() instead, or better still create the action with the action
group as its parent.
*/
/*!
Adds this action group to the widget \a w.
If isExclusive() is FALSE or usesDropDown() is FALSE, the actions within
the group are added to the widget individually. For example, if the widget
is a menu, the actions will appear as individual menu options, and
if the widget is a toolbar, the actions will appear as toolbar buttons.
If both isExclusive() and usesDropDown() are TRUE, the actions
are presented either in a combobox (if \a w is a toolbar) or in a
submenu (if \a w is a menu).
All actions should be added to the action group \e before the
action group is added to the widget. If actions are added to the
action group \e after the action group has been added to the
widget these later actions will \e not appear.
\sa setExclusive() setUsesDropDown() removeFrom()
*/
bool QActionGroup::addTo( QWidget* w )
{
#ifndef QT_NO_TOOLBAR
if ( ::qt_cast<QToolBar*>(w) ) {
if ( d->dropdown ) {
if ( !d->exclusive ) {
QPtrListIterator<QAction> it( d->actions);
if ( !it.current() )
return TRUE;
QAction *defAction = it.current();
QToolButton* btn = new QToolButton( (QToolBar*) w, "qt_actiongroup_btn" );
addedTo( btn, w );
connect( btn, SIGNAL(destroyed()), SLOT(objectDestroyed()) );
d->menubuttons.append( btn );
if ( !iconSet().isNull() )
btn->setIconSet( iconSet() );
else if ( !defAction->iconSet().isNull() )
btn->setIconSet( defAction->iconSet() );
if ( !!text() )
btn->setTextLabel( text() );
else if ( !!defAction->text() )
btn->setTextLabel( defAction->text() );
#ifndef QT_NO_TOOLTIP
if ( !!toolTip() )
QToolTip::add( btn, toolTip() );
else if ( !!defAction->toolTip() )
QToolTip::add( btn, defAction->toolTip() );
#endif
#ifndef QT_NO_WHATSTHIS
if ( !!whatsThis() )
QWhatsThis::add( btn, whatsThis() );
else if ( !!defAction->whatsThis() )
QWhatsThis::add( btn, defAction->whatsThis() );
#endif
connect( btn, SIGNAL( clicked() ), defAction, SIGNAL( activated() ) );
connect( btn, SIGNAL( toggled(bool) ), defAction, SLOT( toolButtonToggled(bool) ) );
connect( btn, SIGNAL( destroyed() ), defAction, SLOT( objectDestroyed() ) );
QPopupMenu *menu = new QPopupMenu( btn, "qt_actiongroup_menu" );
btn->setPopupDelay( 0 );
btn->setPopup( menu );
while( it.current() ) {
it.current()->addTo( menu );
++it;
}
d->update( this );
return TRUE;
} else {
QComboBox *box = new QComboBox( FALSE, w, "qt_actiongroup_combo" );
addedTo( box, w );
connect( box, SIGNAL(destroyed()), SLOT(objectDestroyed()) );
d->comboboxes.append( box );
#ifndef QT_NO_TOOLTIP
if ( !!toolTip() )
QToolTip::add( box, toolTip() );
#endif
#ifndef QT_NO_WHATSTHIS
if ( !!whatsThis() )
QWhatsThis::add( box, whatsThis() );
#endif
int onIndex = 0;
bool foundOn = FALSE;
for ( QPtrListIterator<QAction> it( d->actions); it.current(); ++it ) {
QAction *action = it.current();
if ( !foundOn )
foundOn = action->isOn();
if ( qstrcmp( action->name(), "qt_separator_action" ) && !foundOn )
onIndex++;
action->addTo( box );
}
if ( foundOn )
box->setCurrentItem( onIndex );
connect( box, SIGNAL(activated(int)), this, SLOT( internalComboBoxActivated(int)) );
connect( box, SIGNAL(highlighted(int)), this, SLOT( internalComboBoxHighlighted(int)) );
d->update( this );
return TRUE;
}
}
} else
#endif
if ( ::qt_cast<QPopupMenu*>(w) ) {
QPopupMenu *popup;
if ( d->dropdown ) {
QPopupMenu *menu = (QPopupMenu*)w;
popup = new QPopupMenu( w, "qt_actiongroup_menu" );
d->popupmenus.append( popup );
connect( popup, SIGNAL(destroyed()), SLOT(objectDestroyed()) );
int id;
if ( !iconSet().isNull() ) {
if ( menuText().isEmpty() )
id = menu->insertItem( iconSet(), text(), popup );
else
id = menu->insertItem( iconSet(), menuText(), popup );
} else {
if ( menuText().isEmpty() )
id = menu->insertItem( text(), popup );
else
id = menu->insertItem( menuText(), popup );
}
addedTo( menu->indexOf( id ), menu );
QActionGroupPrivate::MenuItem *item = new QActionGroupPrivate::MenuItem;
item->id = id;
item->popup = popup;
d->menuitems.append( item );
} else {
popup = (QPopupMenu*)w;
}
for ( QPtrListIterator<QAction> it( d->actions); it.current(); ++it ) {
// #### do an addedTo( index, popup, action), need to find out index
it.current()->addTo( popup );
}
return TRUE;
}
for ( QPtrListIterator<QAction> it( d->actions); it.current(); ++it ) {
// #### do an addedTo( index, popup, action), need to find out index
it.current()->addTo( w );
}
return TRUE;
}
/*! \reimp
*/
bool QActionGroup::removeFrom( QWidget* w )
{
for ( QPtrListIterator<QAction> it( d->actions); it.current(); ++it ) {
it.current()->removeFrom( w );
}
#ifndef QT_NO_TOOLBAR
if ( ::qt_cast<QToolBar*>(w) ) {
QPtrListIterator<QComboBox> cb( d->comboboxes );
while( cb.current() ) {
QComboBox *box = cb.current();
++cb;
if ( box->parentWidget() == w )
delete box;
}
QPtrListIterator<QToolButton> mb( d->menubuttons );
while( mb.current() ) {
QToolButton *btn = mb.current();
++mb;
if ( btn->parentWidget() == w )
delete btn;
}
} else
#endif
if ( ::qt_cast<QPopupMenu*>(w) ) {
QPtrListIterator<QActionGroupPrivate::MenuItem> pu( d->menuitems );
while ( pu.current() ) {
QActionGroupPrivate::MenuItem *mi = pu.current();
++pu;
if ( d->dropdown && mi->popup )
( (QPopupMenu*)w )->removeItem( mi->id );
delete mi->popup;
}
}
return TRUE;
}
/*! \internal
*/
void QActionGroup::childToggled( bool b )
{
if ( !isExclusive() )
return;
QAction* s = (QAction*) sender();
if ( b ) {
if ( s != d->selected ) {
d->selected = s;
for ( QPtrListIterator<QAction> it( d->actions); it.current(); ++it ) {
if ( it.current()->isToggleAction() && it.current() != s )
it.current()->setOn( FALSE );
}
emit activated();
emit selected( s );
} else if ( !s->isToggleAction() ) {
emit activated();
}
} else {
if ( s == d->selected ) {
// at least one has to be selected
s->setOn( TRUE );
}
}
}
/*! \internal
*/
void QActionGroup::childDestroyed()
{
d->actions.removeRef( (QAction*) sender() );
if ( d->selected == sender() )
d->selected = 0;
}
/*! \reimp
*/
void QActionGroup::setEnabled( bool enable )
{
if ( enable == isEnabled() )
return;
QAction::setEnabled( enable );
d->update( this );
}
/*! \reimp
*/
void QActionGroup::setToggleAction( bool toggle )
{
for ( QPtrListIterator<QAction> it( d->actions); it.current(); ++it )
it.current()->setToggleAction( toggle );
QAction::setToggleAction( TRUE );
d->update( this );
}
/*! \reimp
*/
void QActionGroup::setOn( bool on )
{
for ( QPtrListIterator<QAction> it( d->actions); it.current(); ++it ) {
QAction *act = it.current();
if ( act->isToggleAction() )
act->setOn( on );
}
QAction::setOn( on );
d->update( this );
}
/*! \reimp
*/
void QActionGroup::setIconSet( const QIconSet& icon )
{
QAction::setIconSet( icon );
d->update( this );
}
/*! \reimp
*/
void QActionGroup::setText( const QString& txt )
{
if ( txt == text() )
return;
QAction::setText( txt );
d->update( this );
}
/*! \reimp
*/
void QActionGroup::setMenuText( const QString& text )
{
if ( text == menuText() )
return;
QAction::setMenuText( text );
d->update( this );
}
/*! \reimp
*/
void QActionGroup::setToolTip( const QString& text )
{
if ( text == toolTip() )
return;
for ( QPtrListIterator<QAction> it( d->actions); it.current(); ++it ) {
if ( it.current()->toolTip().isNull() )
it.current()->setToolTip( text );
}
QAction::setToolTip( text );
d->update( this );
}
/*! \reimp
*/
void QActionGroup::setWhatsThis( const QString& text )
{
if ( text == whatsThis() )
return;
for ( QPtrListIterator<QAction> it( d->actions); it.current(); ++it ) {
if ( it.current()->whatsThis().isNull() )
it.current()->setWhatsThis( text );
}
QAction::setWhatsThis( text );
d->update( this );
}
/*! \reimp
*/
void QActionGroup::childEvent( QChildEvent *e )
{
if ( !e->removed() )
return;
QAction *action = ::qt_cast<QAction*>(e->child());
if ( !action )
return;
for ( QPtrListIterator<QComboBox> cb( d->comboboxes ); cb.current(); ++cb ) {
for ( int i = 0; i < cb.current()->count(); i++ ) {
if ( cb.current()->text( i ) == action->text() ) {
cb.current()->removeItem( i );
break;
}
}
}
for ( QPtrListIterator<QToolButton> mb( d->menubuttons ); mb.current(); ++mb ) {
QPopupMenu* popup = mb.current()->popup();
if ( !popup )
continue;
action->removeFrom( popup );
}
for ( QPtrListIterator<QActionGroupPrivate::MenuItem> mi( d->menuitems ); mi.current(); ++mi ) {
QPopupMenu* popup = mi.current()->popup;
if ( !popup )
continue;
action->removeFrom( popup );
}
}
/*!
\fn void QActionGroup::selected( QAction* )
This signal is emitted from exclusive groups when toggle actions
change state.
The argument is the action whose state changed to "on".
\sa setExclusive(), isOn() QAction::toggled()
*/
/*! \internal
*/
void QActionGroup::internalComboBoxActivated( int index )
{
QAction *a = 0;
for ( int i = 0; i <= index && i < (int)d->actions.count(); ++i ) {
a = d->actions.at( i );
if ( a && !qstrcmp( a->name(), "qt_separator_action" ) )
index++;
}
a = d->actions.at( index );
if ( a ) {
if ( a != d->selected ) {
d->selected = a;
for ( QPtrListIterator<QAction> it( d->actions); it.current(); ++it ) {
if ( it.current()->isToggleAction() && it.current() != a )
it.current()->setOn( FALSE );
}
if ( a->isToggleAction() )
a->setOn( TRUE );
emit activated();
if ( a->isToggleAction() )
emit selected( d->selected );
emit ((QActionGroup*)a)->activated();
} else if ( !a->isToggleAction() ) {
emit activated();
emit ((QActionGroup*)a)->activated();
}
a->clearStatusText();
}
}
/*! \internal
*/
void QActionGroup::internalComboBoxHighlighted( int index )
{
QAction *a = 0;
for ( int i = 0; i <= index && i < (int)d->actions.count(); ++i ) {
a = d->actions.at( i );
if ( a && !qstrcmp( a->name(), "qt_separator_action" ) )
index++;
}
a = d->actions.at( index );
if ( a )
a->showStatusText(a->statusTip());
else
clearStatusText();
}
/*! \internal
*/
void QActionGroup::internalToggle( QAction *a )
{
int index = d->actions.find( a );
if ( index == -1 )
return;
int lastItem = index;
for ( int i = 0; i < lastItem; i++ ) {
QAction *action = d->actions.at( i );
if ( !qstrcmp( action->name(), "qt_separator_action" ) )
index--;
}
for ( QPtrListIterator<QComboBox> it( d->comboboxes); it.current(); ++it )
it.current()->setCurrentItem( index );
}
/*! \internal
*/
void QActionGroup::objectDestroyed()
{
const QObject* obj = sender();
d->menubuttons.removeRef( (QToolButton*)obj );
for ( QPtrListIterator<QActionGroupPrivate::MenuItem> mi( d->menuitems ); mi.current(); ++mi ) {
if ( mi.current()->popup == obj ) {
d->menuitems.removeRef( mi.current() );
break;
}
}
d->popupmenus.removeRef( (QPopupMenu*)obj );
d->comboboxes.removeRef( (QComboBox*)obj );
}
/*!
\internal
This function is called from the addTo() function when it has
created a widget (\a actionWidget) for the child action \a a in
the \a container.
*/
void QActionGroup::addedTo( QWidget *actionWidget, QWidget *container, QAction *a )
{
Q_UNUSED( actionWidget );
Q_UNUSED( container );
Q_UNUSED( a );
}
/*!
\overload
\internal
This function is called from the addTo() function when it has
created a menu item for the child action at the index position \a
index in the popup menu \a menu.
*/
void QActionGroup::addedTo( int index, QPopupMenu *menu, QAction *a )
{
Q_UNUSED( index );
Q_UNUSED( menu );
Q_UNUSED( a );
}
/*!
\reimp
\overload
This function is called from the addTo() function when it has
created a widget (\a actionWidget) in the \a container.
*/
void QActionGroup::addedTo( QWidget *actionWidget, QWidget *container )
{
Q_UNUSED( actionWidget );
Q_UNUSED( container );
}
/*!
\reimp
\overload
This function is called from the addTo() function when it has
created a menu item at the index position \a index in the popup
menu \a menu.
*/
void QActionGroup::addedTo( int index, QPopupMenu *menu )
{
Q_UNUSED( index );
Q_UNUSED( menu );
}
#endif
|