summaryrefslogtreecommitdiffstats
path: root/tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp
blob: 79ce68e2b5baad647a9a4c6f64e889aeed22674c (plain)
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
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>
#include <qgraphicswidget.h>
#include <qgraphicsscene.h>
#include <qgraphicssceneevent.h>
#include <qgraphicsview.h>
#include <qstyleoption.h>
#include <qgraphicslinearlayout.h>
#include <qcleanlooksstyle.h>
#include <qlineedit.h>
#include <qboxlayout.h>
#include <qaction.h>
#include <qwidgetaction.h>
#include "../../shared/util.h"


class EventSpy : public QObject
{
    Q_OBJECT
public:
    EventSpy(QObject *watched, QEvent::Type type)
        : _count(0), spied(type)
    {
        watched->installEventFilter(this);
    }

    int count() const { return _count; }

protected:
    bool eventFilter(QObject *watched, QEvent *event)
    {
        Q_UNUSED(watched);
        if (event->type() == spied)
            ++_count;
        return false;
    }

    int _count;
    QEvent::Type spied;
};

#ifndef QT_NO_STYLE_CLEANLOOKS
class tst_QGraphicsWidget : public QObject {
Q_OBJECT

public slots:
    void initTestCase();
    void cleanupTestCase();
    void init();
    void cleanup();

private slots:
    void qgraphicswidget();

    void activation();
    void boundingRect_data();
    void boundingRect();
    void dumpFocusChain_data();
    void dumpFocusChain();
    void focusWidget_data();
    void focusWidget();
    void focusWidget2();
    void focusPolicy_data();
    void focusPolicy();
    void font_data();
    void font();
    void fontPropagation();
    void fontChangedEvent();
    void fontPropagationWidgetItemWidget();
    void fontPropagationSceneChange();
    void geometry_data();
    void geometry();
    void getContentsMargins_data();
    void getContentsMargins();
    void initStyleOption_data();
    void initStyleOption();
    void layout_data();
    void layout();
    void layoutDirection_data();
    void layoutDirection();
    void paint_data();
    void paint();
    void palettePropagation();
    void parentWidget_data();
    void parentWidget();
    void resize_data();
    void resize();
    void setAttribute_data();
    void setAttribute();
    void setStyle_data();
    void setStyle();
    void setTabOrder_data();
    void setTabOrder();
    void setTabOrderAndReparent();
    void topLevelWidget_data();
    void topLevelWidget();
    void unsetLayoutDirection_data();
    void unsetLayoutDirection();
    void focusNextPrevChild_data();
    void focusNextPrevChild();
    void verifyFocusChain();
    void updateFocusChainWhenChildDie();
    void sizeHint_data();
    void sizeHint();
    void consistentPosSizeGeometry_data();
    void consistentPosSizeGeometry();
    void setSizes_data();
    void setSizes();
    void closePopupOnOutsideClick();
    void defaultSize();
    void explicitMouseGrabber();
    void implicitMouseGrabber();
    void doubleClickAfterExplicitMouseGrab();
    void popupMouseGrabber();
    void windowFlags_data();
    void windowFlags();
    void shortcutsDeletion();
    void painterStateProtectionOnWindowFrame();
    void ensureClipping();
    void widgetSendsGeometryChanges();
    void respectHFW();
    void addChildInpolishEvent();
    void polishEvent();
    void polishEvent2();
    void autoFillBackground();
    void initialShow();

    // Task fixes
    void task236127_bspTreeIndexFails();
    void task243004_setStyleCrash();
    void task250119_shortcutContext();
    void QT_BUG_6544_tabFocusFirstUnsetWhenRemovingItems();
};


static void sendMouseMove(QWidget *widget, const QPoint &point, Qt::MouseButton button = Qt::NoButton, Qt::MouseButtons buttons = 0)
{
    QTest::mouseMove(widget, point);
    QMouseEvent event(QEvent::MouseMove, point, button, buttons, 0);
    QApplication::sendEvent(widget, &event);
}

// Subclass that exposes the protected functions.
class SubQGraphicsWidget : public QGraphicsWidget {
public:
    SubQGraphicsWidget(QGraphicsItem *parent = 0, Qt::WindowFlags windowFlags = 0)
        : QGraphicsWidget(parent, windowFlags), eventCount(0)
        { }

    void initStyleOption(QStyleOption *option)
        { QGraphicsWidget::initStyleOption(option); }

    void call_changeEvent(QEvent* event)
        { return QGraphicsWidget::changeEvent(event); }

    bool call_event(QEvent *e)
        { return event(e); }

    void call_focusInEvent(QFocusEvent* event)
        { return QGraphicsWidget::focusInEvent(event); }

    bool call_focusNextPrevChild(bool next)
        { return QGraphicsWidget::focusNextPrevChild(next); }

    void call_focusOutEvent(QFocusEvent* event)
        { return QGraphicsWidget::focusOutEvent(event); }

    void call_hideEvent(QHideEvent* event)
        { return QGraphicsWidget::hideEvent(event); }

    QVariant call_itemChange(QGraphicsItem::GraphicsItemChange change, QVariant const& value)
        { return QGraphicsWidget::itemChange(change, value); }

    void call_moveEvent(QGraphicsSceneMoveEvent* event)
        { return QGraphicsWidget::moveEvent(event); }

    void call_polishEvent()
        { return QGraphicsWidget::polishEvent(); }

    QVariant call_propertyChange(QString const& propertyName, QVariant const& value)
        { return QGraphicsWidget::propertyChange(propertyName, value); }

    void call_resizeEvent(QGraphicsSceneResizeEvent* event)
        { return QGraphicsWidget::resizeEvent(event); }

    bool call_sceneEvent(QEvent* event)
        { return QGraphicsWidget::sceneEvent(event); }

    void call_showEvent(QShowEvent* event)
        { return QGraphicsWidget::showEvent(event); }

    QSizeF call_sizeHint(Qt::SizeHint which, QSizeF const& constraint = QSizeF()) const
        { return QGraphicsWidget::sizeHint(which, constraint); }

    void call_updateGeometry()
        { return QGraphicsWidget::updateGeometry(); }

    int eventCount;
    Qt::LayoutDirection m_painterLayoutDirection;

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        m_painterLayoutDirection = painter->layoutDirection();
        QGraphicsWidget::paint(painter, option, widget);
        if (hasFocus()) {
            painter->setPen(Qt::DotLine);
            painter->drawRect(rect());
        }
        //painter->drawText(QPointF(0,15), data(0).toString());
    }

protected:
    bool event(QEvent *event)
    {
        eventCount++;
        return QGraphicsWidget::event(event);
    }
};

// This will be called before the first test function is executed.
// It is only called once.
void tst_QGraphicsWidget::initTestCase()
{
}

// This will be called after the last test function is executed.
// It is only called once.
void tst_QGraphicsWidget::cleanupTestCase()
{
}

// This will be called before each test function is executed.
void tst_QGraphicsWidget::init()
{
}

// This will be called after every test function.
void tst_QGraphicsWidget::cleanup()
{
}

class SizeHinter : public QGraphicsWidget
{
public:
    SizeHinter(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0,
                const QSizeF &min = QSizeF(5,5),
                const QSizeF &pref = QSizeF(50, 50),
                const QSizeF &max = QSizeF(500, 500))
        : QGraphicsWidget(parent, wFlags)
    {
        m_sizes[Qt::MinimumSize] = min;
        m_sizes[Qt::PreferredSize] = pref;
        m_sizes[Qt::MaximumSize] = max;

    }
    void setSizeHint(Qt::SizeHint which, const QSizeF &newSizeHint)
    {
        m_sizes[which] = newSizeHint;
    }

protected:
    QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const
    {
        Q_UNUSED(constraint);
        return m_sizes[which];
    }
private:
    QSizeF m_sizes[4];
};

void tst_QGraphicsWidget::qgraphicswidget()
{
    SubQGraphicsWidget widget;
    QVERIFY(widget.isVisible());

    QVERIFY(!widget.isWindow());
    QCOMPARE(widget.boundingRect(), QRectF(0, 0, 0, 0));
    QCOMPARE(widget.focusWidget(), (QGraphicsWidget*)0);
    QCOMPARE(widget.focusPolicy(), Qt::NoFocus);
    QCOMPARE(widget.font(), QFont());
    QCOMPARE(widget.geometry(), QRectF(widget.pos(), widget.size()));
    QCOMPARE(widget.layout(), (QGraphicsLayout*)0);
    QCOMPARE(widget.layoutDirection(), Qt::LeftToRight);
    QCOMPARE(widget.palette(), QPalette());
    QCOMPARE(widget.parentWidget(), (QGraphicsWidget*)0);
    QCOMPARE(widget.rect(), QRectF(QPointF(), widget.size()));
    QCOMPARE(widget.size(), QSizeF(0, 0));
    QVERIFY(widget.style() != (QStyle*)0);
    QCOMPARE(widget.testAttribute(Qt::WA_AcceptDrops), false);
    QCOMPARE(widget.topLevelWidget(), (QGraphicsWidget*)&widget);
    QCOMPARE(widget.type(), (int)QGraphicsWidget::Type);
    QCOMPARE(widget.call_propertyChange(QString(), QVariant()), QVariant());
    widget.call_sizeHint(Qt::PreferredSize, QSizeF());

    QGraphicsScene scene;
    QGraphicsWidget *parent = new QGraphicsWidget;
    SizeHinter *child = new SizeHinter(parent);

    QCOMPARE(child->minimumSize(), QSizeF(5, 5));
}

void tst_QGraphicsWidget::activation()
{
    QGraphicsWidget *widget = new QGraphicsWidget;
    QGraphicsWidget *window1 = new QGraphicsWidget(0, Qt::Window);
    QGraphicsWidget *window2 = new QGraphicsWidget(0, Qt::Window);
    QVERIFY(!widget->isActiveWindow());
    QVERIFY(!window1->isActiveWindow());
    QVERIFY(!window2->isActiveWindow());

    QGraphicsScene scene;
    scene.addItem(widget);
    scene.addItem(window1);
    scene.addItem(window2);

    QVERIFY(!widget->isActiveWindow());
    QVERIFY(!window1->isActiveWindow());
    QVERIFY(!window2->isActiveWindow());

    QEvent activateEvent(QEvent::WindowActivate);
    QApplication::sendEvent(&scene, &activateEvent);

    QVERIFY(!widget->isActiveWindow());
    QVERIFY(window1->isActiveWindow());
    QVERIFY(!window2->isActiveWindow());

    scene.setActiveWindow(window1);
    QVERIFY(!widget->isActiveWindow());
    QVERIFY(window1->isActiveWindow());
    QVERIFY(!window2->isActiveWindow());

    QEvent deactivateEvent(QEvent::WindowDeactivate);
    QApplication::sendEvent(&scene, &deactivateEvent);

    QVERIFY(!widget->isActiveWindow());
    QVERIFY(!window1->isActiveWindow());
    QVERIFY(!window2->isActiveWindow());
}

void tst_QGraphicsWidget::boundingRect_data()
{
    QTest::addColumn<QSizeF>("size");
    QTest::newRow("null") << QSizeF(0, 0);
    QTest::newRow("avg") << QSizeF(10, 10);
}

// QRectF boundingRect() const public
void tst_QGraphicsWidget::boundingRect()
{
    QFETCH(QSizeF, size);
    SubQGraphicsWidget widget;
    widget.resize(size);
    QCOMPARE(widget.rect(), QRectF(QPointF(), size));
    QCOMPARE(widget.boundingRect(), QRectF(QPointF(0, 0), size));
}

void tst_QGraphicsWidget::dumpFocusChain_data()
{
    QTest::addColumn<bool>("scene");
    QTest::addColumn<int>("children");
    QTest::addColumn<bool>("setFocus");
    QTest::newRow("empty world") << false << 0 << false;
    QTest::newRow("one world") << true << 2 << false;
    QTest::newRow("one world w/focus") << true << 2 << true;
}

// void dumpFocusChain(QGraphicsScene* scene) public (static)
void tst_QGraphicsWidget::dumpFocusChain()
{
    // ### this test is very strange...
    QFETCH(bool, scene);
    SubQGraphicsWidget *parent = new SubQGraphicsWidget;
    QGraphicsScene *theScene = 0;
    if (scene) {
        theScene = new QGraphicsScene(this);
        theScene->addItem(parent);
    }
    QFETCH(int, children);
    QFETCH(bool, setFocus);
    for (int i = 0; i < children; ++i) {
        SubQGraphicsWidget *widget = new SubQGraphicsWidget(parent);
        if (setFocus) {
            widget->setFlag(QGraphicsItem::ItemIsFocusable, true);
            if (scene)
                theScene->setFocusItem(widget);
        }
    }

    if (!scene)
        delete parent;
}

void tst_QGraphicsWidget::focusWidget_data()
{
    QTest::addColumn<int>("childCount");
    QTest::addColumn<int>("childWithFocus");
    QTest::newRow("none") << 0 << 0;
    QTest::newRow("first") << 3 << 0;
    QTest::newRow("last") << 3 << 2;
}

