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

This is a simple input levels slider that has no gamma handle. Use it if you want to show a simple mapping or contrast adjustment. The handles are constrained so that the black point handle can not pass the white point handle and vice-versa. More...

#include <KisLevelsSlider.h>

+ Inheritance diagram for KisInputLevelsSlider:

Public Slots

virtual void reset (qreal newBlackPoint, qreal newWhitePoint)
 Sometimes you want to set the range to a totally different place, but the new black point can be greater than the old white point so the new black point position will be constrained to the old white position. This function allows to set both values at once to prevent that undesirable effect. Use it when the widget has to show new values, for a different set of levels parameters for example.
 
virtual void setBlackPoint (qreal newBlackPoint)
 Sets the black point.
 
virtual void setWhitePoint (qreal newWhitePoint)
 Sets the white point.
 
- Public Slots inherited from KisLevelsSlider
virtual void setHandleColor (int handleIndex, const QColor &newColor)
 Sets the color associated with the given handle.
 
virtual void setHandlePosition (int handleIndex, qreal newPosition)
 Sets the normalized position of the given handle.
 

Signals

void blackPointChanged (qreal newBlackPoint)
 Signal emitted when the black point changes.
 
void whitePointChanged (qreal newWhitePoint)
 Signal emitted when the white point changes.
 
- Signals inherited from KisLevelsSlider
void handleColorChanged (int handleIndex, const QColor &color)
 Signal emitted when the color associated with a handle changes.
 
void handlePositionChanged (int handleIndex, qreal position)
 Signal emitted when the position of a handle changes.
 

Public Member Functions

qreal blackPoint () const
 Get the normalized black point.
 
 KisInputLevelsSlider (QWidget *parent=nullptr)
 
qreal whitePoint () const
 Get the normalized white point.
 
 ~KisInputLevelsSlider ()
 
- Public Member Functions inherited from KisLevelsSlider
virtual QRect gradientRect () const
 Gets the rect where the gradient will be painted.
 
QColor handleColor (int handleIndex) const
 Gets the color associated with a given handle.
 
qreal handlePosition (int handleIndex) const
 Gets the normalized position of a given handle.
 
 KisLevelsSlider (QWidget *parent)
 
QSize minimumSizeHint () const override
 
QSize sizeHint () const override
 
 ~KisLevelsSlider ()
 

Protected Member Functions

virtual void paintBottomGradientMiddleSection (QImage &gradientImage, const QVector< Handle > &sortedHandles_)
 This is used to fill the space between the tho handles in the bottom bar of the "gradient". It just paints a linear gradient that goes from black (first handle's color) to white (last handle's color). Derived classes can override this function if they only want to change that area. "gradientImage" is a 256x1px image.
 
void paintGradient (QPainter &painter, const QRect &rect) override
 Custom gradient painter. This paints two bars in the gradient rect, one on top of the other. The top one shows a simple black to white (more exactly, color of the first handle to color of the last handle) linear gradient. The bottom one shows black (first handle's color) from the right side of the rect to the black point, and white (last handle's color) from the white point to the left side of the rect. In the middle of the two handles, the "paintBottomGradientMiddleSection" is used to fill the bar.
 
- Protected Member Functions inherited from KisLevelsSlider
int closestHandleToPosition (qreal position) const
 Given a normalized position, this function returns the closest handle to that position.
 
int closestHandleToX (int x) const
 Given a widget-relative x position, this function returns the closest handle to that position.
 
void handleIncrementInput (int direction, Qt::KeyboardModifiers modifiers)
 
void keyPressEvent (QKeyEvent *e) override
 
void leaveEvent (QEvent *e) override
 
void mouseMoveEvent (QMouseEvent *e) override
 
void mousePressEvent (QMouseEvent *e) override
 
void paintEvent (QPaintEvent *e) override
 
