annotate layer/Colour3DPlotLayer.cpp @ 213:df791d8c8f58

* Fix real-time plugin corruption when getLatency is called for some plugins * Other minor changes
author Chris Cannam
date Thu, 01 Mar 2007 15:35:27 +0000
parents 6969f21da18a
children 34bbbcb3c01f
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@197 21 #include "ColourMapper.h"
Chris@0 22
Chris@0 23 #include <QPainter>
Chris@0 24 #include <QImage>
Chris@0 25 #include <QRect>
Chris@0 26
Chris@0 27 #include <iostream>
Chris@0 28
Chris@0 29 #include <cassert>
Chris@0 30
Chris@125 31 //#define DEBUG_COLOUR_3D_PLOT_LAYER_PAINT 1
Chris@125 32
Chris@0 33
Chris@44 34 Colour3DPlotLayer::Colour3DPlotLayer() :
Chris@0 35 m_model(0),
Chris@159 36 m_cache(0),
Chris@197 37 m_cacheStart(0),
Chris@197 38 m_colourScale(LinearScale),
Chris@197 39 m_colourMap(0),
Chris@197 40 m_normalizeColumns(false),
Chris@197 41 m_normalizeVisibleArea(false)
Chris@0 42 {
Chris@44 43
Chris@0 44 }
Chris@0 45
Chris@0 46 Colour3DPlotLayer::~Colour3DPlotLayer()
Chris@0 47 {
Chris@0 48 }
Chris@0 49
Chris@0 50 void
Chris@0 51 Colour3DPlotLayer::setModel(const DenseThreeDimensionalModel *model)
Chris@0 52 {
Chris@193 53 if (m_model == model) return;
Chris@193 54 const DenseThreeDimensionalModel *oldModel = m_model;
Chris@0 55 m_model = model;
Chris@0 56 if (!m_model || !m_model->isOK()) return;
Chris@0 57
Chris@0 58 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@0 59 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 60 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@0 61
Chris@0 62 connect(m_model, SIGNAL(completionChanged()),
Chris@0 63 this, SIGNAL(modelCompletionChanged()));
Chris@0 64
Chris@0 65 connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid()));
Chris@0 66 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 67 this, SLOT(cacheInvalid(size_t, size_t)));
Chris@0 68
Chris@0 69 emit modelReplaced();
Chris@193 70 emit sliceableModelReplaced(oldModel, model);
Chris@0 71 }
Chris@0 72
Chris@0 73 void
Chris@0 74 Colour3DPlotLayer::cacheInvalid()
Chris@0 75 {
Chris@0 76 delete m_cache;
Chris@0 77 m_cache = 0;
Chris@0 78 }
Chris@0 79
Chris@0 80 void
Chris@0 81 Colour3DPlotLayer::cacheInvalid(size_t, size_t)
Chris@0 82 {
Chris@0 83 cacheInvalid();
Chris@0 84 }
Chris@0 85
Chris@159 86 Layer::PropertyList
Chris@159 87 Colour3DPlotLayer::getProperties() const
Chris@159 88 {
Chris@159 89 PropertyList list;
Chris@197 90 list.push_back("Colour");
Chris@159 91 list.push_back("Colour Scale");
Chris@197 92 list.push_back("Normalize Columns");
Chris@197 93 list.push_back("Normalize Visible Area");
Chris@159 94 return list;
Chris@159 95 }
Chris@159 96
Chris@159 97 QString
Chris@159 98 Colour3DPlotLayer::getPropertyLabel(const PropertyName &name) const
Chris@159 99 {
Chris@197 100 if (name == "Colour") return tr("Colour");
Chris@197 101 if (name == "Colour Scale") return tr("Scale");
Chris@197 102 if (name == "Normalize Columns") return tr("Normalize Columns");
Chris@197 103 if (name == "Normalize Visible Area") return tr("Normalize Visible Area");
Chris@159 104 return "";
Chris@159 105 }
Chris@159 106
Chris@159 107 Layer::PropertyType
Chris@159 108 Colour3DPlotLayer::getPropertyType(const PropertyName &name) const
Chris@159 109 {
Chris@197 110 if (name == "Normalize Columns") return ToggleProperty;
Chris@197 111 if (name == "Normalize Visible Area") return ToggleProperty;
Chris@159 112 return ValueProperty;
Chris@159 113 }
Chris@159 114
Chris@159 115 QString
Chris@159 116 Colour3DPlotLayer::getPropertyGroupName(const PropertyName &name) const
Chris@159 117 {
Chris@197 118 if (name == "Normalize Columns" ||
Chris@197 119 name == "Normalize Visible Area" ||
Chris@197 120 name == "Colour Scale") return tr("Scale");
Chris@159 121 return QString();
Chris@159 122 }
Chris@159 123
Chris@159 124 int
Chris@159 125 Colour3DPlotLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@159 126 int *min, int *max) const
Chris@159 127 {
Chris@159 128 int deft = 0;
Chris@159 129
Chris@159 130 int garbage0, garbage1;
Chris@159 131 if (!min) min = &garbage0;
Chris@159 132 if (!max) max = &garbage1;
Chris@159 133
Chris@159 134 if (name == "Colour Scale") {
Chris@159 135
Chris@159 136 *min = 0;
Chris@197 137 *max = 2;
Chris@159 138
Chris@159 139 deft = (int)m_colourScale;
Chris@159 140
Chris@197 141 } else if (name == "Colour") {
Chris@197 142
Chris@197 143 *min = 0;
Chris@197 144 *max = ColourMapper::getColourMapCount() - 1;
Chris@197 145
Chris@197 146 deft = m_colourMap;
Chris@197 147
Chris@197 148 } else if (name == "Normalize Columns") {
Chris@197 149
Chris@197 150 deft = (m_normalizeColumns ? 1 : 0);
Chris@197 151
Chris@197 152 } else if (name == "Normalize Visible Area") {
Chris@197 153
Chris@197 154 deft = (m_normalizeVisibleArea ? 1 : 0);
Chris@197 155
Chris@159 156 } else {
Chris@159 157 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@159 158 }
Chris@159 159
Chris@159 160 return deft;
Chris@159 161 }
Chris@159 162
Chris@159 163 QString
Chris@159 164 Colour3DPlotLayer::getPropertyValueLabel(const PropertyName &name,
Chris@159 165 int value) const
Chris@159 166 {
Chris@197 167 if (name == "Colour") {
Chris@197 168 return ColourMapper::getColourMapName(value);
Chris@197 169 }
Chris@159 170 if (name == "Colour Scale") {
Chris@159 171 switch (value) {
Chris@159 172 default:
Chris@198 173 case 0: return tr("Linear");
Chris@198 174 case 1: return tr("Log");
Chris@198 175 case 2: return tr("+/-1");
Chris@159 176 }
Chris@159 177 }
Chris@159 178 return tr("<unknown>");
Chris@159 179 }
Chris@159 180
Chris@159 181 void
Chris@159 182 Colour3DPlotLayer::setProperty(const PropertyName &name, int value)
Chris@159 183 {
Chris@159 184 if (name == "Colour Scale") {
Chris@159 185 switch (value) {
Chris@159 186 default:
Chris@159 187 case 0: setColourScale(LinearScale); break;
Chris@197 188 case 1: setColourScale(LogScale); break;
Chris@197 189 case 2: setColourScale(PlusMinusOneScale); break;
Chris@159 190 }
Chris@197 191 } else if (name == "Colour") {
Chris@197 192 setColourMap(value);
Chris@197 193 } else if (name == "Normalize Columns") {
Chris@197 194 setNormalizeColumns(value ? true : false);
Chris@197 195 } else if (name == "Normalize Visible Area") {
Chris@197 196 setNormalizeVisibleArea(value ? true : false);
Chris@159 197 }
Chris@159 198 }
Chris@159 199
Chris@159 200 void
Chris@159 201 Colour3DPlotLayer::setColourScale(ColourScale scale)
Chris@159 202 {
Chris@159 203 if (m_colourScale == scale) return;
Chris@159 204 m_colourScale = scale;
Chris@159 205 cacheInvalid();
Chris@159 206 emit layerParametersChanged();
Chris@159 207 }
Chris@159 208
Chris@197 209 void
Chris@197 210 Colour3DPlotLayer::setColourMap(int map)
Chris@197 211 {
Chris@197 212 if (m_colourMap == map) return;
Chris@197 213 m_colourMap = map;
Chris@197 214 cacheInvalid();
Chris@197 215 emit layerParametersChanged();
Chris@197 216 }
Chris@197 217
Chris@197 218 void
Chris@197 219 Colour3DPlotLayer::setNormalizeColumns(bool n)
Chris@197 220 {
Chris@197 221 if (m_normalizeColumns == n) return;
Chris@197 222 m_normalizeColumns = n;
Chris@197 223 cacheInvalid();
Chris@197 224 emit layerParametersChanged();
Chris@197 225 }
Chris@197 226
Chris@197 227 bool
Chris@197 228 Colour3DPlotLayer::getNormalizeColumns() const
Chris@197 229 {
Chris@197 230 return m_normalizeColumns;
Chris@197 231 }
Chris@197 232
Chris@197 233 void
Chris@197 234 Colour3DPlotLayer::setNormalizeVisibleArea(bool n)
Chris@197 235 {
Chris@197 236 if (m_normalizeVisibleArea == n) return;
Chris@197 237 m_normalizeVisibleArea = n;
Chris@197 238 cacheInvalid();
Chris@197 239 emit layerParametersChanged();
Chris@197 240 }
Chris@197 241
Chris@197 242 bool
Chris@197 243 Colour3DPlotLayer::getNormalizeVisibleArea() const
Chris@197 244 {
Chris@197 245 return m_normalizeVisibleArea;
Chris@197 246 }
Chris@197 247
Chris@25 248 bool
Chris@44 249 Colour3DPlotLayer::isLayerScrollable(const View *v) const
Chris@25 250 {
Chris@197 251 if (m_normalizeVisibleArea) return false;
Chris@25 252 QPoint discard;
Chris@44 253 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@25 254 }
Chris@25 255
Chris@25 256 QString
Chris@44 257 Colour3DPlotLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@25 258 {
Chris@25 259 if (!m_model) return "";
Chris@25 260
Chris@25 261 int x = pos.x();
Chris@25 262 int y = pos.y();
Chris@25 263
Chris@25 264 size_t modelStart = m_model->getStartFrame();
Chris@130 265 size_t modelResolution = m_model->getResolution();
Chris@25 266
Chris@159 267 float srRatio =
Chris@159 268 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@159 269 float(m_model->getSampleRate());
Chris@159 270
Chris@160 271 int sx0 = int((v->getFrameForX(x) / srRatio - long(modelStart)) /
Chris@160 272 long(modelResolution));
Chris@25 273
Chris@160 274 int f0 = sx0 * modelResolution;
Chris@160 275 int f1 = f0 + modelResolution;
Chris@160 276
Chris@160 277 float binHeight = float(v->height()) / m_model->getHeight();
Chris@44 278 int sy = (v->height() - y) / binHeight;
Chris@25 279
Chris@160 280 float value = m_model->getValueAt(sx0, sy);
Chris@159 281
Chris@159 282 // std::cerr << "bin value (" << sx0 << "," << sy << ") is " << value << std::endl;
Chris@25 283
Chris@25 284 QString binName = m_model->getBinName(sy);
Chris@25 285 if (binName == "") binName = QString("[%1]").arg(sy + 1);
Chris@25 286 else binName = QString("%1 [%2]").arg(binName).arg(sy + 1);
Chris@25 287
Chris@25 288 QString text = tr("Time:\t%1 - %2\nBin:\t%3\nValue:\t%4")
Chris@160 289 .arg(RealTime::frame2RealTime(f0, m_model->getSampleRate())
Chris@25 290 .toText(true).c_str())
Chris@160 291 .arg(RealTime::frame2RealTime(f1, m_model->getSampleRate())
Chris@25 292 .toText(true).c_str())
Chris@25 293 .arg(binName)
Chris@25 294 .arg(value);
Chris@25 295
Chris@25 296 return text;
Chris@25 297 }
Chris@25 298
Chris@25 299 int
Chris@159 300 Colour3DPlotLayer::getColourScaleWidth(QPainter &paint) const
Chris@159 301 {
Chris@159 302 int cw = 20;
Chris@159 303 return cw;
Chris@159 304 }
Chris@159 305
Chris@159 306 int
Chris@44 307 Colour3DPlotLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
Chris@25 308 {
Chris@25 309 if (!m_model) return 0;
Chris@25 310
Chris@160 311 QString sampleText = QString("[%1]").arg(m_model->getHeight());
Chris@25 312 int tw = paint.fontMetrics().width(sampleText);
Chris@98 313 bool another = false;
Chris@25 314
Chris@160 315 for (size_t i = 0; i < m_model->getHeight(); ++i) {
Chris@25 316 if (m_model->getBinName(i).length() > sampleText.length()) {
Chris@25 317 sampleText = m_model->getBinName(i);
Chris@98 318 another = true;
Chris@25 319 }
Chris@25 320 }
Chris@98 321 if (another) {
Chris@25 322 tw = std::max(tw, paint.fontMetrics().width(sampleText));
Chris@25 323 }
Chris@25 324
Chris@159 325 return tw + 13 + getColourScaleWidth(paint);
Chris@25 326 }
Chris@25 327
Chris@25 328 void
Chris@44 329 Colour3DPlotLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@25 330 {
Chris@25 331 if (!m_model) return;
Chris@25 332
Chris@25 333 int h = rect.height(), w = rect.width();
Chris@160 334 float binHeight = float(v->height()) / m_model->getHeight();
Chris@25 335
Chris@159 336 int cw = getColourScaleWidth(paint);
Chris@159 337
Chris@159 338 int ch = h - 20;
Chris@159 339 if (ch > 20 && m_cache) {
Chris@159 340
Chris@159 341 paint.setPen(Qt::black);
Chris@159 342 paint.drawRect(4, 10, cw - 8, ch - 19);
Chris@159 343
Chris@159 344 for (int y = 0; y < ch - 20; ++y) {
Chris@159 345 QRgb c = m_cache->color(((ch - 20 - y) * 255) / (ch - 20));
Chris@159 346 // std::cerr << "y = " << y << ": rgb " << qRed(c) << "," << qGreen(c) << "," << qBlue(c) << std::endl;
Chris@159 347 paint.setPen(QColor(qRed(c), qGreen(c), qBlue(c)));
Chris@159 348 paint.drawLine(5, 11 + y, cw - 5, 11 + y);
Chris@159 349 }
Chris@159 350 }
Chris@159 351
Chris@159 352 paint.setPen(Qt::black);
Chris@159 353
Chris@98 354 int count = v->height() / paint.fontMetrics().height();
Chris@160 355 int step = m_model->getHeight() / count;
Chris@98 356 if (step == 0) step = 1;
Chris@25 357
Chris@160 358 for (size_t i = 0; i < m_model->getHeight(); ++i) {
Chris@25 359
Chris@98 360 if ((i % step) != 0) continue;
Chris@98 361
Chris@44 362 int y0 = v->height() - (i * binHeight) - 1;
Chris@25 363
Chris@25 364 QString text = m_model->getBinName(i);
Chris@25 365 if (text == "") text = QString("[%1]").arg(i + 1);
Chris@25 366
Chris@159 367 paint.drawLine(cw, y0, w, y0);
Chris@25 368
Chris@98 369 int cy = y0 - (step * binHeight)/2;
Chris@25 370 int ty = cy + paint.fontMetrics().ascent()/2;
Chris@25 371
Chris@159 372 paint.drawText(cw + 5, ty, text);
Chris@25 373 }
Chris@25 374 }
Chris@25 375
Chris@0 376 void
Chris@197 377 Colour3DPlotLayer::getColumn(size_t col,
Chris@197 378 DenseThreeDimensionalModel::Column &values) const
Chris@197 379 {
Chris@197 380 m_model->getColumn(col, values);
Chris@197 381
Chris@197 382 float colMax = 0.f;
Chris@197 383
Chris@197 384 float min = 0.f, max = 0.f;
Chris@197 385 if (m_normalizeColumns) {
Chris@197 386 min = m_model->getMinimumLevel();
Chris@197 387 max = m_model->getMaximumLevel();
Chris@197 388 }
Chris@197 389
Chris@197 390 if (m_normalizeColumns) {
Chris@197 391 for (size_t y = 0; y < values.size(); ++y) {
Chris@197 392 if (values[y] > colMax || y == 0) colMax = values[y];
Chris@197 393 }
Chris@197 394 if (m_colourScale == LogScale) {
Chris@197 395 colMax = LogRange::map(colMax);
Chris@197 396 }
Chris@197 397 }
Chris@197 398
Chris@197 399 for (size_t y = 0; y < values.size(); ++y) {
Chris@197 400
Chris@197 401 float value = min;
Chris@197 402
Chris@197 403 value = values[y];
Chris@197 404 if (m_colourScale == LogScale) {
Chris@197 405 value = LogRange::map(value);
Chris@197 406 }
Chris@197 407
Chris@197 408 if (m_normalizeColumns) {
Chris@197 409 if (colMax != 0) {
Chris@197 410 value = max * (value / colMax);
Chris@197 411 } else {
Chris@197 412 value = 0;
Chris@197 413 }
Chris@197 414 }
Chris@197 415
Chris@197 416 values[y] = value;
Chris@197 417 }
Chris@197 418 }
Chris@197 419
Chris@197 420 void
Chris@197 421 Colour3DPlotLayer::fillCache(size_t firstBin, size_t lastBin) const
Chris@197 422 {
Chris@197 423 size_t modelStart = m_model->getStartFrame();
Chris@197 424 size_t modelEnd = m_model->getEndFrame();
Chris@197 425 size_t modelResolution = m_model->getResolution();
Chris@197 426
Chris@197 427 std::cerr << "Colour3DPlotLayer::fillCache: " << firstBin << " -> " << lastBin << std::endl;
Chris@197 428
Chris@197 429 if (!m_normalizeVisibleArea || m_normalizeColumns) {
Chris@197 430 firstBin = modelStart / modelResolution;
Chris@197 431 lastBin = modelEnd / modelResolution;
Chris@197 432 }
Chris@197 433
Chris@197 434 size_t cacheWidth = lastBin - firstBin + 1;
Chris@197 435 size_t cacheHeight = m_model->getHeight();
Chris@197 436
Chris@197 437 if (m_cache &&
Chris@197 438 (m_cacheStart != firstBin ||
Chris@197 439 m_cache->width() != cacheWidth ||
Chris@197 440 m_cache->height() != cacheHeight)) {
Chris@197 441
Chris@197 442 delete m_cache;
Chris@197 443 m_cache = 0;
Chris@197 444 }
Chris@197 445
Chris@197 446 if (m_cache) return;
Chris@197 447
Chris@197 448 m_cache = new QImage(cacheWidth, cacheHeight, QImage::Format_Indexed8);
Chris@197 449 m_cacheStart = firstBin;
Chris@197 450
Chris@197 451 std::cerr << "Cache size " << cacheWidth << "x" << cacheHeight << " starting " << m_cacheStart << std::endl;
Chris@197 452
Chris@197 453 m_cache->setNumColors(256);
Chris@197 454 DenseThreeDimensionalModel::Column values;
Chris@197 455
Chris@197 456 float min = m_model->getMinimumLevel();
Chris@197 457 float max = m_model->getMaximumLevel();
Chris@197 458
Chris@197 459 if (m_colourScale == LogScale) {
Chris@197 460 LogRange::mapRange(min, max);
Chris@197 461 } else if (m_colourScale == PlusMinusOneScale) {
Chris@197 462 min = -1.f;
Chris@197 463 max = 1.f;
Chris@197 464 }
Chris@197 465
Chris@197 466 if (max == min) max = min + 1.0;
Chris@197 467
Chris@197 468 ColourMapper mapper(m_colourMap, 0.f, 255.f);
Chris@197 469
Chris@197 470 for (int index = 0; index < 256; ++index) {
Chris@197 471
Chris@197 472 QColor colour = mapper.map(index);
Chris@197 473 m_cache->setColor(index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@197 474 }
Chris@197 475
Chris@197 476 m_cache->fill(0);
Chris@197 477
Chris@197 478 float visibleMax = 0.f;
Chris@197 479
Chris@197 480 if (m_normalizeVisibleArea && !m_normalizeColumns) {
Chris@197 481
Chris@197 482 for (size_t c = firstBin; c <= lastBin; ++c) {
Chris@197 483
Chris@197 484 values.clear();
Chris@197 485 getColumn(c, values);
Chris@197 486
Chris@197 487 float colMax = 0.f;
Chris@197 488
Chris@197 489 for (size_t y = 0; y < m_model->getHeight(); ++y) {
Chris@197 490 if (y >= values.size()) break;
Chris@197 491 if (y == 0 || values[y] > colMax) colMax = values[y];
Chris@197 492 }
Chris@197 493
Chris@197 494 if (c == firstBin || colMax > visibleMax) visibleMax = colMax;
Chris@197 495 }
Chris@197 496 }
Chris@197 497
Chris@197 498 for (size_t c = firstBin; c <= lastBin; ++c) {
Chris@197 499
Chris@197 500 values.clear();
Chris@197 501 getColumn(c, values);
Chris@197 502
Chris@197 503 for (size_t y = 0; y < m_model->getHeight(); ++y) {
Chris@197 504
Chris@197 505 float value = min;
Chris@197 506 if (y < values.size()) {
Chris@197 507 value = values[y];
Chris@197 508 }
Chris@197 509
Chris@197 510 if (m_normalizeVisibleArea && !m_normalizeColumns) {
Chris@197 511 if (visibleMax != 0) {
Chris@197 512 value = max * (value / visibleMax);
Chris@197 513 }
Chris@197 514 }
Chris@197 515
Chris@197 516 int pixel = int(((value - min) * 256) / (max - min));
Chris@197 517 if (pixel < 0) pixel = 0;
Chris@197 518 if (pixel > 255) pixel = 255;
Chris@197 519
Chris@197 520 m_cache->setPixel(c - firstBin, y, pixel);
Chris@197 521 }
Chris@197 522 }
Chris@197 523 }
Chris@197 524
Chris@197 525 void
Chris@44 526 Colour3DPlotLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 527 {
Chris@0 528 // Profiler profiler("Colour3DPlotLayer::paint");
Chris@125 529 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@125 530 std::cerr << "Colour3DPlotLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << std::endl;
Chris@125 531 #endif
Chris@0 532
Chris@0 533 int completion = 0;
Chris@0 534 if (!m_model || !m_model->isOK() || !m_model->isReady(&completion)) {
Chris@0 535 if (completion > 0) {
Chris@44 536 paint.fillRect(0, 10, v->width() * completion / 100,
Chris@0 537 10, QColor(120, 120, 120));
Chris@0 538 }
Chris@0 539 return;
Chris@0 540 }
Chris@0 541
Chris@197 542 if (m_normalizeVisibleArea && !m_normalizeColumns) rect = v->rect();
Chris@197 543
Chris@0 544 size_t modelStart = m_model->getStartFrame();
Chris@0 545 size_t modelEnd = m_model->getEndFrame();
Chris@130 546 size_t modelResolution = m_model->getResolution();
Chris@0 547
Chris@197 548 // The cache is from the model's start frame to the model's end
Chris@197 549 // frame at the model's window increment frames per pixel. We
Chris@197 550 // want to draw from our start frame + x0 * zoomLevel to our start
Chris@197 551 // frame + x1 * zoomLevel at zoomLevel frames per pixel.
Chris@12 552
Chris@197 553 // We have quite different paint mechanisms for rendering "large"
Chris@197 554 // bins (more than one bin per pixel in both directions) and
Chris@197 555 // "small". This is "large"; see paintDense below for "small".
Chris@12 556
Chris@197 557 int x0 = rect.left();
Chris@197 558 int x1 = rect.right() + 1;
Chris@12 559
Chris@197 560 int h = v->height();
Chris@0 561
Chris@197 562 float srRatio =
Chris@197 563 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@197 564 float(m_model->getSampleRate());
Chris@0 565
Chris@197 566 int sx0 = int((v->getFrameForX(x0) / srRatio - long(modelStart))
Chris@197 567 / long(modelResolution));
Chris@197 568 int sx1 = int((v->getFrameForX(x1) / srRatio - long(modelStart))
Chris@197 569 / long(modelResolution));
Chris@197 570 int sh = m_model->getHeight();
Chris@159 571
Chris@197 572 if (sx0 > 0) --sx0;
Chris@197 573 fillCache(sx0 < 0 ? 0 : sx0,
Chris@197 574 sx1 < 0 ? 0 : sx1);
Chris@0 575
Chris@160 576 if (m_model->getHeight() >= v->height() ||
Chris@159 577 modelResolution < v->getZoomLevel() / 2) {
Chris@98 578 paintDense(v, paint, rect);
Chris@98 579 return;
Chris@98 580 }
Chris@98 581
Chris@125 582 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@0 583 std::cerr << "Colour3DPlotLayer::paint: w " << w << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sw << ", sh " << sh << std::endl;
Chris@130 584 std::cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << std::endl;
Chris@125 585 #endif
Chris@0 586
Chris@25 587 QPoint illuminatePos;
Chris@44 588 bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos);
Chris@54 589 char labelbuf[10];
Chris@25 590
Chris@197 591 for (int sx = sx0; sx <= sx1; ++sx) {
Chris@0 592
Chris@197 593 int scx = 0;
Chris@197 594 if (sx > m_cacheStart) scx = sx - m_cacheStart;
Chris@197 595
Chris@130 596 int fx = sx * int(modelResolution);
Chris@0 597
Chris@130 598 if (fx + modelResolution < int(modelStart) ||
Chris@0 599 fx > int(modelEnd)) continue;
Chris@0 600
Chris@159 601 int rx0 = v->getXForFrame((fx + int(modelStart)) * srRatio);
Chris@159 602 int rx1 = v->getXForFrame((fx + int(modelStart) + int(modelResolution) + 1) * srRatio);
Chris@54 603
Chris@159 604 int rw = rx1 - rx0;
Chris@159 605 if (rw < 1) rw = 1;
Chris@54 606
Chris@159 607 bool showLabel = (rw > 10 &&
Chris@159 608 paint.fontMetrics().width("0.000000") < rw - 3 &&
Chris@54 609 paint.fontMetrics().height() < (h / sh));
Chris@98 610
Chris@0 611 for (int sy = 0; sy < sh; ++sy) {
Chris@0 612
Chris@0 613 int ry0 = h - (sy * h) / sh - 1;
Chris@0 614 QRgb pixel = qRgb(255, 255, 255);
Chris@197 615 if (scx >= 0 && scx < m_cache->width() &&
Chris@0 616 sy >= 0 && sy < m_cache->height()) {
Chris@197 617 pixel = m_cache->pixel(scx, sy);
Chris@0 618 }
Chris@0 619
Chris@159 620 QRect r(rx0, ry0 - h / sh - 1, rw, h / sh + 1);
Chris@159 621
Chris@159 622 if (rw == 1) {
Chris@159 623 paint.setPen(pixel);
Chris@159 624 paint.setBrush(Qt::NoBrush);
Chris@159 625 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
Chris@159 626 continue;
Chris@159 627 }
Chris@159 628
Chris@0 629 QColor pen(255, 255, 255, 80);
Chris@0 630 QColor brush(pixel);
Chris@159 631
Chris@159 632 if (rw > 3 && r.height() > 3) {
Chris@159 633 brush.setAlpha(160);
Chris@159 634 }
Chris@159 635
Chris@0 636 paint.setPen(Qt::NoPen);
Chris@0 637 paint.setBrush(brush);
Chris@0 638
Chris@25 639 if (illuminate) {
Chris@25 640 if (r.contains(illuminatePos)) {
Chris@25 641 paint.setPen(Qt::black);//!!!
Chris@25 642 }
Chris@25 643 }
Chris@76 644
Chris@125 645 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@159 646 std::cerr << "rect " << r.x() << "," << r.y() << " "
Chris@159 647 << r.width() << "x" << r.height() << std::endl;
Chris@125 648 #endif
Chris@25 649
Chris@25 650 paint.drawRect(r);
Chris@0 651
Chris@54 652 if (showLabel) {
Chris@197 653 if (scx >= 0 && scx < m_cache->width() &&
Chris@54 654 sy >= 0 && sy < m_cache->height()) {
Chris@197 655 float value = m_model->getValueAt(scx, sy);
Chris@54 656 sprintf(labelbuf, "%06f", value);
Chris@54 657 QString text(labelbuf);
Chris@54 658 paint.setPen(Qt::white);
Chris@54 659 paint.drawText(rx0 + 2,
Chris@54 660 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
Chris@54 661 text);
Chris@0 662 }
Chris@0 663 }
Chris@0 664 }
Chris@0 665 }
Chris@98 666 }
Chris@0 667
Chris@98 668 void
Chris@98 669 Colour3DPlotLayer::paintDense(View *v, QPainter &paint, QRect rect) const
Chris@98 670 {
Chris@98 671 long startFrame = v->getStartFrame();
Chris@98 672 int zoomLevel = v->getZoomLevel();
Chris@0 673
Chris@98 674 size_t modelStart = m_model->getStartFrame();
Chris@98 675 size_t modelEnd = m_model->getEndFrame();
Chris@130 676 size_t modelResolution = m_model->getResolution();
Chris@0 677
Chris@159 678 float srRatio =
Chris@159 679 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@159 680 float(m_model->getSampleRate());
Chris@159 681
Chris@98 682 int x0 = rect.left();
Chris@98 683 int x1 = rect.right() + 1;
Chris@0 684
Chris@98 685 int w = x1 - x0;
Chris@98 686 int h = v->height();
Chris@160 687 int sh = m_model->getHeight();
Chris@98 688
Chris@98 689 QImage img(w, h, QImage::Format_RGB32);
Chris@98 690
Chris@98 691 for (int x = x0; x < x1; ++x) {
Chris@98 692
Chris@159 693 long xf = v->getFrameForX(x) / srRatio;
Chris@105 694 if (xf < 0) {
Chris@105 695 for (int y = 0; y < h; ++y) {
Chris@105 696 img.setPixel(x - x0, y, m_cache->color(0));
Chris@105 697 }
Chris@105 698 continue;
Chris@105 699 }
Chris@105 700
Chris@130 701 float sx0 = (float(xf) - modelStart) / modelResolution;
Chris@159 702 float sx1 = (float(v->getFrameForX(x+1) / srRatio) - modelStart) / modelResolution;
Chris@98 703
Chris@98 704 int sx0i = int(sx0 + 0.001);
Chris@98 705 int sx1i = int(sx1);
Chris@98 706
Chris@98 707 for (int y = 0; y < h; ++y) {
Chris@98 708
Chris@98 709 float sy0 = (float(h - y - 1) * sh) / h;
Chris@98 710 float sy1 = (float(h - y) * sh) / h;
Chris@98 711
Chris@98 712 int sy0i = int(sy0 + 0.001);
Chris@98 713 int sy1i = int(sy1);
Chris@98 714
Chris@98 715 float mag = 0.0, div = 0.0;
Chris@159 716 int max = 0;
Chris@98 717
Chris@98 718 for (int sx = sx0i; sx <= sx1i; ++sx) {
Chris@98 719
Chris@197 720 int scx = 0;
Chris@197 721 if (sx > m_cacheStart) scx = sx - m_cacheStart;
Chris@197 722
Chris@197 723 if (scx < 0 || scx >= m_cache->width()) continue;
Chris@98 724
Chris@98 725 for (int sy = sy0i; sy <= sy1i; ++sy) {
Chris@98 726
Chris@98 727 if (sy < 0 || sy >= m_cache->height()) continue;
Chris@98 728
Chris@98 729 float prop = 1.0;
Chris@98 730 if (sx == sx0i) prop *= (sx + 1) - sx0;
Chris@98 731 if (sx == sx1i) prop *= sx1 - sx;
Chris@98 732 if (sy == sy0i) prop *= (sy + 1) - sy0;
Chris@98 733 if (sy == sy1i) prop *= sy1 - sy;
Chris@98 734
Chris@197 735 mag += prop * m_cache->pixelIndex(scx, sy);
Chris@197 736 max = std::max(max, m_cache->pixelIndex(scx, sy));
Chris@98 737 div += prop;
Chris@98 738 }
Chris@98 739 }
Chris@98 740
Chris@98 741 if (div != 0) mag /= div;
Chris@98 742 if (mag < 0) mag = 0;
Chris@98 743 if (mag > 255) mag = 255;
Chris@159 744 if (max < 0) max = 0;
Chris@159 745 if (max > 255) max = 255;
Chris@98 746
Chris@98 747 img.setPixel(x - x0, y, m_cache->color(int(mag + 0.001)));
Chris@159 748 // img.setPixel(x - x0, y, m_cache->color(max));
Chris@98 749 }
Chris@0 750 }
Chris@0 751
Chris@98 752 paint.drawImage(x0, 0, img);
Chris@0 753 }
Chris@0 754
Chris@28 755 bool
Chris@44 756 Colour3DPlotLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 757 size_t &resolution,
Chris@28 758 SnapType snap) const
Chris@24 759 {
Chris@24 760 if (!m_model) {
Chris@44 761 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@24 762 }
Chris@24 763
Chris@130 764 resolution = m_model->getResolution();
Chris@28 765 int left = (frame / resolution) * resolution;
Chris@28 766 int right = left + resolution;
Chris@28 767
Chris@28 768 switch (snap) {
Chris@28 769 case SnapLeft: frame = left; break;
Chris@28 770 case SnapRight: frame = right; break;
Chris@28 771 case SnapNearest:
Chris@28 772 case SnapNeighbouring:
Chris@28 773 if (frame - left > right - frame) frame = right;
Chris@28 774 else frame = left;
Chris@28 775 break;
Chris@28 776 }
Chris@24 777
Chris@28 778 return true;
Chris@24 779 }
Chris@24 780
Chris@197 781 QString
Chris@197 782 Colour3DPlotLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@197 783 {
Chris@197 784 QString s;
Chris@197 785
Chris@197 786 s += QString("scale=\"%1\" "
Chris@197 787 "colourScheme=\"%2\" "
Chris@197 788 "normalizeColumns=\"%3\" "
Chris@197 789 "normalizeVisibleArea=\"%4\"")
Chris@197 790 .arg((int)m_colourScale)
Chris@197 791 .arg(m_colourMap)
Chris@197 792 .arg(m_normalizeColumns ? "true" : "false")
Chris@197 793 .arg(m_normalizeVisibleArea ? "true" : "false");
Chris@197 794
Chris@197 795 return Layer::toXmlString(indent, extraAttributes + " " + s);
Chris@197 796 }
Chris@197 797
Chris@197 798 void
Chris@197 799 Colour3DPlotLayer::setProperties(const QXmlAttributes &attributes)
Chris@197 800 {
Chris@197 801 bool ok = false;
Chris@197 802
Chris@197 803 ColourScale scale = (ColourScale)attributes.value("scale").toInt(&ok);
Chris@197 804 if (ok) setColourScale(scale);
Chris@197 805
Chris@197 806 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 807 if (ok) setColourMap(colourMap);
Chris@197 808
Chris@197 809 bool normalizeColumns =
Chris@197 810 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@197 811 setNormalizeColumns(normalizeColumns);
Chris@197 812
Chris@197 813 bool normalizeVisibleArea =
Chris@197 814 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@197 815 setNormalizeVisibleArea(normalizeVisibleArea);
Chris@197 816 }
Chris@197 817