comparison layer/NoteLayer.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 e175ade2d6b0
children c0b9eec70639
comparison
equal deleted inserted replaced
286:7554ae119882 287:cd2492c5fe45
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/Pitch.h" 21 #include "base/Pitch.h"
22 #include "base/LogRange.h" 22 #include "base/LogRange.h"
23 #include "base/ColourDatabase.h"
23 #include "view/View.h" 24 #include "view/View.h"
24 25
25 #include "data/model/NoteModel.h" 26 #include "data/model/NoteModel.h"
26 27
27 #include "widgets/ItemEditDialog.h" 28 #include "widgets/ItemEditDialog.h"
34 35
35 #include <iostream> 36 #include <iostream>
36 #include <cmath> 37 #include <cmath>
37 38
38 NoteLayer::NoteLayer() : 39 NoteLayer::NoteLayer() :
39 Layer(), 40 SingleColourLayer(),
40 m_model(0), 41 m_model(0),
41 m_editing(false), 42 m_editing(false),
42 m_originalPoint(0, 0.0, 0, tr("New Point")), 43 m_originalPoint(0, 0.0, 0, tr("New Point")),
43 m_editingPoint(0, 0.0, 0, tr("New Point")), 44 m_editingPoint(0, 0.0, 0, tr("New Point")),
44 m_editingCommand(0), 45 m_editingCommand(0),
45 m_colour(Qt::black),
46 m_verticalScale(AutoAlignScale) 46 m_verticalScale(AutoAlignScale)
47 { 47 {
48 48
49 } 49 }
50 50
67 } 67 }
68 68
69 Layer::PropertyList 69 Layer::PropertyList
70 NoteLayer::getProperties() const 70 NoteLayer::getProperties() const
71 { 71 {
72 PropertyList list; 72 PropertyList list = SingleColourLayer::getProperties();
73 list.push_back("Colour");
74 list.push_back("Vertical Scale"); 73 list.push_back("Vertical Scale");
75 list.push_back("Scale Units"); 74 list.push_back("Scale Units");
76 return list; 75 return list;
77 } 76 }
78 77
79 QString 78 QString
80 NoteLayer::getPropertyLabel(const PropertyName &name) const 79 NoteLayer::getPropertyLabel(const PropertyName &name) const
81 { 80 {
82 if (name == "Colour") return tr("Colour");
83 if (name == "Vertical Scale") return tr("Vertical Scale"); 81 if (name == "Vertical Scale") return tr("Vertical Scale");
84 if (name == "Scale Units") return tr("Scale Units"); 82 if (name == "Scale Units") return tr("Scale Units");
85 return ""; 83 return SingleColourLayer::getPropertyLabel(name);
86 } 84 }
87 85
88 Layer::PropertyType 86 Layer::PropertyType
89 NoteLayer::getPropertyType(const PropertyName &name) const 87 NoteLayer::getPropertyType(const PropertyName &name) const
90 { 88 {
91 if (name == "Scale Units") return UnitsProperty; 89 if (name == "Scale Units") return UnitsProperty;
92 return ValueProperty; 90 if (name == "Vertical Scale") return ValueProperty;
91 return SingleColourLayer::getPropertyType(name);
93 } 92 }
94 93
95 QString 94 QString
96 NoteLayer::getPropertyGroupName(const PropertyName &name) const 95 NoteLayer::getPropertyGroupName(const PropertyName &name) const
97 { 96 {
98 if (name == "Vertical Scale" || name == "Scale Units") { 97 if (name == "Vertical Scale" || name == "Scale Units") {
99 return tr("Scale"); 98 return tr("Scale");
100 } 99 }
101 return QString(); 100 return SingleColourLayer::getPropertyGroupName(name);
102 } 101 }
103 102
104 int 103 int
105 NoteLayer::getPropertyRangeAndValue(const PropertyName &name, 104 NoteLayer::getPropertyRangeAndValue(const PropertyName &name,
106 int *min, int *max, int *deflt) const 105 int *min, int *max, int *deflt) const
107 { 106 {
108 //!!! factor this colour handling stuff out into a colour manager class
109
110 int val = 0; 107 int val = 0;
111 108
112 if (name == "Colour") { 109 if (name == "Vertical Scale") {
113
114 if (min) *min = 0;
115 if (max) *max = 5;
116 if (deflt) *deflt = 0;
117
118 if (m_colour == Qt::black) val = 0;
119 else if (m_colour == Qt::darkRed) val = 1;
120 else if (m_colour == Qt::darkBlue) val = 2;
121 else if (m_colour == Qt::darkGreen) val = 3;
122 else if (m_colour == QColor(200, 50, 255)) val = 4;
123 else if (m_colour == QColor(255, 150, 50)) val = 5;
124
125 } else if (name == "Vertical Scale") {
126 110
127 if (min) *min = 0; 111 if (min) *min = 0;
128 if (max) *max = 3; 112 if (max) *max = 3;
129 if (deflt) *deflt = int(AutoAlignScale); 113 if (deflt) *deflt = int(AutoAlignScale);
130 114
138 (m_model->getScaleUnits()); 122 (m_model->getScaleUnits());
139 } 123 }
140 124
141 } else { 125 } else {
142 126
143 val = Layer::getPropertyRangeAndValue(name, min, max, deflt); 127 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
144 } 128 }
145 129
146 return val; 130 return val;
147 } 131 }
148 132
149 QString 133 QString
150 NoteLayer::getPropertyValueLabel(const PropertyName &name, 134 NoteLayer::getPropertyValueLabel(const PropertyName &name,
151 int value) const 135 int value) const
152 { 136 {
153 if (name == "Colour") { 137 if (name == "Vertical Scale") {
154 switch (value) {
155 default:
156 case 0: return tr("Black");
157 case 1: return tr("Red");
158 case 2: return tr("Blue");
159 case 3: return tr("Green");
160 case 4: return tr("Purple");
161 case 5: return tr("Orange");
162 }
163 } else if (name == "Vertical Scale") {
164 switch (value) { 138 switch (value) {
165 default: 139 default:
166 case 0: return tr("Auto-Align"); 140 case 0: return tr("Auto-Align");
167 case 1: return tr("Linear"); 141 case 1: return tr("Linear");
168 case 2: return tr("Log"); 142 case 2: return tr("Log");
169 case 3: return tr("MIDI Notes"); 143 case 3: return tr("MIDI Notes");
170 } 144 }
171 } 145 }
172 return tr("<unknown>"); 146 return SingleColourLayer::getPropertyValueLabel(name, value);
173 } 147 }
174 148
175 void 149 void
176 NoteLayer::setProperty(const PropertyName &name, int value) 150 NoteLayer::setProperty(const PropertyName &name, int value)
177 { 151 {
178 if (name == "Colour") { 152 if (name == "Vertical Scale") {
179 switch (value) {
180 default:
181 case 0: setBaseColour(Qt::black); break;
182 case 1: setBaseColour(Qt::darkRed); break;
183 case 2: setBaseColour(Qt::darkBlue); break;
184 case 3: setBaseColour(Qt::darkGreen); break;
185 case 4: setBaseColour(QColor(200, 50, 255)); break;
186 case 5: setBaseColour(QColor(255, 150, 50)); break;
187 }
188 } else if (name == "Vertical Scale") {
189 setVerticalScale(VerticalScale(value)); 153 setVerticalScale(VerticalScale(value));
190 } else if (name == "Scale Units") { 154 } else if (name == "Scale Units") {
191 if (m_model) { 155 if (m_model) {
192 m_model->setScaleUnits 156 m_model->setScaleUnits
193 (UnitDatabase::getInstance()->getUnitById(value)); 157 (UnitDatabase::getInstance()->getUnitById(value));
194 emit modelChanged(); 158 emit modelChanged();
195 } 159 }
196 } 160 } else {
197 } 161 return SingleColourLayer::setProperty(name, value);
198 162 }
199 void
200 NoteLayer::setBaseColour(QColor colour)
201 {
202 if (m_colour == colour) return;
203 m_colour = colour;
204 emit layerParametersChanged();
205 } 163 }
206 164
207 void 165 void
208 NoteLayer::setVerticalScale(VerticalScale scale) 166 NoteLayer::setVerticalScale(VerticalScale scale)
209 { 167 {
590 long frame1 = v->getFrameForX(x1); 548 long frame1 = v->getFrameForX(x1);
591 549
592 NoteModel::PointList points(m_model->getPoints(frame0, frame1)); 550 NoteModel::PointList points(m_model->getPoints(frame0, frame1));
593 if (points.empty()) return; 551 if (points.empty()) return;
594 552
595 paint.setPen(m_colour); 553 paint.setPen(getBaseQColor());
596 554
597 QColor brushColour(m_colour); 555 QColor brushColour(getBaseQColor());
598 brushColour.setAlpha(80); 556 brushColour.setAlpha(80);
599 557
600 // std::cerr << "NoteLayer::paint: resolution is " 558 // std::cerr << "NoteLayer::paint: resolution is "
601 // << m_model->getResolution() << " frames" << std::endl; 559 // << m_model->getResolution() << " frames" << std::endl;
602 560
630 h = y - getYForValue(v, p.value + m_model->getValueQuantization()); 588 h = y - getYForValue(v, p.value + m_model->getValueQuantization());
631 if (h < 3) h = 3; 589 if (h < 3) h = 3;
632 } 590 }
633 591
634 if (w < 1) w = 1; 592 if (w < 1) w = 1;
635 paint.setPen(m_colour); 593 paint.setPen(getBaseQColor());
636 paint.setBrush(brushColour); 594 paint.setBrush(brushColour);
637 595
638 if (illuminateFrame == p.frame) { 596 if (illuminateFrame == p.frame) {
639 if (localPos.y() >= y - h && localPos.y() < y) { 597 if (localPos.y() >= y - h && localPos.y() < y) {
640 paint.setPen(Qt::black);//!!! 598 paint.setPen(v->getForeground());
641 paint.setBrush(Qt::black);//!!! 599 paint.setBrush(v->getForeground());
642 } 600 }
643 } 601 }
644 602
645 paint.drawRect(x, y - h/2, w, h); 603 paint.drawRect(x, y - h/2, w, h);
646 604
980 938
981 command->finish(); 939 command->finish();
982 return true; 940 return true;
983 } 941 }
984 942
943 int
944 NoteLayer::getDefaultColourHint(bool darkbg, bool &impose)
945 {
946 impose = false;
947 return ColourDatabase::getInstance()->getColourIndex
948 (QString(darkbg ? "White" : "Black"));
949 }
950
985 QString 951 QString
986 NoteLayer::toXmlString(QString indent, QString extraAttributes) const 952 NoteLayer::toXmlString(QString indent, QString extraAttributes) const
987 { 953 {
988 return Layer::toXmlString(indent, extraAttributes + 954 return SingleColourLayer::toXmlString(indent, extraAttributes +
989 QString(" colour=\"%1\" verticalScale=\"%2\"") 955 QString(" verticalScale=\"%1\"")
990 .arg(encodeColour(m_colour)).arg(m_verticalScale)); 956 .arg(m_verticalScale));
991 } 957 }
992 958
993 void 959 void
994 NoteLayer::setProperties(const QXmlAttributes &attributes) 960 NoteLayer::setProperties(const QXmlAttributes &attributes)
995 { 961 {
996 QString colourSpec = attributes.value("colour"); 962 SingleColourLayer::setProperties(attributes);
997 if (colourSpec != "") {
998 QColor colour(colourSpec);
999 if (colour.isValid()) {
1000 setBaseColour(QColor(colourSpec));
1001 }
1002 }
1003 963
1004 bool ok; 964 bool ok;
1005 VerticalScale scale = (VerticalScale) 965 VerticalScale scale = (VerticalScale)
1006 attributes.value("verticalScale").toInt(&ok); 966 attributes.value("verticalScale").toInt(&ok);
1007 if (ok) setVerticalScale(scale); 967 if (ok) setVerticalScale(scale);