summaryrefslogtreecommitdiffstats
path: root/lib/kotext/KoParagDia.cpp
blob: b62a148fd7d4f902592802dec7916ed6cea80026 (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
/* This file is part of the KDE project
   Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
   Copyright (C) 2005 Martin Ellis <martin.ellis@kdemail.net>
   Copyright (C) 2006 Thomas Zander <zander@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 "KoParagDia.h"
#include "KoParagDia_p.h"
#include "KoDocument.h"
#include "KoTextFormat.h"
#include "KoTextParag.h"
#include "KoTextDocument.h"
#include "KoTextZoomHandler.h"
#include "KoParagDecorationTab.h"

#include <KoCharSelectDia.h>
#include <KoUnitWidgets.h>
#include <kcolorbutton.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <knumvalidator.h>
#include <KoGlobal.h>
#include <tqgroupbox.h>
#include <knuminput.h>
#include <tdeversion.h>
#include <kpushbutton.h>
#include <kcombobox.h>

#include <tqcheckbox.h>
#include <tqcombobox.h>
#include <tqhbuttongroup.h>
#include <tqlabel.h>
#include <tqradiobutton.h>
#include <tqvbox.h>
#include <tqhbox.h>
#include <tqtooltip.h>
#include <tqlayout.h>
#include <tqapplication.h>
#include <tqwidgetstack.h>

KoCounterStyleWidget::KoCounterStyleWidget( bool displayDepth, bool onlyStyleTypeLetter, bool disableAll, TQWidget * parent, const char* name  )
    :TQWidget( parent, name ),
    stylesList()
{
    noSignals = true;
    styleBuffer = 999;
    TQVBoxLayout *vbox = new TQVBoxLayout( this,0, 0/*KDialog::marginHint(), KDialog::spacingHint()*/ );
    gStyle = new TQGroupBox( i18n( "St&yle" ), this, "styleLayout" );
    vbox->addWidget( gStyle);
    TQGridLayout * grid = new TQGridLayout(gStyle, 12, 5, KDialog::marginHint(), KDialog::spacingHint());
    grid->addRowSpacing(0, fontMetrics().height()/2);

    makeCounterRepresenterList( stylesList, onlyStyleTypeLetter );

    lstStyle = new TQListBox( gStyle, "styleListBox" );
    grid->addMultiCellWidget( lstStyle, 1, 11, 0, 0);
    fillStyleCombo();
    connect( lstStyle, TQT_SIGNAL( selectionChanged() ), this, TQT_SLOT( numStyleChanged() ) );


    TQLabel *lPrefix = new TQLabel( gStyle, "lPrefix" );
    lPrefix->setText( i18n( "Pre&fix text:" ) );
    grid->addWidget( lPrefix, 1, 1);

    sPrefix = new TQLineEdit( gStyle, "sPrefix" );
    lPrefix->setBuddy( sPrefix );
    grid->addWidget( sPrefix, 1, 2);

    TQLabel *lSuffix = new TQLabel( gStyle, "lSuffix" );
    lSuffix->setText( i18n( "Suffi&x text:" ) );
    grid->addWidget( lSuffix, 1, 3);

    sSuffix = new TQLineEdit( gStyle, "sSuffix" );
    lSuffix->setBuddy( sSuffix );
    grid->addWidget( sSuffix, 1, 4 );

    lStart = new TQLabel( gStyle, "lStart" );
    lStart->setText( i18n( "&Start at:" ) );
    grid->addWidget( lStart, 2, 1 );


    spnDepth = new TQSpinBox( 0, 15, 1, gStyle );
    if (  displayDepth )
        grid->addWidget( spnDepth, 3, 2 );
    else
        spnDepth->hide();

    spnDisplayLevels = new TQSpinBox( 0, 15, 1, gStyle );
    spnDisplayLevels->setMinValue( 1 );
    if ( displayDepth )
        grid->addWidget( spnDisplayLevels, 3, 4 );
    else
        spnDisplayLevels->hide();


    TQHBoxLayout *customCharBox = new TQHBoxLayout(0, 0, 6);
    lCustom = new TQLabel( i18n( "Custo&m character:" ), gStyle, "custom char label" );
    customCharBox->addWidget( lCustom );

    bCustom = new TQPushButton( "", gStyle, "bCustom" );
    lCustom->setBuddy( bCustom );
    customCharBox->addWidget( bCustom );
    connect( bCustom, TQT_SIGNAL( clicked() ), this, TQT_SLOT( selectCustomBullet() ) );

    TQSpacerItem* spacer_2 = new TQSpacerItem( 0, 0, TQSizePolicy::Expanding, TQSizePolicy::Minimum );
    customCharBox->addItem( spacer_2 );

    grid->addMultiCellLayout(customCharBox, 4, 4, 1, 4, TQt::AlignLeft);

    spnStart = new KoSpinBox( gStyle );
    spnStart->setMinValue ( 1);
    lStart->setBuddy( spnStart );
    grid->addWidget( spnStart, 2, 2);

    lAlignment = new TQLabel( gStyle, "lAlignment" );
    lAlignment->setText( i18n( "Counter alignment:" ) );
    grid->addWidget( lAlignment, 2, 3 );

    cbAlignment = new KComboBox( gStyle, "cbAlignment" );
    cbAlignment->insertItem(i18n("Align Auto"));
    cbAlignment->insertItem(i18n("Align Left"));
    cbAlignment->insertItem(i18n("Align Right"));
    cbAlignment->setCurrentItem(0);
    grid->addWidget( cbAlignment, 2, 4 );

    TQLabel *lDepth = new TQLabel( gStyle, "lDepth" );
    lDepth->setText( i18n( "&Depth:" ) );
    lDepth->setBuddy( spnDepth );
    if ( displayDepth )
        grid->addWidget( lDepth, 3, 1 );
    else
        lDepth->hide();

    TQLabel *lDisplayLevels = new TQLabel( gStyle );
    lDisplayLevels->setText( i18n( "Display le&vels:" ) );
    lDisplayLevels->setBuddy( spnDisplayLevels );
    if ( displayDepth )
        grid->addWidget( lDisplayLevels, 3, 3 );
    else
        lDisplayLevels->hide();

    cbRestart = new TQCheckBox( i18n( "&Restart numbering at this paragraph" ), gStyle );
    grid->addMultiCellWidget( cbRestart, 5, 5, 1, 3 );

    if ( onlyStyleTypeLetter )
    {
        lCustom->hide();
        bCustom->hide();
        cbRestart->hide();
    }


    connect( cbRestart, TQT_SIGNAL( toggled(bool) ), this, TQT_SLOT( restartChanged(bool) ) );

    connect( sSuffix, TQT_SIGNAL( textChanged (const TQString &) ), this, TQT_SLOT( suffixChanged(const TQString &) ) );
    connect( sPrefix, TQT_SIGNAL( textChanged (const TQString &) ), this, TQT_SLOT( prefixChanged(const TQString &) ) );
    connect( spnStart, TQT_SIGNAL( valueChanged (int) ), this, TQT_SLOT( startChanged(int) ) );
    connect( spnDepth, TQT_SIGNAL( valueChanged (int) ), this, TQT_SLOT( depthChanged(int) ) );
    connect( spnDisplayLevels, TQT_SIGNAL( valueChanged (int) ), this, TQT_SLOT( displayLevelsChanged(int) ) );
    connect( cbAlignment, TQT_SIGNAL( activated (const TQString&) ), this, TQT_SLOT( alignmentChanged(const TQString&) ) );
    noSignals = false;
    if ( disableAll )
    {
        gStyle->setEnabled( false );
        lstStyle->setEnabled( false );
        sSuffix->setEnabled( false );
        sPrefix->setEnabled( false );
        bCustom->setEnabled( false );
        spnStart->setEnabled( false );
        spnDepth->setEnabled( false );
        spnDisplayLevels->setEnabled( false );
        lStart->setEnabled( false );
        lCustom->setEnabled( false );
        cbRestart->setEnabled( false );
        cbAlignment->setEnabled( false );
    }
}

void KoCounterStyleWidget::alignmentChanged(const TQString& s)
{
    int a;
    if(s==i18n("Align Left"))
        a=TQt::AlignLeft;
    else if(s==i18n("Align Right"))
        a=TQt::AlignRight;
    else if(s==i18n("Align Auto"))
        a=TQt::AlignAuto;
    else {
        kdError()<<"Not Implemented"<<endl;
        return;
    }
    m_counter.setAlignment(a);
    emit sig_alignmentChanged(a);
}

void KoCounterStyleWidget::setCounter( const KoParagCounter& counter )
{
    noSignals = true;
    KoParagCounter::Style st = counter.style();
    m_counter = counter;
    // Huh? doesn't the line above do this already?
    //m_counter.setStyle( st );
    changeKWSpinboxType( st);
    displayStyle( st );
    noSignals = false;
}

void KoCounterStyleWidget::changeKWSpinboxType(KoParagCounter::Style st) {
    switch(st)
    {
        case KoParagCounter::STYLE_NONE:
            spnStart->setCounterType(KoSpinBox::NONE);
            break;
        case KoParagCounter::STYLE_NUM:
            spnStart->setCounterType(KoSpinBox::NUM);
            break;
        case KoParagCounter::STYLE_ALPHAB_L:
            spnStart->setCounterType(KoSpinBox::ALPHAB_L);
            break;
        case KoParagCounter::STYLE_ALPHAB_U:
            spnStart->setCounterType(KoSpinBox::ALPHAB_U);
            break;
        case KoParagCounter::STYLE_ROM_NUM_L:
            spnStart->setCounterType(KoSpinBox::ROM_NUM_L);
            break;
        case KoParagCounter::STYLE_ROM_NUM_U:
            spnStart->setCounterType(KoSpinBox::ROM_NUM_U);
            break;
        default:
            spnStart->setCounterType(KoSpinBox::NONE);
    }
}


void KoCounterStyleWidget::fillStyleCombo(KoParagCounter::Numbering type) {
    if(lstStyle==NULL) return;
    noSignals=true;
    unsigned int cur = lstStyle->currentItem();
    lstStyle->clear();
    TQPtrListIterator<StyleRepresenter> style( stylesList );
    while ( style.current() ) {
        if(style.current()->style() == KoParagCounter::STYLE_NONE) {
            if(type == KoParagCounter::NUM_NONE)
                lstStyle->insertItem( style.current()->name() );
        }
        else if(type == KoParagCounter::NUM_LIST || !style.current()->isBullet())
            if(type != KoParagCounter::NUM_NONE)
                lstStyle->insertItem( style.current()->name() );
        ++style;
    }

    if(styleBuffer <= lstStyle->count())
        lstStyle->setCurrentItem(styleBuffer);
    else
        if(cur <= lstStyle->count())
            lstStyle->setCurrentItem(cur);

    if(cur > lstStyle->count()) {
        styleBuffer = cur;
    }
    noSignals=false;
}

void KoCounterStyleWidget::displayStyle( KoParagCounter::Style style )
{
    unsigned int i = 0;
    while ( stylesList.count() > i && stylesList.at(i)->style() != style )
        ++i;
    lstStyle->setCurrentItem(i);

    bCustom->setText( m_counter.customBulletCharacter() );
    if ( !m_counter.customBulletFont().isEmpty() )
        bCustom->setFont( TQFont( m_counter.customBulletFont() ) );

    sPrefix->setText( m_counter.prefix() );
    sSuffix->setText( m_counter.suffix() );

    spnDepth->setValue( m_counter.depth() );
    spnDisplayLevels->setValue( m_counter.displayLevels() );
    spnStart->setValue( m_counter.startNumber() );

    cbRestart->setChecked( m_counter.restartCounter() );
    if(m_counter.alignment()==TQt::AlignLeft)
        cbAlignment->setCurrentText(i18n("Align Left"));
    else if(m_counter.alignment()==TQt::AlignRight)
        cbAlignment->setCurrentText(i18n("Align Right"));
    else if(m_counter.alignment()==TQt::AlignAuto)
        cbAlignment->setCurrentText(i18n("Align Auto"));
    else
        kdError()<<"Not Implemented"<<endl;
}

