annotate widgets/LevelPanWidget.cpp @ 1301:e8368466fa34

Half-steps for level in level-pan widget
author Chris Cannam
date Thu, 21 Jun 2018 15:36:29 +0100
parents a34a2a25907c
children f3d3fab250ac
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@1301 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@1301 115 LevelPanWidget::audioLevelToNotch(float audioLevel) const
Chris@1177 116 {
Chris@1301 117 int notch = AudioLevel::multiplier_to_fader
Chris@1301 118 (audioLevel, m_maxNotch, AudioLevel::ShortFader);
Chris@1301 119 return clampNotch(notch);
Chris@1177 120 }
Chris@1177 121
Chris@1177 122 float
Chris@1301 123 LevelPanWidget::notchToAudioLevel(int notch) const
Chris@1177 124 {
Chris@1301 125 return float(AudioLevel::fader_to_multiplier
Chris@1301 126 (notch, m_maxNotch, AudioLevel::ShortFader));
Chris@1177 127 }
Chris@1177 128
Chris@923 129 void
Chris@1301 130 LevelPanWidget::setLevel(float level)
Chris@923 131 {
Chris@1301 132 int notch = audioLevelToNotch(level);
Chris@1301 133 if (notch != m_notch) {
Chris@1301 134 m_notch = notch;
Chris@1266 135 float convertsTo = getLevel();
Chris@1301 136 if (fabsf(convertsTo - level) > 1e-5) {
Chris@1266 137 emitLevelChanged();
Chris@1266 138 }
Chris@1266 139 update();
Chris@925 140 }
Chris@1301 141 SVCERR << "setLevel: level " << level << " -> notch " << m_notch << " (which converts back to level " << getLevel() << ")" << endl;
Chris@923 142 }
Chris@923 143
Chris@940 144 float
Chris@940 145 LevelPanWidget::getLevel() const
Chris@940 146 {
Chris@1301 147 return notchToAudioLevel(m_notch);
Chris@1177 148 }
Chris@1177 149
Chris@1177 150 int
Chris@1301 151 LevelPanWidget::audioPanToPan(float audioPan) const
Chris@1177 152 {
Chris@1177 153 int pan = int(round(audioPan * maxPan));
Chris@1177 154 if (pan < -maxPan) pan = -maxPan;
Chris@1177 155 if (pan > maxPan) pan = maxPan;
Chris@1177 156 return pan;
Chris@1177 157 }
Chris@1177 158
Chris@1177 159 float
Chris@1301 160 LevelPanWidget::panToAudioPan(int pan) const
Chris@1177 161 {
Chris@1177 162 return float(pan) / float(maxPan);
Chris@1177 163 }
Chris@1177 164
Chris@1177 165 void
Chris@1177 166 LevelPanWidget::setPan(float fpan)
Chris@1177 167 {
Chris@1177 168 int pan = audioPanToPan(fpan);
Chris@1177 169 if (pan != m_pan) {
Chris@1177 170 m_pan = pan;
Chris@1177 171 update();
Chris@940 172 }
Chris@940 173 }
Chris@940 174
Chris@1177 175 float
Chris@1177 176 LevelPanWidget::getPan() const
Chris@1177 177 {
Chris@1177 178 return panToAudioPan(m_pan);
Chris@1177 179 }
Chris@1177 180
Chris@923 181 void
Chris@1177 182 LevelPanWidget::setMonitoringLevels(float left, float right)
Chris@923 183 {
Chris@1177 184 m_monitorLeft = left;
Chris@1177 185 m_monitorRight = right;
Chris@923 186 update();
Chris@923 187 }
Chris@923 188
Chris@940 189 bool
Chris@940 190 LevelPanWidget::isEditable() const
Chris@940 191 {
Chris@940 192 return m_editable;
Chris@940 193 }
Chris@940 194
Chris@940 195 bool
Chris@940 196 LevelPanWidget::includesMute() const
Chris@940 197 {
Chris@940 198 return m_includeMute;
Chris@940 199 }
Chris@940 200
Chris@923 201 void
Chris@923 202 LevelPanWidget::setEditable(bool editable)
Chris@923 203 {
Chris@923 204 m_editable = editable;
Chris@923 205 update();
Chris@923 206 }
Chris@923 207
Chris@940 208 void
Chris@940 209 LevelPanWidget::setIncludeMute(bool include)
Chris@923 210 {
Chris@940 211 m_includeMute = include;
Chris@1301 212 if (m_includeMute) {
Chris@1301 213 m_minNotch = 0;
Chris@1301 214 } else {
Chris@1301 215 m_minNotch = 1;
Chris@1301 216 }
Chris@940 217 emitLevelChanged();
Chris@940 218 update();
Chris@923 219 }
Chris@923 220
Chris@923 221 void
Chris@923 222 LevelPanWidget::emitLevelChanged()
Chris@923 223 {
Chris@923 224 emit levelChanged(getLevel());
Chris@923 225 }
Chris@923 226
Chris@923 227 void
Chris@923 228 LevelPanWidget::emitPanChanged()
Chris@923 229 {
Chris@923 230 emit panChanged(getPan());
Chris@923 231 }
Chris@923 232
Chris@923 233 void
Chris@923 234 LevelPanWidget::mousePressEvent(QMouseEvent *e)
Chris@923 235 {
Chris@1249 236 if (e->button() == Qt::MidButton ||
Chris@1249 237 ((e->button() == Qt::LeftButton) &&
Chris@1249 238 (e->modifiers() & Qt::ControlModifier))) {
Chris@1249 239 setToDefault();
Chris@1249 240 } else if (e->button() == Qt::LeftButton) {
Chris@1249 241 m_editing = true;
Chris@1249 242 mouseMoveEvent(e);
Chris@1249 243 }
Chris@1249 244 }
Chris@1249 245
Chris@1249 246 void
Chris@1249 247 LevelPanWidget::mouseReleaseEvent(QMouseEvent *e)
Chris@1249 248 {
Chris@923 249 mouseMoveEvent(e);
Chris@1249 250 m_editing = false;
Chris@923 251 }
Chris@923 252
Chris@923 253 void
Chris@923 254 LevelPanWidget::mouseMoveEvent(QMouseEvent *e)
Chris@923 255 {
Chris@923 256 if (!m_editable) return;
Chris@1249 257 if (!m_editing) return;
Chris@923 258
Chris@1301 259 int notch = coordsToNotch(rect(), e->pos());
Chris@1301 260 int pan = coordsToPan(rect(), e->pos());
Chris@1301 261
Chris@1301 262 if (notch == m_notch && pan == m_pan) {
Chris@1266 263 return;
Chris@923 264 }
Chris@1301 265 if (notch != m_notch) {
Chris@1301 266 m_notch = notch;
Chris@1266 267 emitLevelChanged();
Chris@923 268 }
Chris@923 269 if (pan != m_pan) {
Chris@1266 270 m_pan = pan;
Chris@1266 271 emitPanChanged();
Chris@923 272 }
Chris@923 273 update();
Chris@923 274 }
Chris@923 275
Chris@923 276 void
Chris@923 277 LevelPanWidget::wheelEvent(QWheelEvent *e)
Chris@923 278 {
Chris@923 279 if (e->modifiers() & Qt::ControlModifier) {
Chris@1266 280 if (e->delta() > 0) {
Chris@1266 281 if (m_pan < maxPan) {
Chris@1266 282 ++m_pan;
Chris@1266 283 emitPanChanged();
Chris@1266 284 update();
Chris@1266 285 }
Chris@1266 286 } else {
Chris@1266 287 if (m_pan > -maxPan) {
Chris@1266 288 --m_pan;
Chris@1266 289 emitPanChanged();
Chris@1266 290 update();
Chris@1266 291 }
Chris@1266 292 }
Chris@923 293 } else {
Chris@1266 294 if (e->delta() > 0) {
Chris@1301 295 if (m_notch < m_maxNotch) {
Chris@1301 296 ++m_notch;
Chris@1266 297 emitLevelChanged();
Chris@1266 298 update();
Chris@1266 299 }
Chris@1266 300 } else {
Chris@1301 301 if (m_notch > m_minNotch) {
Chris@1301 302 --m_notch;
Chris@1266 303 emitLevelChanged();
Chris@1266 304 update();
Chris@1266 305 }
Chris@1266 306 }
Chris@923 307 }
Chris@923 308 }
Chris@923 309
Chris@1301 310 int
Chris@1301 311 LevelPanWidget::coordsToNotch(QRectF rect, QPointF loc) const
Chris@923 312 {
Chris@1301 313 double h = rect.height();
Chris@1301 314
Chris@1301 315 int nnotch = m_maxNotch + 1;
Chris@1301 316 double cell = h / nnotch;
Chris@1301 317
Chris@1301 318 int notch = int((h - (loc.y() - rect.y())) / cell);
Chris@1301 319 notch = clampNotch(notch);
Chris@1301 320
Chris@1301 321 return notch;
Chris@1301 322 }
Chris@1301 323
Chris@1301 324 int
Chris@1301 325 LevelPanWidget::coordsToPan(QRectF rect, QPointF loc) const
Chris@1301 326 {
Chris@1301 327 double w = rect.width();
Chris@929 328
Chris@923 329 int npan = maxPan * 2 + 1;
Chris@1301 330 double cell = w / npan;
Chris@929 331
Chris@1301 332 int pan = int((loc.x() - rect.x()) / cell) - maxPan;
Chris@923 333 if (pan < -maxPan) pan = -maxPan;
Chris@923 334 if (pan > maxPan) pan = maxPan;
Chris@1301 335
Chris@1301 336 return pan;
Chris@923 337 }
Chris@923 338
Chris@923 339 QSizeF
Chris@929 340 LevelPanWidget::cellSize(QRectF rect) const
Chris@923 341 {
Chris@929 342 double w = rect.width(), h = rect.height();
Chris@1301 343 int ncol = maxPan * 2 + 1;
Chris@1301 344 int nrow = m_maxNotch/2;
Chris@1301 345 double wcell = w / ncol, hcell = h / nrow;
Chris@923 346 return QSizeF(wcell, hcell);
Chris@923 347 }
Chris@923 348
Chris@923 349 QPointF
Chris@1301 350 LevelPanWidget::cellCentre(QRectF rect, int row, int col) const
Chris@923 351 {
Chris@929 352 QSizeF cs = cellSize(rect);
Chris@1301 353 return QPointF(rect.x() +
Chris@1301 354 cs.width() * (col + maxPan) + cs.width() / 2.,
Chris@1301 355 rect.y() + rect.height() -
Chris@1301 356 cs.height() * (row + 1) + cs.height() / 2.);
Chris@923 357 }
Chris@923 358
Chris@923 359 QSizeF
Chris@929 360 LevelPanWidget::cellLightSize(QRectF rect) const
Chris@923 361 {
Chris@923 362 double extent = 3. / 4.;
Chris@929 363 QSizeF cs = cellSize(rect);
Chris@923 364 double m = std::min(cs.width(), cs.height());
Chris@923 365 return QSizeF(m * extent, m * extent);
Chris@923 366 }
Chris@923 367
Chris@923 368 QRectF
Chris@1301 369 LevelPanWidget::cellLightRect(QRectF rect, int row, int col) const
Chris@923 370 {
Chris@929 371 QSizeF cls = cellLightSize(rect);
Chris@1301 372 QPointF cc = cellCentre(rect, row, col);
Chris@923 373 return QRectF(cc.x() - cls.width() / 2.,
Chris@1266 374 cc.y() - cls.height() / 2.,
Chris@1266 375 cls.width(),
Chris@1266 376 cls.height());
Chris@923 377 }
Chris@923 378
Chris@923 379 double
Chris@929 380 LevelPanWidget::thinLineWidth(QRectF rect) const
Chris@923 381 {
Chris@929 382 double tw = ceil(rect.width() / (maxPan * 2. * 10.));
Chris@1301 383 double th = ceil(rect.height() / (m_maxNotch/2 * 10.));
Chris@923 384 return std::min(th, tw);
Chris@923 385 }
Chris@923 386
Chris@1301 387 QRectF
Chris@1301 388 LevelPanWidget::cellOutlineRect(QRectF rect, int row, int col) const
Chris@941 389 {
Chris@1301 390 QRectF clr = cellLightRect(rect, row, col);
Chris@1301 391 double adj = thinLineWidth(rect)/2;
Chris@1301 392 return clr.adjusted(-adj, -adj, adj, adj);
Chris@1301 393 }
Chris@1301 394
Chris@1301 395 QColor
Chris@1301 396 LevelPanWidget::notchToColour(int notch) const
Chris@1301 397 {
Chris@1301 398 if (notch < 3) return Qt::black;
Chris@1301 399 if (notch < 5) return QColor(80, 0, 0);
Chris@1301 400 if (notch < 7) return QColor(160, 0, 0);
Chris@1301 401 if (notch < 9) return QColor(255, 0, 0);
Chris@1301 402 return QColor(255, 255, 0);
Chris@941 403 }
Chris@941 404
Chris@923 405 void
Chris@929 406 LevelPanWidget::renderTo(QPaintDevice *dev, QRectF rect, bool asIfEditable) const
Chris@923 407 {
Chris@929 408 QPainter paint(dev);
Chris@923 409
Chris@923 410 paint.setRenderHint(QPainter::Antialiasing, true);
Chris@923 411
Chris@923 412 QPen pen;
Chris@923 413
Chris@929 414 double thin = thinLineWidth(rect);
Chris@938 415
Chris@1301 416 QColor columnBackground = QColor(180, 180, 180);
Chris@1301 417 pen.setColor(columnBackground);
Chris@929 418 pen.setWidthF(cellLightSize(rect).width() + thin);
Chris@923 419 pen.setCapStyle(Qt::RoundCap);
Chris@923 420 paint.setPen(pen);
Chris@1177 421 paint.setBrush(Qt::NoBrush);
Chris@923 422
Chris@923 423 for (int pan = -maxPan; pan <= maxPan; ++pan) {
Chris@1301 424 paint.drawLine(cellCentre(rect, 0, pan),
Chris@1301 425 cellCentre(rect, m_maxNotch/2 - 1, pan));
Chris@924 426 }
Chris@924 427
Chris@1301 428 bool monitoring = (m_monitorLeft > 0.f || m_monitorRight > 0.f);
Chris@1301 429
Chris@1301 430 if (isEnabled()) {
Chris@1301 431 pen.setColor(Qt::black);
Chris@1301 432 } else {
Chris@1301 433 pen.setColor(Qt::darkGray);
Chris@1301 434 }
Chris@1301 435
Chris@1301 436 if (!asIfEditable && m_includeMute && m_notch == 0) {
Chris@1301 437 // The X for mute takes up the whole display when we're not
Chris@1301 438 // being rendered in editable style
Chris@1301 439 pen.setWidthF(thin * 2);
Chris@1301 440 pen.setCapStyle(Qt::RoundCap);
Chris@1301 441 paint.setPen(pen);
Chris@1301 442 paint.drawLine(cellCentre(rect, 0, -maxPan),
Chris@1301 443 cellCentre(rect, m_maxNotch/2 - 1, maxPan));
Chris@1301 444 paint.drawLine(cellCentre(rect, m_maxNotch/2 - 1, -maxPan),
Chris@1301 445 cellCentre(rect, 0, maxPan));
Chris@1301 446 } else {
Chris@1301 447 // the normal case
Chris@1301 448
Chris@1301 449 // pen a bit less thin than in theory, so that we can erase
Chris@1301 450 // semi-circles later without leaving a faint edge
Chris@1301 451 pen.setWidthF(thin * 0.8);
Chris@1301 452 pen.setCapStyle(Qt::FlatCap);
Chris@1301 453 paint.setPen(pen);
Chris@1301 454
Chris@1301 455 if (m_includeMute && m_notch == 0) {
Chris@1301 456 QRectF clr = cellLightRect(rect, 0, m_pan);
Chris@1301 457 paint.drawLine(clr.topLeft(), clr.bottomRight());
Chris@1301 458 paint.drawLine(clr.bottomLeft(), clr.topRight());
Chris@1301 459 } else {
Chris@1301 460 for (int notch = 1; notch <= m_notch; notch += 2) {
Chris@1301 461 if (isEnabled() && !monitoring) {
Chris@1301 462 paint.setBrush(notchToColour(notch));
Chris@1301 463 }
Chris@1301 464 QRectF clr = cellLightRect(rect, notch/2, m_pan);
Chris@1301 465 paint.drawEllipse(clr);
Chris@1301 466 }
Chris@1301 467 if (m_notch % 2 != 0) {
Chris@1301 468 QRectF clr = cellOutlineRect(rect, (m_notch-1)/2, m_pan);
Chris@1301 469 paint.save();
Chris@1301 470 paint.setPen(Qt::NoPen);
Chris@1301 471 paint.setBrush(columnBackground);
Chris@1301 472 paint.drawPie(clr, 0, 180 * 16);
Chris@1301 473 paint.restore();
Chris@1301 474 }
Chris@1301 475 }
Chris@1301 476 }
Chris@1301 477
Chris@1301 478 if (monitoring) {
Chris@1177 479 paint.setPen(Qt::NoPen);
Chris@1177 480 for (int pan = -maxPan; pan <= maxPan; ++pan) {
Chris@1177 481 float audioPan = panToAudioPan(pan);
Chris@1177 482 float audioLevel;
Chris@1177 483 if (audioPan < 0.f) {
Chris@1177 484 audioLevel = m_monitorLeft + m_monitorRight * (1.f + audioPan);
Chris@1177 485 } else {
Chris@1177 486 audioLevel = m_monitorRight + m_monitorLeft * (1.f - audioPan);
Chris@1177 487 }
Chris@1301 488 int notchHere = audioLevelToNotch(audioLevel);
Chris@1301 489 for (int notch = 1; notch <= notchHere; notch += 2) {
Chris@1301 490 paint.setBrush(notchToColour(notch));
Chris@1301 491 QRectF clr = cellLightRect(rect, (notch-1)/2, pan);
Chris@1301 492 double adj = thinLineWidth(rect)/2;
Chris@1301 493 clr = clr.adjusted(adj, adj, -adj, -adj);
Chris@1301 494 if (notch + 2 > notchHere && notchHere % 2 != 0) {
Chris@1301 495 paint.drawPie(clr, 180 * 16, 180 * 16);
Chris@1301 496 } else {
Chris@1301 497 paint.drawEllipse(clr);
Chris@1301 498 }
Chris@1177 499 }
Chris@1177 500 }
Chris@1177 501 paint.setPen(pen);
Chris@1177 502 paint.setBrush(Qt::NoBrush);
Chris@1177 503 }
Chris@923 504 }
Chris@923 505
Chris@929 506 void
Chris@929 507 LevelPanWidget::paintEvent(QPaintEvent *)
Chris@929 508 {
Chris@929 509 renderTo(this, rect(), m_editable);
Chris@929 510 }
Chris@923 511
Chris@1180 512 void
Chris@1180 513 LevelPanWidget::enterEvent(QEvent *e)
Chris@1180 514 {
Chris@1180 515 QWidget::enterEvent(e);
Chris@1180 516 emit mouseEntered();
Chris@1180 517 }
Chris@929 518
Chris@1180 519 void
Chris@1180 520 LevelPanWidget::leaveEvent(QEvent *e)
Chris@1180 521 {
Chris@1180 522 QWidget::enterEvent(e);
Chris@1180 523 emit mouseLeft();
Chris@1180 524 }
Chris@929 525