kaddressbook

colorlistbox.cpp
1/*
2 * kmail: KDE mail client
3 * This file: Copyright (C) 2000 Espen Sand, espen@kde.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20
21#include <tqpainter.h>
22
23#include <kcolordialog.h>
24#include <kcolordrag.h>
25
26#include "colorlistbox.h"
27
28ColorListBox::ColorListBox( TQWidget *parent, const char *name, WFlags f )
29 :TDEListBox( parent, name, f ), mCurrentOnDragEnter(-1)
30{
31 connect( this, TQT_SIGNAL(selected(int)), this, TQT_SLOT(newColor(int)) );
32 setAcceptDrops( true);
33}
34
35
36void ColorListBox::setEnabled( bool state )
37{
38 if( state == isEnabled() )
39 {
40 return;
41 }
42
43 TQListBox::setEnabled( state );
44 for( uint i=0; i<count(); i++ )
45 {
46 updateItem( i );
47 }
48}
49
50
51void ColorListBox::setColor( uint index, const TQColor &color )
52{
53 if( index < count() )
54 {
55 ColorListItem *colorItem = (ColorListItem*)item(index);
56 colorItem->setColor(color);
57 updateItem( colorItem );
58 }
59}
60
61
62TQColor ColorListBox::color( uint index ) const
63{
64 if( index < count() )
65 {
66 ColorListItem *colorItem = (ColorListItem*)item(index);
67 return( colorItem->color() );
68 }
69 else
70 {
71 return( black );
72 }
73}
74
75
76void ColorListBox::newColor( int index )
77{
78 if( isEnabled() == false )
79 {
80 return;
81 }
82
83 if( (uint)index < count() )
84 {
85 TQColor c = color( index );
86 if( KColorDialog::getColor( c, this ) != TQDialog::Rejected )
87 {
88 setColor( index, c );
89 }
90 }
91}
92
93
94void ColorListBox::dragEnterEvent( TQDragEnterEvent *e )
95{
96 if( KColorDrag::canDecode(e) && isEnabled() )
97 {
98 mCurrentOnDragEnter = currentItem();
99 e->accept( true );
100 }
101 else
102 {
103 mCurrentOnDragEnter = -1;
104 e->accept( false );
105 }
106}
107
108
109void ColorListBox::dragLeaveEvent( TQDragLeaveEvent * )
110{
111 if( mCurrentOnDragEnter != -1 )
112 {
113 setCurrentItem( mCurrentOnDragEnter );
114 mCurrentOnDragEnter = -1;
115 }
116}
117
118
119void ColorListBox::dragMoveEvent( TQDragMoveEvent *e )
120{
121 if( KColorDrag::canDecode(e) && isEnabled() )
122 {
123 ColorListItem *item = (ColorListItem*)itemAt( e->pos() );
124 if( item != 0 )
125 {
126 setCurrentItem ( item );
127 }
128 }
129}
130
131
132void ColorListBox::dropEvent( TQDropEvent *e )
133{
134 TQColor color;
135 if( KColorDrag::decode( e, color ) )
136 {
137 int index = currentItem();
138 if( index != -1 )
139 {
140 ColorListItem *colorItem = (ColorListItem*)item(index);
141 colorItem->setColor(color);
142 triggerUpdate( false ); // Redraw item
143 }
144 mCurrentOnDragEnter = -1;
145 }
146}
147
148
149
150ColorListItem::ColorListItem( const TQString &text, const TQColor &color )
151 : TQListBoxItem(), mColor( color ), mBoxWidth( 30 )
152{
153 setText( text );
154}
155
156
157const TQColor &ColorListItem::color( void )
158{
159 return( mColor );
160}
161
162
163void ColorListItem::setColor( const TQColor &color )
164{
165 mColor = color;
166}
167
168
169void ColorListItem::paint( TQPainter *p )
170{
171 TQFontMetrics fm = p->fontMetrics();
172 int h = fm.height();
173
174 p->drawText( mBoxWidth+3*2, fm.ascent() + fm.leading()/2, text() );
175
176 p->setPen( TQt::black );
177 p->drawRect( 3, 1, mBoxWidth, h-1 );
178 p->fillRect( 4, 2, mBoxWidth-2, h-3, mColor );
179}
180
181
182int ColorListItem::height(const TQListBox *lb ) const
183{
184 return( lb->fontMetrics().lineSpacing()+1 );
185}
186
187
188int ColorListItem::width(const TQListBox *lb ) const
189{
190 return( mBoxWidth + lb->fontMetrics().width( text() ) + 6 );
191}
192
193#include "colorlistbox.moc"