annotate 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
rev   line source
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@0 29 * Lopez-Cabanillas.
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@0 52
Chris@0 53 using std::endl;
Chris@0 54 using std::cerr;
Chris@0 55
Chris@0 56
Chris@0 57 //!!! 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 58
Chris@0 59
Chris@0 60 //-------------------------------------------------------------------------
Chris@0 61 // AudioDial - Instance knob widget class.
Chris@0 62 //
Chris@0 63
Chris@0 64 #define AUDIO_DIAL_MIN (0.25 * M_PI)
Chris@0 65 #define AUDIO_DIAL_MAX (1.75 * M_PI)
Chris@0 66 #define AUDIO_DIAL_RANGE (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN)
Chris@0 67
Chris@0 68
Chris@0 69 // Constructor.
Chris@0 70 AudioDial::AudioDial(QWidget *parent) :
Chris@0 71 QDial(parent),
Chris@34 72 m_knobColor(Qt::black), m_meterColor(Qt::white),
Chris@167 73 m_defaultValue(0),
Chris@168 74 m_mappedValue(0),
Chris@168 75 m_noMappedUpdate(false),
Chris@168 76 m_showTooltip(false),
Chris@167 77 m_rangeMapper(0)
Chris@0 78 {
Chris@0 79 m_mouseDial = false;
Chris@0 80 m_mousePressed = false;
Chris@0 81 }
Chris@0 82
Chris@0 83
Chris@0 84 // Destructor.
Chris@0 85 AudioDial::~AudioDial (void)
Chris@0 86 {
Chris@167 87 delete m_rangeMapper;
Chris@167 88 }
Chris@167 89
Chris@167 90
Chris@167 91 void AudioDial::setRangeMapper(RangeMapper *mapper)
Chris@167 92 {
Chris@168 93 if (!m_rangeMapper) {
Chris@168 94 connect(this, SIGNAL(valueChanged(int)),
Chris@168 95 this, SLOT(updateMappedValue(int)));
Chris@168 96 }
Chris@167 97 delete m_rangeMapper;
Chris@167 98 m_rangeMapper = mapper;
Chris@168 99 m_mappedValue = m_rangeMapper->getValueForPosition(value());
Chris@0 100 }
Chris@0 101
Chris@0 102
Chris@0 103 void AudioDial::paintEvent(QPaintEvent *)
Chris@0 104 {
Chris@0 105 QPainter paint;
Chris@0 106
Chris@0 107 float angle = AUDIO_DIAL_MIN // offset
Chris@0 108 + (AUDIO_DIAL_RANGE *
Chris@0 109 (float(QDial::value() - QDial::minimum()) /
Chris@0 110 (float(QDial::maximum() - QDial::minimum()))));
Chris@0 111 int degrees = int(angle * 180.0 / M_PI);
Chris@0 112
Chris@0 113 int ns = notchSize();
Chris@0 114 int numTicks = 1 + (maximum() + ns - minimum()) / ns;
Chris@0 115
Chris@0 116 QColor knobColor(m_knobColor);
Chris@0 117 if (knobColor == Qt::black)
Chris@82 118 knobColor = palette().background().color();
Chris@0 119
Chris@0 120 QColor meterColor(m_meterColor);
Chris@0 121 if (!isEnabled())
Chris@0 122 meterColor = palette().mid().color();
Chris@0 123 else if (m_meterColor == Qt::white)
Chris@0 124 meterColor = palette().highlight().color();
Chris@0 125
Chris@0 126 int m_size = width() < height() ? width() : height();
Chris@0 127 int scale = 1;
Chris@0 128 int width = m_size - 2*scale, height = m_size - 2*scale;
Chris@0 129
Chris@0 130 paint.begin(this);
Chris@0 131 paint.setRenderHint(QPainter::Antialiasing, true);
Chris@0 132 paint.translate(1, 1);
Chris@0 133
Chris@0 134 QPen pen;
Chris@0 135 QColor c;
Chris@0 136
Chris@0 137 // Knob body and face...
Chris@0 138
Chris@0 139 c = knobColor;
Chris@0 140 pen.setColor(knobColor);
Chris@0 141 pen.setWidth(scale * 2);
Chris@0 142 pen.setCapStyle(Qt::FlatCap);
Chris@0 143
Chris@0 144 paint.setPen(pen);
Chris@0 145 paint.setBrush(c);
Chris@0 146
Chris@0 147 int indent = (int)(width * 0.15 + 1);
Chris@0 148
Chris@0 149 paint.drawEllipse(indent-1, indent-1, width-2*indent, width-2*indent);
Chris@0 150
Chris@0 151 pen.setWidth(3 * scale);
Chris@0 152 int pos = indent-1 + (width-2*indent) / 20;
Chris@0 153 int darkWidth = (width-2*indent) * 3 / 4;
Chris@0 154 while (darkWidth) {
Chris@0 155 c = c.light(102);
Chris@0 156 pen.setColor(c);
Chris@0 157 paint.setPen(pen);
Chris@0 158 paint.drawEllipse(pos, pos, darkWidth, darkWidth);
Chris@0 159 if (!--darkWidth) break;
Chris@0 160 paint.drawEllipse(pos, pos, darkWidth, darkWidth);
Chris@0 161 if (!--darkWidth) break;
Chris@0 162 paint.drawEllipse(pos, pos, darkWidth, darkWidth);
Chris@0 163 ++pos; --darkWidth;
Chris@0 164 }
Chris@0 165
Chris@0 166 // Tick notches...
Chris@0 167
Chris@34 168 if ( notchesVisible() ) {
Chris@0 169 // std::cerr << "Notches visible" << std::endl;
Chris@0 170 pen.setColor(palette().dark().color());
Chris@0 171 pen.setWidth(scale);
Chris@0 172 paint.setPen(pen);
Chris@0 173 for (int i = 0; i < numTicks; ++i) {
Chris@0 174 int div = numTicks;
Chris@0 175 if (div > 1) --div;
Chris@0 176 drawTick(paint, AUDIO_DIAL_MIN + (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) * i / div,
Chris@0 177 width, true);
Chris@0 178 }
Chris@0 179 }
Chris@0 180
Chris@0 181 // The bright metering bit...
Chris@0 182
Chris@0 183 c = meterColor;
Chris@0 184 pen.setColor(c);
Chris@0 185 pen.setWidth(indent);
Chris@0 186 paint.setPen(pen);
Chris@0 187
Chris@0 188 // std::cerr << "degrees " << degrees << ", gives us " << -(degrees - 45) * 16 << std::endl;
Chris@0 189
Chris@0 190 int arcLen = -(degrees - 45) * 16;
Chris@0 191 if (arcLen == 0) arcLen = -16;
Chris@0 192
Chris@0 193 paint.drawArc(indent/2, indent/2,
Chris@0 194 width-indent, width-indent, (180 + 45) * 16, arcLen);
Chris@0 195
Chris@0 196 paint.setBrush(Qt::NoBrush);
Chris@0 197
Chris@0 198 // Shadowing...
Chris@0 199
Chris@0 200 pen.setWidth(scale);
Chris@0 201 paint.setPen(pen);
Chris@0 202
Chris@0 203 // Knob shadow...
Chris@0 204
Chris@0 205 int shadowAngle = -720;
Chris@0 206 c = knobColor.dark();
Chris@0 207 for (int arc = 120; arc < 2880; arc += 240) {
Chris@0 208 pen.setColor(c);
Chris@0 209 paint.setPen(pen);
Chris@0 210 paint.drawArc(indent, indent,
Chris@0 211 width-2*indent, width-2*indent, shadowAngle + arc, 240);
Chris@0 212 paint.drawArc(indent, indent,
Chris@0 213 width-2*indent, width-2*indent, shadowAngle - arc, 240);
Chris@0 214 c = c.light(110);
Chris@0 215 }
Chris@0 216
Chris@0 217 // Scale shadow...
Chris@0 218
Chris@0 219 shadowAngle = 2160;
Chris@0 220 c = palette().dark().color();
Chris@0 221 for (int arc = 120; arc < 2880; arc += 240) {
Chris@0 222 pen.setColor(c);
Chris@0 223 paint.setPen(pen);
Chris@0 224 paint.drawArc(scale/2, scale/2,
Chris@0 225 width-scale, width-scale, shadowAngle + arc, 240);
Chris@0 226 paint.drawArc(scale/2, scale/2,
Chris@0 227 width-scale, width-scale, shadowAngle - arc, 240);
Chris@0 228 c = c.light(108);
Chris@0 229 }
Chris@0 230
Chris@0 231 // Undraw the bottom part...
Chris@0 232
Chris@0 233 pen.setColor(palette().background().color());
Chris@0 234 pen.setWidth(scale * 4);
Chris@0 235 paint.setPen(pen);
Chris@0 236 paint.drawArc(scale/2, scale/2,
Chris@0 237 width-scale, width-scale, -45 * 16, -92 * 16);
Chris@0 238
Chris@0 239 // Scale ends...
Chris@0 240
Chris@0 241 pen.setColor(palette().dark().color());
Chris@0 242 pen.setWidth(scale);
Chris@0 243 paint.setPen(pen);
Chris@0 244 for (int i = 0; i < numTicks; ++i) {
Chris@0 245 if (i != 0 && i != numTicks - 1) continue;
Chris@0 246 int div = numTicks;
Chris@0 247 if (div > 1) --div;
Chris@0 248 drawTick(paint, AUDIO_DIAL_MIN + (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) * i / div,
Chris@0 249 width, false);
Chris@0 250 }
Chris@0 251
Chris@0 252 // Pointer notch...
Chris@0 253
Chris@0 254 float hyp = float(width) / 2.0;
Chris@0 255 float len = hyp - indent;
Chris@0 256 --len;
Chris@0 257
Chris@0 258 float x0 = hyp;
Chris@0 259 float y0 = hyp;
Chris@0 260
Chris@0 261 float x = hyp - len * sin(angle);
Chris@0 262 float y = hyp + len * cos(angle);
Chris@0 263
Chris@0 264 c = palette().dark().color();
Chris@0 265 pen.setColor(isEnabled() ? c.dark(130) : c);
Chris@0 266 pen.setWidth(scale * 2);
Chris@0 267 paint.setPen(pen);
Chris@0 268 paint.drawLine(int(x0), int(y0), int(x), int(y));
Chris@0 269
Chris@0 270 paint.end();
Chris@0 271 }
Chris@0 272
Chris@0 273
Chris@0 274 void AudioDial::drawTick(QPainter &paint,
Chris@0 275 float angle, int size, bool internal)
Chris@0 276 {
Chris@0 277 float hyp = float(size) / 2.0;
Chris@0 278 float x0 = hyp - (hyp - 1) * sin(angle);
Chris@0 279 float y0 = hyp + (hyp - 1) * cos(angle);
Chris@0 280
Chris@0 281 // cerr << "drawTick: angle " << angle << ", size " << size << ", internal " << internal << endl;
Chris@0 282
Chris@0 283 if (internal) {
Chris@0 284
Chris@0 285 float len = hyp / 4;
Chris@0 286 float x1 = hyp - (hyp - len) * sin(angle);
Chris@0 287 float y1 = hyp + (hyp - len) * cos(angle);
Chris@0 288
Chris@0 289 paint.drawLine(int(x0), int(y0), int(x1), int(y1));
Chris@0 290
Chris@0 291 } else {
Chris@0 292
Chris@0 293 float len = hyp / 4;
Chris@0 294 float x1 = hyp - (hyp + len) * sin(angle);
Chris@0 295 float y1 = hyp + (hyp + len) * cos(angle);
Chris@0 296
Chris@0 297 paint.drawLine(int(x0), int(y0), int(x1), int(y1));
Chris@0 298 }
Chris@0 299 }
Chris@0 300
Chris@0 301
Chris@0 302 void AudioDial::setKnobColor(const QColor& color)
Chris@0 303 {
Chris@0 304 m_knobColor = color;
Chris@0 305 update();
Chris@0 306 }
Chris@0 307
Chris@0 308
Chris@0 309 void AudioDial::setMeterColor(const QColor& color)
Chris@0 310 {
Chris@0 311 m_meterColor = color;
Chris@0 312 update();
Chris@0 313 }
Chris@0 314
Chris@0 315
Chris@0 316 void AudioDial::setMouseDial(bool mouseDial)
Chris@0 317 {
Chris@0 318 m_mouseDial = mouseDial;
Chris@0 319 }
Chris@0 320
Chris@0 321
Chris@34 322 void AudioDial::setDefaultValue(int defaultValue)
Chris@34 323 {
Chris@34 324 m_defaultValue = defaultValue;
Chris@34 325 }
Chris@34 326
Chris@34 327
Chris@168 328 void AudioDial::setShowToolTip(bool show)
Chris@168 329 {
Chris@168 330 m_showTooltip = show;
Chris@168 331 m_noMappedUpdate = true;
Chris@168 332 updateMappedValue(value());
Chris@168 333 m_noMappedUpdate = false;
Chris@168 334 }
Chris@168 335
Chris@168 336
Chris@167 337 float AudioDial::mappedValue() const
Chris@167 338 {
Chris@168 339 if (m_rangeMapper) {
Chris@168 340 std::cerr << "AudioDial::mappedValue(): value = " << value() << ", mappedValue = " << m_mappedValue << std::endl;
Chris@168 341 return m_mappedValue;
Chris@168 342 }
Chris@168 343 return value();
Chris@168 344 }
Chris@168 345
Chris@168 346
Chris@168 347 void AudioDial::updateMappedValue(int value)
Chris@168 348 {
Chris@168 349 if (m_rangeMapper) {
Chris@168 350 if (!m_noMappedUpdate) {
Chris@168 351 m_mappedValue = m_rangeMapper->getValueForPosition(value);
Chris@168 352 }
Chris@168 353 }
Chris@168 354
Chris@168 355 if (m_showTooltip) {
Chris@168 356 QString name = objectName();
Chris@168 357 QString unit = "";
Chris@168 358 QString text;
Chris@168 359 if (m_rangeMapper) unit = m_rangeMapper->getUnit();
Chris@168 360 if (name != "") {
Chris@168 361 text = tr("%1: %2%3").arg(name).arg(m_mappedValue).arg(unit);
Chris@168 362 } else {
Chris@168 363 text = tr("%2%3").arg(m_mappedValue).arg(unit);
Chris@168 364 }
Chris@168 365 setToolTip(text);
Chris@168 366 }
Chris@167 367 }
Chris@167 368
Chris@167 369
Chris@0 370 // Alternate mouse behavior event handlers.
Chris@0 371 void AudioDial::mousePressEvent(QMouseEvent *mouseEvent)
Chris@0 372 {
Chris@0 373 if (m_mouseDial) {
Chris@0 374 QDial::mousePressEvent(mouseEvent);
Chris@0 375 } else if (mouseEvent->button() == Qt::LeftButton) {
Chris@0 376 m_mousePressed = true;
Chris@0 377 m_posMouse = mouseEvent->pos();
Chris@34 378 } else if (mouseEvent->button() == Qt::MidButton) {
Chris@34 379 int dv = m_defaultValue;
Chris@34 380 if (dv < minimum()) dv = minimum();
Chris@34 381 if (dv > maximum()) dv = maximum();
Chris@34 382 setValue(m_defaultValue);
Chris@34 383 }
Chris@34 384 }
Chris@34 385
Chris@34 386
Chris@34 387 void AudioDial::mouseDoubleClickEvent(QMouseEvent *mouseEvent)
Chris@34 388 {
Chris@34 389 if (m_mouseDial) {
Chris@34 390 QDial::mouseDoubleClickEvent(mouseEvent);
Chris@34 391 } else if (mouseEvent->button() == Qt::LeftButton) {
Chris@167 392
Chris@34 393 bool ok = false;
Chris@167 394
Chris@167 395 int newPosition = value();
Chris@167 396
Chris@167 397 if (m_rangeMapper) {
Chris@167 398
Chris@167 399 float min = m_rangeMapper->getValueForPosition(minimum());
Chris@167 400 float max = m_rangeMapper->getValueForPosition(maximum());
Chris@167 401
Chris@167 402 QString unit = m_rangeMapper->getUnit();
Chris@167 403
Chris@167 404 QString text;
Chris@167 405 if (objectName() != "") {
Chris@167 406 if (unit != "") {
Chris@167 407 text = tr("New value for %1, from %2 to %3 %4:")
Chris@167 408 .arg(objectName()).arg(min).arg(max).arg(unit);
Chris@167 409 } else {
Chris@167 410 text = tr("New value for %1, from %2 to %3:")
Chris@167 411 .arg(objectName()).arg(min).arg(max);
Chris@167 412 }
Chris@167 413 } else {
Chris@167 414 if (unit != "") {
Chris@167 415 text = tr("Enter a new value from %1 to %2 %3:")
Chris@167 416 .arg(min).arg(max).arg(unit);
Chris@167 417 } else {
Chris@167 418 text = tr("Enter a new value from %1 to %2:")
Chris@167 419 .arg(min).arg(max);
Chris@167 420 }
Chris@167 421 }
Chris@167 422
Chris@167 423 float newValue = QInputDialog::getDouble
Chris@167 424 (this,
Chris@167 425 tr("Enter new value"),
Chris@167 426 text,
Chris@168 427 m_mappedValue,
Chris@167 428 min,
Chris@167 429 max,
Chris@168 430 4,
Chris@167 431 &ok);
Chris@167 432
Chris@168 433 newPosition = m_rangeMapper->getPositionForValue(newValue);
Chris@167 434
Chris@168 435 if (ok) {
Chris@168 436 m_mappedValue = newValue;
Chris@168 437 m_noMappedUpdate = true;
Chris@168 438 if (newPosition != value()) {
Chris@168 439 setValue(newPosition);
Chris@168 440 } else {
Chris@168 441 emit valueChanged(newPosition);
Chris@168 442 }
Chris@168 443 m_noMappedUpdate = false;
Chris@168 444 }
Chris@167 445
Chris@167 446 } else {
Chris@167 447
Chris@167 448 newPosition = QInputDialog::getInteger
Chris@167 449 (this,
Chris@167 450 tr("Enter new value"),
Chris@167 451 tr("Enter a new value from %1 to %2:")
Chris@167 452 .arg(minimum()).arg(maximum()),
Chris@167 453 value(), minimum(), maximum(), pageStep(), &ok);
Chris@168 454
Chris@168 455 if (ok) {
Chris@168 456 setValue(newPosition);
Chris@168 457 }
Chris@167 458 }
Chris@0 459 }
Chris@0 460 }
Chris@0 461
Chris@0 462
Chris@0 463 void AudioDial::mouseMoveEvent(QMouseEvent *mouseEvent)
Chris@0 464 {
Chris@0 465 if (m_mouseDial) {
Chris@0 466 QDial::mouseMoveEvent(mouseEvent);
Chris@0 467 } else if (m_mousePressed) {
Chris@0 468 const QPoint& posMouse = mouseEvent->pos();
Chris@0 469 int v = QDial::value()
Chris@0 470 + (posMouse.x() - m_posMouse.x())
Chris@0 471 + (m_posMouse.y() - posMouse.y());
Chris@0 472 if (v > QDial::maximum())
Chris@0 473 v = QDial::maximum();
Chris@0 474 else
Chris@0 475 if (v < QDial::minimum())
Chris@0 476 v = QDial::minimum();
Chris@0 477 m_posMouse = posMouse;
Chris@0 478 QDial::setValue(v);
Chris@0 479 }
Chris@0 480 }
Chris@0 481
Chris@0 482
Chris@0 483 void AudioDial::mouseReleaseEvent(QMouseEvent *mouseEvent)
Chris@0 484 {
Chris@0 485 if (m_mouseDial) {
Chris@0 486 QDial::mouseReleaseEvent(mouseEvent);
Chris@0 487 } else if (m_mousePressed) {
Chris@0 488 m_mousePressed = false;
Chris@0 489 }
Chris@0 490 }
Chris@0 491
Chris@0 492 #ifdef INCLUDE_MOCFILES
Chris@0 493 #include "AudioDial.moc.cpp"
Chris@0 494 #endif
Chris@0 495