// QGraphicsWidget* focusWidget() const public
void tst_QGraphicsWidget::focusWidget()
{
    SubQGraphicsWidget *parent = new SubQGraphicsWidget;
    QCOMPARE(parent->focusWidget(), (QGraphicsWidget *)0);
    QGraphicsScene scene;
    QEvent windowActivate(QEvent::WindowActivate);
    qApp->sendEvent(&scene, &windowActivate);
    scene.addItem(parent);

    QFETCH(int, childCount);
    QList<SubQGraphicsWidget *> children;
    for (int i = 0; i < childCount; ++i) {
        SubQGraphicsWidget *widget = new SubQGraphicsWidget(parent);
        widget->setFlag(QGraphicsItem::ItemIsFocusable, true);
        children.append(widget);
    }
    if (childCount > 0) {
        QFETCH(int, childWithFocus);
        SubQGraphicsWidget *widget = children[childWithFocus];
        widget->setFocus();
        QVERIFY(widget->hasFocus());
        QCOMPARE(parent->focusWidget(), static_cast<QGraphicsWidget*>(widget));
    }
}

void tst_QGraphicsWidget::focusWidget2()
{
    QGraphicsScene scene;
    QEvent windowActivate(QEvent::WindowActivate);
    qApp->sendEvent(&scene, &windowActivate);

    QGraphicsWidget *widget = new QGraphicsWidget;
    EventSpy focusInSpy(widget, QEvent::FocusIn);
    EventSpy focusOutSpy(widget, QEvent::FocusOut);

    scene.addItem(widget);

    QVERIFY(!widget->hasFocus());
    widget->setFocusPolicy(Qt::StrongFocus);
    QVERIFY(!widget->hasFocus());

    QGraphicsWidget *subWidget = new QGraphicsWidget(widget);
    QVERIFY(!subWidget->hasFocus());

    scene.setFocus();

    QVERIFY(!widget->hasFocus());
    QVERIFY(!subWidget->hasFocus());

    widget->setFocus();

    QVERIFY(widget->hasFocus());
    QCOMPARE(focusInSpy.count(), 1);
    QVERIFY(!subWidget->hasFocus());

    QGraphicsWidget *otherSubWidget = new QGraphicsWidget;
    EventSpy otherFocusInSpy(otherSubWidget, QEvent::FocusIn);
    EventSpy otherFocusOutSpy(otherSubWidget, QEvent::FocusOut);

    otherSubWidget->setFocusPolicy(Qt::StrongFocus);
    otherSubWidget->setParentItem(widget);

    QVERIFY(widget->hasFocus());
    QCOMPARE(scene.focusItem(), (QGraphicsItem *)widget);
    QVERIFY(!subWidget->hasFocus());
    QVERIFY(!otherSubWidget->hasFocus());

    widget->hide();
    QVERIFY(!widget->hasFocus()); // lose but still has subfocus
    QCOMPARE(focusInSpy.count(), 1);
    QCOMPARE(focusOutSpy.count(), 1);

    widget->show();
    QVERIFY(!widget->hasFocus()); // no regain
    QCOMPARE(focusInSpy.count(), 1);
    QCOMPARE(focusOutSpy.count(), 1);

    widget->hide();

    // try to setup subFocus on item that can't take focus
    subWidget->setFocus();
    QVERIFY(!subWidget->hasFocus());
    QVERIFY(!scene.focusItem()); // but isn't the scene's focus item

    // try to setup subFocus on item that can take focus
    otherSubWidget->setFocus();
    QVERIFY(!otherSubWidget->hasFocus());
    QCOMPARE(widget->focusWidget(), otherSubWidget);
    QVERIFY(!scene.focusItem()); // but isn't the scene's focus item

    widget->show();

    QCOMPARE(scene.focusItem(), (QGraphicsItem *)otherSubWidget); // but isn't the scene's focus item
    QCOMPARE(otherFocusInSpy.count(), 1);
    QCOMPARE(otherFocusOutSpy.count(), 0);

    delete otherSubWidget;

    QCOMPARE(otherFocusOutSpy.count(), 1);
    QVERIFY(!scene.focusItem());
    QVERIFY(!widget->focusWidget());
}

Q_DECLARE_METATYPE(Qt::FocusPolicy)
void tst_QGraphicsWidget::focusPolicy_data()
{
    QTest::addColumn<Qt::FocusPolicy>("focusPolicy1");
    QTest::addColumn<Qt::FocusPolicy>("focusPolicy2");

    for (int i = 0; i < 25; ++i) {
        QTestData &data = QTest::newRow(QString("%1").arg(i).toLatin1());
        switch(i % 5) {
        case 0: data << Qt::TabFocus; break;
        case 1: data << Qt::ClickFocus; break;
        case 2: data << Qt::StrongFocus; break;
        case 3: data << Qt::WheelFocus; break;
        case 4: data << Qt::NoFocus; break;
        }
        switch(i / 5) {
        case 0: data << Qt::TabFocus; break;
        case 1: data << Qt::ClickFocus; break;
        case 2: data << Qt::StrongFocus; break;
        case 3: data << Qt::WheelFocus; break;
        case 4: data << Qt::NoFocus; break;
        }
    }
}

// Qt::FocusPolicy focusPolicy() const public
void tst_QGraphicsWidget::focusPolicy()
{
    QGraphicsScene scene;
    QEvent windowActivate(QEvent::WindowActivate);
    qApp->sendEvent(&scene, &windowActivate);

    SubQGraphicsWidget *widget = new SubQGraphicsWidget;
    scene.addItem(widget);
    QCOMPARE(Qt::NoFocus, widget->focusPolicy());

    QFETCH(Qt::FocusPolicy, focusPolicy1);
    widget->setFocusPolicy(focusPolicy1);
    QCOMPARE(widget->focusPolicy(), focusPolicy1);
    bool isFocusable = widget->flags() & QGraphicsItem::ItemIsFocusable;
    bool wasFocusable = isFocusable;
    QVERIFY(isFocusable == (focusPolicy1 != Qt::NoFocus));
    widget->setFocus();
    QCOMPARE(widget->hasFocus(), isFocusable);

    QFETCH(Qt::FocusPolicy, focusPolicy2);
    widget->setFocusPolicy(focusPolicy2);
    QCOMPARE(widget->focusPolicy(), focusPolicy2);
    isFocusable = widget->flags() & QGraphicsItem::ItemIsFocusable;
    QVERIFY(isFocusable == (focusPolicy2 != Qt::NoFocus));
    QCOMPARE(widget->hasFocus(), wasFocusable && isFocusable);
}

void tst_QGraphicsWidget::font_data()
{
    QTest::addColumn<QString>("fontName");
    QTest::newRow("Helvetica") << "Helvetica";
}

// QFont font() const public
void tst_QGraphicsWidget::font()
{
    QFETCH(QString, fontName);
    SubQGraphicsWidget widget;
    QCOMPARE(widget.font(), QFont());

    QFont font(fontName);
    widget.setFont(font);
    QCOMPARE(widget.font().family(), font.family());
}

void tst_QGraphicsWidget::fontPropagation()
{
    QGraphicsWidget *root = new QGraphicsWidget;
    QGraphicsWidget *child0 = new QGraphicsWidget(root);
    QGraphicsWidget *child1 = new QGraphicsWidget(child0);
    QGraphicsWidget *child2 = new QGraphicsWidget(child1);
    QGraphicsScene scene;
    scene.addItem(root);

    // Check that only the application fonts apply.
    QFont appFont = QApplication::font();
    QCOMPARE(scene.font(), appFont);
    QCOMPARE(root->font(), appFont);
    QCOMPARE(child0->font(), appFont);
    QCOMPARE(child1->font(), appFont);

    // Set child0's Text, and set ToolTipBase on child1.
    QFont boldFont;
    boldFont.setBold(true);
    child0->setFont(boldFont);
    QFont italicFont;
    italicFont.setItalic(true);
    child1->setFont(italicFont);

    // Check that the above settings propagate correctly.
    QCOMPARE(root->font(), appFont);
    QCOMPARE(scene.font(), appFont);
    QVERIFY(child0->font().bold());
    QVERIFY(!child0->font().italic());
    QVERIFY(child1->font().bold());
    QVERIFY(child1->font().italic());
    QVERIFY(child2->font().bold());
    QVERIFY(child2->font().italic());

    QGraphicsWidget *child3 = new QGraphicsWidget(child2);
    QVERIFY(child3->font().bold());
    QVERIFY(child3->font().italic());

    QGraphicsWidget *child4 = new QGraphicsWidget;
    child4->setParentItem(child3);
    QVERIFY(child4->font().bold());
    QVERIFY(child4->font().italic());

    // Replace the app font for child2. Button should propagate but Text
    // should still be ignored. The previous ToolTipBase setting is gone.
    QFont sizeFont;
    sizeFont.setPointSize(43);
    child1->setFont(sizeFont);

    // Check that the above settings propagate correctly.
    QCOMPARE(root->font(), appFont);
    QCOMPARE(scene.font(), appFont);
    QVERIFY(child0->font().bold());
    QVERIFY(!child0->font().italic());
    QVERIFY(child1->font().bold());
    QVERIFY(!child1->font().italic());
    QCOMPARE(child1->font().pointSize(), 43);
    QVERIFY(child2->font().bold());
    QVERIFY(!child2->font().italic());
    QCOMPARE(child2->font().pointSize(), 43);
}

void tst_QGraphicsWidget::fontChangedEvent()
{
    QGraphicsWidget *root = new QGraphicsWidget;
    QGraphicsScene scene;
    scene.addItem(root);

    // Check that only the application fonts apply.
    QFont appFont = QApplication::font();
    QCOMPARE(scene.font(), appFont);
    QCOMPARE(root->font(), appFont);

    EventSpy rootSpyFont(root, QEvent::FontChange);
    EventSpy rootSpyPolish(root, QEvent::Polish);
    QCOMPARE(rootSpyFont.count(), 0);
    QApplication::processEvents(); //The polish event is sent
    QCOMPARE(rootSpyPolish.count(), 1);
    QApplication::processEvents(); //Process events to see if we get the font change event
    //The font is still the same so no fontChangeEvent
    QCOMPARE(rootSpyFont.count(), 0);

    QFont font;
    font.setPointSize(43);
    root->setFont(font);
    QApplication::processEvents(); //Process events to get the font change event
    //The font changed
    QCOMPARE(rootSpyFont.count(), 1);

    //then roll back to the default one.
    root->setFont(appFont);
    QApplication::processEvents(); //Process events to get the font change event
    //The font changed
    QCOMPARE(rootSpyFont.count(), 2);
}

void tst_QGraphicsWidget::fontPropagationWidgetItemWidget()
{
    QGraphicsWidget *widget = new QGraphicsWidget;
    QGraphicsRectItem *rect = new QGraphicsRectItem(widget);
    QGraphicsWidget *widget2 = new QGraphicsWidget(rect);

    QGraphicsScene scene;
    scene.addItem(widget);

    QFont font;
    font.setPointSize(43);
    widget->setFont(font);

    QCOMPARE(widget2->font().pointSize(), 43);
    QCOMPARE(widget2->font().resolve(), QFont().resolve());

    widget->setFont(QFont());

    QCOMPARE(widget2->font().pointSize(), qApp->font().pointSize());
}

void tst_QGraphicsWidget::fontPropagationSceneChange()
{
    QGraphicsWidget *widget = new QGraphicsWidget;
    QGraphicsRectItem *rect = new QGraphicsRectItem(widget);
    QGraphicsWidget *widget2 = new QGraphicsWidget(rect);

    QGraphicsScene scene;
    QGraphicsScene scene2;

    QFont font;
    font.setPointSize(47);
    scene.setFont(font);

    QFont font2;
    font2.setPointSize(74);
    scene2.setFont(font2);

    scene.addItem(widget);
    QCOMPARE(widget2->font().pointSize(), 47);
    scene2.addItem(widget);
    QCOMPARE(widget2->font().pointSize(), 74);
}

void tst_QGraphicsWidget::geometry_data()
{
    QTest::addColumn<QPointF>("pos");
    QTest::addColumn<QSizeF>("size");
    QTest::newRow("null, null") << QPointF() << QSizeF(0, 0);
    QTest::newRow("null, normal") << QPointF() << QSizeF(10, 10);
    QTest::newRow("neg, normal") << QPointF(-5, -5) << QSizeF(10, 10);
}

// QRectF geometry() const public
void tst_QGraphicsWidget::geometry()
{
    SubQGraphicsWidget widget;
    QCOMPARE(widget.geometry(), QRectF(widget.pos(), widget.size()));

    QFETCH(QPointF, pos);
    QFETCH(QSizeF, size);
    widget.setPos(pos);
    widget.resize(size);
    QCOMPARE(widget.geometry(), QRectF(pos, size));
}

void tst_QGraphicsWidget::getContentsMargins_data()
{
    QTest::addColumn<qreal>("left");
    QTest::addColumn<qreal>("top");
    QTest::addColumn<qreal>("right");
    QTest::addColumn<qreal>("bottom");
    QTest::newRow("null") << (qreal)0 << (qreal)0 << (qreal)0 << (qreal)0;
    QTest::newRow("something") << (qreal)10 << (qreal)5 << (qreal)3 << (qreal)7;
    QTest::newRow("real") << (qreal)1.7 << (qreal)5.9 << (qreal)3.2 << (qreal)9.7;
}

