7 #include <QResizeEvent>
10 #include <QStyleOptionSlider>
12 #include "nonlinear_fader.h"
18 vector<pair<double, double>> fader_control_points = {
19 // The main area is from +6 to -12 dB (18 dB), and we use half the slider range for it.
23 // -12 to -21 is half the range (9 dB). Halve.
26 // -21 to -30 (9 dB) gets the same range as the previous one.
29 // -30 to -48 (18 dB) gets half of half.
32 // -48 to -84 (36 dB) gets half of half of half.
36 double slider_fraction_to_db(double db)
38 if (db <= fader_control_points[0].second) {
39 return fader_control_points[0].first;
41 if (db >= fader_control_points.back().second) {
42 return fader_control_points.back().first;
44 for (unsigned i = 1; i < fader_control_points.size(); ++i) {
45 const double x0 = fader_control_points[i - 1].second;
46 const double x1 = fader_control_points[i].second;
47 const double y0 = fader_control_points[i - 1].first;
48 const double y1 = fader_control_points[i].first;
49 if (db >= x0 && db <= x1) {
50 const double t = (db - x0) / (x1 - x0);
51 return y0 + t * (y1 - y0);
57 double db_to_slider_fraction(double x)
59 if (x >= fader_control_points[0].first) {
60 return fader_control_points[0].second;
62 if (x <= fader_control_points.back().first) {
63 return fader_control_points.back().second;
65 for (unsigned i = 1; i < fader_control_points.size(); ++i) {
66 const double x0 = fader_control_points[i].first;
67 const double x1 = fader_control_points[i - 1].first;
68 const double y0 = fader_control_points[i].second;
69 const double y1 = fader_control_points[i - 1].second;
70 if (x >= x0 && x <= x1) {
71 const double t = (x - x0) / (x1 - x0);
72 return y0 + t * (y1 - y0);
80 NonLinearFader::NonLinearFader(QWidget *parent)
83 update_slider_position();
86 void NonLinearFader::setDbValue(double db)
89 update_slider_position();
90 emit dbValueChanged(db);
93 void NonLinearFader::paintEvent(QPaintEvent *event)
95 QStyleOptionSlider opt;
96 this->initStyleOption(&opt);
97 QRect gr = this->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
98 QRect sr = this->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
100 // FIXME: Where does the slider_length / 2 come from? I can't really find it
101 // in the Qt code, but it seems to match up with reality.
102 int slider_length = sr.height();
103 int slider_min = gr.top() + (slider_length / 2);
104 int slider_max = gr.bottom() + (slider_length / 2) - slider_length + 1;
108 // Draw some ticks every 6 dB.
109 // FIXME: Find a way to make the slider wider, so that we have more space for tickmarks
110 // and some dB numbering.
112 p.setPen(Qt::darkGray);
113 for (int db = -84; db <= 6; db += 6) {
114 int y = slider_min + lrint(db_to_slider_fraction(db) * (slider_max - slider_min));
115 p.drawLine(QPoint(0, y), QPoint(gr.left() - x_margin, y));
116 p.drawLine(QPoint(gr.right() + x_margin, y), QPoint(width() - 1, y));
119 QSlider::paintEvent(event);
122 void NonLinearFader::resizeEvent(QResizeEvent *event)
124 QSlider::resizeEvent(event);
125 inhibit_updates = true;
126 setRange(0, event->size().height() - 1);
127 inhibit_updates = false;
128 update_slider_position();
131 void NonLinearFader::sliderChange(SliderChange change)
133 QSlider::sliderChange(change);
134 if (change == QAbstractSlider::SliderValueChange && !inhibit_updates) {
136 db_value = -HUGE_VAL;
138 double frac = 1.0 - value() / (height() - 1.0);
139 db_value = slider_fraction_to_db(frac);
141 emit dbValueChanged(db_value);
145 void NonLinearFader::update_slider_position()
147 inhibit_updates = true;
148 setValue(lrint((1.0 - db_to_slider_fraction(db_value)) * (height() - 1.0)));
149 inhibit_updates = false;