Mercurial > hg > svgui
comparison widgets/Fader.cpp @ 0:2a4f26e85b4c
initial import
author | Chris Cannam |
---|---|
date | Tue, 10 Jan 2006 16:33:16 +0000 |
parents | |
children | 37b110168acf |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:2a4f26e85b4c |
---|---|
1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 A waveform viewer and audio annotation editor. | |
5 Chris Cannam, Queen Mary University of London, 2005 | |
6 | |
7 This is experimental software. Not for distribution. | |
8 */ | |
9 | |
10 /** | |
11 * Horizontal audio fader and meter widget. | |
12 * | |
13 * Based on the vertical fader and meter widget from the Hydrogen drum | |
14 * machine. (Any poor taste that has crept in during the | |
15 * modifications for this application is entirely my own, however.) | |
16 * The following copyright notice applies to code from this file, and | |
17 * also to the files in icons/fader_*.png (also modified by me). --cc | |
18 */ | |
19 | |
20 /** | |
21 * Hydrogen | |
22 * Copyright(c) 2002-2005 by Alex >Comix< Cominu [comix@users.sourceforge.net] | |
23 * | |
24 * http://www.hydrogen-music.org | |
25 * | |
26 * This program is free software; you can redistribute it and/or modify | |
27 * it under the terms of the GNU General Public License as published by | |
28 * the Free Software Foundation; either version 2 of the License, or | |
29 * (at your option) any later version. | |
30 * | |
31 * This program is distributed in the hope that it will be useful, | |
32 * but WITHOUT ANY WARRANTY, without even the implied warranty of | |
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
34 * GNU General Public License for more details. | |
35 * | |
36 * You should have received a copy of the GNU General Public License | |
37 * along with this program; if not, write to the Free Software | |
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
39 */ | |
40 | |
41 | |
42 #include "Fader.h" | |
43 | |
44 #include "base/AudioLevel.h" | |
45 | |
46 #include <QMouseEvent> | |
47 #include <QPixmap> | |
48 #include <QWheelEvent> | |
49 #include <QPaintEvent> | |
50 #include <QPainter> | |
51 | |
52 Fader::Fader(QWidget *parent, bool withoutKnob) : | |
53 QWidget(parent), | |
54 m_withoutKnob(withoutKnob), | |
55 m_value(1.0), | |
56 m_peakLeft(0.0), | |
57 m_peakRight(0.0) | |
58 { | |
59 setMinimumSize(116, 23); | |
60 setMaximumSize(116, 23); | |
61 resize(116, 23); | |
62 | |
63 QString background_path = ":/icons/fader_background.png"; | |
64 bool ok = m_back.load(background_path); | |
65 if (ok == false) { | |
66 std::cerr << "Fader: Error loading pixmap" << std::endl; | |
67 } | |
68 | |
69 QString leds_path = ":/icons/fader_leds.png"; | |
70 ok = m_leds.load(leds_path); | |
71 if (ok == false) { | |
72 std::cerr << "Error loading pixmap" << std::endl; | |
73 } | |
74 | |
75 QString knob_path = ":/icons/fader_knob.png"; | |
76 ok = m_knob.load(knob_path); | |
77 if (ok == false) { | |
78 std::cerr << "Error loading pixmap" << std::endl; | |
79 } | |
80 | |
81 QString clip_path = ":/icons/fader_knob_red.png"; | |
82 ok = m_clip.load(clip_path); | |
83 if (ok == false) { | |
84 std::cerr << "Error loading pixmap" << std::endl; | |
85 } | |
86 } | |
87 | |
88 Fader::~Fader() | |
89 { | |
90 | |
91 } | |
92 | |
93 void | |
94 Fader::mouseMoveEvent(QMouseEvent *ev) | |
95 { | |
96 int x = ev->x() - 6; | |
97 const int max_x = 116 - 12; | |
98 | |
99 int value = x; | |
100 | |
101 if (value > max_x) { | |
102 value = max_x; | |
103 } else if (value < 0) { | |
104 value = 0; | |
105 } | |
106 | |
107 // float fval = float(value) / float(max_x); | |
108 float fval = AudioLevel::fader_to_multiplier | |
109 (value, max_x, AudioLevel::LongFader); | |
110 | |
111 setValue(fval); | |
112 emit valueChanged(fval); | |
113 | |
114 update(); | |
115 } | |
116 | |
117 | |
118 void | |
119 Fader::mouseDoubleClickEvent(QMouseEvent *) | |
120 { | |
121 setValue(1.0); | |
122 emit valueChanged(1.0); | |
123 update(); | |
124 } | |
125 | |
126 void | |
127 Fader::mousePressEvent(QMouseEvent *ev) | |
128 { | |
129 int x = ev->x() - 6; | |
130 const int max_x = 116 - 12; | |
131 | |
132 int value = x; | |
133 | |
134 if (value > max_x) { | |
135 value = max_x; | |
136 } else if (value < 0) { | |
137 value = 0; | |
138 } | |
139 | |
140 float fval = AudioLevel::fader_to_multiplier | |
141 (value, max_x, AudioLevel::LongFader); | |
142 | |
143 setValue(fval); | |
144 emit valueChanged(fval); | |
145 | |
146 update(); | |
147 } | |
148 | |
149 | |
150 void | |
151 Fader::wheelEvent(QWheelEvent *ev) | |
152 { | |
153 ev->accept(); | |
154 | |
155 //!!! needs improvement | |
156 | |
157 if (ev->delta() > 0) { | |
158 setValue(m_value * 1.1); | |
159 } else { | |
160 setValue(m_value / 1.1); | |
161 } | |
162 | |
163 update(); | |
164 emit valueChanged(getValue()); | |
165 } | |
166 | |
167 | |
168 void | |
169 Fader::setValue(float v) | |
170 { | |
171 float max = AudioLevel::dB_to_multiplier(10.0); | |
172 | |
173 if (v > max) { | |
174 v = max; | |
175 } else if (v < 0.0) { | |
176 v = 0.0; | |
177 } | |
178 | |
179 if (m_value != v) { | |
180 m_value = v; | |
181 float db = AudioLevel::multiplier_to_dB(m_value); | |
182 if (db <= AudioLevel::DB_FLOOR) { | |
183 setToolTip(tr("Level: Off")); | |
184 } else { | |
185 setToolTip(tr("Level: %1%2.%3%4 dB") | |
186 .arg(db < 0.0 ? "-" : "") | |
187 .arg(abs(int(db))) | |
188 .arg(abs(int(db * 10.0) % 10)) | |
189 .arg(abs(int(db * 100.0) % 10))); | |
190 } | |
191 update(); | |
192 } | |
193 } | |
194 | |
195 | |
196 float | |
197 Fader::getValue() | |
198 { | |
199 return m_value; | |
200 } | |
201 | |
202 | |
203 | |
204 void | |
205 Fader::setPeakLeft(float peak) | |
206 { | |
207 if (this->m_peakLeft != peak) { | |
208 this->m_peakLeft = peak; | |
209 update(); | |
210 } | |
211 } | |
212 | |
213 | |
214 void | |
215 Fader::setPeakRight(float peak) | |
216 { | |
217 if (this->m_peakRight != peak) { | |
218 this->m_peakRight = peak; | |
219 update(); | |
220 } | |
221 } | |
222 | |
223 | |
224 void | |
225 Fader::paintEvent(QPaintEvent *) | |
226 { | |
227 QPainter painter(this); | |
228 | |
229 // background | |
230 painter.drawPixmap(rect(), m_back, QRect(0, 0, 116, 23)); | |
231 | |
232 int offset_L = AudioLevel::multiplier_to_fader(m_peakLeft, 116, | |
233 AudioLevel::IEC268LongMeter); | |
234 | |
235 painter.drawPixmap(QRect(0, 0, offset_L, 11), m_leds, | |
236 QRect(0, 0, offset_L, 11)); | |
237 | |
238 int offset_R = AudioLevel::multiplier_to_fader(m_peakRight, 116, | |
239 AudioLevel::IEC268LongMeter); | |
240 | |
241 painter.drawPixmap(QRect(0, 11, offset_R, 11), m_leds, | |
242 QRect(0, 11, offset_R, 11)); | |
243 | |
244 if (m_withoutKnob == false) { | |
245 | |
246 static const uint knob_width = 29; | |
247 static const uint knob_height = 9; | |
248 | |
249 int x = AudioLevel::multiplier_to_fader(m_value, 116 - knob_width, | |
250 AudioLevel::LongFader); | |
251 | |
252 bool clipping = (m_peakLeft > 1.0 || m_peakRight > 1.0); | |
253 | |
254 painter.drawPixmap(QRect(x, 7, knob_width, knob_height), | |
255 clipping ? m_clip : m_knob, | |
256 QRect(0, 0, knob_width, knob_height)); | |
257 } | |
258 } | |
259 | |
260 |