Krita Source Code Documentation
Loading...
Searching...
No Matches
WGShadeSlider Class Reference

#include <WGShadeSlider.h>

+ Inheritance diagram for WGShadeSlider:

Classes

struct  Private
 

Public Slots

void resetHandle ()
 
void slotSetChannelValues (const QVector4D &values)
 

Signals

void sigChannelValuesChanged (const QVector4D &values)
 
void sigInteraction (bool active)
 

Public Member Functions

const QImage * background ()
 
QVector4D channelValues () const
 
void setDisplayMode (bool slider, int numPatches=-1)
 
void setGradient (const QVector4D &range, const QVector4D &offset)
 
void setModel (KisVisualColorModelSP model)
 
 WGShadeSlider (WGSelectorDisplayConfigSP config, QWidget *parent=nullptr, KisVisualColorModelSP model=nullptr)
 
 ~WGShadeSlider () override
 

Protected Slots

void slotDisplayConfigurationChanged ()
 

Protected Member Functions

bool adjustHandleValue (const QPointF &widgetPos)
 
QVector4D calculateChannelValues (qreal sliderPos) const
 
QPointF convertSliderValueToWidgetCoordinate (qreal value)
 
qreal convertWidgetCoordinateToSliderValue (QPointF coordinate)
 
int getPatch (const QPointF pos) const
 
QSize minimumSizeHint () const override
 
void mouseMoveEvent (QMouseEvent *event) override
 
void mousePressEvent (QMouseEvent *event) override
 
void mouseReleaseEvent (QMouseEvent *event) override
 
void paintEvent (QPaintEvent *) override
 
QRectF patchRect (int index) const
 
void recalculateParameters ()
 
QImage renderBackground ()
 
void resizeEvent (QResizeEvent *) override
 
bool sizeRequirementsMet () const
 
void strokeRect (QPainter &painter, const QRectF &rect, qreal pixelSize, qreal shrinkX)
 strokeRect
 

Private Attributes

const QScopedPointer< Privatem_d
 

Detailed Description

Definition at line 20 of file WGShadeSlider.h.

Constructor & Destructor Documentation

◆ WGShadeSlider()

WGShadeSlider::WGShadeSlider ( WGSelectorDisplayConfigSP config,
QWidget * parent = nullptr,
KisVisualColorModelSP model = nullptr )
explicit

Definition at line 39 of file WGShadeSlider.cpp.

40 : QWidget(parent)
41 , m_d(new Private)
42{
43 m_d->selectorModel = model;
44 m_d->displayConfig = config;
48}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void sigDisplayConfigurationChanged()
void slotDisplayConfigurationChanged()
void recalculateParameters()
const QScopedPointer< Private > m_d

References connect(), m_d, recalculateParameters(), WGSelectorDisplayConfig::sigDisplayConfigurationChanged(), and slotDisplayConfigurationChanged().

◆ ~WGShadeSlider()

WGShadeSlider::~WGShadeSlider ( )
override

Definition at line 50 of file WGShadeSlider.cpp.

51{}

Member Function Documentation

◆ adjustHandleValue()

bool WGShadeSlider::adjustHandleValue ( const QPointF & widgetPos)
protected

Definition at line 186 of file WGShadeSlider.cpp.

187{
188 if (!m_d->widgetSizeOk) {
189 return false;
190 }
191
192 if (m_d->sliderMode) {
193 qreal sliderPos = convertWidgetCoordinateToSliderValue(widgetPos);
194 if (!qFuzzyIsNull(m_d->handleValue - sliderPos)) {
195 m_d->handleValue = sliderPos;
196 return true;
197 }
198 } else {
199 int patchNum = getPatch(widgetPos);
200 if (patchNum >= 0 && patchNum != (int)m_d->handleValue) {
201 m_d->handleValue = patchNum;
202 return true;
203 }
204 }
205 return false;
206}
int getPatch(const QPointF pos) const
qreal convertWidgetCoordinateToSliderValue(QPointF coordinate)
static bool qFuzzyIsNull(half h)

References convertWidgetCoordinateToSliderValue(), getPatch(), m_d, and qFuzzyIsNull().

