summaryrefslogtreecommitdiffstats
path: root/kopete/libkopete/ui/kopetelistviewitem.cpp
blob: 6156b6aab5207d20dfed3c800bef3df9601dc8de (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
/*
    kopetelistviewitem.cpp - Kopete's modular TQListViewItems

    Copyright (c) 2005      by Engin AYDOGAN          <engin@bzzzt.biz>
    Copyright (c) 2004      by Richard Smith          <kde@metafoo.co.uk>

    Kopete    (c) 2002-2004 by the Kopete developers  <kopete-devel@kde.org>

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

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "kopeteprefs.h"
#include "kopetecontact.h"
#include "kopetelistviewitem.h"
#include "kopeteemoticons.h"
#include "kopeteonlinestatus.h"

#include <kdebug.h>
#include <kiconloader.h>
#include <kstringhandler.h>

#include <tqapplication.h>
#include <tqpixmap.h>
#include <tqpainter.h>
#include <tqptrlist.h>
#include <tqrect.h>
#include <tqtimer.h>
#include <tqheader.h>
#include <tqstyle.h>

#ifdef HAVE_XRENDER
#  include <X11/Xlib.h>
#  include <X11/extensions/Xrender.h>
#endif

#include <limits.h>

namespace Kopete {
namespace UI {
namespace ListView {

// ComponentBase --------

class ComponentBase::Private
{
public:
	TQPtrList<Component> components;
};

ComponentBase::ComponentBase()
 : d( new Private )
{
}

ComponentBase::~ComponentBase()
{
	d->components.setAutoDelete( true );
	delete d;
}

uint ComponentBase::components() { return d->components.count(); }
Component *ComponentBase::component( uint n ) { return d->components.at( n ); }

Component *ComponentBase::componentAt( const TQPoint &pt )
{
	for ( uint n = 0; n < components(); ++n )
	{
		if ( component( n )->rect().contains( pt ) )
		{
			if ( Component *comp = component( n )->componentAt( pt ) )
				return comp;
			return component( n );
		}
	}
	return 0;
}

void ComponentBase::componentAdded( Component *component )
{
	d->components.append( component );
}

void ComponentBase::componentRemoved( Component *component )
{
	//TODO: make sure the component is in d->components once and only once.
	// if not, the situation is best referred to as 'very very broken indeed'.
	d->components.remove( component );
}

void ComponentBase::clear()
{
	/* I'm switching setAutoDelete back and forth instead of turning it
	 * on permenantly, because original author of this class set it to
	 * auto delete in the dtor, that might have a reason that I can't
	 * imagine right now */
	bool tmp = d->components.autoDelete();
	d->components.setAutoDelete( true );
	d->components.clear();
	d->components.setAutoDelete( tmp );
}

void ComponentBase::componentResized( Component * )
{
}

std::pair<TQString,TQRect> ComponentBase::toolTip( const TQPoint &relativePos )
{
	for ( uint n = 0; n < components(); ++n )
		if ( component( n )->rect().contains( relativePos ) )
			return component( n )->toolTip( relativePos );

	return std::make_pair( TQString(), TQRect() );
}

void ComponentBase::updateAnimationPosition( int p, int s )
{
	for ( uint n = 0; n < components(); ++n )
	{
		Component *comp = component( n );
		TQRect start = comp->startRect();
		TQRect target = comp->targetRect();
		TQRect rc( start.left() + ((target.left() - start.left()) * p) / s,
		          start.top() + ((target.top() - start.top()) * p) / s,
		          start.width() + ((target.width() - start.width()) * p) / s,
		          start.height() + ((target.height() - start.height()) * p) / s );
		comp->setRect( rc );
		comp->updateAnimationPosition( p, s );
	}
}

// Component --------

class Component::Private
{
public:
	Private( ComponentBase *parent )
	 : parent( parent ), minWidth( 0 ), minHeight( 0 )
	 , growHoriz( false ), growVert( false )
	 , tipSource( 0 )
	{
	}
	ComponentBase *parent;
	TQRect rect;
	TQRect startRect, targetRect;
	int minWidth, minHeight;
	bool growHoriz, growVert;
	bool show; /** @since 23-03-2005 */
	ToolTipSource *tipSource;
};

Component::Component( ComponentBase *parent )
 : d( new Private( parent ) )
{
	d->parent->componentAdded( this );
	d->show = true;
}

int Component::RTTI = Rtti_Component;

Component::~Component()
{
	d->parent->componentRemoved( this );
	delete d;
}


void Component::hide()
{
	d->show = false;
}

void Component::show()
{
	d->show = true;
}

bool Component::isShown()
{
	return d->show;
}

bool Component::isHidden()
{
	return !d->show;
}

void Component::setToolTipSource( ToolTipSource *source )
{
	d->tipSource = source;
}

std::pair<TQString,TQRect> Component::toolTip( const TQPoint &relativePos )
{
	if ( !d->tipSource )
		return ComponentBase::toolTip( relativePos );

	TQRect rc = rect();
	TQString result = (*d->tipSource)( this, relativePos, rc );
	return std::make_pair(result, rc);
}

TQRect Component::rect() { return d->rect; }
TQRect Component::startRect() { return d->startRect; }
TQRect Component::targetRect() { return d->targetRect; }

int Component::minWidth() { return d->minWidth; }
int Component::minHeight() { return d->minHeight; }
int Component::widthForHeight( int ) { return minWidth(); }
int Component::heightForWidth( int ) { return minHeight(); }

bool Component::setMinWidth( int width )
{
	if ( d->minWidth == width ) return false;
	d->minWidth = width;

	d->parent->componentResized( this );
	return true;
}
bool Component::setMinHeight( int height )
{
	if ( d->minHeight == height ) return false;
	d->minHeight = height;

	d->parent->componentResized( this );
	return true;
}

