Warning, file /office/calligra/libs/widgets/KoColorSlider.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2006 Sven Langkamp <sven.langkamp@gmail.com>
0003    Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 #include "KoColorSlider.h"
0021 #include "KoColorSpace.h"
0022 
0023 #include <KoColor.h>
0024 #include <KoMixColorsOp.h>
0025 
0026 #include <QPainter>
0027 #include <QTimer>
0028 #include <QStyleOption>
0029 #include <QPointer>
0030 
0031 #define ARROWSIZE 8
0032 
0033 struct Q_DECL_HIDDEN KoColorSlider::Private
0034 {
0035     Private() : upToDate(false), displayRenderer(nullptr) {}
0036     KoColor minColor;
0037     KoColor maxColor;
0038     QPixmap pixmap;
0039     bool upToDate;
0040     QPointer<KoColorDisplayRendererInterface> displayRenderer;
0041 };
0042 
0043 KoColorSlider::KoColorSlider(QWidget* parent, KoColorDisplayRendererInterface *displayRenderer)
0044   : KSelector(parent)
0045   , d(new Private)
0046 {
0047     setMaximum(255);
0048     d->displayRenderer = displayRenderer;
0049     connect(d->displayRenderer, SIGNAL(displayConfigurationChanged()), SLOT(update()));
0050 }
0051 
0052 KoColorSlider::KoColorSlider(Qt::Orientation o, QWidget *parent, KoColorDisplayRendererInterface *displayRenderer)
0053   : KSelector(o, parent), d(new Private)
0054 {
0055     setMaximum(255);
0056     d->displayRenderer = displayRenderer;
0057     connect(d->displayRenderer, SIGNAL(displayConfigurationChanged()), SLOT(update()));
0058 }
0059 
0060 KoColorSlider::~KoColorSlider()
0061 {
0062     delete d;
0063 }
0064 
0065 void KoColorSlider::setColors(const KoColor& mincolor, const KoColor& maxcolor)
0066 {
0067     d->minColor = mincolor;
0068     d->maxColor = maxcolor;
0069     d->upToDate = false;
0070     QTimer::singleShot(1, this, SLOT(update()));
0071 }
0072 
0073 void KoColorSlider::drawContents( QPainter *painter )
0074 {
0075     QPixmap checker(8, 8);
0076     QPainter p(&checker);
0077     p.fillRect(0, 0, 4, 4, Qt::lightGray);
0078     p.fillRect(4, 0, 4, 4, Qt::darkGray);
0079     p.fillRect(0, 4, 4, 4, Qt::darkGray);
0080     p.fillRect(4, 4, 4, 4, Qt::lightGray);
0081     p.end();
0082     QRect contentsRect_(contentsRect());
0083     painter->fillRect(contentsRect_, QBrush(checker));
0084 
0085     if( !d->upToDate || d->pixmap.isNull() || d->pixmap.width() != contentsRect_.width()
0086         || d->pixmap.height() != contentsRect_.height() )
0087     {
0088         KoColor c = d->minColor; // smart way to fetch colorspace
0089         QColor color;
0090 
0091         const quint8 *colors[2];
0092         colors[0] = d->minColor.data();
0093         colors[1] = d->maxColor.data();
0094 
0095         KoMixColorsOp * mixOp = c.colorSpace()->mixColorsOp();
0096 
0097         QImage image(contentsRect_.width(), contentsRect_.height(), QImage::Format_ARGB32 );
0098 
0099         if( orientation() == Qt::Horizontal ) {
0100             for (int x = 0; x < contentsRect_.width(); x++) {
0101 
0102                 qreal t = static_cast<qreal>(x) / (contentsRect_.width() - 1);
0103 
0104                 qint16 colorWeights[2];
0105                 colorWeights[0] = static_cast<quint8>((1.0 - t) * 255 + 0.5);
0106                 colorWeights[1] = 255 - colorWeights[0];
0107 
0108                 mixOp->mixColors(colors, colorWeights, 2, c.data());
0109 
0110                 if (d->displayRenderer) {
0111                     color = d->displayRenderer->toQColor(c);
0112                 }
0113                 else {
0114                     color = c.toQColor();
0115                 }
0116 
0117                 for (int y = 0; y < contentsRect_.height(); y++)
0118                 image.setPixel(x, y, color.rgba());
0119             }
0120         }
0121         else {
0122             for (int y = 0; y < contentsRect_.height(); y++) {
0123 
0124                 qreal t = static_cast<qreal>(y) / (contentsRect_.height() - 1);
0125 
0126                 qint16 colorWeights[2];
0127                 colorWeights[0] = static_cast<quint8>((t) * 255 + 0.5);
0128                 colorWeights[1] = 255 - colorWeights[0];
0129 
0130                 mixOp->mixColors(colors, colorWeights, 2, c.data());
0131 
0132                 if (d->displayRenderer) {
0133                     color = d->displayRenderer->toQColor(c);
0134                 }
0135                 else {
0136                     color = c.toQColor();
0137                 }
0138 
0139                 for (int x = 0; x < contentsRect_.width(); x++)
0140                 image.setPixel(x, y, color.rgba());
0141             }
0142         }
0143         d->pixmap = QPixmap::fromImage(image);
0144         d->upToDate = true;
0145     }
0146     painter->drawPixmap( contentsRect_, d->pixmap, QRect( 0, 0, d->pixmap.width(), d->pixmap.height()) );
0147 }
0148 
0149 KoColor KoColorSlider::currentColor() const
0150 {
0151     const quint8 *colors[2];
0152     colors[0] = d->minColor.data();
0153     colors[1] = d->maxColor.data();
0154     KoMixColorsOp * mixOp = d->minColor.colorSpace()->mixColorsOp();
0155     KoColor c(d->minColor.colorSpace());
0156     qint16 weights[2];
0157     weights[1] = (value() - minimum()) / qreal(maximum() - minimum()) * 255;
0158     weights[0] = 255 - weights[1];
0159     mixOp->mixColors(colors, weights, 2, c.data());
0160     return c;
0161 }
0162 
0163 void KoColorSlider::drawArrow(QPainter *painter, const QPoint &pos)
0164 {
0165     painter->setPen(QPen(palette().text().color(), 0));
0166     painter->setBrush(palette().text());
0167 
0168     QStyleOption o;
0169     o.initFrom(this);
0170     o.state &= ~QStyle::State_MouseOver;
0171 
0172     if ( orientation() == Qt::Vertical ) {
0173         o.rect = QRect( pos.x(), pos.y() - ARROWSIZE / 2,
0174                         ARROWSIZE, ARROWSIZE );
0175     } else {
0176         o.rect = QRect( pos.x() - ARROWSIZE / 2, pos.y(),
0177                         ARROWSIZE, ARROWSIZE );
0178     }
0179 
0180     QStyle::PrimitiveElement arrowPE;
0181     switch (arrowDirection()) {
0182     case Qt::UpArrow:
0183         arrowPE = QStyle::PE_IndicatorArrowUp;
0184         break;
0185     case Qt::DownArrow:
0186         arrowPE = QStyle::PE_IndicatorArrowDown;
0187         break;
0188     case Qt::RightArrow:
0189         arrowPE = QStyle::PE_IndicatorArrowRight;
0190         break;
0191     case Qt::LeftArrow:
0192     default:
0193         arrowPE = QStyle::PE_IndicatorArrowLeft;
0194         break;
0195     }
0196 
0197     style()->drawPrimitive(arrowPE, &o, painter, this);
0198 
0199 }