annotate layer/Colour3DPlotLayer.cpp @ 514:4ba0476ebbb6

* Activity log updates * Update range lists in play source when a new model is added * Fill cache with empty background in colour 3d plot
author Chris Cannam
date Fri, 27 Feb 2009 13:33:16 +0000
parents f8b98f63f200
children 43bd47468005
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@224 765 // std::cerr << "Colour3DPlotLayer::fillCache: " << firstBin << " -> " << lastBin << std::endl;
Chris@197 766
Chris@461 767 size_t modelStartBin = modelStart / modelResolution;
Chris@461 768 size_t modelEndBin = modelEnd / modelResolution;
Chris@461 769
Chris@461 770 size_t cacheWidth = modelEndBin - modelStartBin + 1;
Chris@471 771 if (lastBin > modelEndBin) cacheWidth = lastBin - modelStartBin + 1;
Chris@461 772 size_t cacheHeight = m_model->getHeight();
Chris@461 773
Chris@461 774 if (m_cache && (m_cache->height() != int(cacheHeight))) {
Chris@461 775 delete m_cache;
Chris@469 776 delete m_peaksCache;
Chris@461 777 m_cache = 0;
Chris@469 778 m_peaksCache = 0;
Chris@461 779 }
Chris@461 780
Chris@461 781 if (m_cache && (m_cache->width() != int(cacheWidth))) {
Chris@469 782 QImage *newCache =
Chris@469 783 new QImage(m_cache->copy(0, 0, cacheWidth, cacheHeight));
Chris@461 784 delete m_cache;
Chris@461 785 m_cache = newCache;
Chris@469 786 if (m_peaksCache) {
Chris@469 787 QImage *newPeaksCache =
Chris@469 788 new QImage(m_peaksCache->copy
Chris@469 789 (0, 0, cacheWidth / m_peakResolution, cacheHeight));
Chris@469 790 delete m_peaksCache;
Chris@469 791 m_peaksCache = newPeaksCache;
Chris@469 792 }
Chris@197 793 }
Chris@197 794
Chris@461 795 if (!m_cache) {
Chris@469 796 m_cache = new QImage
Chris@469 797 (cacheWidth, cacheHeight, QImage::Format_Indexed8);
Chris@461 798 m_cache->setNumColors(256);
Chris@514 799 m_cache->fill(0);
Chris@474 800 if (!m_normalizeVisibleArea) {
Chris@469 801 m_peaksCache = new QImage
Chris@469 802 (cacheWidth / m_peakResolution + 1, cacheHeight,
Chris@469 803 QImage::Format_Indexed8);
Chris@469 804 m_peaksCache->setNumColors(256);
Chris@514 805 m_peaksCache->fill(0);
Chris@469 806 } else if (m_peaksCache) {
Chris@469 807 delete m_peaksCache;
Chris@469 808 m_peaksCache = 0;
Chris@469 809 }
Chris@461 810 m_cacheValidStart = 0;
Chris@461 811 m_cacheValidEnd = 0;
Chris@197 812 }
Chris@197 813
Chris@461 814 if (m_cacheValidStart <= firstBin && m_cacheValidEnd >= lastBin) {
Chris@461 815 return;
Chris@461 816 }
Chris@461 817
Chris@461 818 size_t fillStart = firstBin;
Chris@461 819 size_t fillEnd = lastBin;
Chris@197 820
Chris@461 821 if (fillStart < modelStartBin) fillStart = modelStartBin;
Chris@461 822 if (fillStart > modelEndBin) fillStart = modelEndBin;
Chris@461 823 if (fillEnd < modelStartBin) fillEnd = modelStartBin;
Chris@461 824 if (fillEnd > modelEndBin) fillEnd = modelEndBin;
Chris@197 825
Chris@461 826 bool normalizeVisible = (m_normalizeVisibleArea && !m_normalizeColumns);
Chris@197 827
Chris@461 828 if (!normalizeVisible && (m_cacheValidStart < m_cacheValidEnd)) {
Chris@461 829
Chris@461 830 if (m_cacheValidEnd < fillStart) {
Chris@461 831 fillStart = m_cacheValidEnd + 1;
Chris@461 832 }
Chris@461 833 if (m_cacheValidStart > fillEnd) {
Chris@461 834 fillEnd = m_cacheValidStart - 1;
Chris@461 835 }
Chris@461 836
Chris@461 837 m_cacheValidStart = std::min(fillStart, m_cacheValidStart);
Chris@461 838 m_cacheValidEnd = std::max(fillEnd, m_cacheValidEnd);
Chris@461 839
Chris@461 840 } else {
Chris@461 841
Chris@461 842 // the only valid area, ever, is the currently visible one
Chris@461 843
Chris@461 844 m_cacheValidStart = fillStart;
Chris@461 845 m_cacheValidEnd = fillEnd;
Chris@461 846 }
Chris@461 847
Chris@461 848 // std::cerr << "Cache size " << cacheWidth << "x" << cacheHeight << " will be valid from " << m_cacheValidStart << " to " << m_cacheValidEnd << std::endl;
Chris@461 849
Chris@197 850 DenseThreeDimensionalModel::Column values;
Chris@197 851
Chris@197 852 float min = m_model->getMinimumLevel();
Chris@197 853 float max = m_model->getMaximumLevel();
Chris@197 854
Chris@197 855 if (m_colourScale == LogScale) {
Chris@197 856 LogRange::mapRange(min, max);
Chris@197 857 } else if (m_colourScale == PlusMinusOneScale) {
Chris@197 858 min = -1.f;
Chris@197 859 max = 1.f;
Chris@509 860 } else if (m_colourScale == AbsoluteScale) {
Chris@509 861 if (min < 0) {
Chris@509 862 if (fabsf(min) > fabsf(max)) max = fabsf(min);
Chris@509 863 else max = fabsf(max);
Chris@509 864 min = 0;
Chris@509 865 } else {
Chris@509 866 min = fabsf(min);
Chris@509 867 max = fabsf(max);
Chris@509 868 }
Chris@197 869 }
Chris@197 870
Chris@197 871 if (max == min) max = min + 1.0;
Chris@197 872
Chris@197 873 ColourMapper mapper(m_colourMap, 0.f, 255.f);
Chris@197 874
Chris@197 875 for (int index = 0; index < 256; ++index) {
Chris@197 876 QColor colour = mapper.map(index);
Chris@469 877 m_cache->setColor
Chris@469 878 (index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@469 879 if (m_peaksCache) {
Chris@469 880 m_peaksCache->setColor
Chris@469 881 (index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@469 882 }
Chris@197 883 }
Chris@197 884
Chris@224 885 float visibleMax = 0.f, visibleMin = 0.f;
Chris@197 886
Chris@461 887 if (normalizeVisible) {
Chris@197 888
Chris@461 889 for (size_t c = fillStart; c <= fillEnd; ++c) {
Chris@197 890
Chris@467 891 values = getColumn(c);
Chris@197 892
Chris@224 893 float colMax = 0.f, colMin = 0.f;
Chris@197 894
Chris@461 895 for (size_t y = 0; y < cacheHeight; ++y) {
Chris@197 896 if (y >= values.size()) break;
Chris@197 897 if (y == 0 || values[y] > colMax) colMax = values[y];
Chris@224 898 if (y == 0 || values[y] < colMin) colMin = values[y];
Chris@197 899 }
Chris@197 900
Chris@461 901 if (c == fillStart || colMax > visibleMax) visibleMax = colMax;
Chris@461 902 if (c == fillStart || colMin < visibleMin) visibleMin = colMin;
Chris@461 903 }
Chris@461 904
Chris@461 905 if (m_colourScale == LogScale) {
Chris@461 906 visibleMin = LogRange::map(visibleMin);
Chris@461 907 visibleMax = LogRange::map(visibleMax);
Chris@461 908 if (visibleMin > visibleMax) std::swap(visibleMin, visibleMax);
Chris@509 909 } else if (m_colourScale == AbsoluteScale) {
Chris@509 910 if (visibleMin < 0) {
Chris@509 911 if (fabsf(visibleMin) > fabsf(visibleMax)) visibleMax = fabsf(visibleMin);
Chris@509 912 else visibleMax = fabsf(visibleMax);
Chris@509 913 visibleMin = 0;
Chris@509 914 } else {
Chris@509 915 visibleMin = fabsf(visibleMin);
Chris@509 916 visibleMax = fabsf(visibleMax);
Chris@509 917 }
Chris@197 918 }
Chris@197 919 }
Chris@197 920
Chris@224 921 if (visibleMin == visibleMax) visibleMax = visibleMin + 1;
Chris@224 922
Chris@469 923 int *peaks = 0;
Chris@469 924 if (m_peaksCache) {
Chris@469 925 peaks = new int[cacheHeight];
Chris@469 926 for (int y = 0; y < cacheHeight; ++y) {
Chris@469 927 peaks[y] = 0;
Chris@469 928 }
Chris@469 929 }
Chris@469 930
Chris@461 931 for (size_t c = fillStart; c <= fillEnd; ++c) {
Chris@197 932
Chris@467 933 values = getColumn(c);
Chris@197 934
Chris@461 935 for (size_t y = 0; y < cacheHeight; ++y) {
Chris@197 936
Chris@197 937 float value = min;
Chris@197 938 if (y < values.size()) {
Chris@469 939 value = values.at(y);
Chris@197 940 }
Chris@224 941
Chris@224 942 if (m_colourScale == LogScale) {
Chris@224 943 value = LogRange::map(value);
Chris@509 944 } else if (m_colourScale == AbsoluteScale) {
Chris@509 945 value = fabsf(value);
Chris@197 946 }
Chris@461 947
Chris@461 948 if (normalizeVisible) {
Chris@461 949 float norm = (value - visibleMin) / (visibleMax - visibleMin);
Chris@461 950 value = min + (max - min) * norm;
Chris@461 951 }
Chris@197 952
Chris@197 953 int pixel = int(((value - min) * 256) / (max - min));
Chris@197 954 if (pixel < 0) pixel = 0;
Chris@197 955 if (pixel > 255) pixel = 255;
Chris@469 956 if (peaks && (pixel > peaks[y])) peaks[y] = pixel;
Chris@197 957
Chris@357 958 if (m_invertVertical) {
Chris@461 959 m_cache->setPixel(c, cacheHeight - y - 1, pixel);
Chris@357 960 } else {
Chris@461 961 m_cache->setPixel(c, y, pixel);
Chris@357 962 }
Chris@197 963 }
Chris@469 964
Chris@469 965 if (peaks) {
Chris@469 966 size_t notch = (c % m_peakResolution);
Chris@469 967 if (notch == m_peakResolution-1 || c == fillEnd) {
Chris@469 968 size_t pc = c / m_peakResolution;
Chris@469 969 for (size_t y = 0; y < cacheHeight; ++y) {
Chris@469 970 if (m_invertVertical) {
Chris@469 971 m_peaksCache->setPixel(pc, cacheHeight - y - 1, peaks[y]);
Chris@469 972 } else {
Chris@469 973 m_peaksCache->setPixel(pc, y, peaks[y]);
Chris@469 974 }
Chris@469 975 }
Chris@469 976 for (int y = 0; y < cacheHeight; ++y) {
Chris@469 977 peaks[y] = 0;
Chris@469 978 }
Chris@469 979 }
Chris@469 980 }
Chris@197 981 }
Chris@469 982
Chris@469 983 delete[] peaks;
Chris@197 984 }
Chris@197 985
Chris@197 986 void
Chris@44 987 Colour3DPlotLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 988 {
Chris@514 989 /*
Chris@443 990 if (m_model) {
Chris@443 991 std::cerr << "Colour3DPlotLayer::paint: model says shouldUseLogValueScale = " << m_model->shouldUseLogValueScale() << std::endl;
Chris@443 992 }
Chris@514 993 */
Chris@466 994 Profiler profiler("Colour3DPlotLayer::paint");
Chris@125 995 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@125 996 std::cerr << "Colour3DPlotLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << std::endl;
Chris@125 997 #endif
Chris@0 998
Chris@0 999 int completion = 0;
Chris@0 1000 if (!m_model || !m_model->isOK() || !m_model->isReady(&completion)) {
Chris@0 1001 if (completion > 0) {
Chris@44 1002 paint.fillRect(0, 10, v->width() * completion / 100,
Chris@0 1003 10, QColor(120, 120, 120));
Chris@0 1004 }
Chris@0 1005 return;
Chris@0 1006 }
Chris@0 1007
Chris@197 1008 if (m_normalizeVisibleArea && !m_normalizeColumns) rect = v->rect();
Chris@197 1009
Chris@0 1010 size_t modelStart = m_model->getStartFrame();
Chris@0 1011 size_t modelEnd = m_model->getEndFrame();
Chris@130 1012 size_t modelResolution = m_model->getResolution();
Chris@0 1013
Chris@197 1014 // The cache is from the model's start frame to the model's end
Chris@197 1015 // frame at the model's window increment frames per pixel. We
Chris@197 1016 // want to draw from our start frame + x0 * zoomLevel to our start
Chris@197 1017 // frame + x1 * zoomLevel at zoomLevel frames per pixel.
Chris@12 1018
Chris@197 1019 // We have quite different paint mechanisms for rendering "large"
Chris@197 1020 // bins (more than one bin per pixel in both directions) and
Chris@197 1021 // "small". This is "large"; see paintDense below for "small".
Chris@12 1022
Chris@197 1023 int x0 = rect.left();
Chris@197 1024 int x1 = rect.right() + 1;
Chris@12 1025
Chris@197 1026 int h = v->height();
Chris@0 1027
Chris@197 1028 float srRatio =
Chris@197 1029 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@197 1030 float(m_model->getSampleRate());
Chris@0 1031
Chris@197 1032 int sx0 = int((v->getFrameForX(x0) / srRatio - long(modelStart))
Chris@197 1033 / long(modelResolution));
Chris@197 1034 int sx1 = int((v->getFrameForX(x1) / srRatio - long(modelStart))
Chris@197 1035 / long(modelResolution));
Chris@197 1036 int sh = m_model->getHeight();
Chris@159 1037
Chris@444 1038 int symin = m_miny;
Chris@444 1039 int symax = m_maxy;
Chris@444 1040 if (symax <= symin) {
Chris@444 1041 symin = 0;
Chris@444 1042 symax = sh;
Chris@444 1043 }
Chris@444 1044 if (symin < 0) symin = 0;
Chris@444 1045 if (symax > sh) symax = sh;
Chris@444 1046
Chris@197 1047 if (sx0 > 0) --sx0;
Chris@197 1048 fillCache(sx0 < 0 ? 0 : sx0,
Chris@197 1049 sx1 < 0 ? 0 : sx1);
Chris@0 1050
Chris@351 1051 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@466 1052 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 1053 #endif
Chris@351 1054
Chris@465 1055 if (m_opaque ||
Chris@465 1056 int(m_model->getHeight()) >= v->height() ||
Chris@466 1057 ((modelResolution * srRatio) / v->getZoomLevel()) < 2) {
Chris@347 1058 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@351 1059 std::cerr << "calling paintDense" << std::endl;
Chris@347 1060 #endif
Chris@98 1061 paintDense(v, paint, rect);
Chris@98 1062 return;
Chris@98 1063 }
Chris@98 1064
Chris@125 1065 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@347 1066 std::cerr << "Colour3DPlotLayer::paint: w " << x1-x0 << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sx1-sx0 << ", sh " << sh << std::endl;
Chris@130 1067 std::cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << std::endl;
Chris@125 1068 #endif
Chris@0 1069
Chris@25 1070 QPoint illuminatePos;
Chris@44 1071 bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos);
Chris@54 1072 char labelbuf[10];
Chris@25 1073
Chris@197 1074 for (int sx = sx0; sx <= sx1; ++sx) {
Chris@0 1075
Chris@130 1076 int fx = sx * int(modelResolution);
Chris@0 1077
Chris@351 1078 if (fx + int(modelResolution) <= int(modelStart) ||
Chris@0 1079 fx > int(modelEnd)) continue;
Chris@0 1080
Chris@248 1081 int rx0 = v->getXForFrame(int((fx + int(modelStart)) * srRatio));
Chris@248 1082 int rx1 = v->getXForFrame(int((fx + int(modelStart) + int(modelResolution) + 1) * srRatio));
Chris@54 1083
Chris@159 1084 int rw = rx1 - rx0;
Chris@159 1085 if (rw < 1) rw = 1;
Chris@54 1086
Chris@159 1087 bool showLabel = (rw > 10 &&
Chris@159 1088 paint.fontMetrics().width("0.000000") < rw - 3 &&
Chris@54 1089 paint.fontMetrics().height() < (h / sh));
Chris@98 1090
Chris@444 1091 for (int sy = symin; sy < symax; ++sy) {
Chris@0 1092
Chris@444 1093 int ry0 = h - ((sy - symin) * h) / (symax - symin) - 1;
Chris@0 1094 QRgb pixel = qRgb(255, 255, 255);
Chris@461 1095 if (sx >= 0 && sx < m_cache->width() &&
Chris@0 1096 sy >= 0 && sy < m_cache->height()) {
Chris@461 1097 pixel = m_cache->pixel(sx, sy);
Chris@0 1098 }
Chris@0 1099
Chris@509 1100 QRect r(rx0, ry0 - h / (symax - symin),
Chris@444 1101 rw, h / (symax - symin) + 1);
Chris@159 1102
Chris@159 1103 if (rw == 1) {
Chris@159 1104 paint.setPen(pixel);
Chris@159 1105 paint.setBrush(Qt::NoBrush);
Chris@159 1106 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
Chris@159 1107 continue;
Chris@159 1108 }
Chris@159 1109
Chris@0 1110 QColor pen(255, 255, 255, 80);
Chris@0 1111 QColor brush(pixel);
Chris@159 1112
Chris@159 1113 if (rw > 3 && r.height() > 3) {
Chris@159 1114 brush.setAlpha(160);
Chris@159 1115 }
Chris@159 1116
Chris@0 1117 paint.setPen(Qt::NoPen);
Chris@0 1118 paint.setBrush(brush);
Chris@0 1119
Chris@25 1120 if (illuminate) {
Chris@25 1121 if (r.contains(illuminatePos)) {
Chris@287 1122 paint.setPen(v->getForeground());
Chris@25 1123 }
Chris@25 1124 }
Chris@76 1125
Chris@125 1126 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@159 1127 std::cerr << "rect " << r.x() << "," << r.y() << " "
Chris@159 1128 << r.width() << "x" << r.height() << std::endl;
Chris@125 1129 #endif
Chris@25 1130
Chris@25 1131 paint.drawRect(r);
Chris@0 1132
Chris@54 1133 if (showLabel) {
Chris@461 1134 if (sx >= 0 && sx < m_cache->width() &&
Chris@54 1135 sy >= 0 && sy < m_cache->height()) {
Chris@461 1136 float value = m_model->getValueAt(sx, sy);
Chris@54 1137 sprintf(labelbuf, "%06f", value);
Chris@54 1138 QString text(labelbuf);
Chris@287 1139 paint.setPen(v->getBackground());
Chris@54 1140 paint.drawText(rx0 + 2,
Chris@54 1141 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
Chris@54 1142 text);
Chris@0 1143 }
Chris@0 1144 }
Chris@0 1145 }
Chris@0 1146 }
Chris@98 1147 }
Chris@0 1148
Chris@98 1149 void
Chris@98 1150 Colour3DPlotLayer::paintDense(View *v, QPainter &paint, QRect rect) const
Chris@98 1151 {
Chris@466 1152 Profiler profiler("Colour3DPlotLayer::paintDense");
Chris@463 1153 if (!m_cache) return;
Chris@463 1154
Chris@466 1155 float modelStart = m_model->getStartFrame();
Chris@466 1156 float modelResolution = m_model->getResolution();
Chris@0 1157
Chris@469 1158 int mmsr = v->getViewManager()->getMainModelSampleRate();
Chris@469 1159 int msr = m_model->getSampleRate();
Chris@469 1160 float srRatio = float(mmsr) / float(msr);
Chris@159 1161
Chris@98 1162 int x0 = rect.left();
Chris@98 1163 int x1 = rect.right() + 1;
Chris@0 1164
Chris@470 1165 const int w = x1 - x0; // const so it can be used as array size below
Chris@471 1166 int h = v->height(); // we always paint full height
Chris@160 1167 int sh = m_model->getHeight();
Chris@98 1168
Chris@444 1169 int symin = m_miny;
Chris@444 1170 int symax = m_maxy;
Chris@444 1171 if (symax <= symin) {
Chris@444 1172 symin = 0;
Chris@444 1173 symax = sh;
Chris@444 1174 }
Chris@444 1175 if (symin < 0) symin = 0;
Chris@444 1176 if (symax > sh) symax = sh;
Chris@444 1177
Chris@466 1178 QImage img(w, h, QImage::Format_Indexed8);
Chris@466 1179 img.setColorTable(m_cache->colorTable());
Chris@98 1180
Chris@466 1181 uchar *peaks = new uchar[w];
Chris@466 1182 memset(peaks, 0, w);
Chris@98 1183
Chris@469 1184 int zoomLevel = v->getZoomLevel();
Chris@469 1185
Chris@469 1186 QImage *source = m_cache;
Chris@474 1187
Chris@474 1188 std::cerr << "modelResolution " << modelResolution << ", srRatio "
Chris@474 1189 << srRatio << ", m_peakResolution " << m_peakResolution
Chris@474 1190 << ", zoomLevel " << zoomLevel << ", result "
Chris@474 1191 << ((modelResolution * srRatio * m_peakResolution) / zoomLevel)
Chris@474 1192 << std::endl;
Chris@474 1193
Chris@474 1194 if (m_peaksCache) {
Chris@474 1195 if (((modelResolution * srRatio * m_peakResolution) / zoomLevel) < 1) {
Chris@474 1196 std::cerr << "using peaks cache" << std::endl;
Chris@474 1197 source = m_peaksCache;
Chris@474 1198 modelResolution *= m_peakResolution;
Chris@474 1199 } else {
Chris@474 1200 std::cerr << "not using peaks cache" << std::endl;
Chris@474 1201 }
Chris@469 1202 } else {
Chris@474 1203 std::cerr << "have no peaks cache" << std::endl;
Chris@469 1204 }
Chris@469 1205
Chris@466 1206 int psy1i = -1;
Chris@469 1207 int sw = source->width();
Chris@470 1208
Chris@470 1209 long xf = -1;
Chris@470 1210 long nxf = v->getFrameForX(x0);
Chris@470 1211
Chris@470 1212 int sxa[w * 2];
Chris@470 1213 for (int x = 0; x < w; ++x) {
Chris@470 1214
Chris@470 1215 xf = nxf;
Chris@470 1216 nxf = xf + zoomLevel;
Chris@470 1217
Chris@470 1218 float sx0 = (float(xf) / srRatio - modelStart) / modelResolution;
Chris@470 1219 float sx1 = (float(nxf) / srRatio - modelStart) / modelResolution;
Chris@470 1220
Chris@470 1221 int sx0i = int(sx0 + 0.001);
Chris@470 1222 int sx1i = int(sx1);
Chris@470 1223
Chris@470 1224 sxa[x*2] = sx0i;
Chris@470 1225 sxa[x*2 + 1] = sx1i;
Chris@470 1226 }
Chris@466 1227
Chris@466 1228 for (int y = 0; y < h; ++y) {
Chris@466 1229
Chris@466 1230 float sy0 = symin + (float(h - y - 1) * (symax - symin)) / h;
Chris@466 1231 float sy1 = symin + (float(h - y) * (symax - symin)) / h;
Chris@466 1232
Chris@466 1233 int sy0i = int(sy0 + 0.001);
Chris@466 1234 int sy1i = int(sy1);
Chris@466 1235
Chris@466 1236 uchar *targetLine = img.scanLine(y);
Chris@466 1237
Chris@466 1238 if (sy0i == sy1i && sy0i == psy1i) { // same scan line as just computed
Chris@466 1239 goto copy;
Chris@105 1240 }
Chris@105 1241
Chris@471 1242 for (int x = 0; x < w; ++x) {
Chris@471 1243 peaks[x] = 0;
Chris@471 1244 }
Chris@466 1245
Chris@466 1246 for (int sy = sy0i; sy <= sy1i; ++sy) {
Chris@347 1247
Chris@469 1248 if (sy < 0 || sy >= source->height()) continue;
Chris@466 1249
Chris@469 1250 uchar *sourceLine = source->scanLine(sy);
Chris@98 1251
Chris@466 1252 for (int x = 0; x < w; ++x) {
Chris@98 1253
Chris@474 1254 int sx1i = sxa[x*2 + 1];
Chris@474 1255 if (sx1i < 0) continue;
Chris@474 1256
Chris@470 1257 int sx0i = sxa[x*2];
Chris@474 1258 if (sx0i >= sw) break;
Chris@98 1259
Chris@466 1260 uchar peak = 0;
Chris@466 1261 for (int sx = sx0i; sx <= sx1i; ++sx) {
Chris@469 1262 if (sx < 0 || sx >= sw) continue;
Chris@466 1263 if (sourceLine[sx] > peak) peak = sourceLine[sx];
Chris@98 1264 }
Chris@466 1265 peaks[x] = peak;
Chris@98 1266 }
Chris@466 1267 }
Chris@466 1268
Chris@466 1269 copy:
Chris@466 1270 for (int x = 0; x < w; ++x) {
Chris@466 1271 targetLine[x] = peaks[x];
Chris@98 1272 }
Chris@0 1273 }
Chris@0 1274
Chris@469 1275 delete[] peaks;
Chris@469 1276
Chris@98 1277 paint.drawImage(x0, 0, img);
Chris@0 1278 }
Chris@0 1279
Chris@472 1280 void
Chris@472 1281 Colour3DPlotLayer::paintSmooth(View *v, QPainter &paint, QRect rect) const
Chris@472 1282 {
Chris@472 1283 Profiler profiler("Colour3DPlotLayer:paintSmooth");
Chris@472 1284 if (!m_cache) return;
Chris@472 1285
Chris@472 1286 float modelStart = m_model->getStartFrame();
Chris@472 1287 float modelResolution = m_model->getResolution();
Chris@472 1288
Chris@472 1289 int mmsr = v->getViewManager()->getMainModelSampleRate();
Chris@472 1290 int msr = m_model->getSampleRate();
Chris@472 1291 float srRatio = float(mmsr) / float(msr);
Chris@472 1292
Chris@472 1293 int x0 = rect.left();
Chris@472 1294 int x1 = rect.right() + 1;
Chris@472 1295
Chris@472 1296 int h = v->height(); // we always paint full height
Chris@472 1297 int sh = m_model->getHeight();
Chris@472 1298
Chris@472 1299 int symin = m_miny;
Chris@472 1300 int symax = m_maxy;
Chris@472 1301 if (symax <= symin) {
Chris@472 1302 symin = 0;
Chris@472 1303 symax = sh;
Chris@472 1304 }
Chris@472 1305 if (symin < 0) symin = 0;
Chris@472 1306 if (symax > sh) symax = sh;
Chris@472 1307
Chris@472 1308 // QImage img(w, h, QImage::Format_Indexed8);
Chris@472 1309 // img.setColorTable(m_cache->colorTable());
Chris@472 1310
Chris@472 1311 int zoomLevel = v->getZoomLevel();
Chris@472 1312
Chris@472 1313 QImage *source = m_cache;
Chris@472 1314 if (m_peaksCache &&
Chris@472 1315 ((modelResolution * srRatio * m_peakResolution) / zoomLevel) < 1) {
Chris@472 1316 std::cerr << "using peaks cache" << std::endl;
Chris@472 1317 source = m_peaksCache;
Chris@472 1318 modelResolution *= m_peakResolution;
Chris@472 1319 } else {
Chris@472 1320 std::cerr << "not using peaks cache" << std::endl;
Chris@472 1321 }
Chris@472 1322
Chris@472 1323 float sx0 = (float(v->getFrameForX(x0)) / srRatio - modelStart) / modelResolution;
Chris@472 1324 float sx1 = (float(v->getFrameForX(x1)) / srRatio - modelStart) / modelResolution;
Chris@472 1325 int sx0i = int(sx0 + 0.001);
Chris@472 1326 int sx1i = int(sx1);
Chris@472 1327
Chris@472 1328 if (sx0i < 0) sx0i = 0;
Chris@472 1329 if (sx0i > source->width()) sx0i = source->width();
Chris@472 1330
Chris@472 1331 int tx0 = v->getXForFrame(((sx0i * modelResolution) + modelStart) * srRatio + 0.001);
Chris@472 1332 int tx1 = v->getXForFrame(((sx1i * modelResolution) + modelStart) * srRatio);
Chris@472 1333
Chris@472 1334 std::cerr << "x0 " << x0 << ", x1 " << x1 << " -> sx0 " << sx0i << ", sx1 " << sx1i << " -> tx0 " << tx0 << ", tx1 " << tx1 << std::endl;
Chris@472 1335
Chris@472 1336 QImage img = source->copy(sx0i, 0, sx1i - sx0i, source->height())
Chris@472 1337 .scaled(QSize(tx1 - tx0, h),
Chris@472 1338 Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
Chris@472 1339 paint.drawImage(x0, 0, img);
Chris@472 1340 }
Chris@472 1341
Chris@28 1342 bool
Chris@44 1343 Colour3DPlotLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 1344 size_t &resolution,
Chris@28 1345 SnapType snap) const
Chris@24 1346 {
Chris@24 1347 if (!m_model) {
Chris@44 1348 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@24 1349 }
Chris@24 1350
Chris@130 1351 resolution = m_model->getResolution();
Chris@28 1352 int left = (frame / resolution) * resolution;
Chris@28 1353 int right = left + resolution;
Chris@28 1354
Chris@28 1355 switch (snap) {
Chris@28 1356 case SnapLeft: frame = left; break;
Chris@28 1357 case SnapRight: frame = right; break;
Chris@28 1358 case SnapNearest:
Chris@28 1359 case SnapNeighbouring:
Chris@28 1360 if (frame - left > right - frame) frame = right;
Chris@28 1361 else frame = left;
Chris@28 1362 break;
Chris@28 1363 }
Chris@24 1364
Chris@28 1365 return true;
Chris@24 1366 }
Chris@24 1367
Chris@316 1368 void
Chris@316 1369 Colour3DPlotLayer::toXml(QTextStream &stream,
Chris@316 1370 QString indent, QString extraAttributes) const
Chris@197 1371 {
Chris@316 1372 QString s = QString("scale=\"%1\" "
Chris@316 1373 "colourScheme=\"%2\" "
Chris@316 1374 "normalizeColumns=\"%3\" "
Chris@445 1375 "normalizeVisibleArea=\"%4\" "
Chris@445 1376 "minY=\"%5\" "
Chris@465 1377 "maxY=\"%6\" "
Chris@465 1378 "invertVertical=\"%7\" "
Chris@465 1379 "opaque=\"%8\"")
Chris@197 1380 .arg((int)m_colourScale)
Chris@197 1381 .arg(m_colourMap)
Chris@197 1382 .arg(m_normalizeColumns ? "true" : "false")
Chris@445 1383 .arg(m_normalizeVisibleArea ? "true" : "false")
Chris@445 1384 .arg(m_miny)
Chris@465 1385 .arg(m_maxy)
Chris@465 1386 .arg(m_invertVertical ? "true" : "false")
Chris@465 1387 .arg(m_opaque ? "true" : "false");
Chris@197 1388
Chris@316 1389 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@197 1390 }
Chris@197 1391
Chris@197 1392 void
Chris@197 1393 Colour3DPlotLayer::setProperties(const QXmlAttributes &attributes)
Chris@197 1394 {
Chris@445 1395 bool ok = false, alsoOk = false;
Chris@197 1396
Chris@197 1397 ColourScale scale = (ColourScale)attributes.value("scale").toInt(&ok);
Chris@197 1398 if (ok) setColourScale(scale);
Chris@197 1399
Chris@197 1400 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 1401 if (ok) setColourMap(colourMap);
Chris@197 1402
Chris@197 1403 bool normalizeColumns =
Chris@197 1404 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@197 1405 setNormalizeColumns(normalizeColumns);
Chris@197 1406
Chris@197 1407 bool normalizeVisibleArea =
Chris@197 1408 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@197 1409 setNormalizeVisibleArea(normalizeVisibleArea);
Chris@445 1410
Chris@465 1411 bool invertVertical =
Chris@465 1412 (attributes.value("invertVertical").trimmed() == "true");
Chris@465 1413 setInvertVertical(invertVertical);
Chris@465 1414
Chris@465 1415 bool opaque =
Chris@465 1416 (attributes.value("opaque").trimmed() == "true");
Chris@465 1417 setNormalizeVisibleArea(opaque);
Chris@465 1418
Chris@445 1419 float min = attributes.value("minY").toFloat(&ok);
Chris@445 1420 float max = attributes.value("maxY").toFloat(&alsoOk);
Chris@445 1421 if (ok && alsoOk) setDisplayExtents(min, max);
Chris@197 1422 }
Chris@197 1423