void Component::tqlayout( const TQRect &newRect )
{
	if ( rect().isNull() )
		d->startRect = TQRect( newRect.topLeft(), newRect.topLeft() );
	else
		d->startRect = rect();
	d->targetRect = newRect;
	//kdDebug(14000) << k_funcinfo << "At " << rect << endl;
}

void Component::setRect( const TQRect &rect )
{
	d->rect = rect;
}

void Component::paint( TQPainter *painter, const TQColorGroup &cg )
{
	/*painter->setPen( TQt::red );
	painter->drawRect( rect() );*/
	for ( uint n = 0; n < components(); ++n )
	{
		if( component( n )->isShown() )
			component( n )->paint( painter, cg );
	}
}

void Component::tqrepaint()
{
	d->parent->tqrepaint();
}

void Component::retqlayout()
{
	d->parent->retqlayout();
}

void Component::componentAdded( Component *component )
{
	ComponentBase::componentAdded( component );
	//update( Retqlayout );
}

void Component::componentRemoved( Component *component )
{
	ComponentBase::componentRemoved( component );
	//update( Retqlayout );
}

// BoxComponent --------

class BoxComponent::Private
{
public:
	Private( BoxComponent::Direction dir ) : direction( dir ) {}
	BoxComponent::Direction direction;

	static const int padding = 0;
};

BoxComponent::BoxComponent( ComponentBase *parent, Direction dir )
 : Component( parent ), d( new Private( dir ) )
{
}

int BoxComponent::RTTI = Rtti_BoxComponent;

BoxComponent::~BoxComponent()
{
	delete d;
}

int BoxComponent::widthForHeight( int height )
{
	if ( d->direction !=Qt::Horizontal )
	{
		int width = 0;
		for ( uint n = 0; n < components(); ++n )
			width = TQMAX( width, component( n )->widthForHeight( height ) );
		return width;
	}
	else
	{
		int width = (components() - 1) * Private::padding;
		for ( uint n = 0; n < components(); ++n )
			width += component( n )->widthForHeight( height );
		return width;
	}
}

int BoxComponent::heightForWidth( int width )
{
	if ( d->direction ==Qt::Horizontal )
	{
		int height = 0;
		for ( uint n = 0; n < components(); ++n )
			height = TQMAX( height, component( n )->heightForWidth( width ) );
		return height;
	}
	else
	{
		int height = (components() - 1) * Private::padding;
		for ( uint n = 0; n < components(); ++n )
			height += component( n )->heightForWidth( width );
		return height;
	}
}

void BoxComponent::calcMinSize()
{
	int sum = (components() - 1) * Private::padding, max = 0;
	for ( uint n = 0; n < components(); ++n )
	{
		Component *comp = component( n );
		if ( d->direction ==Qt::Horizontal )
		{
			max = TQMAX( max, comp->minHeight() );
			sum += comp->minWidth();
		}
		else
		{
			max = TQMAX( max, comp->minWidth() );
			sum += comp->minHeight();
		}
	}

	bool sizeChanged = false;
	if ( d->direction ==Qt::Horizontal )
	{
		if ( setMinWidth( sum ) ) sizeChanged = true;
		if ( setMinHeight( max ) ) sizeChanged = true;
	}
	else
	{
		if ( setMinWidth( max ) ) sizeChanged = true;
		if ( setMinHeight( sum ) ) sizeChanged = true;
	}

	if ( sizeChanged )
		tqrepaint();
	else
		retqlayout();
}

void BoxComponent::tqlayout( const TQRect &rect )
{
	Component::tqlayout( rect );

	bool horiz = (d->direction ==Qt::Horizontal);
	int fixedSize = 0;
	for ( uint n = 0; n < components(); ++n )
	{
		Component *comp = component( n );
		if ( horiz )
			fixedSize += comp->minWidth();
		else
			fixedSize += comp->minHeight();
	}

	// remaining space after all fixed items have been allocated
	int padding = Private::padding;

	// ensure total is at least minXXX. the only time the rect
	// will be smaller than that is when we don't fit, and in
	// that cases we should pretend that we're wide/high enough.
	int total;
	if ( horiz )
		total = TQMAX( rect.width(), minWidth() );
	else
		total = TQMAX( rect.height(), minHeight() );

	int remaining = total - fixedSize - padding * (components() - 1);

	// finally, lay everything out
	int pos = 0;
	for ( uint n = 0; n < components(); ++n )
	{
		Component *comp = component( n );
		
		TQRect rc;
		if ( horiz )
		{
			rc.setLeft( rect.left() + pos );
			rc.setTop( rect.top() );
			rc.setHeight( rect.height() );
			int minWidth = comp->minWidth();
			int desiredWidth = comp->widthForHeight( rect.height() );
			rc.setWidth( TQMIN( remaining + minWidth, desiredWidth ) );
			pos += rc.width();
			remaining -= rc.width() - minWidth;
		}
		else
		{
			rc.setLeft( rect.left() );
			rc.setTop( rect.top() + pos );
			rc.setWidth( rect.width() );
			int minHeight = comp->minHeight();
			int desiredHeight = comp->heightForWidth( rect.width() );
			rc.setHeight( TQMIN( remaining + minHeight, desiredHeight ) );
			pos += rc.height();
			remaining -= rc.height() - minHeight;
		}
		comp->tqlayout( rc & rect );
		pos += padding;
	}
}

void BoxComponent::componentAdded( Component *component )
{
	Component::componentAdded( component );
	calcMinSize();
}