void KoCounterStyleWidget::display( const KoParagLayout & lay ) {
    KoParagCounter::Style style = KoParagCounter::STYLE_NONE;
    if ( lay.counter )
    {
        style=lay.counter->style();
        m_counter = *lay.counter;
    }
    else
    {
        m_counter = KoParagCounter();
    }
    styleBuffer = 999;

    numTypeChanged( m_counter.numbering() );
    emit sig_numTypeChanged( m_counter.numbering() );

    displayStyle( style );
}


void KoCounterStyleWidget::numTypeChanged( int nType ) {
    m_counter.setNumbering( static_cast<KoParagCounter::Numbering>( nType ) );
    gStyle->setEnabled( m_counter.numbering() != KoParagCounter::NUM_NONE );
    fillStyleCombo(m_counter.numbering());
    bool state=m_counter.numbering()==KoParagCounter::NUM_LIST;
    bCustom->setEnabled(state);
    lCustom->setEnabled(state);
}


void KoCounterStyleWidget::makeCounterRepresenterList( TQPtrList<StyleRepresenter>& stylesList, bool onlyStyleTypeLetter )
{
    stylesList.setAutoDelete( true );
    stylesList.append( new StyleRepresenter(i18n( "Arabic Numbers" )
            ,  KoParagCounter::STYLE_NUM));
    stylesList.append( new StyleRepresenter(i18n( "Lower Alphabetical" )
            ,  KoParagCounter::STYLE_ALPHAB_L ));
    stylesList.append( new StyleRepresenter(i18n( "Upper Alphabetical" )
            ,  KoParagCounter::STYLE_ALPHAB_U ));
    stylesList.append( new StyleRepresenter(i18n( "Lower Roman Numbers" )
            ,  KoParagCounter::STYLE_ROM_NUM_L ));
    stylesList.append( new StyleRepresenter(i18n( "Upper Roman Numbers" )
            ,  KoParagCounter::STYLE_ROM_NUM_U ));
    if ( !onlyStyleTypeLetter )
    {
        stylesList.append( new StyleRepresenter(i18n( "Disc Bullet" )
                                                ,  KoParagCounter::STYLE_DISCBULLET , true));
        stylesList.append( new StyleRepresenter(i18n( "Square Bullet" )
                                                ,  KoParagCounter::STYLE_SQUAREBULLET , true));
        stylesList.append( new StyleRepresenter(i18n( "Box Bullet" )
                                                ,  KoParagCounter::STYLE_BOXBULLET , true));
        stylesList.append( new StyleRepresenter(i18n( "Circle Bullet" )
                                                ,  KoParagCounter::STYLE_CIRCLEBULLET , true));
        stylesList.append( new StyleRepresenter(i18n( "Custom Bullet" )
                                                ,  KoParagCounter::STYLE_CUSTOMBULLET , true));
    }
    stylesList.append( new StyleRepresenter(i18n( "None" ), KoParagCounter::STYLE_NONE));
}


void KoCounterStyleWidget::selectCustomBullet() {
    unsigned int i = 0;
    while ( stylesList.count() > i && stylesList.at(i)->style() != KoParagCounter::STYLE_CUSTOMBULLET )
        ++i;
    lstStyle->setCurrentItem(i);

    TQString f = m_counter.customBulletFont();
    if ( f.isEmpty() )
        f = "symbol";
    TQChar c = m_counter.customBulletCharacter();

    if ( KoCharSelectDia::selectChar( f, c ) ) {
        emit changeCustomBullet( f, c );
        m_counter.setCustomBulletFont( f );
        m_counter.setCustomBulletCharacter( c );
        if ( !f.isEmpty() )
            bCustom->setFont( TQFont( f ) );
        bCustom->setText( c );
    }
}

void KoCounterStyleWidget::numStyleChanged() {
    if ( noSignals )
        return;
    // We selected another style from the list box.
    styleBuffer = 999;
    StyleRepresenter *sr = stylesList.at(lstStyle->currentItem());
    emit changeStyle( sr->style() );
    m_counter.setStyle( sr->style() );
    bool isNumbered = !sr->isBullet() && !sr->style() == KoParagCounter::STYLE_NONE;
    lStart->setEnabled( isNumbered );
    spnStart->setEnabled( isNumbered );
    cbRestart->setEnabled( isNumbered );
    spnDisplayLevels->setEnabled( isNumbered );
    changeKWSpinboxType(sr->style() );
}



KoSpinBox::KoSpinBox( TQWidget * parent, const char * name )
    : TQSpinBox(parent,name)
{
    m_Etype=NONE;
    //max value supported by roman number
    setMaxValue ( 3999 );
}
KoSpinBox::~KoSpinBox( )
{
}

KoSpinBox::KoSpinBox( int minValue, int maxValue, int step ,
           TQWidget * parent , const char * name  )
    : TQSpinBox(minValue, maxValue,step ,
           parent , name)
{
    m_Etype=NONE;
}

void KoSpinBox::setCounterType(counterType _type)
{
    m_Etype=_type;
    editor()->setText(mapValueToText(value()));
}


TQString KoSpinBox::mapValueToText( int value )
{
    if(value==0 && m_Etype==NUM)
        return TQString("0");
    else if(value==0 && m_Etype!=NUM)
        return TQString();
    switch(m_Etype)
    {
        case NUM:
            return TQString::number(value);
        case ALPHAB_L:
            return KoParagCounter::makeAlphaLowerNumber( value );
        case ALPHAB_U:
            return KoParagCounter::makeAlphaUpperNumber( value );
        case ROM_NUM_L:
            return KoParagCounter::makeRomanNumber( value );
        case ROM_NUM_U:
            return KoParagCounter::makeRomanNumber( value ).upper();
        case NONE:
        default:
            return TQString();
    }
    //never here
    return TQString();
}

int KoSpinBox::mapTextToValue( bool * ok )
{
    int ret;
    TQString txt = text();

    *ok = TRUE;
    switch(m_Etype)
    {
        case NUM:
            ret = txt.toInt ( ok );
            break;
        case ALPHAB_L:
            ret = KoParagCounter::fromAlphaLowerNumber( txt.lower() );
            break;
        case ALPHAB_U:
            ret = KoParagCounter::fromAlphaUpperNumber( txt.upper() );
            break;
        case ROM_NUM_L:
            ret = KoParagCounter::fromRomanNumber( txt.lower() );
            break;
        case ROM_NUM_U:
            ret = KoParagCounter::fromRomanNumber( txt.lower() ); // _not_ upper()
            break;
        case NONE:
        default:
            ret = -1;
            break;
    }

    if (ret == -1)
        *ok = FALSE;

    return ret;
}


/******************************************************************/
/* class KPagePreview                                            */
/******************************************************************/

KPagePreview::KPagePreview( TQWidget* parent, const char* name )
    : TQGroupBox( i18n( "Preview" ), parent, name )
{
    left = 0;
    right = 0;
    first = 0;
    spacing = 0;
    before = 0;
    after = 0;
}

void KPagePreview::drawContents( TQPainter* p )
{
    int wid = 148;
    int hei = 210;
    int _x = ( width() - wid ) / 5;
    int _y = ( height() - hei ) / 5;

    int dl = convert(left);
    int dr = convert(right);

    //first+left because firstlineIndent is relative to leftIndent
    int df = convert(first + left);

    int spc = convert(spacing);

    // draw page
    p->setPen( TQPen( black ) );
    p->setBrush( TQBrush( black ) );

    p->drawRect( _x + 1, _y + 1, wid, hei );

    p->setBrush( TQBrush( white ) );
    p->drawRect( _x, _y, wid, hei );

    // draw parags
    p->setPen( NoPen );
    p->setBrush( TQBrush( lightGray ) );

    for ( int i = 1; i <= 4; i++ )
        p->drawRect( _x + 6, _y + 6 + ( i - 1 ) * 12 + 2, wid - 12 - ( ( i / 4 ) * 4 == i ? 50 : 0 ), 6 );

    p->setBrush( TQBrush( darkGray ) );

    for ( int i = 5; i <= 8; i++ )
      {
	TQRect rect( ( i == 5 ? df : dl ) + _x + 6, _y + 6 + ( i - 1 ) * 12 + 2 + ( i - 5 ) * spc + static_cast<int>( before / 2 ),
		    wid - 12 - ( ( i / 4 ) * 4 == i ? 50 : 0 ) - ( ( i == 12 ? 0 : dr ) + ( i == 5 ? df : dl ) ), 6);

	if(rect.width ()>=0)
	  p->drawRect( rect );
      }
    p->setBrush( TQBrush( lightGray ) );

    for ( int i = 9; i <= 12; i++ )
        p->drawRect( _x + 6, _y + 6 + ( i - 1 ) * 12 + 2 + 3 * spc +
                     static_cast<int>( before / 2 ) + static_cast<int>( after / 2 ),
                     wid - 12 - ( ( i / 4 ) * 4 == i ? 50 : 0 ), 6 );

}

int KPagePreview::convert(double input) {
    if(input < 1) return 0;
    if(input <= 5) return 3;
    if(input <= 10) return 4 + static_cast<int>( (input-5) / 2.5 );
    if(input <= 20) return 6 + static_cast<int>( (input-10) / 4 );
    if(input <= 100) return 10 + static_cast<int>( (input-20) / 8 );
    return static_cast<int>( input / 5);
}

/******************************************************************/
/* class KPagePreview2                                           */
/******************************************************************/

KPagePreview2::KPagePreview2( TQWidget* parent, const char* name )
    : TQGroupBox( i18n( "Preview" ), parent, name )
{
    align = TQt::AlignLeft;
}

void KPagePreview2::drawContents( TQPainter* p )
{
    int wid = 148;
    int hei = 210;
    int _x = ( width() - wid ) / 2;
    int _y = ( height() - hei ) / 2;

    // draw page
    p->setPen( TQPen( black ) );
    p->setBrush( TQBrush( black ) );

    p->drawRect( _x + 1, _y + 1, wid, hei );

    p->setBrush( TQBrush( white ) );
    p->drawRect( _x, _y, wid, hei );

    // draw parags
    p->setPen( NoPen );
    p->setBrush( TQBrush( lightGray ) );

    for ( int i = 1; i <= 4; i++ )
        p->drawRect( _x + 6, _y + 6 + ( i - 1 ) * 12 + 2, wid - 12 - ( ( i / 4 ) * 4 == i ? 50 : 0 ), 6 );

    p->setBrush( TQBrush( darkGray ) );

    int __x = 0, __w = 0;
    for ( int i = 5; i <= 8; i++ ) {
        switch ( i ) {
        case 5: __w = wid - 12;
            break;
        case 6: __w = wid - 52;
            break;
        case 7: __w = wid - 33;
            break;
        case 8: __w = wid - 62;
        default: break;
        }

        switch ( align ) {
#ifndef USE_QT4
            case TQt::AlignAuto:
#endif // USE_QT4
            case TQt::AlignLeft:
                __x = _x + 6;
                break;
            case TQt::AlignHCenter:
                __x = _x + ( wid - __w ) / 2;
                break;
            case TQt::AlignRight:
                __x = _x + ( wid - __w ) - 6;
                break;
            case TQt::AlignJustify:
            {
                if ( i < 8 ) __w = wid - 12;
                __x = _x + 6;
            } break;
        }

        p->drawRect( __x, _y + 6 + ( i - 1 ) * 12 + 2 + ( i - 5 ), __w, 6 );
    }

    p->setBrush( TQBrush( lightGray ) );

    for ( int i = 9; i <= 12; i++ )
        p->drawRect( _x + 6, _y + 6 + ( i - 1 ) * 12 + 2 + 3, wid - 12 - ( ( i / 4 ) * 4 == i ? 50 : 0 ), 6 );

}

