annotate layer/Colour3DPlotLayer.cpp @ 474:efc2bd6d3611

* take a bit more care about peaks cache sizing
author Chris Cannam
date Thu, 29 Jan 2009 14:17:12 +0000
parents 13e8edbffbd7
children 3f4b10e76ccd
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@197 215 *max = 2;
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@159 268 }
Chris@159 269 }
Chris@159 270 return tr("<unknown>");
Chris@159 271 }
Chris@159 272
Chris@159 273 void
Chris@159 274 Colour3DPlotLayer::setProperty(const PropertyName &name, int value)
Chris@159 275 {
Chris@159 276 if (name == "Colour Scale") {
Chris@159 277 switch (value) {
Chris@159 278 default:
Chris@159 279 case 0: setColourScale(LinearScale); break;
Chris@197 280 case 1: setColourScale(LogScale); break;
Chris@197 281 case 2: setColourScale(PlusMinusOneScale); break;
Chris@159 282 }
Chris@197 283 } else if (name == "Colour") {
Chris@197 284 setColourMap(value);
Chris@197 285 } else if (name == "Normalize Columns") {
Chris@197 286 setNormalizeColumns(value ? true : false);
Chris@197 287 } else if (name == "Normalize Visible Area") {
Chris@197 288 setNormalizeVisibleArea(value ? true : false);
Chris@357 289 } else if (name == "Invert Vertical Scale") {
Chris@357 290 setInvertVertical(value ? true : false);
Chris@465 291 } else if (name == "Opaque") {
Chris@465 292 setOpaque(value ? true : false);
Chris@159 293 }
Chris@159 294 }
Chris@159 295
Chris@159 296 void
Chris@159 297 Colour3DPlotLayer::setColourScale(ColourScale scale)
Chris@159 298 {
Chris@159 299 if (m_colourScale == scale) return;
Chris@159 300 m_colourScale = scale;
Chris@461 301 m_colourScaleSet = true;
Chris@159 302 cacheInvalid();
Chris@159 303 emit layerParametersChanged();
Chris@159 304 }
Chris@159 305
Chris@197 306 void
Chris@197 307 Colour3DPlotLayer::setColourMap(int map)
Chris@197 308 {
Chris@197 309 if (m_colourMap == map) return;
Chris@197 310 m_colourMap = map;
Chris@197 311 cacheInvalid();
Chris@197 312 emit layerParametersChanged();
Chris@197 313 }
Chris@197 314
Chris@197 315 void
Chris@197 316 Colour3DPlotLayer::setNormalizeColumns(bool n)
Chris@197 317 {
Chris@197 318 if (m_normalizeColumns == n) return;
Chris@197 319 m_normalizeColumns = n;
Chris@197 320 cacheInvalid();
Chris@197 321 emit layerParametersChanged();
Chris@197 322 }
Chris@197 323
Chris@197 324 bool
Chris@197 325 Colour3DPlotLayer::getNormalizeColumns() const
Chris@197 326 {
Chris@197 327 return m_normalizeColumns;
Chris@197 328 }
Chris@197 329
Chris@197 330 void
Chris@197 331 Colour3DPlotLayer::setNormalizeVisibleArea(bool n)
Chris@197 332 {
Chris@197 333 if (m_normalizeVisibleArea == n) return;
Chris@197 334 m_normalizeVisibleArea = n;
Chris@197 335 cacheInvalid();
Chris@197 336 emit layerParametersChanged();
Chris@197 337 }
Chris@197 338
Chris@197 339 bool
Chris@197 340 Colour3DPlotLayer::getNormalizeVisibleArea() const
Chris@197 341 {
Chris@197 342 return m_normalizeVisibleArea;
Chris@197 343 }
Chris@197 344
Chris@357 345 void
Chris@357 346 Colour3DPlotLayer::setInvertVertical(bool n)
Chris@357 347 {
Chris@357 348 if (m_invertVertical == n) return;
Chris@357 349 m_invertVertical = n;
Chris@357 350 cacheInvalid();
Chris@357 351 emit layerParametersChanged();
Chris@357 352 }
Chris@357 353
Chris@465 354 void
Chris@465 355 Colour3DPlotLayer::setOpaque(bool n)
Chris@465 356 {
Chris@465 357 if (m_opaque == n) return;
Chris@465 358 m_opaque = n;
Chris@465 359 emit layerParametersChanged();
Chris@465 360 }
Chris@465 361
Chris@357 362 bool
Chris@357 363 Colour3DPlotLayer::getInvertVertical() const
Chris@357 364 {
Chris@357 365 return m_invertVertical;
Chris@357 366 }
Chris@357 367
Chris@25 368 bool
Chris@465 369 Colour3DPlotLayer::getOpaque() const
Chris@465 370 {
Chris@465 371 return m_opaque;
Chris@465 372 }
Chris@465 373
Chris@465 374 bool
Chris@44 375 Colour3DPlotLayer::isLayerScrollable(const View *v) const
Chris@25 376 {
Chris@197 377 if (m_normalizeVisibleArea) return false;
Chris@25 378 QPoint discard;
Chris@44 379 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@25 380 }
Chris@25 381
Chris@444 382 bool
Chris@444 383 Colour3DPlotLayer::getValueExtents(float &min, float &max,
Chris@444 384 bool &logarithmic, QString &unit) const
Chris@444 385 {
Chris@444 386 if (!m_model) return false;
Chris@444 387
Chris@444 388 min = 0;
Chris@444 389 max = m_model->getHeight();
Chris@444 390
Chris@444 391 logarithmic = false;
Chris@444 392 unit = "";
Chris@444 393
Chris@444 394 return true;
Chris@444 395 }
Chris@444 396
Chris@444 397 bool
Chris@444 398 Colour3DPlotLayer::getDisplayExtents(float &min, float &max) const
Chris@444 399 {
Chris@444 400 if (!m_model) return false;
Chris@444 401
Chris@444 402 min = m_miny;
Chris@444 403 max = m_maxy;
Chris@444 404 if (max <= min) {
Chris@444 405 min = 0;
Chris@444 406 max = m_model->getHeight();
Chris@444 407 }
Chris@444 408 if (min < 0) min = 0;
Chris@444 409 if (max > m_model->getHeight()) max = m_model->getHeight();
Chris@444 410
Chris@444 411 return true;
Chris@444 412 }
Chris@444 413
Chris@444 414 bool
Chris@444 415 Colour3DPlotLayer::setDisplayExtents(float min, float max)
Chris@444 416 {
Chris@444 417 if (!m_model) return false;
Chris@444 418
Chris@444 419 m_miny = lrintf(min);
Chris@444 420 m_maxy = lrintf(max);
Chris@444 421
Chris@444 422 emit layerParametersChanged();
Chris@444 423 return true;
Chris@444 424 }
Chris@444 425
Chris@444 426 int
Chris@444 427 Colour3DPlotLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@444 428 {
Chris@444 429 if (!m_model) return 0;
Chris@444 430
Chris@444 431 defaultStep = 0;
Chris@444 432 int h = m_model->getHeight();
Chris@444 433 return h;
Chris@444 434 }
Chris@444 435
Chris@444 436 int
Chris@444 437 Colour3DPlotLayer::getCurrentVerticalZoomStep() const
Chris@444 438 {
Chris@444 439 if (!m_model) return 0;
Chris@444 440
Chris@444 441 float min, max;
Chris@444 442 getDisplayExtents(min, max);
Chris@444 443 return m_model->getHeight() - lrintf(max - min);
Chris@444 444 }
Chris@444 445
Chris@444 446 void
Chris@444 447 Colour3DPlotLayer::setVerticalZoomStep(int step)
Chris@444 448 {
Chris@444 449 if (!m_model) return;
Chris@444 450
Chris@445 451 // std::cerr << "Colour3DPlotLayer::setVerticalZoomStep(" <<step <<"): before: miny = " << m_miny << ", maxy = " << m_maxy << std::endl;
Chris@444 452
Chris@444 453 int dist = m_model->getHeight() - step;
Chris@444 454 if (dist < 1) dist = 1;
Chris@444 455 float centre = m_miny + (float(m_maxy) - float(m_miny)) / 2.f;
Chris@444 456 m_miny = lrintf(centre - float(dist)/2);
Chris@444 457 if (m_miny < 0) m_miny = 0;
Chris@444 458 m_maxy = m_miny + dist;
Chris@444 459 if (m_maxy > m_model->getHeight()) m_maxy = m_model->getHeight();
Chris@444 460
Chris@445 461 // std::cerr << "Colour3DPlotLayer::setVerticalZoomStep(" <<step <<"): after: miny = " << m_miny << ", maxy = " << m_maxy << std::endl;
Chris@444 462
Chris@444 463 emit layerParametersChanged();
Chris@444 464 }
Chris@444 465
Chris@444 466 RangeMapper *
Chris@444 467 Colour3DPlotLayer::getNewVerticalZoomRangeMapper() const
Chris@444 468 {
Chris@444 469 if (!m_model) return 0;
Chris@444 470
Chris@444 471 return new LinearRangeMapper(0, m_model->getHeight(),
Chris@444 472 0, m_model->getHeight(), "");
Chris@444 473 }
Chris@444 474
Chris@25 475 QString
Chris@44 476 Colour3DPlotLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@25 477 {
Chris@25 478 if (!m_model) return "";
Chris@25 479
Chris@25 480 int x = pos.x();
Chris@25 481 int y = pos.y();
Chris@25 482
Chris@25 483 size_t modelStart = m_model->getStartFrame();
Chris@130 484 size_t modelResolution = m_model->getResolution();
Chris@25 485
Chris@159 486 float srRatio =
Chris@159 487 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@159 488 float(m_model->getSampleRate());
Chris@159 489
Chris@160 490 int sx0 = int((v->getFrameForX(x) / srRatio - long(modelStart)) /
Chris@160 491 long(modelResolution));
Chris@25 492
Chris@160 493 int f0 = sx0 * modelResolution;
Chris@160 494 int f1 = f0 + modelResolution;
Chris@160 495
Chris@447 496 int sh = m_model->getHeight();
Chris@447 497
Chris@447 498 int symin = m_miny;
Chris@447 499 int symax = m_maxy;
Chris@447 500 if (symax <= symin) {
Chris@447 501 symin = 0;
Chris@447 502 symax = sh;
Chris@447 503 }
Chris@447 504 if (symin < 0) symin = 0;
Chris@447 505 if (symax > sh) symax = sh;
Chris@447 506
Chris@447 507 float binHeight = float(v->height()) / (symax - symin);
Chris@447 508 int sy = int((v->height() - y) / binHeight) + symin;
Chris@25 509
Chris@357 510 if (m_invertVertical) sy = m_model->getHeight() - sy - 1;
Chris@357 511
Chris@160 512 float value = m_model->getValueAt(sx0, sy);
Chris@159 513
Chris@159 514 // std::cerr << "bin value (" << sx0 << "," << sy << ") is " << value << std::endl;
Chris@25 515
Chris@25 516 QString binName = m_model->getBinName(sy);
Chris@25 517 if (binName == "") binName = QString("[%1]").arg(sy + 1);
Chris@25 518 else binName = QString("%1 [%2]").arg(binName).arg(sy + 1);
Chris@25 519
Chris@25 520 QString text = tr("Time:\t%1 - %2\nBin:\t%3\nValue:\t%4")
Chris@160 521 .arg(RealTime::frame2RealTime(f0, m_model->getSampleRate())
Chris@25 522 .toText(true).c_str())
Chris@160 523 .arg(RealTime::frame2RealTime(f1, m_model->getSampleRate())
Chris@25 524 .toText(true).c_str())
Chris@25 525 .arg(binName)
Chris@25 526 .arg(value);
Chris@25 527
Chris@25 528 return text;
Chris@25 529 }
Chris@25 530
Chris@25 531 int
Chris@248 532 Colour3DPlotLayer::getColourScaleWidth(QPainter &) const
Chris@159 533 {
Chris@159 534 int cw = 20;
Chris@159 535 return cw;
Chris@159 536 }
Chris@159 537
Chris@159 538 int
Chris@248 539 Colour3DPlotLayer::getVerticalScaleWidth(View *, QPainter &paint) const
Chris@25 540 {
Chris@25 541 if (!m_model) return 0;
Chris@25 542
Chris@160 543 QString sampleText = QString("[%1]").arg(m_model->getHeight());
Chris@25 544 int tw = paint.fontMetrics().width(sampleText);
Chris@98 545 bool another = false;
Chris@25 546
Chris@160 547 for (size_t i = 0; i < m_model->getHeight(); ++i) {
Chris@25 548 if (m_model->getBinName(i).length() > sampleText.length()) {
Chris@25 549 sampleText = m_model->getBinName(i);
Chris@98 550 another = true;
Chris@25 551 }
Chris@25 552 }
Chris@98 553 if (another) {
Chris@25 554 tw = std::max(tw, paint.fontMetrics().width(sampleText));
Chris@25 555 }
Chris@25 556
Chris@159 557 return tw + 13 + getColourScaleWidth(paint);
Chris@25 558 }
Chris@25 559
Chris@25 560 void
Chris@44 561 Colour3DPlotLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@25 562 {
Chris@25 563 if (!m_model) return;
Chris@25 564
Chris@25 565 int h = rect.height(), w = rect.width();
Chris@25 566
Chris@159 567 int cw = getColourScaleWidth(paint);
Chris@159 568
Chris@159 569 int ch = h - 20;
Chris@159 570 if (ch > 20 && m_cache) {
Chris@159 571
Chris@447 572 float min = m_model->getMinimumLevel();
Chris@447 573 float max = m_model->getMaximumLevel();
Chris@447 574
Chris@447 575 float mmin = min;
Chris@447 576 float mmax = max;
Chris@447 577
Chris@447 578 if (m_colourScale == LogScale) {
Chris@447 579 LogRange::mapRange(mmin, mmax);
Chris@447 580 } else if (m_colourScale == PlusMinusOneScale) {
Chris@447 581 mmin = -1.f;
Chris@447 582 mmax = 1.f;
Chris@447 583 }
Chris@447 584
Chris@447 585 if (max == min) max = min + 1.0;
Chris@465 586 if (mmax == mmin) mmax = mmin + 1.0;
Chris@447 587
Chris@287 588 paint.setPen(v->getForeground());
Chris@447 589 paint.drawRect(4, 10, cw - 8, ch+1);
Chris@159 590
Chris@446 591 for (int y = 0; y < ch; ++y) {
Chris@447 592 float value = ((max - min) * (ch - y - 1)) / ch + min;
Chris@447 593 if (m_colourScale == LogScale) {
Chris@447 594 value = LogRange::map(value);
Chris@447 595 }
Chris@447 596 int pixel = int(((value - mmin) * 256) / (mmax - mmin));
Chris@465 597 if (pixel >= 0 && pixel < 256) {
Chris@465 598 QRgb c = m_cache->color(pixel);
Chris@465 599 paint.setPen(QColor(qRed(c), qGreen(c), qBlue(c)));
Chris@465 600 paint.drawLine(5, 11 + y, cw - 5, 11 + y);
Chris@465 601 } else {
Chris@465 602 std::cerr << "WARNING: Colour3DPlotLayer::paintVerticalScale: value " << value << ", mmin " << mmin << ", mmax " << mmax << " leads to invalid pixel " << pixel << std::endl;
Chris@465 603 }
Chris@159 604 }
Chris@446 605
Chris@446 606 QString minstr = QString("%1").arg(min);
Chris@446 607 QString maxstr = QString("%1").arg(max);
Chris@446 608
Chris@446 609 paint.save();
Chris@446 610
Chris@446 611 QFont font = paint.font();
Chris@446 612 font.setPixelSize(10);
Chris@446 613 paint.setFont(font);
Chris@446 614
Chris@446 615 int msw = paint.fontMetrics().width(maxstr);
Chris@446 616
Chris@446 617 QMatrix m;
Chris@446 618 m.translate(cw - 6, ch + 10);
Chris@446 619 m.rotate(-90);
Chris@446 620
Chris@446 621 paint.setWorldMatrix(m);
Chris@446 622
Chris@446 623 v->drawVisibleText(paint, 2, 0, minstr, View::OutlinedText);
Chris@446 624
Chris@446 625 m.translate(ch - msw - 2, 0);
Chris@446 626 paint.setWorldMatrix(m);
Chris@446 627
Chris@446 628 v->drawVisibleText(paint, 0, 0, maxstr, View::OutlinedText);
Chris@446 629
Chris@446 630 paint.restore();
Chris@159 631 }
Chris@159 632
Chris@287 633 paint.setPen(v->getForeground());
Chris@159 634
Chris@445 635 int sh = m_model->getHeight();
Chris@445 636
Chris@445 637 int symin = m_miny;
Chris@445 638 int symax = m_maxy;
Chris@445 639 if (symax <= symin) {
Chris@445 640 symin = 0;
Chris@445 641 symax = sh;
Chris@445 642 }
Chris@445 643 if (symin < 0) symin = 0;
Chris@445 644 if (symax > sh) symax = sh;
Chris@445 645
Chris@456 646 float binHeight = float(v->height()) / (symax - symin);
Chris@456 647
Chris@456 648 paint.save();
Chris@456 649
Chris@456 650 QFont tf = paint.font();
Chris@456 651 if (paint.fontMetrics().height() >= binHeight) {
Chris@457 652 tf.setPixelSize(binHeight > 7 ? binHeight - 2 : 5);
Chris@456 653 paint.setFont(tf);
Chris@456 654 }
Chris@456 655
Chris@98 656 int count = v->height() / paint.fontMetrics().height();
Chris@445 657 int step = (symax - symin) / count;
Chris@98 658 if (step == 0) step = 1;
Chris@25 659
Chris@445 660 for (size_t i = symin; i < symax; ++i) {
Chris@25 661
Chris@357 662 size_t idx = i;
Chris@357 663 if (m_invertVertical) idx = m_model->getHeight() - idx - 1;
Chris@357 664
Chris@357 665 if ((idx % step) != 0) continue;
Chris@98 666
Chris@445 667 int y0 = int(v->height() - ((i - symin) * binHeight) - 1);
Chris@25 668
Chris@357 669 QString text = m_model->getBinName(idx);
Chris@357 670 if (text == "") text = QString("[%1]").arg(idx + 1);
Chris@25 671
Chris@159 672 paint.drawLine(cw, y0, w, y0);
Chris@25 673
Chris@457 674 if (step > 1) {
Chris@457 675 paint.drawLine(w - 1, y0 - (step * binHeight) + 1,
Chris@457 676 w - 1, y0 - binHeight - 1);
Chris@457 677 paint.drawLine(w - 2, y0 - (step * binHeight) + 1,
Chris@457 678 w - 2, y0 - binHeight - 2);
Chris@457 679 }
Chris@457 680
Chris@248 681 int cy = int(y0 - (step * binHeight)/2);
Chris@25 682 int ty = cy + paint.fontMetrics().ascent()/2;
Chris@25 683
Chris@159 684 paint.drawText(cw + 5, ty, text);
Chris@25 685 }
Chris@456 686
Chris@456 687 paint.restore();
Chris@25 688 }
Chris@25 689
Chris@467 690 DenseThreeDimensionalModel::Column
Chris@467 691 Colour3DPlotLayer::getColumn(size_t col) const
Chris@197 692 {
Chris@467 693 DenseThreeDimensionalModel::Column values = m_model->getColumn(col);
Chris@468 694 while (values.size() < m_model->getHeight()) values.push_back(0.f);
Chris@467 695 if (!m_normalizeColumns) return values;
Chris@461 696
Chris@224 697 float colMax = 0.f, colMin = 0.f;
Chris@461 698 float min = 0.f, max = 0.f;
Chris@197 699
Chris@461 700 min = m_model->getMinimumLevel();
Chris@461 701 max = m_model->getMaximumLevel();
Chris@461 702
Chris@461 703 for (size_t y = 0; y < values.size(); ++y) {
Chris@467 704 if (y == 0 || values.at(y) > colMax) colMax = values.at(y);
Chris@467 705 if (y == 0 || values.at(y) < colMin) colMin = values.at(y);
Chris@197 706 }
Chris@461 707 if (colMin == colMax) colMax = colMin + 1;
Chris@197 708
Chris@197 709 for (size_t y = 0; y < values.size(); ++y) {
Chris@461 710
Chris@467 711 float value = values.at(y);
Chris@461 712 float norm = (value - colMin) / (colMax - colMin);
Chris@467 713 float newvalue = min + (max - min) * norm;
Chris@197 714
Chris@467 715 if (value != newvalue) values[y] = newvalue;
Chris@468 716 }
Chris@468 717
Chris@468 718 return values;
Chris@197 719 }
Chris@197 720
Chris@197 721 void
Chris@197 722 Colour3DPlotLayer::fillCache(size_t firstBin, size_t lastBin) const
Chris@197 723 {
Chris@466 724 Profiler profiler("Colour3DPlotLayer::fillCache");
Chris@197 725 size_t modelStart = m_model->getStartFrame();
Chris@197 726 size_t modelEnd = m_model->getEndFrame();
Chris@197 727 size_t modelResolution = m_model->getResolution();
Chris@197 728
Chris@224 729 // std::cerr << "Colour3DPlotLayer::fillCache: " << firstBin << " -> " << lastBin << std::endl;
Chris@197 730
Chris@461 731 size_t modelStartBin = modelStart / modelResolution;
Chris@461 732 size_t modelEndBin = modelEnd / modelResolution;
Chris@461 733
Chris@461 734 size_t cacheWidth = modelEndBin - modelStartBin + 1;
Chris@471 735 if (lastBin > modelEndBin) cacheWidth = lastBin - modelStartBin + 1;
Chris@461 736 size_t cacheHeight = m_model->getHeight();
Chris@461 737
Chris@461 738 if (m_cache && (m_cache->height() != int(cacheHeight))) {
Chris@461 739 delete m_cache;
Chris@469 740 delete m_peaksCache;
Chris@461 741 m_cache = 0;
Chris@469 742 m_peaksCache = 0;
Chris@461 743 }
Chris@461 744
Chris@461 745 if (m_cache && (m_cache->width() != int(cacheWidth))) {
Chris@469 746 QImage *newCache =
Chris@469 747 new QImage(m_cache->copy(0, 0, cacheWidth, cacheHeight));
Chris@461 748 delete m_cache;
Chris@461 749 m_cache = newCache;
Chris@469 750 if (m_peaksCache) {
Chris@469 751 QImage *newPeaksCache =
Chris@469 752 new QImage(m_peaksCache->copy
Chris@469 753 (0, 0, cacheWidth / m_peakResolution, cacheHeight));
Chris@469 754 delete m_peaksCache;
Chris@469 755 m_peaksCache = newPeaksCache;
Chris@469 756 }
Chris@197 757 }
Chris@197 758
Chris@461 759 if (!m_cache) {
Chris@469 760 m_cache = new QImage
Chris@469 761 (cacheWidth, cacheHeight, QImage::Format_Indexed8);
Chris@461 762 m_cache->setNumColors(256);
Chris@474 763 if (!m_normalizeVisibleArea) {
Chris@469 764 m_peaksCache = new QImage
Chris@469 765 (cacheWidth / m_peakResolution + 1, cacheHeight,
Chris@469 766 QImage::Format_Indexed8);
Chris@469 767 m_peaksCache->setNumColors(256);
Chris@469 768 } else if (m_peaksCache) {
Chris@469 769 delete m_peaksCache;
Chris@469 770 m_peaksCache = 0;
Chris@469 771 }
Chris@461 772 m_cacheValidStart = 0;
Chris@461 773 m_cacheValidEnd = 0;
Chris@197 774 }
Chris@197 775
Chris@461 776 if (m_cacheValidStart <= firstBin && m_cacheValidEnd >= lastBin) {
Chris@461 777 return;
Chris@461 778 }
Chris@461 779
Chris@461 780 size_t fillStart = firstBin;
Chris@461 781 size_t fillEnd = lastBin;
Chris@197 782
Chris@461 783 if (fillStart < modelStartBin) fillStart = modelStartBin;
Chris@461 784 if (fillStart > modelEndBin) fillStart = modelEndBin;
Chris@461 785 if (fillEnd < modelStartBin) fillEnd = modelStartBin;
Chris@461 786 if (fillEnd > modelEndBin) fillEnd = modelEndBin;
Chris@197 787
Chris@461 788 bool normalizeVisible = (m_normalizeVisibleArea && !m_normalizeColumns);
Chris@197 789
Chris@461 790 if (!normalizeVisible && (m_cacheValidStart < m_cacheValidEnd)) {
Chris@461 791
Chris@461 792 if (m_cacheValidEnd < fillStart) {
Chris@461 793 fillStart = m_cacheValidEnd + 1;
Chris@461 794 }
Chris@461 795 if (m_cacheValidStart > fillEnd) {
Chris@461 796 fillEnd = m_cacheValidStart - 1;
Chris@461 797 }
Chris@461 798
Chris@461 799 m_cacheValidStart = std::min(fillStart, m_cacheValidStart);
Chris@461 800 m_cacheValidEnd = std::max(fillEnd, m_cacheValidEnd);
Chris@461 801
Chris@461 802 } else {
Chris@461 803
Chris@461 804 // the only valid area, ever, is the currently visible one
Chris@461 805
Chris@461 806 m_cacheValidStart = fillStart;
Chris@461 807 m_cacheValidEnd = fillEnd;
Chris@461 808 }
Chris@461 809
Chris@461 810 // std::cerr << "Cache size " << cacheWidth << "x" << cacheHeight << " will be valid from " << m_cacheValidStart << " to " << m_cacheValidEnd << std::endl;
Chris@461 811
Chris@197 812 DenseThreeDimensionalModel::Column values;
Chris@197 813
Chris@197 814 float min = m_model->getMinimumLevel();
Chris@197 815 float max = m_model->getMaximumLevel();
Chris@197 816
Chris@197 817 if (m_colourScale == LogScale) {
Chris@197 818 LogRange::mapRange(min, max);
Chris@197 819 } else if (m_colourScale == PlusMinusOneScale) {
Chris@197 820 min = -1.f;
Chris@197 821 max = 1.f;
Chris@197 822 }
Chris@197 823
Chris@197 824 if (max == min) max = min + 1.0;
Chris@197 825
Chris@197 826 ColourMapper mapper(m_colourMap, 0.f, 255.f);
Chris@197 827
Chris@197 828 for (int index = 0; index < 256; ++index) {
Chris@197 829 QColor colour = mapper.map(index);
Chris@469 830 m_cache->setColor
Chris@469 831 (index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@469 832 if (m_peaksCache) {
Chris@469 833 m_peaksCache->setColor
Chris@469 834 (index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@469 835 }
Chris@197 836 }
Chris@197 837
Chris@461 838 // m_cache->fill(0);
Chris@197 839
Chris@224 840 float visibleMax = 0.f, visibleMin = 0.f;
Chris@197 841
Chris@461 842 if (normalizeVisible) {
Chris@197 843
Chris@461 844 for (size_t c = fillStart; c <= fillEnd; ++c) {
Chris@197 845
Chris@467 846 values = getColumn(c);
Chris@197 847
Chris@224 848 float colMax = 0.f, colMin = 0.f;
Chris@197 849
Chris@461 850 for (size_t y = 0; y < cacheHeight; ++y) {
Chris@197 851 if (y >= values.size()) break;
Chris@197 852 if (y == 0 || values[y] > colMax) colMax = values[y];
Chris@224 853 if (y == 0 || values[y] < colMin) colMin = values[y];
Chris@197 854 }
Chris@197 855
Chris@461 856 if (c == fillStart || colMax > visibleMax) visibleMax = colMax;
Chris@461 857 if (c == fillStart || colMin < visibleMin) visibleMin = colMin;
Chris@461 858 }
Chris@461 859
Chris@461 860 if (m_colourScale == LogScale) {
Chris@461 861 visibleMin = LogRange::map(visibleMin);
Chris@461 862 visibleMax = LogRange::map(visibleMax);
Chris@461 863 if (visibleMin > visibleMax) std::swap(visibleMin, visibleMax);
Chris@197 864 }
Chris@197 865 }
Chris@197 866
Chris@224 867 if (visibleMin == visibleMax) visibleMax = visibleMin + 1;
Chris@224 868
Chris@469 869 int *peaks = 0;
Chris@469 870 if (m_peaksCache) {
Chris@469 871 peaks = new int[cacheHeight];
Chris@469 872 for (int y = 0; y < cacheHeight; ++y) {
Chris@469 873 peaks[y] = 0;
Chris@469 874 }
Chris@469 875 }
Chris@469 876
Chris@461 877 for (size_t c = fillStart; c <= fillEnd; ++c) {
Chris@197 878
Chris@467 879 values = getColumn(c);
Chris@197 880
Chris@461 881 for (size_t y = 0; y < cacheHeight; ++y) {
Chris@197 882
Chris@197 883 float value = min;
Chris@197 884 if (y < values.size()) {
Chris@469 885 value = values.at(y);
Chris@197 886 }
Chris@224 887
Chris@224 888 if (m_colourScale == LogScale) {
Chris@224 889 value = LogRange::map(value);
Chris@197 890 }
Chris@461 891
Chris@461 892 if (normalizeVisible) {
Chris@461 893 float norm = (value - visibleMin) / (visibleMax - visibleMin);
Chris@461 894 value = min + (max - min) * norm;
Chris@461 895 }
Chris@197 896
Chris@197 897 int pixel = int(((value - min) * 256) / (max - min));
Chris@197 898 if (pixel < 0) pixel = 0;
Chris@197 899 if (pixel > 255) pixel = 255;
Chris@469 900 if (peaks && (pixel > peaks[y])) peaks[y] = pixel;
Chris@197 901
Chris@357 902 if (m_invertVertical) {
Chris@461 903 m_cache->setPixel(c, cacheHeight - y - 1, pixel);
Chris@357 904 } else {
Chris@461 905 m_cache->setPixel(c, y, pixel);
Chris@357 906 }
Chris@197 907 }
Chris@469 908
Chris@469 909 if (peaks) {
Chris@469 910 size_t notch = (c % m_peakResolution);
Chris@469 911 if (notch == m_peakResolution-1 || c == fillEnd) {
Chris@469 912 size_t pc = c / m_peakResolution;
Chris@469 913 for (size_t y = 0; y < cacheHeight; ++y) {
Chris@469 914 if (m_invertVertical) {
Chris@469 915 m_peaksCache->setPixel(pc, cacheHeight - y - 1, peaks[y]);
Chris@469 916 } else {
Chris@469 917 m_peaksCache->setPixel(pc, y, peaks[y]);
Chris@469 918 }
Chris@469 919 }
Chris@469 920 for (int y = 0; y < cacheHeight; ++y) {
Chris@469 921 peaks[y] = 0;
Chris@469 922 }
Chris@469 923 }
Chris@469 924 }
Chris@197 925 }
Chris@469 926
Chris@469 927 delete[] peaks;
Chris@197 928 }
Chris@197 929
Chris@197 930 void
Chris@44 931 Colour3DPlotLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 932 {
Chris@443 933 /*!!!
Chris@443 934 if (m_model) {
Chris@443 935 std::cerr << "Colour3DPlotLayer::paint: model says shouldUseLogValueScale = " << m_model->shouldUseLogValueScale() << std::endl;
Chris@443 936 }
Chris@443 937 */
Chris@466 938 Profiler profiler("Colour3DPlotLayer::paint");
Chris@125 939 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@125 940 std::cerr << "Colour3DPlotLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << std::endl;
Chris@125 941 #endif
Chris@0 942
Chris@0 943 int completion = 0;
Chris@0 944 if (!m_model || !m_model->isOK() || !m_model->isReady(&completion)) {
Chris@0 945 if (completion > 0) {
Chris@44 946 paint.fillRect(0, 10, v->width() * completion / 100,
Chris@0 947 10, QColor(120, 120, 120));
Chris@0 948 }
Chris@0 949 return;
Chris@0 950 }
Chris@0 951
Chris@197 952 if (m_normalizeVisibleArea && !m_normalizeColumns) rect = v->rect();
Chris@197 953
Chris@0 954 size_t modelStart = m_model->getStartFrame();
Chris@0 955 size_t modelEnd = m_model->getEndFrame();
Chris@130 956 size_t modelResolution = m_model->getResolution();
Chris@0 957
Chris@197 958 // The cache is from the model's start frame to the model's end
Chris@197 959 // frame at the model's window increment frames per pixel. We
Chris@197 960 // want to draw from our start frame + x0 * zoomLevel to our start
Chris@197 961 // frame + x1 * zoomLevel at zoomLevel frames per pixel.
Chris@12 962
Chris@197 963 // We have quite different paint mechanisms for rendering "large"
Chris@197 964 // bins (more than one bin per pixel in both directions) and
Chris@197 965 // "small". This is "large"; see paintDense below for "small".
Chris@12 966
Chris@197 967 int x0 = rect.left();
Chris@197 968 int x1 = rect.right() + 1;
Chris@12 969
Chris@197 970 int h = v->height();
Chris@0 971
Chris@197 972 float srRatio =
Chris@197 973 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@197 974 float(m_model->getSampleRate());
Chris@0 975
Chris@197 976 int sx0 = int((v->getFrameForX(x0) / srRatio - long(modelStart))
Chris@197 977 / long(modelResolution));
Chris@197 978 int sx1 = int((v->getFrameForX(x1) / srRatio - long(modelStart))
Chris@197 979 / long(modelResolution));
Chris@197 980 int sh = m_model->getHeight();
Chris@159 981
Chris@444 982 int symin = m_miny;
Chris@444 983 int symax = m_maxy;
Chris@444 984 if (symax <= symin) {
Chris@444 985 symin = 0;
Chris@444 986 symax = sh;
Chris@444 987 }
Chris@444 988 if (symin < 0) symin = 0;
Chris@444 989 if (symax > sh) symax = sh;
Chris@444 990
Chris@197 991 if (sx0 > 0) --sx0;
Chris@197 992 fillCache(sx0 < 0 ? 0 : sx0,
Chris@197 993 sx1 < 0 ? 0 : sx1);
Chris@0 994
Chris@351 995 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@466 996 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 997 #endif
Chris@351 998
Chris@465 999 if (m_opaque ||
Chris@465 1000 int(m_model->getHeight()) >= v->height() ||
Chris@466 1001 ((modelResolution * srRatio) / v->getZoomLevel()) < 2) {
Chris@347 1002 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@351 1003 std::cerr << "calling paintDense" << std::endl;
Chris@347 1004 #endif
Chris@98 1005 paintDense(v, paint, rect);
Chris@98 1006 return;
Chris@98 1007 }
Chris@98 1008
Chris@125 1009 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@347 1010 std::cerr << "Colour3DPlotLayer::paint: w " << x1-x0 << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sx1-sx0 << ", sh " << sh << std::endl;
Chris@130 1011 std::cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << std::endl;
Chris@125 1012 #endif
Chris@0 1013
Chris@25 1014 QPoint illuminatePos;
Chris@44 1015 bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos);
Chris@54 1016 char labelbuf[10];
Chris@25 1017
Chris@197 1018 for (int sx = sx0; sx <= sx1; ++sx) {
Chris@0 1019
Chris@130 1020 int fx = sx * int(modelResolution);
Chris@0 1021
Chris@351 1022 if (fx + int(modelResolution) <= int(modelStart) ||
Chris@0 1023 fx > int(modelEnd)) continue;
Chris@0 1024
Chris@248 1025 int rx0 = v->getXForFrame(int((fx + int(modelStart)) * srRatio));
Chris@248 1026 int rx1 = v->getXForFrame(int((fx + int(modelStart) + int(modelResolution) + 1) * srRatio));
Chris@54 1027
Chris@159 1028 int rw = rx1 - rx0;
Chris@159 1029 if (rw < 1) rw = 1;
Chris@54 1030
Chris@159 1031 bool showLabel = (rw > 10 &&
Chris@159 1032 paint.fontMetrics().width("0.000000") < rw - 3 &&
Chris@54 1033 paint.fontMetrics().height() < (h / sh));
Chris@98 1034
Chris@444 1035 for (int sy = symin; sy < symax; ++sy) {
Chris@0 1036
Chris@444 1037 int ry0 = h - ((sy - symin) * h) / (symax - symin) - 1;
Chris@0 1038 QRgb pixel = qRgb(255, 255, 255);
Chris@461 1039 if (sx >= 0 && sx < m_cache->width() &&
Chris@0 1040 sy >= 0 && sy < m_cache->height()) {
Chris@461 1041 pixel = m_cache->pixel(sx, sy);
Chris@0 1042 }
Chris@0 1043
Chris@444 1044 QRect r(rx0, ry0 - h / (symax - symin) - 1,
Chris@444 1045 rw, h / (symax - symin) + 1);
Chris@159 1046
Chris@159 1047 if (rw == 1) {
Chris@159 1048 paint.setPen(pixel);
Chris@159 1049 paint.setBrush(Qt::NoBrush);
Chris@159 1050 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
Chris@159 1051 continue;
Chris@159 1052 }
Chris@159 1053
Chris@0 1054 QColor pen(255, 255, 255, 80);
Chris@0 1055 QColor brush(pixel);
Chris@159 1056
Chris@159 1057 if (rw > 3 && r.height() > 3) {
Chris@159 1058 brush.setAlpha(160);
Chris@159 1059 }
Chris@159 1060
Chris@0 1061 paint.setPen(Qt::NoPen);
Chris@0 1062 paint.setBrush(brush);
Chris@0 1063
Chris@25 1064 if (illuminate) {
Chris@25 1065 if (r.contains(illuminatePos)) {
Chris@287 1066 paint.setPen(v->getForeground());
Chris@25 1067 }
Chris@25 1068 }
Chris@76 1069
Chris@125 1070 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@159 1071 std::cerr << "rect " << r.x() << "," << r.y() << " "
Chris@159 1072 << r.width() << "x" << r.height() << std::endl;
Chris@125 1073 #endif
Chris@25 1074
Chris@25 1075 paint.drawRect(r);
Chris@0 1076
Chris@54 1077 if (showLabel) {
Chris@461 1078 if (sx >= 0 && sx < m_cache->width() &&
Chris@54 1079 sy >= 0 && sy < m_cache->height()) {
Chris@461 1080 float value = m_model->getValueAt(sx, sy);
Chris@54 1081 sprintf(labelbuf, "%06f", value);
Chris@54 1082 QString text(labelbuf);
Chris@287 1083 paint.setPen(v->getBackground());
Chris@54 1084 paint.drawText(rx0 + 2,
Chris@54 1085 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
Chris@54 1086 text);
Chris@0 1087 }
Chris@0 1088 }
Chris@0 1089 }
Chris@0 1090 }
Chris@98 1091 }
Chris@0 1092
Chris@98 1093 void
Chris@98 1094 Colour3DPlotLayer::paintDense(View *v, QPainter &paint, QRect rect) const
Chris@98 1095 {
Chris@466 1096 Profiler profiler("Colour3DPlotLayer::paintDense");
Chris@463 1097 if (!m_cache) return;
Chris@463 1098
Chris@466 1099 float modelStart = m_model->getStartFrame();
Chris@466 1100 float modelResolution = m_model->getResolution();
Chris@0 1101
Chris@469 1102 int mmsr = v->getViewManager()->getMainModelSampleRate();
Chris@469 1103 int msr = m_model->getSampleRate();
Chris@469 1104 float srRatio = float(mmsr) / float(msr);
Chris@159 1105
Chris@98 1106 int x0 = rect.left();
Chris@98 1107 int x1 = rect.right() + 1;
Chris@0 1108
Chris@470 1109 const int w = x1 - x0; // const so it can be used as array size below
Chris@471 1110 int h = v->height(); // we always paint full height
Chris@160 1111 int sh = m_model->getHeight();
Chris@98 1112
Chris@444 1113 int symin = m_miny;
Chris@444 1114 int symax = m_maxy;
Chris@444 1115 if (symax <= symin) {
Chris@444 1116 symin = 0;
Chris@444 1117 symax = sh;
Chris@444 1118 }
Chris@444 1119 if (symin < 0) symin = 0;
Chris@444 1120 if (symax > sh) symax = sh;
Chris@444 1121
Chris@466 1122 QImage img(w, h, QImage::Format_Indexed8);
Chris@466 1123 img.setColorTable(m_cache->colorTable());
Chris@98 1124
Chris@466 1125 uchar *peaks = new uchar[w];
Chris@466 1126 memset(peaks, 0, w);
Chris@98 1127
Chris@469 1128 int zoomLevel = v->getZoomLevel();
Chris@469 1129
Chris@469 1130 QImage *source = m_cache;
Chris@474 1131
Chris@474 1132 std::cerr << "modelResolution " << modelResolution << ", srRatio "
Chris@474 1133 << srRatio << ", m_peakResolution " << m_peakResolution
Chris@474 1134 << ", zoomLevel " << zoomLevel << ", result "
Chris@474 1135 << ((modelResolution * srRatio * m_peakResolution) / zoomLevel)
Chris@474 1136 << std::endl;
Chris@474 1137
Chris@474 1138 if (m_peaksCache) {
Chris@474 1139 if (((modelResolution * srRatio * m_peakResolution) / zoomLevel) < 1) {
Chris@474 1140 std::cerr << "using peaks cache" << std::endl;
Chris@474 1141 source = m_peaksCache;
Chris@474 1142 modelResolution *= m_peakResolution;
Chris@474 1143 } else {
Chris@474 1144 std::cerr << "not using peaks cache" << std::endl;
Chris@474 1145 }
Chris@469 1146 } else {
Chris@474 1147 std::cerr << "have no peaks cache" << std::endl;
Chris@469 1148 }
Chris@469 1149
Chris@466 1150 int psy1i = -1;
Chris@469 1151 int sw = source->width();
Chris@470 1152
Chris@470 1153 long xf = -1;
Chris@470 1154 long nxf = v->getFrameForX(x0);
Chris@470 1155
Chris@470 1156 int sxa[w * 2];
Chris@470 1157 for (int x = 0; x < w; ++x) {
Chris@470 1158
Chris@470 1159 xf = nxf;
Chris@470 1160 nxf = xf + zoomLevel;
Chris@470 1161
Chris@470 1162 float sx0 = (float(xf) / srRatio - modelStart) / modelResolution;
Chris@470 1163 float sx1 = (float(nxf) / srRatio - modelStart) / modelResolution;
Chris@470 1164
Chris@470 1165 int sx0i = int(sx0 + 0.001);
Chris@470 1166 int sx1i = int(sx1);
Chris@470 1167
Chris@470 1168 sxa[x*2] = sx0i;
Chris@470 1169 sxa[x*2 + 1] = sx1i;
Chris@470 1170 }
Chris@466 1171
Chris@466 1172 for (int y = 0; y < h; ++y) {
Chris@466 1173
Chris@466 1174 float sy0 = symin + (float(h - y - 1) * (symax - symin)) / h;
Chris@466 1175 float sy1 = symin + (float(h - y) * (symax - symin)) / h;
Chris@466 1176
Chris@466 1177 int sy0i = int(sy0 + 0.001);
Chris@466 1178 int sy1i = int(sy1);
Chris@466 1179
Chris@466 1180 uchar *targetLine = img.scanLine(y);
Chris@466 1181
Chris@466 1182 if (sy0i == sy1i && sy0i == psy1i) { // same scan line as just computed
Chris@466 1183 goto copy;
Chris@105 1184 }
Chris@105 1185
Chris@471 1186 for (int x = 0; x < w; ++x) {
Chris@471 1187 peaks[x] = 0;
Chris@471 1188 }
Chris@466 1189
Chris@466 1190 for (int sy = sy0i; sy <= sy1i; ++sy) {
Chris@347 1191
Chris@469 1192 if (sy < 0 || sy >= source->height()) continue;
Chris@466 1193
Chris@469 1194 uchar *sourceLine = source->scanLine(sy);
Chris@98 1195
Chris@466 1196 for (int x = 0; x < w; ++x) {
Chris@98 1197
Chris@474 1198 int sx1i = sxa[x*2 + 1];
Chris@474 1199 if (sx1i < 0) continue;
Chris@474 1200
Chris@470 1201 int sx0i = sxa[x*2];
Chris@474 1202 if (sx0i >= sw) break;
Chris@98 1203
Chris@466 1204 uchar peak = 0;
Chris@466 1205 for (int sx = sx0i; sx <= sx1i; ++sx) {
Chris@469 1206 if (sx < 0 || sx >= sw) continue;
Chris@466 1207 if (sourceLine[sx] > peak) peak = sourceLine[sx];
Chris@98 1208 }
Chris@466 1209 peaks[x] = peak;
Chris@98 1210 }
Chris@466 1211 }
Chris@466 1212
Chris@466 1213 copy:
Chris@466 1214 for (int x = 0; x < w; ++x) {
Chris@466 1215 targetLine[x] = peaks[x];
Chris@98 1216 }
Chris@0 1217 }
Chris@0 1218
Chris@469 1219 delete[] peaks;
Chris@469 1220
Chris@98 1221 paint.drawImage(x0, 0, img);
Chris@0 1222 }
Chris@0 1223
Chris@472 1224 void
Chris@472 1225 Colour3DPlotLayer::paintSmooth(View *v, QPainter &paint, QRect rect) const
Chris@472 1226 {
Chris@472 1227 Profiler profiler("Colour3DPlotLayer:paintSmooth");
Chris@472 1228 if (!m_cache) return;
Chris@472 1229
Chris@472 1230 float modelStart = m_model->getStartFrame();
Chris@472 1231 float modelResolution = m_model->getResolution();
Chris@472 1232
Chris@472 1233 int mmsr = v->getViewManager()->getMainModelSampleRate();
Chris@472 1234 int msr = m_model->getSampleRate();
Chris@472 1235 float srRatio = float(mmsr) / float(msr);
Chris@472 1236
Chris@472 1237 int x0 = rect.left();
Chris@472 1238 int x1 = rect.right() + 1;
Chris@472 1239
Chris@472 1240 int h = v->height(); // we always paint full height
Chris@472 1241 int sh = m_model->getHeight();
Chris@472 1242
Chris@472 1243 int symin = m_miny;
Chris@472 1244 int symax = m_maxy;
Chris@472 1245 if (symax <= symin) {
Chris@472 1246 symin = 0;
Chris@472 1247 symax = sh;
Chris@472 1248 }
Chris@472 1249 if (symin < 0) symin = 0;
Chris@472 1250 if (symax > sh) symax = sh;
Chris@472 1251
Chris@472 1252 // QImage img(w, h, QImage::Format_Indexed8);
Chris@472 1253 // img.setColorTable(m_cache->colorTable());
Chris@472 1254
Chris@472 1255 int zoomLevel = v->getZoomLevel();
Chris@472 1256
Chris@472 1257 QImage *source = m_cache;
Chris@472 1258 if (m_peaksCache &&
Chris@472 1259 ((modelResolution * srRatio * m_peakResolution) / zoomLevel) < 1) {
Chris@472 1260 std::cerr << "using peaks cache" << std::endl;
Chris@472 1261 source = m_peaksCache;
Chris@472 1262 modelResolution *= m_peakResolution;
Chris@472 1263 } else {
Chris@472 1264 std::cerr << "not using peaks cache" << std::endl;
Chris@472 1265 }
Chris@472 1266
Chris@472 1267 float sx0 = (float(v->getFrameForX(x0)) / srRatio - modelStart) / modelResolution;
Chris@472 1268 float sx1 = (float(v->getFrameForX(x1)) / srRatio - modelStart) / modelResolution;
Chris@472 1269 int sx0i = int(sx0 + 0.001);
Chris@472 1270 int sx1i = int(sx1);
Chris@472 1271
Chris@472 1272 if (sx0i < 0) sx0i = 0;
Chris@472 1273 if (sx0i > source->width()) sx0i = source->width();
Chris@472 1274
Chris@472 1275 int tx0 = v->getXForFrame(((sx0i * modelResolution) + modelStart) * srRatio + 0.001);
Chris@472 1276 int tx1 = v->getXForFrame(((sx1i * modelResolution) + modelStart) * srRatio);
Chris@472 1277
Chris@472 1278 std::cerr << "x0 " << x0 << ", x1 " << x1 << " -> sx0 " << sx0i << ", sx1 " << sx1i << " -> tx0 " << tx0 << ", tx1 " << tx1 << std::endl;
Chris@472 1279
Chris@472 1280 QImage img = source->copy(sx0i, 0, sx1i - sx0i, source->height())
Chris@472 1281 .scaled(QSize(tx1 - tx0, h),
Chris@472 1282 Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
Chris@472 1283 paint.drawImage(x0, 0, img);
Chris@472 1284 }
Chris@472 1285
Chris@28 1286 bool
Chris@44 1287 Colour3DPlotLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 1288 size_t &resolution,
Chris@28 1289 SnapType snap) const
Chris@24 1290 {
Chris@24 1291 if (!m_model) {
Chris@44 1292 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@24 1293 }
Chris@24 1294
Chris@130 1295 resolution = m_model->getResolution();
Chris@28 1296 int left = (frame / resolution) * resolution;
Chris@28 1297 int right = left + resolution;
Chris@28 1298
Chris@28 1299 switch (snap) {
Chris@28 1300 case SnapLeft: frame = left; break;
Chris@28 1301 case SnapRight: frame = right; break;
Chris@28 1302 case SnapNearest:
Chris@28 1303 case SnapNeighbouring:
Chris@28 1304 if (frame - left > right - frame) frame = right;
Chris@28 1305 else frame = left;
Chris@28 1306 break;
Chris@28 1307 }
Chris@24 1308
Chris@28 1309 return true;
Chris@24 1310 }
Chris@24 1311
Chris@316 1312 void
Chris@316 1313 Colour3DPlotLayer::toXml(QTextStream &stream,
Chris@316 1314 QString indent, QString extraAttributes) const
Chris@197 1315 {
Chris@316 1316 QString s = QString("scale=\"%1\" "
Chris@316 1317 "colourScheme=\"%2\" "
Chris@316 1318 "normalizeColumns=\"%3\" "
Chris@445 1319 "normalizeVisibleArea=\"%4\" "
Chris@445 1320 "minY=\"%5\" "
Chris@465 1321 "maxY=\"%6\" "
Chris@465 1322 "invertVertical=\"%7\" "
Chris@465 1323 "opaque=\"%8\"")
Chris@197 1324 .arg((int)m_colourScale)
Chris@197 1325 .arg(m_colourMap)
Chris@197 1326 .arg(m_normalizeColumns ? "true" : "false")
Chris@445 1327 .arg(m_normalizeVisibleArea ? "true" : "false")
Chris@445 1328 .arg(m_miny)
Chris@465 1329 .arg(m_maxy)
Chris@465 1330 .arg(m_invertVertical ? "true" : "false")
Chris@465 1331 .arg(m_opaque ? "true" : "false");
Chris@197 1332
Chris@316 1333 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@197 1334 }
Chris@197 1335
Chris@197 1336 void
Chris@197 1337 Colour3DPlotLayer::setProperties(const QXmlAttributes &attributes)
Chris@197 1338 {
Chris@445 1339 bool ok = false, alsoOk = false;
Chris@197 1340
Chris@197 1341 ColourScale scale = (ColourScale)attributes.value("scale").toInt(&ok);
Chris@197 1342 if (ok) setColourScale(scale);
Chris@197 1343
Chris@197 1344 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 1345 if (ok) setColourMap(colourMap);
Chris@197 1346
Chris@197 1347 bool normalizeColumns =
Chris@197 1348 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@197 1349 setNormalizeColumns(normalizeColumns);
Chris@197 1350
Chris@197 1351 bool normalizeVisibleArea =
Chris@197 1352 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@197 1353 setNormalizeVisibleArea(normalizeVisibleArea);
Chris@445 1354
Chris@465 1355 bool invertVertical =
Chris@465 1356 (attributes.value("invertVertical").trimmed() == "true");
Chris@465 1357 setInvertVertical(invertVertical);
Chris@465 1358
Chris@465 1359 bool opaque =
Chris@465 1360 (attributes.value("opaque").trimmed() == "true");
Chris@465 1361 setNormalizeVisibleArea(opaque);
Chris@465 1362
Chris@445 1363 float min = attributes.value("minY").toFloat(&ok);
Chris@445 1364 float max = attributes.value("maxY").toFloat(&alsoOk);
Chris@445 1365 if (ok && alsoOk) setDisplayExtents(min, max);
Chris@197 1366 }
Chris@197 1367