annotate layer/Colour3DPlotLayer.cpp @ 165:793df5f0c6cb

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