void BoxComponent::componentRemoved( Component *component )
{
	Component::componentRemoved( component );
	calcMinSize();
}

void BoxComponent::componentResized( Component *component )
{
	Component::componentResized( component );
	calcMinSize();
}



/*= ContactBoxComponent =====================================================*/

class ContactBoxComponent::Private
{
public:
	TQRect				sub;

	TQPixmap 			back_pixmap;

	TQPixmap 			corner_tl_pixmap;
	TQPixmap 			corner_bl_pixmap;
	TQPixmap 			corner_tr_pixmap;
	TQPixmap 			corner_br_pixmap;

	TQPixmap 			top_pixmap;
	TQPixmap 			left_pixmap;
	TQPixmap 			right_pixmap;
	TQPixmap 			bottom_pixmap;
};

ContactBoxComponent::ContactBoxComponent(ComponentBase *parent, Direction dir)
 : BoxComponent(parent, dir), d(new Private())
{}

ContactBoxComponent::~ContactBoxComponent()
{
	delete d;
}

void	ContactBoxComponent::reloadTheme()
{
	TQString	path = KopetePrefs::prefs()->themeURL();
	TQString	str;

	str = path + "ContactBackground.png";
	d->back_pixmap.load(str);

	str = path + "ContactTopLeft.png";
	d->corner_tl_pixmap.load(str);
	str = path + "ContactBottomLeft.png";
	d->corner_bl_pixmap.load(str);
	str = path + "ContactTopRight.png";
	d->corner_tr_pixmap.load(str);
	str = path + "ContactBottomRight.png";
	d->corner_br_pixmap.load(str);

	str = path + "ContactTop.png";
	d->top_pixmap.load(str);
	str = path + "ContactLeft.png";
	d->left_pixmap.load(str);
	str = path + "ContactRight.png";
	d->right_pixmap.load(str);
	str = path + "ContactBottom.png";
	d->bottom_pixmap.load(str);
}

void	ContactBoxComponent::tqlayout(const TQRect &rect)
{
	d->sub.setLeft(rect.left() +
			d->left_pixmap.width());
	d->sub.setTop(rect.top() +
			d->top_pixmap.height());
	d->sub.setRight(rect.right() -
			d->right_pixmap.width());
	d->sub.setBottom(rect.bottom() -
			d->bottom_pixmap.height());

	BoxComponent::tqlayout(d->sub);
	Component::tqlayout(rect);
}

int	ContactBoxComponent::widthForHeight(int height)
{
	return BoxComponent::widthForHeight(height) +
			d->left_pixmap.width() +
			d->right_pixmap.width();
}

int	ContactBoxComponent::heightForWidth(int width)
{
	return BoxComponent::heightForWidth(width) +
			d->top_pixmap.height() +
			d->bottom_pixmap.height();
}

void	ContactBoxComponent::paint(TQPainter *painter, const TQColorGroup &cg)
{
	painter->drawPixmap(0,
							  0,
							  d->corner_tl_pixmap);

	painter->drawPixmap(0,
							  d->sub.bottom()+1,
							  d->corner_bl_pixmap);

	painter->drawPixmap(d->sub.right()+1,
							  0,
							  d->corner_tr_pixmap);

	painter->drawPixmap(d->sub.right()+1,
							  d->sub.bottom()+1,
							  d->corner_br_pixmap);

	painter->drawTiledPixmap(0,
									 d->sub.top(),
									 d->left_pixmap.width(),
									 d->sub.height(),
									 d->left_pixmap);

	painter->drawTiledPixmap(d->sub.left(),
									 0,
									 d->sub.width(),
									 d->top_pixmap.height(),
									 d->top_pixmap);

	painter->drawTiledPixmap(d->sub.left(),
									 d->sub.bottom()+1,
									 d->sub.width(),
									 d->bottom_pixmap.height(),
									 d->bottom_pixmap);

	painter->drawTiledPixmap(d->sub.right()+1,
									 d->sub.top(),
									 d->right_pixmap.width(),
									 d->sub.height(),
									 d->right_pixmap);

	painter->drawTiledPixmap(d->sub,
									 d->back_pixmap);

	return BoxComponent::paint(painter, cg);
}



/*= GroupBoxComponent =======================================================*/

class GroupBoxComponent::Private
{
public:
	TQRect				sub;

	TQPixmap 			back_pixmap;

	TQPixmap			open_pixmap;
	TQPixmap			closed_pixmap;

	TQPixmap 			corner_tl_pixmap;
	TQPixmap 			corner_bl_pixmap;
	TQPixmap 			corner_tr_pixmap;
	TQPixmap 			corner_br_pixmap;

	TQPixmap 			top_pixmap;
	TQPixmap 			left_pixmap;
	TQPixmap 			right_pixmap;
	TQPixmap 			bottom_pixmap;
};

GroupBoxComponent::GroupBoxComponent(ComponentBase *parent, Direction dir)
 : BoxComponent(parent, dir), d(new Private())
{}

GroupBoxComponent::~GroupBoxComponent()
{
	delete d;
}

void	GroupBoxComponent::reloadTheme()
{
	TQString	path = KopetePrefs::prefs()->themeURL();
	TQString	str;

	str = path + "GroupBackground.png";
	d->back_pixmap.load(str);

	str = path + "GroupOpen.png";
	d->open_pixmap.load(str);
	str = path + "GroupClosed.png";
	d->closed_pixmap.load(str);

	str = path + "GroupTopLeft.png";
	d->corner_tl_pixmap.load(str);
	str = path + "GroupBottomLeft.png";
	d->corner_bl_pixmap.load(str);
	str = path + "GroupTopRight.png";
	d->corner_tr_pixmap.load(str);
	str = path + "GroupBottomRight.png";
	d->corner_br_pixmap.load(str);

	str = path + "GroupTop.png";
	d->top_pixmap.load(str);
	str = path + "GroupLeft.png";
	d->left_pixmap.load(str);
	str = path + "GroupRight.png";
	d->right_pixmap.load(str);
	str = path + "GroupBottom.png";
	d->bottom_pixmap.load(str);
}

