Krita Source Code Documentation
Loading...
Searching...
No Matches
KoColorSlider.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 SPDX-FileCopyrightText: 2006 Sven Langkamp <sven.langkamp@gmail.com>
3 SPDX-FileCopyrightText: 2009 Cyrille Berger <cberger@cberger.net>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7#include "KoColorSlider.h"
8#include "KoColorSpace.h"
9
10#include <KoColor.h>
11#include <KoMixColorsOp.h>
12
13#include <QPainter>
14#include <QTimer>
15#include <QStyleOption>
16#include <QPointer>
18
19#define ARROWSIZE 8
20
21struct Q_DECL_HIDDEN KoColorSlider::Private
22{
24 : upToDate(false)
25 , displayRenderer(0)
26 , updateCompressor(100, KisSignalCompressor::FIRST_ACTIVE)
27 {
28 }
29
32 QPixmap pixmap;
36};
37
39 : KSelector(parent)
40 , d(new Private)
41{
42 setMaximum(255);
43 d->displayRenderer = displayRenderer;
44 connect(d->displayRenderer, SIGNAL(displayConfigurationChanged()), SLOT(update()), Qt::UniqueConnection);
45 connect(&d->updateCompressor, SIGNAL(timeout()), SLOT(update()), Qt::UniqueConnection);
46}
47
48KoColorSlider::KoColorSlider(Qt::Orientation o, QWidget *parent, KoColorDisplayRendererInterface *displayRenderer)
49 : KSelector(o, parent), d(new Private)
50{
51 setMaximum(255);
52 d->displayRenderer = displayRenderer;
53 connect(d->displayRenderer, SIGNAL(displayConfigurationChanged()), SLOT(update()), Qt::UniqueConnection);
54 connect(&d->updateCompressor, SIGNAL(timeout()), SLOT(update()), Qt::UniqueConnection);
55}
56
58{
59 delete d;
60}
61
62void KoColorSlider::setColors(const KoColor& mincolor, const KoColor& maxcolor)
63{
64 d->minColor = mincolor;
65 d->maxColor = maxcolor;
66 d->upToDate = false;
67 d->updateCompressor.start();
68}
69
70void KoColorSlider::drawContents( QPainter *painter )
71{
72 QPixmap checker(8, 8);
73 QPainter p(&checker);
74 p.fillRect(0, 0, 4, 4, Qt::lightGray);
75 p.fillRect(4, 0, 4, 4, Qt::darkGray);
76 p.fillRect(0, 4, 4, 4, Qt::darkGray);
77 p.fillRect(4, 4, 4, 4, Qt::lightGray);
78 p.end();
79 QRect contentsRect_(contentsRect());
80 painter->fillRect(contentsRect_, QBrush(checker));
81
82 if( !d->upToDate || d->pixmap.isNull() || d->pixmap.width() != contentsRect_.width()
83 || d->pixmap.height() != contentsRect_.height() )
84 {
85 KoColor c = d->minColor; // smart way to fetch colorspace
86 QColor color;
87
88 const quint8 *colors[2];
89 colors[0] = d->minColor.data();
90 colors[1] = d->maxColor.data();
91
92 KoMixColorsOp * mixOp = c.colorSpace()->mixColorsOp();
93 Q_ASSERT(mixOp);
94 QImage image(contentsRect_.width(), contentsRect_.height(), QImage::Format_ARGB32 );
95
96 if( orientation() == Qt::Horizontal ) {
97 for (int x = 0; x < contentsRect_.width(); x++) {
98
99 qreal t = static_cast<qreal>(x) / (contentsRect_.width() - 1);
100
101 qint16 colorWeights[2];
102 colorWeights[0] = static_cast<quint8>((1.0 - t) * 255 + 0.5);
103 colorWeights[1] = 255 - colorWeights[0];
104
105 mixOp->mixColors(colors, colorWeights, 2, c.data());
106
107 if (d->displayRenderer) {
108 color = d->displayRenderer->toQColor(c);
109 }
110 else {
111 color = c.toQColor();
112 }
113
114 for (int y = 0; y < contentsRect_.height(); y++)
115 image.setPixel(x, y, color.rgba());
116 }
117 }
118 else {
119 for (int y = 0; y < contentsRect_.height(); y++) {
120
121 qreal t = static_cast<qreal>(y) / (contentsRect_.height() - 1);
122
123 qint16 colorWeights[2];
124 colorWeights[0] = static_cast<quint8>((t) * 255 + 0.5);
125 colorWeights[1] = 255 - colorWeights[0];
126
127 mixOp->mixColors(colors, colorWeights, 2, c.data());
128
129 if (d->displayRenderer) {
130 color = d->displayRenderer->toQColor(c);
131 }
132 else {
133 color = c.toQColor();
134 }
135
136 for (int x = 0; x < contentsRect_.width(); x++)
137 image.setPixel(x, y, color.rgba());
138 }
139 }
140 d->pixmap = QPixmap::fromImage(image);
141 d->upToDate = true;
142 }
143 painter->drawPixmap( contentsRect_, d->pixmap, QRect( 0, 0, d->pixmap.width(), d->pixmap.height()) );
144}
145
147{
148 const quint8 *colors[2];
149 colors[0] = d->minColor.data();
150 colors[1] = d->maxColor.data();
151 KoMixColorsOp * mixOp = d->minColor.colorSpace()->mixColorsOp();
152 KoColor c(d->minColor.colorSpace());
153 qint16 weights[2];
154 weights[1] = (value() - minimum()) / qreal(maximum() - minimum()) * 255;
155 weights[0] = 255 - weights[1];
156 mixOp->mixColors(colors, weights, 2, c.data());
157 return c;
158}
159
160void KoColorSlider::drawArrow(QPainter *painter, const QPoint &pos)
161{
162 painter->setPen(QPen(palette().text().color(), 0));
163 painter->setBrush(palette().text());
164
165 QStyleOption o;
166 o.initFrom(this);
167 o.state &= ~QStyle::State_MouseOver;
168
169 if ( orientation() == Qt::Vertical ) {
170 o.rect = QRect( pos.x(), pos.y() - ARROWSIZE / 2,
172 } else {
173 o.rect = QRect( pos.x() - ARROWSIZE / 2, pos.y(),
175 }
176
177 QStyle::PrimitiveElement arrowPE;
178 switch (arrowDirection()) {
179 case Qt::UpArrow:
180 arrowPE = QStyle::PE_IndicatorArrowUp;
181 break;
182 case Qt::DownArrow:
183 arrowPE = QStyle::PE_IndicatorArrowDown;
184 break;
185 case Qt::RightArrow:
186 arrowPE = QStyle::PE_IndicatorArrowRight;
187 break;
188 case Qt::LeftArrow:
189 default:
190 arrowPE = QStyle::PE_IndicatorArrowLeft;
191 break;
192 }
193
194 style()->drawPrimitive(arrowPE, &o, painter, this);
195
196}
float value(const T *src, size_t ch)
const Params2D p
#define ARROWSIZE
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
KoMixColorsOp * mixColorsOp
quint8 * data()
Definition KoColor.h:144
const KoColorSpace * colorSpace() const
return the current colorSpace
Definition KoColor.h:82
void toQColor(QColor *c) const
a convenience method for the above.
Definition KoColor.cpp:198
virtual void mixColors(const quint8 *const *colors, const qint16 *weights, int nColors, quint8 *dst, int weightSum=255) const =0
rgba palette[MAX_PALETTE]
Definition palette.c:35
void drawArrow(QPainter *painter, const QPoint &pos) override
void setColors(const KoColor &minColor, const KoColor &maxColor)
~KoColorSlider() override
QPointer< KoColorDisplayRendererInterface > displayRenderer
KisSignalCompressor updateCompressor
KoColor currentColor() const
KoColorSlider(QWidget *parent=0, KoColorDisplayRendererInterface *displayRenderer=KoDumbColorDisplayRenderer::instance())
void drawContents(QPainter *) override
Private *const d