// void getContentsMargins(qreal* left, qreal* top, qreal* right, qreal* bottom) const public
void tst_QGraphicsWidget::getContentsMargins()
{
    qreal gleft;
    qreal gtop;
    qreal gright;
    qreal gbottom;

    SubQGraphicsWidget widget;
    widget.getContentsMargins(&gleft, &gtop, &gright, &gbottom);
    QCOMPARE(gleft, (qreal)0);
    QCOMPARE(gtop, (qreal)0);
    QCOMPARE(gright, (qreal)0);
    QCOMPARE(gbottom, (qreal)0);

    QFETCH(qreal, left);
    QFETCH(qreal, top);
    QFETCH(qreal, right);
    QFETCH(qreal, bottom);
    int oldEventCounts = widget.eventCount;
    widget.setContentsMargins(left, top, right, bottom);
    QVERIFY(left == 0 || oldEventCounts != widget.eventCount);
    widget.getContentsMargins(&gleft, &gtop, &gright, &gbottom);
    QCOMPARE(gleft, left);
    QCOMPARE(gtop, top);
    QCOMPARE(gright, right);
    QCOMPARE(gbottom, bottom);
}

Q_DECLARE_METATYPE(Qt::LayoutDirection)
void tst_QGraphicsWidget::initStyleOption_data()
{
    QTest::addColumn<bool>("enabled");
    QTest::addColumn<bool>("focus");
    QTest::addColumn<bool>("underMouse");
    QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
    QTest::addColumn<QSizeF>("size");
    QTest::addColumn<QPalette>("palette");
    QTest::addColumn<QString>("fontName");
    QTest::newRow("none") << false << false << false << Qt::LeftToRight << QSizeF(0, 0) << QPalette() << QString();
    QTest::newRow("all") << true << true << true << Qt::RightToLeft << QSizeF(300, 300) << QPalette(Qt::magenta) << "Helvetica";
    QTest::newRow("rand") << true << false << false << Qt::RightToLeft << QSizeF(1, 0) << QPalette(Qt::darkCyan) << "Times";
}

// void initStyleOption(QStyleOption* option) const public
void tst_QGraphicsWidget::initStyleOption()
{
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    view.show();
#ifdef Q_WS_X11
    qt_x11_wait_for_window_manager(&view);
#endif
    QApplication::setActiveWindow(&view);
    QTest::qWait(25);
    QTRY_COMPARE(QApplication::activeWindow(), &view);

    view.setAlignment(Qt::AlignTop | Qt::AlignLeft);
    SubQGraphicsWidget *widget = new SubQGraphicsWidget;
    widget->setAcceptsHoverEvents(true);
    QStyleOption option;
    scene.addItem(widget);

    QFETCH(QSizeF, size);
    widget->resize(size);

    QFETCH(bool, enabled);
    widget->setEnabled(enabled);
    QFETCH(bool, focus);
    if (focus) {
        widget->setFlag(QGraphicsItem::ItemIsFocusable, true);
        widget->setFocus();
        QVERIFY(widget->hasFocus());
    }
    QFETCH(bool, underMouse);
    if (underMouse) {
        view.resize(300, 300);
        view.show();
        QTest::qWaitForWindowShown(&view);
        QTest::qWait(20);
        sendMouseMove(view.viewport(), view.mapFromScene(widget->mapToScene(widget->boundingRect().center())));
    }

    QFETCH(QPalette, palette);
    widget->setPalette(palette);

    QFETCH(QString, fontName);
    widget->setFont(QFont(fontName));

    // The test
    widget->initStyleOption(&option);

    bool isEnabled = option.state & QStyle::State_Enabled;
    QCOMPARE(isEnabled, enabled);
    bool hasFocus = option.state & QStyle::State_HasFocus;
    QCOMPARE(hasFocus, focus);
    bool isUnderMouse = option.state & QStyle::State_MouseOver;
#ifndef Q_OS_WINCE
    QCOMPARE(isUnderMouse, underMouse);
#endif
    // if (layoutDirection != Qt::LeftToRight)
    //QEXPECT_FAIL("", "QApplicaiton::layoutDirection doesn't propagate to QGraphicsWidget", Continue);
    //QCOMPARE(option.direction, layoutDirection);
    QCOMPARE(option.rect, QRectF(QPointF(), size).toRect());
    QCOMPARE(option.palette, palette.resolve(QApplication::palette()));
    QCOMPARE(option.fontMetrics, QFontMetrics(widget->font()));
}

void tst_QGraphicsWidget::layout_data()
{
    QTest::addColumn<int>("childCount");
    QTest::newRow("empty") << 0;
    QTest::newRow("10") << 10;
}

// QGraphicsLayout* layout() const public
void tst_QGraphicsWidget::layout()
{
    SubQGraphicsWidget widget;
    widget.setContentsMargins(10, 5, 50, 100);
    QCOMPARE(widget.layout(), (QGraphicsLayout *)0);
    QFETCH(int, childCount);

    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
    QList<SubQGraphicsWidget*> children;
    for (int i = 0; i < childCount; ++i) {
        SubQGraphicsWidget *item = new SubQGraphicsWidget;
        layout->addItem(item);
        children.append(item);
    }
    widget.setLayout(layout);

    QTest::qWait(25);

    QCOMPARE(widget.layout(), static_cast<QGraphicsLayout*>(layout));
    for (int i = 0; i < children.count(); ++i) {
        SubQGraphicsWidget *item = children[i];
        QCOMPARE(item->parentWidget(), (QGraphicsWidget *)&widget);
        QVERIFY(item->geometry() != QRectF(0, 0, -1, -1));
    }

    // don't crash
    widget.setLayout(0);
}

void tst_QGraphicsWidget::layoutDirection_data()
{
    QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
    QTest::newRow("rtl") << Qt::RightToLeft;
    QTest::newRow("ltr") << Qt::LeftToRight;
}

// Qt::LayoutDirection layoutDirection() const public
void tst_QGraphicsWidget::layoutDirection()
{
    QFETCH(Qt::LayoutDirection, layoutDirection);
    QGraphicsScene scene;
    QGraphicsView *view = new QGraphicsView(&scene);
    SubQGraphicsWidget widget;
    scene.addItem(&widget);
    QCOMPARE(widget.layoutDirection(), Qt::LeftToRight);
    QCOMPARE(widget.testAttribute(Qt::WA_SetLayoutDirection), false);

    QList<SubQGraphicsWidget*> children;
    for (int i = 0; i < 10; ++i) {
        SubQGraphicsWidget *item = new SubQGraphicsWidget(&widget);
        children.append(item);
        QCOMPARE(item->testAttribute(Qt::WA_SetLayoutDirection), false);
    }
    widget.setLayoutDirection(layoutDirection);
    QCOMPARE(widget.testAttribute(Qt::WA_SetLayoutDirection), true);
    view->show();
    QTest::qWaitForWindowShown(view);
    for (int i = 0; i < children.count(); ++i) {
        QCOMPARE(children[i]->layoutDirection(), layoutDirection);
        QCOMPARE(children[i]->testAttribute(Qt::WA_SetLayoutDirection), false);
        view->repaint();
        QApplication::processEvents();
        QTRY_COMPARE(children[i]->m_painterLayoutDirection, layoutDirection);
    }
    delete view;
}

void tst_QGraphicsWidget::paint_data()
{
    // currently QGraphicsWidget doesn't paint or do anything ...
}

// void paint(QPainter* painter, QStyleOptionGraphicsItem const* option, QWidget* widget) public
void tst_QGraphicsWidget::paint()
{
    SubQGraphicsWidget widget;
    QPainter painter;
    QStyleOptionGraphicsItem option;
    widget.paint(&painter, &option, 0); // check that widget = 0 works.
}

void tst_QGraphicsWidget::palettePropagation()
{
    QGraphicsWidget *root = new QGraphicsWidget;
    QGraphicsWidget *child0 = new QGraphicsWidget(root);
    QGraphicsWidget *child1 = new QGraphicsWidget(child0);
    QGraphicsWidget *child2 = new QGraphicsWidget(child1);
    QGraphicsScene scene;
    scene.addItem(root);

    // These colors are unlikely to be imposed on the default palette of
    // QWidget ;-).
    QColor sysPalText(21, 22, 23);
    QColor sysPalToolTipBase(12, 13, 14);
    QColor overridePalText(42, 43, 44);
    QColor overridePalToolTipBase(45, 46, 47);
    QColor sysPalButton(99, 98, 97);

    // Check that only the application fonts apply.
    QPalette appPal = QApplication::palette();
    QCOMPARE(scene.palette(), appPal);
    QCOMPARE(root->palette(), appPal);
    QCOMPARE(child0->palette(), appPal);
    QCOMPARE(child1->palette(), appPal);

    // Set child0's Text, and set ToolTipBase on child1.
    QPalette textPalette;
    textPalette.setColor(QPalette::Text, overridePalText);
    child0->setPalette(textPalette);
    QPalette toolTipPalette;
    toolTipPalette.setColor(QPalette::ToolTipBase, overridePalToolTipBase);
    child1->setPalette(toolTipPalette);

    // Check that the above settings propagate correctly.
    QCOMPARE(root->palette(), appPal);
    QCOMPARE(scene.palette(), appPal);
    QCOMPARE(child0->palette().color(QPalette::Text), overridePalText);
    QCOMPARE(child0->palette().color(QPalette::ToolTipBase), appPal.color(QPalette::ToolTipBase));
    QCOMPARE(child1->palette().color(QPalette::Text), overridePalText);
    QCOMPARE(child1->palette().color(QPalette::ToolTipBase), overridePalToolTipBase);
    QCOMPARE(child2->palette().color(QPalette::Text), overridePalText);
    QCOMPARE(child2->palette().color(QPalette::ToolTipBase), overridePalToolTipBase);

    QGraphicsWidget *child3 = new QGraphicsWidget(child2);
    QCOMPARE(child3->palette().color(QPalette::Text), overridePalText);
    QCOMPARE(child3->palette().color(QPalette::ToolTipBase), overridePalToolTipBase);

    QGraphicsWidget *child4 = new QGraphicsWidget;
    child4->setParentItem(child3);
    QCOMPARE(child4->palette().color(QPalette::Text), overridePalText);
    QCOMPARE(child4->palette().color(QPalette::ToolTipBase), overridePalToolTipBase);

    // Replace the app palette for child2. Button should propagate but Text
    // should still be ignored. The previous ToolTipBase setting is gone.
    QPalette buttonPalette;
    buttonPalette.setColor(QPalette::Button, sysPalButton);
    child1->setPalette(buttonPalette);

    QCOMPARE(root->palette(), appPal);
    QCOMPARE(scene.palette(), appPal);
    QCOMPARE(child0->palette().color(QPalette::Text), overridePalText);
    QCOMPARE(child0->palette().color(QPalette::ToolTipBase), appPal.color(QPalette::ToolTipBase));
    QCOMPARE(child1->palette().color(QPalette::Text), overridePalText);
    QCOMPARE(child1->palette().color(QPalette::ToolTipBase), appPal.color(QPalette::ToolTipBase));
    QCOMPARE(child1->palette().color(QPalette::Button), sysPalButton);
    QCOMPARE(child2->palette().color(QPalette::Text), overridePalText);
    QCOMPARE(child2->palette().color(QPalette::ToolTipBase), appPal.color(QPalette::ToolTipBase));
    QCOMPARE(child2->palette().color(QPalette::Button), sysPalButton);
}

void tst_QGraphicsWidget::parentWidget_data()
{
    QTest::addColumn<int>("childrenCount");
    QTest::newRow("0") << 0;
    QTest::newRow("1") << 1;
    QTest::newRow("10") << 10;
}

// QGraphicsWidget* parentWidget() const public
void tst_QGraphicsWidget::parentWidget()
{
    QFETCH(int, childrenCount);
    SubQGraphicsWidget standAlongWidget;
    QGraphicsLineItem standAlongItem;

    SubQGraphicsWidget widgetChild(&standAlongWidget);
    SubQGraphicsWidget itemChild(&standAlongItem);

    QCOMPARE(standAlongWidget.parentWidget(), (QGraphicsWidget*)0);
    QCOMPARE(widgetChild.parentWidget(), static_cast<QGraphicsWidget*>(&standAlongWidget));
    QCOMPARE(itemChild.parentWidget(), (QGraphicsWidget*)0);

    for (int i = 0; i < childrenCount; ++i) {
        SubQGraphicsWidget *item = new SubQGraphicsWidget(&standAlongWidget);
        QCOMPARE(item->parentWidget(), static_cast<QGraphicsWidget*>(&standAlongWidget));
    }
}

void tst_QGraphicsWidget::resize_data()
{
    QTest::addColumn<QSizeF>("size");
    QTest::newRow("null") << QSizeF();
    QTest::newRow("10x10") << QSizeF(10, 10);
    QTest::newRow("10x-1") << QSizeF(10, -1);
}

// void resize(qreal w, qreal h) public
void tst_QGraphicsWidget::resize()
{
    QFETCH(QSizeF, size);
    SubQGraphicsWidget widget;

    int oldEventCounts = widget.eventCount;
    QSizeF oldSize = widget.size();
    widget.resize(size);

    QSizeF boundedSize = size.expandedTo(widget.minimumSize()).boundedTo(widget.maximumSize());
    QCOMPARE(widget.eventCount, oldEventCounts + ((oldSize == boundedSize) ? 0 : 1));
    QCOMPARE(widget.size(), boundedSize);
}

