annotate widgets/LevelPanWidget.cpp @ 1176:125748a569fa more-hidpi

Another use of WidgetScale
author Chris Cannam
date Mon, 05 Dec 2016 14:51:25 +0000
parents d6acb8e36605
children 916b62baf7ac
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@940 33 static const int maxLevel = 4; // min is 0, may be mute or not depending on m_includeMute
Chris@923 34 static const int maxPan = 2; // range is -maxPan to maxPan
Chris@923 35
Chris@923 36 LevelPanWidget::LevelPanWidget(QWidget *parent) :
Chris@923 37 QWidget(parent),
Chris@923 38 m_level(maxLevel),
Chris@923 39 m_pan(0),
Chris@940 40 m_editable(true),
Chris@940 41 m_includeMute(true)
Chris@923 42 {
Chris@923 43 }
Chris@923 44
Chris@923 45 LevelPanWidget::~LevelPanWidget()
Chris@923 46 {
Chris@923 47 }
Chris@923 48
Chris@929 49 QSize
Chris@929 50 LevelPanWidget::sizeHint() const
Chris@929 51 {
Chris@1176 52 return WidgetScale::scaleQSize(QSize(40, 40));
Chris@929 53 }
Chris@929 54
Chris@940 55 static int
Chris@940 56 db_to_level(double db)
Chris@940 57 {
Chris@940 58 // Only if !m_includeMute, otherwise AudioLevel is used.
Chris@940 59 // Levels are: +6 0 -6 -12 -20
Chris@940 60 assert(maxLevel == 4);
Chris@940 61 if (db > 3.) return 4;
Chris@940 62 else if (db > -3.) return 3;
Chris@940 63 else if (db > -9.) return 2;
Chris@940 64 else if (db > -16.) return 1;
Chris@940 65 else return 0;
Chris@940 66 }
Chris@940 67
Chris@940 68 static double
Chris@940 69 level_to_db(int level)
Chris@940 70 {
Chris@940 71 // Only if !m_includeMute, otherwise AudioLevel is used.
Chris@940 72 // Levels are: +6 0 -6 -12 -20
Chris@940 73 assert(maxLevel == 4);
Chris@940 74 if (level >= 4) return 6.;
Chris@940 75 else if (level == 3) return 0.;
Chris@940 76 else if (level == 2) return -6.;
Chris@940 77 else if (level == 1) return -12.;
Chris@940 78 else return -20.;
Chris@940 79 }
Chris@940 80
Chris@923 81 void
Chris@925 82 LevelPanWidget::setLevel(float flevel)
Chris@923 83 {
Chris@940 84 int level;
Chris@940 85 if (m_includeMute) {
Chris@940 86 level = AudioLevel::multiplier_to_fader
Chris@940 87 (flevel, maxLevel, AudioLevel::ShortFader);
Chris@940 88 } else {
Chris@940 89 level = db_to_level(AudioLevel::multiplier_to_dB(flevel));
Chris@940 90 }
Chris@925 91 if (level < 0) level = 0;
Chris@925 92 if (level > maxLevel) level = maxLevel;
Chris@925 93 if (level != m_level) {
Chris@925 94 m_level = level;
Chris@925 95 float convertsTo = getLevel();
Chris@925 96 if (fabsf(convertsTo - flevel) > 1e-5) {
Chris@925 97 emitLevelChanged();
Chris@925 98 }
Chris@925 99 update();
Chris@925 100 }
Chris@923 101 }
Chris@923 102
Chris@940 103 float
Chris@940 104 LevelPanWidget::getLevel() const
Chris@940 105 {
Chris@940 106 if (m_includeMute) {
Chris@940 107 return float(AudioLevel::fader_to_multiplier
Chris@940 108 (m_level, maxLevel, AudioLevel::ShortFader));
Chris@940 109 } else {
Chris@940 110 return float(AudioLevel::dB_to_multiplier(level_to_db(m_level)));
Chris@940 111 }
Chris@940 112 }
Chris@940 113
Chris@923 114 void
Chris@923 115 LevelPanWidget::setPan(float pan)
Chris@923 116 {
Chris@923 117 m_pan = int(round(pan * maxPan));
Chris@923 118 if (m_pan < -maxPan) m_pan = -maxPan;
Chris@923 119 if (m_pan > maxPan) m_pan = maxPan;
Chris@923 120 update();
Chris@923 121 }
Chris@923 122
Chris@940 123 bool
Chris@940 124 LevelPanWidget::isEditable() const
Chris@940 125 {
Chris@940 126 return m_editable;
Chris@940 127 }
Chris@940 128
Chris@940 129 bool
Chris@940 130 LevelPanWidget::includesMute() const
Chris@940 131 {
Chris@940 132 return m_includeMute;
Chris@940 133 }
Chris@940 134
Chris@923 135 void
Chris@923 136 LevelPanWidget::setEditable(bool editable)
Chris@923 137 {
Chris@923 138 m_editable = editable;
Chris@923 139 update();
Chris@923 140 }
Chris@923 141
Chris@940 142 void
Chris@940 143 LevelPanWidget::setIncludeMute(bool include)
Chris@923 144 {
Chris@940 145 m_includeMute = include;
Chris@940 146 emitLevelChanged();
Chris@940 147 update();
Chris@923 148 }
Chris@923 149
Chris@923 150 float
Chris@923 151 LevelPanWidget::getPan() const
Chris@923 152 {
Chris@923 153 return float(m_pan) / float(maxPan);
Chris@923 154 }
Chris@923 155
Chris@923 156 void
Chris@923 157 LevelPanWidget::emitLevelChanged()
Chris@923 158 {
Chris@923 159 cerr << "emitting levelChanged(" << getLevel() << ")" << endl;
Chris@923 160 emit levelChanged(getLevel());
Chris@923 161 }
Chris@923 162
Chris@923 163 void
Chris@923 164 LevelPanWidget::emitPanChanged()
Chris@923 165 {
Chris@923 166 cerr << "emitting panChanged(" << getPan() << ")" << endl;
Chris@923 167 emit panChanged(getPan());
Chris@923 168 }
Chris@923 169
Chris@923 170 void
Chris@923 171 LevelPanWidget::mousePressEvent(QMouseEvent *e)
Chris@923 172 {
Chris@923 173 mouseMoveEvent(e);
Chris@923 174 }
Chris@923 175
Chris@923 176 void
Chris@923 177 LevelPanWidget::mouseMoveEvent(QMouseEvent *e)
Chris@923 178 {
Chris@923 179 if (!m_editable) return;
Chris@923 180
Chris@923 181 int level, pan;
Chris@929 182 toCell(rect(), e->pos(), level, pan);
Chris@923 183 if (level == m_level && pan == m_pan) {
Chris@923 184 return;
Chris@923 185 }
Chris@923 186 if (level != m_level) {
Chris@923 187 m_level = level;
Chris@923 188 emitLevelChanged();
Chris@923 189 }
Chris@923 190 if (pan != m_pan) {
Chris@923 191 m_pan = pan;
Chris@923 192 emitPanChanged();
Chris@923 193 }
Chris@923 194 update();
Chris@923 195 }
Chris@923 196
Chris@923 197 void
Chris@923 198 LevelPanWidget::mouseReleaseEvent(QMouseEvent *e)
Chris@923 199 {
Chris@923 200 mouseMoveEvent(e);
Chris@923 201 }
Chris@923 202
Chris@923 203 void
Chris@923 204 LevelPanWidget::wheelEvent(QWheelEvent *e)
Chris@923 205 {
Chris@923 206 if (e->modifiers() & Qt::ControlModifier) {
Chris@923 207 if (e->delta() > 0) {
Chris@923 208 if (m_pan < maxPan) {
Chris@923 209 ++m_pan;
Chris@923 210 emitPanChanged();
Chris@923 211 update();
Chris@923 212 }
Chris@923 213 } else {
Chris@923 214 if (m_pan > -maxPan) {
Chris@923 215 --m_pan;
Chris@923 216 emitPanChanged();
Chris@923 217 update();
Chris@923 218 }
Chris@923 219 }
Chris@923 220 } else {
Chris@923 221 if (e->delta() > 0) {
Chris@923 222 if (m_level < maxLevel) {
Chris@923 223 ++m_level;
Chris@923 224 emitLevelChanged();
Chris@923 225 update();
Chris@923 226 }
Chris@923 227 } else {
Chris@923 228 if (m_level > 0) {
Chris@923 229 --m_level;
Chris@923 230 emitLevelChanged();
Chris@923 231 update();
Chris@923 232 }
Chris@923 233 }
Chris@923 234 }
Chris@923 235 }
Chris@923 236
Chris@923 237 void
Chris@929 238 LevelPanWidget::toCell(QRectF rect, QPointF loc, int &level, int &pan) const
Chris@923 239 {
Chris@929 240 double w = rect.width(), h = rect.height();
Chris@929 241
Chris@923 242 int npan = maxPan * 2 + 1;
Chris@924 243 int nlevel = maxLevel + 1;
Chris@929 244
Chris@924 245 double wcell = w / npan, hcell = h / nlevel;
Chris@929 246
Chris@932 247 level = int((h - (loc.y() - rect.y())) / hcell);
Chris@924 248 if (level < 0) level = 0;
Chris@923 249 if (level > maxLevel) level = maxLevel;
Chris@929 250
Chris@932 251 pan = int((loc.x() - rect.x()) / wcell) - maxPan;
Chris@923 252 if (pan < -maxPan) pan = -maxPan;
Chris@923 253 if (pan > maxPan) pan = maxPan;
Chris@923 254 }
Chris@923 255
Chris@923 256 QSizeF
Chris@929 257 LevelPanWidget::cellSize(QRectF rect) const
Chris@923 258 {
Chris@929 259 double w = rect.width(), h = rect.height();
Chris@923 260 int npan = maxPan * 2 + 1;
Chris@924 261 int nlevel = maxLevel + 1;
Chris@924 262 double wcell = w / npan, hcell = h / nlevel;
Chris@923 263 return QSizeF(wcell, hcell);
Chris@923 264 }
Chris@923 265
Chris@923 266 QPointF
Chris@929 267 LevelPanWidget::cellCentre(QRectF rect, int level, int pan) const
Chris@923 268 {
Chris@929 269 QSizeF cs = cellSize(rect);
Chris@932 270 return QPointF(rect.x() + cs.width() * (pan + maxPan) + cs.width() / 2.,
Chris@932 271 rect.y() + rect.height() - cs.height() * (level + 1) + cs.height() / 2.);
Chris@923 272 }
Chris@923 273
Chris@923 274 QSizeF
Chris@929 275 LevelPanWidget::cellLightSize(QRectF rect) const
Chris@923 276 {
Chris@923 277 double extent = 3. / 4.;
Chris@929 278 QSizeF cs = cellSize(rect);
Chris@923 279 double m = std::min(cs.width(), cs.height());
Chris@923 280 return QSizeF(m * extent, m * extent);
Chris@923 281 }
Chris@923 282
Chris@923 283 QRectF
Chris@929 284 LevelPanWidget::cellLightRect(QRectF rect, int level, int pan) const
Chris@923 285 {
Chris@929 286 QSizeF cls = cellLightSize(rect);
Chris@929 287 QPointF cc = cellCentre(rect, level, pan);
Chris@923 288 return QRectF(cc.x() - cls.width() / 2.,
Chris@923 289 cc.y() - cls.height() / 2.,
Chris@923 290 cls.width(),
Chris@923 291 cls.height());
Chris@923 292 }
Chris@923 293
Chris@923 294 double
Chris@929 295 LevelPanWidget::thinLineWidth(QRectF rect) const
Chris@923 296 {
Chris@929 297 double tw = ceil(rect.width() / (maxPan * 2. * 10.));
Chris@929 298 double th = ceil(rect.height() / (maxLevel * 10.));
Chris@923 299 return std::min(th, tw);
Chris@923 300 }
Chris@923 301
Chris@941 302 static QColor
Chris@941 303 level_to_colour(int level)
Chris@941 304 {
Chris@941 305 assert(maxLevel == 4);
Chris@941 306 if (level == 0) return Qt::black;
Chris@941 307 else if (level == 1) return QColor(80, 0, 0);
Chris@941 308 else if (level == 2) return QColor(160, 0, 0);
Chris@941 309 else if (level == 3) return QColor(255, 0, 0);
Chris@941 310 else return QColor(255, 255, 0);
Chris@941 311 }
Chris@941 312
Chris@923 313 void
Chris@929 314 LevelPanWidget::renderTo(QPaintDevice *dev, QRectF rect, bool asIfEditable) const
Chris@923 315 {
Chris@929 316 QPainter paint(dev);
Chris@923 317
Chris@923 318 paint.setRenderHint(QPainter::Antialiasing, true);
Chris@923 319
Chris@923 320 QPen pen;
Chris@923 321
Chris@929 322 double thin = thinLineWidth(rect);
Chris@938 323
Chris@923 324 pen.setColor(QColor(127, 127, 127, 127));
Chris@929 325 pen.setWidthF(cellLightSize(rect).width() + thin);
Chris@923 326 pen.setCapStyle(Qt::RoundCap);
Chris@923 327 paint.setPen(pen);
Chris@923 328
Chris@923 329 for (int pan = -maxPan; pan <= maxPan; ++pan) {
Chris@929 330 paint.drawLine(cellCentre(rect, 0, pan), cellCentre(rect, maxLevel, pan));
Chris@924 331 }
Chris@924 332
Chris@924 333 if (isEnabled()) {
Chris@924 334 pen.setColor(Qt::black);
Chris@924 335 } else {
Chris@924 336 pen.setColor(Qt::darkGray);
Chris@923 337 }
Chris@929 338
Chris@940 339 if (!asIfEditable && m_includeMute && m_level == 0) {
Chris@929 340 pen.setWidthF(thin * 2);
Chris@929 341 pen.setCapStyle(Qt::RoundCap);
Chris@929 342 paint.setPen(pen);
Chris@930 343 paint.drawLine(cellCentre(rect, 0, -maxPan),
Chris@930 344 cellCentre(rect, maxLevel, maxPan));
Chris@930 345 paint.drawLine(cellCentre(rect, maxLevel, -maxPan),
Chris@930 346 cellCentre(rect, 0, maxPan));
Chris@929 347 return;
Chris@929 348 }
Chris@923 349
Chris@923 350 pen.setWidthF(thin);
Chris@923 351 pen.setCapStyle(Qt::FlatCap);
Chris@923 352 paint.setPen(pen);
Chris@923 353
Chris@924 354 for (int level = 0; level <= m_level; ++level) {
Chris@924 355 if (isEnabled()) {
Chris@941 356 paint.setBrush(level_to_colour(level));
Chris@924 357 }
Chris@929 358 QRectF clr = cellLightRect(rect, level, m_pan);
Chris@940 359 if (m_includeMute && m_level == 0) {
Chris@924 360 paint.drawLine(clr.topLeft(), clr.bottomRight());
Chris@924 361 paint.drawLine(clr.bottomLeft(), clr.topRight());
Chris@924 362 } else {
Chris@924 363 paint.drawEllipse(clr);
Chris@924 364 }
Chris@923 365 }
Chris@923 366 }
Chris@923 367
Chris@929 368 void
Chris@929 369 LevelPanWidget::paintEvent(QPaintEvent *)
Chris@929 370 {
Chris@929 371 renderTo(this, rect(), m_editable);
Chris@929 372 }
Chris@923 373
Chris@929 374
Chris@929 375