File indexing completed on 2024-04-14 14:20:17

0001 /* This file is part of the KDE libraries
0002    Copyright (C) 1997 Martin Jones (mjones@kde.org)
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License as published by the Free Software Foundation; either
0007     version 2 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Library General Public License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to
0016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kcolorvalueselector.h"
0021 
0022 #include <QPainter>
0023 
0024 #include "kcolorchoosermode_p.h"
0025 
0026 using namespace KDEPrivate;
0027 
0028 class Q_DECL_HIDDEN KColorValueSelector::Private
0029 {
0030 public:
0031     Private(KColorValueSelector *q): q(q), _hue(0), _sat(0), _colorValue(0), _mode(ChooserClassic) {}
0032 
0033     KColorValueSelector *q;
0034     int _hue;
0035     int _sat;
0036     int _colorValue;
0037     KColorChooserMode _mode;
0038     QPixmap pixmap;
0039 };
0040 
0041 KColorValueSelector::KColorValueSelector(QWidget *parent)
0042     : KSelector(Qt::Vertical, parent), d(new Private(this))
0043 {
0044     setRange(0, 255);
0045 }
0046 
0047 KColorValueSelector::KColorValueSelector(Qt::Orientation o, QWidget *parent)
0048     : KSelector(o, parent), d(new Private(this))
0049 {
0050     setRange(0, 255);
0051 }
0052 
0053 KColorValueSelector::~KColorValueSelector()
0054 {
0055     delete d;
0056 }
0057 
0058 int KColorValueSelector::hue() const
0059 {
0060     return d->_hue;
0061 }
0062 
0063 void KColorValueSelector::setHue(int hue)
0064 {
0065     d->_hue = hue;
0066 }
0067 
0068 int KColorValueSelector::saturation() const
0069 {
0070     return d->_sat;
0071 }
0072 
0073 void KColorValueSelector::setSaturation(int saturation)
0074 {
0075     d->_sat = saturation;
0076 }
0077 
0078 int KColorValueSelector::colorValue() const
0079 {
0080     return d->_colorValue;
0081 }
0082 
0083 void KColorValueSelector::setColorValue(int colorValue)
0084 {
0085     d->_colorValue = colorValue;
0086 }
0087 
0088 void KColorValueSelector::updateContents()
0089 {
0090     drawPalette(&d->pixmap);
0091 }
0092 
0093 void KColorValueSelector::resizeEvent(QResizeEvent *)
0094 {
0095     updateContents();
0096 }
0097 
0098 void KColorValueSelector::drawContents(QPainter *painter)
0099 {
0100     painter->drawPixmap(contentsRect().x(), contentsRect().y(), d->pixmap);
0101 }
0102 
0103 void KColorValueSelector::setChooserMode(KColorChooserMode c)
0104 {
0105     if (c == ChooserHue) {
0106         setRange(0, 360);
0107     } else {
0108         setRange(0, 255);
0109     }
0110     d->_mode = c;
0111 
0112     //really needed?
0113     //emit modeChanged();
0114 }
0115 
0116 KColorChooserMode KColorValueSelector::chooserMode() const
0117 {
0118     return d->_mode;
0119 }
0120 
0121 void KColorValueSelector::drawPalette(QPixmap *pixmap)
0122 {
0123     QColor color;
0124     if (chooserMode() == ChooserHue) {
0125         color.setHsv(hue(), 255, 255);
0126     } else {
0127         color.setHsv(hue(), saturation(), colorValue());
0128     }
0129 
0130     QLinearGradient gradient;
0131     if (orientation() == Qt::Vertical) {
0132         gradient.setStart(0, contentsRect().height());
0133         gradient.setFinalStop(0, 0);
0134     } else {
0135         gradient.setStart(0, 0);
0136         gradient.setFinalStop(contentsRect().width(), 0);
0137     }
0138 
0139     const int steps = componentValueSteps(chooserMode());
0140     for (int v = 0; v <= steps; ++v) {
0141         setComponentValue(color, chooserMode(), v * (1.0 / steps));
0142         gradient.setColorAt(v * (1.0 / steps), color);
0143     }
0144 
0145     *pixmap = QPixmap(contentsRect().size());
0146     QPainter painter(pixmap);
0147     painter.fillRect(pixmap->rect(), gradient);
0148 }
0149 
0150 #include "moc_kcolorvalueselector.cpp"