Chris@0
|
1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 A waveform viewer and audio annotation editor.
|
Chris@3
|
5 Chris Cannam, Queen Mary University of London, 2005-2006
|
Chris@0
|
6
|
Chris@0
|
7 This is experimental software. Not for distribution.
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 #include "TimeValueLayer.h"
|
Chris@0
|
11
|
Chris@0
|
12 #include "base/Model.h"
|
Chris@0
|
13 #include "base/RealTime.h"
|
Chris@0
|
14 #include "base/Profiler.h"
|
Chris@0
|
15 #include "base/View.h"
|
Chris@0
|
16
|
Chris@0
|
17 #include "model/SparseTimeValueModel.h"
|
Chris@0
|
18
|
Chris@0
|
19 #include <QPainter>
|
Chris@0
|
20
|
Chris@0
|
21 #include <iostream>
|
Chris@0
|
22 #include <cmath>
|
Chris@0
|
23
|
Chris@0
|
24 TimeValueLayer::TimeValueLayer(View *w) :
|
Chris@0
|
25 Layer(w),
|
Chris@0
|
26 m_model(0),
|
Chris@0
|
27 m_colour(Qt::black),
|
Chris@0
|
28 m_plotStyle(PlotLines)
|
Chris@0
|
29 {
|
Chris@0
|
30 m_view->addLayer(this);
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 void
|
Chris@0
|
34 TimeValueLayer::setModel(SparseTimeValueModel *model)
|
Chris@0
|
35 {
|
Chris@0
|
36 if (m_model == model) return;
|
Chris@0
|
37 m_model = model;
|
Chris@0
|
38
|
Chris@0
|
39 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
|
Chris@0
|
40 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
|
Chris@0
|
41 this, SIGNAL(modelChanged(size_t, size_t)));
|
Chris@0
|
42
|
Chris@0
|
43 connect(m_model, SIGNAL(completionChanged()),
|
Chris@0
|
44 this, SIGNAL(modelCompletionChanged()));
|
Chris@0
|
45
|
Chris@0
|
46 std::cerr << "TimeValueLayer::setModel(" << model << ")" << std::endl;
|
Chris@0
|
47
|
Chris@0
|
48 emit modelReplaced();
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 Layer::PropertyList
|
Chris@0
|
52 TimeValueLayer::getProperties() const
|
Chris@0
|
53 {
|
Chris@0
|
54 PropertyList list;
|
Chris@0
|
55 list.push_back(tr("Colour"));
|
Chris@0
|
56 list.push_back(tr("Plot Type"));
|
Chris@0
|
57 return list;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 Layer::PropertyType
|
Chris@0
|
61 TimeValueLayer::getPropertyType(const PropertyName &name) const
|
Chris@0
|
62 {
|
Chris@0
|
63 return ValueProperty;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 int
|
Chris@0
|
67 TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@0
|
68 int *min, int *max) const
|
Chris@0
|
69 {
|
Chris@0
|
70 //!!! factor this colour handling stuff out into a colour manager class
|
Chris@0
|
71
|
Chris@0
|
72 int deft = 0;
|
Chris@0
|
73
|
Chris@0
|
74 if (name == tr("Colour")) {
|
Chris@0
|
75
|
Chris@0
|
76 *min = 0;
|
Chris@0
|
77 *max = 5;
|
Chris@0
|
78
|
Chris@0
|
79 if (m_colour == Qt::black) deft = 0;
|
Chris@0
|
80 else if (m_colour == Qt::darkRed) deft = 1;
|
Chris@0
|
81 else if (m_colour == Qt::darkBlue) deft = 2;
|
Chris@0
|
82 else if (m_colour == Qt::darkGreen) deft = 3;
|
Chris@0
|
83 else if (m_colour == QColor(200, 50, 255)) deft = 4;
|
Chris@0
|
84 else if (m_colour == QColor(255, 150, 50)) deft = 5;
|
Chris@0
|
85
|
Chris@0
|
86 } else if (name == tr("Plot Type")) {
|
Chris@0
|
87
|
Chris@0
|
88 *min = 0;
|
Chris@3
|
89 *max = 3;
|
Chris@0
|
90
|
Chris@0
|
91 deft = int(m_plotStyle);
|
Chris@0
|
92
|
Chris@0
|
93 } else {
|
Chris@0
|
94
|
Chris@0
|
95 deft = Layer::getPropertyRangeAndValue(name, min, max);
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 return deft;
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 QString
|
Chris@0
|
102 TimeValueLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@0
|
103 int value) const
|
Chris@0
|
104 {
|
Chris@0
|
105 if (name == tr("Colour")) {
|
Chris@0
|
106 switch (value) {
|
Chris@0
|
107 default:
|
Chris@0
|
108 case 0: return tr("Black");
|
Chris@0
|
109 case 1: return tr("Red");
|
Chris@0
|
110 case 2: return tr("Blue");
|
Chris@0
|
111 case 3: return tr("Green");
|
Chris@0
|
112 case 4: return tr("Purple");
|
Chris@0
|
113 case 5: return tr("Orange");
|
Chris@0
|
114 }
|
Chris@0
|
115 } else if (name == tr("Plot Type")) {
|
Chris@0
|
116 switch (value) {
|
Chris@0
|
117 default:
|
Chris@0
|
118 case 0: return tr("Points");
|
Chris@0
|
119 case 1: return tr("Stems");
|
Chris@0
|
120 case 2: return tr("Lines");
|
Chris@3
|
121 case 3: return tr("Curve");
|
Chris@0
|
122 }
|
Chris@0
|
123 }
|
Chris@0
|
124 return tr("<unknown>");
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 void
|
Chris@0
|
128 TimeValueLayer::setProperty(const PropertyName &name, int value)
|
Chris@0
|
129 {
|
Chris@0
|
130 if (name == tr("Colour")) {
|
Chris@0
|
131 switch (value) {
|
Chris@0
|
132 default:
|
Chris@0
|
133 case 0: setBaseColour(Qt::black); break;
|
Chris@0
|
134 case 1: setBaseColour(Qt::darkRed); break;
|
Chris@0
|
135 case 2: setBaseColour(Qt::darkBlue); break;
|
Chris@0
|
136 case 3: setBaseColour(Qt::darkGreen); break;
|
Chris@0
|
137 case 4: setBaseColour(QColor(200, 50, 255)); break;
|
Chris@0
|
138 case 5: setBaseColour(QColor(255, 150, 50)); break;
|
Chris@0
|
139 }
|
Chris@0
|
140 } else if (name == tr("Plot Type")) {
|
Chris@0
|
141 setPlotStyle(PlotStyle(value));
|
Chris@0
|
142 }
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 void
|
Chris@0
|
146 TimeValueLayer::setBaseColour(QColor colour)
|
Chris@0
|
147 {
|
Chris@0
|
148 if (m_colour == colour) return;
|
Chris@0
|
149 m_colour = colour;
|
Chris@0
|
150 emit layerParametersChanged();
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 void
|
Chris@0
|
154 TimeValueLayer::setPlotStyle(PlotStyle style)
|
Chris@0
|
155 {
|
Chris@0
|
156 if (m_plotStyle == style) return;
|
Chris@0
|
157 m_plotStyle = style;
|
Chris@0
|
158 emit layerParametersChanged();
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 bool
|
Chris@0
|
162 TimeValueLayer::isLayerScrollable() const
|
Chris@0
|
163 {
|
Chris@0
|
164 QPoint discard;
|
Chris@0
|
165 return !m_view->shouldIlluminateLocalFeatures(this, discard);
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 QRect
|
Chris@0
|
169 TimeValueLayer::getFeatureDescriptionRect(QPainter &paint, QPoint pos) const
|
Chris@0
|
170 {
|
Chris@0
|
171 return QRect(0, 0,
|
Chris@0
|
172 std::max(100, paint.fontMetrics().width(tr("No local points"))),
|
Chris@0
|
173 70); //!!!
|
Chris@0
|
174 }
|
Chris@0
|
175
|
Chris@0
|
176 //!!! too much in common with TimeInstantLayer
|
Chris@0
|
177
|
Chris@0
|
178 SparseTimeValueModel::PointList
|
Chris@0
|
179 TimeValueLayer::getLocalPoints(int x) const
|
Chris@0
|
180 {
|
Chris@0
|
181 if (!m_model) return SparseTimeValueModel::PointList();
|
Chris@0
|
182
|
Chris@0
|
183 long startFrame = m_view->getStartFrame();
|
Chris@0
|
184 long endFrame = m_view->getEndFrame();
|
Chris@0
|
185 int zoomLevel = m_view->getZoomLevel();
|
Chris@0
|
186 long frame = startFrame + x * zoomLevel;
|
Chris@0
|
187
|
Chris@0
|
188 SparseTimeValueModel::PointList onPoints =
|
Chris@0
|
189 m_model->getPoints(frame);
|
Chris@0
|
190
|
Chris@0
|
191 if (!onPoints.empty()) {
|
Chris@0
|
192 return onPoints;
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 SparseTimeValueModel::PointList prevPoints =
|
Chris@0
|
196 m_model->getPreviousPoints(frame);
|
Chris@0
|
197 SparseTimeValueModel::PointList nextPoints =
|
Chris@0
|
198 m_model->getNextPoints(frame);
|
Chris@0
|
199
|
Chris@0
|
200 SparseTimeValueModel::PointList usePoints = prevPoints;
|
Chris@0
|
201
|
Chris@0
|
202 if (prevPoints.empty()) {
|
Chris@0
|
203 usePoints = nextPoints;
|
Chris@0
|
204 } else if (prevPoints.begin()->frame < startFrame &&
|
Chris@0
|
205 !(nextPoints.begin()->frame > endFrame)) {
|
Chris@0
|
206 usePoints = nextPoints;
|
Chris@0
|
207 } else if (nextPoints.begin()->frame - frame <
|
Chris@0
|
208 frame - prevPoints.begin()->frame) {
|
Chris@0
|
209 usePoints = nextPoints;
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 return usePoints;
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 void
|
Chris@0
|
216 TimeValueLayer::paintLocalFeatureDescription(QPainter &paint, QRect rect,
|
Chris@0
|
217 QPoint pos) const
|
Chris@0
|
218 {
|
Chris@0
|
219 //!!! bleagh
|
Chris@0
|
220
|
Chris@0
|
221 int x = pos.x();
|
Chris@0
|
222
|
Chris@0
|
223 if (!m_model || !m_model->getSampleRate()) return;
|
Chris@0
|
224
|
Chris@0
|
225 SparseTimeValueModel::PointList points = getLocalPoints(x);
|
Chris@0
|
226
|
Chris@0
|
227 QFontMetrics metrics = paint.fontMetrics();
|
Chris@0
|
228 int xbase = rect.x() + 5;
|
Chris@0
|
229 int ybase = rect.y() + 5;
|
Chris@0
|
230
|
Chris@0
|
231 if (points.empty()) {
|
Chris@0
|
232 QString label = tr("No local points");
|
Chris@0
|
233 if (!m_model->isReady()) {
|
Chris@0
|
234 label = tr("In progress");
|
Chris@0
|
235 }
|
Chris@0
|
236 paint.drawText(xbase + 5, ybase + 5 + metrics.ascent(), label);
|
Chris@0
|
237 return;
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@0
|
240 long useFrame = points.begin()->frame;
|
Chris@0
|
241
|
Chris@0
|
242 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
|
Chris@0
|
243 QString timeText = QString("%1").arg(rt.toText(true).c_str());
|
Chris@0
|
244 QString valueText = QString("%1").arg(points.begin()->value);
|
Chris@0
|
245
|
Chris@0
|
246 int timewidth = metrics.width(timeText);
|
Chris@0
|
247 int valuewidth = metrics.width(valueText);
|
Chris@0
|
248 int labelwidth = metrics.width(points.begin()->label);
|
Chris@0
|
249
|
Chris@0
|
250 int boxheight = metrics.height() * 3 + 4;
|
Chris@0
|
251 int boxwidth = std::max(std::max(timewidth, labelwidth), valuewidth);
|
Chris@0
|
252
|
Chris@0
|
253 paint.drawRect(xbase, ybase, boxwidth + 10,
|
Chris@0
|
254 boxheight + 10 - metrics.descent() + 1);
|
Chris@0
|
255
|
Chris@0
|
256 paint.drawText(xbase + 5, ybase + 5 + metrics.ascent(), timeText);
|
Chris@0
|
257 paint.drawText(xbase + 5, ybase + 7 + metrics.ascent() + metrics.height(),
|
Chris@0
|
258 valueText);
|
Chris@0
|
259 paint.drawText(xbase + 5, ybase + 9 + metrics.ascent() + 2*metrics.height(),
|
Chris@0
|
260 points.begin()->label);
|
Chris@0
|
261 }
|
Chris@0
|
262
|
Chris@0
|
263 void
|
Chris@0
|
264 TimeValueLayer::paint(QPainter &paint, QRect rect) const
|
Chris@0
|
265 {
|
Chris@0
|
266 if (!m_model || !m_model->isOK()) return;
|
Chris@0
|
267
|
Chris@0
|
268 int sampleRate = m_model->getSampleRate();
|
Chris@0
|
269 if (!sampleRate) return;
|
Chris@0
|
270
|
Chris@0
|
271 // Profiler profiler("TimeValueLayer::paint", true);
|
Chris@0
|
272
|
Chris@0
|
273 long startFrame = m_view->getStartFrame();
|
Chris@0
|
274 int zoomLevel = m_view->getZoomLevel();
|
Chris@0
|
275
|
Chris@0
|
276 int x0 = rect.left(), x1 = rect.right();
|
Chris@0
|
277 long frame0 = startFrame + x0 * zoomLevel;
|
Chris@0
|
278 long frame1 = startFrame + x1 * zoomLevel;
|
Chris@0
|
279
|
Chris@0
|
280 SparseTimeValueModel::PointList points(m_model->getPoints
|
Chris@0
|
281 (frame0, frame1));
|
Chris@0
|
282
|
Chris@0
|
283 paint.setPen(m_colour);
|
Chris@0
|
284
|
Chris@0
|
285 QColor brushColour(m_colour);
|
Chris@0
|
286 brushColour.setAlpha(80);
|
Chris@0
|
287 paint.setBrush(brushColour);
|
Chris@0
|
288
|
Chris@0
|
289 // std::cerr << "TimeValueLayer::paint: resolution is "
|
Chris@0
|
290 // << m_model->getResolution() << " frames" << std::endl;
|
Chris@0
|
291
|
Chris@0
|
292 float min = m_model->getValueMinimum();
|
Chris@0
|
293 float max = m_model->getValueMaximum();
|
Chris@0
|
294 if (max == min) max = min + 1.0;
|
Chris@0
|
295
|
Chris@0
|
296 int origin = int(nearbyint(m_view->height() -
|
Chris@0
|
297 (-min * m_view->height()) / (max - min)));
|
Chris@0
|
298
|
Chris@0
|
299 QPoint localPos;
|
Chris@0
|
300 long illuminateFrame = -1;
|
Chris@0
|
301
|
Chris@0
|
302 if (m_view->shouldIlluminateLocalFeatures(this, localPos)) {
|
Chris@0
|
303 SparseTimeValueModel::PointList localPoints =
|
Chris@0
|
304 getLocalPoints(localPos.x());
|
Chris@0
|
305 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
|
Chris@0
|
306 }
|
Chris@0
|
307
|
Chris@0
|
308 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
|
Chris@0
|
309 i != points.end(); ++i) {
|
Chris@0
|
310
|
Chris@0
|
311 const SparseTimeValueModel::Point &p(*i);
|
Chris@0
|
312
|
Chris@0
|
313 int x = (p.frame - startFrame) / zoomLevel;
|
Chris@0
|
314 int y = int(nearbyint(m_view->height() -
|
Chris@0
|
315 ((p.value - min) * m_view->height()) /
|
Chris@0
|
316 (max - min)));
|
Chris@0
|
317 int w = m_model->getResolution() / zoomLevel;
|
Chris@0
|
318
|
Chris@0
|
319 if (w < 1) w = 1;
|
Chris@0
|
320
|
Chris@3
|
321 if (m_plotStyle == PlotCurve) {
|
Chris@3
|
322 paint.setPen(QPen(QBrush(m_colour), 2));
|
Chris@3
|
323 } else {
|
Chris@3
|
324 paint.setPen(m_colour);
|
Chris@3
|
325 }
|
Chris@0
|
326 paint.setBrush(brushColour);
|
Chris@0
|
327
|
Chris@0
|
328 if (m_plotStyle == PlotStems) {
|
Chris@0
|
329 paint.setPen(brushColour);
|
Chris@0
|
330 if (y < origin - 1) {
|
Chris@0
|
331 paint.drawRect(x + w/2, y + 1, 1, origin - y);
|
Chris@0
|
332 } else if (y > origin + 1) {
|
Chris@0
|
333 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
|
Chris@0
|
334 }
|
Chris@0
|
335 paint.setPen(m_colour);
|
Chris@0
|
336 }
|
Chris@0
|
337
|
Chris@0
|
338 if (illuminateFrame == p.frame) {
|
Chris@0
|
339 //!!! aside from the problem of choosing a colour, it'd be
|
Chris@0
|
340 //better to save the highlighted rects and draw them at
|
Chris@0
|
341 //the end perhaps
|
Chris@0
|
342 paint.setPen(Qt::black);//!!!
|
Chris@0
|
343 paint.setBrush(Qt::black);//!!!
|
Chris@0
|
344 }
|
Chris@0
|
345
|
Chris@3
|
346 if (m_plotStyle != PlotCurve) {
|
Chris@3
|
347 paint.drawRect(x, y - 1, w, 2);
|
Chris@3
|
348 }
|
Chris@0
|
349
|
Chris@3
|
350 if (m_plotStyle == PlotLines || m_plotStyle == PlotCurve) {
|
Chris@0
|
351
|
Chris@0
|
352 SparseTimeValueModel::PointList::const_iterator j = i;
|
Chris@0
|
353 ++j;
|
Chris@3
|
354
|
Chris@0
|
355 if (j != points.end()) {
|
Chris@3
|
356
|
Chris@0
|
357 const SparseTimeValueModel::Point &q(*j);
|
Chris@0
|
358 int nx = (q.frame - startFrame) / zoomLevel;
|
Chris@0
|
359 int ny = int(nearbyint(m_view->height() -
|
Chris@0
|
360 ((q.value - min) * m_view->height()) /
|
Chris@0
|
361 (max - min)));
|
Chris@3
|
362
|
Chris@3
|
363 if (m_plotStyle == PlotLines) {
|
Chris@3
|
364 paint.setPen(brushColour);
|
Chris@3
|
365 paint.drawLine(x + w, y, nx, ny);
|
Chris@3
|
366 } else {
|
Chris@3
|
367 paint.drawLine(x, y, nx, ny);
|
Chris@3
|
368 }
|
Chris@0
|
369 }
|
Chris@0
|
370 }
|
Chris@0
|
371
|
Chris@0
|
372
|
Chris@0
|
373 /// if (p.label != "") {
|
Chris@0
|
374 /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label);
|
Chris@0
|
375 /// }
|
Chris@0
|
376 }
|
Chris@0
|
377
|
Chris@0
|
378
|
Chris@0
|
379 }
|
Chris@0
|
380
|
Chris@0
|
381
|
Chris@0
|
382 #ifdef INCLUDE_MOCFILES
|
Chris@0
|
383 #include "TimeValueLayer.moc.cpp"
|
Chris@0
|
384 #endif
|
Chris@0
|
385
|