/******************************************************************/
/* class KoBorderPreview                                          */
/******************************************************************/


KoBorderPreview::KoBorderPreview( TQWidget* parent, const char* name )
    :TQFrame(parent,name)
{
}

void KoBorderPreview::mousePressEvent( TQMouseEvent *_ev )
{
    emit choosearea(_ev);
}

void KoBorderPreview::setBorder( KoBorder::BorderType which, const KoBorder& border)
{
    switch( which ) {
    case KoBorder::TopBorder:
        setTopBorder( border );
        break;
    case KoBorder::BottomBorder:
        setBottomBorder( border );
        break;
    case KoBorder::LeftBorder:
        setLeftBorder( border );
        break;
    case KoBorder::RightBorder:
        setRightBorder( border );
        break;
    default:
        kdError() << "KoBorderPreview: unknown border type" << endl;
    }
}

void KoBorderPreview::drawContents( TQPainter* painter )
{
    TQRect r = contentsRect();
    TQFontMetrics fm( font() );

    painter->fillRect( r.x() + fm.width( 'W' ), r.y() + fm.height(), r.width() - 2 * fm.width( 'W' ),
                       r.height() - 2 * fm.height(), white );
    painter->setClipRect( r.x() + fm.width( 'W' ), r.y() + fm.height(), r.width() - 2 * fm.width( 'W' ),
                          r.height() - 2 * fm.height() );

    bool leftdouble = m_leftBorder.width() > 0 && m_leftBorder.getStyle() == KoBorder::DOUBLE_LINE;
    bool rightdouble = m_rightBorder.width() > 0 && m_rightBorder.getStyle() == KoBorder::DOUBLE_LINE;
    bool topdouble = m_topBorder.width() > 0 && m_topBorder.getStyle() == KoBorder::DOUBLE_LINE;
    bool bottomdouble = m_bottomBorder.width() > 0 && m_bottomBorder.getStyle() == KoBorder::DOUBLE_LINE;

    if ( m_topBorder.width() > 0 ) {
        painter->setPen( setBorderPen( m_topBorder ) );
        painter->drawLine( r.x() + 20, r.y() + 30, r.right() - 19, r.y() + 30 );
        if ( m_topBorder.getStyle()==KoBorder::DOUBLE_LINE)
            painter->drawLine( int(r.x() + 20 + ( leftdouble ? m_leftBorder.width() + 1 : 0) ),
                               int(r.y() + 30 + m_topBorder.width()+1),
                               int(r.right() - 19 - ( rightdouble ? m_rightBorder.width() + 1 : 0) ),
                               int(r.y() + 30 + m_topBorder.width()+1)
                             );
    }

    if ( m_bottomBorder.width() > 0 ) {
        painter->setPen( setBorderPen( m_bottomBorder ) );
        painter->drawLine( r.x() + 20, r.bottom() - 30, r.right() - 19, r.bottom() - 30 );
        if ( m_bottomBorder.getStyle()==KoBorder::DOUBLE_LINE)
            painter->drawLine( int(r.x() + 20 + ( leftdouble ? m_leftBorder.width() + 1 : 0) ),
                               int(r.bottom() - 30 - m_bottomBorder.width()-1),
                               int(r.right() - 19 - ( rightdouble ? m_rightBorder.width() + 1 : 0) ),
                               int(r.bottom() - 30 - m_bottomBorder.width() - 1)
                             );
    }

    if ( m_leftBorder.width() > 0 ) {
        painter->setPen( setBorderPen( m_leftBorder ) );
        painter->drawLine( r.x() + 20, r.y() + 30, r.x() + 20, r.bottom() - 29 );
        if ( m_leftBorder.getStyle()==KoBorder::DOUBLE_LINE)
            painter->drawLine( int(r.x() + 20 + m_leftBorder.width() +1),
                               int(r.y() + 30 + ( topdouble ? m_topBorder.width() + 1 : 0) ),
                               int(r.x() + 20 + m_leftBorder.width() +1),
                               int(r.bottom() - 29 - ( bottomdouble ? m_bottomBorder.width() + 1 : 0) )
                             );
    }

    if ( m_rightBorder.width() > 0 ) {
        painter->setPen( setBorderPen( m_rightBorder ) );
        painter->drawLine( r.right() - 20, r.y() + 30, r.right() - 20, r.bottom() - 29 );
        if ( m_rightBorder.getStyle()==KoBorder::DOUBLE_LINE)
            painter->drawLine( int(r.right() - 20 - m_rightBorder.width() - 1 ),
                               int(r.y() + 30 + ( topdouble ? m_topBorder.width() + 1 : 0) ),
                               int(r.right() - 20 - m_rightBorder.width() - 1),
                               int(r.bottom() - 29 - ( bottomdouble ? m_bottomBorder.width() + 1 : 0) )
                             );
    }
}

TQPen KoBorderPreview::setBorderPen( KoBorder _brd )
{
    TQPen pen( black, 1, SolidLine );

    pen.setWidth( static_cast<int>( _brd.penWidth() ) );
    pen.setColor( _brd.color );

    switch ( _brd.getStyle() ) {
    case KoBorder::SOLID:
        pen.setStyle( Qt::SolidLine );
        break;
    case KoBorder::DASH:
        pen.setStyle( Qt::DashLine );
        break;
    case KoBorder::DOT:
        pen.setStyle( Qt::DotLine );
        break;
    case KoBorder::DASH_DOT:
        pen.setStyle( Qt::DashDotLine );
        break;
    case KoBorder::DASH_DOT_DOT:
        pen.setStyle( Qt::DashDotDotLine );
        break;
    case KoBorder::DOUBLE_LINE:
        pen.setStyle( Qt::SolidLine );
        break;
    }

    return TQPen( pen );
}

/******************************************************************/
/* Class: KoStylePreview. Previewing text with style ;)           */
/******************************************************************/
class MyFlow : public KoTextFlow {
    public:
        MyFlow(TQWidget *parent, KoTextZoomHandler *zoom) {
            m_parent = parent;
            m_zoomHandler = zoom;
        }
        int availableHeight() const {
            return m_zoomHandler->pixelToLayoutUnitY(m_parent->height());
        }
    private:
        TQWidget *m_parent;
        KoTextZoomHandler *m_zoomHandler;
};

KoStylePreview::KoStylePreview( const TQString& title, const TQString& text, TQWidget* parent, const char* name )
    : TQGroupBox( title, parent, name )
{
    setMinimumHeight(80);
    m_zoomHandler = new KoTextZoomHandler;
    TQFont font = KoGlobal::defaultFont();
    m_textdoc = new KoTextDocument( m_zoomHandler, new KoTextFormatCollection( font, TQColor(), TDEGlobal::locale()->language(), false ) );

    m_textdoc->setFlow( new MyFlow(this, m_zoomHandler) );
    //m_textdoc->setWidth( KoTextZoomHandler::ptToLayoutUnitPt( 1000 ) );
    KoTextParag * parag = m_textdoc->firstParag();
    parag->insert( 0, text );
}

KoStylePreview::~KoStylePreview()
{
    delete m_textdoc;
    delete m_zoomHandler;
}

void KoStylePreview::setCounter( const KoParagCounter & counter )
{
    KoTextParag * parag = m_textdoc->firstParag();
    parag->setCounter( counter );
    repaint( true );
}

void KoStylePreview::setStyle( KoParagStyle * style )
{
    KoTextParag * parag = m_textdoc->firstParag();
    parag->applyStyle( style );
    repaint(true);
}

void KoStylePreview::drawContents( TQPainter *painter )
{
    painter->save();
    TQRect r = contentsRect();
    //kdDebug(32500) << "KoStylePreview::drawContents contentsRect=" << DEBUGRECT(r) << endl;

    TQRect whiteRect( r.x() + 10, r.y() + 10,
                     r.width() - 20, r.height() - 20 );
    TQColorGroup cg = TQApplication::palette().active();
    painter->fillRect( whiteRect, cg.brush( TQColorGroup::Base ) );

    KoTextParag * parag = m_textdoc->firstParag();
    int widthLU = m_zoomHandler->pixelToLayoutUnitX( whiteRect.width() - 2 ); // keep one pixel border horizontally
    if ( m_textdoc->width() != widthLU )
    {
        // For centering to work, and to even get word wrapping when the thing is too big :)
        m_textdoc->setWidth( widthLU );
        parag->invalidate(0);
    }

    parag->format();
    TQRect textRect = parag->pixelRect( m_zoomHandler );

    // Center vertically, but not horizontally, to keep the parag alignment working,
    textRect.moveTopLeft( TQPoint( whiteRect.x(),
                                  whiteRect.y() + ( whiteRect.height() - textRect.height() ) / 2 ) );
    // Move it from the left border a little
    textRect.rLeft() += 4;
    textRect.rRight() += 4;
    //kdDebug(32500) << "KoStylePreview::drawContents textRect=" << DEBUGRECT(textRect)
    //          << " textSize=" << textSize.width() << "," << textSize.height() << endl;
    painter->setClipRect( textRect.intersect( whiteRect ) );
    painter->translate( textRect.x(), textRect.y() );

    m_textdoc->drawWYSIWYG( painter, 0, 0, textRect.width(), textRect.height(), cg, m_zoomHandler );
    painter->restore();
}

