annotate layer/SliceLayer.cpp @ 1484:b43ff1abdc02 by-id

Fix inability to set layer's model to None
author Chris Cannam
date Wed, 17 Jul 2019 11:20:08 +0100
parents dbff4b290bf0
children 0e971e3d93e2
rev   line source
Chris@133 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@133 2
Chris@133 3 /*
Chris@133 4 Sonic Visualiser
Chris@133 5 An audio file viewer and annotation editor.
Chris@133 6 Centre for Digital Music, Queen Mary, University of London.
Chris@195 7 This file copyright 2006-2007 QMUL.
Chris@133 8
Chris@133 9 This program is free software; you can redistribute it and/or
Chris@133 10 modify it under the terms of the GNU General Public License as
Chris@133 11 published by the Free Software Foundation; either version 2 of the
Chris@133 12 License, or (at your option) any later version. See the file
Chris@133 13 COPYING included with this distribution for more information.
Chris@133 14 */
Chris@133 15
Chris@193 16 #include "SliceLayer.h"
Chris@133 17
Chris@133 18 #include "view/View.h"
Chris@153 19 #include "base/AudioLevel.h"
Chris@167 20 #include "base/RangeMapper.h"
Chris@195 21 #include "base/RealTime.h"
Chris@376 22 #include "ColourMapper.h"
Chris@376 23 #include "ColourDatabase.h"
Chris@195 24
Chris@195 25 #include "PaintAssistant.h"
Chris@133 26
Chris@1383 27 #include "base/Profiler.h"
Chris@1383 28
Chris@133 29 #include <QPainter>
Chris@133 30 #include <QPainterPath>
Chris@316 31 #include <QTextStream>
Chris@316 32
Chris@133 33
Chris@193 34 SliceLayer::SliceLayer() :
Chris@1400 35 m_binAlignment(BinsSpanScalePoints),
Chris@1281 36 m_colourMap(int(ColourMapper::Ice)),
Chris@1362 37 m_colourInverted(false),
Chris@153 38 m_energyScale(dBScale),
Chris@198 39 m_samplingMode(SampleMean),
Chris@1233 40 m_plotStyle(PlotLines),
Chris@193 41 m_binScale(LinearBins),
Chris@153 42 m_normalize(false),
Chris@284 43 m_threshold(0.0),
Chris@284 44 m_initialThreshold(0.0),
Chris@198 45 m_gain(1.0),
Chris@1238 46 m_minbin(0),
Chris@1238 47 m_maxbin(0),
Chris@198 48 m_currentf0(0),
Chris@198 49 m_currentf1(0)
Chris@133 50 {
Chris@133 51 }
Chris@133 52
Chris@193 53 SliceLayer::~SliceLayer()
Chris@133 54 {
Chris@193 55
Chris@133 56 }
Chris@133 57
Chris@133 58 void
Chris@1472 59 SliceLayer::setSliceableModel(ModelId modelId)
Chris@133 60 {
Chris@1472 61 auto newModel = ModelById::getAs<DenseThreeDimensionalModel>(modelId);
Chris@1472 62
Chris@1472 63 if (!modelId.isNone() && !newModel) {
Chris@1472 64 throw std::logic_error("Not a DenseThreeDimensionalModel");
Chris@193 65 }
Chris@193 66
Chris@1472 67 if (m_sliceableModel == modelId) return;
Chris@1472 68 m_sliceableModel = modelId;
Chris@193 69
Chris@1472 70 if (newModel) {
Chris@1472 71 connectSignals(m_sliceableModel);
Chris@193 72
Chris@1472 73 if (m_minbin == 0 && m_maxbin == 0) {
Chris@1472 74 m_minbin = 0;
Chris@1472 75 m_maxbin = newModel->getHeight();
Chris@1472 76 }
Chris@1389 77 }
Chris@1238 78
Chris@193 79 emit modelReplaced();
Chris@1238 80 emit layerParametersChanged();
Chris@153 81 }
Chris@153 82
Chris@153 83 void
Chris@1472 84 SliceLayer::sliceableModelReplaced(ModelId orig, ModelId replacement)
Chris@153 85 {
Chris@587 86 SVDEBUG << "SliceLayer::sliceableModelReplaced(" << orig << ", " << replacement << ")" << endl;
Chris@153 87
Chris@193 88 if (orig == m_sliceableModel) {
Chris@1472 89 setSliceableModel(replacement);
Chris@153 90 }
Chris@153 91 }
Chris@1472 92 /*!!!
Chris@153 93 void
Chris@193 94 SliceLayer::modelAboutToBeDeleted(Model *m)
Chris@153 95 {
Chris@587 96 SVDEBUG << "SliceLayer::modelAboutToBeDeleted(" << m << ")" << endl;
Chris@153 97
Chris@193 98 if (m == m_sliceableModel) {
Chris@1408 99 setSliceableModel(nullptr);
Chris@153 100 }
Chris@133 101 }
Chris@1472 102 */
Chris@198 103 QString
Chris@918 104 SliceLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &p) const
Chris@198 105 {
Chris@199 106 int minbin, maxbin, range;
Chris@805 107 return getFeatureDescriptionAux(v, p, true, minbin, maxbin, range);
Chris@199 108 }
Chris@199 109
Chris@199 110 QString
Chris@918 111 SliceLayer::getFeatureDescriptionAux(LayerGeometryProvider *v, QPoint &p,
Chris@805 112 bool includeBinDescription,
Chris@805 113 int &minbin, int &maxbin, int &range) const
Chris@199 114 {
Chris@199 115 minbin = 0;
Chris@199 116 maxbin = 0;
Chris@1472 117
Chris@1472 118 auto sliceableModel =
Chris@1472 119 ModelById::getAs<DenseThreeDimensionalModel>(m_sliceableModel);
Chris@1472 120 if (!sliceableModel) return "";
Chris@198 121
Chris@1400 122 if (m_binAlignment == BinsSpanScalePoints) {
Chris@1400 123 minbin = int(getBinForX(v, p.x()));
Chris@1400 124 maxbin = int(getBinForX(v, p.x() + 1));
Chris@1400 125 } else {
Chris@1400 126 minbin = int(getBinForX(v, p.x()) + 0.5);
Chris@1400 127 maxbin = int(getBinForX(v, p.x() + 1) + 0.5);
Chris@1400 128 }
Chris@1238 129
Chris@1472 130 int mh = sliceableModel->getHeight();
Chris@199 131 if (minbin >= mh) minbin = mh - 1;
Chris@199 132 if (maxbin >= mh) maxbin = mh - 1;
Chris@199 133 if (minbin < 0) minbin = 0;
Chris@199 134 if (maxbin < 0) maxbin = 0;
Chris@198 135
Chris@1472 136 sv_samplerate_t sampleRate = sliceableModel->getSampleRate();
Chris@198 137
Chris@906 138 sv_frame_t f0 = m_currentf0;
Chris@906 139 sv_frame_t f1 = m_currentf1;
Chris@198 140
Chris@198 141 RealTime rt0 = RealTime::frame2RealTime(f0, sampleRate);
Chris@198 142 RealTime rt1 = RealTime::frame2RealTime(f1, sampleRate);
Chris@198 143
Chris@906 144 range = int(f1 - f0 + 1);
Chris@198 145
Chris@280 146 QString rtrangestr = QString("%1 s").arg((rt1 - rt0).toText().c_str());
Chris@280 147
Chris@199 148 if (includeBinDescription) {
Chris@198 149
Chris@1238 150 int i0 = minbin - m_minbin;
Chris@1238 151 int i1 = maxbin - m_minbin;
Chris@1238 152
Chris@906 153 float minvalue = 0.0;
Chris@1238 154 if (in_range_for(m_values, i0)) minvalue = m_values[i0];
Chris@198 155
Chris@199 156 float maxvalue = minvalue;
Chris@1238 157 if (in_range_for(m_values, i1)) maxvalue = m_values[i1];
Chris@1238 158
Chris@199 159 if (minvalue > maxvalue) std::swap(minvalue, maxvalue);
Chris@199 160
Chris@199 161 QString binstr;
Chris@199 162 if (maxbin != minbin) {
Chris@199 163 binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1);
Chris@199 164 } else {
Chris@199 165 binstr = QString("%1").arg(minbin+1);
Chris@199 166 }
Chris@199 167
Chris@199 168 QString valuestr;
Chris@199 169 if (maxvalue != minvalue) {
Chris@199 170 valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue);
Chris@199 171 } else {
Chris@199 172 valuestr = QString("%1").arg(minvalue);
Chris@199 173 }
Chris@199 174
Chris@280 175 QString description = tr("Time:\t%1 - %2\nRange:\t%3 samples (%4)\nBin:\t%5\n%6 value:\t%7")
Chris@199 176 .arg(QString::fromStdString(rt0.toText(true)))
Chris@199 177 .arg(QString::fromStdString(rt1.toText(true)))
Chris@199 178 .arg(range)
Chris@280 179 .arg(rtrangestr)
Chris@199 180 .arg(binstr)
Chris@199 181 .arg(m_samplingMode == NearestSample ? tr("First") :
Chris@199 182 m_samplingMode == SampleMean ? tr("Mean") : tr("Peak"))
Chris@199 183 .arg(valuestr);
Chris@199 184
Chris@199 185 return description;
Chris@199 186
Chris@199 187 } else {
Chris@199 188
Chris@280 189 QString description = tr("Time:\t%1 - %2\nRange:\t%3 samples (%4)")
Chris@199 190 .arg(QString::fromStdString(rt0.toText(true)))
Chris@199 191 .arg(QString::fromStdString(rt1.toText(true)))
Chris@280 192 .arg(range)
Chris@280 193 .arg(rtrangestr);
Chris@199 194
Chris@199 195 return description;
Chris@199 196 }
Chris@198 197 }
Chris@198 198
Chris@906 199 double
Chris@1238 200 SliceLayer::getXForBin(const LayerGeometryProvider *v, double bin) const
Chris@198 201 {
Chris@1386 202 return getXForScalePoint(v, bin, m_minbin, m_maxbin);
Chris@1386 203 }
Chris@1386 204
Chris@1386 205 double
Chris@1386 206 SliceLayer::getXForScalePoint(const LayerGeometryProvider *v,
Chris@1386 207 double p, double pmin, double pmax) const
Chris@1386 208 {
Chris@906 209 double x = 0;
Chris@198 210
Chris@1238 211 int pw = v->getPaintWidth();
Chris@1238 212 int origin = m_xorigins[v->getId()];
Chris@1238 213 int w = pw - origin;
Chris@1238 214 if (w < 1) w = 1;
Chris@1238 215
Chris@1399 216 if (pmax <= pmin) {
Chris@1399 217 pmax = pmin + 1.0;
Chris@1399 218 }
Chris@1399 219
Chris@1394 220 if (p < pmin) p = pmin;
Chris@1394 221 if (p > pmax) p = pmax;
Chris@1399 222
Chris@1394 223 if (m_binScale == LinearBins) {
Chris@1394 224 x = (w * (p - pmin)) / (pmax - pmin);
Chris@1394 225 } else {
Chris@198 226
Chris@1394 227 if (m_binScale == InvertedLogBins) {
Chris@1394 228 // stoopid
Chris@1394 229 p = pmax - p;
Chris@1394 230 }
Chris@198 231
Chris@1281 232 // The 0.8 here is an awkward compromise. Our x-coord is
Chris@1281 233 // proportional to log of bin number, with the x-coord "of a
Chris@1281 234 // bin" being that of the left edge of the bin range. We can't
Chris@1281 235 // start counting bins from 0, as that would give us x = -Inf
Chris@1281 236 // and hide the first bin entirely. But if we start from 1, we
Chris@1281 237 // are giving a lot of space to the first bin, which in most
Chris@1281 238 // display modes won't be used because the "point" location
Chris@1281 239 // for that bin is in the middle of it. Yet in some modes
Chris@1281 240 // we'll still want it. A compromise is to count our first bin
Chris@1281 241 // as "a bit less than 1", so that most of it is visible but a
Chris@1281 242 // bit is tactfully cropped at the left edge so it doesn't
Chris@1281 243 // take up so much space.
Chris@1394 244 const double origin = 0.8;
Chris@198 245
Chris@1394 246 // sometimes we are called with a pmin/pmax range that begins
Chris@1394 247 // before 0: in that situation, we shift everything along by
Chris@1394 248 // the difference between 0 and pmin before doing any other
Chris@1394 249 // calculations
Chris@1394 250 double reqdshift = 0.0;
Chris@1394 251 if (pmin < 0) reqdshift = -pmin;
Chris@1394 252
Chris@1394 253 double pminlog = log10(pmin + reqdshift + origin);
Chris@1394 254 double pmaxlog = log10(pmax + reqdshift + origin);
Chris@1394 255 double plog = log10(p + reqdshift + origin);
Chris@1394 256 x = (w * (plog - pminlog)) / (pmaxlog - pminlog);
Chris@1399 257 /*
Chris@1394 258 cerr << "getXForScalePoint(" << p << "): pmin = " << pmin
Chris@1394 259 << ", pmax = " << pmax << ", w = " << w
Chris@1394 260 << ", reqdshift = " << reqdshift
Chris@1394 261 << ", pminlog = " << pminlog << ", pmaxlog = " << pmaxlog
Chris@1394 262 << ", plog = " << plog
Chris@1394 263 << " -> x = " << x << endl;
Chris@1399 264 */
Chris@1394 265 if (m_binScale == InvertedLogBins) {
Chris@1394 266 // still stoopid
Chris@1394 267 x = w - x;
Chris@1394 268 }
Chris@198 269 }
Chris@1238 270
Chris@1238 271 return x + origin;
Chris@198 272 }
Chris@198 273
Chris@1238 274 double
Chris@1238 275 SliceLayer::getBinForX(const LayerGeometryProvider *v, double x) const
Chris@198 276 {
Chris@1386 277 return getScalePointForX(v, x, m_minbin, m_maxbin);
Chris@1386 278 }
Chris@198 279
Chris@1386 280 double
Chris@1386 281 SliceLayer::getScalePointForX(const LayerGeometryProvider *v,
Chris@1386 282 double x, double pmin, double pmax) const
Chris@1386 283 {
Chris@1386 284 double p = 0;
Chris@1386 285
Chris@1238 286 int pw = v->getPaintWidth();
Chris@1238 287 int origin = m_xorigins[v->getId()];
Chris@1238 288
Chris@1238 289 int w = pw - origin;
Chris@1238 290 if (w < 1) w = 1;
Chris@1238 291
Chris@1238 292 x = x - origin;
Chris@1238 293 if (x < 0) x = 0;
Chris@1256 294
Chris@1256 295 double eps = 1e-10;
Chris@198 296
Chris@1399 297 if (pmax <= pmin) {
Chris@1399 298 pmax = pmin + 1.0;
Chris@1399 299 }
Chris@1399 300
Chris@1394 301 if (m_binScale == LinearBins) {
Chris@1394 302 p = pmin + eps + (x * (pmax - pmin)) / w;
Chris@1394 303 } else {
Chris@198 304
Chris@1394 305 if (m_binScale == InvertedLogBins) {
Chris@1394 306 x = w - x;
Chris@1394 307 }
Chris@1394 308
Chris@1394 309 // See comments in getXForScalePoint
Chris@1394 310
Chris@1394 311 const double origin = 0.8;
Chris@1394 312 double reqdshift = 0.0;
Chris@1394 313 if (pmin < 0) reqdshift = -pmin;
Chris@1394 314
Chris@1394 315 double pminlog = log10(pmin + reqdshift + origin);
Chris@1394 316 double pmaxlog = log10(pmax + reqdshift + origin);
Chris@1394 317
Chris@1394 318 double plog = pminlog + eps + (x * (pmaxlog - pminlog)) / w;
Chris@1394 319 p = pow(10.0, plog) - reqdshift - origin;
Chris@1394 320
Chris@1394 321 if (m_binScale == InvertedLogBins) {
Chris@1394 322 p = pmax - p;
Chris@1394 323 }
Chris@198 324 }
Chris@198 325
Chris@1394 326 return p;
Chris@198 327 }
Chris@198 328
Chris@906 329 double
Chris@1238 330 SliceLayer::getYForValue(const LayerGeometryProvider *v, double value, double &norm) const
Chris@274 331 {
Chris@906 332 norm = 0.0;
Chris@274 333
Chris@1238 334 if (m_yorigins.find(v->getId()) == m_yorigins.end()) return 0;
Chris@274 335
Chris@274 336 value *= m_gain;
Chris@274 337
Chris@1238 338 int yorigin = m_yorigins[v->getId()];
Chris@1238 339 int h = m_heights[v->getId()];
Chris@906 340 double thresh = getThresholdDb();
Chris@274 341
Chris@906 342 double y = 0.0;
Chris@274 343
Chris@274 344 if (h <= 0) return y;
Chris@274 345
Chris@274 346 switch (m_energyScale) {
Chris@274 347
Chris@274 348 case dBScale:
Chris@274 349 {
Chris@906 350 double db = thresh;
Chris@906 351 if (value > 0.0) db = 10.0 * log10(fabs(value));
Chris@274 352 if (db < thresh) db = thresh;
Chris@274 353 norm = (db - thresh) / -thresh;
Chris@906 354 y = yorigin - (double(h) * norm);
Chris@274 355 break;
Chris@274 356 }
Chris@274 357
Chris@274 358 case MeterScale:
Chris@274 359 y = AudioLevel::multiplier_to_preview(value, h);
Chris@906 360 norm = double(y) / double(h);
Chris@274 361 y = yorigin - y;
Chris@274 362 break;
Chris@274 363
Chris@538 364 case AbsoluteScale:
Chris@906 365 value = fabs(value);
Chris@1264 366 #if (__GNUC__ >= 7)
Chris@1263 367 __attribute__ ((fallthrough));
Chris@1263 368 #endif
Chris@538 369
Chris@805 370 case LinearScale:
Chris@274 371 default:
Chris@538 372 norm = (value - m_threshold);
Chris@284 373 if (norm < 0) norm = 0;
Chris@906 374 y = yorigin - (double(h) * norm);
Chris@274 375 break;
Chris@274 376 }
Chris@274 377
Chris@274 378 return y;
Chris@274 379 }
Chris@274 380
Chris@906 381 double
Chris@1238 382 SliceLayer::getValueForY(const LayerGeometryProvider *v, double y) const
Chris@274 383 {
Chris@906 384 double value = 0.0;
Chris@274 385
Chris@1238 386 if (m_yorigins.find(v->getId()) == m_yorigins.end()) return value;
Chris@274 387
Chris@1238 388 int yorigin = m_yorigins[v->getId()];
Chris@1238 389 int h = m_heights[v->getId()];
Chris@906 390 double thresh = getThresholdDb();
Chris@274 391
Chris@274 392 if (h <= 0) return value;
Chris@274 393
Chris@274 394 y = yorigin - y;
Chris@274 395
Chris@274 396 switch (m_energyScale) {
Chris@274 397
Chris@274 398 case dBScale:
Chris@274 399 {
Chris@906 400 double db = ((y / h) * -thresh) + thresh;
Chris@906 401 value = pow(10.0, db/10.0);
Chris@274 402 break;
Chris@274 403 }
Chris@274 404
Chris@274 405 case MeterScale:
Chris@906 406 value = AudioLevel::preview_to_multiplier(int(lrint(y)), h);
Chris@274 407 break;
Chris@805 408
Chris@805 409 case LinearScale:
Chris@805 410 case AbsoluteScale:
Chris@274 411 default:
Chris@284 412 value = y / h + m_threshold;
Chris@274 413 }
Chris@274 414
Chris@274 415 return value / m_gain;
Chris@274 416 }
Chris@274 417
Chris@133 418 void
Chris@916 419 SliceLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@133 420 {
Chris@1472 421 auto sliceableModel =
Chris@1472 422 ModelById::getAs<DenseThreeDimensionalModel>(m_sliceableModel);
Chris@1472 423 if (!sliceableModel ||
Chris@1472 424 !sliceableModel->isOK() ||
Chris@1472 425 !sliceableModel->isReady()) return;
Chris@133 426
Chris@1383 427 Profiler profiler("SliceLayer::paint()");
Chris@1383 428
Chris@195 429 paint.save();
Chris@1383 430 paint.setRenderHint(QPainter::Antialiasing, true);
Chris@538 431 paint.setBrush(Qt::NoBrush);
Chris@195 432
Chris@195 433 if (v->getViewManager() && v->getViewManager()->shouldShowScaleGuides()) {
Chris@195 434 if (!m_scalePoints.empty()) {
Chris@195 435 paint.setPen(QColor(240, 240, 240)); //!!! and dark background?
Chris@1284 436 int ratio = int(round(double(v->getPaintHeight()) /
Chris@1284 437 m_scalePaintHeight));
Chris@805 438 for (int i = 0; i < (int)m_scalePoints.size(); ++i) {
Chris@1284 439 paint.drawLine(0, m_scalePoints[i] * ratio,
Chris@1284 440 rect.width(), m_scalePoints[i] * ratio);
Chris@195 441 }
Chris@195 442 }
Chris@195 443 }
Chris@195 444
Chris@1472 445 int mh = sliceableModel->getHeight();
Chris@1389 446 int bin0 = 0;
Chris@1389 447 if (m_maxbin > m_minbin) {
Chris@1389 448 mh = m_maxbin - m_minbin;
Chris@1389 449 bin0 = m_minbin;
Chris@1389 450 }
Chris@1387 451
Chris@1233 452 if (m_plotStyle == PlotBlocks) {
Chris@1233 453 // Must use actual zero-width pen, too slow otherwise
Chris@1233 454 paint.setPen(QPen(getBaseQColor(), 0));
Chris@1233 455 } else {
Chris@1389 456 // Similarly, if there are very many bins here, we use a
Chris@1389 457 // thinner pen
Chris@1400 458 QPen pen;
Chris@1389 459 if (mh < 10000) {
Chris@1401 460 pen = v->scalePen(QPen(getBaseQColor(), 0.8));
Chris@1400 461 } else {
Chris@1400 462 pen = QPen(getBaseQColor(), 1);
Chris@1387 463 }
Chris@1389 464 paint.setPen(pen);
Chris@1233 465 }
Chris@195 466
Chris@607 467 int xorigin = getVerticalScaleWidth(v, true, paint) + 1;
Chris@1238 468 m_xorigins[v->getId()] = xorigin; // for use in getFeatureDescription
Chris@133 469
Chris@1383 470 int yorigin = v->getPaintHeight() - getHorizontalScaleHeight(v, paint) -
Chris@1383 471 paint.fontMetrics().height();
Chris@195 472 int h = yorigin - paint.fontMetrics().height() - 8;
Chris@133 473
Chris@1238 474 m_yorigins[v->getId()] = yorigin; // for getYForValue etc
Chris@1238 475 m_heights[v->getId()] = h;
Chris@274 476
Chris@274 477 if (h <= 0) return;
Chris@274 478
Chris@133 479 QPainterPath path;
Chris@1238 480
Chris@193 481 int divisor = 0;
Chris@193 482
Chris@198 483 m_values.clear();
Chris@805 484 for (int bin = 0; bin < mh; ++bin) {
Chris@906 485 m_values.push_back(0.0);
Chris@193 486 }
Chris@193 487
Chris@906 488 sv_frame_t f0 = v->getCentreFrame();
Chris@193 489 int f0x = v->getXForFrame(f0);
Chris@195 490 f0 = v->getFrameForX(f0x);
Chris@906 491 sv_frame_t f1 = v->getFrameForX(f0x + 1);
Chris@195 492 if (f1 > f0) --f1;
Chris@193 493
Chris@682 494 // cerr << "centre frame " << v->getCentreFrame() << ", x " << f0x << ", f0 " << f0 << ", f1 " << f1 << endl;
Chris@274 495
Chris@1472 496 int res = sliceableModel->getResolution();
Chris@906 497 int col0 = int(f0 / res);
Chris@805 498 int col1 = col0;
Chris@906 499 if (m_samplingMode != NearestSample) col1 = int(f1 / res);
Chris@274 500 f0 = col0 * res;
Chris@274 501 f1 = (col1 + 1) * res - 1;
Chris@274 502
Chris@682 503 // cerr << "resolution " << res << ", col0 " << col0 << ", col1 " << col1 << ", f0 " << f0 << ", f1 " << f1 << endl;
Chris@1383 504 // cerr << "mh = " << mh << endl;
Chris@193 505
Chris@198 506 m_currentf0 = f0;
Chris@198 507 m_currentf1 = f1;
Chris@198 508
Chris@254 509 BiasCurve curve;
Chris@254 510 getBiasCurve(curve);
Chris@906 511 int cs = int(curve.size());
Chris@254 512
Chris@805 513 for (int col = col0; col <= col1; ++col) {
Chris@1383 514 DenseThreeDimensionalModel::Column column =
Chris@1472 515 sliceableModel->getColumn(col);
Chris@805 516 for (int bin = 0; bin < mh; ++bin) {
Chris@1383 517 float value = column[bin0 + bin];
Chris@254 518 if (bin < cs) value *= curve[bin];
Chris@193 519 if (m_samplingMode == SamplePeak) {
Chris@198 520 if (value > m_values[bin]) m_values[bin] = value;
Chris@193 521 } else {
Chris@198 522 m_values[bin] += value;
Chris@193 523 }
Chris@153 524 }
Chris@193 525 ++divisor;
Chris@193 526 }
Chris@193 527
Chris@906 528 float max = 0.0;
Chris@805 529 for (int bin = 0; bin < mh; ++bin) {
Chris@847 530 if (m_samplingMode == SampleMean && divisor > 0) {
Chris@906 531 m_values[bin] /= float(divisor);
Chris@847 532 }
Chris@198 533 if (m_values[bin] > max) max = m_values[bin];
Chris@193 534 }
Chris@906 535 if (max != 0.0 && m_normalize) {
Chris@805 536 for (int bin = 0; bin < mh; ++bin) {
Chris@198 537 m_values[bin] /= max;
Chris@193 538 }
Chris@193 539 }
Chris@193 540
Chris@1362 541 ColourMapper mapper(m_colourMap, m_colourInverted, 0, 1);
Chris@197 542
Chris@1383 543 double ytop = 0, ybottom = 0;
Chris@1383 544 bool firstBinOfPixel = true;
Chris@1384 545
Chris@1384 546 QColor prevColour = v->getBackground();
Chris@1384 547 double prevYtop = 0;
Chris@1383 548
Chris@1400 549 double xleft = -1, xmiddle = -1, xright = -1;
Chris@1400 550 double prevXmiddle = 0;
Chris@1400 551
Chris@805 552 for (int bin = 0; bin < mh; ++bin) {
Chris@193 553
Chris@1400 554 if (m_binAlignment == BinsSpanScalePoints) {
Chris@1400 555 if (xright >= 0) xleft = xright; // previous value of
Chris@1400 556 else xleft = getXForBin(v, bin0 + bin);
Chris@1400 557 xmiddle = getXForBin(v, bin0 + bin + 0.5);
Chris@1400 558 xright = getXForBin(v, bin0 + bin + 1);
Chris@1400 559 } else {
Chris@1400 560 if (xright >= 0) xleft = xright; // previous value of
Chris@1400 561 else xleft = getXForBin(v, bin0 + bin - 0.5);
Chris@1400 562 xmiddle = getXForBin(v, bin0 + bin);
Chris@1400 563 xright = getXForBin(v, bin0 + bin + 0.5);
Chris@1400 564 }
Chris@193 565
Chris@906 566 double value = m_values[bin];
Chris@906 567 double norm = 0.0;
Chris@1238 568 double y = getYForValue(v, value, norm);
Chris@133 569
Chris@1383 570 if (y < ytop || firstBinOfPixel) {
Chris@1383 571 ytop = y;
Chris@1383 572 }
Chris@1383 573 if (y > ybottom || firstBinOfPixel) {
Chris@1383 574 ybottom = y;
Chris@1383 575 }
Chris@193 576
Chris@1400 577 if (int(xright) != int(xleft) || bin+1 == mh) {
Chris@1383 578
Chris@1383 579 if (m_plotStyle == PlotLines) {
Chris@1383 580
Chris@1383 581 if (bin == 0) {
Chris@1400 582 path.moveTo(xmiddle, y);
Chris@1383 583 } else {
Chris@1383 584 if (ytop != ybottom) {
Chris@1400 585 path.lineTo(xmiddle, ybottom);
Chris@1400 586 path.lineTo(xmiddle, ytop);
Chris@1400 587 path.moveTo(xmiddle, ybottom);
Chris@1383 588 } else {
Chris@1400 589 path.lineTo(xmiddle, ytop);
Chris@1383 590 }
Chris@1383 591 }
Chris@1383 592
Chris@1383 593 } else if (m_plotStyle == PlotSteps) {
Chris@1383 594
Chris@1383 595 if (bin == 0) {
Chris@1400 596 path.moveTo(xleft, y);
Chris@1383 597 } else {
Chris@1400 598 path.lineTo(xleft, ytop);
Chris@1383 599 }
Chris@1400 600 path.lineTo(xright, ytop);
Chris@1383 601
Chris@1383 602 } else if (m_plotStyle == PlotBlocks) {
Chris@1383 603
Chris@1384 604 // work in pixel coords here, as we don't want the
Chris@1384 605 // vertical edges to be antialiased
Chris@1384 606
Chris@1400 607 path.moveTo(QPoint(int(xleft), int(yorigin)));
Chris@1400 608 path.lineTo(QPoint(int(xleft), int(ytop)));
Chris@1400 609 path.lineTo(QPoint(int(xright), int(ytop)));
Chris@1400 610 path.lineTo(QPoint(int(xright), int(yorigin)));
Chris@1400 611 path.lineTo(QPoint(int(xleft), int(yorigin)));
Chris@1383 612
Chris@1383 613 } else if (m_plotStyle == PlotFilledBlocks) {
Chris@1383 614
Chris@1384 615 QColor c = mapper.map(norm);
Chris@1384 616 paint.setPen(Qt::NoPen);
Chris@1384 617
Chris@1384 618 // work in pixel coords here, as we don't want the
Chris@1384 619 // vertical edges to be antialiased
Chris@1384 620
Chris@1400 621 if (xright > xleft + 1) {
Chris@1384 622
Chris@1384 623 QVector<QPoint> pp;
Chris@1384 624
Chris@1384 625 if (bin > 0) {
Chris@1384 626 paint.setBrush(prevColour);
Chris@1384 627 pp.clear();
Chris@1400 628 pp << QPoint(int(prevXmiddle), int(yorigin));
Chris@1400 629 pp << QPoint(int(prevXmiddle), int(prevYtop));
Chris@1400 630 pp << QPoint(int((xmiddle + prevXmiddle) / 2),
Chris@1384 631 int((ytop + prevYtop) / 2));
Chris@1400 632 pp << QPoint(int((xmiddle + prevXmiddle) / 2),
Chris@1384 633 int(yorigin));
Chris@1384 634 paint.drawConvexPolygon(QPolygon(pp));
Chris@1384 635
Chris@1384 636 paint.setBrush(c);
Chris@1384 637 pp.clear();
Chris@1400 638 pp << QPoint(int((xmiddle + prevXmiddle) / 2),
Chris@1384 639 int(yorigin));
Chris@1400 640 pp << QPoint(int((xmiddle + prevXmiddle) / 2),
Chris@1384 641 int((ytop + prevYtop) / 2));
Chris@1400 642 pp << QPoint(int(xmiddle), int(ytop));
Chris@1400 643 pp << QPoint(int(xmiddle), int(yorigin));
Chris@1384 644 paint.drawConvexPolygon(QPolygon(pp));
Chris@1384 645 }
Chris@1384 646
Chris@1384 647 prevColour = c;
Chris@1384 648 prevYtop = ytop;
Chris@1384 649
Chris@1384 650 } else {
Chris@1384 651
Chris@1400 652 paint.fillRect(QRect(int(xleft), int(ytop),
Chris@1400 653 int(xright) - int(xleft),
Chris@1384 654 int(yorigin) - int(ytop)),
Chris@1384 655 c);
Chris@1384 656 }
Chris@1400 657
Chris@1400 658 prevXmiddle = xmiddle;
Chris@193 659 }
Chris@193 660
Chris@1383 661 firstBinOfPixel = true;
Chris@193 662
Chris@1383 663 } else {
Chris@1383 664 firstBinOfPixel = false;
Chris@133 665 }
Chris@133 666 }
Chris@133 667
Chris@197 668 if (m_plotStyle != PlotFilledBlocks) {
Chris@197 669 paint.drawPath(path);
Chris@197 670 }
Chris@133 671 paint.restore();
Chris@195 672 }
Chris@195 673
Chris@195 674 int
Chris@918 675 SliceLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool, QPainter &paint) const
Chris@195 676 {
Chris@1472 677 // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
Chris@1472 678 // replacement (horizontalAdvance) was only added in Qt 5.11
Chris@1472 679 // which is too new for us
Chris@1472 680 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Chris@1472 681
Chris@1238 682 int width;
Chris@538 683 if (m_energyScale == LinearScale || m_energyScale == AbsoluteScale) {
Chris@1266 684 width = std::max(paint.fontMetrics().width("0.0") + 13,
Chris@1238 685 paint.fontMetrics().width("x10-10"));
Chris@195 686 } else {
Chris@1238 687 width = std::max(paint.fontMetrics().width(tr("0dB")),
Chris@1238 688 paint.fontMetrics().width(tr("-Inf"))) + 13;
Chris@195 689 }
Chris@1238 690 return width;
Chris@195 691 }
Chris@195 692
Chris@195 693 void
Chris@918 694 SliceLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect rect) const
Chris@195 695 {
Chris@906 696 double thresh = m_threshold;
Chris@538 697 if (m_energyScale != LinearScale && m_energyScale != AbsoluteScale) {
Chris@284 698 thresh = AudioLevel::dB_to_multiplier(getThresholdDb());
Chris@195 699 }
Chris@195 700
Chris@195 701 // int h = (rect.height() * 3) / 4;
Chris@195 702 // int y = (rect.height() / 2) - (h / 2);
Chris@195 703
Chris@1383 704 int yorigin = v->getPaintHeight() - getHorizontalScaleHeight(v, paint) -
Chris@1383 705 paint.fontMetrics().height();
Chris@195 706 int h = yorigin - paint.fontMetrics().height() - 8;
Chris@195 707 if (h < 0) return;
Chris@195 708
Chris@195 709 QRect actual(rect.x(), rect.y() + yorigin - h, rect.width(), h);
Chris@195 710
Chris@220 711 int mult = 1;
Chris@220 712
Chris@195 713 PaintAssistant::paintVerticalLevelScale
Chris@195 714 (paint, actual, thresh, 1.0 / m_gain,
Chris@195 715 PaintAssistant::Scale(m_energyScale),
Chris@220 716 mult,
Chris@195 717 const_cast<std::vector<int> *>(&m_scalePoints));
Chris@220 718
Chris@1284 719 // Ugly hack (but then everything about this scale drawing is a
Chris@1284 720 // bit ugly). In pixel-doubling hi-dpi scenarios, the scale is
Chris@1284 721 // painted at pixel-doubled resolution but we do explicit
Chris@1284 722 // pixel-doubling ourselves when painting the layer content. We
Chris@1284 723 // make a note of this here so that we can compare with the
Chris@1284 724 // equivalent dimension in the paint method when deciding where to
Chris@1284 725 // place scale continuation lines.
Chris@1284 726 m_scalePaintHeight = v->getPaintHeight();
Chris@1284 727
Chris@220 728 if (mult != 1 && mult != 0) {
Chris@906 729 int log = int(lrint(log10(mult)));
Chris@220 730 QString a = tr("x10");
Chris@220 731 QString b = QString("%1").arg(-log);
Chris@220 732 paint.drawText(3, 8 + paint.fontMetrics().ascent(), a);
Chris@220 733 paint.drawText(3 + paint.fontMetrics().width(a),
Chris@220 734 3 + paint.fontMetrics().ascent(), b);
Chris@220 735 }
Chris@133 736 }
Chris@133 737
Chris@1281 738 bool
Chris@1281 739 SliceLayer::hasLightBackground() const
Chris@1281 740 {
Chris@1281 741 if (usesSolidColour()) {
Chris@1362 742 ColourMapper mapper(m_colourMap, m_colourInverted, 0, 1);
Chris@1281 743 return mapper.hasLightBackground();
Chris@1281 744 } else {
Chris@1281 745 return SingleColourLayer::hasLightBackground();
Chris@1281 746 }
Chris@1281 747 }
Chris@1281 748
Chris@153 749 Layer::PropertyList
Chris@193 750 SliceLayer::getProperties() const
Chris@153 751 {
Chris@287 752 PropertyList list = SingleColourLayer::getProperties();
Chris@538 753 list.push_back("Bin Scale");
Chris@193 754 list.push_back("Plot Type");
Chris@153 755 list.push_back("Scale");
Chris@153 756 list.push_back("Normalize");
Chris@284 757 list.push_back("Threshold");
Chris@153 758 list.push_back("Gain");
Chris@153 759
Chris@153 760 return list;
Chris@153 761 }
Chris@153 762
Chris@153 763 QString
Chris@193 764 SliceLayer::getPropertyLabel(const PropertyName &name) const
Chris@153 765 {
Chris@193 766 if (name == "Plot Type") return tr("Plot Type");
Chris@290 767 if (name == "Scale") return tr("Scale");
Chris@153 768 if (name == "Normalize") return tr("Normalize");
Chris@284 769 if (name == "Threshold") return tr("Threshold");
Chris@153 770 if (name == "Gain") return tr("Gain");
Chris@193 771 if (name == "Sampling Mode") return tr("Sampling Mode");
Chris@538 772 if (name == "Bin Scale") return tr("Bin Scale");
Chris@287 773 return SingleColourLayer::getPropertyLabel(name);
Chris@153 774 }
Chris@153 775
Chris@335 776 QString
Chris@335 777 SliceLayer::getPropertyIconName(const PropertyName &name) const
Chris@335 778 {
Chris@335 779 if (name == "Normalize") return "normalise";
Chris@335 780 return "";
Chris@335 781 }
Chris@335 782
Chris@153 783 Layer::PropertyType
Chris@193 784 SliceLayer::getPropertyType(const PropertyName &name) const
Chris@153 785 {
Chris@153 786 if (name == "Gain") return RangeProperty;
Chris@153 787 if (name == "Normalize") return ToggleProperty;
Chris@284 788 if (name == "Threshold") return RangeProperty;
Chris@287 789 if (name == "Plot Type") return ValueProperty;
Chris@290 790 if (name == "Scale") return ValueProperty;
Chris@287 791 if (name == "Sampling Mode") return ValueProperty;
Chris@287 792 if (name == "Bin Scale") return ValueProperty;
Chris@1281 793 if (name == "Colour" && usesSolidColour()) return ColourMapProperty;
Chris@287 794 return SingleColourLayer::getPropertyType(name);
Chris@153 795 }
Chris@153 796
Chris@153 797 QString
Chris@193 798 SliceLayer::getPropertyGroupName(const PropertyName &name) const
Chris@153 799 {
Chris@153 800 if (name == "Scale" ||
Chris@153 801 name == "Normalize" ||
Chris@193 802 name == "Sampling Mode" ||
Chris@284 803 name == "Threshold" ||
Chris@193 804 name == "Gain") return tr("Scale");
Chris@193 805 if (name == "Plot Type" ||
Chris@538 806 name == "Bin Scale") return tr("Bins");
Chris@287 807 return SingleColourLayer::getPropertyGroupName(name);
Chris@153 808 }
Chris@153 809
Chris@153 810 int
Chris@193 811 SliceLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 812 int *min, int *max, int *deflt) const
Chris@153 813 {
Chris@216 814 int val = 0;
Chris@153 815
Chris@216 816 int garbage0, garbage1, garbage2;
Chris@153 817 if (!min) min = &garbage0;
Chris@153 818 if (!max) max = &garbage1;
Chris@248 819 if (!deflt) deflt = &garbage2;
Chris@153 820
Chris@153 821 if (name == "Gain") {
Chris@153 822
Chris@1266 823 *min = -50;
Chris@1266 824 *max = 50;
Chris@216 825 *deflt = 0;
Chris@153 826
Chris@1238 827 // cerr << "gain is " << m_gain << ", mode is " << m_samplingMode << endl;
Chris@193 828
Chris@1266 829 val = int(lrint(log10(m_gain) * 20.0));
Chris@1266 830 if (val < *min) val = *min;
Chris@1266 831 if (val > *max) val = *max;
Chris@153 832
Chris@284 833 } else if (name == "Threshold") {
Chris@284 834
Chris@1266 835 *min = -80;
Chris@1266 836 *max = 0;
Chris@284 837
Chris@906 838 *deflt = int(lrint(AudioLevel::multiplier_to_dB(m_initialThreshold)));
Chris@1266 839 if (*deflt < *min) *deflt = *min;
Chris@1266 840 if (*deflt > *max) *deflt = *max;
Chris@284 841
Chris@1266 842 val = int(lrint(AudioLevel::multiplier_to_dB(m_threshold)));
Chris@1266 843 if (val < *min) val = *min;
Chris@1266 844 if (val > *max) val = *max;
Chris@284 845
Chris@153 846 } else if (name == "Normalize") {
Chris@1266 847
Chris@1266 848 val = (m_normalize ? 1 : 0);
Chris@216 849 *deflt = 0;
Chris@153 850
Chris@1281 851 } else if (name == "Colour" && usesSolidColour()) {
Chris@197 852
Chris@287 853 *min = 0;
Chris@287 854 *max = ColourMapper::getColourMapCount() - 1;
Chris@1281 855 *deflt = int(ColourMapper::Ice);
Chris@287 856
Chris@287 857 val = m_colourMap;
Chris@153 858
Chris@153 859 } else if (name == "Scale") {
Chris@153 860
Chris@1266 861 *min = 0;
Chris@1266 862 *max = 3;
Chris@216 863 *deflt = (int)dBScale;
Chris@153 864
Chris@1266 865 val = (int)m_energyScale;
Chris@153 866
Chris@193 867 } else if (name == "Sampling Mode") {
Chris@153 868
Chris@1266 869 *min = 0;
Chris@1266 870 *max = 2;
Chris@216 871 *deflt = (int)SampleMean;
Chris@193 872
Chris@1266 873 val = (int)m_samplingMode;
Chris@153 874
Chris@193 875 } else if (name == "Plot Type") {
Chris@193 876
Chris@193 877 *min = 0;
Chris@197 878 *max = 3;
Chris@216 879 *deflt = (int)PlotSteps;
Chris@193 880
Chris@216 881 val = (int)m_plotStyle;
Chris@193 882
Chris@193 883 } else if (name == "Bin Scale") {
Chris@193 884
Chris@193 885 *min = 0;
Chris@198 886 *max = 2;
Chris@216 887 *deflt = (int)LinearBins;
Chris@198 888 // *max = 1; // I don't think we really do want to offer inverted log
Chris@193 889
Chris@216 890 val = (int)m_binScale;
Chris@193 891
Chris@153 892 } else {
Chris@1266 893 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@153 894 }
Chris@153 895
Chris@216 896 return val;
Chris@153 897 }
Chris@153 898
Chris@153 899 QString
Chris@193 900 SliceLayer::getPropertyValueLabel(const PropertyName &name,
Chris@1281 901 int value) const
Chris@153 902 {
Chris@1281 903 if (name == "Colour" && usesSolidColour()) {
Chris@1362 904 return ColourMapper::getColourMapLabel(value);
Chris@153 905 }
Chris@153 906 if (name == "Scale") {
Chris@1266 907 switch (value) {
Chris@1266 908 default:
Chris@1266 909 case 0: return tr("Linear");
Chris@1266 910 case 1: return tr("Meter");
Chris@1266 911 case 2: return tr("Log");
Chris@1266 912 case 3: return tr("Absolute");
Chris@1266 913 }
Chris@153 914 }
Chris@193 915 if (name == "Sampling Mode") {
Chris@1266 916 switch (value) {
Chris@1266 917 default:
Chris@1266 918 case 0: return tr("Any");
Chris@1266 919 case 1: return tr("Mean");
Chris@1266 920 case 2: return tr("Peak");
Chris@1266 921 }
Chris@193 922 }
Chris@193 923 if (name == "Plot Type") {
Chris@1266 924 switch (value) {
Chris@1266 925 default:
Chris@1266 926 case 0: return tr("Lines");
Chris@1266 927 case 1: return tr("Steps");
Chris@1266 928 case 2: return tr("Blocks");
Chris@1266 929 case 3: return tr("Colours");
Chris@1266 930 }
Chris@193 931 }
Chris@193 932 if (name == "Bin Scale") {
Chris@1266 933 switch (value) {
Chris@1266 934 default:
Chris@1266 935 case 0: return tr("Linear");
Chris@1266 936 case 1: return tr("Log");
Chris@1266 937 case 2: return tr("Rev Log");
Chris@1266 938 }
Chris@153 939 }
Chris@287 940 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@153 941 }
Chris@153 942
Chris@167 943 RangeMapper *
Chris@193 944 SliceLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 945 {
Chris@167 946 if (name == "Gain") {
Chris@167 947 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
Chris@167 948 }
Chris@284 949 if (name == "Threshold") {
Chris@284 950 return new LinearRangeMapper(-80, 0, -80, 0, tr("dB"));
Chris@284 951 }
Chris@287 952 return SingleColourLayer::getNewPropertyRangeMapper(name);
Chris@167 953 }
Chris@167 954
Chris@133 955 void
Chris@193 956 SliceLayer::setProperty(const PropertyName &name, int value)
Chris@133 957 {
Chris@153 958 if (name == "Gain") {
Chris@1266 959 setGain(powf(10, float(value)/20.0f));
Chris@284 960 } else if (name == "Threshold") {
Chris@1266 961 if (value == -80) setThreshold(0.0f);
Chris@1266 962 else setThreshold(float(AudioLevel::dB_to_multiplier(value)));
Chris@1281 963 } else if (name == "Colour" && usesSolidColour()) {
Chris@287 964 setFillColourMap(value);
Chris@153 965 } else if (name == "Scale") {
Chris@1266 966 switch (value) {
Chris@1266 967 default:
Chris@1266 968 case 0: setEnergyScale(LinearScale); break;
Chris@1266 969 case 1: setEnergyScale(MeterScale); break;
Chris@1266 970 case 2: setEnergyScale(dBScale); break;
Chris@1266 971 case 3: setEnergyScale(AbsoluteScale); break;
Chris@1266 972 }
Chris@193 973 } else if (name == "Plot Type") {
Chris@1266 974 setPlotStyle(PlotStyle(value));
Chris@193 975 } else if (name == "Sampling Mode") {
Chris@1266 976 switch (value) {
Chris@1266 977 default:
Chris@1266 978 case 0: setSamplingMode(NearestSample); break;
Chris@1266 979 case 1: setSamplingMode(SampleMean); break;
Chris@1266 980 case 2: setSamplingMode(SamplePeak); break;
Chris@1266 981 }
Chris@193 982 } else if (name == "Bin Scale") {
Chris@1266 983 switch (value) {
Chris@1266 984 default:
Chris@1266 985 case 0: setBinScale(LinearBins); break;
Chris@1266 986 case 1: setBinScale(LogBins); break;
Chris@1266 987 case 2: setBinScale(InvertedLogBins); break;
Chris@1266 988 }
Chris@153 989 } else if (name == "Normalize") {
Chris@1266 990 setNormalize(value ? true : false);
Chris@287 991 } else {
Chris@287 992 SingleColourLayer::setProperty(name, value);
Chris@153 993 }
Chris@153 994 }
Chris@153 995
Chris@153 996 void
Chris@197 997 SliceLayer::setFillColourMap(int map)
Chris@197 998 {
Chris@197 999 if (m_colourMap == map) return;
Chris@197 1000 m_colourMap = map;
Chris@197 1001 emit layerParametersChanged();
Chris@197 1002 }
Chris@197 1003
Chris@197 1004 void
Chris@193 1005 SliceLayer::setEnergyScale(EnergyScale scale)
Chris@153 1006 {
Chris@153 1007 if (m_energyScale == scale) return;
Chris@153 1008 m_energyScale = scale;
Chris@153 1009 emit layerParametersChanged();
Chris@153 1010 }
Chris@153 1011
Chris@153 1012 void
Chris@193 1013 SliceLayer::setSamplingMode(SamplingMode mode)
Chris@153 1014 {
Chris@193 1015 if (m_samplingMode == mode) return;
Chris@193 1016 m_samplingMode = mode;
Chris@153 1017 emit layerParametersChanged();
Chris@153 1018 }
Chris@153 1019
Chris@153 1020 void
Chris@193 1021 SliceLayer::setPlotStyle(PlotStyle style)
Chris@153 1022 {
Chris@193 1023 if (m_plotStyle == style) return;
Chris@197 1024 bool colourTypeChanged = (style == PlotFilledBlocks ||
Chris@197 1025 m_plotStyle == PlotFilledBlocks);
Chris@193 1026 m_plotStyle = style;
Chris@197 1027 if (colourTypeChanged) {
Chris@197 1028 emit layerParameterRangesChanged();
Chris@197 1029 }
Chris@153 1030 emit layerParametersChanged();
Chris@153 1031 }
Chris@153 1032
Chris@153 1033 void
Chris@193 1034 SliceLayer::setBinScale(BinScale scale)
Chris@153 1035 {
Chris@193 1036 if (m_binScale == scale) return;
Chris@193 1037 m_binScale = scale;
Chris@153 1038 emit layerParametersChanged();
Chris@153 1039 }
Chris@153 1040
Chris@153 1041 void
Chris@193 1042 SliceLayer::setNormalize(bool n)
Chris@153 1043 {
Chris@153 1044 if (m_normalize == n) return;
Chris@153 1045 m_normalize = n;
Chris@153 1046 emit layerParametersChanged();
Chris@153 1047 }
Chris@153 1048
Chris@153 1049 void
Chris@284 1050 SliceLayer::setThreshold(float thresh)
Chris@284 1051 {
Chris@284 1052 if (m_threshold == thresh) return;
Chris@284 1053 m_threshold = thresh;
Chris@284 1054 emit layerParametersChanged();
Chris@284 1055 }
Chris@284 1056
Chris@284 1057 void
Chris@193 1058 SliceLayer::setGain(float gain)
Chris@153 1059 {
Chris@153 1060 if (m_gain == gain) return;
Chris@153 1061 m_gain = gain;
Chris@153 1062 emit layerParametersChanged();
Chris@153 1063 }
Chris@153 1064
Chris@284 1065 float
Chris@284 1066 SliceLayer::getThresholdDb() const
Chris@284 1067 {
Chris@284 1068 if (m_threshold == 0.0) return -80.f;
Chris@906 1069 float db = float(AudioLevel::multiplier_to_dB(m_threshold));
Chris@284 1070 return db;
Chris@284 1071 }
Chris@284 1072
Chris@287 1073 int
Chris@287 1074 SliceLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 1075 {
Chris@287 1076 impose = false;
Chris@287 1077 return ColourDatabase::getInstance()->getColourIndex
Chris@287 1078 (QString(darkbg ? "Bright Blue" : "Blue"));
Chris@287 1079 }
Chris@287 1080
Chris@316 1081 void
Chris@316 1082 SliceLayer::toXml(QTextStream &stream,
Chris@316 1083 QString indent, QString extraAttributes) const
Chris@153 1084 {
Chris@153 1085 QString s;
Chris@153 1086
Chris@1362 1087 s += QString("energyScale=\"%1\" "
Chris@1362 1088 "samplingMode=\"%2\" "
Chris@1362 1089 "plotStyle=\"%3\" "
Chris@1362 1090 "binScale=\"%4\" "
Chris@1362 1091 "gain=\"%5\" "
Chris@1362 1092 "threshold=\"%6\" "
Chris@1362 1093 "normalize=\"%7\" %8 ")
Chris@1266 1094 .arg(m_energyScale)
Chris@193 1095 .arg(m_samplingMode)
Chris@598 1096 .arg(m_plotStyle)
Chris@598 1097 .arg(m_binScale)
Chris@153 1098 .arg(m_gain)
Chris@598 1099 .arg(m_threshold)
Chris@1238 1100 .arg(m_normalize ? "true" : "false")
Chris@1238 1101 .arg(QString("minbin=\"%1\" "
Chris@1238 1102 "maxbin=\"%2\"")
Chris@1238 1103 .arg(m_minbin)
Chris@1238 1104 .arg(m_maxbin));
Chris@153 1105
Chris@1362 1106 // New-style colour map attribute, by string id rather than by
Chris@1362 1107 // number
Chris@1362 1108
Chris@1362 1109 s += QString("fillColourMap=\"%1\" ")
Chris@1362 1110 .arg(ColourMapper::getColourMapId(m_colourMap));
Chris@1362 1111
Chris@1362 1112 // Old-style colour map attribute
Chris@1362 1113
Chris@1362 1114 s += QString("colourScheme=\"%1\" ")
Chris@1362 1115 .arg(ColourMapper::getBackwardCompatibilityColourMap(m_colourMap));
Chris@1362 1116
Chris@316 1117 SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s);
Chris@153 1118 }
Chris@153 1119
Chris@153 1120 void
Chris@193 1121 SliceLayer::setProperties(const QXmlAttributes &attributes)
Chris@153 1122 {
Chris@153 1123 bool ok = false;
Chris@153 1124
Chris@287 1125 SingleColourLayer::setProperties(attributes);
Chris@153 1126
Chris@153 1127 EnergyScale scale = (EnergyScale)
Chris@1266 1128 attributes.value("energyScale").toInt(&ok);
Chris@153 1129 if (ok) setEnergyScale(scale);
Chris@153 1130
Chris@193 1131 SamplingMode mode = (SamplingMode)
Chris@1266 1132 attributes.value("samplingMode").toInt(&ok);
Chris@193 1133 if (ok) setSamplingMode(mode);
Chris@153 1134
Chris@1362 1135 QString colourMapId = attributes.value("fillColourMap");
Chris@1362 1136 int colourMap = ColourMapper::getColourMapById(colourMapId);
Chris@1362 1137 if (colourMap >= 0) {
Chris@1362 1138 setFillColourMap(colourMap);
Chris@1362 1139 } else {
Chris@1362 1140 colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@1362 1141 if (ok && colourMap < ColourMapper::getColourMapCount()) {
Chris@1362 1142 setFillColourMap(colourMap);
Chris@1362 1143 }
Chris@1362 1144 }
Chris@197 1145
Chris@598 1146 PlotStyle s = (PlotStyle)
Chris@1266 1147 attributes.value("plotStyle").toInt(&ok);
Chris@598 1148 if (ok) setPlotStyle(s);
Chris@598 1149
Chris@598 1150 BinScale b = (BinScale)
Chris@1266 1151 attributes.value("binScale").toInt(&ok);
Chris@598 1152 if (ok) setBinScale(b);
Chris@598 1153
Chris@153 1154 float gain = attributes.value("gain").toFloat(&ok);
Chris@153 1155 if (ok) setGain(gain);
Chris@153 1156
Chris@598 1157 float threshold = attributes.value("threshold").toFloat(&ok);
Chris@598 1158 if (ok) setThreshold(threshold);
Chris@598 1159
Chris@153 1160 bool normalize = (attributes.value("normalize").trimmed() == "true");
Chris@153 1161 setNormalize(normalize);
Chris@1238 1162
Chris@1238 1163 bool alsoOk = false;
Chris@1238 1164
Chris@1238 1165 float min = attributes.value("minbin").toFloat(&ok);
Chris@1238 1166 float max = attributes.value("maxbin").toFloat(&alsoOk);
Chris@1238 1167 if (ok && alsoOk) setDisplayExtents(min, max);
Chris@133 1168 }
Chris@133 1169
Chris@133 1170 bool
Chris@1238 1171 SliceLayer::getValueExtents(double &min, double &max, bool &logarithmic,
Chris@1238 1172 QString &unit) const
Chris@133 1173 {
Chris@1472 1174 auto sliceableModel =
Chris@1472 1175 ModelById::getAs<DenseThreeDimensionalModel>(m_sliceableModel);
Chris@1472 1176 if (!sliceableModel) return false;
Chris@1238 1177
Chris@1238 1178 min = 0;
Chris@1472 1179 max = double(sliceableModel->getHeight());
Chris@1238 1180
Chris@1238 1181 logarithmic = (m_binScale == BinScale::LogBins);
Chris@1238 1182 unit = "";
Chris@1238 1183
Chris@1238 1184 return true;
Chris@133 1185 }
Chris@133 1186
Chris@1238 1187 bool
Chris@1238 1188 SliceLayer::getDisplayExtents(double &min, double &max) const
Chris@1238 1189 {
Chris@1472 1190 auto sliceableModel =
Chris@1472 1191 ModelById::getAs<DenseThreeDimensionalModel>(m_sliceableModel);
Chris@1472 1192 if (!sliceableModel) return false;
Chris@1238 1193
Chris@1472 1194 double hmax = double(sliceableModel->getHeight());
Chris@1238 1195
Chris@1238 1196 min = m_minbin;
Chris@1238 1197 max = m_maxbin;
Chris@1238 1198 if (max <= min) {
Chris@1238 1199 min = 0;
Chris@1238 1200 max = hmax;
Chris@1238 1201 }
Chris@1238 1202 if (min < 0) min = 0;
Chris@1238 1203 if (max > hmax) max = hmax;
Chris@1238 1204
Chris@1238 1205 return true;
Chris@1238 1206 }
Chris@1238 1207
Chris@1238 1208 bool
Chris@1238 1209 SliceLayer::setDisplayExtents(double min, double max)
Chris@1238 1210 {
Chris@1472 1211 auto sliceableModel =
Chris@1472 1212 ModelById::getAs<DenseThreeDimensionalModel>(m_sliceableModel);
Chris@1472 1213 if (!sliceableModel) return false;
Chris@1238 1214
Chris@1238 1215 m_minbin = int(lrint(min));
Chris@1238 1216 m_maxbin = int(lrint(max));
Chris@1403 1217
Chris@1403 1218 if (m_minbin < 0) {
Chris@1403 1219 m_minbin = 0;
Chris@1403 1220 }
Chris@1403 1221 if (m_maxbin < 0) {
Chris@1403 1222 m_maxbin = 0;
Chris@1403 1223 }
Chris@1472 1224 if (m_minbin > sliceableModel->getHeight()) {
Chris@1472 1225 m_minbin = sliceableModel->getHeight();
Chris@1403 1226 }
Chris@1472 1227 if (m_maxbin > sliceableModel->getHeight()) {
Chris@1472 1228 m_maxbin = sliceableModel->getHeight();
Chris@1403 1229 }
Chris@1403 1230 if (m_maxbin < m_minbin) {
Chris@1403 1231 m_maxbin = m_minbin;
Chris@1403 1232 }
Chris@1403 1233
Chris@1238 1234 emit layerParametersChanged();
Chris@1238 1235 return true;
Chris@1238 1236 }
Chris@1238 1237
Chris@1238 1238 int
Chris@1238 1239 SliceLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@1238 1240 {
Chris@1472 1241 auto sliceableModel =
Chris@1472 1242 ModelById::getAs<DenseThreeDimensionalModel>(m_sliceableModel);
Chris@1472 1243 if (!sliceableModel) return 0;
Chris@1238 1244
Chris@1238 1245 defaultStep = 0;
Chris@1472 1246 int h = sliceableModel->getHeight();
Chris@1238 1247 return h;
Chris@1238 1248 }
Chris@1238 1249
Chris@1238 1250 int
Chris@1238 1251 SliceLayer::getCurrentVerticalZoomStep() const
Chris@1238 1252 {
Chris@1472 1253 auto sliceableModel =
Chris@1472 1254 ModelById::getAs<DenseThreeDimensionalModel>(m_sliceableModel);
Chris@1472 1255 if (!sliceableModel) return 0;
Chris@1238 1256
Chris@1238 1257 double min, max;
Chris@1238 1258 getDisplayExtents(min, max);
Chris@1472 1259 return sliceableModel->getHeight() - int(lrint(max - min));
Chris@1238 1260 }
Chris@1238 1261
Chris@1238 1262 void
Chris@1238 1263 SliceLayer::setVerticalZoomStep(int step)
Chris@1238 1264 {
Chris@1472 1265 auto sliceableModel =
Chris@1472 1266 ModelById::getAs<DenseThreeDimensionalModel>(m_sliceableModel);
Chris@1472 1267 if (!sliceableModel) return;
Chris@1238 1268
Chris@1238 1269 // SVDEBUG << "SliceLayer::setVerticalZoomStep(" <<step <<"): before: minbin = " << m_minbin << ", maxbin = " << m_maxbin << endl;
Chris@1238 1270
Chris@1472 1271 int dist = sliceableModel->getHeight() - step;
Chris@1238 1272 if (dist < 1) dist = 1;
Chris@1238 1273 double centre = m_minbin + (m_maxbin - m_minbin) / 2.0;
Chris@1403 1274 int minbin = int(lrint(centre - dist/2.0));
Chris@1403 1275 int maxbin = minbin + dist;
Chris@1403 1276 setDisplayExtents(minbin, maxbin);
Chris@1238 1277 }
Chris@1238 1278
Chris@1238 1279 RangeMapper *
Chris@1238 1280 SliceLayer::getNewVerticalZoomRangeMapper() const
Chris@1238 1281 {
Chris@1472 1282 auto sliceableModel =
Chris@1472 1283 ModelById::getAs<DenseThreeDimensionalModel>(m_sliceableModel);
Chris@1472 1284 if (!sliceableModel) return nullptr;
Chris@1238 1285
Chris@1472 1286 return new LinearRangeMapper(0, sliceableModel->getHeight(),
Chris@1472 1287 0, sliceableModel->getHeight(), "");
Chris@1238 1288 }
Chris@1389 1289
Chris@1389 1290 void
Chris@1389 1291 SliceLayer::zoomToRegion(const LayerGeometryProvider *v, QRect rect)
Chris@1389 1292 {
Chris@1389 1293 double bin0 = getBinForX(v, rect.x());
Chris@1389 1294 double bin1 = getBinForX(v, rect.x() + rect.width());
Chris@1389 1295
Chris@1389 1296 // ignore y for now...
Chris@1389 1297
Chris@1389 1298 SVDEBUG << "SliceLayer::zoomToRegion: zooming to bin range "
Chris@1389 1299 << bin0 << " -> " << bin1 << endl;
Chris@1389 1300
Chris@1389 1301 setDisplayExtents(floor(bin0), ceil(bin1));
Chris@1389 1302 }
Chris@1389 1303