Chris@58
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@59
|
4 Sonic Visualiser
|
Chris@59
|
5 An audio file viewer and annotation editor.
|
Chris@59
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@0
|
7
|
Chris@59
|
8 This program is free software; you can redistribute it and/or
|
Chris@59
|
9 modify it under the terms of the GNU General Public License as
|
Chris@59
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@59
|
11 License, or (at your option) any later version. See the file
|
Chris@59
|
12 COPYING included with this distribution for more information.
|
Chris@0
|
13 */
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * A rotary dial widget.
|
Chris@0
|
17 *
|
Chris@0
|
18 * Based on an original design by Thorsten Wilms.
|
Chris@0
|
19 *
|
Chris@0
|
20 * Implemented as a widget for the Rosegarden MIDI and audio sequencer
|
Chris@0
|
21 * and notation editor by Chris Cannam.
|
Chris@0
|
22 *
|
Chris@0
|
23 * Extracted into a standalone Qt3 widget by Pedro Lopez-Cabanillas
|
Chris@0
|
24 * and adapted for use in QSynth.
|
Chris@0
|
25 *
|
Chris@0
|
26 * Ported to Qt4 by Chris Cannam.
|
Chris@0
|
27 *
|
Chris@168
|
28 * This file copyright 2003-2006 Chris Cannam, copyright 2005 Pedro
|
Chris@182
|
29 * Lopez-Cabanillas, copyright 2006 Queen Mary, University of London.
|
Chris@0
|
30 *
|
Chris@0
|
31 * This program is free software; you can redistribute it and/or
|
Chris@0
|
32 * modify it under the terms of the GNU General Public License as
|
Chris@0
|
33 * published by the Free Software Foundation; either version 2 of the
|
Chris@0
|
34 * License, or (at your option) any later version. See the file
|
Chris@0
|
35 * COPYING included with this distribution for more information.
|
Chris@0
|
36 */
|
Chris@0
|
37
|
Chris@0
|
38 #include "AudioDial.h"
|
Chris@0
|
39
|
Chris@167
|
40 #include "base/RangeMapper.h"
|
Chris@167
|
41
|
Chris@0
|
42 #include <cmath>
|
Chris@0
|
43 #include <iostream>
|
Chris@0
|
44
|
Chris@0
|
45 #include <QTimer>
|
Chris@0
|
46 #include <QPainter>
|
Chris@0
|
47 #include <QPixmap>
|
Chris@0
|
48 #include <QColormap>
|
Chris@0
|
49 #include <QMouseEvent>
|
Chris@0
|
50 #include <QPaintEvent>
|
Chris@34
|
51 #include <QInputDialog>
|
Chris@1584
|
52 #include <QMenu>
|
Chris@0
|
53
|
Chris@382
|
54 #include "base/Profiler.h"
|
Chris@382
|
55
|
Chris@1584
|
56 #include "MenuTitle.h"
|
Chris@1584
|
57
|
Chris@682
|
58
|
Chris@682
|
59
|
Chris@0
|
60
|
Chris@0
|
61
|
Chris@0
|
62 //!!! 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
|
Chris@0
|
63
|
Chris@0
|
64
|
Chris@0
|
65 //-------------------------------------------------------------------------
|
Chris@0
|
66 // AudioDial - Instance knob widget class.
|
Chris@0
|
67 //
|
Chris@0
|
68
|
Chris@0
|
69 #define AUDIO_DIAL_MIN (0.25 * M_PI)
|
Chris@0
|
70 #define AUDIO_DIAL_MAX (1.75 * M_PI)
|
Chris@0
|
71 #define AUDIO_DIAL_RANGE (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN)
|
Chris@0
|
72
|
Chris@0
|
73
|
Chris@189
|
74
|
Chris@0
|
75 // Constructor.
|
Chris@0
|
76 AudioDial::AudioDial(QWidget *parent) :
|
Chris@0
|
77 QDial(parent),
|
Chris@1443
|
78 m_knobColor(Qt::black), // shorthand for "background colour" in paint()
|
Chris@1443
|
79 m_meterColor(Qt::white), // shorthand for "foreground colour" in paint()
|
Chris@167
|
80 m_defaultValue(0),
|
Chris@344
|
81 m_defaultMappedValue(0),
|
Chris@168
|
82 m_mappedValue(0),
|
Chris@168
|
83 m_noMappedUpdate(false),
|
Chris@187
|
84 m_showTooltip(true),
|
Chris@1584
|
85 m_provideContextMenu(true),
|
Chris@1584
|
86 m_lastContextMenu(nullptr),
|
Chris@1408
|
87 m_rangeMapper(nullptr)
|
Chris@0
|
88 {
|
Chris@0
|
89 m_mouseDial = false;
|
Chris@0
|
90 m_mousePressed = false;
|
Chris@1584
|
91 setContextMenuPolicy(Qt::CustomContextMenu);
|
Chris@1584
|
92 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
|
Chris@1584
|
93 this, SLOT(contextMenuRequested(const QPoint &)));
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96
|
Chris@0
|
97 // Destructor.
|
Chris@0
|
98 AudioDial::~AudioDial (void)
|
Chris@0
|
99 {
|
Chris@167
|
100 delete m_rangeMapper;
|
Chris@1584
|
101 delete m_lastContextMenu;
|
Chris@167
|
102 }
|
Chris@167
|
103
|
Chris@1584
|
104 void AudioDial::contextMenuRequested(const QPoint &pos)
|
Chris@1584
|
105 {
|
Chris@1584
|
106 if (!m_provideContextMenu) {
|
Chris@1584
|
107 return;
|
Chris@1584
|
108 }
|
Chris@1584
|
109
|
Chris@1584
|
110 if (m_lastContextMenu) {
|
Chris@1584
|
111 delete m_lastContextMenu;
|
Chris@1584
|
112 }
|
Chris@1584
|
113
|
Chris@1584
|
114 QMenu *m = new QMenu;
|
Chris@1584
|
115
|
Chris@1584
|
116 if (m_title == "") {
|
Chris@1584
|
117 MenuTitle::addTitle(m, tr("Dial"));
|
Chris@1584
|
118 } else {
|
Chris@1584
|
119 MenuTitle::addTitle(m, m_title);
|
Chris@1584
|
120 }
|
Chris@1584
|
121
|
Chris@1584
|
122 m->addAction(tr("&Edit..."),
|
Chris@1584
|
123 [=]() {
|
Chris@1584
|
124 edit();
|
Chris@1584
|
125 });
|
Chris@1584
|
126 m->addAction(tr("&Reset to Default"),
|
Chris@1584
|
127 [=]() {
|
Chris@1584
|
128 setToDefault();
|
Chris@1584
|
129 });
|
Chris@1584
|
130
|
Chris@1584
|
131 m->popup(mapToGlobal(pos));
|
Chris@1584
|
132 m_lastContextMenu = m;
|
Chris@1584
|
133 }
|
Chris@167
|
134
|
Chris@167
|
135 void AudioDial::setRangeMapper(RangeMapper *mapper)
|
Chris@167
|
136 {
|
Chris@187
|
137 if (m_rangeMapper == mapper) return;
|
Chris@187
|
138
|
Chris@170
|
139 if (!m_rangeMapper && mapper) {
|
Chris@168
|
140 connect(this, SIGNAL(valueChanged(int)),
|
Chris@168
|
141 this, SLOT(updateMappedValue(int)));
|
Chris@168
|
142 }
|
Chris@170
|
143
|
Chris@167
|
144 delete m_rangeMapper;
|
Chris@167
|
145 m_rangeMapper = mapper;
|
Chris@170
|
146
|
Chris@187
|
147 updateMappedValue(value());
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150
|
Chris@0
|
151 void AudioDial::paintEvent(QPaintEvent *)
|
Chris@0
|
152 {
|
Chris@382
|
153 Profiler profiler("AudioDial::paintEvent");
|
Chris@382
|
154
|
Chris@0
|
155 QPainter paint;
|
Chris@0
|
156
|
Chris@908
|
157 double angle = AUDIO_DIAL_MIN // offset
|
Chris@1266
|
158 + (AUDIO_DIAL_RANGE *
|
Chris@1266
|
159 (double(QDial::value() - QDial::minimum()) /
|
Chris@1266
|
160 (double(QDial::maximum() - QDial::minimum()))));
|
Chris@0
|
161 int degrees = int(angle * 180.0 / M_PI);
|
Chris@0
|
162
|
Chris@0
|
163 int ns = notchSize();
|
Chris@0
|
164 int numTicks = 1 + (maximum() + ns - minimum()) / ns;
|
Chris@1266
|
165
|
Chris@0
|
166 QColor knobColor(m_knobColor);
|
Chris@1443
|
167 if (knobColor == Qt::black) {
|
Chris@1476
|
168 knobColor = palette().window().color().lighter(150);
|
Chris@1443
|
169 }
|
Chris@1443
|
170 bool knobIsDark =
|
Chris@1443
|
171 (knobColor.red() + knobColor.green() + knobColor.blue() <= 384);
|
Chris@0
|
172
|
Chris@0
|
173 QColor meterColor(m_meterColor);
|
Chris@1443
|
174 if (!isEnabled()) {
|
Chris@1266
|
175 meterColor = palette().mid().color();
|
Chris@1443
|
176 } else if (m_meterColor == Qt::white) {
|
Chris@1443
|
177 if (knobIsDark) {
|
Chris@1545
|
178 meterColor = palette().text().color();
|
Chris@1443
|
179 } else {
|
Chris@1443
|
180 meterColor = palette().highlight().color();
|
Chris@1443
|
181 }
|
Chris@1443
|
182 }
|
Chris@0
|
183
|
Chris@1443
|
184 QColor notchColor(palette().dark().color());
|
Chris@1443
|
185 if (knobIsDark) {
|
Chris@1545
|
186 notchColor = palette().text().color();
|
Chris@1443
|
187 }
|
Chris@1443
|
188
|
Chris@0
|
189 int m_size = width() < height() ? width() : height();
|
Chris@0
|
190 int scale = 1;
|
Chris@249
|
191 int width = m_size - 2*scale;
|
Chris@0
|
192
|
Chris@0
|
193 paint.begin(this);
|
Chris@0
|
194 paint.setRenderHint(QPainter::Antialiasing, true);
|
Chris@0
|
195 paint.translate(1, 1);
|
Chris@0
|
196
|
Chris@0
|
197 QPen pen;
|
Chris@0
|
198 QColor c;
|
Chris@0
|
199
|
Chris@0
|
200 // Knob body and face...
|
Chris@0
|
201
|
Chris@0
|
202 c = knobColor;
|
Chris@0
|
203 pen.setColor(knobColor);
|
Chris@0
|
204 pen.setWidth(scale * 2);
|
Chris@0
|
205 pen.setCapStyle(Qt::FlatCap);
|
Chris@1266
|
206
|
Chris@0
|
207 paint.setPen(pen);
|
Chris@0
|
208 paint.setBrush(c);
|
Chris@0
|
209
|
Chris@0
|
210 int indent = (int)(width * 0.15 + 1);
|
Chris@0
|
211
|
Chris@0
|
212 paint.drawEllipse(indent-1, indent-1, width-2*indent, width-2*indent);
|
Chris@0
|
213
|
Chris@0
|
214 pen.setWidth(3 * scale);
|
Chris@0
|
215 int pos = indent-1 + (width-2*indent) / 20;
|
Chris@0
|
216 int darkWidth = (width-2*indent) * 3 / 4;
|
Chris@0
|
217 while (darkWidth) {
|
Chris@1443
|
218 if (knobIsDark) {
|
Chris@1476
|
219 c = c.darker(102);
|
Chris@1443
|
220 } else {
|
Chris@1476
|
221 c = c.lighter(102);
|
Chris@1443
|
222 }
|
Chris@1266
|
223 pen.setColor(c);
|
Chris@1266
|
224 paint.setPen(pen);
|
Chris@1266
|
225 paint.drawEllipse(pos, pos, darkWidth, darkWidth);
|
Chris@1266
|
226 if (!--darkWidth) break;
|
Chris@1266
|
227 paint.drawEllipse(pos, pos, darkWidth, darkWidth);
|
Chris@1266
|
228 if (!--darkWidth) break;
|
Chris@1266
|
229 paint.drawEllipse(pos, pos, darkWidth, darkWidth);
|
Chris@1266
|
230 ++pos; --darkWidth;
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 // Tick notches...
|
Chris@0
|
234
|
Chris@34
|
235 if ( notchesVisible() ) {
|
Chris@1443
|
236 pen.setColor(notchColor);
|
Chris@1266
|
237 pen.setWidth(scale);
|
Chris@1266
|
238 paint.setPen(pen);
|
Chris@1266
|
239 for (int i = 0; i < numTicks; ++i) {
|
Chris@1266
|
240 int div = numTicks;
|
Chris@1266
|
241 if (div > 1) --div;
|
Chris@1266
|
242 drawTick(paint, AUDIO_DIAL_MIN + (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) * i / div,
|
Chris@1266
|
243 width, true);
|
Chris@1266
|
244 }
|
Chris@0
|
245 }
|
Chris@0
|
246
|
Chris@0
|
247 // The bright metering bit...
|
Chris@0
|
248
|
Chris@0
|
249 c = meterColor;
|
Chris@0
|
250 pen.setColor(c);
|
Chris@0
|
251 pen.setWidth(indent);
|
Chris@0
|
252 paint.setPen(pen);
|
Chris@0
|
253
|
Chris@682
|
254 // cerr << "degrees " << degrees << ", gives us " << -(degrees - 45) * 16 << endl;
|
Chris@0
|
255
|
Chris@0
|
256 int arcLen = -(degrees - 45) * 16;
|
Chris@0
|
257 if (arcLen == 0) arcLen = -16;
|
Chris@0
|
258
|
Chris@0
|
259 paint.drawArc(indent/2, indent/2,
|
Chris@1266
|
260 width-indent, width-indent, (180 + 45) * 16, arcLen);
|
Chris@0
|
261
|
Chris@0
|
262 paint.setBrush(Qt::NoBrush);
|
Chris@0
|
263
|
Chris@0
|
264 // Shadowing...
|
Chris@0
|
265
|
Chris@0
|
266 pen.setWidth(scale);
|
Chris@0
|
267 paint.setPen(pen);
|
Chris@0
|
268
|
Chris@0
|
269 // Knob shadow...
|
Chris@0
|
270
|
Chris@0
|
271 int shadowAngle = -720;
|
Chris@1443
|
272 if (knobIsDark) {
|
Chris@1476
|
273 c = knobColor.lighter();
|
Chris@1443
|
274 } else {
|
Chris@1476
|
275 c = knobColor.darker();
|
Chris@1443
|
276 }
|
Chris@0
|
277 for (int arc = 120; arc < 2880; arc += 240) {
|
Chris@1266
|
278 pen.setColor(c);
|
Chris@1266
|
279 paint.setPen(pen);
|
Chris@1266
|
280 paint.drawArc(indent, indent,
|
Chris@1266
|
281 width-2*indent, width-2*indent, shadowAngle + arc, 240);
|
Chris@1266
|
282 paint.drawArc(indent, indent,
|
Chris@1266
|
283 width-2*indent, width-2*indent, shadowAngle - arc, 240);
|
Chris@1443
|
284 if (knobIsDark) {
|
Chris@1476
|
285 c = c.darker(110);
|
Chris@1443
|
286 } else {
|
Chris@1476
|
287 c = c.lighter(110);
|
Chris@1443
|
288 }
|
Chris@0
|
289 }
|
Chris@0
|
290
|
Chris@738
|
291 // Scale shadow, omitting the bottom part...
|
Chris@0
|
292
|
Chris@0
|
293 shadowAngle = 2160;
|
Chris@738
|
294 c = palette().shadow().color();
|
Chris@738
|
295 for (int i = 0; i < 5; ++i) {
|
Chris@1266
|
296 pen.setColor(c);
|
Chris@1266
|
297 paint.setPen(pen);
|
Chris@738
|
298 int arc = i * 240 + 120;
|
Chris@1266
|
299 paint.drawArc(scale/2, scale/2,
|
Chris@1266
|
300 width-scale, width-scale, shadowAngle + arc, 240);
|
Chris@1476
|
301 c = c.lighter(110);
|
Chris@738
|
302 }
|
Chris@738
|
303 c = palette().shadow().color();
|
Chris@738
|
304 for (int i = 0; i < 12; ++i) {
|
Chris@1266
|
305 pen.setColor(c);
|
Chris@1266
|
306 paint.setPen(pen);
|
Chris@738
|
307 int arc = i * 240 + 120;
|
Chris@1266
|
308 paint.drawArc(scale/2, scale/2,
|
Chris@1266
|
309 width-scale, width-scale, shadowAngle - arc, 240);
|
Chris@1476
|
310 c = c.lighter(110);
|
Chris@0
|
311 }
|
Chris@0
|
312
|
Chris@0
|
313 // Scale ends...
|
Chris@0
|
314
|
Chris@1443
|
315 if (knobIsDark) {
|
Chris@1443
|
316 pen.setColor(palette().mid().color());
|
Chris@1443
|
317 } else {
|
Chris@1443
|
318 pen.setColor(palette().shadow().color());
|
Chris@1443
|
319 }
|
Chris@0
|
320 pen.setWidth(scale);
|
Chris@0
|
321 paint.setPen(pen);
|
Chris@0
|
322 for (int i = 0; i < numTicks; ++i) {
|
Chris@1266
|
323 if (i != 0 && i != numTicks - 1) continue;
|
Chris@1266
|
324 int div = numTicks;
|
Chris@1266
|
325 if (div > 1) --div;
|
Chris@1266
|
326 drawTick(paint, AUDIO_DIAL_MIN + (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) * i / div,
|
Chris@1266
|
327 width, false);
|
Chris@0
|
328 }
|
Chris@0
|
329
|
Chris@0
|
330 // Pointer notch...
|
Chris@0
|
331
|
Chris@908
|
332 double hyp = double(width) / 2.0;
|
Chris@908
|
333 double len = hyp - indent;
|
Chris@0
|
334 --len;
|
Chris@0
|
335
|
Chris@908
|
336 double x0 = hyp;
|
Chris@908
|
337 double y0 = hyp;
|
Chris@0
|
338
|
Chris@908
|
339 double x = hyp - len * sin(angle);
|
Chris@908
|
340 double y = hyp + len * cos(angle);
|
Chris@0
|
341
|
Chris@1443
|
342 c = notchColor;
|
Chris@1443
|
343 if (isEnabled()) {
|
Chris@1443
|
344 if (knobIsDark) {
|
Chris@1476
|
345 c = c.lighter(130);
|
Chris@1443
|
346 } else {
|
Chris@1476
|
347 c = c.darker(130);
|
Chris@1443
|
348 }
|
Chris@1443
|
349 }
|
Chris@1443
|
350 pen.setColor(c);
|
Chris@0
|
351 pen.setWidth(scale * 2);
|
Chris@0
|
352 paint.setPen(pen);
|
Chris@0
|
353 paint.drawLine(int(x0), int(y0), int(x), int(y));
|
Chris@0
|
354
|
Chris@0
|
355 paint.end();
|
Chris@0
|
356 }
|
Chris@0
|
357
|
Chris@0
|
358
|
Chris@0
|
359 void AudioDial::drawTick(QPainter &paint,
|
Chris@1266
|
360 double angle, int size, bool internal)
|
Chris@0
|
361 {
|
Chris@908
|
362 double hyp = double(size) / 2.0;
|
Chris@908
|
363 double x0 = hyp - (hyp - 1) * sin(angle);
|
Chris@908
|
364 double y0 = hyp + (hyp - 1) * cos(angle);
|
Chris@0
|
365
|
Chris@0
|
366 // cerr << "drawTick: angle " << angle << ", size " << size << ", internal " << internal << endl;
|
Chris@0
|
367
|
Chris@0
|
368 if (internal) {
|
Chris@0
|
369
|
Chris@1266
|
370 double len = hyp / 4;
|
Chris@1266
|
371 double x1 = hyp - (hyp - len) * sin(angle);
|
Chris@1266
|
372 double y1 = hyp + (hyp - len) * cos(angle);
|
Chris@1266
|
373
|
Chris@1266
|
374 paint.drawLine(int(x0), int(y0), int(x1), int(y1));
|
Chris@0
|
375
|
Chris@0
|
376 } else {
|
Chris@0
|
377
|
Chris@1266
|
378 double len = hyp / 4;
|
Chris@1266
|
379 double x1 = hyp - (hyp + len) * sin(angle);
|
Chris@1266
|
380 double y1 = hyp + (hyp + len) * cos(angle);
|
Chris@0
|
381
|
Chris@1266
|
382 paint.drawLine(int(x0), int(y0), int(x1), int(y1));
|
Chris@0
|
383 }
|
Chris@0
|
384 }
|
Chris@0
|
385
|
Chris@0
|
386
|
Chris@0
|
387 void AudioDial::setKnobColor(const QColor& color)
|
Chris@0
|
388 {
|
Chris@0
|
389 m_knobColor = color;
|
Chris@0
|
390 update();
|
Chris@0
|
391 }
|
Chris@0
|
392
|
Chris@0
|
393
|
Chris@0
|
394 void AudioDial::setMeterColor(const QColor& color)
|
Chris@0
|
395 {
|
Chris@0
|
396 m_meterColor = color;
|
Chris@0
|
397 update();
|
Chris@0
|
398 }
|
Chris@0
|
399
|
Chris@0
|
400
|
Chris@0
|
401 void AudioDial::setMouseDial(bool mouseDial)
|
Chris@0
|
402 {
|
Chris@0
|
403 m_mouseDial = mouseDial;
|
Chris@0
|
404 }
|
Chris@0
|
405
|
Chris@0
|
406
|
Chris@34
|
407 void AudioDial::setDefaultValue(int defaultValue)
|
Chris@34
|
408 {
|
Chris@34
|
409 m_defaultValue = defaultValue;
|
Chris@344
|
410 if (m_rangeMapper) {
|
Chris@344
|
411 m_defaultMappedValue = m_rangeMapper->getValueForPosition(defaultValue);
|
Chris@344
|
412 }
|
Chris@34
|
413 }
|
Chris@34
|
414
|
Chris@219
|
415 void AudioDial::setValue(int value)
|
Chris@219
|
416 {
|
Chris@219
|
417 QDial::setValue(value);
|
Chris@219
|
418 updateMappedValue(value);
|
Chris@219
|
419 }
|
Chris@219
|
420
|
Chris@908
|
421 void AudioDial::setDefaultMappedValue(double value)
|
Chris@344
|
422 {
|
Chris@344
|
423 m_defaultMappedValue = value;
|
Chris@344
|
424 if (m_rangeMapper) {
|
Chris@344
|
425 m_defaultValue = m_rangeMapper->getPositionForValue(value);
|
Chris@344
|
426 }
|
Chris@344
|
427 }
|
Chris@219
|
428
|
Chris@908
|
429 void AudioDial::setMappedValue(double mappedValue)
|
Chris@177
|
430 {
|
Chris@177
|
431 if (m_rangeMapper) {
|
Chris@177
|
432 int newPosition = m_rangeMapper->getPositionForValue(mappedValue);
|
Chris@190
|
433 bool changed = (m_mappedValue != mappedValue);
|
Chris@177
|
434 m_mappedValue = mappedValue;
|
Chris@177
|
435 m_noMappedUpdate = true;
|
Chris@587
|
436 SVDEBUG << "AudioDial::setMappedValue(" << mappedValue << "): new position is " << newPosition << endl;
|
Chris@177
|
437 if (newPosition != value()) {
|
Chris@177
|
438 setValue(newPosition);
|
Chris@190
|
439 } else if (changed) {
|
Chris@177
|
440 emit valueChanged(newPosition);
|
Chris@177
|
441 }
|
Chris@177
|
442 m_noMappedUpdate = false;
|
Chris@177
|
443 } else {
|
Chris@187
|
444 setValue(int(mappedValue));
|
Chris@177
|
445 }
|
Chris@177
|
446 }
|
Chris@177
|
447
|
Chris@177
|
448
|
Chris@168
|
449 void AudioDial::setShowToolTip(bool show)
|
Chris@168
|
450 {
|
Chris@168
|
451 m_showTooltip = show;
|
Chris@168
|
452 m_noMappedUpdate = true;
|
Chris@168
|
453 updateMappedValue(value());
|
Chris@168
|
454 m_noMappedUpdate = false;
|
Chris@168
|
455 }
|
Chris@168
|
456
|
Chris@168
|
457
|
Chris@1584
|
458 void AudioDial::setProvideContextMenu(bool provide)
|
Chris@1584
|
459 {
|
Chris@1584
|
460 m_provideContextMenu = provide;
|
Chris@1584
|
461 }
|
Chris@1584
|
462
|
Chris@1584
|
463
|
Chris@908
|
464 double AudioDial::mappedValue() const
|
Chris@167
|
465 {
|
Chris@168
|
466 if (m_rangeMapper) {
|
Chris@587
|
467 // SVDEBUG << "AudioDial::mappedValue(): value = " << value() << ", mappedValue = " << m_mappedValue << endl;
|
Chris@168
|
468 return m_mappedValue;
|
Chris@168
|
469 }
|
Chris@168
|
470 return value();
|
Chris@168
|
471 }
|
Chris@168
|
472
|
Chris@168
|
473
|
Chris@168
|
474 void AudioDial::updateMappedValue(int value)
|
Chris@168
|
475 {
|
Chris@170
|
476 if (!m_noMappedUpdate) {
|
Chris@170
|
477 if (m_rangeMapper) {
|
Chris@168
|
478 m_mappedValue = m_rangeMapper->getValueForPosition(value);
|
Chris@170
|
479 } else {
|
Chris@170
|
480 m_mappedValue = value;
|
Chris@168
|
481 }
|
Chris@168
|
482 }
|
Chris@168
|
483
|
Chris@1584
|
484 QString name = objectName();
|
Chris@1584
|
485 QString label;
|
Chris@1584
|
486 if (m_rangeMapper) {
|
Chris@1584
|
487 label = m_rangeMapper->getLabel(value);
|
Chris@1584
|
488 }
|
Chris@1584
|
489 QString text;
|
Chris@1584
|
490 if (label != "") {
|
Chris@1584
|
491 if (name != "") {
|
Chris@1584
|
492 text = tr("%1: %2").arg(name).arg(label);
|
Chris@1584
|
493 } else {
|
Chris@1584
|
494 text = label;
|
Chris@1584
|
495 }
|
Chris@1584
|
496 } else {
|
Chris@1584
|
497 QString unit = "";
|
Chris@1584
|
498 if (m_rangeMapper) {
|
Chris@1584
|
499 unit = m_rangeMapper->getUnit();
|
Chris@1584
|
500 }
|
Chris@1584
|
501 if (name != "") {
|
Chris@1584
|
502 text = tr("%1: %2%3").arg(name).arg(m_mappedValue).arg(unit);
|
Chris@1584
|
503 } else {
|
Chris@1584
|
504 text = tr("%2%3").arg(m_mappedValue).arg(unit);
|
Chris@1584
|
505 }
|
Chris@1584
|
506 }
|
Chris@1584
|
507
|
Chris@1584
|
508 m_title = text;
|
Chris@1584
|
509
|
Chris@168
|
510 if (m_showTooltip) {
|
Chris@168
|
511 setToolTip(text);
|
Chris@1584
|
512 } else {
|
Chris@1584
|
513 setToolTip("");
|
Chris@168
|
514 }
|
Chris@167
|
515 }
|
Chris@167
|
516
|
Chris@344
|
517 void
|
Chris@344
|
518 AudioDial::setToDefault()
|
Chris@344
|
519 {
|
Chris@344
|
520 if (m_rangeMapper) {
|
Chris@344
|
521 setMappedValue(m_defaultMappedValue);
|
Chris@344
|
522 return;
|
Chris@344
|
523 }
|
Chris@344
|
524 int dv = m_defaultValue;
|
Chris@344
|
525 if (dv < minimum()) dv = minimum();
|
Chris@344
|
526 if (dv > maximum()) dv = maximum();
|
Chris@344
|
527 setValue(m_defaultValue);
|
Chris@344
|
528 }
|
Chris@167
|
529
|
Chris@0
|
530 // Alternate mouse behavior event handlers.
|
Chris@0
|
531 void AudioDial::mousePressEvent(QMouseEvent *mouseEvent)
|
Chris@0
|
532 {
|
Chris@0
|
533 if (m_mouseDial) {
|
Chris@1266
|
534 QDial::mousePressEvent(mouseEvent);
|
Chris@187
|
535 } else if (mouseEvent->button() == Qt::MidButton ||
|
Chris@187
|
536 ((mouseEvent->button() == Qt::LeftButton) &&
|
Chris@187
|
537 (mouseEvent->modifiers() & Qt::ControlModifier))) {
|
Chris@344
|
538 setToDefault();
|
Chris@187
|
539 } else if (mouseEvent->button() == Qt::LeftButton) {
|
Chris@1266
|
540 m_mousePressed = true;
|
Chris@1266
|
541 m_posMouse = mouseEvent->pos();
|
Chris@34
|
542 }
|
Chris@34
|
543 }
|
Chris@34
|
544
|
Chris@34
|
545
|
Chris@34
|
546 void AudioDial::mouseDoubleClickEvent(QMouseEvent *mouseEvent)
|
Chris@34
|
547 {
|
Chris@187
|
548 //!!! needs a common base class with Thumbwheel
|
Chris@187
|
549
|
Chris@34
|
550 if (m_mouseDial) {
|
Chris@1266
|
551 QDial::mouseDoubleClickEvent(mouseEvent);
|
Chris@187
|
552 } else if (mouseEvent->button() != Qt::LeftButton) {
|
Chris@187
|
553 return;
|
Chris@187
|
554 }
|
Chris@167
|
555
|
Chris@1584
|
556 edit();
|
Chris@1584
|
557 }
|
Chris@1584
|
558
|
Chris@1584
|
559 void AudioDial::edit()
|
Chris@1584
|
560 {
|
Chris@187
|
561 bool ok = false;
|
Chris@167
|
562
|
Chris@187
|
563 if (m_rangeMapper) {
|
Chris@187
|
564
|
Chris@908
|
565 double min = m_rangeMapper->getValueForPosition(minimum());
|
Chris@908
|
566 double max = m_rangeMapper->getValueForPosition(maximum());
|
Chris@187
|
567
|
Chris@187
|
568 if (min > max) {
|
Chris@908
|
569 double tmp = min;
|
Chris@187
|
570 min = max;
|
Chris@187
|
571 max = tmp;
|
Chris@187
|
572 }
|
Chris@167
|
573
|
Chris@187
|
574 QString unit = m_rangeMapper->getUnit();
|
Chris@187
|
575
|
Chris@187
|
576 QString text;
|
Chris@187
|
577 if (objectName() != "") {
|
Chris@187
|
578 if (unit != "") {
|
Chris@187
|
579 text = tr("New value for %1, from %2 to %3 %4:")
|
Chris@187
|
580 .arg(objectName()).arg(min).arg(max).arg(unit);
|
Chris@167
|
581 } else {
|
Chris@187
|
582 text = tr("New value for %1, from %2 to %3:")
|
Chris@187
|
583 .arg(objectName()).arg(min).arg(max);
|
Chris@167
|
584 }
|
Chris@187
|
585 } else {
|
Chris@187
|
586 if (unit != "") {
|
Chris@187
|
587 text = tr("Enter a new value from %1 to %2 %3:")
|
Chris@187
|
588 .arg(min).arg(max).arg(unit);
|
Chris@187
|
589 } else {
|
Chris@187
|
590 text = tr("Enter a new value from %1 to %2:")
|
Chris@187
|
591 .arg(min).arg(max);
|
Chris@168
|
592 }
|
Chris@187
|
593 }
|
Chris@187
|
594
|
Chris@908
|
595 double newValue = QInputDialog::getDouble
|
Chris@187
|
596 (this,
|
Chris@187
|
597 tr("Enter new value"),
|
Chris@187
|
598 text,
|
Chris@187
|
599 m_mappedValue,
|
Chris@187
|
600 min,
|
Chris@187
|
601 max,
|
Chris@187
|
602 4,
|
Chris@187
|
603 &ok);
|
Chris@187
|
604
|
Chris@187
|
605 if (ok) {
|
Chris@187
|
606 setMappedValue(newValue);
|
Chris@187
|
607 }
|
Chris@187
|
608
|
Chris@187
|
609 } else {
|
Chris@187
|
610
|
Chris@616
|
611 int newPosition = QInputDialog::getInt
|
Chris@187
|
612 (this,
|
Chris@187
|
613 tr("Enter new value"),
|
Chris@187
|
614 tr("Enter a new value from %1 to %2:")
|
Chris@187
|
615 .arg(minimum()).arg(maximum()),
|
Chris@395
|
616 value(), minimum(), maximum(), singleStep(), &ok);
|
Chris@187
|
617
|
Chris@187
|
618 if (ok) {
|
Chris@187
|
619 setValue(newPosition);
|
Chris@167
|
620 }
|
Chris@0
|
621 }
|
Chris@0
|
622 }
|
Chris@0
|
623
|
Chris@0
|
624
|
Chris@0
|
625 void AudioDial::mouseMoveEvent(QMouseEvent *mouseEvent)
|
Chris@0
|
626 {
|
Chris@0
|
627 if (m_mouseDial) {
|
Chris@1266
|
628 QDial::mouseMoveEvent(mouseEvent);
|
Chris@0
|
629 } else if (m_mousePressed) {
|
Chris@1266
|
630 const QPoint& posMouse = mouseEvent->pos();
|
Chris@1266
|
631 int v = QDial::value()
|
Chris@1266
|
632 + (posMouse.x() - m_posMouse.x())
|
Chris@1266
|
633 + (m_posMouse.y() - posMouse.y());
|
Chris@1266
|
634 if (v > QDial::maximum())
|
Chris@1266
|
635 v = QDial::maximum();
|
Chris@1266
|
636 else
|
Chris@1266
|
637 if (v < QDial::minimum())
|
Chris@1266
|
638 v = QDial::minimum();
|
Chris@1266
|
639 m_posMouse = posMouse;
|
Chris@1266
|
640 QDial::setValue(v);
|
Chris@0
|
641 }
|
Chris@0
|
642 }
|
Chris@0
|
643
|
Chris@0
|
644
|
Chris@0
|
645 void AudioDial::mouseReleaseEvent(QMouseEvent *mouseEvent)
|
Chris@0
|
646 {
|
Chris@0
|
647 if (m_mouseDial) {
|
Chris@1266
|
648 QDial::mouseReleaseEvent(mouseEvent);
|
Chris@0
|
649 } else if (m_mousePressed) {
|
Chris@1266
|
650 m_mousePressed = false;
|
Chris@0
|
651 }
|
Chris@0
|
652 }
|
Chris@0
|
653
|
Chris@189
|
654 void
|
Chris@189
|
655 AudioDial::enterEvent(QEvent *e)
|
Chris@189
|
656 {
|
Chris@189
|
657 QDial::enterEvent(e);
|
Chris@189
|
658 emit mouseEntered();
|
Chris@189
|
659 }
|
Chris@189
|
660
|
Chris@189
|
661 void
|
Chris@189
|
662 AudioDial::leaveEvent(QEvent *e)
|
Chris@189
|
663 {
|
Chris@189
|
664 QDial::enterEvent(e);
|
Chris@189
|
665 emit mouseLeft();
|
Chris@189
|
666 }
|
Chris@189
|
667
|