Diffstat (limited to 'kaddressbook/views/colorlistbox.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kaddressbook/views/colorlistbox.cpp | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/kaddressbook/views/colorlistbox.cpp b/kaddressbook/views/colorlistbox.cpp new file mode 100644 index 0000000..c243fa0 --- a/dev/null +++ b/kaddressbook/views/colorlistbox.cpp | |||
@@ -0,0 +1,230 @@ | |||
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
18 | * | ||
19 | */ | ||
20 | |||
21 | #include <qpainter.h> | ||
22 | |||
23 | #include <kcolordialog.h> | ||
24 | |||
25 | #ifndef KAB_EMBEDDED | ||
26 | #include <kcolordrag.h> | ||
27 | #endif //KAB_EMBEDDED | ||
28 | |||
29 | #include "colorlistbox.h" | ||
30 | |||
31 | ColorListBox::ColorListBox( QWidget *parent, const char *name, WFlags f ) | ||
32 | :KListBox( parent, name, f ), mCurrentOnDragEnter(-1) | ||
33 | { | ||
34 | connect( this, SIGNAL(selected(int)), this, SLOT(newColor(int)) ); | ||
35 | setAcceptDrops( true); | ||
36 | } | ||
37 | |||
38 | |||
39 | void ColorListBox::setEnabled( bool state ) | ||
40 | { | ||
41 | if( state == isEnabled() ) | ||
42 | { | ||
43 | return; | ||
44 | } | ||
45 | |||
46 | QListBox::setEnabled( state ); | ||
47 | for( uint i=0; i<count(); i++ ) | ||
48 | { | ||
49 | updateItem( i ); | ||
50 | } | ||
51 | } | ||
52 | |||
53 | |||
54 | void ColorListBox::setColor( uint index, const QColor &color ) | ||
55 | { | ||
56 | if( index < count() ) | ||
57 | { | ||
58 | ColorListItem *colorItem = (ColorListItem*)item(index); | ||
59 | colorItem->setColor(color); | ||
60 | updateItem( colorItem ); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | |||
65 | QColor ColorListBox::color( uint index ) const | ||
66 | { | ||
67 | if( index < count() ) | ||
68 | { | ||
69 | ColorListItem *colorItem = (ColorListItem*)item(index); | ||
70 | return( colorItem->color() ); | ||
71 | } | ||
72 | else | ||
73 | { | ||
74 | return( black ); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | |||
79 | void ColorListBox::newColor( int index ) | ||
80 | { | ||
81 | if( isEnabled() == false ) | ||
82 | { | ||
83 | return; | ||
84 | } | ||
85 | |||
86 | if( (uint)index < count() ) | ||
87 | { | ||
88 | QColor c = color( index ); | ||
89 | #ifndef KAB_EMBEDDED | ||
90 | if( KColorDialog::getColor( c, this ) != QDialog::Rejected ) | ||
91 | { | ||
92 | setColor( index, c ); | ||
93 | } | ||
94 | #else //KAB_EMBEDDED | ||
95 | KColorDialog* k = new KColorDialog( this ); | ||
96 | k->setColor( c ); | ||
97 | int res = k->exec(); | ||
98 | if ( res ) { | ||
99 | setColor( index, k->getColor() ); | ||
100 | } | ||
101 | delete k; | ||
102 | #endif //KAB_EMBEDDED | ||
103 | |||
104 | } | ||
105 | } | ||
106 | |||
107 | |||
108 | void ColorListBox::dragEnterEvent( QDragEnterEvent *e ) | ||
109 | { | ||
110 | #ifndef KAB_EMBEDDED | ||
111 | if( KColorDrag::canDecode(e) && isEnabled() ) | ||
112 | { | ||
113 | mCurrentOnDragEnter = currentItem(); | ||
114 | e->accept( true ); | ||
115 | } | ||
116 | else | ||
117 | { | ||
118 | mCurrentOnDragEnter = -1; | ||
119 | e->accept( false ); | ||
120 | } | ||
121 | #else //KAB_EMBEDDED | ||
122 | qDebug("ColorListBox::dragEnterEvent drag&drop currently not supported"); | ||
123 | #endif //KAB_EMBEDDED | ||
124 | |||
125 | } | ||
126 | |||
127 | |||
128 | void ColorListBox::dragLeaveEvent( QDragLeaveEvent * ) | ||
129 | { | ||
130 | #ifndef KAB_EMBEDDED | ||
131 | |||
132 | if( mCurrentOnDragEnter != -1 ) | ||
133 | { | ||
134 | setCurrentItem( mCurrentOnDragEnter ); | ||
135 | mCurrentOnDragEnter = -1; | ||
136 | } | ||
137 | #else //KAB_EMBEDDED | ||
138 | qDebug("ColorListBox::dragLeaveEvent drag&drop currently not supported"); | ||
139 | #endif //KAB_EMBEDDED | ||
140 | } | ||
141 | |||
142 | |||
143 | void ColorListBox::dragMoveEvent( QDragMoveEvent *e ) | ||
144 | { | ||
145 | #ifndef KAB_EMBEDDED | ||
146 | if( KColorDrag::canDecode(e) && isEnabled() ) | ||
147 | { | ||
148 | ColorListItem *item = (ColorListItem*)itemAt( e->pos() ); | ||
149 | if( item != 0 ) | ||
150 | { | ||
151 | setCurrentItem ( item ); | ||
152 | } | ||
153 | } | ||
154 | #else //KAB_EMBEDDED | ||
155 | qDebug("ColorListBox::dragMoveEvent drag&drop currently not supported"); | ||
156 | #endif //KAB_EMBEDDED | ||
157 | |||
158 | } | ||
159 | |||
160 | |||
161 | void ColorListBox::dropEvent( QDropEvent *e ) | ||
162 | { | ||
163 | #ifndef KAB_EMBEDDED | ||
164 | QColor color; | ||
165 | if( KColorDrag::decode( e, color ) ) | ||
166 | { | ||
167 | int index = currentItem(); | ||
168 | if( index != -1 ) | ||
169 | { | ||
170 | ColorListItem *colorItem = (ColorListItem*)item(index); | ||
171 | colorItem->setColor(color); | ||
172 | triggerUpdate( false ); // Redraw item | ||
173 | } | ||
174 | mCurrentOnDragEnter = -1; | ||
175 | } | ||
176 | |||
177 | #else //KAB_EMBEDDED | ||
178 | qDebug("ColorListBox::dropEvent drag&drop currently not supported"); | ||
179 | #endif //KAB_EMBEDDED | ||
180 | |||
181 | } | ||
182 | |||
183 | |||
184 | |||
185 | ColorListItem::ColorListItem( const QString &text, const QColor &color ) | ||
186 | : QListBoxItem(), mColor( color ), mBoxWidth( 30 ) | ||
187 | { | ||
188 | setText( text ); | ||
189 | } | ||
190 | |||
191 | |||
192 | const QColor &ColorListItem::color( void ) | ||
193 | { | ||
194 | return( mColor ); | ||
195 | } | ||
196 | |||
197 | |||
198 | void ColorListItem::setColor( const QColor &color ) | ||
199 | { | ||
200 | mColor = color; | ||
201 | } | ||
202 | |||
203 | |||
204 | void ColorListItem::paint( QPainter *p ) | ||
205 | { | ||
206 | QFontMetrics fm = p->fontMetrics(); | ||
207 | int h = fm.height(); | ||
208 | |||
209 | p->drawText( mBoxWidth+3*2, fm.ascent() + fm.leading()/2, text() ); | ||
210 | |||
211 | p->setPen( Qt::black ); | ||
212 | p->drawRect( 3, 1, mBoxWidth, h-1 ); | ||
213 | p->fillRect( 4, 2, mBoxWidth-2, h-3, mColor ); | ||
214 | } | ||
215 | |||
216 | |||
217 | int ColorListItem::height(const QListBox *lb ) const | ||
218 | { | ||
219 | return( lb->fontMetrics().lineSpacing()+1 ); | ||
220 | } | ||
221 | |||
222 | |||
223 | int ColorListItem::width(const QListBox *lb ) const | ||
224 | { | ||
225 | return( mBoxWidth + lb->fontMetrics().width( text() ) + 6 ); | ||
226 | } | ||
227 | |||
228 | #ifndef KAB_EMBEDDED | ||
229 | #include "colorlistbox.moc" | ||
230 | #endif //KAB_EMBEDDED | ||