void	GroupBoxComponent::tqlayout(const TQRect &rect)
{
	d->sub.setLeft(rect.left() +
			d->left_pixmap.width());
	d->sub.setTop(rect.top() +
			d->top_pixmap.height());
	d->sub.setRight(rect.right() -
			d->right_pixmap.width());
	d->sub.setBottom(rect.bottom() -
			d->bottom_pixmap.height());

	BoxComponent::tqlayout(d->sub);
	Component::tqlayout(rect);
}

int	GroupBoxComponent::widthForHeight(int height)
{
	return BoxComponent::widthForHeight(height) +
			d->left_pixmap.width() +
			d->right_pixmap.width();
}

int	GroupBoxComponent::heightForWidth( int width )
{
	return BoxComponent::heightForWidth(width) +
			d->top_pixmap.height() +
			d->bottom_pixmap.height();
}

void	GroupBoxComponent::paint( TQPainter *painter, const TQColorGroup &cg )
{
	painter->drawPixmap(0,
							  0,
							  d->corner_tl_pixmap);

	painter->drawPixmap(0,
							  d->sub.bottom()+1,
							  d->corner_bl_pixmap);

	painter->drawPixmap(d->sub.right()+1,
							  0,
							  d->corner_tr_pixmap);

	painter->drawPixmap(d->sub.right()+1,
							  d->sub.bottom()+1,
							  d->corner_br_pixmap);

	painter->drawTiledPixmap(0,
									 d->sub.top(),
									 d->left_pixmap.width(),
									 d->sub.height(),
									 d->left_pixmap);

	painter->drawTiledPixmap(d->sub.left(),
									 0,
									 d->sub.width(),
									 d->top_pixmap.height(),
									 d->top_pixmap);

	painter->drawTiledPixmap(d->sub.left(),
									 d->sub.bottom()+1,
									 d->sub.width(),
									 d->bottom_pixmap.height(),
									 d->bottom_pixmap);

	painter->drawTiledPixmap(d->sub.right()+1,
									 d->sub.top(),
									 d->right_pixmap.width(),
									 d->sub.height(),
									 d->right_pixmap);

	painter->drawTiledPixmap(d->sub,
									 d->back_pixmap);

	return BoxComponent::paint(painter, cg);
}

// ImageComponent --------

class ImageComponent::Private
{
public:
	TQPixmap image;
};

ImageComponent::ImageComponent( ComponentBase *parent )
 : Component( parent ), d( new Private )
{
}

int ImageComponent::RTTI = Rtti_ImageComponent;

ImageComponent::ImageComponent( ComponentBase *parent, int minW, int minH )
 : Component( parent ), d( new Private )
{
	setMinWidth( minW );
	setMinHeight( minH );
	tqrepaint();
}

ImageComponent::~ImageComponent()
{
	delete d;
}

TQPixmap ImageComponent::pixmap()
{
	return d->image;
}

void ImageComponent::setPixmap( const TQPixmap &img, bool)
{
	d->image = img;
	setMinWidth(d->image.width());
	setMinHeight(d->image.height());

	tqrepaint();
}

void ImageComponent::paint( TQPainter *painter, const TQColorGroup & )
{
	TQRect ourRc = rect();
	TQRect rc = d->image.rect();
	// center rc within our rect
	rc.moveTopLeft(ourRc.topLeft());
	// paint, shrunk to be within our rect
	painter->drawPixmap( rc & ourRc, d->image );
}

void ImageComponent::scale( int w, int h, TQ_ScaleMode mode )
{
	TQImage im = d->image.convertToImage();
	setPixmap( TQPixmap( im.smoothScale( w, h, mode ) ) );
}



/*= FaceComponent ===========================================================*/

void		FaceComponent::setPixmap(const TQPixmap &img, bool)
{
	d->image = img;

	setMinWidth(d->image.width());
	setMinHeight(d->image.height());

	if (img.width() >= 30)
	{
		d->image = TQPixmap(img.convertToImage().smoothScale(30, 30));
		setMinWidth(d->image.width() + 4);
		setMinHeight(d->image.height() + 4);
	}

	tqrepaint();
}

static TQPoint	operator+(const TQPoint &pt, const TQSize &sz)
{
	return TQPoint(pt.x() + sz.width(), pt.y() + sz.height());
}

void	FaceComponent::paint(TQPainter *painter, const TQColorGroup &)
{
	TQRect	outRc = rect();
	TQRect pixRc = d->image.rect();

	pixRc.moveTopLeft(outRc.topLeft() + (outRc.size() - pixRc.size()) / 2);

	if (d->image.width() == 30)
	{
		TQPixmap	pixBorder;
		TQString	path = KopetePrefs::prefs()->themeURL();
		TQString	str = path + "ContactFace.png";

		pixBorder.load(str);
		TQRect pixRc2 =  pixBorder.rect();

		pixRc2.moveTopLeft(outRc.topLeft() + (outRc.size() - pixRc2.size()) / 2);
		painter->drawPixmap(pixRc2, pixBorder);
	}

	painter->drawPixmap(pixRc, d->image);
}


// TextComponent

class TextComponent::Private
{
public:
	Private() : customColor( false ) {}
	TQString text;
	bool customColor;
	TQColor color;
	TQFont font;
};

