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