annotate layer/Colour3DPlotLayer.cpp @ 193:57c2350a8c40

* Add slice layers (so you can display a slice of a colour 3d plot as if it were a spectrum) * Make spectrum layer a subclass of slice layer
author Chris Cannam
date Fri, 26 Jan 2007 16:59:57 +0000
parents 42118892f428
children 6b023411087b
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@0 20
Chris@0 21 #include <QPainter>
Chris@0 22 #include <QImage>
Chris@0 23 #include <QRect>
Chris@0 24
Chris@0 25 #include <iostream>
Chris@0 26
Chris@0 27 #include <cassert>
Chris@0 28
Chris@125 29 //#define DEBUG_COLOUR_3D_PLOT_LAYER_PAINT 1
Chris@125 30
Chris@0 31
Chris@44 32 Colour3DPlotLayer::Colour3DPlotLayer() :
Chris@0 33 m_model(0),
Chris@159 34 m_cache(0),
Chris@159 35 m_colourScale(LinearScale)
Chris@0 36 {
Chris@44 37
Chris@0 38 }
Chris@0 39
Chris@0 40 Colour3DPlotLayer::~Colour3DPlotLayer()
Chris@0 41 {
Chris@0 42 }
Chris@0 43
Chris@0 44 void
Chris@0 45 Colour3DPlotLayer::setModel(const DenseThreeDimensionalModel *model)
Chris@0 46 {
Chris@193 47 if (m_model == model) return;
Chris@193 48 const DenseThreeDimensionalModel *oldModel = m_model;
Chris@0 49 m_model = model;
Chris@0 50 if (!m_model || !m_model->isOK()) return;
Chris@0 51
Chris@0 52 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@0 53 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 54 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@0 55
Chris@0 56 connect(m_model, SIGNAL(completionChanged()),
Chris@0 57 this, SIGNAL(modelCompletionChanged()));
Chris@0 58
Chris@0 59 connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid()));
Chris@0 60 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 61 this, SLOT(cacheInvalid(size_t, size_t)));
Chris@0 62
Chris@0 63 emit modelReplaced();
Chris@193 64 emit sliceableModelReplaced(oldModel, model);
Chris@0 65 }
Chris@0 66
Chris@0 67 void
Chris@0 68 Colour3DPlotLayer::cacheInvalid()
Chris@0 69 {
Chris@0 70 delete m_cache;
Chris@0 71 m_cache = 0;
Chris@0 72 }
Chris@0 73
Chris@0 74 void
Chris@0 75 Colour3DPlotLayer::cacheInvalid(size_t, size_t)
Chris@0 76 {
Chris@0 77 cacheInvalid();
Chris@0 78 }
Chris@0 79
Chris@159 80 Layer::PropertyList
Chris@159 81 Colour3DPlotLayer::getProperties() const
Chris@159 82 {
Chris@159 83 PropertyList list;
Chris@159 84 list.push_back("Colour Scale");
Chris@159 85 return list;
Chris@159 86 }
Chris@159 87
Chris@159 88 QString
Chris@159 89 Colour3DPlotLayer::getPropertyLabel(const PropertyName &name) const
Chris@159 90 {
Chris@159 91 if (name == "Colour Scale") return tr("Colour Scale");
Chris@159 92 return "";
Chris@159 93 }
Chris@159 94
Chris@159 95 Layer::PropertyType
Chris@159 96 Colour3DPlotLayer::getPropertyType(const PropertyName &name) const
Chris@159 97 {
Chris@159 98 return ValueProperty;
Chris@159 99 }
Chris@159 100
Chris@159 101 QString
Chris@159 102 Colour3DPlotLayer::getPropertyGroupName(const PropertyName &name) const
Chris@159 103 {
Chris@159 104 return QString();
Chris@159 105 }
Chris@159 106
Chris@159 107 int
Chris@159 108 Colour3DPlotLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@159 109 int *min, int *max) const
Chris@159 110 {
Chris@159 111 int deft = 0;
Chris@159 112
Chris@159 113 int garbage0, garbage1;
Chris@159 114 if (!min) min = &garbage0;
Chris@159 115 if (!max) max = &garbage1;
Chris@159 116
Chris@159 117 if (name == "Colour Scale") {
Chris@159 118
Chris@159 119 *min = 0;
Chris@159 120 *max = 3;
Chris@159 121
Chris@159 122 deft = (int)m_colourScale;
Chris@159 123
Chris@159 124 } else {
Chris@159 125 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@159 126 }
Chris@159 127
Chris@159 128 return deft;
Chris@159 129 }
Chris@159 130
Chris@159 131 QString
Chris@159 132 Colour3DPlotLayer::getPropertyValueLabel(const PropertyName &name,
Chris@159 133 int value) const
Chris@159 134 {
Chris@159 135 if (name == "Colour Scale") {
Chris@159 136 switch (value) {
Chris@159 137 default:
Chris@159 138 case 0: return tr("Linear");
Chris@159 139 case 1: return tr("Absolute");
Chris@159 140 case 2: return tr("Meter");
Chris@159 141 case 3: return tr("dB");
Chris@159 142 }
Chris@159 143 }
Chris@159 144 return tr("<unknown>");
Chris@159 145 }
Chris@159 146
Chris@159 147 void
Chris@159 148 Colour3DPlotLayer::setProperty(const PropertyName &name, int value)
Chris@159 149 {
Chris@159 150 if (name == "Colour Scale") {
Chris@159 151 switch (value) {
Chris@159 152 default:
Chris@159 153 case 0: setColourScale(LinearScale); break;
Chris@159 154 case 1: setColourScale(AbsoluteScale); break;
Chris@159 155 case 2: setColourScale(MeterScale); break;
Chris@159 156 case 3: setColourScale(dBScale); break;
Chris@159 157 }
Chris@159 158 }
Chris@159 159 }
Chris@159 160
Chris@159 161 void
Chris@159 162 Colour3DPlotLayer::setColourScale(ColourScale scale)
Chris@159 163 {
Chris@159 164 if (m_colourScale == scale) return;
Chris@159 165 m_colourScale = scale;
Chris@159 166 cacheInvalid();
Chris@159 167 emit layerParametersChanged();
Chris@159 168 }
Chris@159 169
Chris@25 170 bool
Chris@44 171 Colour3DPlotLayer::isLayerScrollable(const View *v) const
Chris@25 172 {
Chris@25 173 QPoint discard;
Chris@44 174 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@25 175 }
Chris@25 176
Chris@25 177 QString
Chris@44 178 Colour3DPlotLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@25 179 {
Chris@25 180 if (!m_model) return "";
Chris@25 181
Chris@25 182 int x = pos.x();
Chris@25 183 int y = pos.y();
Chris@25 184
Chris@25 185 size_t modelStart = m_model->getStartFrame();
Chris@130 186 size_t modelResolution = m_model->getResolution();
Chris@25 187
Chris@159 188 float srRatio =
Chris@159 189 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@159 190 float(m_model->getSampleRate());
Chris@159 191
Chris@160 192 int sx0 = int((v->getFrameForX(x) / srRatio - long(modelStart)) /
Chris@160 193 long(modelResolution));
Chris@25 194
Chris@160 195 int f0 = sx0 * modelResolution;
Chris@160 196 int f1 = f0 + modelResolution;
Chris@160 197
Chris@160 198 float binHeight = float(v->height()) / m_model->getHeight();
Chris@44 199 int sy = (v->height() - y) / binHeight;
Chris@25 200
Chris@160 201 float value = m_model->getValueAt(sx0, sy);
Chris@159 202
Chris@159 203 // std::cerr << "bin value (" << sx0 << "," << sy << ") is " << value << std::endl;
Chris@25 204
Chris@25 205 QString binName = m_model->getBinName(sy);
Chris@25 206 if (binName == "") binName = QString("[%1]").arg(sy + 1);
Chris@25 207 else binName = QString("%1 [%2]").arg(binName).arg(sy + 1);
Chris@25 208
Chris@25 209 QString text = tr("Time:\t%1 - %2\nBin:\t%3\nValue:\t%4")
Chris@160 210 .arg(RealTime::frame2RealTime(f0, m_model->getSampleRate())
Chris@25 211 .toText(true).c_str())
Chris@160 212 .arg(RealTime::frame2RealTime(f1, m_model->getSampleRate())
Chris@25 213 .toText(true).c_str())
Chris@25 214 .arg(binName)
Chris@25 215 .arg(value);
Chris@25 216
Chris@25 217 return text;
Chris@25 218 }
Chris@25 219
Chris@25 220 int
Chris@159 221 Colour3DPlotLayer::getColourScaleWidth(QPainter &paint) const
Chris@159 222 {
Chris@159 223 int cw = 20;
Chris@159 224 return cw;
Chris@159 225 }
Chris@159 226
Chris@159 227 int
Chris@44 228 Colour3DPlotLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
Chris@25 229 {
Chris@25 230 if (!m_model) return 0;
Chris@25 231
Chris@160 232 QString sampleText = QString("[%1]").arg(m_model->getHeight());
Chris@25 233 int tw = paint.fontMetrics().width(sampleText);
Chris@98 234 bool another = false;
Chris@25 235
Chris@160 236 for (size_t i = 0; i < m_model->getHeight(); ++i) {
Chris@25 237 if (m_model->getBinName(i).length() > sampleText.length()) {
Chris@25 238 sampleText = m_model->getBinName(i);
Chris@98 239 another = true;
Chris@25 240 }
Chris@25 241 }
Chris@98 242 if (another) {
Chris@25 243 tw = std::max(tw, paint.fontMetrics().width(sampleText));
Chris@25 244 }
Chris@25 245
Chris@159 246 return tw + 13 + getColourScaleWidth(paint);
Chris@25 247 }
Chris@25 248
Chris@25 249 void
Chris@44 250 Colour3DPlotLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@25 251 {
Chris@25 252 if (!m_model) return;
Chris@25 253
Chris@25 254 int h = rect.height(), w = rect.width();
Chris@160 255 float binHeight = float(v->height()) / m_model->getHeight();
Chris@25 256
Chris@159 257 int cw = getColourScaleWidth(paint);
Chris@159 258
Chris@159 259 int ch = h - 20;
Chris@159 260 if (ch > 20 && m_cache) {
Chris@159 261
Chris@159 262 paint.setPen(Qt::black);
Chris@159 263 paint.drawRect(4, 10, cw - 8, ch - 19);
Chris@159 264
Chris@159 265 for (int y = 0; y < ch - 20; ++y) {
Chris@159 266 QRgb c = m_cache->color(((ch - 20 - y) * 255) / (ch - 20));
Chris@159 267 // std::cerr << "y = " << y << ": rgb " << qRed(c) << "," << qGreen(c) << "," << qBlue(c) << std::endl;
Chris@159 268 paint.setPen(QColor(qRed(c), qGreen(c), qBlue(c)));
Chris@159 269 paint.drawLine(5, 11 + y, cw - 5, 11 + y);
Chris@159 270 }
Chris@159 271 }
Chris@159 272
Chris@159 273 paint.setPen(Qt::black);
Chris@159 274
Chris@98 275 int count = v->height() / paint.fontMetrics().height();
Chris@160 276 int step = m_model->getHeight() / count;
Chris@98 277 if (step == 0) step = 1;
Chris@25 278
Chris@160 279 for (size_t i = 0; i < m_model->getHeight(); ++i) {
Chris@25 280
Chris@98 281 if ((i % step) != 0) continue;
Chris@98 282
Chris@44 283 int y0 = v->height() - (i * binHeight) - 1;
Chris@25 284
Chris@25 285 QString text = m_model->getBinName(i);
Chris@25 286 if (text == "") text = QString("[%1]").arg(i + 1);
Chris@25 287
Chris@159 288 paint.drawLine(cw, y0, w, y0);
Chris@25 289
Chris@98 290 int cy = y0 - (step * binHeight)/2;
Chris@25 291 int ty = cy + paint.fontMetrics().ascent()/2;
Chris@25 292
Chris@159 293 paint.drawText(cw + 5, ty, text);
Chris@25 294 }
Chris@25 295 }
Chris@25 296
Chris@0 297 void
Chris@44 298 Colour3DPlotLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 299 {
Chris@0 300 // Profiler profiler("Colour3DPlotLayer::paint");
Chris@125 301 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@125 302 std::cerr << "Colour3DPlotLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << std::endl;
Chris@125 303 #endif
Chris@0 304
Chris@0 305 int completion = 0;
Chris@0 306 if (!m_model || !m_model->isOK() || !m_model->isReady(&completion)) {
Chris@0 307 if (completion > 0) {
Chris@44 308 paint.fillRect(0, 10, v->width() * completion / 100,
Chris@0 309 10, QColor(120, 120, 120));
Chris@0 310 }
Chris@0 311 return;
Chris@0 312 }
Chris@0 313
Chris@0 314 size_t modelStart = m_model->getStartFrame();
Chris@0 315 size_t modelEnd = m_model->getEndFrame();
Chris@130 316 size_t modelResolution = m_model->getResolution();
Chris@0 317
Chris@130 318 size_t cacheWidth = (modelEnd - modelStart) / modelResolution + 1;
Chris@160 319 size_t cacheHeight = m_model->getHeight();
Chris@12 320
Chris@12 321 if (m_cache &&
Chris@12 322 (m_cache->width() != cacheWidth ||
Chris@12 323 m_cache->height() != cacheHeight)) {
Chris@12 324
Chris@12 325 delete m_cache;
Chris@12 326 m_cache = 0;
Chris@12 327 }
Chris@12 328
Chris@159 329 if (!m_cache) {
Chris@0 330
Chris@12 331 m_cache = new QImage(cacheWidth, cacheHeight, QImage::Format_Indexed8);
Chris@0 332
Chris@159 333 std::cerr << "Cache size " << cacheWidth << "x" << cacheHeight << std::endl;
Chris@159 334
Chris@0 335 m_cache->setNumColors(256);
Chris@160 336 DenseThreeDimensionalModel::Column values;
Chris@0 337
Chris@0 338 float min = m_model->getMinimumLevel();
Chris@0 339 float max = m_model->getMaximumLevel();
Chris@0 340
Chris@0 341 if (max == min) max = min + 1.0;
Chris@0 342
Chris@159 343 int zeroIndex = 0;
Chris@159 344 if (min < 0.f) {
Chris@159 345 if (m_colourScale == LinearScale) {
Chris@159 346 zeroIndex = int(((-min) * 256) / (max - min));
Chris@159 347 } else {
Chris@159 348 max = std::max(-min, max);
Chris@159 349 min = 0;
Chris@159 350 }
Chris@159 351 }
Chris@159 352 if (zeroIndex < 0) zeroIndex = 0;
Chris@159 353 if (zeroIndex > 255) zeroIndex = 255;
Chris@159 354
Chris@159 355 //!!! want this and spectrogram to share a colour mapping unit
Chris@159 356
Chris@159 357 for (int index = 0; index < 256; ++index) {
Chris@159 358 int effective = abs(((index - zeroIndex) * 255) /
Chris@159 359 std::max(255 - zeroIndex, zeroIndex));
Chris@159 360 int hue = 256 - effective;
Chris@159 361 if (zeroIndex > 0) {
Chris@159 362 if (index <= zeroIndex) hue = 255;
Chris@159 363 else hue = 0;
Chris@159 364 }
Chris@159 365 while (hue < 0) hue += 255;
Chris@159 366 while (hue > 255) hue -= 255;
Chris@159 367 int saturation = effective / 2 + 128;
Chris@159 368 if (saturation < 0) saturation = -saturation;
Chris@159 369 if (saturation > 255) saturation = 255;
Chris@159 370 int value = effective;
Chris@159 371 if (value < 0) value = -value;
Chris@159 372 if (value > 255) value = 255;
Chris@159 373 // std::cerr << "min: " << min << ", max: " << max << ", zi " << zeroIndex << ", index " << index << ": " << hue << ", " << saturation << ", " << value << std::endl;
Chris@159 374 QColor colour = QColor::fromHsv(hue, saturation, value);
Chris@159 375 // std::cerr << "rgb: " << colour.red() << "," << colour.green() << "," << colour.blue() << std::endl;
Chris@159 376 m_cache->setColor(index, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@0 377 }
Chris@0 378
Chris@159 379 m_cache->fill(zeroIndex);
Chris@0 380
Chris@130 381 for (size_t f = modelStart; f <= modelEnd; f += modelResolution) {
Chris@0 382
Chris@0 383 values.clear();
Chris@160 384 m_model->getColumn(f / modelResolution, values);
Chris@0 385
Chris@160 386 for (size_t y = 0; y < m_model->getHeight(); ++y) {
Chris@0 387
Chris@0 388 float value = min;
Chris@159 389 if (y < values.size()) {
Chris@159 390 value = values[y];
Chris@159 391 if (m_colourScale != LinearScale) {
Chris@159 392 value = fabs(value);
Chris@159 393 }
Chris@159 394 }
Chris@0 395
Chris@0 396 int pixel = int(((value - min) * 256) / (max - min));
Chris@98 397 if (pixel < 0) pixel = 0;
Chris@98 398 if (pixel > 255) pixel = 255;
Chris@24 399
Chris@130 400 m_cache->setPixel(f / modelResolution, y, pixel);
Chris@0 401 }
Chris@0 402 }
Chris@0 403 }
Chris@0 404
Chris@160 405 if (m_model->getHeight() >= v->height() ||
Chris@159 406 modelResolution < v->getZoomLevel() / 2) {
Chris@98 407 paintDense(v, paint, rect);
Chris@98 408 return;
Chris@98 409 }
Chris@98 410
Chris@0 411 int x0 = rect.left();
Chris@0 412 int x1 = rect.right() + 1;
Chris@0 413
Chris@44 414 int h = v->height();
Chris@0 415
Chris@0 416 // The cache is from the model's start frame to the model's end
Chris@0 417 // frame at the model's window increment frames per pixel. We
Chris@0 418 // want to draw from our start frame + x0 * zoomLevel to our start
Chris@0 419 // frame + x1 * zoomLevel at zoomLevel frames per pixel.
Chris@0 420
Chris@0 421 //!!! Strictly speaking we want quite different paint mechanisms
Chris@0 422 //for models that have more than one bin per pixel in either
Chris@0 423 //direction. This one is only really appropriate for models with
Chris@0 424 //far fewer bins in both directions.
Chris@0 425
Chris@159 426 float srRatio =
Chris@159 427 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@159 428 float(m_model->getSampleRate());
Chris@159 429
Chris@159 430 int sx0 = int((v->getFrameForX(x0) / srRatio - long(modelStart)) / long(modelResolution));
Chris@159 431 int sx1 = int((v->getFrameForX(x1) / srRatio - long(modelStart)) / long(modelResolution));
Chris@160 432 int sh = m_model->getHeight();
Chris@0 433
Chris@125 434 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@0 435 std::cerr << "Colour3DPlotLayer::paint: w " << w << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sw << ", sh " << sh << std::endl;
Chris@130 436 std::cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << std::endl;
Chris@125 437 #endif
Chris@0 438
Chris@25 439 QPoint illuminatePos;
Chris@44 440 bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos);
Chris@54 441 char labelbuf[10];
Chris@25 442
Chris@0 443 for (int sx = sx0 - 1; sx <= sx1; ++sx) {
Chris@0 444
Chris@130 445 int fx = sx * int(modelResolution);
Chris@0 446
Chris@130 447 if (fx + modelResolution < int(modelStart) ||
Chris@0 448 fx > int(modelEnd)) continue;
Chris@0 449
Chris@159 450 int rx0 = v->getXForFrame((fx + int(modelStart)) * srRatio);
Chris@159 451 int rx1 = v->getXForFrame((fx + int(modelStart) + int(modelResolution) + 1) * srRatio);
Chris@54 452
Chris@159 453 int rw = rx1 - rx0;
Chris@159 454 if (rw < 1) rw = 1;
Chris@54 455
Chris@159 456 bool showLabel = (rw > 10 &&
Chris@159 457 paint.fontMetrics().width("0.000000") < rw - 3 &&
Chris@54 458 paint.fontMetrics().height() < (h / sh));
Chris@98 459
Chris@0 460 for (int sy = 0; sy < sh; ++sy) {
Chris@0 461
Chris@0 462 int ry0 = h - (sy * h) / sh - 1;
Chris@0 463 QRgb pixel = qRgb(255, 255, 255);
Chris@0 464 if (sx >= 0 && sx < m_cache->width() &&
Chris@0 465 sy >= 0 && sy < m_cache->height()) {
Chris@0 466 pixel = m_cache->pixel(sx, sy);
Chris@0 467 }
Chris@0 468
Chris@159 469 QRect r(rx0, ry0 - h / sh - 1, rw, h / sh + 1);
Chris@159 470
Chris@159 471 if (rw == 1) {
Chris@159 472 paint.setPen(pixel);
Chris@159 473 paint.setBrush(Qt::NoBrush);
Chris@159 474 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
Chris@159 475 continue;
Chris@159 476 }
Chris@159 477
Chris@0 478 QColor pen(255, 255, 255, 80);
Chris@0 479 QColor brush(pixel);
Chris@159 480
Chris@159 481 if (rw > 3 && r.height() > 3) {
Chris@159 482 brush.setAlpha(160);
Chris@159 483 }
Chris@159 484
Chris@0 485 paint.setPen(Qt::NoPen);
Chris@0 486 paint.setBrush(brush);
Chris@0 487
Chris@25 488 if (illuminate) {
Chris@25 489 if (r.contains(illuminatePos)) {
Chris@25 490 paint.setPen(Qt::black);//!!!
Chris@25 491 }
Chris@25 492 }
Chris@76 493
Chris@125 494 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@159 495 std::cerr << "rect " << r.x() << "," << r.y() << " "
Chris@159 496 << r.width() << "x" << r.height() << std::endl;
Chris@125 497 #endif
Chris@25 498
Chris@25 499 paint.drawRect(r);
Chris@0 500
Chris@54 501 if (showLabel) {
Chris@54 502 if (sx >= 0 && sx < m_cache->width() &&
Chris@54 503 sy >= 0 && sy < m_cache->height()) {
Chris@160 504 float value = m_model->getValueAt(sx, sy);
Chris@54 505 sprintf(labelbuf, "%06f", value);
Chris@54 506 QString text(labelbuf);
Chris@54 507 paint.setPen(Qt::white);
Chris@54 508 paint.drawText(rx0 + 2,
Chris@54 509 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
Chris@54 510 text);
Chris@0 511 }
Chris@0 512 }
Chris@0 513 }
Chris@0 514 }
Chris@98 515 }
Chris@0 516
Chris@98 517 void
Chris@98 518 Colour3DPlotLayer::paintDense(View *v, QPainter &paint, QRect rect) const
Chris@98 519 {
Chris@98 520 long startFrame = v->getStartFrame();
Chris@98 521 int zoomLevel = v->getZoomLevel();
Chris@0 522
Chris@98 523 size_t modelStart = m_model->getStartFrame();
Chris@98 524 size_t modelEnd = m_model->getEndFrame();
Chris@130 525 size_t modelResolution = m_model->getResolution();
Chris@0 526
Chris@159 527 float srRatio =
Chris@159 528 float(v->getViewManager()->getMainModelSampleRate()) /
Chris@159 529 float(m_model->getSampleRate());
Chris@159 530
Chris@98 531 int x0 = rect.left();
Chris@98 532 int x1 = rect.right() + 1;
Chris@0 533
Chris@98 534 int w = x1 - x0;
Chris@98 535 int h = v->height();
Chris@160 536 int sh = m_model->getHeight();
Chris@98 537
Chris@98 538 QImage img(w, h, QImage::Format_RGB32);
Chris@98 539
Chris@98 540 for (int x = x0; x < x1; ++x) {
Chris@98 541
Chris@159 542 long xf = v->getFrameForX(x) / srRatio;
Chris@105 543 if (xf < 0) {
Chris@105 544 for (int y = 0; y < h; ++y) {
Chris@105 545 img.setPixel(x - x0, y, m_cache->color(0));
Chris@105 546 }
Chris@105 547 continue;
Chris@105 548 }
Chris@105 549
Chris@130 550 float sx0 = (float(xf) - modelStart) / modelResolution;
Chris@159 551 float sx1 = (float(v->getFrameForX(x+1) / srRatio) - modelStart) / modelResolution;
Chris@98 552
Chris@98 553 int sx0i = int(sx0 + 0.001);
Chris@98 554 int sx1i = int(sx1);
Chris@98 555
Chris@98 556 for (int y = 0; y < h; ++y) {
Chris@98 557
Chris@98 558 float sy0 = (float(h - y - 1) * sh) / h;
Chris@98 559 float sy1 = (float(h - y) * sh) / h;
Chris@98 560
Chris@98 561 int sy0i = int(sy0 + 0.001);
Chris@98 562 int sy1i = int(sy1);
Chris@98 563
Chris@98 564 float mag = 0.0, div = 0.0;
Chris@159 565 int max = 0;
Chris@98 566
Chris@98 567 for (int sx = sx0i; sx <= sx1i; ++sx) {
Chris@98 568
Chris@98 569 if (sx < 0 || sx >= m_cache->width()) continue;
Chris@98 570
Chris@98 571 for (int sy = sy0i; sy <= sy1i; ++sy) {
Chris@98 572
Chris@98 573 if (sy < 0 || sy >= m_cache->height()) continue;
Chris@98 574
Chris@98 575 float prop = 1.0;
Chris@98 576 if (sx == sx0i) prop *= (sx + 1) - sx0;
Chris@98 577 if (sx == sx1i) prop *= sx1 - sx;
Chris@98 578 if (sy == sy0i) prop *= (sy + 1) - sy0;
Chris@98 579 if (sy == sy1i) prop *= sy1 - sy;
Chris@98 580
Chris@98 581 mag += prop * m_cache->pixelIndex(sx, sy);
Chris@159 582 max = std::max(max, m_cache->pixelIndex(sx, sy));
Chris@98 583 div += prop;
Chris@98 584 }
Chris@98 585 }
Chris@98 586
Chris@98 587 if (div != 0) mag /= div;
Chris@98 588 if (mag < 0) mag = 0;
Chris@98 589 if (mag > 255) mag = 255;
Chris@159 590 if (max < 0) max = 0;
Chris@159 591 if (max > 255) max = 255;
Chris@98 592
Chris@98 593 img.setPixel(x - x0, y, m_cache->color(int(mag + 0.001)));
Chris@159 594 // img.setPixel(x - x0, y, m_cache->color(max));
Chris@98 595 }
Chris@0 596 }
Chris@0 597
Chris@98 598 paint.drawImage(x0, 0, img);
Chris@0 599 }
Chris@0 600
Chris@28 601 bool
Chris@44 602 Colour3DPlotLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 603 size_t &resolution,
Chris@28 604 SnapType snap) const
Chris@24 605 {
Chris@24 606 if (!m_model) {
Chris@44 607 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@24 608 }
Chris@24 609
Chris@130 610 resolution = m_model->getResolution();
Chris@28 611 int left = (frame / resolution) * resolution;
Chris@28 612 int right = left + resolution;
Chris@28 613
Chris@28 614 switch (snap) {
Chris@28 615 case SnapLeft: frame = left; break;
Chris@28 616 case SnapRight: frame = right; break;
Chris@28 617 case SnapNearest:
Chris@28 618 case SnapNeighbouring:
Chris@28 619 if (frame - left > right - frame) frame = right;
Chris@28 620 else frame = left;
Chris@28 621 break;
Chris@28 622 }
Chris@24 623
Chris@28 624 return true;
Chris@24 625 }
Chris@24 626
Chris@0 627 #ifdef INCLUDE_MOCFILES
Chris@0 628 #include "Colour3DPlotLayer.moc.cpp"
Chris@0 629 #endif
Chris@0 630
Chris@0 631