TextComponent::TextComponent( ComponentBase *parent, const TQFont &font, const TQString &text )
 : Component( parent ), d( new Private )
{
	setFont( font );
	setText( text );
}

int TextComponent::RTTI = Rtti_TextComponent;

TextComponent::~TextComponent()
{
	delete d;
}

TQString TextComponent::text()
{
	return d->text;
}

void TextComponent::setText( const TQString &text )
{
	if ( text == d->text ) return;
	d->text = text;
	retqlayout();
	calcMinSize();
}

TQFont TextComponent::font()
{
	return d->font;
}

void TextComponent::setFont( const TQFont &font )
{
	if ( font == d->font ) return;
	d->font = font;
	calcMinSize();
}

void TextComponent::calcMinSize()
{
	setMinWidth( 0 );

	if ( !d->text.isEmpty() )
		setMinHeight( TQFontMetrics( font() ).height() );
	else
		setMinHeight( 0 );

	tqrepaint();
}

int TextComponent::widthForHeight( int )
{
	// add 2 to place an extra gap between the text and things to its right.
	// allegedly if this is not done the protocol icons overlap the text.
	// i however have never seen this problem (which would almost certainly
	// be a bug somewhere else).
	return TQFontMetrics( font() ).width( d->text ) + 2;
}

TQColor TextComponent::color()
{
	return d->customColor ? d->color : TQColor();
}

void TextComponent::setColor( const TQColor &color )
{
	d->color = color;
	d->customColor = true;
	tqrepaint();
}

void TextComponent::setDefaultColor()
{
	d->customColor = false;
	tqrepaint();
}

void TextComponent::paint( TQPainter *painter, const TQColorGroup &cg )
{
	if ( d->customColor )
		painter->setPen( d->color );
	else
		painter->setPen( cg.text() );
	TQString dispStr = KStringHandler::rPixelSqueeze( d->text, TQFontMetrics( font() ), rect().width() );
	painter->setFont( font() );
	painter->drawText( rect(), TQt::SingleLine, dispStr );
}

// DisplayNameComponent

class DisplayNameComponent::Private
{
public:
	TQString text;
    TQFont font;
};

DisplayNameComponent::DisplayNameComponent( ComponentBase *parent )
 : BoxComponent( parent ), d( new Private )
{
}

int DisplayNameComponent::RTTI = Rtti_DisplayNameComponent;

DisplayNameComponent::~DisplayNameComponent()
{
	delete d;
}

void DisplayNameComponent::tqlayout( const TQRect &rect )
{
	Component::tqlayout( rect );

	// finally, lay everything out
	TQRect rc;
	int totalWidth = rect.width();
	int usedWidth = 0;
	bool exceeded = false;
	for ( uint n = 0; n < components(); ++n )
	{
		Component *comp = component( n );
		if ( !exceeded )
		{
			if ( ( usedWidth + comp->widthForHeight( rect.height() ) ) > totalWidth )
			{
				exceeded = true;
				// TextComponents can squeeze themselves
				if ( comp->rtti() == Rtti_TextComponent )
				{
					comp->show();
					comp->tqlayout( TQRect( usedWidth+ rect.left(), rect.top(),
							     totalWidth - usedWidth,
							     comp->heightForWidth( totalWidth - usedWidth ) ) );
				} else {
					comp->hide();
				}
			}
			else 
			{
				comp->show();
				comp->tqlayout( TQRect( usedWidth+ rect.left(), rect.top(),
						     comp->widthForHeight( rect.height() ),
						     comp->heightForWidth( rect.width() ) ) );
			}
			usedWidth+= comp->widthForHeight( rect.height() );
		}
		else
		{
			// Shall we implement a hide()/show() in Component class ?
			comp->hide();
		}
	}
}

void DisplayNameComponent::setText( const TQString& text )
{
	if ( d->text == text ) 
		return;
	d->text = text;
	
	redraw();
}

void DisplayNameComponent::redraw()
{
	TQColor color;
	for ( uint n = 0; n < components(); ++n )
		if( component( n )->rtti() == Rtti_TextComponent )
	{
		((TextComponent*)component(n))->color();
	}
	
	TQValueList<Kopete::Emoticons::Token> tokens;
	TQValueList<Kopete::Emoticons::Token>::const_iterator token;
	
	clear(); // clear childs

	tokens = Kopete::Emoticons::tokenizeEmoticons( d->text );
	ImageComponent *ic;
	TextComponent *t;

	TQFontMetrics fontMetrics( d->font );
	int fontHeight = fontMetrics.height();
	for ( token = tokens.begin(); token != tokens.end(); ++token )
	{
		switch ( (*token).type )
		{
		case Kopete::Emoticons::Text:
			t = new TextComponent( this,  d->font, (*token).text );
		break;
		case Kopete::Emoticons::Image:
			ic = new ImageComponent( this );
			ic->setPixmap( TQPixmap( (*token).picPath ) );
			ic->scale( INT_MAX, fontHeight, TQ_ScaleMin );
		break;
		default:
			kdDebug( 14010 ) << k_funcinfo << "This should have not happened!" << endl;
		}
	}
	
	if(color.isValid())
		setColor( color );
}

void DisplayNameComponent::setFont( const TQFont& font )
{
	for ( uint n = 0; n < components(); ++n )
		if( component( n )->rtti() == Rtti_TextComponent )
			((TextComponent*)component(n))->setFont( font );
	d->font = font;
}

void DisplayNameComponent::setColor( const TQColor& color )
{
	for ( uint n = 0; n < components(); ++n )
		if( component( n )->rtti() == Rtti_TextComponent )
			((TextComponent*)component(n))->setColor( color );
}

