Mercurial > hg > easaier-soundaccess
comparison widgets/Fader.cpp @ 0:fc9323a41f5a
start base : Sonic Visualiser sv1-1.0rc1
author | lbajardsilogic |
---|---|
date | Fri, 11 May 2007 09:08:14 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:fc9323a41f5a |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 | |
8 This program is free software; you can redistribute it and/or | |
9 modify it under the terms of the GNU General Public License as | |
10 published by the Free Software Foundation; either version 2 of the | |
11 License, or (at your option) any later version. See the file | |
12 COPYING included with this distribution for more information. | |
13 */ | |
14 | |
15 /** | |
16 * Horizontal audio fader and meter widget. | |
17 * | |
18 * Based on the vertical fader and meter widget from the Hydrogen drum | |
19 * machine. (Any poor taste that has crept in during the | |
20 * modifications for this application is entirely my own, however.) | |
21 * The following copyright notice applies to code from this file, and | |
22 * also to the files in icons/fader_*.png (also modified by me). --cc | |
23 */ | |
24 | |
25 /** | |
26 * Hydrogen | |
27 * Copyright(c) 2002-2005 by Alex >Comix< Cominu [comix@users.sourceforge.net] | |
28 * http://www.hydrogen-music.org | |
29 */ | |
30 | |
31 | |
32 #include "Fader.h" | |
33 | |
34 #include "base/AudioLevel.h" | |
35 | |
36 #include <QMouseEvent> | |
37 #include <QPixmap> | |
38 #include <QWheelEvent> | |
39 #include <QPaintEvent> | |
40 #include <QPainter> | |
41 #include <QInputDialog> | |
42 | |
43 #include <iostream> | |
44 | |
45 Fader::Fader(QWidget *parent, bool withoutKnob) : | |
46 QWidget(parent), | |
47 m_withoutKnob(withoutKnob), | |
48 m_value(1.0), | |
49 m_peakLeft(0.0), | |
50 m_peakRight(0.0), | |
51 m_mousePressed(false) | |
52 { | |
53 setMinimumSize(116, 23); | |
54 setMaximumSize(116, 23); | |
55 resize(116, 23); | |
56 | |
57 QString background_path = ":/icons/fader_background.png"; | |
58 bool ok = m_back.load(background_path); | |
59 if (ok == false) { | |
60 std::cerr << "Fader: Error loading pixmap" << std::endl; | |
61 } | |
62 | |
63 QString leds_path = ":/icons/fader_leds.png"; | |
64 ok = m_leds.load(leds_path); | |
65 if (ok == false) { | |
66 std::cerr << "Error loading pixmap" << std::endl; | |
67 } | |
68 | |
69 QString knob_path = ":/icons/fader_knob.png"; | |
70 ok = m_knob.load(knob_path); | |
71 if (ok == false) { | |
72 std::cerr << "Error loading pixmap" << std::endl; | |
73 } | |
74 | |
75 QString clip_path = ":/icons/fader_knob_red.png"; | |
76 ok = m_clip.load(clip_path); | |
77 if (ok == false) { | |
78 std::cerr << "Error loading pixmap" << std::endl; | |
79 } | |
80 } | |
81 | |
82 Fader::~Fader() | |
83 { | |
84 | |
85 } | |
86 | |
87 void | |
88 Fader::mouseMoveEvent(QMouseEvent *ev) | |
89 { | |
90 if (ev->button() == Qt::MidButton) { | |
91 setValue(1.0); | |
92 emit valueChanged(1.0); | |
93 update(); | |
94 ev->accept(); | |
95 return; | |
96 } | |
97 if (!m_mousePressed) return; | |
98 | |
99 int x = ev->x(); | |
100 int diff = x - m_mousePressX; | |
101 if (diff == 0) return; | |
102 | |
103 int vx = AudioLevel::multiplier_to_fader | |
104 (m_mousePressValue, getMaxX(), AudioLevel::LongFader); | |
105 | |
106 vx += diff; | |
107 | |
108 if (vx > getMaxX()) vx = getMaxX(); | |
109 if (vx < 0) vx = 0; | |
110 | |
111 float fval = AudioLevel::fader_to_multiplier | |
112 (vx, getMaxX(), AudioLevel::LongFader); | |
113 | |
114 setValue(fval); | |
115 emit valueChanged(fval); | |
116 ev->accept(); | |
117 } | |
118 | |
119 | |
120 void | |
121 Fader::mouseReleaseEvent(QMouseEvent *ev) | |
122 { | |
123 if (m_mousePressed) { | |
124 mouseMoveEvent(ev); | |
125 m_mousePressed = false; | |
126 } | |
127 } | |
128 | |
129 void | |
130 Fader::mouseDoubleClickEvent(QMouseEvent *) | |
131 { | |
132 bool ok = false; | |
133 float min = AudioLevel::fader_to_dB | |
134 (0, getMaxX(), AudioLevel::LongFader); | |
135 float max = AudioLevel::fader_to_dB | |
136 (getMaxX(), getMaxX(), AudioLevel::LongFader); | |
137 float deft = AudioLevel::multiplier_to_dB(m_value); | |
138 | |
139 float dB = QInputDialog::getDouble | |
140 (this, | |
141 tr("Enter new fader level"), | |
142 tr("New fader level, from %1 to %2 dBFS:").arg(min).arg(max), | |
143 deft, min, max, 3, &ok); | |
144 | |
145 if (ok) { | |
146 float value = AudioLevel::dB_to_multiplier(dB); | |
147 setValue(value); | |
148 emit valueChanged(value); | |
149 update(); | |
150 } | |
151 } | |
152 | |
153 void | |
154 Fader::mousePressEvent(QMouseEvent *ev) | |
155 { | |
156 if (ev->button() == Qt::MidButton || | |
157 ((ev->button() == Qt::LeftButton) && | |
158 (ev->modifiers() & Qt::ControlModifier))) { | |
159 setValue(1.0); | |
160 emit valueChanged(1.0); | |
161 update(); | |
162 return; | |
163 } | |
164 | |
165 if (ev->button() != Qt::LeftButton) return; | |
166 m_mousePressed = true; | |
167 m_mousePressX = ev->x(); | |
168 m_mousePressValue = getValue(); | |
169 } | |
170 | |
171 | |
172 void | |
173 Fader::wheelEvent(QWheelEvent *ev) | |
174 { | |
175 ev->accept(); | |
176 | |
177 //!!! needs improvement | |
178 | |
179 if (ev->delta() > 0) { | |
180 setValue(m_value * 1.1); | |
181 } else { | |
182 setValue(m_value / 1.1); | |
183 } | |
184 | |
185 update(); | |
186 emit valueChanged(getValue()); | |
187 } | |
188 | |
189 void | |
190 Fader::enterEvent(QEvent *) | |
191 { | |
192 emit mouseEntered(); | |
193 } | |
194 | |
195 void | |
196 Fader::leaveEvent(QEvent *) | |
197 { | |
198 emit mouseLeft(); | |
199 } | |
200 | |
201 void | |
202 Fader::setValue(float v) | |
203 { | |
204 float max = AudioLevel::dB_to_multiplier(10.0); | |
205 | |
206 if (v > max) { | |
207 v = max; | |
208 } else if (v < 0.0) { | |
209 v = 0.0; | |
210 } | |
211 | |
212 if (m_value != v) { | |
213 m_value = v; | |
214 float db = AudioLevel::multiplier_to_dB(m_value); | |
215 QString text; | |
216 if (db <= AudioLevel::DB_FLOOR) { | |
217 text = tr("Level: Off"); | |
218 } else { | |
219 text = tr("Level: %1%2.%3%4 dB") | |
220 .arg(db < 0.0 ? "-" : "") | |
221 .arg(abs(int(db))) | |
222 .arg(abs(int(db * 10.0) % 10)) | |
223 .arg(abs(int(db * 100.0) % 10)); | |
224 } | |
225 std::cerr << "Fader: setting tooltip to \"" << text.toStdString() << "\"" << std::endl; | |
226 QWidget::setToolTip(text); | |
227 update(); | |
228 } | |
229 } | |
230 | |
231 | |
232 float | |
233 Fader::getValue() | |
234 { | |
235 return m_value; | |
236 } | |
237 | |
238 | |
239 | |
240 void | |
241 Fader::setPeakLeft(float peak) | |
242 { | |
243 if (this->m_peakLeft != peak) { | |
244 this->m_peakLeft = peak; | |
245 update(); | |
246 } | |
247 } | |
248 | |
249 | |
250 void | |
251 Fader::setPeakRight(float peak) | |
252 { | |
253 if (this->m_peakRight != peak) { | |
254 this->m_peakRight = peak; | |
255 update(); | |
256 } | |
257 } | |
258 | |
259 | |
260 void | |
261 Fader::paintEvent(QPaintEvent *) | |
262 { | |
263 QPainter painter(this); | |
264 | |
265 // background | |
266 painter.drawPixmap(rect(), m_back, QRect(0, 0, 116, 23)); | |
267 | |
268 int offset_L = AudioLevel::multiplier_to_fader(m_peakLeft, 116, | |
269 AudioLevel::IEC268LongMeter); | |
270 | |
271 painter.drawPixmap(QRect(0, 0, offset_L, 11), m_leds, | |
272 QRect(0, 0, offset_L, 11)); | |
273 | |
274 int offset_R = AudioLevel::multiplier_to_fader(m_peakRight, 116, | |
275 AudioLevel::IEC268LongMeter); | |
276 | |
277 painter.drawPixmap(QRect(0, 11, offset_R, 11), m_leds, | |
278 QRect(0, 11, offset_R, 11)); | |
279 | |
280 if (m_withoutKnob == false) { | |
281 | |
282 static const uint knob_width = 29; | |
283 static const uint knob_height = 9; | |
284 | |
285 int x = AudioLevel::multiplier_to_fader(m_value, 116 - knob_width, | |
286 AudioLevel::LongFader); | |
287 | |
288 bool clipping = (m_peakLeft > 1.0 || m_peakRight > 1.0); | |
289 | |
290 painter.drawPixmap(QRect(x, 7, knob_width, knob_height), | |
291 clipping ? m_clip : m_knob, | |
292 QRect(0, 0, knob_width, knob_height)); | |
293 } | |
294 } | |
295 | |
296 int | |
297 Fader::getMaxX() const | |
298 { | |
299 return 116 - 12; | |
300 } |