KDEUI
kcolorvalueselector.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1997 Martin Jones (mjones@kde.org) 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kcolorvalueselector.h" 00021 00022 #include <QtGui/QPainter> 00023 00024 #include "kcolorchoosermode_p.h" 00025 00026 using namespace KDEPrivate; 00027 00028 class KColorValueSelector::Private 00029 { 00030 public: 00031 Private(KColorValueSelector *q): q(q), _hue(0), _sat(0), _colorValue(0), _mode(ChooserClassic) {} 00032 00033 KColorValueSelector *q; 00034 int _hue; 00035 int _sat; 00036 int _colorValue; 00037 KColorChooserMode _mode; 00038 QPixmap pixmap; 00039 }; 00040 00041 KColorValueSelector::KColorValueSelector( QWidget *parent ) 00042 : KSelector( Qt::Vertical, parent ), d( new Private( this ) ) 00043 { 00044 setRange( 0, 255 ); 00045 } 00046 00047 KColorValueSelector::KColorValueSelector( Qt::Orientation o, QWidget *parent ) 00048 : KSelector( o, parent ), d( new Private( this ) ) 00049 { 00050 setRange( 0, 255 ); 00051 } 00052 00053 KColorValueSelector::~KColorValueSelector() 00054 { 00055 delete d; 00056 } 00057 00058 int KColorValueSelector::hue() const 00059 { 00060 return d->_hue; 00061 } 00062 00063 void KColorValueSelector::setHue( int hue ) 00064 { 00065 d->_hue = hue; 00066 } 00067 00068 int KColorValueSelector::saturation() const 00069 { 00070 return d->_sat; 00071 } 00072 00073 void KColorValueSelector::setSaturation( int saturation ) 00074 { 00075 d->_sat = saturation; 00076 } 00077 00078 int KColorValueSelector::colorValue () const 00079 { 00080 return d->_colorValue; 00081 } 00082 00083 void KColorValueSelector::setColorValue ( int colorValue ) 00084 { 00085 d->_colorValue = colorValue; 00086 } 00087 00088 00089 00090 void KColorValueSelector::updateContents() 00091 { 00092 drawPalette( &d->pixmap ); 00093 } 00094 00095 void KColorValueSelector::resizeEvent( QResizeEvent * ) 00096 { 00097 updateContents(); 00098 } 00099 00100 void KColorValueSelector::drawContents( QPainter *painter ) 00101 { 00102 painter->drawPixmap( contentsRect().x(), contentsRect().y(), d->pixmap ); 00103 } 00104 00105 void KColorValueSelector::setChooserMode( KColorChooserMode c ) 00106 { 00107 if ( c == ChooserHue ) { 00108 setRange( 0, 360 ); 00109 } else { 00110 setRange( 0, 255 ); 00111 } 00112 d->_mode = c; 00113 00114 //really needed? 00115 //emit modeChanged(); 00116 } 00117 00118 KColorChooserMode KColorValueSelector::chooserMode () const 00119 { 00120 return d->_mode; 00121 } 00122 00123 void KColorValueSelector::drawPalette( QPixmap *pixmap ) 00124 { 00125 QColor color; 00126 if (chooserMode() == ChooserHue) { 00127 color.setHsv(hue(), 255, 255); 00128 } else { 00129 color.setHsv(hue(), saturation(), colorValue()); 00130 } 00131 00132 QLinearGradient gradient; 00133 if (orientation() == Qt::Vertical) { 00134 gradient.setStart(0, contentsRect().height()); 00135 gradient.setFinalStop(0, 0); 00136 } else { 00137 gradient.setStart(0, 0); 00138 gradient.setFinalStop(contentsRect().width(), 0); 00139 } 00140 00141 const int steps = componentValueSteps(chooserMode()); 00142 for (int v = 0; v <= steps; ++v) { 00143 setComponentValue(color, chooserMode(), v * (1.0 / steps)); 00144 gradient.setColorAt(v * (1.0 / steps), color); 00145 } 00146 00147 *pixmap = QPixmap(contentsRect().size()); 00148 QPainter painter(pixmap); 00149 painter.fillRect(pixmap->rect(), gradient); 00150 } 00151 00152 00153 #include "kcolorvalueselector.moc"
KDE 4.6 API Reference