Mercurial > hg > svgui
comparison widgets/AudioDial.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 * A rotary dial widget. | |
12 * | |
13 * Based on an original design by Thorsten Wilms. | |
14 * | |
15 * Implemented as a widget for the Rosegarden MIDI and audio sequencer | |
16 * and notation editor by Chris Cannam. | |
17 * | |
18 * Extracted into a standalone Qt3 widget by Pedro Lopez-Cabanillas | |
19 * and adapted for use in QSynth. | |
20 * | |
21 * Ported to Qt4 by Chris Cannam. | |
22 * | |
23 * This file copyright 2003-2005 Chris Cannam, copyright 2005 Pedro | |
24 * Lopez-Cabanillas. | |
25 * | |
26 * This program is free software; you can redistribute it and/or | |
27 * modify it under the terms of the GNU General Public License as | |
28 * published by the Free Software Foundation; either version 2 of the | |
29 * License, or (at your option) any later version. See the file | |
30 * COPYING included with this distribution for more information. | |
31 */ | |
32 | |
33 #include "AudioDial.h" | |
34 | |
35 #include <cmath> | |
36 #include <iostream> | |
37 | |
38 #include <QTimer> | |
39 #include <QPainter> | |
40 #include <QPixmap> | |
41 #include <QImage> | |
42 #include <QColormap> | |
43 #include <QMouseEvent> | |
44 #include <QPaintEvent> | |
45 | |
46 using std::endl; | |
47 using std::cerr; | |
48 | |
49 | |
50 //!!! Pedro updated his version to use my up/down response code from RG -- need to grab that code in preference to this version from Rui | |
51 | |
52 | |
53 //------------------------------------------------------------------------- | |
54 // AudioDial - Instance knob widget class. | |
55 // | |
56 | |
57 #define AUDIO_DIAL_MIN (0.25 * M_PI) | |
58 #define AUDIO_DIAL_MAX (1.75 * M_PI) | |
59 #define AUDIO_DIAL_RANGE (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) | |
60 | |
61 | |
62 // Constructor. | |
63 AudioDial::AudioDial(QWidget *parent) : | |
64 QDial(parent), | |
65 m_knobColor(Qt::black), m_meterColor(Qt::white) | |
66 { | |
67 m_mouseDial = false; | |
68 m_mousePressed = false; | |
69 } | |
70 | |
71 | |
72 // Destructor. | |
73 AudioDial::~AudioDial (void) | |
74 { | |
75 } | |
76 | |
77 | |
78 void AudioDial::paintEvent(QPaintEvent *) | |
79 { | |
80 QPainter paint; | |
81 | |
82 float angle = AUDIO_DIAL_MIN // offset | |
83 + (AUDIO_DIAL_RANGE * | |
84 (float(QDial::value() - QDial::minimum()) / | |
85 (float(QDial::maximum() - QDial::minimum())))); | |
86 int degrees = int(angle * 180.0 / M_PI); | |
87 | |
88 int ns = notchSize(); | |
89 int numTicks = 1 + (maximum() + ns - minimum()) / ns; | |
90 | |
91 QColor knobColor(m_knobColor); | |
92 if (knobColor == Qt::black) | |
93 knobColor = palette().mid().color(); | |
94 | |
95 QColor meterColor(m_meterColor); | |
96 if (!isEnabled()) | |
97 meterColor = palette().mid().color(); | |
98 else if (m_meterColor == Qt::white) | |
99 meterColor = palette().highlight().color(); | |
100 | |
101 int m_size = width() < height() ? width() : height(); | |
102 int scale = 1; | |
103 int width = m_size - 2*scale, height = m_size - 2*scale; | |
104 | |
105 paint.begin(this); | |
106 paint.setRenderHint(QPainter::Antialiasing, true); | |
107 paint.translate(1, 1); | |
108 | |
109 QPen pen; | |
110 QColor c; | |
111 | |
112 // Knob body and face... | |
113 | |
114 c = knobColor; | |
115 pen.setColor(knobColor); | |
116 pen.setWidth(scale * 2); | |
117 pen.setCapStyle(Qt::FlatCap); | |
118 | |
119 paint.setPen(pen); | |
120 paint.setBrush(c); | |
121 | |
122 int indent = (int)(width * 0.15 + 1); | |
123 | |
124 paint.drawEllipse(indent-1, indent-1, width-2*indent, width-2*indent); | |
125 | |
126 pen.setWidth(3 * scale); | |
127 int pos = indent-1 + (width-2*indent) / 20; | |
128 int darkWidth = (width-2*indent) * 3 / 4; | |
129 while (darkWidth) { | |
130 c = c.light(102); | |
131 pen.setColor(c); | |
132 paint.setPen(pen); | |
133 paint.drawEllipse(pos, pos, darkWidth, darkWidth); | |
134 if (!--darkWidth) break; | |
135 paint.drawEllipse(pos, pos, darkWidth, darkWidth); | |
136 if (!--darkWidth) break; | |
137 paint.drawEllipse(pos, pos, darkWidth, darkWidth); | |
138 ++pos; --darkWidth; | |
139 } | |
140 | |
141 // Tick notches... | |
142 | |
143 if ( true/* notchesVisible() */) { | |
144 // std::cerr << "Notches visible" << std::endl; | |
145 pen.setColor(palette().dark().color()); | |
146 pen.setWidth(scale); | |
147 paint.setPen(pen); | |
148 for (int i = 0; i < numTicks; ++i) { | |
149 int div = numTicks; | |
150 if (div > 1) --div; | |
151 drawTick(paint, AUDIO_DIAL_MIN + (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) * i / div, | |
152 width, true); | |
153 } | |
154 } | |
155 | |
156 // The bright metering bit... | |
157 | |
158 c = meterColor; | |
159 pen.setColor(c); | |
160 pen.setWidth(indent); | |
161 paint.setPen(pen); | |
162 | |
163 // std::cerr << "degrees " << degrees << ", gives us " << -(degrees - 45) * 16 << std::endl; | |
164 | |
165 int arcLen = -(degrees - 45) * 16; | |
166 if (arcLen == 0) arcLen = -16; | |
167 | |
168 paint.drawArc(indent/2, indent/2, | |
169 width-indent, width-indent, (180 + 45) * 16, arcLen); | |
170 | |
171 paint.setBrush(Qt::NoBrush); | |
172 | |
173 // Shadowing... | |
174 | |
175 pen.setWidth(scale); | |
176 paint.setPen(pen); | |
177 | |
178 // Knob shadow... | |
179 | |
180 int shadowAngle = -720; | |
181 c = knobColor.dark(); | |
182 for (int arc = 120; arc < 2880; arc += 240) { | |
183 pen.setColor(c); | |
184 paint.setPen(pen); | |
185 paint.drawArc(indent, indent, | |
186 width-2*indent, width-2*indent, shadowAngle + arc, 240); | |
187 paint.drawArc(indent, indent, | |
188 width-2*indent, width-2*indent, shadowAngle - arc, 240); | |
189 c = c.light(110); | |
190 } | |
191 | |
192 // Scale shadow... | |
193 | |
194 shadowAngle = 2160; | |
195 c = palette().dark().color(); | |
196 for (int arc = 120; arc < 2880; arc += 240) { | |
197 pen.setColor(c); | |
198 paint.setPen(pen); | |
199 paint.drawArc(scale/2, scale/2, | |
200 width-scale, width-scale, shadowAngle + arc, 240); | |
201 paint.drawArc(scale/2, scale/2, | |
202 width-scale, width-scale, shadowAngle - arc, 240); | |
203 c = c.light(108); | |
204 } | |
205 | |
206 // Undraw the bottom part... | |
207 | |
208 pen.setColor(palette().background().color()); | |
209 pen.setWidth(scale * 4); | |
210 paint.setPen(pen); | |
211 paint.drawArc(scale/2, scale/2, | |
212 width-scale, width-scale, -45 * 16, -92 * 16); | |
213 | |
214 // Scale ends... | |
215 | |
216 pen.setColor(palette().dark().color()); | |
217 pen.setWidth(scale); | |
218 paint.setPen(pen); | |
219 for (int i = 0; i < numTicks; ++i) { | |
220 if (i != 0 && i != numTicks - 1) continue; | |
221 int div = numTicks; | |
222 if (div > 1) --div; | |
223 drawTick(paint, AUDIO_DIAL_MIN + (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) * i / div, | |
224 width, false); | |
225 } | |
226 | |
227 // Pointer notch... | |
228 | |
229 float hyp = float(width) / 2.0; | |
230 float len = hyp - indent; | |
231 --len; | |
232 | |
233 float x0 = hyp; | |
234 float y0 = hyp; | |
235 | |
236 float x = hyp - len * sin(angle); | |
237 float y = hyp + len * cos(angle); | |
238 | |
239 c = palette().dark().color(); | |
240 pen.setColor(isEnabled() ? c.dark(130) : c); | |
241 pen.setWidth(scale * 2); | |
242 paint.setPen(pen); | |
243 paint.drawLine(int(x0), int(y0), int(x), int(y)); | |
244 | |
245 paint.end(); | |
246 } | |
247 | |
248 | |
249 void AudioDial::drawTick(QPainter &paint, | |
250 float angle, int size, bool internal) | |
251 { | |
252 float hyp = float(size) / 2.0; | |
253 float x0 = hyp - (hyp - 1) * sin(angle); | |
254 float y0 = hyp + (hyp - 1) * cos(angle); | |
255 | |
256 // cerr << "drawTick: angle " << angle << ", size " << size << ", internal " << internal << endl; | |
257 | |
258 if (internal) { | |
259 | |
260 float len = hyp / 4; | |
261 float x1 = hyp - (hyp - len) * sin(angle); | |
262 float y1 = hyp + (hyp - len) * cos(angle); | |
263 | |
264 paint.drawLine(int(x0), int(y0), int(x1), int(y1)); | |
265 | |
266 } else { | |
267 | |
268 float len = hyp / 4; | |
269 float x1 = hyp - (hyp + len) * sin(angle); | |
270 float y1 = hyp + (hyp + len) * cos(angle); | |
271 | |
272 paint.drawLine(int(x0), int(y0), int(x1), int(y1)); | |
273 } | |
274 } | |
275 | |
276 | |
277 void AudioDial::setKnobColor(const QColor& color) | |
278 { | |
279 m_knobColor = color; | |
280 update(); | |
281 } | |
282 | |
283 | |
284 void AudioDial::setMeterColor(const QColor& color) | |
285 { | |
286 m_meterColor = color; | |
287 update(); | |
288 } | |
289 | |
290 | |
291 void AudioDial::setMouseDial(bool mouseDial) | |
292 { | |
293 m_mouseDial = mouseDial; | |
294 } | |
295 | |
296 | |
297 // Alternate mouse behavior event handlers. | |
298 void AudioDial::mousePressEvent(QMouseEvent *mouseEvent) | |
299 { | |
300 if (m_mouseDial) { | |
301 QDial::mousePressEvent(mouseEvent); | |
302 } else if (mouseEvent->button() == Qt::LeftButton) { | |
303 m_mousePressed = true; | |
304 m_posMouse = mouseEvent->pos(); | |
305 } | |
306 } | |
307 | |
308 | |
309 void AudioDial::mouseMoveEvent(QMouseEvent *mouseEvent) | |
310 { | |
311 if (m_mouseDial) { | |
312 QDial::mouseMoveEvent(mouseEvent); | |
313 } else if (m_mousePressed) { | |
314 const QPoint& posMouse = mouseEvent->pos(); | |
315 int v = QDial::value() | |
316 + (posMouse.x() - m_posMouse.x()) | |
317 + (m_posMouse.y() - posMouse.y()); | |
318 if (v > QDial::maximum()) | |
319 v = QDial::maximum(); | |
320 else | |
321 if (v < QDial::minimum()) | |
322 v = QDial::minimum(); | |
323 m_posMouse = posMouse; | |
324 QDial::setValue(v); | |
325 } | |
326 } | |
327 | |
328 | |
329 void AudioDial::mouseReleaseEvent(QMouseEvent *mouseEvent) | |
330 { | |
331 if (m_mouseDial) { | |
332 QDial::mouseReleaseEvent(mouseEvent); | |
333 } else if (m_mousePressed) { | |
334 m_mousePressed = false; | |
335 } | |
336 } | |
337 | |
338 #ifdef INCLUDE_MOCFILES | |
339 #include "AudioDial.moc.cpp" | |
340 #endif | |
341 |