Q_DECLARE_METATYPE(Qt::WidgetAttribute)
void tst_QGraphicsWidget::setAttribute_data()
{
    QTest::addColumn<Qt::WidgetAttribute>("attribute");
    QTest::addColumn<bool>("supported");
    QTest::newRow("WA_SetLayoutDirection") << Qt::WA_SetLayoutDirection << true;
    QTest::newRow("WA_RightToLeft") << Qt::WA_RightToLeft << true;
    QTest::newRow("WA_SetStyle") << Qt::WA_SetStyle << true;
    QTest::newRow("WA_Resized") << Qt::WA_Resized << true;
    QTest::newRow("unsupported") << Qt::WA_PaintOutsidePaintEvent << false;
}

// void setAttribute(Qt::WidgetAttribute attribute, bool on = true) public
void tst_QGraphicsWidget::setAttribute()
{
    QFETCH(Qt::WidgetAttribute, attribute);
    QFETCH(bool, supported);
    SubQGraphicsWidget widget;
    if (attribute == Qt::WA_PaintOutsidePaintEvent)
        QTest::ignoreMessage(QtWarningMsg, "QGraphicsWidget::setAttribute: unsupported attribute 13");
    widget.setAttribute(attribute);
    QCOMPARE(widget.testAttribute(attribute), supported);
}

void tst_QGraphicsWidget::setStyle_data()
{
    QTest::addColumn<QString>("style");
    QTest::newRow("null") << "";
    QTest::newRow("cleanlooks") << "QCleanlooksStyle";
}

// void setStyle(QStyle* style) public
void tst_QGraphicsWidget::setStyle()
{
    SubQGraphicsWidget widget;
    QCleanlooksStyle cleanlooksStyle;

    int oldEventCounts = widget.eventCount;

    QFETCH(QString, style);
    if (style == "QCleanlooksStyle") {
        widget.setStyle(&cleanlooksStyle);
        QCOMPARE(widget.style(), static_cast<QStyle*>(&cleanlooksStyle));
    } else {
        widget.setStyle(0);
        QVERIFY(widget.style() != (QStyle *)0);
    }
    QCOMPARE(widget.eventCount, oldEventCounts + 1);
    QCOMPARE(widget.testAttribute(Qt::WA_SetStyle), !style.isEmpty());

    // cleanup
    widget.setStyle(0);
}

void tst_QGraphicsWidget::setTabOrder_data()
{
    QTest::addColumn<int>("childrenCount");
    QTest::newRow("0") << 0;
    QTest::newRow("1") << 1;
    QTest::newRow("10") << 10;
}

// void setTabOrder(QGraphicsWidget* first, QGraphicsWidget* second) public
void tst_QGraphicsWidget::setTabOrder()
{
    QFETCH(int, childrenCount);
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    view.show();
#ifdef Q_WS_X11
    qt_x11_wait_for_window_manager(&view);
#endif
    QApplication::setActiveWindow(&view);
    QTest::qWait(25);
    QTRY_COMPARE(QApplication::activeWindow(), &view);

    QGraphicsWidget *lastItem = 0;
    QTest::ignoreMessage(QtWarningMsg, "QGraphicsWidget::setTabOrder(0, 0) is undefined");
    QGraphicsWidget::setTabOrder(0, 0);

    QList<SubQGraphicsWidget*> children;
    for (int i = 0; i < childrenCount; ++i) {
        SubQGraphicsWidget *item = new SubQGraphicsWidget();
        item->setFocusPolicy(Qt::TabFocus);
        children.append(item);
        scene.addItem(item);
        if (lastItem)
            QGraphicsWidget::setTabOrder(lastItem, item);
        lastItem = item;
    }

    if (!children.isEmpty()) {
        QGraphicsWidget *first = children.first();
        view.viewport()->setFocus();
        QApplication::processEvents();
        QTRY_VERIFY(view.viewport()->hasFocus());
        first->setFocus();
        QVERIFY(first->hasFocus());
        QVERIFY(scene.hasFocus());
        QVERIFY(view.viewport()->hasFocus());

        int currentItem = 0;
        while (currentItem < children.count() - 1) {
            QTest::keyPress(view.viewport(), Qt::Key_Tab);
            ++currentItem;
            QVERIFY(children[currentItem % children.size()]->hasFocus());
        }
    }
}

static bool compareFocusChain(QGraphicsView *view, const QList<QGraphicsItem*> &order)
{
    QGraphicsScene *scene = view->scene();
    QStringList actual;
    QGraphicsItem *oldFocusItem = scene->focusItem();
    for (int i = 0; i < order.count(); ++i) {
        QGraphicsItem *focusItem = scene->focusItem();
        actual << focusItem->data(0).toString();
        //qDebug() << "i:" << i << "expected:" << QString::number(uint(order.at(i)), 16) << QString::number(uint(focusItem), 16);
        if (focusItem != order.at(i)) {
            qDebug() << "actual:"  << actual;
            scene->setFocusItem(oldFocusItem);
            return false;
        }
        if (i < order.count() - 1)
            QTest::keyPress(view, Qt::Key_Tab);
    }
    scene->setFocusItem(oldFocusItem);
    return true;
}

void tst_QGraphicsWidget::setTabOrderAndReparent()
{
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    view.show();
    QApplication::setActiveWindow(&view);
    QTest::qWaitForWindowShown(&view);
    QTRY_COMPARE(QApplication::activeWindow(), &view);

    int i;
    QGraphicsWidget *w1, *w2, *w3, *w4;
    for (i = 1; i < 4; ++i) {
        QGraphicsWidget *wid = new QGraphicsWidget;
        wid->setFocusPolicy(Qt::StrongFocus);
        wid->setData(0, QString::fromAscii("w%1").arg(i));
        scene.addItem(wid);
        if (i == 1)
            w1 = wid;
        else if (i == 2)
            w2 = wid;
        else if (i == 3)
            w3 = wid;
    }

    w1->setFocus();
    QTRY_VERIFY(w1->hasFocus());
    QVERIFY(compareFocusChain(&view, QList<QGraphicsItem*>() << w1 << w2 << w3));

    QGraphicsWidget *p = new QGraphicsWidget;
    p->setData(0, QLatin1String("parent"));
    p->setFocusPolicy(Qt::StrongFocus);

    w1->setFocus();
    QVERIFY(compareFocusChain(&view, QList<QGraphicsItem*>() << w1 << w2 << w3));

    w1->setParentItem(p);
    w2->setFocus();
    QVERIFY(compareFocusChain(&view, QList<QGraphicsItem*>() <<  w2 << w3));

    w2->setParentItem(p);
    w3->setFocus();
    QVERIFY(compareFocusChain(&view, QList<QGraphicsItem*>() << w3));
    w3->setParentItem(p);
    QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem*>(0));

    scene.addItem(p);
    p->setFocus();

    QVERIFY(compareFocusChain(&view, QList<QGraphicsItem*>() << p << w1 << w2 << w3));
    delete p;

    for (i = 1; i < 5; ++i) {
        QGraphicsWidget *wid = new QGraphicsWidget;
        wid->setFocusPolicy(Qt::StrongFocus);
        wid->setData(0, QString::fromAscii("w%1").arg(i));
        scene.addItem(wid);
        if (i == 1)
            w1 = wid;
        else if (i == 2)
            w2 = wid;
        else if (i == 3)
            w3 = wid;
        else if (i == 4)
            w4 = wid;
    }
    w4->setParentItem(w1);
    QGraphicsWidget::setTabOrder(w1, w4);
    w1->setFocus();
    QVERIFY(compareFocusChain(&view, QList<QGraphicsItem*>() << w1 << w4 << w2 << w3));

    p = new QGraphicsWidget;
    p->setData(0, QLatin1String("parent"));
    p->setFocusPolicy(Qt::StrongFocus);

    w1->setParentItem(p);
    w2->setFocus();
    QVERIFY(compareFocusChain(&view, QList<QGraphicsItem*>() << w2 << w3));

    scene.addItem(p);
    w2->setFocus();
    QVERIFY(compareFocusChain(&view, QList<QGraphicsItem*>() << w2 << w3 << p << w1 << w4));
}

void tst_QGraphicsWidget::topLevelWidget_data()
{
    QTest::addColumn<QString>("str");
    QTest::newRow("test one") << "foo";
}

// QGraphicsWidget* topLevelWidget() const public
void tst_QGraphicsWidget::topLevelWidget()
{
    QFETCH(QString, str);
    SubQGraphicsWidget widget;
    QCOMPARE(widget.topLevelWidget(), (QGraphicsWidget *)&widget);
}

void tst_QGraphicsWidget::unsetLayoutDirection_data()
{
    QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
    QTest::newRow("rtl") << Qt::RightToLeft;
    QTest::newRow("ltr") << Qt::LeftToRight;
}

// void unsetLayoutDirection() public
void tst_QGraphicsWidget::unsetLayoutDirection()
{
    QApplication::setLayoutDirection(Qt::LeftToRight);
    QFETCH(Qt::LayoutDirection, layoutDirection);
    SubQGraphicsWidget widget;
    QCOMPARE(Qt::LeftToRight, widget.layoutDirection());

    QList<SubQGraphicsWidget*> children;
    for (int i = 0; i < 10; ++i) {
        SubQGraphicsWidget *item = new SubQGraphicsWidget(&widget);
        children.append(item);
    }
    widget.setLayoutDirection(layoutDirection);
    widget.unsetLayoutDirection();
    QCOMPARE(widget.testAttribute(Qt::WA_SetLayoutDirection), false);
    for (int i = 0; i < children.count(); ++i) {
        QCOMPARE(children[i]->layoutDirection(), Qt::LeftToRight);
    }
}

void tst_QGraphicsWidget::focusNextPrevChild_data()
{
    QTest::addColumn<QString>("str");
    QTest::newRow("test one") << "foo";
}

// bool focusNextPrevChild(bool next) protected
void tst_QGraphicsWidget::focusNextPrevChild()
{
    QFETCH(QString, str);
    SubQGraphicsWidget widget;
    // ### write test after just calling it stops crashing :)
    widget.call_focusNextPrevChild(true);
}

void tst_QGraphicsWidget::verifyFocusChain()
{
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    view.show();
    QApplication::setActiveWindow(&view);
    QTest::qWaitForWindowShown(&view);
    QTRY_COMPARE(QApplication::activeWindow(), &view);

    {
        // parent/child focus
        SubQGraphicsWidget *w = new SubQGraphicsWidget(0, Qt::Window);
        w->setFocusPolicy(Qt::StrongFocus);
        SubQGraphicsWidget *w1_1 = new SubQGraphicsWidget(w);
        w1_1->setFocusPolicy(Qt::StrongFocus);
        scene.addItem(w);
        w->setFocus();
        QVERIFY(w->hasFocus());
        w->call_focusNextPrevChild(true);
        QVERIFY(w1_1->hasFocus());
        delete w;
    }

    {
        // delete item in focus chain and verify chain
        SubQGraphicsWidget *w = new SubQGraphicsWidget(0, Qt::Window);
        SubQGraphicsWidget *w1_1 = new SubQGraphicsWidget(w);
        SubQGraphicsWidget *w1_2 = new SubQGraphicsWidget(w);
        SubQGraphicsWidget *w1_3 = new SubQGraphicsWidget(w);
        w1_1->setFocusPolicy(Qt::StrongFocus);
        w1_2->setFocusPolicy(Qt::StrongFocus);
        w1_3->setFocusPolicy(Qt::StrongFocus);
        scene.addItem(w);
        w1_1->setFocus();
        QVERIFY(w1_1->hasFocus());
        QCOMPARE(w->call_focusNextPrevChild(true), true);
        QVERIFY(w1_2->hasFocus());
        QCOMPARE(w->call_focusNextPrevChild(true), true);
        QVERIFY(w1_3->hasFocus());
        w1_1->setFocus();
        delete w1_2;
        w->call_focusNextPrevChild(true);
        QVERIFY(w1_3->hasFocus());
        delete w;
    }
    {
        // parent/child focus
        SubQGraphicsWidget *w = new SubQGraphicsWidget(0, Qt::Window);
        w->setFocusPolicy(Qt::StrongFocus);
        SubQGraphicsWidget *w1_1 = new SubQGraphicsWidget(w);
        w1_1->setFocusPolicy(Qt::StrongFocus);
        scene.addItem(w);
        w->setFocus();
        QVERIFY(w->hasFocus());
        w->call_focusNextPrevChild(true);
        QVERIFY(w1_1->hasFocus());
        delete w;
    }
    {
        // remove the tabFocusFirst widget from the scene.
        QWidget *window = new QWidget;
        QVBoxLayout *layout = new QVBoxLayout;
        window->setLayout(layout);
        QLineEdit *lineEdit = new QLineEdit;
        layout->addWidget(lineEdit);
        QGraphicsView *view = new QGraphicsView(&scene);
        scene.setSceneRect(-20, -20, 200, 50);
        layout->addWidget(view);
        view->setMinimumSize(150, 50);
        SubQGraphicsWidget *w1_1 = new SubQGraphicsWidget;
        w1_1->setData(0, "w1_1");
        w1_1->setGeometry(0,0,25, 25);
        w1_1->setFocusPolicy(Qt::StrongFocus);
        scene.addItem(w1_1);
        SubQGraphicsWidget *w1_2 = new SubQGraphicsWidget;
        w1_2->setData(0, "w1_2");
        w1_2->setGeometry(25,0,25, 25);
        w1_2->setFocusPolicy(Qt::StrongFocus);
        scene.addItem(w1_2);
        window->show();
        QApplication::setActiveWindow(window);
        QTest::qWaitForWindowShown(window);

        lineEdit->setFocus();
        QTest::qWait(25);
        QTRY_VERIFY(lineEdit->hasFocus());
        QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
        QTest::qWait(25);
        QTRY_VERIFY(w1_1->hasFocus());
        QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
        QTest::qWait(25);
        QTRY_VERIFY(w1_2->hasFocus());

        // remove the tabFocusFirst and insert new item
        delete w1_1;            // calls _q_removeItemLater
        QTest::qWait(25);
        SubQGraphicsWidget *w1_3 = new SubQGraphicsWidget;
        w1_3->setFocusPolicy(Qt::StrongFocus);
        w1_3->setData(0, "w1_3");
        w1_3->setGeometry(50,0,25, 25);
        scene.addItem(w1_3);
        QTRY_VERIFY(w1_2->hasFocus());
        QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
        QTest::qWait(25);
        QTRY_VERIFY(lineEdit->hasFocus());
        // tabFocusFirst should now point to w1_2
        QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
        QTest::qWait(25);
        QTRY_VERIFY(w1_2->hasFocus());
        QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
        QTest::qWait(25);
        QTRY_VERIFY(w1_3->hasFocus());
        scene.removeItem(w1_2);   // does not call _q_removeItemLater
        delete w1_2;            // calls _q_removeItemLater

        SubQGraphicsWidget *w1_4 = new SubQGraphicsWidget;
        w1_4->setFocusPolicy(Qt::StrongFocus);
        w1_4->setData(0, "w1_4");
        w1_4->setGeometry(75,0,25, 25);
        scene.addItem(w1_4);
        QTRY_VERIFY(w1_3->hasFocus());
        QTest::qWait(25);
        QTRY_VERIFY(compareFocusChain(view, QList<QGraphicsItem*>() << w1_3 << w1_4));
        QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
        QTest::qWait(25);
        QTRY_VERIFY(lineEdit->hasFocus());
        // tabFocusFirst should now point to w1_3
        QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
        QTest::qWait(25);
        QTRY_VERIFY(w1_3->hasFocus());
        QTest::qWait(25);
        QTRY_VERIFY(compareFocusChain(view, QList<QGraphicsItem*>() << w1_3 << w1_4));
        delete window;
    }
}

