Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_shade_selector_line.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2010 Adam Celarek <kdedev at xibo dot at>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
8
9#include <QPainter>
10#include <QColor>
11#include <QMouseEvent>
12
13#include <cmath>
14
15#include <ksharedconfig.h>
16#include <kconfig.h>
17#include <kconfiggroup.h>
18#include <klocalizedstring.h>
20
21#include "kis_canvas2.h"
22
25#include "kis_paint_device.h"
26
27
29 QWidget *parent)
31 m_cachedColorSpace(0),
32 m_displayHelpText(false),
33 m_parentProxy(parentProxy)
34{
35 setParam(0, 0, 0, 0, 0, 0);
37 setMouseTracking(true);
38 m_mouseX=width()/2;
39 m_isDown=false;
40}
41
42KisShadeSelectorLine::KisShadeSelectorLine(qreal hueDelta, qreal satDelta, qreal valDelta,
43 KisColorSelectorBaseProxy *parentProxy, QWidget *parent, qreal hueShift, qreal satShift, qreal valShift) :
45 m_cachedColorSpace(0),
46 m_displayHelpText(false),
47 m_parentProxy(parentProxy)
48{
49 setParam(hueDelta, satDelta, valDelta, hueShift, satShift, valShift);
51 m_mouseX=width()/2;
52 m_isDown=false;
53}
54
58
59void KisShadeSelectorLine::setParam(qreal hueDelta, qreal satDelta, qreal valDelta, qreal hueShift, qreal satShift, qreal valShift)
60{
61 m_hueDelta = hueDelta;
62 m_saturationDelta = satDelta;
63 m_valueDelta = valDelta;
64
65 m_hueShift = hueShift;
66 m_saturationShift = satShift;
67 m_valueShift = valShift;
68}
69
71{
72 m_realColor = color;
74 m_mouseX=width()/2;
75
76 update();
77}
78
80{
81 KConfigGroup cfg = KSharedConfig::openConfig()->group("advancedColorSelector");
82
83 m_gradient = cfg.readEntry("minimalShadeSelectorAsGradient", false);
84 m_patchCount = cfg.readEntry("minimalShadeSelectorPatchCount", 10);
85 m_lineHeight = cfg.readEntry("minimalShadeSelectorLineHeight", 20);
86
87 setMaximumHeight(m_lineHeight);
88 setMinimumHeight(m_lineHeight);
89}
90
92{
93 return QString("%1|%2|%3|%4|%5|%6|%7").arg(m_lineNumber).arg(m_hueDelta).arg(m_saturationDelta).arg(m_valueDelta).arg(m_hueShift).arg(m_saturationShift).arg(m_valueShift);
94}
95
96void KisShadeSelectorLine::fromString(const QString& string)
97{
98 QStringList strili = string.split('|');
99 m_lineNumber = strili.at(0).toInt();
100 m_hueDelta = strili.at(1).toDouble();
101 m_saturationDelta = strili.at(2).toDouble();
102 m_valueDelta = strili.at(3).toDouble();
103 if(strili.size()==4) return; // don't crash, if reading old config files.
104 m_hueShift = strili.at(4).toDouble();
105 m_saturationShift = strili.at(5).toDouble();
106 m_valueShift = strili.at(6).toDouble();
107}
108
110{
111
115 }
116 else {
118 }
119
120 int patchCount = 0;
121 int patchSpacing = 0;
122 int patchWidth = 0;
123
124 if(m_gradient) {
125 patchCount = width();
126 patchSpacing = 0;
127 patchWidth = 1;
128 }
129 else {
130 patchCount = m_patchCount;
131 patchSpacing = 3;
132 patchWidth = std::lround((width() - patchSpacing * patchCount) / qreal(patchCount));
133 }
134 qreal hueStep;
135 qreal saturationStep;
136 qreal valueStep;
137
138 if(m_gradient){
139 hueStep=m_hueDelta/qreal(patchCount-10);
140 saturationStep=m_saturationDelta/qreal(patchCount-10);
141 valueStep=m_valueDelta/qreal(patchCount-10);
142 }
143 else{
144 hueStep=m_hueDelta/qreal(patchCount);
145 saturationStep=m_saturationDelta/qreal(patchCount);
146 valueStep=m_valueDelta/qreal(patchCount);
147 }
148
149 qreal baseHue;
150 qreal baseSaturation;
151 qreal baseValue;
153 getHsvF(m_realColor, &baseHue, &baseSaturation, &baseValue);
154
155 int z=0;
156 for(int i=-patchCount/2; i<=patchCount/2; i++) {
157 if(i==0 && patchCount%2==0) continue;
158 qreal hue = baseHue + (i * hueStep) + m_hueShift;
159 while (hue < 0.0) hue += 1.0;
160 while (hue > 1.0) hue -= 1.0;
161
162 qreal saturation = qBound<qreal>(0., baseSaturation + (i * saturationStep) + m_saturationShift, 1.);
163 qreal value = qBound<qreal>(0., baseValue + (i * valueStep) + m_valueShift, 1.);
164
165 if (qAbs(i) <= 5 && m_gradient) {
166 hue = baseHue;
167 saturation=baseSaturation;
168 value=baseValue;
169 }
170
171 const QRect patchRect(z * (patchWidth + patchSpacing), 0, patchWidth, m_lineHeight);
172 KoColor patchColor = m_parentProxy->converter()->fromHsvF(hue, saturation, value);
173
175 m_realPixelCache->fill(patchRect, patchColor);
176
177 z++;
178 }
179
180 QPainter wpainter(this);
181 if (!isEnabled()) {
182 wpainter.setOpacity(0.2);
183 }
184 const QImage renderedImage = m_parentProxy->converter()->toQImage(m_realPixelCache).scaledToWidth(m_width);
185 wpainter.drawImage(0, 0, renderedImage);
186 if (m_gradient) {
187 wpainter.setPen(QColor(175,175,175));
188 wpainter.drawRect(renderedImage.width()/2-5,0,10,renderedImage.height()-1);
189 wpainter.setPen(QColor(75,75,75));
190 wpainter.drawRect(renderedImage.width()/2-4,0,8,renderedImage.height()-1);
191 wpainter.setPen(QColor(175,175,175));
192 const int mouseX = qBound(5, m_mouseX, m_width - 5);
193 wpainter.drawRect(mouseX-5,0,10,renderedImage.height()-1);
194 wpainter.setPen(QColor(75,75,75));
195 wpainter.drawRect(mouseX-4,0,8,renderedImage.height()-1);
196 }
197
199 QString helpText(i18n("delta h=%1 s=%2 v=%3 shift h=%4 s=%5 v=%6",
205 m_valueShift));
206 wpainter.setPen(QColor(255,255,255));
207 wpainter.drawText(rect(), helpText);
208 }
209}
210
212{
213 QSize m_old = e->oldSize();
214 QSize m_new = e->size();
215 int m_nWidth = m_new.width();
216 int m_oWidth = m_old.width();
217 m_width = width();
218 m_mouseX = m_mouseX * m_nWidth / m_oWidth;
219}
221{
222 if(e->button()!=Qt::LeftButton && e->button()!=Qt::RightButton) {
223 e->setAccepted(false);
224 return;
225 }
226 if (e->y() > 0 && e->y() < height()) {
228 e->accept();
229 m_mouseX = e->x();
230 m_isDown=true;
231 update();
232 }
233}
234
236{
237 if ((m_isDown) && (e->buttons() & Qt::LeftButton)) {
238 m_mouseX=e->x();
239 const QPoint mouseEv(qBound(5, m_mouseX, m_width - 5), 5);
242 update();
243 }
244}
245
247{
248 if (e->button() != Qt::LeftButton && e->button() != Qt::RightButton) {
249 e->ignore();
250 return;
251 }
252 m_mouseX=e->x();
253 const QPoint mouseEv(qBound(5, m_mouseX, m_width - 5), 5);
256 Acs::ColorRole role = Acs::buttonToRole(e->button());
257
258 KConfigGroup cfg = KSharedConfig::openConfig()->group("advancedColorSelector");
259
260 bool onRightClick = cfg.readEntry("shadeSelectorUpdateOnRightClick", false);
261 bool onLeftClick = cfg.readEntry("shadeSelectorUpdateOnLeftClick", false);
262
263 bool explicitColorReset =
264 (e->button() == Qt::LeftButton && onLeftClick) ||
265 (e->button() == Qt::RightButton && onRightClick);
266
267 m_parentProxy->updateColor(color, role, explicitColorReset);
268 e->accept();
269 m_isDown=false;
270}
float value(const T *src, size_t ch)
virtual void updateColorPreview(const KoColor &color)=0
virtual const KoColorSpace * colorSpace() const =0
virtual KisDisplayColorConverter * converter() const =0
virtual void showColorPreview()=0
virtual void updateColor(const KoColor &color, Acs::ColorRole role, bool needsExplicitColorReset)=0
QImage toQImage(KisPaintDeviceSP srcDevice, bool proofPaintColors=false) const
KoColor fromHsvF(qreal h, qreal s, qreal v, qreal a=1.0)
virtual void clear()
void fill(const QRect &rc, const KoColor &color)
const KoColorSpace * colorSpace() const
void mouseReleaseEvent(QMouseEvent *) override
void mousePressEvent(QMouseEvent *) override
void setParam(qreal hue, qreal sat, qreal val, qreal hueShift, qreal satShift, qreal shiftVal)
KisColorSelectorBaseProxy * m_parentProxy
void mouseMoveEvent(QMouseEvent *) override
void resizeEvent(QResizeEvent *) override
void paintEvent(QPaintEvent *) override
KisShadeSelectorLine(KisColorSelectorBaseProxy *parentProxy, QWidget *parent=0)
KisPaintDeviceSP m_realPixelCache
void fromString(const QString &string) override
void setColor(const KoColor &color)
QString toString() const override
const KoColorSpace * m_cachedColorSpace
void convertTo(const KoColorSpace *cs, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags)
Definition KoColor.cpp:136
ColorRole buttonToRole(Qt::MouseButton button)
KoColor sampleColor(PaintDeviceSP device, const QPoint &pt)