comparison widgets/AudioDial.cpp @ 168:d4be66d61c04

* Make audio dial remember its more accurate mapped value as well as its display position * Add tool tip support to audio dial to show values
author Chris Cannam
date Tue, 17 Oct 2006 13:04:49 +0000
parents 53b9c7656798
children bdba24db0446
comparison
equal deleted inserted replaced
167:53b9c7656798 168:d4be66d61c04
23 * Extracted into a standalone Qt3 widget by Pedro Lopez-Cabanillas 23 * Extracted into a standalone Qt3 widget by Pedro Lopez-Cabanillas
24 * and adapted for use in QSynth. 24 * and adapted for use in QSynth.
25 * 25 *
26 * Ported to Qt4 by Chris Cannam. 26 * Ported to Qt4 by Chris Cannam.
27 * 27 *
28 * This file copyright 2003-2005 Chris Cannam, copyright 2005 Pedro 28 * This file copyright 2003-2006 Chris Cannam, copyright 2005 Pedro
29 * Lopez-Cabanillas. 29 * Lopez-Cabanillas.
30 * 30 *
31 * This program is free software; you can redistribute it and/or 31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License as 32 * modify it under the terms of the GNU General Public License as
33 * published by the Free Software Foundation; either version 2 of the 33 * published by the Free Software Foundation; either version 2 of the
69 // Constructor. 69 // Constructor.
70 AudioDial::AudioDial(QWidget *parent) : 70 AudioDial::AudioDial(QWidget *parent) :
71 QDial(parent), 71 QDial(parent),
72 m_knobColor(Qt::black), m_meterColor(Qt::white), 72 m_knobColor(Qt::black), m_meterColor(Qt::white),
73 m_defaultValue(0), 73 m_defaultValue(0),
74 m_mappedValue(0),
75 m_noMappedUpdate(false),
76 m_showTooltip(false),
74 m_rangeMapper(0) 77 m_rangeMapper(0)
75 { 78 {
76 m_mouseDial = false; 79 m_mouseDial = false;
77 m_mousePressed = false; 80 m_mousePressed = false;
78 } 81 }
85 } 88 }
86 89
87 90
88 void AudioDial::setRangeMapper(RangeMapper *mapper) 91 void AudioDial::setRangeMapper(RangeMapper *mapper)
89 { 92 {
93 if (!m_rangeMapper) {
94 connect(this, SIGNAL(valueChanged(int)),
95 this, SLOT(updateMappedValue(int)));
96 }
90 delete m_rangeMapper; 97 delete m_rangeMapper;
91 m_rangeMapper = mapper; 98 m_rangeMapper = mapper;
99 m_mappedValue = m_rangeMapper->getValueForPosition(value());
92 } 100 }
93 101
94 102
95 void AudioDial::paintEvent(QPaintEvent *) 103 void AudioDial::paintEvent(QPaintEvent *)
96 { 104 {
315 { 323 {
316 m_defaultValue = defaultValue; 324 m_defaultValue = defaultValue;
317 } 325 }
318 326
319 327
328 void AudioDial::setShowToolTip(bool show)
329 {
330 m_showTooltip = show;
331 m_noMappedUpdate = true;
332 updateMappedValue(value());
333 m_noMappedUpdate = false;
334 }
335
336
320 float AudioDial::mappedValue() const 337 float AudioDial::mappedValue() const
321 { 338 {
322 if (m_rangeMapper) return m_rangeMapper->getValueForPosition(value()); 339 if (m_rangeMapper) {
323 else return value(); 340 std::cerr << "AudioDial::mappedValue(): value = " << value() << ", mappedValue = " << m_mappedValue << std::endl;
341 return m_mappedValue;
342 }
343 return value();
344 }
345
346
347 void AudioDial::updateMappedValue(int value)
348 {
349 if (m_rangeMapper) {
350 if (!m_noMappedUpdate) {
351 m_mappedValue = m_rangeMapper->getValueForPosition(value);
352 }
353 }
354
355 if (m_showTooltip) {
356 QString name = objectName();
357 QString unit = "";
358 QString text;
359 if (m_rangeMapper) unit = m_rangeMapper->getUnit();
360 if (name != "") {
361 text = tr("%1: %2%3").arg(name).arg(m_mappedValue).arg(unit);
362 } else {
363 text = tr("%2%3").arg(m_mappedValue).arg(unit);
364 }
365 setToolTip(text);
366 }
324 } 367 }
325 368
326 369
327 // Alternate mouse behavior event handlers. 370 // Alternate mouse behavior event handlers.
328 void AudioDial::mousePressEvent(QMouseEvent *mouseEvent) 371 void AudioDial::mousePressEvent(QMouseEvent *mouseEvent)
379 422
380 float newValue = QInputDialog::getDouble 423 float newValue = QInputDialog::getDouble
381 (this, 424 (this,
382 tr("Enter new value"), 425 tr("Enter new value"),
383 text, 426 text,
384 m_rangeMapper->getValueForPosition(value()), 427 m_mappedValue,
385 min, 428 min,
386 max, 429 max,
387 5, 430 4,
388 &ok); 431 &ok);
389 432
390 //!!! need to avoid this rounding by storing the float value
391
392 newPosition = m_rangeMapper->getPositionForValue(newValue); 433 newPosition = m_rangeMapper->getPositionForValue(newValue);
434
435 if (ok) {
436 m_mappedValue = newValue;
437 m_noMappedUpdate = true;
438 if (newPosition != value()) {
439 setValue(newPosition);
440 } else {
441 emit valueChanged(newPosition);
442 }
443 m_noMappedUpdate = false;
444 }
393 445
394 } else { 446 } else {
395 447
396 newPosition = QInputDialog::getInteger 448 newPosition = QInputDialog::getInteger
397 (this, 449 (this,
398 tr("Enter new value"), 450 tr("Enter new value"),
399 tr("Enter a new value from %1 to %2:") 451 tr("Enter a new value from %1 to %2:")
400 .arg(minimum()).arg(maximum()), 452 .arg(minimum()).arg(maximum()),
401 value(), minimum(), maximum(), pageStep(), &ok); 453 value(), minimum(), maximum(), pageStep(), &ok);
454
455 if (ok) {
456 setValue(newPosition);
457 }
402 } 458 }
403
404 if (ok) {
405 setValue(newPosition);
406 }
407
408
409 /*!!!
410 int newValue = QInputDialog::getInteger
411 (this,
412 tr("Enter new value"),
413 tr("Select a new value in the range %1 to %2:")
414 .arg(minimum()).arg(maximum()),
415 value(), minimum(), maximum(), pageStep(), &ok);
416 if (ok) {
417 setValue(newValue);
418 }
419 */
420 } 459 }
421 } 460 }
422 461
423 462
424 void AudioDial::mouseMoveEvent(QMouseEvent *mouseEvent) 463 void AudioDial::mouseMoveEvent(QMouseEvent *mouseEvent)