KoIndentSpacingWidget::KoIndentSpacingWidget( KoUnit::Unit unit,  double _frameWidth,TQWidget * parent, const char * name )
        : KoParagLayoutWidget( KoParagDia::PD_SPACING, parent, name ), m_unit( unit )
{
    TQString unitName = KoUnit::unitName( m_unit );
    TQGridLayout *mainGrid = new TQGridLayout( this, 3, 2, KDialog::marginHint(), KDialog::spacingHint() );

    // mainGrid gives equal space to each groupbox, apparently
    // I tried setRowStretch but the result is awful (much space between them and not equal!)
    // Any other way (in order to make the 2nd, the one with a single checkbox, a bit
    // smaller than the other 3) ? (DF)


    // --------------- indent ---------------
    double frameWidth=_frameWidth;
    TQString length;
    if(frameWidth==-1) {
        frameWidth=9999;
    } else {
        length=i18n("Frame width: %1 %2")
		.arg(KoUnit::toUserStringValue(frameWidth,m_unit))
		.arg(KoUnit::unitName(m_unit));
        frameWidth=KoUnit::toUserValue(frameWidth,m_unit);
    }

    TQGroupBox * indentFrame = new TQGroupBox( i18n( "Indent" ), this );
    TQGridLayout * indentGrid = new TQGridLayout( indentFrame, 5, 2, KDialog::marginHint(), KDialog::spacingHint() );

    TQLabel * lLimit = new TQLabel(length , indentFrame );
    if(frameWidth!=-1)
    {
        lLimit->setAlignment( AlignRight );
        indentGrid->addWidget( lLimit, 1,0 );
    }

    TQLabel * lLeft = new TQLabel( i18n("&Left:"), indentFrame );
    lLeft->setAlignment( TQt::AlignVCenter | TQt::AlignRight );
    indentGrid->addWidget( lLeft, 1, 0 );

    eLeft = new KoUnitDoubleSpinBox( indentFrame, 0, 9999, 1, 0.0, m_unit );
    lLeft->setBuddy( eLeft );
    indentGrid->addWidget( eLeft, 1, 1 );
    connect( eLeft, TQT_SIGNAL( valueChangedPt(double ) ), this, TQT_SLOT( leftChanged( double ) ) );

    TQLabel * lRight = new TQLabel( i18n("&Right:"), indentFrame );
    lRight->setAlignment( TQt::AlignVCenter | TQt::AlignRight );
    indentGrid->addWidget( lRight, 2, 0 );

    eRight = new KoUnitDoubleSpinBox( indentFrame, 0, 9999, 1, 0.0, m_unit );
    lRight->setBuddy( eRight );
    indentGrid->addWidget( eRight, 2, 1 );
    connect( eRight, TQT_SIGNAL( valueChangedPt( double ) ), this, TQT_SLOT( rightChanged( double ) ) );

    TQLabel * lFirstLine = new TQLabel( i18n("&First line:"), indentFrame );
    lFirstLine->setAlignment( TQt::AlignVCenter | TQt::AlignRight );
    indentGrid->addWidget( lFirstLine, 3, 0 );

    eFirstLine = new KoUnitDoubleSpinBox( indentFrame, -9999, 9999, 1, 0.0, m_unit );
    lFirstLine->setBuddy( eFirstLine );
    connect( eFirstLine, TQT_SIGNAL( valueChangedPt( double ) ), this, TQT_SLOT( firstChanged( double ) ) );
    indentGrid->addWidget( eFirstLine, 3, 1 );

    // grid row spacing
    indentGrid->addRowSpacing( 0, fontMetrics().height() / 2 ); // groupbox title
    for ( int i = 1 ; i < indentGrid->numRows() ; ++i )
        indentGrid->setRowStretch( i, 1 );
    mainGrid->addWidget( indentFrame, 0, 0 );

        // --------------- line spacing ---------------
    TQGroupBox * spacingFrame = new TQGroupBox( i18n( "Line &Spacing" ), this, "spacingFrame" );
    TQGridLayout * spacingGrid = new TQGridLayout( spacingFrame, 2, 1,
                                                 KDialog::marginHint(), KDialog::spacingHint() );

    cSpacing = new TQComboBox( false, spacingFrame, "" );
    // Keep order in sync with lineSpacingType() and display()
    cSpacing->insertItem( i18n( "Line spacing value", "Single" ) );
    cSpacing->insertItem( i18n( "Line spacing value", "1.5 Lines" ) );
    cSpacing->insertItem( i18n( "Line spacing value", "Double" ) );
    cSpacing->insertItem( i18n( "Proportional") ); // LS_MULTIPLE, called Proportional like in OO
    cSpacing->insertItem( i18n( "Line Distance (%1)" ).arg(unitName) ); // LS_CUSTOM
    cSpacing->insertItem( i18n( "At Least (%1)" ).arg(unitName) );
    cSpacing->insertItem( i18n( "Fixed (%1)").arg(unitName) ); // LS_FIXED

    connect( cSpacing, TQT_SIGNAL( activated( int ) ), this, TQT_SLOT( spacingActivated( int ) ) );
    spacingGrid->addWidget( cSpacing, 1, 0 );

    sSpacingStack = new TQWidgetStack( spacingFrame );
    
    eSpacing = new KoUnitDoubleSpinBox( spacingFrame, 0, 9999, CM_TO_POINT(1),
					0.0, m_unit );
    eSpacing->setRange( 0, 9999, 1, false);
    connect( eSpacing, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( spacingChanged( double ) ) );
    eSpacingPercent = new KIntNumInput( 100, spacingFrame );
    eSpacingPercent->setRange( 0, 1000, 10, false );
    eSpacingPercent->setSuffix( " %" );
    connect( eSpacingPercent, TQT_SIGNAL( valueChanged( int ) ), this, TQT_SLOT( spacingChanged( int ) ) );
    
    sSpacingStack->addWidget( eSpacing );
    sSpacingStack->addWidget( eSpacingPercent );
    spacingGrid->addWidget( sSpacingStack, 1, 1 );

    // grid row spacing
    spacingGrid->addRowSpacing( 0, fontMetrics().height() / 2 ); // groupbox title
    for ( int i = 1 ; i < spacingGrid->numRows() ; ++i )
        spacingGrid->setRowStretch( i, 1 );
    mainGrid->addWidget( spacingFrame, 1, 0 );

    eSpacing->setEnabled( true );

    // --------------- paragraph spacing ---------------
    TQGroupBox * pSpaceFrame = new TQGroupBox( i18n( "Para&graph Space" ), this, "pSpaceFrame" );
    TQGridLayout * pSpaceGrid = new TQGridLayout( pSpaceFrame, 3, 2,
                                                KDialog::marginHint(), KDialog::spacingHint() );

    TQLabel * lBefore = new TQLabel( i18n("Before:"), pSpaceFrame );
    lBefore->setAlignment( AlignRight );
    pSpaceGrid->addWidget( lBefore, 1, 0 );

    eBefore = new KoUnitDoubleSpinBox( pSpaceFrame, 0, 9999, CM_TO_POINT(1), 0.0, m_unit );
    eBefore->setRange( 0 , 9999, 1, false);
    connect( eBefore, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( beforeChanged( double ) ) );
    pSpaceGrid->addWidget( eBefore, 1, 1 );

    TQLabel * lAfter = new TQLabel( i18n("After:"), pSpaceFrame );
    lAfter->setAlignment( AlignRight );
    pSpaceGrid->addWidget( lAfter, 2, 0 );

    eAfter = new KoUnitDoubleSpinBox( pSpaceFrame, 0, 9999, 1, 0.0, m_unit );
    eAfter->setRange( 0, 9999, 1, false);
    connect( eAfter, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( afterChanged( double ) ) );
    pSpaceGrid->addWidget( eAfter, 2, 1 );

    // grid row spacing
    pSpaceGrid->addRowSpacing( 0, fontMetrics().height() / 2 ); // groupbox title
    for ( int i = 1 ; i < pSpaceGrid->numRows() ; ++i )
        pSpaceGrid->setRowStretch( i, 1 );
    mainGrid->addWidget( pSpaceFrame, 2, 0 );

    // --------------- preview --------------------
    prev1 = new KPagePreview( this, "KPagePreview" );
    mainGrid->addMultiCellWidget( prev1, 0, mainGrid->numRows()-1, 1, 1 );

    mainGrid->setColStretch( 1, 1 );
    //mainGrid->setRowStretch( 4, 1 );
}

double KoIndentSpacingWidget::leftIndent() const
{
    return TQMAX(0, eLeft->value() );
}

double KoIndentSpacingWidget::rightIndent() const
{
    return TQMAX(0,eRight->value() );
}

double KoIndentSpacingWidget::firstLineIndent() const
{
    return eFirstLine->value();
}

double KoIndentSpacingWidget::spaceBeforeParag() const
{
    return TQMAX(0, eBefore->value() );
}

double KoIndentSpacingWidget::spaceAfterParag() const
{
    return TQMAX(0, eAfter->value() );
}

KoParagLayout::SpacingType KoIndentSpacingWidget::lineSpacingType() const
{
    int index = cSpacing->currentItem();
    switch ( index ) {
    case 0:
        return KoParagLayout::LS_SINGLE;
    case 1:
        return KoParagLayout::LS_ONEANDHALF;
    case 2:
        return KoParagLayout::LS_DOUBLE;
    case 3:
        return KoParagLayout::LS_MULTIPLE;
    case 4:
        return KoParagLayout::LS_CUSTOM;
    case 5:
        return KoParagLayout::LS_AT_LEAST;
    case 6:
        return KoParagLayout::LS_FIXED;
    default:
        kdError(32500) << "Error in KoIndentSpacingWidget::lineSpacingType" << endl;
        return KoParagLayout::LS_SINGLE;
    }
}

double KoIndentSpacingWidget::lineSpacing() const
{
    return (lineSpacingType() == KoParagLayout::LS_MULTIPLE)
                               ? TQMAX( 1, eSpacingPercent->value() ) / 100.0
                               : TQMAX( 0, eSpacing->value() );
}


void KoIndentSpacingWidget::display( const KoParagLayout & lay )
{
    double _left = lay.margins[TQStyleSheetItem::MarginLeft];
    eLeft->changeValue( _left );
    //prev1->setLeft( _left );  done by leftChanged() below
    leftChanged( _left ); // sets min value for eFirstLine

    double _right = lay.margins[TQStyleSheetItem::MarginRight];
    eRight->changeValue( _right );
    prev1->setRight( _right );

    double _first = lay.margins[TQStyleSheetItem::MarginFirstLine];
    eFirstLine->changeValue( _first );
    prev1->setFirst( _first );

    double _before = lay.margins[TQStyleSheetItem::MarginTop];
    eBefore->changeValue( _before );
    prev1->setBefore( _before );

    double _after = lay.margins[TQStyleSheetItem::MarginBottom];
    eAfter->changeValue( _after );
    prev1->setAfter( _after );

    double _spacing = lay.lineSpacingValue();
    KoParagLayout::SpacingType _type = lay.lineSpacingType;
    switch ( _type ) {
    case KoParagLayout::LS_SINGLE: // single
        cSpacing->setCurrentItem( 0 );
        break;
    case KoParagLayout::LS_ONEANDHALF:
        cSpacing->setCurrentItem( 1 );
        break;
    case KoParagLayout::LS_DOUBLE:
        cSpacing->setCurrentItem( 2 );
        break;
    case KoParagLayout::LS_MULTIPLE:
        cSpacing->setCurrentItem( 3 );
        break;
    case KoParagLayout::LS_CUSTOM:
        cSpacing->setCurrentItem( 4 );
        break;
    case KoParagLayout::LS_AT_LEAST:
        cSpacing->setCurrentItem( 5 );
        break;
    case KoParagLayout::LS_FIXED:
        cSpacing->setCurrentItem( 6 );
        break;
    default:
        cSpacing->setCurrentItem( 0 );
        break;
    }

    updateLineSpacing( _type );
    eSpacing->setValue( (_type == KoParagLayout::LS_MULTIPLE) ? TQMAX( 1, _spacing )
                        : KoUnit::toUserValue( _spacing, m_unit ) );
    eSpacingPercent->setValue( ( _type == KoParagLayout::LS_MULTIPLE ) ? tqRound( _spacing * 100 ) : 100 );
}

void KoIndentSpacingWidget::save( KoParagLayout & lay )
{
    lay.setLineSpacingValue(lineSpacing());
    lay.lineSpacingType = lineSpacingType();
    lay.margins[TQStyleSheetItem::MarginLeft] = leftIndent();
    lay.margins[TQStyleSheetItem::MarginRight] = rightIndent();
    lay.margins[TQStyleSheetItem::MarginFirstLine] = firstLineIndent();
    lay.margins[TQStyleSheetItem::MarginTop] = spaceBeforeParag();
    lay.margins[TQStyleSheetItem::MarginBottom] = spaceAfterParag();
}

TQString KoIndentSpacingWidget::tabName()
{
    return i18n( "Indent && S&pacing" );
}

void KoIndentSpacingWidget::leftChanged( double val )
{
    prev1->setLeft( val );
    // The minimum first-line margin is -leftMargin() (where leftMargin>=0)
    eFirstLine->setMinValue( -TQMAX( 0, val ) );
}

void KoIndentSpacingWidget::rightChanged( double val )
{
    prev1->setRight( val );
}

void KoIndentSpacingWidget::firstChanged( double val )
{
    prev1->setFirst( val );
}

void KoIndentSpacingWidget::updateLineSpacing( KoParagLayout::SpacingType _type )
{
    bool needsValue = (_type != KoParagLayout::LS_SINGLE &&
                       _type != KoParagLayout::LS_ONEANDHALF &&
                       _type != KoParagLayout::LS_DOUBLE);

    if ( _type == KoParagLayout::LS_MULTIPLE )
    {
        sSpacingStack->raiseWidget( eSpacingPercent );
    }
    else
    {
        sSpacingStack->raiseWidget( eSpacing );
    }
    eSpacing->setEnabled( needsValue );
    if ( needsValue )
        prev1->setSpacing( eSpacing->value() );
    else
    {
        prev1->setSpacing( _type == KoParagLayout::LS_ONEANDHALF ? 8 :
                           _type == KoParagLayout::LS_DOUBLE ? 16 :0 );
    }
}