◆ background()

const QImage * WGShadeSlider::background ( )

Definition at line 87 of file WGShadeSlider.cpp.

88{
89 if (m_d->imageNeedsUpdate) {
90 m_d->background = renderBackground();
91 m_d->imageNeedsUpdate = false;
92 }
93 return &m_d->background;
94}
QImage renderBackground()

References m_d, and renderBackground().

◆ calculateChannelValues()

QVector4D WGShadeSlider::calculateChannelValues ( qreal sliderPos) const
protected

Definition at line 241 of file WGShadeSlider.cpp.

242{
243 float delta = 0.0f;
244 if (m_d->sliderMode) {
245 delta = (float)sliderPos;
246 } else if (sliderPos >= 0 || m_d->numPatches > 1) {
247 delta = 2.0f * float(sliderPos)/(m_d->numPatches - 1.0f) - 1.0f;
248 }
249
250 QVector4D channelVec = m_d->baseValues + m_d->offset + delta * m_d->range;
251 // Hue wraps around
252 if (m_d->selectorModel->isHSXModel()) {
253 channelVec[0] = (float)fmod(channelVec[0], 1.0);
254 if (channelVec[0] < 0) {
255 channelVec[0] += 1.f;
256 }
257 }
258 else {
259 channelVec[0] = qBound(0.f, channelVec[0], 1.f);
260 }
261
262 for (int i = 1; i < 3; i++) {
263 channelVec[i] = qBound(0.f, channelVec[i], 1.f);
264 }
265 return channelVec;
266}

References m_d.

◆ channelValues()

QVector4D WGShadeSlider::channelValues ( ) const

Definition at line 82 of file WGShadeSlider.cpp.

83{
84 return calculateChannelValues(m_d->handleValue);
85}
QVector4D calculateChannelValues(qreal sliderPos) const

References calculateChannelValues(), and m_d.

◆ convertSliderValueToWidgetCoordinate()

QPointF WGShadeSlider::convertSliderValueToWidgetCoordinate ( qreal value)
protected

Definition at line 208 of file WGShadeSlider.cpp.

209{
210 QPointF pos(0.0, 0.0);
211 if (value < 0) {
212 pos.setX(m_d->leftStart - value * (m_d->leftEnd - m_d->leftStart));
213 }
214 else if (value > 0) {
215 pos.setX(m_d->rightStart + value * (m_d->rightEnd - m_d->rightStart));
216 }
217 else {
218 pos.setX((width() - 1) / 2);
219 }
220 return pos;
221}
float value(const T *src, size_t ch)

References m_d, and value().

◆ convertWidgetCoordinateToSliderValue()

qreal WGShadeSlider::convertWidgetCoordinateToSliderValue ( QPointF coordinate)
protected

Definition at line 223 of file WGShadeSlider.cpp.

224{
225 qreal x = coordinate.x();
226 if (x < m_d->leftEnd) {
227 return -1.0;
228 }
229 else if (x < m_d->leftStart) {
230 return (m_d->leftStart - x) / (m_d->leftEnd - m_d->leftStart);
231 }
232 else if (x < m_d->rightStart) {
233 return 0.0;
234 }
235 else if (x < m_d->rightEnd) {
236 return (x - m_d->rightStart) / (m_d->rightEnd - m_d->rightStart);
237 }
238 return 1.0;
239}

References m_d.

◆ getPatch()

int WGShadeSlider::getPatch ( const QPointF pos) const
protected

Definition at line 268 of file WGShadeSlider.cpp.

269{
270 int patch = m_d->numPatches * pos.x() / width();
271 if (patch >= 0 && patch < m_d->numPatches) {
272 return patch;
273 }
274 return -1;
275}

References m_d.

◆ minimumSizeHint()

QSize WGShadeSlider::minimumSizeHint ( ) const
overrideprotected

Definition at line 96 of file WGShadeSlider.cpp.

97{
98 return QSize(50, 8);
99}

◆ mouseMoveEvent()

void WGShadeSlider::mouseMoveEvent ( QMouseEvent * event)
overrideprotected

