summaryrefslogtreecommitdiffstats
path: root/lib/kotext/KoVariable.cpp
blob: fa1b03feb98717ecc677ec6c138a79a7d9bcaecd (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
/* This file is part of the KDE project
   Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
   Copyright (C) 2005 David Faure <faure@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KoVariable.h"
#include "KoVariable.moc"
#include "KoTextZoomHandler.h"
#include "TimeFormatWidget.h"
#include "DateFormatWidget.h"
#include "KoTextCommand.h"
#include "KoTextObject.h"
#include "KoTextParag.h"
#include "KoOasisContext.h"
#include <KoOasisSettings.h>

#include <KoDocumentInfo.h>
#include <KoOasisStyles.h>
#include <KoXmlWriter.h>
#include <KoDocument.h>
#include <KoXmlNS.h>
#include <KoDom.h>

#include <klocale.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kdialogbase.h>
#include <kconfig.h>
#include <tdeversion.h>
#include <kinstance.h>
#include <kcalendarsystem.h>
#include <kaboutdata.h>

#include <tqstringlist.h>
#include <tqcombobox.h>
#include <tqvaluelist.h>
#include <tqdom.h>
#include <tqradiobutton.h>

#include "IsoDuration.h"

class KoVariableSettings::KoVariableSettingPrivate
{
public:
    KoVariableSettingPrivate()
    {
        m_lastPrintingDate.setTime_t(0); // Default is 1970-01-01 midnight locale time
    }
    TQDateTime m_lastPrintingDate;
    TQDateTime m_creationDate;
    TQDateTime m_modificationDate;
};


KoVariableSettings::KoVariableSettings()
{
    d = new KoVariableSettingPrivate;
    m_startingPageNumber = 1;
    m_displayLink = true;
    m_displayComment = true;
    m_underlineLink = true;
    m_displayFieldCode = false;
}

KoVariableSettings::~KoVariableSettings()
{
    delete d;
    d = 0;
}

TQDateTime KoVariableSettings::lastPrintingDate() const
{
    return d->m_lastPrintingDate;
}

void KoVariableSettings::setLastPrintingDate( const TQDateTime & _date)
{
    d->m_lastPrintingDate = _date;
}

TQDateTime KoVariableSettings::creationDate() const
{
    return d->m_creationDate;
}

void KoVariableSettings::setCreationDate( const TQDateTime & _date )
{
    d->m_creationDate = _date;
}

TQDateTime KoVariableSettings::modificationDate() const
{
    return d->m_modificationDate;
}

void KoVariableSettings::setModificationDate( const TQDateTime & _date)
{
    d->m_modificationDate = _date;
}

void KoVariableSettings::saveOasis( KoXmlWriter &settingsWriter ) const
{
    settingsWriter.startElement("config:config-item-set");
    settingsWriter.addAttribute("config:name", "configuration-variable-settings");
    settingsWriter.addConfigItem("displaylink", m_displayLink );
    settingsWriter.addConfigItem( "underlinelink", m_underlineLink);
    settingsWriter.addConfigItem( "displaycomment", m_displayComment);
    settingsWriter.addConfigItem( "displayfieldcode", m_displayFieldCode);
    // m_startingPageNumber isn't saved to OASIS. Applications must use either
    // style:page-number in the first parag of a page (see KoTextParag), or
    // style:first-page-number in style:page-tqlayout, for spreadsheets etc.
    if ( d->m_lastPrintingDate.isValid())
        settingsWriter.addConfigItem("lastPrintingDate", d->m_lastPrintingDate.toString(Qt::ISODate));

    if ( d->m_creationDate.isValid())
        settingsWriter.addConfigItem("creationDate", d->m_creationDate.toString(Qt::ISODate));

    if ( d->m_modificationDate.isValid())
        settingsWriter.addConfigItem("modificationDate", d->m_modificationDate.toString(Qt::ISODate));

    settingsWriter.endElement(); // config:config-item-set
}

void KoVariableSettings::loadOasis(const KoOasisSettings&settingsDoc)
{
    KoOasisSettings::Items configurationSettings = settingsDoc.itemSet( "configuration-variable-settings" );
    if ( !configurationSettings.isNull() )
    {
        m_displayLink = configurationSettings.parseConfigItemBool( "displaylink", true );
        m_underlineLink = configurationSettings.parseConfigItemBool( "underlinelink", true );
        m_displayComment = configurationSettings.parseConfigItemBool( "displaycomment", true );
        m_displayFieldCode = configurationSettings.parseConfigItemBool( "displayfieldcode", false );

        TQString str = configurationSettings.parseConfigItemString( "lastPrintingDate" );
        if ( !str.isEmpty() )
            d->m_lastPrintingDate = TQDateTime::fromString( str, Qt::ISODate );
        else
            d->m_lastPrintingDate.setTime_t(0); // 1970-01-01 00:00:00.000 locale time

        str = configurationSettings.parseConfigItemString( "creationDate" );
        if ( !str.isEmpty() ) {
            d->m_creationDate = TQDateTime::fromString( str, Qt::ISODate );
        }

        str = configurationSettings.parseConfigItemString( "modificationDate" );
        if ( !str.isEmpty() )
            d->m_modificationDate = TQDateTime::fromString( str, Qt::ISODate );

        // m_startingPageNumber isn't loaded from OASIS here. KWTextParag::loadOasis does it.
    }
}

void KoVariableSettings::save( TQDomElement &parentElem )
{
     TQDomElement elem = parentElem.ownerDocument().createElement( "VARIABLESETTINGS" );
     parentElem.appendChild( elem );
    if(m_startingPageNumber!=1)
    {
        elem.setAttribute( "startingPageNumber", m_startingPageNumber );
    }
    elem.setAttribute("displaylink",(int)m_displayLink);
    elem.setAttribute("underlinelink",(int)m_underlineLink);
    elem.setAttribute("displaycomment",(int)m_displayComment);
    elem.setAttribute("displayfieldcode", (int)m_displayFieldCode);

    if ( d->m_lastPrintingDate.isValid())
        elem.setAttribute("lastPrintingDate", d->m_lastPrintingDate.toString(Qt::ISODate));

    if ( d->m_creationDate.isValid())
        elem.setAttribute("creationDate", d->m_creationDate.toString(Qt::ISODate));

    if ( d->m_modificationDate.isValid())
        elem.setAttribute("modificationDate", d->m_modificationDate.toString(Qt::ISODate));
}

void KoVariableSettings::load( TQDomElement &elem )
{
    TQDomElement e = elem.namedItem( "VARIABLESETTINGS" ).toElement();
    if (!e.isNull())
    {
        if(e.hasAttribute("startingPageNumber"))
            m_startingPageNumber = e.attribute("startingPageNumber").toInt();
        if(e.hasAttribute("displaylink"))
            m_displayLink=(bool)e.attribute("displaylink").toInt();
        if(e.hasAttribute("underlinelink"))
            m_underlineLink=(bool)e.attribute("underlinelink").toInt();
        if(e.hasAttribute("displaycomment"))
            m_displayComment=(bool)e.attribute("displaycomment").toInt();
        if (e.hasAttribute("displayfieldcode"))
            m_displayFieldCode=(bool)e.attribute("displayfieldcode").toInt();

        if (e.hasAttribute("lastPrintingDate"))
            d->m_lastPrintingDate = TQDateTime::fromString( e.attribute( "lastPrintingDate" ), Qt::ISODate );
        else
            d->m_lastPrintingDate.setTime_t(0); // 1970-01-01 00:00:00.000 locale time

        if (e.hasAttribute("creationDate")) {
            d->m_creationDate = TQDateTime::fromString( e.attribute( "creationDate" ), Qt::ISODate );
        }

        if (e.hasAttribute("modificationDate"))
            d->m_modificationDate = TQDateTime::fromString( e.attribute( "modificationDate" ), Qt::ISODate );
    }
}

KoVariableDateFormat::KoVariableDateFormat() : KoVariableFormat()
{
}

TQString KoVariableDateFormat::convert( const TQVariant& data ) const
{
    if ( data.type() != TQVariant::Date && data.type() != TQVariant::DateTime )
    {
        kdWarning(32500)<<" Error in KoVariableDateFormat::convert. Value is a "
                      << data.typeName() << "(" << data.type() << ")" << endl;
        // dateTime will be invalid, then set to 1970-01-01
    }
    TQDateTime dateTime ( data.toDateTime() );
    if ( !dateTime.isValid() )
        return i18n("No date set"); // e.g. old KWord documents

    if (m_strFormat.lower() == "locale" || m_strFormat.isEmpty())
        return KGlobal::locale()->formatDate( dateTime.date(), false );
    else if ( m_strFormat.lower() == "localeshort" )
        return KGlobal::locale()->formatDate( dateTime.date(), true );
    else if ( m_strFormat.lower() == "localedatetime" )
        return KGlobal::locale()->formatDateTime( dateTime, false );
    else if ( m_strFormat.lower() == "localedatetimeshort" )
        return KGlobal::locale()->formatDateTime( dateTime, true );

    TQString tmp ( dateTime.toString(m_strFormat) );
    const int month = dateTime.date().month();
    tmp.replace("PPPP", KGlobal::locale()->calendar()->monthNamePossessive(month, false)); //long possessive month name
    tmp.replace("PPP",  KGlobal::locale()->calendar()->monthNamePossessive(month, true));  //short possessive month name
    return tmp;
}

TQCString KoVariableDateFormat::key() const
{
    return getKey( m_strFormat );
}

TQCString KoVariableDateFormat::getKey( const TQString& props ) const
{
    return TQCString("DATE") + props.utf8();
}

void KoVariableDateFormat::load( const TQCString &key )
{
    TQCString params( key.mid( 4 ) ); // skip "DATE"
    if ( !params.isEmpty() )
    {
        if (params[0] == '1' || params[0] == '0') // old m_bShort crap
            params = params.mid(1); // skip it
        m_strFormat = TQString::fromUtf8( params );
    }
}

// Used by KoVariableFormatCollection::popupActionList(), to apply all formats
// to the current data, in the popup menu.
TQStringList KoVariableDateFormat::staticFormatPropsList()
{
    TQStringList listDateFormat;
    listDateFormat<<"locale";
    listDateFormat<<"localeshort";
    listDateFormat<<"localedatetime";
    listDateFormat<<"localedatetimeshort";
    listDateFormat<<"dd/MM/yy";
    listDateFormat<<"dd/MM/yyyy";
    listDateFormat<<"MMM dd,yy";
    listDateFormat<<"MMM dd,yyyy";
    listDateFormat<<"dd.MMM.yyyy";
    listDateFormat<<"MMMM dd, yyyy";
    listDateFormat<<"ddd, MMM dd,yy";
    listDateFormat<<"dddd, MMM dd,yy";
    listDateFormat<<"MM-dd";
    listDateFormat<<"yyyy-MM-dd";
    listDateFormat<<"dd/yy";
    listDateFormat<<"MMMM";
    listDateFormat<<"yyyy-MM-dd hh:mm";
    listDateFormat<<"dd.MMM.yyyy hh:mm";
    listDateFormat<<"MMM dd,yyyy h:mm AP";
    listDateFormat<<"yyyy-MM-ddThh:mm:ss"; // ISO 8601
    return listDateFormat;
}

// Used by dateformatwidget_impl
// TODO: shouldn't it apply the formats to the value, like the popupmenu does?
TQStringList KoVariableDateFormat::staticTranslatedFormatPropsList()
{
    TQStringList listDateFormat;
    listDateFormat<<i18n("Locale date format");
    listDateFormat<<i18n("Short locale date format");
    listDateFormat<<i18n("Locale date & time format");
    listDateFormat<<i18n("Short locale date & time format");
    listDateFormat<<"dd/MM/yy";
    listDateFormat<<"dd/MM/yyyy";
    listDateFormat<<"MMM dd,yy";
    listDateFormat<<"MMM dd,yyyy";
    listDateFormat<<"dd.MMM.yyyy";
    listDateFormat<<"MMMM dd, yyyy";
    listDateFormat<<"ddd, MMM dd,yy";
    listDateFormat<<"dddd, MMM dd,yy";
    listDateFormat<<"MM-dd";
    listDateFormat<<"yyyy-MM-dd";
    listDateFormat<<"dd/yy";
    listDateFormat<<"MMMM";
    listDateFormat<<"yyyy-MM-dd hh:mm";
    listDateFormat<<"dd.MMM.yyyy hh:mm";
    listDateFormat<<"MMM dd,yyyy h:mm AP";
    listDateFormat<<"yyyy-MM-ddThh:mm:ss"; // ISO 8601
    return listDateFormat;
}

////

KoVariableTimeFormat::KoVariableTimeFormat() : KoVariableFormat()
{
}

void KoVariableTimeFormat::load( const TQCString &key )
{
    TQCString params( key.mid( 4 ) );
    if ( !params.isEmpty() )
	m_strFormat = TQString::fromUtf8(params);
}

TQString KoVariableTimeFormat::convert( const TQVariant & time ) const
{
    if ( time.type() != TQVariant::Time )
    {
        kdDebug(32500)<<" Error in KoVariableTimeFormat::convert. Value is a "
                      << time.typeName() << "(" << time.type() << ")" << endl;
        return TQString();
    }

    if( m_strFormat.lower() == "locale" || m_strFormat.isEmpty() )
	return KGlobal::locale()->formatTime( time.toTime() );
    return time.toTime().toString(m_strFormat);
}

TQCString KoVariableTimeFormat::key() const
{
    return getKey( m_strFormat );
}

TQCString KoVariableTimeFormat::getKey( const TQString& props ) const
{
    return TQCString("TIME") + props.utf8();
}

// Used by KoVariableFormatCollection::popupActionList(), to apply all formats
// to the current data, in the popup menu.
TQStringList KoVariableTimeFormat::staticFormatPropsList()
{
    TQStringList listTimeFormat;
    listTimeFormat<<"locale";
    listTimeFormat<<"hh:mm";
    listTimeFormat<<"hh:mm:ss";
    listTimeFormat<<"hh:mm AP";
    listTimeFormat<<"hh:mm:ss AP";
    listTimeFormat<<"mm:ss.zzz";
    return listTimeFormat;
}

// Used by timeformatwidget_impl
TQStringList KoVariableTimeFormat::staticTranslatedFormatPropsList()
{
    TQStringList listTimeFormat;
    listTimeFormat<<i18n("Locale format");
    listTimeFormat<<"hh:mm";
    listTimeFormat<<"hh:mm:ss";
    listTimeFormat<<"hh:mm AP";
    listTimeFormat<<"hh:mm:ss AP";
    listTimeFormat<<"mm:ss.zzz";
    return listTimeFormat;
}

////

TQString KoVariableStringFormat::convert( const TQVariant & string ) const
{
    if ( string.type() != TQVariant::String )
    {
        kdDebug(32500)<<" Error in KoVariableStringFormat::convert. Value is a " << string.typeName() << endl;
        return TQString();
    }

    return string.toString();
}

TQCString KoVariableStringFormat::key() const
{
    return getKey( TQString() );
    // TODO prefix & suffix
}

TQCString KoVariableStringFormat::getKey( const TQString& props ) const
{
    return TQCString("STRING") + props.utf8();
}

////

TQString KoVariableNumberFormat::convert( const TQVariant &value ) const
{
    if ( value.type() != TQVariant::Int )
    {
        kdDebug(32500)<<" Error in KoVariableNumberFormat::convert. Value is a " << value.typeName() << endl;
        return TQString();
    }

    return TQString::number( value.toInt() );
}

TQCString KoVariableNumberFormat::key() const
{
    return getKey(TQString());
}

TQCString KoVariableNumberFormat::getKey( const TQString& props ) const
{
    return TQCString("NUMB") + props.utf8();
}

////

KoVariableFormatCollection::KoVariableFormatCollection()
{
    m_dict.setAutoDelete( true );
}

KoVariableFormat * KoVariableFormatCollection::format( const TQCString &key )
{
    KoVariableFormat *f = m_dict[ key.data() ];
    if (f)
        return f;
    else
        return createFormat( key );
}

KoVariableFormat * KoVariableFormatCollection::createFormat( const TQCString &key )
{
    kdDebug(32500) << "KoVariableFormatCollection: creating format for key=" << key << endl;
    KoVariableFormat * format = 0L;
    // The first 4 chars identify the class
    TQCString type = key.left(4);
    if ( type == "DATE" )
        format = new KoVariableDateFormat();
    else if ( type == "TIME" )
        format = new KoVariableTimeFormat();
    else if ( type == "NUMB" ) // this type of programming makes me numb ;)
        format = new KoVariableNumberFormat();
    else if ( type == "STRI" )
        format = new KoVariableStringFormat();

    if ( format )
    {
        format->load( key );
        m_dict.insert( format->key() /* not 'key', it could be incomplete */, format );
    }
    return format;
}

