annotate layer/Colour3DPlotLayer.cpp @ 468:762e96217900

* make compression optional in editable dense 3d model, and some tweaks
author Chris Cannam
date Fri, 23 Jan 2009 14:00:29 +0000
parents 76a47146f1f6
children 9863f9a36cc2
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@468 679 while (values.size() < m_model->getHeight()) values.push_back(0.f);
Chris@467 680 if (!m_normalizeColumns) return values;
Chris@461 681
Chris@224 682 float colMax = 0.f, colMin = 0.f;
Chris@461 683 float min = 0.f, max = 0.f;
Chris@197 684
Chris@461 685 min = m_model->getMinimumLevel();
Chris@461 686 max = m_model->getMaximumLevel();
Chris@461 687
Chris@461 688 for (size_t y = 0; y < values.size(); ++y) {
Chris@467 689 if (y == 0 || values.at(y) > colMax) colMax = values.at(y);
Chris@467 690 if (y == 0 || values.at(y) < colMin) colMin = values.at(y);
Chris@197 691 }
Chris@461 692 if (colMin == colMax) colMax = colMin + 1;
Chris@197 693
Chris@197 694 for (size_t y = 0; y < values.size(); ++y) {
Chris@461 695
Chris@467 696 float value = values.at(y);
Chris@461 697 float norm = (value - colMin) / (colMax - colMin);
Chris@467 698 float newvalue = min + (max - min) * norm;
Chris@197 699
Chris@467 700 if (value != newvalue) values[y] = newvalue;
Chris@468 701 }
Chris@468 702
Chris@468 703 return values;
Chris@197 704 }
Chris@197 705
Chris@197 706 void
Chris@197 707 Colour3DPlotLayer::fillCache(size_t firstBin, size_t lastBin) const
Chris@197 708 {
Chris@466 709 Profiler profiler("Colour3DPlotLayer::fillCache");
Chris@197 710 size_t modelStart = m_model->getStartFrame();
Chris@197 711 size_t modelEnd = m_model->getEndFrame();
Chris@197 712 size_t modelResolution = m_model->getResolution();
Chris@197 713
Chris@224 714 // std::cerr << "Colour3DPlotLayer::fillCache: " << firstBin << " -> " << lastBin << std::endl;
Chris@197 715
Chris@461 716 size_t modelStartBin = modelStart / modelResolution;
Chris@461 717 size_t modelEndBin = modelEnd / modelResolution;
Chris@461 718
Chris@461 719 size_t cacheWidth = modelEndBin - modelStartBin + 1;
Chris@461 720 size_t cacheHeight = m_model->getHeight();
Chris@461 721
Chris@461 722 if (m_cache && (m_cache->height() != int(cacheHeight))) {
Chris@461 723 delete m_cache;
Chris@461 724 m_cache = 0;
Chris@461 725 }
Chris@461 726
Chris@461 727 if (m_cache && (m_cache->width() != int(cacheWidth))) {
Chris@461 728 QImage *newCache = new QImage(m_cache->copy(0, 0, cacheWidth, cacheHeight));
Chris@461 729 delete m_cache;
Chris@461 730 m_cache = newCache;
Chris@197 731 }
Chris@197 732
Chris@461 733 if (!m_cache) {
Chris@461 734 m_cache = new QImage(cacheWidth, cacheHeight, QImage::Format_Indexed8);
Chris@461 735 m_cache->setNumColors(256);
Chris@461 736 m_cacheValidStart = 0;
Chris@461 737 m_cacheValidEnd = 0;
Chris@197 738 }
Chris@197 739
Chris@461 740 if (m_cacheValidStart <= firstBin && m_cacheValidEnd >= lastBin) {
Chris@461 741 return;
Chris@461 742 }
Chris@461 743
Chris@461 744 size_t fillStart = firstBin;
Chris@461 745 size_t fillEnd = lastBin;
Chris@197 746
Chris@461 747 if (fillStart < modelStartBin) fillStart = modelStartBin;
Chris@461 748 if (fillStart > modelEndBin) fillStart = modelEndBin;
Chris@461 749 if (fillEnd < modelStartBin) fillEnd = modelStartBin;
Chris@461 750 if (fillEnd > modelEndBin) fillEnd = modelEndBin;
Chris@197 751
Chris@461 752 bool normalizeVisible = (m_normalizeVisibleArea && !m_normalizeColumns);
Chris@197 753
Chris@461 754 if (!normalizeVisible && (m_cacheValidStart < m_cacheValidEnd)) {
Chris@461 755
Chris@461 756 if (m_cacheValidEnd < fillStart) {
Chris@461 757 fillStart = m_cacheValidEnd + 1;
Chris@461 758 }
Chris@461 759 if (m_cacheValidStart > fillEnd) {
Chris@461 760 fillEnd = m_cacheValidStart - 1;
Chris@461 761 }
Chris@461 762
Chris@461 763 m_cacheValidStart = std::min(fillStart, m_cacheValidStart);
Chris@461 764 m_cacheValidEnd = std::max(fillEnd, m_cacheValidEnd);
Chris@461 765
Chris@461 766 } else {
Chris@461 767
Chris@461 768 // the only valid area, ever, is the currently visible one
Chris@461 769
Chris@461 770 m_cacheValidStart = fillStart;
Chris@461 771 m_cacheValidEnd = fillEnd;
Chris@461 772 }
Chris@461 773
Chris@461 774 // std::cerr << "Cache size " << cacheWidth << "x" << cacheHeight << " will be valid from " << m_cacheValidStart << " to " << m_cacheValidEnd << std::endl;
Chris@461 775
Chris@197 776 DenseThreeDimensionalModel::Column values;
Chris@197 777
Chris@197 778 float min = m_model->getMinimumLevel();
Chris@197 779 float max = m_model->getMaximumLevel();
Chris@197 780
Chris@197 781 if (m_colourScale == LogScale) {
Chris@197 782 LogRange::mapRange(min, max);
Chris@197 783 } else if (m_colourScale == PlusMinusOneScale) {
Chris@197 784 min = -1.f;
Chris@197 785 max = 1.f;
Chris@197 786 }
Chris@197 787
Chris@197 788 if (max == min) max = min + 1.0;
Chris@197 789
Chris@197 790 ColourMapper mapper(m_colourMap, 0.f, 255.f);
Chris@197 791
Chris@197 792 for (int index = 0; index < 256; ++index) {
Chris@197 793 QColor colour = mapper.map(index);
Chris@197 794 m_cache->setColor(index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@197 795 }
Chris@197 796
Chris@461 797 // m_cache->fill(0);
Chris@197 798
Chris@224 799 float visibleMax = 0.f, visibleMin = 0.f;
Chris@197 800
Chris@461 801 if (normalizeVisible) {
Chris@197 802
Chris@461 803 for (size_t c = fillStart; c <= fillEnd; ++c) {
Chris@197 804
Chris@467 805 values = getColumn(c);
Chris@197 806
Chris@224 807 float colMax = 0.f, colMin = 0.f;
Chris@197 808
Chris@461 809 for (size_t y = 0; y < cacheHeight; ++y) {
Chris@197 810 if (y >= values.size()) break;
Chris@197 811 if (y == 0 || values[y] > colMax) colMax = values[y];
Chris@224 812 if (y == 0 || values[y] < colMin) colMin = values[y];
Chris@197 813 }
Chris@197 814
Chris@461 815 if (c == fillStart || colMax > visibleMax) visibleMax = colMax;
Chris@461 816 if (c == fillStart || colMin < visibleMin) visibleMin = colMin;
Chris@461 817 }
Chris@461 818
Chris@461 819 if (m_colourScale == LogScale) {
Chris@461 820 visibleMin = LogRange::map(visibleMin);
Chris@461 821 visibleMax = LogRange::map(visibleMax);
Chris@461 822 if (visibleMin > visibleMax) std::swap(visibleMin, visibleMax);
Chris@197 823 }
Chris@197 824 }
Chris@197 825
Chris@224 826 if (visibleMin == visibleMax) visibleMax = visibleMin + 1;
Chris@224 827
Chris@461 828 for (size_t c = fillStart; c <= fillEnd; ++c) {
Chris@197 829
Chris@467 830 values = getColumn(c);
Chris@197 831
Chris@461 832 for (size_t y = 0; y < cacheHeight; ++y) {
Chris@197 833
Chris@197 834 float value = min;
Chris@197 835 if (y < values.size()) {
Chris@197 836 value = values[y];
Chris@197 837 }
Chris@224 838
Chris@224 839 if (m_colourScale == LogScale) {
Chris@224 840 value = LogRange::map(value);
Chris@197 841 }
Chris@461 842
Chris@461 843 if (normalizeVisible) {
Chris@461 844 float norm = (value - visibleMin) / (visibleMax - visibleMin);
Chris@461 845 value = min + (max - min) * norm;
Chris@461 846 }
Chris@197 847
Chris@197 848 int pixel = int(((value - min) * 256) / (max - min));
Chris@197 849 if (pixel < 0) pixel = 0;
Chris@197 850 if (pixel > 255) pixel = 255;
Chris@197 851
Chris@357 852 if (m_invertVertical) {
Chris@461 853 m_cache->setPixel(c, cacheHeight - y - 1, pixel);
Chris@357 854 } else {
Chris@461 855 m_cache->setPixel(c, y, pixel);
Chris@357 856 }
Chris@197 857 }
Chris@197 858 }
Chris@197 859 }
Chris@197 860
Chris@197 861 void
Chris@44 862 Colour3DPlotLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 863 {
Chris@443 864 /*!!!
Chris@443 865 if (m_model) {
Chris@443 866 std::cerr << "Colour3DPlotLayer::paint: model says shouldUseLogValueScale = " << m_model->shouldUseLogValueScale() << std::endl;
Chris@443 867 }
Chris@443 868 */
Chris@466 869 Profiler profiler("Colour3DPlotLayer::paint");
Chris@125 870 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@125 871 std::cerr << "Colour3DPlotLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << std::endl;
Chris@125 872 #endif
Chris@0 873
Chris@0 874 int completion = 0;
Chris@0 875 if (!m_model || !m_model->isOK() || !m_model->isReady(&completion)) {
Chris@0 876 if (completion > 0) {
Chris@44 877 paint.fillRect(0, 10, v->width() * completion / 100,
Chris@0 878 10, QColor(120, 120, 120));
Chris@0 879 }
Chris@0 880 return;
Chris@0 881 }
Chris@0 882
Chris@197 883 if (m_normalizeVisibleArea && !m_normalizeColumns) rect = v->rect();
Chris@197 884
Chris@0 885 size_t modelStart = m_model->getStartFrame();
Chris@0 886 size_t modelEnd = m_model->getEndFrame();
Chris@130 887 size_t modelResolution = m_model->getResolution();
Chris@0 888
Chris@197 889 // The cache is from the model's start frame to the model's end
Chris@197 890 // frame at the model's window increment frames per pixel. We
Chris@197 891 // want to draw from our start frame + x0 * zoomLevel to our start
Chris@197 892 // frame + x1 * zoomLevel at zoomLevel frames per pixel.
Chris@12 893
Chris@197 894 // We have quite different paint mechanisms for rendering "large"
Chris@197 895 // bins (more than one bin per pixel in both directions) and
Chris@197 896 // "small". This is "large"; see paintDense below for "small".
Chris@12 897
Chris@197 898 int x0 = rect.left();
Chris@197 899 int x1 = rect.right() + 1;
Chris@12 900
Chris@197 901 int h = v->height();
Chris@0 902
Chris@197 903 float srRatio =
Chris@197 904 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@197 905 float(m_model->getSampleRate());
Chris@0 906
Chris@197 907 int sx0 = int((v->getFrameForX(x0) / srRatio - long(modelStart))
Chris@197 908 / long(modelResolution));
Chris@197 909 int sx1 = int((v->getFrameForX(x1) / srRatio - long(modelStart))
Chris@197 910 / long(modelResolution));
Chris@197 911 int sh = m_model->getHeight();
Chris@159 912
Chris@444 913 int symin = m_miny;
Chris@444 914 int symax = m_maxy;
Chris@444 915 if (symax <= symin) {
Chris@444 916 symin = 0;
Chris@444 917 symax = sh;
Chris@444 918 }
Chris@444 919 if (symin < 0) symin = 0;
Chris@444 920 if (symax > sh) symax = sh;
Chris@444 921
Chris@197 922 if (sx0 > 0) --sx0;
Chris@197 923 fillCache(sx0 < 0 ? 0 : sx0,
Chris@197 924 sx1 < 0 ? 0 : sx1);
Chris@0 925
Chris@351 926 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@466 927 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 928 #endif
Chris@351 929
Chris@465 930 if (m_opaque ||
Chris@465 931 int(m_model->getHeight()) >= v->height() ||
Chris@466 932 // int(modelResolution * m_model->getSampleRate()) < v->getZoomLevel() / 2) {
Chris@466 933 ((modelResolution * srRatio) / v->getZoomLevel()) < 2) {
Chris@347 934 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@351 935 std::cerr << "calling paintDense" << std::endl;
Chris@347 936 #endif
Chris@98 937 paintDense(v, paint, rect);
Chris@98 938 return;
Chris@98 939 }
Chris@98 940
Chris@125 941 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@347 942 std::cerr << "Colour3DPlotLayer::paint: w " << x1-x0 << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sx1-sx0 << ", sh " << sh << std::endl;
Chris@130 943 std::cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << std::endl;
Chris@125 944 #endif
Chris@0 945
Chris@25 946 QPoint illuminatePos;
Chris@44 947 bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos);
Chris@54 948 char labelbuf[10];
Chris@25 949
Chris@197 950 for (int sx = sx0; sx <= sx1; ++sx) {
Chris@0 951
Chris@130 952 int fx = sx * int(modelResolution);
Chris@0 953
Chris@351 954 if (fx + int(modelResolution) <= int(modelStart) ||
Chris@0 955 fx > int(modelEnd)) continue;
Chris@0 956
Chris@248 957 int rx0 = v->getXForFrame(int((fx + int(modelStart)) * srRatio));
Chris@248 958 int rx1 = v->getXForFrame(int((fx + int(modelStart) + int(modelResolution) + 1) * srRatio));
Chris@54 959
Chris@159 960 int rw = rx1 - rx0;
Chris@159 961 if (rw < 1) rw = 1;
Chris@54 962
Chris@159 963 bool showLabel = (rw > 10 &&
Chris@159 964 paint.fontMetrics().width("0.000000") < rw - 3 &&
Chris@54 965 paint.fontMetrics().height() < (h / sh));
Chris@98 966
Chris@444 967 for (int sy = symin; sy < symax; ++sy) {
Chris@0 968
Chris@444 969 int ry0 = h - ((sy - symin) * h) / (symax - symin) - 1;
Chris@0 970 QRgb pixel = qRgb(255, 255, 255);
Chris@461 971 if (sx >= 0 && sx < m_cache->width() &&
Chris@0 972 sy >= 0 && sy < m_cache->height()) {
Chris@461 973 pixel = m_cache->pixel(sx, sy);
Chris@0 974 }
Chris@0 975
Chris@444 976 QRect r(rx0, ry0 - h / (symax - symin) - 1,
Chris@444 977 rw, h / (symax - symin) + 1);
Chris@159 978
Chris@159 979 if (rw == 1) {
Chris@159 980 paint.setPen(pixel);
Chris@159 981 paint.setBrush(Qt::NoBrush);
Chris@159 982 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
Chris@159 983 continue;
Chris@159 984 }
Chris@159 985
Chris@0 986 QColor pen(255, 255, 255, 80);
Chris@0 987 QColor brush(pixel);
Chris@159 988
Chris@159 989 if (rw > 3 && r.height() > 3) {
Chris@159 990 brush.setAlpha(160);
Chris@159 991 }
Chris@159 992
Chris@0 993 paint.setPen(Qt::NoPen);
Chris@0 994 paint.setBrush(brush);
Chris@0 995
Chris@25 996 if (illuminate) {
Chris@25 997 if (r.contains(illuminatePos)) {
Chris@287 998 paint.setPen(v->getForeground());
Chris@25 999 }
Chris@25 1000 }
Chris@76 1001
Chris@125 1002 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@159 1003 std::cerr << "rect " << r.x() << "," << r.y() << " "
Chris@159 1004 << r.width() << "x" << r.height() << std::endl;
Chris@125 1005 #endif
Chris@25 1006
Chris@25 1007 paint.drawRect(r);
Chris@0 1008
Chris@54 1009 if (showLabel) {
Chris@461 1010 if (sx >= 0 && sx < m_cache->width() &&
Chris@54 1011 sy >= 0 && sy < m_cache->height()) {
Chris@461 1012 float value = m_model->getValueAt(sx, sy);
Chris@54 1013 sprintf(labelbuf, "%06f", value);
Chris@54 1014 QString text(labelbuf);
Chris@287 1015 paint.setPen(v->getBackground());
Chris@54 1016 paint.drawText(rx0 + 2,
Chris@54 1017 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
Chris@54 1018 text);
Chris@0 1019 }
Chris@0 1020 }
Chris@0 1021 }
Chris@0 1022 }
Chris@98 1023 }
Chris@0 1024
Chris@98 1025 void
Chris@98 1026 Colour3DPlotLayer::paintDense(View *v, QPainter &paint, QRect rect) const
Chris@98 1027 {
Chris@466 1028 Profiler profiler("Colour3DPlotLayer::paintDense");
Chris@463 1029 if (!m_cache) return;
Chris@463 1030
Chris@466 1031 float modelStart = m_model->getStartFrame();
Chris@466 1032 float modelResolution = m_model->getResolution();
Chris@0 1033
Chris@159 1034 float srRatio =
Chris@159 1035 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@159 1036 float(m_model->getSampleRate());
Chris@159 1037
Chris@98 1038 int x0 = rect.left();
Chris@98 1039 int x1 = rect.right() + 1;
Chris@0 1040
Chris@98 1041 int w = x1 - x0;
Chris@98 1042 int h = v->height();
Chris@160 1043 int sh = m_model->getHeight();
Chris@98 1044
Chris@444 1045 int symin = m_miny;
Chris@444 1046 int symax = m_maxy;
Chris@444 1047 if (symax <= symin) {
Chris@444 1048 symin = 0;
Chris@444 1049 symax = sh;
Chris@444 1050 }
Chris@444 1051 if (symin < 0) symin = 0;
Chris@444 1052 if (symax > sh) symax = sh;
Chris@444 1053
Chris@466 1054 QImage img(w, h, QImage::Format_Indexed8);
Chris@466 1055 img.setColorTable(m_cache->colorTable());
Chris@98 1056
Chris@466 1057 uchar *peaks = new uchar[w];
Chris@466 1058 memset(peaks, 0, w);
Chris@98 1059
Chris@466 1060 int psy1i = -1;
Chris@466 1061
Chris@466 1062 for (int y = 0; y < h; ++y) {
Chris@466 1063
Chris@466 1064 float sy0 = symin + (float(h - y - 1) * (symax - symin)) / h;
Chris@466 1065 float sy1 = symin + (float(h - y) * (symax - symin)) / h;
Chris@466 1066
Chris@466 1067 int sy0i = int(sy0 + 0.001);
Chris@466 1068 int sy1i = int(sy1);
Chris@466 1069
Chris@466 1070 uchar *targetLine = img.scanLine(y);
Chris@466 1071
Chris@466 1072 if (sy0i == sy1i && sy0i == psy1i) { // same scan line as just computed
Chris@466 1073 goto copy;
Chris@105 1074 }
Chris@105 1075
Chris@466 1076 memset(peaks, 0, w);
Chris@466 1077
Chris@466 1078 for (int sy = sy0i; sy <= sy1i; ++sy) {
Chris@347 1079
Chris@466 1080 if (sy < 0 || sy >= m_cache->height()) continue;
Chris@466 1081
Chris@466 1082 uchar *sourceLine = m_cache->scanLine(sy);
Chris@98 1083
Chris@466 1084 long xf = -1, nxf = -1;
Chris@98 1085
Chris@466 1086 for (int x = 0; x < w; ++x) {
Chris@98 1087
Chris@466 1088 xf = nxf;
Chris@466 1089 nxf = v->getFrameForX(x + 1 + x0);
Chris@466 1090
Chris@466 1091 if (xf < 0) {
Chris@466 1092 xf = v->getFrameForX(x + x0);
Chris@466 1093 }
Chris@466 1094
Chris@466 1095 if (xf < 0) {
Chris@466 1096 peaks[x] = 0;
Chris@466 1097 continue;
Chris@466 1098 }
Chris@466 1099
Chris@466 1100 float sx0 = (float(xf) / srRatio - modelStart) / modelResolution;
Chris@466 1101 float sx1 = (float(nxf) / srRatio - modelStart) / modelResolution;
Chris@98 1102
Chris@466 1103 int sx0i = int(sx0 + 0.001);
Chris@466 1104 int sx1i = int(sx1);
Chris@98 1105
Chris@466 1106 uchar peak = 0;
Chris@466 1107 for (int sx = sx0i; sx <= sx1i; ++sx) {
Chris@466 1108 if (sx < 0 || sx >= m_cache->width()) continue;
Chris@466 1109 if (sourceLine[sx] > peak) peak = sourceLine[sx];
Chris@98 1110 }
Chris@466 1111 peaks[x] = peak;
Chris@98 1112 }
Chris@466 1113 }
Chris@466 1114
Chris@466 1115 copy:
Chris@466 1116 for (int x = 0; x < w; ++x) {
Chris@466 1117 targetLine[x] = peaks[x];
Chris@98 1118 }
Chris@0 1119 }
Chris@0 1120
Chris@98 1121 paint.drawImage(x0, 0, img);
Chris@0 1122 }
Chris@0 1123
Chris@28 1124 bool
Chris@44 1125 Colour3DPlotLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 1126 size_t &resolution,
Chris@28 1127 SnapType snap) const
Chris@24 1128 {
Chris@24 1129 if (!m_model) {
Chris@44 1130 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@24 1131 }
Chris@24 1132
Chris@130 1133 resolution = m_model->getResolution();
Chris@28 1134 int left = (frame / resolution) * resolution;
Chris@28 1135 int right = left + resolution;
Chris@28 1136
Chris@28 1137 switch (snap) {
Chris@28 1138 case SnapLeft: frame = left; break;
Chris@28 1139 case SnapRight: frame = right; break;
Chris@28 1140 case SnapNearest:
Chris@28 1141 case SnapNeighbouring:
Chris@28 1142 if (frame - left > right - frame) frame = right;
Chris@28 1143 else frame = left;
Chris@28 1144 break;
Chris@28 1145 }
Chris@24 1146
Chris@28 1147 return true;
Chris@24 1148 }
Chris@24 1149
Chris@316 1150 void
Chris@316 1151 Colour3DPlotLayer::toXml(QTextStream &stream,
Chris@316 1152 QString indent, QString extraAttributes) const
Chris@197 1153 {
Chris@316 1154 QString s = QString("scale=\"%1\" "
Chris@316 1155 "colourScheme=\"%2\" "
Chris@316 1156 "normalizeColumns=\"%3\" "
Chris@445 1157 "normalizeVisibleArea=\"%4\" "
Chris@445 1158 "minY=\"%5\" "
Chris@465 1159 "maxY=\"%6\" "
Chris@465 1160 "invertVertical=\"%7\" "
Chris@465 1161 "opaque=\"%8\"")
Chris@197 1162 .arg((int)m_colourScale)
Chris@197 1163 .arg(m_colourMap)
Chris@197 1164 .arg(m_normalizeColumns ? "true" : "false")
Chris@445 1165 .arg(m_normalizeVisibleArea ? "true" : "false")
Chris@445 1166 .arg(m_miny)
Chris@465 1167 .arg(m_maxy)
Chris@465 1168 .arg(m_invertVertical ? "true" : "false")
Chris@465 1169 .arg(m_opaque ? "true" : "false");
Chris@197 1170
Chris@316 1171 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@197 1172 }
Chris@197 1173
Chris@197 1174 void
Chris@197 1175 Colour3DPlotLayer::setProperties(const QXmlAttributes &attributes)
Chris@197 1176 {
Chris@445 1177 bool ok = false, alsoOk = false;
Chris@197 1178
Chris@197 1179 ColourScale scale = (ColourScale)attributes.value("scale").toInt(&ok);
Chris@197 1180 if (ok) setColourScale(scale);
Chris@197 1181
Chris@197 1182 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 1183 if (ok) setColourMap(colourMap);
Chris@197 1184
Chris@197 1185 bool normalizeColumns =
Chris@197 1186 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@197 1187 setNormalizeColumns(normalizeColumns);
Chris@197 1188
Chris@197 1189 bool normalizeVisibleArea =
Chris@197 1190 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@197 1191 setNormalizeVisibleArea(normalizeVisibleArea);
Chris@445 1192
Chris@465 1193 bool invertVertical =
Chris@465 1194 (attributes.value("invertVertical").trimmed() == "true");
Chris@465 1195 setInvertVertical(invertVertical);
Chris@465 1196
Chris@465 1197 bool opaque =
Chris@465 1198 (attributes.value("opaque").trimmed() == "true");
Chris@465 1199 setNormalizeVisibleArea(opaque);
Chris@465 1200
Chris@445 1201 float min = attributes.value("minY").toFloat(&ok);
Chris@445 1202 float max = attributes.value("maxY").toFloat(&alsoOk);
Chris@445 1203 if (ok && alsoOk) setDisplayExtents(min, max);
Chris@197 1204 }
Chris@197 1205