annotate layer/Colour3DPlotLayer.cpp @ 527:fb8b1b2f7dea

* Fix crash when opening SV file when the audio data file can't be found and the user doesn't locate it
author Chris Cannam
date Thu, 19 Mar 2009 15:49:47 +0000
parents 43bd47468005
children 0bd0b517e795
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@182 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@0 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "Colour3DPlotLayer.h"
Chris@0 17
Chris@128 18 #include "view/View.h"
Chris@0 19 #include "base/Profiler.h"
Chris@197 20 #include "base/LogRange.h"
Chris@444 21 #include "base/RangeMapper.h"
Chris@376 22 #include "ColourMapper.h"
Chris@0 23
Chris@0 24 #include <QPainter>
Chris@0 25 #include <QImage>
Chris@0 26 #include <QRect>
Chris@316 27 #include <QTextStream>
Chris@0 28
Chris@0 29 #include <iostream>
Chris@0 30
Chris@0 31 #include <cassert>
Chris@0 32
Chris@353 33 //#define DEBUG_COLOUR_3D_PLOT_LAYER_PAINT 1
Chris@125 34
Chris@0 35
Chris@44 36 Colour3DPlotLayer::Colour3DPlotLayer() :
Chris@0 37 m_model(0),
Chris@159 38 m_cache(0),
Chris@469 39 m_peaksCache(0),
Chris@474 40 m_peakResolution(256),
Chris@461 41 m_cacheValidStart(0),
Chris@461 42 m_cacheValidEnd(0),
Chris@197 43 m_colourScale(LinearScale),
Chris@461 44 m_colourScaleSet(false),
Chris@197 45 m_colourMap(0),
Chris@197 46 m_normalizeColumns(false),
Chris@357 47 m_normalizeVisibleArea(false),
Chris@444 48 m_invertVertical(false),
Chris@465 49 m_opaque(false),
Chris@444 50 m_miny(0),
Chris@444 51 m_maxy(0)
Chris@0 52 {
Chris@44 53
Chris@0 54 }
Chris@0 55
Chris@0 56 Colour3DPlotLayer::~Colour3DPlotLayer()
Chris@0 57 {
Chris@461 58 delete m_cache;
Chris@469 59 delete m_peaksCache;
Chris@0 60 }
Chris@0 61
Chris@0 62 void
Chris@0 63 Colour3DPlotLayer::setModel(const DenseThreeDimensionalModel *model)
Chris@0 64 {
Chris@193 65 if (m_model == model) return;
Chris@193 66 const DenseThreeDimensionalModel *oldModel = m_model;
Chris@0 67 m_model = model;
Chris@0 68 if (!m_model || !m_model->isOK()) return;
Chris@0 69
Chris@320 70 connectSignals(m_model);
Chris@0 71
Chris@461 72 connect(m_model, SIGNAL(modelChanged()), this, SLOT(modelChanged()));
Chris@0 73 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@461 74 this, SLOT(modelChanged(size_t, size_t)));
Chris@0 75
Chris@474 76 m_peakResolution = 256;
Chris@474 77 if (model->getResolution() > 512) {
Chris@474 78 m_peakResolution = 16;
Chris@474 79 } else if (model->getResolution() > 128) {
Chris@474 80 m_peakResolution = 64;
Chris@474 81 } else if (model->getResolution() > 2) {
Chris@474 82 m_peakResolution = 128;
Chris@474 83 }
Chris@474 84 cacheInvalid();
Chris@474 85
Chris@0 86 emit modelReplaced();
Chris@193 87 emit sliceableModelReplaced(oldModel, model);
Chris@0 88 }
Chris@0 89
Chris@0 90 void
Chris@0 91 Colour3DPlotLayer::cacheInvalid()
Chris@0 92 {
Chris@469 93 delete m_cache;
Chris@469 94 delete m_peaksCache;
Chris@0 95 m_cache = 0;
Chris@469 96 m_peaksCache = 0;
Chris@461 97 m_cacheValidStart = 0;
Chris@461 98 m_cacheValidEnd = 0;
Chris@0 99 }
Chris@0 100
Chris@0 101 void
Chris@461 102 Colour3DPlotLayer::cacheInvalid(size_t startFrame, size_t endFrame)
Chris@0 103 {
Chris@461 104 if (!m_cache) return;
Chris@461 105
Chris@461 106 size_t modelResolution = m_model->getResolution();
Chris@461 107 size_t start = startFrame / modelResolution;
Chris@461 108 size_t end = endFrame / modelResolution + 1;
Chris@461 109 if (m_cacheValidStart < end) m_cacheValidStart = end;
Chris@461 110 if (m_cacheValidEnd > start) m_cacheValidEnd = start;
Chris@461 111 if (m_cacheValidStart > m_cacheValidEnd) m_cacheValidEnd = m_cacheValidStart;
Chris@461 112 }
Chris@461 113
Chris@461 114 void
Chris@461 115 Colour3DPlotLayer::modelChanged()
Chris@461 116 {
Chris@461 117 if (!m_colourScaleSet && m_colourScale == LinearScale) {
Chris@461 118 if (m_model) {
Chris@461 119 if (m_model->shouldUseLogValueScale()) {
Chris@461 120 setColourScale(LogScale);
Chris@461 121 } else {
Chris@461 122 m_colourScaleSet = true;
Chris@461 123 }
Chris@461 124 }
Chris@461 125 }
Chris@0 126 cacheInvalid();
Chris@0 127 }
Chris@0 128
Chris@461 129 void
Chris@461 130 Colour3DPlotLayer::modelChanged(size_t startFrame, size_t endFrame)
Chris@461 131 {
Chris@461 132 if (!m_colourScaleSet && m_colourScale == LinearScale) {
Chris@461 133 if (m_model && m_model->getWidth() > 50) {
Chris@461 134 if (m_model->shouldUseLogValueScale()) {
Chris@461 135 setColourScale(LogScale);
Chris@461 136 } else {
Chris@461 137 m_colourScaleSet = true;
Chris@461 138 }
Chris@461 139 }
Chris@461 140 }
Chris@461 141 cacheInvalid(startFrame, endFrame);
Chris@461 142 }
Chris@461 143
Chris@159 144 Layer::PropertyList
Chris@159 145 Colour3DPlotLayer::getProperties() const
Chris@159 146 {
Chris@159 147 PropertyList list;
Chris@197 148 list.push_back("Colour");
Chris@159 149 list.push_back("Colour Scale");
Chris@197 150 list.push_back("Normalize Columns");
Chris@197 151 list.push_back("Normalize Visible Area");
Chris@357 152 list.push_back("Invert Vertical Scale");
Chris@465 153 list.push_back("Opaque");
Chris@159 154 return list;
Chris@159 155 }
Chris@159 156
Chris@159 157 QString
Chris@159 158 Colour3DPlotLayer::getPropertyLabel(const PropertyName &name) const
Chris@159 159 {
Chris@197 160 if (name == "Colour") return tr("Colour");
Chris@197 161 if (name == "Colour Scale") return tr("Scale");
Chris@197 162 if (name == "Normalize Columns") return tr("Normalize Columns");
Chris@197 163 if (name == "Normalize Visible Area") return tr("Normalize Visible Area");
Chris@357 164 if (name == "Invert Vertical Scale") return tr("Invert Vertical Scale");
Chris@465 165 if (name == "Opaque") return tr("Always Opaque");
Chris@159 166 return "";
Chris@159 167 }
Chris@159 168
Chris@346 169 QString
Chris@346 170 Colour3DPlotLayer::getPropertyIconName(const PropertyName &name) const
Chris@346 171 {
Chris@346 172 if (name == "Normalize Columns") return "normalise-columns";
Chris@346 173 if (name == "Normalize Visible Area") return "normalise";
Chris@357 174 if (name == "Invert Vertical Scale") return "invert-vertical";
Chris@465 175 if (name == "Opaque") return "opaque";
Chris@346 176 return "";
Chris@346 177 }
Chris@346 178
Chris@159 179 Layer::PropertyType
Chris@159 180 Colour3DPlotLayer::getPropertyType(const PropertyName &name) const
Chris@159 181 {
Chris@197 182 if (name == "Normalize Columns") return ToggleProperty;
Chris@197 183 if (name == "Normalize Visible Area") return ToggleProperty;
Chris@357 184 if (name == "Invert Vertical Scale") return ToggleProperty;
Chris@465 185 if (name == "Opaque") return ToggleProperty;
Chris@159 186 return ValueProperty;
Chris@159 187 }
Chris@159 188
Chris@159 189 QString
Chris@159 190 Colour3DPlotLayer::getPropertyGroupName(const PropertyName &name) const
Chris@159 191 {
Chris@197 192 if (name == "Normalize Columns" ||
Chris@197 193 name == "Normalize Visible Area" ||
Chris@357 194 name == "Invert Vertical Scale" ||
Chris@197 195 name == "Colour Scale") return tr("Scale");
Chris@465 196 if (name == "Opaque" ||
Chris@465 197 name == "Colour") return tr("Colour");
Chris@159 198 return QString();
Chris@159 199 }
Chris@159 200
Chris@159 201 int
Chris@159 202 Colour3DPlotLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 203 int *min, int *max, int *deflt) const
Chris@159 204 {
Chris@216 205 int val = 0;
Chris@159 206
Chris@216 207 int garbage0, garbage1, garbage2;
Chris@159 208 if (!min) min = &garbage0;
Chris@159 209 if (!max) max = &garbage1;
Chris@216 210 if (!deflt) deflt = &garbage2;
Chris@159 211
Chris@159 212 if (name == "Colour Scale") {
Chris@159 213
Chris@159 214 *min = 0;
Chris@509 215 *max = 3;
Chris@216 216 *deflt = (int)LinearScale;
Chris@159 217
Chris@216 218 val = (int)m_colourScale;
Chris@159 219
Chris@197 220 } else if (name == "Colour") {
Chris@197 221
Chris@197 222 *min = 0;
Chris@197 223 *max = ColourMapper::getColourMapCount() - 1;
Chris@216 224 *deflt = 0;
Chris@197 225
Chris@216 226 val = m_colourMap;
Chris@197 227
Chris@197 228 } else if (name == "Normalize Columns") {
Chris@197 229
Chris@216 230 *deflt = 0;
Chris@216 231 val = (m_normalizeColumns ? 1 : 0);
Chris@197 232
Chris@197 233 } else if (name == "Normalize Visible Area") {
Chris@197 234
Chris@216 235 *deflt = 0;
Chris@216 236 val = (m_normalizeVisibleArea ? 1 : 0);
Chris@197 237
Chris@357 238 } else if (name == "Invert Vertical Scale") {
Chris@357 239
Chris@357 240 *deflt = 0;
Chris@357 241 val = (m_invertVertical ? 1 : 0);
Chris@357 242
Chris@465 243 } else if (name == "Opaque") {
Chris@465 244
Chris@465 245 *deflt = 0;
Chris@465 246 val = (m_opaque ? 1 : 0);
Chris@465 247
Chris@159 248 } else {
Chris@216 249 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@159 250 }
Chris@159 251
Chris@216 252 return val;
Chris@159 253 }
Chris@159 254
Chris@159 255 QString
Chris@159 256 Colour3DPlotLayer::getPropertyValueLabel(const PropertyName &name,
Chris@159 257 int value) const
Chris@159 258 {
Chris@197 259 if (name == "Colour") {
Chris@197 260 return ColourMapper::getColourMapName(value);
Chris@197 261 }
Chris@159 262 if (name == "Colour Scale") {
Chris@159 263 switch (value) {
Chris@159 264 default:
Chris@198 265 case 0: return tr("Linear");
Chris@198 266 case 1: return tr("Log");
Chris@198 267 case 2: return tr("+/-1");
Chris@509 268 case 3: return tr("Absolute");
Chris@159 269 }
Chris@159 270 }
Chris@159 271 return tr("<unknown>");
Chris@159 272 }
Chris@159 273
Chris@159 274 void
Chris@159 275 Colour3DPlotLayer::setProperty(const PropertyName &name, int value)
Chris@159 276 {
Chris@159 277 if (name == "Colour Scale") {
Chris@159 278 switch (value) {
Chris@159 279 default:
Chris@159 280 case 0: setColourScale(LinearScale); break;
Chris@197 281 case 1: setColourScale(LogScale); break;
Chris@197 282 case 2: setColourScale(PlusMinusOneScale); break;
Chris@509 283 case 3: setColourScale(AbsoluteScale); break;
Chris@159 284 }
Chris@197 285 } else if (name == "Colour") {
Chris@197 286 setColourMap(value);
Chris@197 287 } else if (name == "Normalize Columns") {
Chris@197 288 setNormalizeColumns(value ? true : false);
Chris@197 289 } else if (name == "Normalize Visible Area") {
Chris@197 290 setNormalizeVisibleArea(value ? true : false);
Chris@357 291 } else if (name == "Invert Vertical Scale") {
Chris@357 292 setInvertVertical(value ? true : false);
Chris@465 293 } else if (name == "Opaque") {
Chris@465 294 setOpaque(value ? true : false);
Chris@159 295 }
Chris@159 296 }
Chris@159 297
Chris@159 298 void
Chris@159 299 Colour3DPlotLayer::setColourScale(ColourScale scale)
Chris@159 300 {
Chris@159 301 if (m_colourScale == scale) return;
Chris@159 302 m_colourScale = scale;
Chris@461 303 m_colourScaleSet = true;
Chris@159 304 cacheInvalid();
Chris@159 305 emit layerParametersChanged();
Chris@159 306 }
Chris@159 307
Chris@197 308 void
Chris@197 309 Colour3DPlotLayer::setColourMap(int map)
Chris@197 310 {
Chris@197 311 if (m_colourMap == map) return;
Chris@197 312 m_colourMap = map;
Chris@197 313 cacheInvalid();
Chris@197 314 emit layerParametersChanged();
Chris@197 315 }
Chris@197 316
Chris@197 317 void
Chris@197 318 Colour3DPlotLayer::setNormalizeColumns(bool n)
Chris@197 319 {
Chris@197 320 if (m_normalizeColumns == n) return;
Chris@197 321 m_normalizeColumns = n;
Chris@197 322 cacheInvalid();
Chris@197 323 emit layerParametersChanged();
Chris@197 324 }
Chris@197 325
Chris@197 326 bool
Chris@197 327 Colour3DPlotLayer::getNormalizeColumns() const
Chris@197 328 {
Chris@197 329 return m_normalizeColumns;
Chris@197 330 }
Chris@197 331
Chris@197 332 void
Chris@197 333 Colour3DPlotLayer::setNormalizeVisibleArea(bool n)
Chris@197 334 {
Chris@197 335 if (m_normalizeVisibleArea == n) return;
Chris@197 336 m_normalizeVisibleArea = n;
Chris@197 337 cacheInvalid();
Chris@197 338 emit layerParametersChanged();
Chris@197 339 }
Chris@197 340
Chris@197 341 bool
Chris@197 342 Colour3DPlotLayer::getNormalizeVisibleArea() const
Chris@197 343 {
Chris@197 344 return m_normalizeVisibleArea;
Chris@197 345 }
Chris@197 346
Chris@357 347 void
Chris@357 348 Colour3DPlotLayer::setInvertVertical(bool n)
Chris@357 349 {
Chris@357 350 if (m_invertVertical == n) return;
Chris@357 351 m_invertVertical = n;
Chris@357 352 cacheInvalid();
Chris@357 353 emit layerParametersChanged();
Chris@357 354 }
Chris@357 355
Chris@465 356 void
Chris@465 357 Colour3DPlotLayer::setOpaque(bool n)
Chris@465 358 {
Chris@465 359 if (m_opaque == n) return;
Chris@465 360 m_opaque = n;
Chris@465 361 emit layerParametersChanged();
Chris@465 362 }
Chris@465 363
Chris@357 364 bool
Chris@357 365 Colour3DPlotLayer::getInvertVertical() const
Chris@357 366 {
Chris@357 367 return m_invertVertical;
Chris@357 368 }
Chris@357 369
Chris@25 370 bool
Chris@465 371 Colour3DPlotLayer::getOpaque() const
Chris@465 372 {
Chris@465 373 return m_opaque;
Chris@465 374 }
Chris@465 375
Chris@475 376 void
Chris@475 377 Colour3DPlotLayer::setLayerDormant(const View *v, bool dormant)
Chris@475 378 {
Chris@475 379 if (dormant) {
Chris@475 380
Chris@475 381 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@475 382 std::cerr << "Colour3DPlotLayer::setLayerDormant(" << dormant << ")"
Chris@475 383 << std::endl;
Chris@475 384 #endif
Chris@475 385
Chris@475 386 if (isLayerDormant(v)) {
Chris@475 387 return;
Chris@475 388 }
Chris@475 389
Chris@475 390 Layer::setLayerDormant(v, true);
Chris@475 391
Chris@475 392 cacheInvalid();
Chris@475 393
Chris@475 394 } else {
Chris@475 395
Chris@475 396 Layer::setLayerDormant(v, false);
Chris@475 397 }
Chris@475 398 }
Chris@475 399
Chris@465 400 bool
Chris@44 401 Colour3DPlotLayer::isLayerScrollable(const View *v) const
Chris@25 402 {
Chris@197 403 if (m_normalizeVisibleArea) return false;
Chris@25 404 QPoint discard;
Chris@44 405 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@25 406 }
Chris@25 407
Chris@444 408 bool
Chris@444 409 Colour3DPlotLayer::getValueExtents(float &min, float &max,
Chris@444 410 bool &logarithmic, QString &unit) const
Chris@444 411 {
Chris@444 412 if (!m_model) return false;
Chris@444 413
Chris@444 414 min = 0;
Chris@444 415 max = m_model->getHeight();
Chris@444 416
Chris@444 417 logarithmic = false;
Chris@444 418 unit = "";
Chris@444 419
Chris@444 420 return true;
Chris@444 421 }
Chris@444 422
Chris@444 423 bool
Chris@444 424 Colour3DPlotLayer::getDisplayExtents(float &min, float &max) const
Chris@444 425 {
Chris@444 426 if (!m_model) return false;
Chris@444 427
Chris@444 428 min = m_miny;
Chris@444 429 max = m_maxy;
Chris@444 430 if (max <= min) {
Chris@444 431 min = 0;
Chris@444 432 max = m_model->getHeight();
Chris@444 433 }
Chris@444 434 if (min < 0) min = 0;
Chris@444 435 if (max > m_model->getHeight()) max = m_model->getHeight();
Chris@444 436
Chris@444 437 return true;
Chris@444 438 }
Chris@444 439
Chris@444 440 bool
Chris@444 441 Colour3DPlotLayer::setDisplayExtents(float min, float max)
Chris@444 442 {
Chris@444 443 if (!m_model) return false;
Chris@444 444
Chris@444 445 m_miny = lrintf(min);
Chris@444 446 m_maxy = lrintf(max);
Chris@444 447
Chris@444 448 emit layerParametersChanged();
Chris@444 449 return true;
Chris@444 450 }
Chris@444 451
Chris@444 452 int
Chris@444 453 Colour3DPlotLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@444 454 {
Chris@444 455 if (!m_model) return 0;
Chris@444 456
Chris@444 457 defaultStep = 0;
Chris@444 458 int h = m_model->getHeight();
Chris@444 459 return h;
Chris@444 460 }
Chris@444 461
Chris@444 462 int
Chris@444 463 Colour3DPlotLayer::getCurrentVerticalZoomStep() const
Chris@444 464 {
Chris@444 465 if (!m_model) return 0;
Chris@444 466
Chris@444 467 float min, max;
Chris@444 468 getDisplayExtents(min, max);
Chris@444 469 return m_model->getHeight() - lrintf(max - min);
Chris@444 470 }
Chris@444 471
Chris@444 472 void
Chris@444 473 Colour3DPlotLayer::setVerticalZoomStep(int step)
Chris@444 474 {
Chris@444 475 if (!m_model) return;
Chris@444 476
Chris@445 477 // std::cerr << "Colour3DPlotLayer::setVerticalZoomStep(" <<step <<"): before: miny = " << m_miny << ", maxy = " << m_maxy << std::endl;
Chris@444 478
Chris@444 479 int dist = m_model->getHeight() - step;
Chris@444 480 if (dist < 1) dist = 1;
Chris@444 481 float centre = m_miny + (float(m_maxy) - float(m_miny)) / 2.f;
Chris@444 482 m_miny = lrintf(centre - float(dist)/2);
Chris@444 483 if (m_miny < 0) m_miny = 0;
Chris@444 484 m_maxy = m_miny + dist;
Chris@444 485 if (m_maxy > m_model->getHeight()) m_maxy = m_model->getHeight();
Chris@444 486
Chris@445 487 // std::cerr << "Colour3DPlotLayer::setVerticalZoomStep(" <<step <<"): after: miny = " << m_miny << ", maxy = " << m_maxy << std::endl;
Chris@444 488
Chris@444 489 emit layerParametersChanged();
Chris@444 490 }
Chris@444 491
Chris@444 492 RangeMapper *
Chris@444 493 Colour3DPlotLayer::getNewVerticalZoomRangeMapper() const
Chris@444 494 {
Chris@444 495 if (!m_model) return 0;
Chris@444 496
Chris@444 497 return new LinearRangeMapper(0, m_model->getHeight(),
Chris@444 498 0, m_model->getHeight(), "");
Chris@444 499 }
Chris@444 500
Chris@25 501 QString
Chris@44 502 Colour3DPlotLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@25 503 {
Chris@25 504 if (!m_model) return "";
Chris@25 505
Chris@25 506 int x = pos.x();
Chris@25 507 int y = pos.y();
Chris@25 508
Chris@25 509 size_t modelStart = m_model->getStartFrame();
Chris@130 510 size_t modelResolution = m_model->getResolution();
Chris@25 511
Chris@159 512 float srRatio =
Chris@159 513 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@159 514 float(m_model->getSampleRate());
Chris@159 515
Chris@160 516 int sx0 = int((v->getFrameForX(x) / srRatio - long(modelStart)) /
Chris@160 517 long(modelResolution));
Chris@25 518
Chris@160 519 int f0 = sx0 * modelResolution;
Chris@160 520 int f1 = f0 + modelResolution;
Chris@160 521
Chris@447 522 int sh = m_model->getHeight();
Chris@447 523
Chris@447 524 int symin = m_miny;
Chris@447 525 int symax = m_maxy;
Chris@447 526 if (symax <= symin) {
Chris@447 527 symin = 0;
Chris@447 528 symax = sh;
Chris@447 529 }
Chris@447 530 if (symin < 0) symin = 0;
Chris@447 531 if (symax > sh) symax = sh;
Chris@447 532
Chris@447 533 float binHeight = float(v->height()) / (symax - symin);
Chris@447 534 int sy = int((v->height() - y) / binHeight) + symin;
Chris@25 535
Chris@357 536 if (m_invertVertical) sy = m_model->getHeight() - sy - 1;
Chris@357 537
Chris@160 538 float value = m_model->getValueAt(sx0, sy);
Chris@159 539
Chris@159 540 // std::cerr << "bin value (" << sx0 << "," << sy << ") is " << value << std::endl;
Chris@25 541
Chris@25 542 QString binName = m_model->getBinName(sy);
Chris@25 543 if (binName == "") binName = QString("[%1]").arg(sy + 1);
Chris@25 544 else binName = QString("%1 [%2]").arg(binName).arg(sy + 1);
Chris@25 545
Chris@25 546 QString text = tr("Time:\t%1 - %2\nBin:\t%3\nValue:\t%4")
Chris@160 547 .arg(RealTime::frame2RealTime(f0, m_model->getSampleRate())
Chris@25 548 .toText(true).c_str())
Chris@160 549 .arg(RealTime::frame2RealTime(f1, m_model->getSampleRate())
Chris@25 550 .toText(true).c_str())
Chris@25 551 .arg(binName)
Chris@25 552 .arg(value);
Chris@25 553
Chris@25 554 return text;
Chris@25 555 }
Chris@25 556
Chris@25 557 int
Chris@248 558 Colour3DPlotLayer::getColourScaleWidth(QPainter &) const
Chris@159 559 {
Chris@159 560 int cw = 20;
Chris@159 561 return cw;
Chris@159 562 }
Chris@159 563
Chris@159 564 int
Chris@248 565 Colour3DPlotLayer::getVerticalScaleWidth(View *, QPainter &paint) const
Chris@25 566 {
Chris@25 567 if (!m_model) return 0;
Chris@25 568
Chris@160 569 QString sampleText = QString("[%1]").arg(m_model->getHeight());
Chris@25 570 int tw = paint.fontMetrics().width(sampleText);
Chris@98 571 bool another = false;
Chris@25 572
Chris@160 573 for (size_t i = 0; i < m_model->getHeight(); ++i) {
Chris@25 574 if (m_model->getBinName(i).length() > sampleText.length()) {
Chris@25 575 sampleText = m_model->getBinName(i);
Chris@98 576 another = true;
Chris@25 577 }
Chris@25 578 }
Chris@98 579 if (another) {
Chris@25 580 tw = std::max(tw, paint.fontMetrics().width(sampleText));
Chris@25 581 }
Chris@25 582
Chris@159 583 return tw + 13 + getColourScaleWidth(paint);
Chris@25 584 }
Chris@25 585
Chris@25 586 void
Chris@44 587 Colour3DPlotLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@25 588 {
Chris@25 589 if (!m_model) return;
Chris@25 590
Chris@25 591 int h = rect.height(), w = rect.width();
Chris@25 592
Chris@159 593 int cw = getColourScaleWidth(paint);
Chris@159 594
Chris@159 595 int ch = h - 20;
Chris@159 596 if (ch > 20 && m_cache) {
Chris@159 597
Chris@447 598 float min = m_model->getMinimumLevel();
Chris@447 599 float max = m_model->getMaximumLevel();
Chris@447 600
Chris@447 601 float mmin = min;
Chris@447 602 float mmax = max;
Chris@447 603
Chris@447 604 if (m_colourScale == LogScale) {
Chris@447 605 LogRange::mapRange(mmin, mmax);
Chris@447 606 } else if (m_colourScale == PlusMinusOneScale) {
Chris@447 607 mmin = -1.f;
Chris@447 608 mmax = 1.f;
Chris@509 609 } else if (m_colourScale == AbsoluteScale) {
Chris@509 610 if (mmin < 0) {
Chris@509 611 if (fabsf(mmin) > fabsf(mmax)) mmax = fabsf(mmin);
Chris@509 612 else mmax = fabsf(mmax);
Chris@509 613 mmin = 0;
Chris@509 614 } else {
Chris@509 615 mmin = fabsf(mmin);
Chris@509 616 mmax = fabsf(mmax);
Chris@509 617 }
Chris@447 618 }
Chris@447 619
Chris@447 620 if (max == min) max = min + 1.0;
Chris@465 621 if (mmax == mmin) mmax = mmin + 1.0;
Chris@447 622
Chris@287 623 paint.setPen(v->getForeground());
Chris@447 624 paint.drawRect(4, 10, cw - 8, ch+1);
Chris@159 625
Chris@446 626 for (int y = 0; y < ch; ++y) {
Chris@447 627 float value = ((max - min) * (ch - y - 1)) / ch + min;
Chris@447 628 if (m_colourScale == LogScale) {
Chris@447 629 value = LogRange::map(value);
Chris@447 630 }
Chris@447 631 int pixel = int(((value - mmin) * 256) / (mmax - mmin));
Chris@465 632 if (pixel >= 0 && pixel < 256) {
Chris@465 633 QRgb c = m_cache->color(pixel);
Chris@465 634 paint.setPen(QColor(qRed(c), qGreen(c), qBlue(c)));
Chris@465 635 paint.drawLine(5, 11 + y, cw - 5, 11 + y);
Chris@465 636 } else {
Chris@465 637 std::cerr << "WARNING: Colour3DPlotLayer::paintVerticalScale: value " << value << ", mmin " << mmin << ", mmax " << mmax << " leads to invalid pixel " << pixel << std::endl;
Chris@465 638 }
Chris@159 639 }
Chris@446 640
Chris@446 641 QString minstr = QString("%1").arg(min);
Chris@446 642 QString maxstr = QString("%1").arg(max);
Chris@446 643
Chris@446 644 paint.save();
Chris@446 645
Chris@446 646 QFont font = paint.font();
Chris@446 647 font.setPixelSize(10);
Chris@446 648 paint.setFont(font);
Chris@446 649
Chris@446 650 int msw = paint.fontMetrics().width(maxstr);
Chris@446 651
Chris@446 652 QMatrix m;
Chris@446 653 m.translate(cw - 6, ch + 10);
Chris@446 654 m.rotate(-90);
Chris@446 655
Chris@446 656 paint.setWorldMatrix(m);
Chris@446 657
Chris@446 658 v->drawVisibleText(paint, 2, 0, minstr, View::OutlinedText);
Chris@446 659
Chris@446 660 m.translate(ch - msw - 2, 0);
Chris@446 661 paint.setWorldMatrix(m);
Chris@446 662
Chris@446 663 v->drawVisibleText(paint, 0, 0, maxstr, View::OutlinedText);
Chris@446 664
Chris@446 665 paint.restore();
Chris@159 666 }
Chris@159 667
Chris@287 668 paint.setPen(v->getForeground());
Chris@159 669
Chris@445 670 int sh = m_model->getHeight();
Chris@445 671
Chris@445 672 int symin = m_miny;
Chris@445 673 int symax = m_maxy;
Chris@445 674 if (symax <= symin) {
Chris@445 675 symin = 0;
Chris@445 676 symax = sh;
Chris@445 677 }
Chris@445 678 if (symin < 0) symin = 0;
Chris@445 679 if (symax > sh) symax = sh;
Chris@445 680
Chris@456 681 float binHeight = float(v->height()) / (symax - symin);
Chris@456 682
Chris@456 683 paint.save();
Chris@456 684
Chris@456 685 QFont tf = paint.font();
Chris@456 686 if (paint.fontMetrics().height() >= binHeight) {
Chris@457 687 tf.setPixelSize(binHeight > 7 ? binHeight - 2 : 5);
Chris@456 688 paint.setFont(tf);
Chris@456 689 }
Chris@456 690
Chris@98 691 int count = v->height() / paint.fontMetrics().height();
Chris@445 692 int step = (symax - symin) / count;
Chris@98 693 if (step == 0) step = 1;
Chris@25 694
Chris@445 695 for (size_t i = symin; i < symax; ++i) {
Chris@25 696
Chris@357 697 size_t idx = i;
Chris@357 698 if (m_invertVertical) idx = m_model->getHeight() - idx - 1;
Chris@357 699
Chris@357 700 if ((idx % step) != 0) continue;
Chris@98 701
Chris@445 702 int y0 = int(v->height() - ((i - symin) * binHeight) - 1);
Chris@25 703
Chris@357 704 QString text = m_model->getBinName(idx);
Chris@357 705 if (text == "") text = QString("[%1]").arg(idx + 1);
Chris@25 706
Chris@159 707 paint.drawLine(cw, y0, w, y0);
Chris@25 708
Chris@457 709 if (step > 1) {
Chris@457 710 paint.drawLine(w - 1, y0 - (step * binHeight) + 1,
Chris@457 711 w - 1, y0 - binHeight - 1);
Chris@457 712 paint.drawLine(w - 2, y0 - (step * binHeight) + 1,
Chris@457 713 w - 2, y0 - binHeight - 2);
Chris@457 714 }
Chris@457 715
Chris@248 716 int cy = int(y0 - (step * binHeight)/2);
Chris@25 717 int ty = cy + paint.fontMetrics().ascent()/2;
Chris@25 718
Chris@159 719 paint.drawText(cw + 5, ty, text);
Chris@25 720 }
Chris@456 721
Chris@456 722 paint.restore();
Chris@25 723 }
Chris@25 724
Chris@467 725 DenseThreeDimensionalModel::Column
Chris@467 726 Colour3DPlotLayer::getColumn(size_t col) const
Chris@197 727 {
Chris@467 728 DenseThreeDimensionalModel::Column values = m_model->getColumn(col);
Chris@468 729 while (values.size() < m_model->getHeight()) values.push_back(0.f);
Chris@467 730 if (!m_normalizeColumns) return values;
Chris@461 731
Chris@224 732 float colMax = 0.f, colMin = 0.f;
Chris@461 733 float min = 0.f, max = 0.f;
Chris@197 734
Chris@461 735 min = m_model->getMinimumLevel();
Chris@461 736 max = m_model->getMaximumLevel();
Chris@461 737
Chris@461 738 for (size_t y = 0; y < values.size(); ++y) {
Chris@467 739 if (y == 0 || values.at(y) > colMax) colMax = values.at(y);
Chris@467 740 if (y == 0 || values.at(y) < colMin) colMin = values.at(y);
Chris@197 741 }
Chris@461 742 if (colMin == colMax) colMax = colMin + 1;
Chris@197 743
Chris@197 744 for (size_t y = 0; y < values.size(); ++y) {
Chris@461 745
Chris@467 746 float value = values.at(y);
Chris@461 747 float norm = (value - colMin) / (colMax - colMin);
Chris@467 748 float newvalue = min + (max - min) * norm;
Chris@197 749
Chris@467 750 if (value != newvalue) values[y] = newvalue;
Chris@468 751 }
Chris@468 752
Chris@468 753 return values;
Chris@197 754 }
Chris@197 755
Chris@197 756 void
Chris@197 757 Colour3DPlotLayer::fillCache(size_t firstBin, size_t lastBin) const
Chris@197 758 {
Chris@466 759 Profiler profiler("Colour3DPlotLayer::fillCache");
Chris@476 760
Chris@197 761 size_t modelStart = m_model->getStartFrame();
Chris@197 762 size_t modelEnd = m_model->getEndFrame();
Chris@197 763 size_t modelResolution = m_model->getResolution();
Chris@197 764
Chris@519 765 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@519 766 std::cerr << "Colour3DPlotLayer::fillCache: " << firstBin << " -> " << lastBin << std::endl;
Chris@519 767 #endif
Chris@197 768
Chris@461 769 size_t modelStartBin = modelStart / modelResolution;
Chris@461 770 size_t modelEndBin = modelEnd / modelResolution;
Chris@461 771
Chris@461 772 size_t cacheWidth = modelEndBin - modelStartBin + 1;
Chris@471 773 if (lastBin > modelEndBin) cacheWidth = lastBin - modelStartBin + 1;
Chris@461 774 size_t cacheHeight = m_model->getHeight();
Chris@461 775
Chris@461 776 if (m_cache && (m_cache->height() != int(cacheHeight))) {
Chris@461 777 delete m_cache;
Chris@469 778 delete m_peaksCache;
Chris@461 779 m_cache = 0;
Chris@469 780 m_peaksCache = 0;
Chris@461 781 }
Chris@461 782
Chris@461 783 if (m_cache && (m_cache->width() != int(cacheWidth))) {
Chris@469 784 QImage *newCache =
Chris@469 785 new QImage(m_cache->copy(0, 0, cacheWidth, cacheHeight));
Chris@461 786 delete m_cache;
Chris@461 787 m_cache = newCache;
Chris@469 788 if (m_peaksCache) {
Chris@469 789 QImage *newPeaksCache =
Chris@469 790 new QImage(m_peaksCache->copy
Chris@469 791 (0, 0, cacheWidth / m_peakResolution, cacheHeight));
Chris@469 792 delete m_peaksCache;
Chris@469 793 m_peaksCache = newPeaksCache;
Chris@469 794 }
Chris@197 795 }
Chris@197 796
Chris@461 797 if (!m_cache) {
Chris@469 798 m_cache = new QImage
Chris@469 799 (cacheWidth, cacheHeight, QImage::Format_Indexed8);
Chris@461 800 m_cache->setNumColors(256);
Chris@514 801 m_cache->fill(0);
Chris@474 802 if (!m_normalizeVisibleArea) {
Chris@469 803 m_peaksCache = new QImage
Chris@469 804 (cacheWidth / m_peakResolution + 1, cacheHeight,
Chris@469 805 QImage::Format_Indexed8);
Chris@469 806 m_peaksCache->setNumColors(256);
Chris@514 807 m_peaksCache->fill(0);
Chris@469 808 } else if (m_peaksCache) {
Chris@469 809 delete m_peaksCache;
Chris@469 810 m_peaksCache = 0;
Chris@469 811 }
Chris@461 812 m_cacheValidStart = 0;
Chris@461 813 m_cacheValidEnd = 0;
Chris@197 814 }
Chris@197 815
Chris@461 816 if (m_cacheValidStart <= firstBin && m_cacheValidEnd >= lastBin) {
Chris@519 817 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@519 818 std::cerr << "Cache is valid in this region already" << std::endl;
Chris@519 819 #endif
Chris@461 820 return;
Chris@461 821 }
Chris@461 822
Chris@461 823 size_t fillStart = firstBin;
Chris@461 824 size_t fillEnd = lastBin;
Chris@197 825
Chris@461 826 if (fillStart < modelStartBin) fillStart = modelStartBin;
Chris@461 827 if (fillStart > modelEndBin) fillStart = modelEndBin;
Chris@461 828 if (fillEnd < modelStartBin) fillEnd = modelStartBin;
Chris@461 829 if (fillEnd > modelEndBin) fillEnd = modelEndBin;
Chris@197 830
Chris@461 831 bool normalizeVisible = (m_normalizeVisibleArea && !m_normalizeColumns);
Chris@197 832
Chris@461 833 if (!normalizeVisible && (m_cacheValidStart < m_cacheValidEnd)) {
Chris@461 834
Chris@461 835 if (m_cacheValidEnd < fillStart) {
Chris@461 836 fillStart = m_cacheValidEnd + 1;
Chris@461 837 }
Chris@461 838 if (m_cacheValidStart > fillEnd) {
Chris@461 839 fillEnd = m_cacheValidStart - 1;
Chris@461 840 }
Chris@461 841
Chris@461 842 m_cacheValidStart = std::min(fillStart, m_cacheValidStart);
Chris@461 843 m_cacheValidEnd = std::max(fillEnd, m_cacheValidEnd);
Chris@461 844
Chris@461 845 } else {
Chris@461 846
Chris@461 847 // the only valid area, ever, is the currently visible one
Chris@461 848
Chris@461 849 m_cacheValidStart = fillStart;
Chris@461 850 m_cacheValidEnd = fillEnd;
Chris@461 851 }
Chris@461 852
Chris@519 853 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@519 854 std::cerr << "Cache size " << cacheWidth << "x" << cacheHeight << " will be valid from " << m_cacheValidStart << " to " << m_cacheValidEnd << std::endl;
Chris@519 855 #endif
Chris@461 856
Chris@197 857 DenseThreeDimensionalModel::Column values;
Chris@197 858
Chris@197 859 float min = m_model->getMinimumLevel();
Chris@197 860 float max = m_model->getMaximumLevel();
Chris@197 861
Chris@197 862 if (m_colourScale == LogScale) {
Chris@197 863 LogRange::mapRange(min, max);
Chris@197 864 } else if (m_colourScale == PlusMinusOneScale) {
Chris@197 865 min = -1.f;
Chris@197 866 max = 1.f;
Chris@509 867 } else if (m_colourScale == AbsoluteScale) {
Chris@509 868 if (min < 0) {
Chris@509 869 if (fabsf(min) > fabsf(max)) max = fabsf(min);
Chris@509 870 else max = fabsf(max);
Chris@509 871 min = 0;
Chris@509 872 } else {
Chris@509 873 min = fabsf(min);
Chris@509 874 max = fabsf(max);
Chris@509 875 }
Chris@197 876 }
Chris@197 877
Chris@197 878 if (max == min) max = min + 1.0;
Chris@197 879
Chris@197 880 ColourMapper mapper(m_colourMap, 0.f, 255.f);
Chris@197 881
Chris@197 882 for (int index = 0; index < 256; ++index) {
Chris@197 883 QColor colour = mapper.map(index);
Chris@469 884 m_cache->setColor
Chris@469 885 (index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@469 886 if (m_peaksCache) {
Chris@469 887 m_peaksCache->setColor
Chris@469 888 (index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@469 889 }
Chris@197 890 }
Chris@197 891
Chris@224 892 float visibleMax = 0.f, visibleMin = 0.f;
Chris@197 893
Chris@461 894 if (normalizeVisible) {
Chris@197 895
Chris@461 896 for (size_t c = fillStart; c <= fillEnd; ++c) {
Chris@197 897
Chris@467 898 values = getColumn(c);
Chris@197 899
Chris@224 900 float colMax = 0.f, colMin = 0.f;
Chris@197 901
Chris@461 902 for (size_t y = 0; y < cacheHeight; ++y) {
Chris@197 903 if (y >= values.size()) break;
Chris@197 904 if (y == 0 || values[y] > colMax) colMax = values[y];
Chris@224 905 if (y == 0 || values[y] < colMin) colMin = values[y];
Chris@197 906 }
Chris@197 907
Chris@461 908 if (c == fillStart || colMax > visibleMax) visibleMax = colMax;
Chris@461 909 if (c == fillStart || colMin < visibleMin) visibleMin = colMin;
Chris@461 910 }
Chris@461 911
Chris@461 912 if (m_colourScale == LogScale) {
Chris@461 913 visibleMin = LogRange::map(visibleMin);
Chris@461 914 visibleMax = LogRange::map(visibleMax);
Chris@461 915 if (visibleMin > visibleMax) std::swap(visibleMin, visibleMax);
Chris@509 916 } else if (m_colourScale == AbsoluteScale) {
Chris@509 917 if (visibleMin < 0) {
Chris@509 918 if (fabsf(visibleMin) > fabsf(visibleMax)) visibleMax = fabsf(visibleMin);
Chris@509 919 else visibleMax = fabsf(visibleMax);
Chris@509 920 visibleMin = 0;
Chris@509 921 } else {
Chris@509 922 visibleMin = fabsf(visibleMin);
Chris@509 923 visibleMax = fabsf(visibleMax);
Chris@509 924 }
Chris@197 925 }
Chris@197 926 }
Chris@197 927
Chris@224 928 if (visibleMin == visibleMax) visibleMax = visibleMin + 1;
Chris@224 929
Chris@469 930 int *peaks = 0;
Chris@469 931 if (m_peaksCache) {
Chris@469 932 peaks = new int[cacheHeight];
Chris@469 933 for (int y = 0; y < cacheHeight; ++y) {
Chris@469 934 peaks[y] = 0;
Chris@469 935 }
Chris@469 936 }
Chris@469 937
Chris@461 938 for (size_t c = fillStart; c <= fillEnd; ++c) {
Chris@197 939
Chris@467 940 values = getColumn(c);
Chris@197 941
Chris@461 942 for (size_t y = 0; y < cacheHeight; ++y) {
Chris@197 943
Chris@197 944 float value = min;
Chris@197 945 if (y < values.size()) {
Chris@469 946 value = values.at(y);
Chris@197 947 }
Chris@224 948
Chris@224 949 if (m_colourScale == LogScale) {
Chris@224 950 value = LogRange::map(value);
Chris@509 951 } else if (m_colourScale == AbsoluteScale) {
Chris@509 952 value = fabsf(value);
Chris@197 953 }
Chris@461 954
Chris@461 955 if (normalizeVisible) {
Chris@461 956 float norm = (value - visibleMin) / (visibleMax - visibleMin);
Chris@461 957 value = min + (max - min) * norm;
Chris@461 958 }
Chris@197 959
Chris@197 960 int pixel = int(((value - min) * 256) / (max - min));
Chris@197 961 if (pixel < 0) pixel = 0;
Chris@197 962 if (pixel > 255) pixel = 255;
Chris@469 963 if (peaks && (pixel > peaks[y])) peaks[y] = pixel;
Chris@197 964
Chris@357 965 if (m_invertVertical) {
Chris@461 966 m_cache->setPixel(c, cacheHeight - y - 1, pixel);
Chris@357 967 } else {
Chris@461 968 m_cache->setPixel(c, y, pixel);
Chris@357 969 }
Chris@197 970 }
Chris@469 971
Chris@469 972 if (peaks) {
Chris@469 973 size_t notch = (c % m_peakResolution);
Chris@469 974 if (notch == m_peakResolution-1 || c == fillEnd) {
Chris@469 975 size_t pc = c / m_peakResolution;
Chris@469 976 for (size_t y = 0; y < cacheHeight; ++y) {
Chris@469 977 if (m_invertVertical) {
Chris@469 978 m_peaksCache->setPixel(pc, cacheHeight - y - 1, peaks[y]);
Chris@469 979 } else {
Chris@469 980 m_peaksCache->setPixel(pc, y, peaks[y]);
Chris@469 981 }
Chris@469 982 }
Chris@469 983 for (int y = 0; y < cacheHeight; ++y) {
Chris@469 984 peaks[y] = 0;
Chris@469 985 }
Chris@469 986 }
Chris@469 987 }
Chris@197 988 }
Chris@469 989
Chris@469 990 delete[] peaks;
Chris@197 991 }
Chris@197 992
Chris@197 993 void
Chris@44 994 Colour3DPlotLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 995 {
Chris@514 996 /*
Chris@443 997 if (m_model) {
Chris@443 998 std::cerr << "Colour3DPlotLayer::paint: model says shouldUseLogValueScale = " << m_model->shouldUseLogValueScale() << std::endl;
Chris@443 999 }
Chris@514 1000 */
Chris@466 1001 Profiler profiler("Colour3DPlotLayer::paint");
Chris@125 1002 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@125 1003 std::cerr << "Colour3DPlotLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << std::endl;
Chris@125 1004 #endif
Chris@0 1005
Chris@0 1006 int completion = 0;
Chris@0 1007 if (!m_model || !m_model->isOK() || !m_model->isReady(&completion)) {
Chris@0 1008 if (completion > 0) {
Chris@44 1009 paint.fillRect(0, 10, v->width() * completion / 100,
Chris@0 1010 10, QColor(120, 120, 120));
Chris@0 1011 }
Chris@0 1012 return;
Chris@0 1013 }
Chris@0 1014
Chris@197 1015 if (m_normalizeVisibleArea && !m_normalizeColumns) rect = v->rect();
Chris@197 1016
Chris@0 1017 size_t modelStart = m_model->getStartFrame();
Chris@0 1018 size_t modelEnd = m_model->getEndFrame();
Chris@130 1019 size_t modelResolution = m_model->getResolution();
Chris@0 1020
Chris@197 1021 // The cache is from the model's start frame to the model's end
Chris@197 1022 // frame at the model's window increment frames per pixel. We
Chris@197 1023 // want to draw from our start frame + x0 * zoomLevel to our start
Chris@197 1024 // frame + x1 * zoomLevel at zoomLevel frames per pixel.
Chris@12 1025
Chris@197 1026 // We have quite different paint mechanisms for rendering "large"
Chris@197 1027 // bins (more than one bin per pixel in both directions) and
Chris@197 1028 // "small". This is "large"; see paintDense below for "small".
Chris@12 1029
Chris@197 1030 int x0 = rect.left();
Chris@197 1031 int x1 = rect.right() + 1;
Chris@12 1032
Chris@197 1033 int h = v->height();
Chris@0 1034
Chris@197 1035 float srRatio =
Chris@197 1036 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@197 1037 float(m_model->getSampleRate());
Chris@0 1038
Chris@197 1039 int sx0 = int((v->getFrameForX(x0) / srRatio - long(modelStart))
Chris@197 1040 / long(modelResolution));
Chris@197 1041 int sx1 = int((v->getFrameForX(x1) / srRatio - long(modelStart))
Chris@197 1042 / long(modelResolution));
Chris@197 1043 int sh = m_model->getHeight();
Chris@159 1044
Chris@444 1045 int symin = m_miny;
Chris@444 1046 int symax = m_maxy;
Chris@444 1047 if (symax <= symin) {
Chris@444 1048 symin = 0;
Chris@444 1049 symax = sh;
Chris@444 1050 }
Chris@444 1051 if (symin < 0) symin = 0;
Chris@444 1052 if (symax > sh) symax = sh;
Chris@444 1053
Chris@197 1054 if (sx0 > 0) --sx0;
Chris@197 1055 fillCache(sx0 < 0 ? 0 : sx0,
Chris@197 1056 sx1 < 0 ? 0 : sx1);
Chris@0 1057
Chris@351 1058 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@466 1059 std::cerr << "Colour3DPlotLayer::paint: height = "<< m_model->getHeight() << ", modelStart = " << modelStart << ", resolution = " << modelResolution << ", model rate = " << m_model->getSampleRate() << " (zoom level = " << v->getZoomLevel() << ", srRatio = " << srRatio << ")" << std::endl;
Chris@351 1060 #endif
Chris@351 1061
Chris@465 1062 if (m_opaque ||
Chris@465 1063 int(m_model->getHeight()) >= v->height() ||
Chris@466 1064 ((modelResolution * srRatio) / v->getZoomLevel()) < 2) {
Chris@347 1065 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@351 1066 std::cerr << "calling paintDense" << std::endl;
Chris@347 1067 #endif
Chris@98 1068 paintDense(v, paint, rect);
Chris@98 1069 return;
Chris@98 1070 }
Chris@98 1071
Chris@125 1072 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@347 1073 std::cerr << "Colour3DPlotLayer::paint: w " << x1-x0 << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sx1-sx0 << ", sh " << sh << std::endl;
Chris@130 1074 std::cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << std::endl;
Chris@125 1075 #endif
Chris@0 1076
Chris@25 1077 QPoint illuminatePos;
Chris@44 1078 bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos);
Chris@54 1079 char labelbuf[10];
Chris@25 1080
Chris@197 1081 for (int sx = sx0; sx <= sx1; ++sx) {
Chris@0 1082
Chris@130 1083 int fx = sx * int(modelResolution);
Chris@0 1084
Chris@351 1085 if (fx + int(modelResolution) <= int(modelStart) ||
Chris@0 1086 fx > int(modelEnd)) continue;
Chris@0 1087
Chris@248 1088 int rx0 = v->getXForFrame(int((fx + int(modelStart)) * srRatio));
Chris@248 1089 int rx1 = v->getXForFrame(int((fx + int(modelStart) + int(modelResolution) + 1) * srRatio));
Chris@54 1090
Chris@159 1091 int rw = rx1 - rx0;
Chris@159 1092 if (rw < 1) rw = 1;
Chris@54 1093
Chris@159 1094 bool showLabel = (rw > 10 &&
Chris@159 1095 paint.fontMetrics().width("0.000000") < rw - 3 &&
Chris@54 1096 paint.fontMetrics().height() < (h / sh));
Chris@98 1097
Chris@444 1098 for (int sy = symin; sy < symax; ++sy) {
Chris@0 1099
Chris@444 1100 int ry0 = h - ((sy - symin) * h) / (symax - symin) - 1;
Chris@0 1101 QRgb pixel = qRgb(255, 255, 255);
Chris@461 1102 if (sx >= 0 && sx < m_cache->width() &&
Chris@0 1103 sy >= 0 && sy < m_cache->height()) {
Chris@461 1104 pixel = m_cache->pixel(sx, sy);
Chris@0 1105 }
Chris@0 1106
Chris@509 1107 QRect r(rx0, ry0 - h / (symax - symin),
Chris@444 1108 rw, h / (symax - symin) + 1);
Chris@159 1109
Chris@159 1110 if (rw == 1) {
Chris@159 1111 paint.setPen(pixel);
Chris@159 1112 paint.setBrush(Qt::NoBrush);
Chris@159 1113 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
Chris@159 1114 continue;
Chris@159 1115 }
Chris@159 1116
Chris@0 1117 QColor pen(255, 255, 255, 80);
Chris@0 1118 QColor brush(pixel);
Chris@159 1119
Chris@159 1120 if (rw > 3 && r.height() > 3) {
Chris@159 1121 brush.setAlpha(160);
Chris@159 1122 }
Chris@159 1123
Chris@0 1124 paint.setPen(Qt::NoPen);
Chris@0 1125 paint.setBrush(brush);
Chris@0 1126
Chris@25 1127 if (illuminate) {
Chris@25 1128 if (r.contains(illuminatePos)) {
Chris@287 1129 paint.setPen(v->getForeground());
Chris@25 1130 }
Chris@25 1131 }
Chris@76 1132
Chris@125 1133 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@519 1134 // std::cerr << "rect " << r.x() << "," << r.y() << " "
Chris@519 1135 // << r.width() << "x" << r.height() << std::endl;
Chris@125 1136 #endif
Chris@25 1137
Chris@25 1138 paint.drawRect(r);
Chris@0 1139
Chris@54 1140 if (showLabel) {
Chris@461 1141 if (sx >= 0 && sx < m_cache->width() &&
Chris@54 1142 sy >= 0 && sy < m_cache->height()) {
Chris@461 1143 float value = m_model->getValueAt(sx, sy);
Chris@54 1144 sprintf(labelbuf, "%06f", value);
Chris@54 1145 QString text(labelbuf);
Chris@287 1146 paint.setPen(v->getBackground());
Chris@54 1147 paint.drawText(rx0 + 2,
Chris@54 1148 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
Chris@54 1149 text);
Chris@0 1150 }
Chris@0 1151 }
Chris@0 1152 }
Chris@0 1153 }
Chris@98 1154 }
Chris@0 1155
Chris@98 1156 void
Chris@98 1157 Colour3DPlotLayer::paintDense(View *v, QPainter &paint, QRect rect) const
Chris@98 1158 {
Chris@466 1159 Profiler profiler("Colour3DPlotLayer::paintDense");
Chris@463 1160 if (!m_cache) return;
Chris@463 1161
Chris@466 1162 float modelStart = m_model->getStartFrame();
Chris@466 1163 float modelResolution = m_model->getResolution();
Chris@0 1164
Chris@469 1165 int mmsr = v->getViewManager()->getMainModelSampleRate();
Chris@469 1166 int msr = m_model->getSampleRate();
Chris@469 1167 float srRatio = float(mmsr) / float(msr);
Chris@159 1168
Chris@98 1169 int x0 = rect.left();
Chris@98 1170 int x1 = rect.right() + 1;
Chris@0 1171
Chris@470 1172 const int w = x1 - x0; // const so it can be used as array size below
Chris@471 1173 int h = v->height(); // we always paint full height
Chris@160 1174 int sh = m_model->getHeight();
Chris@98 1175
Chris@444 1176 int symin = m_miny;
Chris@444 1177 int symax = m_maxy;
Chris@444 1178 if (symax <= symin) {
Chris@444 1179 symin = 0;
Chris@444 1180 symax = sh;
Chris@444 1181 }
Chris@444 1182 if (symin < 0) symin = 0;
Chris@444 1183 if (symax > sh) symax = sh;
Chris@444 1184
Chris@466 1185 QImage img(w, h, QImage::Format_Indexed8);
Chris@466 1186 img.setColorTable(m_cache->colorTable());
Chris@98 1187
Chris@466 1188 uchar *peaks = new uchar[w];
Chris@466 1189 memset(peaks, 0, w);
Chris@98 1190
Chris@469 1191 int zoomLevel = v->getZoomLevel();
Chris@469 1192
Chris@469 1193 QImage *source = m_cache;
Chris@474 1194
Chris@474 1195 std::cerr << "modelResolution " << modelResolution << ", srRatio "
Chris@474 1196 << srRatio << ", m_peakResolution " << m_peakResolution
Chris@474 1197 << ", zoomLevel " << zoomLevel << ", result "
Chris@474 1198 << ((modelResolution * srRatio * m_peakResolution) / zoomLevel)
Chris@474 1199 << std::endl;
Chris@474 1200
Chris@474 1201 if (m_peaksCache) {
Chris@474 1202 if (((modelResolution * srRatio * m_peakResolution) / zoomLevel) < 1) {
Chris@474 1203 std::cerr << "using peaks cache" << std::endl;
Chris@474 1204 source = m_peaksCache;
Chris@474 1205 modelResolution *= m_peakResolution;
Chris@474 1206 } else {
Chris@474 1207 std::cerr << "not using peaks cache" << std::endl;
Chris@474 1208 }
Chris@469 1209 } else {
Chris@474 1210 std::cerr << "have no peaks cache" << std::endl;
Chris@469 1211 }
Chris@469 1212
Chris@466 1213 int psy1i = -1;
Chris@469 1214 int sw = source->width();
Chris@470 1215
Chris@470 1216 long xf = -1;
Chris@470 1217 long nxf = v->getFrameForX(x0);
Chris@470 1218
Chris@470 1219 int sxa[w * 2];
Chris@470 1220 for (int x = 0; x < w; ++x) {
Chris@470 1221
Chris@470 1222 xf = nxf;
Chris@470 1223 nxf = xf + zoomLevel;
Chris@470 1224
Chris@470 1225 float sx0 = (float(xf) / srRatio - modelStart) / modelResolution;
Chris@470 1226 float sx1 = (float(nxf) / srRatio - modelStart) / modelResolution;
Chris@470 1227
Chris@470 1228 int sx0i = int(sx0 + 0.001);
Chris@470 1229 int sx1i = int(sx1);
Chris@470 1230
Chris@470 1231 sxa[x*2] = sx0i;
Chris@470 1232 sxa[x*2 + 1] = sx1i;
Chris@470 1233 }
Chris@466 1234
Chris@466 1235 for (int y = 0; y < h; ++y) {
Chris@466 1236
Chris@466 1237 float sy0 = symin + (float(h - y - 1) * (symax - symin)) / h;
Chris@466 1238 float sy1 = symin + (float(h - y) * (symax - symin)) / h;
Chris@466 1239
Chris@466 1240 int sy0i = int(sy0 + 0.001);
Chris@466 1241 int sy1i = int(sy1);
Chris@466 1242
Chris@466 1243 uchar *targetLine = img.scanLine(y);
Chris@466 1244
Chris@466 1245 if (sy0i == sy1i && sy0i == psy1i) { // same scan line as just computed
Chris@466 1246 goto copy;
Chris@105 1247 }
Chris@105 1248
Chris@471 1249 for (int x = 0; x < w; ++x) {
Chris@471 1250 peaks[x] = 0;
Chris@471 1251 }
Chris@466 1252
Chris@466 1253 for (int sy = sy0i; sy <= sy1i; ++sy) {
Chris@347 1254
Chris@469 1255 if (sy < 0 || sy >= source->height()) continue;
Chris@466 1256
Chris@469 1257 uchar *sourceLine = source->scanLine(sy);
Chris@98 1258
Chris@466 1259 for (int x = 0; x < w; ++x) {
Chris@98 1260
Chris@474 1261 int sx1i = sxa[x*2 + 1];
Chris@474 1262 if (sx1i < 0) continue;
Chris@474 1263
Chris@470 1264 int sx0i = sxa[x*2];
Chris@474 1265 if (sx0i >= sw) break;
Chris@98 1266
Chris@466 1267 uchar peak = 0;
Chris@466 1268 for (int sx = sx0i; sx <= sx1i; ++sx) {
Chris@469 1269 if (sx < 0 || sx >= sw) continue;
Chris@466 1270 if (sourceLine[sx] > peak) peak = sourceLine[sx];
Chris@98 1271 }
Chris@466 1272 peaks[x] = peak;
Chris@98 1273 }
Chris@466 1274 }
Chris@466 1275
Chris@466 1276 copy:
Chris@466 1277 for (int x = 0; x < w; ++x) {
Chris@466 1278 targetLine[x] = peaks[x];
Chris@98 1279 }
Chris@0 1280 }
Chris@0 1281
Chris@469 1282 delete[] peaks;
Chris@469 1283
Chris@98 1284 paint.drawImage(x0, 0, img);
Chris@0 1285 }
Chris@0 1286
Chris@472 1287 void
Chris@472 1288 Colour3DPlotLayer::paintSmooth(View *v, QPainter &paint, QRect rect) const
Chris@472 1289 {
Chris@472 1290 Profiler profiler("Colour3DPlotLayer:paintSmooth");
Chris@472 1291 if (!m_cache) return;
Chris@472 1292
Chris@472 1293 float modelStart = m_model->getStartFrame();
Chris@472 1294 float modelResolution = m_model->getResolution();
Chris@472 1295
Chris@472 1296 int mmsr = v->getViewManager()->getMainModelSampleRate();
Chris@472 1297 int msr = m_model->getSampleRate();
Chris@472 1298 float srRatio = float(mmsr) / float(msr);
Chris@472 1299
Chris@472 1300 int x0 = rect.left();
Chris@472 1301 int x1 = rect.right() + 1;
Chris@472 1302
Chris@472 1303 int h = v->height(); // we always paint full height
Chris@472 1304 int sh = m_model->getHeight();
Chris@472 1305
Chris@472 1306 int symin = m_miny;
Chris@472 1307 int symax = m_maxy;
Chris@472 1308 if (symax <= symin) {
Chris@472 1309 symin = 0;
Chris@472 1310 symax = sh;
Chris@472 1311 }
Chris@472 1312 if (symin < 0) symin = 0;
Chris@472 1313 if (symax > sh) symax = sh;
Chris@472 1314
Chris@472 1315 // QImage img(w, h, QImage::Format_Indexed8);
Chris@472 1316 // img.setColorTable(m_cache->colorTable());
Chris@472 1317
Chris@472 1318 int zoomLevel = v->getZoomLevel();
Chris@472 1319
Chris@472 1320 QImage *source = m_cache;
Chris@472 1321 if (m_peaksCache &&
Chris@472 1322 ((modelResolution * srRatio * m_peakResolution) / zoomLevel) < 1) {
Chris@472 1323 std::cerr << "using peaks cache" << std::endl;
Chris@472 1324 source = m_peaksCache;
Chris@472 1325 modelResolution *= m_peakResolution;
Chris@472 1326 } else {
Chris@472 1327 std::cerr << "not using peaks cache" << std::endl;
Chris@472 1328 }
Chris@472 1329
Chris@472 1330 float sx0 = (float(v->getFrameForX(x0)) / srRatio - modelStart) / modelResolution;
Chris@472 1331 float sx1 = (float(v->getFrameForX(x1)) / srRatio - modelStart) / modelResolution;
Chris@472 1332 int sx0i = int(sx0 + 0.001);
Chris@472 1333 int sx1i = int(sx1);
Chris@472 1334
Chris@472 1335 if (sx0i < 0) sx0i = 0;
Chris@472 1336 if (sx0i > source->width()) sx0i = source->width();
Chris@472 1337
Chris@472 1338 int tx0 = v->getXForFrame(((sx0i * modelResolution) + modelStart) * srRatio + 0.001);
Chris@472 1339 int tx1 = v->getXForFrame(((sx1i * modelResolution) + modelStart) * srRatio);
Chris@472 1340
Chris@472 1341 std::cerr << "x0 " << x0 << ", x1 " << x1 << " -> sx0 " << sx0i << ", sx1 " << sx1i << " -> tx0 " << tx0 << ", tx1 " << tx1 << std::endl;
Chris@472 1342
Chris@472 1343 QImage img = source->copy(sx0i, 0, sx1i - sx0i, source->height())
Chris@472 1344 .scaled(QSize(tx1 - tx0, h),
Chris@472 1345 Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
Chris@472 1346 paint.drawImage(x0, 0, img);
Chris@472 1347 }
Chris@472 1348
Chris@28 1349 bool
Chris@44 1350 Colour3DPlotLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 1351 size_t &resolution,
Chris@28 1352 SnapType snap) const
Chris@24 1353 {
Chris@24 1354 if (!m_model) {
Chris@44 1355 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@24 1356 }
Chris@24 1357
Chris@130 1358 resolution = m_model->getResolution();
Chris@28 1359 int left = (frame / resolution) * resolution;
Chris@28 1360 int right = left + resolution;
Chris@28 1361
Chris@28 1362 switch (snap) {
Chris@28 1363 case SnapLeft: frame = left; break;
Chris@28 1364 case SnapRight: frame = right; break;
Chris@28 1365 case SnapNearest:
Chris@28 1366 case SnapNeighbouring:
Chris@28 1367 if (frame - left > right - frame) frame = right;
Chris@28 1368 else frame = left;
Chris@28 1369 break;
Chris@28 1370 }
Chris@24 1371
Chris@28 1372 return true;
Chris@24 1373 }
Chris@24 1374
Chris@316 1375 void
Chris@316 1376 Colour3DPlotLayer::toXml(QTextStream &stream,
Chris@316 1377 QString indent, QString extraAttributes) const
Chris@197 1378 {
Chris@316 1379 QString s = QString("scale=\"%1\" "
Chris@316 1380 "colourScheme=\"%2\" "
Chris@316 1381 "normalizeColumns=\"%3\" "
Chris@445 1382 "normalizeVisibleArea=\"%4\" "
Chris@445 1383 "minY=\"%5\" "
Chris@465 1384 "maxY=\"%6\" "
Chris@465 1385 "invertVertical=\"%7\" "
Chris@465 1386 "opaque=\"%8\"")
Chris@197 1387 .arg((int)m_colourScale)
Chris@197 1388 .arg(m_colourMap)
Chris@197 1389 .arg(m_normalizeColumns ? "true" : "false")
Chris@445 1390 .arg(m_normalizeVisibleArea ? "true" : "false")
Chris@445 1391 .arg(m_miny)
Chris@465 1392 .arg(m_maxy)
Chris@465 1393 .arg(m_invertVertical ? "true" : "false")
Chris@465 1394 .arg(m_opaque ? "true" : "false");
Chris@197 1395
Chris@316 1396 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@197 1397 }
Chris@197 1398
Chris@197 1399 void
Chris@197 1400 Colour3DPlotLayer::setProperties(const QXmlAttributes &attributes)
Chris@197 1401 {
Chris@445 1402 bool ok = false, alsoOk = false;
Chris@197 1403
Chris@197 1404 ColourScale scale = (ColourScale)attributes.value("scale").toInt(&ok);
Chris@197 1405 if (ok) setColourScale(scale);
Chris@197 1406
Chris@197 1407 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 1408 if (ok) setColourMap(colourMap);
Chris@197 1409
Chris@197 1410 bool normalizeColumns =
Chris@197 1411 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@197 1412 setNormalizeColumns(normalizeColumns);
Chris@197 1413
Chris@197 1414 bool normalizeVisibleArea =
Chris@197 1415 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@197 1416 setNormalizeVisibleArea(normalizeVisibleArea);
Chris@445 1417
Chris@465 1418 bool invertVertical =
Chris@465 1419 (attributes.value("invertVertical").trimmed() == "true");
Chris@465 1420 setInvertVertical(invertVertical);
Chris@465 1421
Chris@465 1422 bool opaque =
Chris@465 1423 (attributes.value("opaque").trimmed() == "true");
Chris@465 1424 setNormalizeVisibleArea(opaque);
Chris@465 1425
Chris@445 1426 float min = attributes.value("minY").toFloat(&ok);
Chris@445 1427 float max = attributes.value("maxY").toFloat(&alsoOk);
Chris@445 1428 if (ok && alsoOk) setDisplayExtents(min, max);
Chris@197 1429 }
Chris@197 1430