annotate widgets/LevelPanWidget.cpp @ 1303:13f5f84fbfad

Collect the bits of bookkeeping for mouse wheel events, and use in all widgets
author Chris Cannam
date Fri, 22 Jun 2018 17:19:48 +0100
parents f3d3fab250ac
children a575dae05fbf
rev   line source
Chris@923 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@923 2
Chris@923 3 /*
Chris@923 4 Sonic Visualiser
Chris@923 5 An audio file viewer and annotation editor.
Chris@923 6 Centre for Digital Music, Queen Mary, University of London.
Chris@923 7
Chris@923 8 This program is free software; you can redistribute it and/or
Chris@923 9 modify it under the terms of the GNU General Public License as
Chris@923 10 published by the Free Software Foundation; either version 2 of the
Chris@923 11 License, or (at your option) any later version. See the file
Chris@923 12 COPYING included with this distribution for more information.
Chris@923 13 */
Chris@923 14
Chris@923 15 #include "LevelPanWidget.h"
Chris@923 16
Chris@923 17 #include <QPainter>
Chris@923 18 #include <QMouseEvent>
Chris@923 19 #include <QWheelEvent>
Chris@923 20
Chris@923 21 #include "layer/ColourMapper.h"
Chris@925 22 #include "base/AudioLevel.h"
Chris@923 23
Chris@1176 24 #include "WidgetScale.h"
Chris@1176 25
Chris@923 26 #include <iostream>
Chris@926 27 #include <cmath>
Chris@940 28 #include <cassert>
Chris@923 29
Chris@923 30 using std::cerr;
Chris@923 31 using std::endl;
Chris@923 32
Chris@1301 33 /**
Chris@1301 34 * Gain and pan scales:
Chris@1301 35 *
Chris@1301 36 * Gain: we have 5 circles vertically in the display, each of which
Chris@1301 37 * has half-circle and full-circle versions, and we also have "no
Chris@1301 38 * circles", so there are in total 11 distinct levels, which we refer
Chris@1301 39 * to as "notches" and number 0-10. (We use "notch" because "level" is
Chris@1301 40 * used by the external API to refer to audio gain.)
Chris@1301 41 *
Chris@1301 42 * i.e. the levels are represented by these (schematic, rotated to
Chris@1301 43 * horizontal) displays:
Chris@1301 44 *
Chris@1301 45 * 0 X
Chris@1301 46 * 1 [
Chris@1301 47 * 2 []
Chris@1301 48 * 3 [][
Chris@1301 49 * ...
Chris@1301 50 * 9 [][][][][
Chris@1301 51 * 10 [][][][][]
Chris@1301 52 *
Chris@1301 53 * If we have mute enabled, then we map the range 0-10 to gain using
Chris@1301 54 * AudioLevel::fader_to_* with the ShortFader type, which treats fader
Chris@1301 55 * 0 as muted. If mute is disabled, then we map the range 1-10.
Chris@1301 56 *
Chris@1301 57 * We can also disable half-circles, which leaves the range unchanged
Chris@1301 58 * but limits the notches to even values.
Chris@1301 59 *
Chris@1301 60 * Pan: we have 5 columns with no finer resolution, so we only have 2
Chris@1301 61 * possible pan values on each side of centre.
Chris@1301 62 */
Chris@1301 63
Chris@923 64 static const int maxPan = 2; // range is -maxPan to maxPan
Chris@923 65
Chris@923 66 LevelPanWidget::LevelPanWidget(QWidget *parent) :
Chris@923 67 QWidget(parent),
Chris@1301 68 m_minNotch(0),
Chris@1301 69 m_maxNotch(10),
Chris@1301 70 m_notch(m_maxNotch),
Chris@923 71 m_pan(0),
Chris@1177 72 m_monitorLeft(-1),
Chris@1177 73 m_monitorRight(-1),
Chris@940 74 m_editable(true),
Chris@1249 75 m_editing(false),
Chris@1301 76 m_includeMute(true),
Chris@1303 77 m_includeHalfSteps(true)
Chris@923 78 {
Chris@1191 79 setToolTip(tr("Drag vertically to adjust level, horizontally to adjust pan"));
Chris@1201 80 setLevel(1.0);
Chris@1201 81 setPan(0.0);
Chris@923 82 }
Chris@923 83
Chris@923 84 LevelPanWidget::~LevelPanWidget()
Chris@923 85 {
Chris@923 86 }
Chris@923 87
Chris@1249 88 void
Chris@1249 89 LevelPanWidget::setToDefault()
Chris@1249 90 {
Chris@1249 91 setLevel(1.0);
Chris@1249 92 setPan(0.0);
Chris@1250 93 emitLevelChanged();
Chris@1250 94 emitPanChanged();
Chris@1249 95 }
Chris@1249 96
Chris@929 97 QSize
Chris@929 98 LevelPanWidget::sizeHint() const
Chris@929 99 {
Chris@1176 100 return WidgetScale::scaleQSize(QSize(40, 40));
Chris@929 101 }
Chris@929 102
Chris@1301 103 int
Chris@1301 104 LevelPanWidget::clampNotch(int notch) const
Chris@940 105 {
Chris@1301 106 if (notch < m_minNotch) notch = m_minNotch;
Chris@1301 107 if (notch > m_maxNotch) notch = m_maxNotch;
Chris@1301 108 if (!m_includeHalfSteps) {
Chris@1301 109 notch = (notch / 2) * 2;
Chris@1301 110 }
Chris@1301 111 return notch;
Chris@940 112 }
Chris@940 113
Chris@1177 114 int
Chris@1302 115 LevelPanWidget::clampPan(int pan) const
Chris@1302 116 {
Chris@1302 117 if (pan < -maxPan) pan = -maxPan;
Chris@1302 118 if (pan > maxPan) pan = maxPan;
Chris@1302 119 return pan;
Chris@1302 120 }
Chris@1302 121
Chris@1302 122 int
Chris@1301 123 LevelPanWidget::audioLevelToNotch(float audioLevel) const
Chris@1177 124 {
Chris@1301 125 int notch = AudioLevel::multiplier_to_fader
Chris@1301 126 (audioLevel, m_maxNotch, AudioLevel::ShortFader);
Chris@1301 127 return clampNotch(notch);
Chris@1177 128 }
Chris@1177 129
Chris@1177 130 float
Chris@1301 131 LevelPanWidget::notchToAudioLevel(int notch) const
Chris@1177 132 {
Chris@1301 133 return float(AudioLevel::fader_to_multiplier
Chris@1301 134 (notch, m_maxNotch, AudioLevel::ShortFader));
Chris@1177 135 }
Chris@1177 136
Chris@923 137 void
Chris@1301 138 LevelPanWidget::setLevel(float level)
Chris@923 139 {
Chris@1301 140 int notch = audioLevelToNotch(level);
Chris@1301 141 if (notch != m_notch) {
Chris@1301 142 m_notch = notch;
Chris@1266 143 float convertsTo = getLevel();
Chris@1301 144 if (fabsf(convertsTo - level) > 1e-5) {
Chris@1266 145 emitLevelChanged();
Chris@1266 146 }
Chris@1266 147 update();
Chris@925 148 }
Chris@1301 149 SVCERR << "setLevel: level " << level << " -> notch " << m_notch << " (which converts back to level " << getLevel() << ")" << endl;
Chris@923 150 }
Chris@923 151
Chris@940 152 float
Chris@940 153 LevelPanWidget::getLevel() const
Chris@940 154 {
Chris@1301 155 return notchToAudioLevel(m_notch);
Chris@1177 156 }
Chris@1177 157
Chris@1177 158 int
Chris@1301 159 LevelPanWidget::audioPanToPan(float audioPan) const
Chris@1177 160 {
Chris@1177 161 int pan = int(round(audioPan * maxPan));
Chris@1302 162 pan = clampPan(pan);
Chris@1177 163 return pan;
Chris@1177 164 }
Chris@1177 165
Chris@1177 166 float
Chris@1301 167 LevelPanWidget::panToAudioPan(int pan) const
Chris@1177 168 {
Chris@1177 169 return float(pan) / float(maxPan);
Chris@1177 170 }
Chris@1177 171
Chris@1177 172 void
Chris@1177 173 LevelPanWidget::setPan(float fpan)
Chris@1177 174 {
Chris@1177 175 int pan = audioPanToPan(fpan);
Chris@1177 176 if (pan != m_pan) {
Chris@1177 177 m_pan = pan;
Chris@1177 178 update();
Chris@940 179 }
Chris@940 180 }
Chris@940 181
Chris@1177 182 float
Chris@1177 183 LevelPanWidget::getPan() const
Chris@1177 184 {
Chris@1177 185 return panToAudioPan(m_pan);
Chris@1177 186 }
Chris@1177 187
Chris@923 188 void
Chris@1177 189 LevelPanWidget::setMonitoringLevels(float left, float right)
Chris@923 190 {
Chris@1177 191 m_monitorLeft = left;
Chris@1177 192 m_monitorRight = right;
Chris@923 193 update();
Chris@923 194 }
Chris@923 195
Chris@940 196 bool
Chris@940 197 LevelPanWidget::isEditable() const
Chris@940 198 {
Chris@940 199 return m_editable;
Chris@940 200 }
Chris@940 201
Chris@940 202 bool
Chris@940 203 LevelPanWidget::includesMute() const
Chris@940 204 {
Chris@940 205 return m_includeMute;
Chris@940 206 }
Chris@940 207
Chris@923 208 void
Chris@923 209 LevelPanWidget::setEditable(bool editable)
Chris@923 210 {
Chris@923 211 m_editable = editable;
Chris@923 212 update();
Chris@923 213 }
Chris@923 214
Chris@940 215 void
Chris@940 216 LevelPanWidget::setIncludeMute(bool include)
Chris@923 217 {
Chris@940 218 m_includeMute = include;
Chris@1301 219 if (m_includeMute) {
Chris@1301 220 m_minNotch = 0;
Chris@1301 221 } else {
Chris@1301 222 m_minNotch = 1;
Chris@1301 223 }
Chris@940 224 emitLevelChanged();
Chris@940 225 update();
Chris@923 226 }
Chris@923 227
Chris@923 228 void
Chris@923 229 LevelPanWidget::emitLevelChanged()
Chris@923 230 {
Chris@923 231 emit levelChanged(getLevel());
Chris@923 232 }
Chris@923 233
Chris@923 234 void
Chris@923 235 LevelPanWidget::emitPanChanged()
Chris@923 236 {
Chris@923 237 emit panChanged(getPan());
Chris@923 238 }
Chris@923 239
Chris@923 240 void
Chris@923 241 LevelPanWidget::mousePressEvent(QMouseEvent *e)
Chris@923 242 {
Chris@1249 243 if (e->button() == Qt::MidButton ||
Chris@1249 244 ((e->button() == Qt::LeftButton) &&
Chris@1249 245 (e->modifiers() & Qt::ControlModifier))) {
Chris@1249 246 setToDefault();
Chris@1249 247 } else if (e->button() == Qt::LeftButton) {
Chris@1249 248 m_editing = true;
Chris@1249 249 mouseMoveEvent(e);
Chris@1249 250 }
Chris@1249 251 }
Chris@1249 252
Chris@1249 253 void
Chris@1249 254 LevelPanWidget::mouseReleaseEvent(QMouseEvent *e)
Chris@1249 255 {
Chris@923 256 mouseMoveEvent(e);
Chris@1249 257 m_editing = false;
Chris@923 258 }
Chris@923 259
Chris@923 260 void
Chris@923 261 LevelPanWidget::mouseMoveEvent(QMouseEvent *e)
Chris@923 262 {
Chris@923 263 if (!m_editable) return;
Chris@1249 264 if (!m_editing) return;
Chris@923 265
Chris@1301 266 int notch = coordsToNotch(rect(), e->pos());
Chris@1301 267 int pan = coordsToPan(rect(), e->pos());
Chris@1301 268
Chris@1301 269 if (notch == m_notch && pan == m_pan) {
Chris@1266 270 return;
Chris@923 271 }
Chris@1301 272 if (notch != m_notch) {
Chris@1301 273 m_notch = notch;
Chris@1266 274 emitLevelChanged();
Chris@923 275 }
Chris@923 276 if (pan != m_pan) {
Chris@1266 277 m_pan = pan;
Chris@1266 278 emitPanChanged();
Chris@923 279 }
Chris@923 280 update();
Chris@923 281 }
Chris@923 282
Chris@923 283 void
Chris@923 284 LevelPanWidget::wheelEvent(QWheelEvent *e)
Chris@923 285 {
Chris@1303 286 int delta = m_wheelCounter.count(e);
Chris@1303 287
Chris@1303 288 if (delta == 0) {
Chris@1302 289 return;
Chris@1302 290 }
Chris@1302 291
Chris@1303 292 if (e->modifiers() & Qt::ControlModifier) {
Chris@1303 293 m_pan = clampPan(m_pan + delta);
Chris@1303 294 emitPanChanged();
Chris@1303 295 update();
Chris@1302 296 } else {
Chris@1303 297 m_notch = clampNotch(m_notch + delta);
Chris@1303 298 emitLevelChanged();
Chris@1303 299 update();
Chris@923 300 }
Chris@923 301 }
Chris@923 302
Chris@1301 303 int
Chris@1301 304 LevelPanWidget::coordsToNotch(QRectF rect, QPointF loc) const
Chris@923 305 {
Chris@1301 306 double h = rect.height();
Chris@1301 307
Chris@1301 308 int nnotch = m_maxNotch + 1;
Chris@1301 309 double cell = h / nnotch;
Chris@1301 310
Chris@1301 311 int notch = int((h - (loc.y() - rect.y())) / cell);
Chris@1301 312 notch = clampNotch(notch);
Chris@1301 313
Chris@1301 314 return notch;
Chris@1301 315 }
Chris@1301 316
Chris@1301 317 int
Chris@1301 318 LevelPanWidget::coordsToPan(QRectF rect, QPointF loc) const
Chris@1301 319 {
Chris@1301 320 double w = rect.width();
Chris@929 321
Chris@923 322 int npan = maxPan * 2 + 1;
Chris@1301 323 double cell = w / npan;
Chris@929 324
Chris@1301 325 int pan = int((loc.x() - rect.x()) / cell) - maxPan;
Chris@1302 326 pan = clampPan(pan);
Chris@1301 327
Chris@1301 328 return pan;
Chris@923 329 }
Chris@923 330
Chris@923 331 QSizeF
Chris@929 332 LevelPanWidget::cellSize(QRectF rect) const
Chris@923 333 {
Chris@929 334 double w = rect.width(), h = rect.height();
Chris@1301 335 int ncol = maxPan * 2 + 1;
Chris@1301 336 int nrow = m_maxNotch/2;
Chris@1301 337 double wcell = w / ncol, hcell = h / nrow;
Chris@923 338 return QSizeF(wcell, hcell);
Chris@923 339 }
Chris@923 340
Chris@923 341 QPointF
Chris@1301 342 LevelPanWidget::cellCentre(QRectF rect, int row, int col) const
Chris@923 343 {
Chris@929 344 QSizeF cs = cellSize(rect);
Chris@1301 345 return QPointF(rect.x() +
Chris@1301 346 cs.width() * (col + maxPan) + cs.width() / 2.,
Chris@1301 347 rect.y() + rect.height() -
Chris@1301 348 cs.height() * (row + 1) + cs.height() / 2.);
Chris@923 349 }
Chris@923 350
Chris@923 351 QSizeF
Chris@929 352 LevelPanWidget::cellLightSize(QRectF rect) const
Chris@923 353 {
Chris@923 354 double extent = 3. / 4.;
Chris@929 355 QSizeF cs = cellSize(rect);
Chris@923 356 double m = std::min(cs.width(), cs.height());
Chris@923 357 return QSizeF(m * extent, m * extent);
Chris@923 358 }
Chris@923 359
Chris@923 360 QRectF
Chris@1301 361 LevelPanWidget::cellLightRect(QRectF rect, int row, int col) const
Chris@923 362 {
Chris@929 363 QSizeF cls = cellLightSize(rect);
Chris@1301 364 QPointF cc = cellCentre(rect, row, col);
Chris@923 365 return QRectF(cc.x() - cls.width() / 2.,
Chris@1266 366 cc.y() - cls.height() / 2.,
Chris@1266 367 cls.width(),
Chris@1266 368 cls.height());
Chris@923 369 }
Chris@923 370
Chris@923 371 double
Chris@929 372 LevelPanWidget::thinLineWidth(QRectF rect) const
Chris@923 373 {
Chris@929 374 double tw = ceil(rect.width() / (maxPan * 2. * 10.));
Chris@1301 375 double th = ceil(rect.height() / (m_maxNotch/2 * 10.));
Chris@923 376 return std::min(th, tw);
Chris@923 377 }
Chris@923 378
Chris@1301 379 QRectF
Chris@1301 380 LevelPanWidget::cellOutlineRect(QRectF rect, int row, int col) const
Chris@941 381 {
Chris@1301 382 QRectF clr = cellLightRect(rect, row, col);
Chris@1301 383 double adj = thinLineWidth(rect)/2;
Chris@1301 384 return clr.adjusted(-adj, -adj, adj, adj);
Chris@1301 385 }
Chris@1301 386
Chris@1301 387 QColor
Chris@1301 388 LevelPanWidget::notchToColour(int notch) const
Chris@1301 389 {
Chris@1301 390 if (notch < 3) return Qt::black;
Chris@1301 391 if (notch < 5) return QColor(80, 0, 0);
Chris@1301 392 if (notch < 7) return QColor(160, 0, 0);
Chris@1301 393 if (notch < 9) return QColor(255, 0, 0);
Chris@1301 394 return QColor(255, 255, 0);
Chris@941 395 }
Chris@941 396
Chris@923 397 void
Chris@929 398 LevelPanWidget::renderTo(QPaintDevice *dev, QRectF rect, bool asIfEditable) const
Chris@923 399 {
Chris@929 400 QPainter paint(dev);
Chris@923 401
Chris@923 402 paint.setRenderHint(QPainter::Antialiasing, true);
Chris@923 403
Chris@923 404 QPen pen;
Chris@923 405
Chris@929 406 double thin = thinLineWidth(rect);
Chris@938 407
Chris@1301 408 QColor columnBackground = QColor(180, 180, 180);
Chris@1301 409 pen.setColor(columnBackground);
Chris@929 410 pen.setWidthF(cellLightSize(rect).width() + thin);
Chris@923 411 pen.setCapStyle(Qt::RoundCap);
Chris@923 412 paint.setPen(pen);
Chris@1177 413 paint.setBrush(Qt::NoBrush);
Chris@923 414
Chris@923 415 for (int pan = -maxPan; pan <= maxPan; ++pan) {
Chris@1301 416 paint.drawLine(cellCentre(rect, 0, pan),
Chris@1301 417 cellCentre(rect, m_maxNotch/2 - 1, pan));
Chris@924 418 }
Chris@924 419
Chris@1301 420 bool monitoring = (m_monitorLeft > 0.f || m_monitorRight > 0.f);
Chris@1301 421
Chris@1301 422 if (isEnabled()) {
Chris@1301 423 pen.setColor(Qt::black);
Chris@1301 424 } else {
Chris@1301 425 pen.setColor(Qt::darkGray);
Chris@1301 426 }
Chris@1301 427
Chris@1301 428 if (!asIfEditable && m_includeMute && m_notch == 0) {
Chris@1301 429 // The X for mute takes up the whole display when we're not
Chris@1301 430 // being rendered in editable style
Chris@1301 431 pen.setWidthF(thin * 2);
Chris@1301 432 pen.setCapStyle(Qt::RoundCap);
Chris@1301 433 paint.setPen(pen);
Chris@1301 434 paint.drawLine(cellCentre(rect, 0, -maxPan),
Chris@1301 435 cellCentre(rect, m_maxNotch/2 - 1, maxPan));
Chris@1301 436 paint.drawLine(cellCentre(rect, m_maxNotch/2 - 1, -maxPan),
Chris@1301 437 cellCentre(rect, 0, maxPan));
Chris@1301 438 } else {
Chris@1301 439 // the normal case
Chris@1301 440
Chris@1301 441 // pen a bit less thin than in theory, so that we can erase
Chris@1301 442 // semi-circles later without leaving a faint edge
Chris@1301 443 pen.setWidthF(thin * 0.8);
Chris@1301 444 pen.setCapStyle(Qt::FlatCap);
Chris@1301 445 paint.setPen(pen);
Chris@1301 446
Chris@1301 447 if (m_includeMute && m_notch == 0) {
Chris@1301 448 QRectF clr = cellLightRect(rect, 0, m_pan);
Chris@1301 449 paint.drawLine(clr.topLeft(), clr.bottomRight());
Chris@1301 450 paint.drawLine(clr.bottomLeft(), clr.topRight());
Chris@1301 451 } else {
Chris@1301 452 for (int notch = 1; notch <= m_notch; notch += 2) {
Chris@1301 453 if (isEnabled() && !monitoring) {
Chris@1301 454 paint.setBrush(notchToColour(notch));
Chris@1301 455 }
Chris@1301 456 QRectF clr = cellLightRect(rect, notch/2, m_pan);
Chris@1301 457 paint.drawEllipse(clr);
Chris@1301 458 }
Chris@1301 459 if (m_notch % 2 != 0) {
Chris@1301 460 QRectF clr = cellOutlineRect(rect, (m_notch-1)/2, m_pan);
Chris@1301 461 paint.save();
Chris@1301 462 paint.setPen(Qt::NoPen);
Chris@1301 463 paint.setBrush(columnBackground);
Chris@1301 464 paint.drawPie(clr, 0, 180 * 16);
Chris@1301 465 paint.restore();
Chris@1301 466 }
Chris@1301 467 }
Chris@1301 468 }
Chris@1301 469
Chris@1301 470 if (monitoring) {
Chris@1177 471 paint.setPen(Qt::NoPen);
Chris@1177 472 for (int pan = -maxPan; pan <= maxPan; ++pan) {
Chris@1177 473 float audioPan = panToAudioPan(pan);
Chris@1177 474 float audioLevel;
Chris@1177 475 if (audioPan < 0.f) {
Chris@1177 476 audioLevel = m_monitorLeft + m_monitorRight * (1.f + audioPan);
Chris@1177 477 } else {
Chris@1177 478 audioLevel = m_monitorRight + m_monitorLeft * (1.f - audioPan);
Chris@1177 479 }
Chris@1301 480 int notchHere = audioLevelToNotch(audioLevel);
Chris@1301 481 for (int notch = 1; notch <= notchHere; notch += 2) {
Chris@1301 482 paint.setBrush(notchToColour(notch));
Chris@1301 483 QRectF clr = cellLightRect(rect, (notch-1)/2, pan);
Chris@1301 484 double adj = thinLineWidth(rect)/2;
Chris@1301 485 clr = clr.adjusted(adj, adj, -adj, -adj);
Chris@1301 486 if (notch + 2 > notchHere && notchHere % 2 != 0) {
Chris@1301 487 paint.drawPie(clr, 180 * 16, 180 * 16);
Chris@1301 488 } else {
Chris@1301 489 paint.drawEllipse(clr);
Chris@1301 490 }
Chris@1177 491 }
Chris@1177 492 }
Chris@1177 493 paint.setPen(pen);
Chris@1177 494 paint.setBrush(Qt::NoBrush);
Chris@1177 495 }
Chris@923 496 }
Chris@923 497
Chris@929 498 void
Chris@929 499 LevelPanWidget::paintEvent(QPaintEvent *)
Chris@929 500 {
Chris@929 501 renderTo(this, rect(), m_editable);
Chris@929 502 }
Chris@923 503
Chris@1180 504 void
Chris@1180 505 LevelPanWidget::enterEvent(QEvent *e)
Chris@1180 506 {
Chris@1180 507 QWidget::enterEvent(e);
Chris@1180 508 emit mouseEntered();
Chris@1180 509 }
Chris@929 510
Chris@1180 511 void
Chris@1180 512 LevelPanWidget::leaveEvent(QEvent *e)
Chris@1180 513 {
Chris@1180 514 QWidget::enterEvent(e);
Chris@1180 515 emit mouseLeft();
Chris@1180 516 }
Chris@929 517