virtual void paintHandle (QPainter &painter, const QRect &rect, const Handle &handle)
 Override this function to paint custom handles.
 
qreal positionFromX (int x) const
 Given a widget-relative x position in pixels, this function returns the normalized position relative to the gradient rect.
 
QVector< HandlesortedHandles () const
 Regardless the index of a handle, they can be unordered in terms of the position. This returns a sorted vector with the handles that have a smaller position first. If two handles have the same position then the index is used for sorting.
 
void wheelEvent (QWheelEvent *e) override
 
int xFromPosition (qreal position) const
 Given a gradient rect relative position, this function returns the x position in pixels relative to the widget.
 

Additional Inherited Members

- Protected Attributes inherited from KisLevelsSlider
int m_constrainPositions
 This variable indicates if the handles can have unordered positions. If it is set to true then the user won't be able to move a handle pass another one. If it is set to false then the ser will be able to move the handles freely.
 
QVector< Handlem_handles
 The collection of handles.
 
int m_hoveredHandle
 
int m_selectedHandle
 
- Static Protected Attributes inherited from KisLevelsSlider
static constexpr int handleHeight {11}
 
static constexpr int handleWidth {11}
 
static constexpr qreal minimumSpaceBetweenHandles {0.001}
 
static constexpr qreal normalPositionIncrement {0.01}
 
static constexpr qreal slowPositionIncrement {0.001}
 

Detailed Description

This is a simple input levels slider that has no gamma handle. Use it if you want to show a simple mapping or contrast adjustment. The handles are constrained so that the black point handle can not pass the white point handle and vice-versa.

Definition at line 147 of file KisLevelsSlider.h.

Constructor & Destructor Documentation

◆ KisInputLevelsSlider()

KisInputLevelsSlider::KisInputLevelsSlider ( QWidget * parent = nullptr)

Definition at line 319 of file KisLevelsSlider.cpp.

320 : KisLevelsSlider(parent)
321{
322 m_handles.resize(2);
323 m_handles[0].index = 0;
324 m_handles[0].position = 0.0;
325 m_handles[0].color = Qt::black;
326 m_handles[1].index = 1;
327 m_handles[1].position = 1.0;
328 m_handles[1].color = Qt::white;
331 [this](int handleIndex, qreal position)
332 {
333 if (handleIndex == m_handles.first().index) {
334 Q_EMIT blackPointChanged(position);
335 } else if (handleIndex == m_handles.last().index) {
336 Q_EMIT whitePointChanged(position);
337 }
338 }
339 );
340}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void handlePositionChanged(int handleIndex, qreal position)
Signal emitted when the position of a handle changes.
KisLevelsSlider(QWidget *parent)
QVector< Handle > m_handles
The collection of handles.

References connect(), KisLevelsSlider::handlePositionChanged(), KisLevelsSlider::m_handles, and KisLevelsSlider::m_selectedHandle.

◆ ~KisInputLevelsSlider()

KisInputLevelsSlider::~KisInputLevelsSlider ( )

Definition at line 342 of file KisLevelsSlider.cpp.

343{}

Member Function Documentation

◆ blackPoint()

qreal KisInputLevelsSlider::blackPoint ( ) const

Get the normalized black point.

Definition at line 345 of file KisLevelsSlider.cpp.

346{
347 return m_handles.first().position;
348}

References KisLevelsSlider::m_handles.

◆ blackPointChanged

void KisInputLevelsSlider::blackPointChanged ( qreal newBlackPoint)
signal

Signal emitted when the black point changes.

◆ paintBottomGradientMiddleSection()

void KisInputLevelsSlider::paintBottomGradientMiddleSection ( QImage & gradientImage,
const QVector< Handle > & sortedHandles_ )
protectedvirtual

This is used to fill the space between the tho handles in the bottom bar of the "gradient". It just paints a linear gradient that goes from black (first handle's color) to white (last handle's color). Derived classes can override this function if they only want to change that area. "gradientImage" is a 256x1px image.