void KoIndentSpacingWidget::spacingActivated( int /*_index*/ )
{
    updateLineSpacing( lineSpacingType() );
    if ( eSpacing->isEnabled() ) // i.e. needsValue = true
        eSpacing->setFocus();
}

void KoIndentSpacingWidget::spacingChanged( double _val )
{
    prev1->setSpacing( _val );
}

void KoIndentSpacingWidget::spacingChanged( int _val )
{
    prev1->setSpacing( _val / 100.0 );
}

void KoIndentSpacingWidget::beforeChanged( double _val )
{
    prev1->setBefore( KoUnit::fromUserValue( _val, m_unit ) );
}

void KoIndentSpacingWidget::afterChanged( double _val )
{
    prev1->setAfter( KoUnit::fromUserValue( _val, m_unit ) );
}


KoParagAlignWidget::KoParagAlignWidget( bool breakLine, TQWidget * parent, const char * name )
        : KoParagLayoutWidget( KoParagDia::PD_ALIGN, parent, name )
{
    TQGridLayout *grid = new TQGridLayout( this, 3, 2, KDialog::marginHint(), KDialog::spacingHint() );

    TQVGroupBox * AlignGroup = new TQVGroupBox( i18n( "Alignment" ), this );

    rLeft = new TQRadioButton( i18n( "&Left" ), AlignGroup );
    connect( rLeft, TQT_SIGNAL( clicked() ), this, TQT_SLOT( alignLeft() ) );

    rCenter = new TQRadioButton( i18n( "C&enter" ), AlignGroup );
    connect( rCenter, TQT_SIGNAL( clicked() ), this, TQT_SLOT( alignCenter() ) );

    rRight = new TQRadioButton( i18n( "&Right" ), AlignGroup );
    connect( rRight, TQT_SIGNAL( clicked() ), this, TQT_SLOT( alignRight() ) );

    rJustify = new TQRadioButton( i18n( "&Justify" ), AlignGroup );
    connect( rJustify, TQT_SIGNAL( clicked() ), this, TQT_SLOT( alignJustify() ) );

    clearAligns();
    rLeft->setChecked( true );

    grid->addWidget(AlignGroup, 0, 0);

    // --------------- End of page /frame ---------------
    TQGroupBox * endFramePage = new TQGroupBox( i18n( "Behavior at &End of Frame/Page" ), this );
    TQGridLayout * endFramePageGrid = new TQGridLayout( endFramePage, 4, 1,
                                                      KDialog::marginHint(), KDialog::spacingHint() );

    cKeepLinesTogether = new TQCheckBox( i18n("&Keep lines together"),endFramePage);
    endFramePageGrid->addWidget( cKeepLinesTogether, 1, 0 );
    cHardBreakBefore = new TQCheckBox( i18n("Insert break before paragraph"),endFramePage);
    endFramePageGrid->addWidget( cHardBreakBefore, 2, 0 );
    cHardBreakAfter = new TQCheckBox( i18n("Insert break after paragraph"),endFramePage);
    endFramePageGrid->addWidget( cHardBreakAfter, 3, 0 );

    endFramePageGrid->addRowSpacing( 0, fontMetrics().height() / 2 ); // groupbox title
    for ( int i = 0 ; i < endFramePageGrid->numRows()-1 ; ++i )
        endFramePageGrid->setRowStretch( 0, 0 );
    endFramePageGrid->setRowStretch( endFramePageGrid->numRows()-1, 1 );
    grid->addWidget( endFramePage, 2, 0 );

    endFramePage->setEnabled(breakLine);

    // --------------- preview --------------------
    prev2 = new KPagePreview2( this, "KPagePreview2" );
    grid->addMultiCellWidget( prev2, 0, 2, 1, 1 );

    // --------------- main grid ------------------
    grid->setColStretch( 1, 1 );
    grid->setRowStretch( 1, 1 );
}

int KoParagAlignWidget::pageBreaking() const
{
    int pb = 0;
    if ( cKeepLinesTogether->isChecked() )
        pb |= KoParagLayout::KeepLinesTogether;
    if ( cHardBreakBefore->isChecked() )
        pb |= KoParagLayout::HardFrameBreakBefore;
    if ( cHardBreakAfter->isChecked() )
        pb |= KoParagLayout::HardFrameBreakAfter;
    return pb;
}


void KoParagAlignWidget::display( const KoParagLayout & lay )
{
    int align = lay.alignment;
    prev2->setAlign( align );

    clearAligns();
    switch ( align ) {
#ifndef USE_QT4
        case TQt::AlignAuto: // see KoView::setAlign
#endif // USE_QT4
        case TQt::AlignLeft:
            rLeft->setChecked( true );
            break;
        case TQt::AlignHCenter:
            rCenter->setChecked( true );
            break;
        case TQt::AlignRight:
            rRight->setChecked( true );
            break;
        case TQt::AlignJustify:
            rJustify->setChecked( true );
    }

    cKeepLinesTogether->setChecked( lay.pageBreaking & KoParagLayout::KeepLinesTogether );
    cHardBreakBefore->setChecked( lay.pageBreaking & KoParagLayout::HardFrameBreakBefore );
    cHardBreakAfter->setChecked( lay.pageBreaking & KoParagLayout::HardFrameBreakAfter );
    // ## preview support for end-of-frame ?
}

void KoParagAlignWidget::save( KoParagLayout & lay )
{
    lay.alignment = align();
    lay.pageBreaking = pageBreaking();
}

int KoParagAlignWidget::align() const
{
    if ( rLeft->isChecked() ) return TQt::AlignLeft;
    else if ( rCenter->isChecked() ) return TQt::AlignHCenter;
    else if ( rRight->isChecked() ) return TQt::AlignRight;
    else if ( rJustify->isChecked() ) return TQt::AlignJustify;

    return TQt::AlignLeft;
}

TQString KoParagAlignWidget::tabName()
{
    return i18n( "General &Layout" );
}

void KoParagAlignWidget::alignLeft()
{
    prev2->setAlign( TQt::AlignLeft );
    clearAligns();
    rLeft->setChecked( true );
}

void KoParagAlignWidget::alignCenter()
{
    prev2->setAlign( TQt::AlignHCenter );
    clearAligns();
    rCenter->setChecked( true );
}

void KoParagAlignWidget::alignRight()
{
    prev2->setAlign( TQt::AlignRight );
    clearAligns();
    rRight->setChecked( true );
}

void KoParagAlignWidget::alignJustify()
{
    prev2->setAlign( TQt::AlignJustify );
    clearAligns();
    rJustify->setChecked( true );
}

void KoParagAlignWidget::clearAligns()
{
    rLeft->setChecked( false );
    rCenter->setChecked( false );
    rRight->setChecked( false );
    rJustify->setChecked( false );
}

////////////////////////////////////////////////////////////////////////////////

KoParagDecorationWidget::KoParagDecorationWidget( TQWidget * parent,
                                                    const char * name )
    : KoParagLayoutWidget( KoParagDia::PD_DECORATION, parent, name )
{
    TQVBoxLayout *tabLayout = new TQVBoxLayout( this );
    wDeco = new KoParagDecorationTab( this );
    tabLayout->add( wDeco );

    // Set up Border Style combo box
    wDeco->cbBorderStyle->insertItem( KoBorder::getStyle( KoBorder::SOLID ) );
    wDeco->cbBorderStyle->insertItem( KoBorder::getStyle( KoBorder::DASH ) );
    wDeco->cbBorderStyle->insertItem( KoBorder::getStyle( KoBorder::DOT ) );
    wDeco->cbBorderStyle->insertItem( KoBorder::getStyle( KoBorder::DASH_DOT ) );
    wDeco->cbBorderStyle->insertItem( KoBorder::getStyle( KoBorder::DASH_DOT_DOT ) );
    wDeco->cbBorderStyle->insertItem( KoBorder::getStyle( KoBorder::DOUBLE_LINE  ) );

    // Set up Border Width combo box
    for( unsigned int i = 1; i <= 10; i++ )
        wDeco->cbBorderWidth->insertItem(TQString::number(i));

    // Setup the border toggle buttons, and merge checkbox
    connect( wDeco->bBorderLeft, TQT_SIGNAL( toggled( bool ) ),
             this, TQT_SLOT( brdLeftToggled( bool ) ) );
    connect( wDeco->bBorderRight, TQT_SIGNAL( toggled( bool ) ),
             this, TQT_SLOT( brdRightToggled( bool ) ) );
    connect( wDeco->bBorderTop, TQT_SIGNAL( toggled( bool ) ),
             this, TQT_SLOT( brdTopToggled( bool ) ) );
    connect( wDeco->bBorderBottom, TQT_SIGNAL( toggled( bool ) ),
             this, TQT_SLOT( brdBottomToggled( bool ) ) );
    connect( wDeco->cbJoinBorder, TQT_SIGNAL( toggled( bool ) ),
             this, TQT_SLOT( brdJoinToggled( bool ) ) );

    // Set up Border preview widget
    wPreview  = new KoBorderPreview( wDeco->borderPreview );
    TQVBoxLayout *previewLayout = new TQVBoxLayout( wDeco->borderPreview );
    previewLayout->addWidget( wPreview );
    connect( wPreview, TQT_SIGNAL( choosearea(TQMouseEvent * ) ),
             this, TQT_SLOT( slotPressEvent(TQMouseEvent *) ) );
}

///////
// Current GUI selections
KoBorder::BorderStyle KoParagDecorationWidget::curBorderStyle() const
{
    TQString selection = wDeco->cbBorderStyle->currentText();
    return KoBorder::getStyle( selection );
}

unsigned int KoParagDecorationWidget::curBorderWidth() const {
    return wDeco->cbBorderWidth->currentText().toUInt();
}

TQColor KoParagDecorationWidget::curBorderColor() const {
    return wDeco->bBorderColor->color();
}
///////

// Check whether a border is the same as that selected in the GUI
bool KoParagDecorationWidget::borderChanged( const KoBorder& border ) {
    return (unsigned int)border.penWidth() != curBorderWidth() ||
           border.color != curBorderColor() ||
           border.getStyle() != curBorderStyle();
}

// Set a given border according to the values selected in the GUI
void KoParagDecorationWidget::updateBorder( KoBorder& border )
{
    border.setPenWidth( curBorderWidth() );
    border.color = curBorderColor();
    border.setStyle( curBorderStyle () );
}

void KoParagDecorationWidget::clickedBorderPreview( KoBorder& border,
                                                     KoBorder::BorderType position,
                                                     KPushButton *corresponding )
{
    if ( borderChanged( border ) && corresponding->isOn() ) {
        updateBorder( border );
        wPreview->setBorder( position, border );
    }
    else
        corresponding->setOn( !corresponding->isOn() );
}

// Establish which border position was clicked in the border preview,
// and update the appropriate border
void KoParagDecorationWidget::slotPressEvent(TQMouseEvent *_ev)
{
    const int OFFSETX = 15;
    const int OFFSETY = 7;
    const int Ko_SPACE = 30;

    TQRect r = wPreview->contentsRect();
    TQRect rect(r.x() + OFFSETX, r.y() + OFFSETY,
               r.width() - OFFSETX, r.y() + OFFSETY + Ko_SPACE);
    if(rect.contains(TQPoint(_ev->x(),_ev->y())))
    {
        clickedBorderPreview( m_topBorder, KoBorder::TopBorder,
                              wDeco->bBorderTop );
    }

    rect.setCoords(r.x() + OFFSETX, r.height() - OFFSETY - Ko_SPACE,
                   r.width() - OFFSETX, r.height() - OFFSETY);
    if(rect.contains(TQPoint(_ev->x(),_ev->y())))
    {
        clickedBorderPreview( m_bottomBorder, KoBorder::BottomBorder,
                              wDeco->bBorderBottom );
    }

    rect.setCoords(r.x() + OFFSETX, r.y() + OFFSETY,
                   r.x() + Ko_SPACE + OFFSETX, r.height() - OFFSETY);
    if(rect.contains(TQPoint(_ev->x(),_ev->y())))
    {
        clickedBorderPreview( m_leftBorder, KoBorder::LeftBorder,
                              wDeco->bBorderLeft );
    }

    rect.setCoords(r.width() - OFFSETX - Ko_SPACE, r.y() + OFFSETY,
                   r.width() - OFFSETX, r.height() - OFFSETY);
    if(rect.contains(TQPoint(_ev->x(),_ev->y())))
    {
        clickedBorderPreview( m_rightBorder, KoBorder::RightBorder,
                              wDeco->bBorderRight );
    }
}