void tst_QGraphicsWidget::updateFocusChainWhenChildDie()
{
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    view.show();
#ifdef Q_WS_X11
    qt_x11_wait_for_window_manager(&view);
#endif
    QApplication::setActiveWindow(&view);
    QTest::qWait(25);
    QTRY_COMPARE(QApplication::activeWindow(), &view);

    // delete item in focus chain with no focus and verify chain
    SubQGraphicsWidget *parent = new SubQGraphicsWidget(0, Qt::Window);
    SubQGraphicsWidget *w = new SubQGraphicsWidget(0, Qt::Window);
    w->resize(50,50);
    w->resize(100,100);
    SubQGraphicsWidget *w1_1 = new SubQGraphicsWidget(w);
    w1_1->setFocusPolicy(Qt::StrongFocus);
    w->setFocusPolicy(Qt::StrongFocus);
    scene.addItem(w);
    scene.addItem(parent);
    w1_1->setFocus();

    QVERIFY(w1_1->hasFocus());
    QWidget myWidget(0);
    QLineEdit edit(&myWidget);
    myWidget.show();
    edit.setFocus();
    QTRY_VERIFY(edit.hasFocus());
    delete w1_1;
    myWidget.hide();
    w->setParentItem(parent);
    //We don't crash perfect
    QVERIFY(w);
    QTest::mouseMove(view.viewport());
    QTest::mouseClick(view.viewport(), Qt::LeftButton, 0);
    QTRY_COMPARE(qApp->activeWindow(), static_cast<QWidget *>(&view));
    QTRY_COMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(w));
}

void tst_QGraphicsWidget::sizeHint_data()
{
    QTest::addColumn<bool>("layout");
    QTest::newRow("no layout") << false;
    QTest::newRow("layout") << true;
}

// QSizeF sizeHint(Qt::SizeHint which, QSizeF const& constraint = QSizeF()) const protected
void tst_QGraphicsWidget::sizeHint()
{
    QFETCH(bool, layout);
    SubQGraphicsWidget widget;

    if (layout) {
        QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
        widget.setLayout(layout);
    }
    widget.call_sizeHint(Qt::MinimumSize, QSizeF());
}

void tst_QGraphicsWidget::consistentPosSizeGeometry_data()
{
    QTest::addColumn<QSizeF>("minSize");
    QTest::addColumn<QSizeF>("maxSize");
    QTest::addColumn<QRectF>("geometry");
    QTest::addColumn<QRectF>("expectedGeometry");

    QTest::newRow("size is valid") << QSizeF(0, 0) << QSizeF(200, 200) << QRectF(0, 0, 100, 100) << QRectF(0, 0, 100, 100);
    QTest::newRow("size is larger than max") << QSizeF(0, 0) << QSizeF(50, 50) << QRectF(0, 0, 100, 100) << QRectF(0, 0, 50, 50);
    QTest::newRow("size is smaller than min") << QSizeF(50, 50) << QSizeF(150, 150) << QRectF(0, 0, 10, 10) << QRectF(0, 0, 50, 50);
}

void tst_QGraphicsWidget::consistentPosSizeGeometry()
{
    QFETCH(QSizeF, minSize);
    QFETCH(QSizeF, maxSize);
    QFETCH(QRectF, geometry);
    QFETCH(QRectF, expectedGeometry);

    QGraphicsScene scene;
    QGraphicsView view(&scene);
    QGraphicsWidget *w = new QGraphicsWidget;
    scene.addItem(w);
    w->setMinimumSize(minSize);
    w->setMaximumSize(maxSize);
    w->setGeometry(geometry);
    QCOMPARE(w->geometry(), expectedGeometry);
    QCOMPARE(w->pos(), expectedGeometry.topLeft());
    QCOMPARE(w->size(), expectedGeometry.size());

    QRectF otherGeom = QRectF(QPointF(12.34,12.34), minSize);
    w->setGeometry(otherGeom);
    QCOMPARE(w->geometry(), otherGeom);
    QCOMPARE(w->pos(), otherGeom.topLeft());
    QCOMPARE(w->size(), otherGeom.size());

    w->setPos(geometry.topLeft());
    QCOMPARE(w->geometry().topLeft(), expectedGeometry.topLeft());
    QCOMPARE(w->pos(), expectedGeometry.topLeft());

    w->resize(geometry.size());
    QCOMPARE(w->geometry().size(), expectedGeometry.size());
    QCOMPARE(w->geometry(), expectedGeometry);

    view.show();

}


enum WhichSize {
    MinimumWidth,
    PreferredWidth,
    MaximumWidth,
    MinimumHeight,
    PreferredHeight,
    MaximumHeight,
    MinimumSize,
    PreferredSize,
    MaximumSize,
    MinimumSizeHint,
    PreferredSizeHint,
    MaximumSizeHint,
    Size,
    None,
};

typedef QPair<int, QVariant> Inst;

Q_DECLARE_METATYPE(Inst)
Q_DECLARE_METATYPE(QVector<Inst>)

void tst_QGraphicsWidget::setSizes_data()
{

    QTest::addColumn<QVector<Inst> >("inputInstructions");
    QTest::addColumn<QVector<Inst> >("compareInstructions");

    QTest::newRow("minSize1") << (QVector<Inst>() << Inst(Size, QSize(25, 25)) << Inst(MinimumSize, QSize(10, 10)))
                                << (QVector<Inst>() << Inst(Size, QSize(25,25)));
    QTest::newRow("minSize2") << (QVector<Inst>() << Inst(Size, QSizeF(20, 20)) << Inst(MinimumSize, QSizeF(25, 25)))
                                 << (QVector<Inst>() << Inst(Size, QSizeF(25, 25)));
    QTest::newRow("minWidth1") << (QVector<Inst>() << Inst(Size, QSizeF(20, 20)) << Inst(MinimumWidth, 5.0))
                                 << (QVector<Inst>() << Inst(Size, QSizeF(20, 20)));
    QTest::newRow("minWidth2") << (QVector<Inst>() << Inst(Size, QSizeF(20, 20)) << Inst(MinimumWidth, 25.0))
                                 << (QVector<Inst>() << Inst(Size, QSizeF(25, 20)));
    QTest::newRow("minHeight1") << (QVector<Inst>() << Inst(Size, QSizeF(20, 20)) << Inst(MinimumHeight, 5.0))
                                 << (QVector<Inst>() << Inst(Size, QSizeF(20, 20)));
    QTest::newRow("minHeight2") << (QVector<Inst>() << Inst(Size, QSizeF(20, 20)) << Inst(MinimumHeight, 25.0))
                                 << (QVector<Inst>() << Inst(Size, QSizeF(20, 25)));
    QTest::newRow("maxSize1") << (QVector<Inst>() << Inst(Size, QSizeF(40, 40)) << Inst(MaximumSize, QSizeF(30, 30)))
                                 << (QVector<Inst>() << Inst(Size, QSizeF(30, 30)));
    QTest::newRow("maxSize2") << (QVector<Inst>() << Inst(Size, QSizeF(40, 40)) << Inst(MaximumSize, QSizeF(30, -1)))
                                 << (QVector<Inst>() << Inst(Size, QSizeF(30, 40)));
    QTest::newRow("maxSize3") << (QVector<Inst>() << Inst(Size, QSizeF(40, 40)) << Inst(MaximumSize, QSizeF(-1, 30)))
                                 << (QVector<Inst>() << Inst(Size, QSizeF(40, 30)));
    QTest::newRow("maxWidth1")<< (QVector<Inst>() << Inst(Size, QSizeF(40, 40)) << Inst(MaximumWidth, 30))
                                 << (QVector<Inst>() << Inst(Size, QSizeF(30, 40)));
    QTest::newRow("maxHeight")<< (QVector<Inst>() << Inst(Size, QSizeF(40, 40)) << Inst(MaximumHeight, 20))
                                 << (QVector<Inst>() << Inst(Size, QSizeF(40, 20)));
    QTest::newRow("unsetMinSize")<< (QVector<Inst>() << Inst(Size, QSizeF(40, 40)) << Inst(MinimumSize, QSizeF(-1, -1)))
                                 << (QVector<Inst>() << Inst(MinimumSize, QSizeF(5, 5)));
    QTest::newRow("unsetMaxSize")<< (QVector<Inst>() << Inst(Size, QSizeF(40, 40)) << Inst(MaximumSize, QSizeF(-1, -1)))
                                 << (QVector<Inst>() << Inst(MaximumSize, QSizeF(500, 500)));
    QTest::newRow("unsetMinSize, expand size to minimumSizeHint") << (QVector<Inst>()
                                        << Inst(MinimumSize, QSize(0, 0))
                                        << Inst(Size, QSize(1,1))
                                        << Inst(MinimumSize, QSize(-1.0, -1.0))
                                        )
                                    << (QVector<Inst>()
                                        << Inst(Size, QSize(5,5))
                                        << Inst(MinimumSize, QSize(5,5))
                                        );

}

void tst_QGraphicsWidget::setSizes()
{
    QFETCH(QVector<Inst>, inputInstructions);
    QFETCH(QVector<Inst>, compareInstructions);

    QGraphicsScene scene;
    QGraphicsView view(&scene);
    SizeHinter *widget = new SizeHinter(0, Qt::Window);
    QSizeF min = QSizeF(10, 10);
    QSizeF pref = QSizeF(25, 25);
    QSizeF max = QSizeF(50, 50);

    int i;
    for (i = 0; i < inputInstructions.count(); ++i) {
        Inst input = inputInstructions.at(i);

        // defaults
        switch (input.first) {
            case MinimumSize:
                min = input.second.toSizeF();
                break;
            case PreferredSize:
                pref = input.second.toSizeF();
                break;
            case MaximumSize:
                max = input.second.toSizeF();
                break;
            case Size:
                widget->resize(input.second.toSizeF());
                break;
            case MinimumWidth:
                widget->setMinimumWidth(qreal(input.second.toDouble()));
                break;
            case PreferredWidth:
                widget->setPreferredWidth(qreal(input.second.toDouble()));
                break;
            case MaximumWidth:
                widget->setMaximumWidth(qreal(input.second.toDouble()));
                break;
            case MinimumHeight:
                widget->setMinimumHeight(qreal(input.second.toDouble()));
                break;
            case PreferredHeight:
                widget->setPreferredHeight(qreal(input.second.toDouble()));
                break;
            case MaximumHeight:
                widget->setMaximumHeight(qreal(input.second.toDouble()));
                break;
            case MinimumSizeHint:
                widget->setSizeHint(Qt::MinimumSize, input.second.toSizeF());
                break;
            case PreferredSizeHint:
                widget->setSizeHint(Qt::PreferredSize, input.second.toSizeF());
                break;
            case MaximumSizeHint:
                widget->setSizeHint(Qt::MaximumSize, input.second.toSizeF());
                break;
            default:
                qWarning("instruction not implemented");
                break;
        }
    }

    widget->setMinimumSize(min);
    widget->setPreferredSize(pref);
    widget->setMaximumSize(max);

    QApplication::processEvents();

    for (i = 0; i < compareInstructions.count(); ++i) {
        Inst input = compareInstructions.at(i);
        switch (input.first) {
            case MinimumSize:
                QCOMPARE(widget->minimumSize(), input.second.toSizeF());
                break;
            case PreferredSize:
                QCOMPARE(widget->preferredSize(), input.second.toSizeF());
                break;
            case MaximumSize:
                QCOMPARE(widget->maximumSize(), input.second.toSizeF());
                break;
            case Size:
                QCOMPARE(widget->size(), input.second.toSizeF());
                break;
            case MinimumWidth:
                QCOMPARE(widget->minimumWidth(), qreal(input.second.toDouble()));
                break;
            case PreferredWidth:
                QCOMPARE(widget->preferredWidth(), qreal(input.second.toDouble()));
                break;
            case MaximumWidth:
                QCOMPARE(widget->maximumWidth(), qreal(input.second.toDouble()));
                break;
            default:
                qWarning("instruction not implemented");
                break;
        }
    }
    delete widget;
}

