Chris@58
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@59
|
4 Sonic Visualiser
|
Chris@59
|
5 An audio file viewer and annotation editor.
|
Chris@59
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@59
|
7 This file copyright 2006 Chris Cannam.
|
Chris@0
|
8
|
Chris@59
|
9 This program is free software; you can redistribute it and/or
|
Chris@59
|
10 modify it under the terms of the GNU General Public License as
|
Chris@59
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@59
|
12 License, or (at your option) any later version. See the file
|
Chris@59
|
13 COPYING included with this distribution for more information.
|
Chris@0
|
14 */
|
Chris@0
|
15
|
Chris@0
|
16 #include "TimeRulerLayer.h"
|
Chris@0
|
17
|
Chris@128
|
18 #include "data/model/Model.h"
|
Chris@0
|
19 #include "base/RealTime.h"
|
Chris@128
|
20 #include "view/View.h"
|
Chris@0
|
21
|
Chris@0
|
22 #include <QPainter>
|
Chris@0
|
23
|
Chris@0
|
24 #include <iostream>
|
Chris@271
|
25 #include <cmath>
|
Chris@0
|
26
|
Chris@0
|
27 using std::cerr;
|
Chris@0
|
28 using std::endl;
|
Chris@0
|
29
|
Chris@44
|
30 TimeRulerLayer::TimeRulerLayer() :
|
Chris@44
|
31 Layer(),
|
Chris@0
|
32 m_model(0),
|
Chris@0
|
33 m_colour(Qt::black),
|
Chris@0
|
34 m_labelHeight(LabelTop)
|
Chris@0
|
35 {
|
Chris@44
|
36
|
Chris@0
|
37 }
|
Chris@0
|
38
|
Chris@0
|
39 void
|
Chris@0
|
40 TimeRulerLayer::setModel(Model *model)
|
Chris@0
|
41 {
|
Chris@0
|
42 if (m_model == model) return;
|
Chris@0
|
43 m_model = model;
|
Chris@0
|
44 emit modelReplaced();
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 void
|
Chris@0
|
48 TimeRulerLayer::setBaseColour(QColor colour)
|
Chris@0
|
49 {
|
Chris@0
|
50 if (m_colour == colour) return;
|
Chris@0
|
51 m_colour = colour;
|
Chris@0
|
52 emit layerParametersChanged();
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 Layer::PropertyList
|
Chris@0
|
56 TimeRulerLayer::getProperties() const
|
Chris@0
|
57 {
|
Chris@0
|
58 PropertyList list;
|
Chris@87
|
59 list.push_back("Colour");
|
Chris@0
|
60 return list;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@87
|
63 QString
|
Chris@87
|
64 TimeRulerLayer::getPropertyLabel(const PropertyName &name) const
|
Chris@87
|
65 {
|
Chris@87
|
66 if (name == "Colour") return tr("Colour");
|
Chris@87
|
67 return "";
|
Chris@87
|
68 }
|
Chris@87
|
69
|
Chris@0
|
70 Layer::PropertyType
|
Chris@248
|
71 TimeRulerLayer::getPropertyType(const PropertyName &) const
|
Chris@0
|
72 {
|
Chris@0
|
73 return ValueProperty;
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 int
|
Chris@0
|
77 TimeRulerLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@216
|
78 int *min, int *max, int *deflt) const
|
Chris@0
|
79 {
|
Chris@216
|
80 int val = 0;
|
Chris@0
|
81
|
Chris@87
|
82 if (name == "Colour") {
|
Chris@0
|
83
|
Chris@10
|
84 if (min) *min = 0;
|
Chris@10
|
85 if (max) *max = 5;
|
Chris@216
|
86 if (deflt) *deflt = 0;
|
Chris@0
|
87
|
Chris@216
|
88 if (m_colour == Qt::black) val = 0;
|
Chris@216
|
89 else if (m_colour == Qt::darkRed) val = 1;
|
Chris@216
|
90 else if (m_colour == Qt::darkBlue) val = 2;
|
Chris@216
|
91 else if (m_colour == Qt::darkGreen) val = 3;
|
Chris@216
|
92 else if (m_colour == QColor(200, 50, 255)) val = 4;
|
Chris@216
|
93 else if (m_colour == QColor(255, 150, 50)) val = 5;
|
Chris@0
|
94
|
Chris@0
|
95 } else {
|
Chris@0
|
96
|
Chris@216
|
97 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@216
|
100 return val;
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 QString
|
Chris@0
|
104 TimeRulerLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@0
|
105 int value) const
|
Chris@0
|
106 {
|
Chris@87
|
107 if (name == "Colour") {
|
Chris@0
|
108 switch (value) {
|
Chris@0
|
109 default:
|
Chris@0
|
110 case 0: return tr("Black");
|
Chris@0
|
111 case 1: return tr("Red");
|
Chris@0
|
112 case 2: return tr("Blue");
|
Chris@0
|
113 case 3: return tr("Green");
|
Chris@0
|
114 case 4: return tr("Purple");
|
Chris@0
|
115 case 5: return tr("Orange");
|
Chris@0
|
116 }
|
Chris@0
|
117 }
|
Chris@0
|
118 return tr("<unknown>");
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 void
|
Chris@0
|
122 TimeRulerLayer::setProperty(const PropertyName &name, int value)
|
Chris@0
|
123 {
|
Chris@87
|
124 if (name == "Colour") {
|
Chris@0
|
125 switch (value) {
|
Chris@0
|
126 default:
|
Chris@0
|
127 case 0: setBaseColour(Qt::black); break;
|
Chris@0
|
128 case 1: setBaseColour(Qt::darkRed); break;
|
Chris@0
|
129 case 2: setBaseColour(Qt::darkBlue); break;
|
Chris@0
|
130 case 3: setBaseColour(Qt::darkGreen); break;
|
Chris@0
|
131 case 4: setBaseColour(QColor(200, 50, 255)); break;
|
Chris@0
|
132 case 5: setBaseColour(QColor(255, 150, 50)); break;
|
Chris@0
|
133 }
|
Chris@0
|
134 }
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@271
|
137 bool
|
Chris@271
|
138 TimeRulerLayer::snapToFeatureFrame(View *v, int &frame,
|
Chris@271
|
139 size_t &resolution, SnapType snap) const
|
Chris@271
|
140 {
|
Chris@271
|
141 if (!m_model) {
|
Chris@271
|
142 resolution = 1;
|
Chris@271
|
143 return false;
|
Chris@271
|
144 }
|
Chris@271
|
145
|
Chris@271
|
146 bool q;
|
Chris@271
|
147 int tick = getMajorTickSpacing(v, q);
|
Chris@271
|
148 RealTime rtick = RealTime::fromMilliseconds(tick);
|
Chris@271
|
149 int rate = m_model->getSampleRate();
|
Chris@271
|
150
|
Chris@271
|
151 RealTime rt = RealTime::frame2RealTime(frame, rate);
|
Chris@271
|
152 double ratio = rt / rtick;
|
Chris@271
|
153
|
Chris@271
|
154 int rounded = lrint(ratio);
|
Chris@271
|
155 RealTime rdrt = rtick * rounded;
|
Chris@271
|
156
|
Chris@271
|
157 int left = RealTime::realTime2Frame(rdrt, rate);
|
Chris@271
|
158 resolution = RealTime::realTime2Frame(rtick, rate);
|
Chris@271
|
159 int right = left + resolution;
|
Chris@271
|
160
|
Chris@271
|
161 switch (snap) {
|
Chris@271
|
162
|
Chris@271
|
163 case SnapLeft:
|
Chris@271
|
164 frame = left;
|
Chris@271
|
165 break;
|
Chris@271
|
166
|
Chris@271
|
167 case SnapRight:
|
Chris@271
|
168 frame = right;
|
Chris@271
|
169 break;
|
Chris@271
|
170
|
Chris@271
|
171 case SnapNearest:
|
Chris@271
|
172 {
|
Chris@271
|
173 if (abs(frame - left) > abs(right - frame)) {
|
Chris@271
|
174 frame = right;
|
Chris@271
|
175 } else {
|
Chris@271
|
176 frame = left;
|
Chris@271
|
177 }
|
Chris@271
|
178 break;
|
Chris@271
|
179 }
|
Chris@271
|
180
|
Chris@271
|
181 case SnapNeighbouring:
|
Chris@271
|
182 {
|
Chris@271
|
183 int dl = -1, dr = -1;
|
Chris@271
|
184 int x = v->getXForFrame(frame);
|
Chris@271
|
185
|
Chris@271
|
186 if (left > v->getStartFrame() &&
|
Chris@271
|
187 left < v->getEndFrame()) {
|
Chris@271
|
188 dl = abs(v->getXForFrame(left) - x);
|
Chris@271
|
189 }
|
Chris@271
|
190
|
Chris@271
|
191 if (right > v->getStartFrame() &&
|
Chris@271
|
192 right < v->getEndFrame()) {
|
Chris@271
|
193 dr = abs(v->getXForFrame(right) - x);
|
Chris@271
|
194 }
|
Chris@271
|
195
|
Chris@271
|
196 int fuzz = 2;
|
Chris@271
|
197
|
Chris@271
|
198 if (dl >= 0 && dr >= 0) {
|
Chris@271
|
199 if (dl < dr) {
|
Chris@271
|
200 if (dl <= fuzz) {
|
Chris@271
|
201 frame = left;
|
Chris@271
|
202 }
|
Chris@271
|
203 } else {
|
Chris@271
|
204 if (dr < fuzz) {
|
Chris@271
|
205 frame = right;
|
Chris@271
|
206 }
|
Chris@271
|
207 }
|
Chris@271
|
208 } else if (dl >= 0) {
|
Chris@271
|
209 if (dl <= fuzz) {
|
Chris@271
|
210 frame = left;
|
Chris@271
|
211 }
|
Chris@271
|
212 } else if (dr >= 0) {
|
Chris@271
|
213 if (dr <= fuzz) {
|
Chris@271
|
214 frame = right;
|
Chris@271
|
215 }
|
Chris@271
|
216 }
|
Chris@271
|
217 }
|
Chris@271
|
218 }
|
Chris@271
|
219
|
Chris@271
|
220 return true;
|
Chris@271
|
221 }
|
Chris@271
|
222
|
Chris@271
|
223 int
|
Chris@271
|
224 TimeRulerLayer::getMajorTickSpacing(View *v, bool &quarterTicks) const
|
Chris@271
|
225 {
|
Chris@271
|
226 // return value is in milliseconds
|
Chris@271
|
227
|
Chris@271
|
228 if (!m_model || !v) return 1000;
|
Chris@271
|
229
|
Chris@271
|
230 int sampleRate = m_model->getSampleRate();
|
Chris@271
|
231 if (!sampleRate) return 1000;
|
Chris@271
|
232
|
Chris@271
|
233 long startFrame = v->getStartFrame();
|
Chris@271
|
234 long endFrame = v->getEndFrame();
|
Chris@271
|
235
|
Chris@271
|
236 int minPixelSpacing = 50;
|
Chris@271
|
237
|
Chris@271
|
238 RealTime rtStart = RealTime::frame2RealTime(startFrame, sampleRate);
|
Chris@271
|
239 RealTime rtEnd = RealTime::frame2RealTime(endFrame, sampleRate);
|
Chris@271
|
240
|
Chris@271
|
241 int count = v->width() / minPixelSpacing;
|
Chris@271
|
242 if (count < 1) count = 1;
|
Chris@271
|
243 RealTime rtGap = (rtEnd - rtStart) / count;
|
Chris@271
|
244
|
Chris@271
|
245 int incms;
|
Chris@271
|
246 quarterTicks = false;
|
Chris@271
|
247
|
Chris@271
|
248 if (rtGap.sec > 0) {
|
Chris@271
|
249 incms = 1000;
|
Chris@271
|
250 int s = rtGap.sec;
|
Chris@271
|
251 if (s > 0) { incms *= 5; s /= 5; }
|
Chris@271
|
252 if (s > 0) { incms *= 2; s /= 2; }
|
Chris@271
|
253 if (s > 0) { incms *= 6; s /= 6; quarterTicks = true; }
|
Chris@271
|
254 if (s > 0) { incms *= 5; s /= 5; quarterTicks = false; }
|
Chris@271
|
255 if (s > 0) { incms *= 2; s /= 2; }
|
Chris@271
|
256 if (s > 0) { incms *= 6; s /= 6; quarterTicks = true; }
|
Chris@271
|
257 while (s > 0) {
|
Chris@271
|
258 incms *= 10;
|
Chris@271
|
259 s /= 10;
|
Chris@271
|
260 quarterTicks = false;
|
Chris@271
|
261 }
|
Chris@271
|
262 } else {
|
Chris@271
|
263 incms = 1;
|
Chris@271
|
264 int ms = rtGap.msec();
|
Chris@271
|
265 if (ms > 0) { incms *= 10; ms /= 10; }
|
Chris@271
|
266 if (ms > 0) { incms *= 10; ms /= 10; }
|
Chris@271
|
267 if (ms > 0) { incms *= 5; ms /= 5; }
|
Chris@271
|
268 if (ms > 0) { incms *= 2; ms /= 2; }
|
Chris@271
|
269 }
|
Chris@271
|
270
|
Chris@271
|
271 return incms;
|
Chris@271
|
272 }
|
Chris@271
|
273
|
Chris@0
|
274 void
|
Chris@44
|
275 TimeRulerLayer::paint(View *v, QPainter &paint, QRect rect) const
|
Chris@0
|
276 {
|
Chris@0
|
277 // std::cerr << "TimeRulerLayer::paint (" << rect.x() << "," << rect.y()
|
Chris@0
|
278 // << ") [" << rect.width() << "x" << rect.height() << "]" << std::endl;
|
Chris@0
|
279
|
Chris@0
|
280 if (!m_model || !m_model->isOK()) return;
|
Chris@0
|
281
|
Chris@0
|
282 int sampleRate = m_model->getSampleRate();
|
Chris@0
|
283 if (!sampleRate) return;
|
Chris@0
|
284
|
Chris@44
|
285 long startFrame = v->getStartFrame();
|
Chris@44
|
286 long endFrame = v->getEndFrame();
|
Chris@0
|
287
|
Chris@44
|
288 int zoomLevel = v->getZoomLevel();
|
Chris@0
|
289
|
Chris@0
|
290 long rectStart = startFrame + (rect.x() - 100) * zoomLevel;
|
Chris@0
|
291 long rectEnd = startFrame + (rect.x() + rect.width() + 100) * zoomLevel;
|
Chris@0
|
292
|
Chris@0
|
293 // std::cerr << "TimeRulerLayer::paint: calling paint.save()" << std::endl;
|
Chris@0
|
294 paint.save();
|
Chris@44
|
295 //!!! paint.setClipRect(v->rect());
|
Chris@0
|
296
|
Chris@0
|
297 int minPixelSpacing = 50;
|
Chris@0
|
298
|
Chris@0
|
299 bool quarter = false;
|
Chris@271
|
300 int incms = getMajorTickSpacing(v, quarter);
|
Chris@0
|
301
|
Chris@0
|
302 RealTime rt = RealTime::frame2RealTime(rectStart, sampleRate);
|
Chris@0
|
303 long ms = rt.sec * 1000 + rt.msec();
|
Chris@0
|
304 ms = (ms / incms) * incms - incms;
|
Chris@0
|
305
|
Chris@30
|
306 RealTime incRt = RealTime::fromMilliseconds(incms);
|
Chris@0
|
307 long incFrame = RealTime::realTime2Frame(incRt, sampleRate);
|
Chris@0
|
308 int incX = incFrame / zoomLevel;
|
Chris@0
|
309 int ticks = 10;
|
Chris@0
|
310 if (incX < minPixelSpacing * 2) {
|
Chris@0
|
311 ticks = quarter ? 4 : 5;
|
Chris@0
|
312 }
|
Chris@0
|
313
|
Chris@0
|
314 QRect oldClipRect = rect;
|
Chris@0
|
315 QRect newClipRect(oldClipRect.x() - 25, oldClipRect.y(),
|
Chris@0
|
316 oldClipRect.width() + 50, oldClipRect.height());
|
Chris@0
|
317 paint.setClipRect(newClipRect);
|
Chris@55
|
318 paint.setClipRect(rect);
|
Chris@0
|
319
|
Chris@0
|
320 QColor greyColour(m_colour);
|
Chris@0
|
321 if (m_colour == Qt::black) {
|
Chris@0
|
322 greyColour = QColor(200,200,200);
|
Chris@0
|
323 } else {
|
Chris@0
|
324 greyColour = m_colour.light(150);
|
Chris@0
|
325 }
|
Chris@0
|
326
|
Chris@0
|
327 while (1) {
|
Chris@0
|
328
|
Chris@30
|
329 rt = RealTime::fromMilliseconds(ms);
|
Chris@0
|
330 ms += incms;
|
Chris@0
|
331
|
Chris@0
|
332 long frame = RealTime::realTime2Frame(rt, sampleRate);
|
Chris@0
|
333 if (frame >= rectEnd) break;
|
Chris@0
|
334
|
Chris@0
|
335 int x = (frame - startFrame) / zoomLevel;
|
Chris@0
|
336 if (x < rect.x() || x >= rect.x() + rect.width()) continue;
|
Chris@0
|
337
|
Chris@0
|
338 paint.setPen(greyColour);
|
Chris@44
|
339 paint.drawLine(x, 0, x, v->height());
|
Chris@0
|
340
|
Chris@0
|
341 paint.setPen(m_colour);
|
Chris@0
|
342 paint.drawLine(x, 0, x, 5);
|
Chris@44
|
343 paint.drawLine(x, v->height() - 6, x, v->height() - 1);
|
Chris@0
|
344
|
Chris@0
|
345 QString text(QString::fromStdString(rt.toText()));
|
Chris@0
|
346
|
Chris@0
|
347 int y;
|
Chris@0
|
348 QFontMetrics metrics = paint.fontMetrics();
|
Chris@0
|
349 switch (m_labelHeight) {
|
Chris@0
|
350 default:
|
Chris@0
|
351 case LabelTop:
|
Chris@0
|
352 y = 6 + metrics.ascent();
|
Chris@0
|
353 break;
|
Chris@0
|
354 case LabelMiddle:
|
Chris@44
|
355 y = v->height() / 2 - metrics.height() / 2 + metrics.ascent();
|
Chris@0
|
356 break;
|
Chris@0
|
357 case LabelBottom:
|
Chris@44
|
358 y = v->height() - metrics.height() + metrics.ascent() - 6;
|
Chris@0
|
359 }
|
Chris@0
|
360
|
Chris@0
|
361 int tw = metrics.width(text);
|
Chris@0
|
362
|
Chris@70
|
363 if (v->getViewManager() && v->getViewManager()->getOverlayMode() !=
|
Chris@70
|
364 ViewManager::NoOverlays) {
|
Chris@70
|
365
|
Chris@70
|
366 if (v->getLayer(0) == this) {
|
Chris@70
|
367 // backmost layer, don't worry about outlining the text
|
Chris@70
|
368 paint.drawText(x+2 - tw/2, y, text);
|
Chris@70
|
369 } else {
|
Chris@70
|
370 v->drawVisibleText(paint, x+2 - tw/2, y, text, View::OutlinedText);
|
Chris@70
|
371 }
|
Chris@70
|
372 }
|
Chris@0
|
373
|
Chris@0
|
374 paint.setPen(greyColour);
|
Chris@0
|
375
|
Chris@0
|
376 for (int i = 1; i < ticks; ++i) {
|
Chris@0
|
377 rt = rt + (incRt / ticks);
|
Chris@0
|
378 frame = RealTime::realTime2Frame(rt, sampleRate);
|
Chris@0
|
379 x = (frame - startFrame) / zoomLevel;
|
Chris@0
|
380 int sz = 5;
|
Chris@0
|
381 if (ticks == 10) {
|
Chris@0
|
382 if ((i % 2) == 1) {
|
Chris@0
|
383 if (i == 5) {
|
Chris@44
|
384 paint.drawLine(x, 0, x, v->height());
|
Chris@0
|
385 } else sz = 3;
|
Chris@0
|
386 } else {
|
Chris@0
|
387 sz = 7;
|
Chris@0
|
388 }
|
Chris@0
|
389 }
|
Chris@0
|
390 paint.drawLine(x, 0, x, sz);
|
Chris@44
|
391 paint.drawLine(x, v->height() - sz - 1, x, v->height() - 1);
|
Chris@0
|
392 }
|
Chris@0
|
393 }
|
Chris@0
|
394
|
Chris@0
|
395 paint.restore();
|
Chris@0
|
396 }
|
Chris@0
|
397
|
Chris@6
|
398 QString
|
Chris@6
|
399 TimeRulerLayer::toXmlString(QString indent, QString extraAttributes) const
|
Chris@6
|
400 {
|
Chris@6
|
401 return Layer::toXmlString(indent, extraAttributes +
|
Chris@6
|
402 QString(" colour=\"%1\"").arg(encodeColour(m_colour)));
|
Chris@6
|
403 }
|
Chris@6
|
404
|
Chris@11
|
405 void
|
Chris@11
|
406 TimeRulerLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@11
|
407 {
|
Chris@11
|
408 QString colourSpec = attributes.value("colour");
|
Chris@11
|
409 if (colourSpec != "") {
|
Chris@11
|
410 QColor colour(colourSpec);
|
Chris@11
|
411 if (colour.isValid()) {
|
Chris@11
|
412 setBaseColour(QColor(colourSpec));
|
Chris@11
|
413 }
|
Chris@11
|
414 }
|
Chris@11
|
415 }
|
Chris@11
|
416
|