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 "TimeValueLayer.h"
|
Chris@0
|
17
|
Chris@0
|
18 #include "base/Model.h"
|
Chris@0
|
19 #include "base/RealTime.h"
|
Chris@0
|
20 #include "base/Profiler.h"
|
Chris@0
|
21 #include "base/View.h"
|
Chris@0
|
22
|
Chris@0
|
23 #include "model/SparseTimeValueModel.h"
|
Chris@0
|
24
|
Chris@70
|
25 #include "widgets/ItemEditDialog.h"
|
Chris@70
|
26
|
Chris@66
|
27 #include "SpectrogramLayer.h" // for optional frequency alignment
|
Chris@66
|
28
|
Chris@0
|
29 #include <QPainter>
|
Chris@6
|
30 #include <QPainterPath>
|
Chris@21
|
31 #include <QMouseEvent>
|
Chris@0
|
32
|
Chris@0
|
33 #include <iostream>
|
Chris@0
|
34 #include <cmath>
|
Chris@0
|
35
|
Chris@44
|
36 TimeValueLayer::TimeValueLayer() :
|
Chris@44
|
37 Layer(),
|
Chris@0
|
38 m_model(0),
|
Chris@21
|
39 m_editing(false),
|
Chris@23
|
40 m_originalPoint(0, 0.0, tr("New Point")),
|
Chris@21
|
41 m_editingPoint(0, 0.0, tr("New Point")),
|
Chris@22
|
42 m_editingCommand(0),
|
Chris@67
|
43 m_colour(Qt::darkGreen),
|
Chris@66
|
44 m_plotStyle(PlotConnectedPoints),
|
Chris@66
|
45 m_verticalScale(LinearScale)
|
Chris@0
|
46 {
|
Chris@44
|
47
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 void
|
Chris@0
|
51 TimeValueLayer::setModel(SparseTimeValueModel *model)
|
Chris@0
|
52 {
|
Chris@0
|
53 if (m_model == model) return;
|
Chris@0
|
54 m_model = model;
|
Chris@0
|
55
|
Chris@0
|
56 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
|
Chris@0
|
57 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
|
Chris@0
|
58 this, SIGNAL(modelChanged(size_t, size_t)));
|
Chris@0
|
59
|
Chris@0
|
60 connect(m_model, SIGNAL(completionChanged()),
|
Chris@0
|
61 this, SIGNAL(modelCompletionChanged()));
|
Chris@0
|
62
|
Chris@0
|
63 std::cerr << "TimeValueLayer::setModel(" << model << ")" << std::endl;
|
Chris@0
|
64
|
Chris@0
|
65 emit modelReplaced();
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 Layer::PropertyList
|
Chris@0
|
69 TimeValueLayer::getProperties() const
|
Chris@0
|
70 {
|
Chris@0
|
71 PropertyList list;
|
Chris@87
|
72 list.push_back("Colour");
|
Chris@87
|
73 list.push_back("Plot Type");
|
Chris@87
|
74 list.push_back("Vertical Scale");
|
Chris@0
|
75 return list;
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@87
|
78 QString
|
Chris@87
|
79 TimeValueLayer::getPropertyLabel(const PropertyName &name) const
|
Chris@87
|
80 {
|
Chris@87
|
81 if (name == "Colour") return tr("Colour");
|
Chris@87
|
82 if (name == "Plot Type") return tr("Plot Type");
|
Chris@87
|
83 if (name == "Vertical Scale") return tr("Vertical Scale");
|
Chris@87
|
84 return "";
|
Chris@87
|
85 }
|
Chris@87
|
86
|
Chris@0
|
87 Layer::PropertyType
|
Chris@0
|
88 TimeValueLayer::getPropertyType(const PropertyName &name) const
|
Chris@0
|
89 {
|
Chris@0
|
90 return ValueProperty;
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 int
|
Chris@0
|
94 TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@0
|
95 int *min, int *max) const
|
Chris@0
|
96 {
|
Chris@0
|
97 //!!! factor this colour handling stuff out into a colour manager class
|
Chris@0
|
98
|
Chris@0
|
99 int deft = 0;
|
Chris@0
|
100
|
Chris@87
|
101 if (name == "Colour") {
|
Chris@0
|
102
|
Chris@10
|
103 if (min) *min = 0;
|
Chris@10
|
104 if (max) *max = 5;
|
Chris@0
|
105
|
Chris@0
|
106 if (m_colour == Qt::black) deft = 0;
|
Chris@0
|
107 else if (m_colour == Qt::darkRed) deft = 1;
|
Chris@0
|
108 else if (m_colour == Qt::darkBlue) deft = 2;
|
Chris@0
|
109 else if (m_colour == Qt::darkGreen) deft = 3;
|
Chris@0
|
110 else if (m_colour == QColor(200, 50, 255)) deft = 4;
|
Chris@0
|
111 else if (m_colour == QColor(255, 150, 50)) deft = 5;
|
Chris@0
|
112
|
Chris@87
|
113 } else if (name == "Plot Type") {
|
Chris@0
|
114
|
Chris@10
|
115 if (min) *min = 0;
|
Chris@26
|
116 if (max) *max = 5;
|
Chris@0
|
117
|
Chris@0
|
118 deft = int(m_plotStyle);
|
Chris@0
|
119
|
Chris@87
|
120 } else if (name == "Vertical Scale") {
|
Chris@66
|
121
|
Chris@66
|
122 if (min) *min = 0;
|
Chris@66
|
123 if (max) *max = 3;
|
Chris@66
|
124
|
Chris@66
|
125 deft = int(m_verticalScale);
|
Chris@66
|
126
|
Chris@0
|
127 } else {
|
Chris@0
|
128
|
Chris@0
|
129 deft = Layer::getPropertyRangeAndValue(name, min, max);
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 return deft;
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 QString
|
Chris@0
|
136 TimeValueLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@0
|
137 int value) const
|
Chris@0
|
138 {
|
Chris@87
|
139 if (name == "Colour") {
|
Chris@0
|
140 switch (value) {
|
Chris@0
|
141 default:
|
Chris@0
|
142 case 0: return tr("Black");
|
Chris@0
|
143 case 1: return tr("Red");
|
Chris@0
|
144 case 2: return tr("Blue");
|
Chris@0
|
145 case 3: return tr("Green");
|
Chris@0
|
146 case 4: return tr("Purple");
|
Chris@0
|
147 case 5: return tr("Orange");
|
Chris@0
|
148 }
|
Chris@87
|
149 } else if (name == "Plot Type") {
|
Chris@0
|
150 switch (value) {
|
Chris@0
|
151 default:
|
Chris@0
|
152 case 0: return tr("Points");
|
Chris@0
|
153 case 1: return tr("Stems");
|
Chris@6
|
154 case 2: return tr("Connected Points");
|
Chris@6
|
155 case 3: return tr("Lines");
|
Chris@6
|
156 case 4: return tr("Curve");
|
Chris@26
|
157 case 5: return tr("Segmentation");
|
Chris@0
|
158 }
|
Chris@87
|
159 } else if (name == "Vertical Scale") {
|
Chris@66
|
160 switch (value) {
|
Chris@66
|
161 default:
|
Chris@66
|
162 case 0: return tr("Linear Scale");
|
Chris@66
|
163 case 1: return tr("Log Scale");
|
Chris@66
|
164 case 2: return tr("+/-1 Scale");
|
Chris@66
|
165 case 3: return tr("Frequency Scale");
|
Chris@66
|
166 }
|
Chris@0
|
167 }
|
Chris@0
|
168 return tr("<unknown>");
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 void
|
Chris@0
|
172 TimeValueLayer::setProperty(const PropertyName &name, int value)
|
Chris@0
|
173 {
|
Chris@87
|
174 if (name == "Colour") {
|
Chris@0
|
175 switch (value) {
|
Chris@0
|
176 default:
|
Chris@0
|
177 case 0: setBaseColour(Qt::black); break;
|
Chris@0
|
178 case 1: setBaseColour(Qt::darkRed); break;
|
Chris@0
|
179 case 2: setBaseColour(Qt::darkBlue); break;
|
Chris@0
|
180 case 3: setBaseColour(Qt::darkGreen); break;
|
Chris@0
|
181 case 4: setBaseColour(QColor(200, 50, 255)); break;
|
Chris@0
|
182 case 5: setBaseColour(QColor(255, 150, 50)); break;
|
Chris@0
|
183 }
|
Chris@87
|
184 } else if (name == "Plot Type") {
|
Chris@0
|
185 setPlotStyle(PlotStyle(value));
|
Chris@87
|
186 } else if (name == "Vertical Scale") {
|
Chris@66
|
187 setVerticalScale(VerticalScale(value));
|
Chris@0
|
188 }
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 void
|
Chris@0
|
192 TimeValueLayer::setBaseColour(QColor colour)
|
Chris@0
|
193 {
|
Chris@0
|
194 if (m_colour == colour) return;
|
Chris@0
|
195 m_colour = colour;
|
Chris@0
|
196 emit layerParametersChanged();
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 void
|
Chris@0
|
200 TimeValueLayer::setPlotStyle(PlotStyle style)
|
Chris@0
|
201 {
|
Chris@0
|
202 if (m_plotStyle == style) return;
|
Chris@0
|
203 m_plotStyle = style;
|
Chris@0
|
204 emit layerParametersChanged();
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@66
|
207 void
|
Chris@66
|
208 TimeValueLayer::setVerticalScale(VerticalScale scale)
|
Chris@66
|
209 {
|
Chris@66
|
210 if (m_verticalScale == scale) return;
|
Chris@66
|
211 m_verticalScale = scale;
|
Chris@66
|
212 emit layerParametersChanged();
|
Chris@66
|
213 }
|
Chris@66
|
214
|
Chris@0
|
215 bool
|
Chris@44
|
216 TimeValueLayer::isLayerScrollable(const View *v) const
|
Chris@0
|
217 {
|
Chris@6
|
218 // We don't illuminate sections in the line or curve modes, so
|
Chris@6
|
219 // they're always scrollable
|
Chris@6
|
220
|
Chris@6
|
221 if (m_plotStyle == PlotLines ||
|
Chris@6
|
222 m_plotStyle == PlotCurve) return true;
|
Chris@6
|
223
|
Chris@0
|
224 QPoint discard;
|
Chris@44
|
225 return !v->shouldIlluminateLocalFeatures(this, discard);
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@79
|
228 bool
|
Chris@79
|
229 TimeValueLayer::getValueExtents(float &min, float &max, QString &unit) const
|
Chris@79
|
230 {
|
Chris@79
|
231 min = m_model->getValueMinimum();
|
Chris@79
|
232 max = m_model->getValueMaximum();
|
Chris@79
|
233 unit = m_model->getScaleUnits();
|
Chris@79
|
234 return true;
|
Chris@79
|
235 }
|
Chris@79
|
236
|
Chris@0
|
237 SparseTimeValueModel::PointList
|
Chris@44
|
238 TimeValueLayer::getLocalPoints(View *v, int x) const
|
Chris@0
|
239 {
|
Chris@0
|
240 if (!m_model) return SparseTimeValueModel::PointList();
|
Chris@0
|
241
|
Chris@44
|
242 long frame = v->getFrameForX(x);
|
Chris@0
|
243
|
Chris@0
|
244 SparseTimeValueModel::PointList onPoints =
|
Chris@0
|
245 m_model->getPoints(frame);
|
Chris@0
|
246
|
Chris@0
|
247 if (!onPoints.empty()) {
|
Chris@0
|
248 return onPoints;
|
Chris@0
|
249 }
|
Chris@0
|
250
|
Chris@0
|
251 SparseTimeValueModel::PointList prevPoints =
|
Chris@0
|
252 m_model->getPreviousPoints(frame);
|
Chris@0
|
253 SparseTimeValueModel::PointList nextPoints =
|
Chris@0
|
254 m_model->getNextPoints(frame);
|
Chris@0
|
255
|
Chris@0
|
256 SparseTimeValueModel::PointList usePoints = prevPoints;
|
Chris@0
|
257
|
Chris@0
|
258 if (prevPoints.empty()) {
|
Chris@0
|
259 usePoints = nextPoints;
|
Chris@44
|
260 } else if (prevPoints.begin()->frame < v->getStartFrame() &&
|
Chris@44
|
261 !(nextPoints.begin()->frame > v->getEndFrame())) {
|
Chris@0
|
262 usePoints = nextPoints;
|
Chris@0
|
263 } else if (nextPoints.begin()->frame - frame <
|
Chris@0
|
264 frame - prevPoints.begin()->frame) {
|
Chris@0
|
265 usePoints = nextPoints;
|
Chris@0
|
266 }
|
Chris@0
|
267
|
Chris@28
|
268 if (!usePoints.empty()) {
|
Chris@28
|
269 int fuzz = 2;
|
Chris@44
|
270 int px = v->getXForFrame(usePoints.begin()->frame);
|
Chris@28
|
271 if ((px > x && px - x > fuzz) ||
|
Chris@28
|
272 (px < x && x - px > fuzz + 1)) {
|
Chris@28
|
273 usePoints.clear();
|
Chris@28
|
274 }
|
Chris@28
|
275 }
|
Chris@28
|
276
|
Chris@0
|
277 return usePoints;
|
Chris@0
|
278 }
|
Chris@0
|
279
|
Chris@25
|
280 QString
|
Chris@44
|
281 TimeValueLayer::getFeatureDescription(View *v, QPoint &pos) const
|
Chris@0
|
282 {
|
Chris@25
|
283 int x = pos.x();
|
Chris@0
|
284
|
Chris@25
|
285 if (!m_model || !m_model->getSampleRate()) return "";
|
Chris@0
|
286
|
Chris@44
|
287 SparseTimeValueModel::PointList points = getLocalPoints(v, x);
|
Chris@0
|
288
|
Chris@0
|
289 if (points.empty()) {
|
Chris@0
|
290 if (!m_model->isReady()) {
|
Chris@25
|
291 return tr("In progress");
|
Chris@25
|
292 } else {
|
Chris@25
|
293 return tr("No local points");
|
Chris@0
|
294 }
|
Chris@0
|
295 }
|
Chris@0
|
296
|
Chris@0
|
297 long useFrame = points.begin()->frame;
|
Chris@0
|
298
|
Chris@0
|
299 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
|
Chris@25
|
300
|
Chris@25
|
301 QString text;
|
Chris@0
|
302
|
Chris@25
|
303 if (points.begin()->label == "") {
|
Chris@25
|
304 text = QString(tr("Time:\t%1\nValue:\t%2\nNo label"))
|
Chris@25
|
305 .arg(rt.toText(true).c_str())
|
Chris@25
|
306 .arg(points.begin()->value);
|
Chris@25
|
307 } else {
|
Chris@25
|
308 text = QString(tr("Time:\t%1\nValue:\t%2\nLabel:\t%3"))
|
Chris@25
|
309 .arg(rt.toText(true).c_str())
|
Chris@25
|
310 .arg(points.begin()->value)
|
Chris@25
|
311 .arg(points.begin()->label);
|
Chris@25
|
312 }
|
Chris@0
|
313
|
Chris@44
|
314 pos = QPoint(v->getXForFrame(useFrame),
|
Chris@44
|
315 getYForValue(v, points.begin()->value));
|
Chris@25
|
316 return text;
|
Chris@0
|
317 }
|
Chris@0
|
318
|
Chris@28
|
319 bool
|
Chris@44
|
320 TimeValueLayer::snapToFeatureFrame(View *v, int &frame,
|
Chris@28
|
321 size_t &resolution,
|
Chris@28
|
322 SnapType snap) const
|
Chris@13
|
323 {
|
Chris@13
|
324 if (!m_model) {
|
Chris@44
|
325 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
|
Chris@13
|
326 }
|
Chris@13
|
327
|
Chris@13
|
328 resolution = m_model->getResolution();
|
Chris@28
|
329 SparseTimeValueModel::PointList points;
|
Chris@13
|
330
|
Chris@28
|
331 if (snap == SnapNeighbouring) {
|
Chris@28
|
332
|
Chris@44
|
333 points = getLocalPoints(v, v->getXForFrame(frame));
|
Chris@28
|
334 if (points.empty()) return false;
|
Chris@28
|
335 frame = points.begin()->frame;
|
Chris@28
|
336 return true;
|
Chris@28
|
337 }
|
Chris@28
|
338
|
Chris@28
|
339 points = m_model->getPoints(frame, frame);
|
Chris@28
|
340 int snapped = frame;
|
Chris@28
|
341 bool found = false;
|
Chris@13
|
342
|
Chris@13
|
343 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
|
Chris@13
|
344 i != points.end(); ++i) {
|
Chris@13
|
345
|
Chris@28
|
346 if (snap == SnapRight) {
|
Chris@28
|
347
|
Chris@13
|
348 if (i->frame > frame) {
|
Chris@28
|
349 snapped = i->frame;
|
Chris@28
|
350 found = true;
|
Chris@13
|
351 break;
|
Chris@13
|
352 }
|
Chris@28
|
353
|
Chris@28
|
354 } else if (snap == SnapLeft) {
|
Chris@28
|
355
|
Chris@13
|
356 if (i->frame <= frame) {
|
Chris@28
|
357 snapped = i->frame;
|
Chris@28
|
358 found = true; // don't break, as the next may be better
|
Chris@28
|
359 } else {
|
Chris@28
|
360 break;
|
Chris@28
|
361 }
|
Chris@28
|
362
|
Chris@28
|
363 } else { // nearest
|
Chris@28
|
364
|
Chris@28
|
365 SparseTimeValueModel::PointList::const_iterator j = i;
|
Chris@28
|
366 ++j;
|
Chris@28
|
367
|
Chris@28
|
368 if (j == points.end()) {
|
Chris@28
|
369
|
Chris@28
|
370 snapped = i->frame;
|
Chris@28
|
371 found = true;
|
Chris@28
|
372 break;
|
Chris@28
|
373
|
Chris@28
|
374 } else if (j->frame >= frame) {
|
Chris@28
|
375
|
Chris@28
|
376 if (j->frame - frame < frame - i->frame) {
|
Chris@28
|
377 snapped = j->frame;
|
Chris@28
|
378 } else {
|
Chris@28
|
379 snapped = i->frame;
|
Chris@28
|
380 }
|
Chris@28
|
381 found = true;
|
Chris@28
|
382 break;
|
Chris@13
|
383 }
|
Chris@13
|
384 }
|
Chris@13
|
385 }
|
Chris@13
|
386
|
Chris@28
|
387 frame = snapped;
|
Chris@28
|
388 return found;
|
Chris@13
|
389 }
|
Chris@13
|
390
|
Chris@21
|
391 int
|
Chris@66
|
392 TimeValueLayer::getYForValue(View *v, float val) const
|
Chris@21
|
393 {
|
Chris@79
|
394 float min = 0.0, max = 0.0;
|
Chris@79
|
395 int h = v->height();
|
Chris@79
|
396
|
Chris@79
|
397 if (!v->getValueExtents(m_model->getScaleUnits(), min, max)) {
|
Chris@79
|
398 min = m_model->getValueMinimum();
|
Chris@79
|
399 max = m_model->getValueMaximum();
|
Chris@79
|
400 }
|
Chris@79
|
401
|
Chris@79
|
402 if (max == min) max = min + 1.0;
|
Chris@79
|
403
|
Chris@79
|
404 /*!!!
|
Chris@21
|
405 float min = m_model->getValueMinimum();
|
Chris@21
|
406 float max = m_model->getValueMaximum();
|
Chris@21
|
407 if (max == min) max = min + 1.0;
|
Chris@21
|
408
|
Chris@44
|
409 int h = v->height();
|
Chris@21
|
410
|
Chris@66
|
411 if (m_verticalScale == FrequencyScale || m_verticalScale == LogScale) {
|
Chris@66
|
412
|
Chris@66
|
413 if (m_verticalScale == FrequencyScale) {
|
Chris@66
|
414 // If we have a spectrogram layer on the same view as us, align
|
Chris@66
|
415 // ourselves with it...
|
Chris@66
|
416 for (int i = 0; i < v->getLayerCount(); ++i) {
|
Chris@66
|
417 SpectrogramLayer *spectrogram = dynamic_cast<SpectrogramLayer *>
|
Chris@66
|
418 (v->getLayer(i));
|
Chris@66
|
419 if (spectrogram) {
|
Chris@66
|
420 return spectrogram->getYForFrequency(v, val);
|
Chris@66
|
421 }
|
Chris@66
|
422 }
|
Chris@66
|
423 }
|
Chris@66
|
424
|
Chris@66
|
425 min = (min < 0.0) ? -log10(-min) : (min == 0.0) ? 0.0 : log10(min);
|
Chris@66
|
426 max = (max < 0.0) ? -log10(-max) : (max == 0.0) ? 0.0 : log10(max);
|
Chris@66
|
427 val = (val < 0.0) ? -log10(-val) : (val == 0.0) ? 0.0 : log10(val);
|
Chris@66
|
428
|
Chris@66
|
429 } else if (m_verticalScale == PlusMinusOneScale) {
|
Chris@66
|
430 min = -1.0;
|
Chris@66
|
431 max = 1.0;
|
Chris@66
|
432 }
|
Chris@79
|
433 */
|
Chris@66
|
434
|
Chris@66
|
435 return int(h - ((val - min) * h) / (max - min));
|
Chris@21
|
436 }
|
Chris@21
|
437
|
Chris@21
|
438 float
|
Chris@44
|
439 TimeValueLayer::getValueForY(View *v, int y) const
|
Chris@21
|
440 {
|
Chris@66
|
441 //!!!
|
Chris@66
|
442
|
Chris@21
|
443 float min = m_model->getValueMinimum();
|
Chris@21
|
444 float max = m_model->getValueMaximum();
|
Chris@21
|
445 if (max == min) max = min + 1.0;
|
Chris@21
|
446
|
Chris@44
|
447 int h = v->height();
|
Chris@21
|
448
|
Chris@21
|
449 return min + (float(h - y) * float(max - min)) / h;
|
Chris@21
|
450 }
|
Chris@21
|
451
|
Chris@68
|
452 QColor
|
Chris@68
|
453 TimeValueLayer::getColourForValue(float val) const
|
Chris@68
|
454 {
|
Chris@68
|
455 float min = m_model->getValueMinimum();
|
Chris@68
|
456 float max = m_model->getValueMaximum();
|
Chris@68
|
457 if (max == min) max = min + 1.0;
|
Chris@68
|
458
|
Chris@68
|
459 if (m_verticalScale == FrequencyScale || m_verticalScale == LogScale) {
|
Chris@68
|
460 min = (min < 0.0) ? -log10(-min) : (min == 0.0) ? 0.0 : log10(min);
|
Chris@68
|
461 max = (max < 0.0) ? -log10(-max) : (max == 0.0) ? 0.0 : log10(max);
|
Chris@68
|
462 val = (val < 0.0) ? -log10(-val) : (val == 0.0) ? 0.0 : log10(val);
|
Chris@68
|
463 } else if (m_verticalScale == PlusMinusOneScale) {
|
Chris@68
|
464 min = -1.0;
|
Chris@68
|
465 max = 1.0;
|
Chris@68
|
466 }
|
Chris@68
|
467
|
Chris@68
|
468 int iv = ((val - min) / (max - min)) * 255.999;
|
Chris@68
|
469
|
Chris@68
|
470 QColor colour = QColor::fromHsv(256 - iv, iv / 2 + 128, iv);
|
Chris@68
|
471 return QColor(colour.red(), colour.green(), colour.blue(), 120);
|
Chris@68
|
472 }
|
Chris@68
|
473
|
Chris@0
|
474 void
|
Chris@44
|
475 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const
|
Chris@0
|
476 {
|
Chris@0
|
477 if (!m_model || !m_model->isOK()) return;
|
Chris@0
|
478
|
Chris@0
|
479 int sampleRate = m_model->getSampleRate();
|
Chris@0
|
480 if (!sampleRate) return;
|
Chris@0
|
481
|
Chris@0
|
482 // Profiler profiler("TimeValueLayer::paint", true);
|
Chris@0
|
483
|
Chris@0
|
484 int x0 = rect.left(), x1 = rect.right();
|
Chris@44
|
485 long frame0 = v->getFrameForX(x0);
|
Chris@44
|
486 long frame1 = v->getFrameForX(x1);
|
Chris@0
|
487
|
Chris@0
|
488 SparseTimeValueModel::PointList points(m_model->getPoints
|
Chris@0
|
489 (frame0, frame1));
|
Chris@11
|
490 if (points.empty()) return;
|
Chris@0
|
491
|
Chris@0
|
492 paint.setPen(m_colour);
|
Chris@0
|
493
|
Chris@0
|
494 QColor brushColour(m_colour);
|
Chris@0
|
495 brushColour.setAlpha(80);
|
Chris@0
|
496 paint.setBrush(brushColour);
|
Chris@0
|
497
|
Chris@0
|
498 // std::cerr << "TimeValueLayer::paint: resolution is "
|
Chris@0
|
499 // << m_model->getResolution() << " frames" << std::endl;
|
Chris@0
|
500
|
Chris@0
|
501 float min = m_model->getValueMinimum();
|
Chris@0
|
502 float max = m_model->getValueMaximum();
|
Chris@0
|
503 if (max == min) max = min + 1.0;
|
Chris@0
|
504
|
Chris@44
|
505 int origin = int(nearbyint(v->height() -
|
Chris@44
|
506 (-min * v->height()) / (max - min)));
|
Chris@0
|
507
|
Chris@0
|
508 QPoint localPos;
|
Chris@0
|
509 long illuminateFrame = -1;
|
Chris@0
|
510
|
Chris@44
|
511 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
|
Chris@0
|
512 SparseTimeValueModel::PointList localPoints =
|
Chris@44
|
513 getLocalPoints(v, localPos.x());
|
Chris@0
|
514 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
|
Chris@0
|
515 }
|
Chris@6
|
516
|
Chris@20
|
517 int w =
|
Chris@44
|
518 v->getXForFrame(frame0 + m_model->getResolution()) -
|
Chris@44
|
519 v->getXForFrame(frame0);
|
Chris@7
|
520
|
Chris@6
|
521 paint.save();
|
Chris@6
|
522
|
Chris@6
|
523 QPainterPath path;
|
Chris@55
|
524 int pointCount = 0;
|
Chris@79
|
525
|
Chris@79
|
526 int textY = 0;
|
Chris@79
|
527 if (m_plotStyle == PlotSegmentation) {
|
Chris@79
|
528 textY = v->getTextLabelHeight(this, paint);
|
Chris@79
|
529 }
|
Chris@6
|
530
|
Chris@0
|
531 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
|
Chris@0
|
532 i != points.end(); ++i) {
|
Chris@0
|
533
|
Chris@0
|
534 const SparseTimeValueModel::Point &p(*i);
|
Chris@0
|
535
|
Chris@44
|
536 int x = v->getXForFrame(p.frame);
|
Chris@44
|
537 int y = getYForValue(v, p.value);
|
Chris@0
|
538
|
Chris@79
|
539 if (m_plotStyle != PlotSegmentation) {
|
Chris@79
|
540 textY = y - paint.fontMetrics().height()
|
Chris@79
|
541 + paint.fontMetrics().ascent();
|
Chris@79
|
542 }
|
Chris@79
|
543
|
Chris@34
|
544 bool haveNext = false;
|
Chris@76
|
545 int nx = v->getXForFrame(v->getModelsEndFrame());
|
Chris@76
|
546 // m_model->getEndFrame());
|
Chris@34
|
547 int ny = y;
|
Chris@34
|
548
|
Chris@34
|
549 SparseTimeValueModel::PointList::const_iterator j = i;
|
Chris@34
|
550 ++j;
|
Chris@34
|
551
|
Chris@34
|
552 if (j != points.end()) {
|
Chris@34
|
553 const SparseTimeValueModel::Point &q(*j);
|
Chris@44
|
554 nx = v->getXForFrame(q.frame);
|
Chris@44
|
555 ny = getYForValue(v, q.value);
|
Chris@34
|
556 haveNext = true;
|
Chris@76
|
557 }
|
Chris@76
|
558
|
Chris@76
|
559 // std::cout << "frame = " << p.frame << ", x = " << x << ", haveNext = " << haveNext
|
Chris@76
|
560 // << ", nx = " << nx << std::endl;
|
Chris@34
|
561
|
Chris@34
|
562 int labelY = y;
|
Chris@34
|
563
|
Chris@0
|
564 if (w < 1) w = 1;
|
Chris@26
|
565 paint.setPen(m_colour);
|
Chris@0
|
566
|
Chris@26
|
567 if (m_plotStyle == PlotSegmentation) {
|
Chris@68
|
568 paint.setBrush(getColourForValue(p.value));
|
Chris@44
|
569 labelY = v->height();
|
Chris@26
|
570 } else if (m_plotStyle == PlotLines ||
|
Chris@26
|
571 m_plotStyle == PlotCurve) {
|
Chris@6
|
572 paint.setBrush(Qt::NoBrush);
|
Chris@3
|
573 } else {
|
Chris@6
|
574 paint.setBrush(brushColour);
|
Chris@3
|
575 }
|
Chris@0
|
576
|
Chris@0
|
577 if (m_plotStyle == PlotStems) {
|
Chris@0
|
578 paint.setPen(brushColour);
|
Chris@0
|
579 if (y < origin - 1) {
|
Chris@0
|
580 paint.drawRect(x + w/2, y + 1, 1, origin - y);
|
Chris@0
|
581 } else if (y > origin + 1) {
|
Chris@0
|
582 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
|
Chris@0
|
583 }
|
Chris@0
|
584 paint.setPen(m_colour);
|
Chris@0
|
585 }
|
Chris@0
|
586
|
Chris@0
|
587 if (illuminateFrame == p.frame) {
|
Chris@6
|
588
|
Chris@0
|
589 //!!! aside from the problem of choosing a colour, it'd be
|
Chris@0
|
590 //better to save the highlighted rects and draw them at
|
Chris@0
|
591 //the end perhaps
|
Chris@6
|
592
|
Chris@6
|
593 //!!! not equipped to illuminate the right section in line
|
Chris@6
|
594 //or curve mode
|
Chris@6
|
595
|
Chris@6
|
596 if (m_plotStyle != PlotCurve &&
|
Chris@6
|
597 m_plotStyle != PlotLines) {
|
Chris@6
|
598 paint.setPen(Qt::black);//!!!
|
Chris@26
|
599 if (m_plotStyle != PlotSegmentation) {
|
Chris@26
|
600 paint.setBrush(Qt::black);//!!!
|
Chris@26
|
601 }
|
Chris@6
|
602 }
|
Chris@0
|
603 }
|
Chris@0
|
604
|
Chris@6
|
605 if (m_plotStyle != PlotLines &&
|
Chris@26
|
606 m_plotStyle != PlotCurve &&
|
Chris@26
|
607 m_plotStyle != PlotSegmentation) {
|
Chris@3
|
608 paint.drawRect(x, y - 1, w, 2);
|
Chris@3
|
609 }
|
Chris@0
|
610
|
Chris@6
|
611 if (m_plotStyle == PlotConnectedPoints ||
|
Chris@6
|
612 m_plotStyle == PlotLines ||
|
Chris@6
|
613 m_plotStyle == PlotCurve) {
|
Chris@0
|
614
|
Chris@34
|
615 if (haveNext) {
|
Chris@3
|
616
|
Chris@6
|
617 if (m_plotStyle == PlotConnectedPoints) {
|
Chris@34
|
618
|
Chris@79
|
619 paint.save();
|
Chris@3
|
620 paint.setPen(brushColour);
|
Chris@3
|
621 paint.drawLine(x + w, y, nx, ny);
|
Chris@79
|
622 paint.restore();
|
Chris@6
|
623
|
Chris@6
|
624 } else if (m_plotStyle == PlotLines) {
|
Chris@6
|
625
|
Chris@6
|
626 paint.drawLine(x + w/2, y, nx + w/2, ny);
|
Chris@6
|
627
|
Chris@3
|
628 } else {
|
Chris@6
|
629
|
Chris@55
|
630 float x0 = x + float(w)/2;
|
Chris@55
|
631 float x1 = nx + float(w)/2;
|
Chris@55
|
632
|
Chris@55
|
633 float y0 = y;
|
Chris@55
|
634 float y1 = ny;
|
Chris@55
|
635
|
Chris@55
|
636 if (pointCount == 0) {
|
Chris@55
|
637 path.moveTo((x0 + x1) / 2, (y0 + y1) / 2);
|
Chris@6
|
638 }
|
Chris@55
|
639 ++pointCount;
|
Chris@6
|
640
|
Chris@6
|
641 if (nx - x > 5) {
|
Chris@55
|
642 path.cubicTo(x0, y0,
|
Chris@55
|
643 x0, y0,
|
Chris@55
|
644 (x0 + x1) / 2, (y0 + y1) / 2);
|
Chris@55
|
645
|
Chris@55
|
646 // // or
|
Chris@55
|
647 // path.quadTo(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2);
|
Chris@55
|
648
|
Chris@6
|
649 } else {
|
Chris@55
|
650 path.lineTo((x0 + x1) / 2, (y0 + y1) / 2);
|
Chris@6
|
651 }
|
Chris@3
|
652 }
|
Chris@0
|
653 }
|
Chris@0
|
654 }
|
Chris@0
|
655
|
Chris@26
|
656 if (m_plotStyle == PlotSegmentation) {
|
Chris@76
|
657
|
Chris@76
|
658 // std::cerr << "drawing rect" << std::endl;
|
Chris@26
|
659
|
Chris@27
|
660 if (nx <= x) continue;
|
Chris@26
|
661
|
Chris@28
|
662 if (illuminateFrame != p.frame &&
|
Chris@44
|
663 (nx < x + 5 || x >= v->width() - 1)) {
|
Chris@27
|
664 paint.setPen(Qt::NoPen);
|
Chris@27
|
665 }
|
Chris@26
|
666
|
Chris@44
|
667 paint.drawRect(x, -1, nx - x, v->height() + 1);
|
Chris@26
|
668 }
|
Chris@26
|
669
|
Chris@55
|
670 if (p.label != "") {
|
Chris@79
|
671 if (!haveNext || nx > x + 6 + paint.fontMetrics().width(p.label)) {
|
Chris@79
|
672 paint.drawText(x + 5, textY, p.label);
|
Chris@79
|
673 }
|
Chris@55
|
674 }
|
Chris@0
|
675 }
|
Chris@6
|
676
|
Chris@6
|
677 if (m_plotStyle == PlotCurve && !path.isEmpty()) {
|
Chris@55
|
678 paint.setRenderHint(QPainter::Antialiasing, pointCount <= v->width());
|
Chris@6
|
679 paint.drawPath(path);
|
Chris@6
|
680 }
|
Chris@6
|
681
|
Chris@6
|
682 paint.restore();
|
Chris@6
|
683
|
Chris@6
|
684 // looks like save/restore doesn't deal with this:
|
Chris@6
|
685 paint.setRenderHint(QPainter::Antialiasing, false);
|
Chris@6
|
686 }
|
Chris@6
|
687
|
Chris@42
|
688 int
|
Chris@44
|
689 TimeValueLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
|
Chris@42
|
690 {
|
Chris@68
|
691 int w = paint.fontMetrics().width("-000.000");
|
Chris@68
|
692 if (m_plotStyle == PlotSegmentation) return w + 20;
|
Chris@68
|
693 else return w + 10;
|
Chris@42
|
694 }
|
Chris@42
|
695
|
Chris@42
|
696 void
|
Chris@44
|
697 TimeValueLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
|
Chris@42
|
698 {
|
Chris@42
|
699 if (!m_model) return;
|
Chris@42
|
700
|
Chris@68
|
701 int h = v->height();
|
Chris@68
|
702
|
Chris@68
|
703 int n = 10;
|
Chris@68
|
704
|
Chris@68
|
705 float max = m_model->getValueMaximum();
|
Chris@68
|
706 float min = m_model->getValueMinimum();
|
Chris@68
|
707 float val = min;
|
Chris@68
|
708 float inc = (max - val) / n;
|
Chris@42
|
709
|
Chris@55
|
710 char buffer[40];
|
Chris@55
|
711
|
Chris@55
|
712 int w = getVerticalScaleWidth(v, paint);
|
Chris@55
|
713
|
Chris@68
|
714 int tx = 5;
|
Chris@68
|
715
|
Chris@68
|
716 int boxx = 5, boxy = 5;
|
Chris@68
|
717 if (m_model->getScaleUnits() != "") {
|
Chris@68
|
718 boxy += paint.fontMetrics().height();
|
Chris@68
|
719 }
|
Chris@68
|
720 int boxw = 10, boxh = h - boxy - 5;
|
Chris@68
|
721
|
Chris@68
|
722 if (m_plotStyle == PlotSegmentation) {
|
Chris@68
|
723 tx += boxx + boxw;
|
Chris@68
|
724 paint.drawRect(boxx, boxy, boxw, boxh);
|
Chris@68
|
725 }
|
Chris@68
|
726
|
Chris@68
|
727 if (m_plotStyle == PlotSegmentation) {
|
Chris@68
|
728 paint.save();
|
Chris@68
|
729 for (int y = 0; y < boxh; ++y) {
|
Chris@68
|
730 float val = ((boxh - y) * (max - min)) / boxh + min;
|
Chris@68
|
731 paint.setPen(getColourForValue(val));
|
Chris@68
|
732 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
|
Chris@68
|
733 }
|
Chris@68
|
734 paint.restore();
|
Chris@68
|
735 }
|
Chris@68
|
736
|
Chris@68
|
737 for (int i = 0; i < n; ++i) {
|
Chris@68
|
738
|
Chris@68
|
739 int y, ty;
|
Chris@68
|
740 bool drawText = true;
|
Chris@68
|
741
|
Chris@68
|
742 if (m_plotStyle == PlotSegmentation) {
|
Chris@68
|
743 y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
|
Chris@68
|
744 ty = y;
|
Chris@68
|
745 } else {
|
Chris@68
|
746 if (i == n-1) {
|
Chris@68
|
747 if (m_model->getScaleUnits() != "") drawText = false;
|
Chris@68
|
748 }
|
Chris@68
|
749 y = getYForValue(v, val);
|
Chris@68
|
750 ty = y - paint.fontMetrics().height() +
|
Chris@68
|
751 paint.fontMetrics().ascent();
|
Chris@68
|
752 }
|
Chris@68
|
753
|
Chris@68
|
754 sprintf(buffer, "%.3f", val);
|
Chris@55
|
755 QString label = QString(buffer);
|
Chris@68
|
756
|
Chris@68
|
757 if (m_plotStyle != PlotSegmentation) {
|
Chris@68
|
758 paint.drawLine(w - 5, y, w, y);
|
Chris@68
|
759 } else {
|
Chris@68
|
760 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
|
Chris@68
|
761 }
|
Chris@68
|
762
|
Chris@68
|
763 if (drawText) paint.drawText(tx, ty, label);
|
Chris@44
|
764 val += inc;
|
Chris@42
|
765 }
|
Chris@68
|
766
|
Chris@67
|
767 if (m_model->getScaleUnits() != "") {
|
Chris@67
|
768 paint.drawText(5, 5 + paint.fontMetrics().ascent(),
|
Chris@67
|
769 m_model->getScaleUnits());
|
Chris@67
|
770 }
|
Chris@42
|
771 }
|
Chris@42
|
772
|
Chris@21
|
773 void
|
Chris@44
|
774 TimeValueLayer::drawStart(View *v, QMouseEvent *e)
|
Chris@21
|
775 {
|
Chris@21
|
776 std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@21
|
777
|
Chris@21
|
778 if (!m_model) return;
|
Chris@21
|
779
|
Chris@44
|
780 long frame = v->getFrameForX(e->x());
|
Chris@76
|
781 long resolution = m_model->getResolution();
|
Chris@21
|
782 if (frame < 0) frame = 0;
|
Chris@76
|
783 frame = (frame / resolution) * resolution;
|
Chris@21
|
784
|
Chris@44
|
785 float value = getValueForY(v, e->y());
|
Chris@21
|
786
|
Chris@76
|
787 bool havePoint = false;
|
Chris@76
|
788
|
Chris@76
|
789 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
|
Chris@76
|
790 if (!points.empty()) {
|
Chris@76
|
791 for (SparseTimeValueModel::PointList::iterator i = points.begin();
|
Chris@76
|
792 i != points.end(); ++i) {
|
Chris@76
|
793 if (((i->frame / resolution) * resolution) != frame) {
|
Chris@76
|
794 std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
|
Chris@76
|
795 continue;
|
Chris@76
|
796 }
|
Chris@76
|
797 m_editingPoint = *i;
|
Chris@76
|
798 havePoint = true;
|
Chris@76
|
799 }
|
Chris@76
|
800 }
|
Chris@76
|
801
|
Chris@76
|
802 if (!havePoint) {
|
Chris@76
|
803 m_editingPoint = SparseTimeValueModel::Point
|
Chris@76
|
804 (frame, value, tr("New Point"));
|
Chris@76
|
805 }
|
Chris@76
|
806
|
Chris@23
|
807 m_originalPoint = m_editingPoint;
|
Chris@22
|
808
|
Chris@22
|
809 if (m_editingCommand) m_editingCommand->finish();
|
Chris@22
|
810 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
|
Chris@22
|
811 tr("Draw Point"));
|
Chris@76
|
812 if (!havePoint) {
|
Chris@76
|
813 m_editingCommand->addPoint(m_editingPoint);
|
Chris@76
|
814 }
|
Chris@22
|
815
|
Chris@21
|
816 m_editing = true;
|
Chris@21
|
817 }
|
Chris@21
|
818
|
Chris@21
|
819 void
|
Chris@44
|
820 TimeValueLayer::drawDrag(View *v, QMouseEvent *e)
|
Chris@21
|
821 {
|
Chris@21
|
822 std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@21
|
823
|
Chris@21
|
824 if (!m_model || !m_editing) return;
|
Chris@21
|
825
|
Chris@44
|
826 long frame = v->getFrameForX(e->x());
|
Chris@76
|
827 long resolution = m_model->getResolution();
|
Chris@21
|
828 if (frame < 0) frame = 0;
|
Chris@76
|
829 frame = (frame / resolution) * resolution;
|
Chris@21
|
830
|
Chris@44
|
831 float value = getValueForY(v, e->y());
|
Chris@21
|
832
|
Chris@76
|
833 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
|
Chris@76
|
834
|
Chris@76
|
835 std::cerr << points.size() << " points" << std::endl;
|
Chris@76
|
836
|
Chris@76
|
837 bool havePoint = false;
|
Chris@76
|
838
|
Chris@76
|
839 if (!points.empty()) {
|
Chris@76
|
840 for (SparseTimeValueModel::PointList::iterator i = points.begin();
|
Chris@76
|
841 i != points.end(); ++i) {
|
Chris@76
|
842 if (i->frame == m_editingPoint.frame &&
|
Chris@76
|
843 i->value == m_editingPoint.value) {
|
Chris@76
|
844 std::cerr << "ignoring current editing point at " << i->frame << ", " << i->value << std::endl;
|
Chris@76
|
845 continue;
|
Chris@76
|
846 }
|
Chris@76
|
847 if (((i->frame / resolution) * resolution) != frame) {
|
Chris@76
|
848 std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
|
Chris@76
|
849 continue;
|
Chris@76
|
850 }
|
Chris@76
|
851 std::cerr << "adjusting to new point at " << i->frame << ", " << i->value << std::endl;
|
Chris@76
|
852 m_editingPoint = *i;
|
Chris@76
|
853 m_originalPoint = m_editingPoint;
|
Chris@76
|
854 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@76
|
855 havePoint = true;
|
Chris@76
|
856 }
|
Chris@76
|
857 }
|
Chris@76
|
858
|
Chris@76
|
859 if (!havePoint) {
|
Chris@76
|
860 if (frame == m_editingPoint.frame) {
|
Chris@76
|
861 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@76
|
862 }
|
Chris@76
|
863 }
|
Chris@76
|
864
|
Chris@76
|
865 // m_editingCommand->deletePoint(m_editingPoint);
|
Chris@21
|
866 m_editingPoint.frame = frame;
|
Chris@21
|
867 m_editingPoint.value = value;
|
Chris@22
|
868 m_editingCommand->addPoint(m_editingPoint);
|
Chris@21
|
869 }
|
Chris@21
|
870
|
Chris@21
|
871 void
|
Chris@44
|
872 TimeValueLayer::drawEnd(View *v, QMouseEvent *e)
|
Chris@21
|
873 {
|
Chris@21
|
874 std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@21
|
875 if (!m_model || !m_editing) return;
|
Chris@22
|
876 m_editingCommand->finish();
|
Chris@22
|
877 m_editingCommand = 0;
|
Chris@21
|
878 m_editing = false;
|
Chris@21
|
879 }
|
Chris@21
|
880
|
Chris@21
|
881 void
|
Chris@44
|
882 TimeValueLayer::editStart(View *v, QMouseEvent *e)
|
Chris@21
|
883 {
|
Chris@21
|
884 std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@21
|
885
|
Chris@21
|
886 if (!m_model) return;
|
Chris@21
|
887
|
Chris@44
|
888 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
|
Chris@21
|
889 if (points.empty()) return;
|
Chris@21
|
890
|
Chris@21
|
891 m_editingPoint = *points.begin();
|
Chris@23
|
892 m_originalPoint = m_editingPoint;
|
Chris@22
|
893
|
Chris@22
|
894 if (m_editingCommand) {
|
Chris@22
|
895 m_editingCommand->finish();
|
Chris@22
|
896 m_editingCommand = 0;
|
Chris@22
|
897 }
|
Chris@22
|
898
|
Chris@21
|
899 m_editing = true;
|
Chris@21
|
900 }
|
Chris@21
|
901
|
Chris@21
|
902 void
|
Chris@44
|
903 TimeValueLayer::editDrag(View *v, QMouseEvent *e)
|
Chris@21
|
904 {
|
Chris@21
|
905 std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@21
|
906
|
Chris@21
|
907 if (!m_model || !m_editing) return;
|
Chris@21
|
908
|
Chris@44
|
909 long frame = v->getFrameForX(e->x());
|
Chris@21
|
910 if (frame < 0) frame = 0;
|
Chris@21
|
911 frame = frame / m_model->getResolution() * m_model->getResolution();
|
Chris@21
|
912
|
Chris@44
|
913 float value = getValueForY(v, e->y());
|
Chris@21
|
914
|
Chris@22
|
915 if (!m_editingCommand) {
|
Chris@22
|
916 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
|
Chris@22
|
917 tr("Drag Point"));
|
Chris@22
|
918 }
|
Chris@22
|
919
|
Chris@22
|
920 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@21
|
921 m_editingPoint.frame = frame;
|
Chris@21
|
922 m_editingPoint.value = value;
|
Chris@22
|
923 m_editingCommand->addPoint(m_editingPoint);
|
Chris@21
|
924 }
|
Chris@21
|
925
|
Chris@21
|
926 void
|
Chris@44
|
927 TimeValueLayer::editEnd(View *v, QMouseEvent *e)
|
Chris@21
|
928 {
|
Chris@21
|
929 std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@21
|
930 if (!m_model || !m_editing) return;
|
Chris@23
|
931
|
Chris@23
|
932 if (m_editingCommand) {
|
Chris@23
|
933
|
Chris@23
|
934 QString newName = m_editingCommand->getName();
|
Chris@23
|
935
|
Chris@23
|
936 if (m_editingPoint.frame != m_originalPoint.frame) {
|
Chris@23
|
937 if (m_editingPoint.value != m_originalPoint.value) {
|
Chris@23
|
938 newName = tr("Edit Point");
|
Chris@23
|
939 } else {
|
Chris@23
|
940 newName = tr("Relocate Point");
|
Chris@23
|
941 }
|
Chris@23
|
942 } else {
|
Chris@23
|
943 newName = tr("Change Point Value");
|
Chris@23
|
944 }
|
Chris@23
|
945
|
Chris@23
|
946 m_editingCommand->setName(newName);
|
Chris@23
|
947 m_editingCommand->finish();
|
Chris@23
|
948 }
|
Chris@23
|
949
|
Chris@22
|
950 m_editingCommand = 0;
|
Chris@21
|
951 m_editing = false;
|
Chris@21
|
952 }
|
Chris@21
|
953
|
Chris@43
|
954 void
|
Chris@70
|
955 TimeValueLayer::editOpen(View *v, QMouseEvent *e)
|
Chris@70
|
956 {
|
Chris@70
|
957 if (!m_model) return;
|
Chris@70
|
958
|
Chris@70
|
959 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
|
Chris@70
|
960 if (points.empty()) return;
|
Chris@70
|
961
|
Chris@70
|
962 SparseTimeValueModel::Point point = *points.begin();
|
Chris@70
|
963
|
Chris@70
|
964 ItemEditDialog *dialog = new ItemEditDialog
|
Chris@70
|
965 (m_model->getSampleRate(),
|
Chris@70
|
966 ItemEditDialog::ShowTime |
|
Chris@70
|
967 ItemEditDialog::ShowValue |
|
Chris@73
|
968 ItemEditDialog::ShowText,
|
Chris@73
|
969 m_model->getScaleUnits());
|
Chris@70
|
970
|
Chris@70
|
971 dialog->setFrameTime(point.frame);
|
Chris@70
|
972 dialog->setValue(point.value);
|
Chris@70
|
973 dialog->setText(point.label);
|
Chris@70
|
974
|
Chris@70
|
975 if (dialog->exec() == QDialog::Accepted) {
|
Chris@70
|
976
|
Chris@70
|
977 SparseTimeValueModel::Point newPoint = point;
|
Chris@70
|
978 newPoint.frame = dialog->getFrameTime();
|
Chris@70
|
979 newPoint.value = dialog->getValue();
|
Chris@70
|
980 newPoint.label = dialog->getText();
|
Chris@70
|
981
|
Chris@70
|
982 SparseTimeValueModel::EditCommand *command =
|
Chris@70
|
983 new SparseTimeValueModel::EditCommand(m_model, tr("Edit Point"));
|
Chris@70
|
984 command->deletePoint(point);
|
Chris@70
|
985 command->addPoint(newPoint);
|
Chris@70
|
986 command->finish();
|
Chris@70
|
987 }
|
Chris@70
|
988
|
Chris@70
|
989 delete dialog;
|
Chris@70
|
990 }
|
Chris@70
|
991
|
Chris@70
|
992 void
|
Chris@43
|
993 TimeValueLayer::moveSelection(Selection s, size_t newStartFrame)
|
Chris@43
|
994 {
|
Chris@99
|
995 if (!m_model) return;
|
Chris@99
|
996
|
Chris@43
|
997 SparseTimeValueModel::EditCommand *command =
|
Chris@43
|
998 new SparseTimeValueModel::EditCommand(m_model,
|
Chris@43
|
999 tr("Drag Selection"));
|
Chris@43
|
1000
|
Chris@43
|
1001 SparseTimeValueModel::PointList points =
|
Chris@43
|
1002 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@43
|
1003
|
Chris@43
|
1004 for (SparseTimeValueModel::PointList::iterator i = points.begin();
|
Chris@43
|
1005 i != points.end(); ++i) {
|
Chris@43
|
1006
|
Chris@43
|
1007 if (s.contains(i->frame)) {
|
Chris@43
|
1008 SparseTimeValueModel::Point newPoint(*i);
|
Chris@43
|
1009 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
|
Chris@43
|
1010 command->deletePoint(*i);
|
Chris@43
|
1011 command->addPoint(newPoint);
|
Chris@43
|
1012 }
|
Chris@43
|
1013 }
|
Chris@43
|
1014
|
Chris@43
|
1015 command->finish();
|
Chris@43
|
1016 }
|
Chris@43
|
1017
|
Chris@43
|
1018 void
|
Chris@43
|
1019 TimeValueLayer::resizeSelection(Selection s, Selection newSize)
|
Chris@43
|
1020 {
|
Chris@99
|
1021 if (!m_model) return;
|
Chris@99
|
1022
|
Chris@43
|
1023 SparseTimeValueModel::EditCommand *command =
|
Chris@43
|
1024 new SparseTimeValueModel::EditCommand(m_model,
|
Chris@43
|
1025 tr("Resize Selection"));
|
Chris@43
|
1026
|
Chris@43
|
1027 SparseTimeValueModel::PointList points =
|
Chris@43
|
1028 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@43
|
1029
|
Chris@43
|
1030 double ratio =
|
Chris@43
|
1031 double(newSize.getEndFrame() - newSize.getStartFrame()) /
|
Chris@43
|
1032 double(s.getEndFrame() - s.getStartFrame());
|
Chris@43
|
1033
|
Chris@43
|
1034 for (SparseTimeValueModel::PointList::iterator i = points.begin();
|
Chris@43
|
1035 i != points.end(); ++i) {
|
Chris@43
|
1036
|
Chris@43
|
1037 if (s.contains(i->frame)) {
|
Chris@43
|
1038
|
Chris@43
|
1039 double target = i->frame;
|
Chris@43
|
1040 target = newSize.getStartFrame() +
|
Chris@43
|
1041 double(target - s.getStartFrame()) * ratio;
|
Chris@43
|
1042
|
Chris@43
|
1043 SparseTimeValueModel::Point newPoint(*i);
|
Chris@43
|
1044 newPoint.frame = lrint(target);
|
Chris@43
|
1045 command->deletePoint(*i);
|
Chris@43
|
1046 command->addPoint(newPoint);
|
Chris@43
|
1047 }
|
Chris@43
|
1048 }
|
Chris@43
|
1049
|
Chris@43
|
1050 command->finish();
|
Chris@43
|
1051 }
|
Chris@43
|
1052
|
Chris@76
|
1053 void
|
Chris@76
|
1054 TimeValueLayer::deleteSelection(Selection s)
|
Chris@76
|
1055 {
|
Chris@99
|
1056 if (!m_model) return;
|
Chris@99
|
1057
|
Chris@76
|
1058 SparseTimeValueModel::EditCommand *command =
|
Chris@76
|
1059 new SparseTimeValueModel::EditCommand(m_model,
|
Chris@76
|
1060 tr("Delete Selected Points"));
|
Chris@76
|
1061
|
Chris@76
|
1062 SparseTimeValueModel::PointList points =
|
Chris@76
|
1063 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@76
|
1064
|
Chris@76
|
1065 for (SparseTimeValueModel::PointList::iterator i = points.begin();
|
Chris@76
|
1066 i != points.end(); ++i) {
|
Chris@76
|
1067
|
Chris@76
|
1068 if (s.contains(i->frame)) {
|
Chris@76
|
1069 command->deletePoint(*i);
|
Chris@76
|
1070 }
|
Chris@76
|
1071 }
|
Chris@76
|
1072
|
Chris@76
|
1073 command->finish();
|
Chris@76
|
1074 }
|
Chris@76
|
1075
|
Chris@76
|
1076 void
|
Chris@76
|
1077 TimeValueLayer::copy(Selection s, Clipboard &to)
|
Chris@76
|
1078 {
|
Chris@99
|
1079 if (!m_model) return;
|
Chris@99
|
1080
|
Chris@76
|
1081 SparseTimeValueModel::PointList points =
|
Chris@76
|
1082 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@76
|
1083
|
Chris@76
|
1084 for (SparseTimeValueModel::PointList::iterator i = points.begin();
|
Chris@76
|
1085 i != points.end(); ++i) {
|
Chris@76
|
1086 if (s.contains(i->frame)) {
|
Chris@83
|
1087 Clipboard::Point point(i->frame, i->value, i->label);
|
Chris@76
|
1088 to.addPoint(point);
|
Chris@76
|
1089 }
|
Chris@76
|
1090 }
|
Chris@76
|
1091 }
|
Chris@76
|
1092
|
Chris@76
|
1093 void
|
Chris@76
|
1094 TimeValueLayer::paste(const Clipboard &from, int frameOffset)
|
Chris@76
|
1095 {
|
Chris@99
|
1096 if (!m_model) return;
|
Chris@99
|
1097
|
Chris@76
|
1098 const Clipboard::PointList &points = from.getPoints();
|
Chris@76
|
1099
|
Chris@76
|
1100 SparseTimeValueModel::EditCommand *command =
|
Chris@76
|
1101 new SparseTimeValueModel::EditCommand(m_model, tr("Paste"));
|
Chris@76
|
1102
|
Chris@76
|
1103 for (Clipboard::PointList::const_iterator i = points.begin();
|
Chris@76
|
1104 i != points.end(); ++i) {
|
Chris@76
|
1105
|
Chris@76
|
1106 if (!i->haveFrame()) continue;
|
Chris@76
|
1107 size_t frame = 0;
|
Chris@76
|
1108 if (frameOffset > 0 || -frameOffset < i->getFrame()) {
|
Chris@76
|
1109 frame = i->getFrame() + frameOffset;
|
Chris@76
|
1110 }
|
Chris@76
|
1111 SparseTimeValueModel::Point newPoint(frame);
|
Chris@76
|
1112
|
Chris@76
|
1113 if (i->haveLabel()) newPoint.label = i->getLabel();
|
Chris@76
|
1114 if (i->haveValue()) newPoint.value = i->getValue();
|
Chris@76
|
1115 else newPoint.value = (m_model->getValueMinimum() +
|
Chris@76
|
1116 m_model->getValueMaximum()) / 2;
|
Chris@76
|
1117
|
Chris@76
|
1118 command->addPoint(newPoint);
|
Chris@76
|
1119 }
|
Chris@76
|
1120
|
Chris@76
|
1121 command->finish();
|
Chris@76
|
1122 }
|
Chris@76
|
1123
|
Chris@6
|
1124 QString
|
Chris@6
|
1125 TimeValueLayer::toXmlString(QString indent, QString extraAttributes) const
|
Chris@6
|
1126 {
|
Chris@6
|
1127 return Layer::toXmlString(indent, extraAttributes +
|
Chris@6
|
1128 QString(" colour=\"%1\" plotStyle=\"%2\"")
|
Chris@6
|
1129 .arg(encodeColour(m_colour)).arg(m_plotStyle));
|
Chris@0
|
1130 }
|
Chris@0
|
1131
|
Chris@11
|
1132 void
|
Chris@11
|
1133 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@11
|
1134 {
|
Chris@11
|
1135 QString colourSpec = attributes.value("colour");
|
Chris@11
|
1136 if (colourSpec != "") {
|
Chris@11
|
1137 QColor colour(colourSpec);
|
Chris@11
|
1138 if (colour.isValid()) {
|
Chris@11
|
1139 setBaseColour(QColor(colourSpec));
|
Chris@11
|
1140 }
|
Chris@11
|
1141 }
|
Chris@11
|
1142
|
Chris@11
|
1143 bool ok;
|
Chris@11
|
1144 PlotStyle style = (PlotStyle)
|
Chris@11
|
1145 attributes.value("plotStyle").toInt(&ok);
|
Chris@11
|
1146 if (ok) setPlotStyle(style);
|
Chris@11
|
1147 }
|
Chris@11
|
1148
|
Chris@0
|
1149
|
Chris@0
|
1150 #ifdef INCLUDE_MOCFILES
|
Chris@0
|
1151 #include "TimeValueLayer.moc.cpp"
|
Chris@0
|
1152 #endif
|
Chris@0
|
1153
|