Definition at line 114 of file WGShadeSlider.cpp.

115{
116 if (event->buttons() & Qt::LeftButton) {
117 if (adjustHandleValue(event->localPos())) {
119 update();
120 }
121 } else {
122 event->ignore();
123 }
124}
bool adjustHandleValue(const QPointF &widgetPos)
QVector4D channelValues() const
void sigChannelValuesChanged(const QVector4D &values)
bool update(QSpinBox *spinBox)

References adjustHandleValue(), channelValues(), and sigChannelValuesChanged().

◆ mousePressEvent()

void WGShadeSlider::mousePressEvent ( QMouseEvent * event)
overrideprotected

Definition at line 101 of file WGShadeSlider.cpp.

102{
103 if (event->button() == Qt::LeftButton) {
104 Q_EMIT sigInteraction(true);
105 if (adjustHandleValue(event->localPos())) {
107 update();
108 }
109 } else {
110 event->ignore();
111 }
112}
void sigInteraction(bool active)

References adjustHandleValue(), channelValues(), sigChannelValuesChanged(), and sigInteraction().

◆ mouseReleaseEvent()

void WGShadeSlider::mouseReleaseEvent ( QMouseEvent * event)
overrideprotected

Definition at line 126 of file WGShadeSlider.cpp.

127{
128 if (event->button() == Qt::LeftButton) {
129 Q_EMIT sigInteraction(false);
130 } else {
131 event->ignore();
132 }
133}

References sigInteraction().

◆ paintEvent()

void WGShadeSlider::paintEvent ( QPaintEvent * )
overrideprotected

Definition at line 135 of file WGShadeSlider.cpp.

