annotate layer/Colour3DPlotLayer.cpp @ 475:3f4b10e76ccd

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