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
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
|
// Copyright (C) 2026 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
// Qt-Security score:significant reason:default
#include "qstylekitstyle.h"
#include "qstylekitstyle_p.h"
#include <QtWidgets/qstyleoption.h>
#include <QtWidgets/qstyle.h>
#include <QtWidgets/qapplication.h>
#if QT_CONFIG(scrollarea)
# include <QtWidgets/qabstractscrollarea.h>
#endif
#if QT_CONFIG(itemviews)
# include <QtWidgets/qabstractitemview.h>
#endif
#if QT_CONFIG(lineedit)
# include <QtWidgets/qlineedit.h>
#endif
#if QT_CONFIG(pushbutton)
# include <QtWidgets/qpushbutton.h>
#endif
#if QT_CONFIG(checkbox)
# include <QtWidgets/qcheckbox.h>
#endif
#if QT_CONFIG(radiobutton)
# include <QtWidgets/qradiobutton.h>
#endif
#if QT_CONFIG(combobox)
# include <QtWidgets/qcombobox.h>
#endif
#if QT_CONFIG(slider)
# include <QtWidgets/qslider.h>
#endif
#if QT_CONFIG(scrollbar)
# include <QtWidgets/qscrollbar.h>
#endif
#if QT_CONFIG(spinbox)
# include <QtWidgets/qspinbox.h>
#endif
#if QT_CONFIG(progressbar)
# include <QtWidgets/qprogressbar.h>
#endif
#if QT_CONFIG(textedit)
# include <QtWidgets/qtextedit.h>
# include <QtWidgets/qplaintextedit.h>
#endif
#if QT_CONFIG(tabbar)
# include <QtWidgets/qtabbar.h>
#endif
#if QT_CONFIG(tabwidget)
# include <QtWidgets/qtabwidget.h>
#endif
#if QT_CONFIG(toolbar)
# include <QtWidgets/qtoolbar.h>
#endif
#if QT_CONFIG(toolbutton)
# include <QtWidgets/qtoolbutton.h>
#endif
#if QT_CONFIG(groupbox)
# include <QtWidgets/qgroupbox.h>
#endif
#if QT_CONFIG(menu)
# include <QtWidgets/qmenu.h>
# include <QtWidgets/qmenubar.h>
#endif
#if QT_CONFIG(label)
# include <QtWidgets/qlabel.h>
#endif
#include <QtWidgets/private/qwidget_p.h>
#include <QtCore/private/qobject_p.h>
#include <QtGui/qpainter.h>
#include <QtGui/qpainterpath.h>
#include <QtGui/qpainterstateguard.h>
#include <QtGui/qstylehints.h>
#include <QtCore/qloggingcategory.h>
#include <QtCore/qnumeric.h>
#include <QtQml/private/qqmlcomponent_p.h>
#include <QtQml/qqmlengine.h>
#include <QtQuick/private/qquicktransition_p.h>
#include <QtLabsStyleKit/private/qqstylekit_p.h>
#include <QtLabsStyleKit/private/qqstylekitcontrolproperties_p.h>
#include <QtLabsStyleKit/private/qqstylekitstyle_p.h>
QT_BEGIN_NAMESPACE
Q_STATIC_LOGGING_CATEGORY(lcStyleKit, "qt.labs.stylekit")
/*!
\class QStyleKitStyle
\inmodule QtLabsStyleKit
\ingroup appearance
\since 6.12
\brief The QStyleKitStyle class applies a \l {Qt Labs StyleKit} style
to Qt Widgets.
QStyleKitStyle is a QStyle implementation that uses a \l StyleKit \l Style
to style Qt Widgets. The \l Style is a QML file that declaratively describes
the visual design (colors, sizes, radii, borders, and other properties)
for each widget type and state. Those property values drive the painting,
which is done entirely with QPainter. Qt Quick and the scene graph play
no part in the rendering.
This separation means the same \l Style QML file can drive both
Qt Quick Controls and Qt Widgets, sharing one design definition across
both systems.
\note StyleKit is a Qt Labs module, and its API may change between
Qt releases.
\section1 Loading a Style
A style is a QML file whose root object is a \l Style. To load it,
pass the file path to the constructor or to \l setStylePath():
\if !defined(qtforpython)
\code
auto *style = new QStyleKitStyle(QStringLiteral(":/styles/MyStyle.qml"));
QApplication::setStyle(style);
\endcode
\else
\code
style = QStyleKitStyle(":/styles/MyStyle.qml")
QApplication.setStyle(style)
\endcode
\endif
QStyleKitStyle loads the Style with an internal QQmlEngine that the
QStyleKitStyle instance owns. If the path is invalid or the root object
is not a \l Style, QStyleKitStyle emits a warning and keeps the
previously loaded style. Until a style loads successfully, the widgets
use an empty fallback style.
\section1 Themes
A Style may define one or more named \l {Theme}{themes}. The active
theme is selected with \l setThemeName(); the list of available
themes is exposed through \l availableThemeNames. The special theme name
\c System makes the style follow the platform color scheme: when the
OS color scheme changes, the active theme is recreated automatically
and all widgets are repolished.
\section1 Widget to StyleKit Control Mapping
Each Qt Widgets class is mapped to a StyleKit control type, which
determines which control entry in the \l Style applies to it. Use the
corresponding control entry to configure visual properties for that widget
type, including individual parts of the widget such as its background,
indicator, handle, etc. See \l ControlStyleProperties for the full list of
stylable properties.
Properties not set in a specific control entry fall back through the control
type hierarchy: for example, \c button falls back to \c abstractButton, which
falls back to \c control.
\table
\header
\li Qt Widgets class
\li StyleKit control
\row
\li QPushButton (flat)
\li \l {StylableControls::flatButton}{flatButton}
\row
\li QPushButton
\li \l {StylableControls::button}{button}
\row
\li QCheckBox
\li \l {StylableControls::checkBox}{checkBox}
\row
\li QRadioButton
\li \l {StylableControls::radioButton}{radioButton}
\row
\li QComboBox
\li \l {StylableControls::comboBox}{comboBox}
\row
\li QSlider
\li \l {StylableControls::slider}{slider}
\row
\li QScrollBar
\li \l {StylableControls::scrollBar}{scrollBar}
\row
\li QSpinBox, QDoubleSpinBox
\li \l {StylableControls::spinBox}{spinBox}
\row
\li QProgressBar
\li \l {StylableControls::progressBar}{progressBar}
\row
\li QLineEdit
\li \l {StylableControls::textField}{textField}
\row
\li QTextEdit, QPlainTextEdit
\li \l {StylableControls::textArea}{textArea}
\row
\li QTabBar
\li \l {StylableControls::tabBar}{tabBar}
\row
\li QTabWidget
\li \l {StylableControls::page}{page}
\row
\li QToolBar
\li \l {StylableControls::toolBar}{toolBar}
\row
\li QToolButton
\li \l {StylableControls::toolButton}{toolButton}
\row
\li QGroupBox
\li \l {StylableControls::groupBox}{groupBox}
\row
\li QFrame
\li \l {StylableControls::frame}{frame}
\row
\li QLabel
\li \l {StylableControls::label}{label}
\row
\li QMenu
\li \l {StylableControls::menu}{menu}
\row
\li QMenuBar
\li \l {StylableControls::menuBar}{menuBar}
\row
\li Everything else
\li \l {StylableControls::control}{control}
\endtable
Widgets not listed above are not yet supported by QStyleKitStyle and will be
painted by \l QCommonStyle. Support for remaining widgets is planned for future
releases. Conversely, some control entries in \l StylableControls have no
Qt Widgets equivalent and are not applied when styling widgets.
\section2 Sub-controls within a widget
Separate sub-controls within a widget can be styled individually, as each one maps
to a separate control entry in the \l Style:
\table
\header
\li Sub-element
\li StyleKit control
\row
\li \l QStyledItemDelegate items - the default delegate for all Qt item views,
including the \l QComboBox popup list
\li \l {StylableControls::itemDelegate}{itemDelegate}
\row
\li The same items, when user-checkable (i.e. showing a check indicator)
\li \l {StylableControls::checkDelegate}{checkDelegate};
falls back to \c itemDelegate for anything not set explicitly
\row
\li Individual tabs in a \l QTabBar
\li \l {StylableControls::tabButton}{tabButton}
\row
\li \l QMenu items
\li \l {StylableControls::menuItem}{menuItem}
\row
\li Separators in a \l QMenu
\li \l {StylableControls::menuSeparator}{menuSeparator}
\row
\li \l QMenuBar items
\li \l {StylableControls::menuBarItem}{menuBarItem}
\row
\li Separators in a \l QToolBar
\li \l {StylableControls::toolSeparator}{toolSeparator}
\row
\li The \l QComboBox popup list container
\li \l {StylableControls::popup}{popup}
\endtable
\section1 Known Limitations
QStyleKitStyle is in Tech Preview. The following StyleKit features are
currently not supported when used with Qt Widgets:
\list
\li \b{Shadows} — shadows are not rendered.
\li \b{Delegate scale above 1.0 on a control's background} — a widget cannot paint
outside its own rect, so the scaled background is clipped at the widget edge.
Use \l {DelegateStyle::}{margins} to inset the background and reserve room for
it to grow. Scaling indicators, handles and foregrounds is unaffected.
\li \b{Variations} — setting a \l StyleVariation on a widget instance is
not yet supported.
\li \b{Custom controls} — styling custom widgets using \l CustomControl
is not yet supported.
\li \b{Custom delegates} — the \l {DelegateStyle::}{delegate} property is
not used; the built-in rendering is always applied.
\endlist
Support for these features is planned for a future release.
\sa QStyle, QCommonStyle, {Qt Labs StyleKit}, Style, Theme
*/
/*!
\property QStyleKitStyle::stylePath
\brief the path to the QML \l Style file driving this style.
The value is a path to a local file or to a file in the resource file
system, (for example, \c{:/styles/MyStyle.qml} or \c{qrc:/qt/qml/MyQmlModule/Style.qml}).
QStyleKitStyle resolves a relative path against the application's working directory.
The file must contain a QML component whose root object is a \l Style.
Setting this property reloads the style. If the new file does not load,
QStyleKitStyle emits a warning and keeps the previously loaded style
and style path.
*/
/*!
\property QStyleKitStyle::themeName
\brief the name of the active theme.
The value must be one of the entries in \l availableThemeNames. That
list includes the special name \c System, which makes the style follow
the platform color scheme. The comparison ignores case.
Setting this property repolishes all widgets so that they repaint with
the new theme. If the name matches no theme, the \l Style emits a
warning and applies no theme.
*/
/*!
\property QStyleKitStyle::availableThemeNames
\brief the list of theme names exposed by the loaded \l Style.
This list always includes \c System, which follows the platform color
scheme. It also includes \c Light and \c Dark when the \l Style defines
those themes, together with any custom themes that the style author
defines. The list is empty when no style is loaded.
*/
/*!
\property QStyleKitStyle::customThemeNames
\brief the list of custom theme names defined by the loaded \l Style.
Unlike \l availableThemeNames, this list excludes \c System, \c Light,
and \c Dark, and contains only the themes that the style author
defines explicitly. The list is empty when no style is loaded.
\sa availableThemeNames, themeName
*/
static QQStyleKitReader::ControlType controlTypeForWidget(const QWidget *widget)
{
if (!widget)
return QQStyleKitReader::Control;
#if QT_CONFIG(pushbutton)
if (qobject_cast<const QPushButton *>(widget))
return QQStyleKitReader::Button;
#endif
#if QT_CONFIG(checkbox)
if (qobject_cast<const QCheckBox *>(widget))
return QQStyleKitReader::CheckBox;
#endif
#if QT_CONFIG(radiobutton)
if (qobject_cast<const QRadioButton *>(widget))
return QQStyleKitReader::RadioButton;
#endif
#if QT_CONFIG(combobox)
if (qobject_cast<const QComboBox *>(widget))
return QQStyleKitReader::ComboBox;
#endif
#if QT_CONFIG(slider)
if (qobject_cast<const QSlider *>(widget))
return QQStyleKitReader::Slider;
#endif
#if QT_CONFIG(scrollbar)
if (qobject_cast<const QScrollBar *>(widget))
return QQStyleKitReader::ScrollBar;
#endif
#if QT_CONFIG(spinbox)
if (qobject_cast<const QSpinBox *>(widget) || qobject_cast<const QDoubleSpinBox *>(widget))
return QQStyleKitReader::SpinBox;
#endif
#if QT_CONFIG(itemviews)
if (qobject_cast<const QAbstractItemView *>(widget))
return QQStyleKitReader::ItemDelegate;
#endif
#if QT_CONFIG(progressbar)
if (qobject_cast<const QProgressBar *>(widget))
return QQStyleKitReader::ProgressBar;
#endif
#if QT_CONFIG(lineedit)
if (qobject_cast<const QLineEdit *>(widget))
return QQStyleKitReader::TextField;
#endif
#if QT_CONFIG(textedit)
if (qobject_cast<const QTextEdit *>(widget) || qobject_cast<const QPlainTextEdit *>(widget))
return QQStyleKitReader::TextArea;
#endif
#if QT_CONFIG(tabbar)
if (qobject_cast<const QTabBar *>(widget))
return QQStyleKitReader::TabBar;
#endif
#if QT_CONFIG(tabwidget)
if (qobject_cast<const QTabWidget *>(widget))
return QQStyleKitReader::Page;
#endif
#if QT_CONFIG(toolbar)
if (qobject_cast<const QToolBar *>(widget))
return QQStyleKitReader::ToolBar;
#endif
#if QT_CONFIG(toolbutton)
if (qobject_cast<const QToolButton *>(widget))
return QQStyleKitReader::ToolButton;
#endif
#if QT_CONFIG(groupbox)
if (qobject_cast<const QGroupBox *>(widget))
return QQStyleKitReader::GroupBox;
#endif
#if QT_CONFIG(menu)
if (qobject_cast<const QMenuBar *>(widget))
return QQStyleKitReader::MenuBar;
if (qobject_cast<const QMenu *>(widget))
return QQStyleKitReader::Menu;
#endif
if (widget->windowType() == Qt::Popup)
return QQStyleKitReader::Popup;
if (widget->windowType() == Qt::Dialog)
return QQStyleKitReader::Dialog;
if (widget->windowType() == Qt::Window)
return QQStyleKitReader::ApplicationWindow;
#ifndef QT_NO_FRAME
if (qobject_cast<const QFrame *>(widget))
return QQStyleKitReader::Frame;
#endif
return QQStyleKitReader::Control;
}
#if QT_CONFIG(itemviews)
static QQStyleKitReader::ControlType itemViewControlType(const QStyleOption *opt)
{
const auto *itemViewOption = qstyleoption_cast<const QStyleOptionViewItem *>(opt);
return itemViewOption && (itemViewOption->features & QStyleOptionViewItem::HasCheckIndicator)
? QQStyleKitReader::ControlType::CheckDelegate
: QQStyleKitReader::ControlType::ItemDelegate;
}
#endif
// Some widgets (like QAbstractScrollArea) paint on a child widget (the viewport)
// instead of on themselves. This function returns the widget that is actually painted on,
// which is the one that should be updated when the style values change.
static QWidget *paintTarget(const QWidget *widget)
{
#if QT_CONFIG(scrollarea)
if (const auto *area = qobject_cast<const QAbstractScrollArea *>(widget))
return area->viewport();
#endif
return const_cast<QWidget *>(widget);
}
// Returns true for widgets that draw themselves using their widget font and
// palette directly, bypassing the style's drawControl path.
static bool isSelfPaintingWidget(const QWidget *widget)
{
return false
#if QT_CONFIG(label)
|| qobject_cast<const QLabel *>(widget)
#endif
#if QT_CONFIG(textedit)
|| qobject_cast<const QTextEdit *>(widget)
|| qobject_cast<const QPlainTextEdit *>(widget)
#endif
#if QT_CONFIG(lineedit)
|| qobject_cast<const QLineEdit *>(widget)
#endif
;
}
static QWidget *managedViewport(QWidget *widget)
{
#if QT_CONFIG(textedit)
if (auto *textEdit = qobject_cast<QTextEdit *>(widget))
return textEdit->viewport();
if (auto *plainTextEdit = qobject_cast<QPlainTextEdit *>(widget))
return plainTextEdit->viewport();
#endif
#if QT_CONFIG(itemviews) && QT_CONFIG(combobox)
if (auto *view = qobject_cast<QAbstractItemView *>(widget)) {
if (auto *p = view->parentWidget(); p && p->inherits("QComboBoxPrivateContainer"))
return view->viewport();
}
#endif
return nullptr;
}
static uint resolvedAlignment(uint raw, Qt::Alignment hDefault, Qt::Alignment vDefault)
{
uint result = 0;
const uint h = raw & Qt::AlignHorizontal_Mask;
const uint v = raw & Qt::AlignVertical_Mask;
result |= h ? h : uint(hDefault);
result |= v ? v : uint(vDefault);
return result;
}
static qreal resolvedWidth(const QQStyleKitDelegateProperties *element, qreal availableW)
{
return qMax(0.0, element->fillWidth() ? availableW : element->width());
}
static qreal resolvedHeight(const QQStyleKitDelegateProperties *element, qreal availableH)
{
return qMax(0.0, element->fillHeight() ? availableH : element->height());
}
static QMargins elementMargins(const QQStyleKitDelegateProperties *element)
{
using QtPrivate::qSaturateRound;
return QMargins(qSaturateRound(element->leftMargin()),
qSaturateRound(element->topMargin()),
qSaturateRound(element->rightMargin()),
qSaturateRound(element->bottomMargin()));
}
/*! \internal
Applies \a element's rotation and scale to \a painter about the centre of \a box.
Returns \c false if the scale is 0 or if \a element is null, otherwise returns \c true.
*/
static bool applyDelegateTransform(QPainter *painter,
const QQStyleKitDelegateProperties *element,
const QRectF &box)
{
if (!element)
return false;
const qreal scale = element->scale();
if (qFuzzyIsNull(scale))
return false;
const qreal rotation = element->rotation();
if (qFuzzyIsNull(rotation) && qFuzzyCompare(scale, 1.0))
return true;
const QPointF center = box.center();
painter->translate(center);
if (!qFuzzyIsNull(rotation))
painter->rotate(rotation);
if (!qFuzzyCompare(scale, 1.0))
painter->scale(scale, scale);
painter->translate(-center);
return true;
}
// Copied from qstylesheetstyle.cpp
static const QWidget *containerWidget(const QWidget *w)
{
#if QT_CONFIG(lineedit)
if (qobject_cast<const QLineEdit *>(w)) {
// if the QLineEdit is an embeddedWidget, we need the real widget
QWidget *parent = w->parentWidget();
if (false
# if QT_CONFIG(combobox)
|| qobject_cast<const QComboBox *>(parent)
# endif
# if QT_CONFIG(spinbox)
|| qobject_cast<const QAbstractSpinBox *>(parent)
# endif
) {
return parent;
}
}
#endif
#if QT_CONFIG(scrollarea)
if (const QAbstractScrollArea *sa =
qobject_cast<const QAbstractScrollArea *>(w->parentWidget())) {
if (sa->viewport() == w)
return w->parentWidget();
}
#endif
return w;
}
QStyleKitStylePrivate::QStyleKitStylePrivate()
: QCommonStylePrivate()
{
}
static QUrl urlFromStylePath(const QString &filePath)
{
if (filePath.startsWith(QLatin1Char(':')))
return QUrl(QLatin1String("qrc") + filePath);
if (filePath.startsWith(QLatin1String("qrc:")) || filePath.startsWith(QLatin1String("file:")))
return QUrl(filePath);
return QUrl::fromLocalFile(filePath);
}
bool QStyleKitStylePrivate::loadStyle()
{
Q_Q(QStyleKitStyle);
if (!qmlEngine)
qmlEngine = new QQmlEngine(q);
if (!qmlEngine) {
qWarning("QStyleKitStyle: No QML engine available to load style.");
return false;
}
const QUrl url = stylePath.isEmpty() ? QUrl() : urlFromStylePath(stylePath);
if (stylePath.isEmpty() || !url.isValid()) {
qWarning("QStyleKitStyle: No valid style path provided: %s", qPrintable(stylePath));
return false;
}
QQmlComponent component(qmlEngine, url);
if (component.isError()) {
qWarning("QStyleKitStyle: Failed to load style from %s: %s", qPrintable(stylePath),
qPrintable(component.errorString()));
return false;
}
// Avoid creating anything other than StyleKit Style objects
// by checking the metaobject of the root type before creating the object
QQmlComponentPrivate *componentPrivate = QQmlComponentPrivate::get(&component);
const auto compilationUnit = componentPrivate->compilationUnit();
const auto propertyCache = compilationUnit ? compilationUnit->rootPropertyCache() : nullptr;
const auto firstMetaObject = propertyCache ? propertyCache->firstCppMetaObject() : nullptr;
if (!firstMetaObject || !firstMetaObject->inherits(&QQStyleKitStyle::staticMetaObject)) {
qWarning("QStyleKitStyle: Failed to load style from %s: component is not a StyleKit Style.",
qPrintable(stylePath));
return false;
}
QQStyleKitStyle *styleObject = qobject_cast<QQStyleKitStyle *>(component.create());
if (!styleObject) {
qWarning("QStyleKitStyle: Failed to create style object from %s: "
"component is not a StyleKit style.",
qPrintable(stylePath));
return false;
}
const bool isReload = style != nullptr;
delete style;
style = styleObject;
style->setParent(q);
if (!isReload) {
QObject::connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, q,
[this]() {
if (style && style->themeName() == QLatin1String("System")) {
style->recreateTheme();
updateStyle();
}
});
}
if (style->loaded())
QQStyleKitReader::resetReadersForStyle(style);
return true;
}
/*! \internal
Creates and returns the empty fallback style used when no user
style is loaded.
*/
QQStyleKitStyle *QStyleKitStylePrivate::ensureDefaultStyle()
{
if (defaultStyle)
return defaultStyle;
Q_Q(QStyleKitStyle);
if (!qmlEngine)
qmlEngine = new QQmlEngine(q);
QQmlComponent component(qmlEngine);
component.loadFromModule("Qt.labs.StyleKit", "Style");
if (component.isError()) {
qWarning("QStyleKitStyle: Failed to create default fallback style: %s",
qPrintable(component.errorString()));
return nullptr;
}
defaultStyle = qobject_cast<QQStyleKitStyle *>(component.create());
if (defaultStyle) {
defaultStyle->setParent(q);
qCDebug(lcStyleKit,
"No style set; using a default fallback style. "
"Set stylePath to load a StyleKit Style.");
}
return defaultStyle;
}
/*! \internal
Returns the user style if one is loaded, otherwise the default fallback
style
*/
QQStyleKitStyle *QStyleKitStylePrivate::effectiveStyle() const
{
return style ? style : defaultStyle;
}
void QStyleKitStylePrivate::updateStyle()
{
clearMetricsCache();
QQStyleKitStyle *effective = effectiveStyle();
if (sharedReader && sharedReader->style() != effective)
sharedReader->setExplicitStyle(effective);
if (subElementReader && subElementReader->style() != effective)
subElementReader->setExplicitStyle(effective);
clearAllSubElements();
for (auto *wr : std::as_const(widgetReaders)) {
if (wr->style() != effective)
wr->setExplicitStyle(effective);
}
if (!QWidgetPrivate::allWidgets)
return;
for (auto *widget : std::as_const(*QWidgetPrivate::allWidgets)) {
if (customFontWidgets.contains(widget))
unsetStyleFont(widget);
refreshStyleFont(widget);
if (customPaletteWidgets.contains(widget))
unsetStylePalette(widget);
refreshStylePalette(widget);
}
QEvent styleChange(QEvent::StyleChange);
for (auto *widget : std::as_const(*QWidgetPrivate::allWidgets)) {
QApplication::sendEvent(widget, &styleChange);
widget->update();
}
}
// Follow the approach in qstylesheetstyle.cpp
void QStyleKitStylePrivate::unsetStyleFont(QWidget *widget)
{
auto it = customFontWidgets.find(widget);
if (it == customFontWidgets.end())
return;
auto customFont = std::move(*it);
customFontWidgets.erase(it);
widget->setFont(std::move(customFont).reverted(widget->font()));
}
void QStyleKitStylePrivate::setStyleFont(QWidget *widget, const QFont &styleFont)
{
if (!effectiveStyle() || !widget)
return;
const auto styleMask = styleFont.resolveMask();
auto it = customFontWidgets.find(widget);
if (it == customFontWidgets.end()) {
if (styleMask == 0)
return;
it = customFontWidgets.insert(widget, { QWidgetPrivate::get(widget)->localFont(), 0 });
QObject::connect(widget, &QObject::destroyed, widget,
[this, widget]() { customFontWidgets.remove(widget); });
}
it->resolveMask = styleMask;
const QFont &baseline = it->oldWidgetValue;
QFont merged = styleFont.resolve(baseline);
merged.setResolveMask(baseline.resolveMask() | styleMask);
if (widget->font() != merged)
widget->setFont(merged);
}
void QStyleKitStylePrivate::refreshStyleFont(QWidget *widget)
{
if (!effectiveStyle() || !widget)
return;
const QWidget *targetWidget = containerWidget(widget);
QQStyleKitReader::ControlType controlType = controlTypeForWidget(targetWidget);
auto *shared = ensureSharedReader();
if (!shared)
return;
QStyleOption opt;
opt.initFrom(targetWidget);
const QQSK::State currentState = resolvedStateFor(controlType, opt.state, targetWidget);
shared->setControlTypeAndState(controlType, currentState, false);
setStyleFont(widget, shared->font());
}
void QStyleKitStylePrivate::unsetStylePalette(QWidget *widget)
{
auto it = customPaletteWidgets.find(widget);
if (it == customPaletteWidgets.end())
return;
auto customPalette = std::move(*it);
customPaletteWidgets.erase(it);
widget->setPalette(std::move(customPalette).reverted(widget->palette()));
}
void QStyleKitStylePrivate::setStylePalette(QWidget *widget, const QPalette &stylePalette) const
{
if (!effectiveStyle() || !widget)
return;
const quint64 styleMask = stylePalette.resolveMask();
if (styleMask == 0)
return;
if (!customPaletteWidgets.contains(widget)) {
customPaletteWidgets.insert(widget, { widget->palette(), 0 });
QObject::connect(widget, &QObject::destroyed, widget,
[this, widget]() { customPaletteWidgets.remove(widget); });
}
customPaletteWidgets[widget].resolveMask |= styleMask;
QPalette merged = stylePalette.resolve(widget->palette());
merged.setResolveMask(widget->palette().resolveMask() | styleMask);
widget->setPalette(merged);
}
// For widgets that partially or fully draw themselves (instead of delegating to the style),
// using palette roles, we need to push the style colors to those roles
void QStyleKitStylePrivate::refreshStylePalette(QWidget *widget)
{
if (!effectiveStyle() || !widget)
return;
const bool isWindow = widget->windowType() & (Qt::Popup | Qt::Window | Qt::Dialog);
const bool isPaletteManaged = isSelfPaintingWidget(widget) || isWindow;
if (!isPaletteManaged)
return;
const QWidget *targetWidget = containerWidget(widget);
QQStyleKitReader::ControlType controlType = controlTypeForWidget(targetWidget);
auto *shared = ensureSharedReader();
if (!shared)
return;
QStyleOption opt;
opt.initFrom(targetWidget);
const QQSK::State currentState = resolvedStateFor(controlType, opt.state, targetWidget);
QPalette stylePalette;
if (isWindow) {
// Windows draw their own background using the QPalette::Window role
shared->setControlTypeAndState(controlType, currentState, false);
if (const auto *bg = shared->global()->background();
bg && bg->isDefined(QQSK::Property::Color)) {
stylePalette.setColor(QPalette::Window, bg->color());
}
shared->setControlTypeAndState(controlType, QQSK::StateFlag::Disabled, false);
if (const auto *dbg = shared->global()->background();
dbg && dbg->isDefined(QQSK::Property::Color)) {
stylePalette.setColor(QPalette::Disabled, QPalette::Window, dbg->color());
}
} else {
// The remaining text-based widgets use the QPalette::Text/WindowText roles for their
// foreground color
shared->setControlTypeAndState(controlType, currentState, false);
if (const auto *text = shared->global()->text();
text && text->isDefined(QQSK::Property::Color)) {
stylePalette.setColor(QPalette::Text, text->color());
stylePalette.setColor(QPalette::WindowText, text->color());
}
shared->setControlTypeAndState(controlType, QQSK::StateFlag::Disabled, false);
if (const auto *dt = shared->global()->text();
dt && dt->isDefined(QQSK::Property::Color)) {
stylePalette.setColor(QPalette::Disabled, QPalette::Text, dt->color());
stylePalette.setColor(QPalette::Disabled, QPalette::WindowText, dt->color());
}
}
setStylePalette(widget, stylePalette);
}
/*! \internal
Returns a reader for the given widget, creating and caching it if needed.
*/
QQStyleKitReader *QStyleKitStylePrivate::readerForWidget(const QWidget *widget) const
{
Q_Q(const QStyleKitStyle);
QQStyleKitStyle *effective = effectiveStyle();
if (!effective || !widget)
return nullptr;
if (auto it = widgetReaders.find(widget); it != widgetReaders.end())
return *it;
auto *widgetReader = new QQStyleKitReader(const_cast<QStyleKitStyle *>(q));
widgetReader->setExplicitStyle(effective);
widgetReader->setTarget(paintTarget(widget));
widgetReader->setControlType(controlTypeForWidget(widget));
widgetReader->setCompleted(true);
widgetReaders.insert(widget, widgetReader);
QObjectPrivate::connect(widget, &QObject::destroyed, this,
&QStyleKitStylePrivate::onWidgetDestroyed, Qt::UniqueConnection);
return widgetReader;
}
void QStyleKitStylePrivate::cleanupWidgetReader(const QWidget *widget) const
{
if (auto *reader = widgetReaders.take(widget))
reader->deleteLater();
cleanupSubElements(widget);
}
void QStyleKitStylePrivate::onWidgetDestroyed(QObject *w) const
{
cleanupWidgetReader(static_cast<QWidget *>(w));
}
/*! \internal
Returns the key identifying the sub-element (item-view cell, menu item or
menubar item) that \a opt refers to, or an invalid key if the option does
not identify one.
*/
QStyleKitStylePrivate::SubElementKey
QStyleKitStylePrivate::subElementKeyForOption(const QStyleOption *opt, const QWidget *widget)
{
SubElementKey key;
if (!widget)
return key;
#if QT_CONFIG(itemviews)
if (const auto *viewOpt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
const QModelIndex &idx = viewOpt->index;
if (!idx.isValid())
return key;
key.widget = widget;
key.id = SubElementKey::ItemViewCell{ idx.model(), idx.internalId(), idx.row(),
idx.column() };
return key;
}
#endif
#if QT_CONFIG(menu)
if (const auto *menuOpt = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
if (menuOpt->menuItemType == QStyleOptionMenuItem::Separator)
return key;
QAction *action = nullptr;
if (const auto *menu = qobject_cast<const QMenu *>(widget))
action = menu->actionAt(menuOpt->rect.center());
else if (const auto *menuBar = qobject_cast<const QMenuBar *>(widget))
action = menuBar->actionAt(menuOpt->rect.center());
if (action) {
key.widget = widget;
key.id = SubElementKey::Action{ action };
}
return key;
}
#endif
#if QT_CONFIG(tabbar)
if (const auto *tabOpt = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
if (tabOpt->tabIndex < 0)
return key;
key.widget = widget;
key.id = SubElementKey::Tab{ tabOpt->tabIndex };
return key;
}
#endif
return key;
}
/*! \internal
Creates and returns a transitions-enabled reader for the given sub-element
that animates from \a fromState to \a toState
*/
QQStyleKitReader *QStyleKitStylePrivate::startSubElementTransition(
const SubElementKey &key, QQStyleKitReader::ControlType type, QQSK::State fromState,
QQSK::State toState, QQuickTransition *transition) const
{
Q_Q(const QStyleKitStyle);
QQStyleKitStyle *effective = effectiveStyle();
if (!effective)
return nullptr;
// Reclaim finished readers. running() is per (shared) transition object, so
// it turns false only once no instance of that transition animates anywhere
for (auto it = subElementAnimationReaders.begin(); it != subElementAnimationReaders.end();) {
const QQuickTransition *t = it.value()->transition();
if (!t || !t->running()) {
it.value()->deleteLater();
it = subElementAnimationReaders.erase(it);
} else {
++it;
}
}
if (subElementAnimationReaders.size() >= kMaxSubElementAnimationReaders) {
const auto it = subElementAnimationReaders.begin();
if (const QWidget *widget = it.key().widget)
paintTarget(widget)->update(); // snap to end state
it.value()->deleteLater();
subElementAnimationReaders.erase(it);
}
auto *reader = new QQStyleKitReader(const_cast<QStyleKitStyle *>(q));
reader->setExplicitStyle(effective);
reader->setTarget(paintTarget(key.widget));
// just stores the fromState, doesn't start the animation yet
reader->setControlTypeAndState(type, fromState);
reader->setCompleted(true);
// As delegates are created lazily, a fresh reader has none,
// so we need to create them now so delegates can already start animating changes
// Only create delegates actually used by subelements, ie: background, indicators
reader->background();
auto *indicator = reader->indicator();
indicator->first()->foreground();
indicator->second()->foreground();
// Start the transition to the new state
reader->setControlTypeAndState(type, toState);
if (!transition || !transition->running()) {
reader->deleteLater();
return nullptr;
}
subElementAnimationReaders.insert(key, reader);
return reader;
}
/*! \internal
Drops all sub-element state tracking and animation readers for \a widget.
*/
void QStyleKitStylePrivate::cleanupSubElements(const QWidget *widget) const
{
for (auto it = subElementStates.begin(); it != subElementStates.end();) {
if (it.key().widget == widget)
it = subElementStates.erase(it);
else
++it;
}
for (auto it = subElementAnimationReaders.begin(); it != subElementAnimationReaders.end();) {
if (it.key().widget == widget) {
it.value()->deleteLater();
it = subElementAnimationReaders.erase(it);
} else {
++it;
}
}
}
/*! \internal
Drops all sub-element state tracking and animation readers.
*/
void QStyleKitStylePrivate::clearAllSubElements() const
{
subElementStates.clear();
for (QQStyleKitReader *reader : std::as_const(subElementAnimationReaders))
reader->deleteLater();
subElementAnimationReaders.clear();
}
/*! \internal
Resolves the given QStyle::State into a QQSK::State based on the mapping of
state flags for the given control type.
*/
QQSK::State QStyleKitStylePrivate::resolvedStateFor(QQStyleKitReader::ControlType type,
QStyle::State state,
const QWidget *widget) const
{
QQSK::State flags;
flags.setFlag(QQSK::StateFlag::Hovered, (state & QStyle::State_MouseOver));
flags.setFlag(QQSK::StateFlag::Pressed, state & QStyle::State_Sunken);
flags.setFlag(QQSK::StateFlag::Checked, state & QStyle::State_On);
flags.setFlag(QQSK::StateFlag::Focused, state & QStyle::State_HasFocus);
flags.setFlag(QQSK::StateFlag::Highlighted, state & QStyle::State_Selected);
bool hasOrientation = false;
switch (type) {
case QQStyleKitReader::ProgressBar:
case QQStyleKitReader::ScrollBar:
case QQStyleKitReader::ScrollIndicator:
case QQStyleKitReader::Slider:
case QQStyleKitReader::ToolBar:
case QQStyleKitReader::ToolSeparator:
hasOrientation = true;
break;
default:
break;
}
if (hasOrientation)
flags.setFlag(QQSK::StateFlag::Vertical, !(state & QStyle::State_Horizontal));
// Some widgets don't set State_Enabled in their
// style option even when the widget is enabled.
// Fall back to widget->isEnabled() in that case
const bool enabled = (state & QStyle::State_Enabled) || (widget && widget->isEnabled());
flags.setFlag(QQSK::StateFlag::Disabled, !enabled);
// ComboBox uses QStyle::State_On to indicate the popup is open, which is
// not a "checked" semantic.
if (type == QQStyleKitReader::ControlType::ComboBox)
flags.setFlag(QQSK::StateFlag::Checked, false);
// Popup has no hover state in the Controls style
if (type == QQStyleKitReader::ControlType::Popup)
flags.setFlag(QQSK::StateFlag::Hovered, false);
// QTabBar sets State_Selected (not State_On) on the current tab
if (type == QQStyleKitReader::ControlType::TabButton) {
flags.setFlag(QQSK::StateFlag::Checked, state & QStyle::State_Selected);
flags.setFlag(QQSK::StateFlag::Highlighted, false);
}
return flags;
}
/*! \internal
Returns the ControlMetrics for the given control type and state,
reading from the style if needed and caching the result.
*/
const QStyleKitStylePrivate::ControlMetrics &
QStyleKitStylePrivate::metricsFor(QQStyleKitReader::ControlType type, QQSK::State state) const
{
if (auto it = metricsCache.find({ type, state }); it != metricsCache.end())
return *it;
auto *reader = ensureSharedReader();
Q_ASSERT(reader);
reader->setControlTypeAndState(type, state, false);
return *metricsCache.insert({ type, state }, metricsForReader(reader));
}
/*! \internal
Resolves the properties for the given widget, control type and state,
and returns them as a QQStyleKitResolved.
*/
QStyleKitStylePrivate::QQStyleKitResolved
QStyleKitStylePrivate::resolve(const QWidget *w, QQStyleKitReader::ControlType type,
QStyle::State state) const
{
QQStyleKitResolved out;
out.widget = w;
auto *reader = readerForWidget(w);
if (!reader)
return out;
const QQSK::State resolvedState = resolvedStateFor(type, state, w);
reader->setControlTypeAndState(type, resolvedState);
out.reader = reader;
out.metrics = &metricsFor(type, resolvedState);
return out;
}
/*! \internal
Resolves the properties for a sub-element of \a w (an item-view item, menu/
menuBar item), and returns them as a QQStyleKitResolved.
Sub-elements in a steady-state use the shared, transitions-disabled
subElementReader. When a sub-element is observed to change state and
the style defines a transition for the new state, a transient per-item
reader is created to run that transition
Pass \a track = false for synthetic resolves that should not register state changes
or retarget a running animation.
*/
QStyleKitStylePrivate::QQStyleKitResolved
QStyleKitStylePrivate::resolveSubElement(const QWidget *w, const QStyleOption *opt,
QQStyleKitReader::ControlType type, QStyle::State state,
bool track) const
{
QQStyleKitResolved out;
out.widget = w;
auto *steadyReader = ensureSubElementReader();
if (!steadyReader)
return out;
const QQSK::State newState = resolvedStateFor(type, state, w);
const SubElementKey key = subElementKeyForOption(opt, w);
QQStyleKitReader *reader = nullptr;
if (track && key.isValid()) {
QQStyleKitReader *animReader = subElementAnimationReaders.value(key);
const auto it = subElementStates.constFind(key);
const bool seen = it != subElementStates.cend();
// Fallback to a reasonable base state to transition from by stripping the
// interactive/pointer-driven flags from the new state.
constexpr QQSK::State InteractiveStates = QQSK::State(QQSK::StateFlag::Hovered)
| QQSK::StateFlag::Pressed | QQSK::StateFlag::Checked
| QQSK::StateFlag::Focused | QQSK::StateFlag::Highlighted;
const auto baseState = newState & ~InteractiveStates;
const QQSK::State prevState = seen ? *it : baseState;
if (prevState != newState) {
if (animReader) {
// State changed before transition finished
// Retarget to new state, the transition continues from the current interpolated
// values
animReader->setControlTypeAndState(type, newState);
} else {
// Only pointer-driven states should animate
// If an item first comes into view checked/highlighted, etc.,
// it should not animate
const bool shouldAnimate = seen || newState.testFlag(QQSK::StateFlag::Hovered)
|| newState.testFlag(QQSK::StateFlag::Pressed);
if (shouldAnimate) {
steadyReader->setControlTypeAndState(type, newState);
if (QQuickTransition *transition = steadyReader->transition())
animReader = startSubElementTransition(key, type, prevState,
newState, transition);
}
}
}
if (!seen || prevState != newState) {
QObjectPrivate::connect(w, &QObject::destroyed, this,
&QStyleKitStylePrivate::onWidgetDestroyed,
Qt::UniqueConnection);
if (subElementStates.size() >= kMaxSubElementStates)
subElementStates.clear();
subElementStates.insert(key, newState);
}
reader = animReader;
}
if (!reader)
reader = steadyReader;
reader->setControlTypeAndState(type, newState);
out.reader = reader;
out.metrics = &metricsFor(type, newState);
return out;
}
const QQStyleKitTextProperties *QStyleKitStylePrivate::QQStyleKitResolved::text() const
{
return reader ? reader->text() : nullptr;
}
const QQStyleKitDelegateProperties *QStyleKitStylePrivate::QQStyleKitResolved::background() const
{
return reader ? reader->background() : nullptr;
}
const QQStyleKitHandleProperties *QStyleKitStylePrivate::QQStyleKitResolved::handle() const
{
return reader ? reader->handle() : nullptr;
}
const QQStyleKitIndicatorWithSubTypes *QStyleKitStylePrivate::QQStyleKitResolved::indicator() const
{
return reader ? reader->indicator() : nullptr;
}
QFont QStyleKitStylePrivate::QQStyleKitResolved::font() const
{
return reader ? reader->font() : QFont();
}
QQStyleKitReader *QStyleKitStylePrivate::ensureSharedReader() const
{
if (sharedReader)
return sharedReader;
Q_Q(const QStyleKitStyle);
QQStyleKitStyle *effective = effectiveStyle();
if (!effective)
return nullptr;
sharedReader = new QQStyleKitReader(const_cast<QStyleKitStyle *>(q));
// Disable transitions since this reader is used for one-off metric reads in layout queries
sharedReader->setTransitionsEnabled(false);
sharedReader->setExplicitStyle(effective);
sharedReader->setCompleted(true);
return sharedReader;
}
QQStyleKitReader *QStyleKitStylePrivate::ensureSubElementReader() const
{
if (subElementReader)
return subElementReader;
Q_Q(const QStyleKitStyle);
QQStyleKitStyle *effective = effectiveStyle();
if (!effective)
return nullptr;
subElementReader = new QQStyleKitReader(const_cast<QStyleKitStyle *>(q));
// Disable transitions: this reader is retargeted across many sub-elements
// per paint pass and must never animate between their states
subElementReader->setTransitionsEnabled(false);
subElementReader->setExplicitStyle(effective);
subElementReader->setCompleted(true);
return subElementReader;
}
/*! \internal
Resolves the properties for the given control type and state using the shared reader,
which disables transitions and is used for layout queries, so that layout never depends
on animation state.
*/
QStyleKitStylePrivate::QQStyleKitLayoutResolved
QStyleKitStylePrivate::resolveLayout(QQStyleKitReader::ControlType type, QStyle::State state) const
{
QQStyleKitLayoutResolved out;
auto *reader = ensureSharedReader();
if (!reader)
return out;
const QQSK::State resolvedState = resolvedStateFor(type, state);
out.metrics = &metricsFor(type, resolvedState);
// metricsFor() sets the sharedReader to (type, resolvedState) on cache miss.
// On hit it didn't, so make sure staticProps / staticFont reflect the state
// and type of the caller
reader->setControlTypeAndState(type, resolvedState, false);
out.staticProps = reader->global();
out.staticFont = reader->font();
return out;
}
const QQStyleKitTextProperties *QStyleKitStylePrivate::QQStyleKitLayoutResolved::text() const
{
return staticProps ? staticProps->text() : nullptr;
}
const QQStyleKitDelegateProperties *
QStyleKitStylePrivate::QQStyleKitLayoutResolved::background() const
{
return staticProps ? staticProps->background() : nullptr;
}
const QQStyleKitHandleProperties *QStyleKitStylePrivate::QQStyleKitLayoutResolved::handle() const
{
return staticProps ? staticProps->handle() : nullptr;
}
const QQStyleKitIndicatorWithSubTypes *
QStyleKitStylePrivate::QQStyleKitLayoutResolved::indicator() const
{
return staticProps ? staticProps->indicator() : nullptr;
}
void QStyleKitStylePrivate::clearMetricsCache()
{
metricsCache.clear();
}
void QStyleKitStylePrivate::drawControlIndicator(const QQStyleKitDelegateProperties *indicator,
const QRectF &rect, QPainter *painter) const
{
if (!indicator || !indicator->visible() || indicator->opacity() <= 0 || !rect.isValid())
return;
// The foreground is a "child" of the indicator container so it
// inherits the indicator's transform. Apply it here and let the foreground stack its
// own on top.
QPainterStateGuard stateGuard(painter);
if (!applyDelegateTransform(painter, indicator, rect))
return;
// indicator (background)
QRectF indicatorRect = rect;
drawStyledItemContents(indicator, indicatorRect, painter);
const QQStyleKitDelegateProperties *foreground = nullptr;
if (auto *indicatorWithSubTypes =
qobject_cast<const QQStyleKitIndicatorWithSubTypes *>(indicator)) {
foreground = indicatorWithSubTypes->foreground();
} else if (auto *indicatorProps =
qobject_cast<const QQStyleKitIndicatorProperties *>(indicator)) {
foreground = indicatorProps->foreground();
} else {
return;
}
if (!foreground || !foreground->visible() || foreground->opacity() <= 0)
return;
// foreground
QRectF foregroundRect;
const uint foregroundAlign = resolvedAlignment(foreground->alignment(), Qt::AlignHCenter,
Qt::AlignVCenter);
const auto foregroundW = resolvedWidth(
foreground, indicatorRect.width() - foreground->leftMargin() - foreground->rightMargin());
const auto foregroundH = resolvedHeight(
foreground, indicatorRect.height() - foreground->topMargin() - foreground->bottomMargin());
foregroundRect.setSize(QSizeF(foregroundW, foregroundH));
if (foregroundAlign & Qt::AlignLeft)
foregroundRect.moveLeft(indicatorRect.left() + foreground->leftMargin());
else if (foregroundAlign & Qt::AlignHCenter)
foregroundRect.moveLeft(indicatorRect.left() + (indicatorRect.width() - foregroundW) / 2.0);
else if (foregroundAlign & Qt::AlignRight)
foregroundRect.moveLeft(indicatorRect.right() - foreground->rightMargin() - foregroundW);
if (foregroundAlign & Qt::AlignTop)
foregroundRect.moveTop(indicatorRect.top() + foreground->topMargin());
else if (foregroundAlign & Qt::AlignVCenter)
foregroundRect.moveTop(indicatorRect.top() + (indicatorRect.height() - foregroundH) / 2.0);
else if (foregroundAlign & Qt::AlignBottom)
foregroundRect.moveTop(indicatorRect.bottom() - foreground->bottomMargin() - foregroundH);
drawStyledItemRect(foreground, foregroundRect, painter);
}
void QStyleKitStylePrivate::drawControlText(const QQStyleKitTextProperties *textProps,
const QFont &font, const QRect &rect,
const QString &text, uint textFlags, QPainter *p,
Qt::Alignment defaultAlignment) const
{
uint flags = textFlags;
flags |= textProps
? resolvedAlignment(textProps->alignment(), defaultAlignment & Qt::AlignHorizontal_Mask,
defaultAlignment & Qt::AlignVertical_Mask)
: uint(defaultAlignment);
const QFont oldFont = p->font();
p->setFont(font.resolve(oldFont));
p->setBrush(Qt::NoBrush);
p->setPen(textProps ? textProps->color() : QColor());
p->drawText(rect, flags, text, nullptr);
}
void QStyleKitStylePrivate::drawStyledItemRect(const QQStyleKitDelegateProperties *props,
const QRectF &rect, QPainter *painter) const
{
if (!props || !props->visible() || props->opacity() <= 0 || !rect.isValid())
return;
QPainterStateGuard stateGuard(painter);
if (!applyDelegateTransform(painter, props, rect))
return;
drawStyledItemContents(props, rect, painter);
}
void QStyleKitStylePrivate::drawStyledItemContents(const QQStyleKitDelegateProperties *props,
const QRectF &rect, QPainter *painter) const
{
if (!props || !props->visible() || props->opacity() <= 0 || !rect.isValid())
return;
QPainterStateGuard stateGuard(painter);
painter->setRenderHint(QPainter::Antialiasing, true);
if (props->clip())
painter->setClipping(true);
// opacity
const auto opacity = props->opacity();
// border
const auto *border = props->border();
auto borderWidth = 0;
auto inset = 0.0;
QColor borderColor;
if (border) {
borderWidth = border->width();
borderColor = border->color();
inset = borderWidth / 2.0;
}
if (borderWidth > 0 && borderColor.isValid() && borderColor.alpha() > 0) {
QPen pen(borderColor, borderWidth);
painter->setPen(pen);
} else {
painter->setPen(Qt::NoPen);
}
// radius
// TODO: support different radius for each corner
const qreal minDimension = qMin(rect.width(), rect.height());
const qreal xRadius = qMax(0.0, qMin(props->topLeftRadius() - inset, minDimension / 2.0));
const qreal yRadius = qMax(0.0, qMin(props->bottomLeftRadius() - inset, minDimension / 2.0));
QRectF adjustedRect = rect.adjusted(inset, inset, -inset, -inset);
// gradients/color
// TODO: support palette roles for colors and gradients
QColor color = props->color();
QBrush colorBrush(color);
painter->setBrush(colorBrush);
if (color.isValid()) {
painter->setOpacity(opacity);
painter->drawRoundedRect(adjustedRect, xRadius, yRadius, Qt::AbsoluteSize);
}
if (QQuickGradient *gradient = props->gradient()) {
QLinearGradient linearGradient(rect.topLeft(), rect.bottomLeft());
QQmlListProperty<QQuickGradientStop> stops = gradient->stops();
const int stopCount = stops.count(&stops);
for (int i = 0; i < stopCount; i++) {
QQuickGradientStop *stop = static_cast<QQuickGradientStop *>(stops.at(&stops, i));
linearGradient.setColorAt(stop->position(), stop->color());
}
QBrush gradientBrush(linearGradient);
painter->setBrush(gradientBrush);
painter->setOpacity(opacity);
painter->drawRoundedRect(adjustedRect, xRadius, yRadius, Qt::AbsoluteSize);
}
// image
drawStyledItemImage(props->image(), adjustedRect, opacity, painter);
}
void QStyleKitStylePrivate::drawStyledItemImage(const QQStyleKitImageProperties *image,
const QRectF &rect, qreal opacity,
QPainter *painter) const
{
if (!image || image->source().isEmpty() || image->color().alpha() <= 0)
return;
QString imageSource = image->source().toString();
if (imageSource.startsWith(QLatin1String("qrc:/")))
imageSource = imageSource.mid(3);
QUrl imageUrl(imageSource);
QString imagePath = imageUrl.isLocalFile() ? imageUrl.toLocalFile() : imageSource;
QPixmap pixmap(imagePath);
if (pixmap.isNull())
return;
if (image->color().isValid()) {
QImage coloredImage = pixmap.toImage();
QPainter imagePainter(&coloredImage);
imagePainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
imagePainter.fillRect(coloredImage.rect(), image->color());
imagePainter.end();
pixmap = QPixmap::fromImage(coloredImage);
}
painter->setOpacity(opacity);
const QSizeF pixmapSize = pixmap.deviceIndependentSize();
switch (image->fillMode()) {
case QQuickImage::PreserveAspectFit: {
const QSize scaled = pixmapSize.scaled(rect.size(), Qt::KeepAspectRatio).toSize();
const QPointF topLeft(rect.x() + (rect.width() - scaled.width()) / 2.0,
rect.y() + (rect.height() - scaled.height()) / 2.0);
painter->drawPixmap(QRectF(topLeft, scaled), pixmap, QRectF(QPointF(0, 0), pixmapSize));
break;
}
case QQuickImage::PreserveAspectCrop: {
const QSize scaled = pixmapSize
.scaled(rect.size(), Qt::KeepAspectRatioByExpanding)
.toSize();
const QPointF topLeft(rect.x() + (rect.width() - scaled.width()) / 2.0,
rect.y() + (rect.height() - scaled.height()) / 2.0);
QPainterStateGuard stateGuard(painter);
painter->setClipRect(rect);
painter->drawPixmap(QRectF(topLeft, scaled), pixmap, QRectF(QPointF(0, 0), pixmapSize));
break;
}
// TODO: Support remaining fill modes
case QQuickImage::Stretch:
default:
painter->drawPixmap(rect, pixmap, QRectF(QPointF(0, 0), pixmapSize));
break;
}
}
QRect QStyleKitStylePrivate::getAlignedRectInContainer(const QRect &container, const QSize &size,
uint alignment, const QMargins &padding,
const QMargins &margins) const
{
QRect r(QPoint(0, 0), size);
if (alignment & Qt::AlignLeft)
r.moveLeft(container.x() + padding.left() + margins.left());
else if (alignment & Qt::AlignHCenter)
r.moveLeft(container.x() + (container.width() - size.width()) / 2);
else // AlignRight
r.moveLeft(container.right() + 1 - padding.right() - margins.right() - size.width());
if (alignment & Qt::AlignTop)
r.moveTop(container.y() + padding.top() + margins.top());
else if (alignment & Qt::AlignVCenter)
r.moveTop(container.y() + (container.height() - size.height()) / 2);
else // AlignBottom
r.moveTop(container.bottom() + 1 - padding.bottom() - margins.bottom() - size.height());
return r;
}
QStyleKitStylePrivate::ControlMetrics
QStyleKitStylePrivate::metricsForReader(QQStyleKitReader *reader) const
{
Q_ASSERT(reader);
using QtPrivate::qSaturateRound;
const QQStyleKitControlProperties *props = reader->global();
ControlMetrics metrics;
metrics.bgImplicitSize = QSize(0, 0);
metrics.textPadding = QMargins(0, 0, 0, 0);
metrics.padding = QMargins(qSaturateRound(props->leftPadding()),
qSaturateRound(props->topPadding()),
qSaturateRound(props->rightPadding()),
qSaturateRound(props->bottomPadding()));
metrics.spacing = qSaturateRound(props->spacing());
metrics.margins = QMargins(0, 0, 0, 0);
metrics.indicatorImplicitSize = QSize(0, 0);
metrics.indicatorMargins = QMargins(0, 0, 0, 0);
const auto *background = props->background();
// Note: scale is deliberately absent here as it should not affect layout
const auto elementSize = [](qreal w, qreal h) {
constexpr qreal zero(.0);
return QSizeF(std::max(zero, w), std::max(zero, h)).toSize();
};
if (background) {
metrics.bgImplicitSize = elementSize(background->width(), background->height());
metrics.margins = elementMargins(background);
}
const auto *textProps = props->text();
if (textProps) {
metrics.textPadding = QMargins(qSaturateRound(textProps->leftPadding()),
qSaturateRound(textProps->topPadding()),
qSaturateRound(textProps->rightPadding()),
qSaturateRound(textProps->bottomPadding()));
}
const auto *indicator = props->indicator();
if (indicator) {
metrics.indicatorMargins = elementMargins(indicator);
metrics.indicatorImplicitSize = elementSize(indicator->width(), indicator->height());
}
const auto *handle = props->handle();
if (handle) {
metrics.handleImplicitSize = elementSize(handle->width(), handle->height());
metrics.handleMargins = elementMargins(handle);
}
return metrics;
}
/*!
Constructs a QStyleKitStyle with no style loaded.
Use \l setStylePath() to load a QML \l Style after construction. Until a
Style loads successfully, the widgets use an empty fallback style.
*/
QStyleKitStyle::QStyleKitStyle()
: QCommonStyle(*new QStyleKitStylePrivate())
{
}
/*!
Constructs a QStyleKitStyle and loads the QML \l Style at \a filePath.
See the \l stylePath property for the accepted path forms.
If the path is invalid or the root object of the loaded component is not
a \l Style, QStyleKitStyle emits a warning and the widgets use an empty
fallback style until a valid \l stylePath is set.
*/
QStyleKitStyle::QStyleKitStyle(const QString &filePath)
: QCommonStyle(*new QStyleKitStylePrivate())
{
Q_D(QStyleKitStyle);
d->stylePath = filePath;
if (!d->loadStyle()) {
d->stylePath.clear();
qWarning("QStyleKitStyle: Failed to load style from %s", qPrintable(filePath));
}
}
/*!
Destroys the QStyleKitStyle.
*/
QStyleKitStyle::~QStyleKitStyle()
{
}
/*!
Returns the path most recently set for the QML \l Style file.
\sa setStylePath()
*/
QString QStyleKitStyle::stylePath() const
{
Q_D(const QStyleKitStyle);
return d->stylePath;
}
/*!
Loads the QML \l Style at \a filePath and applies it to all widgets.
\a filePath is a path to a local file or a path to a file in the resource
file system; see the \l stylePath property for the accepted forms. If it is
the same as the current \l stylePath, this function does nothing.
If the new style does not load, QStyleKitStyle emits a warning, keeps
the previously loaded style active, and does not emit \l stylePathChanged().
\sa stylePath()
*/
void QStyleKitStyle::setStylePath(const QString &filePath)
{
Q_D(QStyleKitStyle);
if (d->stylePath == filePath)
return;
const QString oldThemeName = themeName();
const QStringList oldThemeNames = availableThemeNames();
const QStringList oldCustomThemeNames = customThemeNames();
const QString previousPath = d->stylePath;
d->stylePath = filePath;
if (!d->loadStyle()) {
d->stylePath = previousPath;
return;
}
d->updateStyle();
if (const QStringList names = availableThemeNames(); names != oldThemeNames)
emit availableThemeNamesChanged(names);
if (const QStringList names = customThemeNames(); names != oldCustomThemeNames)
emit customThemeNamesChanged(names);
emit stylePathChanged(d->stylePath);
}
/*!
Returns the name of the requested theme, or an empty string when no
\l Style is loaded.
When the theme name is \c System, this function returns \c System rather
than the \c Light or \c Dark theme that the platform color scheme
resolves to.
\sa setThemeName(), availableThemeNames()
*/
QString QStyleKitStyle::themeName() const
{
Q_D(const QStyleKitStyle);
return d->style ? d->style->themeName() : QString();
}
/*!
Activates the theme named \a themeName.
\a themeName must be one of the entries in \l availableThemeNames(),
which includes the special name \c System for following the platform
color scheme. The comparison ignores case.
If no \l Style is loaded, this function emits a warning and returns
without changing the active theme. If a Style is loaded but \a themeName
matches none of its themes, the Style emits a warning and applies no
theme.
\sa themeName(), availableThemeNames()
*/
void QStyleKitStyle::setThemeName(const QString &themeName)
{
Q_D(QStyleKitStyle);
if (!d->style) {
qWarning("QStyleKitStyle: No style loaded, cannot set theme name.");
return;
}
if (d->style->themeName() == themeName)
return;
d->style->setThemeName(themeName);
d->updateStyle();
emit themeNameChanged(d->style->themeName());
}
/*!
Returns the names of all themes that the loaded \l Style exposes. The
list always includes \c System, and includes \c Light and \c Dark when
the Style defines those themes, together with any custom themes that
the style author defines. Returns an empty list when no style is
loaded.
\sa customThemeNames(), themeName()
*/
QStringList QStyleKitStyle::availableThemeNames() const
{
Q_D(const QStyleKitStyle);
return d->style ? d->style->availableThemeNames() : QStringList();
}
/*!
Returns the names of the custom themes defined by the loaded
\l Style, excluding the built-in \c Light and \c Dark themes.
Returns an empty list when no style is loaded.
\sa availableThemeNames()
*/
QStringList QStyleKitStyle::customThemeNames() const
{
Q_D(const QStyleKitStyle);
return d->style ? d->style->customThemeNames() : QStringList();
}
/*! \reimp */
void QStyleKitStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p,
const QWidget *w) const
{
Q_D(const QStyleKitStyle);
switch (pe) {
#if QT_CONFIG(menu)
case PE_PanelMenu: {
const auto controlType = w && w->inherits("QComboBoxPrivateContainer")
? QQStyleKitReader::ControlType::Popup
: QQStyleKitReader::ControlType::Menu;
const auto r = d->resolve(w, controlType, opt->state);
if (!r.isValid())
break;
d->drawStyledItemRect(r.background(), opt->rect.marginsRemoved(r.metrics->margins), p);
return;
}
case PE_FrameMenu:
// already drawn in PE_PanelMenu
return;
#endif // QT_CONFIG(menu)
#if QT_CONFIG(menubar)
case PE_PanelMenuBar: {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::MenuBar, opt->state);
if (!r.isValid())
break;
d->drawStyledItemRect(r.background(), opt->rect.marginsRemoved(r.metrics->margins), p);
return;
}
#endif // QT_CONFIG(menubar)
case PE_FrameButtonBevel:
if (const auto *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
const auto controlType = btn->features & QStyleOptionButton::Flat
? QQStyleKitReader::ControlType::FlatButton
: QQStyleKitReader::ControlType::Button;
const auto r = d->resolve(w, controlType, btn->state);
if (!r.isValid())
break;
d->drawStyledItemRect(r.background(), opt->rect, p);
return;
}
break;
case PE_Frame: {
QQStyleKitReader::ControlType controlType;
const bool isPopup = w
&& (false
#if QT_CONFIG(combobox)
|| w->inherits("QComboBoxPrivateContainer")
#endif
);
if (isPopup)
controlType = QQStyleKitReader::ControlType::Popup;
#if QT_CONFIG(lineedit)
else if (qobject_cast<const QLineEdit *>(w))
controlType = QQStyleKitReader::ControlType::TextField;
#endif
else
controlType = QQStyleKitReader::ControlType::Frame;
const auto r = d->resolve(w, controlType, opt->state);
if (!r.isValid())
break;
d->drawStyledItemRect(r.background(), opt->rect, p);
return;
}
#if QT_CONFIG(lineedit)
case PE_PanelLineEdit:
if (const auto *lineEdit = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
// LineEdit sets Sunken flag to indicate Sunken frame,
// but the style uses it to indicate pressed state, so ignore it
QStyleOption lineEditOpt(*lineEdit);
lineEditOpt.state &= ~QStyle::State_Sunken;
const auto r = d->resolve(w, QQStyleKitReader::ControlType::TextField,
lineEditOpt.state);
if (!r.isValid())
break;
// LineEdit draws its own text using the Text role so update that role in the palette
if (auto *le = qobject_cast<const QLineEdit *>(w)) {
if (const auto *txt = r.text(); txt && txt->isDefined(QQSK::Property::Color)) {
QPalette stylePalette;
stylePalette.setColor(QPalette::Text, txt->color());
d->setStylePalette(const_cast<QLineEdit *>(le), stylePalette);
}
const_cast<QStyleKitStylePrivate *>(d)->setStyleFont(const_cast<QLineEdit *>(le),
r.font());
}
const QObject *parent = w ? w->parent() : nullptr;
# if QT_CONFIG(spinbox)
const bool isInSpinBox = qobject_cast<const QAbstractSpinBox *>(parent);
# else
const bool isInSpinBox = false;
# endif
# if QT_CONFIG(combobox)
const bool isInComboBox = qobject_cast<const QComboBox *>(parent);
# else
const bool isInComboBox = false;
# endif
// For spinbox and combobox, the line edit doesn't have its own background in the Controls style
if (isInSpinBox || isInComboBox)
return;
d->drawStyledItemRect(r.background(), opt->rect, p);
return;
}
break;
#endif // QT_CONFIG(lineedit)
#if QT_CONFIG(itemviews)
case PE_PanelItemViewItem: {
const auto r = d->resolveSubElement(w, opt, itemViewControlType(opt), opt->state);
if (!r.isValid())
break;
d->drawStyledItemRect(r.background(), opt->rect, p);
return;
}
#endif // QT_CONFIG(itemviews)
case PE_IndicatorCheckBox:
case PE_IndicatorRadioButton: {
const auto controlType = pe == PE_IndicatorCheckBox
? QQStyleKitReader::ControlType::CheckBox
: QQStyleKitReader::ControlType::RadioButton;
const auto r = d->resolve(w, controlType, opt->state);
if (!r.isValid())
break;
d->drawControlIndicator(r.indicator(), opt->rect, p);
return;
}
case PE_IndicatorArrowDown: {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ComboBox, opt->state);
if (!r.isValid())
break;
d->drawControlIndicator(r.indicator(), opt->rect, p);
return;
}
case PE_IndicatorItemViewItemCheck: {
// track = false: Checked state is synthetic
const auto r = d->resolveSubElement(w, opt, itemViewControlType(opt), opt->state, false);
if (!r.isValid())
break;
d->drawControlIndicator(r.indicator(), opt->rect, p);
return;
}
#if QT_CONFIG(spinbox)
case PE_IndicatorSpinUp:
case PE_IndicatorSpinDown: {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::SpinBox, opt->state);
if (!r.isValid())
break;
const auto *indicator = r.indicator();
const auto *upDownIndicator = indicator
? (pe == PE_IndicatorSpinUp ? indicator->first() : indicator->second())
: nullptr;
d->drawControlIndicator(upDownIndicator, opt->rect, p);
return;
}
#endif // QT_CONFIG(spinbox)
#if QT_CONFIG(groupbox)
case PE_FrameGroupBox: {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::GroupBox, opt->state);
if (!r.isValid())
break;
d->drawStyledItemRect(r.background(), opt->rect, p);
return;
}
#endif // QT_CONFIG(groupbox)
#if QT_CONFIG(tabwidget)
case PE_FrameTabWidget: {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::Page, opt->state);
if (!r.isValid())
break;
d->drawStyledItemRect(r.background(), opt->rect, p);
return;
}
#endif // QT_CONFIG(tabwidget)
#if QT_CONFIG(tabbar)
// Not (yet) supported in StyleKit
case PE_IndicatorTabClose:
case PE_IndicatorTabTearRight:
case PE_IndicatorTabTearLeft:
return;
#endif // QT_CONFIG(tabbar)
#if QT_CONFIG(toolbutton)
case PE_FrameButtonTool:
case PE_PanelButtonTool: {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ToolButton, opt->state);
if (!r.isValid())
break;
d->drawStyledItemRect(r.background(), opt->rect, p);
return;
}
#endif // QT_CONFIG(toolbutton)
#if QT_CONFIG(toolbar)
case PE_IndicatorToolBarHandle:
return; // no handle in Controls style
case PE_IndicatorToolBarSeparator: {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ToolSeparator, opt->state);
if (!r.isValid())
break;
const auto &metrics = *r.metrics;
// background
QRect frameRect = opt->rect.marginsRemoved(metrics.margins);
d->drawStyledItemRect(r.background(), frameRect, p);
// indicator
const auto *indicator = r.indicator();
if (!indicator || !indicator->visible() || indicator->opacity() == 0)
return;
QRect contentRect = frameRect.marginsRemoved(metrics.padding);
const int iw = QtPrivate::qSaturateRound(
resolvedWidth(indicator,
contentRect.width()
- metrics.indicatorMargins.left()
- metrics.indicatorMargins.right()));
const int ih = QtPrivate::qSaturateRound(
resolvedHeight(indicator,
contentRect.height()
- metrics.indicatorMargins.top()
- metrics.indicatorMargins.bottom()));
const uint alignment = resolvedAlignment(indicator->alignment(), Qt::AlignHCenter,
Qt::AlignVCenter);
const QRect indicatorRect = d->getAlignedRectInContainer(
contentRect, QSize(iw, ih), alignment, QMargins(), metrics.indicatorMargins);
d->drawControlIndicator(indicator, visualRect(opt->direction, contentRect, indicatorRect),
p);
return;
}
#endif
default:
break;
}
QCommonStyle::drawPrimitive(pe, opt, p, w);
}
/*! \reimp */
void QStyleKitStyle::drawControl(ControlElement element, const QStyleOption *opt, QPainter *p,
const QWidget *w) const
{
Q_D(const QStyleKitStyle);
switch (element) {
case CE_PushButton:
if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
proxy()->drawControl(CE_PushButtonBevel, btn, p, w);
QStyleOptionButton btnContent(*btn);
btnContent.rect = subElementRect(SE_PushButtonContents, btn, w);
proxy()->drawControl(CE_PushButtonLabel, &btnContent, p, w);
}
return;
case CE_PushButtonBevel:
if (const auto *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
QStyleOptionButton btnBg(*btn);
btnBg.rect = subElementRect(SE_PushButtonBevel, btn, w);
proxy()->drawPrimitive(PE_FrameButtonBevel, &btnBg, p, w);
}
return;
case CE_PushButtonLabel:
if (const auto *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
const auto controlType = btn->features & QStyleOptionButton::Flat
? QQStyleKitReader::ControlType::FlatButton
: QQStyleKitReader::ControlType::Button;
const auto r = d->resolve(w, controlType, btn->state);
if (!r.isValid())
break;
const auto metrics = r.metrics;
QRect textRect = opt->rect;
uint textFlags = Qt::TextShowMnemonic;
if (!styleHint(SH_UnderlineShortcut, opt, w))
textFlags |= Qt::TextHideMnemonic;
// icon
const QIcon icon = btn->icon;
if (!icon.isNull()) {
const auto *textProps = r.text();
uint iconTextFlags = textFlags;
iconTextFlags |= textProps
? resolvedAlignment(textProps->alignment(), Qt::AlignHCenter, Qt::AlignVCenter)
: uint(Qt::AlignHCenter | Qt::AlignVCenter);
QIcon::Mode mode = btn->state & State_Enabled ? QIcon::Normal : QIcon::Disabled;
if (mode == QIcon::Normal && btn->state & State_HasFocus)
mode = QIcon::Active;
QIcon::State iconState = btn->state & State_On ? QIcon::On : QIcon::Off;
const auto paintDeviceDpr = p->device()->devicePixelRatio();
QPixmap pixmap = icon.pixmap(btn->iconSize, paintDeviceDpr, mode, iconState);
int pixmapWidth = pixmap.width() / pixmap.devicePixelRatio();
int pixmapHeight = pixmap.height() / pixmap.devicePixelRatio();
int labelWidth = pixmapWidth;
int iconSpacing = metrics->spacing;
int textWidth = btn->fontMetrics
.boundingRect(opt->rect, iconTextFlags, btn->text)
.width();
if (!btn->text.isEmpty()) {
labelWidth += (textWidth
+ iconSpacing + textProps->leftPadding() + textProps->rightPadding());
}
QRect iconRect;
if (iconTextFlags & Qt::AlignLeft) {
iconRect = QRect(textRect.x(),
textRect.y() + (textRect.height() - pixmapHeight) / 2,
pixmapWidth, pixmapHeight);
} else if (iconTextFlags & Qt::AlignHCenter) {
iconRect = QRect(textRect.x() + (textRect.width() - labelWidth) / 2,
textRect.y() + (textRect.height() - pixmapHeight) / 2,
pixmapWidth, pixmapHeight);
} else {
iconRect = QRect(textRect.x() + textRect.width() - labelWidth,
textRect.y() + (textRect.height() - pixmapHeight) / 2,
pixmapWidth, pixmapHeight);
}
iconRect = visualRect(btn->direction, textRect, iconRect);
// After placing the icon, left-align the text relative to it
textFlags &= ~Qt::AlignHorizontal_Mask;
textFlags |= Qt::AlignLeft;
if (btn->direction == Qt::RightToLeft)
textRect.setRight(iconRect.left() - iconSpacing);
else
textRect.setLeft(iconRect.left() + iconRect.width() + iconSpacing);
p->drawPixmap(iconRect, pixmap);
}
if (btn->features & QStyleOptionButton::HasMenu) {
int indicatorSize = pixelMetric(PM_MenuButtonIndicator, btn, w);
if (btn->direction == Qt::LeftToRight)
textRect = textRect.adjusted(0, 0, -indicatorSize, 0);
else
textRect = textRect.adjusted(indicatorSize, 0, 0, 0);
}
d->drawControlText(r.text(), r.font(), textRect, btn->text, textFlags, p);
return;
}
break;
case CE_CheckBox:
case CE_RadioButton:
if (const auto *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
const auto controlType = element == CE_CheckBox
? QQStyleKitReader::ControlType::CheckBox
: QQStyleKitReader::ControlType::RadioButton;
const auto r = d->resolve(w, controlType, btn->state);
if (!r.isValid())
break;
QRect backgroundRect = btn->rect.marginsRemoved(r.metrics->margins);
// background
d->drawStyledItemRect(r.background(), backgroundRect, p);
// label
QStyleOptionButton btnContent(*btn);
const auto contentElement = element == CE_CheckBox
? SE_CheckBoxContents
: SE_RadioButtonContents;
btnContent.rect = subElementRect(contentElement, btn, w);
const auto labelElement = element == CE_CheckBox
? CE_CheckBoxLabel
: CE_RadioButtonLabel;
proxy()->drawControl(labelElement, &btnContent, p, w);
// indicator
QStyleOptionButton indicator(*btn);
const auto indicatorElement = element == CE_CheckBox
? SE_CheckBoxIndicator
: SE_RadioButtonIndicator;
indicator.rect = subElementRect(indicatorElement, btn, w);
const auto primitiveElement = element == CE_CheckBox
? PE_IndicatorCheckBox
: PE_IndicatorRadioButton;
proxy()->drawPrimitive(primitiveElement, &indicator, p, w);
return;
}
break;
case CE_CheckBoxLabel:
case CE_RadioButtonLabel:
if (const auto *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
const auto controlType = element == CE_CheckBoxLabel
? QQStyleKitReader::ControlType::CheckBox
: QQStyleKitReader::ControlType::RadioButton;
const auto r = d->resolve(w, controlType, btn->state);
if (!r.isValid())
break;
uint textFlags = Qt::TextShowMnemonic;
if (!styleHint(SH_UnderlineShortcut, opt, w))
textFlags |= Qt::TextHideMnemonic;
d->drawControlText(r.text(), r.font(), opt->rect, btn->text, textFlags, p);
return;
}
break;
#if QT_CONFIG(combobox)
case CE_ComboBoxLabel:
if (const auto *comboBox = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
if (comboBox->editable)
return;
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ComboBox, comboBox->state);
if (!r.isValid())
break;
const QRect textRect = subControlRect(CC_ComboBox, comboBox, SC_ComboBoxEditField, w)
.marginsRemoved(r.metrics->textPadding);
uint textFlags = Qt::TextShowMnemonic;
if (!styleHint(SH_UnderlineShortcut, opt, w))
textFlags |= Qt::TextHideMnemonic;
d->drawControlText(r.text(), r.font(), textRect, comboBox->currentText, textFlags, p);
return;
}
break;
#endif // QT_CONFIG(combobox)
#if QT_CONFIG(progressbar)
case CE_ProgressBar:
if (const QStyleOptionProgressBar *progressBar =
qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ProgressBar,
progressBar->state);
if (!r.isValid())
break;
QRect backgroundRect = progressBar->rect.marginsRemoved(r.metrics->margins);
// background
d->drawStyledItemRect(r.background(), backgroundRect, p);
// groove
QStyleOptionProgressBar contents(*progressBar);
contents.rect = subElementRect(SE_ProgressBarGroove, progressBar, w);
const QRect grooveRect = contents.rect;
proxy()->drawControl(CE_ProgressBarGroove, &contents, p, w);
// track
contents.rect = subElementRect(SE_ProgressBarContents, progressBar, w);
// The track is the groove's foreground child, so it inherits the indicator's
// transform
QPainterStateGuard stateGuard(p);
if (applyDelegateTransform(p, r.indicator(), grooveRect))
proxy()->drawControl(CE_ProgressBarContents, &contents, p, w);
// We intentionally don't draw the label as it is not drawn on the Controls Style
return;
}
break;
case CE_ProgressBarGroove: {
// groove = indicator background
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ProgressBar, opt->state);
if (!r.isValid())
break;
d->drawStyledItemRect(r.indicator(), opt->rect, p);
return;
}
case CE_ProgressBarContents:
// contents = indicator foreground
if (const auto *progressBar = qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ProgressBar,
progressBar->state);
if (!r.isValid())
break;
const auto progress = progressBar->progress;
const auto ratio = progressBar->maximum > progressBar->minimum
? (progress - progressBar->minimum)
/ static_cast<qreal>(progressBar->maximum - progressBar->minimum)
: 0;
const auto width = static_cast<int>(progressBar->rect.width() * ratio);
const auto x = progressBar->invertedAppearance
? progressBar->rect.right() - width
: progressBar->rect.left();
const auto contentsRect = QRect(x, progressBar->rect.y(),
width, progressBar->rect.height());
const auto *foreground = r.indicator() ? r.indicator()->foreground() : nullptr;
if (foreground && foreground->visible() && foreground->opacity() > 0) {
QPainterStateGuard stateGuard(p);
if (applyDelegateTransform(p, foreground, progressBar->rect))
d->drawStyledItemContents(foreground, contentsRect, p);
}
return;
}
break;
#endif // QT_CONFIG(progressbar)
#if QT_CONFIG(itemviews)
case CE_ItemViewItem:
if (const auto *itemViewOption = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
const auto r = d->resolveSubElement(w, opt, itemViewControlType(opt), opt->state);
if (!r.isValid())
break;
QStyleOptionViewItem optBg(*itemViewOption);
optBg.rect = optBg.rect.marginsRemoved(r.metrics->margins);
QRect indicatorRect = subElementRect(SE_ItemViewItemCheckIndicator, itemViewOption, w);
QRect iconRect = subElementRect(SE_ItemViewItemDecoration, itemViewOption, w);
QRect textRect = subElementRect(SE_ItemViewItemText, itemViewOption, w);
// Capture text properties + font now, before the indicator
// drawPrimitive below potentially mutates the reader's state
const auto *itemTextProps = r.text();
const QFont itemFont = r.font();
// background
proxy()->drawPrimitive(PE_PanelItemViewItem, &optBg, p, w);
// indicator
if (itemViewOption->features & QStyleOptionViewItem::HasCheckIndicator) {
QStyleOptionViewItem option(*itemViewOption);
option.rect = indicatorRect;
option.state = option.state & ~QStyle::State_HasFocus;
switch (itemViewOption->checkState) {
case Qt::Unchecked:
option.state |= QStyle::State_Off;
break;
case Qt::PartiallyChecked:
option.state |= QStyle::State_NoChange;
break;
case Qt::Checked:
option.state |= QStyle::State_On;
break;
}
proxy()->drawPrimitive(QStyle::PE_IndicatorItemViewItemCheck, &option, p, w);
}
// icon
QIcon::Mode mode = QIcon::Normal;
if (!(itemViewOption->state & QStyle::State_Enabled))
mode = QIcon::Disabled;
else if (itemViewOption->state & QStyle::State_Selected)
mode = QIcon::Selected;
QIcon::State state = itemViewOption->state & QStyle::State_Open
? QIcon::On
: QIcon::Off;
itemViewOption->icon.paint(p, iconRect, itemViewOption->decorationAlignment, mode,
state);
// draw text
uint textFlags = Qt::TextShowMnemonic;
if (!styleHint(SH_UnderlineShortcut, opt, w))
textFlags |= Qt::TextHideMnemonic;
d->drawControlText(itemTextProps, itemFont, textRect, itemViewOption->text, textFlags,
p, Qt::AlignLeft | Qt::AlignVCenter);
return;
}
break;
#endif // QT_CONFIG(itemviews)
#ifndef QT_NO_FRAME
case CE_ShapedFrame: {
# if QT_CONFIG(scrollarea)
if (qobject_cast<const QAbstractScrollArea *>(w))
// ScrollView has no styling in Controls, keep consistent
return;
# endif
auto controlType = QQStyleKitReader::ControlType::Frame;
# if QT_CONFIG(combobox)
if (w && w->inherits("QComboBoxPrivateContainer"))
controlType = QQStyleKitReader::ControlType::Popup;
# endif
# if QT_CONFIG(textedit)
else if (w && qobject_cast<const QTextEdit *>(w))
controlType = QQStyleKitReader::ControlType::TextArea;
# endif
# if QT_CONFIG(label)
else if (w && qobject_cast<const QLabel *>(w))
controlType = QQStyleKitReader::ControlType::Label;
# endif
// Fallback to Frame
if (qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
const auto r = d->resolve(w, controlType, opt->state);
if (!r.isValid())
break;
QRect backgroundRect = opt->rect.marginsRemoved(r.metrics->margins);
d->drawStyledItemRect(r.background(), backgroundRect, p);
return;
}
break;
}
#endif // QT_NO_FRAME
#if QT_CONFIG(scrollbar)
case CE_ScrollBarSlider: {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ScrollBar, opt->state);
if (!r.isValid())
break;
d->drawControlIndicator(r.indicator(), opt->rect, p);
return;
}
#endif // QT_CONFIG(scrollbar)
#if QT_CONFIG(toolbutton)
case CE_ToolButtonLabel:
if (const auto *tool = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ToolButton, tool->state);
if (!r.isValid())
break;
const auto *metrics = r.metrics;
QRect rect = tool->rect.marginsRemoved(metrics->textPadding);
uint textFlags = Qt::TextShowMnemonic;
if (!styleHint(SH_UnderlineShortcut, opt, w))
textFlags |= Qt::TextHideMnemonic;
const bool hasArrow = tool->features & QStyleOptionToolButton::Arrow;
if ((!hasArrow && tool->icon.isNull() && !tool->text.isEmpty())
|| tool->toolButtonStyle == Qt::ToolButtonTextOnly) {
d->drawControlText(r.text(), r.font(), rect, tool->text, textFlags, p);
return;
}
QPixmap pm;
QSize pmSize;
if (!hasArrow && !tool->icon.isNull()) {
const QIcon::State iconState = tool->state & State_On ? QIcon::On : QIcon::Off;
QIcon::Mode mode;
if (!(tool->state & State_Enabled))
mode = QIcon::Disabled;
else if ((tool->state & State_MouseOver) && (tool->state & State_AutoRaise))
mode = QIcon::Active;
else
mode = QIcon::Normal;
pm = tool->icon.pixmap(tool->iconSize, p->device()->devicePixelRatio(), mode,
iconState);
pmSize = pm.size() / pm.devicePixelRatio();
}
if (tool->toolButtonStyle == Qt::ToolButtonIconOnly || tool->text.isEmpty()) {
if (!pm.isNull()) {
p->drawPixmap(visualRect(tool->direction, rect,
QRect(rect.x() + (rect.width() - pmSize.width()) / 2,
rect.y() + (rect.height() - pmSize.height()) / 2,
pmSize.width(), pmSize.height())),
pm);
}
return;
}
if (tool->toolButtonStyle == Qt::ToolButtonTextUnderIcon) {
if (!pm.isNull()) {
const QRect pr(rect.x() + (rect.width() - pmSize.width()) / 2, rect.y(),
pmSize.width(), pmSize.height());
p->drawPixmap(visualRect(tool->direction, rect, pr), pm);
QRect tr = rect;
tr.setTop(rect.y() + pmSize.height() + metrics->spacing);
d->drawControlText(r.text(), r.font(), visualRect(tool->direction, rect, tr),
tool->text, textFlags, p, Qt::AlignCenter);
} else {
d->drawControlText(r.text(), r.font(), rect, tool->text, textFlags, p,
Qt::AlignCenter);
}
} else {
if (!pm.isNull()) {
const QRect pr(rect.x(), rect.y() + (rect.height() - pmSize.height()) / 2,
pmSize.width(), pmSize.height());
p->drawPixmap(visualRect(tool->direction, rect, pr), pm);
QRect tr = rect;
tr.setLeft(rect.x() + pmSize.width() + metrics->spacing);
d->drawControlText(r.text(), r.font(), visualRect(tool->direction, rect, tr),
tool->text, textFlags, p, Qt::AlignLeft | Qt::AlignVCenter);
} else {
d->drawControlText(r.text(), r.font(), rect, tool->text, textFlags, p,
Qt::AlignLeft | Qt::AlignVCenter);
}
}
return;
}
break;
#endif // QT_CONFIG(toolbutton)
#if QT_CONFIG(menu)
case CE_MenuItem:
if (const auto *menuItem = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
using QtPrivate::qSaturateRound;
// MenuItemSeperator
if (menuItem->menuItemType == QStyleOptionMenuItem::Separator) {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::MenuSeparator,
menuItem->state);
if (!r.isValid())
break;
const auto metrics = r.metrics;
QRect contentRect = opt->rect
.marginsRemoved(metrics->margins)
.marginsRemoved(metrics->padding);
const auto *indicator = r.indicator();
if (indicator && indicator->visible() && indicator->opacity() > 0) {
const int w = qSaturateRound(resolvedWidth(
indicator,
contentRect.width() - indicator->leftMargin() - indicator->rightMargin()));
const int h = qSaturateRound(resolvedHeight(
indicator,
contentRect.height() - indicator->topMargin() - indicator->bottomMargin()));
const uint align = resolvedAlignment(indicator->alignment(), Qt::AlignHCenter,
Qt::AlignVCenter);
const QRect indicatorRect = d->getAlignedRectInContainer(
contentRect, QSize(w, h), align, QMargins(), elementMargins(indicator));
d->drawControlIndicator(
indicator, visualRect(opt->direction, contentRect, indicatorRect), p);
}
return;
}
// MenuItem
QStyle::State adjustedState = menuItem->state;
if (adjustedState & State_Selected)
adjustedState |= State_MouseOver;
const auto r = d->resolveSubElement(w, opt, QQStyleKitReader::ControlType::MenuItem,
adjustedState);
if (!r.isValid())
break;
// background
d->drawStyledItemRect(r.background(), opt->rect.marginsRemoved(r.metrics->margins), p);
// indicators
const QRect contentRect = opt->rect.marginsRemoved(r.metrics->padding);
const auto *indicator = r.indicator();
const auto *first = indicator ? indicator->first() : nullptr;
const auto *second = indicator ? indicator->second() : nullptr;
int leftOffset = 0, rightOffset = 0;
const int spacing = r.metrics->spacing;
QRect firstDrawRect, secondDrawRect;
auto placeSubIndicator = [&](const QQStyleKitIndicatorProperties *sub,
QRect &drawRect) {
if (!sub)
return;
const QSize sz(qSaturateRound(sub->width()), qSaturateRound(sub->height()));
const QMargins subMargins = elementMargins(sub);
const int slotW = qSaturateRound(sub->leftMargin() + sub->width() + sub->rightMargin());
if (sub->alignment() & Qt::AlignRight) {
const QRect slot(contentRect.right() - rightOffset - slotW, contentRect.top(),
slotW, contentRect.height());
drawRect = d->getAlignedRectInContainer(
slot, sz, Qt::AlignRight | Qt::AlignVCenter, { }, subMargins);
rightOffset += slotW + spacing;
} else {
const QRect slot(contentRect.left() + leftOffset, contentRect.top(), slotW,
contentRect.height());
drawRect = d->getAlignedRectInContainer(
slot, sz, Qt::AlignLeft | Qt::AlignVCenter, { }, subMargins);
leftOffset += slotW + spacing;
}
};
placeSubIndicator(first, firstDrawRect);
placeSubIndicator(second, secondDrawRect);
if (first && menuItem->checkType != QStyleOptionMenuItem::NotCheckable
&& menuItem->checked) {
d->drawControlIndicator(
first, visualRect(menuItem->direction, contentRect, firstDrawRect), p);
}
if (second && menuItem->menuItemType == QStyleOptionMenuItem::SubMenu) {
d->drawControlIndicator(
second, visualRect(menuItem->direction, contentRect, secondDrawRect), p);
}
// text
QRect textRect = contentRect
.adjusted(leftOffset, 0, -rightOffset, 0)
.marginsRemoved(r.metrics->textPadding);
if (!menuItem->icon.isNull()) {
const int iconW = menuItem->maxIconWidth;
const QIcon::Mode mode = (menuItem->state & State_Enabled)
? QIcon::Normal
: QIcon::Disabled;
const int iconExtent = proxy()->pixelMetric(PM_SmallIconSize, opt, w);
const QPixmap pm = menuItem->icon.pixmap(
QSize(iconExtent, iconExtent), p->device()->devicePixelRatio(), mode);
const QSize pmSize = pm.size() / pm.devicePixelRatio();
const int pmW = pmSize.width();
const int pmH = pmSize.height();
p->drawPixmap(
visualRect(menuItem->direction, textRect,
QRect(textRect.left(),
textRect.top() + (textRect.height() - pmH) / 2, pmW, pmH)),
pm);
textRect.setLeft(textRect.left() + iconW + r.metrics->spacing);
}
const int tabIdx = menuItem->text.indexOf(QLatin1Char('\t'));
const QString label = tabIdx >= 0 ? menuItem->text.left(tabIdx) : menuItem->text;
const QString shortcut = tabIdx >= 0 ? menuItem->text.mid(tabIdx + 1) : QString();
uint textFlags = Qt::TextShowMnemonic;
if (!styleHint(SH_UnderlineShortcut, opt, w))
textFlags |= Qt::TextHideMnemonic;
QRect labelRect = textRect;
if (!shortcut.isEmpty() && menuItem->reservedShortcutWidth > 0) {
const QRect shortcutRect(textRect.right() - menuItem->reservedShortcutWidth + 1,
textRect.top(), menuItem->reservedShortcutWidth,
textRect.height());
labelRect.setRight(shortcutRect.left() - r.metrics->spacing - 1);
const int shortcutFlags =
int(QStyle::visualAlignment(menuItem->direction,
Qt::AlignRight | Qt::AlignVCenter))
| int(textFlags);
p->save();
p->setFont(r.font().resolve(p->font()));
p->setBrush(Qt::NoBrush);
if (const auto *shortcutText = r.text())
p->setPen(shortcutText->color());
p->drawText(visualRect(menuItem->direction, textRect, shortcutRect), shortcutFlags,
shortcut);
p->restore();
}
d->drawControlText(r.text(), r.font(),
visualRect(menuItem->direction, textRect, labelRect), label,
textFlags, p, Qt::AlignLeft | Qt::AlignVCenter);
return;
}
break;
// no tearoff or bottom scroller support in Controls style,
// so make it consistent and don't draw them
case CE_MenuScroller:
case CE_MenuTearoff:
return;
case CE_MenuEmptyArea:
return;
#endif // QT_CONFIG(menu)
#if QT_CONFIG(menubar)
case CE_MenuBarEmptyArea:
return;
case CE_MenuBarItem:
if (const auto *menuItem = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
QStyle::State adjustedState = menuItem->state;
if (adjustedState & State_Selected)
adjustedState |= State_MouseOver;
const auto r = d->resolveSubElement(w, opt, QQStyleKitReader::ControlType::MenuBarItem,
adjustedState);
if (!r.isValid())
break;
d->drawStyledItemRect(r.background(), opt->rect.marginsRemoved(r.metrics->margins), p);
const QRect textRect = opt->rect.marginsRemoved(
r.metrics->margins + r.metrics->padding + r.metrics->textPadding);
uint textFlags = Qt::TextShowMnemonic;
if (!styleHint(SH_UnderlineShortcut, opt, w))
textFlags |= Qt::TextHideMnemonic;
d->drawControlText(r.text(), r.font(), textRect, menuItem->text, textFlags, p);
return;
}
break;
#endif // QT_CONFIG(menubar)
#if QT_CONFIG(tabbar)
case CE_TabBarTab:
if (const auto *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
const auto r = d->resolveSubElement(w, opt, QQStyleKitReader::ControlType::TabButton,
tab->state);
if (!r.isValid())
break;
const auto &m = *r.metrics;
const QRect backgroundRect = opt->rect.marginsRemoved(m.margins);
d->drawStyledItemRect(r.background(), backgroundRect, p);
const QRect contentRect = backgroundRect.marginsRemoved(m.padding);
uint textFlags = Qt::TextShowMnemonic;
if (!styleHint(SH_UnderlineShortcut, opt, w))
textFlags |= Qt::TextHideMnemonic;
if (!tab->icon.isNull()) {
const QIcon::Mode mode = (tab->state & State_Enabled)
? QIcon::Normal
: QIcon::Disabled;
const QIcon::State iconState = (tab->state & State_Selected)
? QIcon::On
: QIcon::Off;
const QPixmap pm = tab->icon.pixmap(tab->iconSize, p->device()->devicePixelRatio(),
mode, iconState);
const QSize pmSize = pm.size() / pm.devicePixelRatio();
const QRect pr(contentRect.x(),
contentRect.y() + (contentRect.height() - pmSize.height()) / 2,
pmSize.width(), pmSize.height());
p->drawPixmap(visualRect(tab->direction, contentRect, pr), pm);
QRect tr = contentRect;
tr.setLeft(contentRect.x() + pmSize.width() + m.spacing);
d->drawControlText(r.text(), r.font(), visualRect(tab->direction, contentRect, tr),
tab->text, textFlags, p, Qt::AlignCenter);
} else {
d->drawControlText(r.text(), r.font(), contentRect, tab->text, textFlags, p,
Qt::AlignCenter);
}
return;
}
break;
#endif // QT_CONFIG(tabbar)
#if QT_CONFIG(toolbar)
case CE_ToolBar:
if (const auto *toolBar = qstyleoption_cast<const QStyleOptionToolBar *>(opt)) {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ToolBar, toolBar->state);
if (!r.isValid())
break;
d->drawStyledItemRect(r.background(), opt->rect.marginsRemoved(r.metrics->margins), p);
return;
}
break;
#endif // QT_CONFIG(toolbar)
default:
break;
}
QCommonStyle::drawControl(element, opt, p, w);
}
/*! \reimp */
QRect QStyleKitStyle::subElementRect(SubElement r, const QStyleOption *opt,
const QWidget *widget) const
{
using QtPrivate::qSaturateRound;
Q_D(const QStyleKitStyle);
switch (r) {
case SE_PushButtonBevel:
if (const auto *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
const auto controlType = btn->features & QStyleOptionButton::Flat
? QQStyleKitReader::ControlType::FlatButton
: QQStyleKitReader::ControlType::Button;
const auto resolved = d->resolveLayout(controlType, opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect rect = opt->rect.marginsRemoved(metrics.margins);
return visualRect(opt->direction, opt->rect, rect);
}
break;
case SE_PushButtonContents:
if (const auto *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
const auto controlType = btn->features & QStyleOptionButton::Flat
? QQStyleKitReader::ControlType::FlatButton
: QQStyleKitReader::ControlType::Button;
const auto resolved = d->resolveLayout(controlType, opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect rect = opt->rect.marginsRemoved(
metrics.margins + metrics.padding + metrics.textPadding);
return visualRect(opt->direction, opt->rect, rect);
}
break;
case SE_CheckBoxContents:
case SE_RadioButtonContents: {
const auto controlType = r == SE_CheckBoxContents
? QQStyleKitReader::ControlType::CheckBox
: QQStyleKitReader::ControlType::RadioButton;
const auto resolved = d->resolveLayout(controlType, opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect contentsRect = opt->rect.marginsRemoved(metrics.margins + metrics.padding);
const auto subElement = r == SE_CheckBoxContents
? SE_CheckBoxIndicator
: SE_RadioButtonIndicator;
QRect indicatorRect = visualRect(opt->direction, opt->rect,
subElementRect(subElement, opt, widget));
const int spacing = metrics.spacing;
const auto *indicator = resolved.indicator();
const uint alignment = indicator
? resolvedAlignment(indicator->alignment(), Qt::AlignLeft, Qt::AlignVCenter)
: uint(Qt::AlignLeft | Qt::AlignVCenter);
if (alignment & Qt::AlignLeft) {
contentsRect.setLeft(indicatorRect.right() + spacing + metrics.textPadding.left());
} else if (alignment & Qt::AlignHCenter) {
contentsRect.setLeft(indicatorRect.right() + spacing + metrics.textPadding.left());
contentsRect.setRight(indicatorRect.left() - spacing - metrics.textPadding.right());
} else {
contentsRect.setRight(indicatorRect.left() - spacing - metrics.textPadding.right());
}
return visualRect(opt->direction, opt->rect, contentsRect);
}
case SE_CheckBoxIndicator:
case SE_RadioButtonIndicator: {
const auto controlType = r == SE_CheckBoxIndicator
? QQStyleKitReader::ControlType::CheckBox
: QQStyleKitReader::ControlType::RadioButton;
const auto resolved = d->resolveLayout(controlType, opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect rect = opt->rect.marginsRemoved(metrics.margins);
const auto *indicator = resolved.indicator();
if (!indicator || !indicator->visible() || indicator->opacity() == 0)
return rect;
const int w = qSaturateRound(resolvedWidth(
indicator,
rect.width() - metrics.padding.left() - metrics.padding.right()
- metrics.indicatorMargins.left() - metrics.indicatorMargins.right()));
const int h = qSaturateRound(resolvedHeight(
indicator,
rect.height()
- metrics.padding.top() - metrics.padding.bottom()
- metrics.indicatorMargins.top() - metrics.indicatorMargins.bottom()));
const uint alignment = resolvedAlignment(indicator->alignment(), Qt::AlignLeft,
Qt::AlignVCenter);
const QRect indicatorRect = d->getAlignedRectInContainer(
rect, QSize(w, h), alignment, metrics.padding, metrics.indicatorMargins);
return visualRect(opt->direction, rect, indicatorRect);
}
#if QT_CONFIG(itemviews)
case SE_ItemViewItemCheckIndicator: {
const auto resolved = d->resolveLayout(itemViewControlType(opt), opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect rect = opt->rect.marginsRemoved(metrics.margins);
const auto *indicator = resolved.indicator();
if (!indicator || !indicator->visible() || indicator->opacity() == 0)
return rect;
const int w = qSaturateRound(resolvedWidth(
indicator,
rect.width()
- metrics.padding.left() - metrics.padding.right()
- metrics.indicatorMargins.left() - metrics.indicatorMargins.right()));
const int h = qSaturateRound(resolvedHeight(
indicator,
rect.height() - metrics.padding.top() - metrics.padding.bottom()
- metrics.indicatorMargins.top() - metrics.indicatorMargins.bottom()));
const uint alignment = resolvedAlignment(indicator->alignment(), Qt::AlignLeft,
Qt::AlignVCenter);
const QRect indicatorRect = d->getAlignedRectInContainer(
rect, QSize(w, h), alignment, metrics.padding, metrics.indicatorMargins);
return visualRect(opt->direction, rect, indicatorRect);
}
case SE_ItemViewItemDecoration:
if (const auto *itemViewOption = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
if (!(itemViewOption->features & QStyleOptionViewItem::HasDecoration))
return QRect();
const auto resolved = d->resolveLayout(itemViewControlType(opt), opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect contentsRect = opt->rect.marginsRemoved(metrics.margins + metrics.padding);
if (itemViewOption->features & QStyleOptionViewItem::HasCheckIndicator) {
const QRect indicatorRect = subElementRect(
SE_ItemViewItemCheckIndicator, opt, widget);
const auto *checkIndicator = resolved.indicator();
const uint checkAlign = checkIndicator
? resolvedAlignment(checkIndicator->alignment(), Qt::AlignLeft,
Qt::AlignVCenter)
: uint(Qt::AlignLeft | Qt::AlignVCenter);
if (checkAlign & Qt::AlignLeft) {
contentsRect.setLeft(indicatorRect.right()
+ metrics.indicatorMargins.right() + metrics.spacing);
} else if (checkAlign & Qt::AlignRight) {
contentsRect.setRight(indicatorRect.left()
- metrics.indicatorMargins.left() - metrics.spacing);
}
}
const QSize decorationSize = itemViewOption->decorationSize
.boundedTo(contentsRect.size());
QRect decorationRect;
switch (itemViewOption->decorationPosition) {
case QStyleOptionViewItem::Top:
decorationRect = QRect(contentsRect.left(), contentsRect.top(),
contentsRect.width(), decorationSize.height());
break;
case QStyleOptionViewItem::Bottom:
decorationRect = QRect(contentsRect.left(),
contentsRect.bottom() - decorationSize.height() + 1,
contentsRect.width(), decorationSize.height());
break;
case QStyleOptionViewItem::Right:
decorationRect = QRect(contentsRect.right() - decorationSize.width() + 1,
contentsRect.top(), decorationSize.width(),
contentsRect.height());
break;
case QStyleOptionViewItem::Left:
default:
decorationRect = QRect(contentsRect.left(), contentsRect.top(),
decorationSize.width(), contentsRect.height());
break;
}
const uint decorationAlign = resolvedAlignment(itemViewOption->decorationAlignment,
Qt::AlignLeft, Qt::AlignVCenter);
decorationRect = d->getAlignedRectInContainer(
decorationRect, decorationSize, decorationAlign, QMargins(0, 0, 0, 0),
QMargins(0, 0, 0, 0));
return visualRect(opt->direction, opt->rect, decorationRect);
}
break;
case SE_ItemViewItemText:
if (const auto *itemViewOption = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
const auto resolved = d->resolveLayout(itemViewControlType(opt), opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect contentsRect = opt->rect.marginsRemoved(metrics.margins + metrics.padding);
const int spacing = metrics.spacing;
if (itemViewOption->features & QStyleOptionViewItem::HasCheckIndicator) {
const QRect indicatorRect = subElementRect(
SE_ItemViewItemCheckIndicator, opt, widget);
const auto *checkIndicator = resolved.indicator();
const uint checkAlign = checkIndicator
? resolvedAlignment(checkIndicator->alignment(), Qt::AlignLeft,
Qt::AlignVCenter)
: uint(Qt::AlignLeft | Qt::AlignVCenter);
if (checkAlign & Qt::AlignLeft) {
contentsRect.setLeft(
indicatorRect.right() + metrics.indicatorMargins.right() + spacing);
} else if (checkAlign & Qt::AlignRight) {
contentsRect.setRight(
indicatorRect.left() - metrics.indicatorMargins.left() - spacing);
}
}
QRect textRect = contentsRect;
if (itemViewOption->features & QStyleOptionViewItem::HasDecoration) {
const QRect decorationRect = subElementRect(
SE_ItemViewItemDecoration, opt, widget);
switch (itemViewOption->decorationPosition) {
case QStyleOptionViewItem::Top:
textRect.setTop(decorationRect.bottom() + spacing);
break;
case QStyleOptionViewItem::Bottom:
textRect.setBottom(decorationRect.top() - spacing);
break;
case QStyleOptionViewItem::Right:
textRect.setRight(decorationRect.left() - spacing);
break;
case QStyleOptionViewItem::Left:
default:
textRect.setLeft(decorationRect.right() + spacing);
break;
}
}
const auto *textProps = resolved.text();
const uint textAlign = textProps
? resolvedAlignment(textProps->alignment(), Qt::AlignLeft, Qt::AlignVCenter)
: uint(Qt::AlignLeft | Qt::AlignVCenter);
if (textAlign & Qt::AlignLeft) {
textRect.setLeft(textRect.left() + metrics.textPadding.left());
} else if (textAlign & Qt::AlignHCenter) {
textRect.setLeft(textRect.left() + metrics.textPadding.left() / 2);
textRect.setRight(textRect.right() - metrics.textPadding.right() / 2);
} else {
textRect.setRight(textRect.right() - metrics.textPadding.right());
}
if (textAlign & Qt::AlignTop) {
textRect.setTop(textRect.top() + metrics.textPadding.top());
} else if (textAlign & Qt::AlignVCenter) {
textRect.setTop(textRect.top() + metrics.textPadding.top() / 2);
textRect.setBottom(textRect.bottom() - metrics.textPadding.bottom() / 2);
} else {
textRect.setBottom(textRect.bottom() - metrics.textPadding.bottom());
}
return visualRect(opt->direction, opt->rect, textRect);
}
break;
#endif // QT_CONFIG(itemviews)
case SE_PushButtonFocusRect:
case SE_CheckBoxClickRect:
case SE_RadioButtonClickRect:
return opt->rect;
#if QT_CONFIG(progressbar)
case SE_ProgressBarGroove: {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::ProgressBar,
opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect rect = opt->rect.marginsRemoved(metrics.margins);
const auto *indicator = resolved.indicator();
if (!indicator || !indicator->visible() || indicator->opacity() == 0)
return rect;
const int w = qSaturateRound(resolvedWidth(
indicator,
rect.width() - metrics.padding.left() - metrics.padding.right()
- metrics.indicatorMargins.left() - metrics.indicatorMargins.right()));
const int h = qSaturateRound(resolvedHeight(
indicator,
rect.height() - metrics.padding.top() - metrics.padding.bottom()
- metrics.indicatorMargins.top() - metrics.indicatorMargins.bottom()));
const uint alignment = resolvedAlignment(indicator->alignment(), Qt::AlignLeft,
Qt::AlignVCenter);
const QRect indicatorRect = d->getAlignedRectInContainer(
rect, QSize(w, h), alignment, metrics.padding, metrics.indicatorMargins);
return visualRect(opt->direction, rect, indicatorRect);
}
case SE_ProgressBarContents: {
if (qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::ProgressBar,
opt->state);
if (!resolved.isValid())
break;
const auto *foreground = resolved.indicator()
? resolved.indicator()->foreground()
: nullptr;
if (!foreground || !foreground->visible() || foreground->opacity() == 0)
return QRect();
QRect indicatorRect = subElementRect(SE_ProgressBarGroove, opt, widget);
const int foregroundW = qSaturateRound(resolvedWidth(
foreground,
indicatorRect.width() - foreground->leftMargin() - foreground->rightMargin()));
const int foregroundH = qSaturateRound(resolvedHeight(
foreground,
indicatorRect.height() - foreground->topMargin() - foreground->bottomMargin()));
const uint foregroundAlign = resolvedAlignment(foreground->alignment(), Qt::AlignLeft, Qt::AlignVCenter);
const QMargins foregroundMargins = elementMargins(foreground);
const QRect foregroundRect = d->getAlignedRectInContainer(
indicatorRect, QSize(foregroundW, foregroundH), foregroundAlign,
QMargins(0, 0, 0, 0), foregroundMargins);
return visualRect(opt->direction, opt->rect, foregroundRect);
}
break;
}
#endif // QT_CONFIG(progressbar)
case SE_LineEditContents:
if (qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
#if QT_CONFIG(spinbox)
const bool isInSpinBox = widget && qobject_cast<const QSpinBox *>(widget->parentWidget());
#else
const bool isInSpinBox = false;
#endif
const auto controlType = isInSpinBox ? QQStyleKitReader::ControlType::SpinBox
: QQStyleKitReader::ControlType::TextField;
const auto resolved = d->resolveLayout(controlType, opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect contentsRect = opt->rect.marginsRemoved(metrics.padding);
const auto *textProps = resolved.text();
const uint textAlign = resolvedAlignment(textProps ? textProps->alignment() : 0u,
Qt::AlignLeft, Qt::AlignVCenter);
if (textAlign & Qt::AlignLeft)
contentsRect.setLeft(contentsRect.left() + metrics.textPadding.left());
else if (textAlign & Qt::AlignHCenter) {
contentsRect.setLeft(contentsRect.left() + metrics.textPadding.left() / 2);
contentsRect.setRight(contentsRect.right() - metrics.textPadding.right() / 2);
} else {
contentsRect.setRight(contentsRect.right() - metrics.textPadding.right());
}
if (textAlign & Qt::AlignTop)
contentsRect.setTop(contentsRect.top() + metrics.textPadding.top());
else if (textAlign & Qt::AlignVCenter) {
contentsRect.setTop(contentsRect.top() + metrics.textPadding.top() / 2);
contentsRect.setBottom(contentsRect.bottom() - metrics.textPadding.bottom() / 2);
} else {
contentsRect.setBottom(contentsRect.bottom() - metrics.textPadding.bottom());
}
return visualRect(opt->direction, opt->rect, contentsRect);
}
break;
case SE_ShapedFrameContents:
#if QT_CONFIG(combobox)
if (widget && widget->inherits("QComboBoxPrivateContainer")) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::Popup,
opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect contentsRect = opt->rect.marginsRemoved(
metrics.margins + metrics.padding + metrics.textPadding);
return visualRect(opt->direction, opt->rect, contentsRect);
}
#endif
#if QT_CONFIG(textedit)
if (qobject_cast<const QTextEdit *>(widget)) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::TextArea,
opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect contentsRect = opt->rect.marginsRemoved(
metrics.margins + metrics.padding + metrics.textPadding);
return visualRect(opt->direction, opt->rect, contentsRect);
}
#endif // textedit
break;
#if QT_CONFIG(tabbar)
case SE_TabBarTabText:
if (const auto *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::TabButton,
opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect rect = tab->rect.marginsRemoved(
metrics.margins + metrics.padding + metrics.textPadding);
if (!tab->icon.isNull())
rect.setLeft(rect.left() + tab->iconSize.width() + metrics.spacing);
return visualRect(tab->direction, tab->rect, rect);
}
break;
case SE_TabBarTearIndicatorRight:
case SE_TabBarTearIndicatorLeft:
case SE_TabBarScrollLeftButton:
case SE_TabBarScrollRightButton:
// Not supported
return QRect();
#endif // QT_CONFIG(tabbar)
#if QT_CONFIG(tabwidget)
case SE_TabWidgetTabContents:
if (qstyleoption_cast<const QStyleOptionTabWidgetFrame *>(opt)) {
// TabWidget corresponds to Page
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::Page, opt->state);
if (!resolved.isValid())
break;
const QRect paneRect = subElementRect(SE_TabWidgetTabPane, opt, widget);
const QRect rect = paneRect.marginsRemoved(resolved.metrics->padding);
return visualRect(opt->direction, opt->rect, rect);
}
break;
#endif // QT_CONFIG(tabwidget)
#if QT_CONFIG(toolbar)
case SE_ToolBarHandle:
// Not supported
return QRect();
#endif // QT_CONFIG(toolbar)
default:
break;
}
return QCommonStyle::subElementRect(r, opt, widget);
}
/*! \reimp */
void QStyleKitStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt,
QPainter *p, const QWidget *w) const
{
Q_D(const QStyleKitStyle);
switch (cc) {
#if QT_CONFIG(slider)
case CC_Slider:
if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::Slider, slider->state);
if (!r.isValid())
break;
const auto &metrics = *r.metrics;
QRect backgroundRect = opt->rect.marginsRemoved(metrics.margins);
// background
d->drawStyledItemRect(r.background(), backgroundRect, p);
// groove
if (slider->subControls & SC_SliderGroove) {
// groove
const auto grooveRect = proxy()->subControlRect(CC_Slider, opt, SC_SliderGroove, w);
const auto *indicator = r.indicator();
d->drawStyledItemRect(indicator, grooveRect, p);
// track
const auto *foreground = indicator ? indicator->foreground() : nullptr;
if (foreground && foreground->visible() && foreground->opacity() > 0) {
const bool isHorizontal = slider->orientation == Qt::Horizontal;
const qreal availableW = grooveRect.width()
- foreground->leftMargin() - foreground->rightMargin();
const qreal availableH = grooveRect.height()
- foreground->topMargin() - foreground->bottomMargin();
const qreal range = slider->maximum - slider->minimum;
const qreal ratio = range > 0
? (slider->sliderPosition - slider->minimum) / range
: 0;
const auto hAlign = foreground->alignment() & Qt::AlignHorizontal_Mask;
const auto vAlign = foreground->alignment() & Qt::AlignVertical_Mask;
const qreal fgW = resolvedWidth(foreground, availableW);
const qreal fgH = resolvedHeight(foreground, availableH);
const qreal fgX = hAlign & Qt::AlignRight
? grooveRect.left() + foreground->leftMargin() + availableW - fgW
: hAlign & Qt::AlignHCenter
? grooveRect.left() + foreground->leftMargin() + (availableW - fgW) / 2
: grooveRect.left() + foreground->leftMargin();
const qreal fgY = vAlign & Qt::AlignBottom
? grooveRect.top() + foreground->topMargin() + availableH - fgH
: vAlign & Qt::AlignVCenter
? grooveRect.top() + foreground->topMargin() + (availableH - fgH) / 2
: grooveRect.top() + foreground->topMargin();
const qreal minW = foreground->minimumWidth();
QRectF trackRect;
if (isHorizontal) {
const qreal trackW = foreground->fillWidth()
? minW + ratio * (fgW - minW)
: ratio * fgW;
trackRect = QRectF(fgX, fgY, trackW, fgH);
} else {
const qreal trackH = foreground->fillHeight()
? minW + ratio * (fgH - minW)
: ratio * fgH;
const qreal trackY = fgY
+ (1.0 - ratio) * (foreground->fillHeight() ? fgH - minW : fgH);
trackRect = QRectF(fgX, trackY, fgW, trackH);
}
// The track is the groove's foreground child, so it inherits the
// indicator's transform.
const QRect foregroundBox = visualRect(
opt->direction, grooveRect, QRectF(fgX, fgY, fgW, fgH).toAlignedRect());
QPainterStateGuard stateGuard(p);
if (applyDelegateTransform(p, indicator, grooveRect)
&& applyDelegateTransform(p, foreground, foregroundBox)) {
d->drawStyledItemContents(
foreground,
visualRect(opt->direction, grooveRect, trackRect.toAlignedRect()), p);
}
}
}
// handle
if (slider->subControls & SC_SliderHandle) {
QStyleOptionSlider handleOpt(*slider);
handleOpt.rect = subControlRect(CC_Slider, opt, SC_SliderHandle, w);
const auto *handle = r.handle();
if (handle && handle->visible() && handle->opacity() > 0)
d->drawStyledItemRect(handle, handleOpt.rect, p);
}
return;
}
break;
#endif // QT_CONFIG(slider)
#if QT_CONFIG(combobox)
case CC_ComboBox:
if (const QStyleOptionComboBox *combo =
qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
// background
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ComboBox, combo->state);
if (!r.isValid())
break;
QRect frameRect = subControlRect(CC_ComboBox, opt, SC_ComboBoxFrame, w);
d->drawStyledItemRect(r.background(), frameRect, p);
// indicator
if (combo->subControls & SC_ComboBoxArrow) {
QStyleOptionComboBox indicatorOpt(*combo);
indicatorOpt.rect = subControlRect(CC_ComboBox, opt, SC_ComboBoxArrow, w);
proxy()->drawPrimitive(PE_IndicatorArrowDown, &indicatorOpt, p, w);
}
// The editable combobox paints its own line edit using the QPalette::Text role
// for the text color, so update that color in the palette
if (auto *cb = qobject_cast<const QComboBox *>(w); cb && cb->isEditable()) {
if (const auto *txt = r.text(); txt && txt->isDefined(QQSK::Property::Color)) {
QPalette stylePalette;
stylePalette.setColor(QPalette::Text, txt->color());
d->setStylePalette(cb->lineEdit(), stylePalette);
}
}
return;
}
break;
#endif // QT_CONFIG(combobox)
#if QT_CONFIG(spinbox)
case CC_SpinBox:
if (const QStyleOptionSpinBox *spin = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::SpinBox, spin->state);
if (!r.isValid())
break;
QRect frameRect = opt->rect.marginsRemoved(r.metrics->margins);
// background
d->drawStyledItemRect(r.background(), frameRect, p);
// up/down buttons
if (spin->subControls & SC_SpinBoxUp) {
QStyleOptionSpinBox upOpt(*spin);
upOpt.rect = subControlRect(CC_SpinBox, opt, SC_SpinBoxUp, w);
proxy()->drawPrimitive(PE_IndicatorSpinUp, &upOpt, p, w);
}
if (spin->subControls & SC_SpinBoxDown) {
QStyleOptionSpinBox downOpt(*spin);
downOpt.rect = subControlRect(CC_SpinBox, opt, SC_SpinBoxDown, w);
proxy()->drawPrimitive(PE_IndicatorSpinDown, &downOpt, p, w);
}
// The spinbox line edit paints its text using the QPalette::Text role,
// so update that color in the palette
if (auto *sb = qobject_cast<const QSpinBox *>(w)) {
if (const auto *txt = r.text(); txt && txt->isDefined(QQSK::Property::Color)) {
QLineEdit *lineEdit = sb->findChild<QLineEdit *>();
if (lineEdit) {
QPalette p = lineEdit->palette();
if (p.color(QPalette::Text) != txt->color()) {
p.setColor(QPalette::Text, txt->color());
lineEdit->setPalette(p);
}
}
}
}
return;
}
break;
#endif // QT_CONFIG(spinbox)
#if QT_CONFIG(groupbox)
case CC_GroupBox:
if (const QStyleOptionGroupBox *groupBox =
qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::GroupBox, groupBox->state);
if (!r.isValid())
break;
// background/frame
if (groupBox->subControls & SC_GroupBoxFrame) {
QStyleOptionFrame frameOpt;
frameOpt.QStyleOption::operator=(*groupBox);
frameOpt.rect = subControlRect(CC_GroupBox, opt, SC_GroupBoxFrame, w);
drawPrimitive(PE_FrameGroupBox, &frameOpt, p, w);
}
// title
if (groupBox->subControls & SC_GroupBoxLabel && !groupBox->text.isEmpty()) {
const auto *textProps = r.text();
const QFont textFont = r.font();
QRect titleRect = subControlRect(CC_GroupBox, opt, SC_GroupBoxLabel, w);
d->drawControlText(textProps, textFont,
titleRect.marginsRemoved(r.metrics->textPadding), groupBox->text,
Qt::TextShowMnemonic, p, Qt::AlignLeft | Qt::AlignVCenter);
}
// don't draw checkmark as the Controls style doesn't draw it
// and StyleKit doesn't provide a way to style it
return;
}
break;
#endif // QT_CONFIG(groupbox)
#if QT_CONFIG(scrollbar)
case CC_ScrollBar:
if (const auto *scrollbar = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ScrollBar,
scrollbar->state);
if (!r.isValid())
break;
// background = groove
if (scrollbar->subControls & SC_ScrollBarGroove) {
QRect backgroundRect = proxy()->subControlRect(
CC_ScrollBar, opt, SC_ScrollBarGroove, w);
d->drawStyledItemRect(r.background(), backgroundRect, p);
}
// TODO: increase button
// TODO: decrease button
// slider = indicator
if (scrollbar->subControls & SC_ScrollBarSlider) {
QStyleOptionSlider newScrollbar(*scrollbar);
newScrollbar.rect = proxy()->subControlRect(
CC_ScrollBar, opt, SC_ScrollBarSlider, w);
proxy()->drawControl(CE_ScrollBarSlider, &newScrollbar, p, w);
}
return;
}
break;
#endif // QT_CONFIG(scrollbar)
#if QT_CONFIG(toolbutton)
case CC_ToolButton:
if (const QStyleOptionToolButton *tool =
qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
const auto r = d->resolve(w, QQStyleKitReader::ControlType::ToolButton, tool->state);
if (!r.isValid())
break;
// background
d->drawStyledItemRect(r.background(), opt->rect, p);
// menu arrow indicator
if ((tool->features
& (QStyleOptionToolButton::MenuButtonPopup | QStyleOptionToolButton::PopupDelay))
== QStyleOptionToolButton::MenuButtonPopup
&& (tool->subControls & SC_ToolButtonMenu)) {
const QRect menuRect = subControlRect(CC_ToolButton, opt, SC_ToolButtonMenu, w);
d->drawControlIndicator(r.indicator(), menuRect, p);
}
// label
State bflags = tool->state & ~State_Sunken;
if (bflags & State_AutoRaise) {
if (!(bflags & State_MouseOver) || !(bflags & State_Enabled))
bflags &= ~State_Raised;
}
if (tool->state & State_Sunken && tool->activeSubControls & SC_ToolButton)
bflags |= State_Sunken;
QStyleOptionToolButton label = *tool;
label.state = bflags;
label.rect = subControlRect(CC_ToolButton, opt, SC_ToolButton, w);
proxy()->drawControl(CE_ToolButtonLabel, &label, p, w);
return;
}
break;
#endif // QT_CONFIG(toolbutton)
default:
break;
}
QCommonStyle::drawComplexControl(cc, opt, p, w);
}
/*! \reimp */
QStyle::SubControl QStyleKitStyle::hitTestComplexControl(ComplexControl cc,
const QStyleOptionComplex *opt,
const QPoint &pt, const QWidget *w) const
{
return QCommonStyle::hitTestComplexControl(cc, opt, pt, w);
}
/*! \reimp */
QRect QStyleKitStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *opt,
SubControl sc, const QWidget *w) const
{
using QtPrivate::qSaturateRound;
Q_D(const QStyleKitStyle);
switch (cc) {
#if QT_CONFIG(slider)
case CC_Slider:
if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::Slider,
opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
const bool horizontal = slider->orientation == Qt::Horizontal;
QRect contentsRect = opt->rect
.marginsRemoved(metrics.margins)
.marginsRemoved(metrics.padding);
const auto *indicator = resolved.indicator();
if (!indicator)
return contentsRect;
switch (sc) {
case SC_SliderGroove: {
const auto availableW = contentsRect.width()
- indicator->leftMargin() - indicator->rightMargin();
const auto availableH = contentsRect.height()
- indicator->topMargin() - indicator->bottomMargin();
const qreal grooveW = resolvedWidth(indicator, availableW);
const qreal grooveH = resolvedHeight(indicator, availableH);
QRectF grooveRect(0, 0, grooveW, grooveH);
const uint rawHAlign = indicator->alignment() & Qt::AlignHorizontal_Mask;
const uint rawVAlign = indicator->alignment() & Qt::AlignVertical_Mask;
const uint hAlign = rawHAlign
? static_cast<Qt::Alignment>(rawHAlign)
: Qt::AlignLeft;
const uint vAlign = rawVAlign
? static_cast<Qt::Alignment>(rawVAlign)
: Qt::AlignVCenter;
if (hAlign & Qt::AlignLeft) {
grooveRect.moveLeft(contentsRect.x() + indicator->leftMargin());
} else if (hAlign & Qt::AlignHCenter) {
const qreal availableWidth = contentsRect.width()
- indicator->leftMargin() - indicator->rightMargin();
grooveRect.moveLeft(contentsRect.x() + indicator->leftMargin()
+ (availableWidth - grooveW) / 2.0);
} else {
grooveRect.moveLeft(contentsRect.x() + contentsRect.width()
- indicator->rightMargin() - grooveW);
}
if (vAlign & Qt::AlignTop) {
grooveRect.moveTop(contentsRect.y() + indicator->topMargin());
} else if (vAlign & Qt::AlignVCenter) {
const qreal availableHeight = contentsRect.height()
- indicator->topMargin() - indicator->bottomMargin();
grooveRect.moveTop(contentsRect.y()
+ indicator->topMargin()
+ (availableHeight - grooveH) / 2.0);
} else {
grooveRect.moveTop(contentsRect.y()
+ contentsRect.height()
- indicator->bottomMargin() - grooveH);
}
return visualRect(opt->direction, opt->rect, grooveRect.toAlignedRect());
}
case SC_SliderHandle: {
const auto *handle = resolved.handle();
if (!handle || !handle->visible() || handle->opacity() == 0)
return contentsRect;
const qreal handleW = resolvedWidth(
handle, contentsRect.width() - handle->leftMargin() - handle->rightMargin());
const qreal handleH = resolvedHeight(
handle, contentsRect.height() - handle->topMargin() - handle->bottomMargin());
QRectF handleRect(0, 0, handleW, handleH);
if (horizontal) {
const int range = qSaturateRound(contentsRect.width()
- handle->leftMargin() - handle->rightMargin() - handleW);
const int sliderPos = QStyle::sliderPositionFromValue(
slider->minimum, slider->maximum, slider->sliderPosition, range, false);
handleRect.moveLeft(contentsRect.x() + handle->leftMargin() + sliderPos);
handleRect.moveTop(contentsRect.y()
+ handle->topMargin() - handle->bottomMargin()
+ (contentsRect.height() - handleH) / 2.0);
} else {
const int range = qSaturateRound(contentsRect.height()
- handle->topMargin() - handle->bottomMargin() - handleH);
const int sliderPos = QStyle::sliderPositionFromValue(
slider->minimum, slider->maximum, slider->sliderPosition, range, true);
handleRect.moveLeft(contentsRect.x()
+ handle->leftMargin() - handle->rightMargin()
+ (contentsRect.width() - handleW) / 2.0);
handleRect.moveTop(contentsRect.y() + handle->topMargin() + sliderPos);
}
return visualRect(opt->direction, opt->rect, handleRect.toAlignedRect());
}
default:
break;
}
}
break;
#endif // QT_CONFIG(slider)
#if QT_CONFIG(combobox)
case CC_ComboBox:
if (const QStyleOptionComboBox *combo =
qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
const auto r = d->resolveLayout(QQStyleKitReader::ControlType::ComboBox, combo->state);
if (!r.isValid())
break;
const auto &metrics = *r.metrics;
QRect frameRect = opt->rect.marginsRemoved(metrics.margins);
switch (sc) {
case SC_ComboBoxFrame:
return visualRect(opt->direction, opt->rect, frameRect);
case SC_ComboBoxEditField: {
QRect contentsRect = frameRect.marginsRemoved(metrics.padding);
QRect indicatorRect = subControlRect(CC_ComboBox, opt, SC_ComboBoxArrow, w);
const int spacing = metrics.spacing;
const auto *indicator = r.indicator();
const uint indicatorAlign = indicator
? resolvedAlignment(indicator->alignment(), Qt::AlignRight, Qt::AlignVCenter)
: uint(Qt::AlignRight | Qt::AlignVCenter);
if (indicatorAlign & Qt::AlignLeft) {
contentsRect.setLeft(indicatorRect.right() + spacing);
} else if (indicatorAlign & Qt::AlignRight) {
contentsRect.setRight(indicatorRect.left() - spacing);
}
return visualRect(opt->direction, opt->rect, contentsRect);
}
case SC_ComboBoxArrow: {
QRect contentsRect = frameRect.marginsRemoved(metrics.padding);
const auto *indicator = r.indicator();
if (!indicator || !indicator->visible() || indicator->opacity() == 0)
return QRect();
const int w = qSaturateRound(resolvedWidth(
indicator,
contentsRect.width() - indicator->leftMargin() - indicator->rightMargin()));
const int h = qSaturateRound(resolvedHeight(
indicator,
contentsRect.height() - indicator->topMargin() - indicator->bottomMargin()));
const uint indicatorAlign = resolvedAlignment(
indicator->alignment(), Qt::AlignRight, Qt::AlignVCenter);
const QMargins indicatorMargins = elementMargins(indicator);
return visualRect(
opt->direction, opt->rect,
d->getAlignedRectInContainer(contentsRect, QSize(w, h),
indicatorAlign, QMargins(),
indicatorMargins));
}
case SC_ComboBoxListBoxPopup: {
QRect popupRect = opt->rect;
popupRect.setTop(opt->rect.bottom());
return visualRect(opt->direction, opt->rect, popupRect);
}
default:
break;
}
}
break;
#endif // QT_CONFIG(combobox)
#if QT_CONFIG(spinbox)
case CC_SpinBox:
if (const QStyleOptionSpinBox *spinBox =
qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) {
const auto r = d->resolveLayout(QQStyleKitReader::ControlType::SpinBox, spinBox->state);
if (!r.isValid())
break;
const auto &metrics = *r.metrics;
QRect frameRect = opt->rect.marginsRemoved(metrics.margins);
QRect contentsRect = frameRect.marginsRemoved(metrics.padding);
switch (sc) {
case SC_SpinBoxFrame:
return visualRect(opt->direction, opt->rect, frameRect);
case SC_SpinBoxEditField: {
QRect upIndicatorRect = subControlRect(CC_SpinBox, opt, SC_SpinBoxUp, w);
QRect downIndicatorRect = subControlRect(CC_SpinBox, opt, SC_SpinBoxDown, w);
const int spacing = metrics.spacing;
const auto *upIndicator = r.indicator() ? r.indicator()->first() : nullptr;
const auto *downIndicator = r.indicator() ? r.indicator()->second() : nullptr;
const bool hasUpIndicator = upIndicator
&& upIndicator->visible() && upIndicator->opacity() > 0;
const bool hasDownIndicator = downIndicator
&& downIndicator->visible() && downIndicator->opacity() > 0;
const uint upAlign = hasUpIndicator
? resolvedAlignment(upIndicator->alignment(), Qt::AlignLeft, Qt::AlignVCenter)
: uint(Qt::AlignLeft | Qt::AlignVCenter);
const uint downAlign = hasDownIndicator
? resolvedAlignment(downIndicator->alignment(), Qt::AlignLeft, Qt::AlignVCenter)
: uint(Qt::AlignLeft | Qt::AlignVCenter);
if (hasUpIndicator && hasDownIndicator) {
if ((upAlign & Qt::AlignLeft) && (downAlign & Qt::AlignRight)) {
contentsRect.setLeft(upIndicatorRect.right() + spacing);
contentsRect.setRight(downIndicatorRect.left() - spacing);
} else if ((upAlign & Qt::AlignRight) && (downAlign & Qt::AlignLeft)) {
contentsRect.setRight(upIndicatorRect.left() - spacing);
contentsRect.setLeft(downIndicatorRect.right() + spacing);
}
} else if (hasUpIndicator) {
if (upAlign & Qt::AlignLeft) {
contentsRect.setLeft(upIndicatorRect.right() + spacing);
} else if (upAlign & Qt::AlignRight) {
contentsRect.setRight(upIndicatorRect.left() - spacing);
}
} else if (hasDownIndicator) {
if (downAlign & Qt::AlignLeft) {
contentsRect.setLeft(downIndicatorRect.right() + spacing);
} else if (downAlign & Qt::AlignRight) {
contentsRect.setRight(downIndicatorRect.left() - spacing);
}
}
return visualRect(opt->direction, opt->rect, contentsRect);
}
case SC_SpinBoxUp: {
const auto *upIndicator = r.indicator() ? r.indicator()->first() : nullptr;
if (!upIndicator || !upIndicator->visible() || upIndicator->opacity() == 0)
return contentsRect;
const int w = qSaturateRound(resolvedWidth(
upIndicator,
contentsRect.width() - upIndicator->leftMargin() - upIndicator->rightMargin()));
const int h = qSaturateRound(resolvedHeight(
upIndicator,
contentsRect.height() - upIndicator->topMargin() - upIndicator->bottomMargin()));
const uint upAlign = resolvedAlignment(
upIndicator->alignment(), Qt::AlignLeft, Qt::AlignVCenter);
const QMargins upMargins = elementMargins(upIndicator);
return visualRect(opt->direction, opt->rect,
d->getAlignedRectInContainer(contentsRect, QSize(w, h), upAlign,
QMargins(), upMargins));
}
case SC_SpinBoxDown: {
const auto *downIndicator = r.indicator() ? r.indicator()->second() : nullptr;
if (!downIndicator || !downIndicator->visible() || downIndicator->opacity() == 0)
return frameRect;
const int w = qSaturateRound(resolvedWidth(
downIndicator,
contentsRect.width()
- downIndicator->leftMargin() - downIndicator->rightMargin()));
const int h = qSaturateRound(resolvedHeight(
downIndicator,
contentsRect.height()
- downIndicator->topMargin() - downIndicator->bottomMargin()));
const uint downAlign = resolvedAlignment(downIndicator->alignment(), Qt::AlignLeft,
Qt::AlignVCenter);
const QMargins downMargins = elementMargins(downIndicator);
return visualRect(opt->direction, opt->rect,
d->getAlignedRectInContainer(contentsRect, QSize(w, h), downAlign,
QMargins(), downMargins));
}
default:
break;
}
}
break;
#endif // QT_CONFIG(spinbox)
#if QT_CONFIG(groupbox)
case CC_GroupBox:
if (const QStyleOptionGroupBox *groupBox =
qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
const auto r = d->resolveLayout(QQStyleKitReader::ControlType::GroupBox,
groupBox->state);
if (!r.isValid())
break;
const auto &metrics = *r.metrics;
QRect frameRect = opt->rect.marginsRemoved(metrics.margins);
switch (sc) {
case SC_GroupBoxFrame:
return visualRect(opt->direction, opt->rect, frameRect);
case SC_GroupBoxLabel: {
const int fontHeight = opt->fontMetrics.height();
const int labelHeight = metrics.textPadding.top()
+ fontHeight + metrics.textPadding.bottom();
const QRect labelContainer(opt->rect.left(), opt->rect.top(), opt->rect.width(),
labelHeight);
return visualRect(opt->direction, opt->rect, labelContainer);
}
case SC_GroupBoxCheckBox: {
// The controls style doesn't draw the checkbox, so we don't return a rect for it
return QRect();
}
case SC_GroupBoxContents: {
const auto labelHeight = subControlRect(CC_GroupBox, opt, SC_GroupBoxLabel, w)
.height();
const auto offset = metrics.padding.top()
+ qMax(labelHeight, metrics.margins.top() + metrics.spacing);
QRect contentsRect(
opt->rect.left() + metrics.padding.left(), opt->rect.top() + offset,
opt->rect.width() - metrics.padding.left() - metrics.padding.right(),
opt->rect.bottom() - opt->rect.top() - offset - metrics.padding.bottom());
return visualRect(opt->direction, opt->rect, contentsRect);
}
default:
break;
}
}
break;
#endif // QT_CONFIG(groupbox)
#if QT_CONFIG(scrollbar)
case CC_ScrollBar:
if (const auto *scrollbar = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::ScrollBar,
opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
const bool horizontal = scrollbar->orientation == Qt::Horizontal;
const QRectF grooveRect = opt->rect.marginsRemoved(metrics.margins);
switch (sc) {
// TODO: support up/down buttons
case SC_ScrollBarAddLine:
case SC_ScrollBarSubLine:
case SC_ScrollBarSubPage:
case SC_ScrollBarAddPage:
return QRect();
case SC_ScrollBarGroove:
return visualRect(opt->direction, opt->rect, grooveRect.toAlignedRect());
case SC_ScrollBarSlider: {
const auto *indicator = resolved.indicator();
if (!indicator || !indicator->visible() || indicator->opacity() == 0)
return QRect();
const QRectF contentsRect = grooveRect.marginsRemoved(metrics.padding);
const qreal availableW = contentsRect.width()
- indicator->leftMargin() - indicator->rightMargin();
const qreal availableH = contentsRect.height()
- indicator->topMargin() - indicator->bottomMargin();
const int totalRange = scrollbar->maximum
- scrollbar->minimum + scrollbar->pageStep;
const qreal ratio = totalRange > 0 ? qreal(scrollbar->pageStep) / totalRange : 1.0;
QRectF sliderRect;
if (horizontal) {
const qreal sliderW = qMax(
qreal(metrics.indicatorImplicitSize.width()), availableW * ratio);
const qreal sliderH = resolvedHeight(indicator, availableH);
const qreal travelRange = qMax(0.0, availableW - sliderW);
const int sliderPos = QStyle::sliderPositionFromValue(
scrollbar->minimum, scrollbar->maximum, scrollbar->sliderPosition,
int(travelRange), scrollbar->upsideDown);
sliderRect = QRectF(
contentsRect.x() + indicator->leftMargin() + sliderPos,
contentsRect.y() + indicator->topMargin() + (availableH - sliderH) / 2.0,
sliderW, sliderH);
} else {
const qreal sliderH = qMax(
qreal(metrics.indicatorImplicitSize.width()), availableH * ratio);
const qreal sliderW = resolvedWidth(indicator, availableW);
const qreal travelRange = qMax(0.0, availableH - sliderH);
const int sliderPos = QStyle::sliderPositionFromValue(
scrollbar->minimum, scrollbar->maximum, scrollbar->sliderPosition,
int(travelRange), scrollbar->upsideDown);
sliderRect = QRectF(
contentsRect.x() + indicator->leftMargin() + (availableW - sliderW) / 2.0,
contentsRect.y() + indicator->topMargin() + sliderPos, sliderW, sliderH);
}
return visualRect(opt->direction, opt->rect, sliderRect.toAlignedRect());
}
default:
break;
}
}
break;
#endif // QT_CONFIG(scrollbar)
#if QT_CONFIG(toolbutton)
case CC_ToolButton:
if (const auto *tool = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::ToolButton,
opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QRect frameRect = opt->rect.marginsRemoved(metrics.margins);
switch (sc) {
case SC_ToolButton: {
QRect contentsRect = frameRect.marginsRemoved(metrics.padding);
const bool hasMenuButton = (tool->features
& (QStyleOptionToolButton::MenuButtonPopup | QStyleOptionToolButton::HasMenu))
!= 0;
const auto *indicator = resolved.indicator();
if (hasMenuButton && indicator && indicator->visible()
&& indicator->opacity() > 0) {
const QRect menuRect = subControlRect(CC_ToolButton, opt, SC_ToolButtonMenu, w);
const int spacing = metrics.spacing;
const uint indicatorAlign = resolvedAlignment(indicator->alignment(),
Qt::AlignRight, Qt::AlignVCenter);
if (indicatorAlign & Qt::AlignLeft)
contentsRect.setLeft(menuRect.right() + spacing);
else if (indicatorAlign & Qt::AlignRight)
contentsRect.setRight(menuRect.left() - spacing);
}
return visualRect(opt->direction, opt->rect, contentsRect);
}
case SC_ToolButtonMenu: {
QRect contentsRect = frameRect.marginsRemoved(metrics.padding);
if ((tool->features
& (QStyleOptionToolButton::MenuButtonPopup
| QStyleOptionToolButton::PopupDelay))
!= QStyleOptionToolButton::MenuButtonPopup) {
break;
}
const auto *indicator = resolved.indicator();
if (!indicator || !indicator->visible() || indicator->opacity() == 0)
return QRect();
const int w = qSaturateRound(resolvedWidth(
indicator,
contentsRect.width() - indicator->leftMargin() - indicator->rightMargin()));
const int h = qSaturateRound(resolvedHeight(
indicator,
contentsRect.height() - indicator->topMargin() - indicator->bottomMargin()));
const uint indicatorAlign = resolvedAlignment(indicator->alignment(),
Qt::AlignRight, Qt::AlignVCenter);
const QMargins indicatorMargins = elementMargins(indicator);
return visualRect(opt->direction, opt->rect,
d->getAlignedRectInContainer(contentsRect, QSize(w, h), indicatorAlign, QMargins(), indicatorMargins));
}
default:
break;
}
}
break;
#endif // QT_CONFIG(toolbutton)
default:
break;
}
return QCommonStyle::subControlRect(cc, opt, sc, w);
}
/*! \reimp */
QSize QStyleKitStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt,
const QSize &contentsSize, const QWidget *widget) const
{
Q_D(const QStyleKitStyle);
switch (ct) {
#if QT_CONFIG(pushbutton)
case CT_PushButton:
#endif
#if QT_CONFIG(toolbutton)
case CT_ToolButton: {
auto controlType = QQStyleKitReader::ControlType::ToolButton;
if (const auto *buttonOpt = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
controlType = (buttonOpt->features & QStyleOptionButton::Flat)
? QQStyleKitReader::ControlType::FlatButton
: QQStyleKitReader::ControlType::Button;
}
const auto resolved = d->resolveLayout(controlType, opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
const QSize contentSize = contentsSize
+ QSize(metrics.padding.left() + metrics.padding.right(),
metrics.padding.top() + metrics.padding.bottom())
+ QSize(metrics.textPadding.left() + metrics.textPadding.right(),
metrics.textPadding.top() + metrics.textPadding.bottom());
const QSize bgSize = metrics.bgImplicitSize
+ QSize(metrics.margins.left() + metrics.margins.right(),
metrics.margins.top() + metrics.margins.bottom());
return contentSize.expandedTo(bgSize);
}
#endif
case CT_CheckBox:
case CT_RadioButton:
if (const auto *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
const auto controlType = ct == CT_CheckBox ? QQStyleKitReader::ControlType::CheckBox
: QQStyleKitReader::ControlType::RadioButton;
const auto resolved = d->resolveLayout(controlType, opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
const QSize textSize = opt->fontMetrics.size(Qt::TextShowMnemonic, btn->text);
const int bgWidth = metrics.bgImplicitSize.width()
+ metrics.margins.left() + metrics.margins.right();
const int bgHeight = metrics.bgImplicitSize.height()
+ metrics.margins.top() + metrics.margins.bottom();
const int contentWidth = std::max(textSize.width(), contentsSize.width())
+ metrics.padding.left() + metrics.padding.right() + metrics.textPadding.left()
+ metrics.textPadding.right();
const int contentHeight = std::max(textSize.height(), contentsSize.height())
+ metrics.padding.top() + metrics.padding.bottom() + metrics.textPadding.top()
+ metrics.textPadding.bottom();
const int indicatorWidth = metrics.indicatorImplicitSize.width()
+ metrics.indicatorMargins.left() + metrics.indicatorMargins.right();
const int indicatorHeight = metrics.indicatorImplicitSize.height()
+ metrics.indicatorMargins.top() + metrics.indicatorMargins.bottom();
return QSize(std::max({ contentWidth + indicatorWidth + metrics.spacing, bgWidth }),
std::max({ contentHeight, indicatorHeight, bgHeight }));
}
break;
#if QT_CONFIG(itemviews)
case CT_ItemViewItem:
if (const auto *item = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
const auto resolved = d->resolveLayout(itemViewControlType(opt), opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
const QSize textSize = opt->fontMetrics.size(Qt::TextShowMnemonic, item->text);
const int bgWidth = metrics.bgImplicitSize.width()
+ metrics.margins.left() + metrics.margins.right();
const int bgHeight = metrics.bgImplicitSize.height()
+ metrics.margins.top() + metrics.margins.bottom();
const int contentWidth = std::max(textSize.width(), contentsSize.width())
+ metrics.padding.left() + metrics.padding.right() + metrics.textPadding.left()
+ metrics.textPadding.right();
const int contentHeight = std::max(textSize.height(), contentsSize.height())
+ metrics.padding.top() + metrics.padding.bottom() + metrics.textPadding.top()
+ metrics.textPadding.bottom();
const int indicatorWidth = metrics.indicatorImplicitSize.width()
+ metrics.indicatorMargins.left() + metrics.indicatorMargins.right();
const int indicatorHeight = metrics.indicatorImplicitSize.height()
+ metrics.indicatorMargins.top() + metrics.indicatorMargins.bottom();
return QSize(std::max({ contentWidth + indicatorWidth + metrics.spacing, bgWidth }),
std::max({ contentHeight, indicatorHeight, bgHeight }));
}
break;
#endif // QT_CONFIG(itemviews)
case CT_ProgressBar:
if (qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::ProgressBar,
opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
const auto indicatorW = metrics.indicatorImplicitSize.width()
+ metrics.indicatorMargins.left()
+ metrics.indicatorMargins.right();
const auto indicatorH = metrics.indicatorImplicitSize.height()
+ metrics.indicatorMargins.top()
+ metrics.indicatorMargins.bottom();
const int bgW = metrics.bgImplicitSize.width()
+ metrics.margins.left() + metrics.margins.right();
const int bgH = metrics.bgImplicitSize.height()
+ metrics.margins.top() + metrics.margins.bottom();
// For progress bar in Controls, the content size is based on the indicator size
const int contentW = indicatorW + metrics.padding.left() + metrics.padding.right();
const int contentH = indicatorH + metrics.padding.top() + metrics.padding.bottom();
return QSize(std::max(contentW, bgW), std::max(contentH, bgH));
}
break;
case CT_Slider:
if (qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::Slider,
opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
// background = indicator = groove + track
const int bgW = std::max(
metrics.padding.left() + metrics.padding.right()
+ metrics.indicatorImplicitSize.width()
+ metrics.indicatorMargins.left() + metrics.indicatorMargins.right(),
metrics.bgImplicitSize.width() + metrics.margins.left() + metrics.margins.right());
const int bgH = std::max(
metrics.padding.top() + metrics.padding.bottom()
+ metrics.indicatorImplicitSize.height()
+ metrics.indicatorMargins.top() + metrics.indicatorMargins.bottom(),
metrics.bgImplicitSize.height() + metrics.margins.top() + metrics.margins.bottom());
const int handleW = metrics.handleImplicitSize.width()
+ metrics.padding.left()
+ metrics.padding.right();
const int handleH = metrics.handleImplicitSize.height()
+ metrics.padding.top()
+ metrics.padding.bottom();
return QSize(std::max(handleW, bgW), std::max(handleH, bgH));
}
break;
#if QT_CONFIG(lineedit)
case CT_LineEdit:
if (const auto *lineEdit = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
QStyleOption lineEditOpt(*lineEdit);
lineEditOpt.state &= ~QStyle::State_Sunken;
# if QT_CONFIG(spinbox)
const bool isInSpinBox = widget && qobject_cast<const QSpinBox *>(widget->parent());
# else
const bool isInSpinBox = false;
# endif
auto controlType = isInSpinBox ? QQStyleKitReader::ControlType::SpinBox
: QQStyleKitReader::ControlType::TextField;
const auto resolved = d->resolveLayout(controlType, lineEditOpt.state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
QSize bgSize(0, 0);
// For spinbox, the line edit doesn't have its own background in the Controls style
if (!isInSpinBox)
bgSize = metrics.bgImplicitSize.grownBy(metrics.margins);
const QSize contentSizeWithPadding = contentsSize
.grownBy(metrics.textPadding)
.grownBy(metrics.padding);
return contentSizeWithPadding.expandedTo(bgSize);
}
break;
#endif // QT_CONFIG(lineedit)
#if QT_CONFIG(combobox)
case CT_ComboBox:
if (const auto *comboBox = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::ComboBox,
opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
const QSize textSize = opt->fontMetrics.size(
Qt::TextShowMnemonic, comboBox->currentText);
const int bgW = metrics.bgImplicitSize.width()
+ metrics.margins.left() + metrics.margins.right();
const int bgH = metrics.bgImplicitSize.height()
+ metrics.margins.top() + metrics.margins.bottom();
const int contentW = textSize.width()
+ metrics.padding.left() + metrics.padding.right()
+ metrics.textPadding.left() + metrics.textPadding.right();
const int contentH = textSize.height()
+ metrics.padding.top() + metrics.padding.bottom()
+ metrics.textPadding.top() + metrics.textPadding.bottom();
const int indicatorW = metrics.indicatorImplicitSize.width()
+ metrics.indicatorMargins.left() + metrics.indicatorMargins.right();
const int indicatorH = metrics.indicatorImplicitSize.height()
+ metrics.indicatorMargins.top() + metrics.indicatorMargins.bottom();
return QSize(std::max({ contentW + indicatorW + metrics.spacing, bgW }),
std::max({ contentH, indicatorH, bgH }));
}
break;
#endif // QT_CONFIG(combobox)
#if QT_CONFIG(spinbox)
case CT_SpinBox: {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::SpinBox, opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
const int bgW = metrics.bgImplicitSize.width()
+ metrics.margins.left() + metrics.margins.right();
const int bgH = metrics.bgImplicitSize.height()
+ metrics.margins.top() + metrics.margins.bottom();
const int contentW = contentsSize.width()
+ metrics.padding.left() + metrics.padding.right()
+ metrics.textPadding.left() + metrics.textPadding.right();
const int contentH = contentsSize.height()
+ metrics.padding.top() + metrics.padding.bottom()
+ metrics.textPadding.top() + metrics.textPadding.bottom();
// TODO: Support vertical layout for spinbox, currently we assume horizontal layout
// TODO: Calculate each indicator (up/down) size separately if they have different sizes in
// the style
const int indicatorW = (metrics.indicatorImplicitSize.width()
+ metrics.indicatorMargins.left() + metrics.indicatorMargins.right()) * 2;
const int indicatorH = metrics.indicatorImplicitSize.height()
+ metrics.indicatorMargins.top() + metrics.indicatorMargins.bottom();
return QSize(std::max({ contentW + indicatorW + metrics.spacing, bgW }),
std::max({ contentH, indicatorH, bgH }));
}
#endif // QT_CONFIG(spinbox)
#if QT_CONFIG(tabbar)
case CT_TabBarTab:
if (qstyleoption_cast<const QStyleOptionTab *>(opt)) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::TabButton,
opt->state);
if (!resolved.isValid())
break;
const auto &m = *resolved.metrics;
const auto bgSize = m.bgImplicitSize.grownBy(m.margins);
const auto contentSizeWithPadding = contentsSize.grownBy(m.padding + m.textPadding);
return contentSizeWithPadding.expandedTo(bgSize);
}
break;
#endif // QT_CONFIG(tabbar)
#if QT_CONFIG(tabwidget)
case CT_TabWidget:
if (qstyleoption_cast<const QStyleOptionTabWidgetFrame *>(opt)) {
// TabWidget corresponds to Page
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::Page, opt->state);
if (!resolved.isValid())
break;
const auto &m = *resolved.metrics;
const auto bgSize = m.bgImplicitSize.grownBy(m.margins);
const auto contentSizeWithPadding = contentsSize.grownBy(m.padding);
return contentSizeWithPadding.expandedTo(bgSize);
}
break;
#endif // QT_CONFIG(tabwidget)
#if QT_CONFIG(groupbox)
case CT_GroupBox:
if (const auto *groupBox = qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::GroupBox,
opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
const QSize bgSize = metrics.bgImplicitSize
+ QSize(metrics.margins.left() + metrics.margins.right(),
metrics.margins.top() + metrics.margins.bottom());
const QSize contentSize = contentsSize.grownBy(metrics.padding);
const QSize textSize = opt->fontMetrics
.size(Qt::TextShowMnemonic, groupBox->text)
.grownBy(metrics.textPadding);
return textSize.expandedTo(contentSize).expandedTo(bgSize);
}
break;
#endif // QT_CONFIG(groupbox)
#if QT_CONFIG(scrollbar)
case CT_ScrollBar:
if (qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::ScrollBar, opt->state);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
const int iH = metrics.indicatorImplicitSize.height()
+ metrics.padding.top() + metrics.padding.bottom()
+ metrics.indicatorMargins.top() + metrics.indicatorMargins.bottom();
const int iW = metrics.indicatorImplicitSize.width()
+ metrics.padding.left() + metrics.padding.right()
+ metrics.indicatorMargins.left() + metrics.indicatorMargins.right();
const int bgH = metrics.bgImplicitSize.height()
+ metrics.margins.top() + metrics.margins.bottom();
const int bgW = metrics.bgImplicitSize.width()
+ metrics.margins.left() + metrics.margins.right();
return QSize(std::max(iW, bgW), std::max(iH, bgH));
}
break;
#endif // QT_CONFIG(scrollbar)
#if QT_CONFIG(menu)
case CT_MenuItem:
if (const auto *menuItem = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
// The menu dictates the item width
// QMenu uses for PM_MenuHMargin for both left/right paddings,
// so the available width is bgImplicitWidth + margins - 2 * padding.left.
const auto menuType = widget && widget->inherits("QComboBoxPrivateContainer")
? QQStyleKitReader::ControlType::Popup
: QQStyleKitReader::ControlType::Menu;
const auto menuResolved = d->resolveLayout(menuType, opt->state);
int minMenuW = 0;
if (menuResolved.isValid()) {
const auto &mm = *menuResolved.metrics;
minMenuW = mm.bgImplicitSize.width()
+ mm.margins.left() + mm.margins.right() - 2 * mm.padding.left();
}
if (menuItem->menuItemType == QStyleOptionMenuItem::Separator) {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::MenuSeparator,
opt->state);
if (!resolved.isValid())
break;
const auto &m = *resolved.metrics;
const int bgWidth = m.bgImplicitSize.width()
+ m.margins.left() + m.margins.right();
const int bgHeight = m.bgImplicitSize.height()
+ m.margins.top() + m.margins.bottom();
return QSize(std::max(bgWidth, minMenuW), bgHeight);
}
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::MenuItem, opt->state);
if (!resolved.isValid())
break;
const auto &m = *resolved.metrics;
const auto *indicator = resolved.indicator();
const auto *first = indicator ? indicator->first() : nullptr;
const auto *second = indicator ? indicator->second() : nullptr;
int indicatorW = 0;
int indicatorH = 0;
using QtPrivate::qSaturateRound;
if (first && menuItem->checkType != QStyleOptionMenuItem::NotCheckable) {
indicatorW += qSaturateRound(
first->leftMargin() + first->width() + first->rightMargin()) + m.spacing;
indicatorH = std::max(
qSaturateRound(first->topMargin() + first->bottomMargin() + first->height()),
indicatorH);
}
if (second && menuItem->menuItemType == QStyleOptionMenuItem::SubMenu) {
indicatorW += qSaturateRound(
second->leftMargin() + second->width() + second->rightMargin()) + m.spacing;
indicatorH = std::max(
qSaturateRound(second->topMargin() + second->bottomMargin() + second->height()),
indicatorH);
}
const int bgH = m.bgImplicitSize.height() + m.margins.top() + m.margins.bottom();
const int iconReserved = menuItem->maxIconWidth > 0
? menuItem->maxIconWidth + m.spacing
: 0;
const int contentW = contentsSize.width()
+ m.padding.left() + m.padding.right()
+ m.textPadding.left() + m.textPadding.right()
+ indicatorW + iconReserved;
const int contentH = contentsSize.height()
+ m.padding.top() + m.padding.bottom()
+ m.textPadding.top() + m.textPadding.bottom();
return QSize(std::max(contentW, minMenuW), std::max({ contentH, indicatorH, bgH }));
}
break;
case CT_Menu: {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::Menu, opt->state);
if (!resolved.isValid())
break;
const auto &m = *resolved.metrics;
const int height = std::max(contentsSize.height(), m.bgImplicitSize.height())
+ m.margins.top() + m.margins.bottom();
return QSize(contentsSize.width(), height);
}
#endif // QT_CONFIG(menu)
#if QT_CONFIG(menubar)
case CT_MenuBar: {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::MenuBar, opt->state);
if (!resolved.isValid())
break;
const auto &m = *resolved.metrics;
const int bgWidth = m.bgImplicitSize.width() + m.margins.left() + m.margins.right();
const int bgHeight = m.bgImplicitSize.height() + m.margins.top() + m.margins.bottom();
const int contentW = contentsSize.width() + m.padding.left() + m.padding.right();
const int contentH = contentsSize.height() + m.padding.top() + m.padding.bottom();
return QSize(std::max(contentW, bgWidth), std::max(contentH, bgHeight));
}
case CT_MenuBarItem: {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::MenuBarItem,
opt->state);
if (!resolved.isValid())
break;
const auto &m = *resolved.metrics;
const int bgWidth = m.bgImplicitSize.width() + m.margins.left() + m.margins.right();
const int bgHeight = m.bgImplicitSize.height() + m.margins.top() + m.margins.bottom();
const int contentW = contentsSize.width()
+ m.padding.left() + m.padding.right()
+ m.textPadding.left() + m.textPadding.right();
const int contentH = contentsSize.height()
+ m.padding.top() + m.padding.bottom()
+ m.textPadding.top() + m.textPadding.bottom();
return QSize(std::max(contentW, bgWidth), std::max(contentH, bgHeight));
}
#endif // QT_CONFIG(menubar)
default:
break;
}
return QCommonStyle::sizeFromContents(ct, opt, contentsSize, widget);
}
/*! \reimp */
int QStyleKitStyle::pixelMetric(PixelMetric m, const QStyleOption *opt, const QWidget *widget) const
{
Q_D(const QStyleKitStyle);
switch (m) {
#if QT_CONFIG(menu)
case PM_MenuPanelWidth:
// The panel border is drawn as part of the resolved background (see PE_PanelMenu);
return 0;
case PM_MenuTearoffHeight:
case PM_MenuScrollerHeight:
return 0;
case PM_MenuHMargin:
case PM_MenuVMargin: {
const auto controlType = widget && widget->inherits("QComboBoxPrivateContainer")
? QQStyleKitReader::ControlType::Popup
: QQStyleKitReader::ControlType::Menu;
const auto resolved = d->resolveLayout(controlType, opt ? opt->state : QStyle::State_None);
if (!resolved.isValid())
break;
return m == PM_MenuHMargin ? resolved.metrics->padding.left()
: resolved.metrics->padding.top();
}
#endif
#if QT_CONFIG(menubar)
case PM_MenuBarItemSpacing: {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::MenuBarItem,
opt ? opt->state : QStyle::State_None);
if (resolved.isValid())
return resolved.metrics->spacing;
break;
}
#endif
#if QT_CONFIG(toolbar)
case PM_ToolBarFrameWidth:
// The frame is folded into the resolved background/margins (see CE_ToolBar);
return 0;
case PM_ToolBarItemMargin: {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::ToolBar,
opt ? opt->state : QStyle::State_None);
if (!resolved.isValid())
break;
return resolved.metrics->padding.left();
}
case PM_ToolBarItemSpacing: {
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::ToolBar,
opt ? opt->state : QStyle::State_None);
if (resolved.isValid())
return resolved.metrics->spacing;
break;
}
case PM_ToolBarHandleExtent: {
// The only lever available to honor the style's background width/height
// QToolBarLayout uses this to calculate the minimum size of the toolbar
const auto resolved = d->resolveLayout(QQStyleKitReader::ControlType::ToolBar,
opt ? opt->state : QStyle::State_None);
if (!resolved.isValid())
break;
const auto &metrics = *resolved.metrics;
const bool horizontal = !opt || (opt->state & State_Horizontal);
const int crossImplicit = horizontal
? metrics.bgImplicitSize.height()
: metrics.bgImplicitSize.width();
const int crossMargins = horizontal
? metrics.margins.top() + metrics.margins.bottom()
: metrics.margins.left() + metrics.margins.right();
const int crossPadding = horizontal
? metrics.padding.top() + metrics.padding.bottom()
: metrics.padding.left() + metrics.padding.right();
return qMax(0, crossImplicit + crossMargins - crossPadding);
}
#endif // QT_CONFIG(toolbar)
#if QT_CONFIG(tabbar)
case PM_TabBarTabHSpace:
case PM_TabBarTabVSpace:
case PM_TabBarTabOverlap:
// Not supported in StyleKit
case PM_TabCloseIndicatorWidth:
case PM_TabCloseIndicatorHeight:
return 0;
#endif // QT_CONFIG(tabbar)
default:
break;
}
return QCommonStyle::pixelMetric(m, opt, widget);
}
/*! \reimp */
int QStyleKitStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget *w,
QStyleHintReturn *shret) const
{
switch (sh) {
// keep consistent with the Controls style behavior
case SH_SpinBox_SelectOnStep:
case SH_ToolBar_Movable:
case SH_TabBar_PreferNoArrows:
return 0;
case SH_Menu_MouseTracking:
case SH_MenuBar_MouseTracking:
case SH_Menu_Scrollable:
return 1;
default:
break;
}
return QCommonStyle::styleHint(sh, opt, w, shret);
}
/*! \reimp */
QPalette QStyleKitStyle::standardPalette() const
{
return QCommonStyle::standardPalette();
}
/*! \reimp */
void QStyleKitStyle::polish(QWidget *widget)
{
if (!widget)
return;
Q_D(QStyleKitStyle);
// When no user style is loaded, create the empty fallback style now,
// before any reader is created, so that all widgets are styled consistently
if (!d->style)
d->ensureDefaultStyle();
widget->setAttribute(Qt::WA_Hover);
#if QT_CONFIG(scrollbar)
// QScrollBar sets WA_OpaquePaintEvent in its constructor, which skips
// background erase before paint. This can leave artifacts when the
// style's background is invisible, so disable it
if (qobject_cast<QScrollBar *>(widget))
widget->setAttribute(Qt::WA_OpaquePaintEvent, false);
#endif
// Create per-widget reader for interactive controls (transitions)
const bool isInteractiveControl = false
#if QT_CONFIG(pushbutton)
|| qobject_cast<const QPushButton *>(widget)
#endif
#if QT_CONFIG(checkbox)
|| qobject_cast<const QCheckBox *>(widget)
#endif
#if QT_CONFIG(radiobutton)
|| qobject_cast<const QRadioButton *>(widget)
#endif
#if QT_CONFIG(combobox)
|| qobject_cast<const QComboBox *>(widget)
|| (widget && widget->inherits("QComboBoxPrivateContainer"))
#endif
#if QT_CONFIG(lineedit)
|| qobject_cast<const QLineEdit *>(widget)
#endif
#if QT_CONFIG(textedit)
|| qobject_cast<const QTextEdit *>(widget)
|| qobject_cast<const QPlainTextEdit *>(widget)
#endif
#if QT_CONFIG(label)
|| qobject_cast<const QLabel *>(widget)
#endif
#if QT_CONFIG(progressbar)
|| qobject_cast<const QProgressBar *>(widget)
#endif
#if QT_CONFIG(slider)
|| qobject_cast<const QSlider *>(widget)
#endif
#if QT_CONFIG(scrollbar)
|| qobject_cast<const QScrollBar *>(widget)
#endif
#if QT_CONFIG(spinbox)
|| qobject_cast<const QSpinBox *>(widget)
#endif
#if QT_CONFIG(tabbar)
|| qobject_cast<const QTabBar *>(widget)
#endif
#if QT_CONFIG(tabwidget)
|| qobject_cast<const QTabWidget *>(widget)
#endif
#if QT_CONFIG(menu)
|| qobject_cast<const QMenu *>(widget)
|| qobject_cast<const QMenuBar *>(widget)
#endif
;
if (isInteractiveControl)
d->readerForWidget(widget);
// Disable the viewport's autoFillBackground so the styled background shows through.
// Only flip it when it was enabled, and store the widget so unpolish() can restore it
// (e.g. when the application switches to a non-StyleKit style).
if (QWidget *vp = managedViewport(widget); vp && vp->autoFillBackground()) {
vp->setAutoFillBackground(false);
d->bgFillDisabledWidgets.insert(widget);
}
#if QT_CONFIG(menu)
if (qobject_cast<QMenu *>(widget)
# if QT_CONFIG(combobox)
|| widget->inherits("QComboBoxPrivateContainer")
# endif
) {
// Enable translucent backgrounds so that we can draw rounded corners on popup menus.
// Store it in bgFillDisabledWidgets so that we can restore it in unpolish()
if (!widget->testAttribute(Qt::WA_TranslucentBackground)) {
widget->setAttribute(Qt::WA_TranslucentBackground, true);
d->bgFillDisabledWidgets.insert(widget);
}
}
#endif
#if QT_CONFIG(lineedit)
if (auto *lineEdit = qobject_cast<QLineEdit *>(widget)) {
if (!lineEdit->property("_q_stylekit_alignment_set").toBool()) {
QQStyleKitReader *r = d->readerForWidget(widget);
if (r) {
const QWidget *target = containerWidget(widget);
QQStyleKitReader::ControlType ct = controlTypeForWidget(target);
r->setControlType(ct);
const auto *textProps = r->global()->text();
if (textProps) {
const uint align = resolvedAlignment(
textProps->alignment(), Qt::AlignLeft, Qt::AlignVCenter);
lineEdit->setAlignment(Qt::Alignment(align));
}
}
}
}
#endif
d->refreshStyleFont(widget);
d->refreshStylePalette(widget);
bool needsEventFilter = isSelfPaintingWidget(widget);
#if QT_CONFIG(menu)
// Menus are transient: drop their sub-element tracking when they hide
needsEventFilter = needsEventFilter || qobject_cast<QMenu *>(widget);
#endif
if (needsEventFilter)
widget->installEventFilter(this);
QCommonStyle::polish(widget);
}
/*! \reimp */
void QStyleKitStyle::polish(QApplication *app)
{
QCommonStyle::polish(app);
}
/*! \reimp */
void QStyleKitStyle::polish(QPalette &palette)
{
QCommonStyle::polish(palette);
}
/*! \reimp */
void QStyleKitStyle::unpolish(QWidget *widget)
{
Q_D(QStyleKitStyle);
d->unsetStylePalette(widget);
d->unsetStyleFont(widget);
bool hasEventFilter = isSelfPaintingWidget(widget);
#if QT_CONFIG(menu)
hasEventFilter = hasEventFilter || qobject_cast<QMenu *>(widget);
#endif
if (hasEventFilter)
widget->removeEventFilter(this);
if (d->bgFillDisabledWidgets.remove(widget)) {
if (QWidget *vp = managedViewport(widget))
vp->setAutoFillBackground(true);
#if QT_CONFIG(menu)
else if (qobject_cast<QMenu *>(widget)
# if QT_CONFIG(combobox)
|| widget->inherits("QComboBoxPrivateContainer")
# endif
) {
widget->setAttribute(Qt::WA_TranslucentBackground, false);
}
#endif
}
d->cleanupWidgetReader(widget);
#if QT_CONFIG(scrollbar)
if (qobject_cast<QScrollBar *>(widget))
widget->setAttribute(Qt::WA_OpaquePaintEvent);
#endif
QCommonStyle::unpolish(widget);
}
/*! \reimp */
void QStyleKitStyle::unpolish(QApplication *app)
{
Q_D(QStyleKitStyle);
d->clearMetricsCache();
QCommonStyle::unpolish(app);
}
/*! \reimp */
bool QStyleKitStyle::eventFilter(QObject *obj, QEvent *event)
{
Q_D(QStyleKitStyle);
switch (event->type()) {
case QEvent::EnabledChange:
case QEvent::HoverEnter:
case QEvent::HoverLeave:
case QEvent::FocusIn:
case QEvent::FocusOut:
if (auto *w = qobject_cast<QWidget *>(obj)) {
if (d->customPaletteWidgets.contains(w))
d->refreshStylePalette(w);
if (d->customFontWidgets.contains(w))
d->refreshStyleFont(w);
}
break;
#if QT_CONFIG(menu)
case QEvent::Hide:
if (auto *menu = qobject_cast<QMenu *>(obj))
d->cleanupSubElements(menu);
break;
#endif
default:
break;
}
return QCommonStyle::eventFilter(obj, event);
}
/*! \reimp */
bool QStyleKitStyle::event(QEvent *event)
{
return QCommonStyle::event(event);
}
QT_END_NAMESPACE
#include "moc_qstylekitstyle.cpp"
|