comparison layer/TimeValueLayer.cpp @ 287:cd2492c5fe45

* Add SingleColourLayer to manage colours for layers that have a single predominant colour (i.e. most of them).
author Chris Cannam
date Thu, 12 Jul 2007 16:14:59 +0000
parents 7554ae119882
children e27f546f83ad
comparison
equal deleted inserted replaced
286:7554ae119882 287:cd2492c5fe45
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 "base/LogRange.h"
22 #include "base/ColourDatabase.h"
22 #include "view/View.h" 23 #include "view/View.h"
23 24
24 #include "data/model/SparseTimeValueModel.h" 25 #include "data/model/SparseTimeValueModel.h"
25 26
26 #include "widgets/ItemEditDialog.h" 27 #include "widgets/ItemEditDialog.h"
36 37
37 #include <iostream> 38 #include <iostream>
38 #include <cmath> 39 #include <cmath>
39 40
40 TimeValueLayer::TimeValueLayer() : 41 TimeValueLayer::TimeValueLayer() :
41 Layer(), 42 SingleColourLayer(),
42 m_model(0), 43 m_model(0),
43 m_editing(false), 44 m_editing(false),
44 m_originalPoint(0, 0.0, tr("New Point")), 45 m_originalPoint(0, 0.0, tr("New Point")),
45 m_editingPoint(0, 0.0, tr("New Point")), 46 m_editingPoint(0, 0.0, tr("New Point")),
46 m_editingCommand(0), 47 m_editingCommand(0),
47 m_colour(Qt::darkGreen),
48 m_colourMap(0), 48 m_colourMap(0),
49 m_plotStyle(PlotConnectedPoints), 49 m_plotStyle(PlotConnectedPoints),
50 m_verticalScale(AutoAlignScale) 50 m_verticalScale(AutoAlignScale)
51 { 51 {
52 52
71 } 71 }
72 72
73 Layer::PropertyList 73 Layer::PropertyList
74 TimeValueLayer::getProperties() const 74 TimeValueLayer::getProperties() const
75 { 75 {
76 PropertyList list; 76 PropertyList list = SingleColourLayer::getProperties();
77 list.push_back("Colour");
78 list.push_back("Plot Type"); 77 list.push_back("Plot Type");
79 list.push_back("Vertical Scale"); 78 list.push_back("Vertical Scale");
80 list.push_back("Scale Units"); 79 list.push_back("Scale Units");
81 return list; 80 return list;
82 } 81 }
83 82
84 QString 83 QString
85 TimeValueLayer::getPropertyLabel(const PropertyName &name) const 84 TimeValueLayer::getPropertyLabel(const PropertyName &name) const
86 { 85 {
87 if (name == "Colour") return tr("Colour");
88 if (name == "Plot Type") return tr("Plot Type"); 86 if (name == "Plot Type") return tr("Plot Type");
89 if (name == "Vertical Scale") return tr("Vertical Scale"); 87 if (name == "Vertical Scale") return tr("Vertical Scale");
90 if (name == "Scale Units") return tr("Scale Units"); 88 if (name == "Scale Units") return tr("Scale Units");
91 return ""; 89 return SingleColourLayer::getPropertyLabel(name);
92 } 90 }
93 91
94 Layer::PropertyType 92 Layer::PropertyType
95 TimeValueLayer::getPropertyType(const PropertyName &name) const 93 TimeValueLayer::getPropertyType(const PropertyName &name) const
96 { 94 {
95 if (name == "Plot Type") return ValueProperty;
96 if (name == "Vertical Scale") return ValueProperty;
97 if (name == "Scale Units") return UnitsProperty; 97 if (name == "Scale Units") return UnitsProperty;
98 else return ValueProperty; 98 if (name == "Colour" && m_plotStyle == PlotSegmentation) return ValueProperty;
99 return SingleColourLayer::getPropertyType(name);
99 } 100 }
100 101
101 QString 102 QString
102 TimeValueLayer::getPropertyGroupName(const PropertyName &name) const 103 TimeValueLayer::getPropertyGroupName(const PropertyName &name) const
103 { 104 {
104 if (name == "Vertical Scale" || name == "Scale Units") { 105 if (name == "Vertical Scale" || name == "Scale Units") {
105 return tr("Scale"); 106 return tr("Scale");
106 } 107 }
107 return QString(); 108 return SingleColourLayer::getPropertyGroupName(name);
108 } 109 }
109 110
110 int 111 int
111 TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name, 112 TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name,
112 int *min, int *max, int *deflt) const 113 int *min, int *max, int *deflt) const
113 { 114 {
114 //!!! factor this colour handling stuff out into a colour manager class
115
116 int val = 0; 115 int val = 0;
117 116
118 if (name == "Colour") { 117 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
119
120 if (m_plotStyle == PlotSegmentation) {
121 118
122 if (min) *min = 0; 119 if (min) *min = 0;
123 if (max) *max = ColourMapper::getColourMapCount() - 1; 120 if (max) *max = ColourMapper::getColourMapCount() - 1;
124 if (deflt) *deflt = 0; 121 if (deflt) *deflt = 0;
125
126 val = m_colourMap;
127 122
128 } else { 123 val = m_colourMap;
129
130 if (min) *min = 0;
131 if (max) *max = 5;
132 if (deflt) *deflt = 0;
133
134 if (m_colour == Qt::black) val = 0;
135 else if (m_colour == Qt::darkRed) val = 1;
136 else if (m_colour == Qt::darkBlue) val = 2;
137 else if (m_colour == Qt::darkGreen) val = 3;
138 else if (m_colour == QColor(200, 50, 255)) val = 4;
139 else if (m_colour == QColor(255, 150, 50)) val = 5;
140 }
141 124
142 } else if (name == "Plot Type") { 125 } else if (name == "Plot Type") {
143 126
144 if (min) *min = 0; 127 if (min) *min = 0;
145 if (max) *max = 5; 128 if (max) *max = 5;
163 (m_model->getScaleUnits()); 146 (m_model->getScaleUnits());
164 } 147 }
165 148
166 } else { 149 } else {
167 150
168 val = Layer::getPropertyRangeAndValue(name, min, max, deflt); 151 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
169 } 152 }
170 153
171 return val; 154 return val;
172 } 155 }
173 156
174 QString 157 QString
175 TimeValueLayer::getPropertyValueLabel(const PropertyName &name, 158 TimeValueLayer::getPropertyValueLabel(const PropertyName &name,
176 int value) const 159 int value) const
177 { 160 {
178 if (name == "Colour") { 161 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
179 if (m_plotStyle == PlotSegmentation) { 162 return ColourMapper::getColourMapName(value);
180 return ColourMapper::getColourMapName(value);
181 } else {
182 switch (value) {
183 default:
184 case 0: return tr("Black");
185 case 1: return tr("Red");
186 case 2: return tr("Blue");
187 case 3: return tr("Green");
188 case 4: return tr("Purple");
189 case 5: return tr("Orange");
190 }
191 }
192 } else if (name == "Plot Type") { 163 } else if (name == "Plot Type") {
193 switch (value) { 164 switch (value) {
194 default: 165 default:
195 case 0: return tr("Points"); 166 case 0: return tr("Points");
196 case 1: return tr("Stems"); 167 case 1: return tr("Stems");
206 case 1: return tr("Linear"); 177 case 1: return tr("Linear");
207 case 2: return tr("Log"); 178 case 2: return tr("Log");
208 case 3: return tr("+/-1"); 179 case 3: return tr("+/-1");
209 } 180 }
210 } 181 }
211 return tr("<unknown>"); 182 return SingleColourLayer::getPropertyValueLabel(name, value);
212 } 183 }
213 184
214 void 185 void
215 TimeValueLayer::setProperty(const PropertyName &name, int value) 186 TimeValueLayer::setProperty(const PropertyName &name, int value)
216 { 187 {
217 if (name == "Colour") { 188 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
218 if (m_plotStyle == PlotSegmentation) { 189 setFillColourMap(value);
219 setFillColourMap(value);
220 } else {
221 switch (value) {
222 default:
223 case 0: setBaseColour(Qt::black); break;
224 case 1: setBaseColour(Qt::darkRed); break;
225 case 2: setBaseColour(Qt::darkBlue); break;
226 case 3: setBaseColour(Qt::darkGreen); break;
227 case 4: setBaseColour(QColor(200, 50, 255)); break;
228 case 5: setBaseColour(QColor(255, 150, 50)); break;
229 }
230 }
231 } else if (name == "Plot Type") { 190 } else if (name == "Plot Type") {
232 setPlotStyle(PlotStyle(value)); 191 setPlotStyle(PlotStyle(value));
233 } else if (name == "Vertical Scale") { 192 } else if (name == "Vertical Scale") {
234 setVerticalScale(VerticalScale(value)); 193 setVerticalScale(VerticalScale(value));
235 } else if (name == "Scale Units") { 194 } else if (name == "Scale Units") {
236 if (m_model) { 195 if (m_model) {
237 m_model->setScaleUnits 196 m_model->setScaleUnits
238 (UnitDatabase::getInstance()->getUnitById(value)); 197 (UnitDatabase::getInstance()->getUnitById(value));
239 emit modelChanged(); 198 emit modelChanged();
240 } 199 }
241 } 200 } else {
242 } 201 SingleColourLayer::setProperty(name, value);
243 202 }
244 void
245 TimeValueLayer::setBaseColour(QColor colour)
246 {
247 if (m_colour == colour) return;
248 m_colour = colour;
249 emit layerParametersChanged();
250 } 203 }
251 204
252 void 205 void
253 TimeValueLayer::setFillColourMap(int map) 206 TimeValueLayer::setFillColourMap(int map)
254 { 207 {
563 516
564 QColor solid = ColourMapper(m_colourMap, min, max).map(val); 517 QColor solid = ColourMapper(m_colourMap, min, max).map(val);
565 return QColor(solid.red(), solid.green(), solid.blue(), 120); 518 return QColor(solid.red(), solid.green(), solid.blue(), 120);
566 } 519 }
567 520
521 int
522 TimeValueLayer::getDefaultColourHint(bool darkbg, bool &impose)
523 {
524 impose = false;
525 return ColourDatabase::getInstance()->getColourIndex
526 (QString(darkbg ? "Bright Green" : "Green"));
527 }
528
568 void 529 void
569 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const 530 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const
570 { 531 {
571 if (!m_model || !m_model->isOK()) return; 532 if (!m_model || !m_model->isOK()) return;
572 533
581 542
582 SparseTimeValueModel::PointList points(m_model->getPoints 543 SparseTimeValueModel::PointList points(m_model->getPoints
583 (frame0, frame1)); 544 (frame0, frame1));
584 if (points.empty()) return; 545 if (points.empty()) return;
585 546
586 paint.setPen(m_colour); 547 paint.setPen(getBaseQColor());
587 548
588 QColor brushColour(m_colour); 549 QColor brushColour(getBaseQColor());
589 brushColour.setAlpha(80); 550 brushColour.setAlpha(80);
590 paint.setBrush(brushColour); 551 paint.setBrush(brushColour);
591 552
592 // std::cerr << "TimeValueLayer::paint: resolution is " 553 // std::cerr << "TimeValueLayer::paint: resolution is "
593 // << m_model->getResolution() << " frames" << std::endl; 554 // << m_model->getResolution() << " frames" << std::endl;
654 // << ", nx = " << nx << std::endl; 615 // << ", nx = " << nx << std::endl;
655 616
656 int labelY = y; 617 int labelY = y;
657 618
658 if (w < 1) w = 1; 619 if (w < 1) w = 1;
659 paint.setPen(m_colour); 620 paint.setPen(getBaseQColor());
660 621
661 if (m_plotStyle == PlotSegmentation) { 622 if (m_plotStyle == PlotSegmentation) {
662 paint.setPen(Qt::black); 623 paint.setPen(getForegroundQColor(v));
663 paint.setBrush(getColourForValue(v, p.value)); 624 paint.setBrush(getColourForValue(v, p.value));
664 labelY = v->height(); 625 labelY = v->height();
665 } else if (m_plotStyle == PlotLines || 626 } else if (m_plotStyle == PlotLines ||
666 m_plotStyle == PlotCurve) { 627 m_plotStyle == PlotCurve) {
667 paint.setBrush(Qt::NoBrush); 628 paint.setBrush(Qt::NoBrush);
674 if (y < origin - 1) { 635 if (y < origin - 1) {
675 paint.drawRect(x + w/2, y + 1, 1, origin - y); 636 paint.drawRect(x + w/2, y + 1, 1, origin - y);
676 } else if (y > origin + 1) { 637 } else if (y > origin + 1) {
677 paint.drawRect(x + w/2, origin, 1, y - origin - 1); 638 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
678 } 639 }
679 paint.setPen(m_colour); 640 paint.setPen(getBaseQColor());
680 } 641 }
681 642
682 if (illuminateFrame == p.frame) { 643 if (illuminateFrame == p.frame) {
683 644
684 //!!! aside from the problem of choosing a colour, it'd be 645 //!!! aside from the problem of choosing a colour, it'd be
688 //!!! not equipped to illuminate the right section in line 649 //!!! not equipped to illuminate the right section in line
689 //or curve mode 650 //or curve mode
690 651
691 if (m_plotStyle != PlotCurve && 652 if (m_plotStyle != PlotCurve &&
692 m_plotStyle != PlotLines) { 653 m_plotStyle != PlotLines) {
693 paint.setPen(Qt::black);//!!! 654 paint.setPen(getForegroundQColor(v));
694 if (m_plotStyle != PlotSegmentation) {
695 paint.setBrush(Qt::black);//!!!
696 }
697 } 655 }
698 } 656 }
699 657
700 if (m_plotStyle != PlotLines && 658 if (m_plotStyle != PlotLines &&
701 m_plotStyle != PlotCurve && 659 m_plotStyle != PlotCurve &&
1421 } 1379 }
1422 1380
1423 QString 1381 QString
1424 TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const 1382 TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const
1425 { 1383 {
1426 return Layer::toXmlString(indent, extraAttributes + 1384 return SingleColourLayer::toXmlString(indent, extraAttributes +
1427 QString(" colour=\"%1\" plotStyle=\"%2\" verticalScale=\"%3\"") 1385 QString(" colourMap=\"%1\" plotStyle=\"%2\" verticalScale=\"%3\"")
1428 .arg(encodeColour(m_colour)).arg(m_plotStyle) 1386 .arg(m_colourMap)
1429 .arg(m_verticalScale)); 1387 .arg(m_plotStyle)
1388 .arg(m_verticalScale));
1430 } 1389 }
1431 1390
1432 void 1391 void
1433 TimeValueLayer::setProperties(const QXmlAttributes &attributes) 1392 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
1434 { 1393 {
1435 QString colourSpec = attributes.value("colour"); 1394 SingleColourLayer::setProperties(attributes);
1436 if (colourSpec != "") {
1437 QColor colour(colourSpec);
1438 if (colour.isValid()) {
1439 setBaseColour(QColor(colourSpec));
1440 }
1441 }
1442 1395
1443 bool ok; 1396 bool ok;
1397
1398 int cmap = attributes.value("colourMap").toInt(&ok);
1399 if (ok) setFillColourMap(cmap);
1400
1444 PlotStyle style = (PlotStyle) 1401 PlotStyle style = (PlotStyle)
1445 attributes.value("plotStyle").toInt(&ok); 1402 attributes.value("plotStyle").toInt(&ok);
1446 if (ok) setPlotStyle(style); 1403 if (ok) setPlotStyle(style);
1447 1404
1448 VerticalScale scale = (VerticalScale) 1405 VerticalScale scale = (VerticalScale)