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.
20 // Adjust slightly so that the MIDI controller value of 106 becomes exactly 0.0 dB
21 // (cf. map_controller_to_float()); otherwise, we'd miss ever so slightly, which is
22 // really frustrating.
24 { -12.0, 1.0 - (1.0 - 106.5/127.0) * 3.0 }, // About 0.492.
26 // -12 to -21 is half the range (9 dB). Halve.
29 // -21 to -30 (9 dB) gets the same range as the previous one.
32 // -30 to -48 (18 dB) gets half of half.
35 // -48 to -84 (36 dB) gets half of half of half.
39 double slider_fraction_to_db(double db)
41 if (db >= fader_control_points[0].second) {
42 return fader_control_points[0].first;
44 if (db <= fader_control_points.back().second) {
45 return fader_control_points.back().first;
47 for (unsigned i = 1; i < fader_control_points.size(); ++i) {
48 const double x0 = fader_control_points[i].second;
49 const double x1 = fader_control_points[i - 1].second;
50 const double y0 = fader_control_points[i].first;
51 const double y1 = fader_control_points[i - 1].first;
52 if (db >= x0 && db <= x1) {
53 const double t = (db - x0) / (x1 - x0);
54 return y0 + t * (y1 - y0);
60 double db_to_slider_fraction(double x)
62 if (x >= fader_control_points[0].first) {
63 return fader_control_points[0].second;
65 if (x <= fader_control_points.back().first) {
66 return fader_control_points.back().second;
68 for (unsigned i = 1; i < fader_control_points.size(); ++i) {
69 const double x0 = fader_control_points[i].first;
70 const double x1 = fader_control_points[i - 1].first;
71 const double y0 = fader_control_points[i].second;
72 const double y1 = fader_control_points[i - 1].second;
73 if (x >= x0 && x <= x1) {
74 const double t = (x - x0) / (x1 - x0);
75 return y0 + t * (y1 - y0);
83 NonLinearFader::NonLinearFader(QWidget *parent)
86 update_slider_position();
89 void NonLinearFader::setDbValue(double db)
92 update_slider_position();
93 emit dbValueChanged(db);
96 void NonLinearFader::paintEvent(QPaintEvent *event)
98 QStyleOptionSlider opt;
99 this->initStyleOption(&opt);
100 QRect gr = this->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, this);
101 QRect sr = this->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);
103 // FIXME: Where does the slider_length / 2 come from? I can't really find it
104 // in the Qt code, but it seems to match up with reality.
105 int slider_length = sr.height();
106 int slider_max = gr.top() + (slider_length / 2);
107 int slider_min = gr.bottom() + (slider_length / 2) - slider_length + 1;
111 // Draw some ticks every 6 dB.
112 // FIXME: Find a way to make the slider wider, so that we have more space for tickmarks
113 // and some dB numbering.
115 p.setPen(Qt::darkGray);
116 for (int db = -84; db <= 6; db += 6) {
117 int y = slider_min + lrint(db_to_slider_fraction(db) * (slider_max - slider_min));
118 p.drawLine(QPoint(0, y), QPoint(gr.left() - x_margin, y));
119 p.drawLine(QPoint(gr.right() + x_margin, y), QPoint(width() - 1, y));
122 QSlider::paintEvent(event);
125 void NonLinearFader::sliderChange(SliderChange change)
127 QSlider::sliderChange(change);
128 if (change == QAbstractSlider::SliderValueChange && !inhibit_updates) {
130 db_value = -HUGE_VAL;
132 double frac = double(value() - minimum()) / (maximum() - minimum());
133 db_value = slider_fraction_to_db(frac);
135 emit dbValueChanged(db_value);
139 void NonLinearFader::update_slider_position()
141 inhibit_updates = true;
142 double val = db_to_slider_fraction(db_value) * (maximum() - minimum()) + minimum();
143 setValue(lrint(val));
144 inhibit_updates = false;