void DisplayNameComponent::setDefaultColor()
{
	for ( uint n = 0; n < components(); ++n )
		if( component( n )->rtti() == Rtti_TextComponent )
			((TextComponent*)component(n))->setDefaultColor();
}

TQString DisplayNameComponent::text()
{
	return d->text;
}

// HSpacerComponent --------

HSpacerComponent::HSpacerComponent( ComponentBase *parent )
 : Component( parent )
{
	setMinWidth( 0 );
	setMinHeight( 0 );
}

int HSpacerComponent::RTTI = Rtti_HSpacerComponent;

int HSpacerComponent::widthForHeight( int )
{
	return INT_MAX;
}

// VSpacerComponent --------

VSpacerComponent::VSpacerComponent( ComponentBase *parent )
 : Component( parent )
{
	setMinWidth( 0 );
	setMinHeight( 0 );
}

int VSpacerComponent::RTTI = Rtti_VSpacerComponent;

int VSpacerComponent::heightForWidth( int )
{
	return INT_MAX;
}

//////////////////  ContactComponent /////////////////////////

class ContactComponent::Private
{
public:
	Kopete::Contact *contact;
	int iconSize;
};

ContactComponent::ContactComponent( ComponentBase *parent, Kopete::Contact *contact, int) : ImageComponent( parent ) , d( new Private )
{
	d->contact = contact;
	d->iconSize = 12; // size of the image is fixed to 12 pixels
	updatePixmap();
}

ContactComponent::~ContactComponent()
{
	delete d;
}

void ContactComponent::updatePixmap()
{
	setPixmap( contact()->onlinetqStatus().iconFor( contact(), d->iconSize ) );
}
Kopete::Contact *ContactComponent::contact()
{
	return d->contact;
}

// we don't need to use a tooltip source here - this way is simpler
std::pair<TQString,TQRect> ContactComponent::toolTip( const TQPoint &/*relativePos*/ )
{
	return std::make_pair(d->contact->toolTip(),rect());
}

//////////////////  SpacerComponent /////////////////////////

SpacerComponent::SpacerComponent( ComponentBase *parent, int w, int h ) : Component( parent )
{
	setMinWidth(w);
	setMinHeight(h);
}

// Item --------

/**
 * A periodic timer intended to be shared amongst multiple objects. Will run only
 * if an object is attached to it.
 */
class SharedTimer : private TQTimer
{
	int period;
	int users;
public:
	SharedTimer( int period ) : period(period), users(0) {}
	void attach( TQObject *target, const char *slot )
	{
		connect( this, TQT_SIGNAL(timeout()), target, slot );
		if( users++ == 0 )
			start( period );
		//kdDebug(14000) << "SharedTimer::attach: users is now " << users << "\n";
	}
	void detach( TQObject *target, const char *slot )
	{
		disconnect( this, TQT_SIGNAL(timeout()), target, slot );
		if( --users == 0 )
			stop();
		//kdDebug(14000) << "SharedTimer::detach: users is now " << users << "\n";
	}
};

class SharedTimerRef
{
	SharedTimer &timer;
	TQObject * const object;
	const char * const slot;
	bool attached;
public:
	SharedTimerRef( SharedTimer &timer, TQObject *obj, const char *slot )
	 : timer(timer), object(obj), slot(slot), attached(false)
	{
	}
	void start()
	{
		if( attached ) return;
		timer.attach( object, slot );
		attached = true;
	}
	void stop()
	{
		if( !attached ) return;
		timer.detach( object, slot );
		attached = false;
	}
	bool isActive()
	{
		return attached;
	}
};

class Item::Private
{
public:
	Private( Item *item )
	 : layoutAnimateTimer( theLayoutAnimateTimer(), item, TQT_SLOT( slotLayoutAnimateItems() ) )
	 , animateLayout( true ), opacity( 1.0 )
	 , visibilityTimer( theVisibilityTimer(), item, TQT_SLOT( slotUpdateVisibility() ) )
	 , visibilityLevel( 0 ), visibilityTarget( false ), searchMatch( true )
	{
	}

	TQTimer layoutTimer;

	//TQTimer layoutAnimateTimer;
	SharedTimerRef layoutAnimateTimer;
	SharedTimer &theLayoutAnimateTimer()
	{
		static SharedTimer timer( 10 );
		return timer;
	}
	
	bool animateLayout;
	int layoutAnimateSteps;
	static const int layoutAnimateStepsTotal = 10;

	float opacity;

	//TQTimer visibilityTimer;
	SharedTimerRef visibilityTimer;
	SharedTimer &theVisibilityTimer()
	{
		static SharedTimer timer( 40 );
		return timer;
	}

	int visibilityLevel;
	bool visibilityTarget;
	static const int visibilityFoldSteps = 7;
#ifdef HAVE_XRENDER
	static const int visibilityFadeSteps = 7;
#else
	static const int visibilityFadeSteps = 0;
#endif
	static const int visibilityStepsTotal = visibilityFoldSteps + visibilityFadeSteps;
	
	bool searchMatch;
		
	static bool animateChanges;
	static bool fadeVisibility;
	static bool foldVisibility;
};

bool Item::Private::animateChanges = true;
bool Item::Private::fadeVisibility = true;
bool Item::Private::foldVisibility = true;

Item::Item( TQListViewItem *parent, TQObject *owner, const char *name )
 : TQObject( owner, name ), KListViewItem( parent ), d( new Private(this) )
{
	initLVI();
}

Item::Item( TQListView *parent, TQObject *owner, const char *name )
 : TQObject( owner, name ), KListViewItem( parent ), d( new Private(this) )
{
	initLVI();
}