136{
137 if (m_d->imageNeedsUpdate) {
138 m_d->background = renderBackground();
139 m_d->imageNeedsUpdate = false;
140 }
141 QPainter painter(this);
142 painter.drawImage(0, 0, m_d->background);
143 painter.scale(1.0/devicePixelRatioF(), 1.0/devicePixelRatioF());
144 QRectF handleRect;
145 if (m_d->sliderMode) {
146 QPointF sliderPos = convertSliderValueToWidgetCoordinate(m_d->handleValue);
147 int sliderX = qRound(sliderPos.x());
148 handleRect = QRectF(sliderX - m_d->cursorWidth/2, 0, m_d->cursorWidth, height());
149 } else if (m_d->handleValue >= 0) {
150 handleRect = patchRect(m_d->handleValue);
151 }
152 if (handleRect.isValid()) {
153 QPen pen(QColor(175,175,175), m_d->lineWidth, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
154 painter.setPen(pen);
155 strokeRect(painter, handleRect, devicePixelRatioF(), 0);
156 pen.setColor(QColor(75,75,75));
157 painter.setPen(pen);
158 strokeRect(painter, handleRect, devicePixelRatioF(), 1);
159 }
160}
QPointF convertSliderValueToWidgetCoordinate(qreal value)
QRectF patchRect(int index) const
void strokeRect(QPainter &painter, const QRectF &rect, qreal pixelSize, qreal shrinkX)
strokeRect

References convertSliderValueToWidgetCoordinate(), m_d, patchRect(), renderBackground(), and strokeRect().

◆ patchRect()

QRectF WGShadeSlider::patchRect ( int index) const
protected

Definition at line 277 of file WGShadeSlider.cpp.

278{
279 qreal patchWidth = width() / qreal(m_d->numPatches);
280 qreal margin = 1.5;
281 QPointF topLeft(index * patchWidth + margin, 0);
282 QPointF bottomRight((index+1) * patchWidth - margin, height());
283 return QRectF(topLeft, bottomRight);
284}

References m_d.

◆ recalculateParameters()

void WGShadeSlider::recalculateParameters ( )
protected

Definition at line 286 of file WGShadeSlider.cpp.

287{
288 int center = (width() - 1) / 2;
289 int halfCursor = m_d->cursorWidth / 2;
290
291 m_d->leftEnd = halfCursor;
292 m_d->leftStart = center - halfCursor;
293
294 m_d->rightStart = center + halfCursor;
295 m_d->rightEnd = 2 * center - halfCursor;
296
297 m_d->lineWidth = qRound(devicePixelRatioF() - 0.1);
298 m_d->widgetSizeOk = sizeRequirementsMet();
299 m_d->imageNeedsUpdate = true;
300}
bool sizeRequirementsMet() const

References m_d, and sizeRequirementsMet().

◆ renderBackground()

QImage WGShadeSlider::renderBackground ( )
protected

Definition at line 311 of file WGShadeSlider.cpp.

312{
313 if (!m_d->widgetSizeOk || !m_d->selectorModel || !m_d->selectorModel->colorSpace()) {
314 return QImage();
315 }
316
317 // Hi-DPI aware rendering requires that we determine the device pixel dimension;
318 // actual widget size in device pixels is not accessible unfortunately, it might be 1px smaller...
319 const qreal deviceDivider = 1.0 / devicePixelRatioF();
320 const int deviceWidth = qCeil(width() * devicePixelRatioF());
321 const int deviceHeight = qCeil(height() * devicePixelRatioF());
322 if (m_d->sliderMode) {
323 const KoColorSpace *currentCS = m_d->selectorModel->colorSpace();
324 const quint32 pixelSize = currentCS->pixelSize();
325 quint32 imageSize = deviceWidth * pixelSize;
326 QScopedArrayPointer<quint8> raw(new quint8[imageSize] {});
327 quint8 *dataPtr = raw.data();
328
329 for (int x = 0; x < deviceWidth; x++, dataPtr += pixelSize) {
330 qreal sliderVal = convertWidgetCoordinateToSliderValue(QPointF(x, 0) * deviceDivider);
331 QVector4D coordinates = calculateChannelValues(sliderVal);
332 KoColor c = m_d->selectorModel->convertChannelValuesToKoColor(coordinates);
333 memcpy(dataPtr, c.data(), pixelSize);
334 }
335
336 QImage image = m_d->displayConfig->displayConverter()->toQImage(currentCS, raw.data(), {deviceWidth, 1},
337 m_d->displayConfig->previewInPaintingCS());
338 image = image.scaled(QSize(deviceWidth, deviceHeight));
339
340 QPainter painter(&image);
341 QPen pen(QColor(175,175,175), m_d->lineWidth, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
342 painter.setPen(pen);
343 strokeRect(painter, QRectF(m_d->leftStart, 0, m_d->cursorWidth, height()), devicePixelRatioF(), 0);
344 pen.setColor(QColor(75,75,75));
345 painter.setPen(pen);
346 strokeRect(painter, QRectF(m_d->leftStart, 0, m_d->cursorWidth, height()), devicePixelRatioF(), 1);
347
348 image.setDevicePixelRatio(devicePixelRatioF());
349 return image;
350 } else {
351 QImage image(deviceWidth, deviceHeight, QImage::Format_ARGB32);
352 image.fill(Qt::transparent);
353 image.setDevicePixelRatio(devicePixelRatioF());
354 QPainter painter(&image);
355 painter.setPen(Qt::NoPen);
356
357 for (int i = 0; i < m_d->numPatches; i++) {
358 QVector4D values = calculateChannelValues(i);
359 KoColor col = m_d->selectorModel->convertChannelValuesToKoColor(values);
360 QColor qCol = m_d->displayConfig->displayConverter()->toQColor(col, m_d->displayConfig->previewInPaintingCS());
361 painter.setBrush(qCol);
362 painter.drawRect(patchRect(i));
363 }
364 return image;
365 }
366}
virtual quint32 pixelSize() const =0
quint8 * data()
Definition KoColor.h:144
void toQColor(QColor *c) const
a convenience method for the above.
Definition KoColor.cpp:198

References calculateChannelValues(), convertWidgetCoordinateToSliderValue(), KoColor::data(), m_d, patchRect(), KoColorSpace::pixelSize(), strokeRect(), and KoColor::toQColor().

◆ resetHandle

void WGShadeSlider::resetHandle ( )
slot

Definition at line 174 of file WGShadeSlider.cpp.

175{
176 m_d->handleValue = m_d->sliderMode ? 0 : -1;
177 update();
178}

References m_d.

◆ resizeEvent()

void WGShadeSlider::resizeEvent ( QResizeEvent * )
overrideprotected

Definition at line 162 of file WGShadeSlider.cpp.

163{
165}

References recalculateParameters().

◆ setDisplayMode()

void WGShadeSlider::setDisplayMode ( bool slider,
int numPatches = -1 )

Definition at line 61 of file WGShadeSlider.cpp.

62{
63 if (slider != m_d->sliderMode ||
64 (!slider && numPatches != m_d->numPatches)) {
65 m_d->sliderMode = slider;
66 if (!slider && numPatches > 2) {
67 m_d->numPatches = numPatches;
68 }
69 m_d->widgetSizeOk = sizeRequirementsMet();
70 m_d->imageNeedsUpdate = true;
72 }
73}

References m_d, resetHandle(), and sizeRequirementsMet().

◆ setGradient()

void WGShadeSlider::setGradient ( const QVector4D & range,
const QVector4D & offset )

Definition at line 53 of file WGShadeSlider.cpp.

54{
55 m_d->range = range;
56 m_d->offset = offset;
57 m_d->imageNeedsUpdate = true;
59}

References m_d, and resetHandle().

◆ setModel()

void WGShadeSlider::setModel ( KisVisualColorModelSP model)

Definition at line 75 of file WGShadeSlider.cpp.

76{
77 m_d->selectorModel = model;
78 m_d->imageNeedsUpdate = true;
79 update();
80}

References m_d.

◆ sigChannelValuesChanged

void WGShadeSlider::sigChannelValuesChanged ( const QVector4D & values)
signal

◆ sigInteraction

void WGShadeSlider::sigInteraction ( bool active)
signal

◆ sizeRequirementsMet()

bool WGShadeSlider::sizeRequirementsMet ( ) const
protected

Definition at line 302 of file WGShadeSlider.cpp.

303{
304 if (m_d->sliderMode) {
305 return m_d->leftStart - m_d->leftEnd > 0 && m_d->rightEnd - m_d->rightStart > 0;
306 } else {
307 return width() > m_d->numPatches;
308 }
309}

References m_d.

◆ slotDisplayConfigurationChanged

void WGShadeSlider::slotDisplayConfigurationChanged ( )
protectedslot

Definition at line 180 of file WGShadeSlider.cpp.

181{
182 m_d->imageNeedsUpdate = true;
183 update();
184}

References m_d.

◆ slotSetChannelValues

void WGShadeSlider::slotSetChannelValues ( const QVector4D & values)
slot

Definition at line 167 of file WGShadeSlider.cpp.

168{
169 m_d->baseValues = values;
170 m_d->imageNeedsUpdate = true;
171 resetHandle();
172}

References m_d, and resetHandle().

◆ strokeRect()

void WGShadeSlider::strokeRect ( QPainter & painter,
const QRectF & rect,
qreal pixelSize,
qreal shrinkX )
protected

strokeRect

Parameters
paintershall already be scaled so that 1 unit == 1 real Pixel
rectthe rectangle to stroke in (logical) widget coordinates
pixelSizedevicePixelRatioF() that was used to determine the real dimensions
shrinkXshrinks the rect by a multiple of the line width used to stroke

Definition at line 368 of file WGShadeSlider.cpp.

369{
370 qreal lineWidth = painter.pen().widthF();
371 QPointF topLeft(qRound(rect.left() * pixelSize) + (shrinkX + 0.5) * lineWidth,
372 qRound(rect.top() * pixelSize) + 0.5 * lineWidth);
373 QPointF bottomRight(qRound(rect.right() * pixelSize) - (shrinkX + 0.5) * lineWidth,
374 qRound(rect.bottom() * pixelSize) - 0.5 * lineWidth);
375 painter.drawRect(QRectF(topLeft, bottomRight));
376}

Member Data Documentation

◆ m_d

const QScopedPointer<Private> WGShadeSlider::m_d
private

Definition at line 72 of file WGShadeSlider.h.


The documentation for this class was generated from the following files: