annotate widgets/AudioDial.cpp @ 167:53b9c7656798

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