annotate widgets/AudioDial.cpp @ 1595:8b78b3c330fb

Use our base column type
author Chris Cannam
date Thu, 09 Apr 2020 11:23:40 +0100
parents 27ea5d61b402
children a798a7b5e215
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@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@1589 109
Chris@1589 110 delete m_lastContextMenu;
Chris@1589 111 m_lastContextMenu = new QMenu;
Chris@1589 112 auto m = m_lastContextMenu;
Chris@1584 113
Chris@1584 114 if (m_title == "") {
Chris@1584 115 MenuTitle::addTitle(m, tr("Dial"));
Chris@1584 116 } else {
Chris@1584 117 MenuTitle::addTitle(m, m_title);
Chris@1584 118 }
Chris@1584 119
Chris@1584 120 m->addAction(tr("&Edit..."),
Chris@1584 121 [=]() {
Chris@1584 122 edit();
Chris@1584 123 });
Chris@1584 124 m->addAction(tr("&Reset to Default"),
Chris@1584 125 [=]() {
Chris@1584 126 setToDefault();
Chris@1584 127 });
Chris@1584 128
Chris@1584 129 m->popup(mapToGlobal(pos));
Chris@1584 130 }
Chris@167 131
Chris@167 132 void AudioDial::setRangeMapper(RangeMapper *mapper)
Chris@167 133 {
Chris@187 134 if (m_rangeMapper == mapper) return;
Chris@187 135
Chris@170 136 if (!m_rangeMapper && mapper) {
Chris@168 137 connect(this, SIGNAL(valueChanged(int)),
Chris@168 138 this, SLOT(updateMappedValue(int)));
Chris@168 139 }
Chris@170 140
Chris@167 141 delete m_rangeMapper;
Chris@167 142 m_rangeMapper = mapper;
Chris@170 143
Chris@187 144 updateMappedValue(value());
Chris@0 145 }
Chris@0 146
Chris@0 147
Chris@0 148 void AudioDial::paintEvent(QPaintEvent *)
Chris@0 149 {
Chris@382 150 Profiler profiler("AudioDial::paintEvent");
Chris@382 151
Chris@0 152 QPainter paint;
Chris@0 153
Chris@908 154 double angle = AUDIO_DIAL_MIN // offset
Chris@1266 155 + (AUDIO_DIAL_RANGE *
Chris@1266 156 (double(QDial::value() - QDial::minimum()) /
Chris@1266 157 (double(QDial::maximum() - QDial::minimum()))));
Chris@0 158 int degrees = int(angle * 180.0 / M_PI);
Chris@0 159
Chris@0 160 int ns = notchSize();
Chris@0 161 int numTicks = 1 + (maximum() + ns - minimum()) / ns;
Chris@1266 162
Chris@0 163 QColor knobColor(m_knobColor);
Chris@1443 164 if (knobColor == Qt::black) {
Chris@1476 165 knobColor = palette().window().color().lighter(150);
Chris@1443 166 }
Chris@1443 167 bool knobIsDark =
Chris@1443 168 (knobColor.red() + knobColor.green() + knobColor.blue() <= 384);
Chris@0 169
Chris@0 170 QColor meterColor(m_meterColor);
Chris@1443 171 if (!isEnabled()) {
Chris@1266 172 meterColor = palette().mid().color();
Chris@1443 173 } else if (m_meterColor == Qt::white) {
Chris@1443 174 if (knobIsDark) {
Chris@1545 175 meterColor = palette().text().color();
Chris@1443 176 } else {
Chris@1443 177 meterColor = palette().highlight().color();
Chris@1443 178 }
Chris@1443 179 }
Chris@0 180
Chris@1443 181 QColor notchColor(palette().dark().color());
Chris@1443 182 if (knobIsDark) {
Chris@1545 183 notchColor = palette().text().color();
Chris@1443 184 }
Chris@1443 185
Chris@0 186 int m_size = width() < height() ? width() : height();
Chris@0 187 int scale = 1;
Chris@249 188 int width = m_size - 2*scale;
Chris@0 189
Chris@0 190 paint.begin(this);
Chris@0 191 paint.setRenderHint(QPainter::Antialiasing, true);
Chris@0 192 paint.translate(1, 1);
Chris@0 193
Chris@0 194 QPen pen;
Chris@0 195 QColor c;
Chris@0 196
Chris@0 197 // Knob body and face...
Chris@0 198
Chris@0 199 c = knobColor;
Chris@0 200 pen.setColor(knobColor);
Chris@0 201 pen.setWidth(scale * 2);
Chris@0 202 pen.setCapStyle(Qt::FlatCap);
Chris@1266 203
Chris@0 204 paint.setPen(pen);
Chris@0 205 paint.setBrush(c);
Chris@0 206
Chris@0 207 int indent = (int)(width * 0.15 + 1);
Chris@0 208
Chris@0 209 paint.drawEllipse(indent-1, indent-1, width-2*indent, width-2*indent);
Chris@0 210
Chris@0 211 pen.setWidth(3 * scale);
Chris@0 212 int pos = indent-1 + (width-2*indent) / 20;
Chris@0 213 int darkWidth = (width-2*indent) * 3 / 4;
Chris@0 214 while (darkWidth) {
Chris@1443 215 if (knobIsDark) {
Chris@1476 216 c = c.darker(102);
Chris@1443 217 } else {
Chris@1476 218 c = c.lighter(102);
Chris@1443 219 }
Chris@1266 220 pen.setColor(c);
Chris@1266 221 paint.setPen(pen);
Chris@1266 222 paint.drawEllipse(pos, pos, darkWidth, darkWidth);
Chris@1266 223 if (!--darkWidth) break;
Chris@1266 224 paint.drawEllipse(pos, pos, darkWidth, darkWidth);
Chris@1266 225 if (!--darkWidth) break;
Chris@1266 226 paint.drawEllipse(pos, pos, darkWidth, darkWidth);
Chris@1266 227 ++pos; --darkWidth;
Chris@0 228 }
Chris@0 229
Chris@0 230 // Tick notches...
Chris@0 231
Chris@34 232 if ( notchesVisible() ) {
Chris@1443 233 pen.setColor(notchColor);
Chris@1266 234 pen.setWidth(scale);
Chris@1266 235 paint.setPen(pen);
Chris@1266 236 for (int i = 0; i < numTicks; ++i) {
Chris@1266 237 int div = numTicks;
Chris@1266 238 if (div > 1) --div;
Chris@1266 239 drawTick(paint, AUDIO_DIAL_MIN + (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) * i / div,
Chris@1266 240 width, true);
Chris@1266 241 }
Chris@0 242 }
Chris@0 243
Chris@0 244 // The bright metering bit...
Chris@0 245
Chris@0 246 c = meterColor;
Chris@0 247 pen.setColor(c);
Chris@0 248 pen.setWidth(indent);
Chris@0 249 paint.setPen(pen);
Chris@0 250
Chris@682 251 // cerr << "degrees " << degrees << ", gives us " << -(degrees - 45) * 16 << endl;
Chris@0 252
Chris@0 253 int arcLen = -(degrees - 45) * 16;
Chris@0 254 if (arcLen == 0) arcLen = -16;
Chris@0 255
Chris@0 256 paint.drawArc(indent/2, indent/2,
Chris@1266 257 width-indent, width-indent, (180 + 45) * 16, arcLen);
Chris@0 258
Chris@0 259 paint.setBrush(Qt::NoBrush);
Chris@0 260
Chris@0 261 // Shadowing...
Chris@0 262
Chris@0 263 pen.setWidth(scale);
Chris@0 264 paint.setPen(pen);
Chris@0 265
Chris@0 266 // Knob shadow...
Chris@0 267
Chris@0 268 int shadowAngle = -720;
Chris@1443 269 if (knobIsDark) {
Chris@1476 270 c = knobColor.lighter();
Chris@1443 271 } else {
Chris@1476 272 c = knobColor.darker();
Chris@1443 273 }
Chris@0 274 for (int arc = 120; arc < 2880; arc += 240) {
Chris@1266 275 pen.setColor(c);
Chris@1266 276 paint.setPen(pen);
Chris@1266 277 paint.drawArc(indent, indent,
Chris@1266 278 width-2*indent, width-2*indent, shadowAngle + arc, 240);
Chris@1266 279 paint.drawArc(indent, indent,
Chris@1266 280 width-2*indent, width-2*indent, shadowAngle - arc, 240);
Chris@1443 281 if (knobIsDark) {
Chris@1476 282 c = c.darker(110);
Chris@1443 283 } else {
Chris@1476 284 c = c.lighter(110);
Chris@1443 285 }
Chris@0 286 }
Chris@0 287
Chris@738 288 // Scale shadow, omitting the bottom part...
Chris@0 289
Chris@0 290 shadowAngle = 2160;
Chris@738 291 c = palette().shadow().color();
Chris@738 292 for (int i = 0; i < 5; ++i) {
Chris@1266 293 pen.setColor(c);
Chris@1266 294 paint.setPen(pen);
Chris@738 295 int arc = i * 240 + 120;
Chris@1266 296 paint.drawArc(scale/2, scale/2,
Chris@1266 297 width-scale, width-scale, shadowAngle + arc, 240);
Chris@1476 298 c = c.lighter(110);
Chris@738 299 }
Chris@738 300 c = palette().shadow().color();
Chris@738 301 for (int i = 0; i < 12; ++i) {
Chris@1266 302 pen.setColor(c);
Chris@1266 303 paint.setPen(pen);
Chris@738 304 int arc = i * 240 + 120;
Chris@1266 305 paint.drawArc(scale/2, scale/2,
Chris@1266 306 width-scale, width-scale, shadowAngle - arc, 240);
Chris@1476 307 c = c.lighter(110);
Chris@0 308 }
Chris@0 309
Chris@0 310 // Scale ends...
Chris@0 311
Chris@1443 312 if (knobIsDark) {
Chris@1443 313 pen.setColor(palette().mid().color());
Chris@1443 314 } else {
Chris@1443 315 pen.setColor(palette().shadow().color());
Chris@1443 316 }
Chris@0 317 pen.setWidth(scale);
Chris@0 318 paint.setPen(pen);
Chris@0 319 for (int i = 0; i < numTicks; ++i) {
Chris@1266 320 if (i != 0 && i != numTicks - 1) continue;
Chris@1266 321 int div = numTicks;
Chris@1266 322 if (div > 1) --div;
Chris@1266 323 drawTick(paint, AUDIO_DIAL_MIN + (AUDIO_DIAL_MAX - AUDIO_DIAL_MIN) * i / div,
Chris@1266 324 width, false);
Chris@0 325 }
Chris@0 326
Chris@0 327 // Pointer notch...
Chris@0 328
Chris@908 329 double hyp = double(width) / 2.0;
Chris@908 330 double len = hyp - indent;
Chris@0 331 --len;
Chris@0 332
Chris@908 333 double x0 = hyp;
Chris@908 334 double y0 = hyp;
Chris@0 335
Chris@908 336 double x = hyp - len * sin(angle);
Chris@908 337 double y = hyp + len * cos(angle);
Chris@0 338
Chris@1443 339 c = notchColor;
Chris@1443 340 if (isEnabled()) {
Chris@1443 341 if (knobIsDark) {
Chris@1476 342 c = c.lighter(130);
Chris@1443 343 } else {
Chris@1476 344 c = c.darker(130);
Chris@1443 345 }
Chris@1443 346 }
Chris@1443 347 pen.setColor(c);
Chris@0 348 pen.setWidth(scale * 2);
Chris@0 349 paint.setPen(pen);
Chris@0 350 paint.drawLine(int(x0), int(y0), int(x), int(y));
Chris@0 351
Chris@0 352 paint.end();
Chris@0 353 }
Chris@0 354
Chris@0 355
Chris@0 356 void AudioDial::drawTick(QPainter &paint,
Chris@1266 357 double angle, int size, bool internal)
Chris@0 358 {
Chris@908 359 double hyp = double(size) / 2.0;
Chris@908 360 double x0 = hyp - (hyp - 1) * sin(angle);
Chris@908 361 double y0 = hyp + (hyp - 1) * cos(angle);
Chris@0 362
Chris@0 363 // cerr << "drawTick: angle " << angle << ", size " << size << ", internal " << internal << endl;
Chris@0 364
Chris@0 365 if (internal) {
Chris@0 366
Chris@1266 367 double len = hyp / 4;
Chris@1266 368 double x1 = hyp - (hyp - len) * sin(angle);
Chris@1266 369 double y1 = hyp + (hyp - len) * cos(angle);
Chris@1266 370
Chris@1266 371 paint.drawLine(int(x0), int(y0), int(x1), int(y1));
Chris@0 372
Chris@0 373 } else {
Chris@0 374
Chris@1266 375 double len = hyp / 4;
Chris@1266 376 double x1 = hyp - (hyp + len) * sin(angle);
Chris@1266 377 double y1 = hyp + (hyp + len) * cos(angle);
Chris@0 378
Chris@1266 379 paint.drawLine(int(x0), int(y0), int(x1), int(y1));
Chris@0 380 }
Chris@0 381 }
Chris@0 382
Chris@0 383
Chris@0 384 void AudioDial::setKnobColor(const QColor& color)
Chris@0 385 {
Chris@0 386 m_knobColor = color;
Chris@0 387 update();
Chris@0 388 }
Chris@0 389
Chris@0 390
Chris@0 391 void AudioDial::setMeterColor(const QColor& color)
Chris@0 392 {
Chris@0 393 m_meterColor = color;
Chris@0 394 update();
Chris@0 395 }
Chris@0 396
Chris@0 397
Chris@0 398 void AudioDial::setMouseDial(bool mouseDial)
Chris@0 399 {
Chris@0 400 m_mouseDial = mouseDial;
Chris@0 401 }
Chris@0 402
Chris@0 403
Chris@34 404 void AudioDial::setDefaultValue(int defaultValue)
Chris@34 405 {
Chris@34 406 m_defaultValue = defaultValue;
Chris@344 407 if (m_rangeMapper) {
Chris@344 408 m_defaultMappedValue = m_rangeMapper->getValueForPosition(defaultValue);
Chris@344 409 }
Chris@34 410 }
Chris@34 411
Chris@219 412 void AudioDial::setValue(int value)
Chris@219 413 {
Chris@219 414 QDial::setValue(value);
Chris@219 415 updateMappedValue(value);
Chris@219 416 }
Chris@219 417
Chris@908 418 void AudioDial::setDefaultMappedValue(double value)
Chris@344 419 {
Chris@344 420 m_defaultMappedValue = value;
Chris@344 421 if (m_rangeMapper) {
Chris@344 422 m_defaultValue = m_rangeMapper->getPositionForValue(value);
Chris@344 423 }
Chris@344 424 }
Chris@219 425
Chris@908 426 void AudioDial::setMappedValue(double mappedValue)
Chris@177 427 {
Chris@177 428 if (m_rangeMapper) {
Chris@177 429 int newPosition = m_rangeMapper->getPositionForValue(mappedValue);
Chris@190 430 bool changed = (m_mappedValue != mappedValue);
Chris@177 431 m_mappedValue = mappedValue;
Chris@177 432 m_noMappedUpdate = true;
Chris@587 433 SVDEBUG << "AudioDial::setMappedValue(" << mappedValue << "): new position is " << newPosition << endl;
Chris@177 434 if (newPosition != value()) {
Chris@177 435 setValue(newPosition);
Chris@190 436 } else if (changed) {
Chris@177 437 emit valueChanged(newPosition);
Chris@177 438 }
Chris@177 439 m_noMappedUpdate = false;
Chris@177 440 } else {
Chris@187 441 setValue(int(mappedValue));
Chris@177 442 }
Chris@177 443 }
Chris@177 444
Chris@177 445
Chris@168 446 void AudioDial::setShowToolTip(bool show)
Chris@168 447 {
Chris@168 448 m_showTooltip = show;
Chris@168 449 m_noMappedUpdate = true;
Chris@168 450 updateMappedValue(value());
Chris@168 451 m_noMappedUpdate = false;
Chris@168 452 }
Chris@168 453
Chris@168 454
Chris@1584 455 void AudioDial::setProvideContextMenu(bool provide)
Chris@1584 456 {
Chris@1584 457 m_provideContextMenu = provide;
Chris@1584 458 }
Chris@1584 459
Chris@1584 460
Chris@908 461 double AudioDial::mappedValue() const
Chris@167 462 {
Chris@168 463 if (m_rangeMapper) {
Chris@587 464 // SVDEBUG << "AudioDial::mappedValue(): value = " << value() << ", mappedValue = " << m_mappedValue << endl;
Chris@168 465 return m_mappedValue;
Chris@168 466 }
Chris@168 467 return value();
Chris@168 468 }
Chris@168 469
Chris@168 470
Chris@168 471 void AudioDial::updateMappedValue(int value)
Chris@168 472 {
Chris@170 473 if (!m_noMappedUpdate) {
Chris@170 474 if (m_rangeMapper) {
Chris@168 475 m_mappedValue = m_rangeMapper->getValueForPosition(value);
Chris@170 476 } else {
Chris@170 477 m_mappedValue = value;
Chris@168 478 }
Chris@168 479 }
Chris@168 480
Chris@1584 481 QString name = objectName();
Chris@1584 482 QString label;
Chris@1584 483 if (m_rangeMapper) {
Chris@1584 484 label = m_rangeMapper->getLabel(value);
Chris@1584 485 }
Chris@1584 486 QString text;
Chris@1584 487 if (label != "") {
Chris@1584 488 if (name != "") {
Chris@1584 489 text = tr("%1: %2").arg(name).arg(label);
Chris@1584 490 } else {
Chris@1584 491 text = label;
Chris@1584 492 }
Chris@1584 493 } else {
Chris@1584 494 QString unit = "";
Chris@1584 495 if (m_rangeMapper) {
Chris@1584 496 unit = m_rangeMapper->getUnit();
Chris@1584 497 }
Chris@1584 498 if (name != "") {
Chris@1584 499 text = tr("%1: %2%3").arg(name).arg(m_mappedValue).arg(unit);
Chris@1584 500 } else {
Chris@1584 501 text = tr("%2%3").arg(m_mappedValue).arg(unit);
Chris@1584 502 }
Chris@1584 503 }
Chris@1584 504
Chris@1584 505 m_title = text;
Chris@1584 506
Chris@168 507 if (m_showTooltip) {
Chris@168 508 setToolTip(text);
Chris@1584 509 } else {
Chris@1584 510 setToolTip("");
Chris@168 511 }
Chris@167 512 }
Chris@167 513
Chris@344 514 void
Chris@344 515 AudioDial::setToDefault()
Chris@344 516 {
Chris@344 517 if (m_rangeMapper) {
Chris@344 518 setMappedValue(m_defaultMappedValue);
Chris@344 519 return;
Chris@344 520 }
Chris@344 521 int dv = m_defaultValue;
Chris@344 522 if (dv < minimum()) dv = minimum();
Chris@344 523 if (dv > maximum()) dv = maximum();
Chris@344 524 setValue(m_defaultValue);
Chris@344 525 }
Chris@167 526
Chris@0 527 // Alternate mouse behavior event handlers.
Chris@0 528 void AudioDial::mousePressEvent(QMouseEvent *mouseEvent)
Chris@0 529 {
Chris@0 530 if (m_mouseDial) {
Chris@1266 531 QDial::mousePressEvent(mouseEvent);
Chris@187 532 } else if (mouseEvent->button() == Qt::MidButton ||
Chris@187 533 ((mouseEvent->button() == Qt::LeftButton) &&
Chris@187 534 (mouseEvent->modifiers() & Qt::ControlModifier))) {
Chris@344 535 setToDefault();
Chris@187 536 } else if (mouseEvent->button() == Qt::LeftButton) {
Chris@1266 537 m_mousePressed = true;
Chris@1266 538 m_posMouse = mouseEvent->pos();
Chris@34 539 }
Chris@34 540 }
Chris@34 541
Chris@34 542
Chris@34 543 void AudioDial::mouseDoubleClickEvent(QMouseEvent *mouseEvent)
Chris@34 544 {
Chris@187 545 //!!! needs a common base class with Thumbwheel
Chris@187 546
Chris@34 547 if (m_mouseDial) {
Chris@1266 548 QDial::mouseDoubleClickEvent(mouseEvent);
Chris@187 549 } else if (mouseEvent->button() != Qt::LeftButton) {
Chris@187 550 return;
Chris@187 551 }
Chris@167 552
Chris@1584 553 edit();
Chris@1584 554 }
Chris@1584 555
Chris@1584 556 void AudioDial::edit()
Chris@1584 557 {
Chris@187 558 bool ok = false;
Chris@167 559
Chris@187 560 if (m_rangeMapper) {
Chris@187 561
Chris@908 562 double min = m_rangeMapper->getValueForPosition(minimum());
Chris@908 563 double max = m_rangeMapper->getValueForPosition(maximum());
Chris@187 564
Chris@187 565 if (min > max) {
Chris@908 566 double tmp = min;
Chris@187 567 min = max;
Chris@187 568 max = tmp;
Chris@187 569 }
Chris@167 570
Chris@187 571 QString unit = m_rangeMapper->getUnit();
Chris@187 572
Chris@187 573 QString text;
Chris@187 574 if (objectName() != "") {
Chris@187 575 if (unit != "") {
Chris@187 576 text = tr("New value for %1, from %2 to %3 %4:")
Chris@187 577 .arg(objectName()).arg(min).arg(max).arg(unit);
Chris@167 578 } else {
Chris@187 579 text = tr("New value for %1, from %2 to %3:")
Chris@187 580 .arg(objectName()).arg(min).arg(max);
Chris@167 581 }
Chris@187 582 } else {
Chris@187 583 if (unit != "") {
Chris@187 584 text = tr("Enter a new value from %1 to %2 %3:")
Chris@187 585 .arg(min).arg(max).arg(unit);
Chris@187 586 } else {
Chris@187 587 text = tr("Enter a new value from %1 to %2:")
Chris@187 588 .arg(min).arg(max);
Chris@168 589 }
Chris@187 590 }
Chris@187 591
Chris@908 592 double newValue = QInputDialog::getDouble
Chris@187 593 (this,
Chris@187 594 tr("Enter new value"),
Chris@187 595 text,
Chris@187 596 m_mappedValue,
Chris@187 597 min,
Chris@187 598 max,
Chris@187 599 4,
Chris@187 600 &ok);
Chris@187 601
Chris@187 602 if (ok) {
Chris@187 603 setMappedValue(newValue);
Chris@187 604 }
Chris@187 605
Chris@187 606 } else {
Chris@187 607
Chris@616 608 int newPosition = QInputDialog::getInt
Chris@187 609 (this,
Chris@187 610 tr("Enter new value"),
Chris@187 611 tr("Enter a new value from %1 to %2:")
Chris@187 612 .arg(minimum()).arg(maximum()),
Chris@395 613 value(), minimum(), maximum(), singleStep(), &ok);
Chris@187 614
Chris@187 615 if (ok) {
Chris@187 616 setValue(newPosition);
Chris@167 617 }
Chris@0 618 }
Chris@0 619 }
Chris@0 620
Chris@0 621
Chris@0 622 void AudioDial::mouseMoveEvent(QMouseEvent *mouseEvent)
Chris@0 623 {
Chris@0 624 if (m_mouseDial) {
Chris@1266 625 QDial::mouseMoveEvent(mouseEvent);
Chris@0 626 } else if (m_mousePressed) {
Chris@1266 627 const QPoint& posMouse = mouseEvent->pos();
Chris@1266 628 int v = QDial::value()
Chris@1266 629 + (posMouse.x() - m_posMouse.x())
Chris@1266 630 + (m_posMouse.y() - posMouse.y());
Chris@1266 631 if (v > QDial::maximum())
Chris@1266 632 v = QDial::maximum();
Chris@1266 633 else
Chris@1266 634 if (v < QDial::minimum())
Chris@1266 635 v = QDial::minimum();
Chris@1266 636 m_posMouse = posMouse;
Chris@1266 637 QDial::setValue(v);
Chris@0 638 }
Chris@0 639 }
Chris@0 640
Chris@0 641
Chris@0 642 void AudioDial::mouseReleaseEvent(QMouseEvent *mouseEvent)
Chris@0 643 {
Chris@0 644 if (m_mouseDial) {
Chris@1266 645 QDial::mouseReleaseEvent(mouseEvent);
Chris@0 646 } else if (m_mousePressed) {
Chris@1266 647 m_mousePressed = false;
Chris@0 648 }
Chris@0 649 }
Chris@0 650
Chris@189 651 void
Chris@189 652 AudioDial::enterEvent(QEvent *e)
Chris@189 653 {
Chris@189 654 QDial::enterEvent(e);
Chris@189 655 emit mouseEntered();
Chris@189 656 }
Chris@189 657
Chris@189 658 void
Chris@189 659 AudioDial::leaveEvent(QEvent *e)
Chris@189 660 {
Chris@189 661 QDial::enterEvent(e);
Chris@189 662 emit mouseLeft();
Chris@189 663 }
Chris@189 664