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
|
matthiasm@620
|
16 #include "FlexiNoteLayer.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
|
matthiasm@620
|
27 #include "data/model/FlexiNoteModel.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@551
|
39 #include <utility>
|
Chris@30
|
40
|
matthiasm@620
|
41 FlexiNoteLayer::FlexiNoteLayer() :
|
gyorgyf@646
|
42 SingleColourLayer(),
|
gyorgyf@625
|
43
|
gyorgyf@646
|
44 // m_model(0),
|
gyorgyf@646
|
45 // m_editing(false),
|
gyorgyf@646
|
46 // m_originalPoint(0, 0.0, 0, 1.f, tr("New Point")),
|
gyorgyf@646
|
47 // m_editingPoint(0, 0.0, 0, 1.f, tr("New Point")),
|
gyorgyf@646
|
48 // m_editingCommand(0),
|
gyorgyf@646
|
49 // m_verticalScale(AutoAlignScale),
|
gyorgyf@646
|
50 // m_scaleMinimum(0),
|
gyorgyf@646
|
51 // m_scaleMaximum(0)
|
gyorgyf@625
|
52
|
gyorgyf@627
|
53 m_model(0),
|
gyorgyf@628
|
54 m_editing(false),
|
gyorgyf@627
|
55 m_originalPoint(0, 0.0, 0, 1.f, tr("New Point")),
|
gyorgyf@627
|
56 m_editingPoint(0, 0.0, 0, 1.f, tr("New Point")),
|
gyorgyf@627
|
57 m_editingCommand(0),
|
matthiasm@634
|
58 m_verticalScale(AutoAlignScale),
|
gyorgyf@628
|
59 m_scaleMinimum(34),
|
gyorgyf@627
|
60 m_scaleMaximum(77)
|
Chris@30
|
61 {
|
Chris@30
|
62 }
|
Chris@30
|
63
|
Chris@30
|
64 void
|
gyorgyf@626
|
65 FlexiNoteLayer::setModel(FlexiNoteModel *model)
|
Chris@30
|
66 {
|
Chris@30
|
67 if (m_model == model) return;
|
Chris@30
|
68 m_model = model;
|
Chris@30
|
69
|
Chris@320
|
70 connectSignals(m_model);
|
Chris@30
|
71
|
gyorgyf@628
|
72 // m_scaleMinimum = 0;
|
gyorgyf@628
|
73 // m_scaleMaximum = 0;
|
Chris@439
|
74
|
Chris@30
|
75 emit modelReplaced();
|
Chris@30
|
76 }
|
Chris@30
|
77
|
Chris@30
|
78 Layer::PropertyList
|
matthiasm@620
|
79 FlexiNoteLayer::getProperties() const
|
Chris@30
|
80 {
|
Chris@287
|
81 PropertyList list = SingleColourLayer::getProperties();
|
Chris@87
|
82 list.push_back("Vertical Scale");
|
Chris@100
|
83 list.push_back("Scale Units");
|
Chris@30
|
84 return list;
|
Chris@30
|
85 }
|
Chris@30
|
86
|
Chris@87
|
87 QString
|
matthiasm@620
|
88 FlexiNoteLayer::getPropertyLabel(const PropertyName &name) const
|
Chris@87
|
89 {
|
Chris@87
|
90 if (name == "Vertical Scale") return tr("Vertical Scale");
|
Chris@116
|
91 if (name == "Scale Units") return tr("Scale Units");
|
Chris@287
|
92 return SingleColourLayer::getPropertyLabel(name);
|
Chris@87
|
93 }
|
Chris@87
|
94
|
Chris@30
|
95 Layer::PropertyType
|
matthiasm@620
|
96 FlexiNoteLayer::getPropertyType(const PropertyName &name) const
|
Chris@30
|
97 {
|
Chris@100
|
98 if (name == "Scale Units") return UnitsProperty;
|
Chris@287
|
99 if (name == "Vertical Scale") return ValueProperty;
|
Chris@287
|
100 return SingleColourLayer::getPropertyType(name);
|
Chris@30
|
101 }
|
Chris@30
|
102
|
Chris@198
|
103 QString
|
matthiasm@620
|
104 FlexiNoteLayer::getPropertyGroupName(const PropertyName &name) const
|
Chris@198
|
105 {
|
Chris@198
|
106 if (name == "Vertical Scale" || name == "Scale Units") {
|
Chris@198
|
107 return tr("Scale");
|
Chris@198
|
108 }
|
Chris@287
|
109 return SingleColourLayer::getPropertyGroupName(name);
|
Chris@198
|
110 }
|
Chris@198
|
111
|
Chris@30
|
112 int
|
matthiasm@620
|
113 FlexiNoteLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@216
|
114 int *min, int *max, int *deflt) const
|
Chris@30
|
115 {
|
Chris@216
|
116 int val = 0;
|
Chris@30
|
117
|
Chris@287
|
118 if (name == "Vertical Scale") {
|
gyorgyf@646
|
119
|
gyorgyf@646
|
120 if (min) *min = 0;
|
gyorgyf@646
|
121 if (max) *max = 3;
|
Chris@216
|
122 if (deflt) *deflt = int(AutoAlignScale);
|
gyorgyf@646
|
123
|
gyorgyf@646
|
124 val = int(m_verticalScale);
|
Chris@30
|
125
|
Chris@100
|
126 } else if (name == "Scale Units") {
|
Chris@100
|
127
|
Chris@216
|
128 if (deflt) *deflt = 0;
|
Chris@100
|
129 if (m_model) {
|
Chris@216
|
130 val = UnitDatabase::getInstance()->getUnitId
|
Chris@100
|
131 (m_model->getScaleUnits());
|
Chris@100
|
132 }
|
Chris@100
|
133
|
Chris@30
|
134 } else {
|
Chris@216
|
135
|
gyorgyf@646
|
136 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
|
Chris@30
|
137 }
|
Chris@30
|
138
|
Chris@216
|
139 return val;
|
Chris@30
|
140 }
|
Chris@30
|
141
|
Chris@30
|
142 QString
|
matthiasm@620
|
143 FlexiNoteLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@287
|
144 int value) const
|
Chris@30
|
145 {
|
Chris@287
|
146 if (name == "Vertical Scale") {
|
gyorgyf@646
|
147 switch (value) {
|
gyorgyf@646
|
148 default:
|
gyorgyf@646
|
149 case 0: return tr("Auto-Align");
|
gyorgyf@646
|
150 case 1: return tr("Linear");
|
gyorgyf@646
|
151 case 2: return tr("Log");
|
gyorgyf@646
|
152 case 3: return tr("MIDI Notes");
|
gyorgyf@646
|
153 }
|
Chris@30
|
154 }
|
Chris@287
|
155 return SingleColourLayer::getPropertyValueLabel(name, value);
|
Chris@30
|
156 }
|
Chris@30
|
157
|
Chris@30
|
158 void
|
matthiasm@620
|
159 FlexiNoteLayer::setProperty(const PropertyName &name, int value)
|
Chris@30
|
160 {
|
Chris@287
|
161 if (name == "Vertical Scale") {
|
gyorgyf@646
|
162 setVerticalScale(VerticalScale(value));
|
Chris@100
|
163 } else if (name == "Scale Units") {
|
Chris@100
|
164 if (m_model) {
|
Chris@100
|
165 m_model->setScaleUnits
|
Chris@100
|
166 (UnitDatabase::getInstance()->getUnitById(value));
|
Chris@100
|
167 emit modelChanged();
|
Chris@100
|
168 }
|
Chris@287
|
169 } else {
|
Chris@287
|
170 return SingleColourLayer::setProperty(name, value);
|
Chris@30
|
171 }
|
Chris@30
|
172 }
|
Chris@30
|
173
|
Chris@30
|
174 void
|
matthiasm@620
|
175 FlexiNoteLayer::setVerticalScale(VerticalScale scale)
|
Chris@30
|
176 {
|
Chris@30
|
177 if (m_verticalScale == scale) return;
|
Chris@30
|
178 m_verticalScale = scale;
|
Chris@30
|
179 emit layerParametersChanged();
|
Chris@30
|
180 }
|
Chris@30
|
181
|
Chris@30
|
182 bool
|
matthiasm@620
|
183 FlexiNoteLayer::isLayerScrollable(const View *v) const
|
Chris@30
|
184 {
|
Chris@30
|
185 QPoint discard;
|
Chris@44
|
186 return !v->shouldIlluminateLocalFeatures(this, discard);
|
Chris@30
|
187 }
|
Chris@30
|
188
|
Chris@79
|
189 bool
|
matthiasm@620
|
190 FlexiNoteLayer::shouldConvertMIDIToHz() const
|
Chris@101
|
191 {
|
Chris@101
|
192 QString unit = m_model->getScaleUnits();
|
Chris@101
|
193 return (unit != "Hz");
|
Chris@101
|
194 // if (unit == "" ||
|
Chris@101
|
195 // unit.startsWith("MIDI") ||
|
Chris@101
|
196 // unit.startsWith("midi")) return true;
|
Chris@101
|
197 // return false;
|
Chris@101
|
198 }
|
Chris@101
|
199
|
Chris@101
|
200 bool
|
matthiasm@620
|
201 FlexiNoteLayer::getValueExtents(float &min, float &max,
|
Chris@101
|
202 bool &logarithmic, QString &unit) const
|
Chris@79
|
203 {
|
Chris@79
|
204 if (!m_model) return false;
|
Chris@79
|
205 min = m_model->getValueMinimum();
|
Chris@79
|
206 max = m_model->getValueMaximum();
|
Chris@101
|
207
|
Chris@105
|
208 if (shouldConvertMIDIToHz()) {
|
Chris@105
|
209 unit = "Hz";
|
Chris@105
|
210 min = Pitch::getFrequencyForPitch(lrintf(min));
|
Chris@105
|
211 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
|
Chris@105
|
212 } else unit = m_model->getScaleUnits();
|
Chris@101
|
213
|
Chris@101
|
214 if (m_verticalScale == MIDIRangeScale ||
|
Chris@101
|
215 m_verticalScale == LogScale) logarithmic = true;
|
Chris@101
|
216
|
Chris@101
|
217 return true;
|
Chris@101
|
218 }
|
Chris@101
|
219
|
Chris@101
|
220 bool
|
matthiasm@620
|
221 FlexiNoteLayer::getDisplayExtents(float &min, float &max) const
|
Chris@101
|
222 {
|
matthiasm@623
|
223 if (!m_model || shouldAutoAlign()) {
|
gyorgyf@646
|
224 std::cerr << "No model or shouldAutoAlign()" << std::endl;
|
gyorgyf@646
|
225 return false;
|
gyorgyf@646
|
226 }
|
Chris@101
|
227
|
Chris@101
|
228 if (m_verticalScale == MIDIRangeScale) {
|
Chris@101
|
229 min = Pitch::getFrequencyForPitch(0);
|
matthiasm@634
|
230 max = Pitch::getFrequencyForPitch(127);
|
Chris@101
|
231 return true;
|
Chris@101
|
232 }
|
Chris@101
|
233
|
Chris@439
|
234 if (m_scaleMinimum == m_scaleMaximum) {
|
Chris@455
|
235 min = m_model->getValueMinimum();
|
Chris@455
|
236 max = m_model->getValueMaximum();
|
Chris@455
|
237 } else {
|
Chris@455
|
238 min = m_scaleMinimum;
|
Chris@455
|
239 max = m_scaleMaximum;
|
Chris@439
|
240 }
|
Chris@439
|
241
|
Chris@101
|
242 if (shouldConvertMIDIToHz()) {
|
Chris@101
|
243 min = Pitch::getFrequencyForPitch(lrintf(min));
|
Chris@101
|
244 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
|
Chris@101
|
245 }
|
Chris@101
|
246
|
Chris@79
|
247 return true;
|
Chris@79
|
248 }
|
Chris@79
|
249
|
Chris@439
|
250 bool
|
matthiasm@620
|
251 FlexiNoteLayer::setDisplayExtents(float min, float max)
|
Chris@439
|
252 {
|
Chris@439
|
253 if (!m_model) return false;
|
Chris@439
|
254
|
Chris@439
|
255 if (min == max) {
|
Chris@439
|
256 if (min == 0.f) {
|
Chris@439
|
257 max = 1.f;
|
Chris@439
|
258 } else {
|
Chris@439
|
259 max = min * 1.0001;
|
Chris@439
|
260 }
|
Chris@439
|
261 }
|
Chris@439
|
262
|
Chris@439
|
263 m_scaleMinimum = min;
|
Chris@439
|
264 m_scaleMaximum = max;
|
Chris@439
|
265
|
matthiasm@620
|
266 // SVDEBUG << "FlexiNoteLayer::setDisplayExtents: min = " << min << ", max = " << max << endl;
|
Chris@439
|
267
|
Chris@439
|
268 emit layerParametersChanged();
|
Chris@439
|
269 return true;
|
Chris@439
|
270 }
|
Chris@439
|
271
|
Chris@439
|
272 int
|
matthiasm@620
|
273 FlexiNoteLayer::getVerticalZoomSteps(int &defaultStep) const
|
Chris@439
|
274 {
|
Chris@439
|
275 if (shouldAutoAlign()) return 0;
|
Chris@439
|
276 if (!m_model) return 0;
|
Chris@439
|
277
|
Chris@439
|
278 defaultStep = 0;
|
Chris@439
|
279 return 100;
|
Chris@439
|
280 }
|
Chris@439
|
281
|
Chris@439
|
282 int
|
matthiasm@620
|
283 FlexiNoteLayer::getCurrentVerticalZoomStep() const
|
Chris@439
|
284 {
|
Chris@439
|
285 if (shouldAutoAlign()) return 0;
|
Chris@439
|
286 if (!m_model) return 0;
|
Chris@439
|
287
|
Chris@439
|
288 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
|
Chris@439
|
289 if (!mapper) return 0;
|
Chris@439
|
290
|
Chris@439
|
291 float dmin, dmax;
|
Chris@439
|
292 getDisplayExtents(dmin, dmax);
|
Chris@439
|
293
|
Chris@439
|
294 int nr = mapper->getPositionForValue(dmax - dmin);
|
Chris@439
|
295
|
Chris@439
|
296 delete mapper;
|
Chris@439
|
297
|
Chris@439
|
298 return 100 - nr;
|
Chris@439
|
299 }
|
Chris@439
|
300
|
Chris@439
|
301 //!!! lots of duplication with TimeValueLayer
|
Chris@439
|
302
|
Chris@439
|
303 void
|
matthiasm@620
|
304 FlexiNoteLayer::setVerticalZoomStep(int step)
|
Chris@439
|
305 {
|
Chris@439
|
306 if (shouldAutoAlign()) return;
|
Chris@439
|
307 if (!m_model) return;
|
Chris@439
|
308
|
Chris@439
|
309 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
|
Chris@439
|
310 if (!mapper) return;
|
Chris@439
|
311
|
Chris@439
|
312 float min, max;
|
Chris@439
|
313 bool logarithmic;
|
Chris@439
|
314 QString unit;
|
Chris@439
|
315 getValueExtents(min, max, logarithmic, unit);
|
Chris@439
|
316
|
Chris@439
|
317 float dmin, dmax;
|
Chris@439
|
318 getDisplayExtents(dmin, dmax);
|
Chris@439
|
319
|
Chris@439
|
320 float newdist = mapper->getValueForPosition(100 - step);
|
Chris@439
|
321
|
Chris@439
|
322 float newmin, newmax;
|
Chris@439
|
323
|
Chris@439
|
324 if (logarithmic) {
|
Chris@439
|
325
|
Chris@439
|
326 // see SpectrogramLayer::setVerticalZoomStep
|
Chris@439
|
327
|
Chris@439
|
328 newmax = (newdist + sqrtf(newdist*newdist + 4*dmin*dmax)) / 2;
|
Chris@439
|
329 newmin = newmax - newdist;
|
Chris@439
|
330
|
Chris@439
|
331 // std::cerr << "newmin = " << newmin << ", newmax = " << newmax << std::endl;
|
Chris@439
|
332
|
Chris@439
|
333 } else {
|
Chris@439
|
334 float dmid = (dmax + dmin) / 2;
|
Chris@439
|
335 newmin = dmid - newdist / 2;
|
Chris@439
|
336 newmax = dmid + newdist / 2;
|
Chris@439
|
337 }
|
Chris@439
|
338
|
Chris@439
|
339 if (newmin < min) {
|
Chris@439
|
340 newmax += (min - newmin);
|
Chris@439
|
341 newmin = min;
|
Chris@439
|
342 }
|
Chris@439
|
343 if (newmax > max) {
|
Chris@439
|
344 newmax = max;
|
Chris@439
|
345 }
|
Chris@439
|
346
|
matthiasm@620
|
347 SVDEBUG << "FlexiNoteLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl;
|
Chris@439
|
348
|
Chris@439
|
349 setDisplayExtents(newmin, newmax);
|
Chris@439
|
350 }
|
Chris@439
|
351
|
Chris@439
|
352 RangeMapper *
|
matthiasm@620
|
353 FlexiNoteLayer::getNewVerticalZoomRangeMapper() const
|
Chris@439
|
354 {
|
Chris@439
|
355 if (!m_model) return 0;
|
Chris@439
|
356
|
Chris@439
|
357 RangeMapper *mapper;
|
Chris@439
|
358
|
Chris@439
|
359 float min, max;
|
Chris@439
|
360 bool logarithmic;
|
Chris@439
|
361 QString unit;
|
Chris@439
|
362 getValueExtents(min, max, logarithmic, unit);
|
Chris@439
|
363
|
Chris@439
|
364 if (min == max) return 0;
|
Chris@439
|
365
|
Chris@439
|
366 if (logarithmic) {
|
Chris@439
|
367 mapper = new LogRangeMapper(0, 100, min, max, unit);
|
Chris@439
|
368 } else {
|
Chris@439
|
369 mapper = new LinearRangeMapper(0, 100, min, max, unit);
|
Chris@439
|
370 }
|
Chris@439
|
371
|
Chris@439
|
372 return mapper;
|
Chris@439
|
373 }
|
Chris@439
|
374
|
matthiasm@620
|
375 FlexiNoteModel::PointList
|
matthiasm@620
|
376 FlexiNoteLayer::getLocalPoints(View *v, int x) const
|
Chris@30
|
377 {
|
matthiasm@620
|
378 if (!m_model) return FlexiNoteModel::PointList();
|
Chris@30
|
379
|
Chris@44
|
380 long frame = v->getFrameForX(x);
|
Chris@30
|
381
|
matthiasm@620
|
382 FlexiNoteModel::PointList onPoints =
|
gyorgyf@646
|
383 m_model->getPoints(frame);
|
Chris@30
|
384
|
Chris@30
|
385 if (!onPoints.empty()) {
|
gyorgyf@646
|
386 return onPoints;
|
Chris@30
|
387 }
|
Chris@30
|
388
|
matthiasm@620
|
389 FlexiNoteModel::PointList prevPoints =
|
gyorgyf@646
|
390 m_model->getPreviousPoints(frame);
|
matthiasm@620
|
391 FlexiNoteModel::PointList nextPoints =
|
gyorgyf@646
|
392 m_model->getNextPoints(frame);
|
Chris@30
|
393
|
matthiasm@620
|
394 FlexiNoteModel::PointList usePoints = prevPoints;
|
Chris@30
|
395
|
Chris@30
|
396 if (prevPoints.empty()) {
|
gyorgyf@646
|
397 usePoints = nextPoints;
|
Chris@248
|
398 } else if (long(prevPoints.begin()->frame) < v->getStartFrame() &&
|
gyorgyf@646
|
399 !(nextPoints.begin()->frame > v->getEndFrame())) {
|
gyorgyf@646
|
400 usePoints = nextPoints;
|
Chris@248
|
401 } else if (long(nextPoints.begin()->frame) - frame <
|
gyorgyf@646
|
402 frame - long(prevPoints.begin()->frame)) {
|
gyorgyf@646
|
403 usePoints = nextPoints;
|
Chris@30
|
404 }
|
Chris@30
|
405
|
Chris@30
|
406 if (!usePoints.empty()) {
|
gyorgyf@646
|
407 int fuzz = 2;
|
gyorgyf@646
|
408 int px = v->getXForFrame(usePoints.begin()->frame);
|
gyorgyf@646
|
409 if ((px > x && px - x > fuzz) ||
|
gyorgyf@646
|
410 (px < x && x - px > fuzz + 1)) {
|
gyorgyf@646
|
411 usePoints.clear();
|
gyorgyf@646
|
412 }
|
Chris@30
|
413 }
|
Chris@30
|
414
|
Chris@30
|
415 return usePoints;
|
Chris@30
|
416 }
|
Chris@30
|
417
|
Chris@550
|
418 bool
|
matthiasm@622
|
419 FlexiNoteLayer::getPointToDrag(View *v, int x, int y, FlexiNoteModel::Point &p) const
|
Chris@550
|
420 {
|
Chris@550
|
421 if (!m_model) return false;
|
Chris@550
|
422
|
Chris@550
|
423 long frame = v->getFrameForX(x);
|
Chris@550
|
424
|
matthiasm@620
|
425 FlexiNoteModel::PointList onPoints = m_model->getPoints(frame);
|
Chris@550
|
426 if (onPoints.empty()) return false;
|
Chris@550
|
427
|
Chris@551
|
428 // std::cerr << "frame " << frame << ": " << onPoints.size() << " candidate points" << std::endl;
|
Chris@550
|
429
|
Chris@550
|
430 int nearestDistance = -1;
|
Chris@550
|
431
|
matthiasm@620
|
432 for (FlexiNoteModel::PointList::const_iterator i = onPoints.begin();
|
Chris@550
|
433 i != onPoints.end(); ++i) {
|
Chris@550
|
434
|
Chris@550
|
435 int distance = getYForValue(v, (*i).value) - y;
|
Chris@550
|
436 if (distance < 0) distance = -distance;
|
Chris@550
|
437 if (nearestDistance == -1 || distance < nearestDistance) {
|
Chris@550
|
438 nearestDistance = distance;
|
Chris@550
|
439 p = *i;
|
Chris@550
|
440 }
|
Chris@550
|
441 }
|
Chris@550
|
442
|
Chris@550
|
443 return true;
|
Chris@550
|
444 }
|
Chris@550
|
445
|
gyorgyf@646
|
446 bool
|
gyorgyf@646
|
447 FlexiNoteLayer::getNoteToEdit(View *v, int x, int y, FlexiNoteModel::Point &p) const
|
gyorgyf@646
|
448 {
|
gyorgyf@647
|
449 // GF: find the note that is closest to the cursor
|
gyorgyf@646
|
450 if (!m_model) return false;
|
gyorgyf@646
|
451
|
gyorgyf@646
|
452 long frame = v->getFrameForX(x);
|
gyorgyf@646
|
453
|
gyorgyf@646
|
454 FlexiNoteModel::PointList onPoints = m_model->getPoints(frame);
|
gyorgyf@646
|
455 if (onPoints.empty()) return false;
|
gyorgyf@646
|
456
|
gyorgyf@646
|
457 // std::cerr << "frame " << frame << ": " << onPoints.size() << " candidate points" << std::endl;
|
gyorgyf@646
|
458
|
gyorgyf@646
|
459 int nearestDistance = -1;
|
gyorgyf@646
|
460
|
gyorgyf@646
|
461 for (FlexiNoteModel::PointList::const_iterator i = onPoints.begin();
|
gyorgyf@646
|
462 i != onPoints.end(); ++i) {
|
gyorgyf@646
|
463
|
gyorgyf@646
|
464 int distance = getYForValue(v, (*i).value) - y;
|
gyorgyf@646
|
465 if (distance < 0) distance = -distance;
|
gyorgyf@646
|
466 if (nearestDistance == -1 || distance < nearestDistance) {
|
gyorgyf@646
|
467 nearestDistance = distance;
|
gyorgyf@646
|
468 p = *i;
|
gyorgyf@646
|
469 }
|
gyorgyf@646
|
470 }
|
gyorgyf@646
|
471
|
gyorgyf@646
|
472 return true;
|
gyorgyf@646
|
473 }
|
gyorgyf@646
|
474
|
Chris@30
|
475 QString
|
matthiasm@620
|
476 FlexiNoteLayer::getFeatureDescription(View *v, QPoint &pos) const
|
Chris@30
|
477 {
|
Chris@30
|
478 int x = pos.x();
|
Chris@30
|
479
|
Chris@30
|
480 if (!m_model || !m_model->getSampleRate()) return "";
|
Chris@30
|
481
|
matthiasm@620
|
482 FlexiNoteModel::PointList points = getLocalPoints(v, x);
|
Chris@30
|
483
|
Chris@30
|
484 if (points.empty()) {
|
gyorgyf@653
|
485 if (!m_model->isReady()) {
|
gyorgyf@653
|
486 return tr("In progress");
|
gyorgyf@653
|
487 } else {
|
gyorgyf@653
|
488 return tr("No local points");
|
gyorgyf@653
|
489 }
|
Chris@30
|
490 }
|
Chris@30
|
491
|
matthiasm@620
|
492 FlexiNote note(0);
|
matthiasm@620
|
493 FlexiNoteModel::PointList::iterator i;
|
Chris@30
|
494
|
Chris@30
|
495 for (i = points.begin(); i != points.end(); ++i) {
|
Chris@30
|
496
|
gyorgyf@653
|
497 int y = getYForValue(v, i->value);
|
gyorgyf@653
|
498 int h = NOTE_HEIGHT; // GF: larger notes
|
Chris@30
|
499
|
gyorgyf@653
|
500 if (m_model->getValueQuantization() != 0.0) {
|
gyorgyf@653
|
501 h = y - getYForValue(v, i->value + m_model->getValueQuantization());
|
gyorgyf@653
|
502 if (h < NOTE_HEIGHT) h = NOTE_HEIGHT;
|
gyorgyf@653
|
503 }
|
Chris@30
|
504
|
gyorgyf@647
|
505 // GF: this is not quite correct
|
gyorgyf@653
|
506 if (pos.y() >= y - 4 && pos.y() <= y + h) {
|
gyorgyf@653
|
507 note = *i;
|
gyorgyf@653
|
508 break;
|
gyorgyf@653
|
509 }
|
Chris@30
|
510 }
|
Chris@30
|
511
|
Chris@30
|
512 if (i == points.end()) return tr("No local points");
|
Chris@30
|
513
|
Chris@30
|
514 RealTime rt = RealTime::frame2RealTime(note.frame,
|
gyorgyf@653
|
515 m_model->getSampleRate());
|
Chris@30
|
516 RealTime rd = RealTime::frame2RealTime(note.duration,
|
gyorgyf@653
|
517 m_model->getSampleRate());
|
Chris@30
|
518
|
Chris@101
|
519 QString pitchText;
|
Chris@101
|
520
|
Chris@101
|
521 if (shouldConvertMIDIToHz()) {
|
Chris@101
|
522
|
Chris@101
|
523 int mnote = lrintf(note.value);
|
Chris@101
|
524 int cents = lrintf((note.value - mnote) * 100);
|
Chris@101
|
525 float freq = Pitch::getFrequencyForPitch(mnote, cents);
|
Chris@544
|
526 pitchText = tr("%1 (%2, %3 Hz)")
|
Chris@544
|
527 .arg(Pitch::getPitchLabel(mnote, cents))
|
Chris@544
|
528 .arg(mnote)
|
Chris@544
|
529 .arg(freq);
|
Chris@101
|
530
|
Chris@101
|
531 } else if (m_model->getScaleUnits() == "Hz") {
|
Chris@101
|
532
|
Chris@544
|
533 pitchText = tr("%1 Hz (%2, %3)")
|
Chris@101
|
534 .arg(note.value)
|
Chris@544
|
535 .arg(Pitch::getPitchLabelForFrequency(note.value))
|
Chris@544
|
536 .arg(Pitch::getPitchForFrequency(note.value));
|
Chris@101
|
537
|
Chris@101
|
538 } else {
|
Chris@234
|
539 pitchText = tr("%1 %2")
|
Chris@101
|
540 .arg(note.value).arg(m_model->getScaleUnits());
|
Chris@101
|
541 }
|
Chris@101
|
542
|
Chris@30
|
543 QString text;
|
Chris@30
|
544
|
Chris@30
|
545 if (note.label == "") {
|
gyorgyf@653
|
546 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label"))
|
gyorgyf@653
|
547 .arg(rt.toText(true).c_str())
|
gyorgyf@653
|
548 .arg(pitchText)
|
gyorgyf@653
|
549 .arg(rd.toText(true).c_str());
|
Chris@30
|
550 } else {
|
gyorgyf@653
|
551 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4"))
|
gyorgyf@653
|
552 .arg(rt.toText(true).c_str())
|
gyorgyf@653
|
553 .arg(pitchText)
|
gyorgyf@653
|
554 .arg(rd.toText(true).c_str())
|
gyorgyf@653
|
555 .arg(note.label);
|
Chris@30
|
556 }
|
Chris@30
|
557
|
Chris@44
|
558 pos = QPoint(v->getXForFrame(note.frame),
|
gyorgyf@653
|
559 getYForValue(v, note.value));
|
Chris@30
|
560 return text;
|
Chris@30
|
561 }
|
Chris@30
|
562
|
Chris@30
|
563 bool
|
matthiasm@620
|
564 FlexiNoteLayer::snapToFeatureFrame(View *v, int &frame,
|
gyorgyf@646
|
565 size_t &resolution,
|
gyorgyf@646
|
566 SnapType snap) const
|
Chris@30
|
567 {
|
Chris@30
|
568 if (!m_model) {
|
gyorgyf@646
|
569 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
|
Chris@30
|
570 }
|
Chris@30
|
571
|
Chris@30
|
572 resolution = m_model->getResolution();
|
matthiasm@620
|
573 FlexiNoteModel::PointList points;
|
Chris@30
|
574
|
Chris@30
|
575 if (snap == SnapNeighbouring) {
|
gyorgyf@646
|
576
|
gyorgyf@646
|
577 points = getLocalPoints(v, v->getXForFrame(frame));
|
gyorgyf@646
|
578 if (points.empty()) return false;
|
gyorgyf@646
|
579 frame = points.begin()->frame;
|
gyorgyf@646
|
580 return true;
|
Chris@30
|
581 }
|
Chris@30
|
582
|
Chris@30
|
583 points = m_model->getPoints(frame, frame);
|
Chris@30
|
584 int snapped = frame;
|
Chris@30
|
585 bool found = false;
|
Chris@30
|
586
|
matthiasm@620
|
587 for (FlexiNoteModel::PointList::const_iterator i = points.begin();
|
gyorgyf@646
|
588 i != points.end(); ++i) {
|
Chris@30
|
589
|
gyorgyf@646
|
590 if (snap == SnapRight) {
|
Chris@30
|
591
|
gyorgyf@646
|
592 if (i->frame > frame) {
|
gyorgyf@646
|
593 snapped = i->frame;
|
gyorgyf@646
|
594 found = true;
|
gyorgyf@646
|
595 break;
|
gyorgyf@646
|
596 }
|
Chris@30
|
597
|
gyorgyf@646
|
598 } else if (snap == SnapLeft) {
|
Chris@30
|
599
|
gyorgyf@646
|
600 if (i->frame <= frame) {
|
gyorgyf@646
|
601 snapped = i->frame;
|
gyorgyf@646
|
602 found = true; // don't break, as the next may be better
|
gyorgyf@646
|
603 } else {
|
gyorgyf@646
|
604 break;
|
gyorgyf@646
|
605 }
|
Chris@30
|
606
|
gyorgyf@646
|
607 } else { // nearest
|
Chris@30
|
608
|
gyorgyf@646
|
609 FlexiNoteModel::PointList::const_iterator j = i;
|
gyorgyf@646
|
610 ++j;
|
Chris@30
|
611
|
gyorgyf@646
|
612 if (j == points.end()) {
|
Chris@30
|
613
|
gyorgyf@646
|
614 snapped = i->frame;
|
gyorgyf@646
|
615 found = true;
|
gyorgyf@646
|
616 break;
|
Chris@30
|
617
|
gyorgyf@646
|
618 } else if (j->frame >= frame) {
|
Chris@30
|
619
|
gyorgyf@646
|
620 if (j->frame - frame < frame - i->frame) {
|
gyorgyf@646
|
621 snapped = j->frame;
|
gyorgyf@646
|
622 } else {
|
gyorgyf@646
|
623 snapped = i->frame;
|
gyorgyf@646
|
624 }
|
gyorgyf@646
|
625 found = true;
|
gyorgyf@646
|
626 break;
|
gyorgyf@646
|
627 }
|
gyorgyf@646
|
628 }
|
Chris@30
|
629 }
|
Chris@30
|
630
|
Chris@30
|
631 frame = snapped;
|
Chris@30
|
632 return found;
|
Chris@30
|
633 }
|
Chris@30
|
634
|
Chris@101
|
635 void
|
matthiasm@620
|
636 FlexiNoteLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
|
Chris@30
|
637 {
|
Chris@101
|
638 min = 0.0;
|
Chris@101
|
639 max = 0.0;
|
Chris@101
|
640 log = false;
|
Chris@42
|
641
|
Chris@101
|
642 QString queryUnits;
|
Chris@101
|
643 if (shouldConvertMIDIToHz()) queryUnits = "Hz";
|
Chris@101
|
644 else queryUnits = m_model->getScaleUnits();
|
Chris@30
|
645
|
Chris@439
|
646 if (shouldAutoAlign()) {
|
Chris@30
|
647
|
Chris@101
|
648 if (!v->getValueExtents(queryUnits, min, max, log)) {
|
Chris@30
|
649
|
Chris@101
|
650 min = m_model->getValueMinimum();
|
Chris@101
|
651 max = m_model->getValueMaximum();
|
Chris@42
|
652
|
Chris@101
|
653 if (shouldConvertMIDIToHz()) {
|
Chris@101
|
654 min = Pitch::getFrequencyForPitch(lrintf(min));
|
Chris@101
|
655 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
|
Chris@101
|
656 }
|
Chris@42
|
657
|
gyorgyf@635
|
658 // std::cerr << "FlexiNoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
|
Chris@105
|
659
|
Chris@101
|
660 } else if (log) {
|
Chris@101
|
661
|
Chris@197
|
662 LogRange::mapRange(min, max);
|
Chris@105
|
663
|
gyorgyf@635
|
664 // std::cerr << "FlexiNoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
|
Chris@105
|
665
|
Chris@101
|
666 }
|
Chris@101
|
667
|
Chris@101
|
668 } else {
|
Chris@101
|
669
|
Chris@439
|
670 getDisplayExtents(min, max);
|
Chris@101
|
671
|
Chris@101
|
672 if (m_verticalScale == MIDIRangeScale) {
|
Chris@101
|
673 min = Pitch::getFrequencyForPitch(0);
|
matthiasm@623
|
674 max = Pitch::getFrequencyForPitch(70);
|
Chris@101
|
675 } else if (shouldConvertMIDIToHz()) {
|
Chris@101
|
676 min = Pitch::getFrequencyForPitch(lrintf(min));
|
Chris@101
|
677 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
|
Chris@101
|
678 }
|
Chris@101
|
679
|
Chris@101
|
680 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
|
Chris@197
|
681 LogRange::mapRange(min, max);
|
Chris@101
|
682 log = true;
|
Chris@101
|
683 }
|
Chris@30
|
684 }
|
Chris@30
|
685
|
Chris@101
|
686 if (max == min) max = min + 1.0;
|
Chris@101
|
687 }
|
Chris@30
|
688
|
Chris@101
|
689 int
|
matthiasm@620
|
690 FlexiNoteLayer::getYForValue(View *v, float val) const
|
Chris@101
|
691 {
|
Chris@101
|
692 float min = 0.0, max = 0.0;
|
Chris@101
|
693 bool logarithmic = false;
|
Chris@101
|
694 int h = v->height();
|
Chris@101
|
695
|
Chris@101
|
696 getScaleExtents(v, min, max, logarithmic);
|
Chris@101
|
697
|
matthiasm@620
|
698 // std::cerr << "FlexiNoteLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << std::endl;
|
Chris@101
|
699
|
Chris@101
|
700 if (shouldConvertMIDIToHz()) {
|
Chris@101
|
701 val = Pitch::getFrequencyForPitch(lrintf(val),
|
Chris@101
|
702 lrintf((val - lrintf(val)) * 100));
|
Chris@101
|
703 // std::cerr << "shouldConvertMIDIToHz true, val now = " << val << std::endl;
|
Chris@101
|
704 }
|
Chris@101
|
705
|
Chris@101
|
706 if (logarithmic) {
|
Chris@197
|
707 val = LogRange::map(val);
|
Chris@101
|
708 // std::cerr << "logarithmic true, val now = " << val << std::endl;
|
Chris@101
|
709 }
|
Chris@101
|
710
|
Chris@101
|
711 int y = int(h - ((val - min) * h) / (max - min)) - 1;
|
Chris@101
|
712 // std::cerr << "y = " << y << std::endl;
|
Chris@101
|
713 return y;
|
Chris@30
|
714 }
|
Chris@30
|
715
|
Chris@30
|
716 float
|
matthiasm@620
|
717 FlexiNoteLayer::getValueForY(View *v, int y) const
|
Chris@30
|
718 {
|
Chris@101
|
719 float min = 0.0, max = 0.0;
|
Chris@101
|
720 bool logarithmic = false;
|
Chris@44
|
721 int h = v->height();
|
Chris@30
|
722
|
Chris@101
|
723 getScaleExtents(v, min, max, logarithmic);
|
Chris@101
|
724
|
Chris@101
|
725 float val = min + (float(h - y) * float(max - min)) / h;
|
Chris@101
|
726
|
Chris@101
|
727 if (logarithmic) {
|
Chris@197
|
728 val = powf(10.f, val);
|
Chris@101
|
729 }
|
Chris@101
|
730
|
Chris@101
|
731 if (shouldConvertMIDIToHz()) {
|
Chris@101
|
732 val = Pitch::getPitchForFrequency(val);
|
Chris@101
|
733 }
|
Chris@101
|
734
|
Chris@101
|
735 return val;
|
Chris@30
|
736 }
|
Chris@30
|
737
|
Chris@439
|
738 bool
|
matthiasm@620
|
739 FlexiNoteLayer::shouldAutoAlign() const
|
Chris@439
|
740 {
|
Chris@439
|
741 if (!m_model) return false;
|
Chris@439
|
742 return (m_verticalScale == AutoAlignScale);
|
Chris@439
|
743 }
|
Chris@439
|
744
|
Chris@30
|
745 void
|
matthiasm@620
|
746 FlexiNoteLayer::paint(View *v, QPainter &paint, QRect rect) const
|
Chris@30
|
747 {
|
Chris@30
|
748 if (!m_model || !m_model->isOK()) return;
|
Chris@30
|
749
|
Chris@30
|
750 int sampleRate = m_model->getSampleRate();
|
Chris@30
|
751 if (!sampleRate) return;
|
Chris@30
|
752
|
matthiasm@620
|
753 // Profiler profiler("FlexiNoteLayer::paint", true);
|
Chris@30
|
754
|
Chris@30
|
755 int x0 = rect.left(), x1 = rect.right();
|
Chris@44
|
756 long frame0 = v->getFrameForX(x0);
|
Chris@44
|
757 long frame1 = v->getFrameForX(x1);
|
Chris@30
|
758
|
matthiasm@620
|
759 FlexiNoteModel::PointList points(m_model->getPoints(frame0, frame1));
|
Chris@30
|
760 if (points.empty()) return;
|
Chris@30
|
761
|
Chris@287
|
762 paint.setPen(getBaseQColor());
|
Chris@30
|
763
|
Chris@287
|
764 QColor brushColour(getBaseQColor());
|
Chris@30
|
765 brushColour.setAlpha(80);
|
Chris@30
|
766
|
matthiasm@620
|
767 // SVDEBUG << "FlexiNoteLayer::paint: resolution is "
|
gyorgyf@646
|
768 // << m_model->getResolution() << " frames" << endl;
|
Chris@30
|
769
|
Chris@30
|
770 float min = m_model->getValueMinimum();
|
Chris@30
|
771 float max = m_model->getValueMaximum();
|
Chris@30
|
772 if (max == min) max = min + 1.0;
|
Chris@30
|
773
|
Chris@30
|
774 QPoint localPos;
|
matthiasm@620
|
775 FlexiNoteModel::Point illuminatePoint(0);
|
Chris@551
|
776 bool shouldIlluminate = false;
|
Chris@30
|
777
|
Chris@44
|
778 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
|
Chris@551
|
779 shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(),
|
Chris@551
|
780 illuminatePoint);
|
Chris@30
|
781 }
|
Chris@30
|
782
|
Chris@30
|
783 paint.save();
|
Chris@30
|
784 paint.setRenderHint(QPainter::Antialiasing, false);
|
Chris@30
|
785
|
matthiasm@620
|
786 for (FlexiNoteModel::PointList::const_iterator i = points.begin();
|
gyorgyf@646
|
787 i != points.end(); ++i) {
|
Chris@30
|
788
|
gyorgyf@646
|
789 const FlexiNoteModel::Point &p(*i);
|
Chris@30
|
790
|
gyorgyf@646
|
791 int x = v->getXForFrame(p.frame);
|
gyorgyf@646
|
792 int y = getYForValue(v, p.value);
|
gyorgyf@646
|
793 int w = v->getXForFrame(p.frame + p.duration) - x;
|
gyorgyf@647
|
794 int h = NOTE_HEIGHT; //GF: larger notes
|
gyorgyf@646
|
795
|
gyorgyf@646
|
796 if (m_model->getValueQuantization() != 0.0) {
|
gyorgyf@646
|
797 h = y - getYForValue(v, p.value + m_model->getValueQuantization());
|
gyorgyf@647
|
798 if (h < NOTE_HEIGHT) h = NOTE_HEIGHT; //GF: larger notes
|
gyorgyf@646
|
799 }
|
Chris@30
|
800
|
gyorgyf@646
|
801 if (w < 1) w = 1;
|
gyorgyf@646
|
802 paint.setPen(getBaseQColor());
|
gyorgyf@646
|
803 paint.setBrush(brushColour);
|
Chris@30
|
804
|
matthiasm@651
|
805 // if (shouldIlluminate &&
|
matthiasm@651
|
806 // // "illuminatePoint == p"
|
matthiasm@651
|
807 // !FlexiNoteModel::Point::Comparator()(illuminatePoint, p) &&
|
matthiasm@651
|
808 // !FlexiNoteModel::Point::Comparator()(p, illuminatePoint)) {
|
matthiasm@651
|
809 //
|
matthiasm@651
|
810 // paint.setPen(v->getForeground());
|
matthiasm@651
|
811 // paint.setBrush(v->getForeground());
|
matthiasm@651
|
812 //
|
matthiasm@651
|
813 // QString vlabel = QString("%1%2").arg(p.value).arg(m_model->getScaleUnits());
|
matthiasm@651
|
814 // v->drawVisibleText(paint,
|
matthiasm@651
|
815 // x - paint.fontMetrics().width(vlabel) - 2,
|
matthiasm@651
|
816 // y + paint.fontMetrics().height()/2
|
matthiasm@651
|
817 // - paint.fontMetrics().descent(),
|
matthiasm@651
|
818 // vlabel, View::OutlinedText);
|
matthiasm@651
|
819 //
|
matthiasm@651
|
820 // QString hlabel = RealTime::frame2RealTime
|
matthiasm@651
|
821 // (p.frame, m_model->getSampleRate()).toText(true).c_str();
|
matthiasm@651
|
822 // v->drawVisibleText(paint,
|
matthiasm@651
|
823 // x,
|
matthiasm@651
|
824 // y - h/2 - paint.fontMetrics().descent() - 2,
|
matthiasm@651
|
825 // hlabel, View::OutlinedText);
|
matthiasm@651
|
826 // }
|
gyorgyf@646
|
827
|
gyorgyf@646
|
828 paint.drawRect(x, y - h/2, w, h);
|
Chris@30
|
829 }
|
Chris@30
|
830
|
Chris@30
|
831 paint.restore();
|
Chris@30
|
832 }
|
Chris@30
|
833
|
Chris@30
|
834 void
|
matthiasm@620
|
835 FlexiNoteLayer::drawStart(View *v, QMouseEvent *e)
|
Chris@30
|
836 {
|
matthiasm@620
|
837 // SVDEBUG << "FlexiNoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl;
|
Chris@30
|
838
|
Chris@30
|
839 if (!m_model) return;
|
Chris@30
|
840
|
Chris@44
|
841 long frame = v->getFrameForX(e->x());
|
Chris@30
|
842 if (frame < 0) frame = 0;
|
Chris@30
|
843 frame = frame / m_model->getResolution() * m_model->getResolution();
|
Chris@30
|
844
|
Chris@44
|
845 float value = getValueForY(v, e->y());
|
Chris@30
|
846
|
matthiasm@620
|
847 m_editingPoint = FlexiNoteModel::Point(frame, value, 0, 0.8, tr("New Point"));
|
Chris@30
|
848 m_originalPoint = m_editingPoint;
|
Chris@30
|
849
|
Chris@376
|
850 if (m_editingCommand) finish(m_editingCommand);
|
matthiasm@620
|
851 m_editingCommand = new FlexiNoteModel::EditCommand(m_model,
|
gyorgyf@646
|
852 tr("Draw Point"));
|
Chris@30
|
853 m_editingCommand->addPoint(m_editingPoint);
|
Chris@30
|
854
|
Chris@30
|
855 m_editing = true;
|
Chris@30
|
856 }
|
Chris@30
|
857
|
Chris@30
|
858 void
|
matthiasm@620
|
859 FlexiNoteLayer::drawDrag(View *v, QMouseEvent *e)
|
Chris@30
|
860 {
|
matthiasm@620
|
861 // SVDEBUG << "FlexiNoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl;
|
Chris@30
|
862
|
Chris@30
|
863 if (!m_model || !m_editing) return;
|
Chris@30
|
864
|
Chris@44
|
865 long frame = v->getFrameForX(e->x());
|
Chris@30
|
866 if (frame < 0) frame = 0;
|
Chris@30
|
867 frame = frame / m_model->getResolution() * m_model->getResolution();
|
Chris@30
|
868
|
Chris@101
|
869 float newValue = getValueForY(v, e->y());
|
Chris@101
|
870
|
Chris@101
|
871 long newFrame = m_editingPoint.frame;
|
Chris@101
|
872 long newDuration = frame - newFrame;
|
Chris@101
|
873 if (newDuration < 0) {
|
Chris@101
|
874 newFrame = frame;
|
Chris@101
|
875 newDuration = -newDuration;
|
Chris@101
|
876 } else if (newDuration == 0) {
|
Chris@101
|
877 newDuration = 1;
|
Chris@101
|
878 }
|
Chris@30
|
879
|
Chris@30
|
880 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@101
|
881 m_editingPoint.frame = newFrame;
|
Chris@101
|
882 m_editingPoint.value = newValue;
|
Chris@101
|
883 m_editingPoint.duration = newDuration;
|
Chris@30
|
884 m_editingCommand->addPoint(m_editingPoint);
|
Chris@30
|
885 }
|
Chris@30
|
886
|
Chris@30
|
887 void
|
matthiasm@620
|
888 FlexiNoteLayer::drawEnd(View *, QMouseEvent *)
|
Chris@30
|
889 {
|
matthiasm@620
|
890 // SVDEBUG << "FlexiNoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl;
|
Chris@30
|
891 if (!m_model || !m_editing) return;
|
Chris@376
|
892 finish(m_editingCommand);
|
Chris@30
|
893 m_editingCommand = 0;
|
Chris@30
|
894 m_editing = false;
|
Chris@30
|
895 }
|
Chris@30
|
896
|
Chris@30
|
897 void
|
matthiasm@620
|
898 FlexiNoteLayer::eraseStart(View *v, QMouseEvent *e)
|
Chris@335
|
899 {
|
Chris@335
|
900 if (!m_model) return;
|
Chris@335
|
901
|
Chris@550
|
902 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
|
Chris@335
|
903
|
Chris@335
|
904 if (m_editingCommand) {
|
gyorgyf@646
|
905 finish(m_editingCommand);
|
gyorgyf@646
|
906 m_editingCommand = 0;
|
Chris@335
|
907 }
|
Chris@335
|
908
|
Chris@335
|
909 m_editing = true;
|
Chris@335
|
910 }
|
Chris@335
|
911
|
Chris@335
|
912 void
|
matthiasm@620
|
913 FlexiNoteLayer::eraseDrag(View *v, QMouseEvent *e)
|
Chris@335
|
914 {
|
Chris@335
|
915 }
|
Chris@335
|
916
|
Chris@335
|
917 void
|
matthiasm@620
|
918 FlexiNoteLayer::eraseEnd(View *v, QMouseEvent *e)
|
Chris@335
|
919 {
|
Chris@335
|
920 if (!m_model || !m_editing) return;
|
Chris@335
|
921
|
Chris@335
|
922 m_editing = false;
|
Chris@335
|
923
|
matthiasm@620
|
924 FlexiNoteModel::Point p(0);
|
Chris@550
|
925 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
|
Chris@550
|
926 if (p.frame != m_editingPoint.frame || p.value != m_editingPoint.value) return;
|
Chris@550
|
927
|
matthiasm@620
|
928 m_editingCommand = new FlexiNoteModel::EditCommand(m_model, tr("Erase Point"));
|
Chris@335
|
929
|
Chris@335
|
930 m_editingCommand->deletePoint(m_editingPoint);
|
Chris@335
|
931
|
Chris@376
|
932 finish(m_editingCommand);
|
Chris@335
|
933 m_editingCommand = 0;
|
Chris@335
|
934 m_editing = false;
|
Chris@335
|
935 }
|
Chris@335
|
936
|
Chris@335
|
937 void
|
matthiasm@620
|
938 FlexiNoteLayer::editStart(View *v, QMouseEvent *e)
|
Chris@30
|
939 {
|
matthiasm@620
|
940 // SVDEBUG << "FlexiNoteLayer::editStart(" << e->x() << "," << e->y() << ")" << endl;
|
gyorgyf@635
|
941 std::cerr << "FlexiNoteLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@30
|
942
|
Chris@30
|
943 if (!m_model) return;
|
Chris@30
|
944
|
Chris@550
|
945 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
|
matthiasm@651
|
946 m_originalPoint = FlexiNote(m_editingPoint);
|
gyorgyf@649
|
947
|
matthiasm@651
|
948 if (m_editMode == RightBoundary) {
|
matthiasm@651
|
949 m_dragPointX = v->getXForFrame(m_editingPoint.frame + m_editingPoint.duration);
|
gyorgyf@649
|
950 } else {
|
gyorgyf@649
|
951 m_dragPointX = v->getXForFrame(m_editingPoint.frame);
|
gyorgyf@649
|
952 }
|
Chris@551
|
953 m_dragPointY = getYForValue(v, m_editingPoint.value);
|
Chris@551
|
954
|
Chris@30
|
955 if (m_editingCommand) {
|
gyorgyf@646
|
956 finish(m_editingCommand);
|
gyorgyf@646
|
957 m_editingCommand = 0;
|
Chris@30
|
958 }
|
Chris@30
|
959
|
Chris@30
|
960 m_editing = true;
|
Chris@551
|
961 m_dragStartX = e->x();
|
Chris@551
|
962 m_dragStartY = e->y();
|
matthiasm@651
|
963
|
matthiasm@651
|
964 long onset = m_originalPoint.frame;
|
matthiasm@651
|
965 long offset = m_originalPoint.frame + m_originalPoint.duration - 1;
|
matthiasm@651
|
966
|
matthiasm@651
|
967 m_greatestLeftNeighbourFrame = -1;
|
matthiasm@651
|
968 m_smallestRightNeighbourFrame = std::numeric_limits<long>::max();
|
matthiasm@651
|
969
|
matthiasm@651
|
970 for (FlexiNoteModel::PointList::const_iterator i = m_model->getPoints().begin();
|
matthiasm@651
|
971 i != m_model->getPoints().end(); ++i) {
|
matthiasm@651
|
972 FlexiNote currentNote = *i;
|
matthiasm@651
|
973
|
matthiasm@651
|
974 // left boundary
|
matthiasm@651
|
975 if (currentNote.frame + currentNote.duration - 1 < onset) {
|
matthiasm@651
|
976 m_greatestLeftNeighbourFrame = currentNote.frame + currentNote.duration - 1;
|
matthiasm@651
|
977 }
|
matthiasm@651
|
978
|
matthiasm@651
|
979 // right boundary
|
matthiasm@651
|
980 if (currentNote.frame > offset) {
|
matthiasm@651
|
981 m_smallestRightNeighbourFrame = currentNote.frame;
|
matthiasm@651
|
982 break;
|
matthiasm@651
|
983 }
|
matthiasm@651
|
984 }
|
matthiasm@651
|
985 std::cerr << "note frame: " << onset << ", left boundary: " << m_greatestLeftNeighbourFrame << ", right boundary: " << m_smallestRightNeighbourFrame << std::endl;
|
Chris@30
|
986 }
|
Chris@30
|
987
|
Chris@30
|
988 void
|
matthiasm@620
|
989 FlexiNoteLayer::editDrag(View *v, QMouseEvent *e)
|
Chris@30
|
990 {
|
matthiasm@620
|
991 // SVDEBUG << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl;
|
gyorgyf@635
|
992 std::cerr << "FlexiNoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
|
Chris@30
|
993
|
Chris@30
|
994 if (!m_model || !m_editing) return;
|
Chris@30
|
995
|
Chris@551
|
996 int xdist = e->x() - m_dragStartX;
|
Chris@551
|
997 int ydist = e->y() - m_dragStartY;
|
Chris@551
|
998 int newx = m_dragPointX + xdist;
|
Chris@551
|
999 int newy = m_dragPointY + ydist;
|
Chris@551
|
1000
|
Chris@551
|
1001 long frame = v->getFrameForX(newx);
|
Chris@30
|
1002 if (frame < 0) frame = 0;
|
Chris@30
|
1003 frame = frame / m_model->getResolution() * m_model->getResolution();
|
matthiasm@651
|
1004
|
Chris@551
|
1005 float value = getValueForY(v, newy);
|
Chris@30
|
1006
|
Chris@30
|
1007 if (!m_editingCommand) {
|
gyorgyf@646
|
1008 m_editingCommand = new FlexiNoteModel::EditCommand(m_model,
|
gyorgyf@646
|
1009 tr("Drag Point"));
|
Chris@30
|
1010 }
|
Chris@30
|
1011
|
Chris@30
|
1012 m_editingCommand->deletePoint(m_editingPoint);
|
matthiasm@651
|
1013
|
matthiasm@651
|
1014 std::cerr << "edit mode: " << m_editMode << std::endl;
|
matthiasm@651
|
1015 switch (m_editMode) {
|
matthiasm@651
|
1016 case LeftBoundary : {
|
matthiasm@651
|
1017 if ((frame > m_greatestLeftNeighbourFrame)
|
matthiasm@651
|
1018 && (frame < m_originalPoint.frame + m_originalPoint.duration - 1)
|
matthiasm@651
|
1019 && (frame < m_smallestRightNeighbourFrame)) {
|
matthiasm@651
|
1020 m_editingPoint.duration = m_editingPoint.frame + m_editingPoint.duration - frame + 1;
|
matthiasm@651
|
1021 m_editingPoint.frame = frame;
|
matthiasm@651
|
1022 }
|
matthiasm@651
|
1023 break;
|
matthiasm@651
|
1024 }
|
matthiasm@651
|
1025 case RightBoundary : {
|
matthiasm@651
|
1026 long tempDuration = frame - m_originalPoint.frame;
|
matthiasm@651
|
1027 if (tempDuration > 0 && m_originalPoint.frame + tempDuration - 1 < m_smallestRightNeighbourFrame) {
|
matthiasm@651
|
1028 m_editingPoint.duration = tempDuration;
|
matthiasm@651
|
1029 }
|
matthiasm@651
|
1030 break;
|
matthiasm@651
|
1031 }
|
matthiasm@651
|
1032 case DragNote : {
|
matthiasm@651
|
1033 if (frame <= m_smallestRightNeighbourFrame - m_editingPoint.duration
|
matthiasm@651
|
1034 && frame > m_greatestLeftNeighbourFrame) {
|
matthiasm@651
|
1035 m_editingPoint.frame = frame; // only move if it doesn't overlap with right note or left note
|
matthiasm@651
|
1036 }
|
matthiasm@651
|
1037 m_editingPoint.value = value;
|
matthiasm@651
|
1038 break;
|
matthiasm@651
|
1039 }
|
gyorgyf@649
|
1040 }
|
gyorgyf@653
|
1041 <<<<<<< local
|
gyorgyf@653
|
1042 if (m_editMode == dragNote) m_editingPoint.value = value;
|
gyorgyf@653
|
1043 =======
|
matthiasm@651
|
1044
|
gyorgyf@653
|
1045 >>>>>>> other
|
Chris@30
|
1046 m_editingCommand->addPoint(m_editingPoint);
|
gyorgyf@649
|
1047 std::cerr << "added new point(" << m_editingPoint.frame << "," << m_editingPoint.duration << ")" << std::endl;
|
gyorgyf@649
|
1048
|
Chris@30
|
1049 }
|
Chris@30
|
1050
|
Chris@30
|
1051 void
|
gyorgyf@635
|
1052 FlexiNoteLayer::editEnd(View *v, QMouseEvent *e)
|
Chris@30
|
1053 {
|
matthiasm@620
|
1054 // SVDEBUG << "FlexiNoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl;
|
gyorgyf@635
|
1055 std::cerr << "FlexiNoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
|
gyorgyf@635
|
1056
|
Chris@30
|
1057 if (!m_model || !m_editing) return;
|
Chris@30
|
1058
|
Chris@30
|
1059 if (m_editingCommand) {
|
Chris@30
|
1060
|
gyorgyf@646
|
1061 QString newName = m_editingCommand->getName();
|
Chris@30
|
1062
|
gyorgyf@646
|
1063 if (m_editingPoint.frame != m_originalPoint.frame) {
|
gyorgyf@646
|
1064 if (m_editingPoint.value != m_originalPoint.value) {
|
gyorgyf@646
|
1065 newName = tr("Edit Point");
|
gyorgyf@646
|
1066 } else {
|
gyorgyf@646
|
1067 newName = tr("Relocate Point");
|
gyorgyf@646
|
1068 }
|
gyorgyf@646
|
1069 } else {
|
gyorgyf@646
|
1070 newName = tr("Change Point Value");
|
gyorgyf@646
|
1071 }
|
Chris@30
|
1072
|
gyorgyf@646
|
1073 m_editingCommand->setName(newName);
|
gyorgyf@646
|
1074 finish(m_editingCommand);
|
Chris@30
|
1075 }
|
Chris@30
|
1076
|
Chris@30
|
1077 m_editingCommand = 0;
|
Chris@30
|
1078 m_editing = false;
|
Chris@30
|
1079 }
|
Chris@30
|
1080
|
gyorgyf@635
|
1081 void
|
gyorgyf@635
|
1082 FlexiNoteLayer::splitStart(View *v, QMouseEvent *e)
|
gyorgyf@635
|
1083 {
|
gyorgyf@646
|
1084 // GF: note splitting starts (!! remove printing soon)
|
gyorgyf@646
|
1085 std::cerr << "splitStart" << std::endl;
|
gyorgyf@646
|
1086 if (!m_model) return;
|
gyorgyf@635
|
1087
|
gyorgyf@635
|
1088 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
|
gyorgyf@635
|
1089 // m_originalPoint = m_editingPoint;
|
gyorgyf@635
|
1090 //
|
gyorgyf@635
|
1091 // m_dragPointX = v->getXForFrame(m_editingPoint.frame);
|
gyorgyf@635
|
1092 // m_dragPointY = getYForValue(v, m_editingPoint.value);
|
gyorgyf@635
|
1093
|
gyorgyf@635
|
1094 if (m_editingCommand) {
|
gyorgyf@646
|
1095 finish(m_editingCommand);
|
gyorgyf@646
|
1096 m_editingCommand = 0;
|
gyorgyf@635
|
1097 }
|
gyorgyf@635
|
1098
|
gyorgyf@635
|
1099 m_editing = true;
|
gyorgyf@635
|
1100 m_dragStartX = e->x();
|
gyorgyf@635
|
1101 m_dragStartY = e->y();
|
gyorgyf@635
|
1102
|
gyorgyf@635
|
1103 }
|
gyorgyf@635
|
1104
|
gyorgyf@635
|
1105 void
|
gyorgyf@635
|
1106 FlexiNoteLayer::splitEnd(View *v, QMouseEvent *e)
|
gyorgyf@635
|
1107 {
|
gyorgyf@646
|
1108 // GF: note splitting ends. (!! remove printing soon)
|
gyorgyf@646
|
1109 std::cerr << "splitEnd" << std::endl;
|
matthiasm@651
|
1110 if (!m_model || !m_editing || m_editMode != SplitNote) return;
|
gyorgyf@635
|
1111
|
gyorgyf@635
|
1112 int xdist = e->x() - m_dragStartX;
|
gyorgyf@635
|
1113 int ydist = e->y() - m_dragStartY;
|
gyorgyf@635
|
1114 if (xdist != 0 || ydist != 0) {
|
gyorgyf@646
|
1115 std::cerr << "mouse moved" << std::endl;
|
gyorgyf@635
|
1116 return;
|
gyorgyf@635
|
1117 }
|
gyorgyf@635
|
1118
|
gyorgyf@649
|
1119 // MM: simpler declaration
|
matthiasm@648
|
1120 FlexiNote note(0);
|
gyorgyf@635
|
1121 if (!getPointToDrag(v, e->x(), e->y(), note)) return;
|
gyorgyf@635
|
1122
|
gyorgyf@635
|
1123 long frame = v->getFrameForX(e->x());
|
gyorgyf@635
|
1124
|
matthiasm@648
|
1125 int gap = 0; // MM: I prefer a gap of 0, but we can decide later
|
matthiasm@648
|
1126
|
gyorgyf@649
|
1127 // MM: changed this a bit, to make it slightly clearer (// GF: nice changes!)
|
matthiasm@648
|
1128 FlexiNote newNote1(note.frame, note.value,
|
matthiasm@648
|
1129 frame - note.frame - gap,
|
matthiasm@648
|
1130 note.level, note.label);
|
matthiasm@648
|
1131
|
matthiasm@648
|
1132 FlexiNote newNote2(frame, note.value,
|
matthiasm@648
|
1133 note.duration - newNote1.duration,
|
matthiasm@648
|
1134 note.level, note.label);
|
gyorgyf@635
|
1135
|
gyorgyf@635
|
1136 FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand
|
gyorgyf@635
|
1137 (m_model, tr("Edit Point"));
|
gyorgyf@635
|
1138 command->deletePoint(note);
|
gyorgyf@649
|
1139 if ((e->modifiers() & Qt::ShiftModifier)) {
|
gyorgyf@649
|
1140 finish(command);
|
gyorgyf@649
|
1141 return;
|
gyorgyf@649
|
1142 }
|
gyorgyf@635
|
1143 command->addPoint(newNote1);
|
gyorgyf@635
|
1144 command->addPoint(newNote2);
|
gyorgyf@635
|
1145 finish(command);
|
gyorgyf@646
|
1146
|
gyorgyf@646
|
1147 }
|
gyorgyf@646
|
1148
|
gyorgyf@646
|
1149 void
|
gyorgyf@646
|
1150 FlexiNoteLayer::mouseMoveEvent(View *v, QMouseEvent *e)
|
gyorgyf@646
|
1151 {
|
gyorgyf@646
|
1152 // GF: context sensitive cursors
|
gyorgyf@646
|
1153 // v->setCursor(Qt::ArrowCursor);
|
gyorgyf@646
|
1154 FlexiNoteModel::Point note(0);
|
gyorgyf@646
|
1155 if (!getNoteToEdit(v, e->x(), e->y(), note)) {
|
gyorgyf@646
|
1156 // v->setCursor(Qt::UpArrowCursor);
|
gyorgyf@646
|
1157 return;
|
gyorgyf@646
|
1158 }
|
gyorgyf@646
|
1159
|
gyorgyf@646
|
1160 bool closeToLeft = false, closeToRight = false, closeToTop = false, closeToBottom = false;
|
gyorgyf@646
|
1161 getRelativeMousePosition(v, note, e->x(), e->y(), closeToLeft, closeToRight, closeToTop, closeToBottom);
|
gyorgyf@646
|
1162 // if (!closeToLeft) return;
|
gyorgyf@646
|
1163 // if (closeToTop) v->setCursor(Qt::SizeVerCursor);
|
gyorgyf@649
|
1164
|
matthiasm@651
|
1165 if (closeToLeft) { v->setCursor(Qt::SizeHorCursor); m_editMode = LeftBoundary; return; }
|
matthiasm@651
|
1166 if (closeToRight) { v->setCursor(Qt::SizeHorCursor); m_editMode = RightBoundary; return; }
|
matthiasm@651
|
1167 if (closeToTop) { v->setCursor(Qt::CrossCursor); m_editMode = DragNote; return; }
|
matthiasm@651
|
1168 if (closeToBottom) { v->setCursor(Qt::UpArrowCursor); m_editMode = SplitNote; return; }
|
gyorgyf@649
|
1169
|
gyorgyf@646
|
1170 v->setCursor(Qt::ArrowCursor);
|
gyorgyf@646
|
1171
|
gyorgyf@649
|
1172 // std::cerr << "Mouse moved in edit mode over FlexiNoteLayer" << std::endl;
|
gyorgyf@646
|
1173 // v->setCursor(Qt::SizeHorCursor);
|
gyorgyf@646
|
1174
|
gyorgyf@646
|
1175 }
|
gyorgyf@646
|
1176
|
gyorgyf@646
|
1177 void
|
gyorgyf@646
|
1178 FlexiNoteLayer::getRelativeMousePosition(View *v, FlexiNoteModel::Point ¬e, int x, int y, bool &closeToLeft, bool &closeToRight, bool &closeToTop, bool &closeToBottom) const
|
gyorgyf@646
|
1179 {
|
gyorgyf@649
|
1180 // GF: TODO: consoloidate the tolerance values
|
gyorgyf@646
|
1181 if (!m_model) return;
|
gyorgyf@646
|
1182
|
matthiasm@651
|
1183 int ctol = 0;
|
gyorgyf@646
|
1184 int noteStartX = v->getXForFrame(note.frame);
|
gyorgyf@646
|
1185 int noteEndX = v->getXForFrame(note.frame + note.duration);
|
gyorgyf@646
|
1186 int noteValueY = getYForValue(v,note.value);
|
gyorgyf@646
|
1187 int noteStartY = noteValueY - (NOTE_HEIGHT / 2);
|
gyorgyf@646
|
1188 int noteEndY = noteValueY + (NOTE_HEIGHT / 2);
|
gyorgyf@646
|
1189
|
gyorgyf@646
|
1190 bool closeToNote = false;
|
gyorgyf@646
|
1191
|
gyorgyf@646
|
1192 if (y >= noteStartY-ctol && y <= noteEndY+ctol && x >= noteStartX-ctol && x <= noteEndX+ctol) closeToNote = true;
|
gyorgyf@646
|
1193 if (!closeToNote) return;
|
gyorgyf@646
|
1194
|
matthiasm@651
|
1195 int tol = NOTE_HEIGHT / 2;
|
gyorgyf@646
|
1196
|
gyorgyf@646
|
1197 if (x >= noteStartX - tol && x <= noteStartX + tol) closeToLeft = true;
|
gyorgyf@646
|
1198 if (x >= noteEndX - tol && x <= noteEndX + tol) closeToRight = true;
|
gyorgyf@646
|
1199 if (y >= noteStartY - tol && y <= noteStartY + tol) closeToTop = true;
|
gyorgyf@646
|
1200 if (y >= noteEndY - tol && y <= noteEndY + tol) closeToBottom = true;
|
gyorgyf@646
|
1201
|
gyorgyf@635
|
1202 }
|
gyorgyf@635
|
1203
|
gyorgyf@635
|
1204
|
Chris@255
|
1205 bool
|
matthiasm@620
|
1206 FlexiNoteLayer::editOpen(View *v, QMouseEvent *e)
|
Chris@70
|
1207 {
|
gyorgyf@633
|
1208 std::cerr << "Opening note editor dialog" << std::endl;
|
Chris@255
|
1209 if (!m_model) return false;
|
Chris@70
|
1210
|
matthiasm@620
|
1211 FlexiNoteModel::Point note(0);
|
Chris@550
|
1212 if (!getPointToDrag(v, e->x(), e->y(), note)) return false;
|
Chris@550
|
1213
|
matthiasm@620
|
1214 // FlexiNoteModel::Point note = *points.begin();
|
Chris@70
|
1215
|
Chris@70
|
1216 ItemEditDialog *dialog = new ItemEditDialog
|
Chris@70
|
1217 (m_model->getSampleRate(),
|
Chris@70
|
1218 ItemEditDialog::ShowTime |
|
Chris@70
|
1219 ItemEditDialog::ShowDuration |
|
Chris@70
|
1220 ItemEditDialog::ShowValue |
|
Chris@100
|
1221 ItemEditDialog::ShowText,
|
Chris@100
|
1222 m_model->getScaleUnits());
|
Chris@70
|
1223
|
Chris@70
|
1224 dialog->setFrameTime(note.frame);
|
Chris@70
|
1225 dialog->setValue(note.value);
|
Chris@70
|
1226 dialog->setFrameDuration(note.duration);
|
Chris@70
|
1227 dialog->setText(note.label);
|
Chris@70
|
1228
|
Chris@70
|
1229 if (dialog->exec() == QDialog::Accepted) {
|
Chris@70
|
1230
|
matthiasm@620
|
1231 FlexiNoteModel::Point newNote = note;
|
Chris@70
|
1232 newNote.frame = dialog->getFrameTime();
|
Chris@70
|
1233 newNote.value = dialog->getValue();
|
Chris@70
|
1234 newNote.duration = dialog->getFrameDuration();
|
Chris@70
|
1235 newNote.label = dialog->getText();
|
Chris@70
|
1236
|
matthiasm@620
|
1237 FlexiNoteModel::EditCommand *command = new FlexiNoteModel::EditCommand
|
Chris@70
|
1238 (m_model, tr("Edit Point"));
|
Chris@70
|
1239 command->deletePoint(note);
|
Chris@70
|
1240 command->addPoint(newNote);
|
Chris@376
|
1241 finish(command);
|
Chris@70
|
1242 }
|
Chris@70
|
1243
|
Chris@70
|
1244 delete dialog;
|
Chris@255
|
1245 return true;
|
Chris@70
|
1246 }
|
Chris@70
|
1247
|
Chris@70
|
1248 void
|
matthiasm@620
|
1249 FlexiNoteLayer::moveSelection(Selection s, size_t newStartFrame)
|
Chris@43
|
1250 {
|
Chris@99
|
1251 if (!m_model) return;
|
Chris@99
|
1252
|
matthiasm@620
|
1253 FlexiNoteModel::EditCommand *command =
|
gyorgyf@646
|
1254 new FlexiNoteModel::EditCommand(m_model, tr("Drag Selection"));
|
Chris@43
|
1255
|
matthiasm@620
|
1256 FlexiNoteModel::PointList points =
|
gyorgyf@646
|
1257 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@43
|
1258
|
matthiasm@620
|
1259 for (FlexiNoteModel::PointList::iterator i = points.begin();
|
gyorgyf@646
|
1260 i != points.end(); ++i) {
|
Chris@43
|
1261
|
gyorgyf@646
|
1262 if (s.contains(i->frame)) {
|
gyorgyf@646
|
1263 FlexiNoteModel::Point newPoint(*i);
|
gyorgyf@646
|
1264 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
|
gyorgyf@646
|
1265 command->deletePoint(*i);
|
gyorgyf@646
|
1266 command->addPoint(newPoint);
|
gyorgyf@646
|
1267 }
|
Chris@43
|
1268 }
|
Chris@43
|
1269
|
Chris@376
|
1270 finish(command);
|
Chris@43
|
1271 }
|
Chris@43
|
1272
|
Chris@43
|
1273 void
|
matthiasm@620
|
1274 FlexiNoteLayer::resizeSelection(Selection s, Selection newSize)
|
Chris@43
|
1275 {
|
Chris@99
|
1276 if (!m_model) return;
|
Chris@99
|
1277
|
matthiasm@620
|
1278 FlexiNoteModel::EditCommand *command =
|
gyorgyf@646
|
1279 new FlexiNoteModel::EditCommand(m_model, tr("Resize Selection"));
|
Chris@43
|
1280
|
matthiasm@620
|
1281 FlexiNoteModel::PointList points =
|
gyorgyf@646
|
1282 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@43
|
1283
|
Chris@43
|
1284 double ratio =
|
gyorgyf@646
|
1285 double(newSize.getEndFrame() - newSize.getStartFrame()) /
|
gyorgyf@646
|
1286 double(s.getEndFrame() - s.getStartFrame());
|
Chris@43
|
1287
|
matthiasm@620
|
1288 for (FlexiNoteModel::PointList::iterator i = points.begin();
|
gyorgyf@646
|
1289 i != points.end(); ++i) {
|
Chris@43
|
1290
|
gyorgyf@646
|
1291 if (s.contains(i->frame)) {
|
Chris@43
|
1292
|
gyorgyf@646
|
1293 double targetStart = i->frame;
|
gyorgyf@646
|
1294 targetStart = newSize.getStartFrame() +
|
gyorgyf@646
|
1295 double(targetStart - s.getStartFrame()) * ratio;
|
Chris@43
|
1296
|
gyorgyf@646
|
1297 double targetEnd = i->frame + i->duration;
|
gyorgyf@646
|
1298 targetEnd = newSize.getStartFrame() +
|
gyorgyf@646
|
1299 double(targetEnd - s.getStartFrame()) * ratio;
|
Chris@43
|
1300
|
gyorgyf@646
|
1301 FlexiNoteModel::Point newPoint(*i);
|
gyorgyf@646
|
1302 newPoint.frame = lrint(targetStart);
|
gyorgyf@646
|
1303 newPoint.duration = lrint(targetEnd - targetStart);
|
gyorgyf@646
|
1304 command->deletePoint(*i);
|
gyorgyf@646
|
1305 command->addPoint(newPoint);
|
gyorgyf@646
|
1306 }
|
Chris@43
|
1307 }
|
Chris@43
|
1308
|
Chris@376
|
1309 finish(command);
|
Chris@43
|
1310 }
|
Chris@43
|
1311
|
Chris@76
|
1312 void
|
matthiasm@620
|
1313 FlexiNoteLayer::deleteSelection(Selection s)
|
Chris@76
|
1314 {
|
Chris@99
|
1315 if (!m_model) return;
|
Chris@99
|
1316
|
matthiasm@620
|
1317 FlexiNoteModel::EditCommand *command =
|
gyorgyf@646
|
1318 new FlexiNoteModel::EditCommand(m_model, tr("Delete Selected Points"));
|
Chris@76
|
1319
|
matthiasm@620
|
1320 FlexiNoteModel::PointList points =
|
gyorgyf@646
|
1321 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@76
|
1322
|
matthiasm@620
|
1323 for (FlexiNoteModel::PointList::iterator i = points.begin();
|
gyorgyf@646
|
1324 i != points.end(); ++i) {
|
Chris@76
|
1325
|
Chris@76
|
1326 if (s.contains(i->frame)) {
|
Chris@76
|
1327 command->deletePoint(*i);
|
Chris@76
|
1328 }
|
Chris@76
|
1329 }
|
Chris@76
|
1330
|
Chris@376
|
1331 finish(command);
|
Chris@76
|
1332 }
|
Chris@76
|
1333
|
Chris@76
|
1334 void
|
matthiasm@620
|
1335 FlexiNoteLayer::copy(View *v, Selection s, Clipboard &to)
|
Chris@76
|
1336 {
|
Chris@99
|
1337 if (!m_model) return;
|
Chris@99
|
1338
|
matthiasm@620
|
1339 FlexiNoteModel::PointList points =
|
gyorgyf@646
|
1340 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
|
Chris@76
|
1341
|
matthiasm@620
|
1342 for (FlexiNoteModel::PointList::iterator i = points.begin();
|
gyorgyf@646
|
1343 i != points.end(); ++i) {
|
gyorgyf@646
|
1344 if (s.contains(i->frame)) {
|
Chris@335
|
1345 Clipboard::Point point(i->frame, i->value, i->duration, i->level, i->label);
|
Chris@360
|
1346 point.setReferenceFrame(alignToReference(v, i->frame));
|
Chris@76
|
1347 to.addPoint(point);
|
Chris@76
|
1348 }
|
Chris@76
|
1349 }
|
Chris@76
|
1350 }
|
Chris@76
|
1351
|
Chris@125
|
1352 bool
|
matthiasm@620
|
1353 FlexiNoteLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */)
|
Chris@76
|
1354 {
|
Chris@125
|
1355 if (!m_model) return false;
|
Chris@99
|
1356
|
Chris@76
|
1357 const Clipboard::PointList &points = from.getPoints();
|
Chris@76
|
1358
|
Chris@360
|
1359 bool realign = false;
|
Chris@360
|
1360
|
Chris@360
|
1361 if (clipboardHasDifferentAlignment(v, from)) {
|
Chris@360
|
1362
|
Chris@360
|
1363 QMessageBox::StandardButton button =
|
Chris@360
|
1364 QMessageBox::question(v, tr("Re-align pasted items?"),
|
Chris@360
|
1365 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
|
1366 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
|
Chris@360
|
1367 QMessageBox::Yes);
|
Chris@360
|
1368
|
Chris@360
|
1369 if (button == QMessageBox::Cancel) {
|
Chris@360
|
1370 return false;
|
Chris@360
|
1371 }
|
Chris@360
|
1372
|
Chris@360
|
1373 if (button == QMessageBox::Yes) {
|
Chris@360
|
1374 realign = true;
|
Chris@360
|
1375 }
|
Chris@360
|
1376 }
|
Chris@360
|
1377
|
matthiasm@620
|
1378 FlexiNoteModel::EditCommand *command =
|
gyorgyf@646
|
1379 new FlexiNoteModel::EditCommand(m_model, tr("Paste"));
|
Chris@76
|
1380
|
Chris@76
|
1381 for (Clipboard::PointList::const_iterator i = points.begin();
|
Chris@76
|
1382 i != points.end(); ++i) {
|
Chris@76
|
1383
|
Chris@76
|
1384 if (!i->haveFrame()) continue;
|
Chris@76
|
1385 size_t frame = 0;
|
Chris@360
|
1386
|
Chris@360
|
1387 if (!realign) {
|
Chris@360
|
1388
|
Chris@360
|
1389 frame = i->getFrame();
|
Chris@360
|
1390
|
Chris@360
|
1391 } else {
|
Chris@360
|
1392
|
Chris@360
|
1393 if (i->haveReferenceFrame()) {
|
Chris@360
|
1394 frame = i->getReferenceFrame();
|
Chris@360
|
1395 frame = alignFromReference(v, frame);
|
Chris@360
|
1396 } else {
|
Chris@360
|
1397 frame = i->getFrame();
|
Chris@360
|
1398 }
|
Chris@76
|
1399 }
|
Chris@360
|
1400
|
matthiasm@620
|
1401 FlexiNoteModel::Point newPoint(frame);
|
Chris@76
|
1402
|
Chris@76
|
1403 if (i->haveLabel()) newPoint.label = i->getLabel();
|
Chris@76
|
1404 if (i->haveValue()) newPoint.value = i->getValue();
|
Chris@76
|
1405 else newPoint.value = (m_model->getValueMinimum() +
|
Chris@76
|
1406 m_model->getValueMaximum()) / 2;
|
Chris@335
|
1407 if (i->haveLevel()) newPoint.level = i->getLevel();
|
Chris@76
|
1408 if (i->haveDuration()) newPoint.duration = i->getDuration();
|
Chris@125
|
1409 else {
|
Chris@125
|
1410 size_t nextFrame = frame;
|
Chris@125
|
1411 Clipboard::PointList::const_iterator j = i;
|
Chris@125
|
1412 for (; j != points.end(); ++j) {
|
Chris@125
|
1413 if (!j->haveFrame()) continue;
|
Chris@125
|
1414 if (j != i) break;
|
Chris@125
|
1415 }
|
Chris@125
|
1416 if (j != points.end()) {
|
Chris@125
|
1417 nextFrame = j->getFrame();
|
Chris@125
|
1418 }
|
Chris@125
|
1419 if (nextFrame == frame) {
|
Chris@125
|
1420 newPoint.duration = m_model->getResolution();
|
Chris@125
|
1421 } else {
|
Chris@125
|
1422 newPoint.duration = nextFrame - frame;
|
Chris@125
|
1423 }
|
Chris@125
|
1424 }
|
Chris@76
|
1425
|
Chris@76
|
1426 command->addPoint(newPoint);
|
Chris@76
|
1427 }
|
Chris@76
|
1428
|
Chris@376
|
1429 finish(command);
|
Chris@125
|
1430 return true;
|
Chris@76
|
1431 }
|
Chris@76
|
1432
|
Chris@507
|
1433 void
|
matthiasm@620
|
1434 FlexiNoteLayer::addNoteOn(long frame, int pitch, int velocity)
|
Chris@507
|
1435 {
|
matthiasm@620
|
1436 m_pendingNoteOns.insert(FlexiNote(frame, pitch, 0, float(velocity) / 127.0, ""));
|
Chris@507
|
1437 }
|
Chris@507
|
1438
|
Chris@507
|
1439 void
|
matthiasm@620
|
1440 FlexiNoteLayer::addNoteOff(long frame, int pitch)
|
Chris@507
|
1441 {
|
matthiasm@620
|
1442 for (FlexiNoteSet::iterator i = m_pendingNoteOns.begin();
|
Chris@507
|
1443 i != m_pendingNoteOns.end(); ++i) {
|
Chris@507
|
1444 if (lrintf((*i).value) == pitch) {
|
matthiasm@620
|
1445 FlexiNote note(*i);
|
Chris@507
|
1446 m_pendingNoteOns.erase(i);
|
Chris@507
|
1447 note.duration = frame - note.frame;
|
Chris@507
|
1448 if (m_model) {
|
matthiasm@620
|
1449 FlexiNoteModel::AddPointCommand *c = new FlexiNoteModel::AddPointCommand
|
matthiasm@620
|
1450 (m_model, note, tr("Record FlexiNote"));
|
Chris@507
|
1451 // execute and bundle:
|
Chris@507
|
1452 CommandHistory::getInstance()->addCommand(c, true, true);
|
Chris@507
|
1453 }
|
Chris@507
|
1454 break;
|
Chris@507
|
1455 }
|
Chris@507
|
1456 }
|
Chris@507
|
1457 }
|
Chris@507
|
1458
|
Chris@507
|
1459 void
|
matthiasm@620
|
1460 FlexiNoteLayer::abandonNoteOns()
|
Chris@507
|
1461 {
|
Chris@507
|
1462 m_pendingNoteOns.clear();
|
Chris@507
|
1463 }
|
Chris@507
|
1464
|
Chris@287
|
1465 int
|
matthiasm@620
|
1466 FlexiNoteLayer::getDefaultColourHint(bool darkbg, bool &impose)
|
Chris@287
|
1467 {
|
Chris@287
|
1468 impose = false;
|
Chris@287
|
1469 return ColourDatabase::getInstance()->getColourIndex
|
Chris@287
|
1470 (QString(darkbg ? "White" : "Black"));
|
Chris@287
|
1471 }
|
Chris@287
|
1472
|
Chris@316
|
1473 void
|
matthiasm@620
|
1474 FlexiNoteLayer::toXml(QTextStream &stream,
|
Chris@316
|
1475 QString indent, QString extraAttributes) const
|
Chris@30
|
1476 {
|
Chris@316
|
1477 SingleColourLayer::toXml(stream, indent, extraAttributes +
|
Chris@445
|
1478 QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ")
|
Chris@445
|
1479 .arg(m_verticalScale)
|
Chris@445
|
1480 .arg(m_scaleMinimum)
|
Chris@445
|
1481 .arg(m_scaleMaximum));
|
Chris@30
|
1482 }
|
Chris@30
|
1483
|
Chris@30
|
1484 void
|
matthiasm@620
|
1485 FlexiNoteLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@30
|
1486 {
|
Chris@287
|
1487 SingleColourLayer::setProperties(attributes);
|
Chris@30
|
1488
|
Chris@445
|
1489 bool ok, alsoOk;
|
Chris@30
|
1490 VerticalScale scale = (VerticalScale)
|
gyorgyf@646
|
1491 attributes.value("verticalScale").toInt(&ok);
|
Chris@30
|
1492 if (ok) setVerticalScale(scale);
|
Chris@445
|
1493
|
Chris@445
|
1494 float min = attributes.value("scaleMinimum").toFloat(&ok);
|
Chris@445
|
1495 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
|
Chris@445
|
1496 if (ok && alsoOk) setDisplayExtents(min, max);
|
Chris@30
|
1497 }
|
Chris@30
|
1498
|
matthiasm@651
|
1499 void
|
matthiasm@651
|
1500 FlexiNoteLayer::setVerticalRangeToNoteRange()
|
matthiasm@651
|
1501 {
|
matthiasm@651
|
1502 float minf = std::numeric_limits<float>::max();;
|
matthiasm@651
|
1503 float maxf = 0;
|
matthiasm@651
|
1504 for (FlexiNoteModel::PointList::const_iterator i = m_model->getPoints().begin();
|
matthiasm@651
|
1505 i != m_model->getPoints().end(); ++i) {
|
matthiasm@651
|
1506 FlexiNote note = *i;
|
matthiasm@651
|
1507 if (note.value < minf) minf = note.value;
|
matthiasm@651
|
1508 else if (note.value > maxf) maxf = note.value;
|
matthiasm@651
|
1509 std::cerr << "min frequency:" << minf << ", max frequency: " << maxf << std::endl;
|
matthiasm@651
|
1510 }
|
matthiasm@651
|
1511
|
matthiasm@651
|
1512 if (this) {
|
matthiasm@651
|
1513 setDisplayExtents(minf,maxf);
|
matthiasm@651
|
1514 }
|
matthiasm@651
|
1515 }
|
Chris@30
|
1516
|
matthiasm@651
|
1517
|