Reimplemented in KisInputLevelsSliderWithGamma, and KisThresholdSlider.

Definition at line 391 of file KisLevelsSlider.cpp.

392{
393 if (m_handles.size() < 2) {
394 return;
395 }
396
397 const int startPos = static_cast<int>(qRound(sortedHandles_.first().position * static_cast<qreal>(gradientImage.width() - 1))) + 1;
398 const int endPos = static_cast<int>(qRound(sortedHandles_.last().position * static_cast<qreal>(gradientImage.width() - 1))) + 1;
399 QRgb *pixel = reinterpret_cast<QRgb*>(gradientImage.bits()) + startPos;
400 for (int x = startPos; x < endPos; ++x, ++pixel) {
401 const qreal t = static_cast<qreal>(x - startPos) / static_cast<qreal>(endPos - startPos);
403 sortedHandles_.last().color,
404 sortedHandles_.first().color,
405 t
406 ).rgba();
407 }
408}
unsigned int QRgb
QColor blendColors(const QColor &c1, const QColor &c2, qreal r1)

References KisPaintingTweaks::blendColors(), and KisLevelsSlider::m_handles.

◆ paintGradient()

void KisInputLevelsSlider::paintGradient ( QPainter & painter,
const QRect & rect )
overrideprotectedvirtual

Custom gradient painter. This paints two bars in the gradient rect, one on top of the other. The top one shows a simple black to white (more exactly, color of the first handle to color of the last handle) linear gradient. The bottom one shows black (first handle's color) from the right side of the rect to the black point, and white (last handle's color) from the white point to the left side of the rect. In the middle of the two handles, the "paintBottomGradientMiddleSection" is used to fill the bar.

Implements KisLevelsSlider.

Definition at line 410 of file KisLevelsSlider.cpp.

411{
412 if (m_handles.size() == 0) {
413 return;
414 }
415
416 painter.setRenderHint(QPainter::SmoothPixmapTransform);
417 if (!isEnabled()) {
418 painter.setOpacity(0.5);
419 }
420 QImage gradientImage(rect.width(), 1, QImage::Format_ARGB32);
421 const int halfGradientHeight = rect.height() / 2;
422
423 // Top gradient
424 {
425 QRgb *pixel = reinterpret_cast<QRgb*>(gradientImage.bits());
426 for (int x = 0; x < gradientImage.width(); ++x, ++pixel) {
427 const qreal t = static_cast<qreal>(x) / static_cast<qreal>(gradientImage.width() - 1);
428 *pixel =KisPaintingTweaks::blendColors(m_handles.last().color, m_handles.first().color, t).rgba();
429 }
430 }
431 painter.drawImage(rect.adjusted(0, 0, 0, -(halfGradientHeight + (rect.height() & 1 ? 1 : 0))), gradientImage);
432
433 // Bottom gradient
434 QVector<Handle> sortedHandles_ = sortedHandles();
435 {
436 const int endPos = static_cast<int>(qRound(sortedHandles_.first().position * static_cast<qreal>(gradientImage.width() - 1)) + 1);
437 QRgb *pixel = reinterpret_cast<QRgb*>(gradientImage.bits());
438 for (int x = 0; x < endPos; ++x, ++pixel) {
439 *pixel = sortedHandles_.first().color.rgba();
440 }
441 }
442 paintBottomGradientMiddleSection(gradientImage, sortedHandles_);
443 {
444 const int startPos = static_cast<int>(qRound(sortedHandles_.last().position * static_cast<qreal>(gradientImage.width() - 1)) + 1);
445 QRgb *pixel = reinterpret_cast<QRgb*>(gradientImage.bits()) + startPos;
446 for (int x = startPos; x < rect.width(); ++x, ++pixel) {
447 *pixel = sortedHandles_.last().color.rgba();
448 }
449 }
450 painter.drawImage(rect.adjusted(0, halfGradientHeight, 0, 0), gradientImage);
451}
virtual void paintBottomGradientMiddleSection(QImage &gradientImage, const QVector< Handle > &sortedHandles_)
This is used to fill the space between the tho handles in the bottom bar of the "gradient"....
QVector< Handle > sortedHandles() const
Regardless the index of a handle, they can be unordered in terms of the position. This returns a sort...

