kmail

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#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <tqpainter.h>
26
27#include <kcolordialog.h>
28#include <kcolordrag.h>
29
30#include "colorlistbox.h"
31
32ColorListBox::ColorListBox( TQWidget *parent, const char *name, WFlags f )
33 :TDEListBox( parent, name, f ), mCurrentOnDragEnter(-1)
34{
35 connect( this, TQ_SIGNAL(selected(int)), this, TQ_SLOT(newColor(int)) );
36 setAcceptDrops( true);
37}
38
39
40void ColorListBox::setEnabled( bool state )
41{
42 if( state == isEnabled() )
43 {
44 return;
45 }
46
47 TQListBox::setEnabled( state );
48 for( uint i=0; i<count(); i++ )
49 {
50 updateItem( i );
51 }
52}
53
54
55void ColorListBox::setColor( uint index, const TQColor &color )
56{
57 if( index < count() )
58 {
59 ColorListItem *colorItem = (ColorListItem*)item(index);
60 colorItem->setColor(color);
61 updateItem( colorItem );
62 emit changed();
63 }
64}
65
66
67TQColor ColorListBox::color( uint index ) const
68{
69 if( index < count() )
70 {
71 ColorListItem *colorItem = (ColorListItem*)item(index);
72 return( colorItem->color() );
73 }
74 else
75 {
76 return( black );
77 }
78}
79
80
81void ColorListBox::newColor( int index )
82{
83 if( isEnabled() == false )
84 {
85 return;
86 }
87
88 if( (uint)index < count() )
89 {
90 TQColor c = color( index );
91 if( KColorDialog::getColor( c, this ) != TQDialog::Rejected )
92 {
93 setColor( index, c );
94 }
95 }
96}
97
98
99void ColorListBox::dragEnterEvent( TQDragEnterEvent *e )
100{
101 if( KColorDrag::canDecode(e) && isEnabled() )
102 {
103 mCurrentOnDragEnter = currentItem();
104 e->accept( true );
105 }
106 else
107 {
108 mCurrentOnDragEnter = -1;
109 e->accept( false );
110 }
111}
112
113
114void ColorListBox::dragLeaveEvent( TQDragLeaveEvent * )
115{
116 if( mCurrentOnDragEnter != -1 )
117 {
118 setCurrentItem( mCurrentOnDragEnter );
119 mCurrentOnDragEnter = -1;
120 }
121}
122
123
124void ColorListBox::dragMoveEvent( TQDragMoveEvent *e )
125{
126 if( KColorDrag::canDecode(e) && isEnabled() )
127 {
128 ColorListItem *item = (ColorListItem*)itemAt( e->pos() );
129 if( item != 0 )
130 {
131 setCurrentItem ( item );
132 }
133 }
134}
135
136
137void ColorListBox::dropEvent( TQDropEvent *e )
138{
139 TQColor color;
140 if( KColorDrag::decode( e, color ) )
141 {
142 int index = currentItem();
143 if( index != -1 )
144 {
145 ColorListItem *colorItem = (ColorListItem*)item(index);
146 colorItem->setColor(color);
147 triggerUpdate( false ); // Redraw item
148 }
149 mCurrentOnDragEnter = -1;
150 }
151}
152
153
154
155ColorListItem::ColorListItem( const TQString &text, const TQColor &color )
156 : TQListBoxItem(), mColor( color ), mBoxWidth( 30 )
157{
158 setText( text );
159}
160
161
162const TQColor &ColorListItem::color( void )
163{
164 return( mColor );
165}
166
167
168void ColorListItem::setColor( const TQColor &color )
169{
170 mColor = color;
171}
172
173
174void ColorListItem::paint( TQPainter *p )
175{
176 TQFontMetrics fm = p->fontMetrics();
177 int h = fm.height();
178
179 p->drawText( mBoxWidth+3*2, fm.ascent() + fm.leading()/2, text() );
180
181 p->setPen( TQt::black );
182 p->drawRect( 3, 1, mBoxWidth, h-1 );
183 p->fillRect( 4, 2, mBoxWidth-2, h-3, mColor );
184}
185
186
187int ColorListItem::height(const TQListBox *lb ) const
188{
189 return( lb->fontMetrics().lineSpacing()+1 );
190}
191
192
193int ColorListItem::width(const TQListBox *lb ) const
194{
195 return( mBoxWidth + lb->fontMetrics().width( text() ) + 6 );
196}
197
198#include "colorlistbox.moc"