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
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qstandarditemmodel.h"
#ifndef QT_NO_STANDARDITEMMODEL
#include <QtCore/qdatetime.h>
#include <QtCore/qlist.h>
#include <QtCore/qmap.h>
#include <QtCore/qpair.h>
#include <QtCore/qvariant.h>
#include <QtCore/qvector.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qbitarray.h>
#include <QtCore/qmimedata.h>
#include <private/qstandarditemmodel_p.h>
#include <qdebug.h>
QT_BEGIN_NAMESPACE
class QStandardItemModelLessThan
{
public:
inline QStandardItemModelLessThan()
{ }
inline bool operator()(const QPair<QStandardItem*, int> &l,
const QPair<QStandardItem*, int> &r) const
{
return *(l.first) < *(r.first);
}
};
class QStandardItemModelGreaterThan
{
public:
inline QStandardItemModelGreaterThan()
{ }
inline bool operator()(const QPair<QStandardItem*, int> &l,
const QPair<QStandardItem*, int> &r) const
{
return *(r.first) < *(l.first);
}
};
/*!
\internal
*/
QStandardItemPrivate::~QStandardItemPrivate()
{
QVector<QStandardItem*>::const_iterator it;
for (it = children.constBegin(); it != children.constEnd(); ++it) {
QStandardItem *child = *it;
if (child)
child->d_func()->setModel(0);
delete child;
}
children.clear();
if (parent && model)
parent->d_func()->childDeleted(q_func());
}
/*!
\internal
*/
QPair<int, int> QStandardItemPrivate::position() const
{
if (QStandardItem *par = parent) {
int idx = par->d_func()->childIndex(q_func());
if (idx == -1)
return QPair<int, int>(-1, -1);
return QPair<int, int>(idx / par->columnCount(), idx % par->columnCount());
}
// ### support header items?
return QPair<int, int>(-1, -1);
}
/*!
\internal
*/
void QStandardItemPrivate::setChild(int row, int column, QStandardItem *item,
bool emitChanged)
{
Q_Q(QStandardItem);
if (item == q) {
qWarning("QStandardItem::setChild: Can't make an item a child of itself %p",
item);
return;
}
if ((row < 0) || (column < 0))
return;
if (rows <= row)
q->setRowCount(row + 1);
if (columns <= column)
q->setColumnCount(column + 1);
int index = childIndex(row, column);
Q_ASSERT(index != -1);
QStandardItem *oldItem = children.at(index);
if (item == oldItem)
return;
if (item) {
if (item->d_func()->parent == 0) {
item->d_func()->setParentAndModel(q, model);
} else {
qWarning("QStandardItem::setChild: Ignoring duplicate insertion of item %p",
item);
return;
}
}
if (oldItem)
oldItem->d_func()->setModel(0);
delete oldItem;
children.replace(index, item);
if (emitChanged && model)
model->d_func()->itemChanged(item);
}
/*!
\internal
*/
void QStandardItemPrivate::changeFlags(bool enable, Qt::ItemFlags f)
{
Q_Q(QStandardItem);
Qt::ItemFlags flags = q->flags();
if (enable)
flags |= f;
else
flags &= ~f;
q->setFlags(flags);
}
/*!
\internal
*/
void QStandardItemPrivate::childDeleted(QStandardItem *child)
{
int index = childIndex(child);
Q_ASSERT(index != -1);
children.replace(index, 0);
}
/*!
\internal
*/
void QStandardItemPrivate::setItemData(const QMap<int, QVariant> &roles)
{
Q_Q(QStandardItem);
//let's build the vector of new values
QVector<QWidgetItemData> newValues;
QMap<int, QVariant>::const_iterator it;
for (it = roles.begin(); it != roles.end(); ++it) {
QVariant value = it.value();
if (value.isValid()) {
int role = it.key();
role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
QWidgetItemData wid(role,it.value());
newValues.append(wid);
}
}
if (values!=newValues) {
values=newValues;
if (model)
model->d_func()->itemChanged(q);
}
}
/*!
\internal
*/
const QMap<int, QVariant> QStandardItemPrivate::itemData() const
{
QMap<int, QVariant> result;
QVector<QWidgetItemData>::const_iterator it;
for (it = values.begin(); it != values.end(); ++it)
result.insert((*it).role, (*it).value);
return result;
}
/*!
\internal
*/
void QStandardItemPrivate::sortChildren(int column, Qt::SortOrder order)
{
Q_Q(QStandardItem);
if (column >= columnCount())
return;
QVector<QPair<QStandardItem*, int> > sortable;
QVector<int> unsortable;
sortable.reserve(rowCount());
unsortable.reserve(rowCount());
for (int row = 0; row < rowCount(); ++row) {
QStandardItem *itm = q->child(row, column);
if (itm)
sortable.append(QPair<QStandardItem*,int>(itm, row));
else
unsortable.append(row);
}
if (order == Qt::AscendingOrder) {
QStandardItemModelLessThan lt;
qStableSort(sortable.begin(), sortable.end(), lt);
} else {
QStandardItemModelGreaterThan gt;
qStableSort(sortable.begin(), sortable.end(), gt);
}
QModelIndexList changedPersistentIndexesFrom, changedPersistentIndexesTo;
QVector<QStandardItem*> sorted_children(children.count());
for (int i = 0; i < rowCount(); ++i) {
int r = (i < sortable.count()
? sortable.at(i).second
: unsortable.at(i - sortable.count()));
for (int c = 0; c < columnCount(); ++c) {
QStandardItem *itm = q->child(r, c);
sorted_children[childIndex(i, c)] = itm;
if (model) {
QModelIndex from = model->createIndex(r, c, q);
if (model->d_func()->persistent.indexes.contains(from)) {
QModelIndex to = model->createIndex(i, c, q);
changedPersistentIndexesFrom.append(from);
changedPersistentIndexesTo.append(to);
}
}
}
}
children = sorted_children;
if (model) {
model->changePersistentIndexList(changedPersistentIndexesFrom, changedPersistentIndexesTo);
}
QVector<QStandardItem*>::iterator it;
for (it = children.begin(); it != children.end(); ++it) {
if (*it)
(*it)->d_func()->sortChildren(column, order);
}
}
/*!
\internal
set the model of this item and all its children
*/
void QStandardItemPrivate::setModel(QStandardItemModel *mod)
{
if (children.isEmpty()) {
if (model)
model->d_func()->invalidatePersistentIndex(model->indexFromItem(q_ptr));
model = mod;
} else {
QStack<QStandardItem*> stack;
stack.push(q_ptr);
while (!stack.isEmpty()) {
QStandardItem *itm = stack.pop();
if (itm->d_func()->model) {
itm->d_func()->model->d_func()->invalidatePersistentIndex(itm->d_func()->model->indexFromItem(itm));
}
itm->d_func()->model = mod;
const QVector<QStandardItem*> &childList = itm->d_func()->children;
for (int i = 0; i < childList.count(); ++i) {
QStandardItem *chi = childList.at(i);
if (chi)
stack.push(chi);
}
}
}
}
/*!
\internal
*/
QStandardItemModelPrivate::QStandardItemModelPrivate()
: root(new QStandardItem),
itemPrototype(0),
sortRole(Qt::DisplayRole)
{
root->setFlags(Qt::ItemIsDropEnabled);
}
/*!
\internal
*/
QStandardItemModelPrivate::~QStandardItemModelPrivate()
{
delete itemPrototype;
qDeleteAll(columnHeaderItems);
qDeleteAll(rowHeaderItems);
}
/*!
\internal
*/
void QStandardItemModelPrivate::init()
{
Q_Q(QStandardItemModel);
QObject::connect(q, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
q, SLOT(_q_emitItemChanged(QModelIndex,QModelIndex)));
}
/*!
\internal
*/
void QStandardItemModelPrivate::_q_emitItemChanged(const QModelIndex &topLeft,
const QModelIndex &bottomRight)
{
Q_Q(QStandardItemModel);
QModelIndex parent = topLeft.parent();
for (int row = topLeft.row(); row <= bottomRight.row(); ++row) {
for (int column = topLeft.column(); column <= bottomRight.column(); ++column) {
QModelIndex index = q->index(row, column, parent);
if (QStandardItem *item = itemFromIndex(index))
emit q->itemChanged(item);
}
}
}
/*!
\internal
*/
bool QStandardItemPrivate::insertRows(int row, const QList<QStandardItem*> &items)
{
Q_Q(QStandardItem);
if ((row < 0) || (row > rowCount()))
return false;
int count = items.count();
if (model)
model->d_func()->rowsAboutToBeInserted(q, row, row + count - 1);
if (rowCount() == 0) {
if (columnCount() == 0)
q->setColumnCount(1);
children.resize(columnCount() * count);
rows = count;
} else {
rows += count;
int index = childIndex(row, 0);
if (index != -1)
children.insert(index, columnCount() * count, 0);
}
for (int i = 0; i < items.count(); ++i) {
QStandardItem *item = items.at(i);
item->d_func()->model = model;
item->d_func()->parent = q;
int index = childIndex(i + row, 0);
children.replace(index, item);
}
if (model)
model->d_func()->rowsInserted(q, row, count);
return true;
}
bool QStandardItemPrivate::insertRows(int row, int count, const QList<QStandardItem*> &items)
{
Q_Q(QStandardItem);
if ((count < 1) || (row < 0) || (row > rowCount()))
return false;
if (model)
model->d_func()->rowsAboutToBeInserted(q, row, row + count - 1);
if (rowCount() == 0) {
children.resize(columnCount() * count);
rows = count;
} else {
rows += count;
int index = childIndex(row, 0);
if (index != -1)
children.insert(index, columnCount() * count, 0);
}
if (!items.isEmpty()) {
int index = childIndex(row, 0);
int limit = qMin(items.count(), columnCount() * count);
for (int i = 0; i < limit; ++i) {
QStandardItem *item = items.at(i);
if (item) {
if (item->d_func()->parent == 0) {
item->d_func()->setParentAndModel(q, model);
} else {
qWarning("QStandardItem::insertRows: Ignoring duplicate insertion of item %p",
item);
item = 0;
}
}
children.replace(index, item);
++index;
}
}
if (model)
model->d_func()->rowsInserted(q, row, count);
return true;
}
/*!
\internal
*/
bool QStandardItemPrivate::insertColumns(int column, int count, const QList<QStandardItem*> &items)
{
Q_Q(QStandardItem);
if ((count < 1) || (column < 0) || (column > columnCount()))
return false;
if (model)
model->d_func()->columnsAboutToBeInserted(q, column, column + count - 1);
if (columnCount() == 0) {
children.resize(rowCount() * count);
columns = count;
} else {
columns += count;
int index = childIndex(0, column);
for (int row = 0; row < rowCount(); ++row) {
children.insert(index, count, 0);
index += columnCount();
}
}
if (!items.isEmpty()) {
int limit = qMin(items.count(), rowCount() * count);
for (int i = 0; i < limit; ++i) {
QStandardItem *item = items.at(i);
if (item) {
if (item->d_func()->parent == 0) {
item->d_func()->setParentAndModel(q, model);
} else {
qWarning("QStandardItem::insertColumns: Ignoring duplicate insertion of item %p",
item);
item = 0;
}
}
int r = i / count;
int c = column + (i % count);
int index = childIndex(r, c);
children.replace(index, item);
}
}
if (model)
model->d_func()->columnsInserted(q, column, count);
return true;
}
/*!
\internal
*/
void QStandardItemModelPrivate::itemChanged(QStandardItem *item)
{
Q_Q(QStandardItemModel);
if (item->d_func()->parent == 0) {
// Header item
int idx = columnHeaderItems.indexOf(item);
if (idx != -1) {
emit q->headerDataChanged(Qt::Horizontal, idx, idx);
} else {
idx = rowHeaderItems.indexOf(item);
if (idx != -1)
emit q->headerDataChanged(Qt::Vertical, idx, idx);
}
} else {
// Normal item
QModelIndex index = q->indexFromItem(item);
emit q->dataChanged(index, index);
}
}
/*!
\internal
*/
void QStandardItemModelPrivate::rowsAboutToBeInserted(QStandardItem *parent,
int start, int end)
{
Q_Q(QStandardItemModel);
QModelIndex index = q->indexFromItem(parent);
q->beginInsertRows(index, start, end);
}
/*!
\internal
*/
void QStandardItemModelPrivate::columnsAboutToBeInserted(QStandardItem *parent,
int start, int end)
{
Q_Q(QStandardItemModel);
QModelIndex index = q->indexFromItem(parent);
q->beginInsertColumns(index, start, end);
}
/*!
\internal
*/
void QStandardItemModelPrivate::rowsAboutToBeRemoved(QStandardItem *parent,
int start, int end)
{
Q_Q(QStandardItemModel);
QModelIndex index = q->indexFromItem(parent);
q->beginRemoveRows(index, start, end);
}
/*!
\internal
*/
void QStandardItemModelPrivate::columnsAboutToBeRemoved(QStandardItem *parent,
int start, int end)
{
Q_Q(QStandardItemModel);
QModelIndex index = q->indexFromItem(parent);
q->beginRemoveColumns(index, start, end);
}
/*!
\internal
*/
void QStandardItemModelPrivate::rowsInserted(QStandardItem *parent,
int row, int count)
{
Q_Q(QStandardItemModel);
if (parent == root.data())
rowHeaderItems.insert(row, count, 0);
q->endInsertRows();
}
/*!
\internal
*/
void QStandardItemModelPrivate::columnsInserted(QStandardItem *parent,
int column, int count)
{
Q_Q(QStandardItemModel);
if (parent == root.data())
columnHeaderItems.insert(column, count, 0);
q->endInsertColumns();
}
/*!
\internal
*/
void QStandardItemModelPrivate::rowsRemoved(QStandardItem *parent,
int row, int count)
{
Q_Q(QStandardItemModel);
if (parent == root.data()) {
for (int i = row; i < row + count; ++i) {
QStandardItem *oldItem = rowHeaderItems.at(i);
if (oldItem)
oldItem->d_func()->setModel(0);
delete oldItem;
}
rowHeaderItems.remove(row, count);
}
q->endRemoveRows();
}
/*!
\internal
*/
void QStandardItemModelPrivate::columnsRemoved(QStandardItem *parent,
int column, int count)
{
Q_Q(QStandardItemModel);
if (parent == root.data()) {
for (int i = column; i < column + count; ++i) {
QStandardItem *oldItem = columnHeaderItems.at(i);
if (oldItem)
oldItem->d_func()->setModel(0);
delete oldItem;
}
columnHeaderItems.remove(column, count);
}
q->endRemoveColumns();
}
/*!
\class QStandardItem
\brief The QStandardItem class provides an item for use with the
QStandardItemModel class.
\since 4.2
\ingroup model-view
\inmodule QtWidgets
Items usually contain text, icons, or checkboxes.
Each item can have its own background brush which is set with the
setBackground() function. The current background brush can be found with
background(). The text label for each item can be rendered with its own
font and brush. These are specified with the setFont() and setForeground()
functions, and read with font() and foreground().
By default, items are enabled, editable, selectable, checkable, and can be
used both as the source of a drag and drop operation and as a drop target.
Each item's flags can be changed by calling setFlags(). Checkable items
can be checked and unchecked with the setCheckState() function. The
corresponding checkState() function indicates whether the item is
currently checked.
You can store application-specific data in an item by calling setData().
Each item can have a two-dimensional table of child items. This makes it
possible to build hierarchies of items. The typical hierarchy is the tree,
in which case the child table is a table with a single column (a list).
The dimensions of the child table can be set with setRowCount() and
setColumnCount(). Items can be positioned in the child table with
setChild(). Get a pointer to a child item with child(). New rows and
columns of children can also be inserted with insertRow() and
insertColumn(), or appended with appendRow() and appendColumn(). When
using the append and insert functions, the dimensions of the child table
will grow as needed.
An existing row of children can be removed with removeRow() or takeRow();
correspondingly, a column can be removed with removeColumn() or
takeColumn().
An item's children can be sorted by calling sortChildren().
\section1 Subclassing
When subclassing QStandardItem to provide custom items, it is possible to
define new types for them so that they can be distinguished from the base
class. The type() function should be reimplemented to return a new type
value equal to or greater than \l UserType.
Reimplement data() and setData() if you want to perform custom handling of
data queries and/or control how an item's data is represented.
Reimplement clone() if you want QStandardItemModel to be able to create
instances of your custom item class on demand (see
QStandardItemModel::setItemPrototype()).
Reimplement read() and write() if you want to control how items are
represented in their serialized form.
Reimplement \l{operator<()} if you want to control the semantics of item
comparison. \l{operator<()} determines the sorted order when sorting items
with sortChildren() or with QStandardItemModel::sort().
\sa QStandardItemModel, {Item View Convenience Classes}, {Model/View Programming}
*/
/*!
\enum QStandardItem::ItemType
This enum describes the types that are used to describe standard items.
\value Type The default type for standard items.
\value UserType The minimum value for custom types. Values below UserType are
reserved by Qt.
You can define new user types in QStandardItem subclasses to ensure that
custom items are treated specially; for example, when items are sorted.
\sa type()
*/
/*!
Constructs an item.
*/
QStandardItem::QStandardItem()
: d_ptr(new QStandardItemPrivate)
{
Q_D(QStandardItem);
d->q_ptr = this;
}
/*!
Constructs an item with the given \a text.
*/
QStandardItem::QStandardItem(const QString &text)
: d_ptr(new QStandardItemPrivate)
{
Q_D(QStandardItem);
d->q_ptr = this;
setText(text);
}
/*!
Constructs an item with the given \a icon and \a text.
*/
QStandardItem::QStandardItem(const QIcon &icon, const QString &text)
: d_ptr(new QStandardItemPrivate)
{
Q_D(QStandardItem);
d->q_ptr = this;
setIcon(icon);
setText(text);
}
/*!
Constructs an item with \a rows rows and \a columns columns of child items.
*/
QStandardItem::QStandardItem(int rows, int columns)
: d_ptr(new QStandardItemPrivate)
{
Q_D(QStandardItem);
d->q_ptr = this;
setRowCount(rows);
setColumnCount(columns);
}
/*!
\internal
*/
QStandardItem::QStandardItem(QStandardItemPrivate &dd)
: d_ptr(&dd)
{
Q_D(QStandardItem);
d->q_ptr = this;
}
/*!
Constructs a copy of \a other. Note that model() is
not copied.
This function is useful when reimplementing clone().
*/
QStandardItem::QStandardItem(const QStandardItem &other)
: d_ptr(new QStandardItemPrivate)
{
Q_D(QStandardItem);
d->q_ptr = this;
operator=(other);
}
/*!
Assigns \a other's data and flags to this item. Note that
type() and model() are not copied.
This function is useful when reimplementing clone().
*/
QStandardItem &QStandardItem::operator=(const QStandardItem &other)
{
Q_D(QStandardItem);
d->values = other.d_func()->values;
return *this;
}
/*!
Destructs the item.
This causes the item's children to be destructed as well.
*/
QStandardItem::~QStandardItem()
{
}
/*!
Returns the item's parent item, or 0 if the item has no parent.
\sa child()
*/
QStandardItem *QStandardItem::parent() const
{
Q_D(const QStandardItem);
if (!d->model || (d->model->d_func()->root.data() != d->parent))
return d->parent;
return 0;
}
/*!
Sets the item's data for the given \a role to the specified \a value.
If you subclass QStandardItem and reimplement this function, your
reimplementation should call emitDataChanged() if you do not call
the base implementation of setData(). This will ensure that e.g.
views using the model are notified of the changes.
\note The default implementation treats Qt::EditRole and Qt::DisplayRole
as referring to the same data.
\sa Qt::ItemDataRole, data(), setFlags()
*/
void QStandardItem::setData(const QVariant &value, int role)
{
Q_D(QStandardItem);
role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
QVector<QWidgetItemData>::iterator it;
for (it = d->values.begin(); it != d->values.end(); ++it) {
if ((*it).role == role) {
if (value.isValid()) {
if ((*it).value.type() == value.type() && (*it).value == value)
return;
(*it).value = value;
} else {
d->values.erase(it);
}
if (d->model)
d->model->d_func()->itemChanged(this);
return;
}
}
d->values.append(QWidgetItemData(role, value));
if (d->model)
d->model->d_func()->itemChanged(this);
}
/*!
Returns the item's data for the given \a role, or an invalid
QVariant if there is no data for the role.
\note The default implementation treats Qt::EditRole and Qt::DisplayRole
as referring to the same data.
*/
QVariant QStandardItem::data(int role) const
{
Q_D(const QStandardItem);
role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
QVector<QWidgetItemData>::const_iterator it;
for (it = d->values.begin(); it != d->values.end(); ++it) {
if ((*it).role == role)
return (*it).value;
}
return QVariant();
}
/*!
\since 4.4
Causes the model associated with this item to emit a
\l{QAbstractItemModel::dataChanged()}{dataChanged}() signal for this
item.
You normally only need to call this function if you have subclassed
QStandardItem and reimplemented data() and/or setData().
\sa setData()
*/
void QStandardItem::emitDataChanged()
{
Q_D(QStandardItem);
if (d->model)
d->model->d_func()->itemChanged(this);
}
/*!
Sets the item flags for the item to \a flags.
The item flags determine how the user can interact with the item.
This is often used to disable an item.
\sa flags(), setData()
*/
void QStandardItem::setFlags(Qt::ItemFlags flags)
{
setData((int)flags, Qt::UserRole - 1);
}
/*!
Returns the item flags for the item.
The item flags determine how the user can interact with the item.
By default, items are enabled, editable, selectable, checkable, and can be
used both as the source of a drag and drop operation and as a drop target.
\sa setFlags()
*/
Qt::ItemFlags QStandardItem::flags() const
{
QVariant v = data(Qt::UserRole - 1);
if (!v.isValid())
return (Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable
|Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled);
return Qt::ItemFlags(v.toInt());
}
/*!
\fn QString QStandardItem::text() const
Returns the item's text. This is the text that's presented to the user
in a view.
\sa setText()
*/
/*!
\fn void QStandardItem::setText(const QString &text)
Sets the item's text to the \a text specified.
\sa text(), setFont(), setForeground()
*/
/*!
\fn QIcon QStandardItem::icon() const
Returns the item's icon.
\sa setIcon(), {QAbstractItemView::iconSize}{iconSize}
*/
/*!
\fn void QStandardItem::setIcon(const QIcon &icon)
Sets the item's icon to the \a icon specified.
*/
/*!
\fn QString QStandardItem::statusTip() const
Returns the item's status tip.
\sa setStatusTip(), toolTip(), whatsThis()
*/
/*!
\fn void QStandardItem::setStatusTip(const QString &statusTip)
Sets the item's status tip to the string specified by \a statusTip.
\sa statusTip(), setToolTip(), setWhatsThis()
*/
/*!
\fn QString QStandardItem::toolTip() const
Returns the item's tooltip.
\sa setToolTip(), statusTip(), whatsThis()
*/
/*!
\fn void QStandardItem::setToolTip(const QString &toolTip)
Sets the item's tooltip to the string specified by \a toolTip.
\sa toolTip(), setStatusTip(), setWhatsThis()
*/
/*!
\fn QString QStandardItem::whatsThis() const
Returns the item's "What's This?" help.
\sa setWhatsThis(), toolTip(), statusTip()
*/
/*!
\fn void QStandardItem::setWhatsThis(const QString &whatsThis)
Sets the item's "What's This?" help to the string specified by \a whatsThis.
\sa whatsThis(), setStatusTip(), setToolTip()
*/
/*!
\fn QFont QStandardItem::font() const
Returns the font used to render the item's text.
\sa setFont()
*/
/*!
\fn void QStandardItem::setFont(const QFont &font)
Sets the font used to display the item's text to the given \a font.
\sa font() setText() setForeground()
*/
/*!
\fn QBrush QStandardItem::background() const
Returns the brush used to render the item's background.
\sa foreground() setBackground()
*/
/*!
\fn void QStandardItem::setBackground(const QBrush &brush)
Sets the item's background brush to the specified \a brush.
\sa background() setForeground()
*/
/*!
\fn QBrush QStandardItem::foreground() const
Returns the brush used to render the item's foreground (e.g. text).
\sa setForeground() background()
*/
/*!
\fn void QStandardItem::setForeground(const QBrush &brush)
Sets the brush used to display the item's foreground (e.g. text) to the
given \a brush.
\sa foreground() setBackground() setFont()
*/
/*!
\fn int QStandardItem::textAlignment() const
Returns the text alignment for the item's text.
*/
/*!
\fn void QStandardItem::setTextAlignment(Qt::Alignment alignment)
Sets the text alignment for the item's text to the \a alignment
specified.
\sa textAlignment()
*/
/*!
\fn QSize QStandardItem::sizeHint() const
Returns the size hint set for the item, or an invalid QSize if no
size hint has been set.
If no size hint has been set, the item delegate will compute the
size hint based on the item data.
\sa setSizeHint()
*/
/*!
\fn void QStandardItem::setSizeHint(const QSize &size)
Sets the size hint for the item to be \a size.
If no size hint is set, the item delegate will compute the
size hint based on the item data.
\sa sizeHint()
*/
/*!
\fn Qt::CheckState QStandardItem::checkState() const
Returns the checked state of the item.
\sa setCheckState(), isCheckable()
*/
/*!
\fn void QStandardItem::setCheckState(Qt::CheckState state)
Sets the check state of the item to be \a state.
\sa checkState(), setCheckable()
*/
/*!
\fn QString QStandardItem::accessibleText() const
Returns the item's accessible text.
The accessible text is used by assistive technologies (i.e. for users who
cannot use conventional means of interaction).
\sa setAccessibleText(), accessibleDescription()
*/
/*!
\fn void QStandardItem::setAccessibleText(const QString &accessibleText)
Sets the item's accessible text to the string specified by \a accessibleText.
The accessible text is used by assistive technologies (i.e. for users who
cannot use conventional means of interaction).
\sa accessibleText(), setAccessibleDescription()
*/
/*!
\fn QString QStandardItem::accessibleDescription() const
Returns the item's accessible description.
The accessible description is used by assistive technologies (i.e. for
users who cannot use conventional means of interaction).
\sa setAccessibleDescription(), accessibleText()
*/
/*!
\fn void QStandardItem::setAccessibleDescription(const QString &accessibleDescription)
Sets the item's accessible description to the string specified by \a
accessibleDescription.
The accessible description is used by assistive technologies (i.e. for
users who cannot use conventional means of interaction).
\sa accessibleDescription(), setAccessibleText()
*/
/*!
Sets whether the item is enabled. If \a enabled is true, the item is enabled,
meaning that the user can interact with the item; if \a enabled is false, the
user cannot interact with the item.
This flag takes precedence over the other item flags; e.g. if an item is not
enabled, it cannot be selected by the user, even if the Qt::ItemIsSelectable
flag has been set.
\sa isEnabled(), Qt::ItemIsEnabled, setFlags()
*/
void QStandardItem::setEnabled(bool enabled)
{
Q_D(QStandardItem);
d->changeFlags(enabled, Qt::ItemIsEnabled);
}
/*!
\fn bool QStandardItem::isEnabled() const
Returns whether the item is enabled.
When an item is enabled, the user can interact with it. The possible
types of interaction are specified by the other item flags, such as
isEditable() and isSelectable().
The default value is true.
\sa setEnabled(), flags()
*/
/*!
Sets whether the item is editable. If \a editable is true, the item can be
edited by the user; otherwise, the user cannot edit the item.
How the user can edit items in a view is determined by the view's edit
triggers; see QAbstractItemView::editTriggers.
\sa isEditable(), setFlags()
*/
void QStandardItem::setEditable(bool editable)
{
Q_D(QStandardItem);
d->changeFlags(editable, Qt::ItemIsEditable);
}
/*!
\fn bool QStandardItem::isEditable() const
Returns whether the item can be edited by the user.
When an item is editable (and enabled), the user can edit the item by
invoking one of the view's edit triggers; see
QAbstractItemView::editTriggers.
The default value is true.
\sa setEditable(), flags()
*/
/*!
Sets whether the item is selectable. If \a selectable is true, the item
can be selected by the user; otherwise, the user cannot select the item.
You can control the selection behavior and mode by manipulating their
view properties; see QAbstractItemView::selectionMode and
QAbstractItemView::selectionBehavior.
\sa isSelectable(), setFlags()
*/
void QStandardItem::setSelectable(bool selectable)
{
Q_D(QStandardItem);
d->changeFlags(selectable, Qt::ItemIsSelectable);
}
/*!
\fn bool QStandardItem::isSelectable() const
Returns whether the item is selectable by the user.
The default value is true.
\sa setSelectable(), flags()
*/
/*!
Sets whether the item is user-checkable. If \a checkable is true, the
item can be checked by the user; otherwise, the user cannot check
the item.
The item delegate will render a checkable item with a check box next to the
item's text.
\sa isCheckable(), setCheckState(), setTristate()
*/
void QStandardItem::setCheckable(bool checkable)
{
Q_D(QStandardItem);
if (checkable && !isCheckable()) {
// make sure there's data for the checkstate role
if (!data(Qt::CheckStateRole).isValid())
setData(Qt::Unchecked, Qt::CheckStateRole);
}
d->changeFlags(checkable, Qt::ItemIsUserCheckable);
}
/*!
\fn bool QStandardItem::isCheckable() const
Returns whether the item is user-checkable.
The default value is false.
\sa setCheckable(), checkState(), isTristate()
*/
/*!
Sets whether the item is tristate. If \a tristate is true, the
item is checkable with three separate states; otherwise, the item
is checkable with two states. (Note that this also requires that
the item is checkable; see isCheckable().)
\sa isTristate(), setCheckable(), setCheckState()
*/
void QStandardItem::setTristate(bool tristate)
{
Q_D(QStandardItem);
d->changeFlags(tristate, Qt::ItemIsTristate);
}
/*!
\fn bool QStandardItem::isTristate() const
Returns whether the item is tristate; that is, if it's checkable with three
separate states.
The default value is false.
\sa setTristate(), isCheckable(), checkState()
*/
#ifndef QT_NO_DRAGANDDROP
/*!
Sets whether the item is drag enabled. If \a dragEnabled is true, the item
can be dragged by the user; otherwise, the user cannot drag the item.
Note that you also need to ensure that item dragging is enabled in the view;
see QAbstractItemView::dragEnabled.
\sa isDragEnabled(), setDropEnabled(), setFlags()
*/
void QStandardItem::setDragEnabled(bool dragEnabled)
{
Q_D(QStandardItem);
d->changeFlags(dragEnabled, Qt::ItemIsDragEnabled);
}
/*!
\fn bool QStandardItem::isDragEnabled() const
Returns whether the item is drag enabled. An item that is drag enabled can
be dragged by the user.
The default value is true.
Note that item dragging must be enabled in the view for dragging to work;
see QAbstractItemView::dragEnabled.
\sa setDragEnabled(), isDropEnabled(), flags()
*/
/*!
Sets whether the item is drop enabled. If \a dropEnabled is true, the item
can be used as a drop target; otherwise, it cannot.
Note that you also need to ensure that drops are enabled in the view; see
QWidget::acceptDrops(); and that the model supports the desired drop actions;
see QAbstractItemModel::supportedDropActions().
\sa isDropEnabled(), setDragEnabled(), setFlags()
*/
void QStandardItem::setDropEnabled(bool dropEnabled)
{
Q_D(QStandardItem);
d->changeFlags(dropEnabled, Qt::ItemIsDropEnabled);
}
/*!
\fn bool QStandardItem::isDropEnabled() const
Returns whether the item is drop enabled. When an item is drop enabled, it
can be used as a drop target.
The default value is true.
\sa setDropEnabled(), isDragEnabled(), flags()
*/
#endif // QT_NO_DRAGANDDROP
/*!
Returns the row where the item is located in its parent's child table, or
-1 if the item has no parent.
\sa column(), parent()
*/
int QStandardItem::row() const
{
Q_D(const QStandardItem);
QPair<int, int> pos = d->position();
return pos.first;
}
/*!
Returns the column where the item is located in its parent's child table,
or -1 if the item has no parent.
\sa row(), parent()
*/
int QStandardItem::column() const
{
Q_D(const QStandardItem);
QPair<int, int> pos = d->position();
return pos.second;
}
/*!
Returns the QModelIndex associated with this item.
When you need to invoke item functionality in a QModelIndex-based API (e.g.
QAbstractItemView), you can call this function to obtain an index that
corresponds to the item's location in the model.
If the item is not associated with a model, an invalid QModelIndex is
returned.
\sa model(), QStandardItemModel::itemFromIndex()
*/
QModelIndex QStandardItem::index() const
{
Q_D(const QStandardItem);
return d->model ? d->model->indexFromItem(this) : QModelIndex();
}
/*!
Returns the QStandardItemModel that this item belongs to.
If the item is not a child of another item that belongs to the model, this
function returns 0.
\sa index()
*/
QStandardItemModel *QStandardItem::model() const
{
Q_D(const QStandardItem);
return d->model;
}
/*!
Sets the number of child item rows to \a rows. If this is less than
rowCount(), the data in the unwanted rows is discarded.
\sa rowCount(), setColumnCount()
*/
void QStandardItem::setRowCount(int rows)
{
int rc = rowCount();
if (rc == rows)
return;
if (rc < rows)
insertRows(qMax(rc, 0), rows - rc);
else
removeRows(qMax(rows, 0), rc - rows);
}
/*!
Returns the number of child item rows that the item has.
\sa setRowCount(), columnCount()
*/
int QStandardItem::rowCount() const
{
Q_D(const QStandardItem);
return d->rowCount();
}
/*!
Sets the number of child item columns to \a columns. If this is less than
columnCount(), the data in the unwanted columns is discarded.
\sa columnCount(), setRowCount()
*/
void QStandardItem::setColumnCount(int columns)
{
int cc = columnCount();
if (cc == columns)
return;
if (cc < columns)
insertColumns(qMax(cc, 0), columns - cc);
else
removeColumns(qMax(columns, 0), cc - columns);
}
/*!
Returns the number of child item columns that the item has.
\sa setColumnCount(), rowCount()
*/
int QStandardItem::columnCount() const
{
Q_D(const QStandardItem);
return d->columnCount();
}
/*!
Inserts a row at \a row containing \a items. If necessary, the column
count is increased to the size of \a items.
\sa insertRows(), insertColumn()
*/
void QStandardItem::insertRow(int row, const QList<QStandardItem*> &items)
{
Q_D(QStandardItem);
if (row < 0)
return;
if (columnCount() < items.count())
setColumnCount(items.count());
d->insertRows(row, 1, items);
}
/*!
Inserts \a items at \a row. The column count wont be changed.
\sa insertRow(), insertColumn()
*/
void QStandardItem::insertRows(int row, const QList<QStandardItem*> &items)
{
Q_D(QStandardItem);
if (row < 0)
return;
d->insertRows(row, items);
}
/*!
Inserts a column at \a column containing \a items. If necessary,
the row count is increased to the size of \a items.
\sa insertColumns(), insertRow()
*/
void QStandardItem::insertColumn(int column, const QList<QStandardItem*> &items)
{
Q_D(QStandardItem);
if (column < 0)
return;
if (rowCount() < items.count())
setRowCount(items.count());
d->insertColumns(column, 1, items);
}
/*!
Inserts \a count rows of child items at row \a row.
\sa insertRow(), insertColumns()
*/
void QStandardItem::insertRows(int row, int count)
{
Q_D(QStandardItem);
if (rowCount() < row) {
count += row - rowCount();
row = rowCount();
}
d->insertRows(row, count, QList<QStandardItem*>());
}
/*!
Inserts \a count columns of child items at column \a column.
\sa insertColumn(), insertRows()
*/
void QStandardItem::insertColumns(int column, int count)
{
Q_D(QStandardItem);
if (columnCount() < column) {
count += column - columnCount();
column = columnCount();
}
d->insertColumns(column, count, QList<QStandardItem*>());
}
/*!
\fn void QStandardItem::appendRow(const QList<QStandardItem*> &items)
Appends a row containing \a items. If necessary, the column count is
increased to the size of \a items.
\sa insertRow()
*/
/*!
\fn void QStandardItem::appendRows(const QList<QStandardItem*> &items)
Appends rows containing \a items. The column count will not change.
\sa insertRow()
*/
/*!
\fn void QStandardItem::appendColumn(const QList<QStandardItem*> &items)
Appends a column containing \a items. If necessary, the row count is
increased to the size of \a items.
\sa insertColumn()
*/
/*!
\fn bool QStandardItemModel::insertRow(int row, const QModelIndex &parent)
Inserts a single row before the given \a row in the child items of the
\a parent specified. Returns true if the row is inserted; otherwise
returns false.
\sa insertRows(), insertColumn(), removeRow()
*/
/*!
\fn bool QStandardItemModel::insertColumn(int column, const QModelIndex &parent)
Inserts a single column before the given \a column in the child items of
the \a parent specified. Returns true if the column is inserted; otherwise
returns false.
\sa insertColumns(), insertRow(), removeColumn()
*/
/*!
\fn QStandardItem::insertRow(int row, QStandardItem *item)
\overload
Inserts a row at \a row containing \a item.
When building a list or a tree that has only one column, this function
provides a convenient way to insert a single new item.
*/
/*!
\fn QStandardItem::appendRow(QStandardItem *item)
\overload
Appends a row containing \a item.
When building a list or a tree that has only one column, this function
provides a convenient way to append a single new item.
*/
/*!
Removes the given \a row. The items that were in the row are deleted.
\sa takeRow(), removeRows(), removeColumn()
*/
void QStandardItem::removeRow(int row)
{
removeRows(row, 1);
}
/*!
Removes the given \a column. The items that were in the
column are deleted.
\sa takeColumn(), removeColumns(), removeRow()
*/
void QStandardItem::removeColumn(int column)
{
removeColumns(column, 1);
}
/*!
Removes \a count rows at row \a row. The items that were in those rows are
deleted.
\sa removeRow(), removeColumn()
*/
void QStandardItem::removeRows(int row, int count)
{
Q_D(QStandardItem);
if ((count < 1) || (row < 0) || ((row + count) > rowCount()))
return;
if (d->model)
d->model->d_func()->rowsAboutToBeRemoved(this, row, row + count - 1);
int i = d->childIndex(row, 0);
int n = count * d->columnCount();
for (int j = i; j < n+i; ++j) {
QStandardItem *oldItem = d->children.at(j);
if (oldItem)
oldItem->d_func()->setModel(0);
delete oldItem;
}
d->children.remove(qMax(i, 0), n);
d->rows -= count;
if (d->model)
d->model->d_func()->rowsRemoved(this, row, count);
}
/*!
Removes \a count columns at column \a column. The items that were in those
columns are deleted.
\sa removeColumn(), removeRows()
*/
void QStandardItem::removeColumns(int column, int count)
{
Q_D(QStandardItem);
if ((count < 1) || (column < 0) || ((column + count) > columnCount()))
return;
if (d->model)
d->model->d_func()->columnsAboutToBeRemoved(this, column, column + count - 1);
for (int row = d->rowCount() - 1; row >= 0; --row) {
int i = d->childIndex(row, column);
for (int j=i; j<i+count; ++j) {
QStandardItem *oldItem = d->children.at(j);
if (oldItem)
oldItem->d_func()->setModel(0);
delete oldItem;
}
d->children.remove(i, count);
}
d->columns -= count;
if (d->model)
d->model->d_func()->columnsRemoved(this, column, count);
}
/*!
Returns true if this item has any children; otherwise returns false.
\sa rowCount(), columnCount(), child()
*/
bool QStandardItem::hasChildren() const
{
return (rowCount() > 0) && (columnCount() > 0);
}
/*!
Sets the child item at (\a row, \a column) to \a item. This item (the parent
item) takes ownership of \a item. If necessary, the row count and column
count are increased to fit the item.
\sa child()
*/
void QStandardItem::setChild(int row, int column, QStandardItem *item)
{
Q_D(QStandardItem);
d->setChild(row, column, item, true);
}
/*!
\fn QStandardItem::setChild(int row, QStandardItem *item)
\overload
Sets the child at \a row to \a item.
*/
/*!
Returns the child item at (\a row, \a column) if one has been set; otherwise
returns 0.
\sa setChild(), takeChild(), parent()
*/
QStandardItem *QStandardItem::child(int row, int column) const
{
Q_D(const QStandardItem);
int index = d->childIndex(row, column);
if (index == -1)
return 0;
return d->children.at(index);
}
/*!
Removes the child item at (\a row, \a column) without deleting it, and returns
a pointer to the item. If there was no child at the given location, then
this function returns 0.
Note that this function, unlike takeRow() and takeColumn(), does not affect
the dimensions of the child table.
\sa child(), takeRow(), takeColumn()
*/
QStandardItem *QStandardItem::takeChild(int row, int column)
{
Q_D(QStandardItem);
QStandardItem *item = 0;
int index = d->childIndex(row, column);
if (index != -1) {
item = d->children.at(index);
if (item)
item->d_func()->setParentAndModel(0, 0);
d->children.replace(index, 0);
}
return item;
}
/*!
Removes \a row without deleting the row items, and returns a list of
pointers to the removed items. For items in the row that have not been
set, the corresponding pointers in the list will be 0.
\sa removeRow(), insertRow(), takeColumn()
*/
QList<QStandardItem*> QStandardItem::takeRow(int row)
{
Q_D(QStandardItem);
if ((row < 0) || (row >= rowCount()))
return QList<QStandardItem*>();
if (d->model)
d->model->d_func()->rowsAboutToBeRemoved(this, row, row);
QList<QStandardItem*> items;
int index = d->childIndex(row, 0); // Will return -1 if there are no columns
if (index != -1) {
int col_count = d->columnCount();
for (int column = 0; column < col_count; ++column) {
QStandardItem *ch = d->children.at(index + column);
if (ch)
ch->d_func()->setParentAndModel(0, 0);
items.append(ch);
}
d->children.remove(index, col_count);
}
d->rows--;
if (d->model)
d->model->d_func()->rowsRemoved(this, row, 1);
return items;
}
/*!
Removes \a column without deleting the column items, and returns a list of
pointers to the removed items. For items in the column that have not been
set, the corresponding pointers in the list will be 0.
\sa removeColumn(), insertColumn(), takeRow()
*/
QList<QStandardItem*> QStandardItem::takeColumn(int column)
{
Q_D(QStandardItem);
if ((column < 0) || (column >= columnCount()))
return QList<QStandardItem*>();
if (d->model)
d->model->d_func()->columnsAboutToBeRemoved(this, column, column);
QList<QStandardItem*> items;
for (int row = d->rowCount() - 1; row >= 0; --row) {
int index = d->childIndex(row, column);
QStandardItem *ch = d->children.at(index);
if (ch)
ch->d_func()->setParentAndModel(0, 0);
d->children.remove(index);
items.prepend(ch);
}
d->columns--;
if (d->model)
d->model->d_func()->columnsRemoved(this, column, 1);
return items;
}
/*!
Returns true if this item is less than \a other; otherwise returns false.
The default implementation uses the data for the item's sort role (see
QStandardItemModel::sortRole) to perform the comparison if the item
belongs to a model; otherwise, the data for the item's Qt::DisplayRole
(text()) is used to perform the comparison.
sortChildren() and QStandardItemModel::sort() use this function when
sorting items. If you want custom sorting, you can subclass QStandardItem
and reimplement this function.
*/
bool QStandardItem::operator<(const QStandardItem &other) const
{
const int role = model() ? model()->sortRole() : Qt::DisplayRole;
const QVariant l = data(role), r = other.data(role);
// this code is copied from QSortFilterProxyModel::lessThan()
switch (l.userType()) {
case QVariant::Invalid:
return (r.type() == QVariant::Invalid);
case QVariant::Int:
return l.toInt() < r.toInt();
case QVariant::UInt:
return l.toUInt() < r.toUInt();
case QVariant::LongLong:
return l.toLongLong() < r.toLongLong();
case QVariant::ULongLong:
return l.toULongLong() < r.toULongLong();
case QMetaType::Float:
return l.toFloat() < r.toFloat();
case QVariant::Double:
return l.toDouble() < r.toDouble();
case QVariant::Char:
return l.toChar() < r.toChar();
case QVariant::Date:
return l.toDate() < r.toDate();
case QVariant::Time:
return l.toTime() < r.toTime();
case QVariant::DateTime:
return l.toDateTime() < r.toDateTime();
case QVariant::String:
default:
return l.toString().compare(r.toString()) < 0;
}
}
/*!
Sorts the children of the item using the given \a order, by the values in
the given \a column.
\note This function is recursive, therefore it sorts the children of the
item, its grandchildren, etc.
\sa {operator<()}
*/
void QStandardItem::sortChildren(int column, Qt::SortOrder order)
{
Q_D(QStandardItem);
if ((column < 0) || (rowCount() == 0))
return;
if (d->model)
emit d->model->layoutAboutToBeChanged();
d->sortChildren(column, order);
if (d->model)
emit d->model->layoutChanged();
}
/*!
Returns a copy of this item. The item's children are not copied.
When subclassing QStandardItem, you can reimplement this function
to provide QStandardItemModel with a factory that it can use to
create new items on demand.
\sa QStandardItemModel::setItemPrototype(), operator=()
*/
QStandardItem *QStandardItem::clone() const
{
return new QStandardItem(*this);
}
/*!
Returns the type of this item. The type is used to distinguish custom
items from the base class. When subclassing QStandardItem, you should
reimplement this function and return a new value greater than or equal
to \l UserType.
\sa QStandardItem::Type
*/
int QStandardItem::type() const
{
return Type;
}
#ifndef QT_NO_DATASTREAM
/*!
Reads the item from stream \a in. Only the data and flags of the item are
read, not the child items.
\sa write()
*/
void QStandardItem::read(QDataStream &in)
{
Q_D(QStandardItem);
in >> d->values;
qint32 flags;
in >> flags;
setFlags(Qt::ItemFlags(flags));
}
/*!
Writes the item to stream \a out. Only the data and flags of the item
are written, not the child items.
\sa read()
*/
void QStandardItem::write(QDataStream &out) const
{
Q_D(const QStandardItem);
out << d->values;
out << flags();
}
/*!
\relates QStandardItem
\since 4.2
Reads a QStandardItem from stream \a in into \a item.
This operator uses QStandardItem::read().
\sa {Serializing Qt Data Types}
*/
QDataStream &operator>>(QDataStream &in, QStandardItem &item)
{
item.read(in);
return in;
}
/*!
\relates QStandardItem
\since 4.2
Writes the QStandardItem \a item to stream \a out.
This operator uses QStandardItem::write().
\sa {Serializing Qt Data Types}
*/
QDataStream &operator<<(QDataStream &out, const QStandardItem &item)
{
item.write(out);
return out;
}
#endif // QT_NO_DATASTREAM
/*!
\class QStandardItemModel
\brief The QStandardItemModel class provides a generic model for storing custom data.
\ingroup model-view
\inmodule QtWidgets
QStandardItemModel can be used as a repository for standard Qt
data types. It is one of the \l {Model/View Classes} and is part
of Qt's \l {Model/View Programming}{model/view} framework.
QStandardItemModel provides a classic item-based approach to working with
the model. The items in a QStandardItemModel are provided by
QStandardItem.
QStandardItemModel implements the QAbstractItemModel interface, which
means that the model can be used to provide data in any view that supports
that interface (such as QListView, QTableView and QTreeView, and your own
custom views). For performance and flexibility, you may want to subclass
QAbstractItemModel to provide support for different kinds of data
repositories. For example, the QDirModel provides a model interface to the
underlying file system.
When you want a list or tree, you typically create an empty
QStandardItemModel and use appendRow() to add items to the model, and
item() to access an item. If your model represents a table, you typically
pass the dimensions of the table to the QStandardItemModel constructor and
use setItem() to position items into the table. You can also use setRowCount()
and setColumnCount() to alter the dimensions of the model. To insert items,
use insertRow() or insertColumn(), and to remove items, use removeRow() or
removeColumn().
You can set the header labels of your model with setHorizontalHeaderLabels()
and setVerticalHeaderLabels().
You can search for items in the model with findItems(), and sort the model by
calling sort().
Call clear() to remove all items from the model.
An example usage of QStandardItemModel to create a table:
\snippet doc/src/snippets/code/src_gui_itemviews_qstandarditemmodel.cpp 0
An example usage of QStandardItemModel to create a tree:
\snippet doc/src/snippets/code/src_gui_itemviews_qstandarditemmodel.cpp 1
After setting the model on a view, you typically want to react to user
actions, such as an item being clicked. Since a QAbstractItemView provides
QModelIndex-based signals and functions, you need a way to obtain the
QStandardItem that corresponds to a given QModelIndex, and vice
versa. itemFromIndex() and indexFromItem() provide this mapping. Typical
usage of itemFromIndex() includes obtaining the item at the current index
in a view, and obtaining the item that corresponds to an index carried by
a QAbstractItemView signal, such as QAbstractItemView::clicked(). First
you connect the view's signal to a slot in your class:
\snippet doc/src/snippets/code/src_gui_itemviews_qstandarditemmodel.cpp 2
When you receive the signal, you call itemFromIndex() on the given model
index to get a pointer to the item:
\snippet doc/src/snippets/code/src_gui_itemviews_qstandarditemmodel.cpp 3
Conversely, you must obtain the QModelIndex of an item when you want to
invoke a model/view function that takes an index as argument. You can
obtain the index either by using the model's indexFromItem() function, or,
equivalently, by calling QStandardItem::index():
\snippet doc/src/snippets/code/src_gui_itemviews_qstandarditemmodel.cpp 4
You are, of course, not required to use the item-based approach; you could
instead rely entirely on the QAbstractItemModel interface when working with
the model, or use a combination of the two as appropriate.
\sa QStandardItem, {Model/View Programming}, QAbstractItemModel,
{itemviews/simpletreemodel}{Simple Tree Model example},
{Item View Convenience Classes}
*/
/*!
\fn void QStandardItemModel::itemChanged(QStandardItem *item)
\since 4.2
This signal is emitted whenever the data of \a item has changed.
*/
/*!
Constructs a new item model with the given \a parent.
*/
QStandardItemModel::QStandardItemModel(QObject *parent)
: QAbstractItemModel(*new QStandardItemModelPrivate, parent)
{
Q_D(QStandardItemModel);
d->init();
d->root->d_func()->setModel(this);
}
/*!
Constructs a new item model that initially has \a rows rows and \a columns
columns, and that has the given \a parent.
*/
QStandardItemModel::QStandardItemModel(int rows, int columns, QObject *parent)
: QAbstractItemModel(*new QStandardItemModelPrivate, parent)
{
Q_D(QStandardItemModel);
d->init();
d->root->insertColumns(0, columns);
d->columnHeaderItems.insert(0, columns, 0);
d->root->insertRows(0, rows);
d->rowHeaderItems.insert(0, rows, 0);
d->root->d_func()->setModel(this);
}
/*!
\internal
*/
QStandardItemModel::QStandardItemModel(QStandardItemModelPrivate &dd, QObject *parent)
: QAbstractItemModel(dd, parent)
{
Q_D(QStandardItemModel);
d->init();
}
/*!
Destructs the model. The model destroys all its items.
*/
QStandardItemModel::~QStandardItemModel()
{
}
/*!
Sets the item role names to \a roleNames.
*/
void QStandardItemModel::setItemRoleNames(const QHash<int,QByteArray> &roleNames)
{
Q_D(QStandardItemModel);
d->roleNames = roleNames;
}
/*!
Removes all items (including header items) from the model and sets the
number of rows and columns to zero.
\sa removeColumns(), removeRows()
*/
void QStandardItemModel::clear()
{
Q_D(QStandardItemModel);
d->root.reset(new QStandardItem);
d->root->d_func()->setModel(this);
qDeleteAll(d->columnHeaderItems);
d->columnHeaderItems.clear();
qDeleteAll(d->rowHeaderItems);
d->rowHeaderItems.clear();
reset();
}
/*!
\since 4.2
Returns a pointer to the QStandardItem associated with the given \a index.
Calling this function is typically the initial step when processing
QModelIndex-based signals from a view, such as
QAbstractItemView::activated(). In your slot, you call itemFromIndex(),
with the QModelIndex carried by the signal as argument, to obtain a
pointer to the corresponding QStandardItem.
Note that this function will lazily create an item for the index (using
itemPrototype()), and set it in the parent item's child table, if no item
already exists at that index.
If \a index is an invalid index, this function returns 0.
\sa indexFromItem()
*/
QStandardItem *QStandardItemModel::itemFromIndex(const QModelIndex &index) const
{
Q_D(const QStandardItemModel);
if ((index.row() < 0) || (index.column() < 0) || (index.model() != this))
return 0;
QStandardItem *parent = static_cast<QStandardItem*>(index.internalPointer());
if (parent == 0)
return 0;
QStandardItem *item = parent->child(index.row(), index.column());
// lazy part
if (item == 0) {
item = d->createItem();
parent->d_func()->setChild(index.row(), index.column(), item);
}
return item;
}
/*!
\since 4.2
Returns the QModelIndex associated with the given \a item.
Use this function when you want to perform an operation that requires the
QModelIndex of the item, such as
QAbstractItemView::scrollTo(). QStandardItem::index() is provided as
convenience; it is equivalent to calling this function.
\sa itemFromIndex(), QStandardItem::index()
*/
QModelIndex QStandardItemModel::indexFromItem(const QStandardItem *item) const
{
if (item && item->d_func()->parent) {
QPair<int, int> pos = item->d_func()->position();
return createIndex(pos.first, pos.second, item->d_func()->parent);
}
return QModelIndex();
}
/*!
\since 4.2
Sets the number of rows in this model to \a rows. If
this is less than rowCount(), the data in the unwanted rows
is discarded.
\sa setColumnCount()
*/
void QStandardItemModel::setRowCount(int rows)
{
Q_D(QStandardItemModel);
d->root->setRowCount(rows);
}
/*!
\since 4.2
Sets the number of columns in this model to \a columns. If
this is less than columnCount(), the data in the unwanted columns
is discarded.
\sa setRowCount()
*/
void QStandardItemModel::setColumnCount(int columns)
{
Q_D(QStandardItemModel);
d->root->setColumnCount(columns);
}
/*!
\since 4.2
Sets the item for the given \a row and \a column to \a item. The model
takes ownership of the item. If necessary, the row count and column count
are increased to fit the item. The previous item at the given location (if
there was one) is deleted.
\sa item()
*/
void QStandardItemModel::setItem(int row, int column, QStandardItem *item)
{
Q_D(QStandardItemModel);
d->root->d_func()->setChild(row, column, item, true);
}
/*!
\fn QStandardItemModel::setItem(int row, QStandardItem *item)
\overload
*/
/*!
\since 4.2
Returns the item for the given \a row and \a column if one has been set;
otherwise returns 0.
\sa setItem(), takeItem(), itemFromIndex()
*/
QStandardItem *QStandardItemModel::item(int row, int column) const
{
Q_D(const QStandardItemModel);
return d->root->child(row, column);
}
/*!
\since 4.2
Returns the model's invisible root item.
The invisible root item provides access to the model's top-level items
through the QStandardItem API, making it possible to write functions that
can treat top-level items and their children in a uniform way; for
example, recursive functions involving a tree model.
\note Calling \l{QAbstractItemModel::index()}{index()} on the QStandardItem object
retrieved from this function is not valid.
*/
QStandardItem *QStandardItemModel::invisibleRootItem() const
{
Q_D(const QStandardItemModel);
return d->root.data();
}
/*!
\since 4.2
Sets the horizontal header item for \a column to \a item. The model takes
ownership of the item. If necessary, the column count is increased to fit
the item. The previous header item (if there was one) is deleted.
\sa horizontalHeaderItem(), setHorizontalHeaderLabels(),
setVerticalHeaderItem()
*/
void QStandardItemModel::setHorizontalHeaderItem(int column, QStandardItem *item)
{
Q_D(QStandardItemModel);
if (column < 0)
return;
if (columnCount() <= column)
setColumnCount(column + 1);
QStandardItem *oldItem = d->columnHeaderItems.at(column);
if (item == oldItem)
return;
if (item) {
if (item->model() == 0) {
item->d_func()->setModel(this);
} else {
qWarning("QStandardItem::setHorizontalHeaderItem: Ignoring duplicate insertion of item %p",
item);
return;
}
}
if (oldItem)
oldItem->d_func()->setModel(0);
delete oldItem;
d->columnHeaderItems.replace(column, item);
emit headerDataChanged(Qt::Horizontal, column, column);
}
/*!
\since 4.2
Returns the horizontal header item for \a column if one has been set;
otherwise returns 0.
\sa setHorizontalHeaderItem(), verticalHeaderItem()
*/
QStandardItem *QStandardItemModel::horizontalHeaderItem(int column) const
{
Q_D(const QStandardItemModel);
if ((column < 0) || (column >= columnCount()))
return 0;
return d->columnHeaderItems.at(column);
}
/*!
\since 4.2
Sets the vertical header item for \a row to \a item. The model takes
ownership of the item. If necessary, the row count is increased to fit the
item. The previous header item (if there was one) is deleted.
\sa verticalHeaderItem(), setVerticalHeaderLabels(),
setHorizontalHeaderItem()
*/
void QStandardItemModel::setVerticalHeaderItem(int row, QStandardItem *item)
{
Q_D(QStandardItemModel);
if (row < 0)
return;
if (rowCount() <= row)
setRowCount(row + 1);
QStandardItem *oldItem = d->rowHeaderItems.at(row);
if (item == oldItem)
return;
if (item) {
if (item->model() == 0) {
item->d_func()->setModel(this);
} else {
qWarning("QStandardItem::setVerticalHeaderItem: Ignoring duplicate insertion of item %p",
item);
return;
}
}
if (oldItem)
oldItem->d_func()->setModel(0);
delete oldItem;
d->rowHeaderItems.replace(row, item);
emit headerDataChanged(Qt::Vertical, row, row);
}
/*!
\since 4.2
Returns the vertical header item for row \a row if one has been set;
otherwise returns 0.
\sa setVerticalHeaderItem(), horizontalHeaderItem()
*/
QStandardItem *QStandardItemModel::verticalHeaderItem(int row) const
{
Q_D(const QStandardItemModel);
if ((row < 0) || (row >= rowCount()))
return 0;
return d->rowHeaderItems.at(row);
}
/*!
\since 4.2
Sets the horizontal header labels using \a labels. If necessary, the
column count is increased to the size of \a labels.
\sa setHorizontalHeaderItem()
*/
void QStandardItemModel::setHorizontalHeaderLabels(const QStringList &labels)
{
Q_D(QStandardItemModel);
if (columnCount() < labels.count())
setColumnCount(labels.count());
for (int i = 0; i < labels.count(); ++i) {
QStandardItem *item = horizontalHeaderItem(i);
if (!item) {
item = d->createItem();
setHorizontalHeaderItem(i, item);
}
item->setText(labels.at(i));
}
}
/*!
\since 4.2
Sets the vertical header labels using \a labels. If necessary, the row
count is increased to the size of \a labels.
\sa setVerticalHeaderItem()
*/
void QStandardItemModel::setVerticalHeaderLabels(const QStringList &labels)
{
Q_D(QStandardItemModel);
if (rowCount() < labels.count())
setRowCount(labels.count());
for (int i = 0; i < labels.count(); ++i) {
QStandardItem *item = verticalHeaderItem(i);
if (!item) {
item = d->createItem();
setVerticalHeaderItem(i, item);
}
item->setText(labels.at(i));
}
}
/*!
\since 4.2
Sets the item prototype for the model to the specified \a item. The model
takes ownership of the prototype.
The item prototype acts as a QStandardItem factory, by relying on the
QStandardItem::clone() function. To provide your own prototype, subclass
QStandardItem, reimplement QStandardItem::clone() and set the prototype to
be an instance of your custom class. Whenever QStandardItemModel needs to
create an item on demand (for instance, when a view or item delegate calls
setData())), the new items will be instances of your custom class.
\sa itemPrototype(), QStandardItem::clone()
*/
void QStandardItemModel::setItemPrototype(const QStandardItem *item)
{
Q_D(QStandardItemModel);
if (d->itemPrototype != item) {
delete d->itemPrototype;
d->itemPrototype = item;
}
}
/*!
\since 4.2
Returns the item prototype used by the model. The model uses the item
prototype as an item factory when it needs to construct new items on
demand (for instance, when a view or item delegate calls setData()).
\sa setItemPrototype()
*/
const QStandardItem *QStandardItemModel::itemPrototype() const
{
Q_D(const QStandardItemModel);
return d->itemPrototype;
}
/*!
\since 4.2
Returns a list of items that match the given \a text, using the given \a
flags, in the given \a column.
*/
QList<QStandardItem*> QStandardItemModel::findItems(const QString &text,
Qt::MatchFlags flags, int column) const
{
QModelIndexList indexes = match(index(0, column, QModelIndex()),
Qt::DisplayRole, text, -1, flags);
QList<QStandardItem*> items;
for (int i = 0; i < indexes.size(); ++i)
items.append(itemFromIndex(indexes.at(i)));
return items;
}
/*!
\since 4.2
Appends a row containing \a items. If necessary, the column count is
increased to the size of \a items.
\sa insertRow(), appendColumn()
*/
void QStandardItemModel::appendRow(const QList<QStandardItem*> &items)
{
invisibleRootItem()->appendRow(items);
}
/*!
\since 4.2
Appends a column containing \a items. If necessary, the row count is
increased to the size of \a items.
\sa insertColumn(), appendRow()
*/
void QStandardItemModel::appendColumn(const QList<QStandardItem*> &items)
{
invisibleRootItem()->appendColumn(items);
}
/*!
\since 4.2
\fn QStandardItemModel::appendRow(QStandardItem *item)
\overload
When building a list or a tree that has only one column, this function
provides a convenient way to append a single new \a item.
*/
/*!
\since 4.2
Inserts a row at \a row containing \a items. If necessary, the column
count is increased to the size of \a items.
\sa takeRow(), appendRow(), insertColumn()
*/
void QStandardItemModel::insertRow(int row, const QList<QStandardItem*> &items)
{
invisibleRootItem()->insertRow(row, items);
}
/*!
\since 4.2
\fn void QStandardItemModel::insertRow(int row, QStandardItem *item)
\overload
Inserts a row at \a row containing \a item.
When building a list or a tree that has only one column, this function
provides a convenient way to append a single new item.
*/
/*!
\since 4.2
Inserts a column at \a column containing \a items. If necessary, the row
count is increased to the size of \a items.
\sa takeColumn(), appendColumn(), insertRow()
*/
void QStandardItemModel::insertColumn(int column, const QList<QStandardItem*> &items)
{
invisibleRootItem()->insertColumn(column, items);
}
/*!
\since 4.2
Removes the item at (\a row, \a column) without deleting it. The model
releases ownership of the item.
\sa item(), takeRow(), takeColumn()
*/
QStandardItem *QStandardItemModel::takeItem(int row, int column)
{
Q_D(QStandardItemModel);
return d->root->takeChild(row, column);
}
/*!
\since 4.2
Removes the given \a row without deleting the row items, and returns a
list of pointers to the removed items. The model releases ownership of the
items. For items in the row that have not been set, the corresponding
pointers in the list will be 0.
\sa takeColumn()
*/
QList<QStandardItem*> QStandardItemModel::takeRow(int row)
{
Q_D(QStandardItemModel);
return d->root->takeRow(row);
}
/*!
\since 4.2
Removes the given \a column without deleting the column items, and returns
a list of pointers to the removed items. The model releases ownership of
the items. For items in the column that have not been set, the
corresponding pointers in the list will be 0.
\sa takeRow()
*/
QList<QStandardItem*> QStandardItemModel::takeColumn(int column)
{
Q_D(QStandardItemModel);
return d->root->takeColumn(column);
}
/*!
\since 4.2
Removes the horizontal header item at \a column from the header without
deleting it, and returns a pointer to the item. The model releases
ownership of the item.
\sa horizontalHeaderItem(), takeVerticalHeaderItem()
*/
QStandardItem *QStandardItemModel::takeHorizontalHeaderItem(int column)
{
Q_D(QStandardItemModel);
if ((column < 0) || (column >= columnCount()))
return 0;
QStandardItem *headerItem = d->columnHeaderItems.at(column);
if (headerItem) {
headerItem->d_func()->setParentAndModel(0, 0);
d->columnHeaderItems.replace(column, 0);
}
return headerItem;
}
/*!
\since 4.2
Removes the vertical header item at \a row from the header without
deleting it, and returns a pointer to the item. The model releases
ownership of the item.
\sa verticalHeaderItem(), takeHorizontalHeaderItem()
*/
QStandardItem *QStandardItemModel::takeVerticalHeaderItem(int row)
{
Q_D(QStandardItemModel);
if ((row < 0) || (row >= rowCount()))
return 0;
QStandardItem *headerItem = d->rowHeaderItems.at(row);
if (headerItem) {
headerItem->d_func()->setParentAndModel(0, 0);
d->rowHeaderItems.replace(row, 0);
}
return headerItem;
}
/*!
\since 4.2
\property QStandardItemModel::sortRole
\brief the item role that is used to query the model's data when sorting items
The default value is Qt::DisplayRole.
\sa sort(), QStandardItem::sortChildren()
*/
int QStandardItemModel::sortRole() const
{
Q_D(const QStandardItemModel);
return d->sortRole;
}
void QStandardItemModel::setSortRole(int role)
{
Q_D(QStandardItemModel);
d->sortRole = role;
}
/*!
\reimp
*/
int QStandardItemModel::columnCount(const QModelIndex &parent) const
{
Q_D(const QStandardItemModel);
QStandardItem *item = d->itemFromIndex(parent);
return item ? item->columnCount() : 0;
}
/*!
\reimp
*/
QVariant QStandardItemModel::data(const QModelIndex &index, int role) const
{
Q_D(const QStandardItemModel);
QStandardItem *item = d->itemFromIndex(index);
return item ? item->data(role) : QVariant();
}
/*!
\reimp
*/
Qt::ItemFlags QStandardItemModel::flags(const QModelIndex &index) const
{
Q_D(const QStandardItemModel);
if (!d->indexValid(index))
return d->root->flags();
QStandardItem *item = d->itemFromIndex(index);
if (item)
return item->flags();
return Qt::ItemIsSelectable
|Qt::ItemIsEnabled
|Qt::ItemIsEditable
|Qt::ItemIsDragEnabled
|Qt::ItemIsDropEnabled;
}
/*!
\reimp
*/
bool QStandardItemModel::hasChildren(const QModelIndex &parent) const
{
Q_D(const QStandardItemModel);
QStandardItem *item = d->itemFromIndex(parent);
return item ? item->hasChildren() : false;
}
/*!
\reimp
*/
QVariant QStandardItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_D(const QStandardItemModel);
if ((section < 0)
|| ((orientation == Qt::Horizontal) && (section >= columnCount()))
|| ((orientation == Qt::Vertical) && (section >= rowCount()))) {
return QVariant();
}
QStandardItem *headerItem = 0;
if (orientation == Qt::Horizontal)
headerItem = d->columnHeaderItems.at(section);
else if (orientation == Qt::Vertical)
headerItem = d->rowHeaderItems.at(section);
return headerItem ? headerItem->data(role)
: QAbstractItemModel::headerData(section, orientation, role);
}
/*!
\reimp
QStandardItemModel supports both copy and move.
*/
Qt::DropActions QStandardItemModel::supportedDropActions () const
{
return Qt::CopyAction | Qt::MoveAction;
}
/*!
\reimp
*/
QModelIndex QStandardItemModel::index(int row, int column, const QModelIndex &parent) const
{
Q_D(const QStandardItemModel);
QStandardItem *parentItem = d->itemFromIndex(parent);
if ((parentItem == 0)
|| (row < 0)
|| (column < 0)
|| (row >= parentItem->rowCount())
|| (column >= parentItem->columnCount())) {
return QModelIndex();
}
return createIndex(row, column, parentItem);
}
/*!
\reimp
*/
bool QStandardItemModel::insertColumns(int column, int count, const QModelIndex &parent)
{
Q_D(QStandardItemModel);
QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root.data();
if (item == 0)
return false;
return item->d_func()->insertColumns(column, count, QList<QStandardItem*>());
}
/*!
\reimp
*/
bool QStandardItemModel::insertRows(int row, int count, const QModelIndex &parent)
{
Q_D(QStandardItemModel);
QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root.data();
if (item == 0)
return false;
return item->d_func()->insertRows(row, count, QList<QStandardItem*>());
}
/*!
\reimp
*/
QMap<int, QVariant> QStandardItemModel::itemData(const QModelIndex &index) const
{
Q_D(const QStandardItemModel);
QStandardItem *item = d->itemFromIndex(index);
return item ? item->d_func()->itemData() : QMap<int, QVariant>();
}
/*!
\reimp
*/
QModelIndex QStandardItemModel::parent(const QModelIndex &child) const
{
Q_D(const QStandardItemModel);
if (!d->indexValid(child))
return QModelIndex();
QStandardItem *parentItem = static_cast<QStandardItem*>(child.internalPointer());
return indexFromItem(parentItem);
}
/*!
\reimp
*/
bool QStandardItemModel::removeColumns(int column, int count, const QModelIndex &parent)
{
Q_D(QStandardItemModel);
QStandardItem *item = d->itemFromIndex(parent);
if ((item == 0) || (count < 1) || (column < 0) || ((column + count) > item->columnCount()))
return false;
item->removeColumns(column, count);
return true;
}
/*!
\reimp
*/
bool QStandardItemModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_D(QStandardItemModel);
QStandardItem *item = d->itemFromIndex(parent);
if ((item == 0) || (count < 1) || (row < 0) || ((row + count) > item->rowCount()))
return false;
item->removeRows(row, count);
return true;
}
/*!
\reimp
*/
int QStandardItemModel::rowCount(const QModelIndex &parent) const
{
Q_D(const QStandardItemModel);
QStandardItem *item = d->itemFromIndex(parent);
return item ? item->rowCount() : 0;
}
/*!
\reimp
*/
bool QStandardItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid())
return false;
QStandardItem *item = itemFromIndex(index);
if (item == 0)
return false;
item->setData(value, role);
return true;
}
/*!
\reimp
*/
bool QStandardItemModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
Q_D(QStandardItemModel);
if ((section < 0)
|| ((orientation == Qt::Horizontal) && (section >= columnCount()))
|| ((orientation == Qt::Vertical) && (section >= rowCount()))) {
return false;
}
QStandardItem *headerItem = 0;
if (orientation == Qt::Horizontal) {
headerItem = d->columnHeaderItems.at(section);
if (headerItem == 0) {
headerItem = d->createItem();
headerItem->d_func()->setModel(this);
d->columnHeaderItems.replace(section, headerItem);
}
} else if (orientation == Qt::Vertical) {
headerItem = d->rowHeaderItems.at(section);
if (headerItem == 0) {
headerItem = d->createItem();
headerItem->d_func()->setModel(this);
d->rowHeaderItems.replace(section, headerItem);
}
}
if (headerItem) {
headerItem->setData(value, role);
return true;
}
return false;
}
/*!
\reimp
*/
bool QStandardItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
{
QStandardItem *item = itemFromIndex(index);
if (item == 0)
return false;
item->d_func()->setItemData(roles);
return true;
}
/*!
\reimp
*/
void QStandardItemModel::sort(int column, Qt::SortOrder order)
{
Q_D(QStandardItemModel);
d->root->sortChildren(column, order);
}
/*!
\fn QObject *QStandardItemModel::parent() const
\internal
*/
/*!
\reimp
*/
QStringList QStandardItemModel::mimeTypes() const
{
return QAbstractItemModel::mimeTypes() << QLatin1String("application/x-qstandarditemmodeldatalist");
}
/*!
\reimp
*/
QMimeData *QStandardItemModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *data = QAbstractItemModel::mimeData(indexes);
if(!data)
return 0;
QString format = QLatin1String("application/x-qstandarditemmodeldatalist");
if (!mimeTypes().contains(format))
return data;
QByteArray encoded;
QDataStream stream(&encoded, QIODevice::WriteOnly);
QSet<QStandardItem*> itemsSet;
QStack<QStandardItem*> stack;
itemsSet.reserve(indexes.count());
stack.reserve(indexes.count());
for (int i = 0; i < indexes.count(); ++i) {
QStandardItem *item = itemFromIndex(indexes.at(i));
itemsSet << item;
stack.push(item);
}
//remove duplicates childrens
{
QSet<QStandardItem *> seen;
while (!stack.isEmpty()) {
QStandardItem *itm = stack.pop();
if (seen.contains(itm))
continue;
seen.insert(itm);
const QVector<QStandardItem*> &childList = itm->d_func()->children;
for (int i = 0; i < childList.count(); ++i) {
QStandardItem *chi = childList.at(i);
if (chi) {
QSet<QStandardItem *>::iterator it = itemsSet.find(chi);
if (it != itemsSet.end()) {
itemsSet.erase(it);
}
stack.push(chi);
}
}
}
}
stack.reserve(itemsSet.count());
foreach (QStandardItem *item, itemsSet) {
stack.push(item);
}
//stream everything recursively
while (!stack.isEmpty()) {
QStandardItem *item = stack.pop();
if(itemsSet.contains(item)) { //if the item is selection 'top-level', strem its position
stream << item->row() << item->column();
}
if(item) {
stream << *item << item->columnCount() << item->d_ptr->children.count();
stack += item->d_ptr->children;
} else {
QStandardItem dummy;
stream << dummy << 0 << 0;
}
}
data->setData(format, encoded);
return data;
}
/* \internal
Used by QStandardItemModel::dropMimeData
stream out an item and his children
*/
void QStandardItemModelPrivate::decodeDataRecursive(QDataStream &stream, QStandardItem *item)
{
int colCount, childCount;
stream >> *item;
stream >> colCount >> childCount;
item->setColumnCount(colCount);
int childPos = childCount;
while(childPos > 0) {
childPos--;
QStandardItem *child = createItem();
decodeDataRecursive(stream, child);
item->setChild( childPos / colCount, childPos % colCount, child);
}
}
/*!
\reimp
*/
bool QStandardItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &parent)
{
Q_D(QStandardItemModel);
// check if the action is supported
if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
return false;
// check if the format is supported
QString format = QLatin1String("application/x-qstandarditemmodeldatalist");
if (!data->hasFormat(format))
return QAbstractItemModel::dropMimeData(data, action, row, column, parent);
if (row > rowCount(parent))
row = rowCount(parent);
if (row == -1)
row = rowCount(parent);
if (column == -1)
column = 0;
// decode and insert
QByteArray encoded = data->data(format);
QDataStream stream(&encoded, QIODevice::ReadOnly);
//code based on QAbstractItemModel::decodeData
// adapted to work with QStandardItem
int top = INT_MAX;
int left = INT_MAX;
int bottom = 0;
int right = 0;
QVector<int> rows, columns;
QVector<QStandardItem *> items;
while (!stream.atEnd()) {
int r, c;
QStandardItem *item = d->createItem();
stream >> r >> c;
d->decodeDataRecursive(stream, item);
rows.append(r);
columns.append(c);
items.append(item);
top = qMin(r, top);
left = qMin(c, left);
bottom = qMax(r, bottom);
right = qMax(c, right);
}
// insert the dragged items into the table, use a bit array to avoid overwriting items,
// since items from different tables can have the same row and column
int dragRowCount = 0;
int dragColumnCount = right - left + 1;
// Compute the number of continuous rows upon insertion and modify the rows to match
QVector<int> rowsToInsert(bottom + 1);
for (int i = 0; i < rows.count(); ++i)
rowsToInsert[rows.at(i)] = 1;
for (int i = 0; i < rowsToInsert.count(); ++i) {
if (rowsToInsert[i] == 1){
rowsToInsert[i] = dragRowCount;
++dragRowCount;
}
}
for (int i = 0; i < rows.count(); ++i)
rows[i] = top + rowsToInsert[rows[i]];
QBitArray isWrittenTo(dragRowCount * dragColumnCount);
// make space in the table for the dropped data
int colCount = columnCount(parent);
if (colCount < dragColumnCount + column) {
insertColumns(colCount, dragColumnCount + column - colCount, parent);
colCount = columnCount(parent);
}
insertRows(row, dragRowCount, parent);
row = qMax(0, row);
column = qMax(0, column);
QStandardItem *parentItem = itemFromIndex (parent);
if (!parentItem)
parentItem = invisibleRootItem();
QVector<QPersistentModelIndex> newIndexes(items.size());
// set the data in the table
for (int j = 0; j < items.size(); ++j) {
int relativeRow = rows.at(j) - top;
int relativeColumn = columns.at(j) - left;
int destinationRow = relativeRow + row;
int destinationColumn = relativeColumn + column;
int flat = (relativeRow * dragColumnCount) + relativeColumn;
// if the item was already written to, or we just can't fit it in the table, create a new row
if (destinationColumn >= colCount || isWrittenTo.testBit(flat)) {
destinationColumn = qBound(column, destinationColumn, colCount - 1);
destinationRow = row + dragRowCount;
insertRows(row + dragRowCount, 1, parent);
flat = (dragRowCount * dragColumnCount) + relativeColumn;
isWrittenTo.resize(++dragRowCount * dragColumnCount);
}
if (!isWrittenTo.testBit(flat)) {
newIndexes[j] = index(destinationRow, destinationColumn, parentItem->index());
isWrittenTo.setBit(flat);
}
}
for(int k = 0; k < newIndexes.size(); k++) {
if (newIndexes.at(k).isValid()) {
parentItem->setChild(newIndexes.at(k).row(), newIndexes.at(k).column(), items.at(k));
} else {
delete items.at(k);
}
}
return true;
}
QT_END_NAMESPACE
#include "moc_qstandarditemmodel.cpp"
#endif // QT_NO_STANDARDITEMMODEL
|