Item::~Item()
{
	delete d;
}

void Item::setEffects( bool animation, bool fading, bool folding )
{
	Private::animateChanges = animation;
	Private::fadeVisibility = fading;
	Private::foldVisibility = folding;
}

void Item::initLVI()
{
	connect( listView()->header(), TQT_SIGNAL( sizeChange( int, int, int ) ), TQT_SLOT( slotColumnResized() ) );
	connect( &d->layoutTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotLayoutItems() ) );
	//connect( &d->layoutAnimateTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotLayoutAnimateItems() ) );
	//connect( &d->visibilityTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotUpdateVisibility() ) );
	setVisible( false );
	setTargetVisibility( true );
}

void Item::slotColumnResized()
{
	scheduleLayout();
	// if we've been resized, don't animate the tqlayout
	d->animateLayout = false;
}

void Item::scheduleLayout()
{
	// perform a delayed tqlayout in order to speed it all up
	if ( ! d->layoutTimer.isActive() )
		d->layoutTimer.start( 30, true );
}

void Item::slotLayoutItems()
{
	d->layoutTimer.stop();

	for ( uint n = 0; n < components(); ++n )
	{
		int width = listView()->columnWidth(n);
		if ( n == 0 )
		{
			int d = depth() + (listView()->rootIsDecorated() ? 1 : 0);
			width -= d * listView()->treeStepSize();
		}

		int height = component( n )->heightForWidth( width );
		component( n )->tqlayout( TQRect( 0, 0, width, height ) );
		//kdDebug(14000) << k_funcinfo << "Component " << n << " is " << width << " x " << height << endl;
	}

	if ( Private::animateChanges && d->animateLayout && !d->visibilityTimer.isActive() )
	{
		d->layoutAnimateTimer.start();
		//if ( !d->layoutAnimateTimer.isActive() )
		//	d->layoutAnimateTimer.start( 10 );
		d->layoutAnimateSteps = 0;
	}
	else
	{
		d->layoutAnimateSteps = Private::layoutAnimateStepsTotal;
		d->animateLayout = true;
	}
	slotLayoutAnimateItems();
}

void Item::slotLayoutAnimateItems()
{
	if ( ++d->layoutAnimateSteps >= Private::layoutAnimateStepsTotal )
		d->layoutAnimateTimer.stop();

	const int s = Private::layoutAnimateStepsTotal;
	const int p = TQMIN( d->layoutAnimateSteps, s );

	updateAnimationPosition( p, s );
	setHeight(0);
	tqrepaint();
}

float Item::opacity()
{
	return d->opacity;
}

void Item::setOpacity( float opacity )
{
	if ( d->opacity == opacity ) return;
	d->opacity = opacity;
	tqrepaint();
}

void Item::setSearchMatch( bool match )
{
	d->searchMatch = match;
	
	if ( !match )
		setVisible( false );
	else
	{
		kdDebug(14000) << k_funcinfo << " match: " << match << ", vis timer active: " << d->visibilityTimer.isActive()
		               << ", target visibility: " << targetVisibility() << endl;
		if ( d->visibilityTimer.isActive() )
			setVisible( true );
		else
			setVisible( targetVisibility() );
	}
}

bool Item::targetVisibility()
{
	return d->visibilityTarget;
}

void Item::setTargetVisibility( bool vis )
{
	if ( d->visibilityTarget == vis )
	{
		// in case we're getting called because our parent was shown and
		// we need to be rehidden
		if ( !d->visibilityTimer.isActive() )
			setVisible( vis && d->searchMatch );
		return;
	}
	d->visibilityTarget = vis;
	d->visibilityTimer.start();
	//d->visibilityTimer.start( 40 );
	if ( targetVisibility() )
		setVisible( d->searchMatch );
	slotUpdateVisibility();
}

void Item::slotUpdateVisibility()
{
	if ( targetVisibility() )
		++d->visibilityLevel;
	else
		--d->visibilityLevel;

	if ( !Private::foldVisibility && !Private::fadeVisibility )
		d->visibilityLevel = targetVisibility() ? Private::visibilityStepsTotal : 0;
	else if ( !Private::fadeVisibility && d->visibilityLevel >= Private::visibilityFoldSteps )
		d->visibilityLevel = targetVisibility() ? Private::visibilityStepsTotal : Private::visibilityFoldSteps - 1;
	else if ( !Private::foldVisibility && d->visibilityLevel <= Private::visibilityFoldSteps )
		d->visibilityLevel = targetVisibility() ? Private::visibilityFoldSteps + 1 : 0;

	if ( d->visibilityLevel >= Private::visibilityStepsTotal )
	{
		d->visibilityLevel = Private::visibilityStepsTotal;
		d->visibilityTimer.stop();
	}
	else if ( d->visibilityLevel <= 0 )
	{
		d->visibilityLevel = 0;
		d->visibilityTimer.stop();
		setVisible( false );
	}
	setHeight( 0 );
	tqrepaint();
}

void Item::tqrepaint()
{
	// if we're about to retqlayout, don't bother painting yet.
	if ( d->layoutTimer.isActive() )
		return;
	listView()->repaintItem( this );
}

void Item::retqlayout()
{
	scheduleLayout();
}

void Item::setup()
{
	KListViewItem::setup();
	slotLayoutItems();
}

void Item::setHeight( int )
{
	int minHeight = 0;
	for ( uint n = 0; n < components(); ++n )
		minHeight = TQMAX( minHeight, component( n )->rect().height() );
	//kdDebug(14000) << k_funcinfo << "Height is " << minHeight << endl;
	if ( Private::foldVisibility && d->visibilityTimer.isActive() )
	{
		int vis = TQMIN( d->visibilityLevel, Private::visibilityFoldSteps );
		minHeight = (minHeight * vis) / Private::visibilityFoldSteps;
	}
	KListViewItem::setHeight( minHeight );
}

