annotate layer/Colour3DPlotLayer.cpp @ 510:683c46d7500b

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