void tst_QGraphicsWidget::closePopupOnOutsideClick()
{
    QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Popup);
    widget->resize(100, 100);

    QGraphicsScene scene;
    scene.addItem(widget);

    QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
    event.ignore();
    event.setScenePos(QPointF(50, 50));
    qApp->sendEvent(&scene, &event);

    QVERIFY(widget->isVisible());
    QVERIFY(event.isAccepted());

    event.ignore();
    event.setScenePos(QPointF(150, 150));
    qApp->sendEvent(&scene, &event);

    QVERIFY(!widget->isVisible());
    QVERIFY(event.isAccepted());
}

void tst_QGraphicsWidget::task236127_bspTreeIndexFails()
{
    QGraphicsWidget *widget = new QGraphicsWidget;
    QGraphicsWidget *widget2 = new QGraphicsWidget;
    widget->resize(10, 10);
    widget2->resize(10, 10);
    widget2->setZValue(1);
    QCOMPARE(widget2->zValue(), qreal(1));
    QCOMPARE(widget->zValue(), qreal(0));
    widget->setData(0, "widget");
    widget2->setData(0, "widget2");

    QGraphicsScene scene;
    scene.addItem(widget);
    scene.addItem(widget2);

    QGraphicsView view(&scene);
    view.show();
#ifdef Q_WS_X11
    qt_x11_wait_for_window_manager(&view);
#endif
    QTest::qWait(100);

    QVERIFY(!scene.itemAt(25, 25));
    widget->setGeometry(0, 112, 360, 528);
    QCOMPARE(scene.itemAt(15, 120), (QGraphicsItem *)widget);
    widget2->setGeometry(0, 573, 360, 67);
    QCOMPARE(scene.itemAt(15, 120), (QGraphicsItem *)widget);
    QCOMPARE(scene.itemAt(50, 585), (QGraphicsItem *)widget2);
}

void tst_QGraphicsWidget::defaultSize()
{
    SubQGraphicsWidget *widget = new SubQGraphicsWidget;
    widget->setMinimumSize(40, 40);
    QGraphicsScene scene;
    scene.addItem(widget);

    QGraphicsView view(&scene);
    view.show();
#ifdef Q_WS_X11
    qt_x11_wait_for_window_manager(&view);
#endif
    QTest::qWait(50);
    QSizeF initialSize = widget->size();

    widget->resize(initialSize);
    QCOMPARE(widget->geometry().size(), initialSize);
    widget->setVisible(false);
    widget->setMinimumSize(10, 10);
    widget->setPreferredSize(60, 60);
    widget->setMaximumSize(110, 110);
    widget->setVisible(true);
    QTest::qWait(50);
    // should still have its size set to initialsize
    QCOMPARE(widget->geometry().size(), initialSize);

}

void tst_QGraphicsWidget::explicitMouseGrabber()
{
    QGraphicsWidget *widget = new QGraphicsWidget;
    EventSpy widgetGrabEventSpy(widget, QEvent::GrabMouse);
    EventSpy widgetUngrabEventSpy(widget, QEvent::UngrabMouse);

    // Grab without scene
    QTest::ignoreMessage(QtWarningMsg, "QGraphicsItem::grabMouse: cannot grab mouse without scene");
    widget->grabMouse();
    QCOMPARE(widgetGrabEventSpy.count(), 0);
    QTest::ignoreMessage(QtWarningMsg, "QGraphicsItem::ungrabMouse: cannot ungrab mouse without scene");
    widget->ungrabMouse();
    QCOMPARE(widgetUngrabEventSpy.count(), 0);

    // Add to scene
    QGraphicsScene scene;
    scene.addItem(widget);

    // Ungrab while not grabber
    QTest::ignoreMessage(QtWarningMsg, "QGraphicsItem::ungrabMouse: not a mouse grabber");
    widget->ungrabMouse();

    // Simple grab with scene
    QVERIFY(!scene.mouseGrabberItem());
    widget->grabMouse();
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget);
    QCOMPARE(widgetGrabEventSpy.count(), 1);
    widget->ungrabMouse();
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
    QCOMPARE(widgetUngrabEventSpy.count(), 1);

    // Grab while grabbing
    widget->grabMouse();
    QCOMPARE(widgetGrabEventSpy.count(), 2);
    QTest::ignoreMessage(QtWarningMsg, "QGraphicsItem::grabMouse: already a mouse grabber");
    widget->grabMouse();
    QCOMPARE(widgetGrabEventSpy.count(), 2);
    QCOMPARE(widgetUngrabEventSpy.count(), 1);
    widget->ungrabMouse();
    QCOMPARE(widgetUngrabEventSpy.count(), 2);

    // Add two more widgets to the scene
    QGraphicsWidget *widget2 = new QGraphicsWidget;
    scene.addItem(widget2);
    EventSpy widget2GrabEventSpy(widget2, QEvent::GrabMouse);
    EventSpy widget2UngrabEventSpy(widget2, QEvent::UngrabMouse);
    QGraphicsWidget *widget3 = new QGraphicsWidget;
    scene.addItem(widget3);
    EventSpy widget3GrabEventSpy(widget3, QEvent::GrabMouse);
    EventSpy widget3UngrabEventSpy(widget3, QEvent::UngrabMouse);

    widget->setData(0, "widget");
    widget2->setData(0, "widget2");
    widget3->setData(0, "widget3");

    // Simple nested grabbing
    widget->grabMouse();
    QCOMPARE(widgetGrabEventSpy.count(), 3);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget);
    widget2->grabMouse();
    QCOMPARE(widgetUngrabEventSpy.count(), 3);
    QCOMPARE(widget2GrabEventSpy.count(), 1);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget2);
    widget3->grabMouse();
    QCOMPARE(widget2UngrabEventSpy.count(), 1);
    QCOMPARE(widget3GrabEventSpy.count(), 1);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget3);
    widget3->ungrabMouse();
    QCOMPARE(widget3UngrabEventSpy.count(), 1);
    QCOMPARE(widget2GrabEventSpy.count(), 2);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget2);
    widget2->ungrabMouse();
    QCOMPARE(widget2UngrabEventSpy.count(), 2);
    QCOMPARE(widgetGrabEventSpy.count(), 4);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget);
    widget->ungrabMouse();
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);

    // Out of order ungrab
    widget->grabMouse();
    QCOMPARE(widgetGrabEventSpy.count(), 5);
    widget2->grabMouse();
    QCOMPARE(widget2GrabEventSpy.count(), 3);
    widget3->grabMouse();
    QCOMPARE(widget3GrabEventSpy.count(), 2);
    widget2->ungrabMouse();
    QCOMPARE(widget3UngrabEventSpy.count(), 2);
    QCOMPARE(widget2UngrabEventSpy.count(), 4);
    QCOMPARE(widgetGrabEventSpy.count(), 6);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget);
}

void tst_QGraphicsWidget::implicitMouseGrabber()
{
    QGraphicsScene scene;
    QGraphicsWidget *widget = new QGraphicsWidget;
    widget->setFlag(QGraphicsItem::ItemIsMovable); // can grab mouse
    widget->resize(200, 200);
    EventSpy widgetGrabEventSpy(widget, QEvent::GrabMouse);
    EventSpy widgetUngrabEventSpy(widget, QEvent::UngrabMouse);
    scene.addItem(widget);

    QVERIFY(!scene.mouseGrabberItem());

    // Click on an item, see if gain and lose implicit mouse grab.
    {
        QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
        event.ignore();
        event.setButton(Qt::LeftButton);
        event.setScenePos(QPointF(50, 50));
        qApp->sendEvent(&scene, &event);
    }
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget);
    QCOMPARE(widgetGrabEventSpy.count(), 1);
    {
        QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease);
        event.ignore();
        event.setButton(Qt::LeftButton);
        event.setScenePos(QPointF(50, 50));
        qApp->sendEvent(&scene, &event);
    }
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
    QCOMPARE(widgetGrabEventSpy.count(), 1);
    QCOMPARE(widgetUngrabEventSpy.count(), 1);

    // Click on an item that already grabs the mouse. Shouldn't have any effect.
    widget->grabMouse();
    QCOMPARE(widgetGrabEventSpy.count(), 2);
    {
        QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
        event.ignore();
        event.setButton(Qt::LeftButton);
        event.setScenePos(QPointF(50, 50));
        qApp->sendEvent(&scene, &event);
    }
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget);
    QCOMPARE(widgetGrabEventSpy.count(), 2);
    {
        QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease);
        event.ignore();
        event.setButton(Qt::LeftButton);
        event.setScenePos(QPointF(50, 50));
        qApp->sendEvent(&scene, &event);
    }
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget);
    QCOMPARE(widgetGrabEventSpy.count(), 2);
    QCOMPARE(widgetUngrabEventSpy.count(), 1);
    widget->ungrabMouse();
    QCOMPARE(widgetUngrabEventSpy.count(), 2);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);

    // Implicit mouse grabber tries to explicitly grab the mouse
    {
        QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
        event.ignore();
        event.setButton(Qt::LeftButton);
        event.setScenePos(QPointF(50, 50));
        qApp->sendEvent(&scene, &event);
    }
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget);
    QCOMPARE(widgetGrabEventSpy.count(), 3);
    widget->grabMouse();
    QCOMPARE(widgetUngrabEventSpy.count(), 2);
    {
        QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease);
        event.ignore();
        event.setButton(Qt::LeftButton);
        event.setScenePos(QPointF(50, 50));
        qApp->sendEvent(&scene, &event);
    }
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget);
    QCOMPARE(widgetGrabEventSpy.count(), 3);
    QCOMPARE(widgetUngrabEventSpy.count(), 2);
    widget->ungrabMouse();
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
    QCOMPARE(widgetGrabEventSpy.count(), 3);
    QCOMPARE(widgetUngrabEventSpy.count(), 3);

    // Arrival of a new widget
    QGraphicsWidget *widget2 = new QGraphicsWidget;
    widget2->setFlag(QGraphicsItem::ItemIsMovable); // can grab mouse
    widget2->resize(200, 200);
    widget2->setPos(205, 0);
    EventSpy widget2GrabEventSpy(widget2, QEvent::GrabMouse);
    EventSpy widget2UngrabEventSpy(widget2, QEvent::UngrabMouse);
    scene.addItem(widget2);

    // Implicit grab while there's an explicit grab is not possible.
    widget->grabMouse();
    QCOMPARE(widgetGrabEventSpy.count(), 4);
    {
        QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
        event.ignore();
        event.setButton(Qt::LeftButton);
        event.setScenePos(QPointF(250, 50));
        qApp->sendEvent(&scene, &event);
    }
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget);
    QCOMPARE(widgetGrabEventSpy.count(), 4);
    QCOMPARE(widget2GrabEventSpy.count(), 0);
    QCOMPARE(widget2UngrabEventSpy.count(), 0);

    scene.removeItem(widget);
    QCOMPARE(widgetUngrabEventSpy.count(), 4);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
}

class GrabOnPressItem : public QGraphicsRectItem
{
public:
    GrabOnPressItem(const QRectF &rect)
        : QGraphicsRectItem(rect),
          npress(0), nrelease(0), ndoubleClick(0),
          ngrab(0), nungrab(0)
    {
    }
    int npress;
    int nrelease;
    int ndoubleClick;
    int ngrab;
    int nungrab;
protected:
    bool sceneEvent(QEvent *event)
    {
        switch (event->type()) {
        case QEvent::GrabMouse:
            ++ngrab;
            break;
        case QEvent::UngrabMouse:
            ++nungrab;
            break;
        default:
            break;
        }
        return QGraphicsRectItem::sceneEvent(event);
    }

    void mousePressEvent(QGraphicsSceneMouseEvent *)
    {
        grabMouse();
        ++npress;
    }
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *)
    {
        ungrabMouse();
        ++nrelease;
    }
    void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *)
    {
        ++ndoubleClick;
    }
};