/******************************************************************/
/* Class:       KoVariableCollection                              */
/******************************************************************/
KoVariableCollection::KoVariableCollection(KoVariableSettings *_settings, KoVariableFormatCollection *formatCollection)
{
    m_variableSettings = _settings;
    m_varSelected = 0L;
    m_formatCollection = formatCollection;
}

KoVariableCollection::~KoVariableCollection()
{
    delete m_variableSettings;
}

void KoVariableCollection::clear()
{
    variables.clear();
    varValues.clear();
    m_varSelected = 0;
}

void KoVariableCollection::registerVariable( KoVariable *var )
{
    if ( !var )
        return;
    variables.append( var );
}

void KoVariableCollection::unregisterVariable( KoVariable *var )
{
    variables.take( variables.findRef( var ) );
}

TQValueList<KoVariable *> KoVariableCollection::recalcVariables(int type)
{
    TQValueList<KoVariable *> modifiedVariables;
    TQPtrListIterator<KoVariable> it( variables );
    for ( ; it.current() ; ++it )
    {
        KoVariable* variable = it.current();
        if ( variable->isDeleted() )
            continue;
        if ( variable->type() == type || type == VT_ALL )
        {
            TQVariant oldValue = variable->varValue();
            variable->recalc();
            if(variable->height == 0)
                variable->resize();
            if ( variable->varValue() != oldValue )
                modifiedVariables.append( variable );
            KoTextParag * parag = variable->paragraph();
            if ( parag )
            {
                //kdDebug(32500) << "KoDoc::recalcVariables -> invalidating parag " << parag->paragId() << endl;
                parag->tqinvalidate( 0 );
                parag->setChanged( true );
            }
        }
    }
#if 0
    // TODO pass list of textdocuments as argument
    // Or even better, call emitRepaintChanged on all modified textobjects
    if( !modifiedVariables.isEmpty() )
        emit repaintVariable();
#endif
    return modifiedVariables;
}


void KoVariableCollection::setVariableValue( const TQString &name, const TQString &value )
{
    varValues[ name ] = value;
}

TQString KoVariableCollection::getVariableValue( const TQString &name ) const
{
    if ( !varValues.contains( name ) )
        return i18n( "No value" );
    return varValues[ name ];
}

bool KoVariableCollection::customVariableExist(const TQString &varname) const
{
    return varValues.contains( varname );
}

void KoVariableCollection::setVariableSelected(KoVariable * var)
{
    m_varSelected=var;
}

// TODO change to TQValueList<KAction *>, but only once plugActionList takes that
TQPtrList<KAction> KoVariableCollection::popupActionList() const
{
    TQPtrList<KAction> listAction;
    // Insert list of actions that change the subtype
    const TQStringList subTypeList = m_varSelected->subTypeList();
    kdDebug() << k_funcinfo << "current subtype=" << m_varSelected->subType() << endl;
    TQStringList::ConstIterator it = subTypeList.begin();
    for ( int i = 0; it != subTypeList.end() ; ++it, ++i )
    {
        if ( !(*it).isEmpty() ) // in case of removed subtypes or placeholders
        {
            // We store the subtype number as the action name
            TQCString name; name.setNum(i);
            KToggleAction * act = new KToggleAction( *it, KShortcut(), 0, name );
            connect( act, TQT_SIGNAL(activated()), this, TQT_SLOT(slotChangeSubType()) );
            if ( i == m_varSelected->subType() )
                act->setChecked( true );
            //m_subTextMap.insert( act, i );
            listAction.append( act );
        }
    }
    // Insert list of actions that change the format properties
    KoVariableFormat* format = m_varSelected->variableFormat();
    TQString currentFormat = format->formatProperties();

    const TQStringList list = format->formatPropsList();
    it = list.begin();
    for ( int i = 0; it != list.end() ; ++it, ++i )
    {
        if( i == 0 ) // first item, and list not empty
            listAction.append( new KActionSeparator() );

        if ( !(*it).isEmpty() ) // in case of removed subtypes or placeholders
        {
            format->setFormatProperties( *it ); // temporary change
            TQString text = format->convert( m_varSelected->varValue() );
            // We store the raw format as the action name
            KToggleAction * act = new KToggleAction(text, KShortcut(), 0, (*it).utf8());
            connect( act, TQT_SIGNAL(activated()), this, TQT_SLOT(slotChangeFormat()) );
            if ( (*it) == currentFormat )
                act->setChecked( true );
            listAction.append( act );
        }
    }

    // Restore current format
    format->setFormatProperties( currentFormat );
    return listAction;
}

void KoVariableCollection::slotChangeSubType()
{
    KAction * act = (KAction *)(sender());
    int menuNumber = TQCString(act->name()).toInt();
    int newSubType = m_varSelected->variableSubType(menuNumber);
    kdDebug(32500) << "slotChangeSubType: menuNumber=" << menuNumber << " newSubType=" << newSubType << endl;
    if ( m_varSelected->subType() != newSubType )
    {
        KoChangeVariableSubType *cmd=new KoChangeVariableSubType(
            m_varSelected->subType(), newSubType, m_varSelected );
        cmd->execute();
        m_varSelected->textDocument()->emitNewCommand(cmd);
    }
}

void KoVariableCollection::slotChangeFormat()
{
    KAction * act = (KAction *)(sender());
    TQString newFormat = TQString::fromUtf8(act->name());
    TQString oldFormat = m_varSelected->variableFormat()->formatProperties();
    if (oldFormat != newFormat )
    {
        KCommand *cmd=new KoChangeVariableFormatProperties(
            oldFormat, newFormat, m_varSelected );
        cmd->execute();
        m_varSelected->textDocument()->emitNewCommand(cmd);
    }
}

KoVariable * KoVariableCollection::createVariable( int type, short int subtype, KoVariableFormatCollection * coll, KoVariableFormat *varFormat,KoTextDocument *textdoc, KoDocument * doc, int _correct, bool _forceDefaultFormat, bool /*loadFootNote*/ )
{
    Q_ASSERT( coll == m_formatCollection ); // why do we need a parameter ?!?
    TQCString string;
    TQStringList stringList;
    if ( varFormat == 0L )
    {
        // Get the default format for this variable (this method is only called in the interactive case, not when loading)
        switch ( type ) {
        case VT_DATE:
        case VT_DATE_VAR_KWORD10:  // compatibility with kword 1.0
        {
            if ( _forceDefaultFormat )
                varFormat = coll->format( KoDateVariable::defaultFormat() );
            else
            {
                TQCString result = KoDateVariable::formatStr(_correct);
                if ( result.isNull() )//we cancel insert variable
                    return 0L;
                varFormat = coll->format( result );
            }
            break;
        }
        case VT_TIME:
        case VT_TIME_VAR_KWORD10:  // compatibility with kword 1.0
        {
            if ( _forceDefaultFormat )
                varFormat = coll->format( KoTimeVariable::defaultFormat() );
            else
            {
                TQCString result = KoTimeVariable::formatStr(_correct);
                if ( result.isNull() )//we cancel insert variable
                    return 0L;
                varFormat = coll->format( result );
            }
            break;
        }
        case VT_PGNUM:
            varFormat = coll->format( "NUMBER" );
            break;
        case VT_FIELD:
        case VT_CUSTOM:
        case VT_MAILMERGE:
        case VT_LINK:
        case VT_NOTE:
            varFormat = coll->format( "STRING" );
            break;
        case VT_FOOTNOTE: // this is a KWord-specific variable
            kdError() << "Footnote type not handled in KoVariableCollection: VT_FOOTNOTE" << endl;
            return 0L;
        case VT_STATISTIC:
            kdError() << " Statistic type not handled in KoVariableCollection: VT_STATISTIC" << endl;
            return 0L;
        }
    }
    Q_ASSERT( varFormat );
    if ( varFormat == 0L ) // still 0 ? Impossible!
        return 0L ;

    kdDebug(32500) << "Creating variable. Format=" << varFormat->key() << " type=" << type << endl;
    KoVariable * var = 0L;
    switch ( type ) {
        case VT_DATE:
        case VT_DATE_VAR_KWORD10:  // compatibility with kword 1.0
            var = new KoDateVariable( textdoc, subtype, varFormat, this, _correct );
            break;
        case VT_TIME:
        case VT_TIME_VAR_KWORD10:  // compatibility with kword 1.0
            var = new KoTimeVariable( textdoc, subtype, varFormat, this, _correct );
            break;
        case VT_PGNUM:
            kdError() << "VT_PGNUM must be handled by the application's reimplementation of KoVariableCollection::createVariable" << endl;
            //var = new KoPageVariable( textdoc, subtype, varFormat, this );
            break;
        case VT_FIELD:
            var = new KoFieldVariable( textdoc, subtype, varFormat,this,doc );
            break;
        case VT_CUSTOM:
            var = new KoCustomVariable( textdoc, TQString(), varFormat, this);
            break;
        case VT_MAILMERGE:
            var = new KoMailMergeVariable( textdoc, TQString(), varFormat ,this);
            break;
        case VT_LINK:
            var = new KoLinkVariable( textdoc,TQString(), TQString(), varFormat ,this);
            break;
        case VT_NOTE:
            var = new KoNoteVariable( textdoc, TQString(), varFormat ,this);
            break;
    }
    Q_ASSERT( var );
    return var;
}