void KoParagDecorationWidget::display( const KoParagLayout & lay )
{
    wDeco->bBackgroundColor->setColor( lay.backgroundColor );

    m_leftBorder = lay.leftBorder;
    m_rightBorder = lay.rightBorder;
    m_topBorder = lay.topBorder;
    m_bottomBorder = lay.bottomBorder;
    m_joinBorder = lay.joinBorder;

    wDeco->bBorderLeft->blockSignals( true );
    wDeco->bBorderRight->blockSignals( true );
    wDeco->bBorderTop->blockSignals( true );
    wDeco->bBorderBottom->blockSignals( true );
    updateBorders();
    wDeco->bBorderLeft->blockSignals( false );
    wDeco->bBorderRight->blockSignals( false );
    wDeco->bBorderTop->blockSignals( false );
    wDeco->bBorderBottom->blockSignals( false );
}

void KoParagDecorationWidget::updateBorders()
{
    wDeco->bBorderLeft->setOn( m_leftBorder.penWidth() > 0 );
    wDeco->bBorderRight->setOn( m_rightBorder.penWidth() > 0 );
    wDeco->bBorderTop->setOn( m_topBorder.penWidth() > 0 );
    wDeco->bBorderBottom->setOn( m_bottomBorder.penWidth() > 0 );
    wDeco->cbJoinBorder->setChecked( m_joinBorder );
    wPreview->setLeftBorder( m_leftBorder );
    wPreview->setRightBorder( m_rightBorder );
    wPreview->setTopBorder( m_topBorder );
    wPreview->setBottomBorder( m_bottomBorder );
}


void KoParagDecorationWidget::save( KoParagLayout & lay )
{
    lay.backgroundColor = wDeco->bBackgroundColor->color();
    lay.topBorder = m_topBorder;
    lay.bottomBorder = m_bottomBorder;
    lay.leftBorder = m_leftBorder;
    lay.rightBorder = m_rightBorder;
    lay.joinBorder = m_joinBorder;
}

TQColor KoParagDecorationWidget::backgroundColor() const {
    return wDeco->bBackgroundColor->color();
}

TQString KoParagDecorationWidget::tabName() {
    // Why D&e..?  Because &De.. conflicts with &Delete in
    // the style manager.
    return i18n( "D&ecorations" );
}

void KoParagDecorationWidget::brdLeftToggled( bool _on )
{
    if ( !_on )
        m_leftBorder.setPenWidth(0);
    else {
        m_leftBorder.setPenWidth( curBorderWidth() );
        m_leftBorder.color = curBorderColor();
        m_leftBorder.setStyle( curBorderStyle() );
    }
    wPreview->setLeftBorder( m_leftBorder );
}

void KoParagDecorationWidget::brdRightToggled( bool _on )
{
    if ( !_on )
        m_rightBorder.setPenWidth(0);
    else {
        m_rightBorder.setPenWidth( curBorderWidth() );
        m_rightBorder.color = curBorderColor();
        m_rightBorder.setStyle( curBorderStyle() );
    }
    wPreview->setRightBorder( m_rightBorder );
}

void KoParagDecorationWidget::brdTopToggled( bool _on )
{
    if ( !_on )
        m_topBorder.setPenWidth(0);
    else {
        m_topBorder.setPenWidth( curBorderWidth() );
        m_topBorder.color = curBorderColor();
        m_topBorder.setStyle( curBorderStyle() );
    }
    wPreview->setTopBorder( m_topBorder );
}

void KoParagDecorationWidget::brdBottomToggled( bool _on )
{
    if ( !_on )
        m_bottomBorder.setPenWidth ( 0 );
    else {
        m_bottomBorder.setPenWidth( curBorderWidth() );
        m_bottomBorder.color = curBorderColor();
        m_bottomBorder.setStyle( curBorderStyle() );
    }
    wPreview->setBottomBorder( m_bottomBorder );
}

void KoParagDecorationWidget::brdJoinToggled( bool _on ) {
    m_joinBorder = _on;
}
////////////////////////////////////////////////////////////////////////////////


KoParagCounterWidget::KoParagCounterWidget( bool disableAll, TQWidget * parent, const char * name )
    : KoParagLayoutWidget( KoParagDia::PD_NUMBERING, parent, name )
{

    TQVBoxLayout *Form1Layout = new TQVBoxLayout( this );
    Form1Layout->setSpacing( KDialog::spacingHint() );
    Form1Layout->setMargin( KDialog::marginHint() );

    gNumbering = new TQButtonGroup( this, "numberingGroup" );
    gNumbering->setTitle( i18n( "Numbering" ) );
    gNumbering->setColumnLayout(0, Qt::Vertical );
    gNumbering->layout()->setSpacing( 0 );
    gNumbering->layout()->setMargin( 0 );
    TQHBoxLayout *numberingGroupLayout = new TQHBoxLayout( gNumbering->layout() );
    numberingGroupLayout->setAlignment( TQt::AlignTop );
    numberingGroupLayout->setSpacing( KDialog::spacingHint() );
    numberingGroupLayout->setMargin( KDialog::marginHint() );

    // What type of numbering is required?
    TQRadioButton *rNone = new TQRadioButton( gNumbering, "rNone" );
    rNone->setText( i18n( "&None" ) );
    numberingGroupLayout->addWidget( rNone );

    gNumbering->insert( rNone , KoParagCounter::NUM_NONE);

    TQRadioButton *rList = new TQRadioButton( gNumbering, "rList" );
    rList->setText( i18n( "&List" ) );
    gNumbering->insert( rList , KoParagCounter::NUM_LIST);
    numberingGroupLayout->addWidget( rList );

    TQRadioButton *rChapter = new TQRadioButton( gNumbering, "rChapter" );
    rChapter->setText( i18n( "Chapt&er" ) );
    gNumbering->insert( rChapter , KoParagCounter::NUM_CHAPTER);
    numberingGroupLayout->addWidget( rChapter );
    Form1Layout->addWidget( gNumbering );
    connect( gNumbering, TQT_SIGNAL( clicked( int ) ), this, TQT_SLOT( numTypeChanged( int ) ) );

    m_styleWidget = new KoCounterStyleWidget( true, false, disableAll, this );

    connect( m_styleWidget, TQT_SIGNAL( sig_suffixChanged (const TQString &) ), this, TQT_SLOT( suffixChanged(const TQString &) ) );
    connect( m_styleWidget, TQT_SIGNAL( sig_prefixChanged (const TQString &) ), this, TQT_SLOT( prefixChanged(const TQString &) ) );
    connect( m_styleWidget, TQT_SIGNAL( sig_startChanged(int) ), this, TQT_SLOT( startChanged(int) ) );
    connect( m_styleWidget, TQT_SIGNAL( sig_restartChanged(bool) ), this, TQT_SLOT( restartChanged(bool) ) );
    connect( m_styleWidget, TQT_SIGNAL( sig_depthChanged (int) ), this, TQT_SLOT( depthChanged(int) ) );
    connect( m_styleWidget, TQT_SIGNAL( sig_displayLevelsChanged (int) ), this, TQT_SLOT( displayLevelsChanged(int) ) );
    connect( m_styleWidget, TQT_SIGNAL( sig_alignmentChanged (int) ), this, TQT_SLOT( alignmentChanged(int) ) );
    connect( m_styleWidget, TQT_SIGNAL( changeCustomBullet( const TQString & , TQChar ) ), this, TQT_SLOT( slotChangeCustomBullet( const TQString & , TQChar ) ) );

    connect( m_styleWidget, TQT_SIGNAL( sig_numTypeChanged( int ) ), this, TQT_SLOT( numTypeChanged(int ) ) );
    connect( m_styleWidget, TQT_SIGNAL( changeStyle( KoParagCounter::Style ) ), this, TQT_SLOT( styleChanged (KoParagCounter::Style ) ) );

    Form1Layout->addWidget( m_styleWidget );


    preview = new KoStylePreview( i18n( "Preview" ), i18n("Normal paragraph text"), this, "counter preview" );
    Form1Layout->addWidget( preview );
    if ( disableAll)
    {
        gNumbering->setEnabled( false);
        preview->setEnabled( false );
    }

    TQSpacerItem* spacer = new TQSpacerItem( 0, 0, TQSizePolicy::Minimum, TQSizePolicy::Expanding);
    Form1Layout->addItem( spacer );
}

void KoParagCounterWidget::styleChanged( KoParagCounter::Style st )
{
    m_counter.setStyle( st );
    updatePreview();
}

void KoParagCounterWidget::slotChangeCustomBullet( const TQString & f, TQChar c)
{
    m_counter.setCustomBulletFont( f );
    m_counter.setCustomBulletCharacter( c );
    preview->setCounter( m_counter );
}

TQString KoParagCounterWidget::tabName() {
    return i18n( "B&ullets/Numbers" );
}

void KoParagCounterWidget::numTypeChanged( int nType ) {
    // radio buttons pressed to change numbering type
    m_counter.setNumbering( static_cast<KoParagCounter::Numbering>( nType ) );
    preview->setEnabled( m_counter.numbering() != KoParagCounter::NUM_NONE );
    m_styleWidget->numTypeChanged( nType );

    updatePreview();
}

void KoParagCounterWidget::display( const KoParagLayout & lay ) {
    KoParagCounter::Style style = KoParagCounter::STYLE_NONE;
    if ( lay.counter )
    {
        style=lay.counter->style();
        m_counter = *lay.counter;
    }
    else
    {
        m_counter = KoParagCounter();
    }
    gNumbering->setButton( m_counter.numbering() );
    preview->setStyle( lay.style );
    preview->setCounter( m_counter );
    m_styleWidget->display( lay );
}

void KoParagCounterWidget::updatePreview() {
    preview->setCounter(m_counter);
    preview->repaint(true);
}

void KoParagCounterWidget::save( KoParagLayout & lay ) {
/*    m_counter.setDepth(spnDepth->value());
    m_counter.setStartNumber(spnStart->value());
    m_counter.setPrefix(sPrefix->text());
    m_counter.setSuffix(sSuffix->text()); */

    if ( lay.counter )
        *lay.counter = m_counter;
    else
        lay.counter = new KoParagCounter( m_counter );
}

KoTabulatorsLineEdit::KoTabulatorsLineEdit( TQWidget *parent, double lower, double upper, double step, double value /*= 0.0*/, KoUnit::Unit unit /*= KoUnit::U_PT*/, unsigned int precision /*= 2*/, const char *name /*= 0*/ )
    : KoUnitDoubleSpinBox ( parent, lower, upper, step, value, unit, precision, name )
{
    setRange( 0, 9999, 1, false);
}

void KoTabulatorsLineEdit::keyPressEvent ( TQKeyEvent *ke )
{
    if( ke->key()  == Qt::Key_Return ||
        ke->key()  == Qt::Key_Enter )
    {
        emit keyReturnPressed();
        return;
    }
    KoUnitDoubleSpinBox::keyPressEvent (ke);
}

