summaryrefslogtreecommitdiffstats
path: root/src/table/qtable.cpp
blob: 4695d09acefee95755845e38eb038e9b97c866bb (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
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
/****************************************************************************
**
** Implementation of TQTable widget class
**
** Created : 000607
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of the table module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at [email protected].
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file.  Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

#include "ntqglobal.h"
#if defined(Q_CC_BOR)
// needed for qsort() because of a std namespace problem on Borland
#include "qplatformdefs.h"
#endif

#include "ntqtable.h"

#ifndef QT_NO_TABLE

#include <ntqpainter.h>
#include <ntqlineedit.h>
#include <ntqcursor.h>
#include <ntqapplication.h>
#include <ntqtimer.h>
#include <ntqobjectlist.h>
#include <ntqiconset.h>
#include <ntqcombobox.h>
#include <ntqcheckbox.h>
#include <ntqdragobject.h>
#include <ntqevent.h>
#include <ntqlistbox.h>
#include <ntqstyle.h>
#include <ntqdatatable.h>
#include <ntqvalidator.h>

#include <stdlib.h>
#include <limits.h>

static bool qt_update_cell_widget = TRUE;
static bool qt_table_clipper_enabled = TRUE;
#ifndef QT_INTERNAL_TABLE
Q_EXPORT
#endif
void qt_set_table_clipper_enabled( bool enabled )
{
    qt_table_clipper_enabled = enabled;
}

class TQM_EXPORT_TABLE TQTableHeader : public TQHeader
{
    friend class TQTable;
    Q_OBJECT

public:
    enum SectionState {
	Normal,
	Bold,
	Selected
    };

    TQTableHeader( int, TQTable *t, TQWidget* parent=0, const char* name=0 );
    ~TQTableHeader() {};
    void addLabel( const TQString &s, int size );
    void setLabel( int section, const TQString & s, int size = -1 );
    void setLabel( int section, const TQIconSet & iconset, const TQString & s,
		   int size = -1 );

    void setLabels(const TQStringList & labels);

    void removeLabel( int section );

    void setSectionState( int s, SectionState state );
    void setSectionStateToAll( SectionState state );
    SectionState sectionState( int s ) const;

    int sectionSize( int section ) const;
    int sectionPos( int section ) const;
    int sectionAt( int section ) const;

    void setSectionStretchable( int s, bool b );
    bool isSectionStretchable( int s ) const;

    void updateCache();

signals:
    void sectionSizeChanged( int s );

protected:
    void paintEvent( TQPaintEvent *e );
    void paintSection( TQPainter *p, int index, const TQRect& fr );
    void mousePressEvent( TQMouseEvent *e );
    void mouseMoveEvent( TQMouseEvent *e );
    void mouseReleaseEvent( TQMouseEvent *e );
    void mouseDoubleClickEvent( TQMouseEvent *e );
    void resizeEvent( TQResizeEvent *e );

private slots:
    void doAutoScroll();
    void sectionWidthChanged( int col, int os, int ns );
    void indexChanged( int sec, int oldIdx, int newIdx );
    void updateStretches();
    void updateWidgetStretches();

private:
    void updateSelections();
    void saveStates();
    void setCaching( bool b );
    void swapSections( int oldIdx, int newIdx, bool swapTable = TRUE );
    bool doSelection( TQMouseEvent *e );
    void sectionLabelChanged( int section );
    void resizeArrays( int n );

private:
    TQMemArray<int> states, oldStates;
    TQMemArray<bool> stretchable;
    TQMemArray<int> sectionSizes, sectionPoses;
    bool mousePressed;
    int pressPos, startPos, endPos;
    TQTable *table;
    TQTimer *autoScrollTimer;
    TQWidget *line1, *line2;
    bool caching;
    int resizedSection;
    bool isResizing;
    int numStretches;
    TQTimer *stretchTimer, *widgetStretchTimer;
    TQTableHeaderPrivate *d;

};

#ifdef _WS_QWS_
# define NO_LINE_WIDGET
#endif



struct TQTablePrivate
{
    TQTablePrivate() : hasRowSpan( FALSE ), hasColSpan( FALSE ),
		      inMenuMode( FALSE ), redirectMouseEvent( FALSE )
    {
	hiddenRows.setAutoDelete( TRUE );
	hiddenCols.setAutoDelete( TRUE );
    }
    uint hasRowSpan : 1;
    uint hasColSpan : 1;
    uint inMenuMode : 1;
    uint redirectMouseEvent : 1;
    TQIntDict<int> hiddenRows, hiddenCols;
    TQTimer *geomTimer;
    int lastVisRow;
    int lastVisCol;
};

struct TQTableHeaderPrivate
{
#ifdef NO_LINE_WIDGET
    int oldLinePos;
#endif
};

static bool isRowSelection( TQTable::SelectionMode selMode )
{
    return selMode == TQTable::SingleRow || selMode == TQTable::MultiRow;
}

/*!
    \class TQTableSelection
    \brief The TQTableSelection class provides access to a selected area in a
    TQTable.
\if defined(commercial)
    It is part of the <a href="commercialeditions.html">TQt Enterprise Edition</a>.
\endif

    \ingroup advanced
    \module table

    The selection is a rectangular set of cells in a TQTable. One of
    the rectangle's cells is called the anchor cell; this is the cell
    that was selected first. The init() function sets the anchor and
    the selection rectangle to exactly this cell; the expandTo()
    function expands the selection rectangle to include additional
    cells.

    There are various access functions to find out about the area:
    anchorRow() and anchorCol() return the anchor's position;
    leftCol(), rightCol(), topRow() and bottomRow() return the
    rectangle's four edges. All four are part of the selection.

    A newly created TQTableSelection is inactive -- isActive() returns
    FALSE. You must use init() and expandTo() to activate it.

    \sa TQTable TQTable::addSelection() TQTable::selection()
    TQTable::selectCells() TQTable::selectRow() TQTable::selectColumn()
*/

/*!
    Creates an inactive selection. Use init() and expandTo() to
    activate it.
*/

TQTableSelection::TQTableSelection()
    : active( FALSE ), inited( FALSE ), tRow( -1 ), lCol( -1 ),
      bRow( -1 ), rCol( -1 ), aRow( -1 ), aCol( -1 )
{
}

/*!
    Creates an active selection, starting at \a start_row and \a
    start_col, ending at \a end_row and \a end_col.
*/

TQTableSelection::TQTableSelection( int start_row, int start_col, int end_row, int end_col )
    : active( FALSE ), inited( FALSE ), tRow( -1 ), lCol( -1 ),
      bRow( -1 ), rCol( -1 ), aRow( -1 ), aCol( -1 )
{
    init( start_row, start_col );
    expandTo( end_row, end_col );
}

/*!
    Sets the selection anchor to cell \a row, \a col and the selection
    to only contain this cell. The selection is not active until
    expandTo() is called.

    To extend the selection to include additional cells, call
    expandTo().

    \sa isActive()
*/

void TQTableSelection::init( int row, int col )
{
    aCol = lCol = rCol = col;
    aRow = tRow = bRow = row;
    active = FALSE;
    inited = TRUE;
}

/*!
    Expands the selection to include cell \a row, \a col. The new
    selection rectangle is the bounding rectangle of \a row, \a col
    and the previous selection rectangle. After calling this function
    the selection is active.

    If you haven't called init(), this function does nothing.

    \sa init() isActive()
*/

void TQTableSelection::expandTo( int row, int col )
{
    if ( !inited )
	return;
    active = TRUE;

    if ( row < aRow ) {
	tRow = row;
	bRow = aRow;
    } else {
	tRow = aRow;
	bRow = row;
    }

    if ( col < aCol ) {
	lCol = col;
	rCol = aCol;
    } else {
	lCol = aCol;
	rCol = col;
    }
}

/*!
    Returns TRUE if \a s includes the same cells as the selection;
    otherwise returns FALSE.
*/

bool TQTableSelection::operator==( const TQTableSelection &s ) const
{
    return ( s.active == active &&
	     s.tRow == tRow && s.bRow == bRow &&
	     s.lCol == lCol && s.rCol == rCol );
}

/*!
    \fn bool TQTableSelection::operator!=( const TQTableSelection &s ) const

    Returns TRUE if \a s does not include the same cells as the
    selection; otherwise returns FALSE.
*/


/*!
    \fn int TQTableSelection::topRow() const

    Returns the top row of the selection.

    \sa bottomRow() leftCol() rightCol()
*/

/*!
    \fn int TQTableSelection::bottomRow() const

    Returns the bottom row of the selection.

    \sa topRow() leftCol() rightCol()
*/

/*!
    \fn int TQTableSelection::leftCol() const

    Returns the left column of the selection.

    \sa topRow() bottomRow() rightCol()
*/

/*!
    \fn int TQTableSelection::rightCol() const

    Returns the right column of the selection.

    \sa topRow() bottomRow() leftCol()
*/

/*!
    \fn int TQTableSelection::anchorRow() const

    Returns the anchor row of the selection.

    \sa anchorCol() expandTo()
*/

/*!
    \fn int TQTableSelection::anchorCol() const

    Returns the anchor column of the selection.

    \sa anchorRow() expandTo()
*/

/*!
    \fn int TQTableSelection::numRows() const

    Returns the number of rows in the selection.

    \sa numCols()
*/
int TQTableSelection::numRows() const
{
    return ( tRow < 0 ) ? 0 : bRow - tRow + 1;
}

/*!
    Returns the number of columns in the selection.

    \sa numRows()
*/
int TQTableSelection::numCols() const
{
    return ( lCol < 0 ) ? 0 : rCol - lCol + 1;
}

/*!
    \fn bool TQTableSelection::isActive() const

    Returns whether the selection is active or not. A selection is
    active after init() \e and expandTo() have been called.
*/

/*!
    \fn bool TQTableSelection::isEmpty() const

    Returns whether the selection is empty or not.

    \sa numRows(), numCols()
*/

/*!
    \class TQTableItem
    \brief The TQTableItem class provides the cell content for TQTable cells.
\if defined(commercial)
    It is part of the <a href="commercialeditions.html">TQt Enterprise Edition</a>.
\endif

    \ingroup advanced
    \module table

    For many applications TQTableItems are ideal for presenting and
    editing the contents of TQTable cells. In situations where you need
    to create very large tables you may prefer an alternative approach
    to using TQTableItems: see the notes on large tables.

    A TQTableItem contains a cell's data, by default, a string and a
    pixmap. The table item also holds the cell's display size and how
    the data should be aligned. The table item specifies the cell's
    \l EditType and the editor used for in-place editing (by default a
    TQLineEdit). If you want checkboxes use \l{TQCheckTableItem}, and if
    you want comboboxes use \l{TQComboTableItem}. The \l EditType (set
    in the constructor) determines whether the cell's contents may be
    edited.

    If a pixmap is specified it is displayed to the left of any text.
    You can change the text or pixmap with setText() and setPixmap()
    respectively. For text you can use setWordWrap().

    When sorting table items the key() function is used; by default
    this returns the table item's text(). Reimplement key() to
    customize how your table items will sort.

    Table items are inserted into a table using TQTable::setItem(). If
    you insert an item into a cell that already contains a table item
    the original item will be deleted.

    Example:
    \code
    for ( int row = 0; row < table->numRows(); row++ ) {
	for ( int col = 0; col < table->numCols(); col++ ) {
	    table->setItem( row, col,
		new TQTableItem( table, TQTableItem::WhenCurrent, TQString::number( row * col ) ) );
	}
    }
    \endcode

    You can move a table item from one cell to another, in the same or
    a different table, using TQTable::takeItem() and TQTable::setItem()
    but see also TQTable::swapCells().

    Table items can be deleted with delete in the standard way; the
    table and cell will be updated accordingly.

    Note, that if you have a table item that is not currently in a table
    then anything you do to that item other than insert it into a table
    will result in undefined behaviour.

    Reimplement createEditor() and setContentFromEditor() if you want
    to use your own widget instead of a TQLineEdit for editing cell
    contents. Reimplement paint() if you want to display custom
    content.

    It is important to ensure that your custom widget can accept the
    keyboard focus, so that the user can use the tab key to navigate the
    table as normal. Therefore, if the widget returned by createEditor()
    does not itself accept the keyboard focus, it is necessary to
    nominate a child widget to do so on its behalf. For example, a
    TQHBox with two child TQLineEdit widgets may use one of them to
    accept the keyboard focus:

    \code
    TQWidget* MyTableItem::createEditor() const
    {
        TQHBox* hbox = new TQHBox( table()->viewport() );
        hbox->setFocusProxy(new TQLineEdit( hbox ));
        new TQLineEdit( hbox );
        return hbox;
    }
    \endcode

    By default, table items may be replaced by new TQTableItems
    during the lifetime of a TQTable. Therefore, if you create your
    own subclass of TQTableItem, and you want to ensure that
    this does not happen, you must call setReplaceable(FALSE)
    in the constructor of your subclass.

    \img qtableitems.png Table Items

    \sa TQCheckTableItem TQComboTableItem

*/

/*!
    \fn TQTable *TQTableItem::table() const

    Returns the TQTable the table item belongs to.

    \sa TQTable::setItem() TQTableItem()
*/

/*!
    \enum TQTableItem::EditType

    \target wheneditable
    This enum is used to define whether a cell is editable or
    read-only (in conjunction with other settings), and how the cell
    should be displayed.

    \value Always
    The cell always \e looks editable.

    Using this EditType ensures that the editor created with
    createEditor() (by default a TQLineEdit) is always visible. This
    has implications for the alignment of the content: the default
    editor aligns everything (even numbers) to the left whilst
    numerical values in the cell are by default aligned to the right.

    If a cell with the edit type \c Always looks misaligned you could
    reimplement createEditor() for these items.

    \value WhenCurrent
    The cell \e looks editable only when it has keyboard focus (see
    TQTable::setCurrentCell()).

    \value OnTyping
    The cell \e looks editable only when the user types in it or
    double-clicks it. It resembles the \c WhenCurrent functionality
    but is, perhaps, nicer.

    The \c OnTyping edit type is the default when TQTableItem objects
    are created by the convenience functions TQTable::setText() and
    TQTable::setPixmap().

    \value Never  The cell is not editable.

    The cell is actually editable only if TQTable::isRowReadOnly() is
    FALSE for its row, TQTable::isColumnReadOnly() is FALSE for its
    column, and TQTable::isReadOnly() is FALSE.

    TQComboTableItems have an isEditable() property. This property is
    used to indicate whether the user may enter their own text or are
    restricted to choosing one of the choices in the list.
    TQComboTableItems may be interacted with only if they are editable
    in accordance with their EditType as described above.

*/

/*!
    Creates a table item that is a child of table \a table with no
    text. The item has the \l EditType \a et.

    The table item will use a TQLineEdit for its editor, will not
    word-wrap and will occupy a single cell. Insert the table item
    into a table with TQTable::setItem().

    The table takes ownership of the table item, so a table item
    should not be inserted into more than one table at a time.
*/

TQTableItem::TQTableItem( TQTable *table, EditType et )
    : txt(), pix(), t( table ), edType( et ), wordwrap( FALSE ),
      tcha( TRUE ), rw( -1 ), cl( -1 ), rowspan( 1 ), colspan( 1 )
{
    enabled = TRUE;
}

/*!
    Creates a table item that is a child of table \a table with text
    \a text. The item has the \l EditType \a et.

    The table item will use a TQLineEdit for its editor, will not
    word-wrap and will occupy a single cell. Insert the table item
    into a table with TQTable::setItem().

    The table takes ownership of the table item, so a table item
    should not be inserted into more than one table at a time.
*/

TQTableItem::TQTableItem( TQTable *table, EditType et, const TQString &text )
    : txt( text ), pix(), t( table ), edType( et ), wordwrap( FALSE ),
      tcha( TRUE ), rw( -1 ), cl( -1 ), rowspan( 1 ), colspan( 1 )
{
    enabled = TRUE;
}

/*!
    Creates a table item that is a child of table \a table with text
    \a text and pixmap \a p. The item has the \l EditType \a et.

    The table item will display the pixmap to the left of the text. It
    will use a TQLineEdit for editing the text, will not word-wrap and
    will occupy a single cell. Insert the table item into a table with
    TQTable::setItem().

    The table takes ownership of the table item, so a table item
    should not be inserted in more than one table at a time.
*/

TQTableItem::TQTableItem( TQTable *table, EditType et,
			const TQString &text, const TQPixmap &p )
    : txt( text ), pix( p ), t( table ), edType( et ), wordwrap( FALSE ),
      tcha( TRUE ), rw( -1 ), cl( -1 ), rowspan( 1 ), colspan( 1 )
{
    enabled = TRUE;
}

/*!
    The destructor deletes this item and frees all allocated
    resources.

    If the table item is in a table (i.e. was inserted with
    setItem()), it will be removed from the table and the cell it
    occupied.
*/

TQTableItem::~TQTableItem()
{
    if ( table() )
	table()->takeItem( this );
}

int TQTableItem::RTTI = 0;

/*!
    Returns the Run Time Type Identification value for this table item
    which for TQTableItems is 0.

    When you create subclasses based on TQTableItem make sure that each
    subclass returns a unique rtti() value. It is advisable to use
    values greater than 1000, preferably large random numbers, to
    allow for extensions to this class.

    \sa TQCheckTableItem::rtti() TQComboTableItem::rtti()
*/

int TQTableItem::rtti() const
{
    return RTTI;
}

/*!
    Returns the table item's pixmap or a null pixmap if no pixmap has
    been set.

    \sa setPixmap() text()
*/

TQPixmap TQTableItem::pixmap() const
{
    return pix;
}


/*!
    Returns the text of the table item or TQString::null if there is no
    text.

    To ensure that the current value of the editor is returned,
    setContentFromEditor() is called:
    \list 1
    \i if the editMode() is \c Always, or
    \i if editMode() is \e not \c Always but the editor of the cell is
    active and the editor is not a TQLineEdit.
    \endlist

    This means that text() returns the original text value of the item
    if the editor is a line edit, until the user commits an edit (e.g.
    by pressing Enter or Tab) in which case the new text is returned.
    For other editors (e.g. a combobox) setContentFromEditor() is
    always called so the currently display value is the one returned.

    \sa setText() pixmap()
*/

TQString TQTableItem::text() const
{
    TQWidget *w = table()->cellWidget( rw, cl );
    if ( w && ( edType == Always ||
		rtti() == TQComboTableItem::RTTI ||
		rtti() == TQCheckTableItem::RTTI ) )
	( (TQTableItem*)this )->setContentFromEditor( w );
    return txt;
}

/*!
    Sets pixmap \a p to be this item's pixmap.

    Note that setPixmap() does not update the cell the table item
    belongs to. Use TQTable::updateCell() to repaint the cell's
    contents.

    For \l{TQComboTableItem}s and \l{TQCheckTableItem}s this function
    has no visible effect.

    \sa TQTable::setPixmap() pixmap() setText()
*/

void TQTableItem::setPixmap( const TQPixmap &p )
{
    pix = p;
}

/*!
    Changes the table item's text to \a str.

    Note that setText() does not update the cell the table item
    belongs to. Use TQTable::updateCell() to repaint the cell's
    contents.

    \sa TQTable::setText() text() setPixmap() TQTable::updateCell()
*/

void TQTableItem::setText( const TQString &str )
{
    txt = str;
}

/*!
    This virtual function is used to paint the contents of an item
    using the painter \a p in the rectangular area \a cr using the
    color group \a cg.

    If \a selected is TRUE the cell is displayed in a way that
    indicates that it is highlighted.

    You don't usually need to use this function but if you want to
    draw custom content in a cell you will need to reimplement it.

    The painter passed to this function is translated so that 0, 0
    is the top-left corner of the item that is being painted.

    Note that the painter is not clipped by default in order to get
    maximum efficiency. If you want clipping, use

    \code
    p->setClipRect( table()->cellRect(row, col), TQPainter::ClipPainter );
    //... your drawing code
    p->setClipping( FALSE );
    \endcode

*/

void TQTableItem::paint( TQPainter *p, const TQColorGroup &cg,
			const TQRect &cr, bool selected )
{
    p->fillRect( 0, 0, cr.width(), cr.height(),
		 selected ? cg.brush( TQColorGroup::Highlight )
			  : cg.brush( TQColorGroup::Base ) );

    int w = cr.width();
    int h = cr.height();

    int x = 0;
    if ( !pix.isNull() ) {
	p->drawPixmap( 0, ( cr.height() - pix.height() ) / 2, pix );
	x = pix.width() + 2;
    }

    if ( selected )
	p->setPen( cg.highlightedText() );
    else
	p->setPen( cg.text() );
    p->drawText( x + 2, 0, w - x - 4, h,
		 wordwrap ? (alignment() | WordBreak) : alignment(), text() );
}

/*!
This virtual function creates an editor which the user can
interact with to edit the cell's contents. The default
implementation creates a TQLineEdit.

If the function returns 0, the cell is read-only.

The returned widget should preferably be invisible, ideally with
TQTable::viewport() as parent.

If you reimplement this function you'll almost certainly need to
reimplement setContentFromEditor(), and may need to reimplement
sizeHint().

\quotefile table/statistics/statistics.cpp
\skipto createEditor
\printto }

\sa TQTable::createEditor() setContentFromEditor() TQTable::viewport() setReplaceable()
*/

TQWidget *TQTableItem::createEditor() const
{
    TQLineEdit *e = new TQLineEdit( table()->viewport(), "qt_tableeditor" );
    e->setFrame( FALSE );
    e->setText( text() );
    return e;
}

/*!
Whenever the content of a cell has been edited by the editor \a w,
TQTable calls this virtual function to copy the new values into the
TQTableItem.

If you reimplement createEditor() and return something that is not
a TQLineEdit you will need to reimplement this function.

\quotefile table/statistics/statistics.cpp
\skipto setContentFromEditor
\printto }

\sa TQTable::setCellContentFromEditor()
*/

void TQTableItem::setContentFromEditor( TQWidget *w )
{
    TQLineEdit *le = ::qt_cast<TQLineEdit*>(w);
    if ( le ) {
	TQString input = le->text();
	if ( le->validator() )
	    le->validator()->fixup( input );
	setText( input );
    }
}

/*!
    The alignment function returns how the text contents of the cell
    are aligned when drawn. The default implementation aligns numbers
    to the right and any other text to the left.

    \sa TQt::AlignmentFlags
*/

// ed: For consistency reasons a setAlignment() should be provided
// as well.

int TQTableItem::alignment() const
{
    bool num;
    bool ok1 = FALSE, ok2 = FALSE;
    (void)text().toInt( &ok1 );
    if ( !ok1 )
	(void)text().toDouble( &ok2 ); // ### should be .-aligned
    num = ok1 || ok2;

    return ( num ? AlignRight : AlignLeft ) | AlignVCenter;
}

/*!
    If \a b is TRUE, the cell's text will be wrapped over multiple
    lines, when necessary, to fit the width of the cell; otherwise the
    text will be written as a single line.

    \sa wordWrap() TQTable::adjustColumn() TQTable::setColumnStretchable()
*/

void TQTableItem::setWordWrap( bool b )
{
    wordwrap = b;
}

/*!
    Returns TRUE if word wrap is enabled for the cell; otherwise
    returns FALSE.

    \sa setWordWrap()
*/

bool TQTableItem::wordWrap() const
{
    return wordwrap;
}

/*! \internal */

void TQTableItem::updateEditor( int oldRow, int oldCol )
{
    if ( edType != Always )
	return;
    if ( oldRow != -1 && oldCol != -1 )
	table()->clearCellWidget( oldRow, oldCol );
    if ( rw != -1 && cl != -1 )
	table()->setCellWidget( rw, cl, createEditor() );
}

/*!
    Returns the table item's edit type.

    This is set when the table item is constructed.

    \sa EditType TQTableItem()
*/

TQTableItem::EditType TQTableItem::editType() const
{
    return edType;
}

/*!
    If \a b is TRUE it is acceptable to replace the contents of the
    cell with the contents of another TQTableItem. If \a b is FALSE the
    contents of the cell may not be replaced by the contents of
    another table item. Table items that span more than one cell may
    not have their contents replaced by another table item.

    (This differs from \l EditType because EditType is concerned with
    whether the \e user is able to change the contents of a cell.)

    \sa isReplaceable()
*/

void TQTableItem::setReplaceable( bool b )
{
    tcha = b;
}

/*!
    This function returns whether the contents of the cell may be
    replaced with the contents of another table item. Regardless of
    this setting, table items that span more than one cell may not
    have their contents replaced by another table item.

    (This differs from \l EditType because EditType is concerned with
    whether the \e user is able to change the contents of a cell.)

    \sa setReplaceable() EditType
*/

bool TQTableItem::isReplaceable() const
{
    if ( rowspan > 1 || colspan > 1 )
	return FALSE;
    return tcha;
}

/*!
    This virtual function returns the key that should be used for
    sorting. The default implementation returns the text() of the
    relevant item.

    \sa TQTable::setSorting()
*/

TQString TQTableItem::key() const
{
    return text();
}

/*!
    This virtual function returns the size a cell needs to show its
    entire content.

    If you subclass TQTableItem you will often need to reimplement this
    function.
*/

TQSize TQTableItem::sizeHint() const
{
    TQSize strutSize = TQApplication::globalStrut();
    if ( edType == Always && table()->cellWidget( rw, cl ) )
	return table()->cellWidget( rw, cl )->sizeHint().expandedTo( strutSize );

    TQSize s;
    int x = 0;
    if ( !pix.isNull() ) {
	s = pix.size();
	s.setWidth( s.width() + 2 );
	x = pix.width() + 2;
    }

    TQString t = text();
    if ( !wordwrap && t.find( '\n' ) == -1 )
	return TQSize( s.width() + table()->fontMetrics().width( text() ) + 10,
		      TQMAX( s.height(), table()->fontMetrics().height() ) ).expandedTo( strutSize );

    TQRect r = table()->fontMetrics().boundingRect( x + 2, 0, table()->columnWidth( col() ) - x - 4, 0,
						   wordwrap ? (alignment() | WordBreak) : alignment(),
						   text() );
    r.setWidth( TQMAX( r.width() + 10, table()->columnWidth( col() ) ) );
    return TQSize( r.width(), TQMAX( s.height(), r.height() ) ).expandedTo( strutSize );
}

/*!
    Changes the extent of the TQTableItem so that it spans multiple
    cells covering \a rs rows and \a cs columns. The top left cell is
    the original cell.

    \warning This function only works if the item has already been
    inserted into the table using e.g. TQTable::setItem(). This
    function also checks to make sure if \a rs and \a cs are within
    the bounds of the table and returns without changing the span if
    they are not. In addition swapping, inserting or removing rows and
    columns that cross TQTableItems spanning more than one cell is not
    supported.

    \sa rowSpan() colSpan()
*/

void TQTableItem::setSpan( int rs, int cs )
{
    if ( rs == rowspan && cs == colspan )
	return;

    if ( !table()->d->hasRowSpan )
	table()->d->hasRowSpan = rs > 1;
    if ( !table()->d->hasColSpan )
	table()->d->hasColSpan = cs > 1;
    // return if we are thinking too big...
    if ( rw + rs > table()->numRows() )
	return;

    if ( cl + cs > table()->numCols() )
	return;

    if ( rw == -1 || cl == -1 )
	return;

    int rrow = rw;
    int rcol = cl;
    if ( rowspan > 1 || colspan > 1 ) {
	TQTable* t = table();
	t->takeItem( this );
	t->setItem( rrow, rcol, this );
    }

    rowspan = rs;
    colspan = cs;

    for ( int r = 0; r < rowspan; ++r ) {
	for ( int c = 0; c < colspan; ++c ) {
	    if ( r == 0 && c == 0 )
		continue;
	    qt_update_cell_widget = FALSE;
	    table()->setItem( r + rw, c + cl, this );
	    qt_update_cell_widget = TRUE;
	    rw = rrow;
	    cl = rcol;
	}
    }

    table()->updateCell( rw, cl );
    TQWidget *w = table()->cellWidget( rw, cl );
    if ( w )
	w->resize( table()->cellGeometry( rw, cl ).size() );
}

/*!
    Returns the row span of the table item, usually 1.

    \sa setSpan() colSpan()
*/

int TQTableItem::rowSpan() const
{
    return rowspan;
}

/*!
    Returns the column span of the table item, usually 1.

    \sa setSpan() rowSpan()
*/

int TQTableItem::colSpan() const
{
    return colspan;
}

/*!
    Sets row \a r as the table item's row. Usually you do not need to
    call this function.

    If the cell spans multiple rows, this function sets the top row
    and retains the height of the multi-cell table item.

    \sa row() setCol() rowSpan()
*/

void TQTableItem::setRow( int r )
{
    rw = r;
}

/*!
    Sets column \a c as the table item's column. Usually you will not
    need to call this function.

    If the cell spans multiple columns, this function sets the
    left-most column and retains the width of the multi-cell table
    item.

    \sa col() setRow() colSpan()
*/

void TQTableItem::setCol( int c )
{
    cl = c;
}

/*!
    Returns the row where the table item is located. If the cell spans
    multiple rows, this function returns the top-most row.

    \sa col() setRow()
*/

int TQTableItem::row() const
{
    return rw;
}

/*!
    Returns the column where the table item is located. If the cell
    spans multiple columns, this function returns the left-most
    column.

    \sa row() setCol()
*/

int TQTableItem::col() const
{
    return cl;
}

/*!
    If \a b is TRUE, the table item is enabled; if \a b is FALSE the
    table item is disabled.

    A disabled item doesn't respond to user interaction.

    \sa isEnabled()
*/

void TQTableItem::setEnabled( bool b )
{
    if ( b == (bool)enabled )
	return;
    enabled = b;
    table()->updateCell( row(), col() );
}

/*!
    Returns TRUE if the table item is enabled; otherwise returns FALSE.

    \sa setEnabled()
*/

bool TQTableItem::isEnabled() const
{
    return (bool)enabled;
}

/*!
    \class TQComboTableItem
    \brief The TQComboTableItem class provides a means of using
    comboboxes in TQTables.
\if defined(commercial)
    It is part of the <a href="commercialeditions.html">TQt Enterprise Edition</a>.
\endif

    \ingroup advanced
    \module table

    A TQComboTableItem is a table item which looks and behaves like a
    combobox. The advantage of using TQComboTableItems rather than real
    comboboxes is that a TQComboTableItem uses far less resources than
    real comboboxes in \l{TQTable}s. When the cell has the focus it
    displays a real combobox which the user can interact with. When
    the cell does not have the focus the cell \e looks like a
    combobox. Only text items (i.e. no pixmaps) may be used in
    TQComboTableItems.

    TQComboTableItem items have the edit type \c WhenCurrent (see
    \l{EditType}). The TQComboTableItem's list of items is provided by
    a TQStringList passed to the constructor.

    The list of items may be changed using setStringList(). The
    current item can be set with setCurrentItem() and retrieved with
    currentItem(). The text of the current item can be obtained with
    currentText(), and the text of a particular item can be retrieved
    with text().

    If isEditable() is TRUE the TQComboTableItem will permit the user
    to either choose an existing list item, or create a new list item
    by entering their own text; otherwise the user may only choose one
    of the existing list items.

    To populate a table cell with a TQComboTableItem use
    TQTable::setItem().

    TQComboTableItems may be deleted with TQTable::clearCell().

    TQComboTableItems can be distinguished from \l{TQTableItem}s and
    \l{TQCheckTableItem}s using their Run Time Type Identification
    number (see rtti()).

    \img qtableitems.png Table Items

    \sa TQCheckTableItem TQTableItem TQComboBox
*/

TQComboBox *TQComboTableItem::fakeCombo = 0;
TQWidget *TQComboTableItem::fakeComboWidget = 0;
int TQComboTableItem::fakeRef = 0;

/*!
    Creates a combo table item for the table \a table. The combobox's
    list of items is passed in the \a list argument. If \a editable is
    TRUE the user may type in new list items; if \a editable is FALSE
    the user may only select from the list of items provided.

    By default TQComboTableItems cannot be replaced by other table
    items since isReplaceable() returns FALSE by default.

    \sa TQTable::clearCell() EditType
*/

TQComboTableItem::TQComboTableItem( TQTable *table, const TQStringList &list, bool editable )
    : TQTableItem( table, WhenCurrent, "" ), entries( list ), current( 0 ), edit( editable )
{
    setReplaceable( FALSE );
    if ( !TQComboTableItem::fakeCombo ) {
	TQComboTableItem::fakeComboWidget = new TQWidget( 0, 0 );
	TQComboTableItem::fakeCombo = new TQComboBox( FALSE, TQComboTableItem::fakeComboWidget, 0 );
	TQComboTableItem::fakeCombo->hide();
    }
    ++TQComboTableItem::fakeRef;
    if ( entries.count() )
	setText( *entries.at( current ) );
}

/*!
    TQComboTableItem destructor.
*/
TQComboTableItem::~TQComboTableItem()
{
    if (--TQComboTableItem::fakeRef <= 0) {
	delete TQComboTableItem::fakeComboWidget;
	TQComboTableItem::fakeComboWidget = 0;
	TQComboTableItem::fakeCombo = 0;
    }
}

/*!
    Sets the list items of this TQComboTableItem to the strings in the
    string list \a l.
*/

void TQComboTableItem::setStringList( const TQStringList &l )
{
    entries = l;
    current = 0;
    if ( entries.count() )
	setText( *entries.at( current ) );
    if ( table()->cellWidget( row(), col() ) ) {
	cb->clear();
	cb->insertStringList( entries );
    }
    table()->updateCell( row(), col() );
}

/*! \reimp */

TQWidget *TQComboTableItem::createEditor() const
{
    // create an editor - a combobox in our case
    ( (TQComboTableItem*)this )->cb = new TQComboBox( edit, table()->viewport(), "qt_editor_cb" );
    cb->insertStringList( entries );
    cb->setCurrentItem( current );
    TQObject::connect( cb, SIGNAL( activated(int) ), table(), SLOT( doValueChanged() ) );
    return cb;
}

/*! \reimp */

void TQComboTableItem::setContentFromEditor( TQWidget *w )
{
    TQComboBox *cb = ::qt_cast<TQComboBox*>(w);
    if ( cb ) {
	entries.clear();
	for ( int i = 0; i < cb->count(); ++i )
	    entries << cb->text( i );
	current = cb->currentItem();
	setText( *entries.at( current ) );
    }
}

/*! \reimp */

void TQComboTableItem::paint( TQPainter *p, const TQColorGroup &cg,
			   const TQRect &cr, bool selected )
{
    fakeCombo->resize( cr.width(), cr.height() );

    TQColorGroup c( cg );
    if ( selected ) {
	c.setBrush( TQColorGroup::Base, cg.brush( TQColorGroup::Highlight ) );
	c.setColor( TQColorGroup::Text, cg.highlightedText() );
    }

    TQStyle::SFlags flags = TQStyle::Style_Default;
    if(isEnabled() && table()->isEnabled())
	flags |= TQStyle::Style_Enabled;
    table()->style().drawComplexControl( TQStyle::CC_ComboBox, p, fakeCombo, fakeCombo->rect(), c, flags );

    p->save();
    TQRect textR = table()->style().querySubControlMetrics(TQStyle::CC_ComboBox, fakeCombo,
							 TQStyle::SC_ComboBoxEditField);
    int align = alignment(); // alignment() changes entries
    p->drawText( textR, wordWrap() ? ( align | WordBreak ) : align, *entries.at( current ) );
    p->restore();
}

/*!
    Sets the list item \a i to be the combo table item's current list
    item.

    \sa currentItem()
*/

void TQComboTableItem::setCurrentItem( int i )
{
    TQWidget *w = table()->cellWidget( row(), col() );
    TQComboBox *cb = ::qt_cast<TQComboBox*>(w);
    if ( cb ) {
	cb->setCurrentItem( i );
	current = i;
	setText( cb->currentText() );
    } else {
	current = i;
	setText( *entries.at( i ) );
	table()->updateCell( row(), col() );
    }
}

/*!
    \overload

    Sets the list item whose text is \a s to be the combo table item's
    current list item. Does nothing if no list item has the text \a s.

    \sa currentItem()
*/

void TQComboTableItem::setCurrentItem( const TQString &s )
{
    int i = entries.findIndex( s );
    if ( i != -1 )
	setCurrentItem( i );
}

/*!
    Returns the index of the combo table item's current list item.

    \sa setCurrentItem()
*/

int TQComboTableItem::currentItem() const
{
    TQWidget *w = table()->cellWidget( row(), col() );
    TQComboBox *cb = ::qt_cast<TQComboBox*>(w);
    if ( cb )
	return cb->currentItem();
    return current;
}

/*!
    Returns the text of the combo table item's current list item.

    \sa currentItem() text()
*/

TQString TQComboTableItem::currentText() const
{
    TQWidget *w = table()->cellWidget( row(), col() );
    TQComboBox *cb = ::qt_cast<TQComboBox*>(w);
    if ( cb )
	return cb->currentText();
    return *entries.at( current );
}

/*!
    Returns the total number of list items in the combo table item.
*/

int TQComboTableItem::count() const
{
    TQWidget *w = table()->cellWidget( row(), col() );
    TQComboBox *cb = ::qt_cast<TQComboBox*>(w);
    if ( cb )
	return cb->count();
    return (int)entries.count();    //### size_t/int cast
}

/*!
    Returns the text of the combo's list item at index \a i.

    \sa currentText()
*/

TQString TQComboTableItem::text( int i ) const
{
    TQWidget *w = table()->cellWidget( row(), col() );
    TQComboBox *cb = ::qt_cast<TQComboBox*>(w);
    if ( cb )
	return cb->text( i );
    return *entries.at( i );
}

/*!
    If \a b is TRUE the combo table item can be edited, i.e. the user
    may enter a new text item themselves. If \a b is FALSE the user may
    may only choose one of the existing items.

    \sa isEditable()
*/

void TQComboTableItem::setEditable( bool b )
{
    edit = b;
}

/*!
    Returns TRUE if the user can add their own list items to the
    combobox's list of items; otherwise returns FALSE.

    \sa setEditable()
*/

bool TQComboTableItem::isEditable() const
{
    return edit;
}

int TQComboTableItem::RTTI = 1;

/*!
    \fn int TQComboTableItem::rtti() const

    Returns 1.

    Make your derived classes return their own values for rtti()to
    distinguish between different table item subclasses. You should
    use values greater than 1000, preferably a large random number, to
    allow for extensions to this class.


    \sa TQTableItem::rtti()
*/

int TQComboTableItem::rtti() const
{
    return RTTI;
}

/*! \reimp */

TQSize TQComboTableItem::sizeHint() const
{
    fakeCombo->insertItem( currentText() );
    fakeCombo->setCurrentItem( fakeCombo->count() - 1 );
    TQSize sh = fakeCombo->sizeHint();
    fakeCombo->removeItem( fakeCombo->count() - 1 );
    return sh.expandedTo( TQApplication::globalStrut() );
}

/*!
    \class TQCheckTableItem
    \brief The TQCheckTableItem class provides checkboxes in TQTables.
\if defined(commercial)
    It is part of the <a href="commercialeditions.html">TQt Enterprise Edition</a>.
\endif

    \ingroup advanced
    \module table

    A TQCheckTableItem is a table item which looks and behaves like a
    checkbox. The advantage of using TQCheckTableItems rather than real
    checkboxes is that a TQCheckTableItem uses far less resources than
    a real checkbox would in a \l{TQTable}. When the cell has the focus
    it displays a real checkbox which the user can interact with. When
    the cell does not have the focus the cell \e looks like a
    checkbox. Pixmaps may not be used in TQCheckTableItems.

    TQCheckTableItem items have the edit type \c WhenCurrent (see
    \l{EditType}).

    To change the checkbox's label use setText(). The checkbox can be
    checked and unchecked with setChecked() and its state retrieved
    using isChecked().

    To populate a table cell with a TQCheckTableItem use
    TQTable::setItem().

    TQCheckTableItems can be distinguished from \l{TQTableItem}s and
    \l{TQComboTableItem}s using their Run Time Type Identification
    (rtti) value.

    \img qtableitems.png Table Items

    \sa rtti() EditType TQComboTableItem TQTableItem TQCheckBox
*/

/*!
    Creates a TQCheckTableItem with an \l{EditType} of \c WhenCurrent
    as a child of \a table. The checkbox is initially unchecked and
    its label is set to the string \a txt.
*/

TQCheckTableItem::TQCheckTableItem( TQTable *table, const TQString &txt )
    : TQTableItem( table, WhenCurrent, txt ), checked( FALSE )
{
}

/*! \reimp */

void TQCheckTableItem::setText( const TQString &t )
{
    TQTableItem::setText( t );
    TQWidget *w = table()->cellWidget( row(), col() );
    TQCheckBox *cb = ::qt_cast<TQCheckBox*>(w);
    if ( cb )
	cb->setText( t );
}


/*! \reimp */

TQWidget *TQCheckTableItem::createEditor() const
{
    // create an editor - a combobox in our case
    ( (TQCheckTableItem*)this )->cb = new TQCheckBox( table()->viewport(), "qt_editor_checkbox" );
    cb->setChecked( checked );
    cb->setText( text() );
    cb->setBackgroundColor( table()->viewport()->backgroundColor() );
    TQObject::connect( cb, SIGNAL( toggled(bool) ), table(), SLOT( doValueChanged() ) );
    return cb;
}

/*! \reimp */

void TQCheckTableItem::setContentFromEditor( TQWidget *w )
{
    TQCheckBox *cb = ::qt_cast<TQCheckBox*>(w);
    if ( cb )
	checked = cb->isChecked();
}

/*! \reimp */

void TQCheckTableItem::paint( TQPainter *p, const TQColorGroup &cg,
				const TQRect &cr, bool selected )
{
    p->fillRect( 0, 0, cr.width(), cr.height(),
		 selected ? cg.brush( TQColorGroup::Highlight )
			  : cg.brush( TQColorGroup::Base ) );

    int w = cr.width();
    int h = cr.height();
    TQSize sz = TQSize( table()->style().pixelMetric( TQStyle::PM_IndicatorWidth ),
		      table()->style().pixelMetric( TQStyle::PM_IndicatorHeight ) );
    TQColorGroup c( cg );
    c.setBrush( TQColorGroup::Background, c.brush( TQColorGroup::Base ) );
    TQStyle::SFlags flags = TQStyle::Style_Default;
    if(isEnabled())
	flags |= TQStyle::Style_Enabled;
    if ( checked )
	flags |= TQStyle::Style_On;
    else
	flags |= TQStyle::Style_Off;
    if ( isEnabled() && table()->isEnabled() )
	flags |= TQStyle::Style_Enabled;

    table()->style().drawPrimitive( TQStyle::PE_Indicator, p,
				    TQRect( 0, ( cr.height() - sz.height() ) / 2, sz.width(), sz.height() ), c, flags );
    int x = sz.width() + 6;
    w = w - x;
    if ( selected )
	p->setPen( cg.highlightedText() );
    else
	p->setPen( cg.text() );
    p->drawText( x, 0, w, h, wordWrap() ? ( alignment() | WordBreak ) : alignment(), text() );
}

/*!
    If \a b is TRUE the checkbox is checked; if \a b is FALSE the
    checkbox is unchecked.

    \sa isChecked()
*/

void TQCheckTableItem::setChecked( bool b )
{
    checked = b;
    table()->updateCell( row(), col() );
    TQWidget *w = table()->cellWidget( row(), col() );
    TQCheckBox *cb = ::qt_cast<TQCheckBox*>(w);
    if ( cb )
	cb->setChecked( b );
}

/*!
    Returns TRUE if the checkbox table item is checked; otherwise
    returns FALSE.

    \sa setChecked()
*/

bool TQCheckTableItem::isChecked() const
{
    // #### why was this next line here. It must not be here, as
    // #### people want to call isChecked() from within paintCell()
    // #### and end up in an infinite loop that way
    // table()->updateCell( row(), col() );
    TQWidget *w = table()->cellWidget( row(), col() );
    TQCheckBox *cb = ::qt_cast<TQCheckBox*>(w);
    if ( cb )
	return cb->isChecked();
    return checked;
}

int TQCheckTableItem::RTTI = 2;

/*!
    \fn int TQCheckTableItem::rtti() const

    Returns 2.

    Make your derived classes return their own values for rtti()to
    distinguish between different table item subclasses. You should
    use values greater than 1000, preferably a large random number, to
    allow for extensions to this class.

    \sa TQTableItem::rtti()
*/

int TQCheckTableItem::rtti() const
{
    return RTTI;
}

/*! \reimp */

TQSize TQCheckTableItem::sizeHint() const
{
    TQSize sz = TQSize( table()->style().pixelMetric( TQStyle::PM_IndicatorWidth ),
		      table()->style().pixelMetric( TQStyle::PM_IndicatorHeight ) );
    sz.setWidth( sz.width() + 6 );
    TQSize sh( TQTableItem::sizeHint() );
    return TQSize( sh.width() + sz.width(), TQMAX( sh.height(), sz.height() ) ).
	expandedTo( TQApplication::globalStrut() );
}

/*! \file table/small-table-demo/main.cpp */
/*! \file table/bigtable/main.cpp */
/*! \file table/statistics/statistics.cpp */

/*!
    \class TQTable
    \brief The TQTable class provides a flexible editable table widget.
\if defined(commercial)
    It is part of the <a href="commercialeditions.html">TQt Enterprise Edition</a>.
\endif

    \mainclass
    \ingroup advanced
    \module table

    TQTable is easy to use, although it does have a large API because
    of the comprehensive functionality that it provides. TQTable
    includes functions for manipulating \link #headers
    headers\endlink, \link #columnsrows rows and columns\endlink,
    \link #cells cells\endlink and \link #selections
    selections\endlink. TQTable also provides in-place editing and
    \link dnd.html drag and drop\endlink, as well as a useful set of
    \link #signals signals\endlink. TQTable efficiently supports very
    large tables, for example, tables one million by one million cells
    are perfectly possible. TQTable is economical with memory, using
    none for unused cells.

    \code
    TQTable *table = new TQTable( 100, 250, this );
    table->setPixmap( 3, 2, pix );
    table->setText( 3, 2, "A pixmap" );
    \endcode

    The first line constructs the table specifying its size in rows
    and columns. We then insert a pixmap and some text into the \e
    same \link #cells cell\endlink, with the pixmap appearing to the
    left of the text. TQTable cells can be populated with
    \l{TQTableItem}s, \l{TQComboTableItem}s or by \l{TQCheckTableItem}s.
    By default a vertical header appears at the left of the table
    showing row numbers and a horizontal header appears at the top of
    the table showing column numbers. (The numbers displayed start at
    1, although row and column numbers within TQTable begin at 0.)

    If you want to use mouse tracking call setMouseTracking( TRUE ) on
    the \e viewport; (see \link ntqscrollview.html#allviews
    TQScrollView\endlink).

    \img qtableitems.png Table Items

    \target headers
    \section1 Headers

    TQTable supports a header column, e.g. to display row numbers, and
    a header row, e.g to display column titles. To set row or column
    labels use TQHeader::setLabel() on the pointers returned by
    verticalHeader() and horizontalHeader() respectively. The vertical
    header is displayed within the table's left margin whose width is
    set with setLeftMargin(). The horizontal header is displayed
    within the table's top margin whose height is set with
    setTopMargin(). The table's grid can be switched off with
    setShowGrid(). If you want to hide a horizontal header call
    hide(), and call setTopMargin( 0 ) so that the area the header
    would have occupied is reduced to zero size.

    Header labels are indexed via their section numbers. Note that the
    default behavior of TQHeader regarding section numbers is overriden
    for TQTable. See the explanation below in the Rows and Columns
    section in the discussion of moving columns and rows.

    \target columnsrows
    \section1 Rows and Columns

    Row and column sizes are set with setRowHeight() and
    setColumnWidth(). If you want a row high enough to show the
    tallest item in its entirety, use adjustRow(). Similarly, to make
    a column wide enough to show the widest item use adjustColumn().
    If you want the row height and column width to adjust
    automatically as the height and width of the table changes use
    setRowStretchable() and setColumnStretchable().

    Rows and columns can be hidden and shown with hideRow(),
    hideColumn(), showRow() and showColumn(). New rows and columns are
    inserted using insertRows() and insertColumns(). Additional rows
    and columns are added at the  bottom (rows) or right (columns) if
    you set setNumRows() or setNumCols() to be larger than numRows()
    or numCols(). Existing rows and columns are removed with
    removeRow() and removeColumn(). Multiple rows and columns can be
    removed with removeRows() and removeColumns().

    Rows and columns can be set to be moveable using
    rowMovingEnabled() and columnMovingEnabled(). The user can drag
    them to reorder them holding down the Ctrl key and dragging the
    mouse. For performance reasons, the default behavior of TQHeader
    section numbers is overridden by TQTable. Currently in TQTable, when
    a row or column is dragged and reordered, the section number is
    also changed to its new position. Therefore, there is no
    difference between the section and the index fields in TQHeader.
    The TQTable TQHeader classes do not provide a mechanism for indexing
    independently of the user interface ordering.

    The table can be sorted using sortColumn(). Users can sort a
    column by clicking its header if setSorting() is set to TRUE. Rows
    can be swapped with swapRows(), columns with swapColumns() and
    cells with swapCells().

    For editable tables (see setReadOnly()) you can set the read-only
    property of individual rows and columns with setRowReadOnly() and
    setColumnReadOnly(). (Whether a cell is editable or read-only
    depends on these settings and the cell's \link
    qtableitem.html#wheneditable TQTableItem::EditType\endlink.)

    The row and column which have the focus are returned by
    currentRow() and currentColumn() respectively.

    Although many TQTable functions operate in terms of rows and
    columns the indexOf() function returns a single integer
    identifying a particular cell.

    \target cells
    \section1 Cells

    All of a TQTable's cells are empty when the table is constructed.

    There are two approaches to populating the table's cells. The
    first and simplest approach is to use TQTableItems or TQTableItem
    subclasses. The second approach doesn't use TQTableItems at all
    which is useful for very large sparse tables but requires you to
    reimplement a number of functions. We'll look at each approach in
    turn.

    To put a string in a cell use setText(). This function will create
    a new TQTableItem for the cell if one doesn't already exist, and
    displays the text in it. By default the table item's widget will
    be a TQLineEdit. A pixmap may be put in a cell with setPixmap(),
    which also creates a table item if required. A cell may contain \e
    both a pixmap and text; the pixmap is displayed to the left of the
    text. Another approach is to construct a TQTableItem or TQTableItem
    subclass, set its properties, then insert it into a cell with
    setItem().

    If you want cells which contain comboboxes use the TQComboTableItem
    class. Similarly if you require cells containing checkboxes use
    the TQCheckTableItem class. These table items look and behave just
    like the combobox or checkbox widgets but consume far less memory.

    \quotefile table/small-table-demo/main.cpp
    \skipto int j
    \printuntil TQCheckTableItem
    In the example above we create a column of TQCheckTableItems and
    insert them into the table using setItem().

    TQTable takes ownership of its TQTableItems and will delete them
    when the table itself is destroyed. You can take ownership of a
    table item using takeItem() which you use to move a cell's
    contents from one cell to another, either within the same table,
    or from one table to another. (See also, swapCells()).

    In-place editing of the text in TQTableItems, and the values in
    TQComboTableItems and TQCheckTableItems works automatically. Cells
    may be editable or read-only, see \link
    qtableitem.html#wheneditable TQTableItem::EditType\endlink. If you
    want fine control over editing see beginEdit() and endEdit().

    The contents of a cell can be retrieved as a TQTableItem using
    item(), or as a string with text() or as a pixmap (if there is
    one) with pixmap(). A cell's bounding rectangle is given by
    cellGeometry(). Use updateCell() to repaint a cell, for example to
    clear away a cell's visual representation after it has been
    deleted with clearCell(). The table can be forced to scroll to
    show a particular cell with ensureCellVisible(). The isSelected()
    function indicates if a cell is selected.

    It is possible to use your own widget as a cell's widget using
    setCellWidget(), but subclassing TQTableItem might be a simpler
    approach. The cell's widget (if there is one) can be removed with
    clearCellWidget().

    \keyword notes on large tables
    \target bigtables
    \section2 Large tables

    For large, sparse, tables using TQTableItems or other widgets is
    inefficient. The solution is to \e draw the cell as it should
    appear and to create and destroy cell editors on demand.

    This approach requires that you reimplement various functions.
    Reimplement paintCell() to display your data, and createEditor()
    and setCellContentFromEditor() to support in-place editing. It
    is very important to reimplement resizeData() to have no
    functionality, to prevent TQTable from attempting to create a huge
    array. You will also need to reimplement item(), setItem(),
    takeItem(), clearCell(), and insertWidget(), cellWidget() and
    clearCellWidget(). In almost every circumstance (for sorting,
    removing and inserting columns and rows, etc.), you also need
    to reimplement swapRows(), swapCells() and swapColumns(), including
    header handling.

    If you represent active cells with a dictionary of TQTableItems and
    TQWidgets, i.e. only store references to cells that are actually
    used, many of the functions can be implemented with a single line
    of code. (See the \l table/bigtable/main.cpp example.)

    For more information on cells see the TQTableItem documenation.

    \target selections
    \section1 Selections

    TQTable's support single selection, multi-selection (multiple
    cells) or no selection. The selection mode is set with
    setSelectionMode(). Use isSelected() to determine if a particular
    cell is selected, and isRowSelected() and isColumnSelected() to
    see if a row or column is selected.

    TQTable's support many simultaneous selections. You can
    programmatically select cells with addSelection(). The number of
    selections is given by numSelections(). The current selection is
    returned by currentSelection(). You can remove a selection with
    removeSelection() and remove all selections with
    clearSelection(). Selections are TQTableSelection objects.

    To easily add a new selection use selectCells(), selectRow() or
    selectColumn().

    Alternatively, use addSelection() to add new selections using
    TQTableSelection objects. The advantage of using TQTableSelection
    objects is that you can call TQTableSelection::expandTo() to resize
    the selection and can query and compare them.

    The number of selections is given by numSelections(). The current
    selection is returned by currentSelection(). You can remove a
    selection with removeSelection() and remove all selections with
    clearSelection().

    \target signals
    \section1 Signals

    When the user clicks a cell the currentChanged() signal is
    emitted. You can also connect to the lower level clicked(),
    doubleClicked() and pressed() signals. If the user changes the
    selection the selectionChanged() signal is emitted; similarly if
    the user changes a cell's value the valueChanged() signal is
    emitted. If the user right-clicks (or presses the appropriate
    platform-specific key sequence) the contextMenuRequested() signal
    is emitted. If the user drops a drag and drop object the dropped()
    signal is emitted with the drop event.
*/

/*!
    \fn void TQTable::currentChanged( int row, int col )

    This signal is emitted when the current cell has changed to \a
    row, \a col.
*/

/*!
    \fn void TQTable::valueChanged( int row, int col )

    This signal is emitted when the user changed the value in the cell
    at \a row, \a col.
*/

/*!
    \fn int TQTable::currentRow() const

    Returns the current row.

    \sa currentColumn()
*/

/*!
    \fn int TQTable::currentColumn() const

    Returns the current column.

    \sa currentRow()
*/

/*!
    \enum TQTable::EditMode

    \value NotEditing  No cell is currently being edited.

    \value Editing  A cell is currently being edited. The editor was
    initialised with the cell's contents.

    \value Replacing  A cell is currently being edited. The editor was
    not initialised with the cell's contents.
*/

/*!
    \enum TQTable::SelectionMode

    \value NoSelection No cell can be selected by the user.

    \value Single The user may only select a single range of cells.

    \value Multi The user may select multiple ranges of cells.

    \value SingleRow The user may select one row at once.

    \value MultiRow The user may select multiple rows.
*/

/*!
    \enum TQTable::FocusStyle

    Specifies how the current cell (focus cell) is drawn.

    \value FollowStyle The current cell is drawn according to the
    current style and the cell's background is also drawn selected, if
    the current cell is within a selection

    \value SpreadSheet The current cell is drawn as in a spreadsheet.
    This means, it is signified by a black rectangle around the cell,
    and the background of the current cell is always drawn with the
    widget's base color - even when selected.

*/

/*!
    \fn void TQTable::clicked( int row, int col, int button, const TQPoint &mousePos )

    This signal is emitted when mouse button \a button is clicked. The
    cell where the event took place is at \a row, \a col, and the
    mouse's position is in \a mousePos.

    \sa TQt::ButtonState
*/

/*!
    \fn void TQTable::doubleClicked( int row, int col, int button, const TQPoint &mousePos )

    This signal is emitted when mouse button \a button is
    double-clicked. The cell where the event took place is at \a row,
    \a col, and the mouse's position is in \a mousePos.

    \sa TQt::ButtonState
*/

/*!
    \fn void TQTable::pressed( int row, int col, int button, const TQPoint &mousePos )

    This signal is emitted when mouse button \a button is pressed. The
    cell where the event took place is at \a row, \a col, and the
    mouse's position is in \a mousePos.

    \sa TQt::ButtonState
*/

/*!
    \fn void TQTable::selectionChanged()

    This signal is emitted whenever a selection changes.

    \sa TQTableSelection
*/

/*!
    \fn void TQTable::contextMenuRequested( int row, int col, const TQPoint & pos )

    This signal is emitted when the user invokes a context menu with
    the right mouse button (or with a system-specific keypress). The
    cell where the event took place is at \a row, \a col. \a pos is
    the position where the context menu will appear in the global
    coordinate system. This signal is always emitted, even if the
    contents of the cell are disabled.
*/

/*!
    Creates an empty table object called \a name as a child of \a
    parent.

    Call setNumRows() and setNumCols() to set the table size before
    populating the table if you're using TQTableItems.

    \sa TQWidget::clearWFlags() TQt::WidgetFlags
*/

TQTable::TQTable( TQWidget *parent, const char *name )
    : TQScrollView( parent, name, WNoAutoErase | WStaticContents ),
      leftHeader( 0 ), topHeader( 0 ),
      currentSel( 0 ), lastSortCol( -1 ), sGrid( TRUE ), mRows( FALSE ), mCols( FALSE ),
      asc( TRUE ), doSort( TRUE ), readOnly( FALSE )
{
    init( 0, 0 );
}

/*!
    Constructs an empty table called \a name with \a numRows rows and
    \a numCols columns. The table is a child of \a parent.

    If you're using \l{TQTableItem}s to populate the table's cells, you
    can create TQTableItem, TQComboTableItem and TQCheckTableItem items
    and insert them into the table using setItem(). (See the notes on
    large tables for an alternative to using TQTableItems.)

    \sa TQWidget::clearWFlags() TQt::WidgetFlags
*/

TQTable::TQTable( int numRows, int numCols, TQWidget *parent, const char *name )
    : TQScrollView( parent, name, WNoAutoErase | WStaticContents ),
      leftHeader( 0 ), topHeader( 0 ),
      currentSel( 0 ), lastSortCol( -1 ), sGrid( TRUE ), mRows( FALSE ), mCols( FALSE ),
      asc( TRUE ), doSort( TRUE ), readOnly( FALSE )
{
    init( numRows, numCols );
}

/*! \internal
*/

void TQTable::init( int rows, int cols )
{
#ifndef QT_NO_DRAGANDDROP
    setDragAutoScroll( FALSE );
#endif
    d = new TQTablePrivate;
    d->geomTimer = new TQTimer( this );
    d->lastVisCol = 0;
    d->lastVisRow = 0;
    connect( d->geomTimer, SIGNAL( timeout() ), this, SLOT( updateGeometriesSlot() ) );
    shouldClearSelection = FALSE;
    dEnabled = FALSE;
    roRows.setAutoDelete( TRUE );
    roCols.setAutoDelete( TRUE );
    setSorting( FALSE );

    unused = TRUE; // It's unused, ain't it? :)

    selMode = Multi;

    contents.setAutoDelete( TRUE );
    widgets.setAutoDelete( TRUE );

    // Enable clipper and set background mode
    enableClipper( qt_table_clipper_enabled );

    viewport()->setFocusProxy( this );
    viewport()->setFocusPolicy( WheelFocus );

    viewport()->setBackgroundMode( PaletteBase );
    setBackgroundMode( PaletteBackground, PaletteBase );
    setResizePolicy( Manual );
    selections.setAutoDelete( TRUE );

    // Create headers
    leftHeader = new TQTableHeader( rows, this, this, "left table header" );
    leftHeader->setOrientation( Vertical );
    leftHeader->setTracking( TRUE );
    leftHeader->setMovingEnabled( TRUE );
    topHeader = new TQTableHeader( cols, this, this, "right table header" );
    topHeader->setOrientation( Horizontal );
    topHeader->setTracking( TRUE );
    topHeader->setMovingEnabled( TRUE );
    if ( TQApplication::reverseLayout() )
	setMargins( 0, fontMetrics().height() + 4, 30, 0 );
    else
	setMargins( 30, fontMetrics().height() + 4, 0, 0 );

    topHeader->setUpdatesEnabled( FALSE );
    leftHeader->setUpdatesEnabled( FALSE );
    // Initialize headers
    int i = 0;
    for ( i = 0; i < numCols(); ++i )
	topHeader->resizeSection( i, TQMAX( 100, TQApplication::globalStrut().height() ) );
    for ( i = 0; i < numRows(); ++i )
	leftHeader->resizeSection( i, TQMAX( 20, TQApplication::globalStrut().width() ) );
    topHeader->setUpdatesEnabled( TRUE );
    leftHeader->setUpdatesEnabled( TRUE );

    // Prepare for contents
    contents.setAutoDelete( FALSE );

    // Connect header, table and scrollbars
    connect( horizontalScrollBar(), SIGNAL( valueChanged(int) ),
	     topHeader, SLOT( setOffset(int) ) );
    connect( verticalScrollBar(), SIGNAL( valueChanged(int) ),
	     leftHeader, SLOT( setOffset(int) ) );
    connect( topHeader, SIGNAL( sectionSizeChanged(int) ),
	     this, SLOT( columnWidthChanged(int) ) );
    connect( topHeader, SIGNAL( indexChange(int,int,int) ),
	     this, SLOT( columnIndexChanged(int,int,int) ) );
    connect( topHeader, SIGNAL( sectionClicked(int) ),
	     this, SLOT( columnClicked(int) ) );
    connect( leftHeader, SIGNAL( sectionSizeChanged(int) ),
	     this, SLOT( rowHeightChanged(int) ) );
    connect( leftHeader, SIGNAL( indexChange(int,int,int) ),
	     this, SLOT( rowIndexChanged(int,int,int) ) );

    // Initialize variables
    autoScrollTimer = new TQTimer( this );
    connect( autoScrollTimer, SIGNAL( timeout() ),
	     this, SLOT( doAutoScroll() ) );
    curRow = curCol = 0;
    topHeader->setSectionState( curCol, TQTableHeader::Bold );
    leftHeader->setSectionState( curRow, TQTableHeader::Bold );
    edMode = NotEditing;
    editRow = editCol = -1;

    drawActiveSelection = TRUE;

    installEventFilter( this );

    focusStl = SpreadSheet;

    was_visible = FALSE;

    // initial size
    resize( 640, 480 );
}

/*!
    Releases all the resources used by the TQTable object,
    including all \l{TQTableItem}s and their widgets.
*/

TQTable::~TQTable()
{
    setUpdatesEnabled( FALSE );
    contents.setAutoDelete( TRUE );
    contents.clear();
    widgets.clear();

    delete d;
}

void TQTable::setReadOnly( bool b )
{
    readOnly = b;

    TQTableItem *i = item(curRow, curCol);
    if (readOnly && isEditing()) {
	endEdit(editRow, editCol, TRUE, FALSE);
    } else if (!readOnly && i && (i->editType() == TQTableItem::WhenCurrent
				  || i->editType() == TQTableItem::Always)) {
	editCell(curRow, curCol);
    }
}

/*!
    If \a ro is TRUE, row \a row is set to be read-only; otherwise the
    row is set to be editable.

    Whether a cell in this row is editable or read-only depends on the
    cell's EditType, and this setting:
    see \link qtableitem.html#wheneditable TQTableItem::EditType\endlink.

    \sa isRowReadOnly() setColumnReadOnly() setReadOnly()
*/

void TQTable::setRowReadOnly( int row, bool ro )
{
    if ( ro )
	roRows.replace( row, new int( 0 ) );
    else
	roRows.remove( row );

    if (curRow == row) {
	TQTableItem *i = item(curRow, curCol);
	if (ro && isEditing()) {
	    endEdit(editRow, editCol, TRUE, FALSE);
	} else if (!ro && i && (i->editType() == TQTableItem::WhenCurrent
				      || i->editType() == TQTableItem::Always)) {
	    editCell(curRow, curCol);
	}
    }
}

/*!
    If \a ro is TRUE, column \a col is set to be read-only; otherwise
    the column is set to be editable.

    Whether a cell in this column is editable or read-only depends on
    the cell's EditType, and this setting:
    see \link qtableitem.html#wheneditable TQTableItem::EditType\endlink.

    \sa isColumnReadOnly() setRowReadOnly() setReadOnly()

*/

void TQTable::setColumnReadOnly( int col, bool ro )
{
    if ( ro )
	roCols.replace( col, new int( 0 ) );
    else
	roCols.remove( col );

    if (curCol == col) {
	TQTableItem *i = item(curRow, curCol);
	if (ro && isEditing()) {
	    endEdit(editRow, editCol, TRUE, FALSE);
	} else if (!ro && i && (i->editType() == TQTableItem::WhenCurrent
				      || i->editType() == TQTableItem::Always)) {
	    editCell(curRow, curCol);
	}
    }
}

/*!
    \property TQTable::readOnly
    \brief whether the table is read-only

    Whether a cell in the table is editable or read-only depends on
    the cell's \link TQTableItem::EditType EditType\endlink, and this setting:
    see \link qtableitem.html#wheneditable
    TQTableItem::EditType\endlink.

    \sa TQWidget::enabled setColumnReadOnly() setRowReadOnly()
*/

bool TQTable::isReadOnly() const
{
    return readOnly;
}

/*!
    Returns TRUE if row \a row is read-only; otherwise returns FALSE.

    Whether a cell in this row is editable or read-only depends on the
    cell's \link TQTableItem::EditType EditType\endlink, and this
    setting: see \link qtableitem.html#wheneditable
    TQTableItem::EditType\endlink.

    \sa setRowReadOnly() isColumnReadOnly()
*/

bool TQTable::isRowReadOnly( int row ) const
{
    return (roRows.find( row ) != 0);
}

/*!
    Returns TRUE if column \a col is read-only; otherwise returns
    FALSE.

    Whether a cell in this column is editable or read-only depends on
    the cell's EditType, and this setting: see \link
    qtableitem.html#wheneditable TQTableItem::EditType\endlink.

    \sa setColumnReadOnly() isRowReadOnly()
*/

bool TQTable::isColumnReadOnly( int col ) const
{
    return (roCols.find( col ) != 0);
}

void TQTable::setSelectionMode( SelectionMode mode )
{
    if ( mode == selMode )
	return;
    selMode = mode;
    clearSelection();
    if ( isRowSelection( selMode ) && numRows() > 0 && numCols() > 0 ) {
	currentSel = new TQTableSelection();
	selections.append( currentSel );
	currentSel->init( curRow, 0 );
	currentSel->expandTo( curRow, numCols() - 1 );
	repaintSelections( 0, currentSel );
    }
}

/*!
    \property TQTable::selectionMode
    \brief the current selection mode

    The default mode is \c Multi which allows the user to select
    multiple ranges of cells.

    \sa SelectionMode setSelectionMode()
*/

TQTable::SelectionMode TQTable::selectionMode() const
{
    return selMode;
}

/*!
    \property TQTable::focusStyle
    \brief how the current (focus) cell is drawn

    The default style is \c SpreadSheet.

    \sa TQTable::FocusStyle
*/

void TQTable::setFocusStyle( FocusStyle fs )
{
    focusStl = fs;
    updateCell( curRow, curCol );
}

TQTable::FocusStyle TQTable::focusStyle() const
{
    return focusStl;
}

/*!
    This functions updates all the header states to be in sync with
    the current selections. This should be called after
    programatically changing, adding or removing selections, so that
    the headers are updated.
*/

void TQTable::updateHeaderStates()
{
    horizontalHeader()->setUpdatesEnabled( FALSE );
    verticalHeader()->setUpdatesEnabled( FALSE );

    ( (TQTableHeader*)verticalHeader() )->setSectionStateToAll( TQTableHeader::Normal );
    ( (TQTableHeader*)horizontalHeader() )->setSectionStateToAll( TQTableHeader::Normal );

    TQPtrListIterator<TQTableSelection> it( selections );
    TQTableSelection *s;
    while ( ( s = it.current() ) != 0 ) {
	++it;
	if ( s->isActive() ) {
	    if ( s->leftCol() == 0 &&
		 s->rightCol() == numCols() - 1 ) {
		for ( int i = 0; i < s->bottomRow() - s->topRow() + 1; ++i )
		    leftHeader->setSectionState( s->topRow() + i, TQTableHeader::Selected );
	    }
	    if ( s->topRow() == 0 &&
		 s->bottomRow() == numRows() - 1 ) {
		for ( int i = 0; i < s->rightCol() - s->leftCol() + 1; ++i )
		    topHeader->setSectionState( s->leftCol() + i, TQTableHeader::Selected );
	    }
	}
    }

    horizontalHeader()->setUpdatesEnabled( TRUE );
    verticalHeader()->setUpdatesEnabled( TRUE );
    horizontalHeader()->repaint( FALSE );
    verticalHeader()->repaint( FALSE );
}

/*!
    Returns the table's top TQHeader.

    This header contains the column labels.

    To modify a column label use TQHeader::setLabel(), e.g.
    \quotefile table/statistics/statistics.cpp
    \skipto horizontalHeader
    \printline

    \sa verticalHeader() setTopMargin() TQHeader
*/

TQHeader *TQTable::horizontalHeader() const
{
    return (TQHeader*)topHeader;
}

/*!
    Returns the table's vertical TQHeader.

    This header contains the row labels.

    \sa horizontalHeader() setLeftMargin() TQHeader
*/

TQHeader *TQTable::verticalHeader() const
{
    return (TQHeader*)leftHeader;
}

void TQTable::setShowGrid( bool b )
{
    if ( sGrid == b )
	return;
    sGrid = b;
    updateContents();
}

/*!
    \property TQTable::showGrid
    \brief whether the table's grid is displayed

    The grid is shown by default.
*/

bool TQTable::showGrid() const
{
    return sGrid;
}

/*!
    \property TQTable::columnMovingEnabled
    \brief whether columns can be moved by the user

    The default is FALSE. Columns are moved by dragging whilst holding
    down the Ctrl key.

    \warning If TQTable is used to move header sections as a result of user
    interaction, the mapping between header indexes and section exposed by
    TQHeader will not reflect the order of the headers in the table; i.e.,
    TQTable does not call TQHeader::moveSection() to move sections but handles
    move operations internally.

    \sa rowMovingEnabled
*/

void TQTable::setColumnMovingEnabled( bool b )
{
    mCols = b;
}

bool TQTable::columnMovingEnabled() const
{
    return mCols;
}

/*!
    \property TQTable::rowMovingEnabled
    \brief whether rows can be moved by the user

    The default is FALSE. Rows are moved by dragging whilst holding
    down the Ctrl key.

    \warning If TQTable is used to move header sections as a result of user
    interaction, the mapping between header indexes and section exposed by
    TQHeader will not reflect the order of the headers in the table; i.e.,
    TQTable does not call TQHeader::moveSection() to move sections but handles
    move operations internally.

    \sa columnMovingEnabled
*/

void TQTable::setRowMovingEnabled( bool b )
{
    mRows = b;
}

bool TQTable::rowMovingEnabled() const
{
    return mRows;
}

/*!
    This is called when TQTable's internal array needs to be resized to
    \a len elements.

    If you don't use TQTableItems you should reimplement this as an
    empty method to avoid wasting memory. See the notes on large
    tables for further details.
*/

void TQTable::resizeData( int len )
{
    contents.resize( len );
    widgets.resize( len );
}

/*!
    Swaps the data in \a row1 and \a row2.

    This function is used to swap the positions of two rows. It is
    called when the user changes the order of rows (see
    setRowMovingEnabled()), and when rows are sorted.

    If you don't use \l{TQTableItem}s and want your users to be able to
    swap rows, e.g. for sorting, you will need to reimplement this
    function. (See the notes on large tables.)

    If \a swapHeader is TRUE, the rows' header contents is also
    swapped.

    This function will not update the TQTable, you will have to do
    this manually, e.g. by calling updateContents().

    \sa swapColumns() swapCells()
*/

void TQTable::swapRows( int row1, int row2, bool swapHeader )
{
    if ( swapHeader )
	leftHeader->swapSections( row1, row2, FALSE );

    TQPtrVector<TQTableItem> tmpContents;
    tmpContents.resize( numCols() );
    TQPtrVector<TQWidget> tmpWidgets;
    tmpWidgets.resize( numCols() );
    int i;

    contents.setAutoDelete( FALSE );
    widgets.setAutoDelete( FALSE );
    for ( i = 0; i < numCols(); ++i ) {
	TQTableItem *i1, *i2;
	i1 = item( row1, i );
	i2 = item( row2, i );
	if ( i1 || i2 ) {
	    tmpContents.insert( i, i1 );
	    contents.remove( indexOf( row1, i ) );
	    contents.insert( indexOf( row1, i ), i2 );
	    contents.remove( indexOf( row2, i ) );
	    contents.insert( indexOf( row2, i ), tmpContents[ i ] );
	    if ( contents[ indexOf( row1, i ) ] )
		contents[ indexOf( row1, i ) ]->setRow( row1 );
	    if ( contents[ indexOf( row2, i ) ] )
		contents[ indexOf( row2, i ) ]->setRow( row2 );
	}

	TQWidget *w1, *w2;
	w1 = cellWidget( row1, i );
	w2 = cellWidget( row2, i );
	if ( w1 || w2 ) {
	    tmpWidgets.insert( i, w1 );
	    widgets.remove( indexOf( row1, i ) );
	    widgets.insert( indexOf( row1, i ), w2 );
	    widgets.remove( indexOf( row2, i ) );
	    widgets.insert( indexOf( row2, i ), tmpWidgets[ i ] );
	}
    }
    contents.setAutoDelete( FALSE );
    widgets.setAutoDelete( TRUE );

    updateRowWidgets( row1 );
    updateRowWidgets( row2 );
    if ( curRow == row1 )
	curRow = row2;
    else if ( curRow == row2 )
	curRow = row1;
    if ( editRow == row1 )
	editRow = row2;
    else if ( editRow == row2 )
	editRow = row1;
}

/*!
    Sets the left margin to be \a m pixels wide.

    The verticalHeader(), which displays row labels, occupies this
    margin.

    In an Arabic or Hebrew localization, the verticalHeader() will
    appear on the right side of the table, and this call will set the
    right margin.

    \sa leftMargin() setTopMargin() verticalHeader()
*/

void TQTable::setLeftMargin( int m )
{
    if ( TQApplication::reverseLayout() )
	setMargins( leftMargin(), topMargin(), m, bottomMargin() );
    else
	setMargins( m, topMargin(), rightMargin(), bottomMargin() );
    updateGeometries();
}

/*!
    Sets the top margin to be \a m pixels high.

    The horizontalHeader(), which displays column labels, occupies
    this margin.

    \sa topMargin() setLeftMargin()
*/

void TQTable::setTopMargin( int m )
{
    setMargins( leftMargin(), m, rightMargin(), bottomMargin() );
    updateGeometries();
}

/*!
    Swaps the data in \a col1 with \a col2.

    This function is used to swap the positions of two columns. It is
    called when the user changes the order of columns (see
    setColumnMovingEnabled(), and when columns are sorted.

    If you don't use \l{TQTableItem}s and want your users to be able to
    swap columns you will need to reimplement this function. (See the
    notes on large tables.)

    If \a swapHeader is TRUE, the columns' header contents is also
    swapped.

    \sa swapCells()
*/

void TQTable::swapColumns( int col1, int col2, bool swapHeader )
{
    if ( swapHeader )
	topHeader->swapSections( col1, col2, FALSE );

    TQPtrVector<TQTableItem> tmpContents;
    tmpContents.resize( numRows() );
    TQPtrVector<TQWidget> tmpWidgets;
    tmpWidgets.resize( numRows() );
    int i;

    contents.setAutoDelete( FALSE );
    widgets.setAutoDelete( FALSE );
    for ( i = 0; i < numRows(); ++i ) {
	TQTableItem *i1, *i2;
	i1 = item( i, col1 );
	i2 = item( i, col2 );
	if ( i1 || i2 ) {
	    tmpContents.insert( i, i1 );
	    contents.remove( indexOf( i, col1 ) );
	    contents.insert( indexOf( i, col1 ), i2 );
	    contents.remove( indexOf( i, col2 ) );
	    contents.insert( indexOf( i, col2 ), tmpContents[ i ] );
	    if ( contents[ indexOf( i, col1 ) ] )
		contents[ indexOf( i, col1 ) ]->setCol( col1 );
	    if ( contents[ indexOf( i, col2 ) ] )
		contents[ indexOf( i, col2 ) ]->setCol( col2 );
	}

	TQWidget *w1, *w2;
	w1 = cellWidget( i, col1 );
	w2 = cellWidget( i, col2 );
	if ( w1 || w2 ) {
	    tmpWidgets.insert( i, w1 );
	    widgets.remove( indexOf( i, col1 ) );
	    widgets.insert( indexOf( i, col1 ), w2 );
	    widgets.remove( indexOf( i, col2 ) );
	    widgets.insert( indexOf( i, col2 ), tmpWidgets[ i ] );
	}
    }
    contents.setAutoDelete( FALSE );
    widgets.setAutoDelete( TRUE );

    columnWidthChanged( col1 );
    columnWidthChanged( col2 );
    if ( curCol == col1 )
	curCol = col2;
    else if ( curCol == col2 )
	curCol = col1;
    if ( editCol == col1 )
	editCol = col2;
    else if ( editCol == col2 )
	editCol = col1;
}

/*!
    Swaps the contents of the cell at \a row1, \a col1 with the
    contents of the cell at \a row2, \a col2.

    This function is also called when the table is sorted.

    If you don't use \l{TQTableItem}s and want your users to be able to
    swap cells, you will need to reimplement this function. (See the
    notes on large tables.)

    \sa swapColumns() swapRows()
*/

void TQTable::swapCells( int row1, int col1, int row2, int col2 )
{
    contents.setAutoDelete( FALSE );
    widgets.setAutoDelete( FALSE );
    TQTableItem *i1, *i2;
    i1 = item( row1, col1 );
    i2 = item( row2, col2 );
    if ( i1 || i2 ) {
	TQTableItem *tmp = i1;
	contents.remove( indexOf( row1, col1 ) );
	contents.insert( indexOf( row1, col1 ), i2 );
	contents.remove( indexOf( row2, col2 ) );
	contents.insert( indexOf( row2, col2 ), tmp );
	if ( contents[ indexOf( row1, col1 ) ] ) {
	    contents[ indexOf( row1, col1 ) ]->setRow( row1 );
	    contents[ indexOf( row1, col1 ) ]->setCol( col1 );
	}
	if ( contents[ indexOf( row2, col2 ) ] ) {
	    contents[ indexOf( row2, col2 ) ]->setRow( row2 );
	    contents[ indexOf( row2, col2 ) ]->setCol( col2 );
	}
    }

    TQWidget *w1, *w2;
    w1 = cellWidget( row1, col1 );
    w2 = cellWidget( row2, col2 );
    if ( w1 || w2 ) {
	TQWidget *tmp = w1;
	widgets.remove( indexOf( row1, col1 ) );
	widgets.insert( indexOf( row1, col1 ), w2 );
	widgets.remove( indexOf( row2, col2 ) );
	widgets.insert( indexOf( row2, col2 ), tmp );
    }

    updateRowWidgets( row1 );
    updateRowWidgets( row2 );
    updateColWidgets( col1 );
    updateColWidgets( col2 );
    contents.setAutoDelete( FALSE );
    widgets.setAutoDelete( TRUE );
}

static bool is_child_of( TQWidget *child, TQWidget *parent )
{
    while ( child ) {
	if ( child == parent )
	    return TRUE;
	child = child->parentWidget();
    }
    return FALSE;
}

/*!
    Draws the table contents on the painter \a p. This function is
    optimized so that it only draws the cells inside the \a cw pixels
    wide and \a ch pixels high clipping rectangle at position \a cx,
    \a cy.

    Additionally, drawContents() highlights the current cell.
*/

void TQTable::drawContents( TQPainter *p, int cx, int cy, int cw, int ch )
{
    int colfirst = columnAt( cx );
    int collast = columnAt( cx + cw );
    int rowfirst = rowAt( cy );
    int rowlast = rowAt( cy + ch );

    if ( rowfirst == -1 || colfirst == -1 ) {
	paintEmptyArea( p, cx, cy, cw, ch );
	return;
    }

    drawActiveSelection = hasFocus() || viewport()->hasFocus() || d->inMenuMode
			|| is_child_of( qApp->focusWidget(), viewport() )
			|| !style().styleHint( TQStyle::SH_ItemView_ChangeHighlightOnFocus, this );
    if ( rowlast == -1 )
	rowlast = numRows() - 1;
    if ( collast == -1 )
	collast = numCols() - 1;

    bool currentInSelection = FALSE;

    TQPtrListIterator<TQTableSelection> it( selections );
    TQTableSelection *s;
    while ( ( s = it.current() ) != 0 ) {
	++it;
	if ( s->isActive() &&
	     curRow >= s->topRow() &&
	     curRow <= s->bottomRow() &&
	     curCol >= s->leftCol() &&
	     curCol <= s->rightCol() ) {
	    currentInSelection = s->topRow() != curRow || s->bottomRow() != curRow || s->leftCol() != curCol || s->rightCol() != curCol;
	    break;
	}
    }

    // Go through the rows
    for ( int r = rowfirst; r <= rowlast; ++r ) {
	// get row position and height
	int rowp = rowPos( r );
	int rowh = rowHeight( r );

	// Go through the columns in row r
	// if we know from where to where, go through [colfirst, collast],
	// else go through all of them
	for ( int c = colfirst; c <= collast; ++c ) {
	    // get position and width of column c
	    int colp, colw;
	    colp = columnPos( c );
	    colw = columnWidth( c );
	    int oldrp = rowp;
	    int oldrh = rowh;

	    TQTableItem *itm = item( r, c );
	    if ( itm &&
		 ( itm->colSpan() > 1 || itm->rowSpan() > 1 ) ) {
		bool goon = r == itm->row() && c == itm->col() ||
			r == rowfirst && c == itm->col() ||
			r == itm->row() && c == colfirst;
		if ( !goon )
		    continue;
		rowp = rowPos( itm->row() );
		rowh = 0;
		int i;
		for ( i = 0; i < itm->rowSpan(); ++i )
		    rowh += rowHeight( i + itm->row() );
		colp = columnPos( itm->col() );
		colw = 0;
		for ( i = 0; i < itm->colSpan(); ++i )
		    colw += columnWidth( i + itm->col() );
	    }

	    // Translate painter and draw the cell
	    p->translate( colp, rowp );
	    bool selected = isSelected( r, c );
	    if ( focusStl != FollowStyle && selected && !currentInSelection &&
		 r == curRow && c == curCol  )
		selected = FALSE;
	    paintCell( p, r, c, TQRect( colp, rowp, colw, rowh ), selected );
	    p->translate( -colp, -rowp );

	    rowp = oldrp;
	    rowh = oldrh;

	    TQWidget *w = cellWidget( r, c );
	    TQRect cg( cellGeometry( r, c ) );
	    if ( w && w->geometry() != TQRect( contentsToViewport( cg.topLeft() ), cg.size() - TQSize( 1, 1 ) ) ) {
		moveChild( w, colp, rowp );
		w->resize( cg.size() - TQSize( 1, 1 ) );
	    }
	}
    }
    d->lastVisCol = collast;
    d->lastVisRow = rowlast;

    // draw indication of current cell
    TQRect focusRect = cellGeometry( curRow, curCol );
    p->translate( focusRect.x(), focusRect.y() );
    paintFocus( p, focusRect );
    p->translate( -focusRect.x(), -focusRect.y() );

    // Paint empty rects
    paintEmptyArea( p, cx, cy, cw, ch );

    drawActiveSelection = TRUE;
}

/*!
    \reimp

    (Implemented to get rid of a compiler warning.)
*/

void TQTable::drawContents( TQPainter * )
{
}

/*!
    Returns the geometry of cell \a row, \a col in the cell's
    coordinate system. This is a convenience function useful in
    paintCell(). It is equivalent to TQRect( TQPoint(0,0), cellGeometry(
    row, col).size() );

    \sa cellGeometry()
*/

TQRect TQTable::cellRect( int row, int col ) const
{
    return TQRect( TQPoint(0,0), cellGeometry( row, col ).size() );
}

/*!
    \overload

    Use the other paintCell() function. This function is only included
    for backwards compatibilty.
*/

void TQTable::paintCell( TQPainter* p, int row, int col,
			const TQRect &cr, bool selected )
{
    if ( cr.width() == 0 || cr.height() == 0 )
	return;
#if defined(Q_WS_WIN)
    const TQColorGroup &cg = ( !drawActiveSelection && style().styleHint( TQStyle::SH_ItemView_ChangeHighlightOnFocus ) ? palette().inactive() : colorGroup() );
#else
    const TQColorGroup &cg = colorGroup();
#endif

    TQTableItem *itm = item( row, col );
    TQColorGroup cg2( cg );
    if ( itm && !itm->isEnabled() )
	cg2 = palette().disabled();

    paintCell( p, row, col, cr, selected, cg2 );
}

/*!
    Paints the cell at \a row, \a col on the painter \a p. The painter
    has already been translated to the cell's origin. \a cr describes
    the cell coordinates in the content coordinate system.

    If \a selected is TRUE the cell is highlighted.

    \a cg is the colorgroup which should be used to draw the cell
    content.

    If you want to draw custom cell content, for example right-aligned
    text, you must either reimplement paintCell(), or subclass
    TQTableItem and reimplement TQTableItem::paint() to do the custom
    drawing.

    If you're using a TQTableItem subclass, for example, to store a
    data structure, then reimplementing TQTableItem::paint() may be the
    best approach. For data you want to draw immediately, e.g. data
    retrieved from a database, it is probably best to reimplement
    paintCell(). Note that if you reimplement paintCell(), i.e. don't
    use \l{TQTableItem}s, you must reimplement other functions: see the
    notes on large tables.

    Note that the painter is not clipped by default in order to get
    maximum efficiency. If you want clipping, use code like this:

    \code
    p->setClipRect( cellRect(row, col), TQPainter::CoordPainter );
    //... your drawing code
    p->setClipping( FALSE );
    \endcode
*/

void TQTable::paintCell( TQPainter *p, int row, int col,
			const TQRect &cr, bool selected, const TQColorGroup &cg )
{
    if ( focusStl == SpreadSheet && selected &&
	 row == curRow &&
	 col == curCol && ( hasFocus() || viewport()->hasFocus() ) )
	selected = FALSE;

    int w = cr.width();
    int h = cr.height();
    int x2 = w - 1;
    int y2 = h - 1;


    TQTableItem *itm = item( row, col );
    if ( itm ) {
	p->save();
	itm->paint( p, cg, cr, selected );
	p->restore();
    } else {
	p->fillRect( 0, 0, w, h, selected ? cg.brush( TQColorGroup::Highlight ) : cg.brush( TQColorGroup::Base ) );
    }

    if ( sGrid ) {
	// Draw our lines
        TQPen pen( p->pen() );
        int gridColor = style().styleHint( TQStyle::SH_Table_GridLineColor, this );
        if (gridColor != -1) {
            const TQPalette &pal = palette();
            if (cg != colorGroup()
                && cg != pal.disabled()
                && cg != pal.inactive())
	        p->setPen(cg.mid());
            else
                p->setPen((TQRgb)gridColor);
        } else {
            p->setPen(cg.mid());
        }
        p->drawLine( x2, 0, x2, y2 );
	p->drawLine( 0, y2, x2, y2 );
	p->setPen( pen );
    }
}

/*!
    Draws the focus rectangle of the current cell (see currentRow(),
    currentColumn()).

    The painter \a p is already translated to the cell's origin, while
    \a cr specifies the cell's geometry in content coordinates.
*/

void TQTable::paintFocus( TQPainter *p, const TQRect &cr )
{
    if ( !hasFocus() && !viewport()->hasFocus() )
	return;
    TQRect focusRect( 0, 0, cr.width(), cr.height() );
    if ( focusStyle() == SpreadSheet ) {
	p->setPen( TQPen( black, 1 ) );
	p->setBrush( NoBrush );
	p->drawRect( focusRect.x(), focusRect.y(), focusRect.width() - 1, focusRect.height() - 1 );
	p->drawRect( focusRect.x() - 1, focusRect.y() - 1, focusRect.width() + 1, focusRect.height() + 1 );
    } else {
	TQColor c = isSelected( curRow, curCol, FALSE ) ?
			     colorGroup().highlight() : colorGroup().base();
	style().drawPrimitive( TQStyle::PE_FocusRect, p, focusRect, colorGroup(),
			       ( isSelected( curRow, curCol, FALSE ) ?
				 TQStyle::Style_FocusAtBorder :
				 TQStyle::Style_Default ),
			         TQStyleOption(c) );
    }
}

/*!
    This function fills the \a cw pixels wide and \a ch pixels high
    rectangle starting at position \a cx, \a cy with the background
    color using the painter \a p.

    paintEmptyArea() is invoked by drawContents() to erase or fill
    unused areas.
*/

void TQTable::paintEmptyArea( TQPainter *p, int cx, int cy, int cw, int ch )
{
    // Regions work with shorts, so avoid an overflow and adjust the
    // table size to the visible size
    TQSize ts( tableSize() );
    ts.setWidth( TQMIN( ts.width(), visibleWidth() ) );
    ts.setHeight( TQMIN( ts.height(), visibleHeight() ) );

    // Region of the rect we should draw, calculated in viewport
    // coordinates, as a region can't handle bigger coordinates
    contentsToViewport2( cx, cy, cx, cy );
    TQRegion reg( TQRect( cx, cy, cw, ch ) );

    // Subtract the table from it
    reg = reg.subtract( TQRect( TQPoint( 0, 0 ), ts ) );

    // And draw the rectangles (transformed inc contents coordinates as needed)
    TQMemArray<TQRect> r = reg.rects();
    for ( int i = 0; i < (int)r.count(); ++i )
	p->fillRect( TQRect(viewportToContents2(r[i].topLeft()),r[i].size()), viewport()->backgroundBrush() );
}

/*!
    Returns the TQTableItem representing the contents of the cell at \a
    row, \a col.

    If \a row or \a col are out of range or no content has been set
    for this cell, item() returns 0.

    If you don't use \l{TQTableItem}s you may need to reimplement this
    function: see the notes on large tables.

    \sa setItem()
*/

TQTableItem *TQTable::item( int row, int col ) const
{
    if ( row < 0 || col < 0 || row > numRows() - 1 ||
	 col > numCols() - 1 || row * col >= (int)contents.size() )
	return 0;

    return contents[ indexOf( row, col ) ];	// contents array lookup
}

/*!
    Inserts the table item \a item into the table at row \a row,
    column \a col, and repaints the cell. If a table item already
    exists in this cell it is deleted and replaced with \a item. The
    table takes ownership of the table item.

    If you don't use \l{TQTableItem}s you may need to reimplement this
    function: see the notes on large tables.

    \sa item() takeItem()
*/

void TQTable::setItem( int row, int col, TQTableItem *item )
{
    if ( !item )
	return;

    if ( (int)contents.size() != numRows() * numCols() )
	resizeData( numRows() * numCols() );

    int orow = item->row();
    int ocol = item->col();
    clearCell( row, col );

    contents.insert( indexOf( row, col ), item );
    item->setRow( row );
    item->setCol( col );
    item->t = this;
    updateCell( row, col );
    if ( qt_update_cell_widget )
	item->updateEditor( orow, ocol );

    if ( row == curRow && col == curCol && item->editType() == TQTableItem::WhenCurrent ) {
	if ( beginEdit( row, col, FALSE ) )
	    setEditMode( Editing, row, col );
    }
}

/*!
    Removes the TQTableItem at \a row, \a col.

    If you don't use \l{TQTableItem}s you may need to reimplement this
    function: see the notes on large tables.
*/

void TQTable::clearCell( int row, int col )
{
    if ( (int)contents.size() != numRows() * numCols() )
	resizeData( numRows() * numCols() );
    clearCellWidget( row, col );
    contents.setAutoDelete( TRUE );
    contents.remove( indexOf( row, col ) );
    contents.setAutoDelete( FALSE );
}

/*!
    Sets the text in the cell at \a row, \a col to \a text.

    If the cell does not contain a table item a TQTableItem is created
    with an \link TQTableItem::EditType EditType\endlink of \c OnTyping,
    otherwise the existing table item's text (if any) is replaced with
    \a text.

    \sa text() setPixmap() setItem() TQTableItem::setText()
*/

void TQTable::setText( int row, int col, const TQString &text )
{
    TQTableItem *itm = item( row, col );
    if ( itm ) {
	itm->setText( text );
	itm->updateEditor( row, col );
	updateCell( row, col );
    } else {
	TQTableItem *i = new TQTableItem( this, TQTableItem::OnTyping,
					text, TQPixmap() );
	setItem( row, col, i );
    }
}

/*!
    Sets the pixmap in the cell at \a row, \a col to \a pix.

    If the cell does not contain a table item a TQTableItem is created
    with an \link TQTableItem::EditType EditType\endlink of \c OnTyping,
    otherwise the existing table item's pixmap (if any) is replaced
    with \a pix.

    Note that \l{TQComboTableItem}s and \l{TQCheckTableItem}s don't show
    pixmaps.

    \sa pixmap() setText() setItem() TQTableItem::setPixmap()
*/

void TQTable::setPixmap( int row, int col, const TQPixmap &pix )
{
    TQTableItem *itm = item( row, col );
    if ( itm ) {
	itm->setPixmap( pix );
	updateCell( row, col );
    } else {
	TQTableItem *i = new TQTableItem( this, TQTableItem::OnTyping,
					TQString::null, pix );
	setItem( row, col, i );
    }
}

/*!
    Returns the text in the cell at \a row, \a col, or TQString::null
    if the relevant item does not exist or has no text.

    \sa setText() setPixmap()
*/

TQString TQTable::text( int row, int col ) const
{
    TQTableItem *itm = item( row, col );
    if ( itm )
	return itm->text();
    return TQString::null;
}

/*!
    Returns the pixmap set for the cell at \a row, \a col, or a
    null-pixmap if the cell contains no pixmap.

    \sa setPixmap()
*/

TQPixmap TQTable::pixmap( int row, int col ) const
{
    TQTableItem *itm = item( row, col );
    if ( itm )
	return itm->pixmap();
    return TQPixmap();
}

/*!
    Moves the focus to the cell at \a row, \a col.

    \sa currentRow() currentColumn()
*/

void TQTable::setCurrentCell( int row, int col )
{
    setCurrentCell( row, col, TRUE, TRUE );
}

// need to use a define, as leftMargin() is protected
#define VERTICALMARGIN \
( TQApplication::reverseLayout() ? \
       rightMargin() \
       : \
       leftMargin() \
)

/*! \internal */

void TQTable::setCurrentCell( int row, int col, bool updateSelections, bool ensureVisible )
{
    TQTableItem *oldItem = item( curRow, curCol );

    if ( row > numRows() - 1 )
	row = numRows() - 1;
    if ( col > numCols() - 1 )
	col = numCols() - 1;

    if ( curRow == row && curCol == col )
	return;


    TQTableItem *itm = oldItem;
    if ( itm && itm->editType() != TQTableItem::Always && itm->editType() != TQTableItem::Never )
	endEdit( itm->row(), itm->col(), TRUE, FALSE );
    int oldRow = curRow;
    int oldCol = curCol;
    curRow = row;
    curCol = col;
    repaintCell( oldRow, oldCol );
    repaintCell( curRow, curCol );
    if ( ensureVisible )
        ensureCellVisible( curRow, curCol );
    emit currentChanged( row, col );

    if ( oldCol != curCol ) {
	if ( !isColumnSelected( oldCol ) )
	    topHeader->setSectionState( oldCol, TQTableHeader::Normal );
	else if ( isRowSelection( selectionMode() ) )
	    topHeader->setSectionState( oldCol, TQTableHeader::Selected );
	topHeader->setSectionState( curCol, isColumnSelected( curCol, TRUE ) ?
				    TQTableHeader::Selected : TQTableHeader::Bold );
    }

    if ( oldRow != curRow ) {
	if ( !isRowSelected( oldRow ) )
	    leftHeader->setSectionState( oldRow, TQTableHeader::Normal );
	leftHeader->setSectionState( curRow, isRowSelected( curRow, TRUE ) ?
				     TQTableHeader::Selected : TQTableHeader::Bold );
    }

    itm = item( curRow, curCol );

    TQPoint cellPos( columnPos( curCol ) + leftMargin() - contentsX(),
		    rowPos( curRow ) + topMargin() - contentsY() );
    setMicroFocusHint( cellPos.x(), cellPos.y(), columnWidth( curCol ),
		       rowHeight( curRow ), ( itm && itm->editType() != TQTableItem::Never ) );

    if ( cellWidget( oldRow, oldCol ) &&
	 cellWidget( oldRow, oldCol )->hasFocus() )
	viewport()->setFocus();

    if ( itm && itm->editType() == TQTableItem::WhenCurrent ) {
	if ( beginEdit( itm->row(), itm->col(), FALSE ) )
	    setEditMode( Editing, itm->row(), itm->col() );
    } else if ( itm && itm->editType() == TQTableItem::Always ) {
	if ( cellWidget( itm->row(), itm->col() ) )
	    cellWidget( itm->row(), itm->col() )->setFocus();
    }

    if ( updateSelections && isRowSelection( selectionMode() ) &&
	 !isSelected( curRow, curCol, FALSE ) ) {
	if ( selectionMode() == TQTable::SingleRow )
	    clearSelection();
	currentSel = new TQTableSelection();
	selections.append( currentSel );
	currentSel->init( curRow, 0 );
	currentSel->expandTo( curRow, numCols() - 1 );
	repaintSelections( 0, currentSel );
    }
}

/*!
    Scrolls the table until the cell at \a row, \a col becomes
    visible.
*/

void TQTable::ensureCellVisible( int row, int col )
{
    if ( !isUpdatesEnabled() || !viewport()->isUpdatesEnabled() )
	return;
    int cw = columnWidth( col );
    int rh = rowHeight( row );
    if ( cw < visibleWidth() )
	ensureVisible( columnPos( col ) + cw / 2, rowPos( row ) + rh / 2, cw / 2, rh / 2 );
    else
	ensureVisible( columnPos( col ), rowPos( row ) + rh / 2, 0, rh / 2 );
}

/*!
    Returns TRUE if the cell at \a row, \a col is selected; otherwise
    returns FALSE.

    \sa isRowSelected() isColumnSelected()
*/

bool TQTable::isSelected( int row, int col ) const
{
    return isSelected( row, col, TRUE );
}

/*! \internal */

bool TQTable::isSelected( int row, int col, bool includeCurrent ) const
{
    TQPtrListIterator<TQTableSelection> it( selections );
    TQTableSelection *s;
    while ( ( s = it.current() ) != 0 ) {
	++it;
	if ( s->isActive() &&
	     row >= s->topRow() &&
	     row <= s->bottomRow() &&
	     col >= s->leftCol() &&
	     col <= s->rightCol() )
	    return TRUE;
	if ( includeCurrent && row == currentRow() && col == currentColumn() )
	    return TRUE;
    }
    return FALSE;
}

/*!
    Returns TRUE if row \a row is selected; otherwise returns FALSE.

    If \a full is FALSE (the default), 'row is selected' means that at
    least one cell in the row is selected. If \a full is TRUE, then 'row
    is selected' means every cell in the row is selected.

    \sa isColumnSelected() isSelected()
*/

bool TQTable::isRowSelected( int row, bool full ) const
{
    if ( !full ) {
	TQPtrListIterator<TQTableSelection> it( selections );
	TQTableSelection *s;
	while ( ( s = it.current() ) != 0 ) {
	    ++it;
	    if ( s->isActive() &&
		 row >= s->topRow() &&
		 row <= s->bottomRow() )
	    return TRUE;
	if ( row == currentRow() )
	    return TRUE;
	}
    } else {
	TQPtrListIterator<TQTableSelection> it( selections );
	TQTableSelection *s;
	while ( ( s = it.current() ) != 0 ) {
	    ++it;
	    if ( s->isActive() &&
		 row >= s->topRow() &&
		 row <= s->bottomRow() &&
		 s->leftCol() == 0 &&
		 s->rightCol() == numCols() - 1 )
		return TRUE;
	}
    }
    return FALSE;
}

/*!
    Returns TRUE if column \a col is selected; otherwise returns FALSE.

    If \a full is FALSE (the default), 'column is selected' means that
    at least one cell in the column is selected. If \a full is TRUE,
    then 'column is selected' means every cell in the column is
    selected.

    \sa isRowSelected() isSelected()
*/

bool TQTable::isColumnSelected( int col, bool full ) const
{
    if ( !full ) {
	TQPtrListIterator<TQTableSelection> it( selections );
	TQTableSelection *s;
	while ( ( s = it.current() ) != 0 ) {
	    ++it;
	    if ( s->isActive() &&
		 col >= s->leftCol() &&
		 col <= s->rightCol() )
	    return TRUE;
	if ( col == currentColumn() )
	    return TRUE;
	}
    } else {
	TQPtrListIterator<TQTableSelection> it( selections );
	TQTableSelection *s;
	while ( ( s = it.current() ) != 0 ) {
	    ++it;
	    if ( s->isActive() &&
		 col >= s->leftCol() &&
		 col <= s->rightCol() &&
		 s->topRow() == 0 &&
		 s->bottomRow() == numRows() - 1 )
		return TRUE;
	}
    }
    return FALSE;
}

/*!
    \property TQTable::numSelections
    \brief The number of selections.

    \sa currentSelection()
*/

int TQTable::numSelections() const
{
    return selections.count();
}

/*!
    Returns selection number \a num, or an inactive TQTableSelection if \a
    num is out of range (see TQTableSelection::isActive()).
*/

TQTableSelection TQTable::selection( int num ) const
{
    if ( num < 0 || num >= (int)selections.count() )
	return TQTableSelection();

    TQTableSelection *s = ( (TQTable*)this )->selections.at( num );
    return *s;
}

/*!
    Adds a selection described by \a s to the table and returns its
    number or -1 if the selection is invalid.

    Remember to call TQTableSelection::init() and
    TQTableSelection::expandTo() to make the selection valid (see also
    TQTableSelection::isActive(), or use the
    TQTableSelection(int,int,int,int) constructor).

    \sa numSelections() removeSelection() clearSelection()
*/

int TQTable::addSelection( const TQTableSelection &s )
{
    if ( !s.isActive() )
	return -1;

    const int maxr = numRows()-1;
    const int maxc = numCols()-1;
    TQTableSelection *sel = new TQTableSelection( TQMIN(s.anchorRow(), maxr), TQMIN(s.anchorCol(), maxc),
				    TQMIN(s.bottomRow(), maxr), TQMIN(s.rightCol(), maxc) );

    selections.append( sel );

    repaintSelections( 0, sel, TRUE, TRUE );

    emit selectionChanged();

    return selections.count() - 1;
}

/*!
    If the table has a selection, \a s, this selection is removed from
    the table.

    \sa addSelection() numSelections()
*/

void TQTable::removeSelection( const TQTableSelection &s )
{
    selections.setAutoDelete( FALSE );
    for ( TQTableSelection *sel = selections.first(); sel; sel = selections.next() ) {
	if ( s == *sel ) {
	    selections.removeRef( sel );
	    repaintSelections( sel, 0, TRUE, TRUE );
	    if ( sel == currentSel )
		currentSel = 0;
	    delete sel;
	}
    }
    selections.setAutoDelete( TRUE );
    emit selectionChanged();
}

/*!
    \overload

    Removes selection number \a num from the table.

    \sa numSelections() addSelection() clearSelection()
*/

void TQTable::removeSelection( int num )
{
    if ( num < 0 || num >= (int)selections.count() )
	return;

    TQTableSelection *s = selections.at( num );
    if ( s == currentSel )
	currentSel = 0;
    selections.removeRef( s );
    repaintContents( FALSE );
}

/*!
    Returns the number of the current selection or -1 if there is no
    current selection.

    \sa numSelections()
*/

int TQTable::currentSelection() const
{
    if ( !currentSel )
	return -1;
    return ( (TQTable*)this )->selections.findRef( currentSel );
}

/*! Selects the range starting at \a start_row and \a start_col and
  ending at \a end_row and \a end_col.

  \sa TQTableSelection
*/

void TQTable::selectCells( int start_row, int start_col, int end_row, int end_col )
{
    const int maxr = numRows()-1;
    const int maxc = numCols()-1;

    start_row = TQMIN( maxr, TQMAX( 0, start_row ) );
    start_col = TQMIN( maxc, TQMAX( 0, start_col ) );
    end_row = TQMIN( maxr, end_row );
    end_col = TQMIN( maxc, end_col );
    TQTableSelection sel( start_row, start_col, end_row, end_col );
    addSelection( sel );
}

/*! Selects the row \a row.

  \sa TQTableSelection
*/

// ### Make this virtual in 4.0 and remove hack for TQDataTable
void TQTable::selectRow( int row )
{
    row = TQMIN(numRows()-1, row);
    if ( row < 0 )
	return;
    bool isDataTable = FALSE;
#ifndef QT_NO_SQL
    isDataTable = ::qt_cast<TQDataTable*>(this) != 0;
#endif
    if ( isDataTable || selectionMode() == SingleRow ) {
	setCurrentCell( row, currentColumn() );
    } else {
	TQTableSelection sel( row, 0, row, numCols() - 1 );
	addSelection( sel );
    }
}

/*! Selects the column \a col.

  \sa TQTableSelection
*/

// ### Make this virtual in 4.0
void TQTable::selectColumn( int col )
{
    col = TQMIN(numCols()-1, col);
    TQTableSelection sel( 0, col, numRows() - 1, col );
    addSelection( sel );
}

/*! \reimp
*/
void TQTable::contentsMousePressEvent( TQMouseEvent* e )
{
    contentsMousePressEventEx( e );
}

void TQTable::contentsMousePressEventEx( TQMouseEvent* e )
{
    shouldClearSelection = FALSE;
    if ( isEditing() ) {
	if ( !cellGeometry( editRow, editCol ).contains( e->pos() ) ) {
	    endEdit( editRow, editCol, TRUE, edMode != Editing );
	} else {
	    e->ignore();
	    return;
	}
    }

    d->redirectMouseEvent = FALSE;

    int tmpRow = rowAt( e->pos().y() );
    int tmpCol = columnAt( e->pos().x() );
    pressedRow = tmpRow;
    pressedCol = tmpCol;
    fixRow( tmpRow, e->pos().y() );
    fixCol( tmpCol, e->pos().x() );
    startDragCol = -1;
    startDragRow = -1;

    if ( isSelected( tmpRow, tmpCol ) ) {
	startDragCol = tmpCol;
	startDragRow = tmpRow;
	dragStartPos = e->pos();
    }

    TQTableItem *itm = item( pressedRow, pressedCol );
    if ( itm && !itm->isEnabled() ) {
	emit pressed( tmpRow, tmpCol, e->button(), e->pos() );
	return;
    }

    if ( ( e->state() & ShiftButton ) == ShiftButton ) {
  	int oldRow = curRow;
  	int oldCol = curCol;
	setCurrentCell( tmpRow, tmpCol, selMode == SingleRow, TRUE );
	if ( selMode != NoSelection && selMode != SingleRow ) {
	    if ( !currentSel ) {
		currentSel = new TQTableSelection();
		selections.append( currentSel );
		if ( !isRowSelection( selectionMode() ) )
		    currentSel->init( oldRow, oldCol );
		else
		    currentSel->init( oldRow, 0 );
	    }
	    TQTableSelection oldSelection = *currentSel;
	    if ( !isRowSelection( selectionMode() ) )
		currentSel->expandTo( tmpRow, tmpCol );
	    else
		currentSel->expandTo( tmpRow, numCols() - 1 );
	    repaintSelections( &oldSelection, currentSel );
	    emit selectionChanged();
	}
    } else if ( ( e->state() & ControlButton ) == ControlButton ) {
	setCurrentCell( tmpRow, tmpCol, FALSE, TRUE );
	if ( selMode != NoSelection ) {
	    if ( selMode == Single || selMode == SingleRow && !isSelected( tmpRow, tmpCol, FALSE ) )
		clearSelection();
	    if ( !(selMode == SingleRow && isSelected( tmpRow, tmpCol, FALSE )) ) {
		currentSel = new TQTableSelection();
		selections.append( currentSel );
		if ( !isRowSelection( selectionMode() ) ) {
		    currentSel->init( tmpRow, tmpCol );
		    currentSel->expandTo( tmpRow, tmpCol );
		} else {
		    currentSel->init( tmpRow, 0 );
		    currentSel->expandTo( tmpRow, numCols() - 1 );
		    repaintSelections( 0, currentSel );
		}
		emit selectionChanged();
	    }
	}
    } else {
	setCurrentCell( tmpRow, tmpCol, FALSE, TRUE );
	TQTableItem *itm = item( tmpRow, tmpCol );
	if ( itm && itm->editType() == TQTableItem::WhenCurrent ) {
	    TQWidget *w = cellWidget( tmpRow, tmpCol );
	    if ( ::qt_cast<TQComboBox*>(w) || ::qt_cast<TQButton*>(w) ) {
		TQMouseEvent ev( e->type(), w->mapFromGlobal( e->globalPos() ),
				e->globalPos(), e->button(), e->state() );
		TQApplication::sendPostedEvents( w, 0 );
		TQApplication::sendEvent( w, &ev );
		d->redirectMouseEvent = TRUE;
	    }
	}
	if ( isSelected( tmpRow, tmpCol, FALSE ) ) {
	    shouldClearSelection = TRUE;
	} else {
	    bool b = signalsBlocked();
	    if ( selMode != NoSelection )
		blockSignals( TRUE );
	    clearSelection();
	    blockSignals( b );
	    if ( selMode != NoSelection ) {
		currentSel = new TQTableSelection();
		selections.append( currentSel );
		if ( !isRowSelection( selectionMode() ) ) {
		    currentSel->init( tmpRow, tmpCol );
		    currentSel->expandTo( tmpRow, tmpCol );
		} else {
		    currentSel->init( tmpRow, 0 );
		    currentSel->expandTo( tmpRow, numCols() - 1 );
		    repaintSelections( 0, currentSel );
		}
		emit selectionChanged();
	    }
	}
    }

    emit pressed( tmpRow, tmpCol, e->button(), e->pos() );
}

/*! \reimp
*/

void TQTable::contentsMouseDoubleClickEvent( TQMouseEvent *e )
{
    if ( e->button() != LeftButton )
	return;
    if ( !isRowSelection( selectionMode() ) )
	clearSelection();
    int tmpRow = rowAt( e->pos().y() );
    int tmpCol = columnAt( e->pos().x() );
    TQTableItem *itm = item( tmpRow, tmpCol );
    if ( itm && !itm->isEnabled() )
	return;
    if ( tmpRow != -1 && tmpCol != -1 ) {
	if ( beginEdit( tmpRow, tmpCol, FALSE ) )
	    setEditMode( Editing, tmpRow, tmpCol );
    }

    emit doubleClicked( tmpRow, tmpCol, e->button(), e->pos() );
}

/*!
    Sets the current edit mode to \a mode, the current edit row to \a
    row and the current edit column to \a col.

    \sa EditMode
*/

void TQTable::setEditMode( EditMode mode, int row, int col )
{
    edMode = mode;
    editRow = row;
    editCol = col;
}


/*! \reimp
*/

void TQTable::contentsMouseMoveEvent( TQMouseEvent *e )
{
    if ( (e->state() & MouseButtonMask) == NoButton )
	return;
    int tmpRow = rowAt( e->pos().y() );
    int tmpCol = columnAt( e->pos().x() );
    fixRow( tmpRow, e->pos().y() );
    fixCol( tmpCol, e->pos().x() );

#ifndef QT_NO_DRAGANDDROP
    if ( dragEnabled() && startDragRow != -1 && startDragCol != -1 ) {
	if (TQPoint(dragStartPos - e->pos()).manhattanLength() > TQApplication::startDragDistance())
	    startDrag();
	return;
    }
#endif
    if ( selectionMode() == MultiRow && ( e->state() & ControlButton ) == ControlButton )
	shouldClearSelection = FALSE;

    if ( shouldClearSelection ) {
	clearSelection();
	if ( selMode != NoSelection ) {
	    currentSel = new TQTableSelection();
	    selections.append( currentSel );
	    if ( !isRowSelection( selectionMode() ) )
		currentSel->init( tmpRow, tmpCol );
	    else
		currentSel->init( tmpRow, 0 );
	    emit selectionChanged();
	}
	shouldClearSelection = FALSE;
    }

    TQPoint pos = mapFromGlobal( e->globalPos() );
    pos -= TQPoint( leftHeader->width(), topHeader->height() );
    autoScrollTimer->stop();
    doAutoScroll();
    if ( pos.x() < 0 || pos.x() > visibleWidth() || pos.y() < 0 || pos.y() > visibleHeight() )
	autoScrollTimer->start( 100, TRUE );
}

/*! \internal
 */

void TQTable::doValueChanged()
{
    emit valueChanged( editRow, editCol );
}

/*! \internal
*/

void TQTable::doAutoScroll()
{
    TQPoint pos = TQCursor::pos();
    pos = mapFromGlobal( pos );
    pos -= TQPoint( leftHeader->width(), topHeader->height() );

    int tmpRow = curRow;
    int tmpCol = curCol;
    if ( pos.y() < 0 )
	tmpRow--;
    else if ( pos.y() > visibleHeight() )
	tmpRow++;
    if ( pos.x() < 0 )
	tmpCol--;
    else if ( pos.x() > visibleWidth() )
	tmpCol++;

    pos += TQPoint( contentsX(), contentsY() );
    if ( tmpRow == curRow )
	tmpRow = rowAt( pos.y() );
    if ( tmpCol == curCol )
	tmpCol = columnAt( pos.x() );
    pos -= TQPoint( contentsX(), contentsY() );

    fixRow( tmpRow, pos.y() );
    fixCol( tmpCol, pos.x() );

    if ( tmpRow < 0 || tmpRow > numRows() - 1 )
	tmpRow = currentRow();
    if ( tmpCol < 0 || tmpCol > numCols() - 1 )
	tmpCol = currentColumn();

    ensureCellVisible( tmpRow, tmpCol );

    if ( currentSel && selMode != NoSelection ) {
	TQTableSelection oldSelection = *currentSel;
	bool useOld = TRUE;
	if ( selMode != SingleRow ) {
	    if ( !isRowSelection( selectionMode() ) ) {
		currentSel->expandTo( tmpRow, tmpCol );
	    } else {
		currentSel->expandTo( tmpRow, numCols() - 1 );
	    }
	} else {
	    bool currentInSelection = tmpRow == curRow && isSelected( tmpRow, tmpCol );
	    if ( !currentInSelection ) {
		useOld = FALSE;
		clearSelection();
		currentSel = new TQTableSelection();
		selections.append( currentSel );
		currentSel->init( tmpRow, 0 );
		currentSel->expandTo( tmpRow, numCols() - 1 );
		repaintSelections( 0, currentSel );
	    } else {
		currentSel->expandTo( tmpRow, numCols() - 1 );
	    }
	}
	setCurrentCell( tmpRow, tmpCol, FALSE, TRUE );
	repaintSelections( useOld ? &oldSelection : 0, currentSel );
	if ( currentSel && oldSelection != *currentSel )
	    emit selectionChanged();
    } else {
	setCurrentCell( tmpRow, tmpCol, FALSE, TRUE );
    }

    if ( pos.x() < 0 || pos.x() > visibleWidth() || pos.y() < 0 || pos.y() > visibleHeight() )
	autoScrollTimer->start( 100, TRUE );
}

/*! \reimp
*/

void TQTable::contentsMouseReleaseEvent( TQMouseEvent *e )
{
    if ( pressedRow == curRow && pressedCol == curCol )
	emit clicked( curRow, curCol, e->button(), e->pos() );

    if ( e->button() != LeftButton )
	return;
    if ( shouldClearSelection ) {
	int tmpRow = rowAt( e->pos().y() );
	int tmpCol = columnAt( e->pos().x() );
	fixRow( tmpRow, e->pos().y() );
	fixCol( tmpCol, e->pos().x() );
	clearSelection();
	if ( selMode != NoSelection ) {
	    currentSel = new TQTableSelection();
	    selections.append( currentSel );
	    if ( !isRowSelection( selectionMode() ) ) {
		currentSel->init( tmpRow, tmpCol );
	    } else {
		currentSel->init( tmpRow, 0 );
		currentSel->expandTo( tmpRow, numCols() - 1 );
		repaintSelections( 0, currentSel );
	    }
	    emit selectionChanged();
	}
	shouldClearSelection = FALSE;
    }
    autoScrollTimer->stop();

    if ( d->redirectMouseEvent && pressedRow == curRow && pressedCol == curCol &&
	 item( pressedRow, pressedCol ) && item( pressedRow, pressedCol )->editType() ==
	 TQTableItem::WhenCurrent ) {
	TQWidget *w = cellWidget( pressedRow, pressedCol );
	if ( w ) {
	    TQMouseEvent ev( e->type(), w->mapFromGlobal( e->globalPos() ),
			    e->globalPos(), e->button(), e->state() );
	    TQApplication::sendPostedEvents( w, 0 );
	    TQApplication::sendEvent( w, &ev );
	}
    }
}

/*!
  \reimp
*/

void TQTable::contentsContextMenuEvent( TQContextMenuEvent *e )
{
    if ( !receivers( SIGNAL(contextMenuRequested(int,int,const TQPoint&)) ) ) {
	e->ignore();
	return;
    }
    if ( e->reason() == TQContextMenuEvent::Keyboard ) {
	TQRect r = cellGeometry( curRow, curCol );
	emit contextMenuRequested( curRow, curCol, viewport()->mapToGlobal( contentsToViewport( r.center() ) ) );
    } else {
	int tmpRow = rowAt( e->pos().y() );
	int tmpCol = columnAt( e->pos().x() );
	emit contextMenuRequested( tmpRow, tmpCol, e->globalPos() );
    }
}


/*! \reimp
*/

bool TQTable::eventFilter( TQObject *o, TQEvent *e )
{
    TQWidget *editorWidget = cellWidget( editRow, editCol );
    switch ( e->type() ) {
    case TQEvent::KeyPress: {
	TQTableItem *itm = item( curRow, curCol );
          int editRow = this->editRow;
          int editCol = this->editCol;
          if ((d->hasRowSpan || d->hasColSpan) && !editorWidget) {
              if (TQTableItem *eitm = item(editRow, editCol)) {
                  editRow = eitm->row();
                  editCol = eitm->col();
                  editorWidget = cellWidget(editRow, editCol);
              }
          }
	if ( isEditing() && editorWidget && o == editorWidget ) {
	    itm = item( editRow, editCol );
	    TQKeyEvent *ke = (TQKeyEvent*)e;
	    if ( ke->key() == Key_Escape ) {
		if ( !itm || itm->editType() == TQTableItem::OnTyping )
		    endEdit( editRow, editCol, FALSE, edMode != Editing );
		return TRUE;
	    }

	    if ( ( ke->state() == NoButton || ke->state() == Keypad )
		&& ( ke->key() == Key_Return || ke->key() == Key_Enter ) ) {
		if ( !itm || itm->editType() == TQTableItem::OnTyping )
		    endEdit( editRow, editCol, TRUE, edMode != Editing );
		activateNextCell();
		return TRUE;
	    }

	    if ( ke->key() == Key_Tab || ke->key() == Key_BackTab ) {
		if ( ke->state() & TQt::ControlButton )
		    return FALSE;
		if ( !itm || itm->editType() == TQTableItem::OnTyping )
		    endEdit( editRow, editCol, TRUE, edMode != Editing );
		if ( (ke->key() == Key_Tab) && !(ke->state() & ShiftButton) ) {
		    if ( currentColumn() >= numCols() - 1 )
			return TRUE;
		    int cc  = TQMIN( numCols() - 1, currentColumn() + 1 );
		    while ( cc < numCols() ) {
			TQTableItem *i = item( currentRow(), cc );
			if ( !d->hiddenCols.find( cc ) && !isColumnReadOnly( cc ) && (!i || i->isEnabled()) )
			    break;
			++cc;
		    }
		    setCurrentCell( currentRow(), cc );
		} else { // Key_BackTab
		    if ( currentColumn() == 0 )
			return TRUE;
		    int cc  = TQMAX( 0, currentColumn() - 1 );
		    while ( cc >= 0 ) {
			TQTableItem *i = item( currentRow(), cc );
			if ( !d->hiddenCols.find( cc ) && !isColumnReadOnly( cc ) && (!i || i->isEnabled()) )
			    break;
			--cc;
		    }
		    setCurrentCell( currentRow(), cc );
		}
		itm = item( curRow, curCol );
		if ( beginEdit( curRow, curCol, FALSE ) )
		    setEditMode( Editing, curRow, curCol );
		return TRUE;
	    }

	    if ( ( edMode == Replacing ||
		   itm && itm->editType() == TQTableItem::WhenCurrent ) &&
		 ( ke->key() == Key_Up || ke->key() == Key_Prior ||
		   ke->key() == Key_Home || ke->key() == Key_Down ||
		   ke->key() == Key_Next || ke->key() == Key_End ||
		   ke->key() == Key_Left || ke->key() == Key_Right ) ) {
		if ( !itm || itm->editType() == TQTableItem::OnTyping ) {
		    endEdit( editRow, editCol, TRUE, edMode != Editing );
		}
		keyPressEvent( ke );
		return TRUE;
	    }
	} else {
	    TQObjectList *l = viewport()->queryList( "TQWidget" );
	    if ( l && l->find( o ) != -1 ) {
                delete l;
		TQKeyEvent *ke = (TQKeyEvent*)e;
		if ( ( ke->state() & ControlButton ) == ControlButton ||
		     ( ke->key() != Key_Left && ke->key() != Key_Right &&
		       ke->key() != Key_Up && ke->key() != Key_Down &&
		       ke->key() != Key_Prior && ke->key() != Key_Next &&
		       ke->key() != Key_Home && ke->key() != Key_End ) )
		    return FALSE;
		keyPressEvent( (TQKeyEvent*)e );
		return TRUE;
	    }
	    delete l;
	}

	} break;
    case TQEvent::FocusOut:
	if ( isEditing() && editorWidget && o == editorWidget && ( (TQFocusEvent*)e )->reason() != TQFocusEvent::Popup ) {
	    TQTableItem *itm = item( editRow, editCol );
	    if ( !itm || itm->editType() == TQTableItem::OnTyping ) {
		endEdit( editRow, editCol, TRUE, edMode != Editing );
		return TRUE;
	    }
	}
	break;
#ifndef QT_NO_WHEELEVENT
    case TQEvent::Wheel:
	if ( o == this || o == viewport() ) {
	    TQWheelEvent* we = (TQWheelEvent*)e;
	    scrollBy( 0, -we->delta() );
	    we->accept();
	    return TRUE;
	}
#endif
    default:
	break;
    }

    return TQScrollView::eventFilter( o, e );
}

void TQTable::fixCell( int &row, int &col, int key )
{
    if ( rowHeight( row ) > 0 && columnWidth( col ) > 0 )
	return;
    if ( rowHeight( row ) <= 0 ) {
	if ( key == Key_Down ||
	     key == Key_Next ||
	     key == Key_End ) {
	    while ( row < numRows() && rowHeight( row ) <= 0 )
		row++;
	    if ( rowHeight( row ) <= 0 )
		row = curRow;
	} else if ( key == Key_Up ||
		    key == Key_Prior ||
		    key == Key_Home )
	    while ( row >= 0 && rowHeight( row ) <= 0 )
		row--;
	    if ( rowHeight( row ) <= 0 )
		row = curRow;
    } else if ( columnWidth( col ) <= 0 ) {
	if ( key == Key_Left ) {
	    while ( col >= 0 && columnWidth( col ) <= 0 )
		col--;
	    if ( columnWidth( col ) <= 0 )
		col = curCol;
	} else if ( key == Key_Right ) {
	    while ( col < numCols() && columnWidth( col ) <= 0 )
		col++;
	    if ( columnWidth( col ) <= 0 )
		col = curCol;
	}
    }
}

/*! \reimp
*/

void TQTable::keyPressEvent( TQKeyEvent* e )
{
    if ( isEditing() && item( editRow, editCol ) &&
	 item( editRow, editCol )->editType() == TQTableItem::OnTyping )
	return;

    int tmpRow = curRow;
    int tmpCol = curCol;
    int oldRow = tmpRow;
    int oldCol = tmpCol;

    bool navigationKey = FALSE;
    int r;
    switch ( e->key() ) {
    case Key_Left:
	tmpCol = TQMAX( 0, tmpCol - 1 );
	navigationKey = TRUE;
	break;
    case Key_Right:
	tmpCol = TQMIN( numCols() - 1, tmpCol + 1 );
	navigationKey = TRUE;
	break;
    case Key_Up:
	tmpRow = TQMAX( 0, tmpRow - 1 );
	navigationKey = TRUE;
	break;
    case Key_Down:
	tmpRow = TQMIN( numRows() - 1, tmpRow + 1 );
	navigationKey = TRUE;
	break;
    case Key_Prior:
	r = TQMAX( 0, rowAt( rowPos( tmpRow ) - visibleHeight() ) );
	if ( r < tmpRow || tmpRow < 0 )
	    tmpRow = r;
	navigationKey = TRUE;
	break;
    case Key_Next:
	r = TQMIN( numRows() - 1, rowAt( rowPos( tmpRow ) + visibleHeight() ) );
	if ( r > tmpRow )
	    tmpRow = r;
	else
	    tmpRow = numRows() - 1;
	navigationKey = TRUE;
	break;
    case Key_Home:
	tmpRow = 0;
	navigationKey = TRUE;
	break;
    case Key_End:
	tmpRow = numRows() - 1;
	navigationKey = TRUE;
	break;
    case Key_F2:
	if ( beginEdit( tmpRow, tmpCol, FALSE ) )
	    setEditMode( Editing, tmpRow, tmpCol );
	break;
    case Key_Enter: case Key_Return:
	activateNextCell();
	return;
    case Key_Tab: case Key_BackTab:
	if ( (e->key() == Key_Tab) && !(e->state() & ShiftButton) ) {
	    if ( currentColumn() >= numCols() - 1 )
		return;
	    int cc  = TQMIN( numCols() - 1, currentColumn() + 1 );
	    while ( cc < numCols() ) {
		TQTableItem *i = item( currentRow(), cc );
		if ( !d->hiddenCols.find( cc ) && !isColumnReadOnly( cc ) && (!i || i->isEnabled()) )
		    break;
		++cc;
	    }
	    setCurrentCell( currentRow(), cc );
	} else { // Key_BackTab
	    if ( currentColumn() == 0 )
		return;
	    int cc  = TQMAX( 0, currentColumn() - 1 );
	    while ( cc >= 0 ) {
		TQTableItem *i = item( currentRow(), cc );
		if ( !d->hiddenCols.find( cc ) && !isColumnReadOnly( cc ) && (!i || i->isEnabled()) )
		    break;
		--cc;
	    }
	    setCurrentCell( currentRow(), cc );
	}
	return;
    case Key_Escape:
	e->ignore();
	return;
    default: // ... or start in-place editing
	if ( e->text()[ 0 ].isPrint() ) {
	    TQTableItem *itm = item( tmpRow, tmpCol );
	    if ( !itm || itm->editType() == TQTableItem::OnTyping ) {
		TQWidget *w = beginEdit( tmpRow, tmpCol,
					itm ? itm->isReplaceable() : TRUE );
		if ( w ) {
		    setEditMode( ( !itm || itm && itm->isReplaceable()
				   ? Replacing : Editing ), tmpRow, tmpCol );
		    TQApplication::sendEvent( w, e );
		    return;
		}
	    }
	}
	e->ignore();
	return;
    }

    if ( navigationKey ) {
	fixCell( tmpRow, tmpCol, e->key() );
	if ( ( e->state() & ShiftButton ) == ShiftButton &&
	     selMode != NoSelection && selMode != SingleRow ) {
	    bool justCreated = FALSE;
	    setCurrentCell( tmpRow, tmpCol, FALSE, TRUE );
	    if ( !currentSel ) {
		justCreated = TRUE;
		currentSel = new TQTableSelection();
		selections.append( currentSel );
		if ( !isRowSelection( selectionMode() ) )
		    currentSel->init( oldRow, oldCol );
		else
		    currentSel->init( oldRow < 0 ? 0 : oldRow, 0 );
	    }
	    TQTableSelection oldSelection = *currentSel;
	    if ( !isRowSelection( selectionMode() ) )
		currentSel->expandTo( tmpRow, tmpCol );
	    else
		currentSel->expandTo( tmpRow, numCols() - 1 );
	    repaintSelections( justCreated ? 0 : &oldSelection, currentSel );
	    emit selectionChanged();
	} else {
	    setCurrentCell( tmpRow, tmpCol, FALSE, TRUE );
	    if ( !isRowSelection( selectionMode() ) ) {
		clearSelection();
	    } else {
		bool currentInSelection = tmpRow == oldRow && isSelected( tmpRow, tmpCol, FALSE );
		if ( !currentInSelection ) {
		    bool hasOldSel = FALSE;
		    TQTableSelection oldSelection;
		    if ( selectionMode() == MultiRow ) {
			bool b = signalsBlocked();
			blockSignals( TRUE );
			clearSelection();
			blockSignals( b );
		    } else {
			if ( currentSel ) {
			    oldSelection = *currentSel;
			    hasOldSel = TRUE;
			    selections.removeRef( currentSel );
			    leftHeader->setSectionState( oldSelection.topRow(), TQTableHeader::Normal );
			}
		    }
		    currentSel = new TQTableSelection();
		    selections.append( currentSel );
		    currentSel->init( tmpRow, 0 );
		    currentSel->expandTo( tmpRow, numCols() - 1 );
		    repaintSelections( hasOldSel ? &oldSelection : 0, currentSel, !hasOldSel );
		    emit selectionChanged();
		}
	    }
	}
    } else {
	setCurrentCell( tmpRow, tmpCol, FALSE, TRUE );
    }
}

/*! \reimp
*/

void TQTable::focusInEvent( TQFocusEvent* )
{
    d->inMenuMode = FALSE;
    TQWidget *editorWidget = cellWidget( editRow, editCol );
    updateCell( curRow, curCol );
    if ( style().styleHint( TQStyle::SH_ItemView_ChangeHighlightOnFocus, this ) )
	repaintSelections();
    if ( isEditing() && editorWidget )
	editorWidget->setFocus();

    TQPoint cellPos( columnPos( curCol ) + leftMargin() - contentsX(), rowPos( curRow ) + topMargin() - contentsY() );
    TQTableItem *itm = item( curRow, curCol );
    setMicroFocusHint( cellPos.x(), cellPos.y(), columnWidth( curCol ), rowHeight( curRow ), ( itm && itm->editType() != TQTableItem::Never ) );
}


/*! \reimp
*/

void TQTable::focusOutEvent( TQFocusEvent* )
{
    updateCell( curRow, curCol );
    if (style().styleHint( TQStyle::SH_ItemView_ChangeHighlightOnFocus, this )) {
	d->inMenuMode =
	    TQFocusEvent::reason() == TQFocusEvent::Popup ||
	    (qApp->focusWidget() && qApp->focusWidget()->inherits("TQMenuBar"));
	if ( !d->inMenuMode )
	    repaintSelections();
    }
}

/*! \reimp
*/

TQSize TQTable::sizeHint() const
{
    if ( cachedSizeHint().isValid() )
	return cachedSizeHint();

    constPolish();

    TQSize s = tableSize();
    TQSize sh;
    if ( s.width() < 500 && s.height() < 500 ) {
	sh = TQSize( tableSize().width() + VERTICALMARGIN + 5,
		    tableSize().height() + topMargin() + 5 );
    } else {
    	sh = TQScrollView::sizeHint();
    	if ( !topHeader->isHidden() )
		sh.setHeight( sh.height() + topHeader->height() );
    	if ( !leftHeader->isHidden() )
		sh.setWidth( sh.width() + leftHeader->width() );
    }
    setCachedSizeHint( sh );
    return sh;
}

/*! \reimp
*/

void TQTable::viewportResizeEvent( TQResizeEvent *e )
{
    TQScrollView::viewportResizeEvent( e );
    updateGeometries();
}

/*! \reimp
*/

void TQTable::showEvent( TQShowEvent *e )
{
    TQScrollView::showEvent( e );
    TQRect r( cellGeometry( numRows() - 1, numCols() - 1 ) );
    resizeContents( r.right() + 1, r.bottom() + 1 );
    updateGeometries();
}

/*! \reimp
*/

void TQTable::paintEvent( TQPaintEvent *e )
{
    TQRect topLeftCorner = TQStyle::visualRect( TQRect(frameWidth(), frameWidth(), VERTICALMARGIN, topMargin() ), rect() );
    erase( topLeftCorner ); // erase instead of widget on top
    TQScrollView::paintEvent( e );

#ifdef Q_OS_TEMP
    TQPainter p( this );
    p.drawLine( topLeftCorner.bottomLeft(), topLeftCorner.bottomRight() );
    p.drawLine( topLeftCorner.bottomRight(), topLeftCorner.topRight() );
#endif
}

static bool inUpdateCell = FALSE;

/*!
    Repaints the cell at \a row, \a col.
*/

void TQTable::updateCell( int row, int col )
{
    if ( inUpdateCell || row < 0 || col < 0 )
	return;
    inUpdateCell = TRUE;
    TQRect cg = cellGeometry( row, col );
    TQRect r( contentsToViewport( TQPoint( cg.x() - 2, cg.y() - 2 ) ),
	     TQSize( cg.width() + 4, cg.height() + 4 ) );
    if (viewport()->rect().intersects(r))
        TQApplication::postEvent( viewport(), new TQPaintEvent( r, FALSE ) );
    inUpdateCell = FALSE;
}

void TQTable::repaintCell( int row, int col )
{
    if ( row == -1 || col == -1 )
	return;
    TQRect cg = cellGeometry( row, col );
    TQRect r( TQPoint( cg.x() - 2, cg.y() - 2 ),
	     TQSize( cg.width() + 4, cg.height() + 4 ) );

    TQRect v = viewport()->rect();
    v.moveBy(contentsX(), contentsY());
    if (v.intersects(r))
        repaintContents( r, FALSE );
}

void TQTable::contentsToViewport2( int x, int y, int& vx, int& vy )
{
    const TQPoint v = contentsToViewport2( TQPoint( x, y ) );
    vx = v.x();
    vy = v.y();
}

TQPoint TQTable::contentsToViewport2( const TQPoint &p )
{
    return TQPoint( p.x() - contentsX(),
		   p.y() - contentsY() );
}

TQPoint TQTable::viewportToContents2( const TQPoint& vp )
{
    return TQPoint( vp.x() + contentsX(),
		   vp.y() + contentsY() );
}

void TQTable::viewportToContents2( int vx, int vy, int& x, int& y )
{
    const TQPoint c = viewportToContents2( TQPoint( vx, vy ) );
    x = c.x();
    y = c.y();
}

/*!
    This function should be called whenever the column width of \a col
    has been changed. It updates the geometry of any affected columns
    and repaints the table to reflect the changes it has made.
*/

void TQTable::columnWidthChanged( int col )
{
    int p = columnPos( col );
    if ( d->hasColSpan )
	p = contentsX();
    updateContents( p, contentsY(), contentsWidth(), visibleHeight() );
    TQSize s( tableSize() );
    int w = contentsWidth();
    resizeContents( s.width(), s.height() );
    if ( contentsWidth() < w )
	repaintContents( s.width(), contentsY(),
			 w - s.width() + 1, visibleHeight(), TRUE );
    else
	repaintContents( w, contentsY(),
			 s.width() - w + 1, visibleHeight(), FALSE );

    // update widgets that are affected by this change
    if ( widgets.size() ) {
        int last = isHidden() ? numCols() - 1 : d->lastVisCol;
	for ( int c = col; c <= last; ++c )
	    updateColWidgets( c );
    }
    delayedUpdateGeometries();
}

/*!
    This function should be called whenever the row height of \a row
    has been changed. It updates the geometry of any affected rows and
    repaints the table to reflect the changes it has made.
*/

void TQTable::rowHeightChanged( int row )
{
    int p = rowPos( row );
    if ( d->hasRowSpan )
	p = contentsY();
    updateContents( contentsX(), p, visibleWidth(), contentsHeight() );
    TQSize s( tableSize() );
    int h = contentsHeight();
    resizeContents( s.width(), s.height() );
    if ( contentsHeight() < h ) {
	repaintContents( contentsX(), contentsHeight(),
			 visibleWidth(), h - s.height() + 1, TRUE );
    } else {
	repaintContents( contentsX(), h,
			 visibleWidth(), s.height() - h + 1, FALSE );
    }

    // update widgets that are affected by this change
    if ( widgets.size() ) {
	d->lastVisRow = rowAt( contentsY() + visibleHeight() + ( s.height() - h + 1 ) );
        int last = isHidden() ? numRows() - 1 : d->lastVisRow;
	for ( int r = row; r <= last; ++r )
	    updateRowWidgets( r );
    }
    delayedUpdateGeometries();
}

/*! \internal */

void TQTable::updateRowWidgets( int row )
{
    for ( int i = 0; i < numCols(); ++i ) {
	TQWidget *w = cellWidget( row, i );
	if ( !w )
	    continue;
	moveChild( w, columnPos( i ), rowPos( row ) );
	w->resize( columnWidth( i ) - 1, rowHeight( row ) - 1 );
    }
}

/*! \internal */

void TQTable::updateColWidgets( int col )
{
    for ( int i = 0; i < numRows(); ++i ) {
	TQWidget *w = cellWidget( i, col );
	if ( !w )
	    continue;
	moveChild( w, columnPos( col ), rowPos( i ) );
	w->resize( columnWidth( col ) - 1, rowHeight( i ) - 1 );
    }
}

/*!
    This function is called when column order is to be changed, i.e.
    when the user moved the column header \a section from \a fromIndex
    to \a toIndex.

    If you want to change the column order programmatically, call
    swapRows() or swapColumns();

    \sa TQHeader::indexChange() rowIndexChanged()
*/

void TQTable::columnIndexChanged( int, int fromIndex, int toIndex )
{
    if ( doSort && lastSortCol == fromIndex && topHeader )
	topHeader->setSortIndicator( toIndex, topHeader->sortIndicatorOrder() );
    repaintContents( contentsX(), contentsY(),
		     visibleWidth(), visibleHeight(), FALSE );
}

/*!
    This function is called when the order of the rows is to be
    changed, i.e. the user moved the row header section \a section
    from \a fromIndex to \a toIndex.

    If you want to change the order programmatically, call swapRows()
    or swapColumns();

    \sa TQHeader::indexChange() columnIndexChanged()
*/

void TQTable::rowIndexChanged( int, int, int )
{
    repaintContents( contentsX(), contentsY(),
		     visibleWidth(), visibleHeight(), FALSE );
}

/*!
    This function is called when the column \a col has been clicked.
    The default implementation sorts this column if sorting() is TRUE.
*/

void TQTable::columnClicked( int col )
{
    if ( !sorting() )
	return;

    if ( col == lastSortCol ) {
	asc = !asc;
    } else {
	lastSortCol = col;
	asc = TRUE;
    }
    sortColumn( lastSortCol, asc );
}

/*!
    \property TQTable::sorting
    \brief whether a click on the header of a column sorts that column

    \sa sortColumn()
*/

void TQTable::setSorting( bool b )
{
    doSort = b;
    if ( topHeader )
 	topHeader->setSortIndicator( b ? lastSortCol : -1 );
}

bool TQTable::sorting() const
{
    return doSort;
}

static bool inUpdateGeometries = FALSE;

void TQTable::delayedUpdateGeometries()
{
    d->geomTimer->start( 0, TRUE );
}

void TQTable::updateGeometriesSlot()
{
    updateGeometries();
}

/*!
    This function updates the geometries of the left and top header.
    You do not normally need to call this function.
*/

void TQTable::updateGeometries()
{
    if ( inUpdateGeometries )
	return;
    inUpdateGeometries = TRUE;
    TQSize ts = tableSize();
    if ( topHeader->offset() &&
	 ts.width() < topHeader->offset() + topHeader->width() )
	horizontalScrollBar()->setValue( ts.width() - topHeader->width() );
    if ( leftHeader->offset() &&
	 ts.height() < leftHeader->offset() + leftHeader->height() )
	verticalScrollBar()->setValue( ts.height() - leftHeader->height() );

    leftHeader->setGeometry( TQStyle::visualRect( TQRect( frameWidth(), topMargin() + frameWidth(),
			     VERTICALMARGIN, visibleHeight() ), rect() ) );
    topHeader->setGeometry( TQStyle::visualRect( TQRect(VERTICALMARGIN + frameWidth(), frameWidth(),
						      visibleWidth(), topMargin() ), rect() ) );
    horizontalScrollBar()->raise();
    verticalScrollBar()->raise();
    topHeader->updateStretches();
    leftHeader->updateStretches();
    inUpdateGeometries = FALSE;
}

/*!
    Returns the width of column \a col.

    \sa setColumnWidth() rowHeight()
*/

int TQTable::columnWidth( int col ) const
{
    return topHeader->sectionSize( col );
}

/*!
    Returns the height of row \a row.

    \sa setRowHeight() columnWidth()
*/

int TQTable::rowHeight( int row ) const
{
    return leftHeader->sectionSize( row );
}

/*!
    Returns the x-coordinate of the column \a col in content
    coordinates.

    \sa columnAt() rowPos()
*/

int TQTable::columnPos( int col ) const
{
    return topHeader->sectionPos( col );
}

/*!
    Returns the y-coordinate of the row \a row in content coordinates.

    \sa rowAt() columnPos()
*/

int TQTable::rowPos( int row ) const
{
    return leftHeader->sectionPos( row );
}

/*!
    Returns the number of the column at position \a x. \a x must be
    given in content coordinates.

    \sa columnPos() rowAt()
*/

int TQTable::columnAt( int x ) const
{
    return topHeader->sectionAt( x );
}

/*!
    Returns the number of the row at position \a y. \a y must be given
    in content coordinates.

    \sa rowPos() columnAt()
*/

int TQTable::rowAt( int y ) const
{
    return leftHeader->sectionAt( y );
}

/*!
    Returns the bounding rectangle of the cell at \a row, \a col in
    content coordinates.
*/

TQRect TQTable::cellGeometry( int row, int col ) const
{
    TQTableItem *itm = item( row, col );

    if ( !itm || itm->rowSpan() == 1 && itm->colSpan() == 1 )
	return TQRect( columnPos( col ), rowPos( row ),
		      columnWidth( col ), rowHeight( row ) );

    while ( row != itm->row() )
	row--;
    while ( col != itm->col() )
	col--;

    TQRect rect( columnPos( col ), rowPos( row ),
		columnWidth( col ), rowHeight( row ) );

    for ( int r = 1; r < itm->rowSpan(); ++r )
	rect.setHeight( rect.height() + rowHeight( r + row ) );

    for ( int c = 1; c < itm->colSpan(); ++c )
	rect.setWidth( rect.width() + columnWidth( c + col ) );

    return rect;
}

/*!
    Returns the size of the table.

    This is the same as the coordinates of the bottom-right edge of
    the last table cell.
*/

TQSize TQTable::tableSize() const
{
    return TQSize( columnPos( numCols() - 1 ) + columnWidth( numCols() - 1 ),
		  rowPos( numRows() - 1 ) + rowHeight( numRows() - 1 ) );
}

/*!
    \property TQTable::numRows
    \brief The number of rows in the table

    \sa numCols
*/

int TQTable::numRows() const
{
    return leftHeader->count();
}

/*!
    \property TQTable::numCols
    \brief The number of columns in the table

    \sa numRows
*/

int TQTable::numCols() const
{
    return topHeader->count();
}

void TQTable::saveContents( TQPtrVector<TQTableItem> &tmp,
			   TQPtrVector<TQTable::TableWidget> &tmp2)
{
    int nCols = numCols();
    if ( editRow != -1 && editCol != -1 )
	endEdit( editRow, editCol, FALSE, edMode != Editing );
    tmp.resize( contents.size() );
    tmp2.resize( widgets.size() );
    int i;
    for ( i = 0; i < (int)tmp.size(); ++i ) {
	TQTableItem *item = contents[ i ];
	if ( item && ( item->row() * nCols) + item->col() == i )
	    tmp.insert( i, item );
	else
	    tmp.insert( i, 0 );
    }
    for ( i = 0; i < (int)tmp2.size(); ++i ) {
	TQWidget *w = widgets[ i ];
	if ( w )
	    tmp2.insert( i, new TableWidget( w, i / nCols, i % nCols ) );
	else
	    tmp2.insert( i, 0 );
    }
}

void TQTable::updateHeaderAndResizeContents( TQTableHeader *header,
					    int num, int rowCol,
					    int width, bool &updateBefore )
{
    updateBefore = rowCol < num;
    if ( rowCol > num ) {
	header->TQHeader::resizeArrays( rowCol );
	header->TQTableHeader::resizeArrays( rowCol );
	int old = num;
	clearSelection( FALSE );
	int i = 0;
	for ( i = old; i < rowCol; ++i )
	    header->addLabel( TQString::null, width );
    } else {
	clearSelection( FALSE );
	if ( header == leftHeader ) {
	    while ( numRows() > rowCol )
		header->removeLabel( numRows() - 1 );
	} else {
	    while ( numCols() > rowCol )
		header->removeLabel( numCols() - 1 );
	}
    }

    contents.setAutoDelete( FALSE );
    contents.clear();
    contents.setAutoDelete( TRUE );
    widgets.setAutoDelete( FALSE );
    widgets.clear();
    widgets.setAutoDelete( TRUE );
    resizeData( numRows() * numCols() );

    // keep numStretches in sync
    int n = 0;
    for ( uint i = 0; i < header->stretchable.size(); i++ )
	n += ( header->stretchable.at(i) & 1 ); // avoid cmp
     header->numStretches = n;
}

void TQTable::restoreContents( TQPtrVector<TQTableItem> &tmp,
			      TQPtrVector<TQTable::TableWidget> &tmp2 )
{
    int i;
    int nCols = numCols();
    for ( i = 0; i < (int)tmp.size(); ++i ) {
	TQTableItem *it = tmp[ i ];
	if ( it ) {
	    int idx = ( it->row() * nCols ) + it->col();
	    if ( (uint)idx < contents.size() &&
		 it->row() == idx /  nCols && it->col() == idx % nCols ) {
		contents.insert( idx, it );
		if ( it->rowSpan() > 1 || it->colSpan() > 1 ) {
		    int ridx, iidx;
		    for ( int irow = 0; irow < it->rowSpan(); irow++ ) {
			ridx = idx + irow * nCols;
			for ( int icol = 0; icol < it->colSpan(); icol++ ) {
			    iidx = ridx + icol;
			    if ( idx != iidx && (uint)iidx < contents.size() )
				contents.insert( iidx, it );
			}
		    }

		}
	    } else {
		delete it;
	    }
	}
    }
    for ( i = 0; i < (int)tmp2.size(); ++i ) {
	TableWidget *w = tmp2[ i ];
	if ( w ) {
	    int idx = ( w->row * nCols ) + w->col;
	    if ( (uint)idx < widgets.size() &&
		 w->row == idx / nCols && w->col == idx % nCols )
		widgets.insert( idx, w->wid );
	    else
		delete w->wid;
	    delete w;
	}
    }
}

void TQTable::finishContentsResze( bool updateBefore )
{
    TQRect r( cellGeometry( numRows() - 1, numCols() - 1 ) );
    resizeContents( r.right() + 1, r.bottom() + 1 );
    updateGeometries();
    if ( updateBefore )
	repaintContents( contentsX(), contentsY(),
			 visibleWidth(), visibleHeight(), TRUE );
    else
	repaintContents( contentsX(), contentsY(),
			 visibleWidth(), visibleHeight(), FALSE );

    if ( isRowSelection( selectionMode() ) ) {
	int r = curRow;
	curRow = -1;
	setCurrentCell( r, curCol );
    }
}

void TQTable::setNumRows( int r )
{
    if ( r < 0 )
	return;

    if (r < numRows()) {
	// Removed rows are no longer hidden, and should thus be removed from "hiddenRows"
	for (int rr = numRows()-1; rr >= r; --rr) {
	    if (d->hiddenRows.find(rr))
		d->hiddenRows.remove(rr);
	}
    }

    fontChange(font()); // invalidate the sizeHintCache

    TQPtrVector<TQTableItem> tmp;
    TQPtrVector<TableWidget> tmp2;
    saveContents( tmp, tmp2 );

    bool isUpdatesEnabled = leftHeader->isUpdatesEnabled();
    leftHeader->setUpdatesEnabled( FALSE );

    bool updateBefore;
    updateHeaderAndResizeContents( leftHeader, numRows(), r, 20, updateBefore );

    int w = fontMetrics().width( TQString::number( r ) + "W" );
    if ( VERTICALMARGIN > 0 && w > VERTICALMARGIN )
	setLeftMargin( w );

    restoreContents( tmp, tmp2 );

    leftHeader->calculatePositions();
    finishContentsResze( updateBefore );
    leftHeader->setUpdatesEnabled( isUpdatesEnabled );
    if ( isUpdatesEnabled )
	leftHeader->update();
    leftHeader->updateCache();
    if ( curRow >= numRows() ) {
	curRow = numRows() - 1;
	if ( curRow < 0 )
	    curCol = -1;
	else
	    repaintCell( curRow, curCol );
    }

    if ( curRow > numRows() )
	curRow = numRows();
}

void TQTable::setNumCols( int c )
{
    if ( c < 0 )
	return;

    if (c < numCols()) {
	// Removed columns are no longer hidden, and should thus be removed from "hiddenCols"
	for (int cc = numCols()-1; cc >= c; --cc) {
	    if (d->hiddenCols.find(cc))
		d->hiddenCols.remove(cc);
	}
    }

    fontChange(font()); // invalidate the sizeHintCache

    TQPtrVector<TQTableItem> tmp;
    TQPtrVector<TableWidget> tmp2;
    saveContents( tmp, tmp2 );

    bool isUpdatesEnabled = topHeader->isUpdatesEnabled();
    topHeader->setUpdatesEnabled( FALSE );

    bool updateBefore;
    updateHeaderAndResizeContents( topHeader, numCols(), c, 100, updateBefore );

    restoreContents( tmp, tmp2 );

    topHeader->calculatePositions();
    finishContentsResze( updateBefore );
    topHeader->setUpdatesEnabled( isUpdatesEnabled );
    if ( isUpdatesEnabled )
	topHeader->update();
    topHeader->updateCache();
    if ( curCol >= numCols() ) {
	curCol = numCols() - 1;
	if ( curCol < 0 )
	    curRow = -1;
	else
	    repaintCell( curRow, curCol );
    }
}

/*! Sets the section labels of the verticalHeader() to \a labels */

void TQTable::setRowLabels( const TQStringList &labels )
{
    leftHeader->setLabels(labels);
}

/*! Sets the section labels of the horizontalHeader() to \a labels */

void TQTable::setColumnLabels( const TQStringList &labels )
{
   topHeader->setLabels(labels);
}

/*!
    This function returns the widget which should be used as an editor
    for the contents of the cell at \a row, \a col.

    If \a initFromCell is TRUE, the editor is used to edit the current
    contents of the cell (so the editor widget should be initialized
    with this content). If \a initFromCell is FALSE, the content of
    the cell is replaced with the new content which the user entered
    into the widget created by this function.

    The default functionality is as follows: if \a initFromCell is
    TRUE or the cell has a TQTableItem and the table item's
    TQTableItem::isReplaceable() is FALSE then the cell is asked to
    create an appropriate editor (using TQTableItem::createEditor()).
    Otherwise a TQLineEdit is used as the editor.

    If you want to create your own editor for certain cells, implement
    a custom TQTableItem subclass and reimplement
    TQTableItem::createEditor().

    If you are not using \l{TQTableItem}s and you don't want to use a
    TQLineEdit as the default editor, subclass TQTable and reimplement
    this function with code like this:
    \code
    TQTableItem *i = item( row, col );
    if ( initFromCell || ( i && !i->isReplaceable() ) )
	// If we had a TQTableItem ask the base class to create the editor
	return TQTable::createEditor( row, col, initFromCell );
    else
	return ...(create your own editor)
    \endcode
    Ownership of the editor widget is transferred to the caller.

    If you reimplement this function return 0 for read-only cells. You
    will need to reimplement setCellContentFromEditor() to retrieve
    the data the user entered.

    \sa TQTableItem::createEditor()
*/

TQWidget *TQTable::createEditor( int row, int col, bool initFromCell ) const
{
    if ( isReadOnly() || isRowReadOnly( row ) || isColumnReadOnly( col ) )
	return 0;

    TQWidget *e = 0;

    // the current item in the cell should be edited if possible
    TQTableItem *i = item( row, col );
    if ( initFromCell || ( i && !i->isReplaceable() ) ) {
	if ( i ) {
	    if ( i->editType() == TQTableItem::Never )
		return 0;

	    e = i->createEditor();
	    if ( !e )
		return 0;
	}
    }

    // no contents in the cell yet, so open the default editor
    if ( !e ) {
	e = new TQLineEdit( viewport(), "qt_lineeditor" );
	( (TQLineEdit*)e )->setFrame( FALSE );
    }

    return e;
}

/*!
    This function is called to start in-place editing of the cell at
    \a row, \a col. Editing is achieved by creating an editor
    (createEditor() is called) and setting the cell's editor with
    setCellWidget() to the newly created editor. (After editing is
    complete endEdit() will be called to replace the cell's content
    with the editor's content.) If \a replace is TRUE the editor will
    start empty; otherwise it will be initialized with the cell's
    content (if any), i.e. the user will be modifying the original
    cell content.

    \sa endEdit()
*/

TQWidget *TQTable::beginEdit( int row, int col, bool replace )
{
    if ( isReadOnly() || isRowReadOnly( row ) || isColumnReadOnly( col ) )
	return 0;
    if ( row < 0 || row >= numRows() || col < 0 || col >= numCols() )
        return 0;

    TQTableItem *itm = item( row, col );
    if ( itm && !itm->isEnabled() )
	return 0;
    if ( cellWidget( row, col ) )
	return 0;
    ensureCellVisible( row, col );
    TQWidget *e = createEditor( row, col, !replace );
    if ( !e )
	return 0;
    setCellWidget( row, col, e );
    e->setActiveWindow();
    e->setFocus();
    updateCell( row, col );
    return e;
}

/*!
    This function is called when in-place editing of the cell at \a
    row, \a col is requested to stop.

    If the cell is not being edited or \a accept is FALSE the function
    returns and the cell's contents are left unchanged.

    If \a accept is TRUE the content of the editor must be transferred
    to the relevant cell. If \a replace is TRUE the current content of
    this cell should be replaced by the content of the editor (this
    means removing the current TQTableItem of the cell and creating a
    new one for the cell). Otherwise (if possible) the content of the
    editor should just be set to the existing TQTableItem of this cell.

    setCellContentFromEditor() is called to replace the contents of
    the cell with the contents of the cell's editor.

    Finally clearCellWidget() is called to remove the editor widget.

    \sa setCellContentFromEditor(), beginEdit()
*/

void TQTable::endEdit( int row, int col, bool accept, bool replace )
{
    TQWidget *editor = cellWidget( row, col );
    if ( !editor )
	return;

    if ( !accept ) {
	if ( row == editRow && col == editCol )
	    setEditMode( NotEditing, -1, -1 );
	clearCellWidget( row, col );
	updateCell( row, col );
	viewport()->setFocus();
	updateCell( row, col );
	return;
    }

    TQTableItem *i = item( row, col );
    TQString oldContent;
    if ( i )
	oldContent = i->text();

    if ( !i || replace ) {
	setCellContentFromEditor( row, col );
	i = item( row, col );
    } else {
	i->setContentFromEditor( editor );
    }

    if ( row == editRow && col == editCol )
	setEditMode( NotEditing, -1, -1 );

    viewport()->setFocus();
    updateCell( row, col );

    if (!i || (oldContent != i->text()))
	emit valueChanged( row, col );

    clearCellWidget( row, col );
}

/*!
    This function is called to replace the contents of the cell at \a
    row, \a col with the contents of the cell's editor.

    If there already exists a TQTableItem for the cell,
    it calls TQTableItem::setContentFromEditor() on this TQTableItem.

    If, for example, you want to create different \l{TQTableItem}s
    depending on the contents of the editor, you might reimplement
    this function.

    If you want to work without \l{TQTableItem}s, you will need to
    reimplement this function to save the data the user entered into
    your data structure. (See the notes on large tables.)

    \sa TQTableItem::setContentFromEditor() createEditor()
*/

void TQTable::setCellContentFromEditor( int row, int col )
{
    TQWidget *editor = cellWidget( row, col );
    if ( !editor )
	return;

    TQTableItem *i = item( row, col );
    if ( i ) {
	i->setContentFromEditor( editor );
    } else {
	TQLineEdit *le = ::qt_cast<TQLineEdit*>(editor);
	if ( le )
	    setText( row, col, le->text() );
    }
}

/*!
    Returns TRUE if the \l EditMode is \c Editing or \c Replacing;
    otherwise (i.e. the \l EditMode is \c NotEditing) returns FALSE.

    \sa TQTable::EditMode
*/

bool TQTable::isEditing() const
{
    return edMode != NotEditing;
}

/*!
    Returns the current edit mode

    \sa TQTable::EditMode
*/

TQTable::EditMode TQTable::editMode() const
{
    return edMode;
}

/*!
    Returns the current edited row
*/

int TQTable::currEditRow() const
{
    return editRow;
}

/*!
    Returns the current edited column
*/

int TQTable::currEditCol() const
{
    return editCol;
}

/*!
    Returns a single integer which identifies a particular \a row and \a
    col by mapping the 2D table to a 1D array.

    This is useful, for example, if you have a sparse table and want to
    use a TQIntDict to map integers to the cells that are used.
*/

int TQTable::indexOf( int row, int col ) const
{
    return ( row * numCols() ) + col;
}

/*! \internal
*/

void TQTable::repaintSelections( TQTableSelection *oldSelection,
				TQTableSelection *newSelection,
				bool updateVertical, bool updateHorizontal )
{
    if ( !oldSelection && !newSelection )
	return;
    if ( oldSelection && newSelection && *oldSelection == *newSelection )
	return;
    if ( oldSelection && !oldSelection->isActive() )
 	oldSelection = 0;

    bool optimizeOld = FALSE;
    bool optimizeNew = FALSE;

    TQRect old;
    if ( oldSelection )
	old = rangeGeometry( oldSelection->topRow(),
			     oldSelection->leftCol(),
			     oldSelection->bottomRow(),
			     oldSelection->rightCol(),
			     optimizeOld );
    else
	old = TQRect( 0, 0, 0, 0 );

    TQRect cur;
    if ( newSelection )
	cur = rangeGeometry( newSelection->topRow(),
			     newSelection->leftCol(),
			     newSelection->bottomRow(),
			     newSelection->rightCol(),
			     optimizeNew );
    else
	cur = TQRect( 0, 0, 0, 0 );
    int i;

    if ( !optimizeOld || !optimizeNew ||
	 old.width() > SHRT_MAX || old.height() > SHRT_MAX ||
	 cur.width() > SHRT_MAX || cur.height() > SHRT_MAX ) {
	TQRect rr = cur.unite( old );
	repaintContents( rr, FALSE );
    } else {
	old = TQRect( contentsToViewport2( old.topLeft() ), old.size() );
	cur = TQRect( contentsToViewport2( cur.topLeft() ), cur.size() );
	TQRegion r1( old );
	TQRegion r2( cur );
 	TQRegion r3 = r1.subtract( r2 );
 	TQRegion r4 = r2.subtract( r1 );

	for ( i = 0; i < (int)r3.rects().count(); ++i ) {
	    TQRect r( r3.rects()[ i ] );
	    r = TQRect( viewportToContents2( r.topLeft() ), r.size() );
	    repaintContents( r, FALSE );
	}
	for ( i = 0; i < (int)r4.rects().count(); ++i ) {
	    TQRect r( r4.rects()[ i ] );
	    r = TQRect( viewportToContents2( r.topLeft() ), r.size() );
	    repaintContents( r, FALSE );
	}
    }

    int top, left, bottom, right;
    {
        int oldTopRow = oldSelection ? oldSelection->topRow() : numRows() - 1;
        int newTopRow = newSelection ? newSelection->topRow() : numRows() - 1;
        top = TQMIN(oldTopRow, newTopRow);
    }

    {
        int oldLeftCol = oldSelection ? oldSelection->leftCol() : numCols() - 1;
        int newLeftCol = newSelection ? newSelection->leftCol() : numCols() - 1;
        left = TQMIN(oldLeftCol, newLeftCol);
    }

    {
        int oldBottomRow = oldSelection ? oldSelection->bottomRow() : 0;
        int newBottomRow = newSelection ? newSelection->bottomRow() : 0;
        bottom = TQMAX(oldBottomRow, newBottomRow);
    }

    {
        int oldRightCol = oldSelection ? oldSelection->rightCol() : 0;
        int newRightCol = newSelection ? newSelection->rightCol() : 0;
        right = TQMAX(oldRightCol, newRightCol);
    }

    if ( updateHorizontal && numCols() > 0 && left >= 0 && !isRowSelection( selectionMode() ) ) {
	register int *s = &topHeader->states.data()[left];
	for ( i = left; i <= right; ++i ) {
	    if ( !isColumnSelected( i ) )
		*s = TQTableHeader::Normal;
	    else if ( isColumnSelected( i, TRUE ) )
		*s = TQTableHeader::Selected;
	    else
		*s = TQTableHeader::Bold;
	    ++s;
	}
	topHeader->repaint( FALSE );
    }

    if ( updateVertical && numRows() > 0 && top >= 0 ) {
	register int *s = &leftHeader->states.data()[top];
	for ( i = top; i <= bottom; ++i ) {
	    if ( !isRowSelected( i ) )
		*s = TQTableHeader::Normal;
	    else if ( isRowSelected( i, TRUE ) )
		*s = TQTableHeader::Selected;
	    else
		*s = TQTableHeader::Bold;
	    ++s;
	}
	leftHeader->repaint( FALSE );
    }
}

/*!
    Repaints all selections
*/

void TQTable::repaintSelections()
{
    if ( selections.isEmpty() )
	return;

    TQRect r;
    for ( TQTableSelection *s = selections.first(); s; s = selections.next() ) {
	bool b;
	r = r.unite( rangeGeometry( s->topRow(),
				    s->leftCol(),
				    s->bottomRow(),
				    s->rightCol(), b ) );
    }

    repaintContents( r, FALSE );
}

/*!
    Clears all selections and repaints the appropriate regions if \a
    repaint is TRUE.

    \sa removeSelection()
*/

void TQTable::clearSelection( bool repaint )
{
    if ( selections.isEmpty() )
	return;
    bool needRepaint = !selections.isEmpty();

    TQRect r;
    for ( TQTableSelection *s = selections.first(); s; s = selections.next() ) {
	bool b;
	r = r.unite( rangeGeometry( s->topRow(),
				    s->leftCol(),
				    s->bottomRow(),
				    s->rightCol(), b ) );
    }

    currentSel = 0;
    selections.clear();

    if ( needRepaint && repaint )
	repaintContents( r, FALSE );

    leftHeader->setSectionStateToAll( TQTableHeader::Normal );
    leftHeader->repaint( FALSE );
    if ( !isRowSelection( selectionMode() ) ) {
	topHeader->setSectionStateToAll( TQTableHeader::Normal );
	topHeader->repaint( FALSE );
    }
    topHeader->setSectionState( curCol, TQTableHeader::Bold );
    leftHeader->setSectionState( curRow, TQTableHeader::Bold );
    emit selectionChanged();
}

/*! \internal
*/

TQRect TQTable::rangeGeometry( int topRow, int leftCol,
			     int bottomRow, int rightCol, bool &optimize )
{
    topRow = TQMAX( topRow, rowAt( contentsY() ) );
    leftCol = TQMAX( leftCol, columnAt( contentsX() ) );
    int ra = rowAt( contentsY() + visibleHeight() );
    if ( ra != -1 )
	bottomRow = TQMIN( bottomRow, ra );
    int ca = columnAt( contentsX() + visibleWidth() );
    if ( ca != -1 )
	rightCol = TQMIN( rightCol, ca );
    optimize = TRUE;
    TQRect rect;
    for ( int r = topRow; r <= bottomRow; ++r ) {
	for ( int c = leftCol; c <= rightCol; ++c ) {
	    rect = rect.unite( cellGeometry( r, c ) );
	    TQTableItem *i = item( r, c );
	    if ( i && ( i->rowSpan() > 1 || i->colSpan() > 1 ) )
		optimize = FALSE;
	}
    }
    return rect;
}

/*!
    This function is called to activate the next cell if in-place
    editing was finished by pressing the Enter key.

    The default behaviour is to move from top to bottom, i.e. move to
    the cell beneath the cell being edited. Reimplement this function
    if you want different behaviour, e.g. moving from left to right.
*/

void TQTable::activateNextCell()
{
    int firstRow = 0;
    while ( d->hiddenRows.find( firstRow ) )
	firstRow++;
    int firstCol = 0;
    while ( d->hiddenCols.find( firstCol ) )
	firstCol++;
    int nextRow = curRow;
    int nextCol = curCol;
    while ( d->hiddenRows.find( ++nextRow ) );
    if ( nextRow >= numRows() ) {
	nextRow = firstRow;
	while ( d->hiddenCols.find( ++nextCol ) );
	if ( nextCol >= numCols() )
	    nextCol = firstCol;
    }

    if ( !currentSel || !currentSel->isActive() ||
	 ( currentSel->leftCol() == currentSel->rightCol() &&
	   currentSel->topRow() == currentSel->bottomRow() ) ) {
	clearSelection();
	setCurrentCell( nextRow, nextCol );
    } else {
	if ( curRow < currentSel->bottomRow() )
	    setCurrentCell( nextRow, curCol );
	else if ( curCol < currentSel->rightCol() )
	    setCurrentCell( currentSel->topRow(), nextCol );
	else
	    setCurrentCell( currentSel->topRow(), currentSel->leftCol() );
    }

}

/*! \internal
*/

void TQTable::fixRow( int &row, int y )
{
    if ( row == -1 ) {
	if ( y < 0 )
	    row = 0;
	else
	    row = numRows() - 1;
    }
}

/*! \internal
*/

void TQTable::fixCol( int &col, int x )
{
    if ( col == -1 ) {
	if ( x < 0 )
	    col = 0;
	else
	    col = numCols() - 1;
    }
}

struct SortableTableItem
{
    TQTableItem *item;
};

#if defined(Q_C_CALLBACKS)
extern "C" {
#endif

#ifdef Q_OS_TEMP
static int _cdecl cmpTableItems( const void *n1, const void *n2 )
#else
static int cmpTableItems( const void *n1, const void *n2 )
#endif
{
    if ( !n1 || !n2 )
	return 0;

    SortableTableItem *i1 = (SortableTableItem *)n1;
    SortableTableItem *i2 = (SortableTableItem *)n2;

    return i1->item->key().localeAwareCompare( i2->item->key() );
}

#if defined(Q_C_CALLBACKS)
}
#endif

/*!
    Sorts column \a col. If \a ascending is TRUE the sort is in
    ascending order, otherwise the sort is in descending order.

    If \a wholeRows is TRUE, entire rows are sorted using swapRows();
    otherwise only cells in the column are sorted using swapCells().

    Note that if you are not using TQTableItems you will need to
    reimplement swapRows() and swapCells(). (See the notes on large
    tables.)

    \sa swapRows()
*/

void TQTable::sortColumn( int col, bool ascending, bool wholeRows )
{
    int filledRows = 0, i;
    for ( i = 0; i < numRows(); ++i ) {
	TQTableItem *itm = item( i, col );
	if ( itm )
	    filledRows++;
    }

    if ( !filledRows )
	return;

    SortableTableItem *items = new SortableTableItem[ filledRows ];
    int j = 0;
    for ( i = 0; i < numRows(); ++i ) {
	TQTableItem *itm = item( i, col );
	if ( !itm )
	    continue;
	items[ j++ ].item = itm;
    }

    qsort( items, filledRows, sizeof( SortableTableItem ), cmpTableItems );

    bool updatesEnabled = isUpdatesEnabled();
    setUpdatesEnabled( FALSE );
    for ( i = 0; i < numRows(); ++i ) {
	if ( i < filledRows ) {
	    if ( ascending ) {
		if ( items[ i ].item->row() == i )
		    continue;
		if ( wholeRows )
		    swapRows( items[ i ].item->row(), i );
		else
		    swapCells( items[ i ].item->row(), col, i, col );
	    } else {
		if ( items[ i ].item->row() == filledRows - i - 1 )
		    continue;
		if ( wholeRows )
		    swapRows( items[ i ].item->row(), filledRows - i - 1 );
		else
		    swapCells( items[ i ].item->row(), col,
			       filledRows - i - 1, col );
	    }
	}
    }
    setUpdatesEnabled( updatesEnabled );
    if ( topHeader )
 	topHeader->setSortIndicator( col, ascending ? TQt::Ascending : TQt::Descending );

    if ( !wholeRows )
	repaintContents( columnPos( col ), contentsY(),
			 columnWidth( col ), visibleHeight(), FALSE );
    else
	repaintContents( contentsX(), contentsY(),
			 visibleWidth(), visibleHeight(), FALSE );

    delete [] items;
}

/*!
    Hides row \a row.

    \sa showRow() hideColumn()
*/

void TQTable::hideRow( int row )
{
    if ( d->hiddenRows.find( row ) )
	return;
    d->hiddenRows.replace( row, new int( leftHeader->sectionSize( row ) ) );
    leftHeader->resizeSection( row, 0 );
    leftHeader->setResizeEnabled( FALSE, row );
    if ( isRowStretchable(row) )
	leftHeader->numStretches--;
    rowHeightChanged( row );
    if ( curRow == row ) {
	int r = curRow;
	int c = curCol;
	int k = ( r >= numRows() - 1 ? Key_Up : Key_Down );
	fixCell( r, c, k );
	if ( numRows() > 0 )
	    setCurrentCell( r, c );
    }
}

/*!
    Hides column \a col.

    \sa showColumn() hideRow()
*/

void TQTable::hideColumn( int col )
{
    if ( !numCols() || d->hiddenCols.find( col ) )
	return;
    d->hiddenCols.replace( col, new int( topHeader->sectionSize( col ) ) );
    topHeader->resizeSection( col, 0 );
    topHeader->setResizeEnabled( FALSE, col );
    if ( isColumnStretchable(col) )
	topHeader->numStretches--;
    columnWidthChanged( col );
    if ( curCol == col ) {
	int r = curRow;
	int c = curCol;
	int k = ( c >= numCols() - 1 ? Key_Left : Key_Right );
	fixCell( r, c, k );
	if ( numCols() > 0 )
	    setCurrentCell( r, c );
    }
}

/*!
    Shows row \a row.

    \sa hideRow() showColumn()
*/

void TQTable::showRow( int row )
{
    int *h = d->hiddenRows.find( row );
    if ( h ) {
	int rh = *h;
	d->hiddenRows.remove( row );
	setRowHeight( row, rh );
	if ( isRowStretchable(row) )
	    leftHeader->numStretches++;
    } else if ( rowHeight( row ) == 0 ) {
	setRowHeight( row, 20 );
    }
    leftHeader->setResizeEnabled( TRUE, row );
}

/*!
    Shows column \a col.

    \sa hideColumn() showRow()
*/

void TQTable::showColumn( int col )
{
    int *w = d->hiddenCols.find( col );
    if ( w ) {
	int cw = *w;
	d->hiddenCols.remove( col );
	setColumnWidth( col, cw );
	if ( isColumnStretchable( col ) )
	    topHeader->numStretches++;
    } else if ( columnWidth( col ) == 0 ) {
	setColumnWidth( col, 20 );
    }
    topHeader->setResizeEnabled( TRUE, col );
}

/*!
    Returns TRUE if row \a row is hidden; otherwise returns
    FALSE.

    \sa hideRow(), isColumnHidden()
*/
bool TQTable::isRowHidden( int row ) const
{
    return d->hiddenRows.find( row );
}

/*!
    Returns TRUE if column \a col is hidden; otherwise returns
    FALSE.

    \sa hideColumn(), isRowHidden()
*/
bool TQTable::isColumnHidden( int col ) const
{
    return d->hiddenCols.find( col );
}

/*!
    Resizes column \a col to be \a w pixels wide.

    \sa columnWidth() setRowHeight()
*/

void TQTable::setColumnWidth( int col, int w )
{
    int *ow = d->hiddenCols.find( col );
    if ( ow ) {
	d->hiddenCols.replace( col, new int( w ) );
    } else {
	topHeader->resizeSection( col, w );
	columnWidthChanged( col );
    }
}

/*!
    Resizes row \a row to be \a h pixels high.

    \sa rowHeight() setColumnWidth()
*/

void TQTable::setRowHeight( int row, int h )
{
    int *oh = d->hiddenRows.find( row );
    if ( oh ) {
	d->hiddenRows.replace( row, new int( h ) );
    } else {
	leftHeader->resizeSection( row, h );
	rowHeightChanged( row );
    }
}

/*!
    Resizes column \a col so that the column width is wide enough to
    display the widest item the column contains.

    \sa adjustRow()
*/

void TQTable::adjustColumn( int col )
{
    int w;
    if ( currentColumn() == col ) {
        TQFont f = font();
        f.setBold(true);
        w = topHeader->sectionSizeHint( col, TQFontMetrics(f) ).width();
    } else {
        w = topHeader->sectionSizeHint( col, fontMetrics() ).width();
    }
    if ( topHeader->iconSet( col ) )
	w += topHeader->iconSet( col )->pixmap().width();
    w = TQMAX( w, 20 );
    for ( int i = 0; i < numRows(); ++i ) {
	TQTableItem *itm = item( i, col );
	if ( !itm ) {
	    TQWidget *widget = cellWidget( i, col );
	    if ( widget )
		w = TQMAX( w, widget->sizeHint().width() );
	} else {
	    if ( itm->colSpan() > 1 )
		w = TQMAX( w, itm->sizeHint().width() / itm->colSpan() );
	    else
		w = TQMAX( w, itm->sizeHint().width() );
	}
    }
    w = TQMAX( w, TQApplication::globalStrut().width() );
    setColumnWidth( col, w );
}

/*!
    Resizes row \a row so that the row height is tall enough to
    display the tallest item the row contains.

    \sa adjustColumn()
*/

void TQTable::adjustRow( int row )
{
    int h = 20;
    h = TQMAX( h, leftHeader->sectionSizeHint( row, leftHeader->fontMetrics() ).height() );
    if ( leftHeader->iconSet( row ) )
	h = TQMAX( h, leftHeader->iconSet( row )->pixmap().height() );
    for ( int i = 0; i < numCols(); ++i ) {
	TQTableItem *itm = item( row, i );
	if ( !itm ) {
	    TQWidget *widget = cellWidget( row, i );
	    if ( widget )
		h = TQMAX( h, widget->sizeHint().height() );
	} else {
	    if ( itm->rowSpan() > 1 )
		h = TQMAX( h, itm->sizeHint().height() / itm->rowSpan() );
	    else
		h = TQMAX( h, itm->sizeHint().height() );
	}
    }
    h = TQMAX( h, TQApplication::globalStrut().height() );
    setRowHeight( row, h );
}

/*!
    If \a stretch is TRUE, column \a col is set to be stretchable;
    otherwise column \a col is set to be unstretchable.

    If the table widget's width decreases or increases stretchable
    columns will grow narrower or wider to fit the space available as
    completely as possible. The user cannot manually resize stretchable
    columns.

    \sa isColumnStretchable() setRowStretchable() adjustColumn()
*/

void TQTable::setColumnStretchable( int col, bool stretch )
{
    topHeader->setSectionStretchable( col, stretch );

    if ( stretch && d->hiddenCols.find(col) )
	topHeader->numStretches--;
}

/*!
    If \a stretch is TRUE, row \a row is set to be stretchable;
    otherwise row \a row is set to be unstretchable.

    If the table widget's height decreases or increases stretchable
    rows will grow shorter or taller to fit the space available as
    completely as possible. The user cannot manually resize
    stretchable rows.

    \sa isRowStretchable() setColumnStretchable()
*/

void TQTable::setRowStretchable( int row, bool stretch )
{
    leftHeader->setSectionStretchable( row, stretch );

    if ( stretch && d->hiddenRows.find(row) )
	leftHeader->numStretches--;
}

/*!
    Returns TRUE if column \a col is stretchable; otherwise returns
    FALSE.

    \sa setColumnStretchable() isRowStretchable()
*/

bool TQTable::isColumnStretchable( int col ) const
{
    return topHeader->isSectionStretchable( col );
}

/*!
    Returns TRUE if row \a row is stretchable; otherwise returns
    FALSE.

    \sa setRowStretchable() isColumnStretchable()
*/

bool TQTable::isRowStretchable( int row ) const
{
    return leftHeader->isSectionStretchable( row );
}

/*!
    Takes the table item \a i out of the table. This function does \e
    not delete the table item. You must either delete the table item
    yourself or put it into a table (using setItem()) which will then
    take ownership of it.

    Use this function if you want to move an item from one cell in a
    table to another, or to move an item from one table to another,
    reinserting the item with setItem().

    If you want to exchange two cells use swapCells().
*/

void TQTable::takeItem( TQTableItem *i )
{
    if ( !i )
	return;
    TQRect rect = cellGeometry( i->row(), i->col() );
    contents.setAutoDelete( FALSE );
    int bottom = i->row() + i->rowSpan();
    if ( bottom > numRows() )
	bottom = numRows();
    int right = i->col() + i->colSpan();
    if ( right > numCols() )
	right = numCols();
    for ( int r = i->row(); r < bottom; ++r ) {
	for ( int c = i->col(); c < right; ++c )
	    contents.remove( indexOf( r, c ) );
    }
    contents.setAutoDelete( TRUE );
    repaintContents( rect, FALSE );
    int orow = i->row();
    int ocol = i->col();
    i->setRow( -1 );
    i->setCol( -1 );
    i->updateEditor( orow, ocol );
    i->t = 0;
}

/*!
    Sets the widget \a e to the cell at \a row, \a col and takes care of
    placing and resizing the widget when the cell geometry changes.

    By default widgets are inserted into a vector with numRows() *
    numCols() elements. In very large tables you will probably want to
    store the widgets in a data structure that consumes less memory (see
    the notes on large tables). To support the use of your own data
    structure this function calls insertWidget() to add the widget to
    the internal data structure. To use your own data structure
    reimplement insertWidget(), cellWidget() and clearCellWidget().

    Cell widgets are created dynamically with the \c new operator. The
    cell widgets are destroyed automatically once the table is
    destroyed; the table takes ownership of the widget when using
    setCellWidget.

*/

void TQTable::setCellWidget( int row, int col, TQWidget *e )
{
    if ( !e || row >= numRows() || col >= numCols() )
	return;

    TQWidget *w = cellWidget( row, col );
    if ( w && row == editRow && col == editCol )
	endEdit( editRow, editCol, FALSE, edMode != Editing );

    e->installEventFilter( this );
    clearCellWidget( row, col );
    if ( e->parent() != viewport() )
	e->reparent( viewport(), TQPoint( 0,0 ) );
    TQTableItem *itm = item(row, col);
    if (itm && itm->row() >= 0 && itm->col() >= 0) { // get the correct row and col if the item is spanning
        row = itm->row();
        col = itm->col();
    }
    insertWidget( row, col, e );
    TQRect cr = cellGeometry( row, col );
    e->resize( cr.size() );
    moveChild( e, cr.x(), cr.y() );
    e->show();
}

/*!
    Inserts widget \a w at \a row, \a col into the internal
    data structure. See the documentation of setCellWidget() for
    further details.

    If you don't use \l{TQTableItem}s you may need to reimplement this
    function: see the notes on large tables.
*/

void TQTable::insertWidget( int row, int col, TQWidget *w )
{
    if ( row < 0 || col < 0 || row > numRows() - 1 || col > numCols() - 1 )
	return;

    if ( (int)widgets.size() != numRows() * numCols() )
	widgets.resize( numRows() * numCols() );

    widgets.insert( indexOf( row, col ), w );
}

/*!
    Returns the widget that has been set for the cell at \a row, \a
    col, or 0 if no widget has been set.

    If you don't use \l{TQTableItem}s you may need to reimplement this
    function: see the notes on large tables.

    \sa clearCellWidget() setCellWidget()
*/

TQWidget *TQTable::cellWidget( int row, int col ) const
{
    if ( row < 0 || col < 0 || row > numRows() - 1 || col > numCols() - 1 )
	return 0;

    if ( (int)widgets.size() != numRows() * numCols() )
	( (TQTable*)this )->widgets.resize( numRows() * numCols() );

    return widgets[ indexOf( row, col ) ];
}

/*!
    Removes the widget (if there is one) set for the cell at \a row,
    \a col.

    If you don't use \l{TQTableItem}s you may need to reimplement this
    function: see the notes on large tables.

    This function deletes the widget at \a row, \a col. Note that the
    widget is not deleted immediately; instead TQObject::deleteLater()
    is called on the widget to avoid problems with timing issues.

    \sa cellWidget() setCellWidget()
*/

void TQTable::clearCellWidget( int row, int col )
{
    if ( row < 0 || col < 0 || row > numRows() - 1 || col > numCols() - 1 )
	return;

    if ( (int)widgets.size() != numRows() * numCols() )
	widgets.resize( numRows() * numCols() );

    TQWidget *w = cellWidget( row, col );
    if ( w ) {
	w->removeEventFilter( this );
	w->deleteLater();
    }
    widgets.setAutoDelete( FALSE );
    widgets.remove( indexOf( row, col ) );
    widgets.setAutoDelete( TRUE );
}

/*!
    \fn void TQTable::dropped ( TQDropEvent * e )

    This signal is emitted when a drop event occurred on the table.

    \a e contains information about the drop.
*/

/*!
    If \a b is TRUE, the table starts a drag (see dragObject()) when
    the user presses and moves the mouse on a selected cell.
*/

void TQTable::setDragEnabled( bool b )
{
    dEnabled = b;
}

/*!
    If this function returns TRUE, the table supports dragging.

    \sa setDragEnabled();
*/

bool TQTable::dragEnabled() const
{
    return dEnabled;
}

/*!
    Inserts \a count empty rows at row \a row. Also clears the selection(s).

    \sa insertColumns() removeRow()
*/

void TQTable::insertRows( int row, int count )
{
    // special case, so a call like insertRow( currentRow(), 1 ) also
    // works, when we have 0 rows and currentRow() is -1
    if ( row == -1 && curRow == -1 )
	row = 0;
    if ( row < 0 || count <= 0 )
	return;

    if ( curRow >= row && curRow < row + count )
	curRow = row + count;

    --row;
    if ( row >= numRows() )
	return;

    bool updatesEnabled = isUpdatesEnabled();
    setUpdatesEnabled( FALSE );
    bool leftHeaderUpdatesEnabled = leftHeader->isUpdatesEnabled();
    leftHeader->setUpdatesEnabled( FALSE );
    int oldLeftMargin = leftMargin();

    setNumRows( numRows() + count );

    for ( int i = numRows() - count - 1; i > row; --i )
	leftHeader->swapSections( i, i + count );

    leftHeader->setUpdatesEnabled( leftHeaderUpdatesEnabled );
    setUpdatesEnabled( updatesEnabled );

    int cr = TQMAX( 0, currentRow() );
    int cc = TQMAX( 0, currentColumn() );
    if ( curRow > row )
	curRow -= count; // this is where curRow was
    setCurrentCell( cr, cc, TRUE, FALSE ); // without ensureCellVisible

    // Repaint the header
    if ( leftHeaderUpdatesEnabled ) {
	int y = rowPos( row ) - contentsY();
	if ( leftMargin() != oldLeftMargin || d->hasRowSpan )
	    y = 0; // full repaint
	TQRect rect( 0, y, leftHeader->width(), contentsHeight() );
	leftHeader->update( rect );
    }

    if ( updatesEnabled ) {
	int p = rowPos( row );
	if ( d->hasRowSpan )
	    p = contentsY();
	updateContents( contentsX(), p, visibleWidth(), contentsHeight() + 1 );
    }
}

/*!
    Inserts \a count empty columns at column \a col.  Also clears the selection(s).

    \sa insertRows() removeColumn()
*/

void TQTable::insertColumns( int col, int count )
{
    // see comment in insertRows()
    if ( col == -1 && curCol == -1 )
	col = 0;
    if ( col < 0 || count <= 0 )
	return;

    if ( curCol >= col && curCol < col + count )
	curCol = col + count;

    --col;
    if ( col >= numCols() )
	return;

    bool updatesEnabled = isUpdatesEnabled();
    setUpdatesEnabled( FALSE );
    bool topHeaderUpdatesEnabled = topHeader->isUpdatesEnabled();
    topHeader->setUpdatesEnabled( FALSE );
    int oldTopMargin = topMargin();

    setNumCols( numCols() + count );

    for ( int i = numCols() - count - 1; i > col; --i )
	topHeader->swapSections( i, i + count );

    topHeader->setUpdatesEnabled( topHeaderUpdatesEnabled );
    setUpdatesEnabled( updatesEnabled );

    int cr = TQMAX( 0, currentRow() );
    int cc = TQMAX( 0, currentColumn() );
    if ( curCol > col )
	curCol -= count; // this is where curCol was
    setCurrentCell( cr, cc, TRUE, FALSE ); // without ensureCellVisible

    // Repaint the header
    if ( topHeaderUpdatesEnabled ) {
	int x = columnPos( col ) - contentsX();
	if ( topMargin() != oldTopMargin || d->hasColSpan )
	    x = 0; // full repaint
	TQRect rect( x, 0, contentsWidth(), topHeader->height() );
	topHeader->update( rect );
    }

    if ( updatesEnabled ) {
	int p = columnPos( col );
	if ( d->hasColSpan )
	    p = contentsX();
	updateContents( p, contentsY(), contentsWidth() + 1, visibleHeight() );
    }
}

/*!
    Removes row \a row, and deletes all its cells including any table
    items and widgets the cells may contain. Also clears the selection(s).

    \sa hideRow() insertRows() removeColumn() removeRows()
*/

void TQTable::removeRow( int row )
{
    if ( row < 0 || row >= numRows() )
	return;
    if ( row < numRows() - 1 ) {
	if (d->hiddenRows.find(row))
	    d->hiddenRows.remove(row);

	for ( int i = row; i < numRows() - 1; ++i )
	    ( (TQTableHeader*)verticalHeader() )->swapSections( i, i + 1 );
    }
    setNumRows( numRows() - 1 );
}

/*!
    Removes the rows listed in the array \a rows, and deletes all their
    cells including any table items and widgets the cells may contain.

    The array passed in must only contain valid rows (in the range
    from 0 to numRows() - 1) with no duplicates, and must be sorted in
    ascending order. Also clears the selection(s).

    \sa removeRow() insertRows() removeColumns()
*/

void TQTable::removeRows( const TQMemArray<int> &rows )
{
    if ( rows.count() == 0 )
	return;
    int i;
    for ( i = 0; i < (int)rows.count() - 1; ++i ) {
	for ( int j = rows[i] - i; j < rows[i + 1] - i - 1; j++ ) {
	    ( (TQTableHeader*)verticalHeader() )->swapSections( j, j + i + 1 );
	}
    }

    for ( int j = rows[i] - i; j < numRows() - (int)rows.size(); j++)
	( (TQTableHeader*)verticalHeader() )->swapSections( j, j + rows.count() );

    setNumRows( numRows() - rows.count() );
}

/*!
    Removes column \a col, and deletes all its cells including any
    table items and widgets the cells may contain. Also clears the
    selection(s).

    \sa removeColumns() hideColumn() insertColumns() removeRow()
*/

void TQTable::removeColumn( int col )
{
    if ( col < 0 || col >= numCols() )
	return;
    if ( col < numCols() - 1 ) {
	if (d->hiddenCols.find(col))
	    d->hiddenCols.remove(col);

	for ( int i = col; i < numCols() - 1; ++i )
	    ( (TQTableHeader*)horizontalHeader() )->swapSections( i, i + 1 );
    }
    setNumCols( numCols() - 1 );
}

/*!
    Removes the columns listed in the array \a cols, and deletes all
    their cells including any table items and widgets the cells may
    contain.

    The array passed in must only contain valid columns (in the range
    from 0 to numCols() - 1) with no duplicates, and must be sorted in
    ascending order. Also clears the selection(s).

   \sa removeColumn() insertColumns() removeRows()
*/

void TQTable::removeColumns( const TQMemArray<int> &cols )
{
    if ( cols.count() == 0 )
	return;
    int i;
    for ( i = 0; i < (int)cols.count() - 1; ++i ) {
	for ( int j = cols[i] - i; j < cols[i + 1] - i - 1; j++ ) {
	    ( (TQTableHeader*)horizontalHeader() )->swapSections( j, j + i + 1 );
	}
    }

    for ( int j = cols[i] - i; j < numCols() - (int)cols.size(); j++)
	( (TQTableHeader*)horizontalHeader() )->swapSections( j, j + cols.count() );

    setNumCols( numCols() - cols.count() );
}

/*!
    Starts editing the cell at \a row, \a col.

    If \a replace is TRUE the content of this cell will be replaced by
    the content of the editor when editing is finished, i.e. the user
    will be entering new data; otherwise the current content of the
    cell (if any) will be modified in the editor.

    \sa beginEdit()
*/

void TQTable::editCell( int row, int col, bool replace )
{
    if ( row < 0 || col < 0 || row > numRows() - 1 || col > numCols() - 1 )
	return;

    if ( beginEdit( row, col, replace ) ) {
	edMode = Editing;
	editRow = row;
	editCol = col;
    }
}

#ifndef QT_NO_DRAGANDDROP

/*!
    This event handler is called whenever a TQTable object receives a
    \l TQDragEnterEvent \a e, i.e. when the user pressed the mouse
    button to drag something.

    The focus is moved to the cell where the TQDragEnterEvent occurred.
*/

void TQTable::contentsDragEnterEvent( TQDragEnterEvent *e )
{
    oldCurrentRow = curRow;
    oldCurrentCol = curCol;
    int tmpRow = rowAt( e->pos().y() );
    int tmpCol = columnAt( e->pos().x() );
    fixRow( tmpRow, e->pos().y() );
    fixCol( tmpCol, e->pos().x() );
    if (e->source() != (TQObject*)cellWidget( currentRow(), currentColumn() ) )
	setCurrentCell( tmpRow, tmpCol, FALSE, TRUE );
    e->accept();
}

/*!
    This event handler is called whenever a TQTable object receives a
    \l TQDragMoveEvent \a e, i.e. when the user actually drags the
    mouse.

    The focus is moved to the cell where the TQDragMoveEvent occurred.
*/

void TQTable::contentsDragMoveEvent( TQDragMoveEvent *e )
{
    int tmpRow = rowAt( e->pos().y() );
    int tmpCol = columnAt( e->pos().x() );
    fixRow( tmpRow, e->pos().y() );
    fixCol( tmpCol, e->pos().x() );
    if (e->source() != (TQObject*)cellWidget( currentRow(), currentColumn() ) )
	setCurrentCell( tmpRow, tmpCol, FALSE, TRUE );
    e->accept();
}

/*!
    This event handler is called when a drag activity leaves \e this
    TQTable object with event \a e.
*/

void TQTable::contentsDragLeaveEvent( TQDragLeaveEvent * )
{
    setCurrentCell( oldCurrentRow, oldCurrentCol, FALSE, TRUE );
}

/*!
    This event handler is called when the user ends a drag and drop by
    dropping something onto \e this TQTable and thus triggers the drop
    event, \a e.
*/

void TQTable::contentsDropEvent( TQDropEvent *e )
{
    setCurrentCell( oldCurrentRow, oldCurrentCol, FALSE, TRUE );
    emit dropped( e );
}

/*!
    If the user presses the mouse on a selected cell, starts moving
    (i.e. dragging), and dragEnabled() is TRUE, this function is
    called to obtain a drag object. A drag using this object begins
    immediately unless dragObject() returns 0.

    By default this function returns 0. You might reimplement it and
    create a TQDragObject depending on the selected items.

    \sa dropped()
*/

TQDragObject *TQTable::dragObject()
{
    return 0;
}

/*!
    Starts a drag.

    Usually you don't need to call or reimplement this function yourself.

    \sa dragObject();
*/

void TQTable::startDrag()
{
    if ( startDragRow == -1 || startDragCol == -1 )
	return;

    startDragRow = startDragCol = -1;

    TQDragObject *drag = dragObject();
    if ( !drag )
	return;

    drag->drag();
}

#endif

/*! \reimp */
void TQTable::windowActivationChange( bool oldActive )
{
    if ( oldActive && autoScrollTimer )
	autoScrollTimer->stop();

    if ( !isVisible() )
	return;

    if ( palette().active() != palette().inactive() )
	updateContents();
}

/*! \reimp */
void TQTable::setEnabled( bool b )
{
    if ( !b ) {
	// editor will lose focus, causing a crash deep in setEnabled(),
	// so we'll end the edit early.
	endEdit( editRow, editCol, TRUE, edMode != Editing );
    }
    TQScrollView::setEnabled(b);
}


/*
    \class TQTableHeader
    \brief The TQTableHeader class allows for creation and manipulation
    of table headers.
\if defined(commercial)
    It is part of the <a href="commercialeditions.html">TQt Enterprise Edition</a>.
\endif

    \ingroup advanced
    \module table


   TQTable uses this subclass of TQHeader for its headers. TQTable has a
   horizontalHeader() for displaying column labels, and a
   verticalHeader() for displaying row labels.

*/

/*
    \enum TQTableHeader::SectionState

    This enum type denotes the state of the header's text

    \value Normal the default
    \value Bold
    \value Selected  typically represented by showing the section "sunken"
    or "pressed in"
*/

/*!
    Creates a new table header called \a name with \a i sections. It
    is a child of widget \a parent and attached to table \a t.
*/

TQTableHeader::TQTableHeader( int i, TQTable *t,
			    TQWidget *parent, const char *name )
    : TQHeader( i, parent, name ), mousePressed(FALSE), startPos(-1),
      table( t ), caching( FALSE ), resizedSection(-1),
      numStretches( 0 )
{
    setIsATableHeader( TRUE );
    d = 0;
    states.resize( i );
    stretchable.resize( i );
    states.fill( Normal, -1 );
    stretchable.fill( FALSE, -1 );
    autoScrollTimer = new TQTimer( this );
    connect( autoScrollTimer, SIGNAL( timeout() ),
	     this, SLOT( doAutoScroll() ) );
#ifndef NO_LINE_WIDGET
    line1 = new TQWidget( table->viewport(), "qt_line1" );
    line1->hide();
    line1->setBackgroundMode( PaletteText );
    table->addChild( line1 );
    line2 = new TQWidget( table->viewport(), "qt_line2" );
    line2->hide();
    line2->setBackgroundMode( PaletteText );
    table->addChild( line2 );
#else
    d = new TQTableHeaderPrivate;
    d->oldLinePos = -1; //outside, in contents coords
#endif
    connect( this, SIGNAL( sizeChange(int,int,int) ),
	     this, SLOT( sectionWidthChanged(int,int,int) ) );
    connect( this, SIGNAL( indexChange(int,int,int) ),
	     this, SLOT( indexChanged(int,int,int) ) );

    stretchTimer = new TQTimer( this );
    widgetStretchTimer = new TQTimer( this );
    connect( stretchTimer, SIGNAL( timeout() ),
	     this, SLOT( updateStretches() ) );
    connect( widgetStretchTimer, SIGNAL( timeout() ),
	     this, SLOT( updateWidgetStretches() ) );
    startPos = -1;
}

/*!
    Adds a new section, \a size pixels wide (or high for vertical
    headers) with the label \a s. If \a size is negative the section's
    size is calculated based on the width (or height) of the label's
    text.
*/

void TQTableHeader::addLabel( const TQString &s , int size )
{
    TQHeader::addLabel( s, size );
    if ( count() > (int)states.size() ) {
	int s = states.size();
	states.resize( count() );
	stretchable.resize( count() );
	for ( ; s < count(); ++s ) {
	    states[ s ] = Normal;
	    stretchable[ s ] = FALSE;
	}
    }
}

void TQTableHeader::removeLabel( int section )
{
    TQHeader::removeLabel( section );
    if ( section == (int)states.size() - 1 ) {
	states.resize( states.size() - 1 );
	stretchable.resize( stretchable.size() - 1 );
    }
}

void TQTableHeader::resizeArrays( int n )
{
    int old = states.size();
    states.resize( n );
    stretchable.resize( n );
    if ( n > old ) {
	for ( int i = old; i < n; ++i ) {
	    stretchable[ i ] = FALSE;
	    states[ i ] = Normal;
	}
    }
}

void TQTableHeader::setLabel( int section, const TQString & s, int size )
{
    TQHeader::setLabel( section, s, size );
    sectionLabelChanged( section );
}

void TQTableHeader::setLabel( int section, const TQIconSet & iconset,
			     const TQString & s, int size )
{
    TQHeader::setLabel( section, iconset, s, size );
    sectionLabelChanged( section );
}

/*!
    Sets the SectionState of section \a s to \a astate.

    \sa sectionState()
*/

void TQTableHeader::setSectionState( int s, SectionState astate )
{
    if ( s < 0 || s >= (int)states.count() )
	return;
    if ( states.data()[ s ] == astate )
	return;
    if ( isRowSelection( table->selectionMode() ) && orientation() == Horizontal )
	return;

    states.data()[ s ] = astate;
    if ( isUpdatesEnabled() ) {
	if ( orientation() == Horizontal )
	    repaint( sectionPos( s ) - offset(), 0, sectionSize( s ), height(), FALSE );
	else
	    repaint( 0, sectionPos( s ) - offset(), width(), sectionSize( s ), FALSE );
    }
}

void TQTableHeader::setSectionStateToAll( SectionState state )
{
    if ( isRowSelection( table->selectionMode() ) && orientation() == Horizontal )
	return;

    register int *d = (int *) states.data();
    int n = count();

    while (n >= 4) {
	d[0] = state;
	d[1] = state;
	d[2] = state;
	d[3] = state;
	d += 4;
	n -= 4;
    }

    if (n > 0) {
	d[0] = state;
	if (n > 1) {
	    d[1] = state;
	    if (n > 2) {
		d[2] = state;
	    }
	}
    }
}

/*!
    Returns the SectionState of section \a s.

    \sa setSectionState()
*/

TQTableHeader::SectionState TQTableHeader::sectionState( int s ) const
{
    return (s < 0 || s >= (int)states.count() ? Normal : (TQTableHeader::SectionState)states[s]);
}

/*! \reimp
*/

void TQTableHeader::paintEvent( TQPaintEvent *e )
{
    TQPainter p( this );
    p.setPen( colorGroup().buttonText() );
    int pos = orientation() == Horizontal
		     ? e->rect().left()
		     : e->rect().top();
    int id = mapToIndex( sectionAt( pos + offset() ) );
    if ( id < 0 ) {
	if ( pos > 0 )
	    return;
	else
	    id = 0;
    }

    TQRegion reg = e->region();
    for ( int i = id; i < count(); i++ ) {
	TQRect r = sRect( i );
	reg -= r;
	p.save();
	if ( !( orientation() == Horizontal && isRowSelection( table->selectionMode() ) ) &&
	     ( sectionState( i ) == Bold || sectionState( i ) == Selected ) ) {
	    TQFont f( font() );
	    f.setBold( TRUE );
	    p.setFont( f );
	}
	paintSection( &p, i, r );
	p.restore();
	if ( orientation() == Horizontal && r. right() >= e->rect().right() ||
	     orientation() == Vertical && r. bottom() >= e->rect().bottom() )
	    return;
    }
    if ( !reg.isEmpty() )
	erase( reg );
}

/*!
    \reimp

    Paints the header section with index \a index into the rectangular
    region \a fr on the painter \a p.
*/

void TQTableHeader::paintSection( TQPainter *p, int index, const TQRect& fr )
{
    int section = mapToSection( index );
    if ( section < 0 || cellSize( section ) <= 0 )
	return;

   if ( sectionState( index ) != Selected ||
	 orientation() == Horizontal && isRowSelection( table->selectionMode() ) ) {
	TQHeader::paintSection( p, index, fr );
   } else {
       TQStyle::SFlags flags = TQStyle::Style_Off | ( orient == Horizontal ? TQStyle::Style_Horizontal : 0 );
       if(isEnabled())
	   flags |= TQStyle::Style_Enabled;
       if(isClickEnabled()) {
	   if(sectionState(index) == Selected) {
	       flags |= TQStyle::Style_Down;
	       if(!mousePressed)
		   flags |= TQStyle::Style_Sunken;
	   }
       }
       if(!(flags & TQStyle::Style_Down))
	   flags |= TQStyle::Style_Raised;
       style().drawPrimitive( TQStyle::PE_HeaderSection, p, TQRect(fr.x(), fr.y(), fr.width(), fr.height()),
			      colorGroup(), flags );
       paintSectionLabel( p, index, fr );
   }
}

static int real_pos( const TQPoint &p, TQt::Orientation o )
{
    if ( o == TQt::Horizontal )
	return p.x();
    return p.y();
}

/*! \reimp
*/

void TQTableHeader::mousePressEvent( TQMouseEvent *e )
{
    if ( e->button() != LeftButton )
	return;
    TQHeader::mousePressEvent( e );
    mousePressed = TRUE;
    pressPos = real_pos( e->pos(), orientation() );
    if ( !table->currentSel || ( e->state() & ShiftButton ) != ShiftButton )
	startPos = -1;
    setCaching( TRUE );
    resizedSection = -1;
#ifdef QT_NO_CURSOR
    isResizing = FALSE;
#else
    isResizing = cursor().shape() != ArrowCursor;
    if ( !isResizing && sectionAt( pressPos ) != -1 )
	doSelection( e );
#endif
}

/*! \reimp
*/

void TQTableHeader::mouseMoveEvent( TQMouseEvent *e )
{
    if ( (e->state() & MouseButtonMask) != LeftButton // Using LeftButton simulates old behavior.
#ifndef QT_NO_CURSOR
         || cursor().shape() != ArrowCursor
#endif
         || ( ( e->state() & ControlButton ) == ControlButton &&
              ( orientation() == Horizontal
	     ? table->columnMovingEnabled() : table->rowMovingEnabled() ) ) ) {
	TQHeader::mouseMoveEvent( e );
	return;
    }

    if ( !doSelection( e ) )
	TQHeader::mouseMoveEvent( e );
}

bool TQTableHeader::doSelection( TQMouseEvent *e )
{
    int p = real_pos( e->pos(), orientation() ) + offset();

    if ( isRowSelection( table->selectionMode() ) ) {
	if ( orientation() == Horizontal )
	    return TRUE;
	if ( table->selectionMode() == TQTable::SingleRow ) {
	    int secAt = sectionAt( p );
	    if ( secAt == -1 )
		return TRUE;
	    table->setCurrentCell( secAt, table->currentColumn() );
	    return TRUE;
	}
    }

    if ( startPos == -1 ) {
 	int secAt = sectionAt( p );
	if ( ( e->state() & ControlButton ) != ControlButton &&
	     ( e->state() & ShiftButton ) != ShiftButton ||
	     table->selectionMode() == TQTable::Single ||
	     table->selectionMode() == TQTable::SingleRow ) {
	    startPos = p;
	    bool b = table->signalsBlocked();
	    table->blockSignals( TRUE );
	    table->clearSelection();
	    table->blockSignals( b );
	}
	saveStates();

	if ( table->selectionMode() != TQTable::NoSelection ) {
	    startPos = p;
	    TQTableSelection *oldSelection = table->currentSel;

	    if ( orientation() == Vertical ) {
		if ( !table->isRowSelected( secAt, TRUE ) ) {
		    table->currentSel = new TQTableSelection();
		    table->selections.append( table->currentSel );
		    table->currentSel->init( secAt, 0 );
		    table->currentSel->expandTo( secAt, table->numCols() - 1 );
                    emit table->selectionChanged();
		}
		table->setCurrentCell( secAt, 0 );
	    } else { // orientation == Horizontal
		if ( !table->isColumnSelected( secAt, TRUE ) ) {
		    table->currentSel = new TQTableSelection();
		    table->selections.append( table->currentSel );
		    table->currentSel->init( 0, secAt );
		    table->currentSel->expandTo( table->numRows() - 1, secAt );
                    emit table->selectionChanged();
		}
		table->setCurrentCell( 0, secAt );
	    }

            if ( orientation() == Horizontal && table->isColumnSelected(secAt) ||
                 orientation() == Vertical && table->isRowSelected(secAt)) {
                setSectionState( secAt, Selected );
            }

 	    table->repaintSelections( oldSelection, table->currentSel,
 				      orientation() == Horizontal,
 				      orientation() == Vertical );
	    if ( sectionAt( p ) != -1 )
 		endPos = p;

 	    return TRUE;
	}
    }

    if ( sectionAt( p ) != -1 )
	endPos = p;
    if ( startPos != -1 ) {
	updateSelections();
	p -= offset();
	if ( orientation() == Horizontal && ( p < 0 || p > width() ) ) {
	    doAutoScroll();
	    autoScrollTimer->start( 100, TRUE );
	} else if ( orientation() == Vertical && ( p < 0 || p > height() ) ) {
	    doAutoScroll();
	    autoScrollTimer->start( 100, TRUE );
	}
	return TRUE;
    }
    return table->selectionMode() == TQTable::NoSelection;
}

static inline bool mayOverwriteMargin( int before, int after )
{
    /*
      0 is the only user value that we always respect. We also never
      shrink a margin, in case the user wanted it that way.
    */
    return before != 0 && before < after;
}

void TQTableHeader::sectionLabelChanged( int section )
{
    emit sectionSizeChanged( section );

    // this does not really belong here
    if ( orientation() == Horizontal ) {
	int h = sizeHint().height();
	if ( h != height() && mayOverwriteMargin(table->topMargin(), h) )
	    table->setTopMargin( h );
    } else {
	int w = sizeHint().width();
	if ( w != width() && mayOverwriteMargin( ( TQApplication::reverseLayout() ? table->rightMargin() : table->leftMargin() ), w) )
	    table->setLeftMargin( w );
    }
}

/*! \reimp */
void TQTableHeader::mouseReleaseEvent( TQMouseEvent *e )
{
    if ( e->button() != LeftButton )
	return;
    autoScrollTimer->stop();
    mousePressed = FALSE;
    setCaching( FALSE );
    TQHeader::mouseReleaseEvent( e );
#ifndef NO_LINE_WIDGET
    line1->hide();
    line2->hide();
#else
    if ( d->oldLinePos >= 0 )
	if ( orientation() == Horizontal )
	    table->updateContents( d->oldLinePos, table->contentsY(),
				   1, table->visibleHeight() );
	else
	    table->updateContents(  table->contentsX(), d->oldLinePos,
				    table->visibleWidth(), 1 );
    d->oldLinePos = -1;
#endif
    if ( resizedSection != -1 ) {
	emit sectionSizeChanged( resizedSection );
	updateStretches();
    }

    //Make sure all newly selected sections are painted one last time
    TQRect selectedRects;
    for ( int i = 0; i < count(); i++ ) {
	if(sectionState( i ) == Selected)
	    selectedRects |= sRect( i );
    }
    if(!selectedRects.isNull())
	repaint(selectedRects);
}

/*! \reimp
*/

void TQTableHeader::mouseDoubleClickEvent( TQMouseEvent *e )
{
    if ( e->button() != LeftButton )
	return;
    if ( isResizing ) {
	int p = real_pos( e->pos(), orientation() ) + offset();
	int section = sectionAt( p );
	if ( section == -1 )
	    return;
	section--;
	if ( p >= sectionPos( count() - 1 ) + sectionSize( count() - 1 ) )
	    ++section;
	while ( sectionSize( section ) == 0 )
	    section--;
	if ( section < 0 )
	    return;
	int oldSize = sectionSize( section );
	if ( orientation() == Horizontal ) {
	    table->adjustColumn( section );
	    int newSize = sectionSize( section );
	    if ( oldSize != newSize )
		emit sizeChange( section, oldSize, newSize );
	    for ( int i = 0; i < table->numCols(); ++i ) {
		if ( table->isColumnSelected( i ) && sectionSize( i ) != 0 )
		    table->adjustColumn( i );
	    }
	} else {
	    table->adjustRow( section );
	    int newSize = sectionSize( section );
	    if ( oldSize != newSize )
		emit sizeChange( section, oldSize, newSize );
	    for ( int i = 0; i < table->numRows(); ++i ) {
		if ( table->isRowSelected( i )  && sectionSize( i ) != 0 )
		    table->adjustRow( i );
	    }
	}
    }
}

/*! \reimp
*/

void TQTableHeader::resizeEvent( TQResizeEvent *e )
{
    stretchTimer->stop();
    widgetStretchTimer->stop();
    TQHeader::resizeEvent( e );
    if ( numStretches == 0 )
	return;
    stretchTimer->start( 0, TRUE );
}

void TQTableHeader::updateStretches()
{
    if ( numStretches == 0 )
	return;

    int dim = orientation() == Horizontal ? width() : height();
    if ( sectionPos(count() - 1) + sectionSize(count() - 1) == dim )
	return;
    int i;
    int pd = dim - ( sectionPos(count() - 1)
		     + sectionSize(count() - 1) );
    bool block = signalsBlocked();
    blockSignals( TRUE );
    for ( i = 0; i < (int)stretchable.count(); ++i ) {
	if ( !stretchable[i] ||
	     ( stretchable[i] && table->d->hiddenCols[i] ) )
	    continue;
	pd += sectionSize( i );
    }
    pd /= numStretches;
    for ( i = 0; i < (int)stretchable.count(); ++i ) {
	if ( !stretchable[i] ||
	     ( stretchable[i] && table->d->hiddenCols[i] ) )
	    continue;
	if ( i == (int)stretchable.count() - 1 &&
	     sectionPos( i ) + pd < dim )
	    pd = dim - sectionPos( i );
	resizeSection( i, TQMAX( 20, pd ) );
    }
    blockSignals( block );
    table->repaintContents( FALSE );
    widgetStretchTimer->start( 100, TRUE );
}

void TQTableHeader::updateWidgetStretches()
{
    TQSize s = table->tableSize();
    table->resizeContents( s.width(), s.height() );
    for ( int i = 0; i < table->numCols(); ++i )
	table->updateColWidgets( i );
}

void TQTableHeader::updateSelections()
{
    if ( table->selectionMode() == TQTable::NoSelection ||
	 (isRowSelection( table->selectionMode() ) && orientation() != Vertical  ) )
	return;
    int a = sectionAt( startPos );
    int b = sectionAt( endPos );
    int start = TQMIN( a, b );
    int end = TQMAX( a, b );
    register int *s = states.data();
    for ( int i = 0; i < count(); ++i ) {
	if ( i < start || i > end )
	    *s = oldStates.data()[ i ];
	else
	    *s = Selected;
	++s;
    }
    repaint( FALSE );

    if (table->currentSel) {
	TQTableSelection oldSelection = *table->currentSel;
	if ( orientation() == Vertical )
	    table->currentSel->expandTo( b, table->horizontalHeader()->count() - 1 );
	else
	    table->currentSel->expandTo( table->verticalHeader()->count() - 1, b );
	table->repaintSelections( &oldSelection, table->currentSel,
				  orientation() == Horizontal,
				  orientation() == Vertical );
    }
    emit table->selectionChanged();
}

void TQTableHeader::saveStates()
{
    oldStates.resize( count() );
    register int *s = states.data();
    register int *s2 = oldStates.data();
    for ( int i = 0; i < count(); ++i ) {
	*s2 = *s;
	++s2;
	++s;
    }
}

void TQTableHeader::doAutoScroll()
{
    TQPoint pos = mapFromGlobal( TQCursor::pos() );
    int p = real_pos( pos, orientation() ) + offset();
    if ( sectionAt( p ) != -1 )
	endPos = p;
    if ( orientation() == Horizontal )
	table->ensureVisible( endPos, table->contentsY() );
    else
	table->ensureVisible( table->contentsX(), endPos );
    updateSelections();
    autoScrollTimer->start( 100, TRUE );
}

void TQTableHeader::sectionWidthChanged( int col, int, int )
{
    resizedSection = col;
    if ( orientation() == Horizontal ) {
#ifndef NO_LINE_WIDGET
	table->moveChild( line1, TQHeader::sectionPos( col ) - 1,
			  table->contentsY() );
	line1->resize( 1, table->visibleHeight() );
	line1->show();
	line1->raise();
	table->moveChild( line2,
			  TQHeader::sectionPos( col ) + TQHeader::sectionSize( col ) - 1,
			  table->contentsY() );
	line2->resize( 1, table->visibleHeight() );
	line2->show();
	line2->raise();
#else
	TQPainter p( table->viewport() );
	int lx = TQHeader::sectionPos( col ) + TQHeader::sectionSize( col ) - 1;
	int ly = table->contentsY();

	if ( lx != d->oldLinePos ) {
	    TQPoint pt = table->contentsToViewport( TQPoint( lx, ly ) );
	    p.drawLine( pt.x(), pt.y()+1,
			pt.x(), pt.y()+ table->visibleHeight() );
	    if ( d->oldLinePos >= 0 )
		table->repaintContents( d->oldLinePos, table->contentsY(),
				       1, table->visibleHeight() );

	    d->oldLinePos = lx;
	}
#endif
    } else {
#ifndef NO_LINE_WIDGET
	table->moveChild( line1, table->contentsX(),
			  TQHeader::sectionPos( col ) - 1 );
	line1->resize( table->visibleWidth(), 1 );
	line1->show();
	line1->raise();
	table->moveChild( line2, table->contentsX(),
			  TQHeader::sectionPos( col ) + TQHeader::sectionSize( col ) - 1 );
	line2->resize( table->visibleWidth(), 1 );
	line2->show();
	line2->raise();

#else
	TQPainter p( table->viewport() );
	int lx = table->contentsX();
	int ly = TQHeader::sectionPos( col ) + TQHeader::sectionSize( col ) - 1;

	if ( ly != d->oldLinePos ) {
	    TQPoint pt = table->contentsToViewport( TQPoint( lx, ly ) );
	    p.drawLine( pt.x()+1, pt.y(),
			pt.x() + table->visibleWidth(), pt.y() );
	    if ( d->oldLinePos >= 0 )
		table->repaintContents(  table->contentsX(), d->oldLinePos,
					table->visibleWidth(), 1 );
	    d->oldLinePos = ly;
	}

#endif
    }
}

/*!
    \reimp

    Returns the size of section \a section in pixels or -1 if \a
    section is out of range.
*/

int TQTableHeader::sectionSize( int section ) const
{
    if ( count() <= 0 || section < 0 || section >= count() )
	return -1;
    if ( caching && section < (int)sectionSizes.count() )
	 return sectionSizes[ section ];
    return TQHeader::sectionSize( section );
}

/*!
    \reimp

    Returns the start position of section \a section in pixels or -1
    if \a section is out of range.

    \sa sectionAt()
*/

int TQTableHeader::sectionPos( int section ) const
{
    if ( count() <= 0 || section < 0 || section >= count() )
	return -1;
    if ( caching && section < (int)sectionPoses.count() )
	return sectionPoses[ section ];
    return TQHeader::sectionPos( section );
}

/*!
    \reimp

    Returns the number of the section at index position \a pos or -1
    if there is no section at the position given.

    \sa sectionPos()
*/

int TQTableHeader::sectionAt( int pos ) const
{
    if ( !caching || sectionSizes.count() <= 0 || sectionPoses.count() <= 0 )
	return TQHeader::sectionAt( pos );
    if ( count() <= 0 || pos > sectionPoses[ count() - 1 ] + sectionSizes[ count() - 1 ] )
	return -1;
    int l = 0;
    int r = count() - 1;
    int i = ( (l+r+1) / 2 );
    while ( r - l ) {
	if ( sectionPoses[i] > pos )
	    r = i -1;
	else
	    l = i;
	i = ( (l+r+1) / 2 );
    }
    if ( sectionPoses[i] <= pos &&
	 pos <= sectionPoses[i] + sectionSizes[ mapToSection( i ) ] )
	return mapToSection( i );
    return -1;
}

void TQTableHeader::updateCache()
{
    sectionPoses.resize( count() );
    sectionSizes.resize( count() );
    if ( !caching )
	return;
    for ( int i = 0; i < count(); ++i ) {
	sectionSizes[ i ] = TQHeader::sectionSize( i );
	sectionPoses[ i ] = TQHeader::sectionPos( i );
    }
}

void TQTableHeader::setCaching( bool b )
{
    if ( caching == b )
	return;
    caching = b;
    sectionPoses.resize( count() );
    sectionSizes.resize( count() );
    if ( b ) {
	for ( int i = 0; i < count(); ++i ) {
	    sectionSizes[ i ] = TQHeader::sectionSize( i );
	    sectionPoses[ i ] = TQHeader::sectionPos( i );
	}
    }
}

/*!
    If \a b is TRUE, section \a s is stretchable; otherwise the
    section is not stretchable.

    \sa isSectionStretchable()
*/

void TQTableHeader::setSectionStretchable( int s, bool b )
{
    if ( stretchable[ s ] == b )
	return;
    stretchable[ s ] = b;
    if ( b )
	numStretches++;
    else
	numStretches--;
}

/*!
    Returns TRUE if section \a s is stretcheable; otherwise returns
    FALSE.

    \sa setSectionStretchable()
*/

bool TQTableHeader::isSectionStretchable( int s ) const
{
    return stretchable[ s ];
}

void TQTableHeader::swapSections( int oldIdx, int newIdx, bool swapTable )
{
    extern bool qt_qheader_label_return_null_strings; // qheader.cpp
    qt_qheader_label_return_null_strings = TRUE;

    TQIconSet oldIconSet, newIconSet;
    if ( iconSet( oldIdx ) )
	oldIconSet = *iconSet( oldIdx );
    if ( iconSet( newIdx ) )
	newIconSet = *iconSet( newIdx );
    TQString oldLabel = label( oldIdx );
    TQString newLabel = label( newIdx );
    bool sectionsHasContent = !(oldIconSet.isNull() && newIconSet.isNull()
                            && oldLabel.isNull() && newLabel.isNull());
    if (sectionsHasContent) {
        setLabel( oldIdx, newIconSet, newLabel );
        setLabel( newIdx, oldIconSet, oldLabel );
    }

    qt_qheader_label_return_null_strings = FALSE;

    int w1 = sectionSize( oldIdx );
    int w2 = sectionSize( newIdx );
    if ( w1 != w2 ) {
        resizeSection( oldIdx, w2 );
        resizeSection( newIdx, w1 );
    }

    if ( !swapTable )
        return;
    if ( orientation() == Horizontal )
        table->swapColumns( oldIdx, newIdx );
    else
        table->swapRows( oldIdx, newIdx );
}

void TQTableHeader::indexChanged( int sec, int oldIdx, int newIdx )
{
    newIdx = mapToIndex( sec );
    if ( oldIdx > newIdx )
	moveSection( sec, oldIdx + 1 );
    else
	moveSection( sec, oldIdx );

    if ( oldIdx < newIdx ) {
	while ( oldIdx < newIdx ) {
	    swapSections( oldIdx, oldIdx + 1 );
	    oldIdx++;
	}
    } else {
	while ( oldIdx > newIdx ) {
	    swapSections( oldIdx - 1, oldIdx );
	    oldIdx--;
	}
    }

    table->repaintContents( table->contentsX(), table->contentsY(),
			    table->visibleWidth(), table->visibleHeight() );
}

void TQTableHeader::setLabels(const TQStringList & labels)
{
    int i = 0;
    bool updates = isUpdatesEnabled();
    const int c = TQMIN(count(), (int)labels.count());
    setUpdatesEnabled(FALSE);
    for ( TQStringList::ConstIterator it = labels.begin(); i < c; ++i, ++it ) {
	if (i == c - 1) {
	    setUpdatesEnabled(updates);
	    setLabel( i, *it );
	} else {
	    TQHeader::setLabel( i, *it );
	    emit sectionSizeChanged( i );
	}
    }
}

#include "qtable.moc"

#endif // QT_NO_TABLE