annotate layer/SliceLayer.cpp @ 1385:37e9d6a1e00c spectrogramparam

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