void tst_QGraphicsWidget::doubleClickAfterExplicitMouseGrab()
{
    QGraphicsScene scene;
    GrabOnPressItem *item = new GrabOnPressItem(QRectF(0, 0, 100, 100));
    scene.addItem(item);

    {
        QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
        event.setButton(Qt::LeftButton);
        event.setButtons(Qt::LeftButton);
        event.ignore();
        event.setScenePos(QPointF(50, 50));
        qApp->sendEvent(&scene, &event);
    }
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)item);
    QCOMPARE(item->npress, 1);
    QCOMPARE(item->ngrab, 1);
    {
        QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease);
        event.setButton(Qt::LeftButton);
        event.setButtons(0);
        event.ignore();
        event.setScenePos(QPointF(50, 50));
        qApp->sendEvent(&scene, &event);
    }
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
    QCOMPARE(item->nrelease, 1);
    QCOMPARE(item->nungrab, 1);
    {
        QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseDoubleClick);
        event.setButton(Qt::LeftButton);
        event.setButtons(Qt::LeftButton);
        event.ignore();
        event.setScenePos(QPointF(50, 50));
        qApp->sendEvent(&scene, &event);
    }
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)item);
    QCOMPARE(item->ndoubleClick, 1);
    QCOMPARE(item->ngrab, 2);
    {
        QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease);
        event.setButton(Qt::LeftButton);
        event.setButtons(0);
        event.ignore();
        event.setScenePos(QPointF(50, 50));
        qApp->sendEvent(&scene, &event);
    }
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);
    QCOMPARE(item->nrelease, 2);
    QCOMPARE(item->nungrab, 2);
}

void tst_QGraphicsWidget::popupMouseGrabber()
{
    QGraphicsScene scene;
    QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Popup);
    widget->setFlag(QGraphicsItem::ItemIsMovable); // can grab mouse
    widget->resize(200, 200);
    EventSpy widgetGrabEventSpy(widget, QEvent::GrabMouse);
    EventSpy widgetUngrabEventSpy(widget, QEvent::UngrabMouse);

    // Simply adding a visible popup to the scene immediately grabs the mouse.
    scene.addItem(widget);
    QCOMPARE(widgetGrabEventSpy.count(), 1);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget);

    // Hiding it loses the grab again.
    widget->hide();
    QCOMPARE(widgetUngrabEventSpy.count(), 1);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)0);

    // Showing it grabs the mosue again
    widget->show();
    QCOMPARE(widgetGrabEventSpy.count(), 2);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget);

    // Add two popups
    QGraphicsWidget *widget2 = new QGraphicsWidget(0, Qt::Popup);
    widget2->setFlag(QGraphicsItem::ItemIsMovable); // can grab mouse
    widget2->resize(200, 200);
    EventSpy widget2GrabEventSpy(widget2, QEvent::GrabMouse);
    EventSpy widget2UngrabEventSpy(widget2, QEvent::UngrabMouse);
    QGraphicsWidget *widget3 = new QGraphicsWidget(0, Qt::Popup);
    widget3->setFlag(QGraphicsItem::ItemIsMovable); // can grab mouse
    widget3->resize(200, 200);
    EventSpy widget3GrabEventSpy(widget3, QEvent::GrabMouse);
    EventSpy widget3UngrabEventSpy(widget3, QEvent::UngrabMouse);

    // Adding to the scene grabs
    scene.addItem(widget2);
    QCOMPARE(widgetUngrabEventSpy.count(), 2);
    QCOMPARE(widget2GrabEventSpy.count(), 1);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget2);

    // Adding to the scene grabs again
    scene.addItem(widget3);
    QCOMPARE(widget2UngrabEventSpy.count(), 1);
    QCOMPARE(widget3GrabEventSpy.count(), 1);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget3);

    // Hiding the topmost widget causes widget 2 to regain grab.
    widget3->hide();
    QCOMPARE(widget2GrabEventSpy.count(), 2);
    QCOMPARE(widget3UngrabEventSpy.count(), 1);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget2);
    widget3->show();
    QCOMPARE(widget2UngrabEventSpy.count(), 2);
    QCOMPARE(widget3GrabEventSpy.count(), 2);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget3);

    // Clicking outside the popup still causes it to close (despite that it's
    // an explicit mouse grabber).
    {
        QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
        event.ignore();
        event.setButton(Qt::LeftButton);
        event.setScenePos(QPointF(500, 500)); // outside
        qApp->sendEvent(&scene, &event);
    }
    QVERIFY(!widget3->isVisible());
    QCOMPARE(widget3UngrabEventSpy.count(), 2);
    QCOMPARE(widget2GrabEventSpy.count(), 3);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget2);
    QVERIFY(widget2->isVisible());
    QVERIFY(widget->isVisible());
    widget3->show();
    QCOMPARE(widget3GrabEventSpy.count(), 3);
    QCOMPARE(widget2UngrabEventSpy.count(), 3);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget3);

    // This is something of a curiosity. What happens if you call
    // ungrabMouse() on a popup? The answer is - it loses the grab. If you
    // hide and show the popup again, it will regain the grab.
    widget3->ungrabMouse();
    QCOMPARE(widget3UngrabEventSpy.count(), 3);
    QCOMPARE(widget2GrabEventSpy.count(), 4);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget2);
    widget3->hide();
    widget3->show();
    QCOMPARE(widget3GrabEventSpy.count(), 4);
    QCOMPARE(widget2UngrabEventSpy.count(), 4);
    QCOMPARE(scene.mouseGrabberItem(), (QGraphicsItem *)widget3);
}