References KisPaintingTweaks::blendColors(), KisLevelsSlider::m_handles, paintBottomGradientMiddleSection(), and KisLevelsSlider::sortedHandles().

◆ reset

void KisInputLevelsSlider::reset ( qreal newBlackPoint,
qreal newWhitePoint )
virtualslot

Sometimes you want to set the range to a totally different place, but the new black point can be greater than the old white point so the new black point position will be constrained to the old white position. This function allows to set both values at once to prevent that undesirable effect. Use it when the widget has to show new values, for a different set of levels parameters for example.

Definition at line 365 of file KisLevelsSlider.cpp.

366{
367 newBlackPoint = qBound(0.0, newBlackPoint, 1.0);
368 newWhitePoint = qBound(0.0, newWhitePoint, 1.0);
369
371 if (newWhitePoint < newBlackPoint + minimumSpaceBetweenHandles) {
372 newWhitePoint = newBlackPoint + minimumSpaceBetweenHandles;
373 if (newWhitePoint < 1.0) {
374 newWhitePoint = 1.0;
375 newBlackPoint = 1.0 - minimumSpaceBetweenHandles;
376 }
377 }
378 if (newBlackPoint <= whitePoint() - minimumSpaceBetweenHandles) {
379 setBlackPoint(newBlackPoint);
380 setWhitePoint(newWhitePoint);
381 } else {
382 setWhitePoint(newWhitePoint);
383 setBlackPoint(newBlackPoint);
384 }
385 } else {
386 setBlackPoint(newBlackPoint);
387 setWhitePoint(newWhitePoint);
388 }
389}
qreal whitePoint() const
Get the normalized white point.
virtual void setBlackPoint(qreal newBlackPoint)
Sets the black point.
virtual void setWhitePoint(qreal newWhitePoint)
Sets the white point.
int m_constrainPositions
This variable indicates if the handles can have unordered positions. If it is set to true then the us...
static constexpr qreal minimumSpaceBetweenHandles

References KisLevelsSlider::m_constrainPositions, KisLevelsSlider::minimumSpaceBetweenHandles, setBlackPoint(), setWhitePoint(), and whitePoint().

◆ setBlackPoint

void KisInputLevelsSlider::setBlackPoint ( qreal newBlackPoint)
virtualslot

Sets the black point.

Definition at line 355 of file KisLevelsSlider.cpp.

356{
357 setHandlePosition(m_handles.first().index, newBlackPoint);
358}
virtual void setHandlePosition(int handleIndex, qreal newPosition)
Sets the normalized position of the given handle.

References KisLevelsSlider::m_handles, and KisLevelsSlider::setHandlePosition().

◆ setWhitePoint

void KisInputLevelsSlider::setWhitePoint ( qreal newWhitePoint)
virtualslot

Sets the white point.

Definition at line 360 of file KisLevelsSlider.cpp.

361{
362 setHandlePosition(m_handles.last().index, newWhitePoint);
363}

References KisLevelsSlider::m_handles, and KisLevelsSlider::setHandlePosition().

◆ whitePoint()

qreal KisInputLevelsSlider::whitePoint ( ) const

Get the normalized white point.

Definition at line 350 of file KisLevelsSlider.cpp.

351{
352 return m_handles.last().position;
353}

References KisLevelsSlider::m_handles.

◆ whitePointChanged

void KisInputLevelsSlider::whitePointChanged ( qreal newWhitePoint)
signal

Signal emitted when the white point changes.


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