KoParagTabulatorsWidget::KoParagTabulatorsWidget( KoUnit::Unit unit, double frameWidth,TQWidget * parent, const char * name )
    : KoParagLayoutWidget( KoParagDia::PD_TABS, parent, name ), m_unit(unit) {
    TQString length;
    if(frameWidth==-1) {
        frameWidth=9999;
        m_toplimit=9999;
    } else {
        m_toplimit=frameWidth;
        length=i18n("Frame width: %1 %2")
		.arg(KoUnit::toUserStringValue(frameWidth,m_unit))
		.arg(KoUnit::unitName(m_unit));
        frameWidth=KoUnit::toUserValue(frameWidth,m_unit);
    }
    TQVBoxLayout* Form1Layout = new TQVBoxLayout( this );
    Form1Layout->setSpacing( KDialog::spacingHint() );
    Form1Layout->setMargin( KDialog::marginHint() );

    TQHBoxLayout* Layout13 = new TQHBoxLayout;
    Layout13->setSpacing( KDialog::spacingHint() );
    Layout13->setMargin( 0 ); //?

    lstTabs = new TQListBox( this);
    lstTabs->insertItem( "mytabvalue" );
    lstTabs->setMaximumSize( TQSize( 300, 32767 ) );
    Layout13->addWidget( lstTabs );

    editLayout = new TQVBoxLayout;
    editLayout->setSpacing( KDialog::spacingHint() );
    editLayout->setMargin( 0 ); //?

    gPosition = new TQGroupBox( this, "gPosition" );
    gPosition->setTitle( i18n( "Po&sition" ) );
    gPosition->setColumnLayout(0, Qt::Vertical );
    gPosition->layout()->setSpacing( 0 );
    gPosition->layout()->setMargin( 0 );
    TQVBoxLayout* GroupBox2Layout = new TQVBoxLayout( gPosition->layout() );
    GroupBox2Layout->setAlignment( TQt::AlignTop );
    GroupBox2Layout->setSpacing( KDialog::spacingHint() );
    GroupBox2Layout->setMargin( KDialog::marginHint() );

    TQHBoxLayout* Layout5 = new TQHBoxLayout;
    Layout5->setSpacing( KDialog::spacingHint() );
    Layout5->setMargin( 0 ); //?

    sTabPos = new KoTabulatorsLineEdit( gPosition, 0, 9999, 1, 0.0, m_unit );
    sTabPos->setRange( 0, 9999, 1 );
    sTabPos->setMaximumSize( TQSize( 100, 32767 ) );
    Layout5->addWidget( sTabPos );
    TQSpacerItem* spacer = new TQSpacerItem( 20, 20, TQSizePolicy::Expanding, TQSizePolicy::Minimum );
    Layout5->addItem( spacer );
    GroupBox2Layout->addLayout( Layout5 );
    editLayout->addWidget( gPosition );

    TQLabel* TextLabel1 = new TQLabel( gPosition );
    TQString unitDescription = KoUnit::unitDescription( m_unit );
    TextLabel1->setText( length );
    GroupBox2Layout->addWidget( TextLabel1 );

    bgAlign = new TQButtonGroup( this );
    bgAlign->setTitle( i18n( "Alignment" ) );
    bgAlign->setColumnLayout(0, Qt::Vertical );
    bgAlign->layout()->setSpacing( 0 );
    bgAlign->layout()->setMargin( 0 );
    TQVBoxLayout* ButtonGroup1Layout = new TQVBoxLayout( bgAlign->layout() );
    ButtonGroup1Layout->setAlignment( TQt::AlignTop );
    ButtonGroup1Layout->setSpacing( KDialog::spacingHint() );
    ButtonGroup1Layout->setMargin( KDialog::marginHint() );

    rAlignLeft = new TQRadioButton( bgAlign );
    rAlignLeft->setText( i18n( "&Left" ) );
    ButtonGroup1Layout->addWidget( rAlignLeft );

    rAlignCentre = new TQRadioButton( bgAlign );
    rAlignCentre->setText( i18n( "C&enter" ) );
    ButtonGroup1Layout->addWidget( rAlignCentre );

    rAlignRight = new TQRadioButton( bgAlign );
    rAlignRight->setText( i18n( "&Right" ) );
    ButtonGroup1Layout->addWidget( rAlignRight );

    TQHBoxLayout* Layout8 = new TQHBoxLayout;
    Layout8->setSpacing( KDialog::spacingHint() );
    Layout8->setMargin( 0 );

    rAlignVar = new TQRadioButton( bgAlign );
    rAlignVar->setText( i18n( "On followin&g character: " ) );
    Layout8->addWidget( rAlignVar );

    sAlignChar = new TQLineEdit( bgAlign);
    sAlignChar->setMaximumSize( TQSize( 60, 32767 ) );
    sAlignChar->setText(TQString(TDEGlobal::locale()->decimalSymbol()[0]));
    Layout8->addWidget( sAlignChar );
    TQSpacerItem* spacer_2 = new TQSpacerItem( 20, 20, TQSizePolicy::Expanding, TQSizePolicy::Minimum );
    Layout8->addItem( spacer_2 );
    ButtonGroup1Layout->addLayout( Layout8 );
    editLayout->addWidget( bgAlign );

    gTabLeader = new TQGroupBox( this, "gTabLeader" );
    gTabLeader->setTitle( i18n( "Tab Leader" ) );
    TQVBoxLayout* GroupBox5Layout = new TQVBoxLayout( gTabLeader );
    GroupBox5Layout->setAlignment( TQt::AlignTop );
    GroupBox5Layout->setSpacing( KDialog::spacingHint() );
    GroupBox5Layout->setMargin( KDialog::marginHint() );
    GroupBox5Layout->addSpacing( fontMetrics().height() / 2 ); // groupbox title

    TQLabel* TextLabel1_2 = new TQLabel( gTabLeader );
    TextLabel1_2->setText( i18n( "The space a tab uses can be filled with a pattern." ) );
    GroupBox5Layout->addWidget( TextLabel1_2 );

    TQGridLayout *fillingGrid = new TQGridLayout( 0L, 2, 2, 0, KDialog::spacingHint() );

    TQLabel* TextLabel2 = new TQLabel( gTabLeader);
    TextLabel2->setText( i18n( "&Filling:" ) );
    TextLabel2->setAlignment( AlignRight );
    fillingGrid->addWidget( TextLabel2, 0, 0 );

    cFilling = new TQComboBox( FALSE, gTabLeader);
    cFilling->insertItem( i18n( "Blank" ) );
    cFilling->insertItem( "_ _ _ _ _ _"); // DOT
    cFilling->insertItem( "_________");   // SOLID
    cFilling->insertItem( "___ ___ __");  // DASH
    cFilling->insertItem( "___ _ ___ _"); // DASH_DOT
    cFilling->insertItem( "___ _ _ ___"); // DASH_DOT_DOT
    TextLabel2->setBuddy( cFilling );
    fillingGrid->addWidget( cFilling, 0, 1 );

    TQLabel * TextLabel3 = new TQLabel( i18n("&Width:"), gTabLeader );
    TextLabel3->setAlignment( AlignRight );
    fillingGrid->addWidget( TextLabel3, 1, 0 );

    eWidth = new KoUnitDoubleSpinBox( gTabLeader );
    eWidth->setMinValue(0.01);
    eWidth->setUnit( m_unit );
    TextLabel3->setBuddy( eWidth );
    fillingGrid->addWidget( eWidth, 1, 1 );

    GroupBox5Layout->addLayout( fillingGrid );
    editLayout->addWidget( gTabLeader );
    TQSpacerItem* spacer_4 = new TQSpacerItem( 20, 20, TQSizePolicy::Minimum, TQSizePolicy::Expanding );
    editLayout->addItem( spacer_4 );
    Layout13->addLayout( editLayout );
    Form1Layout->addLayout( Layout13 );

    TQHBoxLayout* Layout4 = new TQHBoxLayout;
    Layout4->setSpacing( KDialog::spacingHint() );
    Layout4->setMargin( 0 );

    bNew = new TQPushButton( this);
    bNew->setText( i18n( "&New" ) );
    Layout4->addWidget( bNew );

    bDelete = new TQPushButton( this);
    bDelete->setText( i18n( "&Delete" ) );
    Layout4->addWidget( bDelete );

    bDeleteAll = new TQPushButton( this);
    bDeleteAll->setText( i18n( "Delete All" ) );
    Layout4->addWidget( bDeleteAll );

    TQSpacerItem* spacer_5 = new TQSpacerItem( 20, 20, TQSizePolicy::Expanding, TQSizePolicy::Minimum );
    Layout4->addItem( spacer_5 );
    Form1Layout->addLayout( Layout4 );

    //signal valueChanged passes value which the user see (unlike the value() function)
    //so fromUserValue has to be used in slotTabValueChanged
    connect(sTabPos,TQT_SIGNAL(valueChanged(double)), this, TQT_SLOT(slotTabValueChanged(double )));
    connect(sTabPos,TQT_SIGNAL( keyReturnPressed()),this,TQT_SLOT(newClicked()));
    connect(sAlignChar,TQT_SIGNAL(textChanged( const TQString & )), this, TQT_SLOT(slotAlignCharChanged( const TQString & )));
    connect(bNew,TQT_SIGNAL(clicked ()),this,TQT_SLOT(newClicked()));
    connect(bDelete,TQT_SIGNAL(clicked ()),this,TQT_SLOT(deleteClicked()));
    connect(bDeleteAll,TQT_SIGNAL(clicked ()),this,TQT_SLOT(deleteAllClicked()));
    connect(bgAlign,TQT_SIGNAL(clicked (int)),this,TQT_SLOT(updateAlign(int)));
    connect(cFilling,TQT_SIGNAL(activated (int)),this,TQT_SLOT(updateFilling(int)));
    connect(eWidth,TQT_SIGNAL(valueChangedPt( double ) ),this,TQT_SLOT(updateWidth()));
    connect(lstTabs,TQT_SIGNAL(highlighted (int)),this,TQT_SLOT(setActiveItem(int)));
    noSignals=false;
}

void KoParagTabulatorsWidget::slotTabValueChanged( double val ) {
    if(noSignals) return;
    noSignals=true;
    //see comment where this slot is connected
    m_tabList[lstTabs->currentItem()].ptPos = KoUnit::fromUserValue( val, m_unit );

    lstTabs->changeItem(tabToString(m_tabList[lstTabs->currentItem()]), lstTabs->currentItem());

    sortLists();
    noSignals=false;
}

void KoParagTabulatorsWidget::slotAlignCharChanged( const TQString &/*_text*/ ) {
    // select align 3 and update data structures.
    bgAlign->setButton(3);
    m_tabList[lstTabs->currentItem()].alignChar=sAlignChar->text()[0];
    m_tabList[lstTabs->currentItem()].type=T_DEC_PNT;
}

void KoParagTabulatorsWidget::newClicked() {
    int selected=lstTabs->currentItem();
    KoTabulator newTab;
    if(selected < 0) {
        newTab.ptPos=0;
        newTab.type=T_LEFT;
        newTab.filling=TF_BLANK;
        newTab.ptWidth=0.5;
        m_tabList.append(newTab);
        lstTabs->insertItem(tabToString(newTab));
        lstTabs->setCurrentItem(0);
    } else {
        double pos = m_tabList[selected].ptPos;
        double add=1.0;
        if(m_unit==KoUnit::U_INCH) // inches are 25 times as big as mm, take it easy with adding..
            add=0.1;

        pos=pos + KoUnit::fromUserValue( add, m_unit );
        if(pos<m_toplimit)
        {
            newTab.ptPos=pos + KoUnit::fromUserValue( add, m_unit );
            newTab.type=m_tabList[selected].type;
            newTab.filling=m_tabList[selected].filling;
            newTab.ptWidth=m_tabList[selected].ptWidth;
            m_tabList.insert(m_tabList.at(selected), newTab);
            lstTabs->insertItem( tabToString(newTab), selected);
            lstTabs->setCurrentItem(lstTabs->findItem(tabToString(newTab)));
            sortLists();
        }
    }
}

