Chris@58
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@30
|
2
|
Chris@30
|
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@30
|
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@30
|
14 */
|
Chris@30
|
15
|
Chris@30
|
16 #include "NoteLayer.h"
|
Chris@30
|
17
|
Chris@128
|
18 #include "data/model/Model.h"
|
Chris@30
|
19 #include "base/RealTime.h"
|
Chris@30
|
20 #include "base/Profiler.h"
|
Chris@30
|
21 #include "base/Pitch.h"
|
Chris@197
|
22 #include "base/LogRange.h"
|
Chris@439
|
23 #include "base/RangeMapper.h"
|
Chris@376
|
24 #include "ColourDatabase.h"
|
Chris@128
|
25 #include "view/View.h"
|
Chris@30
|
26
|
Chris@128
|
27 #include "data/model/NoteModel.h"
|
Chris@30
|
28
|
Chris@70
|
29 #include "widgets/ItemEditDialog.h"
|
Chris@70
|
30
|
Chris@30
|
31 #include <QPainter>
|
Chris@30
|
32 #include <QPainterPath>
|
Chris@30
|
33 #include <QMouseEvent>
|
Chris@316
|
34 #include <QTextStream>
|
Chris@360
|
35 #include <QMessageBox>
|
Chris@30
|
36
|
Chris@30
|
37 #include <iostream>
|
Chris@30
|
38 #include <cmath>
|
Chris@30
|
39
|
Chris@44
|
40 NoteLayer::NoteLayer() :
|
Chris@287
|
41 SingleColourLayer(),
|
Chris@30
|
42 m_model(0),
|
Chris@30
|
43 m_editing(false),
|
Chris@335
|
44 m_originalPoint(0, 0.0, 0, 1.f, tr("New Point")),
|
Chris@335
|
45 m_editingPoint(0, 0.0, 0, 1.f, tr("New Point")),
|
Chris@30
|
46 m_editingCommand(0),
|
Chris@439
|
47 m_verticalScale(AutoAlignScale),
|
Chris@439
|
48 m_scaleMinimum(0),
|
Chris@439
|
49 m_scaleMaximum(0)
|
Chris@30
|
50 {
|
Chris@44
|
51
|
Chris@30
|
52 }
|
Chris@30
|
53
|
Chris@30
|
54 void
|
Chris@30
|
55 NoteLayer::setModel(NoteModel *model)
|
Chris@30
|
56 {
|
Chris@30
|
57 if (m_model == model) return;
|
Chris@30
|
58 m_model = model;
|
Chris@30
|
59
|
Chris@320
|
60 connectSignals(m_model);
|
Chris@30
|
61
|
Chris@101
|
62 // std::cerr << "NoteLayer::setModel(" << model << ")" << std::endl;
|
Chris@30
|
63
|
Chris@439
|
64 m_scaleMinimum = 0;
|
Chris@439
|
65 m_scaleMaximum = 0;
|
Chris@439
|
66
|
Chris@30
|
67 emit modelReplaced();
|
Chris@30
|
68 }
|
Chris@30
|
69
|
Chris@30
|
70 Layer::PropertyList
|
Chris@30
|
71 NoteLayer::getProperties() const
|
Chris@30
|
72 {
|
Chris@287
|
73 PropertyList list = SingleColourLayer::getProperties();
|
Chris@87
|
74 list.push_back("Vertical Scale");
|
Chris@100
|
75 list.push_back("Scale Units");
|
Chris@30
|
76 return list;
|
Chris@30
|
77 }
|
Chris@30
|
78
|
Chris@87
|
79 QString
|
Chris@87
|
80 NoteLayer::getPropertyLabel(const PropertyName &name) const
|
Chris@87
|
81 {
|
Chris@87
|
82 if (name == "Vertical Scale") return tr("Vertical Scale");
|
Chris@116
|
83 if (name == "Scale Units") return tr("Scale Units");
|
Chris@287
|
84 return SingleColourLayer::getPropertyLabel(name);
|
Chris@87
|
85 }
|
Chris@87
|
86
|
Chris@30
|
87 Layer::PropertyType
|
Chris@100
|
88 NoteLayer::getPropertyType(const PropertyName &name) const
|
Chris@30
|
89 {
|
Chris@100
|
90 if (name == "Scale Units") return UnitsProperty;
|
Chris@287
|
91 if (name == "Vertical Scale") return ValueProperty;
|
Chris@287
|
92 return SingleColourLayer::getPropertyType(name);
|
Chris@30
|
93 }
|
Chris@30
|
94
|
Chris@198
|
95 QString
|
Chris@198
|
96 NoteLayer::getPropertyGroupName(const PropertyName &name) const
|
Chris@198
|
97 {
|
Chris@198
|
98 if (name == "Vertical Scale" || name == "Scale Units") {
|
Chris@198
|
99 return tr("Scale");
|
Chris@198
|
100 }
|
Chris@287
|
101 return SingleColourLayer::getPropertyGroupName(name);
|
Chris@198
|
102 }
|
Chris@198
|
103
|
Chris@30
|
104 int
|
Chris@30
|
105 NoteLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@216
|
106 int *min, int *max, int *deflt) const
|
Chris@30
|
107 {
|
Chris@216
|
108 int val = 0;
|
Chris@30
|
109
|
Chris@287
|
110 if (name == "Vertical Scale") {
|
Chris@30
|
111
|
Chris@30
|
112 if (min) *min = 0;
|
Chris@101
|
113 if (max) *max = 3;
|
Chris@216
|
114 if (deflt) *deflt = int(AutoAlignScale);
|
Chris@30
|
115
|
Chris@216
|
116 val = int(m_verticalScale);
|
Chris@30
|
117
|
Chris@100
|
118 } else if (name == "Scale Units") {
|
Chris@100
|
119
|
Chris@216
|
120 if (deflt) *deflt = 0;
|
Chris@100
|
121 if (m_model) {
|
Chris@216
|
122 val = UnitDatabase::getInstance()->getUnitId
|
Chris@100
|
123 (m_model->getScaleUnits());
|
Chris@100
|
124 }
|
Chris@100
|
125
|
Chris@30
|
126 } else {
|
Chris@216
|
127
|
Chris@287
|
128 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
|
Chris@30
|
129 }
|
Chris@30
|
130
|
Chris@216
|
131 return val;
|
Chris@30
|
132 }
|
Chris@30
|
133
|
Chris@30
|
134 QString
|
Chris@30
|
135 NoteLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@287
|
136 int value) const
|
Chris@30
|
137 {
|
Chris@287
|
138 if (name == "Vertical Scale") {
|
Chris@30
|
139 switch (value) {
|
Chris@30
|
140 default:
|
Chris@101
|
141 case 0: return tr("Auto-Align");
|
Chris@198
|
142 case 1: return tr("Linear");
|
Chris@198
|
143 case 2: return tr("Log");
|
Chris@198
|
144 case 3: return tr("MIDI Notes");
|
Chris@30
|
145 }
|
Chris@30
|
146 }
|
Chris@287
|
147 return SingleColourLayer::getPropertyValueLabel(name, value);
|
Chris@30
|
148 }
|
Chris@30
|
149
|
Chris@30
|
150 void
|
Chris@30
|
151 NoteLayer::setProperty(const PropertyName &name, int value)
|
Chris@30
|
152 {
|
Chris@287
|
153 if (name == "Vertical Scale") {
|
Chris@30
|
154 setVerticalScale(VerticalScale(value));
|
Chris@100
|
155 } else if (name == "Scale Units") {
|
Chris@100
|
156 if (m_model) {
|
Chris@100
|
157 m_model->setScaleUnits
|
Chris@100
|
158 (UnitDatabase::getInstance()->getUnitById(value));
|
Chris@100
|
159 emit modelChanged();
|
Chris@100
|
160 }
|
Chris@287
|
161 } else {
|
Chris@287
|
162 return SingleColourLayer::setProperty(name, value);
|
Chris@30
|
163 }
|
Chris@30
|
164 }
|
Chris@30
|
165
|
Chris@30
|
166 void
|
Chris@30
|
167 NoteLayer::setVerticalScale(VerticalScale scale)
|
Chris@30
|
168 {
|
Chris@30
|
169 if (m_verticalScale == scale) return;
|
Chris@30
|
170 m_verticalScale = scale;
|
Chris@30
|
171 emit layerParametersChanged();
|
Chris@30
|
172 }
|
Chris@30
|
173
|
Chris@30
|
174 bool
|
Chris@44
|
175 NoteLayer::isLayerScrollable(const View *v) const
|
Chris@30
|
176 {
|
Chris@30
|
177 QPoint discard;
|
Chris@44
|
178 return !v->shouldIlluminateLocalFeatures(this, discard);
|
Chris@30
|
179 }
|
Chris@30
|
180
|
Chris@79
|
181 bool
|
Chris@101
|
182 NoteLayer::shouldConvertMIDIToHz() const
|
Chris@101
|
183 {
|
Chris@101
|
184 QString unit = m_model->getScaleUnits();
|
Chris@101
|
185 return (unit != "Hz");
|
Chris@101
|
186 // if (unit == "" ||
|
Chris@101
|
187 // unit.startsWith("MIDI") ||
|
Chris@101
|
188 // unit.startsWith("midi")) return true;
|
Chris@101
|
189 // return false;
|
Chris@101
|
190 }
|
Chris@101
|
191
|
Chris@101
|
192 bool
|
Chris@101
|
193 NoteLayer::getValueExtents(float &min, float &max,
|
Chris@101
|
194 bool &logarithmic, QString &unit) const
|
Chris@79
|
195 {
|
Chris@79
|
196 if (!m_model) return false;
|
Chris@79
|
197 min = m_model->getValueMinimum();
|
Chris@79
|
198 max = m_model->getValueMaximum();
|
Chris@101
|
199
|
Chris@105
|
200 if (shouldConvertMIDIToHz()) {
|
Chris@105
|
201 unit = "Hz";
|
Chris@105
|
202 min = Pitch::getFrequencyForPitch(lrintf(min));
|
Chris@105
|
203 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
|
Chris@105
|
204 } else unit = m_model->getScaleUnits();
|
Chris@101
|
205
|
Chris@101
|
206 if (m_verticalScale == MIDIRangeScale ||
|
Chris@101
|
207 m_verticalScale == LogScale) logarithmic = true;
|
Chris@101
|
208
|
Chris@101
|
209 return true;
|
Chris@101
|
210 }
|
Chris@101
|
211
|
Chris@101
|
212 bool
|
Chris@101
|
213 NoteLayer::getDisplayExtents(float &min, float &max) const
|
Chris@101
|
214 {
|
Chris@439
|
215 if (!m_model || shouldAutoAlign()) return false;
|
Chris@101
|
216
|
Chris@101
|
217 if (m_verticalScale == MIDIRangeScale) {
|
Chris@101
|
218 min = Pitch::getFrequencyForPitch(0);
|
Chris@101
|
219 max = Pitch::getFrequencyForPitch(127);
|
Chris@101
|
220 return true;
|
Chris@101
|
221 }
|
Chris@101
|
222
|
Chris@439
|
223 if (m_scaleMinimum == m_scaleMaximum) {
|
Chris@455
|
224 min = m_model->getValueMinimum();
|
Chris@455
|
225 max = m_model->getValueMaximum();
|
Chris@455
|
226 } else {
|
Chris@455
|
227 min = m_scaleMinimum;
|
Chris@455
|
228 max = m_scaleMaximum;
|
Chris@439
|
229 }
|
Chris@439
|
230
|
Chris@101
|
231 if (shouldConvertMIDIToHz()) {
|
Chris@101
|
232 min = Pitch::getFrequencyForPitch(lrintf(min));
|
Chris@101
|
233 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
|
Chris@101
|
234 }
|
Chris@101
|
235
|
Chris@79
|
236 return true;
|
Chris@79
|
237 }
|
Chris@79
|
238
|
Chris@439
|
239 bool
|
Chris@439
|
240 NoteLayer::setDisplayExtents(float min, float max)
|
Chris@439
|
241 {
|
Chris@439
|
242 if (!m_model) return false;
|
Chris@439
|
243
|
Chris@439
|
244 if (min == max) {
|
Chris@439
|
245 if (min == 0.f) {
|
Chris@439
|
246 max = 1.f;
|
Chris@439
|
247 } else {
|
Chris@439
|
248 max = min * 1.0001;
|
Chris@439
|
249 }
|
Chris@439
|
250 }
|
Chris@439
|
251
|
Chris@439
|
252 m_scaleMinimum = min;
|
Chris@439
|
253 m_scaleMaximum = max;
|
Chris@439
|
254
|
Chris@439
|
255 // std::cerr << "NoteLayer::setDisplayExtents: min = " << min << ", max = " << max << std::endl;
|
Chris@439
|
256
|
Chris@439
|
257 emit layerParametersChanged();
|
Chris@439
|
258 return true;
|
Chris@439
|
259 }
|
Chris@439
|
260
|
Chris@439
|
261 int
|
Chris@439
|
262 NoteLayer::getVerticalZoomSteps(int &defaultStep) const
|
Chris@439
|
263 {
|
Chris@439
|
264 if (shouldAutoAlign()) return 0;
|
Chris@439
|
265 if (!m_model) return 0;
|
Chris@439
|
266
|
Chris@439
|
267 defaultStep = 0;
|
Chris@439
|
268 return 100;
|
Chris@439
|
269 }
|
Chris@439
|
270
|
Chris@439
|
271 int
|
Chris@439
|
272 NoteLayer::getCurrentVerticalZoomStep() const
|
Chris@439
|
273 {
|
Chris@439
|
274 if (shouldAutoAlign()) return 0;
|
Chris@439
|
275 if (!m_model) return 0;
|
Chris@439
|
276
|
Chris@439
|
277 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
|
Chris@439
|
278 if (!mapper) return 0;
|
Chris@439
|
279
|
Chris@439
|
280 float dmin, dmax;
|
Chris@439
|
281 getDisplayExtents(dmin, dmax);
|
Chris@439
|
282
|
Chris@439
|
283 int nr = mapper->getPositionForValue(dmax - dmin);
|
Chris@439
|
284
|
Chris@439
|
285 delete mapper;
|
Chris@439
|
286
|
Chris@439
|
287 return 100 - nr;
|
Chris@439
|
288 }
|
Chris@439
|
289
|
Chris@439
|
290 //!!! lots of duplication with TimeValueLayer
|
Chris@439
|
291
|
Chris@439
|
292 void
|
Chris@439
|
293 NoteLayer::setVerticalZoomStep(int step)
|
Chris@439
|
294 {
|
Chris@439
|
295 if (shouldAutoAlign()) return;
|
Chris@439
|
296 if (!m_model) return;
|
Chris@439
|
297
|
Chris@439
|
298 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
|
Chris@439
|
299 if (!mapper) return;
|
Chris@439
|
300
|
Chris@439
|
301 float min, max;
|
Chris@439
|
302 bool logarithmic;
|
Chris@439
|
303 QString unit;
|
Chris@439
|
304 getValueExtents(min, max, logarithmic, unit);
|
Chris@439
|
305
|
Chris@439
|
306 float dmin, dmax;
|
Chris@439
|
307 getDisplayExtents(dmin, dmax);
|
Chris@439
|
308
|
Chris@439
|
309 float newdist = mapper->getValueForPosition(100 - step);
|
Chris@439
|
310
|
Chris@439
|
311 float newmin, newmax;
|
Chris@439
|
312
|
Chris@439
|
313 if (logarithmic) {
|
Chris@439
|
314
|
Chris@439
|
315 // see SpectrogramLayer::setVerticalZoomStep
|
Chris@439
|
316
|
Chris@439
|
317 newmax = (newdist + sqrtf(newdist*newdist + 4*dmin*dmax)) / 2;
|
Chris@439
|
318 newmin = newmax - newdist;
|
Chris@439
|
319
|
Chris@439
|
320 // std::cerr << "newmin = " << newmin << ", newmax = " << newmax << std::endl;
|
Chris@439
|
321
|
Chris@439
|
322 } else {
|
Chris@439
|
323 float dmid = (dmax + dmin) / 2;
|
Chris@439
|
324 newmin = dmid - newdist / 2;
|
Chris@439
|
325 newmax = dmid + newdist / 2;
|
Chris@439
|
326 }
|
Chris@439
|
327
|
Chris@439
|
328 if (newmin < min) {
|
Chris@439
|
329 newmax += (min - newmin);
|
Chris@439
|
330 newmin = min;
|
Chris@439
|
331 }
|
Chris@439
|
332 if (newmax > max) {
|
Chris@439
|
333 newmax = max;
|
Chris@439
|
334 }
|
Chris@439
|
335
|
Chris@439
|
336 std::cerr << "NoteLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << std::endl;
|
Chris@439
|
337
|
Chris@439
|
338 setDisplayExtents(newmin, newmax);
|
Chris@439
|
339 }
|
Chris@439
|
340
|
Chris@439
|
341 RangeMapper *
|
Chris@439
|
342 NoteLayer::getNewVerticalZoomRangeMapper() const
|
Chris@439
|
343 {
|
Chris@439
|
344 if (!m_model) return 0;
|
Chris@439
|
345
|
Chris@439
|
346 RangeMapper *mapper;
|
Chris@439
|
347
|
Chris@439
|
348 float min, max;
|
Chris@439
|
349 bool logarithmic;
|
Chris@439
|
350 QString unit;
|
Chris@439
|
351 getValueExtents(min, max, logarithmic, unit);
|
Chris@439
|
352
|
Chris@439
|
353 if (min == max) return 0;
|
Chris@439
|
354
|
Chris@439
|
355 if (logarithmic) {
|
Chris@439
|
356 mapper = new LogRangeMapper(0, 100, min, max, unit);
|
Chris@439
|
357 } else {
|
Chris@439
|
358 mapper = new LinearRangeMapper(0, 100, min, max, unit);
|
Chris@439
|
359 }
|
Chris@439
|
360
|
Chris@439
|
361 return mapper;
|
Chris@439
|
362 }
|
Chris@439
|
363
|
Chris@30
|
364 NoteModel::PointList
|
Chris@44
|
365 NoteLayer::getLocalPoints(View *v, int x) const
|
Chris@30
|
366 {
|
Chris@30
|
367 if (!m_model) return NoteModel::PointList();
|
Chris@30
|
368
|
Chris@44
|
369 long frame = v->getFrameForX(x);
|
Chris@30
|
370
|
Chris@30
|
371 NoteModel::PointList onPoints =
|
Chris@30
|
372 m_model->getPoints(frame);
|
Chris@30
|
373
|
Chris@30
|
374 if (!onPoints.empty()) {
|
Chris@30
|
375 return onPoints;
|
Chris@30
|
376 }
|
Chris@30
|
377
|
Chris@30
|
378 NoteModel::PointList prevPoints =
|
Chris@30
|
379 m_model->getPreviousPoints(frame);
|
Chris@30
|
380 NoteModel::PointList nextPoints =
|
Chris@30
|
381 m_model->getNextPoints(frame);
|
Chris@30
|
382
|
Chris@30
|
383 NoteModel::PointList usePoints = prevPoints;
|
Chris@30
|
384
|
Chris@30
|
385 if (prevPoints.empty()) {
|
Chris@30
|
386 usePoints = nextPoints;
|
Chris@248
|
387 } else if (long(prevPoints.begin()->frame) < v->getStartFrame() &&
|
Chris@44
|
388 !(nextPoints.begin()->frame > v->getEndFrame())) {
|
Chris@30
|
389 usePoints = nextPoints;
|
Chris@248
|
390 } else if (long(nextPoints.begin()->frame) - frame <
|
Chris@248
|
391 frame - long(prevPoints.begin()->frame)) {
|
Chris@30
|
392 usePoints = nextPoints;
|
Chris@30
|
393 }
|
Chris@30
|
394
|
Chris@30
|
395 if (!usePoints.empty()) {
|
Chris@30
|
396 int fuzz = 2;
|
Chris@44
|
397 int px = v->getXForFrame(usePoints.begin()->frame);
|
Chris@30
|
398 if ((px > x && px - x > fuzz) ||
|
Chris@30
|
399 (px < x && x - px > fuzz + 1)) {
|
Chris@30
|
400 usePoints.clear();
|
Chris@30
|
401 }
|
Chris@30
|
402 }
|
Chris@30
|
403
|
Chris@30
|
404 return usePoints;
|
Chris@30
|
405 }
|
Chris@30
|
406
|
Chris@30
|
407 QString
|
Chris@44
|
408 NoteLayer::getFeatureDescription(View *v, QPoint &pos) const
|
Chris@30
|
409 {
|
Chris@30
|
410 int x = pos.x();
|
Chris@30
|
411
|
Chris@30
|
412 if (!m_model || !m_model->getSampleRate()) return "";
|
Chris@30
|
413
|
Chris@44
|
414 NoteModel::PointList points = getLocalPoints(v, x);
|
Chris@30
|
415
|
Chris@30
|
416 if (points.empty()) {
|
Chris@30
|
417 if (!m_model->isReady()) {
|
Chris@30
|
418 return tr("In progress");
|
Chris@30
|
419 } else {
|
Chris@30
|
420 return tr("No local points");
|
Chris@30
|
421 }
|
Chris@30
|
422 }
|
Chris@30
|
423
|
Chris@30
|
424 Note note(0);
|
Chris@30
|
425 NoteModel::PointList::iterator i;
|
Chris@30
|
426
|
Chris@30
|
427 for (i = points.begin(); i != points.end(); ++i) {
|
Chris@30
|
428
|
Chris@44
|
429 int y = getYForValue(v, i->value);
|
Chris@30
|
430 int h = 3;
|
Chris@30
|
431
|
Chris@30
|
432 if (m_model->getValueQuantization() != 0.0) {
|
Chris@44
|
433 h = y - getYForValue(v, i->value + m_model->getValueQuantization());
|
Chris@30
|
434 if (h < 3) h = 3;
|
Chris@30
|
435 }
|
Chris@30
|
436
|
Chris@30
|
437 if (pos.y() >= y - h && pos.y() <= y) {
|
Chris@30
|
438 note = *i;
|
Chris@30
|
439 break;
|
Chris@30
|
440 }
|
Chris@30
|
441 }
|
Chris@30
|
442
|
Chris@30
|
443 if (i == points.end()) return tr("No local points");
|
Chris@30
|
444
|
Chris@30
|
445 RealTime rt = RealTime::frame2RealTime(note.frame,
|
Chris@30
|
446 m_model->getSampleRate());
|
Chris@30
|
447 RealTime rd = RealTime::frame2RealTime(note.duration,
|
Chris@30
|
448 m_model->getSampleRate());
|
Chris@30
|
449
|
Chris@101
|
450 QString pitchText;
|
Chris@101
|
451
|
Chris@101
|
452 if (shouldConvertMIDIToHz()) {
|
Chris@101
|
453
|
Chris@101
|
454 int mnote = lrintf(note.value);
|
Chris@101
|
455 int cents = lrintf((note.value - mnote) * 100);
|
Chris@101
|
456 float freq = Pitch::getFrequencyForPitch(mnote, cents);
|
Chris@544
|
457 pitchText = tr("%1 (%2, %3 Hz)")
|
Chris@544
|
458 .arg(Pitch::getPitchLabel(mnote, cents))
|
Chris@544
|
459 .arg(mnote)
|
Chris@544
|
460 .arg(freq);
|
Chris@101
|
461
|
Chris@101
|
462 } else if (m_model->getScaleUnits() == "Hz") {
|
Chris@101
|
463
|
Chris@544
|
464 pitchText = tr("%1 Hz (%2, %3)")
|
Chris@101
|
465 .arg(note.value)
|
Chris@544
|
466 .arg(Pitch::getPitchLabelForFrequency(note.value))
|
Chris@544
|
467 .arg(Pitch::getPitchForFrequency(note.value));
|
Chris@101
|
468
|
Chris@101
|
469 } else {
|
Chris@234
|
470 pitchText = tr("%1 %2")
|
Chris@101
|
471 .arg(note.value).arg(m_model->getScaleUnits());
|
Chris@101
|
472 }
|
Chris@101
|
473
|
Chris@30
|
474 QString text;
|
Chris@30
|
475
|
Chris@30
|
476 if (note.label == "") {
|
Chris@30
|
477 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label"))
|
Chris@30
|
478 .arg(rt.toText(true).c_str())
|
Chris@101
|
479 .arg(pitchText)
|
Chris@30
|
480 .arg(rd.toText(true).c_str());
|
Chris@30
|
481 } else {
|
Chris@30
|
482 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4"))
|
Chris@30
|
483 .arg(rt.toText(true).c_str())
|
Chris@101
|
484 .arg(pitchText)
|
Chris@30
|
485 .arg(rd.toText(true).c_str())
|
Chris@30
|
486 .arg(note.label);
|
Chris@30
|
487 }
|
Chris@30
|
488
|
Chris@44
|
489 pos = QPoint(v->getXForFrame(note.frame),
|
Chris@44
|
490 getYForValue(v, note.value));
|
Chris@30
|
491 return text;
|
Chris@30
|
492 }
|
Chris@30
|
493
|
Chris@30
|
494 bool
|
Chris@44
|
495 NoteLayer::snapToFeatureFrame(View *v, int &frame,
|
Chris@44
|
496 size_t &resolution,
|
Chris@44
|
497 SnapType snap) const
|
Chris@30
|
498 {
|
Chris@30
|
499 if (!m_model) {
|
Chris@44
|
500 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
|
Chris@30
|
501 }
|
Chris@30
|
502
|
Chris@30
|
503 resolution = m_model->getResolution();
|
Chris@30
|
504 NoteModel::PointList points;
|
Chris@30
|
505
|
Chris@30
|
506 if (snap == SnapNeighbouring) {
|
Chris@30
|
507
|
Chris@44
|
508 points = getLocalPoints(v, v->getXForFrame(frame));
|
Chris@30
|
509 if (points.empty()) return false;
|
Chris@30
|
510 frame = points.begin()->frame;
|
Chris@30
|
511 return true;
|
Chris@30
|
512 }
|
Chris@30
|
513
|
Chris@30
|
514 points = m_model->getPoints(frame, frame);
|
Chris@30
|
515 int snapped = frame;
|
Chris@30
|
516 bool found = false;
|
Chris@30
|
517
|
Chris@30
|
518 for (NoteModel::PointList::const_iterator i = points.begin();
|
Chris@30
|
519 i != points.end(); ++i) {
|
Chris@30
|
520
|
Chris@30
|
521 if (snap == SnapRight) {
|
Chris@30
|
522
|
Chris@30
|
523 if (i->frame > frame) {
|
Chris@30
|
524 snapped = i->frame;
|
Chris@30
|
525 found = true;
|
Chris@30
|
526 break;
|
Chris@30
|
527 }
|
Chris@30
|
528
|
Chris@30
|
529 } else if (snap == SnapLeft) {
|
Chris@30
|
530
|
Chris@30
|
531 if (i->frame <= frame) {
|
Chris@30
|
532 snapped = i->frame;
|
Chris@30
|
533 found = true; // don't break, as the next may be better
|
Chris@30
|
534 } else {
|
Chris@30
|
535 break;
|
Chris@30
|
536 }
|
Chris@30
|
537
|
Chris@30
|
538 } else { // nearest
|
Chris@30
|
539
|
Chris@30
|
540 NoteModel::PointList::const_iterator j = i;
|
Chris@30
|
541 ++j;
|
Chris@30
|
542
|
Chris@30
|
543 if (j == points.end()) {
|
Chris@30
|
544
|
Chris@30
|
545 snapped = i->frame;
|
Chris@30
|
546 found = true;
|
Chris@30
|
547 break;
|
Chris@30
|
548
|
Chris@30
|
549 } else if (j->frame >= frame) {
|
Chris@30
|
550
|
Chris@30
|
551 if (j->frame - frame < frame - i->frame) {
|
Chris@30
|
552 snapped = j->frame;
|
Chris@30
|
553 } else {
|
Chris@30
|
554 snapped = i->frame;
|
Chris@30
|
555 }
|
Chris@30
|
556 found = true;
|
Chris@30
|
557 break;
|
Chris@30
|
558 }
|
Chris@30
|
559 }
|
Chris@30
|
560 }
|
Chris@30
|
561
|
Chris@30
|
562 frame = snapped;
|
Chris@30
|
563 return found;
|
Chris@30
|
564 }
|
Chris@30
|
565
|
Chris@101
|
566 void
|
Chris@101
|
567 NoteLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
|
Chris@30
|
568 {
|
Chris@101
|
569 min = 0.0;
|
Chris@101
|
570 max = 0.0;
|
Chris@101
|
571 log = false;
|
Chris@42
|
572
|
Chris@101
|
573 QString queryUnits;
|
Chris@101
|
574 if (shouldConvertMIDIToHz()) queryUnits = "Hz";
|
Chris@101
|
575 else queryUnits = m_model->getScaleUnits();
|
Chris@30
|
576
|
Chris@439
|
577 if (shouldAutoAlign()) {
|
Chris@30
|
578
|
Chris@101
|
579 if (!v->getValueExtents(queryUnits, min, max, log)) {
|
Chris@30
|
580
|
Chris@101
|
581 min = m_model->getValueMinimum();
|
Chris@101
|
582 max = m_model->getValueMaximum();
|
Chris@42
|
583
|
Chris@101
|
584 if (shouldConvertMIDIToHz()) {
|
Chris@101
|
585 min = Pitch::getFrequencyForPitch(lrintf(min));
|
Chris@101
|
586 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
|
Chris@101
|
587 }
|
Chris@42
|
588
|
Chris@105
|
589 std::cerr << "NoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
|
Chris@105
|
590
|
Chris@101
|
591 } else if (log) {
|
Chris@101
|
592
|
Chris@197
|
593 LogRange::mapRange(min, max);
|
Chris@105
|
594
|
Chris@105
|
595 std::cerr << "NoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
|
Chris@105
|
596
|
Chris@101
|
597 }
|
Chris@101
|
598
|
Chris@101
|
599 } else {
|
Chris@101
|
600
|
Chris@439
|
601 getDisplayExtents(min, max);
|
Chris@101
|
602
|
Chris@101
|
603 if (m_verticalScale == MIDIRangeScale) {
|
Chris@101
|
604 min = Pitch::getFrequencyForPitch(0);
|
Chris@101
|
605 max = Pitch::getFrequencyForPitch(127);
|
Chris@101
|
606 } else if (shouldConvertMIDIToHz()) {
|
Chris@101
|
607 min = Pitch::getFrequencyForPitch(lrintf(min));
|
Chris@101
|
608 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
|
Chris@101
|
609 }
|
Chris@101
|
610
|
Chris@101
|
611 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
|
Chris@197
|
612 LogRange::mapRange(min, max);
|
Chris@101
|
613 log = true;
|
Chris@101
|
614 }
|
Chris@30
|
615 }
|
Chris@30
|
616
|
Chris@101
|
617 if (max == min) max = min + 1.0;
|
Chris@101
|
618 }
|
Chris@30
|
619
|
Chris@101
|
620 int
|
Chris@101
|
621 NoteLayer::getYForValue(View *v, float val) const
|
Chris@101
|
622 {
|
Chris@101
|
623 float min = 0.0, max = 0.0;
|
Chris@101
|
624 bool logarithmic = false;
|
Chris@101
|
625 int h = v->height();
|
Chris@101
|
626
|
Chris@101
|
627 getScaleExtents(v, min, max, logarithmic);
|
Chris@101
|
628
|
Chris@101
|
629 // std::cerr << "NoteLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << std::endl;
|
Chris@101
|
630
|
Chris@101
|
631 if (shouldConvertMIDIToHz()) {
|
Chris@101
|
632 val = Pitch::getFrequencyForPitch(lrintf(val),
|
Chris@101
|
633 lrintf((val - lrintf(val)) * 100));
|
Chris@101
|
634 // std::cerr << "shouldConvertMIDIToHz true, val now = " << val << std::endl;
|
Chris@101
|
635 }
|
Chris@101
|
636
|
Chris@101
|
637 if (logarithmic) {
|
Chris@197
|
638 val = LogRange::map(val);
|
Chris@101
|
639 // std::cerr << "logarithmic true, val now = " << val << std::endl;
|
Chris@101
|
640 }
|
Chris@101
|
641
|
Chris@101
|
642 int y = int(h - ((val - min) * h) / (max - min)) - 1;
|
Chris@101
|
643 // std::cerr << "y = " << y << std::endl;
|
Chris@101
|
644 return y;
|
Chris@30
|
645 }
|
Chris@30
|
646
|
Chris@30
|
647 float
|
Chris@44
|
648 NoteLayer::getValueForY(View *v, int y) const
|
Chris@30
|
649 {
|
Chris@101
|
650 float min = 0.0, max = 0.0;
|
Chris@101
|
651 bool logarithmic = false;
|
Chris@44
|
652 int h = v->height();
|
Chris@30
|
653
|
Chris@101
|
654 getScaleExtents(v, min, max, logarithmic);
|
Chris@101
|
655
|
Chris@101
|
656 float val = min + (float(h - y) * float(max - min)) / h;
|
Chris@101
|
657
|
Chris@101
|
658 if (logarithmic) {
|
Chris@197
|
659 val = powf(10.f, val);
|
Chris@101
|
660 }
|
Chris@101
|
661
|
Chris@101
|
662 if (shouldConvertMIDIToHz()) {
|
Chris@101
|
663 val = Pitch::getPitchForFrequency(val);
|
Chris@101
|
664 }
|
Chris@101
|
665
|
Chris@101
|
666 return val;
|
Chris@30
|
667 }
|
Chris@30
|
668
|
Chris@439
|
669 bool
|
Chris@439
|
670 NoteLayer::shouldAutoAlign() const
|
Chris@439
|
671 {
|
Chris@439
|
672 if (!m_model) return false;
|
Chris@439
|
673 return (m_verticalScale == AutoAlignScale);
|
Chris@439
|
674 }
|
Chris@439
|
675
|
Chris@30
|
676 void
|
Chris@44
|
677 NoteLayer::paint(View *v, QPainter &paint, QRect rect) const
|
Chris@30
|
678 {
|
Chris@30
|
679 if (!m_model || !m_model->isOK()) return;
|
Chris@30
|
680
|
Chris@30
|
681 int sampleRate = m_model->getSampleRate();
|
Chris@30
|
682 if (!sampleRate) return;
|
Chris@30
|
683
|
Chris@30
|
684 // Profiler profiler("NoteLayer::paint", true);
|
Chris@30
|
685
|
Chris@30
|
686 int x0 = rect.left(), x1 = rect.right();
|
Chris@44
|
687 long frame0 = v->getFrameForX(x0);
|
Chris@44
|
688 long frame1 = v->getFrameForX(x1);
|
Chris@30
|
689
|
Chris@30
|
690 NoteModel::PointList points(m_model->getPoints(frame0, frame1));
|
Chris@30
|
691 if (points.empty()) return;
|
Chris@30
|
692
|
Chris@287
|
693 paint.setPen(getBaseQColor());
|
Chris@30
|
694
|
Chris@287
|
695 QColor brushColour(getBaseQColor());
|
Chris@30
|
696 brushColour.setAlpha(80);
|
Chris@30
|
697
|
Chris@30
|
698 // std::cerr << "NoteLayer::paint: resolution is "
|
Chris@30
|
699 // << m_model->getResolution() << " frames" << std::endl;
|
Chris@30
|
700
|
Chris@30
|
701 float min = m_model->getValueMinimum();
|
Chris@30
|
702 float max = m_model->getValueMaximum();
|
Chris@30
|
703 if (max == min) max = min + 1.0;
|
Chris@30
|
704
|
Chris@30
|
705 QPoint localPos;
|
Chris@30
|
706 long illuminateFrame = -1;
|
Chris@30
|
707
|
Chris@44
|
708 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
|
Chris@30
|
709 NoteModel::PointList localPoints =
|
Chris@44
|
710 getLocalPoints(v, localPos.x());
|
Chris@30
|
711 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
|
Chris@30
|
712 }
|
Chris@30
|
713
|
Chris@30
|
714 paint.save();
|
Chris@30
|
715 paint.setRenderHint(QPainter::Antialiasing, false);
|
Chris@30
|
716
|
Chris@30
|
717 for (NoteModel::PointList::const_iterator i = points.begin();
|
Chris@30
|
718 i != points.end(); ++i) {
|
Chris@30
|
719
|
Chris@30
|
720 const NoteModel::Point &p(*i);
|
Chris@30
|
721
|
Chris@44
|
722 int x = v->getXForFrame(p.frame);
|
Chris@44
|
723 int y = getYForValue(v, p.value);
|
Chris@44
|
724 int w = v->getXForFrame(p.frame + p.duration) - x;
|
Chris@30
|
725 int h = 3;
|
Chris@30
|
726
|
Chris@30
|
727 if (m_model->getValueQuantization() != 0.0) {
|
Chris@44
|
728 h = y - getYForValue(v, p.value + m_model->getValueQuantization());
|
Chris@30
|
729 if (h < 3) h = 3;
|
Chris@30
|
730 }
|
Chris@30
|
731
|
Chris@30
|
732 if (w < 1) w = 1;
|
Chris@287
|
733 paint.setPen(getBaseQColor());
|
Chris@30
|
734 paint.setBrush(brushColour);
|
Chris@30
|
735
|
Chris@30
|
736 if (illuminateFrame == p.frame) {
|
Chris@30
|
737 if (localPos.y() >= y - h && localPos.y() < y) {
|
Chris@287
|
738 paint.setPen(v->getForeground());
|
Chris@287
|
739 paint.setBrush(v->getForeground());
|
Chris@30
|
740 }
|
Chris@30
|
741 }
|
Chris@30
|
742
|
Chris@124
|
743 paint.drawRect(x, y - h/2, w, h);
|
Chris@30
|
744
|
Chris@30
|
745 /// if (p.label != "") {
|
Chris@30
|
746 /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label);
|
Chris@30
|
747 /// }
|
Chris@30
|
748 }
|
Chris@30
|
749
|
Chris@30
|
750 paint.restore();
|
Chris@30
|
751 }
|
Chris@30
|
752
|
Chris@30
|
753 void
|
Chris@44
|
754 NoteLayer::drawStart(View *v, QMouseEvent *e)
|
Chris@30
|
755 {
|
Chris@101
|
756 // std::cerr << "NoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@30
|
757
|
Chris@30
|
758 if (!m_model) return;
|
Chris@30
|
759
|
Chris@44
|
760 long frame = v->getFrameForX(e->x());
|
Chris@30
|
761 if (frame < 0) frame = 0;
|
Chris@30
|
762 frame = frame / m_model->getResolution() * m_model->getResolution();
|
Chris@30
|
763
|
Chris@44
|
764 float value = getValueForY(v, e->y());
|
Chris@30
|
765
|
Chris@335
|
766 m_editingPoint = NoteModel::Point(frame, value, 0, 0.8, tr("New Point"));
|
Chris@30
|
767 m_originalPoint = m_editingPoint;
|
Chris@30
|
768
|
Chris@376
|
769 if (m_editingCommand) finish(m_editingCommand);
|
Chris@30
|
770 m_editingCommand = new NoteModel::EditCommand(m_model,
|
Chris@30
|
771 tr("Draw Point"));
|
Chris@30
|
772 m_editingCommand->addPoint(m_editingPoint);
|
Chris@30
|
773
|
Chris@30
|
774 m_editing = true;
|
Chris@30
|
775 }
|
Chris@30
|
776
|
Chris@30
|
777 void
|
Chris@44
|
778 NoteLayer::drawDrag(View *v, QMouseEvent *e)
|
Chris@30
|
779 {
|
Chris@101
|
780 // std::cerr << "NoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@30
|
781
|
Chris@30
|
782 if (!m_model || !m_editing) return;
|
Chris@30
|
783
|
Chris@44
|
784 long frame = v->getFrameForX(e->x());
|
Chris@30
|
785 if (frame < 0) frame = 0;
|
Chris@30
|
786 frame = frame / m_model->getResolution() * m_model->getResolution();
|
Chris@30
|
787
|
Chris@101
|
788 float newValue = getValueForY(v, e->y());
|
Chris@101
|
789
|
Chris@101
|
790 long newFrame = m_editingPoint.frame;
|
Chris@101
|
791 long newDuration = frame - newFrame;
|
Chris@101
|
792 if (newDuration < 0) {
|
Chris@101
|
793 newFrame = frame;
|
Chris@101
|
794 newDuration = -newDuration;
|
Chris@101
|
795 } else if (newDuration == 0) {
|
Chris@101
|
796 newDuration = 1;
|
Chris@101
|
797 }
|
Chris@30
|
798
|
Chris@30
|
799 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@101
|
800 m_editingPoint.frame = newFrame;
|
Chris@101
|
801 m_editingPoint.value = newValue;
|
Chris@101
|
802 m_editingPoint.duration = newDuration;
|
Chris@30
|
803 m_editingCommand->addPoint(m_editingPoint);
|
Chris@30
|
804 }
|
Chris@30
|
805
|
Chris@30
|
806 void
|
Chris@248
|
807 NoteLayer::drawEnd(View *, QMouseEvent *)
|
Chris@30
|
808 {
|
Chris@101
|
809 // std::cerr << "NoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@30
|
810 if (!m_model || !m_editing) return;
|
Chris@376
|
811 finish(m_editingCommand);
|
Chris@30
|
812 m_editingCommand = 0;
|
Chris@30
|
813 m_editing = false;
|
Chris@30
|
814 }
|
Chris@30
|
815
|
Chris@30
|
816 void
|
Chris@335
|
817 NoteLayer::eraseStart(View *v, QMouseEvent *e)
|
Chris@335
|
818 {
|
Chris@335
|
819 if (!m_model) return;
|
Chris@335
|
820
|
Chris@335
|
821 NoteModel::PointList points = getLocalPoints(v, e->x());
|
Chris@335
|
822 if (points.empty()) return;
|
Chris@335
|
823
|
Chris@335
|
824 m_editingPoint = *points.begin();
|
Chris@335
|
825
|
Chris@335
|
826 if (m_editingCommand) {
|
Chris@376
|
827 finish(m_editingCommand);
|
Chris@335
|
828 m_editingCommand = 0;
|
Chris@335
|
829 }
|
Chris@335
|
830
|
Chris@335
|
831 m_editing = true;
|
Chris@335
|
832 }
|
Chris@335
|
833
|
Chris@335
|
834 void
|
Chris@335
|
835 NoteLayer::eraseDrag(View *v, QMouseEvent *e)
|
Chris@335
|
836 {
|
Chris@335
|
837 }
|
Chris@335
|
838
|
Chris@335
|
839 void
|
Chris@335
|
840 NoteLayer::eraseEnd(View *v, QMouseEvent *e)
|
Chris@335
|
841 {
|
Chris@335
|
842 if (!m_model || !m_editing) return;
|
Chris@335
|
843
|
Chris@335
|
844 m_editing = false;
|
Chris@335
|
845
|
Chris@335
|
846 NoteModel::PointList points = getLocalPoints(v, e->x());
|
Chris@335
|
847 if (points.empty()) return;
|
Chris@335
|
848 if (points.begin()->frame != m_editingPoint.frame ||
|
Chris@335
|
849 points.begin()->value != m_editingPoint.value) return;
|
Chris@335
|
850
|
Chris@335
|
851 m_editingCommand = new NoteModel::EditCommand
|
Chris@335
|
852 (m_model, tr("Erase Point"));
|
Chris@335
|
853
|
Chris@335
|
854 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@335
|
855
|
Chris@376
|
856 finish(m_editingCommand);
|
Chris@335
|
857 m_editingCommand = 0;
|
Chris@335
|
858 m_editing = false;
|
Chris@335
|
859 }
|
Chris@335
|
860
|
Chris@335
|
861 void
|
Chris@44
|
862 NoteLayer::editStart(View *v, QMouseEvent *e)
|
Chris@30
|
863 {
|
Chris@101
|
864 // std::cerr << "NoteLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@30
|
865
|
Chris@30
|
866 if (!m_model) return;
|
Chris@30
|
867
|
Chris@44
|
868 NoteModel::PointList points = getLocalPoints(v, e->x());
|
Chris@30
|
869 if (points.empty()) return;
|
Chris@30
|
870
|
Chris@30
|
871 m_editingPoint = *points.begin();
|
Chris@30
|
872 m_originalPoint = m_editingPoint;
|
Chris@30
|
873
|
Chris@30
|
874 if (m_editingCommand) {
|
Chris@376
|
875 finish(m_editingCommand);
|
Chris@30
|
876 m_editingCommand = 0;
|
Chris@30
|
877 }
|
Chris@30
|
878
|
Chris@30
|
879 m_editing = true;
|
Chris@30
|
880 }
|
Chris@30
|
881
|
Chris@30
|
882 void
|
Chris@44
|
883 NoteLayer::editDrag(View *v, QMouseEvent *e)
|
Chris@30
|
884 {
|
Chris@101
|
885 // std::cerr << "NoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@30
|
886
|
Chris@30
|
887 if (!m_model || !m_editing) return;
|
Chris@30
|
888
|
Chris@44
|
889 long frame = v->getFrameForX(e->x());
|
Chris@30
|
890 if (frame < 0) frame = 0;
|
Chris@30
|
891 frame = frame / m_model->getResolution() * m_model->getResolution();
|
Chris@30
|
892
|
Chris@44
|
893 float value = getValueForY(v, e->y());
|
Chris@30
|
894
|
Chris@30
|
895 if (!m_editingCommand) {
|
Chris@30
|
896 m_editingCommand = new NoteModel::EditCommand(m_model,
|
Chris@30
|
897 tr("Drag Point"));
|
Chris@30
|
898 }
|
Chris@30
|
899
|
Chris@30
|
900 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@30
|
901 m_editingPoint.frame = frame;
|
Chris@30
|
902 m_editingPoint.value = value;
|
Chris@30
|
903 m_editingCommand->addPoint(m_editingPoint);
|
Chris@30
|
904 }
|
Chris@30
|
905
|
Chris@30
|
906 void
|
Chris@248
|
907 NoteLayer::editEnd(View *, QMouseEvent *)
|
Chris@30
|
908 {
|
Chris@101
|
909 // std::cerr << "NoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@30
|
910 if (!m_model || !m_editing) return;
|
Chris@30
|
911
|
Chris@30
|
912 if (m_editingCommand) {
|
Chris@30
|
913
|
Chris@30
|
914 QString newName = m_editingCommand->getName();
|
Chris@30
|
915
|
Chris@30
|
916 if (m_editingPoint.frame != m_originalPoint.frame) {
|
Chris@30
|
917 if (m_editingPoint.value != m_originalPoint.value) {
|
Chris@30
|
918 newName = tr("Edit Point");
|
Chris@30
|
919 } else {
|
Chris@30
|
920 newName = tr("Relocate Point");
|
Chris@30
|
921 }
|
Chris@30
|
922 } else {
|
Chris@30
|
923 newName = tr("Change Point Value");
|
Chris@30
|
924 }
|
Chris@30
|
925
|
Chris@30
|
926 m_editingCommand->setName(newName);
|
Chris@376
|
927 finish(m_editingCommand);
|
Chris@30
|
928 }
|
Chris@30
|
929
|
Chris@30
|
930 m_editingCommand = 0;
|
Chris@30
|
931 m_editing = false;
|
Chris@30
|
932 }
|
Chris@30
|
933
|
Chris@255
|
934 bool
|
Chris@70
|
935 NoteLayer::editOpen(View *v, QMouseEvent *e)
|
Chris@70
|
936 {
|
Chris@255
|
937 if (!m_model) return false;
|
Chris@70
|
938
|
Chris@70
|
939 NoteModel::PointList points = getLocalPoints(v, e->x());
|
Chris@255
|
940 if (points.empty()) return false;
|
Chris@70
|
941
|
Chris@70
|
942 NoteModel::Point note = *points.begin();
|
Chris@70
|
943
|
Chris@70
|
944 ItemEditDialog *dialog = new ItemEditDialog
|
Chris@70
|
945 (m_model->getSampleRate(),
|
Chris@70
|
946 ItemEditDialog::ShowTime |
|
Chris@70
|
947 ItemEditDialog::ShowDuration |
|
Chris@70
|
948 ItemEditDialog::ShowValue |
|
Chris@100
|
949 ItemEditDialog::ShowText,
|
Chris@100
|
950 m_model->getScaleUnits());
|
Chris@70
|
951
|
Chris@70
|
952 dialog->setFrameTime(note.frame);
|
Chris@70
|
953 dialog->setValue(note.value);
|
Chris@70
|
954 dialog->setFrameDuration(note.duration);
|
Chris@70
|
955 dialog->setText(note.label);
|
Chris@70
|
956
|
Chris@70
|
957 if (dialog->exec() == QDialog::Accepted) {
|
Chris@70
|
958
|
Chris@70
|
959 NoteModel::Point newNote = note;
|
Chris@70
|
960 newNote.frame = dialog->getFrameTime();
|
Chris@70
|
961 newNote.value = dialog->getValue();
|
Chris@70
|
962 newNote.duration = dialog->getFrameDuration();
|
Chris@70
|
963 newNote.label = dialog->getText();
|
Chris@70
|
964
|
Chris@70
|
965 NoteModel::EditCommand *command = new NoteModel::EditCommand
|
Chris@70
|
966 (m_model, tr("Edit Point"));
|
Chris@70
|
967 command->deletePoint(note);
|
Chris@70
|
968 command->addPoint(newNote);
|
Chris@376
|
969 finish(command);
|
Chris@70
|
970 }
|
Chris@70
|
971
|
Chris@70
|
972 delete dialog;
|
Chris@255
|
973 return true;
|
Chris@70
|
974 }
|
Chris@70
|
975
|
Chris@70
|
976 void
|
Chris@43
|
977 NoteLayer::moveSelection(Selection s, size_t newStartFrame)
|
Chris@43
|
978 {
|
Chris@99
|
979 if (!m_model) return;
|
Chris@99
|
980
|
Chris@43
|
981 NoteModel::EditCommand *command =
|
Chris@43
|
982 new NoteModel::EditCommand(m_model, tr("Drag Selection"));
|
Chris@43
|
983
|
Chris@43
|
984 NoteModel::PointList points =
|
Chris@43
|
985 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@43
|
986
|
Chris@43
|
987 for (NoteModel::PointList::iterator i = points.begin();
|
Chris@43
|
988 i != points.end(); ++i) {
|
Chris@43
|
989
|
Chris@43
|
990 if (s.contains(i->frame)) {
|
Chris@43
|
991 NoteModel::Point newPoint(*i);
|
Chris@43
|
992 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
|
Chris@43
|
993 command->deletePoint(*i);
|
Chris@43
|
994 command->addPoint(newPoint);
|
Chris@43
|
995 }
|
Chris@43
|
996 }
|
Chris@43
|
997
|
Chris@376
|
998 finish(command);
|
Chris@43
|
999 }
|
Chris@43
|
1000
|
Chris@43
|
1001 void
|
Chris@43
|
1002 NoteLayer::resizeSelection(Selection s, Selection newSize)
|
Chris@43
|
1003 {
|
Chris@99
|
1004 if (!m_model) return;
|
Chris@99
|
1005
|
Chris@43
|
1006 NoteModel::EditCommand *command =
|
Chris@43
|
1007 new NoteModel::EditCommand(m_model, tr("Resize Selection"));
|
Chris@43
|
1008
|
Chris@43
|
1009 NoteModel::PointList points =
|
Chris@43
|
1010 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@43
|
1011
|
Chris@43
|
1012 double ratio =
|
Chris@43
|
1013 double(newSize.getEndFrame() - newSize.getStartFrame()) /
|
Chris@43
|
1014 double(s.getEndFrame() - s.getStartFrame());
|
Chris@43
|
1015
|
Chris@43
|
1016 for (NoteModel::PointList::iterator i = points.begin();
|
Chris@43
|
1017 i != points.end(); ++i) {
|
Chris@43
|
1018
|
Chris@43
|
1019 if (s.contains(i->frame)) {
|
Chris@43
|
1020
|
Chris@43
|
1021 double targetStart = i->frame;
|
Chris@43
|
1022 targetStart = newSize.getStartFrame() +
|
Chris@43
|
1023 double(targetStart - s.getStartFrame()) * ratio;
|
Chris@43
|
1024
|
Chris@43
|
1025 double targetEnd = i->frame + i->duration;
|
Chris@43
|
1026 targetEnd = newSize.getStartFrame() +
|
Chris@43
|
1027 double(targetEnd - s.getStartFrame()) * ratio;
|
Chris@43
|
1028
|
Chris@43
|
1029 NoteModel::Point newPoint(*i);
|
Chris@43
|
1030 newPoint.frame = lrint(targetStart);
|
Chris@43
|
1031 newPoint.duration = lrint(targetEnd - targetStart);
|
Chris@43
|
1032 command->deletePoint(*i);
|
Chris@43
|
1033 command->addPoint(newPoint);
|
Chris@43
|
1034 }
|
Chris@43
|
1035 }
|
Chris@43
|
1036
|
Chris@376
|
1037 finish(command);
|
Chris@43
|
1038 }
|
Chris@43
|
1039
|
Chris@76
|
1040 void
|
Chris@76
|
1041 NoteLayer::deleteSelection(Selection s)
|
Chris@76
|
1042 {
|
Chris@99
|
1043 if (!m_model) return;
|
Chris@99
|
1044
|
Chris@76
|
1045 NoteModel::EditCommand *command =
|
Chris@76
|
1046 new NoteModel::EditCommand(m_model, tr("Delete Selected Points"));
|
Chris@76
|
1047
|
Chris@76
|
1048 NoteModel::PointList points =
|
Chris@76
|
1049 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@76
|
1050
|
Chris@76
|
1051 for (NoteModel::PointList::iterator i = points.begin();
|
Chris@76
|
1052 i != points.end(); ++i) {
|
Chris@76
|
1053
|
Chris@76
|
1054 if (s.contains(i->frame)) {
|
Chris@76
|
1055 command->deletePoint(*i);
|
Chris@76
|
1056 }
|
Chris@76
|
1057 }
|
Chris@76
|
1058
|
Chris@376
|
1059 finish(command);
|
Chris@76
|
1060 }
|
Chris@76
|
1061
|
Chris@76
|
1062 void
|
Chris@359
|
1063 NoteLayer::copy(View *v, Selection s, Clipboard &to)
|
Chris@76
|
1064 {
|
Chris@99
|
1065 if (!m_model) return;
|
Chris@99
|
1066
|
Chris@76
|
1067 NoteModel::PointList points =
|
Chris@76
|
1068 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@76
|
1069
|
Chris@76
|
1070 for (NoteModel::PointList::iterator i = points.begin();
|
Chris@76
|
1071 i != points.end(); ++i) {
|
Chris@76
|
1072 if (s.contains(i->frame)) {
|
Chris@335
|
1073 Clipboard::Point point(i->frame, i->value, i->duration, i->level, i->label);
|
Chris@360
|
1074 point.setReferenceFrame(alignToReference(v, i->frame));
|
Chris@76
|
1075 to.addPoint(point);
|
Chris@76
|
1076 }
|
Chris@76
|
1077 }
|
Chris@76
|
1078 }
|
Chris@76
|
1079
|
Chris@125
|
1080 bool
|
Chris@359
|
1081 NoteLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */)
|
Chris@76
|
1082 {
|
Chris@125
|
1083 if (!m_model) return false;
|
Chris@99
|
1084
|
Chris@76
|
1085 const Clipboard::PointList &points = from.getPoints();
|
Chris@76
|
1086
|
Chris@360
|
1087 bool realign = false;
|
Chris@360
|
1088
|
Chris@360
|
1089 if (clipboardHasDifferentAlignment(v, from)) {
|
Chris@360
|
1090
|
Chris@360
|
1091 QMessageBox::StandardButton button =
|
Chris@360
|
1092 QMessageBox::question(v, tr("Re-align pasted items?"),
|
Chris@360
|
1093 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
|
1094 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
|
Chris@360
|
1095 QMessageBox::Yes);
|
Chris@360
|
1096
|
Chris@360
|
1097 if (button == QMessageBox::Cancel) {
|
Chris@360
|
1098 return false;
|
Chris@360
|
1099 }
|
Chris@360
|
1100
|
Chris@360
|
1101 if (button == QMessageBox::Yes) {
|
Chris@360
|
1102 realign = true;
|
Chris@360
|
1103 }
|
Chris@360
|
1104 }
|
Chris@360
|
1105
|
Chris@76
|
1106 NoteModel::EditCommand *command =
|
Chris@76
|
1107 new NoteModel::EditCommand(m_model, tr("Paste"));
|
Chris@76
|
1108
|
Chris@76
|
1109 for (Clipboard::PointList::const_iterator i = points.begin();
|
Chris@76
|
1110 i != points.end(); ++i) {
|
Chris@76
|
1111
|
Chris@76
|
1112 if (!i->haveFrame()) continue;
|
Chris@76
|
1113 size_t frame = 0;
|
Chris@360
|
1114
|
Chris@360
|
1115 if (!realign) {
|
Chris@360
|
1116
|
Chris@360
|
1117 frame = i->getFrame();
|
Chris@360
|
1118
|
Chris@360
|
1119 } else {
|
Chris@360
|
1120
|
Chris@360
|
1121 if (i->haveReferenceFrame()) {
|
Chris@360
|
1122 frame = i->getReferenceFrame();
|
Chris@360
|
1123 frame = alignFromReference(v, frame);
|
Chris@360
|
1124 } else {
|
Chris@360
|
1125 frame = i->getFrame();
|
Chris@360
|
1126 }
|
Chris@76
|
1127 }
|
Chris@360
|
1128
|
Chris@76
|
1129 NoteModel::Point newPoint(frame);
|
Chris@76
|
1130
|
Chris@76
|
1131 if (i->haveLabel()) newPoint.label = i->getLabel();
|
Chris@76
|
1132 if (i->haveValue()) newPoint.value = i->getValue();
|
Chris@76
|
1133 else newPoint.value = (m_model->getValueMinimum() +
|
Chris@76
|
1134 m_model->getValueMaximum()) / 2;
|
Chris@335
|
1135 if (i->haveLevel()) newPoint.level = i->getLevel();
|
Chris@76
|
1136 if (i->haveDuration()) newPoint.duration = i->getDuration();
|
Chris@125
|
1137 else {
|
Chris@125
|
1138 size_t nextFrame = frame;
|
Chris@125
|
1139 Clipboard::PointList::const_iterator j = i;
|
Chris@125
|
1140 for (; j != points.end(); ++j) {
|
Chris@125
|
1141 if (!j->haveFrame()) continue;
|
Chris@125
|
1142 if (j != i) break;
|
Chris@125
|
1143 }
|
Chris@125
|
1144 if (j != points.end()) {
|
Chris@125
|
1145 nextFrame = j->getFrame();
|
Chris@125
|
1146 }
|
Chris@125
|
1147 if (nextFrame == frame) {
|
Chris@125
|
1148 newPoint.duration = m_model->getResolution();
|
Chris@125
|
1149 } else {
|
Chris@125
|
1150 newPoint.duration = nextFrame - frame;
|
Chris@125
|
1151 }
|
Chris@125
|
1152 }
|
Chris@76
|
1153
|
Chris@76
|
1154 command->addPoint(newPoint);
|
Chris@76
|
1155 }
|
Chris@76
|
1156
|
Chris@376
|
1157 finish(command);
|
Chris@125
|
1158 return true;
|
Chris@76
|
1159 }
|
Chris@76
|
1160
|
Chris@507
|
1161 void
|
Chris@507
|
1162 NoteLayer::addNoteOn(long frame, int pitch, int velocity)
|
Chris@507
|
1163 {
|
Chris@507
|
1164 m_pendingNoteOns.insert(Note(frame, pitch, 0, float(velocity) / 127.0, ""));
|
Chris@507
|
1165 }
|
Chris@507
|
1166
|
Chris@507
|
1167 void
|
Chris@507
|
1168 NoteLayer::addNoteOff(long frame, int pitch)
|
Chris@507
|
1169 {
|
Chris@507
|
1170 for (NoteSet::iterator i = m_pendingNoteOns.begin();
|
Chris@507
|
1171 i != m_pendingNoteOns.end(); ++i) {
|
Chris@507
|
1172 if (lrintf((*i).value) == pitch) {
|
Chris@507
|
1173 Note note(*i);
|
Chris@507
|
1174 m_pendingNoteOns.erase(i);
|
Chris@507
|
1175 note.duration = frame - note.frame;
|
Chris@507
|
1176 if (m_model) {
|
Chris@507
|
1177 NoteModel::AddPointCommand *c = new NoteModel::AddPointCommand
|
Chris@507
|
1178 (m_model, note, tr("Record Note"));
|
Chris@507
|
1179 // execute and bundle:
|
Chris@507
|
1180 CommandHistory::getInstance()->addCommand(c, true, true);
|
Chris@507
|
1181 }
|
Chris@507
|
1182 break;
|
Chris@507
|
1183 }
|
Chris@507
|
1184 }
|
Chris@507
|
1185 }
|
Chris@507
|
1186
|
Chris@507
|
1187 void
|
Chris@507
|
1188 NoteLayer::abandonNoteOns()
|
Chris@507
|
1189 {
|
Chris@507
|
1190 m_pendingNoteOns.clear();
|
Chris@507
|
1191 }
|
Chris@507
|
1192
|
Chris@287
|
1193 int
|
Chris@287
|
1194 NoteLayer::getDefaultColourHint(bool darkbg, bool &impose)
|
Chris@287
|
1195 {
|
Chris@287
|
1196 impose = false;
|
Chris@287
|
1197 return ColourDatabase::getInstance()->getColourIndex
|
Chris@287
|
1198 (QString(darkbg ? "White" : "Black"));
|
Chris@287
|
1199 }
|
Chris@287
|
1200
|
Chris@316
|
1201 void
|
Chris@316
|
1202 NoteLayer::toXml(QTextStream &stream,
|
Chris@316
|
1203 QString indent, QString extraAttributes) const
|
Chris@30
|
1204 {
|
Chris@316
|
1205 SingleColourLayer::toXml(stream, indent, extraAttributes +
|
Chris@445
|
1206 QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ")
|
Chris@445
|
1207 .arg(m_verticalScale)
|
Chris@445
|
1208 .arg(m_scaleMinimum)
|
Chris@445
|
1209 .arg(m_scaleMaximum));
|
Chris@30
|
1210 }
|
Chris@30
|
1211
|
Chris@30
|
1212 void
|
Chris@30
|
1213 NoteLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@30
|
1214 {
|
Chris@287
|
1215 SingleColourLayer::setProperties(attributes);
|
Chris@30
|
1216
|
Chris@445
|
1217 bool ok, alsoOk;
|
Chris@30
|
1218 VerticalScale scale = (VerticalScale)
|
Chris@30
|
1219 attributes.value("verticalScale").toInt(&ok);
|
Chris@30
|
1220 if (ok) setVerticalScale(scale);
|
Chris@445
|
1221
|
Chris@445
|
1222 float min = attributes.value("scaleMinimum").toFloat(&ok);
|
Chris@445
|
1223 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
|
Chris@445
|
1224 if (ok && alsoOk) setDisplayExtents(min, max);
|
Chris@30
|
1225 }
|
Chris@30
|
1226
|
Chris@30
|
1227
|