annotate layer/SliceLayer.cpp @ 1389:1eb560b363e7 spectrogramparam

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