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@128
|
18 #include "data/model/Model.h"
|
Chris@0
|
19 #include "base/RealTime.h"
|
Chris@0
|
20 #include "base/Profiler.h"
|
Chris@197
|
21 #include "base/LogRange.h"
|
Chris@437
|
22 #include "base/RangeMapper.h"
|
Chris@376
|
23 #include "ColourDatabase.h"
|
Chris@128
|
24 #include "view/View.h"
|
Chris@0
|
25
|
Chris@128
|
26 #include "data/model/SparseTimeValueModel.h"
|
Chris@340
|
27 #include "data/model/Labeller.h"
|
Chris@0
|
28
|
Chris@70
|
29 #include "widgets/ItemEditDialog.h"
|
Chris@125
|
30 #include "widgets/ListInputDialog.h"
|
Chris@70
|
31
|
Chris@66
|
32 #include "SpectrogramLayer.h" // for optional frequency alignment
|
Chris@376
|
33 #include "ColourMapper.h"
|
Chris@66
|
34
|
Chris@0
|
35 #include <QPainter>
|
Chris@6
|
36 #include <QPainterPath>
|
Chris@21
|
37 #include <QMouseEvent>
|
Chris@125
|
38 #include <QRegExp>
|
Chris@316
|
39 #include <QTextStream>
|
Chris@360
|
40 #include <QMessageBox>
|
Chris@340
|
41 #include <QInputDialog>
|
Chris@0
|
42
|
Chris@0
|
43 #include <iostream>
|
Chris@0
|
44 #include <cmath>
|
Chris@0
|
45
|
Chris@44
|
46 TimeValueLayer::TimeValueLayer() :
|
Chris@287
|
47 SingleColourLayer(),
|
Chris@0
|
48 m_model(0),
|
Chris@21
|
49 m_editing(false),
|
Chris@23
|
50 m_originalPoint(0, 0.0, tr("New Point")),
|
Chris@21
|
51 m_editingPoint(0, 0.0, tr("New Point")),
|
Chris@22
|
52 m_editingCommand(0),
|
Chris@197
|
53 m_colourMap(0),
|
Chris@66
|
54 m_plotStyle(PlotConnectedPoints),
|
Chris@437
|
55 m_verticalScale(AutoAlignScale),
|
Chris@437
|
56 m_scaleMinimum(0),
|
Chris@437
|
57 m_scaleMaximum(0)
|
Chris@0
|
58 {
|
Chris@44
|
59
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 void
|
Chris@0
|
63 TimeValueLayer::setModel(SparseTimeValueModel *model)
|
Chris@0
|
64 {
|
Chris@0
|
65 if (m_model == model) return;
|
Chris@0
|
66 m_model = model;
|
Chris@0
|
67
|
Chris@320
|
68 connectSignals(m_model);
|
Chris@0
|
69
|
Chris@101
|
70 // std::cerr << "TimeValueLayer::setModel(" << model << ")" << std::endl;
|
Chris@0
|
71
|
Chris@0
|
72 emit modelReplaced();
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 Layer::PropertyList
|
Chris@0
|
76 TimeValueLayer::getProperties() const
|
Chris@0
|
77 {
|
Chris@287
|
78 PropertyList list = SingleColourLayer::getProperties();
|
Chris@87
|
79 list.push_back("Plot Type");
|
Chris@87
|
80 list.push_back("Vertical Scale");
|
Chris@100
|
81 list.push_back("Scale Units");
|
Chris@0
|
82 return list;
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@87
|
85 QString
|
Chris@87
|
86 TimeValueLayer::getPropertyLabel(const PropertyName &name) const
|
Chris@87
|
87 {
|
Chris@87
|
88 if (name == "Plot Type") return tr("Plot Type");
|
Chris@87
|
89 if (name == "Vertical Scale") return tr("Vertical Scale");
|
Chris@100
|
90 if (name == "Scale Units") return tr("Scale Units");
|
Chris@287
|
91 return SingleColourLayer::getPropertyLabel(name);
|
Chris@87
|
92 }
|
Chris@87
|
93
|
Chris@0
|
94 Layer::PropertyType
|
Chris@0
|
95 TimeValueLayer::getPropertyType(const PropertyName &name) const
|
Chris@0
|
96 {
|
Chris@287
|
97 if (name == "Plot Type") return ValueProperty;
|
Chris@287
|
98 if (name == "Vertical Scale") return ValueProperty;
|
Chris@100
|
99 if (name == "Scale Units") return UnitsProperty;
|
Chris@287
|
100 if (name == "Colour" && m_plotStyle == PlotSegmentation) return ValueProperty;
|
Chris@287
|
101 return SingleColourLayer::getPropertyType(name);
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@198
|
104 QString
|
Chris@198
|
105 TimeValueLayer::getPropertyGroupName(const PropertyName &name) const
|
Chris@198
|
106 {
|
Chris@198
|
107 if (name == "Vertical Scale" || name == "Scale Units") {
|
Chris@198
|
108 return tr("Scale");
|
Chris@198
|
109 }
|
Chris@287
|
110 return SingleColourLayer::getPropertyGroupName(name);
|
Chris@198
|
111 }
|
Chris@198
|
112
|
Chris@0
|
113 int
|
Chris@0
|
114 TimeValueLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@216
|
115 int *min, int *max, int *deflt) const
|
Chris@0
|
116 {
|
Chris@216
|
117 int val = 0;
|
Chris@0
|
118
|
Chris@287
|
119 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
|
Chris@197
|
120
|
Chris@287
|
121 if (min) *min = 0;
|
Chris@287
|
122 if (max) *max = ColourMapper::getColourMapCount() - 1;
|
Chris@287
|
123 if (deflt) *deflt = 0;
|
Chris@197
|
124
|
Chris@287
|
125 val = m_colourMap;
|
Chris@0
|
126
|
Chris@87
|
127 } else if (name == "Plot Type") {
|
Chris@0
|
128
|
Chris@10
|
129 if (min) *min = 0;
|
Chris@26
|
130 if (max) *max = 5;
|
Chris@216
|
131 if (deflt) *deflt = int(PlotConnectedPoints);
|
Chris@0
|
132
|
Chris@216
|
133 val = int(m_plotStyle);
|
Chris@0
|
134
|
Chris@87
|
135 } else if (name == "Vertical Scale") {
|
Chris@66
|
136
|
Chris@66
|
137 if (min) *min = 0;
|
Chris@66
|
138 if (max) *max = 3;
|
Chris@216
|
139 if (deflt) *deflt = int(AutoAlignScale);
|
Chris@66
|
140
|
Chris@216
|
141 val = int(m_verticalScale);
|
Chris@66
|
142
|
Chris@100
|
143 } else if (name == "Scale Units") {
|
Chris@100
|
144
|
Chris@216
|
145 if (deflt) *deflt = 0;
|
Chris@100
|
146 if (m_model) {
|
Chris@216
|
147 val = UnitDatabase::getInstance()->getUnitId
|
Chris@100
|
148 (m_model->getScaleUnits());
|
Chris@100
|
149 }
|
Chris@100
|
150
|
Chris@0
|
151 } else {
|
Chris@0
|
152
|
Chris@287
|
153 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@216
|
156 return val;
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 QString
|
Chris@0
|
160 TimeValueLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@0
|
161 int value) const
|
Chris@0
|
162 {
|
Chris@287
|
163 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
|
Chris@287
|
164 return ColourMapper::getColourMapName(value);
|
Chris@87
|
165 } else if (name == "Plot Type") {
|
Chris@0
|
166 switch (value) {
|
Chris@0
|
167 default:
|
Chris@0
|
168 case 0: return tr("Points");
|
Chris@0
|
169 case 1: return tr("Stems");
|
Chris@6
|
170 case 2: return tr("Connected Points");
|
Chris@6
|
171 case 3: return tr("Lines");
|
Chris@6
|
172 case 4: return tr("Curve");
|
Chris@26
|
173 case 5: return tr("Segmentation");
|
Chris@0
|
174 }
|
Chris@87
|
175 } else if (name == "Vertical Scale") {
|
Chris@66
|
176 switch (value) {
|
Chris@66
|
177 default:
|
Chris@101
|
178 case 0: return tr("Auto-Align");
|
Chris@198
|
179 case 1: return tr("Linear");
|
Chris@198
|
180 case 2: return tr("Log");
|
Chris@198
|
181 case 3: return tr("+/-1");
|
Chris@66
|
182 }
|
Chris@0
|
183 }
|
Chris@287
|
184 return SingleColourLayer::getPropertyValueLabel(name, value);
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@0
|
187 void
|
Chris@0
|
188 TimeValueLayer::setProperty(const PropertyName &name, int value)
|
Chris@0
|
189 {
|
Chris@287
|
190 if (name == "Colour" && m_plotStyle == PlotSegmentation) {
|
Chris@287
|
191 setFillColourMap(value);
|
Chris@87
|
192 } else if (name == "Plot Type") {
|
Chris@0
|
193 setPlotStyle(PlotStyle(value));
|
Chris@87
|
194 } else if (name == "Vertical Scale") {
|
Chris@66
|
195 setVerticalScale(VerticalScale(value));
|
Chris@100
|
196 } else if (name == "Scale Units") {
|
Chris@100
|
197 if (m_model) {
|
Chris@100
|
198 m_model->setScaleUnits
|
Chris@100
|
199 (UnitDatabase::getInstance()->getUnitById(value));
|
Chris@100
|
200 emit modelChanged();
|
Chris@100
|
201 }
|
Chris@287
|
202 } else {
|
Chris@287
|
203 SingleColourLayer::setProperty(name, value);
|
Chris@0
|
204 }
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 void
|
Chris@197
|
208 TimeValueLayer::setFillColourMap(int map)
|
Chris@197
|
209 {
|
Chris@197
|
210 if (m_colourMap == map) return;
|
Chris@197
|
211 m_colourMap = map;
|
Chris@197
|
212 emit layerParametersChanged();
|
Chris@197
|
213 }
|
Chris@197
|
214
|
Chris@197
|
215 void
|
Chris@0
|
216 TimeValueLayer::setPlotStyle(PlotStyle style)
|
Chris@0
|
217 {
|
Chris@0
|
218 if (m_plotStyle == style) return;
|
Chris@197
|
219 bool colourTypeChanged = (style == PlotSegmentation ||
|
Chris@197
|
220 m_plotStyle == PlotSegmentation);
|
Chris@0
|
221 m_plotStyle = style;
|
Chris@197
|
222 if (colourTypeChanged) {
|
Chris@197
|
223 emit layerParameterRangesChanged();
|
Chris@197
|
224 }
|
Chris@0
|
225 emit layerParametersChanged();
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@66
|
228 void
|
Chris@66
|
229 TimeValueLayer::setVerticalScale(VerticalScale scale)
|
Chris@66
|
230 {
|
Chris@66
|
231 if (m_verticalScale == scale) return;
|
Chris@66
|
232 m_verticalScale = scale;
|
Chris@66
|
233 emit layerParametersChanged();
|
Chris@66
|
234 }
|
Chris@66
|
235
|
Chris@0
|
236 bool
|
Chris@44
|
237 TimeValueLayer::isLayerScrollable(const View *v) const
|
Chris@0
|
238 {
|
Chris@6
|
239 // We don't illuminate sections in the line or curve modes, so
|
Chris@6
|
240 // they're always scrollable
|
Chris@6
|
241
|
Chris@6
|
242 if (m_plotStyle == PlotLines ||
|
Chris@6
|
243 m_plotStyle == PlotCurve) return true;
|
Chris@6
|
244
|
Chris@0
|
245 QPoint discard;
|
Chris@44
|
246 return !v->shouldIlluminateLocalFeatures(this, discard);
|
Chris@0
|
247 }
|
Chris@0
|
248
|
Chris@79
|
249 bool
|
Chris@101
|
250 TimeValueLayer::getValueExtents(float &min, float &max,
|
Chris@101
|
251 bool &logarithmic, QString &unit) const
|
Chris@79
|
252 {
|
Chris@101
|
253 if (!m_model) return false;
|
Chris@79
|
254 min = m_model->getValueMinimum();
|
Chris@79
|
255 max = m_model->getValueMaximum();
|
Chris@101
|
256 logarithmic = (m_verticalScale == LogScale);
|
Chris@79
|
257 unit = m_model->getScaleUnits();
|
Chris@79
|
258 return true;
|
Chris@79
|
259 }
|
Chris@79
|
260
|
Chris@101
|
261 bool
|
Chris@101
|
262 TimeValueLayer::getDisplayExtents(float &min, float &max) const
|
Chris@101
|
263 {
|
Chris@296
|
264 if (!m_model || shouldAutoAlign()) return false;
|
Chris@101
|
265
|
Chris@437
|
266 if (m_scaleMinimum == m_scaleMaximum) {
|
Chris@437
|
267 m_scaleMinimum = m_model->getValueMinimum();
|
Chris@437
|
268 m_scaleMaximum = m_model->getValueMaximum();
|
Chris@437
|
269 }
|
Chris@437
|
270
|
Chris@437
|
271 min = m_scaleMinimum;
|
Chris@437
|
272 max = m_scaleMaximum;
|
Chris@437
|
273
|
Chris@437
|
274 // std::cerr << "TimeValueLayer::getDisplayExtents: min = " << min << ", max = " << max << std::endl;
|
Chris@437
|
275
|
Chris@101
|
276 return true;
|
Chris@101
|
277 }
|
Chris@101
|
278
|
Chris@437
|
279 bool
|
Chris@437
|
280 TimeValueLayer::setDisplayExtents(float min, float max)
|
Chris@437
|
281 {
|
Chris@437
|
282 if (!m_model) return false;
|
Chris@437
|
283
|
Chris@437
|
284 if (min == max) {
|
Chris@437
|
285 if (min == 0.f) {
|
Chris@437
|
286 max = 1.f;
|
Chris@437
|
287 } else {
|
Chris@437
|
288 max = min * 1.0001;
|
Chris@437
|
289 }
|
Chris@437
|
290 }
|
Chris@437
|
291
|
Chris@437
|
292 m_scaleMinimum = min;
|
Chris@437
|
293 m_scaleMaximum = max;
|
Chris@437
|
294
|
Chris@437
|
295 std::cerr << "TimeValueLayer::setDisplayExtents: min = " << min << ", max = " << max << std::endl;
|
Chris@437
|
296
|
Chris@437
|
297 emit layerParametersChanged();
|
Chris@437
|
298 return true;
|
Chris@437
|
299 }
|
Chris@437
|
300
|
Chris@437
|
301 int
|
Chris@437
|
302 TimeValueLayer::getVerticalZoomSteps(int &defaultStep) const
|
Chris@437
|
303 {
|
Chris@437
|
304 if (shouldAutoAlign()) return 0;
|
Chris@437
|
305 if (!m_model) return 0;
|
Chris@437
|
306
|
Chris@437
|
307 defaultStep = 100;
|
Chris@437
|
308 return 100;
|
Chris@437
|
309 }
|
Chris@437
|
310
|
Chris@437
|
311 int
|
Chris@437
|
312 TimeValueLayer::getCurrentVerticalZoomStep() const
|
Chris@437
|
313 {
|
Chris@437
|
314 if (shouldAutoAlign()) return 0;
|
Chris@437
|
315 if (!m_model) return 0;
|
Chris@437
|
316
|
Chris@437
|
317 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
|
Chris@437
|
318 if (!mapper) return 0;
|
Chris@437
|
319
|
Chris@437
|
320 float dmin, dmax;
|
Chris@437
|
321 getDisplayExtents(dmin, dmax);
|
Chris@437
|
322
|
Chris@437
|
323 int nr = mapper->getPositionForValue(dmax - dmin);
|
Chris@437
|
324 /*
|
Chris@437
|
325 int n0 = mapper->getPositionForValue(dmax);
|
Chris@437
|
326 int n1 = mapper->getPositionForValue(dmin);
|
Chris@437
|
327 int nr = n1 - n0;
|
Chris@437
|
328 if (nr < 0) nr = -nr;
|
Chris@437
|
329
|
Chris@437
|
330 std::cerr << "TimeValueLayer::getCurrentVerticalZoomStep: dmin = " << dmin << ", dmax = " << dmax << ", n0 = " << n0 << ", n1 = " << n1 << ", nr = " << nr << std::endl;
|
Chris@437
|
331 */
|
Chris@437
|
332 delete mapper;
|
Chris@437
|
333
|
Chris@437
|
334 return 100 - nr;
|
Chris@437
|
335 }
|
Chris@437
|
336
|
Chris@437
|
337 void
|
Chris@437
|
338 TimeValueLayer::setVerticalZoomStep(int step)
|
Chris@437
|
339 {
|
Chris@437
|
340 if (shouldAutoAlign()) return;
|
Chris@437
|
341 if (!m_model) return;
|
Chris@437
|
342
|
Chris@437
|
343 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
|
Chris@437
|
344 if (!mapper) return;
|
Chris@437
|
345
|
Chris@437
|
346 float min, max;
|
Chris@437
|
347 bool logarithmic;
|
Chris@437
|
348 QString unit;
|
Chris@437
|
349 getValueExtents(min, max, logarithmic, unit);
|
Chris@437
|
350
|
Chris@437
|
351 float dmin, dmax;
|
Chris@437
|
352 getDisplayExtents(dmin, dmax);
|
Chris@437
|
353
|
Chris@437
|
354 float newdist = mapper->getValueForPosition(100 - step);
|
Chris@437
|
355
|
Chris@437
|
356 float newmin, newmax;
|
Chris@437
|
357
|
Chris@437
|
358 if (logarithmic) {
|
Chris@437
|
359
|
Chris@437
|
360 // see SpectrogramLayer::setVerticalZoomStep
|
Chris@437
|
361
|
Chris@437
|
362 newmax = (newdist + sqrtf(newdist*newdist + 4*dmin*dmax)) / 2;
|
Chris@437
|
363 newmin = newmax - newdist;
|
Chris@437
|
364
|
Chris@437
|
365 // std::cerr << "newmin = " << newmin << ", newmax = " << newmax << std::endl;
|
Chris@437
|
366
|
Chris@437
|
367 } else {
|
Chris@437
|
368 float dmid = (dmax + dmin) / 2;
|
Chris@437
|
369 newmin = dmid - newdist / 2;
|
Chris@437
|
370 newmax = dmid + newdist / 2;
|
Chris@437
|
371 }
|
Chris@437
|
372
|
Chris@437
|
373 if (newmin < min) {
|
Chris@437
|
374 newmax += (min - newmin);
|
Chris@437
|
375 newmin = min;
|
Chris@437
|
376 }
|
Chris@437
|
377 if (newmax > max) {
|
Chris@437
|
378 newmax = max;
|
Chris@437
|
379 }
|
Chris@437
|
380
|
Chris@437
|
381 std::cerr << "TimeValueLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << std::endl;
|
Chris@437
|
382
|
Chris@437
|
383 setDisplayExtents(newmin, newmax);
|
Chris@437
|
384 }
|
Chris@437
|
385
|
Chris@437
|
386 RangeMapper *
|
Chris@437
|
387 TimeValueLayer::getNewVerticalZoomRangeMapper() const
|
Chris@437
|
388 {
|
Chris@437
|
389 if (!m_model) return 0;
|
Chris@437
|
390
|
Chris@437
|
391 RangeMapper *mapper;
|
Chris@437
|
392
|
Chris@437
|
393 float min, max;
|
Chris@437
|
394 bool logarithmic;
|
Chris@437
|
395 QString unit;
|
Chris@437
|
396 getValueExtents(min, max, logarithmic, unit);
|
Chris@437
|
397
|
Chris@437
|
398 if (min == max) return 0;
|
Chris@437
|
399
|
Chris@437
|
400 if (logarithmic) {
|
Chris@437
|
401 mapper = new LogRangeMapper(0, 100, min, max, unit);
|
Chris@437
|
402 } else {
|
Chris@437
|
403 mapper = new LinearRangeMapper(0, 100, min, max, unit);
|
Chris@437
|
404 }
|
Chris@437
|
405
|
Chris@437
|
406 return mapper;
|
Chris@437
|
407 }
|
Chris@437
|
408
|
Chris@0
|
409 SparseTimeValueModel::PointList
|
Chris@44
|
410 TimeValueLayer::getLocalPoints(View *v, int x) const
|
Chris@0
|
411 {
|
Chris@0
|
412 if (!m_model) return SparseTimeValueModel::PointList();
|
Chris@0
|
413
|
Chris@44
|
414 long frame = v->getFrameForX(x);
|
Chris@0
|
415
|
Chris@0
|
416 SparseTimeValueModel::PointList onPoints =
|
Chris@0
|
417 m_model->getPoints(frame);
|
Chris@0
|
418
|
Chris@0
|
419 if (!onPoints.empty()) {
|
Chris@0
|
420 return onPoints;
|
Chris@0
|
421 }
|
Chris@0
|
422
|
Chris@0
|
423 SparseTimeValueModel::PointList prevPoints =
|
Chris@0
|
424 m_model->getPreviousPoints(frame);
|
Chris@0
|
425 SparseTimeValueModel::PointList nextPoints =
|
Chris@0
|
426 m_model->getNextPoints(frame);
|
Chris@0
|
427
|
Chris@0
|
428 SparseTimeValueModel::PointList usePoints = prevPoints;
|
Chris@0
|
429
|
Chris@0
|
430 if (prevPoints.empty()) {
|
Chris@0
|
431 usePoints = nextPoints;
|
Chris@248
|
432 } else if (long(prevPoints.begin()->frame) < v->getStartFrame() &&
|
Chris@44
|
433 !(nextPoints.begin()->frame > v->getEndFrame())) {
|
Chris@0
|
434 usePoints = nextPoints;
|
Chris@0
|
435 } else if (nextPoints.begin()->frame - frame <
|
Chris@0
|
436 frame - prevPoints.begin()->frame) {
|
Chris@0
|
437 usePoints = nextPoints;
|
Chris@0
|
438 }
|
Chris@0
|
439
|
Chris@28
|
440 if (!usePoints.empty()) {
|
Chris@28
|
441 int fuzz = 2;
|
Chris@44
|
442 int px = v->getXForFrame(usePoints.begin()->frame);
|
Chris@28
|
443 if ((px > x && px - x > fuzz) ||
|
Chris@28
|
444 (px < x && x - px > fuzz + 1)) {
|
Chris@28
|
445 usePoints.clear();
|
Chris@28
|
446 }
|
Chris@28
|
447 }
|
Chris@28
|
448
|
Chris@0
|
449 return usePoints;
|
Chris@0
|
450 }
|
Chris@0
|
451
|
Chris@25
|
452 QString
|
Chris@44
|
453 TimeValueLayer::getFeatureDescription(View *v, QPoint &pos) const
|
Chris@0
|
454 {
|
Chris@25
|
455 int x = pos.x();
|
Chris@0
|
456
|
Chris@25
|
457 if (!m_model || !m_model->getSampleRate()) return "";
|
Chris@0
|
458
|
Chris@44
|
459 SparseTimeValueModel::PointList points = getLocalPoints(v, x);
|
Chris@0
|
460
|
Chris@0
|
461 if (points.empty()) {
|
Chris@0
|
462 if (!m_model->isReady()) {
|
Chris@25
|
463 return tr("In progress");
|
Chris@25
|
464 } else {
|
Chris@25
|
465 return tr("No local points");
|
Chris@0
|
466 }
|
Chris@0
|
467 }
|
Chris@0
|
468
|
Chris@0
|
469 long useFrame = points.begin()->frame;
|
Chris@0
|
470
|
Chris@0
|
471 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
|
Chris@25
|
472
|
Chris@25
|
473 QString text;
|
Chris@101
|
474 QString unit = m_model->getScaleUnits();
|
Chris@101
|
475 if (unit != "") unit = " " + unit;
|
Chris@0
|
476
|
Chris@25
|
477 if (points.begin()->label == "") {
|
Chris@101
|
478 text = QString(tr("Time:\t%1\nValue:\t%2%3\nNo label"))
|
Chris@25
|
479 .arg(rt.toText(true).c_str())
|
Chris@25
|
480 .arg(points.begin()->value)
|
Chris@101
|
481 .arg(unit);
|
Chris@101
|
482 } else {
|
Chris@101
|
483 text = QString(tr("Time:\t%1\nValue:\t%2%3\nLabel:\t%4"))
|
Chris@101
|
484 .arg(rt.toText(true).c_str())
|
Chris@101
|
485 .arg(points.begin()->value)
|
Chris@101
|
486 .arg(unit)
|
Chris@25
|
487 .arg(points.begin()->label);
|
Chris@25
|
488 }
|
Chris@0
|
489
|
Chris@44
|
490 pos = QPoint(v->getXForFrame(useFrame),
|
Chris@44
|
491 getYForValue(v, points.begin()->value));
|
Chris@25
|
492 return text;
|
Chris@0
|
493 }
|
Chris@0
|
494
|
Chris@28
|
495 bool
|
Chris@44
|
496 TimeValueLayer::snapToFeatureFrame(View *v, int &frame,
|
Chris@28
|
497 size_t &resolution,
|
Chris@28
|
498 SnapType snap) const
|
Chris@13
|
499 {
|
Chris@13
|
500 if (!m_model) {
|
Chris@44
|
501 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
|
Chris@13
|
502 }
|
Chris@13
|
503
|
Chris@13
|
504 resolution = m_model->getResolution();
|
Chris@28
|
505 SparseTimeValueModel::PointList points;
|
Chris@13
|
506
|
Chris@28
|
507 if (snap == SnapNeighbouring) {
|
Chris@28
|
508
|
Chris@44
|
509 points = getLocalPoints(v, v->getXForFrame(frame));
|
Chris@28
|
510 if (points.empty()) return false;
|
Chris@28
|
511 frame = points.begin()->frame;
|
Chris@28
|
512 return true;
|
Chris@28
|
513 }
|
Chris@28
|
514
|
Chris@28
|
515 points = m_model->getPoints(frame, frame);
|
Chris@28
|
516 int snapped = frame;
|
Chris@28
|
517 bool found = false;
|
Chris@13
|
518
|
Chris@13
|
519 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
|
Chris@13
|
520 i != points.end(); ++i) {
|
Chris@13
|
521
|
Chris@28
|
522 if (snap == SnapRight) {
|
Chris@28
|
523
|
Chris@13
|
524 if (i->frame > frame) {
|
Chris@28
|
525 snapped = i->frame;
|
Chris@28
|
526 found = true;
|
Chris@13
|
527 break;
|
Chris@13
|
528 }
|
Chris@28
|
529
|
Chris@28
|
530 } else if (snap == SnapLeft) {
|
Chris@28
|
531
|
Chris@13
|
532 if (i->frame <= frame) {
|
Chris@28
|
533 snapped = i->frame;
|
Chris@28
|
534 found = true; // don't break, as the next may be better
|
Chris@28
|
535 } else {
|
Chris@28
|
536 break;
|
Chris@28
|
537 }
|
Chris@28
|
538
|
Chris@28
|
539 } else { // nearest
|
Chris@28
|
540
|
Chris@28
|
541 SparseTimeValueModel::PointList::const_iterator j = i;
|
Chris@28
|
542 ++j;
|
Chris@28
|
543
|
Chris@28
|
544 if (j == points.end()) {
|
Chris@28
|
545
|
Chris@28
|
546 snapped = i->frame;
|
Chris@28
|
547 found = true;
|
Chris@28
|
548 break;
|
Chris@28
|
549
|
Chris@28
|
550 } else if (j->frame >= frame) {
|
Chris@28
|
551
|
Chris@28
|
552 if (j->frame - frame < frame - i->frame) {
|
Chris@28
|
553 snapped = j->frame;
|
Chris@28
|
554 } else {
|
Chris@28
|
555 snapped = i->frame;
|
Chris@28
|
556 }
|
Chris@28
|
557 found = true;
|
Chris@28
|
558 break;
|
Chris@13
|
559 }
|
Chris@13
|
560 }
|
Chris@13
|
561 }
|
Chris@13
|
562
|
Chris@28
|
563 frame = snapped;
|
Chris@28
|
564 return found;
|
Chris@13
|
565 }
|
Chris@13
|
566
|
Chris@101
|
567 void
|
Chris@101
|
568 TimeValueLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
|
Chris@101
|
569 {
|
Chris@101
|
570 min = 0.0;
|
Chris@101
|
571 max = 0.0;
|
Chris@101
|
572 log = false;
|
Chris@101
|
573
|
Chris@296
|
574 if (shouldAutoAlign()) {
|
Chris@101
|
575
|
Chris@101
|
576 if (!v->getValueExtents(m_model->getScaleUnits(), min, max, log)) {
|
Chris@101
|
577 min = m_model->getValueMinimum();
|
Chris@101
|
578 max = m_model->getValueMaximum();
|
Chris@101
|
579 } else if (log) {
|
Chris@197
|
580 LogRange::mapRange(min, max);
|
Chris@101
|
581 }
|
Chris@101
|
582
|
Chris@101
|
583 } else if (m_verticalScale == PlusMinusOneScale) {
|
Chris@101
|
584
|
Chris@101
|
585 min = -1.0;
|
Chris@101
|
586 max = 1.0;
|
Chris@101
|
587
|
Chris@101
|
588 } else {
|
Chris@101
|
589
|
Chris@437
|
590 getDisplayExtents(min, max);
|
Chris@101
|
591
|
Chris@101
|
592 if (m_verticalScale == LogScale) {
|
Chris@197
|
593 LogRange::mapRange(min, max);
|
Chris@101
|
594 log = true;
|
Chris@101
|
595 }
|
Chris@101
|
596 }
|
Chris@101
|
597
|
Chris@101
|
598 if (max == min) max = min + 1.0;
|
Chris@101
|
599 }
|
Chris@101
|
600
|
Chris@21
|
601 int
|
Chris@66
|
602 TimeValueLayer::getYForValue(View *v, float val) const
|
Chris@21
|
603 {
|
Chris@79
|
604 float min = 0.0, max = 0.0;
|
Chris@101
|
605 bool logarithmic = false;
|
Chris@79
|
606 int h = v->height();
|
Chris@79
|
607
|
Chris@101
|
608 getScaleExtents(v, min, max, logarithmic);
|
Chris@101
|
609
|
Chris@101
|
610 // std::cerr << "getYForValue(" << val << "): min " << min << ", max "
|
Chris@101
|
611 // << max << ", log " << logarithmic << std::endl;
|
Chris@101
|
612
|
Chris@101
|
613 if (logarithmic) {
|
Chris@197
|
614 val = LogRange::map(val);
|
Chris@79
|
615 }
|
Chris@79
|
616
|
Chris@66
|
617 return int(h - ((val - min) * h) / (max - min));
|
Chris@21
|
618 }
|
Chris@21
|
619
|
Chris@21
|
620 float
|
Chris@44
|
621 TimeValueLayer::getValueForY(View *v, int y) const
|
Chris@21
|
622 {
|
Chris@101
|
623 float min = 0.0, max = 0.0;
|
Chris@101
|
624 bool logarithmic = false;
|
Chris@44
|
625 int h = v->height();
|
Chris@21
|
626
|
Chris@101
|
627 getScaleExtents(v, min, max, logarithmic);
|
Chris@101
|
628
|
Chris@101
|
629 float val = min + (float(h - y) * float(max - min)) / h;
|
Chris@101
|
630
|
Chris@101
|
631 if (logarithmic) {
|
Chris@437
|
632 val = LogRange::map(val);
|
Chris@101
|
633 }
|
Chris@101
|
634
|
Chris@101
|
635 return val;
|
Chris@21
|
636 }
|
Chris@21
|
637
|
Chris@296
|
638 bool
|
Chris@296
|
639 TimeValueLayer::shouldAutoAlign() const
|
Chris@296
|
640 {
|
Chris@296
|
641 if (!m_model) return false;
|
Chris@296
|
642 QString unit = m_model->getScaleUnits();
|
Chris@296
|
643 return (m_verticalScale == AutoAlignScale && unit != "");
|
Chris@296
|
644 }
|
Chris@296
|
645
|
Chris@68
|
646 QColor
|
Chris@101
|
647 TimeValueLayer::getColourForValue(View *v, float val) const
|
Chris@68
|
648 {
|
Chris@101
|
649 float min, max;
|
Chris@101
|
650 bool log;
|
Chris@101
|
651 getScaleExtents(v, min, max, log);
|
Chris@68
|
652
|
Chris@197
|
653 if (min > max) std::swap(min, max);
|
Chris@197
|
654 if (max == min) max = min + 1;
|
Chris@197
|
655
|
Chris@101
|
656 if (log) {
|
Chris@197
|
657 val = LogRange::map(val);
|
Chris@68
|
658 }
|
Chris@68
|
659
|
Chris@197
|
660 // std::cerr << "TimeValueLayer::getColourForValue: min " << min << ", max "
|
Chris@197
|
661 // << max << ", log " << log << ", value " << val << std::endl;
|
Chris@68
|
662
|
Chris@197
|
663 QColor solid = ColourMapper(m_colourMap, min, max).map(val);
|
Chris@197
|
664 return QColor(solid.red(), solid.green(), solid.blue(), 120);
|
Chris@68
|
665 }
|
Chris@68
|
666
|
Chris@287
|
667 int
|
Chris@287
|
668 TimeValueLayer::getDefaultColourHint(bool darkbg, bool &impose)
|
Chris@287
|
669 {
|
Chris@287
|
670 impose = false;
|
Chris@287
|
671 return ColourDatabase::getInstance()->getColourIndex
|
Chris@287
|
672 (QString(darkbg ? "Bright Green" : "Green"));
|
Chris@287
|
673 }
|
Chris@287
|
674
|
Chris@0
|
675 void
|
Chris@44
|
676 TimeValueLayer::paint(View *v, QPainter &paint, QRect rect) const
|
Chris@0
|
677 {
|
Chris@0
|
678 if (!m_model || !m_model->isOK()) return;
|
Chris@0
|
679
|
Chris@0
|
680 int sampleRate = m_model->getSampleRate();
|
Chris@0
|
681 if (!sampleRate) return;
|
Chris@0
|
682
|
Chris@353
|
683 paint.setRenderHint(QPainter::Antialiasing, false);
|
Chris@353
|
684
|
Chris@0
|
685 // Profiler profiler("TimeValueLayer::paint", true);
|
Chris@0
|
686
|
Chris@0
|
687 int x0 = rect.left(), x1 = rect.right();
|
Chris@44
|
688 long frame0 = v->getFrameForX(x0);
|
Chris@44
|
689 long frame1 = v->getFrameForX(x1);
|
Chris@0
|
690
|
Chris@0
|
691 SparseTimeValueModel::PointList points(m_model->getPoints
|
Chris@0
|
692 (frame0, frame1));
|
Chris@11
|
693 if (points.empty()) return;
|
Chris@0
|
694
|
Chris@287
|
695 paint.setPen(getBaseQColor());
|
Chris@0
|
696
|
Chris@287
|
697 QColor brushColour(getBaseQColor());
|
Chris@0
|
698 brushColour.setAlpha(80);
|
Chris@0
|
699 paint.setBrush(brushColour);
|
Chris@0
|
700
|
Chris@0
|
701 // std::cerr << "TimeValueLayer::paint: resolution is "
|
Chris@0
|
702 // << m_model->getResolution() << " frames" << std::endl;
|
Chris@0
|
703
|
Chris@0
|
704 float min = m_model->getValueMinimum();
|
Chris@0
|
705 float max = m_model->getValueMaximum();
|
Chris@0
|
706 if (max == min) max = min + 1.0;
|
Chris@0
|
707
|
Chris@44
|
708 int origin = int(nearbyint(v->height() -
|
Chris@44
|
709 (-min * v->height()) / (max - min)));
|
Chris@0
|
710
|
Chris@0
|
711 QPoint localPos;
|
Chris@0
|
712 long illuminateFrame = -1;
|
Chris@0
|
713
|
Chris@44
|
714 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
|
Chris@0
|
715 SparseTimeValueModel::PointList localPoints =
|
Chris@44
|
716 getLocalPoints(v, localPos.x());
|
Chris@296
|
717 // std::cerr << "TimeValueLayer: " << localPoints.size() << " local points" << std::endl;
|
Chris@0
|
718 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
|
Chris@0
|
719 }
|
Chris@6
|
720
|
Chris@20
|
721 int w =
|
Chris@44
|
722 v->getXForFrame(frame0 + m_model->getResolution()) -
|
Chris@44
|
723 v->getXForFrame(frame0);
|
Chris@7
|
724
|
Chris@6
|
725 paint.save();
|
Chris@6
|
726
|
Chris@6
|
727 QPainterPath path;
|
Chris@55
|
728 int pointCount = 0;
|
Chris@79
|
729
|
Chris@79
|
730 int textY = 0;
|
Chris@79
|
731 if (m_plotStyle == PlotSegmentation) {
|
Chris@79
|
732 textY = v->getTextLabelHeight(this, paint);
|
Chris@79
|
733 }
|
Chris@6
|
734
|
Chris@0
|
735 for (SparseTimeValueModel::PointList::const_iterator i = points.begin();
|
Chris@0
|
736 i != points.end(); ++i) {
|
Chris@0
|
737
|
Chris@0
|
738 const SparseTimeValueModel::Point &p(*i);
|
Chris@0
|
739
|
Chris@44
|
740 int x = v->getXForFrame(p.frame);
|
Chris@44
|
741 int y = getYForValue(v, p.value);
|
Chris@0
|
742
|
Chris@79
|
743 if (m_plotStyle != PlotSegmentation) {
|
Chris@79
|
744 textY = y - paint.fontMetrics().height()
|
Chris@79
|
745 + paint.fontMetrics().ascent();
|
Chris@372
|
746 if (textY < paint.fontMetrics().ascent() + 1) {
|
Chris@372
|
747 textY = paint.fontMetrics().ascent() + 1;
|
Chris@372
|
748 }
|
Chris@79
|
749 }
|
Chris@79
|
750
|
Chris@34
|
751 bool haveNext = false;
|
Chris@76
|
752 int nx = v->getXForFrame(v->getModelsEndFrame());
|
Chris@34
|
753 int ny = y;
|
Chris@34
|
754
|
Chris@34
|
755 SparseTimeValueModel::PointList::const_iterator j = i;
|
Chris@34
|
756 ++j;
|
Chris@34
|
757
|
Chris@34
|
758 if (j != points.end()) {
|
Chris@34
|
759 const SparseTimeValueModel::Point &q(*j);
|
Chris@44
|
760 nx = v->getXForFrame(q.frame);
|
Chris@44
|
761 ny = getYForValue(v, q.value);
|
Chris@34
|
762 haveNext = true;
|
Chris@76
|
763 }
|
Chris@76
|
764
|
Chris@76
|
765 // std::cout << "frame = " << p.frame << ", x = " << x << ", haveNext = " << haveNext
|
Chris@76
|
766 // << ", nx = " << nx << std::endl;
|
Chris@34
|
767
|
Chris@0
|
768 if (w < 1) w = 1;
|
Chris@287
|
769 paint.setPen(getBaseQColor());
|
Chris@0
|
770
|
Chris@26
|
771 if (m_plotStyle == PlotSegmentation) {
|
Chris@287
|
772 paint.setPen(getForegroundQColor(v));
|
Chris@101
|
773 paint.setBrush(getColourForValue(v, p.value));
|
Chris@26
|
774 } else if (m_plotStyle == PlotLines ||
|
Chris@26
|
775 m_plotStyle == PlotCurve) {
|
Chris@6
|
776 paint.setBrush(Qt::NoBrush);
|
Chris@3
|
777 } else {
|
Chris@6
|
778 paint.setBrush(brushColour);
|
Chris@3
|
779 }
|
Chris@0
|
780
|
Chris@0
|
781 if (m_plotStyle == PlotStems) {
|
Chris@430
|
782 /*
|
Chris@0
|
783 paint.setPen(brushColour);
|
Chris@0
|
784 if (y < origin - 1) {
|
Chris@0
|
785 paint.drawRect(x + w/2, y + 1, 1, origin - y);
|
Chris@0
|
786 } else if (y > origin + 1) {
|
Chris@0
|
787 paint.drawRect(x + w/2, origin, 1, y - origin - 1);
|
Chris@0
|
788 }
|
Chris@430
|
789 */
|
Chris@287
|
790 paint.setPen(getBaseQColor());
|
Chris@430
|
791 if (y < origin - 1) {
|
Chris@430
|
792 paint.drawLine(x + w/2, y + 1, x + w/2, origin);
|
Chris@430
|
793 } else if (y > origin + 1) {
|
Chris@430
|
794 paint.drawLine(x + w/2, origin, x + w/2, y - 1);
|
Chris@430
|
795 }
|
Chris@0
|
796 }
|
Chris@0
|
797
|
Chris@0
|
798 if (illuminateFrame == p.frame) {
|
Chris@6
|
799
|
Chris@0
|
800 //!!! aside from the problem of choosing a colour, it'd be
|
Chris@0
|
801 //better to save the highlighted rects and draw them at
|
Chris@0
|
802 //the end perhaps
|
Chris@6
|
803
|
Chris@6
|
804 //!!! not equipped to illuminate the right section in line
|
Chris@6
|
805 //or curve mode
|
Chris@6
|
806
|
Chris@6
|
807 if (m_plotStyle != PlotCurve &&
|
Chris@6
|
808 m_plotStyle != PlotLines) {
|
Chris@287
|
809 paint.setPen(getForegroundQColor(v));
|
Chris@6
|
810 }
|
Chris@0
|
811 }
|
Chris@0
|
812
|
Chris@6
|
813 if (m_plotStyle != PlotLines &&
|
Chris@26
|
814 m_plotStyle != PlotCurve &&
|
Chris@26
|
815 m_plotStyle != PlotSegmentation) {
|
Chris@326
|
816 if (m_plotStyle != PlotStems ||
|
Chris@326
|
817 w > 1) {
|
Chris@326
|
818 paint.drawRect(x, y - 1, w, 2);
|
Chris@326
|
819 }
|
Chris@3
|
820 }
|
Chris@0
|
821
|
Chris@6
|
822 if (m_plotStyle == PlotConnectedPoints ||
|
Chris@6
|
823 m_plotStyle == PlotLines ||
|
Chris@6
|
824 m_plotStyle == PlotCurve) {
|
Chris@0
|
825
|
Chris@34
|
826 if (haveNext) {
|
Chris@3
|
827
|
Chris@6
|
828 if (m_plotStyle == PlotConnectedPoints) {
|
Chris@34
|
829
|
Chris@79
|
830 paint.save();
|
Chris@3
|
831 paint.setPen(brushColour);
|
Chris@3
|
832 paint.drawLine(x + w, y, nx, ny);
|
Chris@79
|
833 paint.restore();
|
Chris@6
|
834
|
Chris@6
|
835 } else if (m_plotStyle == PlotLines) {
|
Chris@430
|
836
|
Chris@430
|
837 if (pointCount == 0) {
|
Chris@430
|
838 path.moveTo(x + w/2, y);
|
Chris@430
|
839 }
|
Chris@430
|
840 ++pointCount;
|
Chris@6
|
841
|
Chris@430
|
842 // paint.drawLine(x + w/2, y, nx + w/2, ny);
|
Chris@430
|
843 path.lineTo(nx + w/2, ny);
|
Chris@6
|
844
|
Chris@3
|
845 } else {
|
Chris@6
|
846
|
Chris@55
|
847 float x0 = x + float(w)/2;
|
Chris@55
|
848 float x1 = nx + float(w)/2;
|
Chris@55
|
849
|
Chris@55
|
850 float y0 = y;
|
Chris@55
|
851 float y1 = ny;
|
Chris@55
|
852
|
Chris@55
|
853 if (pointCount == 0) {
|
Chris@55
|
854 path.moveTo((x0 + x1) / 2, (y0 + y1) / 2);
|
Chris@6
|
855 }
|
Chris@55
|
856 ++pointCount;
|
Chris@6
|
857
|
Chris@6
|
858 if (nx - x > 5) {
|
Chris@55
|
859 path.cubicTo(x0, y0,
|
Chris@55
|
860 x0, y0,
|
Chris@55
|
861 (x0 + x1) / 2, (y0 + y1) / 2);
|
Chris@55
|
862
|
Chris@55
|
863 // // or
|
Chris@55
|
864 // path.quadTo(x0, y0, (x0 + x1) / 2, (y0 + y1) / 2);
|
Chris@55
|
865
|
Chris@6
|
866 } else {
|
Chris@55
|
867 path.lineTo((x0 + x1) / 2, (y0 + y1) / 2);
|
Chris@6
|
868 }
|
Chris@3
|
869 }
|
Chris@0
|
870 }
|
Chris@0
|
871 }
|
Chris@0
|
872
|
Chris@26
|
873 if (m_plotStyle == PlotSegmentation) {
|
Chris@76
|
874
|
Chris@76
|
875 // std::cerr << "drawing rect" << std::endl;
|
Chris@26
|
876
|
Chris@27
|
877 if (nx <= x) continue;
|
Chris@26
|
878
|
Chris@28
|
879 if (illuminateFrame != p.frame &&
|
Chris@44
|
880 (nx < x + 5 || x >= v->width() - 1)) {
|
Chris@27
|
881 paint.setPen(Qt::NoPen);
|
Chris@27
|
882 }
|
Chris@26
|
883
|
Chris@44
|
884 paint.drawRect(x, -1, nx - x, v->height() + 1);
|
Chris@26
|
885 }
|
Chris@26
|
886
|
Chris@55
|
887 if (p.label != "") {
|
Chris@79
|
888 if (!haveNext || nx > x + 6 + paint.fontMetrics().width(p.label)) {
|
Chris@79
|
889 paint.drawText(x + 5, textY, p.label);
|
Chris@79
|
890 }
|
Chris@55
|
891 }
|
Chris@0
|
892 }
|
Chris@6
|
893
|
Chris@430
|
894 if ((m_plotStyle == PlotCurve || m_plotStyle == PlotLines)
|
Chris@430
|
895 && !path.isEmpty()) {
|
Chris@55
|
896 paint.setRenderHint(QPainter::Antialiasing, pointCount <= v->width());
|
Chris@6
|
897 paint.drawPath(path);
|
Chris@6
|
898 }
|
Chris@6
|
899
|
Chris@6
|
900 paint.restore();
|
Chris@6
|
901
|
Chris@6
|
902 // looks like save/restore doesn't deal with this:
|
Chris@6
|
903 paint.setRenderHint(QPainter::Antialiasing, false);
|
Chris@6
|
904 }
|
Chris@6
|
905
|
Chris@42
|
906 int
|
Chris@248
|
907 TimeValueLayer::getVerticalScaleWidth(View *, QPainter &paint) const
|
Chris@42
|
908 {
|
Chris@68
|
909 int w = paint.fontMetrics().width("-000.000");
|
Chris@68
|
910 if (m_plotStyle == PlotSegmentation) return w + 20;
|
Chris@68
|
911 else return w + 10;
|
Chris@42
|
912 }
|
Chris@42
|
913
|
Chris@42
|
914 void
|
Chris@248
|
915 TimeValueLayer::paintVerticalScale(View *v, QPainter &paint, QRect) const
|
Chris@42
|
916 {
|
Chris@42
|
917 if (!m_model) return;
|
Chris@42
|
918
|
Chris@68
|
919 int h = v->height();
|
Chris@68
|
920
|
Chris@68
|
921 int n = 10;
|
Chris@68
|
922
|
Chris@437
|
923 float min, max;
|
Chris@437
|
924 bool logarithmic;
|
Chris@437
|
925 getScaleExtents(v, min, max, logarithmic);
|
Chris@437
|
926
|
Chris@437
|
927 if (m_plotStyle == PlotSegmentation) {
|
Chris@437
|
928 QString unit;
|
Chris@437
|
929 getValueExtents(min, max, logarithmic, unit);
|
Chris@437
|
930 if (logarithmic) {
|
Chris@437
|
931 LogRange::mapRange(min, max);
|
Chris@437
|
932 }
|
Chris@437
|
933 }
|
Chris@437
|
934
|
Chris@68
|
935 float val = min;
|
Chris@68
|
936 float inc = (max - val) / n;
|
Chris@42
|
937
|
Chris@55
|
938 char buffer[40];
|
Chris@55
|
939
|
Chris@55
|
940 int w = getVerticalScaleWidth(v, paint);
|
Chris@55
|
941
|
Chris@68
|
942 int tx = 5;
|
Chris@68
|
943
|
Chris@68
|
944 int boxx = 5, boxy = 5;
|
Chris@68
|
945 if (m_model->getScaleUnits() != "") {
|
Chris@68
|
946 boxy += paint.fontMetrics().height();
|
Chris@68
|
947 }
|
Chris@68
|
948 int boxw = 10, boxh = h - boxy - 5;
|
Chris@68
|
949
|
Chris@68
|
950 if (m_plotStyle == PlotSegmentation) {
|
Chris@68
|
951 tx += boxx + boxw;
|
Chris@68
|
952 paint.drawRect(boxx, boxy, boxw, boxh);
|
Chris@68
|
953 }
|
Chris@68
|
954
|
Chris@68
|
955 if (m_plotStyle == PlotSegmentation) {
|
Chris@68
|
956 paint.save();
|
Chris@68
|
957 for (int y = 0; y < boxh; ++y) {
|
Chris@68
|
958 float val = ((boxh - y) * (max - min)) / boxh + min;
|
Chris@437
|
959 if (logarithmic) {
|
Chris@437
|
960 paint.setPen(getColourForValue(v, LogRange::unmap(val)));
|
Chris@437
|
961 } else {
|
Chris@437
|
962 paint.setPen(getColourForValue(v, val));
|
Chris@437
|
963 }
|
Chris@68
|
964 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
|
Chris@68
|
965 }
|
Chris@68
|
966 paint.restore();
|
Chris@68
|
967 }
|
Chris@434
|
968
|
Chris@434
|
969 float round = 1.f;
|
Chris@434
|
970 int dp = 0;
|
Chris@434
|
971 if (inc > 0) {
|
Chris@434
|
972 int prec = trunc(log10f(inc));
|
Chris@434
|
973 prec -= 1;
|
Chris@434
|
974 if (prec < 0) dp = -prec;
|
Chris@434
|
975 round = powf(10.f, prec);
|
Chris@434
|
976 // std::cerr << "inc = " << inc << ", round = " << round << std::endl;
|
Chris@434
|
977 }
|
Chris@68
|
978
|
Chris@434
|
979 int prevy = -1;
|
Chris@434
|
980
|
Chris@68
|
981 for (int i = 0; i < n; ++i) {
|
Chris@68
|
982
|
Chris@68
|
983 int y, ty;
|
Chris@68
|
984 bool drawText = true;
|
Chris@68
|
985
|
Chris@434
|
986 float dispval = val;
|
Chris@434
|
987
|
Chris@68
|
988 if (m_plotStyle == PlotSegmentation) {
|
Chris@68
|
989 y = boxy + int(boxh - ((val - min) * boxh) / (max - min));
|
Chris@68
|
990 ty = y;
|
Chris@68
|
991 } else {
|
Chris@437
|
992 if (i == n-1 &&
|
Chris@437
|
993 v->height() < paint.fontMetrics().height() * (n*2)) {
|
Chris@68
|
994 if (m_model->getScaleUnits() != "") drawText = false;
|
Chris@68
|
995 }
|
Chris@434
|
996 dispval = lrintf(val / round) * round;
|
Chris@434
|
997 // std::cerr << "val = " << val << ", dispval = " << dispval << std::endl;
|
Chris@437
|
998 if (logarithmic) {
|
Chris@437
|
999 y = getYForValue(v, LogRange::unmap(dispval));
|
Chris@437
|
1000 } else {
|
Chris@437
|
1001 y = getYForValue(v, dispval);
|
Chris@437
|
1002 }
|
Chris@68
|
1003 ty = y - paint.fontMetrics().height() +
|
Chris@434
|
1004 paint.fontMetrics().ascent() + 2;
|
Chris@434
|
1005
|
Chris@434
|
1006 if (prevy >= 0 && (prevy - y) < paint.fontMetrics().height()) {
|
Chris@434
|
1007 val += inc;
|
Chris@434
|
1008 continue;
|
Chris@434
|
1009 }
|
Chris@68
|
1010 }
|
Chris@68
|
1011
|
Chris@437
|
1012 if (logarithmic) {
|
Chris@437
|
1013 sprintf(buffer, "%.*g", dp < 2 ? 2 : dp, LogRange::unmap(dispval));
|
Chris@437
|
1014 } else {
|
Chris@437
|
1015 sprintf(buffer, "%.*f", dp, dispval);
|
Chris@437
|
1016 }
|
Chris@55
|
1017 QString label = QString(buffer);
|
Chris@68
|
1018
|
Chris@68
|
1019 if (m_plotStyle != PlotSegmentation) {
|
Chris@68
|
1020 paint.drawLine(w - 5, y, w, y);
|
Chris@68
|
1021 } else {
|
Chris@68
|
1022 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
|
Chris@68
|
1023 }
|
Chris@68
|
1024
|
Chris@434
|
1025 if (drawText) {
|
Chris@435
|
1026 if (m_plotStyle != PlotSegmentation) {
|
Chris@435
|
1027 paint.drawText(tx + w - paint.fontMetrics().width(label) - 8,
|
Chris@435
|
1028 ty, label);
|
Chris@435
|
1029 } else {
|
Chris@435
|
1030 paint.drawText(tx, ty, label);
|
Chris@435
|
1031 }
|
Chris@434
|
1032 }
|
Chris@434
|
1033
|
Chris@434
|
1034 prevy = y;
|
Chris@44
|
1035 val += inc;
|
Chris@42
|
1036 }
|
Chris@68
|
1037
|
Chris@67
|
1038 if (m_model->getScaleUnits() != "") {
|
Chris@67
|
1039 paint.drawText(5, 5 + paint.fontMetrics().ascent(),
|
Chris@67
|
1040 m_model->getScaleUnits());
|
Chris@67
|
1041 }
|
Chris@42
|
1042 }
|
Chris@42
|
1043
|
Chris@21
|
1044 void
|
Chris@44
|
1045 TimeValueLayer::drawStart(View *v, QMouseEvent *e)
|
Chris@21
|
1046 {
|
Chris@101
|
1047 // std::cerr << "TimeValueLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@21
|
1048
|
Chris@21
|
1049 if (!m_model) return;
|
Chris@21
|
1050
|
Chris@44
|
1051 long frame = v->getFrameForX(e->x());
|
Chris@76
|
1052 long resolution = m_model->getResolution();
|
Chris@21
|
1053 if (frame < 0) frame = 0;
|
Chris@76
|
1054 frame = (frame / resolution) * resolution;
|
Chris@21
|
1055
|
Chris@44
|
1056 float value = getValueForY(v, e->y());
|
Chris@21
|
1057
|
Chris@76
|
1058 bool havePoint = false;
|
Chris@76
|
1059
|
Chris@76
|
1060 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
|
Chris@76
|
1061 if (!points.empty()) {
|
Chris@76
|
1062 for (SparseTimeValueModel::PointList::iterator i = points.begin();
|
Chris@76
|
1063 i != points.end(); ++i) {
|
Chris@76
|
1064 if (((i->frame / resolution) * resolution) != frame) {
|
Chris@101
|
1065 // std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
|
Chris@76
|
1066 continue;
|
Chris@76
|
1067 }
|
Chris@76
|
1068 m_editingPoint = *i;
|
Chris@76
|
1069 havePoint = true;
|
Chris@76
|
1070 }
|
Chris@76
|
1071 }
|
Chris@76
|
1072
|
Chris@76
|
1073 if (!havePoint) {
|
Chris@76
|
1074 m_editingPoint = SparseTimeValueModel::Point
|
Chris@76
|
1075 (frame, value, tr("New Point"));
|
Chris@76
|
1076 }
|
Chris@76
|
1077
|
Chris@23
|
1078 m_originalPoint = m_editingPoint;
|
Chris@22
|
1079
|
Chris@376
|
1080 if (m_editingCommand) finish(m_editingCommand);
|
Chris@22
|
1081 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
|
Chris@22
|
1082 tr("Draw Point"));
|
Chris@76
|
1083 if (!havePoint) {
|
Chris@76
|
1084 m_editingCommand->addPoint(m_editingPoint);
|
Chris@76
|
1085 }
|
Chris@22
|
1086
|
Chris@21
|
1087 m_editing = true;
|
Chris@21
|
1088 }
|
Chris@21
|
1089
|
Chris@21
|
1090 void
|
Chris@44
|
1091 TimeValueLayer::drawDrag(View *v, QMouseEvent *e)
|
Chris@21
|
1092 {
|
Chris@101
|
1093 // std::cerr << "TimeValueLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@21
|
1094
|
Chris@21
|
1095 if (!m_model || !m_editing) return;
|
Chris@21
|
1096
|
Chris@44
|
1097 long frame = v->getFrameForX(e->x());
|
Chris@76
|
1098 long resolution = m_model->getResolution();
|
Chris@21
|
1099 if (frame < 0) frame = 0;
|
Chris@76
|
1100 frame = (frame / resolution) * resolution;
|
Chris@21
|
1101
|
Chris@44
|
1102 float value = getValueForY(v, e->y());
|
Chris@21
|
1103
|
Chris@76
|
1104 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
|
Chris@76
|
1105
|
Chris@101
|
1106 // std::cerr << points.size() << " points" << std::endl;
|
Chris@76
|
1107
|
Chris@76
|
1108 bool havePoint = false;
|
Chris@76
|
1109
|
Chris@76
|
1110 if (!points.empty()) {
|
Chris@76
|
1111 for (SparseTimeValueModel::PointList::iterator i = points.begin();
|
Chris@76
|
1112 i != points.end(); ++i) {
|
Chris@76
|
1113 if (i->frame == m_editingPoint.frame &&
|
Chris@76
|
1114 i->value == m_editingPoint.value) {
|
Chris@101
|
1115 // std::cerr << "ignoring current editing point at " << i->frame << ", " << i->value << std::endl;
|
Chris@76
|
1116 continue;
|
Chris@76
|
1117 }
|
Chris@76
|
1118 if (((i->frame / resolution) * resolution) != frame) {
|
Chris@101
|
1119 // std::cerr << "ignoring out-of-range frame at " << i->frame << std::endl;
|
Chris@76
|
1120 continue;
|
Chris@76
|
1121 }
|
Chris@101
|
1122 // std::cerr << "adjusting to new point at " << i->frame << ", " << i->value << std::endl;
|
Chris@76
|
1123 m_editingPoint = *i;
|
Chris@76
|
1124 m_originalPoint = m_editingPoint;
|
Chris@76
|
1125 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@76
|
1126 havePoint = true;
|
Chris@76
|
1127 }
|
Chris@76
|
1128 }
|
Chris@76
|
1129
|
Chris@76
|
1130 if (!havePoint) {
|
Chris@76
|
1131 if (frame == m_editingPoint.frame) {
|
Chris@76
|
1132 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@76
|
1133 }
|
Chris@76
|
1134 }
|
Chris@76
|
1135
|
Chris@76
|
1136 // m_editingCommand->deletePoint(m_editingPoint);
|
Chris@21
|
1137 m_editingPoint.frame = frame;
|
Chris@21
|
1138 m_editingPoint.value = value;
|
Chris@22
|
1139 m_editingCommand->addPoint(m_editingPoint);
|
Chris@21
|
1140 }
|
Chris@21
|
1141
|
Chris@21
|
1142 void
|
Chris@248
|
1143 TimeValueLayer::drawEnd(View *, QMouseEvent *)
|
Chris@21
|
1144 {
|
Chris@101
|
1145 // std::cerr << "TimeValueLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@21
|
1146 if (!m_model || !m_editing) return;
|
Chris@376
|
1147 finish(m_editingCommand);
|
Chris@22
|
1148 m_editingCommand = 0;
|
Chris@21
|
1149 m_editing = false;
|
Chris@21
|
1150 }
|
Chris@21
|
1151
|
Chris@21
|
1152 void
|
Chris@335
|
1153 TimeValueLayer::eraseStart(View *v, QMouseEvent *e)
|
Chris@335
|
1154 {
|
Chris@335
|
1155 if (!m_model) return;
|
Chris@335
|
1156
|
Chris@335
|
1157 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
|
Chris@335
|
1158 if (points.empty()) return;
|
Chris@335
|
1159
|
Chris@335
|
1160 m_editingPoint = *points.begin();
|
Chris@335
|
1161
|
Chris@335
|
1162 if (m_editingCommand) {
|
Chris@376
|
1163 finish(m_editingCommand);
|
Chris@335
|
1164 m_editingCommand = 0;
|
Chris@335
|
1165 }
|
Chris@335
|
1166
|
Chris@335
|
1167 m_editing = true;
|
Chris@335
|
1168 }
|
Chris@335
|
1169
|
Chris@335
|
1170 void
|
Chris@335
|
1171 TimeValueLayer::eraseDrag(View *v, QMouseEvent *e)
|
Chris@335
|
1172 {
|
Chris@335
|
1173 }
|
Chris@335
|
1174
|
Chris@335
|
1175 void
|
Chris@335
|
1176 TimeValueLayer::eraseEnd(View *v, QMouseEvent *e)
|
Chris@335
|
1177 {
|
Chris@335
|
1178 if (!m_model || !m_editing) return;
|
Chris@335
|
1179
|
Chris@335
|
1180 m_editing = false;
|
Chris@335
|
1181
|
Chris@335
|
1182 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
|
Chris@335
|
1183 if (points.empty()) return;
|
Chris@335
|
1184 if (points.begin()->frame != m_editingPoint.frame ||
|
Chris@335
|
1185 points.begin()->value != m_editingPoint.value) return;
|
Chris@335
|
1186
|
Chris@335
|
1187 m_editingCommand = new SparseTimeValueModel::EditCommand
|
Chris@335
|
1188 (m_model, tr("Erase Point"));
|
Chris@335
|
1189
|
Chris@335
|
1190 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@335
|
1191
|
Chris@376
|
1192 finish(m_editingCommand);
|
Chris@335
|
1193 m_editingCommand = 0;
|
Chris@335
|
1194 m_editing = false;
|
Chris@335
|
1195 }
|
Chris@335
|
1196
|
Chris@335
|
1197 void
|
Chris@44
|
1198 TimeValueLayer::editStart(View *v, QMouseEvent *e)
|
Chris@21
|
1199 {
|
Chris@101
|
1200 // std::cerr << "TimeValueLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@21
|
1201
|
Chris@21
|
1202 if (!m_model) return;
|
Chris@21
|
1203
|
Chris@44
|
1204 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
|
Chris@21
|
1205 if (points.empty()) return;
|
Chris@21
|
1206
|
Chris@21
|
1207 m_editingPoint = *points.begin();
|
Chris@23
|
1208 m_originalPoint = m_editingPoint;
|
Chris@22
|
1209
|
Chris@22
|
1210 if (m_editingCommand) {
|
Chris@376
|
1211 finish(m_editingCommand);
|
Chris@22
|
1212 m_editingCommand = 0;
|
Chris@22
|
1213 }
|
Chris@22
|
1214
|
Chris@21
|
1215 m_editing = true;
|
Chris@21
|
1216 }
|
Chris@21
|
1217
|
Chris@21
|
1218 void
|
Chris@44
|
1219 TimeValueLayer::editDrag(View *v, QMouseEvent *e)
|
Chris@21
|
1220 {
|
Chris@101
|
1221 // std::cerr << "TimeValueLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@21
|
1222
|
Chris@21
|
1223 if (!m_model || !m_editing) return;
|
Chris@21
|
1224
|
Chris@44
|
1225 long frame = v->getFrameForX(e->x());
|
Chris@21
|
1226 if (frame < 0) frame = 0;
|
Chris@21
|
1227 frame = frame / m_model->getResolution() * m_model->getResolution();
|
Chris@21
|
1228
|
Chris@44
|
1229 float value = getValueForY(v, e->y());
|
Chris@21
|
1230
|
Chris@22
|
1231 if (!m_editingCommand) {
|
Chris@22
|
1232 m_editingCommand = new SparseTimeValueModel::EditCommand(m_model,
|
Chris@22
|
1233 tr("Drag Point"));
|
Chris@22
|
1234 }
|
Chris@22
|
1235
|
Chris@22
|
1236 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@21
|
1237 m_editingPoint.frame = frame;
|
Chris@21
|
1238 m_editingPoint.value = value;
|
Chris@22
|
1239 m_editingCommand->addPoint(m_editingPoint);
|
Chris@21
|
1240 }
|
Chris@21
|
1241
|
Chris@21
|
1242 void
|
Chris@248
|
1243 TimeValueLayer::editEnd(View *, QMouseEvent *)
|
Chris@21
|
1244 {
|
Chris@101
|
1245 // std::cerr << "TimeValueLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@21
|
1246 if (!m_model || !m_editing) return;
|
Chris@23
|
1247
|
Chris@23
|
1248 if (m_editingCommand) {
|
Chris@23
|
1249
|
Chris@23
|
1250 QString newName = m_editingCommand->getName();
|
Chris@23
|
1251
|
Chris@23
|
1252 if (m_editingPoint.frame != m_originalPoint.frame) {
|
Chris@23
|
1253 if (m_editingPoint.value != m_originalPoint.value) {
|
Chris@23
|
1254 newName = tr("Edit Point");
|
Chris@23
|
1255 } else {
|
Chris@23
|
1256 newName = tr("Relocate Point");
|
Chris@23
|
1257 }
|
Chris@23
|
1258 } else {
|
Chris@23
|
1259 newName = tr("Change Point Value");
|
Chris@23
|
1260 }
|
Chris@23
|
1261
|
Chris@23
|
1262 m_editingCommand->setName(newName);
|
Chris@376
|
1263 finish(m_editingCommand);
|
Chris@23
|
1264 }
|
Chris@23
|
1265
|
Chris@22
|
1266 m_editingCommand = 0;
|
Chris@21
|
1267 m_editing = false;
|
Chris@21
|
1268 }
|
Chris@21
|
1269
|
Chris@255
|
1270 bool
|
Chris@70
|
1271 TimeValueLayer::editOpen(View *v, QMouseEvent *e)
|
Chris@70
|
1272 {
|
Chris@255
|
1273 if (!m_model) return false;
|
Chris@70
|
1274
|
Chris@70
|
1275 SparseTimeValueModel::PointList points = getLocalPoints(v, e->x());
|
Chris@255
|
1276 if (points.empty()) return false;
|
Chris@70
|
1277
|
Chris@70
|
1278 SparseTimeValueModel::Point point = *points.begin();
|
Chris@70
|
1279
|
Chris@70
|
1280 ItemEditDialog *dialog = new ItemEditDialog
|
Chris@70
|
1281 (m_model->getSampleRate(),
|
Chris@70
|
1282 ItemEditDialog::ShowTime |
|
Chris@70
|
1283 ItemEditDialog::ShowValue |
|
Chris@73
|
1284 ItemEditDialog::ShowText,
|
Chris@73
|
1285 m_model->getScaleUnits());
|
Chris@70
|
1286
|
Chris@70
|
1287 dialog->setFrameTime(point.frame);
|
Chris@70
|
1288 dialog->setValue(point.value);
|
Chris@70
|
1289 dialog->setText(point.label);
|
Chris@70
|
1290
|
Chris@70
|
1291 if (dialog->exec() == QDialog::Accepted) {
|
Chris@70
|
1292
|
Chris@70
|
1293 SparseTimeValueModel::Point newPoint = point;
|
Chris@70
|
1294 newPoint.frame = dialog->getFrameTime();
|
Chris@70
|
1295 newPoint.value = dialog->getValue();
|
Chris@70
|
1296 newPoint.label = dialog->getText();
|
Chris@70
|
1297
|
Chris@70
|
1298 SparseTimeValueModel::EditCommand *command =
|
Chris@70
|
1299 new SparseTimeValueModel::EditCommand(m_model, tr("Edit Point"));
|
Chris@70
|
1300 command->deletePoint(point);
|
Chris@70
|
1301 command->addPoint(newPoint);
|
Chris@376
|
1302 finish(command);
|
Chris@70
|
1303 }
|
Chris@70
|
1304
|
Chris@70
|
1305 delete dialog;
|
Chris@255
|
1306 return true;
|
Chris@70
|
1307 }
|
Chris@70
|
1308
|
Chris@70
|
1309 void
|
Chris@43
|
1310 TimeValueLayer::moveSelection(Selection s, size_t newStartFrame)
|
Chris@43
|
1311 {
|
Chris@99
|
1312 if (!m_model) return;
|
Chris@99
|
1313
|
Chris@43
|
1314 SparseTimeValueModel::EditCommand *command =
|
Chris@43
|
1315 new SparseTimeValueModel::EditCommand(m_model,
|
Chris@43
|
1316 tr("Drag Selection"));
|
Chris@43
|
1317
|
Chris@43
|
1318 SparseTimeValueModel::PointList points =
|
Chris@43
|
1319 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@43
|
1320
|
Chris@43
|
1321 for (SparseTimeValueModel::PointList::iterator i = points.begin();
|
Chris@43
|
1322 i != points.end(); ++i) {
|
Chris@43
|
1323
|
Chris@43
|
1324 if (s.contains(i->frame)) {
|
Chris@43
|
1325 SparseTimeValueModel::Point newPoint(*i);
|
Chris@43
|
1326 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
|
Chris@43
|
1327 command->deletePoint(*i);
|
Chris@43
|
1328 command->addPoint(newPoint);
|
Chris@43
|
1329 }
|
Chris@43
|
1330 }
|
Chris@43
|
1331
|
Chris@376
|
1332 finish(command);
|
Chris@43
|
1333 }
|
Chris@43
|
1334
|
Chris@43
|
1335 void
|
Chris@43
|
1336 TimeValueLayer::resizeSelection(Selection s, Selection newSize)
|
Chris@43
|
1337 {
|
Chris@99
|
1338 if (!m_model) return;
|
Chris@99
|
1339
|
Chris@43
|
1340 SparseTimeValueModel::EditCommand *command =
|
Chris@43
|
1341 new SparseTimeValueModel::EditCommand(m_model,
|
Chris@43
|
1342 tr("Resize Selection"));
|
Chris@43
|
1343
|
Chris@43
|
1344 SparseTimeValueModel::PointList points =
|
Chris@43
|
1345 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@43
|
1346
|
Chris@43
|
1347 double ratio =
|
Chris@43
|
1348 double(newSize.getEndFrame() - newSize.getStartFrame()) /
|
Chris@43
|
1349 double(s.getEndFrame() - s.getStartFrame());
|
Chris@43
|
1350
|
Chris@43
|
1351 for (SparseTimeValueModel::PointList::iterator i = points.begin();
|
Chris@43
|
1352 i != points.end(); ++i) {
|
Chris@43
|
1353
|
Chris@43
|
1354 if (s.contains(i->frame)) {
|
Chris@43
|
1355
|
Chris@43
|
1356 double target = i->frame;
|
Chris@43
|
1357 target = newSize.getStartFrame() +
|
Chris@43
|
1358 double(target - s.getStartFrame()) * ratio;
|
Chris@43
|
1359
|
Chris@43
|
1360 SparseTimeValueModel::Point newPoint(*i);
|
Chris@43
|
1361 newPoint.frame = lrint(target);
|
Chris@43
|
1362 command->deletePoint(*i);
|
Chris@43
|
1363 command->addPoint(newPoint);
|
Chris@43
|
1364 }
|
Chris@43
|
1365 }
|
Chris@43
|
1366
|
Chris@376
|
1367 finish(command);
|
Chris@43
|
1368 }
|
Chris@43
|
1369
|
Chris@76
|
1370 void
|
Chris@76
|
1371 TimeValueLayer::deleteSelection(Selection s)
|
Chris@76
|
1372 {
|
Chris@99
|
1373 if (!m_model) return;
|
Chris@99
|
1374
|
Chris@76
|
1375 SparseTimeValueModel::EditCommand *command =
|
Chris@76
|
1376 new SparseTimeValueModel::EditCommand(m_model,
|
Chris@76
|
1377 tr("Delete Selected Points"));
|
Chris@76
|
1378
|
Chris@76
|
1379 SparseTimeValueModel::PointList points =
|
Chris@76
|
1380 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@76
|
1381
|
Chris@76
|
1382 for (SparseTimeValueModel::PointList::iterator i = points.begin();
|
Chris@76
|
1383 i != points.end(); ++i) {
|
Chris@76
|
1384
|
Chris@76
|
1385 if (s.contains(i->frame)) {
|
Chris@76
|
1386 command->deletePoint(*i);
|
Chris@76
|
1387 }
|
Chris@76
|
1388 }
|
Chris@76
|
1389
|
Chris@376
|
1390 finish(command);
|
Chris@76
|
1391 }
|
Chris@76
|
1392
|
Chris@76
|
1393 void
|
Chris@359
|
1394 TimeValueLayer::copy(View *v, Selection s, Clipboard &to)
|
Chris@76
|
1395 {
|
Chris@99
|
1396 if (!m_model) return;
|
Chris@99
|
1397
|
Chris@76
|
1398 SparseTimeValueModel::PointList points =
|
Chris@76
|
1399 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@76
|
1400
|
Chris@76
|
1401 for (SparseTimeValueModel::PointList::iterator i = points.begin();
|
Chris@76
|
1402 i != points.end(); ++i) {
|
Chris@76
|
1403 if (s.contains(i->frame)) {
|
Chris@83
|
1404 Clipboard::Point point(i->frame, i->value, i->label);
|
Chris@360
|
1405 point.setReferenceFrame(alignToReference(v, i->frame));
|
Chris@76
|
1406 to.addPoint(point);
|
Chris@76
|
1407 }
|
Chris@76
|
1408 }
|
Chris@76
|
1409 }
|
Chris@76
|
1410
|
Chris@125
|
1411 bool
|
Chris@359
|
1412 TimeValueLayer::paste(View *v, const Clipboard &from, int frameOffset,
|
Chris@125
|
1413 bool interactive)
|
Chris@76
|
1414 {
|
Chris@125
|
1415 if (!m_model) return false;
|
Chris@99
|
1416
|
Chris@76
|
1417 const Clipboard::PointList &points = from.getPoints();
|
Chris@76
|
1418
|
Chris@360
|
1419 bool realign = false;
|
Chris@360
|
1420
|
Chris@360
|
1421 if (clipboardHasDifferentAlignment(v, from)) {
|
Chris@360
|
1422
|
Chris@360
|
1423 QMessageBox::StandardButton button =
|
Chris@360
|
1424 QMessageBox::question(v, tr("Re-align pasted items?"),
|
Chris@360
|
1425 tr("The items you are pasting came from a layer with different source material from this one. Do you want to re-align them in time, to match the source material for this layer?"),
|
Chris@360
|
1426 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
|
Chris@360
|
1427 QMessageBox::Yes);
|
Chris@360
|
1428
|
Chris@360
|
1429 if (button == QMessageBox::Cancel) {
|
Chris@360
|
1430 return false;
|
Chris@360
|
1431 }
|
Chris@360
|
1432
|
Chris@360
|
1433 if (button == QMessageBox::Yes) {
|
Chris@360
|
1434 realign = true;
|
Chris@360
|
1435 }
|
Chris@360
|
1436 }
|
Chris@360
|
1437
|
Chris@76
|
1438 SparseTimeValueModel::EditCommand *command =
|
Chris@76
|
1439 new SparseTimeValueModel::EditCommand(m_model, tr("Paste"));
|
Chris@76
|
1440
|
Chris@125
|
1441 enum ValueAvailability {
|
Chris@125
|
1442 UnknownAvailability,
|
Chris@125
|
1443 NoValues,
|
Chris@125
|
1444 SomeValues,
|
Chris@125
|
1445 AllValues
|
Chris@125
|
1446 };
|
Chris@125
|
1447
|
Chris@340
|
1448 Labeller::ValueType generation = Labeller::ValueNone;
|
Chris@125
|
1449
|
Chris@125
|
1450 bool haveUsableLabels = false;
|
Chris@125
|
1451 bool haveExistingItems = !(m_model->isEmpty());
|
Chris@340
|
1452 Labeller labeller;
|
Chris@340
|
1453 labeller.setSampleRate(m_model->getSampleRate());
|
Chris@125
|
1454
|
Chris@125
|
1455 if (interactive) {
|
Chris@125
|
1456
|
Chris@125
|
1457 ValueAvailability availability = UnknownAvailability;
|
Chris@125
|
1458
|
Chris@125
|
1459 for (Clipboard::PointList::const_iterator i = points.begin();
|
Chris@125
|
1460 i != points.end(); ++i) {
|
Chris@125
|
1461
|
Chris@125
|
1462 if (!i->haveFrame()) continue;
|
Chris@125
|
1463
|
Chris@125
|
1464 if (availability == UnknownAvailability) {
|
Chris@125
|
1465 if (i->haveValue()) availability = AllValues;
|
Chris@125
|
1466 else availability = NoValues;
|
Chris@125
|
1467 continue;
|
Chris@125
|
1468 }
|
Chris@125
|
1469
|
Chris@125
|
1470 if (i->haveValue()) {
|
Chris@125
|
1471 if (availability == NoValues) {
|
Chris@125
|
1472 availability = SomeValues;
|
Chris@125
|
1473 }
|
Chris@125
|
1474 } else {
|
Chris@125
|
1475 if (availability == AllValues) {
|
Chris@125
|
1476 availability = SomeValues;
|
Chris@125
|
1477 }
|
Chris@125
|
1478 }
|
Chris@125
|
1479
|
Chris@125
|
1480 if (!haveUsableLabels) {
|
Chris@125
|
1481 if (i->haveLabel()) {
|
Chris@125
|
1482 if (i->getLabel().contains(QRegExp("[0-9]"))) {
|
Chris@125
|
1483 haveUsableLabels = true;
|
Chris@125
|
1484 }
|
Chris@125
|
1485 }
|
Chris@125
|
1486 }
|
Chris@125
|
1487
|
Chris@125
|
1488 if (availability == SomeValues && haveUsableLabels) break;
|
Chris@125
|
1489 }
|
Chris@125
|
1490
|
Chris@125
|
1491 if (availability == NoValues || availability == SomeValues) {
|
Chris@125
|
1492
|
Chris@125
|
1493 QString text;
|
Chris@125
|
1494 if (availability == NoValues) {
|
Chris@125
|
1495 text = tr("The items you are pasting do not have values.\nWhat values do you want to use for these items?");
|
Chris@125
|
1496 } else {
|
Chris@125
|
1497 text = tr("Some of the items you are pasting do not have values.\nWhat values do you want to use for these items?");
|
Chris@125
|
1498 }
|
Chris@125
|
1499
|
Chris@340
|
1500 Labeller::TypeNameMap names = labeller.getTypeNames();
|
Chris@340
|
1501
|
Chris@125
|
1502 QStringList options;
|
Chris@340
|
1503 std::vector<Labeller::ValueType> genopts;
|
Chris@125
|
1504
|
Chris@340
|
1505 for (Labeller::TypeNameMap::const_iterator i = names.begin();
|
Chris@340
|
1506 i != names.end(); ++i) {
|
Chris@340
|
1507 if (i->first == Labeller::ValueNone) options << tr("Zero for all items");
|
Chris@340
|
1508 else options << i->second;
|
Chris@340
|
1509 genopts.push_back(i->first);
|
Chris@125
|
1510 }
|
Chris@125
|
1511
|
Chris@125
|
1512 static int prevSelection = 0;
|
Chris@125
|
1513
|
Chris@125
|
1514 bool ok = false;
|
Chris@125
|
1515 QString selected = ListInputDialog::getItem
|
Chris@125
|
1516 (0, tr("Choose value calculation"),
|
Chris@125
|
1517 text, options, prevSelection, &ok);
|
Chris@125
|
1518
|
Chris@125
|
1519 if (!ok) return false;
|
Chris@125
|
1520 int selection = 0;
|
Chris@340
|
1521 generation = Labeller::ValueNone;
|
Chris@125
|
1522
|
Chris@125
|
1523 for (QStringList::const_iterator i = options.begin();
|
Chris@125
|
1524 i != options.end(); ++i) {
|
Chris@125
|
1525 if (selected == *i) {
|
Chris@340
|
1526 generation = genopts[selection];
|
Chris@125
|
1527 break;
|
Chris@125
|
1528 }
|
Chris@125
|
1529 ++selection;
|
Chris@125
|
1530 }
|
Chris@340
|
1531
|
Chris@340
|
1532 labeller.setType(generation);
|
Chris@340
|
1533
|
Chris@340
|
1534 if (generation == Labeller::ValueFromCyclicalCounter ||
|
Chris@340
|
1535 generation == Labeller::ValueFromTwoLevelCounter) {
|
Chris@340
|
1536 int cycleSize = QInputDialog::getInteger
|
Chris@340
|
1537 (0, tr("Select cycle size"),
|
Chris@340
|
1538 tr("Cycle size:"), 4, 2, 16, 1);
|
Chris@340
|
1539 labeller.setCounterCycleSize(cycleSize);
|
Chris@340
|
1540 }
|
Chris@125
|
1541
|
Chris@125
|
1542 prevSelection = selection;
|
Chris@125
|
1543 }
|
Chris@125
|
1544 }
|
Chris@125
|
1545
|
Chris@372
|
1546 SparseTimeValueModel::Point prevPoint(0);
|
Chris@125
|
1547
|
Chris@76
|
1548 for (Clipboard::PointList::const_iterator i = points.begin();
|
Chris@76
|
1549 i != points.end(); ++i) {
|
Chris@76
|
1550
|
Chris@76
|
1551 if (!i->haveFrame()) continue;
|
Chris@360
|
1552
|
Chris@76
|
1553 size_t frame = 0;
|
Chris@360
|
1554
|
Chris@360
|
1555 if (!realign) {
|
Chris@360
|
1556
|
Chris@360
|
1557 frame = i->getFrame();
|
Chris@360
|
1558
|
Chris@360
|
1559 } else {
|
Chris@360
|
1560
|
Chris@360
|
1561 if (i->haveReferenceFrame()) {
|
Chris@360
|
1562 frame = i->getReferenceFrame();
|
Chris@360
|
1563 frame = alignFromReference(v, frame);
|
Chris@360
|
1564 } else {
|
Chris@360
|
1565 frame = i->getFrame();
|
Chris@360
|
1566 }
|
Chris@76
|
1567 }
|
Chris@360
|
1568
|
Chris@76
|
1569 SparseTimeValueModel::Point newPoint(frame);
|
Chris@76
|
1570
|
Chris@125
|
1571 if (i->haveLabel()) {
|
Chris@125
|
1572 newPoint.label = i->getLabel();
|
Chris@125
|
1573 } else if (i->haveValue()) {
|
Chris@125
|
1574 newPoint.label = QString("%1").arg(i->getValue());
|
Chris@125
|
1575 }
|
Chris@125
|
1576
|
Chris@372
|
1577 bool usePrev = false;
|
Chris@372
|
1578 SparseTimeValueModel::Point formerPrevPoint = prevPoint;
|
Chris@372
|
1579
|
Chris@125
|
1580 if (i->haveValue()) {
|
Chris@125
|
1581 newPoint.value = i->getValue();
|
Chris@125
|
1582 } else {
|
Chris@372
|
1583 // std::cerr << "Setting value on point at " << newPoint.frame << " from labeller";
|
Chris@372
|
1584 // if (i == points.begin()) {
|
Chris@372
|
1585 // std::cerr << ", no prev point" << std::endl;
|
Chris@372
|
1586 // } else {
|
Chris@372
|
1587 // std::cerr << ", prev point is at " << prevPoint.frame << std::endl;
|
Chris@372
|
1588 // }
|
Chris@340
|
1589 labeller.setValue<SparseTimeValueModel::Point>
|
Chris@371
|
1590 (newPoint, (i == points.begin()) ? 0 : &prevPoint);
|
Chris@372
|
1591 // std::cerr << "New point value = " << newPoint.value << std::endl;
|
Chris@372
|
1592 if (labeller.actingOnPrevPoint() && i != points.begin()) {
|
Chris@372
|
1593 usePrev = true;
|
Chris@372
|
1594 }
|
Chris@372
|
1595 }
|
Chris@372
|
1596
|
Chris@372
|
1597 if (usePrev) {
|
Chris@372
|
1598 command->deletePoint(formerPrevPoint);
|
Chris@372
|
1599 command->addPoint(prevPoint);
|
Chris@340
|
1600 }
|
Chris@125
|
1601
|
Chris@340
|
1602 prevPoint = newPoint;
|
Chris@76
|
1603 command->addPoint(newPoint);
|
Chris@76
|
1604 }
|
Chris@76
|
1605
|
Chris@376
|
1606 finish(command);
|
Chris@125
|
1607 return true;
|
Chris@360
|
1608 }
|
Chris@76
|
1609
|
Chris@316
|
1610 void
|
Chris@316
|
1611 TimeValueLayer::toXml(QTextStream &stream,
|
Chris@316
|
1612 QString indent, QString extraAttributes) const
|
Chris@6
|
1613 {
|
Chris@316
|
1614 SingleColourLayer::toXml(stream, indent,
|
Chris@316
|
1615 extraAttributes +
|
Chris@316
|
1616 QString(" colourMap=\"%1\" plotStyle=\"%2\" verticalScale=\"%3\"")
|
Chris@316
|
1617 .arg(m_colourMap)
|
Chris@316
|
1618 .arg(m_plotStyle)
|
Chris@316
|
1619 .arg(m_verticalScale));
|
Chris@0
|
1620 }
|
Chris@0
|
1621
|
Chris@11
|
1622 void
|
Chris@11
|
1623 TimeValueLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@11
|
1624 {
|
Chris@287
|
1625 SingleColourLayer::setProperties(attributes);
|
Chris@11
|
1626
|
Chris@11
|
1627 bool ok;
|
Chris@287
|
1628
|
Chris@287
|
1629 int cmap = attributes.value("colourMap").toInt(&ok);
|
Chris@287
|
1630 if (ok) setFillColourMap(cmap);
|
Chris@287
|
1631
|
Chris@11
|
1632 PlotStyle style = (PlotStyle)
|
Chris@11
|
1633 attributes.value("plotStyle").toInt(&ok);
|
Chris@11
|
1634 if (ok) setPlotStyle(style);
|
Chris@286
|
1635
|
Chris@286
|
1636 VerticalScale scale = (VerticalScale)
|
Chris@286
|
1637 attributes.value("verticalScale").toInt(&ok);
|
Chris@286
|
1638 if (ok) setVerticalScale(scale);
|
Chris@11
|
1639 }
|
Chris@11
|
1640
|