kaddressbook
colorlistbox.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qpainter.h>
00022
00023 #include <kcolordialog.h>
00024 #include <kcolordrag.h>
00025
00026 #include "colorlistbox.h"
00027
00028 ColorListBox::ColorListBox( QWidget *parent, const char *name, WFlags f )
00029 :KListBox( parent, name, f ), mCurrentOnDragEnter(-1)
00030 {
00031 connect( this, SIGNAL(selected(int)), this, SLOT(newColor(int)) );
00032 setAcceptDrops( true);
00033 }
00034
00035
00036 void ColorListBox::setEnabled( bool state )
00037 {
00038 if( state == isEnabled() )
00039 {
00040 return;
00041 }
00042
00043 QListBox::setEnabled( state );
00044 for( uint i=0; i<count(); i++ )
00045 {
00046 updateItem( i );
00047 }
00048 }
00049
00050
00051 void ColorListBox::setColor( uint index, const QColor &color )
00052 {
00053 if( index < count() )
00054 {
00055 ColorListItem *colorItem = (ColorListItem*)item(index);
00056 colorItem->setColor(color);
00057 updateItem( colorItem );
00058 }
00059 }
00060
00061
00062 QColor ColorListBox::color( uint index ) const
00063 {
00064 if( index < count() )
00065 {
00066 ColorListItem *colorItem = (ColorListItem*)item(index);
00067 return( colorItem->color() );
00068 }
00069 else
00070 {
00071 return( black );
00072 }
00073 }
00074
00075
00076 void ColorListBox::newColor( int index )
00077 {
00078 if( isEnabled() == false )
00079 {
00080 return;
00081 }
00082
00083 if( (uint)index < count() )
00084 {
00085 QColor c = color( index );
00086 if( KColorDialog::getColor( c, this ) != QDialog::Rejected )
00087 {
00088 setColor( index, c );
00089 }
00090 }
00091 }
00092
00093
00094 void ColorListBox::dragEnterEvent( QDragEnterEvent *e )
00095 {
00096 if( KColorDrag::canDecode(e) && isEnabled() )
00097 {
00098 mCurrentOnDragEnter = currentItem();
00099 e->accept( true );
00100 }
00101 else
00102 {
00103 mCurrentOnDragEnter = -1;
00104 e->accept( false );
00105 }
00106 }
00107
00108
00109 void ColorListBox::dragLeaveEvent( QDragLeaveEvent * )
00110 {
00111 if( mCurrentOnDragEnter != -1 )
00112 {
00113 setCurrentItem( mCurrentOnDragEnter );
00114 mCurrentOnDragEnter = -1;
00115 }
00116 }
00117
00118
00119 void ColorListBox::dragMoveEvent( QDragMoveEvent *e )
00120 {
00121 if( KColorDrag::canDecode(e) && isEnabled() )
00122 {
00123 ColorListItem *item = (ColorListItem*)itemAt( e->pos() );
00124 if( item != 0 )
00125 {
00126 setCurrentItem ( item );
00127 }
00128 }
00129 }
00130
00131
00132 void ColorListBox::dropEvent( QDropEvent *e )
00133 {
00134 QColor color;
00135 if( KColorDrag::decode( e, color ) )
00136 {
00137 int index = currentItem();
00138 if( index != -1 )
00139 {
00140 ColorListItem *colorItem = (ColorListItem*)item(index);
00141 colorItem->setColor(color);
00142 triggerUpdate( false );
00143 }
00144 mCurrentOnDragEnter = -1;
00145 }
00146 }
00147
00148
00149
00150 ColorListItem::ColorListItem( const QString &text, const QColor &color )
00151 : QListBoxItem(), mColor( color ), mBoxWidth( 30 )
00152 {
00153 setText( text );
00154 }
00155
00156
00157 const QColor &ColorListItem::color( void )
00158 {
00159 return( mColor );
00160 }
00161
00162
00163 void ColorListItem::setColor( const QColor &color )
00164 {
00165 mColor = color;
00166 }
00167
00168
00169 void ColorListItem::paint( QPainter *p )
00170 {
00171 QFontMetrics fm = p->fontMetrics();
00172 int h = fm.height();
00173
00174 p->drawText( mBoxWidth+3*2, fm.ascent() + fm.leading()/2, text() );
00175
00176 p->setPen( Qt::black );
00177 p->drawRect( 3, 1, mBoxWidth, h-1 );
00178 p->fillRect( 4, 2, mBoxWidth-2, h-3, mColor );
00179 }
00180
00181
00182 int ColorListItem::height(const QListBox *lb ) const
00183 {
00184 return( lb->fontMetrics().lineSpacing()+1 );
00185 }
00186
00187
00188 int ColorListItem::width(const QListBox *lb ) const
00189 {
00190 return( mBoxWidth + lb->fontMetrics().width( text() ) + 6 );
00191 }
00192
00193 #include "colorlistbox.moc"
|