annotate layer/SliceLayer.cpp @ 197:6b023411087b

* Work on harmonising colour and scale ranges between types of layer * Add normalize options to colour 3d plot layer
author Chris Cannam
date Thu, 01 Feb 2007 14:31:28 +0000
parents 4a3bdde1ef13
children c2ed5014d4ff
rev   line source
Chris@133 1
Chris@133 2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@133 3
Chris@133 4 /*
Chris@133 5 Sonic Visualiser
Chris@133 6 An audio file viewer and annotation editor.
Chris@133 7 Centre for Digital Music, Queen Mary, University of London.
Chris@195 8 This file copyright 2006-2007 QMUL.
Chris@133 9
Chris@133 10 This program is free software; you can redistribute it and/or
Chris@133 11 modify it under the terms of the GNU General Public License as
Chris@133 12 published by the Free Software Foundation; either version 2 of the
Chris@133 13 License, or (at your option) any later version. See the file
Chris@133 14 COPYING included with this distribution for more information.
Chris@133 15 */
Chris@133 16
Chris@193 17 #include "SliceLayer.h"
Chris@133 18
Chris@133 19 #include "view/View.h"
Chris@153 20 #include "base/AudioLevel.h"
Chris@167 21 #include "base/RangeMapper.h"
Chris@195 22 #include "base/RealTime.h"
Chris@195 23
Chris@197 24 #include "ColourMapper.h"
Chris@195 25 #include "PaintAssistant.h"
Chris@133 26
Chris@133 27 #include <QPainter>
Chris@133 28 #include <QPainterPath>
Chris@133 29
Chris@193 30 SliceLayer::SliceLayer() :
Chris@193 31 m_sliceableModel(0),
Chris@153 32 m_colour(Qt::darkBlue),
Chris@197 33 m_colourMap(0),
Chris@153 34 m_energyScale(dBScale),
Chris@193 35 m_samplingMode(SamplePeak),
Chris@195 36 m_plotStyle(PlotSteps),
Chris@193 37 m_binScale(LinearBins),
Chris@153 38 m_normalize(false),
Chris@194 39 m_bias(false),
Chris@193 40 m_gain(1.0)
Chris@133 41 {
Chris@133 42 }
Chris@133 43
Chris@193 44 SliceLayer::~SliceLayer()
Chris@133 45 {
Chris@193 46
Chris@133 47 }
Chris@133 48
Chris@133 49 void
Chris@193 50 SliceLayer::setSliceableModel(const Model *model)
Chris@133 51 {
Chris@193 52 const DenseThreeDimensionalModel *sliceable =
Chris@193 53 dynamic_cast<const DenseThreeDimensionalModel *>(model);
Chris@193 54
Chris@193 55 if (model && !sliceable) {
Chris@193 56 std::cerr << "WARNING: SliceLayer::setSliceableModel(" << model
Chris@193 57 << "): model is not a DenseThreeDimensionalModel" << std::endl;
Chris@193 58 }
Chris@193 59
Chris@193 60 if (m_sliceableModel == sliceable) return;
Chris@193 61
Chris@193 62 m_sliceableModel = sliceable;
Chris@193 63
Chris@193 64 connect(m_sliceableModel, SIGNAL(modelChanged()),
Chris@193 65 this, SIGNAL(modelChanged()));
Chris@193 66
Chris@193 67 connect(m_sliceableModel, SIGNAL(modelChanged(size_t, size_t)),
Chris@193 68 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@193 69
Chris@193 70 connect(m_sliceableModel, SIGNAL(completionChanged()),
Chris@193 71 this, SIGNAL(modelCompletionChanged()));
Chris@193 72
Chris@193 73 emit modelReplaced();
Chris@153 74 }
Chris@153 75
Chris@153 76 void
Chris@193 77 SliceLayer::sliceableModelReplaced(const Model *orig, const Model *replacement)
Chris@153 78 {
Chris@193 79 std::cerr << "SliceLayer::sliceableModelReplaced(" << orig << ", " << replacement << ")" << std::endl;
Chris@153 80
Chris@193 81 if (orig == m_sliceableModel) {
Chris@193 82 setSliceableModel
Chris@193 83 (dynamic_cast<const DenseThreeDimensionalModel *>(replacement));
Chris@153 84 }
Chris@153 85 }
Chris@153 86
Chris@153 87 void
Chris@193 88 SliceLayer::modelAboutToBeDeleted(Model *m)
Chris@153 89 {
Chris@193 90 std::cerr << "SliceLayer::modelAboutToBeDeleted(" << m << ")" << std::endl;
Chris@153 91
Chris@193 92 if (m == m_sliceableModel) {
Chris@193 93 setSliceableModel(0);
Chris@153 94 }
Chris@133 95 }
Chris@133 96
Chris@133 97 void
Chris@193 98 SliceLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@133 99 {
Chris@193 100 if (!m_sliceableModel) return;
Chris@133 101
Chris@195 102 paint.save();
Chris@195 103 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@195 104
Chris@195 105 if (v->getViewManager() && v->getViewManager()->shouldShowScaleGuides()) {
Chris@195 106 if (!m_scalePoints.empty()) {
Chris@195 107 paint.setPen(QColor(240, 240, 240)); //!!! and dark background?
Chris@195 108 for (size_t i = 0; i < m_scalePoints.size(); ++i) {
Chris@195 109 paint.drawLine(0, m_scalePoints[i], rect.width(), m_scalePoints[i]);
Chris@195 110 }
Chris@195 111 }
Chris@195 112 }
Chris@195 113
Chris@195 114 paint.setPen(m_colour);
Chris@195 115
Chris@195 116 // int w = (v->width() * 2) / 3;
Chris@195 117 int xorigin = getVerticalScaleWidth(v, paint) + 1; //!!! (v->width() / 2) - (w / 2);
Chris@195 118 int w = v->width() - xorigin - 1;
Chris@133 119
Chris@195 120 int yorigin = v->height() - 20 - paint.fontMetrics().height() - 7;
Chris@195 121 int h = yorigin - paint.fontMetrics().height() - 8;
Chris@195 122 if (h < 0) return;
Chris@133 123
Chris@195 124 // int h = (v->height() * 3) / 4;
Chris@195 125 // int yorigin = (v->height() / 2) + (h / 2);
Chris@133 126
Chris@133 127 QPainterPath path;
Chris@133 128 float thresh = -80.f;
Chris@133 129
Chris@193 130 int mh = m_sliceableModel->getHeight();
Chris@133 131
Chris@193 132 float *values = new float[mh];
Chris@193 133 int divisor = 0;
Chris@193 134
Chris@193 135 for (size_t bin = 0; bin < mh; ++bin) {
Chris@193 136 values[bin] = 0.f;
Chris@193 137 }
Chris@193 138
Chris@193 139 size_t f0 = v->getCentreFrame();
Chris@193 140 int f0x = v->getXForFrame(f0);
Chris@195 141 f0 = v->getFrameForX(f0x);
Chris@193 142 size_t f1 = v->getFrameForX(f0x + 1);
Chris@195 143 if (f1 > f0) --f1;
Chris@193 144
Chris@193 145 size_t col0 = f0 / m_sliceableModel->getResolution();
Chris@193 146 size_t col1 = col0;
Chris@193 147 if (m_samplingMode != NearestSample) {
Chris@193 148 col1 = f1 / m_sliceableModel->getResolution();
Chris@193 149 }
Chris@195 150 f0 = col0 * m_sliceableModel->getResolution();
Chris@195 151 f1 = (col1 + 1) * m_sliceableModel->getResolution() - 1;
Chris@193 152
Chris@195 153 for (size_t col = col0; col <= col1; ++col) {
Chris@193 154 for (size_t bin = 0; bin < mh; ++bin) {
Chris@193 155 float value = m_sliceableModel->getValueAt(col, bin);
Chris@193 156 if (m_bias) value *= bin + 1;
Chris@193 157 if (m_samplingMode == SamplePeak) {
Chris@193 158 if (value > values[bin]) values[bin] = value;
Chris@193 159 } else {
Chris@193 160 values[bin] += value;
Chris@193 161 }
Chris@153 162 }
Chris@193 163 ++divisor;
Chris@193 164 }
Chris@193 165
Chris@193 166 float max = 0.f;
Chris@193 167 for (size_t bin = 0; bin < mh; ++bin) {
Chris@193 168 if (m_samplingMode == SampleMean) values[bin] /= divisor;
Chris@193 169 if (values[bin] > max) max = values[bin];
Chris@193 170 }
Chris@193 171 if (max != 0.f && m_normalize) {
Chris@193 172 for (size_t bin = 0; bin < mh; ++bin) {
Chris@193 173 values[bin] /= max;
Chris@193 174 }
Chris@193 175 }
Chris@193 176
Chris@193 177 float py = 0;
Chris@193 178 float nx = xorigin;
Chris@193 179
Chris@197 180 ColourMapper mapper(m_colourMap, 0, 1);
Chris@197 181
Chris@193 182 for (size_t bin = 0; bin < mh; ++bin) {
Chris@193 183
Chris@193 184 float x;
Chris@193 185
Chris@193 186 switch (m_binScale) {
Chris@193 187
Chris@193 188 case LinearBins:
Chris@193 189 x = nx;
Chris@193 190 nx = xorigin + (float(w) * (bin + 1)) / mh;
Chris@193 191 break;
Chris@193 192
Chris@193 193 case LogBins:
Chris@193 194 x = nx;
Chris@193 195 nx = xorigin + (float(w) * (log10f(bin + 2) - log10f(1))) /
Chris@193 196 (log10f(mh + 1) - log10f(1));
Chris@193 197 break;
Chris@193 198
Chris@193 199 case InvertedLogBins:
Chris@193 200 x = nx;
Chris@193 201 nx = xorigin + w - (float(w) * (log10f(mh - bin) - log10f(1))) /
Chris@193 202 (log10f(mh) - log10f(1));
Chris@193 203 break;
Chris@193 204 }
Chris@193 205
Chris@193 206 float value = values[bin];
Chris@193 207
Chris@193 208 value *= m_gain;
Chris@197 209 float norm = 0.f;
Chris@153 210 float y = 0.f;
Chris@153 211
Chris@153 212 switch (m_energyScale) {
Chris@153 213
Chris@153 214 case dBScale:
Chris@153 215 {
Chris@153 216 float db = thresh;
Chris@193 217 if (value > 0.f) db = 10.f * log10f(value);
Chris@153 218 if (db < thresh) db = thresh;
Chris@197 219 norm = (db - thresh) / -thresh;
Chris@197 220 y = yorigin - (float(h) * norm);
Chris@153 221 break;
Chris@153 222 }
Chris@153 223
Chris@153 224 case MeterScale:
Chris@197 225 y = AudioLevel::multiplier_to_preview(value, h);
Chris@197 226 norm = float(y) / float(h);
Chris@197 227 y = yorigin - y;
Chris@153 228 break;
Chris@153 229
Chris@153 230 default:
Chris@197 231 norm = value;
Chris@193 232 y = yorigin - (float(h) * value);
Chris@153 233 break;
Chris@153 234 }
Chris@133 235
Chris@193 236 if (m_plotStyle == PlotLines) {
Chris@193 237
Chris@193 238 if (bin == 0) {
Chris@193 239 path.moveTo(x, y);
Chris@193 240 } else {
Chris@193 241 path.lineTo(x, y);
Chris@193 242 }
Chris@193 243
Chris@193 244 } else if (m_plotStyle == PlotSteps) {
Chris@193 245
Chris@193 246 if (bin == 0) {
Chris@193 247 path.moveTo(x, y);
Chris@193 248 } else {
Chris@193 249 path.lineTo(x, y);
Chris@193 250 }
Chris@193 251 path.lineTo(nx, y);
Chris@193 252
Chris@193 253 } else if (m_plotStyle == PlotBlocks) {
Chris@193 254
Chris@193 255 path.moveTo(x, yorigin);
Chris@133 256 path.lineTo(x, y);
Chris@193 257 path.lineTo(nx, y);
Chris@193 258 path.lineTo(nx, yorigin);
Chris@193 259 path.lineTo(x, yorigin);
Chris@197 260
Chris@197 261 } else if (m_plotStyle == PlotFilledBlocks) {
Chris@197 262
Chris@197 263 paint.fillRect(QRectF(x, y, nx - x, yorigin - y), mapper.map(norm));
Chris@133 264 }
Chris@193 265
Chris@193 266 py = y;
Chris@133 267 }
Chris@133 268
Chris@197 269 if (m_plotStyle != PlotFilledBlocks) {
Chris@197 270 paint.drawPath(path);
Chris@197 271 }
Chris@133 272 paint.restore();
Chris@133 273
Chris@197 274 QPoint discard;
Chris@197 275
Chris@197 276 if (v->getViewManager() && v->getViewManager()->shouldShowFrameCount() &&
Chris@197 277 v->shouldIlluminateLocalFeatures(this, discard)) {
Chris@195 278
Chris@195 279 int sampleRate = m_sliceableModel->getSampleRate();
Chris@195 280
Chris@195 281 QString startText = QString("%1 / %2")
Chris@195 282 .arg(QString::fromStdString
Chris@195 283 (RealTime::frame2RealTime
Chris@195 284 (f0, sampleRate).toText(true)))
Chris@195 285 .arg(f0);
Chris@195 286
Chris@195 287 QString endText = QString(" %1 / %2")
Chris@195 288 .arg(QString::fromStdString
Chris@195 289 (RealTime::frame2RealTime
Chris@195 290 (f1, sampleRate).toText(true)))
Chris@195 291 .arg(f1);
Chris@195 292
Chris@195 293 QString durationText = QString("(%1 / %2) ")
Chris@195 294 .arg(QString::fromStdString
Chris@195 295 (RealTime::frame2RealTime
Chris@195 296 (f1 - f0 + 1, sampleRate).toText(true)))
Chris@195 297 .arg(f1 - f0 + 1);
Chris@195 298
Chris@195 299 v->drawVisibleText
Chris@195 300 (paint, xorigin + 5,
Chris@195 301 paint.fontMetrics().ascent() + 5,
Chris@195 302 startText, View::OutlinedText);
Chris@195 303
Chris@195 304 v->drawVisibleText
Chris@195 305 (paint, xorigin + 5,
Chris@195 306 paint.fontMetrics().ascent() + paint.fontMetrics().height() + 10,
Chris@195 307 endText, View::OutlinedText);
Chris@195 308
Chris@195 309 v->drawVisibleText
Chris@195 310 (paint, xorigin + 5,
Chris@195 311 paint.fontMetrics().ascent() + 2*paint.fontMetrics().height() + 15,
Chris@195 312 durationText, View::OutlinedText);
Chris@195 313 }
Chris@195 314
Chris@195 315 /*
Chris@195 316
Chris@195 317 QString frameRange;
Chris@195 318 if (f1 != f0) {
Chris@195 319 frameRange = QString("%1 - %2").arg(f0).arg(f1);
Chris@195 320 } else {
Chris@195 321 frameRange = QString("%1").arg(f0);
Chris@195 322 }
Chris@195 323
Chris@195 324 QString colRange;
Chris@195 325 if (col1 != col0) {
Chris@195 326 colRange = tr("%1 hops").arg(col1 - col0 + 1);
Chris@195 327 } else {
Chris@195 328 colRange = tr("1 hop");
Chris@195 329 }
Chris@195 330
Chris@195 331 if (v->getViewManager() && v->getViewManager()->shouldShowFrameCount()) {
Chris@195 332
Chris@195 333 v->drawVisibleText
Chris@195 334 (paint, xorigin + 5,
Chris@195 335 paint.fontMetrics().ascent() + 5,
Chris@195 336 frameRange, View::OutlinedText);
Chris@195 337
Chris@195 338 v->drawVisibleText
Chris@195 339 (paint, xorigin + 5,
Chris@195 340 paint.fontMetrics().ascent() + paint.fontMetrics().height() + 10,
Chris@195 341 colRange, View::OutlinedText);
Chris@195 342 }
Chris@195 343 */
Chris@195 344 }
Chris@195 345
Chris@195 346 int
Chris@195 347 SliceLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
Chris@195 348 {
Chris@195 349 if (m_energyScale == LinearScale) {
Chris@195 350 return paint.fontMetrics().width("0.0") + 13;
Chris@195 351 } else {
Chris@195 352 return std::max(paint.fontMetrics().width(tr("0dB")),
Chris@195 353 paint.fontMetrics().width(tr("-Inf"))) + 13;
Chris@195 354 }
Chris@195 355 }
Chris@195 356
Chris@195 357 void
Chris@195 358 SliceLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@195 359 {
Chris@195 360 float thresh = 0;
Chris@195 361 if (m_energyScale != LinearScale) {
Chris@195 362 thresh = AudioLevel::dB_to_multiplier(-80); //!!! thresh
Chris@195 363 }
Chris@195 364
Chris@195 365 // int h = (rect.height() * 3) / 4;
Chris@195 366 // int y = (rect.height() / 2) - (h / 2);
Chris@195 367
Chris@195 368 int yorigin = v->height() - 20 - paint.fontMetrics().height() - 6;
Chris@195 369 int h = yorigin - paint.fontMetrics().height() - 8;
Chris@195 370 if (h < 0) return;
Chris@195 371
Chris@195 372 QRect actual(rect.x(), rect.y() + yorigin - h, rect.width(), h);
Chris@195 373
Chris@195 374 PaintAssistant::paintVerticalLevelScale
Chris@195 375 (paint, actual, thresh, 1.0 / m_gain,
Chris@195 376 PaintAssistant::Scale(m_energyScale),
Chris@195 377 const_cast<std::vector<int> *>(&m_scalePoints));
Chris@133 378 }
Chris@133 379
Chris@153 380 Layer::PropertyList
Chris@193 381 SliceLayer::getProperties() const
Chris@153 382 {
Chris@153 383 PropertyList list;
Chris@153 384 list.push_back("Colour");
Chris@193 385 list.push_back("Plot Type");
Chris@193 386 list.push_back("Sampling Mode");
Chris@153 387 list.push_back("Scale");
Chris@153 388 list.push_back("Normalize");
Chris@153 389 list.push_back("Gain");
Chris@193 390 list.push_back("Bin Scale");
Chris@153 391
Chris@153 392 return list;
Chris@153 393 }
Chris@153 394
Chris@153 395 QString
Chris@193 396 SliceLayer::getPropertyLabel(const PropertyName &name) const
Chris@153 397 {
Chris@153 398 if (name == "Colour") return tr("Colour");
Chris@193 399 if (name == "Plot Type") return tr("Plot Type");
Chris@153 400 if (name == "Energy Scale") return tr("Scale");
Chris@153 401 if (name == "Normalize") return tr("Normalize");
Chris@153 402 if (name == "Gain") return tr("Gain");
Chris@193 403 if (name == "Sampling Mode") return tr("Sampling Mode");
Chris@193 404 if (name == "Bin Scale") return tr("Plot X Scale");
Chris@153 405 return "";
Chris@153 406 }
Chris@153 407
Chris@153 408 Layer::PropertyType
Chris@193 409 SliceLayer::getPropertyType(const PropertyName &name) const
Chris@153 410 {
Chris@153 411 if (name == "Gain") return RangeProperty;
Chris@153 412 if (name == "Normalize") return ToggleProperty;
Chris@153 413 return ValueProperty;
Chris@153 414 }
Chris@153 415
Chris@153 416 QString
Chris@193 417 SliceLayer::getPropertyGroupName(const PropertyName &name) const
Chris@153 418 {
Chris@153 419 if (name == "Scale" ||
Chris@153 420 name == "Normalize" ||
Chris@193 421 name == "Sampling Mode" ||
Chris@193 422 name == "Gain") return tr("Scale");
Chris@193 423 if (name == "Plot Type" ||
Chris@193 424 name == "Bin Scale") return tr("Plot Type");
Chris@153 425 return QString();
Chris@153 426 }
Chris@153 427
Chris@153 428 int
Chris@193 429 SliceLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@153 430 int *min, int *max) const
Chris@153 431 {
Chris@153 432 int deft = 0;
Chris@153 433
Chris@153 434 int garbage0, garbage1;
Chris@153 435 if (!min) min = &garbage0;
Chris@153 436 if (!max) max = &garbage1;
Chris@153 437
Chris@153 438 if (name == "Gain") {
Chris@153 439
Chris@153 440 *min = -50;
Chris@153 441 *max = 50;
Chris@153 442
Chris@193 443 std::cerr << "gain is " << m_gain << ", mode is " << m_samplingMode << std::endl;
Chris@193 444
Chris@153 445 deft = lrint(log10(m_gain) * 20.0);
Chris@153 446 if (deft < *min) deft = *min;
Chris@153 447 if (deft > *max) deft = *max;
Chris@153 448
Chris@153 449 } else if (name == "Normalize") {
Chris@153 450
Chris@153 451 deft = (m_normalize ? 1 : 0);
Chris@153 452
Chris@153 453 } else if (name == "Colour") {
Chris@153 454
Chris@197 455 if (m_plotStyle == PlotFilledBlocks) {
Chris@197 456
Chris@197 457 *min = 0;
Chris@197 458 *max = ColourMapper::getColourMapCount() - 1;
Chris@153 459
Chris@197 460 deft = m_colourMap;
Chris@197 461
Chris@197 462 } else {
Chris@197 463
Chris@197 464 *min = 0;
Chris@197 465 *max = 5;
Chris@197 466
Chris@197 467 if (m_colour == Qt::black) deft = 0;
Chris@197 468 else if (m_colour == Qt::darkRed) deft = 1;
Chris@197 469 else if (m_colour == Qt::darkBlue) deft = 2;
Chris@197 470 else if (m_colour == Qt::darkGreen) deft = 3;
Chris@197 471 else if (m_colour == QColor(200, 50, 255)) deft = 4;
Chris@197 472 else if (m_colour == QColor(255, 150, 50)) deft = 5;
Chris@197 473 }
Chris@153 474
Chris@153 475 } else if (name == "Scale") {
Chris@153 476
Chris@153 477 *min = 0;
Chris@153 478 *max = 2;
Chris@153 479
Chris@153 480 deft = (int)m_energyScale;
Chris@153 481
Chris@193 482 } else if (name == "Sampling Mode") {
Chris@153 483
Chris@153 484 *min = 0;
Chris@193 485 *max = 2;
Chris@193 486
Chris@193 487 deft = (int)m_samplingMode;
Chris@153 488
Chris@193 489 } else if (name == "Plot Type") {
Chris@193 490
Chris@193 491 *min = 0;
Chris@197 492 *max = 3;
Chris@193 493
Chris@193 494 deft = (int)m_plotStyle;
Chris@193 495
Chris@193 496 } else if (name == "Bin Scale") {
Chris@193 497
Chris@193 498 *min = 0;
Chris@197 499 // *max = 2;
Chris@197 500 *max = 1; // I don't think we really do want to offer inverted log
Chris@193 501
Chris@193 502 deft = (int)m_binScale;
Chris@193 503
Chris@153 504 } else {
Chris@153 505 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@153 506 }
Chris@153 507
Chris@153 508 return deft;
Chris@153 509 }
Chris@153 510
Chris@153 511 QString
Chris@193 512 SliceLayer::getPropertyValueLabel(const PropertyName &name,
Chris@153 513 int value) const
Chris@153 514 {
Chris@153 515 if (name == "Colour") {
Chris@197 516 if (m_plotStyle == PlotFilledBlocks) {
Chris@197 517 return ColourMapper::getColourMapName(value);
Chris@197 518 } else {
Chris@197 519 switch (value) {
Chris@197 520 default:
Chris@197 521 case 0: return tr("Black");
Chris@197 522 case 1: return tr("Red");
Chris@197 523 case 2: return tr("Blue");
Chris@197 524 case 3: return tr("Green");
Chris@197 525 case 4: return tr("Purple");
Chris@197 526 case 5: return tr("Orange");
Chris@197 527 }
Chris@153 528 }
Chris@153 529 }
Chris@153 530 if (name == "Scale") {
Chris@153 531 switch (value) {
Chris@153 532 default:
Chris@153 533 case 0: return tr("Linear");
Chris@153 534 case 1: return tr("Meter");
Chris@153 535 case 2: return tr("dB");
Chris@153 536 }
Chris@153 537 }
Chris@193 538 if (name == "Sampling Mode") {
Chris@153 539 switch (value) {
Chris@153 540 default:
Chris@193 541 case 0: return tr("Any");
Chris@193 542 case 1: return tr("Mean");
Chris@193 543 case 2: return tr("Peak");
Chris@193 544 }
Chris@193 545 }
Chris@193 546 if (name == "Plot Type") {
Chris@193 547 switch (value) {
Chris@193 548 default:
Chris@193 549 case 0: return tr("Lines");
Chris@193 550 case 1: return tr("Steps");
Chris@193 551 case 2: return tr("Blocks");
Chris@197 552 case 3: return tr("Colours");
Chris@193 553 }
Chris@193 554 }
Chris@193 555 if (name == "Bin Scale") {
Chris@193 556 switch (value) {
Chris@193 557 default:
Chris@193 558 case 0: return tr("Linear");
Chris@193 559 case 1: return tr("Log");
Chris@193 560 case 2: return tr("Rev Log");
Chris@153 561 }
Chris@153 562 }
Chris@153 563 return tr("<unknown>");
Chris@153 564 }
Chris@153 565
Chris@167 566 RangeMapper *
Chris@193 567 SliceLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 568 {
Chris@167 569 if (name == "Gain") {
Chris@167 570 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
Chris@167 571 }
Chris@167 572 return 0;
Chris@167 573 }
Chris@167 574
Chris@133 575 void
Chris@193 576 SliceLayer::setProperty(const PropertyName &name, int value)
Chris@133 577 {
Chris@153 578 if (name == "Gain") {
Chris@153 579 setGain(pow(10, float(value)/20.0));
Chris@153 580 } else if (name == "Colour") {
Chris@197 581 if (m_plotStyle == PlotFilledBlocks) {
Chris@197 582 setFillColourMap(value);
Chris@197 583 } else {
Chris@197 584 switch (value) {
Chris@197 585 default:
Chris@197 586 case 0: setBaseColour(Qt::black); break;
Chris@197 587 case 1: setBaseColour(Qt::darkRed); break;
Chris@197 588 case 2: setBaseColour(Qt::darkBlue); break;
Chris@197 589 case 3: setBaseColour(Qt::darkGreen); break;
Chris@197 590 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@197 591 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@197 592 }
Chris@153 593 }
Chris@153 594 } else if (name == "Scale") {
Chris@153 595 switch (value) {
Chris@153 596 default:
Chris@153 597 case 0: setEnergyScale(LinearScale); break;
Chris@153 598 case 1: setEnergyScale(MeterScale); break;
Chris@153 599 case 2: setEnergyScale(dBScale); break;
Chris@153 600 }
Chris@193 601 } else if (name == "Plot Type") {
Chris@193 602 setPlotStyle(PlotStyle(value));
Chris@193 603 } else if (name == "Sampling Mode") {
Chris@193 604 switch (value) {
Chris@193 605 default:
Chris@193 606 case 0: setSamplingMode(NearestSample); break;
Chris@193 607 case 1: setSamplingMode(SampleMean); break;
Chris@193 608 case 2: setSamplingMode(SamplePeak); break;
Chris@193 609 }
Chris@193 610 } else if (name == "Bin Scale") {
Chris@193 611 switch (value) {
Chris@193 612 default:
Chris@193 613 case 0: setBinScale(LinearBins); break;
Chris@193 614 case 1: setBinScale(LogBins); break;
Chris@193 615 case 2: setBinScale(InvertedLogBins); break;
Chris@193 616 }
Chris@153 617 } else if (name == "Normalize") {
Chris@153 618 setNormalize(value ? true : false);
Chris@153 619 }
Chris@153 620 }
Chris@153 621
Chris@153 622 void
Chris@193 623 SliceLayer::setBaseColour(QColor colour)
Chris@153 624 {
Chris@153 625 if (m_colour == colour) return;
Chris@153 626 m_colour = colour;
Chris@153 627 emit layerParametersChanged();
Chris@153 628 }
Chris@153 629
Chris@153 630 void
Chris@197 631 SliceLayer::setFillColourMap(int map)
Chris@197 632 {
Chris@197 633 if (m_colourMap == map) return;
Chris@197 634 m_colourMap = map;
Chris@197 635 emit layerParametersChanged();
Chris@197 636 }
Chris@197 637
Chris@197 638 void
Chris@193 639 SliceLayer::setEnergyScale(EnergyScale scale)
Chris@153 640 {
Chris@153 641 if (m_energyScale == scale) return;
Chris@153 642 m_energyScale = scale;
Chris@153 643 emit layerParametersChanged();
Chris@153 644 }
Chris@153 645
Chris@153 646 void
Chris@193 647 SliceLayer::setSamplingMode(SamplingMode mode)
Chris@153 648 {
Chris@193 649 if (m_samplingMode == mode) return;
Chris@193 650 m_samplingMode = mode;
Chris@153 651 emit layerParametersChanged();
Chris@153 652 }
Chris@153 653
Chris@153 654 void
Chris@193 655 SliceLayer::setPlotStyle(PlotStyle style)
Chris@153 656 {
Chris@193 657 if (m_plotStyle == style) return;
Chris@197 658 bool colourTypeChanged = (style == PlotFilledBlocks ||
Chris@197 659 m_plotStyle == PlotFilledBlocks);
Chris@193 660 m_plotStyle = style;
Chris@197 661 if (colourTypeChanged) {
Chris@197 662 emit layerParameterRangesChanged();
Chris@197 663 }
Chris@153 664 emit layerParametersChanged();
Chris@153 665 }
Chris@153 666
Chris@153 667 void
Chris@193 668 SliceLayer::setBinScale(BinScale scale)
Chris@153 669 {
Chris@193 670 if (m_binScale == scale) return;
Chris@193 671 m_binScale = scale;
Chris@153 672 emit layerParametersChanged();
Chris@153 673 }
Chris@153 674
Chris@153 675 void
Chris@193 676 SliceLayer::setNormalize(bool n)
Chris@153 677 {
Chris@153 678 if (m_normalize == n) return;
Chris@153 679 m_normalize = n;
Chris@153 680 emit layerParametersChanged();
Chris@153 681 }
Chris@153 682
Chris@153 683 void
Chris@193 684 SliceLayer::setGain(float gain)
Chris@153 685 {
Chris@153 686 if (m_gain == gain) return;
Chris@153 687 m_gain = gain;
Chris@153 688 emit layerParametersChanged();
Chris@153 689 }
Chris@153 690
Chris@153 691 QString
Chris@193 692 SliceLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@153 693 {
Chris@153 694 QString s;
Chris@153 695
Chris@153 696 s += QString("colour=\"%1\" "
Chris@197 697 "colourScheme=\"%2\" "
Chris@197 698 "energyScale=\"%3\" "
Chris@197 699 "samplingMode=\"%4\" "
Chris@197 700 "gain=\"%5\" "
Chris@197 701 "normalize=\"%6\"")
Chris@153 702 .arg(encodeColour(m_colour))
Chris@197 703 .arg(m_colourMap)
Chris@153 704 .arg(m_energyScale)
Chris@193 705 .arg(m_samplingMode)
Chris@153 706 .arg(m_gain)
Chris@153 707 .arg(m_normalize ? "true" : "false");
Chris@153 708
Chris@153 709 return Layer::toXmlString(indent, extraAttributes + " " + s);
Chris@153 710 }
Chris@153 711
Chris@153 712 void
Chris@193 713 SliceLayer::setProperties(const QXmlAttributes &attributes)
Chris@153 714 {
Chris@153 715 bool ok = false;
Chris@153 716
Chris@153 717 QString colourSpec = attributes.value("colour");
Chris@153 718 if (colourSpec != "") {
Chris@153 719 QColor colour(colourSpec);
Chris@153 720 if (colour.isValid()) {
Chris@153 721 setBaseColour(QColor(colourSpec));
Chris@153 722 }
Chris@153 723 }
Chris@153 724
Chris@153 725 EnergyScale scale = (EnergyScale)
Chris@153 726 attributes.value("energyScale").toInt(&ok);
Chris@153 727 if (ok) setEnergyScale(scale);
Chris@153 728
Chris@193 729 SamplingMode mode = (SamplingMode)
Chris@193 730 attributes.value("samplingMode").toInt(&ok);
Chris@193 731 if (ok) setSamplingMode(mode);
Chris@153 732
Chris@197 733 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 734 if (ok) setFillColourMap(colourMap);
Chris@197 735
Chris@153 736 float gain = attributes.value("gain").toFloat(&ok);
Chris@153 737 if (ok) setGain(gain);
Chris@153 738
Chris@153 739 bool normalize = (attributes.value("normalize").trimmed() == "true");
Chris@153 740 setNormalize(normalize);
Chris@133 741 }
Chris@133 742
Chris@133 743 bool
Chris@193 744 SliceLayer::getValueExtents(float &min, float &max, bool &logarithmic,
Chris@133 745 QString &units) const
Chris@133 746 {
Chris@133 747 return false;
Chris@133 748 }
Chris@133 749