00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
#ifndef PURE_QT
00021
#include <klocale.h>
00022
#else
00023
#include "qlocale.h"
00024
#endif
00025
00026
#include <qpainter.h>
00027
#include <qpixmap.h>
00028
00029
#include "mycolorcombo.h"
00030
00031
#ifndef KDE_USE_FINAL
00032 #define STANDARD_PAL_SIZE 17
00033
00034 static QColor *
standardPalette = 0;
00035
00036 static void createStandardPalette()
00037 {
00038
if (
standardPalette )
00039
return;
00040
00041
standardPalette =
new QColor [
STANDARD_PAL_SIZE];
00042
00043
int i = 0;
00044
00045
standardPalette[i++] = Qt::red;
00046
standardPalette[i++] = Qt::green;
00047
standardPalette[i++] = Qt::blue;
00048
standardPalette[i++] = Qt::cyan;
00049
standardPalette[i++] = Qt::magenta;
00050
standardPalette[i++] = Qt::yellow;
00051
standardPalette[i++] = Qt::darkRed;
00052
standardPalette[i++] = Qt::darkGreen;
00053
standardPalette[i++] = Qt::darkBlue;
00054
standardPalette[i++] = Qt::darkCyan;
00055
standardPalette[i++] = Qt::darkMagenta;
00056
standardPalette[i++] = Qt::darkYellow;
00057
standardPalette[i++] = Qt::white;
00058
standardPalette[i++] = Qt::lightGray;
00059
standardPalette[i++] = Qt::gray;
00060
standardPalette[i++] = Qt::darkGray;
00061
standardPalette[i++] = Qt::black;
00062 }
00063
#endif
00064
00065 class KColorCombo::KColorComboPrivate
00066 {
00067
protected:
00068
friend class KColorCombo;
00069 KColorComboPrivate(){}
00070 ~KColorComboPrivate(){}
00071 bool showEmptyList;
00072 };
00073
00074
KColorCombo::KColorCombo(
QWidget *parent,
const char *name )
00075 :
QComboBox( parent, name )
00076 {
00077 d=
new KColorComboPrivate();
00078 d->showEmptyList=
false;
00079
00080 customColor.setRgb( 255, 255, 255 );
00081 internalcolor.setRgb( 255, 255, 255 );
00082
00083
createStandardPalette();
00084
00085 addColors();
00086
00087 connect(
this, SIGNAL( activated(
int) ), SLOT( slotActivated(
int) ) );
00088 connect(
this, SIGNAL( highlighted(
int) ), SLOT( slotHighlighted(
int) ) );
00089 }
00090
00091
00092 KColorCombo::~KColorCombo()
00093 {
00094
delete d;
00095 }
00099
void KColorCombo::setColor(
const QColor &col )
00100 {
00101 internalcolor = col;
00102 d->
showEmptyList=
false;
00103 addColors();
00104 }
00105
00106
00110
QColor KColorCombo::color()
const {
00111
return internalcolor;
00112 }
00113
00114
void KColorCombo::resizeEvent(
QResizeEvent *re )
00115 {
00116 QComboBox::resizeEvent( re );
00117
00118 addColors();
00119 }
00120
00124
void KColorCombo::showEmptyList()
00125 {
00126 d->
showEmptyList=
true;
00127 addColors();
00128 }
00129
00130
void KColorCombo::slotActivated(
int index )
00131 {
00132
if ( index == 0 )
00133 {
00134
if (
QColorDialog::getColor( customColor,
this ) == QDialog::Accepted )
00135 {
00136
QPainter painter;
00137
QPen pen;
00138
QRect rect( 0, 0, width(),
QFontMetrics(painter.font()).height()+4);
00139
QPixmap pixmap( rect.width(), rect.height() );
00140
00141
if ( qGray( customColor.rgb() ) < 128 )
00142 pen.setColor( white );
00143
else
00144 pen.setColor( black );
00145
00146 painter.begin( &pixmap );
00147
QBrush brush( customColor );
00148 painter.fillRect( rect, brush );
00149 painter.setPen( pen );
00150 painter.drawText( 2,
QFontMetrics(painter.font()).ascent()+2, i18n(
"Custom...") );
00151 painter.end();
00152
00153 changeItem( pixmap, 0 );
00154 pixmap.detach();
00155 }
00156
00157 internalcolor = customColor;
00158 }
00159
else
00160 internalcolor =
standardPalette[ index - 1 ];
00161
00162 emit
activated( internalcolor );
00163 }
00164
00165
void KColorCombo::slotHighlighted(
int index )
00166 {
00167
if ( index == 0 )
00168 internalcolor = customColor;
00169
else
00170 internalcolor =
standardPalette[ index - 1 ];
00171
00172 emit
highlighted( internalcolor );
00173 }
00174
00175
void KColorCombo::addColors()
00176 {
00177
QPainter painter;
00178
QPen pen;
00179
QRect rect( 0, 0, width(),
QFontMetrics(painter.font()).height()+4 );
00180
QPixmap pixmap( rect.width(), rect.height() );
00181
int i;
00182
00183
clear();
00184
if (d->
showEmptyList)
return;
00185
00186
createStandardPalette();
00187
00188
for ( i = 0; i <
STANDARD_PAL_SIZE; i++ )
00189
if (
standardPalette[i] == internalcolor )
break;
00190
00191
if ( i ==
STANDARD_PAL_SIZE )
00192 customColor = internalcolor;
00193
00194
if ( qGray( customColor.rgb() ) < 128 )
00195 pen.setColor( white );
00196
else
00197 pen.setColor( black );
00198
00199 painter.begin( &pixmap );
00200
QBrush brush( customColor );
00201 painter.fillRect( rect, brush );
00202 painter.setPen( pen );
00203 painter.drawText( 2,
QFontMetrics(painter.font()).ascent()+2, i18n(
"Custom...") );
00204 painter.end();
00205
00206 insertItem( pixmap );
00207 pixmap.detach();
00208
00209
for ( i = 0; i <
STANDARD_PAL_SIZE; i++ )
00210 {
00211 painter.begin( &pixmap );
00212
QBrush brush( standardPalette[i] );
00213 painter.fillRect( rect, brush );
00214 painter.end();
00215
00216 insertItem( pixmap );
00217 pixmap.detach();
00218
00219
if (
standardPalette[i] == internalcolor )
00220 setCurrentItem( i + 1 );
00221 }
00222 }
00223
00224
void KColorCombo::virtual_hook(
int,
void* )
00225 { }
00226
00227
#ifndef PURE_QT
00228
#include "mycolorcombo.moc"
00229
#endif