annotate widgets/Thumbwheel.cpp @ 249:e6d0b097d102 sv1-1.0rc1

* more compile warning fixes
author Chris Cannam
date Mon, 30 Apr 2007 14:06:45 +0000
parents 6969f21da18a
children c492902dba40
rev   line source
Chris@132 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@132 2
Chris@132 3 /*
Chris@132 4 Sonic Visualiser
Chris@132 5 An audio file viewer and annotation editor.
Chris@132 6 Centre for Digital Music, Queen Mary, University of London.
Chris@182 7 This file copyright 2006 QMUL.
Chris@132 8
Chris@132 9 This program is free software; you can redistribute it and/or
Chris@132 10 modify it under the terms of the GNU General Public License as
Chris@132 11 published by the Free Software Foundation; either version 2 of the
Chris@132 12 License, or (at your option) any later version. See the file
Chris@132 13 COPYING included with this distribution for more information.
Chris@132 14 */
Chris@132 15
Chris@132 16 #include "Thumbwheel.h"
Chris@132 17
Chris@187 18 #include "base/RangeMapper.h"
Chris@190 19 #include "base/Profiler.h"
Chris@187 20
Chris@132 21 #include <QMouseEvent>
Chris@132 22 #include <QPaintEvent>
Chris@132 23 #include <QWheelEvent>
Chris@187 24 #include <QInputDialog>
Chris@132 25 #include <QPainter>
Chris@190 26 #include <QPainterPath>
Chris@132 27
Chris@132 28 #include <cmath>
Chris@132 29 #include <iostream>
Chris@132 30
Chris@133 31 Thumbwheel::Thumbwheel(Qt::Orientation orientation,
Chris@132 32 QWidget *parent) :
Chris@132 33 QWidget(parent),
Chris@133 34 m_min(0),
Chris@133 35 m_max(100),
Chris@133 36 m_default(50),
Chris@133 37 m_value(50),
Chris@187 38 m_mappedValue(50),
Chris@187 39 m_noMappedUpdate(false),
Chris@165 40 m_rotation(0.5),
Chris@132 41 m_orientation(orientation),
Chris@165 42 m_speed(1.0),
Chris@132 43 m_tracking(true),
Chris@132 44 m_showScale(true),
Chris@132 45 m_clicked(false),
Chris@133 46 m_atDefault(true),
Chris@187 47 m_clickRotation(m_rotation),
Chris@187 48 m_showTooltip(true),
Chris@187 49 m_rangeMapper(0)
Chris@132 50 {
Chris@132 51 }
Chris@132 52
Chris@132 53 Thumbwheel::~Thumbwheel()
Chris@132 54 {
Chris@187 55 delete m_rangeMapper;
Chris@187 56 }
Chris@187 57
Chris@187 58 void
Chris@187 59 Thumbwheel::setRangeMapper(RangeMapper *mapper)
Chris@187 60 {
Chris@187 61 if (m_rangeMapper == mapper) return;
Chris@187 62
Chris@187 63 if (!m_rangeMapper && mapper) {
Chris@187 64 connect(this, SIGNAL(valueChanged(int)),
Chris@187 65 this, SLOT(updateMappedValue(int)));
Chris@187 66 }
Chris@187 67
Chris@187 68 delete m_rangeMapper;
Chris@187 69 m_rangeMapper = mapper;
Chris@187 70
Chris@187 71 updateMappedValue(getValue());
Chris@187 72 }
Chris@187 73
Chris@187 74 void
Chris@187 75 Thumbwheel::setShowToolTip(bool show)
Chris@187 76 {
Chris@187 77 m_showTooltip = show;
Chris@187 78 m_noMappedUpdate = true;
Chris@187 79 updateMappedValue(getValue());
Chris@187 80 m_noMappedUpdate = false;
Chris@132 81 }
Chris@132 82
Chris@132 83 void
Chris@133 84 Thumbwheel::setMinimumValue(int min)
Chris@133 85 {
Chris@133 86 if (m_min == min) return;
Chris@133 87
Chris@133 88 m_min = min;
Chris@133 89 if (m_max <= m_min) m_max = m_min + 1;
Chris@133 90 if (m_value < m_min) m_value = m_min;
Chris@133 91 if (m_value > m_max) m_value = m_max;
Chris@165 92
Chris@165 93 m_rotation = float(m_value - m_min) / float(m_max - m_min);
Chris@165 94 update();
Chris@133 95 }
Chris@133 96
Chris@133 97 int
Chris@133 98 Thumbwheel::getMinimumValue() const
Chris@133 99 {
Chris@133 100 return m_min;
Chris@133 101 }
Chris@133 102
Chris@133 103 void
Chris@133 104 Thumbwheel::setMaximumValue(int max)
Chris@133 105 {
Chris@133 106 if (m_max == max) return;
Chris@133 107
Chris@133 108 m_max = max;
Chris@133 109 if (m_min >= m_max) m_min = m_max - 1;
Chris@133 110 if (m_value < m_min) m_value = m_min;
Chris@133 111 if (m_value > m_max) m_value = m_max;
Chris@165 112
Chris@165 113 m_rotation = float(m_value - m_min) / float(m_max - m_min);
Chris@165 114 update();
Chris@133 115 }
Chris@133 116
Chris@133 117 int
Chris@133 118 Thumbwheel::getMaximumValue() const
Chris@133 119 {
Chris@133 120 return m_max;
Chris@133 121 }
Chris@133 122
Chris@133 123 void
Chris@133 124 Thumbwheel::setDefaultValue(int deft)
Chris@133 125 {
Chris@133 126 if (m_default == deft) return;
Chris@133 127
Chris@133 128 m_default = deft;
Chris@133 129 if (m_atDefault) {
Chris@133 130 setValue(m_default);
Chris@165 131 m_atDefault = true; // setValue unsets this
Chris@133 132 emit valueChanged(getValue());
Chris@133 133 }
Chris@133 134 }
Chris@133 135
Chris@187 136 void
Chris@187 137 Thumbwheel::setMappedValue(float mappedValue)
Chris@187 138 {
Chris@187 139 if (m_rangeMapper) {
Chris@187 140 int newValue = m_rangeMapper->getPositionForValue(mappedValue);
Chris@190 141 bool changed = (m_mappedValue != mappedValue);
Chris@187 142 m_mappedValue = mappedValue;
Chris@187 143 m_noMappedUpdate = true;
Chris@205 144 // std::cerr << "Thumbwheel::setMappedValue(" << mappedValue << "): new value is " << newValue << " (visible " << isVisible() << ")" << std::endl;
Chris@187 145 if (newValue != getValue()) {
Chris@187 146 setValue(newValue);
Chris@190 147 changed = true;
Chris@187 148 }
Chris@190 149 if (changed) emit valueChanged(newValue);
Chris@187 150 m_noMappedUpdate = false;
Chris@187 151 } else {
Chris@190 152 int v = int(mappedValue);
Chris@190 153 if (v != getValue()) {
Chris@190 154 setValue(v);
Chris@190 155 emit valueChanged(v);
Chris@190 156 }
Chris@187 157 }
Chris@187 158 }
Chris@187 159
Chris@133 160 int
Chris@133 161 Thumbwheel::getDefaultValue() const
Chris@133 162 {
Chris@133 163 return m_default;
Chris@133 164 }
Chris@133 165
Chris@133 166 void
Chris@132 167 Thumbwheel::setValue(int value)
Chris@132 168 {
Chris@205 169 // std::cerr << "Thumbwheel::setValue(" << value << ") (from " << m_value
Chris@205 170 // << ", rotation " << m_rotation << ")" << " (visible " << isVisible() << ")" << std::endl;
Chris@133 171
Chris@165 172 if (m_value != value) {
Chris@165 173
Chris@165 174 m_atDefault = false;
Chris@165 175
Chris@165 176 if (value < m_min) value = m_min;
Chris@165 177 if (value > m_max) value = m_max;
Chris@165 178 m_value = value;
Chris@165 179 }
Chris@165 180
Chris@165 181 m_rotation = float(m_value - m_min) / float(m_max - m_min);
Chris@192 182 if (isVisible()) update();
Chris@132 183 }
Chris@132 184
Chris@133 185 void
Chris@133 186 Thumbwheel::resetToDefault()
Chris@133 187 {
Chris@133 188 if (m_default == m_value) return;
Chris@133 189 setValue(m_default);
Chris@133 190 m_atDefault = true;
Chris@133 191 emit valueChanged(getValue());
Chris@133 192 }
Chris@133 193
Chris@132 194 int
Chris@132 195 Thumbwheel::getValue() const
Chris@132 196 {
Chris@132 197 return m_value;
Chris@132 198 }
Chris@132 199
Chris@187 200 float
Chris@187 201 Thumbwheel::getMappedValue() const
Chris@187 202 {
Chris@187 203 if (m_rangeMapper) {
Chris@205 204 // std::cerr << "Thumbwheel::getMappedValue(): value = " << getValue() << ", mappedValue = " << m_mappedValue << std::endl;
Chris@187 205 return m_mappedValue;
Chris@187 206 }
Chris@187 207 return getValue();
Chris@187 208 }
Chris@187 209
Chris@187 210 void
Chris@187 211 Thumbwheel::updateMappedValue(int value)
Chris@187 212 {
Chris@187 213 if (!m_noMappedUpdate) {
Chris@187 214 if (m_rangeMapper) {
Chris@187 215 m_mappedValue = m_rangeMapper->getValueForPosition(value);
Chris@187 216 } else {
Chris@187 217 m_mappedValue = value;
Chris@187 218 }
Chris@187 219 }
Chris@187 220
Chris@187 221 if (m_showTooltip) {
Chris@187 222 QString name = objectName();
Chris@187 223 QString unit = "";
Chris@187 224 QString text;
Chris@187 225 if (m_rangeMapper) unit = m_rangeMapper->getUnit();
Chris@187 226 if (name != "") {
Chris@187 227 text = tr("%1: %2%3").arg(name).arg(m_mappedValue).arg(unit);
Chris@187 228 } else {
Chris@187 229 text = tr("%2%3").arg(m_mappedValue).arg(unit);
Chris@187 230 }
Chris@187 231 setToolTip(text);
Chris@187 232 }
Chris@187 233 }
Chris@187 234
Chris@132 235 void
Chris@132 236 Thumbwheel::setSpeed(float speed)
Chris@132 237 {
Chris@132 238 m_speed = speed;
Chris@132 239 }
Chris@132 240
Chris@132 241 float
Chris@132 242 Thumbwheel::getSpeed() const
Chris@132 243 {
Chris@132 244 return m_speed;
Chris@132 245 }
Chris@132 246
Chris@132 247 void
Chris@132 248 Thumbwheel::setTracking(bool tracking)
Chris@132 249 {
Chris@132 250 m_tracking = tracking;
Chris@132 251 }
Chris@132 252
Chris@132 253 bool
Chris@132 254 Thumbwheel::getTracking() const
Chris@132 255 {
Chris@132 256 return m_tracking;
Chris@132 257 }
Chris@132 258
Chris@132 259 void
Chris@132 260 Thumbwheel::setShowScale(bool showScale)
Chris@132 261 {
Chris@132 262 m_showScale = showScale;
Chris@132 263 }
Chris@132 264
Chris@132 265 bool
Chris@132 266 Thumbwheel::getShowScale() const
Chris@132 267 {
Chris@132 268 return m_showScale;
Chris@132 269 }
Chris@132 270
Chris@132 271 void
Chris@189 272 Thumbwheel::enterEvent(QEvent *)
Chris@189 273 {
Chris@189 274 emit mouseEntered();
Chris@189 275 }
Chris@189 276
Chris@189 277 void
Chris@189 278 Thumbwheel::leaveEvent(QEvent *)
Chris@189 279 {
Chris@189 280 emit mouseLeft();
Chris@189 281 }
Chris@189 282
Chris@189 283 void
Chris@132 284 Thumbwheel::mousePressEvent(QMouseEvent *e)
Chris@132 285 {
Chris@187 286 if (e->button() == Qt::MidButton ||
Chris@187 287 ((e->button() == Qt::LeftButton) &&
Chris@187 288 (e->modifiers() & Qt::ControlModifier))) {
Chris@187 289 resetToDefault();
Chris@187 290 } else if (e->button() == Qt::LeftButton) {
Chris@133 291 m_clicked = true;
Chris@133 292 m_clickPos = e->pos();
Chris@165 293 m_clickRotation = m_rotation;
Chris@133 294 }
Chris@132 295 }
Chris@132 296
Chris@132 297 void
Chris@187 298 Thumbwheel::mouseDoubleClickEvent(QMouseEvent *mouseEvent)
Chris@132 299 {
Chris@188 300 //!!! needs a common base class with AudioDial (and Panner?)
Chris@187 301
Chris@187 302 if (mouseEvent->button() != Qt::LeftButton) {
Chris@187 303 return;
Chris@187 304 }
Chris@187 305
Chris@187 306 bool ok = false;
Chris@187 307
Chris@187 308 if (m_rangeMapper) {
Chris@187 309
Chris@187 310 float min = m_rangeMapper->getValueForPosition(m_min);
Chris@187 311 float max = m_rangeMapper->getValueForPosition(m_max);
Chris@187 312
Chris@187 313 if (min > max) {
Chris@187 314 float tmp = min;
Chris@187 315 min = max;
Chris@187 316 max = tmp;
Chris@187 317 }
Chris@187 318
Chris@187 319 QString unit = m_rangeMapper->getUnit();
Chris@187 320
Chris@187 321 QString text;
Chris@187 322 if (objectName() != "") {
Chris@187 323 if (unit != "") {
Chris@187 324 text = tr("New value for %1, from %2 to %3 %4:")
Chris@187 325 .arg(objectName()).arg(min).arg(max).arg(unit);
Chris@187 326 } else {
Chris@187 327 text = tr("New value for %1, from %2 to %3:")
Chris@187 328 .arg(objectName()).arg(min).arg(max);
Chris@187 329 }
Chris@187 330 } else {
Chris@187 331 if (unit != "") {
Chris@187 332 text = tr("Enter a new value from %1 to %2 %3:")
Chris@187 333 .arg(min).arg(max).arg(unit);
Chris@187 334 } else {
Chris@187 335 text = tr("Enter a new value from %1 to %2:")
Chris@187 336 .arg(min).arg(max);
Chris@187 337 }
Chris@187 338 }
Chris@187 339
Chris@187 340 float newValue = QInputDialog::getDouble
Chris@187 341 (this,
Chris@187 342 tr("Enter new value"),
Chris@187 343 text,
Chris@187 344 m_mappedValue,
Chris@187 345 min,
Chris@187 346 max,
Chris@187 347 4,
Chris@187 348 &ok);
Chris@187 349
Chris@187 350 if (ok) {
Chris@187 351 setMappedValue(newValue);
Chris@187 352 }
Chris@187 353
Chris@187 354 } else {
Chris@187 355
Chris@187 356 int newValue = QInputDialog::getInteger
Chris@187 357 (this,
Chris@187 358 tr("Enter new value"),
Chris@187 359 tr("Enter a new value from %1 to %2:")
Chris@187 360 .arg(m_min).arg(m_max),
Chris@187 361 getValue(), m_min, m_max, 1, &ok);
Chris@187 362
Chris@187 363 if (ok) {
Chris@187 364 setValue(newValue);
Chris@187 365 }
Chris@187 366 }
Chris@132 367 }
Chris@132 368
Chris@187 369
Chris@132 370 void
Chris@132 371 Thumbwheel::mouseMoveEvent(QMouseEvent *e)
Chris@132 372 {
Chris@133 373 if (!m_clicked) return;
Chris@132 374 int dist = 0;
Chris@132 375 if (m_orientation == Qt::Horizontal) {
Chris@132 376 dist = e->x() - m_clickPos.x();
Chris@132 377 } else {
Chris@132 378 dist = e->y() - m_clickPos.y();
Chris@132 379 }
Chris@165 380
Chris@165 381 float rotation = m_clickRotation + (m_speed * dist) / 100;
Chris@165 382 if (rotation < 0.f) rotation = 0.f;
Chris@165 383 if (rotation > 1.f) rotation = 1.f;
Chris@165 384 int value = lrintf(m_min + (m_max - m_min) * m_rotation);
Chris@132 385 if (value != m_value) {
Chris@132 386 setValue(value);
Chris@132 387 if (m_tracking) emit valueChanged(getValue());
Chris@165 388 m_rotation = rotation;
Chris@165 389 } else if (fabsf(rotation - m_rotation) > 0.001) {
Chris@165 390 m_rotation = rotation;
Chris@165 391 repaint();
Chris@165 392 }
Chris@132 393 }
Chris@132 394
Chris@132 395 void
Chris@132 396 Thumbwheel::mouseReleaseEvent(QMouseEvent *e)
Chris@132 397 {
Chris@133 398 if (!m_clicked) return;
Chris@132 399 bool reallyTracking = m_tracking;
Chris@132 400 m_tracking = true;
Chris@132 401 mouseMoveEvent(e);
Chris@132 402 m_tracking = reallyTracking;
Chris@133 403 m_clicked = false;
Chris@132 404 }
Chris@132 405
Chris@132 406 void
Chris@132 407 Thumbwheel::wheelEvent(QWheelEvent *e)
Chris@132 408 {
Chris@132 409 int step = lrintf(m_speed);
Chris@132 410 if (step == 0) step = 1;
Chris@132 411
Chris@132 412 if (e->delta() > 0) {
Chris@132 413 setValue(m_value + step);
Chris@132 414 } else {
Chris@132 415 setValue(m_value - step);
Chris@132 416 }
Chris@132 417
Chris@132 418 emit valueChanged(getValue());
Chris@132 419 }
Chris@132 420
Chris@132 421 void
Chris@132 422 Thumbwheel::paintEvent(QPaintEvent *)
Chris@132 423 {
Chris@190 424 Profiler profiler("Thumbwheel::paintEvent", true);
Chris@190 425
Chris@191 426 int bw = 3;
Chris@191 427
Chris@191 428 QRect subclip;
Chris@191 429 if (m_orientation == Qt::Horizontal) {
Chris@191 430 subclip = QRect(bw, bw+1, width() - bw*2, height() - bw*2 - 2);
Chris@191 431 } else {
Chris@191 432 subclip = QRect(bw+1, bw, width() - bw*2 - 2, height() - bw*2);
Chris@191 433 }
Chris@191 434
Chris@133 435 QPainter paint(this);
Chris@191 436 paint.fillRect(subclip, palette().background().color());
Chris@190 437
Chris@190 438 paint.setRenderHint(QPainter::Antialiasing, true);
Chris@133 439
Chris@190 440 float w = width();
Chris@190 441 float w0 = 0.5;
Chris@190 442 float w1 = w - 0.5;
Chris@190 443
Chris@190 444 float h = height();
Chris@190 445 float h0 = 0.5;
Chris@190 446 float h1 = h - 0.5;
Chris@190 447
Chris@190 448 for (int i = bw-1; i >= 0; --i) {
Chris@190 449 // for (int i = 0; i < 1; ++i) {
Chris@190 450
Chris@133 451 int grey = (i + 1) * (256 / (bw + 1));
Chris@133 452 QColor fc = QColor(grey, grey, grey);
Chris@133 453 paint.setPen(fc);
Chris@190 454
Chris@190 455 QPainterPath path;
Chris@190 456
Chris@190 457 if (m_orientation == Qt::Horizontal) {
Chris@190 458 path.moveTo(w0 + i, h0 + i + 2);
Chris@190 459 path.quadTo(w/2, i * 1.25, w1 - i, h0 + i + 2);
Chris@190 460 path.lineTo(w1 - i, h1 - i - 2);
Chris@190 461 path.quadTo(w/2, h - i * 1.25, w0 + i, h1 - i - 2);
Chris@190 462 path.closeSubpath();
Chris@190 463 } else {
Chris@190 464 path.moveTo(w0 + i + 2, h0 + i);
Chris@190 465 path.quadTo(i * 1.25, h/2, w0 + i + 2, h1 - i);
Chris@190 466 path.lineTo(w1 - i - 2, h1 - i);
Chris@190 467 path.quadTo(w - i * 1.25, h/2, w1 - i - 2, h0 + i);
Chris@190 468 path.closeSubpath();
Chris@190 469 }
Chris@190 470
Chris@190 471 paint.drawPath(path);
Chris@133 472 }
Chris@133 473
Chris@191 474 paint.setClipRect(subclip);
Chris@133 475
Chris@165 476 float radians = m_rotation * 1.5f * M_PI;
Chris@132 477
Chris@132 478 // std::cerr << "value = " << m_value << ", min = " << m_min << ", max = " << m_max << ", rotation = " << rotation << std::endl;
Chris@132 479
Chris@190 480 w = (m_orientation == Qt::Horizontal ? width() : height()) - bw*2;
Chris@132 481
Chris@132 482 // total number of notches on the entire wheel
Chris@132 483 int notches = 25;
Chris@132 484
Chris@132 485 // radius of the wheel including invisible part
Chris@249 486 int radius = int(w / 2 + 2);
Chris@132 487
Chris@132 488 for (int i = 0; i < notches; ++i) {
Chris@132 489
Chris@165 490 float a0 = (2.f * M_PI * i) / notches + radians;
Chris@132 491 float a1 = a0 + M_PI / (notches * 2);
Chris@165 492 float a2 = (2.f * M_PI * (i + 1)) / notches + radians;
Chris@132 493
Chris@132 494 float depth = cosf((a0 + a2) / 2);
Chris@132 495 if (depth < 0) continue;
Chris@132 496
Chris@132 497 float x0 = radius * sinf(a0) + w/2;
Chris@132 498 float x1 = radius * sinf(a1) + w/2;
Chris@132 499 float x2 = radius * sinf(a2) + w/2;
Chris@132 500 if (x2 < 0 || x0 > w) continue;
Chris@132 501
Chris@132 502 if (x0 < 0) x0 = 0;
Chris@132 503 if (x2 > w) x2 = w;
Chris@132 504
Chris@133 505 x0 += bw;
Chris@133 506 x1 += bw;
Chris@133 507 x2 += bw;
Chris@133 508
Chris@132 509 int grey = lrintf(255 * depth);
Chris@132 510 QColor fc = QColor(grey, grey, grey);
Chris@132 511 QColor oc = palette().dark().color();
Chris@132 512
Chris@132 513 paint.setPen(oc);
Chris@132 514 paint.setBrush(fc);
Chris@132 515
Chris@132 516 if (m_orientation == Qt::Horizontal) {
Chris@133 517 paint.drawRect(QRectF(x1, bw, x2 - x1, height() - bw*2));
Chris@132 518 } else {
Chris@133 519 paint.drawRect(QRectF(bw, x1, width() - bw*2, x2 - x1));
Chris@132 520 }
Chris@132 521
Chris@132 522 if (m_showScale) {
Chris@132 523
Chris@132 524 paint.setBrush(oc);
Chris@132 525
Chris@132 526 float prop;
Chris@132 527 if (i >= notches / 4) {
Chris@132 528 prop = float(notches - (((i - float(notches) / 4.f) * 4.f) / 3.f))
Chris@132 529 / notches;
Chris@132 530 } else {
Chris@132 531 prop = 0.f;
Chris@132 532 }
Chris@132 533
Chris@132 534 if (m_orientation == Qt::Horizontal) {
Chris@133 535 paint.drawRect(QRectF(x1, height() - (height() - bw*2) * prop - bw,
Chris@132 536 x2 - x1, height() * prop));
Chris@132 537 } else {
Chris@133 538 paint.drawRect(QRectF(bw, x1, (width() - bw*2) * prop, x2 - x1));
Chris@132 539 }
Chris@132 540 }
Chris@132 541
Chris@132 542 paint.setPen(oc);
Chris@132 543 paint.setBrush(palette().background().color());
Chris@132 544
Chris@132 545 if (m_orientation == Qt::Horizontal) {
Chris@133 546 paint.drawRect(QRectF(x0, bw, x1 - x0, height() - bw*2));
Chris@132 547 } else {
Chris@133 548 paint.drawRect(QRectF(bw, x0, width() - bw*2, x1 - x0));
Chris@132 549 }
Chris@132 550 }
Chris@132 551 }
Chris@132 552
Chris@132 553 QSize
Chris@132 554 Thumbwheel::sizeHint() const
Chris@132 555 {
Chris@132 556 if (m_orientation == Qt::Horizontal) {
Chris@132 557 return QSize(80, 12);
Chris@132 558 } else {
Chris@132 559 return QSize(12, 80);
Chris@132 560 }
Chris@132 561 }
Chris@132 562