kmail Library API Documentation

colorlistbox.cpp

00001 /*
00002  *   kmail: KDE mail client
00003  *   This file: Copyright (C) 2000 Espen Sand, espen@kde.org
00004  *
00005  *   This program is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   This program is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with this program; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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   }
00063 }
00064 
00065 
00066 QColor ColorListBox::color( uint index ) const
00067 {
00068   if( index < count() )
00069   {
00070     ColorListItem *colorItem = (ColorListItem*)item(index);
00071     return( colorItem->color() );
00072   }
00073   else
00074   {
00075     return( black );
00076   }
00077 }
00078 
00079 
00080 void ColorListBox::newColor( int index )
00081 {
00082   if( isEnabled() == false )
00083   {
00084     return;
00085   }
00086 
00087   if( (uint)index < count() )
00088   {
00089     QColor c = color( index );
00090     if( KColorDialog::getColor( c, this ) != QDialog::Rejected )
00091     {
00092       setColor( index, c );
00093     }
00094   }
00095 }
00096 
00097 
00098 void ColorListBox::dragEnterEvent( QDragEnterEvent *e )
00099 {
00100   if( KColorDrag::canDecode(e) && isEnabled() )
00101   {
00102     mCurrentOnDragEnter = currentItem();
00103     e->accept( true );
00104   }
00105   else
00106   {
00107     mCurrentOnDragEnter = -1;
00108     e->accept( false );
00109   }
00110 }
00111 
00112 
00113 void ColorListBox::dragLeaveEvent( QDragLeaveEvent * )
00114 {
00115   if( mCurrentOnDragEnter != -1 )
00116   {
00117     setCurrentItem( mCurrentOnDragEnter );
00118     mCurrentOnDragEnter = -1;
00119   }
00120 }
00121 
00122 
00123 void ColorListBox::dragMoveEvent( QDragMoveEvent *e )
00124 {
00125   if( KColorDrag::canDecode(e) && isEnabled() )
00126   {
00127     ColorListItem *item = (ColorListItem*)itemAt( e->pos() );
00128     if( item != 0 )
00129     {
00130       setCurrentItem ( item );
00131     }
00132   }
00133 }
00134 
00135 
00136 void ColorListBox::dropEvent( QDropEvent *e )
00137 {
00138   QColor color;
00139   if( KColorDrag::decode( e, color ) )
00140   {
00141     int index = currentItem();
00142     if( index != -1 )
00143     {
00144       ColorListItem *colorItem = (ColorListItem*)item(index);
00145       colorItem->setColor(color);
00146       triggerUpdate( false ); // Redraw item
00147     }
00148     mCurrentOnDragEnter = -1;
00149   }
00150 }
00151 
00152 
00153 
00154 ColorListItem::ColorListItem( const QString &text, const QColor &color )
00155   : QListBoxItem(), mColor( color ), mBoxWidth( 30 )
00156 {
00157   setText( text );
00158 }
00159 
00160 
00161 const QColor &ColorListItem::color( void )
00162 {
00163   return( mColor );
00164 }
00165 
00166 
00167 void ColorListItem::setColor( const QColor &color )
00168 {
00169   mColor = color;
00170 }
00171 
00172 
00173 void ColorListItem::paint( QPainter *p )
00174 {
00175   QFontMetrics fm = p->fontMetrics();
00176   int h = fm.height();
00177 
00178   p->drawText( mBoxWidth+3*2, fm.ascent() + fm.leading()/2, text() );
00179 
00180   p->setPen( Qt::black );
00181   p->drawRect( 3, 1, mBoxWidth, h-1 );
00182   p->fillRect( 4, 2, mBoxWidth-2, h-3, mColor );
00183 }
00184 
00185 
00186 int ColorListItem::height(const QListBox *lb ) const
00187 {
00188   return( lb->fontMetrics().lineSpacing()+1 );
00189 }
00190 
00191 
00192 int ColorListItem::width(const QListBox *lb ) const
00193 {
00194   return( mBoxWidth + lb->fontMetrics().width( text() ) + 6 );
00195 }
00196 
00197 #include "colorlistbox.moc"
KDE Logo
This file is part of the documentation for kmail Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:37:13 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003