KoVariable* KoVariableCollection::loadOasisField( KoTextDocument* textdoc, const TQDomElement& tag, KoOasisContext& context )
{
    const TQString localName( tag.localName() );
    const bool isTextNS = tag.namespaceURI() == KoXmlNS::text;
    TQString key;
    int type = -1;
    if ( isTextNS )
    {
        if ( localName.endsWith( "date" ) || localName.endsWith( "time" ) )
        {
            TQString dataStyleName = tag.attributeNS( KoXmlNS::style, "data-style-name", TQString() );
            TQString dateFormat = "locale";
            const KoOasisStyles::DataFormatsMap& map = context.oasisStyles().dataFormats();
            KoOasisStyles::DataFormatsMap::const_iterator it = map.find( dataStyleName );
            if ( it != map.end() )
                dateFormat = (*it).formatStr;

            // Only text:time is a pure time (the data behind is only h/m/s)
            // ### FIXME: not true, a time can have a date too (reason: for MS Word (already from long ago) time and date are the same thing. But for OO the correction is not in the same unit for time and date.)
            // Whereas print-time/creation-time etc. are actually related to a date/time value.
            if ( localName == "time" )
            {
                type = VT_TIME;
                key = "TIME" + dateFormat;
            }
            else
            {
                type = VT_DATE;
                key = "DATE" + dateFormat;
            }
        }
        else if (localName == "page-number" || localName == "page-count" )
        {
            type = VT_PGNUM;
            key = "NUMBER";
        }
        else if (localName == "chapter")
        {
            type = VT_PGNUM;
            key = "STRING";
        }
        else if (localName == "file-name")
        {
            type = VT_FIELD;
            key = "STRING";
        }
        else if (localName == "author-name"
                 || localName == "author-initials"
                 || localName == "subject"
                 || localName == "title"
                 || localName == "description"
                 || localName == "keywords")
        {
            type = VT_FIELD;
            key = "STRING";
        }
        else if ( localName.startsWith( "sender-" )
                  && localName != "sender-firstname" // not supported
                  && localName != "sender-lastname" // not supported
                  && localName != "sender-initials" // not supported
            )
        {
            type = VT_FIELD;
            key = "STRING";
        }
        else if ( localName == "variable-set"
                  || localName == "user-defined"
                  || localName == "user-field-get" )
        {
            key = "STRING";
            type = VT_CUSTOM;
        }
        else
            return 0L;
    }
    else if ( tag.namespaceURI() == KoXmlNS::office && localName == "annotation" )
    {
        type = VT_NOTE;
        key = "NUMBER";
    }
    else
    {
        // Not an error. It's simply not a variable tag (the caller doesn't check for that)
        return 0;
    }
// TODO localName == "page-variable-get", "initial-creator" and many more
// TODO VT_MAILMERGE

    return loadOasisFieldCreateVariable( textdoc, tag, context, key, type );
}

KoVariable* KoVariableCollection::loadOasisFieldCreateVariable( KoTextDocument* textdoc, const TQDomElement& tag, KoOasisContext& context, const TQString &key, int type )
{
    KoVariableFormat * varFormat = key.isEmpty() ? 0 : m_formatCollection->format( key.latin1() );
    // If varFormat is 0 (no key specified), the default format will be used.

    KoVariable* var = createVariable( type, -1, m_formatCollection, varFormat, textdoc, context.koDocument(), 0 /*correct*/, true );
    var->loadOasis( tag, context );
    return var;
}

/******************************************************************/
/* Class: KoVariable                                              */
/******************************************************************/
KoVariable::KoVariable( KoTextDocument *textdoc, KoVariableFormat *varFormat, KoVariableCollection *_varColl)
    : KoTextCustomItem( textdoc )
{
    //d = new Private;
    m_varColl=_varColl;
    m_varFormat = varFormat;
    m_varColl->registerVariable( this );
    m_ascent = 0;
}

KoVariable::~KoVariable()
{
    //kdDebug(32500) << "KoVariable::~KoVariable " << this << endl;
    m_varColl->unregisterVariable( this );
    //delete d;
}

TQStringList KoVariable::subTypeList()
{
    return TQStringList();
}

void KoVariable::resize()
{
    if ( m_deleted )
        return;
    KoTextFormat *fmt = format();
    TQFontMetrics fm = fmt->refFontMetrics();
    TQString txt = text();

    width = 0;
     // size at 100%
    for ( int i = 0 ; i < (int)txt.length() ; ++i )
        width += fm.width( txt[i] ); // was fm.charWidth(txt,i), but see drawCustomItemHelper...
    // zoom to LU
    width = tqRound( KoTextZoomHandler::ptToLayoutUnitPt( width ) );
    height = fmt->height();
    m_ascent = fmt->ascent();
    //kdDebug(32500) << "KoVariable::resize text=" << txt << " width=" << width << " height=" << height << " ascent=" << m_ascent << endl;
}

void KoVariable::recalcAndRepaint()
{
    recalc();
    KoTextParag * parag = paragraph();
    if ( parag )
    {
        //kdDebug(32500) << "KoVariable::recalcAndRepaint -> invalidating parag " << parag->paragId() << endl;
        parag->tqinvalidate( 0 );
        parag->setChanged( true );
    }
    textDocument()->emitRepaintChanged();
}

TQString KoVariable::fieldCode()
{
    return i18n("Variable");
}

TQString KoVariable::text(bool realValue)
{
    KoTextFormat *fmt = format();
    TQString str;
    if (m_varColl->variableSetting()->displayFieldCode()&&!realValue)
        str = fieldCode();
    else
        str = m_varFormat->convert( m_varValue );

    return fmt->displayedString( str);
}

void KoVariable::drawCustomItem( TQPainter* p, int x, int y, int wpix, int hpix, int ascentpix, int /*cx*/, int /*cy*/, int /*cw*/, int /*ch*/, const TQColorGroup& cg, bool selected, int offset, bool drawingShadow )
{
    KoTextFormat * fmt = format();
    KoTextZoomHandler * zh = textDocument()->paintingZoomHandler();
    TQFont font( fmt->screenFont( zh ) );
    drawCustomItemHelper( p, x, y, wpix, hpix, ascentpix, cg, selected, offset, fmt, font, fmt->color(), drawingShadow );
}

void KoVariable::drawCustomItemHelper( TQPainter* p, int x, int y, int wpix, int hpix, int ascentpix, const TQColorGroup& cg, bool selected, int offset, KoTextFormat* fmt, const TQFont& font, TQColor textColor, bool drawingShadow )
{
    // Important: the y value already includes the difference between the parag baseline
    // and the char's own baseline (ascent) (see paintDefault in korichtext.cpp)
    // So we just draw the text there. But we need the baseline for drawFontEffects...
    KoTextZoomHandler * zh = textDocument()->paintingZoomHandler();

    p->save();

    if ( fmt->textBackgroundColor().isValid() )
        p->fillRect( x, y, wpix, hpix, fmt->textBackgroundColor() );

    if ( drawingShadow ) // Use shadow color if drawing a shadow
    {
        textColor = fmt->shadowColor();
        p->setPen( textColor );
    }
    else if ( selected )
    {
        textColor = cg.color( TQColorGroup::HighlightedText );
        p->setPen( TQPen( textColor ) );
        p->fillRect( x, y, wpix, hpix, cg.color( TQColorGroup::Highlight ) );
    }
    else if ( textDocument() && textDocument()->drawFormattingChars()
              && p->device()->devType() != TQInternal::Printer )
    {
        textColor = cg.color( TQColorGroup::Highlight );
        p->setPen( TQPen ( textColor, 0, TQt::DotLine ) );
        p->drawRect( x, y, wpix, hpix );
    }
    else {
        if ( !textColor.isValid() ) // Resolve the color at this point
            textColor = KoTextFormat::defaultTextColor( p );
        p->setPen( TQPen( textColor ) );
    }

    p->setFont( font ); // already done by KoTextCustomItem::draw but someone might
                        // change the font passed to drawCustomItemHelper (e.g. KoLinkVariable)
    TQString str = text();
    KoTextParag::drawFontEffects( p, fmt, zh, font, textColor, x, ascentpix, wpix, y, hpix, str[0] );
    int posY = y + ascentpix + offset;
    if ( fmt->vAlign() == KoTextFormat::AlignSubScript )
        posY +=p->fontMetrics().height() / 6;
    if ( fmt->vAlign() != KoTextFormat::AlignSuperScript )
        posY -= fmt->offsetFromBaseLine();
    else if ( fmt->offsetFromBaseLine() < 0 )
        posY -= 2*fmt->offsetFromBaseLine();

    //p->drawText( x, posY, str );
    // We can't just drawText, it wouldn't use the same kerning as the one
    // that resize() planned for [which is zoom-independent].
    // We need to do the tqlayout using tqlayout units instead, so for simplicity
    // I just draw every char individually (whereas KoTextFormatter/KoTextParag
    // detect runs of text that can be drawn together)
    const int len = str.length();
    int xLU = zh->pixelToLayoutUnitX( x );
    TQFontMetrics fm = fmt->refFontMetrics();
    for ( int i = 0; i < len; ++i )
    {
        const TQChar ch = str[i];
        p->drawText( x, posY, TQString(ch) );
        // Do like KoTextFormatter: do the tqlayout in tqlayout units.
        xLU += KoTextZoomHandler::ptToLayoutUnitPt( fm.width( ch ) );
        // And then compute the X position in pixels from the tqlayout unit X.
        x = zh->layoutUnitToPixelX( xLU );
    }

    p->restore();
}

void KoVariable::save( TQDomElement &parentElem )
{
    //kdDebug(32500) << "KoVariable::save" << endl;
    TQDomElement variableElem = parentElem.ownerDocument().createElement( "VARIABLE" );
    parentElem.appendChild( variableElem );
    TQDomElement typeElem = parentElem.ownerDocument().createElement( "TYPE" );
    variableElem.appendChild( typeElem );
    typeElem.setAttribute( "type", static_cast<int>( type() ) );

    //// Of course, saving the key is ugly. We'll drop this when
    //// switching to the OO format.
    typeElem.setAttribute( "key", m_varFormat->key().data() );
    typeElem.setAttribute( "text", text(true) );
    if ( correctValue() != 0)
        typeElem.setAttribute( "correct", correctValue() );
    saveVariable( variableElem );
}

void KoVariable::load( TQDomElement & )
{
}


void KoVariable::loadOasis( const TQDomElement &/*elem*/, KoOasisContext& /*context*/ )
{
    // nothing to do here, reimplemented in subclasses (make it pure virtual?)
}