void tst_QGraphicsWidget::windowFlags_data()
{
    QTest::addColumn<int>("inputFlags");
    QTest::addColumn<int>("outputFlags");

    QTest::newRow("nil") << 0 << 0;

    // Window types
    QTest::newRow("Qt::Window") << int(Qt::Window)
                                << int(Qt::Window | Qt::WindowTitleHint | Qt::WindowSystemMenuHint
                                       | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
    QTest::newRow("Qt::SubWindow") << int(Qt::SubWindow)
                                   << int(Qt::SubWindow | Qt::WindowTitleHint | Qt::WindowSystemMenuHint
                                          | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
    QTest::newRow("Qt::Dialog") << int(Qt::Dialog)
                                << int(Qt::Dialog | Qt::WindowTitleHint | Qt::WindowSystemMenuHint
                                       | Qt::WindowContextHelpButtonHint);
    QTest::newRow("Qt::Sheet") << int(Qt::Sheet)
                               << int(Qt::Sheet | Qt::WindowTitleHint | Qt::WindowSystemMenuHint
                                      | Qt::WindowContextHelpButtonHint);
    QTest::newRow("Qt::Tool") << int(Qt::Tool)
                              << int(Qt::Tool | Qt::WindowTitleHint | Qt::WindowSystemMenuHint);

    // Custom window flags
    QTest::newRow("Qt::FramelessWindowHint") << int(Qt::FramelessWindowHint)
                                             << int(Qt::FramelessWindowHint);
    QTest::newRow("Qt::CustomizeWindowHint") << int(Qt::CustomizeWindowHint)
                                             << int(Qt::CustomizeWindowHint);
}

void tst_QGraphicsWidget::windowFlags()
{
    QFETCH(int, inputFlags);
    QFETCH(int, outputFlags);

    // Construct with flags set already
    QGraphicsWidget widget(0, Qt::WindowFlags(inputFlags));
    QCOMPARE(widget.windowFlags(), Qt::WindowFlags(outputFlags));

    // Set flags after construction
    QGraphicsWidget widget2;
    widget2.setWindowFlags(Qt::WindowFlags(inputFlags));
    QCOMPARE(widget2.windowFlags(), Qt::WindowFlags(outputFlags));

    // Reset flags
    widget2.setWindowFlags(0);
    QVERIFY(!widget2.windowFlags());

    // Set flags back again
    widget2.setWindowFlags(Qt::WindowFlags(inputFlags));
    QCOMPARE(widget2.windowFlags(), Qt::WindowFlags(outputFlags));

    // Construct with custom flags set already
    QGraphicsWidget widget3(0, Qt::WindowFlags(inputFlags | Qt::FramelessWindowHint));
    QCOMPARE(widget3.windowFlags(), Qt::WindowFlags(inputFlags | Qt::FramelessWindowHint));

    // Set custom flags after construction
    QGraphicsWidget widget4;
    widget4.setWindowFlags(Qt::WindowFlags(inputFlags | Qt::FramelessWindowHint));
    QCOMPARE(widget4.windowFlags(), Qt::WindowFlags(inputFlags | Qt::FramelessWindowHint));

    // Reset flags
    widget4.setWindowFlags(0);
    QVERIFY(!widget4.windowFlags());

    // Set custom flags back again
    widget4.setWindowFlags(Qt::WindowFlags(inputFlags | Qt::FramelessWindowHint));
    QCOMPARE(widget4.windowFlags(), Qt::WindowFlags(inputFlags | Qt::FramelessWindowHint));

    QGraphicsWidget *widget5 = new QGraphicsWidget;
    widget5->setWindowFlags(Qt::WindowFlags(inputFlags));
    QCOMPARE(widget5->windowFlags(), Qt::WindowFlags(outputFlags));
    QGraphicsWidget window(0, Qt::Window);
    widget5->setParentItem(&window);
    QCOMPARE(widget5->windowFlags(), Qt::WindowFlags(outputFlags));
}

void tst_QGraphicsWidget::shortcutsDeletion()
{
    QGraphicsWidget *widget = new QGraphicsWidget;
    QGraphicsWidget *widget2 = new QGraphicsWidget;
    widget->setMinimumSize(40, 40);
    QWidgetAction *del = new QWidgetAction(widget);
    del->setIcon(QIcon("edit-delete"));
    del->setShortcut(Qt::Key_Delete);
    del->setShortcutContext(Qt::WidgetShortcut);
    widget2->addAction(del);
    widget2->addAction(del);
    delete widget;
}

class MessUpPainterWidget : public QGraphicsWidget
{
public:
    MessUpPainterWidget(QGraphicsItem * parent = 0, Qt::WindowFlags wFlags = 0)
    : QGraphicsWidget(parent, wFlags)
    {}

    void paintWindowFrame(QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        QCOMPARE(painter->opacity(), 1.0);
        painter->setOpacity(0.0);
        QGraphicsWidget::paintWindowFrame(painter, option, widget);
    }
    void paint(QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        QCOMPARE(painter->opacity(), 1.0);
        painter->drawRect(0, 0, 100, 100);
        QGraphicsWidget::paint(painter, option, widget);
    }

};

void tst_QGraphicsWidget::painterStateProtectionOnWindowFrame()
{
    MessUpPainterWidget *widget = new MessUpPainterWidget(0, Qt::Window);
    QGraphicsScene scene(0, 0, 300, 300);
    QGraphicsView view(&scene);
    scene.addItem(widget);
    view.show();
    QTest::qWaitForWindowShown(&view);
    QTest::qWait(500);
}

class ProxyStyle : public QCommonStyle
{
public:
    ProxyStyle(QStyle *proxyStyle) : QCommonStyle()
    {
        m_proxyStyle = proxyStyle;
    }

    int pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) const
    {
        return m_proxyStyle->pixelMetric(metric, option, widget);
    }

private:
    QStyle *m_proxyStyle;
};

class StyledGraphicsWidget : public QGraphicsWidget
{
public:
    StyledGraphicsWidget(bool useOwnStyle) : QGraphicsWidget(), m_style(0) {
        if (useOwnStyle) {
            QStyle *oldStyle = style();
            m_style = new ProxyStyle(oldStyle);
            setStyle(m_style);
        }

        style()->pixelMetric(QStyle::PM_SmallIconSize); // crash when style() is still in widgetStyles
    }

    ~StyledGraphicsWidget() {
        delete m_style;
    }

private:
    QStyle *m_style;
};

void tst_QGraphicsWidget::task243004_setStyleCrash()
{
    QGraphicsItem *item1 = new StyledGraphicsWidget(true);
    delete item1; // item1 not removed from widgetStyles

    QGraphicsItem *item2 = new StyledGraphicsWidget(false);
    delete item2;
}

class GraphicsWidget_task250119 : public QGraphicsWidget
{
public:
    GraphicsWidget_task250119()
        : shortcutEvents(0)
    {
        setFocusPolicy(Qt::StrongFocus);
        resize(100, 100);
    }

    int shortcutEvents;

private:
    bool event(QEvent *event)
    {
        if (event->type() == QEvent::Shortcut)
            shortcutEvents++;
        return QGraphicsWidget::event(event);
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    {
        if (hasFocus()) {
            painter->setPen(QPen(Qt::black, 0, Qt::DashLine));
            painter->drawRect(rect());
        }
        painter->setPen(QPen(Qt::black, 0, Qt::SolidLine));
        painter->fillRect(rect().adjusted(2, 2, -2, -2), Qt::yellow);
        painter->drawRect(rect().adjusted(2, 2, -2, -2));
    }
};

void tst_QGraphicsWidget::task250119_shortcutContext()
{
    QGraphicsScene scene;
    QGraphicsView view;
    view.setScene(&scene);
    view.show();
    QApplication::setActiveWindow(&view);
    QTest::qWait(25);
    QTRY_COMPARE(QApplication::activeWindow(), &view);


    // *** Event: ***

    GraphicsWidget_task250119 w_event;
    scene.addItem(&w_event);

    const int id = w_event.grabShortcut(Qt::Key_A, Qt::WidgetWithChildrenShortcut);
    w_event.setShortcutEnabled(id, true);

    w_event.setFocus();
    QTest::keyPress(&view, Qt::Key_A);
    QCOMPARE(w_event.shortcutEvents, 1);

    w_event.clearFocus();
    QTest::keyPress(&view, Qt::Key_A);
    QCOMPARE(w_event.shortcutEvents, 1);

    scene.removeItem(&w_event);


    // *** Signal: ***

    GraphicsWidget_task250119 w_signal;
    scene.addItem(&w_signal);

    QAction action(0);
    action.setShortcut(Qt::Key_B);
    action.setShortcutContext(Qt::WidgetWithChildrenShortcut);
    QSignalSpy spy(&action, SIGNAL(triggered()));

    w_signal.addAction(&action);

    w_signal.setFocus();
    QTest::keyPress(&view, Qt::Key_B);
    QCOMPARE(spy.count(), 1);

    w_signal.clearFocus();
    QTest::keyPress(&view, Qt::Key_B);
    QCOMPARE(spy.count(), 1);

    scene.removeItem(&w_signal);
}

class ClippingAndTransformsScene : public QGraphicsScene
{
public:
    QList<QGraphicsItem *> drawnItems;
protected:
    void drawItems(QPainter *painter, int numItems, QGraphicsItem *items[],
                   const QStyleOptionGraphicsItem options[], QWidget *widget = 0)
    {
        drawnItems.clear();
        for (int i = 0; i < numItems; ++i)
            drawnItems << items[i];
        QGraphicsScene::drawItems(painter, numItems, items, options, widget);
    }
};

class RectWidget : public QGraphicsWidget
{
public:

    RectWidget(Qt::GlobalColor color, QGraphicsItem *parent=0) : QGraphicsWidget(parent), mColor(color) {}

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    {
        painter->setBrush(QBrush(mColor));
        painter->drawRect(boundingRect());
    }

    Qt::GlobalColor mColor;
};

class RectItem : public QGraphicsItem
{
public:

    RectItem(Qt::GlobalColor color, QGraphicsItem *parent=0) : QGraphicsItem(parent), mColor(color) {}

    QRectF boundingRect() const
    {return QRectF(10,10,50,50);}

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    {
        painter->setBrush(QBrush(mColor));
        painter->drawRect(boundingRect());
    }

    Qt::GlobalColor mColor;
};

void tst_QGraphicsWidget::ensureClipping()
{
    ClippingAndTransformsScene scene;
    scene.setSceneRect(-50, -50, 200, 200);

    //A root that clip children
    RectWidget *clipWidget = new RectWidget(Qt::black);
    scene.addItem(clipWidget);

    clipWidget->setFlag(QGraphicsItem::ItemClipsChildrenToShape);

    //a child
    RectWidget *childWidget = new RectWidget(Qt::red, clipWidget);
    clipWidget->setGeometry(QRectF(10, 10, 100, 100));
    childWidget->setGeometry(QRectF(25, 25, 50, 50));

    //We put a QGraphicsItem to be sure this one is also paint
    RectItem *childitem = new RectItem(Qt::blue, clipWidget);

    QGraphicsView view(&scene);
    view.setOptimizationFlag(QGraphicsView::IndirectPainting);
    view.show();
    QTest::qWaitForWindowShown(&view);

    QList<QGraphicsItem *> expected;
    expected << clipWidget << childWidget << childitem;
    QTRY_VERIFY(scene.drawnItems.contains(clipWidget));
    QVERIFY(scene.drawnItems.contains(childWidget));
    QVERIFY(scene.drawnItems.contains(childitem));
}

class ItemChangeTester : public QGraphicsWidget
{
public:
    ItemChangeTester()
    { setFlag(ItemSendsGeometryChanges); clear(); }
    ItemChangeTester(QGraphicsItem *parent) : QGraphicsWidget(parent)
    { setFlag(ItemSendsGeometryChanges); clear(); }

    void clear()
    {
        changes.clear();
        values.clear();
        oldValues.clear();
    }
    QList<GraphicsItemChange> changes;
    QList<QVariant> values;
    QList<QVariant> oldValues;
protected:
    QVariant itemChange(GraphicsItemChange change, const QVariant &value)
    {
        changes << change;
        values << value;
        switch (change) {
        case QGraphicsItem::ItemPositionChange:
            oldValues << pos();
            break;
        case QGraphicsItem::ItemPositionHasChanged:
            break;
        default:
            break;
        }
        return value;
    }
};

void tst_QGraphicsWidget::widgetSendsGeometryChanges()
{
    ItemChangeTester widget;
    widget.setFlags(0);
    widget.clear();

    QPointF pos(10, 10);
    widget.setPos(pos);

    QCOMPARE(widget.pos(), pos);
    QCOMPARE(widget.changes.size(), 0);

    widget.setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    QCOMPARE(widget.changes.size(), 2);

    widget.setPos(QPointF());
    QCOMPARE(widget.changes.size(), 4);

    QCOMPARE(widget.pos(), QPointF());

    QRectF geometry(20, 20, 50, 50);
    widget.setGeometry(geometry);
    QCOMPARE(widget.changes.size(), 6);

    QCOMPARE(widget.geometry(), geometry);

    QCOMPARE(widget.changes, QList<QGraphicsItem::GraphicsItemChange>()
         << QGraphicsItem::ItemFlagsChange
         << QGraphicsItem::ItemFlagsHaveChanged
         << QGraphicsItem::ItemPositionChange
         << QGraphicsItem::ItemPositionHasChanged
         << QGraphicsItem::ItemPositionChange
         << QGraphicsItem::ItemPositionHasChanged);
}

class HFWWidget : public QGraphicsWidget
{
public:
    HFWWidget() : QGraphicsWidget(0, Qt::Window)
    {
        QSizePolicy sp;
        sp.setHeightForWidth(true);
        setSizePolicy(sp);
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        Q_UNUSED(option);
        Q_UNUSED(widget);
        qreal w = rect().width();
        QRectF box(0, 0, w, 2400/w);
        painter->drawRoundRect(box);
        painter->drawLine(box.topLeft(), box.bottomRight());
        painter->drawLine(box.bottomLeft(), box.topRight());
    }

protected:
    QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const
    {
        qreal w = constraint.width();
        switch (which) {
        case Qt::MinimumSize:
            if (w >= 0 && constraint.height() < 0) {
                // keep the same area of 60x40 = 2400
                return QSizeF(w, 2400.0/w);
            } else {
                return QSizeF(10, 10);
            }
            break;
        case Qt::PreferredSize:
            return QSizeF(48.989794, 48.989794);
            default:
            break;
        }
        return QGraphicsWidget::sizeHint(which, constraint);
    }
};

void tst_QGraphicsWidget::respectHFW()
{
#if defined(Q_OS_WINCE) || defined(Q_OS_MAC) || defined(Q_WS_QWS)
    qDebug("This test is platform dependent, it fails on wince, mac and qws. Please fix.");
#else
    QGraphicsScene scene;
    HFWWidget *window = new HFWWidget;
    scene.addItem(window);
    QGraphicsView *view = new QGraphicsView(&scene);
    view->resize(400, 400);
    view->setSceneRect(-100, -100, 300,300);

    view->show();
    window->setGeometry(0, 0, 70, 70);
    QTest::qWaitForWindowShown(view);

    {   // here we go - simulate a interactive resize of the window
        QTest::qWait(100);
        QTest::mouseMove(view, view->mapFromScene(71, 71)); // bottom right corner
        QTest::qWait(100);

        QTest::mousePress(view->viewport(), Qt::LeftButton, 0, view->mapFromScene(71, 71), 200);
        view->grabMouse();
        // move both mouse cursor and set correct event in order to emulate resize
        QTest::mouseMove(view->viewport(), view->mapFromScene(60, 30), 200);
        QMouseEvent e = QMouseEvent(QEvent::MouseMove,
                      view->mapFromScene(60, 20),
                      Qt::NoButton,
                      Qt::LeftButton,
                      Qt::NoModifier);
        QApplication::sendEvent(view->viewport(), &e);
        view->releaseMouse();
    }
    QTest::qWait(100);
    const QSizeF winSize = window->size();
    qreal minHFW = window->effectiveSizeHint(Qt::MinimumSize, QSizeF(winSize.width(), -1)).height();
    QVERIFY(qAbs(minHFW - winSize.height()) < 1);
#endif
}

class PolishWidget : public QGraphicsWidget
{
public:

    PolishWidget(Qt::GlobalColor color, QGraphicsItem *parent=0) :
    QGraphicsWidget(parent), mColor(color)
    {
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    {
        painter->setBrush(QBrush(mColor));
        painter->drawRect(boundingRect());
    }

    void polishEvent()
    {
        if (!parentWidget()) {
            //We add a child in the polish event for the parent
            PolishWidget *childWidget = new PolishWidget(Qt::black, this);
            childWidget->setGeometry(QRectF(10,10,30,30));
        }

        QGraphicsWidget::polishEvent();
        mColor = Qt::red;
        update();
        numberOfPolish++;
    }

    static int numberOfPolish;

private:
    Qt::GlobalColor mColor;
};

int PolishWidget::numberOfPolish = 0;

void tst_QGraphicsWidget::addChildInpolishEvent()
{
    QGraphicsScene scene;

    PolishWidget *parentWidget = new PolishWidget(Qt::white);
    scene.addItem(parentWidget);

    QGraphicsView view(&scene);
    view.resize(200, 200);
    view.show();
    QTest::qWaitForWindowShown(&view);
    QCOMPARE(PolishWidget::numberOfPolish, 2);
}

void tst_QGraphicsWidget::polishEvent()
{
    class MyGraphicsWidget : public QGraphicsWidget
    { public:
        void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *)
        { events << QEvent::Paint; }
        void polishEvent()
        { events << QEvent::Polish; }
        QList<QEvent::Type> events;
    };

    QGraphicsScene scene;

    MyGraphicsWidget *widget = new MyGraphicsWidget;
    scene.addItem(widget);

    QGraphicsView view(&scene);
    view.show();
    QTest::qWaitForWindowShown(&view);

    // Make sure the item is painted.
    QTRY_VERIFY(widget->events.contains(QEvent::Paint));

    // Make sure the item got polish before paint.
    QCOMPARE(widget->events.at(0), QEvent::Polish);
}

void tst_QGraphicsWidget::polishEvent2()
{
    class MyGraphicsWidget : public QGraphicsWidget
    { public:
    void polishEvent()
    { events << QEvent::Polish; }
    QList<QEvent::Type> events;
    };

    QGraphicsScene scene;

    MyGraphicsWidget *widget = new MyGraphicsWidget;
    widget->hide();
    scene.addItem(widget);

    widget->events.clear();

    QApplication::processEvents();

    // Make sure the item got polish event.
    QVERIFY(widget->events.contains(QEvent::Polish));
}

void tst_QGraphicsWidget::autoFillBackground()
{
    QGraphicsWidget *widget = new QGraphicsWidget;
    QCOMPARE(widget->autoFillBackground(), false);
    widget->setAutoFillBackground(true);
    QCOMPARE(widget->autoFillBackground(), true);

    const QColor color(Qt::red);
    const QRect rect(0, 0, 1, 1);

    QGraphicsScene scene;
    scene.addItem(widget);
    widget->setGeometry(rect);

    QPalette palette = widget->palette();
    palette.setColor(QPalette::Window, color);
    widget->setPalette(palette);

    QImage image(rect.size(), QImage::Format_RGB32);
    QPainter painter;
    painter.begin(&image);
    scene.render(&painter, rect, rect);
    painter.end();
    QCOMPARE(image.pixel(0, 0), color.rgb());
}

void tst_QGraphicsWidget::initialShow()
{
    class MyGraphicsWidget : public QGraphicsWidget
    { public:
        MyGraphicsWidget() : repaints(0) {}
        int repaints;
        void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget*) { ++repaints; }
        void polishEvent() { update(); }
    };

    QGraphicsScene scene;
    MyGraphicsWidget *widget = new MyGraphicsWidget;

    QGraphicsView view(&scene);
    view.show();
    QTest::qWaitForWindowShown(&view);

    QTest::qWait(100);
    scene.addItem(widget);
    QTest::qWait(100);

    QCOMPARE(widget->repaints, 1);
}

void tst_QGraphicsWidget::QT_BUG_6544_tabFocusFirstUnsetWhenRemovingItems()
{
    QGraphicsScene scene;
    QGraphicsWidget* parent1 = new QGraphicsWidget;
    QGraphicsWidget* child1_0 = new QGraphicsWidget;
    QGraphicsWidget* child1_1 = new QGraphicsWidget;

    QGraphicsWidget* parent2 = new QGraphicsWidget;

    // Add the parent and child to the scene.
    scene.addItem(parent1);
    child1_0->setParentItem(parent1);
    child1_1->setParentItem(parent1);

    // Hide and show the child.
    child1_0->setParentItem(NULL);
    scene.removeItem(child1_0);

    // Remove parent from the scene.
    scene.removeItem(parent1);

    delete child1_0;
    delete child1_1;
    delete parent1;

    // Add an item into the scene.
    scene.addItem(parent2);

    //This should not crash
}

QTEST_MAIN(tst_QGraphicsWidget)
#include "tst_qgraphicswidget.moc"

#else // QT_NO_STYLE_CLEANLOOKS
QTEST_NOOP_MAIN
#endif