Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_equalizer_slider.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <QStyle>
10#include <QPainter>
11#include <QMouseEvent>
12#include <QApplication>
13#include <QStyleOptionSlider>
14
15#include "kis_global.h"
16#include "kis_debug.h"
17
19
20
22{
23 Private(KisEqualizerSlider *_q) : q(_q), isRightmost(false), toggleState(true) {}
24
28
29
30 QRect boundingRect() const;
31 QRect sliderRect() const;
32
33 int mousePosToValue(const QPoint &pt, bool round) const;
34};
35
37 : QAbstractSlider(parent),
38 m_d(new Private(this))
39{
40 setOrientation(Qt::Vertical);
41 setFocusPolicy(Qt::WheelFocus);
42 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
43}
44
48
50{
51 m_d->isRightmost = value;
52}
53
55{
56 m_d->toggleState = value;
57 update();
58
59}
60
62{
63 QRect bounds = q->rect().adjusted(0, 0, -static_cast<int>(isRightmost), -1);
64 return bounds;
65}
66
68{
69 const int offset = 3;
70 QRect filling = boundingRect().adjusted(offset + 1, offset + 1,
71 -offset, -offset);
72
73 return filling;
74}
75
76int KisEqualizerSlider::Private::mousePosToValue(const QPoint &pt, bool round) const
77{
78 const QRect areaRect = sliderRect();
79
80 int rawValue = -pt.y() + (areaRect.top() + areaRect.height());
81 int maxRawValue = areaRect.height();
82
83 int value = QStyle::sliderValueFromPosition(q->minimum(), q->maximum(), rawValue, maxRawValue);
84
85 if (round) {
86 const int singleStep = q->singleStep();
87 value = ((value + singleStep / 2) / singleStep) * singleStep;
88 }
89
90 return value;
91}
92
94{
95 if (maximum() == minimum() || (ev->buttons() ^ ev->button())) {
96 ev->ignore();
97 return;
98 }
99
100 const bool precise = ev->modifiers() & Qt::ControlModifier ||
101 ev->button() == Qt::RightButton;
102
103 int value = m_d->mousePosToValue(ev->pos(), !precise);
104 setSliderPosition(value);
105 triggerAction(SliderMove);
106 setRepeatAction(SliderNoAction);
107}
108
110{
111 if (ev->modifiers() & Qt::ShiftModifier &&
112 !rect().contains(ev->pos())) {
113
114 ev->ignore();
115 return;
116 }
117
118 const bool precise = ev->modifiers() & Qt::ControlModifier ||
119 ev->buttons() & Qt::RightButton;
120
121 int value = m_d->mousePosToValue(ev->pos(), !precise);
122 setSliderPosition(value);
123 triggerAction(SliderMove);
124 setRepeatAction(SliderNoAction);
125}
126
128{
129 Q_UNUSED(ev);
130}
131
133{
134 return QSize(25, 150);
135}
136
138{
139 return QSize(10, 40);
140}
141
142void KisEqualizerSlider::paintEvent(QPaintEvent *event)
143{
144 Q_UNUSED(event);
145
146 const QRect bounds = m_d->boundingRect();
147 const QColor backgroundColor = palette().color(QPalette::Base);
148
149
150 QPainter p(this);
151
152 { // draw border
153
154 QStyleOptionViewItem option; // empty!
155 const int gridHint = style()->styleHint(QStyle::SH_Table_GridLineColor, &option, this);
156 const QColor gridColor = static_cast<QRgb>(gridHint);
157 const QPen gridPen(gridColor);
158
159 p.setPen(gridPen);
160 p.setBrush(backgroundColor);
161 p.drawRect(bounds);
162 }
163
164 { // draw slider
165 QRect sliderRect = m_d->sliderRect();
166 const int sliderPos = QStyle::sliderPositionFromValue(minimum(), maximum(), value(), sliderRect.height());
167 sliderRect.adjust(0, sliderRect.height() - sliderPos, 0, 0);
168
169 p.setPen(Qt::transparent);
170
171 QColor color = m_d->toggleState ?
174 p.setBrush(color);
175
176 p.drawRect(sliderRect);
177 }
178
179 QString textValue = QString::number(value());
180
181 /* Text isn't really needed for onion skinning and makes it look a bit cluttered. Uncomment this out of that changes.
182 { // draw text
183 QPalette::ColorRole textRole = QPalette::Text;
184
185 //Draw text shadow, This will increase readability when the background of same color
186 QRect shadowRect(bounds);
187 shadowRect.translate(1,1);
188 QColor textColor = palette().color(textRole);
189 QColor shadowColor = (textColor.value() <= 128)
190 ? QColor(255,255,255,160) : QColor(0,0,0,160);
191
192 p.setPen(shadowColor);
193 p.drawText(shadowRect, Qt::AlignCenter, textValue);
194
195 p.setPen(textColor);
196 p.drawText(bounds, Qt::AlignCenter, textValue);
197 }
198 */
199
200 // draw focus rect
201 if (hasFocus()) {
202 QStyleOptionFocusRect fropt;
203 fropt.initFrom(this);
204 fropt.backgroundColor = backgroundColor;
205
206 int dfw1 = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &fropt, this) + 1,
207 dfw2 = dfw1 * 2;
208 fropt.rect = kisGrowRect(bounds, -dfw1 - dfw2);
209
210 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &fropt, &p, this);
211 }
212}
float value(const T *src, size_t ch)
const Params2D p
QColor onionSkinsSliderEnabledColor() const
QColor onionSkinsSliderDisabledColor() const
static KisAnimTimelineColors * instance()
void mouseReleaseEvent(QMouseEvent *ev) override
void setRightmost(bool value)
void paintEvent(QPaintEvent *event) override
QSize minimumSizeHint() const override
QSize sizeHint() const override
KisEqualizerSlider(QWidget *parent)
void mouseMoveEvent(QMouseEvent *ev) override
void setToggleState(bool value)
void mousePressEvent(QMouseEvent *ev) override
const QScopedPointer< Private > m_d
unsigned int QRgb
#define bounds(x, a, b)
T kisGrowRect(const T &rect, U offset)
Definition kis_global.h:186
rgba palette[MAX_PALETTE]
Definition palette.c:35
int mousePosToValue(const QPoint &pt, bool round) const
Private(KisEqualizerSlider *_q)