Mercurial > hg > svgui
comparison widgets/LevelPanWidget.cpp @ 1216:dc2af6616c83
Merge from branch 3.0-integration
author | Chris Cannam |
---|---|
date | Fri, 13 Jan 2017 10:29:50 +0000 |
parents | a68c25ba1153 |
children | 8ef67917c301 |
comparison
equal
deleted
inserted
replaced
1048:e8102ff5573b | 1216:dc2af6616c83 |
---|---|
19 #include <QWheelEvent> | 19 #include <QWheelEvent> |
20 | 20 |
21 #include "layer/ColourMapper.h" | 21 #include "layer/ColourMapper.h" |
22 #include "base/AudioLevel.h" | 22 #include "base/AudioLevel.h" |
23 | 23 |
24 #include "WidgetScale.h" | |
25 | |
24 #include <iostream> | 26 #include <iostream> |
25 #include <cmath> | 27 #include <cmath> |
26 #include <cassert> | 28 #include <cassert> |
27 | 29 |
28 using std::cerr; | 30 using std::cerr; |
33 | 35 |
34 LevelPanWidget::LevelPanWidget(QWidget *parent) : | 36 LevelPanWidget::LevelPanWidget(QWidget *parent) : |
35 QWidget(parent), | 37 QWidget(parent), |
36 m_level(maxLevel), | 38 m_level(maxLevel), |
37 m_pan(0), | 39 m_pan(0), |
40 m_monitorLeft(-1), | |
41 m_monitorRight(-1), | |
38 m_editable(true), | 42 m_editable(true), |
39 m_includeMute(true) | 43 m_includeMute(true) |
40 { | 44 { |
45 setToolTip(tr("Drag vertically to adjust level, horizontally to adjust pan")); | |
46 setLevel(1.0); | |
47 setPan(0.0); | |
41 } | 48 } |
42 | 49 |
43 LevelPanWidget::~LevelPanWidget() | 50 LevelPanWidget::~LevelPanWidget() |
44 { | 51 { |
45 } | 52 } |
46 | 53 |
47 QSize | 54 QSize |
48 LevelPanWidget::sizeHint() const | 55 LevelPanWidget::sizeHint() const |
49 { | 56 { |
50 static double ratio = 0.0; | 57 return WidgetScale::scaleQSize(QSize(40, 40)); |
51 if (ratio == 0.0) { | |
52 double baseEm; | |
53 #ifdef Q_OS_MAC | |
54 baseEm = 17.0; | |
55 #else | |
56 baseEm = 15.0; | |
57 #endif | |
58 double em = QFontMetrics(QFont()).height(); | |
59 ratio = em / baseEm; | |
60 } | |
61 | |
62 int pixels = 40; | |
63 int scaled = int(pixels * ratio + 0.5); | |
64 if (pixels != 0 && scaled == 0) scaled = 1; | |
65 return QSize(scaled, scaled); | |
66 } | 58 } |
67 | 59 |
68 static int | 60 static int |
69 db_to_level(double db) | 61 db_to_level(double db) |
70 { | 62 { |
89 else if (level == 2) return -6.; | 81 else if (level == 2) return -6.; |
90 else if (level == 1) return -12.; | 82 else if (level == 1) return -12.; |
91 else return -20.; | 83 else return -20.; |
92 } | 84 } |
93 | 85 |
94 void | 86 int |
95 LevelPanWidget::setLevel(float flevel) | 87 LevelPanWidget::audioLevelToLevel(float audioLevel, bool withMute) |
96 { | 88 { |
97 int level; | 89 int level; |
98 if (m_includeMute) { | 90 if (withMute) { |
99 level = AudioLevel::multiplier_to_fader | 91 level = AudioLevel::multiplier_to_fader |
100 (flevel, maxLevel, AudioLevel::ShortFader); | 92 (audioLevel, maxLevel, AudioLevel::ShortFader); |
101 } else { | 93 } else { |
102 level = db_to_level(AudioLevel::multiplier_to_dB(flevel)); | 94 level = db_to_level(AudioLevel::multiplier_to_dB(audioLevel)); |
103 } | 95 } |
104 if (level < 0) level = 0; | 96 if (level < 0) level = 0; |
105 if (level > maxLevel) level = maxLevel; | 97 if (level > maxLevel) level = maxLevel; |
98 return level; | |
99 } | |
100 | |
101 float | |
102 LevelPanWidget::levelToAudioLevel(int level, bool withMute) | |
103 { | |
104 if (withMute) { | |
105 return float(AudioLevel::fader_to_multiplier | |
106 (level, maxLevel, AudioLevel::ShortFader)); | |
107 } else { | |
108 return float(AudioLevel::dB_to_multiplier(level_to_db(level))); | |
109 } | |
110 } | |
111 | |
112 void | |
113 LevelPanWidget::setLevel(float flevel) | |
114 { | |
115 int level = audioLevelToLevel(flevel, m_includeMute); | |
106 if (level != m_level) { | 116 if (level != m_level) { |
107 m_level = level; | 117 m_level = level; |
108 float convertsTo = getLevel(); | 118 float convertsTo = getLevel(); |
109 if (fabsf(convertsTo - flevel) > 1e-5) { | 119 if (fabsf(convertsTo - flevel) > 1e-5) { |
110 emitLevelChanged(); | 120 emitLevelChanged(); |
114 } | 124 } |
115 | 125 |
116 float | 126 float |
117 LevelPanWidget::getLevel() const | 127 LevelPanWidget::getLevel() const |
118 { | 128 { |
119 if (m_includeMute) { | 129 float flevel = levelToAudioLevel(m_level, m_includeMute); |
120 return float(AudioLevel::fader_to_multiplier | 130 return flevel; |
121 (m_level, maxLevel, AudioLevel::ShortFader)); | 131 } |
122 } else { | 132 |
123 return float(AudioLevel::dB_to_multiplier(level_to_db(m_level))); | 133 int |
124 } | 134 LevelPanWidget::audioPanToPan(float audioPan) |
125 } | 135 { |
126 | 136 int pan = int(round(audioPan * maxPan)); |
127 void | 137 if (pan < -maxPan) pan = -maxPan; |
128 LevelPanWidget::setPan(float pan) | 138 if (pan > maxPan) pan = maxPan; |
129 { | 139 return pan; |
130 m_pan = int(round(pan * maxPan)); | 140 } |
131 if (m_pan < -maxPan) m_pan = -maxPan; | 141 |
132 if (m_pan > maxPan) m_pan = maxPan; | 142 float |
143 LevelPanWidget::panToAudioPan(int pan) | |
144 { | |
145 return float(pan) / float(maxPan); | |
146 } | |
147 | |
148 void | |
149 LevelPanWidget::setPan(float fpan) | |
150 { | |
151 int pan = audioPanToPan(fpan); | |
152 if (pan != m_pan) { | |
153 m_pan = pan; | |
154 update(); | |
155 } | |
156 } | |
157 | |
158 float | |
159 LevelPanWidget::getPan() const | |
160 { | |
161 return panToAudioPan(m_pan); | |
162 } | |
163 | |
164 void | |
165 LevelPanWidget::setMonitoringLevels(float left, float right) | |
166 { | |
167 m_monitorLeft = left; | |
168 m_monitorRight = right; | |
133 update(); | 169 update(); |
134 } | 170 } |
135 | 171 |
136 bool | 172 bool |
137 LevelPanWidget::isEditable() const | 173 LevelPanWidget::isEditable() const |
158 m_includeMute = include; | 194 m_includeMute = include; |
159 emitLevelChanged(); | 195 emitLevelChanged(); |
160 update(); | 196 update(); |
161 } | 197 } |
162 | 198 |
163 float | |
164 LevelPanWidget::getPan() const | |
165 { | |
166 return float(m_pan) / float(maxPan); | |
167 } | |
168 | |
169 void | 199 void |
170 LevelPanWidget::emitLevelChanged() | 200 LevelPanWidget::emitLevelChanged() |
171 { | 201 { |
172 cerr << "emitting levelChanged(" << getLevel() << ")" << endl; | |
173 emit levelChanged(getLevel()); | 202 emit levelChanged(getLevel()); |
174 } | 203 } |
175 | 204 |
176 void | 205 void |
177 LevelPanWidget::emitPanChanged() | 206 LevelPanWidget::emitPanChanged() |
178 { | 207 { |
179 cerr << "emitting panChanged(" << getPan() << ")" << endl; | |
180 emit panChanged(getPan()); | 208 emit panChanged(getPan()); |
181 } | 209 } |
182 | 210 |
183 void | 211 void |
184 LevelPanWidget::mousePressEvent(QMouseEvent *e) | 212 LevelPanWidget::mousePressEvent(QMouseEvent *e) |
336 | 364 |
337 pen.setColor(QColor(127, 127, 127, 127)); | 365 pen.setColor(QColor(127, 127, 127, 127)); |
338 pen.setWidthF(cellLightSize(rect).width() + thin); | 366 pen.setWidthF(cellLightSize(rect).width() + thin); |
339 pen.setCapStyle(Qt::RoundCap); | 367 pen.setCapStyle(Qt::RoundCap); |
340 paint.setPen(pen); | 368 paint.setPen(pen); |
369 paint.setBrush(Qt::NoBrush); | |
341 | 370 |
342 for (int pan = -maxPan; pan <= maxPan; ++pan) { | 371 for (int pan = -maxPan; pan <= maxPan; ++pan) { |
343 paint.drawLine(cellCentre(rect, 0, pan), cellCentre(rect, maxLevel, pan)); | 372 paint.drawLine(cellCentre(rect, 0, pan), cellCentre(rect, maxLevel, pan)); |
344 } | 373 } |
345 | 374 |
375 if (m_monitorLeft > 0.f || m_monitorRight > 0.f) { | |
376 paint.setPen(Qt::NoPen); | |
377 for (int pan = -maxPan; pan <= maxPan; ++pan) { | |
378 float audioPan = panToAudioPan(pan); | |
379 float audioLevel; | |
380 if (audioPan < 0.f) { | |
381 audioLevel = m_monitorLeft + m_monitorRight * (1.f + audioPan); | |
382 } else { | |
383 audioLevel = m_monitorRight + m_monitorLeft * (1.f - audioPan); | |
384 } | |
385 int levelHere = audioLevelToLevel(audioLevel, false); | |
386 for (int level = 0; level <= levelHere; ++level) { | |
387 paint.setBrush(level_to_colour(level)); | |
388 QRectF clr = cellLightRect(rect, level, pan); | |
389 paint.drawEllipse(clr); | |
390 } | |
391 } | |
392 paint.setPen(pen); | |
393 paint.setBrush(Qt::NoBrush); | |
394 } | |
395 | |
346 if (isEnabled()) { | 396 if (isEnabled()) { |
347 pen.setColor(Qt::black); | 397 pen.setColor(Qt::black); |
348 } else { | 398 } else { |
349 pen.setColor(Qt::darkGray); | 399 pen.setColor(Qt::darkGray); |
350 } | 400 } |
382 LevelPanWidget::paintEvent(QPaintEvent *) | 432 LevelPanWidget::paintEvent(QPaintEvent *) |
383 { | 433 { |
384 renderTo(this, rect(), m_editable); | 434 renderTo(this, rect(), m_editable); |
385 } | 435 } |
386 | 436 |
387 | 437 void |
388 | 438 LevelPanWidget::enterEvent(QEvent *e) |
439 { | |
440 QWidget::enterEvent(e); | |
441 emit mouseEntered(); | |
442 } | |
443 | |
444 void | |
445 LevelPanWidget::leaveEvent(QEvent *e) | |
446 { | |
447 QWidget::enterEvent(e); | |
448 emit mouseLeft(); | |
449 } | |
450 |