comparison layer/TimeValueLayer.cpp @ 197:6b023411087b

* Work on harmonising colour and scale ranges between types of layer * Add normalize options to colour 3d plot layer
author Chris Cannam
date Thu, 01 Feb 2007 14:31:28 +0000
parents 33929e0c3c6b
children c2ed5014d4ff
comparison
equal deleted inserted replaced
196:22c99c8aa1e0 197:6b023411087b
16 #include "TimeValueLayer.h" 16 #include "TimeValueLayer.h"
17 17
18 #include "data/model/Model.h" 18 #include "data/model/Model.h"
19 #include "base/RealTime.h" 19 #include "base/RealTime.h"
20 #include "base/Profiler.h" 20 #include "base/Profiler.h"
21 #include "base/LogRange.h"
21 #include "view/View.h" 22 #include "view/View.h"
22 23
23 #include "data/model/SparseTimeValueModel.h" 24 #include "data/model/SparseTimeValueModel.h"
24 25
25 #include "widgets/ItemEditDialog.h" 26 #include "widgets/ItemEditDialog.h"
26 #include "widgets/ListInputDialog.h" 27 #include "widgets/ListInputDialog.h"
27 28
28 #include "SpectrogramLayer.h" // for optional frequency alignment 29 #include "SpectrogramLayer.h" // for optional frequency alignment
30 #include "ColourMapper.h"
29 31
30 #include <QPainter> 32 #include <QPainter>
31 #include <QPainterPath> 33 #include <QPainterPath>
32 #include <QMouseEvent> 34 #include <QMouseEvent>
33 #include <QRegExp> 35 #include <QRegExp>
41 m_editing(false), 43 m_editing(false),
42 m_originalPoint(0, 0.0, tr("New Point")), 44 m_originalPoint(0, 0.0, tr("New Point")),
43 m_editingPoint(0, 0.0, tr("New Point")), 45 m_editingPoint(0, 0.0, tr("New Point")),
44 m_editingCommand(0), 46 m_editingCommand(0),
45 m_colour(Qt::darkGreen), 47 m_colour(Qt::darkGreen),
48 m_colourMap(0),
46 m_plotStyle(PlotConnectedPoints), 49 m_plotStyle(PlotConnectedPoints),
47 m_verticalScale(AutoAlignScale) 50 m_verticalScale(AutoAlignScale)
48 { 51 {
49 52
50 } 53 }
103 106
104 int deft = 0; 107 int deft = 0;
105 108
106 if (name == "Colour") { 109 if (name == "Colour") {
107 110
108 if (min) *min = 0; 111 if (m_plotStyle == PlotSegmentation) {
109 if (max) *max = 5; 112
110 113 if (min) *min = 0;
111 if (m_colour == Qt::black) deft = 0; 114 if (max) *max = ColourMapper::getColourMapCount() - 1;
112 else if (m_colour == Qt::darkRed) deft = 1; 115
113 else if (m_colour == Qt::darkBlue) deft = 2; 116 deft = m_colourMap;
114 else if (m_colour == Qt::darkGreen) deft = 3; 117
115 else if (m_colour == QColor(200, 50, 255)) deft = 4; 118 } else {
116 else if (m_colour == QColor(255, 150, 50)) deft = 5; 119
120 if (min) *min = 0;
121 if (max) *max = 5;
122
123 if (m_colour == Qt::black) deft = 0;
124 else if (m_colour == Qt::darkRed) deft = 1;
125 else if (m_colour == Qt::darkBlue) deft = 2;
126 else if (m_colour == Qt::darkGreen) deft = 3;
127 else if (m_colour == QColor(200, 50, 255)) deft = 4;
128 else if (m_colour == QColor(255, 150, 50)) deft = 5;
129 }
117 130
118 } else if (name == "Plot Type") { 131 } else if (name == "Plot Type") {
119 132
120 if (min) *min = 0; 133 if (min) *min = 0;
121 if (max) *max = 5; 134 if (max) *max = 5;
147 QString 160 QString
148 TimeValueLayer::getPropertyValueLabel(const PropertyName &name, 161 TimeValueLayer::getPropertyValueLabel(const PropertyName &name,
149 int value) const 162 int value) const
150 { 163 {
151 if (name == "Colour") { 164 if (name == "Colour") {
152 switch (value) { 165 if (m_plotStyle == PlotSegmentation) {
153 default: 166 return ColourMapper::getColourMapName(value);
154 case 0: return tr("Black"); 167 } else {
155 case 1: return tr("Red"); 168 switch (value) {
156 case 2: return tr("Blue"); 169 default:
157 case 3: return tr("Green"); 170 case 0: return tr("Black");
158 case 4: return tr("Purple"); 171 case 1: return tr("Red");
159 case 5: return tr("Orange"); 172 case 2: return tr("Blue");
160 } 173 case 3: return tr("Green");
174 case 4: return tr("Purple");
175 case 5: return tr("Orange");
176 }
177 }
161 } else if (name == "Plot Type") { 178 } else if (name == "Plot Type") {
162 switch (value) { 179 switch (value) {
163 default: 180 default:
164 case 0: return tr("Points"); 181 case 0: return tr("Points");
165 case 1: return tr("Stems"); 182 case 1: return tr("Stems");
182 199
183 void 200 void
184 TimeValueLayer::setProperty(const PropertyName &name, int value) 201 TimeValueLayer::setProperty(const PropertyName &name, int value)
185 { 202 {
186 if (name == "Colour") { 203 if (name == "Colour") {
187 switch (value) { 204 if (m_plotStyle == PlotSegmentation) {
188 default: 205 setFillColourMap(value);
189 case 0: setBaseColour(Qt::black); break; 206 } else {
190 case 1: setBaseColour(Qt::darkRed); break; 207 switch (value) {
191 case 2: setBaseColour(Qt::darkBlue); break; 208 default:
192 case 3: setBaseColour(Qt::darkGreen); break; 209 case 0: setBaseColour(Qt::black); break;
193 case 4: setBaseColour(QColor(200, 50, 255)); break; 210 case 1: setBaseColour(Qt::darkRed); break;
194 case 5: setBaseColour(QColor(255, 150, 50)); break; 211 case 2: setBaseColour(Qt::darkBlue); break;
195 } 212 case 3: setBaseColour(Qt::darkGreen); break;
213 case 4: setBaseColour(QColor(200, 50, 255)); break;
214 case 5: setBaseColour(QColor(255, 150, 50)); break;
215 }
216 }
196 } else if (name == "Plot Type") { 217 } else if (name == "Plot Type") {
197 setPlotStyle(PlotStyle(value)); 218 setPlotStyle(PlotStyle(value));
198 } else if (name == "Vertical Scale") { 219 } else if (name == "Vertical Scale") {
199 setVerticalScale(VerticalScale(value)); 220 setVerticalScale(VerticalScale(value));
200 } else if (name == "Scale Units") { 221 } else if (name == "Scale Units") {
213 m_colour = colour; 234 m_colour = colour;
214 emit layerParametersChanged(); 235 emit layerParametersChanged();
215 } 236 }
216 237
217 void 238 void
239 TimeValueLayer::setFillColourMap(int map)
240 {
241 if (m_colourMap == map) return;
242 m_colourMap = map;
243 emit layerParametersChanged();
244 }
245
246 void
218 TimeValueLayer::setPlotStyle(PlotStyle style) 247 TimeValueLayer::setPlotStyle(PlotStyle style)
219 { 248 {
220 if (m_plotStyle == style) return; 249 if (m_plotStyle == style) return;
250 bool colourTypeChanged = (style == PlotSegmentation ||
251 m_plotStyle == PlotSegmentation);
221 m_plotStyle = style; 252 m_plotStyle = style;
253 if (colourTypeChanged) {
254 emit layerParameterRangesChanged();
255 }
222 emit layerParametersChanged(); 256 emit layerParametersChanged();
223 } 257 }
224 258
225 void 259 void
226 TimeValueLayer::setVerticalScale(VerticalScale scale) 260 TimeValueLayer::setVerticalScale(VerticalScale scale)
434 468
435 if (!v->getValueExtents(m_model->getScaleUnits(), min, max, log)) { 469 if (!v->getValueExtents(m_model->getScaleUnits(), min, max, log)) {
436 min = m_model->getValueMinimum(); 470 min = m_model->getValueMinimum();
437 max = m_model->getValueMaximum(); 471 max = m_model->getValueMaximum();
438 } else if (log) { 472 } else if (log) {
439 min = (min < 0.0) ? -log10(-min) : (min == 0.0) ? 0.0 : log10(min); 473 LogRange::mapRange(min, max);
440 max = (max < 0.0) ? -log10(-max) : (max == 0.0) ? 0.0 : log10(max);
441 } 474 }
442 475
443 } else if (m_verticalScale == PlusMinusOneScale) { 476 } else if (m_verticalScale == PlusMinusOneScale) {
444 477
445 min = -1.0; 478 min = -1.0;
449 482
450 min = m_model->getValueMinimum(); 483 min = m_model->getValueMinimum();
451 max = m_model->getValueMaximum(); 484 max = m_model->getValueMaximum();
452 485
453 if (m_verticalScale == LogScale) { 486 if (m_verticalScale == LogScale) {
454 min = (min < 0.0) ? -log10(-min) : (min == 0.0) ? 0.0 : log10(min); 487 LogRange::mapRange(min, max);
455 max = (max < 0.0) ? -log10(-max) : (max == 0.0) ? 0.0 : log10(max);
456 log = true; 488 log = true;
457 } 489 }
458 } 490 }
459 491
460 if (max == min) max = min + 1.0; 492 if (max == min) max = min + 1.0;
471 503
472 // std::cerr << "getYForValue(" << val << "): min " << min << ", max " 504 // std::cerr << "getYForValue(" << val << "): min " << min << ", max "
473 // << max << ", log " << logarithmic << std::endl; 505 // << max << ", log " << logarithmic << std::endl;
474 506
475 if (logarithmic) { 507 if (logarithmic) {
476 val = (val < 0.0) ? -log10(-val) : (val == 0.0) ? 0.0 : log10(val); 508 val = LogRange::map(val);
477 } 509 }
478 510
479 return int(h - ((val - min) * h) / (max - min)); 511 return int(h - ((val - min) * h) / (max - min));
480 } 512 }
481 513
489 getScaleExtents(v, min, max, logarithmic); 521 getScaleExtents(v, min, max, logarithmic);
490 522
491 float val = min + (float(h - y) * float(max - min)) / h; 523 float val = min + (float(h - y) * float(max - min)) / h;
492 524
493 if (logarithmic) { 525 if (logarithmic) {
494 val = pow(10, val); 526 val = powf(10.f, val);
495 } 527 }
496 528
497 return val; 529 return val;
498 } 530 }
499 531
502 { 534 {
503 float min, max; 535 float min, max;
504 bool log; 536 bool log;
505 getScaleExtents(v, min, max, log); 537 getScaleExtents(v, min, max, log);
506 538
539 float logthresh = -80;
540
541 if (min > max) std::swap(min, max);
542 if (max == min) max = min + 1;
543
507 if (log) { 544 if (log) {
508 val = (val < 0.0) ? -log10(-val) : (val == 0.0) ? 0.0 : log10(val); 545 LogRange::mapRange(min, max);
509 } 546 val = LogRange::map(val);
510 547 }
511 int iv = ((val - min) / (max - min)) * 255.999; 548
512 549 // std::cerr << "TimeValueLayer::getColourForValue: min " << min << ", max "
513 QColor colour = QColor::fromHsv(256 - iv, iv / 2 + 128, iv); 550 // << max << ", log " << log << ", value " << val << std::endl;
514 return QColor(colour.red(), colour.green(), colour.blue(), 120); 551
552 QColor solid = ColourMapper(m_colourMap, min, max).map(val);
553 return QColor(solid.red(), solid.green(), solid.blue(), 120);
515 } 554 }
516 555
517 void 556 void
518 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const 557 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const
519 { 558 {
606 645
607 if (w < 1) w = 1; 646 if (w < 1) w = 1;
608 paint.setPen(m_colour); 647 paint.setPen(m_colour);
609 648
610 if (m_plotStyle == PlotSegmentation) { 649 if (m_plotStyle == PlotSegmentation) {
650 paint.setPen(Qt::black);
611 paint.setBrush(getColourForValue(v, p.value)); 651 paint.setBrush(getColourForValue(v, p.value));
612 labelY = v->height(); 652 labelY = v->height();
613 } else if (m_plotStyle == PlotLines || 653 } else if (m_plotStyle == PlotLines ||
614 m_plotStyle == PlotCurve) { 654 m_plotStyle == PlotCurve) {
615 paint.setBrush(Qt::NoBrush); 655 paint.setBrush(Qt::NoBrush);