int Item::width( const TQFontMetrics &, const TQListView *lv, int c ) const
{
	// TQt computes the tqitemRect from this. we want the whole item to be
	// clickable, so we return the widest we could possibly be.
	return lv->header()->sectionSize( c );
}

void Item::paintCell( TQPainter *p, const TQColorGroup &cg, int column, int width, int align )
{
	TQPixmap back( width, height() );
	TQPainter paint( &back );
	//KListViewItem::paintCell( &paint, cg, column, width, align );
	// PASTED FROM KLISTVIEWITEM:
	// set the alternate cell background colour if necessary
	TQColorGroup _cg = cg;
	if (isAlternate())
		if (listView()->viewport()->backgroundMode()==TQt::FixedColor)
			_cg.setColor(TQColorGroup::Background, static_cast< KListView* >(listView())->alternateBackground());
		else
			_cg.setColor(TQColorGroup::Base, static_cast< KListView* >(listView())->alternateBackground());
	// PASTED FROM TQLISTVIEWITEM
	{
		TQPainter *p = &paint;

		TQListView *lv = listView();
		if ( !lv )
			return;
		TQFontMetrics fm( p->fontMetrics() );

		// any text we render is done by the Components, not by this class, so make sure we've nothing to write
		TQString t;

		// removed text truncating code from TQt - we do that differently, further on

		int marg = lv->itemMargin();
		int r = marg;
	//	const TQPixmap * icon = pixmap( column );

		const BackgroundMode bgmode = lv->viewport()->backgroundMode();
		const TQColorGroup::ColorRole crole = TQPalette::backgroundRoleFromMode( bgmode );

		if ( _cg.brush( crole ) != lv->tqcolorGroup().brush( crole ) )
			p->fillRect( 0, 0, width, height(), _cg.brush( crole ) );
		else
		{
			// all copied from TQListView::paintEmptyArea

			//lv->paintEmptyArea( p, TQRect( 0, 0, width, height() ) );
			TQStyleOption opt( lv->sortColumn(), 0 ); // ### hack; in 3.1, add a property in TQListView and TQHeader
			TQStyle::SFlags how = TQStyle::Style_Default;
			if ( lv->isEnabled() )
				how |= TQStyle::Style_Enabled;

			lv->tqstyle().tqdrawComplexControl( TQStyle::CC_ListView,
						p, lv, TQRect( 0, 0, width, height() ), lv->tqcolorGroup(),
						how, TQStyle::SC_ListView, TQStyle::SC_None,
						opt );
		}



		if ( isSelected() &&
		(column == 0 || lv->allColumnsShowFocus()) ) {
			p->fillRect( r - marg, 0, width - r + marg, height(),
					_cg.brush( TQColorGroup::Highlight ) );
	// removed text pen setting code from TQt
		}

		// removed icon drawing code from TQt

		// draw the tree gubbins
		if ( multiLinesEnabled() && column == 0 && isOpen() && childCount() ) {
			int textheight = fm.size( align, t ).height() + 2 * lv->itemMargin();
			textheight = TQMAX( textheight, TQApplication::globalStrut().height() );
			if ( textheight % 2 > 0 )
				textheight++;
			if ( textheight < height() ) {
				int w = lv->treeStepSize() / 2;
				lv->tqstyle().tqdrawComplexControl( TQStyle::CC_ListView, p, lv,
								TQRect( 0, textheight, w + 1, height() - textheight + 1 ), _cg,
								lv->isEnabled() ? TQStyle::Style_Enabled : TQStyle::Style_Default,
								TQStyle::SC_ListViewExpand,
								(uint)TQStyle::SC_All, TQStyleOption( this ) );
			}
		}
	}
	// END OF PASTE
	
	
	//do you see a better way to tell the TextComponent we are selected ?  - Olivier 2004-09-02
	if ( isSelected() ) 
		_cg.setColor(TQColorGroup::Text , _cg.highlightedText() );
	
	if ( Component *comp = component( column ) )
		comp->paint( &paint, _cg );
	paint.end();

#ifdef HAVE_XRENDER
	TQColor rgb = cg.base();//backgroundColor();
	float opac = 1.0;
	if ( d->visibilityTimer.isActive() && Private::fadeVisibility )
	{
		int vis = TQMAX( d->visibilityLevel - Private::visibilityFoldSteps, 0 );
		opac = float(vis) / Private::visibilityFadeSteps;
	}
	opac *= opacity();
	const int alpha = 257 - int(opac * 257);
	if ( alpha != 0 )
	{
		XRenderColor clr = { alpha * rgb.red(), alpha * rgb.green(), alpha * rgb.blue(), alpha * 0xff };
		XRenderFillRectangle( back.x11Display(), PictOpOver, back.x11RenderHandle(),
		                      &clr, 0, 0, width, height() );
	}
#endif

	p->drawPixmap( 0, 0, back );
}

void Item::componentAdded( Component *component )
{
	ComponentBase::componentAdded( component );
	scheduleLayout();
}

void Item::componentRemoved( Component *component )
{
	ComponentBase::componentRemoved( component );
	scheduleLayout();
}

void Item::componentResized( Component *component )
{
	ComponentBase::componentResized( component );
	scheduleLayout();
}

} // END namespace ListView
} // END namespace UI
} // END namespace Kopete

#include "kopetelistviewitem.moc"

// vim: set noet ts=4 sts=4 sw=4: