kcolorcombo.cpp
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 
00035 #include <qdrawutil.h>
00036 #include <qevent.h>
00037 #include <qfile.h>
00038 #include <qimage.h>
00039 #include <qlabel.h>
00040 #include <qlayout.h>
00041 #include <qlineedit.h>
00042 #include <qvalidator.h>
00043 #include <qpainter.h>
00044 #include <qpushbutton.h>
00045 #include <qtimer.h>
00046 
00047 #include <kapplication.h>
00048 #include <kconfig.h>
00049 #include <kglobal.h>
00050 #include <kglobalsettings.h>
00051 #include <kiconloader.h>
00052 #include <klistbox.h>
00053 #include <klocale.h>
00054 #include <kmessagebox.h>
00055 #include <kseparator.h>
00056 #include <kpalette.h>
00057 #include <kimageeffect.h>
00058 
00059 
00060 
00061 #include "kcolorcombo.h"
00062 
00063 
00064 
00065 
00066 
00067 
00068 #ifndef KDE_USE_FINAL
00069 #define STANDARD_PAL_SIZE 17
00070 
00071 static QColor *standardPalette = 0;
00072 
00073 static void createStandardPalette()
00074 {
00075     if ( standardPalette )
00076     return;
00077 
00078     standardPalette = new QColor [STANDARD_PAL_SIZE];
00079 
00080     int i = 0;
00081 
00082     standardPalette[i++] = Qt::red;
00083     standardPalette[i++] = Qt::green;
00084     standardPalette[i++] = Qt::blue;
00085     standardPalette[i++] = Qt::cyan;
00086     standardPalette[i++] = Qt::magenta;
00087     standardPalette[i++] = Qt::yellow;
00088     standardPalette[i++] = Qt::darkRed;
00089     standardPalette[i++] = Qt::darkGreen;
00090     standardPalette[i++] = Qt::darkBlue;
00091     standardPalette[i++] = Qt::darkCyan;
00092     standardPalette[i++] = Qt::darkMagenta;
00093     standardPalette[i++] = Qt::darkYellow;
00094     standardPalette[i++] = Qt::white;
00095     standardPalette[i++] = Qt::lightGray;
00096     standardPalette[i++] = Qt::gray;
00097     standardPalette[i++] = Qt::darkGray;
00098     standardPalette[i++] = Qt::black;
00099 }
00100 #endif
00101 
00102 class KColorCombo::KColorComboPrivate
00103 {
00104     protected:
00105     friend class KColorCombo;
00106     KColorComboPrivate(){}
00107     ~KColorComboPrivate(){}
00108     bool showEmptyList;
00109 };
00110 
00111 KColorCombo::KColorCombo( QWidget *parent, const char *name )
00112     : QComboBox( parent, name )
00113 {
00114     d=new KColorComboPrivate();
00115     d->showEmptyList=false;
00116 
00117     customColor.setRgb( 255, 255, 255 );
00118     internalcolor.setRgb( 255, 255, 255 );
00119 
00120     createStandardPalette();
00121 
00122     addColors();
00123 
00124     connect( this, SIGNAL( activated(int) ), SLOT( slotActivated(int) ) );
00125     connect( this, SIGNAL( highlighted(int) ), SLOT( slotHighlighted(int) ) );
00126 }
00127 
00128 
00129 KColorCombo::~KColorCombo()
00130 {
00131     delete d;
00132 }
00136 void KColorCombo::setColor( const QColor &col )
00137 {
00138     internalcolor = col;
00139     d->showEmptyList=false;
00140     addColors();
00141 }
00142 
00143 
00147 QColor KColorCombo::color() const {
00148   return internalcolor;
00149 }
00150 
00151 void KColorCombo::resizeEvent( QResizeEvent *re )
00152 {
00153     QComboBox::resizeEvent( re );
00154 
00155     addColors();
00156 }
00157 
00161 void KColorCombo::showEmptyList()
00162 {
00163     d->showEmptyList=true;
00164     addColors();
00165 }
00166 
00167 void KColorCombo::slotActivated( int index )
00168 {
00169     if ( index == 0 )
00170     {
00171         if ( KColorDialog::getColor( customColor, this ) == QDialog::Accepted )
00172         {
00173             QPainter painter;
00174             QPen pen;
00175             QRect rect( 0, 0, width(), QFontMetrics(painter.font()).height()+4);
00176             QPixmap pixmap( rect.width(), rect.height() );
00177 
00178             if ( qGray( customColor.rgb() ) < 128 )
00179                 pen.setColor( white );
00180             else
00181                 pen.setColor( black );
00182 
00183             painter.begin( &pixmap );
00184             QBrush brush( customColor );
00185             painter.fillRect( rect, brush );
00186             painter.setPen( pen );
00187             painter.drawText( 2, QFontMetrics(painter.font()).ascent()+2, i18n("Custom...") );
00188             painter.end();
00189 
00190             changeItem( pixmap, 0 );
00191             pixmap.detach();
00192         }
00193 
00194         internalcolor = customColor;
00195     }
00196     else
00197         internalcolor = standardPalette[ index - 1 ];
00198 
00199     emit activated( internalcolor );
00200 }
00201 
00202 void KColorCombo::slotHighlighted( int index )
00203 {
00204     if ( index == 0 )
00205         internalcolor = customColor;
00206     else
00207         internalcolor = standardPalette[ index - 1 ];
00208 
00209     emit highlighted( internalcolor );
00210 }
00211 
00212 void KColorCombo::addColors()
00213 {
00214     QPainter painter;
00215     QPen pen;
00216     QRect rect( 0, 0, width(), QFontMetrics(painter.font()).height()+4 );
00217     QPixmap pixmap( rect.width(), rect.height() );
00218     int i;
00219 
00220     clear();
00221     if (d->showEmptyList) return;
00222 
00223     createStandardPalette();
00224 
00225     for ( i = 0; i < STANDARD_PAL_SIZE; i++ )
00226         if ( standardPalette[i] == internalcolor ) break;
00227 
00228     if ( i == STANDARD_PAL_SIZE )
00229         customColor = internalcolor;
00230 
00231     if ( qGray( customColor.rgb() ) < 128 )
00232         pen.setColor( white );
00233     else
00234         pen.setColor( black );
00235 
00236     painter.begin( &pixmap );
00237     QBrush brush( customColor );
00238     painter.fillRect( rect, brush );
00239     painter.setPen( pen );
00240     painter.drawText( 2, QFontMetrics(painter.font()).ascent()+2, i18n("Custom...") );
00241     painter.end();
00242 
00243     insertItem( pixmap );
00244     pixmap.detach();
00245 
00246     for ( i = 0; i < STANDARD_PAL_SIZE; i++ )
00247     {
00248         painter.begin( &pixmap );
00249         QBrush brush( standardPalette[i] );
00250         painter.fillRect( rect, brush );
00251         painter.end();
00252 
00253         insertItem( pixmap );
00254         pixmap.detach();
00255 
00256         if ( standardPalette[i] == internalcolor )
00257             setCurrentItem( i + 1 );
00258     }
00259 }
00260 
00261 void KColorCombo::virtual_hook( int, void* )
00262 {  }
00263 
00264 #include "kcolorcombo.moc"
 
This file is part of the documentation for kdeui Library Version 3.2.0.