1 #include "nonlinear_fader.h"
9 #include <QStyleOption>
14 #include "piecewise_interpolator.h"
23 PiecewiseInterpolator interpolator({
24 // The main area is from +6 to -12 dB (18 dB), and we use half the slider range for it.
25 // Adjust slightly so that the MIDI controller value of 106 becomes exactly 0.0 dB
26 // (cf. map_controller_to_float()); otherwise, we'd miss ever so slightly, which is
27 // really frustrating.
29 { -12.0, 1.0 - (1.0 - 106.5/127.0) * 3.0 }, // About 0.492.
31 // -12 to -21 is half the range (9 dB). Halve.
34 // -21 to -30 (9 dB) gets the same range as the previous one.
37 // -30 to -48 (18 dB) gets half of half.
40 // -48 to -84 (36 dB) gets half of half of half.
46 NonLinearFader::NonLinearFader(QWidget *parent)
49 update_slider_position();
52 void NonLinearFader::setDbValue(double db)
55 update_slider_position();
56 emit dbValueChanged(db);
59 void NonLinearFader::paintEvent(QPaintEvent *event)
61 QStyleOptionSlider opt;
62 this->initStyleOption(&opt);
63 QRect gr = this->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
64 QRect sr = this->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
66 // FIXME: Where does the slider_length / 2 come from? I can't really find it
67 // in the Qt code, but it seems to match up with reality.
68 int slider_length = sr.height();
69 int slider_max = gr.top() + (slider_length / 2);
70 int slider_min = gr.bottom() + (slider_length / 2) - slider_length + 1;
74 // Draw some ticks every 6 dB.
75 // FIXME: Find a way to make the slider wider, so that we have more space for tickmarks
76 // and some dB numbering.
78 p.setPen(Qt::darkGray);
79 for (int db = -84; db <= 6; db += 6) {
80 int y = slider_min + lrint(interpolator.db_to_fraction(db) * (slider_max - slider_min));
81 p.drawLine(QPoint(0, y), QPoint(gr.left() - x_margin, y));
82 p.drawLine(QPoint(gr.right() + x_margin, y), QPoint(width() - 1, y));
85 QSlider::paintEvent(event);
88 void NonLinearFader::sliderChange(SliderChange change)
90 QSlider::sliderChange(change);
91 if (change == QAbstractSlider::SliderValueChange && !inhibit_updates) {
95 double frac = double(value() - minimum()) / (maximum() - minimum());
96 db_value = interpolator.fraction_to_db(frac);
98 emit dbValueChanged(db_value);
102 void NonLinearFader::update_slider_position()
104 inhibit_updates = true;
105 double val = interpolator.db_to_fraction(db_value) * (maximum() - minimum()) + minimum();
106 setValue(lrint(val));
107 inhibit_updates = false;