annotate layer/SliceLayer.cpp @ 198:c2ed5014d4ff

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