annotate layer/Colour3DPlotLayer.cpp @ 467:76a47146f1f6

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