void KoParagTabulatorsWidget::deleteClicked() {
    int selected = lstTabs->currentItem();
    if (selected < 0) return;
    noSignals=true;
    sTabPos->changeValue(0.0);
    noSignals=false;
    lstTabs->removeItem(selected);
    m_tabList.remove(m_tabList[selected]);
    if(lstTabs->count() >0) {
        lstTabs->setCurrentItem(TQMIN(static_cast<unsigned int>(selected), lstTabs->count()-1 ));
    } else {
        bDeleteAll->setEnabled(false);
        bDelete->setEnabled(false);
        gPosition->setEnabled(false);
        bgAlign->setEnabled(false);
        gTabLeader->setEnabled(false);
    }
}

void KoParagTabulatorsWidget::deleteAllClicked()
{
    noSignals=true;
    sTabPos->changeValue(0.0);
    noSignals=false;
    lstTabs->clear();
    m_tabList.clear();
    bDeleteAll->setEnabled(false);
    bDelete->setEnabled(false);
    gPosition->setEnabled(false);
    bgAlign->setEnabled(false);
    gTabLeader->setEnabled(false);
}

void KoParagTabulatorsWidget::setActiveItem(int selected) {
    if(noSignals) return;
    if(selected < 0) return;
    noSignals=true;
    KoTabulator *selectedTab = &m_tabList[selected];
    switch( selectedTab->type) {
        case T_CENTER:
            bgAlign->setButton(1); break;
        case  T_RIGHT:
            bgAlign->setButton(2); break;
        case T_DEC_PNT:
            bgAlign->setButton(3);
	    sAlignChar->setText(TQString(selectedTab->alignChar));
	    break;
        case T_LEFT:
        default:
            bgAlign->setButton(0);
    }
    switch( selectedTab->filling) {
        case TF_DOTS:
            cFilling->setCurrentItem(1); break;
        case TF_LINE:
            cFilling->setCurrentItem(2); break;
        case TF_DASH:
            cFilling->setCurrentItem(3); break;
        case TF_DASH_DOT:
            cFilling->setCurrentItem(4); break;
        case TF_DASH_DOT_DOT:
            cFilling->setCurrentItem(5); break;
        case TF_BLANK:
        default:
            cFilling->setCurrentItem(0);
    }
    eWidth->changeValue( selectedTab->ptWidth );
    sTabPos->setValue( KoUnit::toUserValue(selectedTab->ptPos, m_unit));
    bDelete->setEnabled(true);
    bDeleteAll->setEnabled(true);
    gPosition->setEnabled(true);
    bgAlign->setEnabled(true);
    gTabLeader->setEnabled(true);
    noSignals=false;
}

void KoParagTabulatorsWidget::setCurrentTab( double tabPos ) {
    KoTabulatorList::ConstIterator it = m_tabList.begin();
    for ( int i = 0; it != m_tabList.end(); ++it, ++i )
        if ( (*it).ptPos == tabPos ) {
            lstTabs->setCurrentItem(i);
            setActiveItem( i );
            return;
        }
    kdWarning() << "KoParagTabulatorsWidget::setCurrentTab: no tab found at pos=" << tabPos << endl;
}

TQString KoParagTabulatorsWidget::tabToString(const KoTabulator &tab) {
    return KoUnit::toUserStringValue( tab.ptPos, m_unit);
}

void KoParagTabulatorsWidget::updateAlign(int selected) {
    KoTabulator *selectedTab = &m_tabList[lstTabs->currentItem()];

    switch( selected) {
        case 1:
            selectedTab->type=T_CENTER; break;
        case  2:
            selectedTab->type=T_RIGHT; break;
        case 3:
            selectedTab->type=T_DEC_PNT;
	    selectedTab->alignChar=sAlignChar->text()[0];
	    break;
        case 0:
        default:
            selectedTab->type=T_LEFT;
    }
}

void KoParagTabulatorsWidget::updateFilling(int selected) {
    KoTabulator *selectedTab = &m_tabList[lstTabs->currentItem()];

    switch( selected) {
        case 1:
            selectedTab->filling=TF_DOTS; break;
        case 2:
            selectedTab->filling=TF_LINE; break;
        case 3:
            selectedTab->filling=TF_DASH; break;
        case 4:
            selectedTab->filling=TF_DASH_DOT; break;
        case 5:
            selectedTab->filling=TF_DASH_DOT_DOT; break;
    case 0:
        default:
            selectedTab->filling=TF_BLANK;
    }
}

void KoParagTabulatorsWidget::updateWidth() {
    KoTabulator *selectedTab = &m_tabList[lstTabs->currentItem()];
    selectedTab->ptWidth = TQMAX( 0, eWidth->value() );
}

void KoParagTabulatorsWidget::sortLists() {

    noSignals=true;
    qHeapSort( m_tabList );

    // we could just sort the listView, but to make sure we never have any problems with
    // inconsistent lists, just re-add..
    TQString curValue=lstTabs->currentText();
    lstTabs->clear();
    KoTabulatorList::ConstIterator it = m_tabList.begin();
    for ( ; it != m_tabList.end(); ++it )
        lstTabs->insertItem( KoUnit::toUserStringValue( (*it).ptPos, m_unit ) );

    lstTabs->setCurrentItem(lstTabs->findItem(curValue));
    noSignals=false;
}

void KoParagTabulatorsWidget::display( const KoParagLayout &lay ) {
    m_tabList.clear();
    lstTabs->clear();
    m_tabList = lay.tabList();
    KoTabulatorList::ConstIterator it = m_tabList.begin();
    for ( ; it != m_tabList.end(); ++it )
        lstTabs->insertItem( KoUnit::toUserStringValue( (*it).ptPos, m_unit ) );

    if(lstTabs->count() > 0)
        lstTabs->setCurrentItem(0);
    else {
        bDelete->setEnabled(false);
        bDeleteAll->setEnabled(false);
        gPosition->setEnabled(false);
        bgAlign->setEnabled(false);
        gTabLeader->setEnabled(false);
    }
}

void KoParagTabulatorsWidget::save( KoParagLayout & lay ) {
    lay.setTabList( m_tabList );
}

TQString KoParagTabulatorsWidget::tabName() {
    return i18n( "&Tabulators" );
}

/******************************************************************/
/* Class: KoParagDia                                              */
/******************************************************************/
KoParagDia::KoParagDia( TQWidget* parent, const char* name,
                        int flags, KoUnit::Unit unit, double _frameWidth, bool breakLine, bool disableAll )
    : KDialogBase(Tabbed, TQString(), Ok | Cancel | User1 | Apply, Ok, parent, name, true )
{
    m_decorationsWidget = 0;
    m_flags = flags;
    setButtonText( KDialogBase::User1, i18n("Reset") );

    if ( m_flags & PD_SPACING )
    {
        TQVBox * page = addVBoxPage( i18n( "Indent && S&pacing" ) );
        m_indentSpacingWidget = new KoIndentSpacingWidget( unit,_frameWidth,page, "indent-spacing" );
        m_indentSpacingWidget->layout()->setMargin(0);
    }
    if ( m_flags & PD_ALIGN )
    {
        TQVBox * page = addVBoxPage( i18n( "General &Layout" ) );
        m_alignWidget = new KoParagAlignWidget( breakLine, page, "align" );
        m_alignWidget->layout()->setMargin(0);
    }
    if ( m_flags & PD_DECORATION )
    {
        TQVBox * page = addVBoxPage( i18n( "D&ecorations" ) );
        m_decorationsWidget = new KoParagDecorationWidget( page, "decorations");
        m_decorationsWidget->layout()->setMargin(0);
    }
    if ( m_flags & PD_NUMBERING )
    {
        TQVBox * page = addVBoxPage( i18n( "B&ullets/Numbers" ) );
        m_counterWidget = new KoParagCounterWidget( disableAll , page, "numbers" );
        m_counterWidget->layout()->setMargin(0);
    }
    if ( m_flags & PD_TABS )
    {
        TQVBox * page = addVBoxPage( i18n( "&Tabulators" ) );
        m_tabulatorsWidget = new KoParagTabulatorsWidget( unit,_frameWidth, page, "tabs");
        m_tabulatorsWidget->layout()->setMargin(0);
    }

    connect( this, TQT_SIGNAL( user1Clicked() ), this, TQT_SLOT(slotReset()));
    setInitialSize( TQSize(630, 500) );
}

KoParagDia::~KoParagDia()
{
}

void KoParagDia::slotApply()
{
    emit applyParagStyle();
}

void KoParagDia::slotOk()
{
    slotApply();
    KDialogBase::slotOk();
}

void KoParagDia::setCurrentPage( int page )
{
    switch( page )
    {
    case PD_SPACING:
        showPage( pageIndex( m_indentSpacingWidget->parentWidget() ) );
        break;
    case PD_ALIGN:
        showPage( pageIndex( m_alignWidget->parentWidget() ) );
        break;
    case PD_DECORATION:
        showPage( pageIndex( m_decorationsWidget->parentWidget() ) );
        break;
    case PD_NUMBERING:
        showPage( pageIndex( m_counterWidget->parentWidget() ) );
        break;
    case PD_TABS:
        showPage( pageIndex( m_tabulatorsWidget->parentWidget() ) );
        break;
    default:
        break;
    }
}

void KoParagDia::setParagLayout( const KoParagLayout & lay )
{
    m_indentSpacingWidget->display( lay );
    m_alignWidget->display( lay );
    m_decorationsWidget->display( lay );
    m_counterWidget->display( lay );
    m_tabulatorsWidget->display( lay );
    oldLayout = lay;
}

void KoParagDia::slotReset()
{
    if( m_indentSpacingWidget )
        m_indentSpacingWidget->display( oldLayout );
    if( m_alignWidget )
        m_alignWidget->display( oldLayout );
    if ( m_decorationsWidget )
        m_decorationsWidget->display( oldLayout );
    if( m_counterWidget )
        m_counterWidget->display( oldLayout );
    if( m_tabulatorsWidget )
        m_tabulatorsWidget->display( oldLayout );
}

bool KoParagDia::isCounterChanged() const
{
    if ( oldLayout.counter ) // We had a counter
        return ! ( *oldLayout.counter == counter() );
    else // We had no counter -> changed if we have one now
        return counter().numbering() != KoParagCounter::NUM_NONE;
}

int KoParagDia::changedFlags() const
{
    return paragLayout().compare( oldLayout );
}

KoParagLayout KoParagDia::paragLayout() const
{
    KoParagLayout newLayout;
    newLayout.setLineSpacingValue( lineSpacing() );
    newLayout.lineSpacingType = lineSpacingType();
    newLayout.setTabList( tabListTabulator() );
    newLayout.alignment = align();
    newLayout.margins[TQStyleSheetItem::MarginFirstLine] = firstLineIndent();
    newLayout.margins[TQStyleSheetItem::MarginLeft] = leftIndent();
    newLayout.margins[TQStyleSheetItem::MarginRight] = rightIndent();
    newLayout.margins[TQStyleSheetItem::MarginTop] = spaceBeforeParag();
    newLayout.margins[TQStyleSheetItem::MarginBottom] = spaceAfterParag();
    newLayout.pageBreaking = pageBreaking();
    newLayout.leftBorder = leftBorder();
    newLayout.rightBorder = rightBorder();
    newLayout.topBorder = topBorder();
    newLayout.bottomBorder = bottomBorder();
    newLayout.joinBorder = joinBorder();
    newLayout.backgroundColor = backgroundColor();
    newLayout.counter = new KoParagCounter( counter() );
    return newLayout;
}

#include "KoParagDia.moc"
#include "KoParagDia_p.moc"