comparison layer/TimeRulerLayer.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 87e4c880b4c8
children c0b9eec70639
comparison
equal deleted inserted replaced
286:7554ae119882 287:cd2492c5fe45
15 15
16 #include "TimeRulerLayer.h" 16 #include "TimeRulerLayer.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/ColourDatabase.h"
20 #include "view/View.h" 21 #include "view/View.h"
21 22
22 #include <QPainter> 23 #include <QPainter>
23 24
24 #include <iostream> 25 #include <iostream>
26 27
27 using std::cerr; 28 using std::cerr;
28 using std::endl; 29 using std::endl;
29 30
30 TimeRulerLayer::TimeRulerLayer() : 31 TimeRulerLayer::TimeRulerLayer() :
31 Layer(), 32 SingleColourLayer(),
32 m_model(0), 33 m_model(0),
33 m_colour(Qt::black),
34 m_labelHeight(LabelTop) 34 m_labelHeight(LabelTop)
35 { 35 {
36 36
37 } 37 }
38 38
40 TimeRulerLayer::setModel(Model *model) 40 TimeRulerLayer::setModel(Model *model)
41 { 41 {
42 if (m_model == model) return; 42 if (m_model == model) return;
43 m_model = model; 43 m_model = model;
44 emit modelReplaced(); 44 emit modelReplaced();
45 }
46
47 void
48 TimeRulerLayer::setBaseColour(QColor colour)
49 {
50 if (m_colour == colour) return;
51 m_colour = colour;
52 emit layerParametersChanged();
53 }
54
55 Layer::PropertyList
56 TimeRulerLayer::getProperties() const
57 {
58 PropertyList list;
59 list.push_back("Colour");
60 return list;
61 }
62
63 QString
64 TimeRulerLayer::getPropertyLabel(const PropertyName &name) const
65 {
66 if (name == "Colour") return tr("Colour");
67 return "";
68 }
69
70 Layer::PropertyType
71 TimeRulerLayer::getPropertyType(const PropertyName &) const
72 {
73 return ValueProperty;
74 }
75
76 int
77 TimeRulerLayer::getPropertyRangeAndValue(const PropertyName &name,
78 int *min, int *max, int *deflt) const
79 {
80 int val = 0;
81
82 if (name == "Colour") {
83
84 if (min) *min = 0;
85 if (max) *max = 5;
86 if (deflt) *deflt = 0;
87
88 if (m_colour == Qt::black) val = 0;
89 else if (m_colour == Qt::darkRed) val = 1;
90 else if (m_colour == Qt::darkBlue) val = 2;
91 else if (m_colour == Qt::darkGreen) val = 3;
92 else if (m_colour == QColor(200, 50, 255)) val = 4;
93 else if (m_colour == QColor(255, 150, 50)) val = 5;
94
95 } else {
96
97 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
98 }
99
100 return val;
101 }
102
103 QString
104 TimeRulerLayer::getPropertyValueLabel(const PropertyName &name,
105 int value) const
106 {
107 if (name == "Colour") {
108 switch (value) {
109 default:
110 case 0: return tr("Black");
111 case 1: return tr("Red");
112 case 2: return tr("Blue");
113 case 3: return tr("Green");
114 case 4: return tr("Purple");
115 case 5: return tr("Orange");
116 }
117 }
118 return tr("<unknown>");
119 }
120
121 void
122 TimeRulerLayer::setProperty(const PropertyName &name, int value)
123 {
124 if (name == "Colour") {
125 switch (value) {
126 default:
127 case 0: setBaseColour(Qt::black); break;
128 case 1: setBaseColour(Qt::darkRed); break;
129 case 2: setBaseColour(Qt::darkBlue); break;
130 case 3: setBaseColour(Qt::darkGreen); break;
131 case 4: setBaseColour(QColor(200, 50, 255)); break;
132 case 5: setBaseColour(QColor(255, 150, 50)); break;
133 }
134 }
135 } 45 }
136 46
137 bool 47 bool
138 TimeRulerLayer::snapToFeatureFrame(View *v, int &frame, 48 TimeRulerLayer::snapToFeatureFrame(View *v, int &frame,
139 size_t &resolution, SnapType snap) const 49 size_t &resolution, SnapType snap) const
321 QRect newClipRect(oldClipRect.x() - 25, oldClipRect.y(), 231 QRect newClipRect(oldClipRect.x() - 25, oldClipRect.y(),
322 oldClipRect.width() + 50, oldClipRect.height()); 232 oldClipRect.width() + 50, oldClipRect.height());
323 paint.setClipRect(newClipRect); 233 paint.setClipRect(newClipRect);
324 paint.setClipRect(rect); 234 paint.setClipRect(rect);
325 235
326 QColor greyColour(m_colour); 236 QColor greyColour = getPartialShades(v)[1];
327 if (m_colour == Qt::black) {
328 greyColour = QColor(200,200,200);
329 } else {
330 greyColour = m_colour.light(150);
331 }
332 237
333 while (1) { 238 while (1) {
334 239
335 rt = RealTime::fromMilliseconds(ms); 240 rt = RealTime::fromMilliseconds(ms);
336 ms += incms; 241 ms += incms;
342 if (x < rect.x() || x >= rect.x() + rect.width()) continue; 247 if (x < rect.x() || x >= rect.x() + rect.width()) continue;
343 248
344 paint.setPen(greyColour); 249 paint.setPen(greyColour);
345 paint.drawLine(x, 0, x, v->height()); 250 paint.drawLine(x, 0, x, v->height());
346 251
347 paint.setPen(m_colour); 252 paint.setPen(getBaseQColor());
348 paint.drawLine(x, 0, x, 5); 253 paint.drawLine(x, 0, x, 5);
349 paint.drawLine(x, v->height() - 6, x, v->height() - 1); 254 paint.drawLine(x, v->height() - 6, x, v->height() - 1);
350 255
351 QString text(QString::fromStdString(rt.toText())); 256 QString text(QString::fromStdString(rt.toText()));
352 257
398 } 303 }
399 } 304 }
400 305
401 paint.restore(); 306 paint.restore();
402 } 307 }
403 308
309 int
310 TimeRulerLayer::getDefaultColourHint(bool darkbg, bool &impose)
311 {
312 impose = true;
313 return ColourDatabase::getInstance()->getColourIndex
314 (QString(darkbg ? "White" : "Black"));
315 }
316
404 QString 317 QString
405 TimeRulerLayer::toXmlString(QString indent, QString extraAttributes) const 318 TimeRulerLayer::toXmlString(QString indent, QString extraAttributes) const
406 { 319 {
407 return Layer::toXmlString(indent, extraAttributes + 320 return SingleColourLayer::toXmlString(indent, extraAttributes);
408 QString(" colour=\"%1\"").arg(encodeColour(m_colour)));
409 } 321 }
410 322
411 void 323 void
412 TimeRulerLayer::setProperties(const QXmlAttributes &attributes) 324 TimeRulerLayer::setProperties(const QXmlAttributes &attributes)
413 { 325 {
414 QString colourSpec = attributes.value("colour"); 326 SingleColourLayer::setProperties(attributes);
415 if (colourSpec != "") { 327 }
416 QColor colour(colourSpec); 328
417 if (colour.isValid()) {
418 setBaseColour(QColor(colourSpec));
419 }
420 }
421 }
422