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@130
|
190 int sx0 = modelResolution *
|
Chris@159
|
191 int((v->getFrameForX(x) / srRatio - long(modelStart)) / long(modelResolution));
|
Chris@130
|
192 int sx1 = sx0 + modelResolution;
|
Chris@25
|
193
|
Chris@44
|
194 float binHeight = float(v->height()) / m_model->getYBinCount();
|
Chris@44
|
195 int sy = (v->height() - y) / binHeight;
|
Chris@25
|
196
|
Chris@25
|
197 float value = m_model->getBinValue(sx0, sy);
|
Chris@159
|
198
|
Chris@159
|
199 // std::cerr << "bin value (" << sx0 << "," << sy << ") is " << value << std::endl;
|
Chris@25
|
200
|
Chris@25
|
201 QString binName = m_model->getBinName(sy);
|
Chris@25
|
202 if (binName == "") binName = QString("[%1]").arg(sy + 1);
|
Chris@25
|
203 else binName = QString("%1 [%2]").arg(binName).arg(sy + 1);
|
Chris@25
|
204
|
Chris@25
|
205 QString text = tr("Time:\t%1 - %2\nBin:\t%3\nValue:\t%4")
|
Chris@25
|
206 .arg(RealTime::frame2RealTime(sx0, m_model->getSampleRate())
|
Chris@25
|
207 .toText(true).c_str())
|
Chris@25
|
208 .arg(RealTime::frame2RealTime(sx1, m_model->getSampleRate())
|
Chris@25
|
209 .toText(true).c_str())
|
Chris@25
|
210 .arg(binName)
|
Chris@25
|
211 .arg(value);
|
Chris@25
|
212
|
Chris@25
|
213 return text;
|
Chris@25
|
214 }
|
Chris@25
|
215
|
Chris@25
|
216 int
|
Chris@159
|
217 Colour3DPlotLayer::getColourScaleWidth(QPainter &paint) const
|
Chris@159
|
218 {
|
Chris@159
|
219 int cw = 20;
|
Chris@159
|
220 return cw;
|
Chris@159
|
221 }
|
Chris@159
|
222
|
Chris@159
|
223 int
|
Chris@44
|
224 Colour3DPlotLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
|
Chris@25
|
225 {
|
Chris@25
|
226 if (!m_model) return 0;
|
Chris@25
|
227
|
Chris@98
|
228 QString sampleText = QString("[%1]").arg(m_model->getYBinCount());
|
Chris@25
|
229 int tw = paint.fontMetrics().width(sampleText);
|
Chris@98
|
230 bool another = false;
|
Chris@25
|
231
|
Chris@25
|
232 for (size_t i = 0; i < m_model->getYBinCount(); ++i) {
|
Chris@25
|
233 if (m_model->getBinName(i).length() > sampleText.length()) {
|
Chris@25
|
234 sampleText = m_model->getBinName(i);
|
Chris@98
|
235 another = true;
|
Chris@25
|
236 }
|
Chris@25
|
237 }
|
Chris@98
|
238 if (another) {
|
Chris@25
|
239 tw = std::max(tw, paint.fontMetrics().width(sampleText));
|
Chris@25
|
240 }
|
Chris@25
|
241
|
Chris@159
|
242 return tw + 13 + getColourScaleWidth(paint);
|
Chris@25
|
243 }
|
Chris@25
|
244
|
Chris@25
|
245 void
|
Chris@44
|
246 Colour3DPlotLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
|
Chris@25
|
247 {
|
Chris@25
|
248 if (!m_model) return;
|
Chris@25
|
249
|
Chris@25
|
250 int h = rect.height(), w = rect.width();
|
Chris@44
|
251 float binHeight = float(v->height()) / m_model->getYBinCount();
|
Chris@25
|
252
|
Chris@159
|
253 int cw = getColourScaleWidth(paint);
|
Chris@159
|
254
|
Chris@159
|
255 int ch = h - 20;
|
Chris@159
|
256 if (ch > 20 && m_cache) {
|
Chris@159
|
257
|
Chris@159
|
258 paint.setPen(Qt::black);
|
Chris@159
|
259 paint.drawRect(4, 10, cw - 8, ch - 19);
|
Chris@159
|
260
|
Chris@159
|
261 for (int y = 0; y < ch - 20; ++y) {
|
Chris@159
|
262 QRgb c = m_cache->color(((ch - 20 - y) * 255) / (ch - 20));
|
Chris@159
|
263 // std::cerr << "y = " << y << ": rgb " << qRed(c) << "," << qGreen(c) << "," << qBlue(c) << std::endl;
|
Chris@159
|
264 paint.setPen(QColor(qRed(c), qGreen(c), qBlue(c)));
|
Chris@159
|
265 paint.drawLine(5, 11 + y, cw - 5, 11 + y);
|
Chris@159
|
266 }
|
Chris@159
|
267 }
|
Chris@159
|
268
|
Chris@159
|
269 paint.setPen(Qt::black);
|
Chris@159
|
270
|
Chris@98
|
271 int count = v->height() / paint.fontMetrics().height();
|
Chris@98
|
272 int step = m_model->getYBinCount() / count;
|
Chris@98
|
273 if (step == 0) step = 1;
|
Chris@25
|
274
|
Chris@25
|
275 for (size_t i = 0; i < m_model->getYBinCount(); ++i) {
|
Chris@25
|
276
|
Chris@98
|
277 if ((i % step) != 0) continue;
|
Chris@98
|
278
|
Chris@44
|
279 int y0 = v->height() - (i * binHeight) - 1;
|
Chris@25
|
280
|
Chris@25
|
281 QString text = m_model->getBinName(i);
|
Chris@25
|
282 if (text == "") text = QString("[%1]").arg(i + 1);
|
Chris@25
|
283
|
Chris@159
|
284 paint.drawLine(cw, y0, w, y0);
|
Chris@25
|
285
|
Chris@98
|
286 int cy = y0 - (step * binHeight)/2;
|
Chris@25
|
287 int ty = cy + paint.fontMetrics().ascent()/2;
|
Chris@25
|
288
|
Chris@159
|
289 paint.drawText(cw + 5, ty, text);
|
Chris@25
|
290 }
|
Chris@25
|
291 }
|
Chris@25
|
292
|
Chris@0
|
293 void
|
Chris@44
|
294 Colour3DPlotLayer::paint(View *v, QPainter &paint, QRect rect) const
|
Chris@0
|
295 {
|
Chris@0
|
296 // Profiler profiler("Colour3DPlotLayer::paint");
|
Chris@125
|
297 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
|
Chris@125
|
298 std::cerr << "Colour3DPlotLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << std::endl;
|
Chris@125
|
299 #endif
|
Chris@0
|
300
|
Chris@0
|
301 int completion = 0;
|
Chris@0
|
302 if (!m_model || !m_model->isOK() || !m_model->isReady(&completion)) {
|
Chris@0
|
303 if (completion > 0) {
|
Chris@44
|
304 paint.fillRect(0, 10, v->width() * completion / 100,
|
Chris@0
|
305 10, QColor(120, 120, 120));
|
Chris@0
|
306 }
|
Chris@0
|
307 return;
|
Chris@0
|
308 }
|
Chris@0
|
309
|
Chris@0
|
310 size_t modelStart = m_model->getStartFrame();
|
Chris@0
|
311 size_t modelEnd = m_model->getEndFrame();
|
Chris@130
|
312 size_t modelResolution = m_model->getResolution();
|
Chris@0
|
313
|
Chris@130
|
314 size_t cacheWidth = (modelEnd - modelStart) / modelResolution + 1;
|
Chris@12
|
315 size_t cacheHeight = m_model->getYBinCount();
|
Chris@12
|
316
|
Chris@12
|
317 if (m_cache &&
|
Chris@12
|
318 (m_cache->width() != cacheWidth ||
|
Chris@12
|
319 m_cache->height() != cacheHeight)) {
|
Chris@12
|
320
|
Chris@12
|
321 delete m_cache;
|
Chris@12
|
322 m_cache = 0;
|
Chris@12
|
323 }
|
Chris@12
|
324
|
Chris@159
|
325 if (!m_cache) {
|
Chris@0
|
326
|
Chris@12
|
327 m_cache = new QImage(cacheWidth, cacheHeight, QImage::Format_Indexed8);
|
Chris@0
|
328
|
Chris@159
|
329 std::cerr << "Cache size " << cacheWidth << "x" << cacheHeight << std::endl;
|
Chris@159
|
330
|
Chris@0
|
331 m_cache->setNumColors(256);
|
Chris@0
|
332 DenseThreeDimensionalModel::BinValueSet values;
|
Chris@0
|
333
|
Chris@0
|
334 float min = m_model->getMinimumLevel();
|
Chris@0
|
335 float max = m_model->getMaximumLevel();
|
Chris@0
|
336
|
Chris@0
|
337 if (max == min) max = min + 1.0;
|
Chris@0
|
338
|
Chris@159
|
339 int zeroIndex = 0;
|
Chris@159
|
340 if (min < 0.f) {
|
Chris@159
|
341 if (m_colourScale == LinearScale) {
|
Chris@159
|
342 zeroIndex = int(((-min) * 256) / (max - min));
|
Chris@159
|
343 } else {
|
Chris@159
|
344 max = std::max(-min, max);
|
Chris@159
|
345 min = 0;
|
Chris@159
|
346 }
|
Chris@159
|
347 }
|
Chris@159
|
348 if (zeroIndex < 0) zeroIndex = 0;
|
Chris@159
|
349 if (zeroIndex > 255) zeroIndex = 255;
|
Chris@159
|
350
|
Chris@159
|
351 //!!! want this and spectrogram to share a colour mapping unit
|
Chris@159
|
352
|
Chris@159
|
353 for (int index = 0; index < 256; ++index) {
|
Chris@159
|
354 int effective = abs(((index - zeroIndex) * 255) /
|
Chris@159
|
355 std::max(255 - zeroIndex, zeroIndex));
|
Chris@159
|
356 int hue = 256 - effective;
|
Chris@159
|
357 if (zeroIndex > 0) {
|
Chris@159
|
358 if (index <= zeroIndex) hue = 255;
|
Chris@159
|
359 else hue = 0;
|
Chris@159
|
360 }
|
Chris@159
|
361 while (hue < 0) hue += 255;
|
Chris@159
|
362 while (hue > 255) hue -= 255;
|
Chris@159
|
363 int saturation = effective / 2 + 128;
|
Chris@159
|
364 if (saturation < 0) saturation = -saturation;
|
Chris@159
|
365 if (saturation > 255) saturation = 255;
|
Chris@159
|
366 int value = effective;
|
Chris@159
|
367 if (value < 0) value = -value;
|
Chris@159
|
368 if (value > 255) value = 255;
|
Chris@159
|
369 // std::cerr << "min: " << min << ", max: " << max << ", zi " << zeroIndex << ", index " << index << ": " << hue << ", " << saturation << ", " << value << std::endl;
|
Chris@159
|
370 QColor colour = QColor::fromHsv(hue, saturation, value);
|
Chris@159
|
371 // std::cerr << "rgb: " << colour.red() << "," << colour.green() << "," << colour.blue() << std::endl;
|
Chris@159
|
372 m_cache->setColor(index, qRgb(colour.red(), colour.green(), colour.blue()));
|
Chris@0
|
373 }
|
Chris@0
|
374
|
Chris@159
|
375 m_cache->fill(zeroIndex);
|
Chris@0
|
376
|
Chris@130
|
377 for (size_t f = modelStart; f <= modelEnd; f += modelResolution) {
|
Chris@0
|
378
|
Chris@0
|
379 values.clear();
|
Chris@0
|
380 m_model->getBinValues(f, values);
|
Chris@0
|
381
|
Chris@0
|
382 for (size_t y = 0; y < m_model->getYBinCount(); ++y) {
|
Chris@0
|
383
|
Chris@0
|
384 float value = min;
|
Chris@159
|
385 if (y < values.size()) {
|
Chris@159
|
386 value = values[y];
|
Chris@159
|
387 if (m_colourScale != LinearScale) {
|
Chris@159
|
388 value = fabs(value);
|
Chris@159
|
389 }
|
Chris@159
|
390 }
|
Chris@0
|
391
|
Chris@0
|
392 int pixel = int(((value - min) * 256) / (max - min));
|
Chris@98
|
393 if (pixel < 0) pixel = 0;
|
Chris@98
|
394 if (pixel > 255) pixel = 255;
|
Chris@24
|
395
|
Chris@130
|
396 m_cache->setPixel(f / modelResolution, y, pixel);
|
Chris@0
|
397 }
|
Chris@0
|
398 }
|
Chris@0
|
399 }
|
Chris@0
|
400
|
Chris@159
|
401 if (m_model->getYBinCount() >= v->height() ||
|
Chris@159
|
402 modelResolution < v->getZoomLevel() / 2) {
|
Chris@98
|
403 paintDense(v, paint, rect);
|
Chris@98
|
404 return;
|
Chris@98
|
405 }
|
Chris@98
|
406
|
Chris@0
|
407 int x0 = rect.left();
|
Chris@0
|
408 int x1 = rect.right() + 1;
|
Chris@0
|
409
|
Chris@44
|
410 int h = v->height();
|
Chris@0
|
411
|
Chris@0
|
412 // The cache is from the model's start frame to the model's end
|
Chris@0
|
413 // frame at the model's window increment frames per pixel. We
|
Chris@0
|
414 // want to draw from our start frame + x0 * zoomLevel to our start
|
Chris@0
|
415 // frame + x1 * zoomLevel at zoomLevel frames per pixel.
|
Chris@0
|
416
|
Chris@0
|
417 //!!! Strictly speaking we want quite different paint mechanisms
|
Chris@0
|
418 //for models that have more than one bin per pixel in either
|
Chris@0
|
419 //direction. This one is only really appropriate for models with
|
Chris@0
|
420 //far fewer bins in both directions.
|
Chris@0
|
421
|
Chris@159
|
422 float srRatio =
|
Chris@159
|
423 float(v->getViewManager()->getMainModelSampleRate()) /
|
Chris@159
|
424 float(m_model->getSampleRate());
|
Chris@159
|
425
|
Chris@159
|
426 int sx0 = int((v->getFrameForX(x0) / srRatio - long(modelStart)) / long(modelResolution));
|
Chris@159
|
427 int sx1 = int((v->getFrameForX(x1) / srRatio - long(modelStart)) / long(modelResolution));
|
Chris@0
|
428 int sh = m_model->getYBinCount();
|
Chris@0
|
429
|
Chris@125
|
430 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
|
Chris@0
|
431 std::cerr << "Colour3DPlotLayer::paint: w " << w << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sw << ", sh " << sh << std::endl;
|
Chris@130
|
432 std::cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", resolution " << m_model->getResolution() << std::endl;
|
Chris@125
|
433 #endif
|
Chris@0
|
434
|
Chris@25
|
435 QPoint illuminatePos;
|
Chris@44
|
436 bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos);
|
Chris@54
|
437 char labelbuf[10];
|
Chris@25
|
438
|
Chris@0
|
439 for (int sx = sx0 - 1; sx <= sx1; ++sx) {
|
Chris@0
|
440
|
Chris@130
|
441 int fx = sx * int(modelResolution);
|
Chris@0
|
442
|
Chris@130
|
443 if (fx + modelResolution < int(modelStart) ||
|
Chris@0
|
444 fx > int(modelEnd)) continue;
|
Chris@0
|
445
|
Chris@159
|
446 int rx0 = v->getXForFrame((fx + int(modelStart)) * srRatio);
|
Chris@159
|
447 int rx1 = v->getXForFrame((fx + int(modelStart) + int(modelResolution) + 1) * srRatio);
|
Chris@54
|
448
|
Chris@159
|
449 int rw = rx1 - rx0;
|
Chris@159
|
450 if (rw < 1) rw = 1;
|
Chris@54
|
451
|
Chris@159
|
452 bool showLabel = (rw > 10 &&
|
Chris@159
|
453 paint.fontMetrics().width("0.000000") < rw - 3 &&
|
Chris@54
|
454 paint.fontMetrics().height() < (h / sh));
|
Chris@98
|
455
|
Chris@0
|
456 for (int sy = 0; sy < sh; ++sy) {
|
Chris@0
|
457
|
Chris@0
|
458 int ry0 = h - (sy * h) / sh - 1;
|
Chris@0
|
459 QRgb pixel = qRgb(255, 255, 255);
|
Chris@0
|
460 if (sx >= 0 && sx < m_cache->width() &&
|
Chris@0
|
461 sy >= 0 && sy < m_cache->height()) {
|
Chris@0
|
462 pixel = m_cache->pixel(sx, sy);
|
Chris@0
|
463 }
|
Chris@0
|
464
|
Chris@159
|
465 QRect r(rx0, ry0 - h / sh - 1, rw, h / sh + 1);
|
Chris@159
|
466
|
Chris@159
|
467 if (rw == 1) {
|
Chris@159
|
468 paint.setPen(pixel);
|
Chris@159
|
469 paint.setBrush(Qt::NoBrush);
|
Chris@159
|
470 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
|
Chris@159
|
471 continue;
|
Chris@159
|
472 }
|
Chris@159
|
473
|
Chris@0
|
474 QColor pen(255, 255, 255, 80);
|
Chris@0
|
475 QColor brush(pixel);
|
Chris@159
|
476
|
Chris@159
|
477 if (rw > 3 && r.height() > 3) {
|
Chris@159
|
478 brush.setAlpha(160);
|
Chris@159
|
479 }
|
Chris@159
|
480
|
Chris@0
|
481 paint.setPen(Qt::NoPen);
|
Chris@0
|
482 paint.setBrush(brush);
|
Chris@0
|
483
|
Chris@25
|
484 if (illuminate) {
|
Chris@25
|
485 if (r.contains(illuminatePos)) {
|
Chris@25
|
486 paint.setPen(Qt::black);//!!!
|
Chris@25
|
487 }
|
Chris@25
|
488 }
|
Chris@76
|
489
|
Chris@125
|
490 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
|
Chris@159
|
491 std::cerr << "rect " << r.x() << "," << r.y() << " "
|
Chris@159
|
492 << r.width() << "x" << r.height() << std::endl;
|
Chris@125
|
493 #endif
|
Chris@25
|
494
|
Chris@25
|
495 paint.drawRect(r);
|
Chris@0
|
496
|
Chris@54
|
497 if (showLabel) {
|
Chris@54
|
498 if (sx >= 0 && sx < m_cache->width() &&
|
Chris@54
|
499 sy >= 0 && sy < m_cache->height()) {
|
Chris@54
|
500 float value = m_model->getBinValue(fx, sy);
|
Chris@54
|
501 sprintf(labelbuf, "%06f", value);
|
Chris@54
|
502 QString text(labelbuf);
|
Chris@54
|
503 paint.setPen(Qt::white);
|
Chris@54
|
504 paint.drawText(rx0 + 2,
|
Chris@54
|
505 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
|
Chris@54
|
506 text);
|
Chris@0
|
507 }
|
Chris@0
|
508 }
|
Chris@0
|
509 }
|
Chris@0
|
510 }
|
Chris@98
|
511 }
|
Chris@0
|
512
|
Chris@98
|
513 void
|
Chris@98
|
514 Colour3DPlotLayer::paintDense(View *v, QPainter &paint, QRect rect) const
|
Chris@98
|
515 {
|
Chris@98
|
516 long startFrame = v->getStartFrame();
|
Chris@98
|
517 int zoomLevel = v->getZoomLevel();
|
Chris@0
|
518
|
Chris@98
|
519 size_t modelStart = m_model->getStartFrame();
|
Chris@98
|
520 size_t modelEnd = m_model->getEndFrame();
|
Chris@130
|
521 size_t modelResolution = m_model->getResolution();
|
Chris@0
|
522
|
Chris@159
|
523 float srRatio =
|
Chris@159
|
524 float(v->getViewManager()->getMainModelSampleRate()) /
|
Chris@159
|
525 float(m_model->getSampleRate());
|
Chris@159
|
526
|
Chris@98
|
527 int x0 = rect.left();
|
Chris@98
|
528 int x1 = rect.right() + 1;
|
Chris@0
|
529
|
Chris@98
|
530 int w = x1 - x0;
|
Chris@98
|
531 int h = v->height();
|
Chris@98
|
532 int sh = m_model->getYBinCount();
|
Chris@98
|
533
|
Chris@98
|
534 QImage img(w, h, QImage::Format_RGB32);
|
Chris@98
|
535
|
Chris@98
|
536 for (int x = x0; x < x1; ++x) {
|
Chris@98
|
537
|
Chris@159
|
538 long xf = v->getFrameForX(x) / srRatio;
|
Chris@105
|
539 if (xf < 0) {
|
Chris@105
|
540 for (int y = 0; y < h; ++y) {
|
Chris@105
|
541 img.setPixel(x - x0, y, m_cache->color(0));
|
Chris@105
|
542 }
|
Chris@105
|
543 continue;
|
Chris@105
|
544 }
|
Chris@105
|
545
|
Chris@130
|
546 float sx0 = (float(xf) - modelStart) / modelResolution;
|
Chris@159
|
547 float sx1 = (float(v->getFrameForX(x+1) / srRatio) - modelStart) / modelResolution;
|
Chris@98
|
548
|
Chris@98
|
549 int sx0i = int(sx0 + 0.001);
|
Chris@98
|
550 int sx1i = int(sx1);
|
Chris@98
|
551
|
Chris@98
|
552 for (int y = 0; y < h; ++y) {
|
Chris@98
|
553
|
Chris@98
|
554 float sy0 = (float(h - y - 1) * sh) / h;
|
Chris@98
|
555 float sy1 = (float(h - y) * sh) / h;
|
Chris@98
|
556
|
Chris@98
|
557 int sy0i = int(sy0 + 0.001);
|
Chris@98
|
558 int sy1i = int(sy1);
|
Chris@98
|
559
|
Chris@98
|
560 float mag = 0.0, div = 0.0;
|
Chris@159
|
561 int max = 0;
|
Chris@98
|
562
|
Chris@98
|
563 for (int sx = sx0i; sx <= sx1i; ++sx) {
|
Chris@98
|
564
|
Chris@98
|
565 if (sx < 0 || sx >= m_cache->width()) continue;
|
Chris@98
|
566
|
Chris@98
|
567 for (int sy = sy0i; sy <= sy1i; ++sy) {
|
Chris@98
|
568
|
Chris@98
|
569 if (sy < 0 || sy >= m_cache->height()) continue;
|
Chris@98
|
570
|
Chris@98
|
571 float prop = 1.0;
|
Chris@98
|
572 if (sx == sx0i) prop *= (sx + 1) - sx0;
|
Chris@98
|
573 if (sx == sx1i) prop *= sx1 - sx;
|
Chris@98
|
574 if (sy == sy0i) prop *= (sy + 1) - sy0;
|
Chris@98
|
575 if (sy == sy1i) prop *= sy1 - sy;
|
Chris@98
|
576
|
Chris@98
|
577 mag += prop * m_cache->pixelIndex(sx, sy);
|
Chris@159
|
578 max = std::max(max, m_cache->pixelIndex(sx, sy));
|
Chris@98
|
579 div += prop;
|
Chris@98
|
580 }
|
Chris@98
|
581 }
|
Chris@98
|
582
|
Chris@98
|
583 if (div != 0) mag /= div;
|
Chris@98
|
584 if (mag < 0) mag = 0;
|
Chris@98
|
585 if (mag > 255) mag = 255;
|
Chris@159
|
586 if (max < 0) max = 0;
|
Chris@159
|
587 if (max > 255) max = 255;
|
Chris@98
|
588
|
Chris@98
|
589 img.setPixel(x - x0, y, m_cache->color(int(mag + 0.001)));
|
Chris@159
|
590 // img.setPixel(x - x0, y, m_cache->color(max));
|
Chris@98
|
591 }
|
Chris@0
|
592 }
|
Chris@0
|
593
|
Chris@98
|
594 paint.drawImage(x0, 0, img);
|
Chris@0
|
595 }
|
Chris@0
|
596
|
Chris@28
|
597 bool
|
Chris@44
|
598 Colour3DPlotLayer::snapToFeatureFrame(View *v, int &frame,
|
Chris@28
|
599 size_t &resolution,
|
Chris@28
|
600 SnapType snap) const
|
Chris@24
|
601 {
|
Chris@24
|
602 if (!m_model) {
|
Chris@44
|
603 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
|
Chris@24
|
604 }
|
Chris@24
|
605
|
Chris@130
|
606 resolution = m_model->getResolution();
|
Chris@28
|
607 int left = (frame / resolution) * resolution;
|
Chris@28
|
608 int right = left + resolution;
|
Chris@28
|
609
|
Chris@28
|
610 switch (snap) {
|
Chris@28
|
611 case SnapLeft: frame = left; break;
|
Chris@28
|
612 case SnapRight: frame = right; break;
|
Chris@28
|
613 case SnapNearest:
|
Chris@28
|
614 case SnapNeighbouring:
|
Chris@28
|
615 if (frame - left > right - frame) frame = right;
|
Chris@28
|
616 else frame = left;
|
Chris@28
|
617 break;
|
Chris@28
|
618 }
|
Chris@24
|
619
|
Chris@28
|
620 return true;
|
Chris@24
|
621 }
|
Chris@24
|
622
|
Chris@0
|
623 #ifdef INCLUDE_MOCFILES
|
Chris@0
|
624 #include "Colour3DPlotLayer.moc.cpp"
|
Chris@0
|
625 #endif
|
Chris@0
|
626
|
Chris@0
|
627
|