void KoVariable::saveOasis( KoXmlWriter& /*writer*/, KoSavingContext& /*context*/ ) const
{
}

void KoVariable::setVariableFormat( KoVariableFormat *_varFormat )
{
    // TODO if ( _varFormat ) _varFormat->deref();
    m_varFormat = _varFormat;
    // TODO m_varFormat->ref();
}

#define addText( text, newFormat ) { \
        if ( !text.isEmpty() ) \
        { \
            newFormat +=text; \
            text=""; \
        } \
}

TQString KoVariable::convertKlocaleToTQDateTimeFormat( const TQString & _format )
{
    TQString newFormat;
    TQString format( _format );
    TQString text;
    do
    {
        if ( format.startsWith( "%Y" ) )
        {
            addText( text, newFormat );
            newFormat+="yyyy";
            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%y" ) )
        {
            addText( text, newFormat );
            newFormat+="yyyy";

            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%n" ) )
        {
            addText( text, newFormat );
            newFormat+="M";
            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%m" ) )
        {
            addText( text, newFormat );
            newFormat+="MM";
            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%e" ) )
        {
            addText( text, newFormat );
            newFormat+="d";
            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%d" ) )
        {
            addText( text, newFormat );
            newFormat+="dd";
            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%b" ) )
        {
            addText( text, newFormat );
            newFormat+="MMM";
            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%B" ) )
        {
            addText( text, newFormat );
            newFormat+="MMMM";
            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%a" ) )
        {
            addText( text, newFormat );
            newFormat+="ddd";

            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%A" ) )
        {
            addText( text, newFormat );
            newFormat+="dddd";
            format = format.remove( 0, 2 );
        }
        if ( format.startsWith( "%H" ) ) //hh
        {
            //hour in 24h
            addText( text, newFormat );
            newFormat+="hh";
            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%k" ) )//h
        {
            addText( text, newFormat );
            newFormat+="h";
            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%I" ) )// ?????
        {
            addText( text, newFormat );
            //TODO hour in 12h
        }
        else if ( format.startsWith( "%l" ) )
        {
            addText( text, newFormat );
            //TODO hour in 12h with 1 digit
        }
        else if ( format.startsWith( "%M" ) )// mm
        {
            addText( text, newFormat );
            newFormat+="mm";
            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%S" ) ) //ss
        {
            addText( text, newFormat );
            newFormat+="ss";
            format = format.remove( 0, 2 );
        }
        else if ( format.startsWith( "%p" ) )
        {
            //TODO am or pm
            addText( text, newFormat );
            newFormat+="ap";
            format = format.remove( 0, 2 );
        }

        else
        {
            text += format[0];
            format = format.remove( 0, 1 );
        }
    }
    while ( format.length() > 0 );
    addText( text, format );
    return format;
}


/******************************************************************/
/* Class: KoDateVariable                                          */
/******************************************************************/
KoDateVariable::KoDateVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *_varFormat, KoVariableCollection *_varColl, int _correctDate)
    : KoVariable( textdoc, _varFormat,_varColl ), m_subtype( subtype ), m_correctDate( _correctDate)
{
}

TQString KoDateVariable::fieldCode()
{
    if ( m_subtype == VST_DATE_FIX )
        return i18n("Date (Fixed)");
    else if ( m_subtype == VST_DATE_CURRENT)
        return i18n("Date");
    else if ( m_subtype == VST_DATE_LAST_PRINTING)
        return i18n("Last Printing");
    else if ( m_subtype == VST_DATE_CREATE_FILE )
        return i18n( "File Creation");
    else if ( m_subtype == VST_DATE_MODIFY_FILE )
        return i18n( "File Modification");
    else
        return i18n("Date");
}

void KoDateVariable::resize()
{
    KoTextFormat * fmt = format();
    TQString oldLanguage;
    if ( !fmt->language().isEmpty())
    {
         oldLanguage=KGlobal::locale()->language();
         bool changeLanguage = KGlobal::locale()->setLanguage( fmt->language() );
         KoVariable::resize();
         if ( changeLanguage )
             KGlobal::locale()->setLanguage( oldLanguage );
    }
    else
        KoVariable::resize();
}

void KoDateVariable::recalc()
{
    if ( m_subtype == VST_DATE_CURRENT )
        m_varValue = TQDateTime(TQDateTime::tqcurrentDateTime().addDays(m_correctDate));
    else if ( m_subtype == VST_DATE_LAST_PRINTING )
        m_varValue = m_varColl->variableSetting()->lastPrintingDate();
    else if ( m_subtype == VST_DATE_CREATE_FILE )
        m_varValue = m_varColl->variableSetting()->creationDate();
    else if ( m_subtype == VST_DATE_MODIFY_FILE )
        m_varValue = m_varColl->variableSetting()->modificationDate();
    else
    {
        // Only if never set before (i.e. upon insertion)
        if ( m_varValue.isNull() )
            m_varValue = TQDateTime(TQDateTime::tqcurrentDateTime().addDays(m_correctDate));
    }
    resize();
}

void KoDateVariable::saveVariable( TQDomElement& varElem )
{
    TQDomElement elem = varElem.ownerDocument().createElement( "DATE" );
    varElem.appendChild( elem );

    TQDate date = m_varValue.toDate(); // works with Date and DateTime
    date = date.addDays( -m_correctDate );//remove correctDate value otherwise value stored is bad
    elem.setAttribute( "year", date.year() );
    elem.setAttribute( "month", date.month() );
    elem.setAttribute( "day", date.day() );
    elem.setAttribute( "fix", m_subtype == VST_DATE_FIX ); // for compat
    elem.setAttribute( "correct", m_correctDate);
    elem.setAttribute( "subtype", m_subtype);
    if ( m_varValue.type() == TQVariant::DateTime )
    {
        TQTime time = m_varValue.toTime();
        elem.setAttribute( "hour", time.hour() );
        elem.setAttribute( "minute", time.minute() );
        elem.setAttribute( "second", time.second() );
    }
}

void KoDateVariable::load( TQDomElement& elem )
{
    KoVariable::load( elem );

    TQDomElement e = elem.namedItem( "DATE" ).toElement();
    if (!e.isNull())
    {
        const bool fix = e.attribute("fix").toInt() == 1;
        if ( e.hasAttribute("correct"))
            m_correctDate = e.attribute("correct").toInt();
        if ( fix )
        {
            const int y = e.attribute("year").toInt();
            const int month = e.attribute("month").toInt();
            const int d = e.attribute("day").toInt();
            const int h = e.attribute("hour").toInt();
            const int min = e.attribute("minute").toInt();
            const int s = e.attribute("second").toInt();
            const int ms = e.attribute("msecond").toInt();
            TQDate date( y, month, d );
            date = date.addDays( m_correctDate );
            const TQTime time( h, min, s, ms );
            if (time.isValid())
                m_varValue = TQVariant ( TQDateTime( date, time ) );
            else
                m_varValue = TQVariant( date );
        }
        //old date variable format
        m_subtype = fix ? VST_DATE_FIX : VST_DATE_CURRENT;
        if ( e.hasAttribute( "subtype" ))
            m_subtype = e.attribute( "subtype").toInt();
    }
}

void KoDateVariable::saveOasis( KoXmlWriter& writer, KoSavingContext& context ) const
{
    switch( m_subtype )
    {
    case VST_DATE_FIX:
    case VST_DATE_CURRENT:
        writer.startElement( "text:date" );
        if ( m_subtype == VST_DATE_FIX )
        {
            writer.addAttribute( "text:date-value", m_varValue.toDate().toString( Qt::ISODate) );
            writer.addAttribute( "text:fixed", "true" );
        }
        break;
    case VST_DATE_LAST_PRINTING:
        writer.startElement( "text:print-date" );
        break;
    case VST_DATE_CREATE_FILE:
        writer.startElement( "text:creation-date" );
        break;
    case VST_DATE_MODIFY_FILE:
        writer.startElement( "text:modification-date" );
        break;
    }
    TQString value(  m_varFormat->formatProperties() );
    bool klocaleFormat = false;
    if ( value.lower() == "locale" ||
         value.isEmpty() ||
         value.lower() == "localeshort" ||
         value.lower() == "localedatetime" ||
         value.lower() == "localedatetimeshort" )
    {
        if ( value.lower() == "locale" || value.isEmpty())
            value =  KGlobal::locale()->dateFormat();
        else if ( value.lower() == "localeshort" )
            value = KGlobal::locale()->dateFormatShort();
        else if ( value.lower() == "localedatetime" )
            value =  TQString( "%1 %2" ).tqarg( KGlobal::locale()->dateFormat() ).tqarg( KGlobal::locale()->timeFormat() );
        else if ( value.lower() == "localedatetimeshort" )
            value =  TQString( "%1 %2" ).tqarg( KGlobal::locale()->dateFormatShort() ).tqarg( KGlobal::locale()->timeFormat() );
        klocaleFormat = true;
    }
    writer.addAttribute( "style:data-style-name", KoOasisStyles::saveOasisDateStyle(context.mainStyles(), value, klocaleFormat ) );

    if ( m_correctDate != 0 )
        writer.addAttribute( "text:date-adjust", daysToISODuration( m_correctDate ) );
    writer.endElement();
}

void KoDateVariable::loadOasis( const TQDomElement &elem, KoOasisContext& /*context*/ )
{
    const TQString localName( elem.localName() );
    if ( localName == "date" ) // current (or fixed) date
    {
        // Standard form of the date is in text:date-value. Example: 2004-01-21T10:57:05
        const TQString dateValue = elem.attributeNS( KoXmlNS::text, "date-value", TQString());
        TQDateTime dt;
        if ( !dateValue.isEmpty() ) // avoid TQDate warning
            dt = TQDate::fromString(dateValue, Qt::ISODate);

        bool fixed = (elem.hasAttributeNS( KoXmlNS::text, "fixed") && elem.attributeNS( KoXmlNS::text, "fixed", TQString())=="true");
        if (!dt.isValid())
            fixed = false; // OOo docs say so: not valid = current datetime
        if ( fixed )
            m_varValue = TQVariant( dt );
        m_subtype = fixed ? VST_DATE_FIX : VST_DATE_CURRENT;
    }
    // For all those the value of the date will be retrieved from meta.xml
    else if ( localName.startsWith( "print" ) )
        m_subtype = VST_DATE_LAST_PRINTING;
    else if ( localName.startsWith( "creation" ) )
        m_subtype = VST_DATE_CREATE_FILE;
    else if ( localName.startsWith( "modification" ) )
        m_subtype = VST_DATE_MODIFY_FILE;
    const TQString adjustStr = elem.attributeNS( KoXmlNS::text, "date-adjust", TQString() );
    if ( !adjustStr.isEmpty() )
        m_correctDate = ISODurationToDays( adjustStr );
}

TQStringList KoDateVariable::actionTexts()
{
    TQStringList lst;
    lst << i18n( "Current Date (fixed)" );
    lst << i18n( "Current Date (variable)" );
    lst << i18n( "Date of Last Printing" );
    lst << i18n( "Date of File Creation" );
    lst << i18n( "Date of File Modification" );
    return lst;
}

TQStringList KoDateVariable::subTypeList()
{
    return KoDateVariable::actionTexts();
}

TQCString KoDateVariable::defaultFormat()
{
    return TQCString("DATE") + "locale";
}

TQCString KoDateVariable::formatStr(int & correct)
{
    TQCString string;
    TQStringList stringList;
    KDialogBase* dialog=new KDialogBase(0, 0, true, i18n("Date Format"), KDialogBase::Ok|KDialogBase::Cancel);
    DateFormatWidget* widget=new DateFormatWidget(dialog);
    int count=0;
    dialog->setMainWidget(widget);
    KConfig* config = KoGlobal::kofficeConfig();
    if( config->hasGroup("Date format history") )
    {
        KConfigGroupSaver cgs( config, "Date format history");
        const int noe=config->readNumEntry("Number Of Entries", 5);
        for(int i=0;i<noe;i++)
        {
            TQString num;
            num.setNum(i);
            const TQString tmpString(config->readEntry("Last Used"+num));
            if(tmpString.startsWith("locale"))
                continue;
            else if(stringList.contains(tmpString))
                continue;
            else if(!tmpString.isEmpty())
            {
                stringList.append(tmpString);
                count++;
            }
        }

    }
    if(!stringList.isEmpty())
    {
        widget->combo1->insertItem("---");
        widget->combo1->insertStringList(stringList);
    }
    if(false) { // ### TODO: select the last used item
        TQComboBox *combo= widget->combo1;
        combo->setCurrentItem(combo->count() -1);
        widget->updateLabel();
    }

    if(dialog->exec()==TQDialog::Accepted)
    {
        string = widget->resultString().utf8();
        correct = widget->correctValue();
    }
    else
    {
        delete dialog;
        return 0;
    }
    config->setGroup("Date format history");
    stringList.remove(string);
    stringList.prepend(string);
    for(int i=0;i<=count;i++)
    {
        TQString num;
        num.setNum(i);
        config->writeEntry("Last Used"+num, stringList[i]);
    }
    config->sync();
    delete dialog;
    return TQCString("DATE") + string;
}

/******************************************************************/
/* Class: KoTimeVariable                                          */
/******************************************************************/
KoTimeVariable::KoTimeVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *varFormat, KoVariableCollection *_varColl, int _correct)
    : KoVariable( textdoc, varFormat,_varColl ), m_subtype( subtype ), m_correctTime( _correct)
{
}

TQString KoTimeVariable::fieldCode()
{
    return (m_subtype == VST_TIME_FIX)?i18n("Time (Fixed)"):i18n("Time");
}


void KoTimeVariable::resize()
{
    KoTextFormat * fmt = format();
    if ( !fmt->language().isEmpty() )
    {
        TQString oldLanguage = KGlobal::locale()->language();
        bool changeLanguage = KGlobal::locale()->setLanguage( fmt->language() );
        KoVariable::resize();
        if ( changeLanguage )
            KGlobal::locale()->setLanguage( oldLanguage );
    }
    else
        KoVariable::resize();
}

void KoTimeVariable::recalc()
{
    if ( m_subtype == VST_TIME_CURRENT )
        m_varValue = TQVariant( TQTime(TQTime::currentTime().addSecs(60*m_correctTime)));
    else
    {
        // Only if never set before (i.e. upon insertion)
        if ( m_varValue.toTime().isNull() )
            m_varValue = TQVariant( TQTime(TQTime::currentTime().addSecs(60*m_correctTime)));
    }
    resize();
}


void KoTimeVariable::saveVariable( TQDomElement& parentElem )
{
    TQDomElement elem = parentElem.ownerDocument().createElement( "TIME" );
    parentElem.appendChild( elem );

    TQTime time = m_varValue.toTime();
    time = time.addSecs(-60*m_correctTime);
    elem.setAttribute( "hour", time.hour() );
    elem.setAttribute( "minute", time.minute() );
    elem.setAttribute( "second", time.second() );
    elem.setAttribute( "msecond", time.msec() );
    elem.setAttribute( "fix", m_subtype == VST_TIME_FIX );
    elem.setAttribute( "correct", m_correctTime );
}

void KoTimeVariable::load( TQDomElement& elem )
{
    KoVariable::load( elem );

    TQDomElement e = elem.namedItem( "TIME" ).toElement();
    if (!e.isNull())
    {
        int h = e.attribute("hour").toInt();
        int m = e.attribute("minute").toInt();
        int s = e.attribute("second").toInt();
        int ms = e.attribute("msecond").toInt();
        int correct = 0;
        if ( e.hasAttribute("correct"))
            correct=e.attribute("correct").toInt();
        bool fix = static_cast<bool>( e.attribute("fix").toInt() );
        if ( fix )
        {
            TQTime time;
            time.setHMS( h, m, s, ms );
            time = time.addSecs( 60*m_correctTime );
            m_varValue = TQVariant( time);

        }
        m_subtype = fix ? VST_TIME_FIX : VST_TIME_CURRENT;
        m_correctTime = correct;
    }
}

void KoTimeVariable::loadOasis( const TQDomElement &elem, KoOasisContext& /*context*/ )
{
    const TQString localName( elem.localName() );
    Q_ASSERT( localName == "time" ); // caller checked for it
    if ( localName == "time" ) // current (or fixed) time
    {
        // Use TQDateTime to work around a possible problem of TQTime::fromString in TQt 3.2.2
        TQDateTime dt(TQDateTime::fromString(elem.attributeNS( KoXmlNS::text, "time-value", TQString()), Qt::ISODate));

        bool fixed = (elem.hasAttributeNS( KoXmlNS::text, "fixed") && elem.attributeNS( KoXmlNS::text, "fixed", TQString())=="true");
        if (!dt.isValid())
            fixed = false; // OOo docs say so: not valid = current datetime
        if ( fixed )
            m_varValue = TQVariant( dt.time() );
        m_subtype = fixed ? VST_TIME_FIX : VST_TIME_CURRENT;
        TQString adjustStr = elem.attributeNS( KoXmlNS::text, "time-adjust", TQString() );
        if ( !adjustStr.isEmpty() )
            m_correctTime = ISODurationToMinutes( adjustStr );
    }
}

void KoTimeVariable::saveOasis( KoXmlWriter& writer, KoSavingContext& context ) const
{
    writer.startElement( "text:time" );
    if ( m_correctTime != 0 ) {
        writer.addAttribute( "text:time-adjust", minutesToISODuration( m_correctTime ) );
    }
    if (m_subtype == VST_TIME_FIX )
    {
        writer.addAttribute( "text:fixed", "true" );
        writer.addAttribute( "text:time-value", m_varValue.toTime().toString( Qt::ISODate ) );
    }

    TQString value(  m_varFormat->formatProperties() );
    bool klocaleFormat = false;
    if ( value.lower() == "locale" )
    {
        value = KGlobal::locale()->timeFormat();
        klocaleFormat = true;
    }
    writer.addAttribute( "style:data-style-name", KoOasisStyles::saveOasisTimeStyle(context.mainStyles(), m_varFormat->formatProperties(), klocaleFormat ) );
    //writer.addTextNode( /*value*/ value displayed as texte );
    //TODO save text value
    //<text:time style:data-style-name="N43" text:time-value="2004-11-11T14:42:19" text:fixed="true">02:42:19 PM</text:time>
    writer.endElement();
}


TQStringList KoTimeVariable::actionTexts()
{
    TQStringList lst;
    lst << i18n( "Current Time (fixed)" );
    lst << i18n( "Current Time (variable)" );
    return lst;
}

TQStringList KoTimeVariable::subTypeList()
{
    return KoTimeVariable::actionTexts();
}

TQCString KoTimeVariable::formatStr(int & _correct)
{
    TQCString string;
    TQStringList stringList;
    KDialogBase* dialog=new KDialogBase(0, 0, true, i18n("Time Format"), KDialogBase::Ok|KDialogBase::Cancel);
    TimeFormatWidget* widget=new TimeFormatWidget(dialog);
    dialog->setMainWidget(widget);
    KConfig* config = KoGlobal::kofficeConfig();
    int count=0;
    if( config->hasGroup("Time format history") )
    {
        KConfigGroupSaver cgs( config, "Time format history" );
        const int noe=config->readNumEntry("Number Of Entries", 5);
        for(int i=0;i<noe;i++)
        {
            TQString num;
            num.setNum(i);
            TQString tmpString(config->readEntry("Last Used"+num));
            if(tmpString.startsWith("locale"))
                continue;
            else if(stringList.contains(tmpString))
                continue;
            else if(!tmpString.isEmpty())
            {
                stringList.append(tmpString);
                count++;
            }
        }
    }
    if(!stringList.isEmpty())
    {
        widget->combo1->insertItem("---");
        widget->combo1->insertStringList(stringList);
    }
    if(false) // ### TODO: select the last used item
    {
        TQComboBox *combo= widget->combo1;
        combo->setCurrentItem(combo->count() -1);
    }
    if(dialog->exec()==TQDialog::Accepted)
    {
        string = widget->resultString().utf8();
        _correct = widget->correctValue();
    }
    else
    {
        delete dialog;
        return 0;
    }
    config->setGroup("Time format history");
    stringList.remove(string);
    stringList.prepend(string);
    for(int i=0;i<=count;i++)
    {
        TQString num;
        num.setNum(i);
        config->writeEntry("Last Used"+num, stringList[i]);
    }
    config->sync();
    delete dialog;
    return TQCString("TIME"+string );
}

TQCString KoTimeVariable::defaultFormat()
{
    return TQCString(TQCString("TIME")+TQCString("locale") );
}


/******************************************************************/
/* Class: KoCustomVariable                                        */
/******************************************************************/
KoCustomVariable::KoCustomVariable( KoTextDocument *textdoc, const TQString &name, KoVariableFormat *varFormat, KoVariableCollection *_varColl )
    : KoVariable( textdoc, varFormat,_varColl )
{
    m_varValue = TQVariant( name );
}

TQString KoCustomVariable::fieldCode()
{
    return i18n("Custom Variable");
}

TQString KoCustomVariable::text(bool realValue)
{
    if (m_varColl->variableSetting()->displayFieldCode()&&!realValue)
        return fieldCode();
    else
        return value();
} // use a format when they are customizable



void KoCustomVariable::saveVariable( TQDomElement& parentElem )
{
    TQDomElement elem = parentElem.ownerDocument().createElement( "CUSTOM" );
    parentElem.appendChild( elem );
    elem.setAttribute( "name", m_varValue.toString() );
    elem.setAttribute( "value", value() );
}

void KoCustomVariable::load( TQDomElement& elem )
{
    KoVariable::load( elem );
    TQDomElement e = elem.namedItem( "CUSTOM" ).toElement();
    if (!e.isNull())
    {
        m_varValue = TQVariant (e.attribute( "name" ));
        setValue( e.attribute( "value" ) );
    }
}

void KoCustomVariable::loadOasis( const TQDomElement &elem, KoOasisContext& /*context*/ )
{
    const TQString localName( elem.localName() );
    // We treat all those the same. For OO/OpenDocument the difference is that
    // - user-field-get is related to text:user-field-decls in <body>
    // - variable-set is related to variable-decls (defined in <body>);
    //                 its value can change in the middle of the document.
    // - user-defined is related to meta:user-defined in meta.xml
    if ( localName == "variable-set"
         || localName == "user-defined"
        || localName == "user-field-get" ) {
        m_varValue = elem.attributeNS( KoXmlNS::text, "name", TQString() );
        setValue( elem.text() );
    }
}

void KoCustomVariable::saveOasis( KoXmlWriter& writer, KoSavingContext& /*context*/ ) const
{
    //TODO save value into meta:user-defined
    writer.startElement( "text:user-field-get" ); //see 6.3.6
    writer.addAttribute( "text:name", m_varValue.toString() );
    writer.addTextNode( value() );
    writer.endElement();
}

TQString KoCustomVariable::value() const
{
    return m_varColl->getVariableValue( m_varValue.toString() );
}

void KoCustomVariable::setValue( const TQString &v )
{
    m_varColl->setVariableValue( m_varValue.toString(), v );
}

TQStringList KoCustomVariable::actionTexts()
{
    return TQStringList( i18n( "Custom..." ) );
}

void KoCustomVariable::recalc()
{
    resize();
}

/******************************************************************/
/* Class: KoMailMergeVariable                                  */
/******************************************************************/
KoMailMergeVariable::KoMailMergeVariable( KoTextDocument *textdoc, const TQString &name, KoVariableFormat *varFormat,KoVariableCollection *_varColl )
    : KoVariable( textdoc, varFormat, _varColl )
{
    m_varValue = TQVariant ( name );
}

TQString KoMailMergeVariable::fieldCode()
{
    return i18n("Mail Merge");
}

void KoMailMergeVariable::loadOasis( const TQDomElement &/*elem*/, KoOasisContext& /*context*/ )
{
    // TODO
}

void KoMailMergeVariable::saveOasis( KoXmlWriter& /*writer*/, KoSavingContext& /*context*/ ) const
{
        kdWarning(32500) << "Not implemented: OASIS saving of mail merge variables" << endl;
}



void KoMailMergeVariable::saveVariable( TQDomElement& parentElem )
{
    TQDomElement elem = parentElem.ownerDocument().createElement( "MAILMERGE" );
    parentElem.appendChild( elem );
    elem.setAttribute( "name", m_varValue.toString() );
}

void KoMailMergeVariable::load( TQDomElement& elem )
{
    KoVariable::load( elem );
    TQDomElement e = elem.namedItem( "MAILMERGE" ).toElement();
    if (!e.isNull())
        m_varValue = TQVariant( e.attribute( "name" ) );
}

TQString KoMailMergeVariable::value() const
{
    return TQString();//m_doc->getMailMergeDataBase()->getValue( m_name );
}

TQString KoMailMergeVariable::text(bool /*realValue*/)
{
    // ## should use a format maybe
    TQString v = value();
    if ( v == name() )
        return "<" + v + ">";
    return v;
}

TQStringList KoMailMergeVariable::actionTexts()
{
    return TQStringList( i18n( "&Mail Merge..." ) );
}

/******************************************************************/
/* Class: KoPageVariable                                         */
/******************************************************************/
KoPageVariable::KoPageVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *varFormat,KoVariableCollection *_varColl )
        : KoVariable( textdoc, varFormat, _varColl ), m_subtype( subtype )
{
}

TQString KoPageVariable::fieldCode()
{
    if ( m_subtype == VST_PGNUM_CURRENT )
        return i18n("Page Current Num");
    else if ( m_subtype == VST_PGNUM_TOTAL )
        return i18n("Total Page Num");
    else if ( m_subtype == VST_CURRENT_SECTION )
        return i18n("Current Section");
    else if ( m_subtype == VST_PGNUM_PREVIOUS )
        return i18n("Previous Page Number");
    else if ( m_subtype == VST_PGNUM_NEXT )
        return i18n("Next Page Number");

    else
        return i18n("Current Section");
}


void KoPageVariable::saveVariable( TQDomElement& parentElem )
{
    TQDomElement pgNumElem = parentElem.ownerDocument().createElement( "PGNUM" );
    parentElem.appendChild( pgNumElem );
    pgNumElem.setAttribute( "subtype", m_subtype );
    if ( m_subtype != VST_CURRENT_SECTION )
        pgNumElem.setAttribute( "value", m_varValue.toInt() );
    else
        pgNumElem.setAttribute( "value", m_varValue.toString() );
}

void KoPageVariable::load( TQDomElement& elem )
{
    KoVariable::load( elem );
    TQDomElement pgNumElem = elem.namedItem( "PGNUM" ).toElement();
    if (!pgNumElem.isNull())
    {
        m_subtype = pgNumElem.attribute("subtype").toInt();
        // ### This could use the format...
        if ( m_subtype != VST_CURRENT_SECTION )
            m_varValue = TQVariant(pgNumElem.attribute("value").toInt());
        else
            m_varValue = TQVariant(pgNumElem.attribute("value"));
    }
}

void KoPageVariable::saveOasis( KoXmlWriter& writer, KoSavingContext& /*context*/ ) const
{
    switch( m_subtype )
    {
    case VST_PGNUM_PREVIOUS:
    case VST_PGNUM_NEXT:
    case VST_PGNUM_CURRENT:
    {
        writer.startElement( "text:page-number" );
        if ( m_subtype == VST_PGNUM_PREVIOUS )
        {
            writer.addAttribute( "text:select-page", "previous" );
        }
        else if ( m_subtype == VST_PGNUM_NEXT )
        {
            writer.addAttribute( "text:select-page", "next" );
        }
        else if ( m_subtype == VST_PGNUM_CURRENT )
        {
            writer.addAttribute( "text:select-page", "current" );
        }
        writer.addTextNode( m_varValue.toString() );
        writer.endElement();
    }
    break;
    case VST_CURRENT_SECTION:
    {
        writer.startElement( "text:chapter" );
        writer.addTextNode( m_varValue.toString() );
        writer.endElement();
    }
    break;
    case VST_PGNUM_TOTAL:
    {
        writer.startElement( "text:page-count" );
        writer.addTextNode( m_varValue.toString() );
        writer.endElement();
    }
    break;
    }
}

void KoPageVariable::loadOasis( const TQDomElement &elem, KoOasisContext& /*context*/ )
{
    const TQString localName( elem.localName() );
    if ( localName == "page-number" )
    {
        m_subtype = VST_PGNUM_CURRENT;

        if ( elem.hasAttributeNS( KoXmlNS::text, "select-page") )
        {
            const TQString select = elem.attributeNS( KoXmlNS::text, "select-page", TQString());
            if (select == "previous")
                m_subtype = VST_PGNUM_PREVIOUS;
            else if (select == "next")
                m_subtype = VST_PGNUM_NEXT;
        }
        // Missing: fixed, page adjustment, formatting style
        m_varValue = TQVariant( elem.text().toInt() );
    }
    else if ( localName == "chapter" )
    {
        m_subtype = VST_CURRENT_SECTION;
        m_varValue = TQVariant( elem.text() );
        // text:display attribute can be name, number (i.e. with prefix/suffix),
        // number-and-name, plain-number-and-name, plain-number
        // TODO: a special format class for this, so that it can be easily switched using the RMB
    }
    else if ( localName == "page-count" )
    {
        m_subtype = VST_PGNUM_TOTAL;
        m_varValue = TQVariant( elem.text() );
    }
}

TQStringList KoPageVariable::actionTexts()
{
    TQStringList lst;
    lst << i18n( "Page Number" );
    lst << i18n( "Number of Pages" );
    lst << i18n( "Section Title" );
    lst << i18n( "Previous Page" );
    lst << i18n( "Next Page" );
    return lst;
}

TQStringList KoPageVariable::subTypeList()
{
    return KoPageVariable::actionTexts();
}

void KoPageVariable::setVariableSubType( short int type )
{
    m_subtype = type;
    Q_ASSERT( m_varColl );
    KoVariableFormatCollection* fc = m_varColl->formatCollection();
    setVariableFormat((m_subtype == VST_CURRENT_SECTION) ? fc->format("STRING") : fc->format("NUMBER"));
}

/******************************************************************/
/* Class: KoFieldVariable                                         */
/******************************************************************/
KoFieldVariable::KoFieldVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *varFormat, KoVariableCollection *_varColl ,KoDocument *_doc )
    : KoVariable( textdoc, varFormat,_varColl ), m_subtype( subtype ), m_doc(_doc)
{
}

TQString KoFieldVariable::fieldCode()
{
    switch( m_subtype ) {
    case VST_FILENAME:
        return i18n("Filename");
        break;
    case VST_DIRECTORYNAME:
        return i18n("Directory Name");
        break;
    case VST_PATHFILENAME:
        return i18n("Path Filename");
        break;
    case VST_FILENAMEWITHOUTEXTENSION:
        return i18n("Filename Without Extension");
        break;
    case VST_AUTHORNAME:
        return i18n("Author Name");
        break;
    case VST_EMAIL:
        return i18n("Email");
        break;
    case VST_COMPANYNAME:
        return i18n("Company Name");
        break;
    case VST_TELEPHONE_WORK:
        return i18n("Telephone (work)");
        break;
    case VST_TELEPHONE_HOME:
        return i18n("Telephone (home)");
        break;
    case VST_FAX:
        return i18n("Fax");
        break;
    case VST_COUNTRY:
        return i18n("Country");
        break;
    case VST_POSTAL_CODE:
        return i18n("Postal Code");
        break;
    case VST_CITY:
        return i18n("City");
        break;
    case VST_STREET:
        return i18n("Street");
        break;
    case VST_AUTHORTITLE:
        return i18n("Author Title");
        break;
    case VST_TITLE:
        return i18n("Title");
        break;
    case VST_SUBJECT:
        return i18n("Subject");
        break;
    case VST_ABSTRACT:
        return i18n("Abstract");
        break;
    case VST_KEYWORDS:
        return i18n("Keywords");
        break;
    case VST_INITIAL:
        return i18n("Initials");
        break;
    }
    return i18n("Field");
}

TQString KoFieldVariable::text(bool realValue)
{
    if (m_varColl->variableSetting()->displayFieldCode()&&!realValue)
        return fieldCode();
    else
        return value();
} // use a format when they are customizable


void KoFieldVariable::saveVariable( TQDomElement& parentElem )
{
    //kdDebug(32500) << "KoFieldVariable::saveVariable" << endl;
    TQDomElement elem = parentElem.ownerDocument().createElement( "FIELD" );
    parentElem.appendChild( elem );
    elem.setAttribute( "subtype", m_subtype );
    elem.setAttribute( "value", m_varValue.toString() );
}

void KoFieldVariable::load( TQDomElement& elem )
{
    KoVariable::load( elem );
    TQDomElement e = elem.namedItem( "FIELD" ).toElement();
    if (!e.isNull())
    {
        m_subtype = e.attribute( "subtype" ).toInt();
        if ( m_subtype == VST_NONE )
            kdWarning() << "Field subtype of -1 found in the file !" << endl;
        m_varValue = TQVariant( e.attribute( "value" ) );
    } else
        kdWarning() << "FIELD element not found !" << endl;
}

void KoFieldVariable::loadOasis( const TQDomElement &elem, KoOasisContext& /*context*/ )
{
    const TQString localName( elem.localName() );
    if ( localName == "file-name" ) {
        const TQString display = elem.attributeNS( KoXmlNS::text, "display", TQString() );
        if (display == "path")
            m_subtype = VST_DIRECTORYNAME;
        else if (display == "name")
            m_subtype = VST_FILENAMEWITHOUTEXTENSION;
        else if (display == "name-and-extension")
            m_subtype = VST_FILENAME;
        else
            m_subtype = VST_PATHFILENAME;
    }
    else if ( localName == "author-name" )
        m_subtype = VST_AUTHORNAME;
    else if ( localName == "author-initials" )
        m_subtype = VST_INITIAL;
    else if ( localName == "subject" )
        m_subtype = VST_SUBJECT;
    else if ( localName == "title" )
        m_subtype = VST_TITLE;
    else if ( localName == "description" )
        m_subtype = VST_ABSTRACT;
    else if ( localName == "keywords" )
        m_subtype = VST_KEYWORDS;

    else if ( localName == "sender-company" )
        m_subtype = VST_COMPANYNAME;
    else if ( localName == "sender-firstname" )
        ; // ## This is different from author-name, but the notion of 'sender' is unclear...
    else if ( localName == "sender-lastname" )
        ; // ## This is different from author-name, but the notion of 'sender' is unclear...
    else if ( localName == "sender-initials" )
        ; // ## This is different from author-initials, but the notion of 'sender' is unclear...
    else if ( localName == "sender-street" )
        m_subtype = VST_STREET;
    else if ( localName == "sender-country" )
        m_subtype = VST_COUNTRY;
    else if ( localName == "sender-postal-code" )
        m_subtype = VST_POSTAL_CODE;
    else if ( localName == "sender-city" )
        m_subtype = VST_CITY;
    else if ( localName == "sender-title" )
        m_subtype = VST_AUTHORTITLE; // Small hack (it's supposed to be about the sender, not about the author)
    else if ( localName == "sender-position" )
        m_subtype = VST_AUTHORPOSITION;
    else if ( localName == "sender-phone-private" )
        m_subtype = VST_TELEPHONE_HOME;
    else if ( localName == "sender-phone-work" )
        m_subtype = VST_TELEPHONE_WORK;
    else if ( localName == "sender-fax" )
        m_subtype = VST_FAX;
    else if ( localName == "sender-email" )
        m_subtype = VST_EMAIL;

    m_varValue = TQVariant( elem.text() );
}

void KoFieldVariable::saveOasis( KoXmlWriter& writer, KoSavingContext& /*context*/ ) const
{
    switch( m_subtype )
    {
    case VST_NONE:
        break;
    case VST_FILENAME:
        writer.startElement( "text:file-name" );
        writer.addAttribute( "text:display", "name-and-extension" );
        break;
    case VST_DIRECTORYNAME:
        writer.startElement( "text:file-name" );
        writer.addAttribute( "text:display", "path" );
        break;
    case VST_AUTHORNAME:
        writer.startElement( "text:author-name" );
        break;
    case VST_EMAIL:
        writer.startElement("text:sender-email" );
        break;
    case VST_COMPANYNAME:
        writer.startElement("text:sender-company" );
        break;
    case VST_PATHFILENAME:
        writer.startElement("text:display" );
        writer.addAttribute( "text:display", "pathfilename" ); // ???????? not define !
        break;
    case VST_FILENAMEWITHOUTEXTENSION:
        writer.startElement("text:display" );
        writer.addAttribute( "text:display", "name-and-extension" ); // ???????? not define !
        break;
    case VST_TELEPHONE_WORK:
        writer.startElement("text:sender-phone-work" );
        break;
    case VST_TELEPHONE_HOME:
        writer.startElement("text:sender-phone-private" );
        break;
    case VST_FAX:
        writer.startElement("text:sender-fax" );
        break;
    case VST_COUNTRY:
        writer.startElement("text:sender-country" );
        break;
    case VST_TITLE:
        writer.startElement("text:title" );
        break;
    case VST_KEYWORDS:
        writer.startElement("text:keywords" );
        break;
    case VST_SUBJECT:
        writer.startElement("text:subject" );
        break;
    case VST_ABSTRACT:
        writer.startElement("text:description" );
        break;
    case VST_POSTAL_CODE:
        writer.startElement("text:sender-postal-code" );
        break;
    case VST_CITY:
        writer.startElement("text:sender-city" );
        break;
    case VST_STREET:
        writer.startElement("text:sender-street" );
        break;
    case VST_AUTHORTITLE:
        writer.startElement("text:sender-title" );
        break;
    case VST_AUTHORPOSITION:
        writer.startElement("text:sender-position" );
        break;
    case VST_INITIAL:
        writer.startElement("text:author-initials" );
        break;
    }
    writer.addTextNode( m_varValue.toString() );
    writer.endElement();
}

void KoFieldVariable::recalc()
{
    TQString value;
    switch( m_subtype ) {
        case VST_NONE:
            kdWarning() << "KoFieldVariable::recalc() called with m_subtype = VST_NONE !" << endl;
            break;
        case VST_FILENAME:
            value = m_doc->url().fileName();
            break;
        case VST_DIRECTORYNAME:
            value = m_doc->url().directory();
            break;
        case VST_PATHFILENAME:
            value=m_doc->url().path();
            break;
        case VST_FILENAMEWITHOUTEXTENSION:
        {
            TQString file=m_doc->url().fileName();
            int pos=file.findRev(".");
            if(pos !=-1)
                value=file.mid(0,pos);
            else
                value=file;
        }
        break;
        case VST_AUTHORNAME:
        case VST_EMAIL:
        case VST_COMPANYNAME:
        case VST_TELEPHONE_WORK:
        case VST_TELEPHONE_HOME:
        case VST_FAX:
        case VST_COUNTRY:
        case VST_POSTAL_CODE:
        case VST_CITY:
        case VST_STREET:
        case VST_AUTHORTITLE:
    case VST_AUTHORPOSITION:
        case VST_INITIAL:
        {
            KoDocumentInfo * info = m_doc->documentInfo();
            KoDocumentInfoAuthor * authorPage = static_cast<KoDocumentInfoAuthor *>(info->page( "author" ));
            if ( !authorPage )
                kdWarning() << "Author information not found in documentInfo !" << endl;
            else
            {
                if ( m_subtype == VST_AUTHORNAME )
                    value = authorPage->fullName();
                else if ( m_subtype == VST_EMAIL )
                    value = authorPage->email();
                else if ( m_subtype == VST_COMPANYNAME )
                    value = authorPage->company();
                else if ( m_subtype == VST_TELEPHONE_WORK )
                    value = authorPage->telephoneWork();
                else if ( m_subtype == VST_TELEPHONE_HOME )
                    value = authorPage->telephoneHome();
                else if ( m_subtype == VST_FAX )
                    value = authorPage->fax();
                else if ( m_subtype == VST_COUNTRY )
                    value = authorPage->country();
                else if ( m_subtype == VST_POSTAL_CODE )
                    value = authorPage->postalCode();
                else if ( m_subtype == VST_CITY )
                    value = authorPage->city();
                else if ( m_subtype == VST_STREET )
                    value = authorPage->street();
                else if ( m_subtype == VST_AUTHORTITLE )
                    value = authorPage->title();
                else if ( m_subtype == VST_INITIAL )
                    value = authorPage->initial();
                else if ( m_subtype == VST_AUTHORPOSITION )
                    value = authorPage->position();
            }
        }
        break;
        case VST_TITLE:
        case VST_ABSTRACT:
    case VST_SUBJECT:
    case VST_KEYWORDS:
        {
            KoDocumentInfo * info = m_doc->documentInfo();
            KoDocumentInfoAbout * aboutPage = static_cast<KoDocumentInfoAbout *>(info->page( "about" ));
            if ( !aboutPage )
                kdWarning() << "'About' page not found in documentInfo !" << endl;
            else
            {
                if ( m_subtype == VST_TITLE )
                    value = aboutPage->title();
                else if ( m_subtype == VST_SUBJECT )
                    value = aboutPage->subject();
                else if ( m_subtype == VST_KEYWORDS )
                    value = aboutPage->keywords();
                else
                    value = aboutPage->abstract();
            }
        }
        break;
    }

    if (value.isEmpty())        // try the initial value
        value = m_varValue.toString();

    if (value.isEmpty())        // still empty? give up
        value = i18n("<None>");

    m_varValue = TQVariant( value );

    resize();
}

TQStringList KoFieldVariable::actionTexts()
{
    // NOTE: if you change here, also change fieldSubType()
    TQStringList lst;
    lst << i18n( "Author Name" );
    lst << i18n( "Title" );
    lst << i18n( "Initials" );
    lst << i18n( "Position" );
    lst << i18n( "Company" );
    lst << i18n( "Email" );
    lst << i18n( "Telephone (work)");
    lst << i18n( "Telephone (private)");

    lst << i18n( "Fax");
    lst << i18n( "Street" );
    lst << i18n( "Postal Code" );
    lst << i18n( "City" );
    lst << i18n( "Country");

    lst << i18n( "Document Title" );
    lst << i18n( "Document Abstract" );
    lst << i18n( "Document Subject" );
    lst << i18n( "Document Keywords" );

    lst << i18n( "File Name" );
    lst << i18n( "File Name without Extension" );
    lst << i18n( "Directory Name" ); // is "Name" necessary ?
    lst << i18n( "Directory && File Name" );
    return lst;
}

short int KoFieldVariable::variableSubType( short int menuNumber )
{
    return fieldSubType(menuNumber);
}

KoFieldVariable::FieldSubType KoFieldVariable::fieldSubType(short int menuNumber)
{
    // NOTE: if you change here, also change actionTexts()
    FieldSubType v;
    switch (menuNumber)
    {
        case 0: v = VST_AUTHORNAME;
                break;
        case 1: v = VST_AUTHORTITLE;
                break;
        case 2: v = VST_INITIAL;
                break;
        case 3: v = VST_AUTHORPOSITION;
                break;
        case 4: v = VST_COMPANYNAME;
                break;
        case 5: v = VST_EMAIL;
                break;
        case 6: v = VST_TELEPHONE_WORK;
                break;
        case 7: v = VST_TELEPHONE_HOME;
                break;
        case 8: v = VST_FAX;
                break;
        case 9: v = VST_STREET;
                break;
        case 10: v = VST_POSTAL_CODE;
                break;
        case 11: v = VST_CITY;
                break;
        case 12: v = VST_COUNTRY;
                break;
        case 13: v = VST_TITLE;
                break;
        case 14: v = VST_ABSTRACT;
                break;
        case 15: v = VST_SUBJECT;
                break;
        case 16: v = VST_KEYWORDS;
                break;
        case 17: v = VST_FILENAME;
                break;
        case 18: v = VST_FILENAMEWITHOUTEXTENSION;
                break;
        case 19: v = VST_DIRECTORYNAME;
                break;
        case 20: v = VST_PATHFILENAME;
                break;
        default:
            v = VST_NONE;
            break;
    }
    return v;
}

TQStringList KoFieldVariable::subTypeList()
{
    return KoFieldVariable::actionTexts();
}

/******************************************************************/
/* Class: KoLinkVariable                                          */
/******************************************************************/
KoLinkVariable::KoLinkVariable( KoTextDocument *textdoc, const TQString & _linkName, const TQString & _ulr,KoVariableFormat *varFormat,KoVariableCollection *_varColl )
    : KoVariable( textdoc, varFormat,_varColl )
    ,m_url(_ulr)
{
    m_varValue = TQVariant( _linkName );
}

TQString KoLinkVariable::fieldCode()
{
    return i18n("Link");
}

void KoLinkVariable::loadOasis( const TQDomElement &elem, KoOasisContext& /*context*/ )
{
    if ( elem.localName() == "a" && elem.namespaceURI() == KoXmlNS::text ) {
        m_url = elem.attributeNS( KoXmlNS::xlink, "href", TQString());
        m_varValue = TQVariant(elem.text());
    }
}

void KoLinkVariable::saveOasis( KoXmlWriter& writer, KoSavingContext& /*context*/ ) const
{
    //<text:a xlink:type="simple" xlink:href="http://www.kde.org/" office:name="sdgfsdfgs">kde org wxc &lt;wxc </text:a>
    writer.startElement( "text:a" );
    writer.addAttribute( "xlink:type", "simple" );
    writer.addAttribute( "xlink:href", m_url );
    writer.addAttribute( "office:name", m_varValue.toString() );
    writer.addTextNode( m_varValue.toString() );
    writer.endElement();

}

TQString KoLinkVariable::text(bool realValue)
{
    if (m_varColl->variableSetting()->displayFieldCode()&&!realValue)
        return fieldCode();
    else
        return value();
}

void KoLinkVariable::saveVariable( TQDomElement& parentElem )
{
    TQDomElement linkElem = parentElem.ownerDocument().createElement( "LINK" );
    parentElem.appendChild( linkElem );
    linkElem.setAttribute( "linkName", m_varValue.toString() );
    linkElem.setAttribute( "hrefName", m_url );
}

void KoLinkVariable::load( TQDomElement& elem )
{
    KoVariable::load( elem );
    TQDomElement linkElem = elem.namedItem( "LINK" ).toElement();
    if (!linkElem.isNull())
    {
        m_varValue = TQVariant(linkElem.attribute("linkName"));
        m_url = linkElem.attribute("hrefName");
    }
}

void KoLinkVariable::recalc()
{
    resize();
}

TQStringList KoLinkVariable::actionTexts()
{
    return TQStringList( i18n( "Link..." ) );
}


void KoLinkVariable::drawCustomItem( TQPainter* p, int x, int y, int wpix, int hpix, int ascentpix, int /*cx*/, int /*cy*/, int /*cw*/, int /*ch*/, const TQColorGroup& cg, bool selected, int offset, bool drawingShadow )
{
    KoTextFormat * fmt = format();
    KoTextZoomHandler * zh = textDocument()->paintingZoomHandler();

    bool displayLink = m_varColl->variableSetting()->displayLink();
    TQFont font( fmt->screenFont( zh ) );
    if ( m_varColl->variableSetting()->underlineLink() )
        font.setUnderline( true );
    TQColor textColor = displayLink ? cg.color( TQColorGroup::Link ) : fmt->color();

    drawCustomItemHelper( p, x, y, wpix, hpix, ascentpix, cg, selected, offset, fmt, font, textColor, drawingShadow );
}


/******************************************************************/
/* Class: KoNoteVariable                                          */
/******************************************************************/
KoNoteVariable::KoNoteVariable( KoTextDocument *textdoc, const TQString & _note,KoVariableFormat *varFormat,KoVariableCollection *_varColl )
    : KoVariable( textdoc, varFormat,_varColl )
    , m_createdNoteDate( TQDate::tqcurrentDate() )
{
    m_varValue = TQVariant( _note );
}

TQString KoNoteVariable::fieldCode()
{
    return i18n("Note");
}

TQString KoNoteVariable::createdNote() const
{
    return KGlobal::locale()->formatDate( m_createdNoteDate, false );
}

void KoNoteVariable::loadOasis( const TQDomElement &elem, KoOasisContext& /*context*/ )
{
    const TQString localName = elem.localName();
    TQString note;
    if ( localName == "annotation" && elem.namespaceURI() == KoXmlNS::office )
    {
        TQDomElement date = KoDom::namedItemNS( elem, KoXmlNS::dc, "date" );
        m_createdNoteDate = TQDate::fromString( date.text(), Qt::ISODate );
        TQDomNode text = KoDom::namedItemNS( elem, KoXmlNS::text, "p" );
        for ( ; !text.isNull(); text = text.nextSibling() )
        {
            if ( text.isElement() )
            {
                TQDomElement t = text.toElement();
                note += t.text() + "\n";
            }
        }
    }
    m_varValue = TQVariant( note  );
}

void KoNoteVariable::saveOasis( KoXmlWriter& writer, KoSavingContext& /*context*/ ) const
{
//    <office:annotation><dc:date>2004-11-10</dc:date><text:p/><text:p>---- 10/11/2004, 16:18 ----</text:p><text:p>dfgsdfsdfg</text:p><text:p>---- 10/11/2004, 16:18 ----</text:p><text:p/><text:p>---- 10/11/2004, 16:18 ----</text:p><text:p>gs</text:p><text:p>---- 10/11/2004, 16:18 ----</text:p><text:p>fg</text:p></office:annotation>
    writer.startElement( "office:annotation" );
    writer.startElement( "dc:date" );
    writer.addTextNode( m_createdNoteDate.toString(Qt::ISODate) );
    writer.endElement();
    TQStringList text = TQStringList::split( "\n", m_varValue.toString() );
    for ( TQStringList::Iterator it = text.begin(); it != text.end(); ++it ) {
        writer.startElement( "text:p" );
        writer.addTextNode( *it );
        writer.endElement();
    }
    writer.endElement();
}

void KoNoteVariable::saveVariable( TQDomElement& parentElem )
{
    TQDomElement linkElem = parentElem.ownerDocument().createElement( "NOTE" );
    parentElem.appendChild( linkElem );
    linkElem.setAttribute( "note", m_varValue.toString() );
}

void KoNoteVariable::load( TQDomElement& elem )
{
    KoVariable::load( elem );
    TQDomElement linkElem = elem.namedItem( "NOTE" ).toElement();
    if (!linkElem.isNull())
    {
        m_varValue = TQVariant(linkElem.attribute("note"));
    }
}

void KoNoteVariable::recalc()
{
    resize();
}

TQStringList KoNoteVariable::actionTexts()
{
    return TQStringList( i18n( "Note..." ) );
}

TQString KoNoteVariable::text(bool realValue)
{
    if (m_varColl->variableSetting()->displayComment() &&
        m_varColl->variableSetting()->displayFieldCode()&&!realValue)
        return fieldCode();
    else
        //for a note return just a "space" we can look at
        //note when we "right button"
        return TQString(" ");

}

void KoNoteVariable::drawCustomItem( TQPainter* p, int x, int y, int wpix, int hpix, int ascentpix, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool selected, int offset, bool drawingShadow )
{
    if ( !m_varColl->variableSetting()->displayComment())
        return;

    KoTextFormat * fmt = format();
    //kdDebug(32500) << "KoNoteVariable::drawCustomItem index=" << index() << " x=" << x << " y=" << y << endl;

    p->save();
    p->setPen( TQPen( fmt->color() ) );
    if ( fmt->textBackgroundColor().isValid() )
        p->fillRect( x, y, wpix, hpix, fmt->textBackgroundColor() );
    if ( selected )
    {
        p->setPen( TQPen( cg.color( TQColorGroup::HighlightedText ) ) );
        p->fillRect( x, y, wpix, hpix, cg.color( TQColorGroup::Highlight ) );
    }
    else if ( textDocument() && p->device()->devType() != TQInternal::Printer
        && !textDocument()->dontDrawingNoteVariable())
    {
        p->fillRect( x, y, wpix, hpix, TQt::yellow);
        p->setPen( TQPen( cg.color( TQColorGroup::Highlight ), 0, TQt::DotLine ) );
        p->drawRect( x, y, wpix, hpix );
    }
    //call it for use drawCustomItemHelper just for draw font effect
    KoVariable::drawCustomItem( p, x, y, wpix, hpix, ascentpix, cx, cy, cw, ch, cg, selected, offset, drawingShadow );

    p->restore();
}

void KoPageVariable::setSectionTitle( const TQString& _title )
{
    TQString title( _title );
    if ( title.isEmpty() )
    {
        title = i18n("<No title>");
    }
    m_varValue = TQVariant( title );
}


// ----------------------------------------------------------------
//                   class KoStatisticVariable


bool KoStatisticVariable::m_extendedType = false;


KoStatisticVariable::KoStatisticVariable( KoTextDocument *textdoc,
					  short int subtype,
					  KoVariableFormat *varFormat,
					  KoVariableCollection *_varColl )
    : KoVariable( textdoc, varFormat, _varColl ),
      m_subtype( subtype )
{
}


TQStringList KoStatisticVariable::actionTexts()
{
    TQStringList lst;
    lst << i18n( "Number of Words" );
    lst << i18n( "Number of Sentences" );
    lst << i18n( "Number of Lines" );
    lst << i18n( "Number of Characters" );
    lst << i18n( "Number of Non-Whitespace Characters" );
    lst << i18n( "Number of Syllables" );
    lst << i18n( "Number of Frames" );
    lst << i18n( "Number of Embedded Objects" );
    lst << i18n( "Number of Pictures" );
    if (  m_extendedType )
        lst << i18n( "Number of Tables" );
    return lst;
}


void KoStatisticVariable::setVariableSubType( short int subtype )
{
    m_subtype = subtype;
    Q_ASSERT( m_varColl );
    KoVariableFormatCollection* fc = m_varColl->formatCollection();
    setVariableFormat(fc->format("NUMBER") );
}


TQStringList KoStatisticVariable::subTypeList()
{
    return KoStatisticVariable::actionTexts();
}


void KoStatisticVariable::saveVariable( TQDomElement& varElem )
{
    TQDomElement  elem = varElem.ownerDocument().createElement( "STATISTIC" );
    varElem.appendChild( elem );

    elem.setAttribute( "type",  TQString::number(m_subtype) );
    elem.setAttribute( "value", TQString::number(m_varValue.toInt()) );
}


void KoStatisticVariable::load( TQDomElement &elem )
{
    KoVariable::load( elem );

    TQDomElement e = elem.namedItem( "STATISTIC" ).toElement();
    if ( !e.isNull() ) {
	// FIXME: Error handling.
	m_subtype  = e.attribute( "type" ).toInt();
	m_varValue = e.attribute( "value" ).toInt();
    }
}


void KoStatisticVariable::loadOasis( const TQDomElement &elem, KoOasisContext& /*context*/ )
{
    const TQString localName( elem.localName() );
    if ( localName == "object-count" )
    {
        m_subtype = VST_STATISTIC_NB_EMBEDDED;
        m_varValue = TQVariant( elem.text().toInt() );
    }
    else if ( localName == "table-count" )
    {
        m_subtype = VST_STATISTIC_NB_TABLE;
        m_varValue = TQVariant( elem.text().toInt() );
    }
    else if ( localName == "picture-count" )
    {
        m_subtype = VST_STATISTIC_NB_PICTURE;
        m_varValue = TQVariant( elem.text().toInt() );
    }
    else if ( localName == "word-count" )
    {
        m_subtype = VST_STATISTIC_NB_WORD;
        m_varValue = TQVariant( elem.text().toInt() );
    }
    else if ( localName == "character-count" )
    {
        m_subtype = VST_STATISTIC_NB_CHARACTERE;
        m_varValue = TQVariant( elem.text().toInt() );
    }
    else if ( localName == "frame-count" )
    {
        m_subtype = VST_STATISTIC_NB_FRAME;
        m_varValue = TQVariant( elem.text().toInt() );
    }
    else if ( localName == "line-count" )
    {
        m_subtype = VST_STATISTIC_NB_LINES;
        m_varValue = TQVariant( elem.text().toInt() );
    }
    else if ( localName == "sentence-count" )
    {
        m_subtype = VST_STATISTIC_NB_SENTENCE;
        m_varValue = TQVariant( elem.text().toInt() );
    }
    else if ( localName == "non-whitespace-character-count" )
    {
        m_subtype = VST_STATISTIC_NB_NON_WHITESPACE_CHARACTERE;
        m_varValue = TQVariant( elem.text().toInt() );
    }
    else if ( localName == "syllable-count" )
    {
        m_subtype = VST_STATISTIC_NB_SYLLABLE;
        m_varValue = TQVariant( elem.text().toInt() );
    }
    //TODO other copy
}

void KoStatisticVariable::saveOasis( KoXmlWriter& writer, KoSavingContext& /*context*/ ) const
{
    switch( m_subtype )
    {
    case VST_STATISTIC_NB_EMBEDDED:
        writer.startElement( "text:object-count" );
        writer.addTextNode( TQString::number( m_varValue.toInt() ) );
        writer.endElement();
        break;
    case VST_STATISTIC_NB_TABLE:
        writer.startElement( "text:table-count" );
        writer.addTextNode( TQString::number( m_varValue.toInt() ) );
        writer.endElement();
        break;
    case VST_STATISTIC_NB_PICTURE:
        writer.startElement( "text:picture-count" );
        writer.addTextNode( TQString::number( m_varValue.toInt() ) );
        writer.endElement();
        break;
    case VST_STATISTIC_NB_FRAME:
        //TODO verify that it's implemented into oasis file format
        writer.startElement( "text:frame-count" );
        writer.addTextNode( TQString::number( m_varValue.toInt() ) );
        writer.endElement();
        break;
    case VST_STATISTIC_NB_WORD:
        writer.startElement( "text:word-count" );
        writer.addTextNode( TQString::number( m_varValue.toInt() ) );
        writer.endElement();
        break;
    case VST_STATISTIC_NB_SENTENCE:
        //TODO verify that it's implemented into oasis file format
        writer.startElement( "text:sentence-count" );
        writer.addTextNode( TQString::number( m_varValue.toInt() ) );
        writer.endElement();
        break;
    case VST_STATISTIC_NB_CHARACTERE:
        writer.startElement( "text:character-count" );
        writer.addTextNode( TQString::number( m_varValue.toInt() ) );
        writer.endElement();
        break;
    case VST_STATISTIC_NB_LINES:
        //TODO verify that it's implemented into oasis file format
        writer.startElement( "text:line-count" );
        writer.addTextNode( TQString::number( m_varValue.toInt() ) );
        writer.endElement();
        break;
    case VST_STATISTIC_NB_NON_WHITESPACE_CHARACTERE:
        //TODO verify that it's implemented into oasis file format
        writer.startElement( "text:non-whitespace-character-count" );
        writer.addTextNode( TQString::number( m_varValue.toInt() ) );
        writer.endElement();
        break;
    case VST_STATISTIC_NB_SYLLABLE:
        //TODO verify that it's implemented into oasis file format
        writer.startElement( "text:syllable-count" );
        writer.addTextNode( TQString::number( m_varValue.toInt() ) );
        writer.endElement();
        break;
    }
}

TQString KoStatisticVariable::fieldCode()
{
    if ( m_subtype == VST_STATISTIC_NB_FRAME )
    {
        return i18n( "Number of Frames" );
    }
    else if( m_subtype == VST_STATISTIC_NB_PICTURE )
    {
        return i18n( "Number of Pictures" );
    }
    else if( m_subtype == VST_STATISTIC_NB_TABLE )
    {
        return i18n( "Number of Tables" );
    }
    else if( m_subtype == VST_STATISTIC_NB_EMBEDDED )
    {
        return i18n( "Number of Embedded Objects" );
    }
    else if( m_subtype == VST_STATISTIC_NB_WORD )
    {
        return i18n( "Number of Words" );
    }
    else if( m_subtype == VST_STATISTIC_NB_SENTENCE )
    {
        return i18n( "Number of Sentences" );
    }
    else if( m_subtype == VST_STATISTIC_NB_LINES )
    {
        return i18n( "Number of Lines" );
    }
    else if ( m_subtype == VST_STATISTIC_NB_CHARACTERE )
    {
        return i18n( "Number of Characters" );
    }
    else if ( m_subtype == VST_STATISTIC_NB_NON_WHITESPACE_CHARACTERE )
    {
        return i18n( "Number of Non-Whitespace Characters" );
    }
    else if ( m_subtype == VST_STATISTIC_NB_SYLLABLE )
    {
        return i18n( "Number of Syllables" );
    }
    else
        return i18n( "Number of Frames" );
}