Chris@1511
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@1511
|
2
|
Chris@1511
|
3 /*
|
Chris@1511
|
4 Sonic Visualiser
|
Chris@1511
|
5 An audio file viewer and annotation editor.
|
Chris@1511
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@1511
|
7
|
Chris@1511
|
8 This program is free software; you can redistribute it and/or
|
Chris@1511
|
9 modify it under the terms of the GNU General Public License as
|
Chris@1511
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@1511
|
11 License, or (at your option) any later version. See the file
|
Chris@1511
|
12 COPYING included with this distribution for more information.
|
Chris@1511
|
13 */
|
Chris@1511
|
14
|
Chris@1511
|
15 #include "TimeFrequencyBoxLayer.h"
|
Chris@1511
|
16
|
Chris@1511
|
17 #include "data/model/Model.h"
|
Chris@1511
|
18 #include "base/RealTime.h"
|
Chris@1511
|
19 #include "base/Profiler.h"
|
Chris@1511
|
20 #include "base/LogRange.h"
|
Chris@1511
|
21
|
Chris@1511
|
22 #include "ColourDatabase.h"
|
Chris@1511
|
23 #include "ColourMapper.h"
|
Chris@1511
|
24 #include "LinearNumericalScale.h"
|
Chris@1511
|
25 #include "LogNumericalScale.h"
|
Chris@1511
|
26 #include "PaintAssistant.h"
|
Chris@1511
|
27
|
Chris@1511
|
28 #include "view/View.h"
|
Chris@1511
|
29
|
Chris@1511
|
30 #include "data/model/TimeFrequencyBoxModel.h"
|
Chris@1511
|
31
|
Chris@1511
|
32 #include "widgets/ItemEditDialog.h"
|
Chris@1511
|
33 #include "widgets/TextAbbrev.h"
|
Chris@1511
|
34
|
Chris@1511
|
35 #include <QPainter>
|
Chris@1511
|
36 #include <QPainterPath>
|
Chris@1511
|
37 #include <QMouseEvent>
|
Chris@1511
|
38 #include <QTextStream>
|
Chris@1511
|
39 #include <QMessageBox>
|
Chris@1511
|
40
|
Chris@1511
|
41 #include <iostream>
|
Chris@1511
|
42 #include <cmath>
|
Chris@1511
|
43
|
Chris@1511
|
44 TimeFrequencyBoxLayer::TimeFrequencyBoxLayer() :
|
Chris@1511
|
45 SingleColourLayer(),
|
Chris@1511
|
46 m_editing(false),
|
Chris@1511
|
47 m_dragPointX(0),
|
Chris@1511
|
48 m_dragPointY(0),
|
Chris@1511
|
49 m_dragStartX(0),
|
Chris@1511
|
50 m_dragStartY(0),
|
Chris@1511
|
51 m_originalPoint(0, 0.0, 0, tr("New Box")),
|
Chris@1511
|
52 m_editingPoint(0, 0.0, 0, tr("New Box")),
|
Chris@1511
|
53 m_editingCommand(nullptr)
|
Chris@1511
|
54 {
|
Chris@1511
|
55
|
Chris@1511
|
56 }
|
Chris@1511
|
57
|
Chris@1511
|
58 int
|
Chris@1511
|
59 TimeFrequencyBoxLayer::getCompletion(LayerGeometryProvider *) const
|
Chris@1511
|
60 {
|
Chris@1511
|
61 auto model = ModelById::get(m_model);
|
Chris@1511
|
62 if (model) return model->getCompletion();
|
Chris@1511
|
63 else return 0;
|
Chris@1511
|
64 }
|
Chris@1511
|
65
|
Chris@1511
|
66 void
|
Chris@1511
|
67 TimeFrequencyBoxLayer::setModel(ModelId modelId)
|
Chris@1511
|
68 {
|
Chris@1511
|
69 auto oldModel = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
70 auto newModel = ModelById::getAs<TimeFrequencyBoxModel>(modelId);
|
Chris@1511
|
71
|
Chris@1511
|
72 if (!modelId.isNone() && !newModel) {
|
Chris@1511
|
73 throw std::logic_error("Not a TimeFrequencyBoxModel");
|
Chris@1511
|
74 }
|
Chris@1511
|
75
|
Chris@1511
|
76 if (m_model == modelId) return;
|
Chris@1511
|
77 m_model = modelId;
|
Chris@1511
|
78
|
Chris@1511
|
79 if (newModel) {
|
Chris@1511
|
80 connectSignals(m_model);
|
Chris@1511
|
81 }
|
Chris@1511
|
82
|
Chris@1511
|
83 emit modelReplaced();
|
Chris@1511
|
84 }
|
Chris@1511
|
85
|
Chris@1511
|
86 Layer::PropertyList
|
Chris@1511
|
87 TimeFrequencyBoxLayer::getProperties() const
|
Chris@1511
|
88 {
|
Chris@1511
|
89 PropertyList list = SingleColourLayer::getProperties();
|
Chris@1511
|
90 list.push_back("Vertical Scale");
|
Chris@1511
|
91 return list;
|
Chris@1511
|
92 }
|
Chris@1511
|
93
|
Chris@1511
|
94 QString
|
Chris@1511
|
95 TimeFrequencyBoxLayer::getPropertyLabel(const PropertyName &name) const
|
Chris@1511
|
96 {
|
Chris@1511
|
97 if (name == "Vertical Scale") return tr("Vertical Scale");
|
Chris@1511
|
98 return SingleColourLayer::getPropertyLabel(name);
|
Chris@1511
|
99 }
|
Chris@1511
|
100
|
Chris@1511
|
101 Layer::PropertyType
|
Chris@1511
|
102 TimeFrequencyBoxLayer::getPropertyType(const PropertyName &name) const
|
Chris@1511
|
103 {
|
Chris@1511
|
104 if (name == "Vertical Scale") return ValueProperty;
|
Chris@1511
|
105 return SingleColourLayer::getPropertyType(name);
|
Chris@1511
|
106 }
|
Chris@1511
|
107
|
Chris@1511
|
108 QString
|
Chris@1511
|
109 TimeFrequencyBoxLayer::getPropertyGroupName(const PropertyName &name) const
|
Chris@1511
|
110 {
|
Chris@1511
|
111 if (name == "Vertical Scale") {
|
Chris@1511
|
112 return tr("Scale");
|
Chris@1511
|
113 }
|
Chris@1511
|
114 return SingleColourLayer::getPropertyGroupName(name);
|
Chris@1511
|
115 }
|
Chris@1511
|
116
|
Chris@1511
|
117 int
|
Chris@1511
|
118 TimeFrequencyBoxLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@1511
|
119 int *min, int *max, int *deflt) const
|
Chris@1511
|
120 {
|
Chris@1511
|
121 int val = 0;
|
Chris@1511
|
122
|
Chris@1511
|
123 if (name == "Vertical Scale") {
|
Chris@1511
|
124
|
Chris@1511
|
125 if (min) *min = 0;
|
Chris@1511
|
126 if (max) *max = 2;
|
Chris@1511
|
127 if (deflt) *deflt = int(LinearScale);
|
Chris@1511
|
128
|
Chris@1511
|
129 val = int(m_verticalScale);
|
Chris@1511
|
130
|
Chris@1511
|
131 } else {
|
Chris@1511
|
132 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
|
Chris@1511
|
133 }
|
Chris@1511
|
134
|
Chris@1511
|
135 return val;
|
Chris@1511
|
136 }
|
Chris@1511
|
137
|
Chris@1511
|
138 QString
|
Chris@1511
|
139 TimeFrequencyBoxLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@1511
|
140 int value) const
|
Chris@1511
|
141 {
|
Chris@1511
|
142 if (name == "Vertical Scale") {
|
Chris@1511
|
143 switch (value) {
|
Chris@1511
|
144 default:
|
Chris@1511
|
145 case 0: return tr("Auto-Align");
|
Chris@1511
|
146 case 1: return tr("Linear");
|
Chris@1511
|
147 case 2: return tr("Log");
|
Chris@1511
|
148 }
|
Chris@1511
|
149 }
|
Chris@1511
|
150 return SingleColourLayer::getPropertyValueLabel(name, value);
|
Chris@1511
|
151 }
|
Chris@1511
|
152
|
Chris@1511
|
153 void
|
Chris@1511
|
154 TimeFrequencyBoxLayer::setProperty(const PropertyName &name, int value)
|
Chris@1511
|
155 {
|
Chris@1511
|
156 if (name == "Vertical Scale") {
|
Chris@1511
|
157 setVerticalScale(VerticalScale(value));
|
Chris@1511
|
158 } else {
|
Chris@1511
|
159 return SingleColourLayer::setProperty(name, value);
|
Chris@1511
|
160 }
|
Chris@1511
|
161 }
|
Chris@1511
|
162
|
Chris@1511
|
163 void
|
Chris@1511
|
164 TimeFrequencyBoxLayer::setVerticalScale(VerticalScale scale)
|
Chris@1511
|
165 {
|
Chris@1511
|
166 if (m_verticalScale == scale) return;
|
Chris@1511
|
167 m_verticalScale = scale;
|
Chris@1511
|
168 emit layerParametersChanged();
|
Chris@1511
|
169 }
|
Chris@1511
|
170
|
Chris@1511
|
171 bool
|
Chris@1511
|
172 TimeFrequencyBoxLayer::isLayerScrollable(const LayerGeometryProvider *v) const
|
Chris@1511
|
173 {
|
Chris@1511
|
174 QPoint discard;
|
Chris@1511
|
175 return !v->shouldIlluminateLocalFeatures(this, discard);
|
Chris@1511
|
176 }
|
Chris@1511
|
177
|
Chris@1511
|
178 bool
|
Chris@1511
|
179 TimeFrequencyBoxLayer::getValueExtents(double &min, double &max,
|
Chris@1511
|
180 bool &logarithmic, QString &unit) const
|
Chris@1511
|
181 {
|
Chris@1511
|
182 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
183 if (!model) return false;
|
Chris@1511
|
184 min = model->getFrequencyMinimum();
|
Chris@1511
|
185 max = model->getFrequencyMaximum();
|
Chris@1511
|
186 unit = getScaleUnits();
|
Chris@1511
|
187
|
Chris@1511
|
188 if (m_verticalScale == LogScale) logarithmic = true;
|
Chris@1511
|
189
|
Chris@1511
|
190 return true;
|
Chris@1511
|
191 }
|
Chris@1511
|
192
|
Chris@1511
|
193 bool
|
Chris@1511
|
194 TimeFrequencyBoxLayer::getDisplayExtents(double &min, double &max) const
|
Chris@1511
|
195 {
|
Chris@1511
|
196 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
197 if (!model || m_verticalScale == AutoAlignScale) return false;
|
Chris@1511
|
198
|
Chris@1511
|
199 min = model->getFrequencyMinimum();
|
Chris@1511
|
200 max = model->getFrequencyMaximum();
|
Chris@1511
|
201
|
Chris@1511
|
202 return true;
|
Chris@1511
|
203 }
|
Chris@1511
|
204
|
Chris@1511
|
205 EventVector
|
Chris@1511
|
206 TimeFrequencyBoxLayer::getLocalPoints(LayerGeometryProvider *v, int x) const
|
Chris@1511
|
207 {
|
Chris@1511
|
208 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
209 if (!model) return EventVector();
|
Chris@1511
|
210
|
Chris@1511
|
211 sv_frame_t frame = v->getFrameForX(x);
|
Chris@1511
|
212
|
Chris@1511
|
213 EventVector local = model->getEventsCovering(frame);
|
Chris@1511
|
214 if (!local.empty()) return local;
|
Chris@1511
|
215
|
Chris@1511
|
216 int fuzz = ViewManager::scalePixelSize(2);
|
Chris@1511
|
217 sv_frame_t start = v->getFrameForX(x - fuzz);
|
Chris@1511
|
218 sv_frame_t end = v->getFrameForX(x + fuzz);
|
Chris@1511
|
219
|
Chris@1511
|
220 local = model->getEventsStartingWithin(frame, end - frame);
|
Chris@1511
|
221 if (!local.empty()) return local;
|
Chris@1511
|
222
|
Chris@1511
|
223 local = model->getEventsSpanning(start, frame - start);
|
Chris@1511
|
224 if (!local.empty()) return local;
|
Chris@1511
|
225
|
Chris@1511
|
226 return {};
|
Chris@1511
|
227 }
|
Chris@1511
|
228
|
Chris@1511
|
229 bool
|
Chris@1511
|
230 TimeFrequencyBoxLayer::getPointToDrag(LayerGeometryProvider *v, int x, int y,
|
Chris@1511
|
231 Event &point) const
|
Chris@1511
|
232 {
|
Chris@1511
|
233 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
234 if (!model) return false;
|
Chris@1511
|
235
|
Chris@1511
|
236 sv_frame_t frame = v->getFrameForX(x);
|
Chris@1511
|
237
|
Chris@1511
|
238 EventVector onPoints = model->getEventsCovering(frame);
|
Chris@1511
|
239 if (onPoints.empty()) return false;
|
Chris@1511
|
240
|
Chris@1511
|
241 int nearestDistance = -1;
|
Chris@1511
|
242 for (const auto &p: onPoints) {
|
Chris@1511
|
243 int distance = getYForValue(v, p.getValue()) - y;
|
Chris@1511
|
244 if (distance < 0) distance = -distance;
|
Chris@1511
|
245 if (nearestDistance == -1 || distance < nearestDistance) {
|
Chris@1511
|
246 nearestDistance = distance;
|
Chris@1511
|
247 point = p;
|
Chris@1511
|
248 }
|
Chris@1511
|
249 }
|
Chris@1511
|
250
|
Chris@1511
|
251 return true;
|
Chris@1511
|
252 }
|
Chris@1511
|
253
|
Chris@1511
|
254 QString
|
Chris@1511
|
255 TimeFrequencyBoxLayer::getLabelPreceding(sv_frame_t frame) const
|
Chris@1511
|
256 {
|
Chris@1511
|
257 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
258 if (!model) return "";
|
Chris@1511
|
259 EventVector points = model->getEventsStartingWithin
|
Chris@1511
|
260 (model->getStartFrame(), frame - model->getStartFrame());
|
Chris@1511
|
261 if (!points.empty()) {
|
Chris@1511
|
262 for (auto i = points.rbegin(); i != points.rend(); ++i) {
|
Chris@1511
|
263 if (i->getLabel() != QString()) {
|
Chris@1511
|
264 return i->getLabel();
|
Chris@1511
|
265 }
|
Chris@1511
|
266 }
|
Chris@1511
|
267 }
|
Chris@1511
|
268 return QString();
|
Chris@1511
|
269 }
|
Chris@1511
|
270
|
Chris@1511
|
271 QString
|
Chris@1511
|
272 TimeFrequencyBoxLayer::getFeatureDescription(LayerGeometryProvider *v,
|
Chris@1511
|
273 QPoint &pos) const
|
Chris@1511
|
274 {
|
Chris@1511
|
275 int x = pos.x();
|
Chris@1511
|
276
|
Chris@1511
|
277 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
278 if (!model || !model->getSampleRate()) return "";
|
Chris@1511
|
279
|
Chris@1511
|
280 EventVector points = getLocalPoints(v, x);
|
Chris@1511
|
281
|
Chris@1511
|
282 if (points.empty()) {
|
Chris@1511
|
283 if (!model->isReady()) {
|
Chris@1511
|
284 return tr("In progress");
|
Chris@1511
|
285 } else {
|
Chris@1511
|
286 return tr("No local points");
|
Chris@1511
|
287 }
|
Chris@1511
|
288 }
|
Chris@1511
|
289
|
Chris@1511
|
290 Event box;
|
Chris@1511
|
291 EventVector::iterator i;
|
Chris@1511
|
292
|
Chris@1511
|
293 for (i = points.begin(); i != points.end(); ++i) {
|
Chris@1511
|
294
|
Chris@1511
|
295 int y0 = getYForValue(v, i->getValue());
|
Chris@1511
|
296 int y1 = getYForValue(v, i->getValue() + fabsf(i->getLevel()));
|
Chris@1511
|
297
|
Chris@1511
|
298 if (pos.y() >= y0 && pos.y() <= y1) {
|
Chris@1511
|
299 box = *i;
|
Chris@1511
|
300 break;
|
Chris@1511
|
301 }
|
Chris@1511
|
302 }
|
Chris@1511
|
303
|
Chris@1511
|
304 if (i == points.end()) return tr("No local points");
|
Chris@1511
|
305
|
Chris@1511
|
306 RealTime rt = RealTime::frame2RealTime(box.getFrame(),
|
Chris@1511
|
307 model->getSampleRate());
|
Chris@1511
|
308 RealTime rd = RealTime::frame2RealTime(box.getDuration(),
|
Chris@1511
|
309 model->getSampleRate());
|
Chris@1511
|
310
|
Chris@1511
|
311 QString rangeText;
|
Chris@1511
|
312
|
Chris@1511
|
313 rangeText = tr("%1 %2 - %3 %4")
|
Chris@1511
|
314 .arg(box.getValue()).arg(getScaleUnits())
|
Chris@1511
|
315 .arg(box.getValue() + fabsf(box.getLevel())).arg(getScaleUnits());
|
Chris@1511
|
316
|
Chris@1511
|
317 QString text;
|
Chris@1511
|
318
|
Chris@1511
|
319 if (box.getLabel() == "") {
|
Chris@1511
|
320 text = QString(tr("Time:\t%1\nDuration:\t%2\nFrequency:\t%3\nNo label"))
|
Chris@1511
|
321 .arg(rt.toText(true).c_str())
|
Chris@1511
|
322 .arg(rd.toText(true).c_str())
|
Chris@1511
|
323 .arg(rangeText);
|
Chris@1511
|
324 } else {
|
Chris@1511
|
325 text = QString(tr("Time:\t%1\nDuration:\t%2\nFrequency:\t%3\nLabel:\t%4"))
|
Chris@1511
|
326 .arg(rt.toText(true).c_str())
|
Chris@1511
|
327 .arg(rd.toText(true).c_str())
|
Chris@1511
|
328 .arg(rangeText)
|
Chris@1511
|
329 .arg(box.getLabel());
|
Chris@1511
|
330 }
|
Chris@1511
|
331
|
Chris@1511
|
332 pos = QPoint(v->getXForFrame(box.getFrame()),
|
Chris@1511
|
333 getYForValue(v, box.getValue()));
|
Chris@1511
|
334 return text;
|
Chris@1511
|
335 }
|
Chris@1511
|
336
|
Chris@1511
|
337 bool
|
Chris@1511
|
338 TimeFrequencyBoxLayer::snapToFeatureFrame(LayerGeometryProvider *v,
|
Chris@1511
|
339 sv_frame_t &frame,
|
Chris@1511
|
340 int &resolution,
|
Chris@1511
|
341 SnapType snap) const
|
Chris@1511
|
342 {
|
Chris@1511
|
343 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
344 if (!model) {
|
Chris@1511
|
345 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
|
Chris@1511
|
346 }
|
Chris@1511
|
347
|
Chris@1511
|
348 // SnapLeft / SnapRight: return frame of nearest feature in that
|
Chris@1511
|
349 // direction no matter how far away
|
Chris@1511
|
350 //
|
Chris@1511
|
351 // SnapNeighbouring: return frame of feature that would be used in
|
Chris@1511
|
352 // an editing operation, i.e. closest feature in either direction
|
Chris@1511
|
353 // but only if it is "close enough"
|
Chris@1511
|
354
|
Chris@1511
|
355 resolution = model->getResolution();
|
Chris@1511
|
356
|
Chris@1511
|
357 if (snap == SnapNeighbouring) {
|
Chris@1511
|
358 EventVector points = getLocalPoints(v, v->getXForFrame(frame));
|
Chris@1511
|
359 if (points.empty()) return false;
|
Chris@1511
|
360 frame = points.begin()->getFrame();
|
Chris@1511
|
361 return true;
|
Chris@1511
|
362 }
|
Chris@1511
|
363
|
Chris@1511
|
364 // Normally we snap to the start frame of whichever event we
|
Chris@1511
|
365 // find. However here, for SnapRight only, if the end frame of
|
Chris@1511
|
366 // whichever event we would have snapped to had we been snapping
|
Chris@1511
|
367 // left is closer than the start frame of the next event to the
|
Chris@1511
|
368 // right, then we snap to that frame instead. Clear?
|
Chris@1511
|
369
|
Chris@1511
|
370 Event left;
|
Chris@1511
|
371 bool haveLeft = false;
|
Chris@1511
|
372 if (model->getNearestEventMatching
|
Chris@1511
|
373 (frame, [](Event) { return true; }, EventSeries::Backward, left)) {
|
Chris@1511
|
374 haveLeft = true;
|
Chris@1511
|
375 }
|
Chris@1511
|
376
|
Chris@1511
|
377 if (snap == SnapLeft) {
|
Chris@1511
|
378 frame = left.getFrame();
|
Chris@1511
|
379 return haveLeft;
|
Chris@1511
|
380 }
|
Chris@1511
|
381
|
Chris@1511
|
382 Event right;
|
Chris@1511
|
383 bool haveRight = false;
|
Chris@1511
|
384 if (model->getNearestEventMatching
|
Chris@1511
|
385 (frame, [](Event) { return true; }, EventSeries::Forward, right)) {
|
Chris@1511
|
386 haveRight = true;
|
Chris@1511
|
387 }
|
Chris@1511
|
388
|
Chris@1511
|
389 if (haveLeft) {
|
Chris@1511
|
390 sv_frame_t leftEnd = left.getFrame() + left.getDuration();
|
Chris@1511
|
391 if (leftEnd > frame) {
|
Chris@1511
|
392 if (haveRight) {
|
Chris@1511
|
393 if (leftEnd - frame < right.getFrame() - frame) {
|
Chris@1511
|
394 frame = leftEnd;
|
Chris@1511
|
395 } else {
|
Chris@1511
|
396 frame = right.getFrame();
|
Chris@1511
|
397 }
|
Chris@1511
|
398 } else {
|
Chris@1511
|
399 frame = leftEnd;
|
Chris@1511
|
400 }
|
Chris@1511
|
401 return true;
|
Chris@1511
|
402 }
|
Chris@1511
|
403 }
|
Chris@1511
|
404
|
Chris@1511
|
405 if (haveRight) {
|
Chris@1511
|
406 frame = right.getFrame();
|
Chris@1511
|
407 return true;
|
Chris@1511
|
408 }
|
Chris@1511
|
409
|
Chris@1511
|
410 return false;
|
Chris@1511
|
411 }
|
Chris@1511
|
412
|
Chris@1511
|
413 QString
|
Chris@1511
|
414 TimeFrequencyBoxLayer::getScaleUnits() const
|
Chris@1511
|
415 {
|
Chris@1511
|
416 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
417 if (model) return model->getScaleUnits();
|
Chris@1511
|
418 else return "";
|
Chris@1511
|
419 }
|
Chris@1511
|
420
|
Chris@1511
|
421 void
|
Chris@1511
|
422 TimeFrequencyBoxLayer::getScaleExtents(LayerGeometryProvider *v,
|
Chris@1511
|
423 double &min, double &max,
|
Chris@1511
|
424 bool &log) const
|
Chris@1511
|
425 {
|
Chris@1511
|
426 min = 0.0;
|
Chris@1511
|
427 max = 0.0;
|
Chris@1511
|
428 log = false;
|
Chris@1511
|
429
|
Chris@1511
|
430 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
431 if (!model) return;
|
Chris@1511
|
432
|
Chris@1511
|
433 QString queryUnits;
|
Chris@1511
|
434 queryUnits = getScaleUnits();
|
Chris@1511
|
435
|
Chris@1511
|
436 if (m_verticalScale == AutoAlignScale) {
|
Chris@1511
|
437
|
Chris@1511
|
438 if (!v->getValueExtents(queryUnits, min, max, log)) {
|
Chris@1511
|
439
|
Chris@1511
|
440 min = model->getFrequencyMinimum();
|
Chris@1511
|
441 max = model->getFrequencyMaximum();
|
Chris@1511
|
442
|
Chris@1511
|
443 // cerr << "TimeFrequencyBoxLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl;
|
Chris@1511
|
444
|
Chris@1511
|
445 } else if (log) {
|
Chris@1511
|
446
|
Chris@1511
|
447 LogRange::mapRange(min, max);
|
Chris@1511
|
448
|
Chris@1511
|
449 // cerr << "TimeFrequencyBoxLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl;
|
Chris@1511
|
450
|
Chris@1511
|
451 }
|
Chris@1511
|
452
|
Chris@1511
|
453 } else {
|
Chris@1511
|
454
|
Chris@1511
|
455 min = model->getFrequencyMinimum();
|
Chris@1511
|
456 max = model->getFrequencyMaximum();
|
Chris@1511
|
457
|
Chris@1511
|
458 if (m_verticalScale == LogScale) {
|
Chris@1511
|
459 LogRange::mapRange(min, max);
|
Chris@1511
|
460 log = true;
|
Chris@1511
|
461 }
|
Chris@1511
|
462 }
|
Chris@1511
|
463
|
Chris@1511
|
464 if (max == min) max = min + 1.0;
|
Chris@1511
|
465 }
|
Chris@1511
|
466
|
Chris@1511
|
467 int
|
Chris@1511
|
468 TimeFrequencyBoxLayer::getYForValue(LayerGeometryProvider *v, double val) const
|
Chris@1511
|
469 {
|
Chris@1511
|
470 double min = 0.0, max = 0.0;
|
Chris@1511
|
471 bool logarithmic = false;
|
Chris@1511
|
472 int h = v->getPaintHeight();
|
Chris@1511
|
473
|
Chris@1511
|
474 getScaleExtents(v, min, max, logarithmic);
|
Chris@1511
|
475
|
Chris@1511
|
476 // cerr << "TimeFrequencyBoxLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << endl;
|
Chris@1511
|
477 // cerr << "h = " << h << ", margin = " << margin << endl;
|
Chris@1511
|
478
|
Chris@1511
|
479 if (logarithmic) {
|
Chris@1511
|
480 val = LogRange::map(val);
|
Chris@1511
|
481 }
|
Chris@1511
|
482
|
Chris@1511
|
483 return int(h - ((val - min) * h) / (max - min));
|
Chris@1511
|
484 }
|
Chris@1511
|
485
|
Chris@1511
|
486 double
|
Chris@1511
|
487 TimeFrequencyBoxLayer::getValueForY(LayerGeometryProvider *v, int y) const
|
Chris@1511
|
488 {
|
Chris@1511
|
489 double min = 0.0, max = 0.0;
|
Chris@1511
|
490 bool logarithmic = false;
|
Chris@1511
|
491 int h = v->getPaintHeight();
|
Chris@1511
|
492
|
Chris@1511
|
493 getScaleExtents(v, min, max, logarithmic);
|
Chris@1511
|
494
|
Chris@1511
|
495 double val = min + (double(h - y) * double(max - min)) / h;
|
Chris@1511
|
496
|
Chris@1511
|
497 if (logarithmic) {
|
Chris@1511
|
498 val = pow(10.0, val);
|
Chris@1511
|
499 }
|
Chris@1511
|
500
|
Chris@1511
|
501 return val;
|
Chris@1511
|
502 }
|
Chris@1511
|
503
|
Chris@1511
|
504 void
|
Chris@1511
|
505 TimeFrequencyBoxLayer::paint(LayerGeometryProvider *v, QPainter &paint,
|
Chris@1511
|
506 QRect rect) const
|
Chris@1511
|
507 {
|
Chris@1511
|
508 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
509 if (!model || !model->isOK()) return;
|
Chris@1511
|
510
|
Chris@1511
|
511 sv_samplerate_t sampleRate = model->getSampleRate();
|
Chris@1511
|
512 if (!sampleRate) return;
|
Chris@1511
|
513
|
Chris@1511
|
514 // Profiler profiler("TimeFrequencyBoxLayer::paint", true);
|
Chris@1511
|
515
|
Chris@1511
|
516 int x0 = rect.left() - 40, x1 = rect.right();
|
Chris@1511
|
517
|
Chris@1511
|
518 sv_frame_t wholeFrame0 = v->getFrameForX(0);
|
Chris@1511
|
519 sv_frame_t wholeFrame1 = v->getFrameForX(v->getPaintWidth());
|
Chris@1511
|
520
|
Chris@1511
|
521 EventVector points(model->getEventsSpanning(wholeFrame0,
|
Chris@1511
|
522 wholeFrame1 - wholeFrame0));
|
Chris@1511
|
523 if (points.empty()) return;
|
Chris@1511
|
524
|
Chris@1511
|
525 paint.setPen(getBaseQColor());
|
Chris@1511
|
526
|
Chris@1511
|
527 QColor brushColour(getBaseQColor());
|
Chris@1511
|
528 brushColour.setAlpha(80);
|
Chris@1511
|
529
|
Chris@1511
|
530 // SVDEBUG << "TimeFrequencyBoxLayer::paint: resolution is "
|
Chris@1511
|
531 // << model->getResolution() << " frames" << endl;
|
Chris@1511
|
532
|
Chris@1511
|
533 double min = model->getFrequencyMinimum();
|
Chris@1511
|
534 double max = model->getFrequencyMaximum();
|
Chris@1511
|
535 if (max == min) max = min + 1.0;
|
Chris@1511
|
536
|
Chris@1511
|
537 QPoint localPos;
|
Chris@1511
|
538 Event illuminatePoint(0);
|
Chris@1511
|
539 bool shouldIlluminate = false;
|
Chris@1511
|
540
|
Chris@1511
|
541 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
|
Chris@1511
|
542 shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(),
|
Chris@1511
|
543 illuminatePoint);
|
Chris@1511
|
544 }
|
Chris@1511
|
545
|
Chris@1511
|
546 paint.save();
|
Chris@1511
|
547 paint.setRenderHint(QPainter::Antialiasing, false);
|
Chris@1511
|
548
|
Chris@1511
|
549 for (EventVector::const_iterator i = points.begin();
|
Chris@1511
|
550 i != points.end(); ++i) {
|
Chris@1511
|
551
|
Chris@1511
|
552 const Event &p(*i);
|
Chris@1511
|
553
|
Chris@1511
|
554 int x = v->getXForFrame(p.getFrame());
|
Chris@1511
|
555 int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x;
|
Chris@1511
|
556 int y = getYForValue(v, p.getValue());
|
Chris@1511
|
557 int h = getYForValue(v, p.getValue() + fabsf(p.getLevel()));
|
Chris@1511
|
558 int ex = x + w;
|
Chris@1511
|
559
|
Chris@1511
|
560 int gap = v->scalePixelSize(2);
|
Chris@1511
|
561
|
Chris@1511
|
562 EventVector::const_iterator j = i;
|
Chris@1511
|
563 ++j;
|
Chris@1511
|
564
|
Chris@1511
|
565 if (j != points.end()) {
|
Chris@1511
|
566 const Event &q(*j);
|
Chris@1511
|
567 int nx = v->getXForFrame(q.getFrame());
|
Chris@1511
|
568 if (nx < ex) ex = nx;
|
Chris@1511
|
569 }
|
Chris@1511
|
570
|
Chris@1511
|
571 if (w < 1) w = 1;
|
Chris@1511
|
572 if (h < 1) h = 1;
|
Chris@1511
|
573
|
Chris@1511
|
574 paint.setPen(getBaseQColor());
|
Chris@1511
|
575 paint.setBrush(brushColour);
|
Chris@1511
|
576
|
Chris@1511
|
577 if (shouldIlluminate && illuminatePoint == p) {
|
Chris@1511
|
578
|
Chris@1511
|
579 paint.setPen(v->getForeground());
|
Chris@1511
|
580 paint.setBrush(v->getForeground());
|
Chris@1511
|
581
|
Chris@1511
|
582 // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
|
Chris@1511
|
583 // replacement (horizontalAdvance) was only added in Qt 5.11
|
Chris@1511
|
584 // which is too new for us
|
Chris@1511
|
585 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
Chris@1511
|
586
|
Chris@1511
|
587 QString vlabel =
|
Chris@1511
|
588 QString("%1%2 - %3%4")
|
Chris@1511
|
589 .arg(p.getValue()).arg(getScaleUnits())
|
Chris@1511
|
590 .arg(p.getValue() + fabsf(p.getLevel())).arg(getScaleUnits());
|
Chris@1511
|
591 PaintAssistant::drawVisibleText
|
Chris@1511
|
592 (v, paint,
|
Chris@1511
|
593 x - paint.fontMetrics().width(vlabel) - gap,
|
Chris@1511
|
594 y + paint.fontMetrics().height()/2
|
Chris@1511
|
595 - paint.fontMetrics().descent(),
|
Chris@1511
|
596 vlabel, PaintAssistant::OutlinedText);
|
Chris@1511
|
597
|
Chris@1511
|
598 QString hlabel = RealTime::frame2RealTime
|
Chris@1511
|
599 (p.getFrame(), model->getSampleRate()).toText(true).c_str();
|
Chris@1511
|
600 PaintAssistant::drawVisibleText
|
Chris@1511
|
601 (v, paint,
|
Chris@1511
|
602 x,
|
Chris@1511
|
603 y - h/2 - paint.fontMetrics().descent() - gap,
|
Chris@1511
|
604 hlabel, PaintAssistant::OutlinedText);
|
Chris@1511
|
605 }
|
Chris@1511
|
606
|
Chris@1511
|
607 paint.drawRect(x, y, w, h);
|
Chris@1511
|
608 }
|
Chris@1511
|
609
|
Chris@1511
|
610 for (EventVector::const_iterator i = points.begin();
|
Chris@1511
|
611 i != points.end(); ++i) {
|
Chris@1511
|
612
|
Chris@1511
|
613 const Event &p(*i);
|
Chris@1511
|
614
|
Chris@1511
|
615 int x = v->getXForFrame(p.getFrame());
|
Chris@1511
|
616 int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x;
|
Chris@1511
|
617 int y = getYForValue(v, p.getValue());
|
Chris@1511
|
618
|
Chris@1511
|
619 QString label = p.getLabel();
|
Chris@1511
|
620 if (label == "") {
|
Chris@1511
|
621 label =
|
Chris@1511
|
622 QString("%1%2 - %3%4")
|
Chris@1511
|
623 .arg(p.getValue()).arg(getScaleUnits())
|
Chris@1511
|
624 .arg(p.getValue() + fabsf(p.getLevel())).arg(getScaleUnits());
|
Chris@1511
|
625 }
|
Chris@1511
|
626 int labelWidth = paint.fontMetrics().width(label);
|
Chris@1511
|
627
|
Chris@1511
|
628 int gap = v->scalePixelSize(2);
|
Chris@1511
|
629
|
Chris@1511
|
630 if (x + w < x0 || x - labelWidth - gap > x1) {
|
Chris@1511
|
631 continue;
|
Chris@1511
|
632 }
|
Chris@1511
|
633
|
Chris@1511
|
634 bool illuminated = false;
|
Chris@1511
|
635
|
Chris@1511
|
636 if (shouldIlluminate && illuminatePoint == p) {
|
Chris@1511
|
637 illuminated = true;
|
Chris@1511
|
638 }
|
Chris@1511
|
639
|
Chris@1511
|
640 if (!illuminated) {
|
Chris@1511
|
641
|
Chris@1511
|
642 int labelX, labelY;
|
Chris@1511
|
643
|
Chris@1511
|
644 labelX = x - labelWidth - gap;
|
Chris@1511
|
645 labelY = y + paint.fontMetrics().height()/2
|
Chris@1511
|
646 - paint.fontMetrics().descent();
|
Chris@1511
|
647
|
Chris@1511
|
648 PaintAssistant::drawVisibleText(v, paint, labelX, labelY, label,
|
Chris@1511
|
649 PaintAssistant::OutlinedText);
|
Chris@1511
|
650 }
|
Chris@1511
|
651 }
|
Chris@1511
|
652
|
Chris@1511
|
653 paint.restore();
|
Chris@1511
|
654 }
|
Chris@1511
|
655
|
Chris@1511
|
656 int
|
Chris@1511
|
657 TimeFrequencyBoxLayer::getVerticalScaleWidth(LayerGeometryProvider *v,
|
Chris@1511
|
658 bool, QPainter &paint) const
|
Chris@1511
|
659 {
|
Chris@1511
|
660 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
661 if (!model || m_verticalScale == AutoAlignScale) {
|
Chris@1511
|
662 return 0;
|
Chris@1511
|
663 } else {
|
Chris@1511
|
664 if (m_verticalScale == LogScale) {
|
Chris@1511
|
665 return LogNumericalScale().getWidth(v, paint);
|
Chris@1511
|
666 } else {
|
Chris@1511
|
667 return LinearNumericalScale().getWidth(v, paint);
|
Chris@1511
|
668 }
|
Chris@1511
|
669 }
|
Chris@1511
|
670 }
|
Chris@1511
|
671
|
Chris@1511
|
672 void
|
Chris@1511
|
673 TimeFrequencyBoxLayer::paintVerticalScale(LayerGeometryProvider *v,
|
Chris@1511
|
674 bool, QPainter &paint, QRect) const
|
Chris@1511
|
675 {
|
Chris@1511
|
676 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
677 if (!model || model->isEmpty()) return;
|
Chris@1511
|
678
|
Chris@1511
|
679 QString unit;
|
Chris@1511
|
680 double min, max;
|
Chris@1511
|
681 bool logarithmic;
|
Chris@1511
|
682
|
Chris@1511
|
683 int w = getVerticalScaleWidth(v, false, paint);
|
Chris@1511
|
684
|
Chris@1511
|
685 getScaleExtents(v, min, max, logarithmic);
|
Chris@1511
|
686
|
Chris@1511
|
687 if (logarithmic) {
|
Chris@1511
|
688 LogNumericalScale().paintVertical(v, this, paint, 0, min, max);
|
Chris@1511
|
689 } else {
|
Chris@1511
|
690 LinearNumericalScale().paintVertical(v, this, paint, 0, min, max);
|
Chris@1511
|
691 }
|
Chris@1511
|
692
|
Chris@1511
|
693 if (getScaleUnits() != "") {
|
Chris@1511
|
694 int mw = w - 5;
|
Chris@1511
|
695 paint.drawText(5,
|
Chris@1511
|
696 5 + paint.fontMetrics().ascent(),
|
Chris@1511
|
697 TextAbbrev::abbreviate(getScaleUnits(),
|
Chris@1511
|
698 paint.fontMetrics(),
|
Chris@1511
|
699 mw));
|
Chris@1511
|
700 }
|
Chris@1511
|
701 }
|
Chris@1511
|
702
|
Chris@1511
|
703 //!!! All of the editing operations still need to be updated for
|
Chris@1511
|
704 //!!! vertical frequency range instead of just value
|
Chris@1511
|
705
|
Chris@1511
|
706 void
|
Chris@1511
|
707 TimeFrequencyBoxLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
708 {
|
Chris@1511
|
709 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
710 if (!model) return;
|
Chris@1511
|
711
|
Chris@1511
|
712 sv_frame_t frame = v->getFrameForX(e->x());
|
Chris@1511
|
713 if (frame < 0) frame = 0;
|
Chris@1511
|
714 frame = frame / model->getResolution() * model->getResolution();
|
Chris@1511
|
715
|
Chris@1511
|
716 double value = getValueForY(v, e->y());
|
Chris@1511
|
717
|
Chris@1511
|
718 m_editingPoint = Event(frame, float(value), 0, "");
|
Chris@1511
|
719 m_originalPoint = m_editingPoint;
|
Chris@1511
|
720
|
Chris@1511
|
721 if (m_editingCommand) finish(m_editingCommand);
|
Chris@1511
|
722 m_editingCommand = new ChangeEventsCommand(m_model.untyped,
|
Chris@1511
|
723 tr("Draw Time-Frequency Box"));
|
Chris@1511
|
724 m_editingCommand->add(m_editingPoint);
|
Chris@1511
|
725
|
Chris@1511
|
726 m_editing = true;
|
Chris@1511
|
727 }
|
Chris@1511
|
728
|
Chris@1511
|
729 void
|
Chris@1511
|
730 TimeFrequencyBoxLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
731 {
|
Chris@1511
|
732 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
733 if (!model || !m_editing) return;
|
Chris@1511
|
734
|
Chris@1511
|
735 sv_frame_t frame = v->getFrameForX(e->x());
|
Chris@1511
|
736 if (frame < 0) frame = 0;
|
Chris@1511
|
737 frame = frame / model->getResolution() * model->getResolution();
|
Chris@1511
|
738
|
Chris@1511
|
739 double newValue = getValueForY(v, e->y());
|
Chris@1511
|
740
|
Chris@1511
|
741 sv_frame_t newFrame = m_editingPoint.getFrame();
|
Chris@1511
|
742 sv_frame_t newDuration = frame - newFrame;
|
Chris@1511
|
743 if (newDuration < 0) {
|
Chris@1511
|
744 newFrame = frame;
|
Chris@1511
|
745 newDuration = -newDuration;
|
Chris@1511
|
746 } else if (newDuration == 0) {
|
Chris@1511
|
747 newDuration = 1;
|
Chris@1511
|
748 }
|
Chris@1511
|
749
|
Chris@1511
|
750 m_editingCommand->remove(m_editingPoint);
|
Chris@1511
|
751 m_editingPoint = m_editingPoint
|
Chris@1511
|
752 .withFrame(newFrame)
|
Chris@1511
|
753 .withValue(float(newValue))
|
Chris@1511
|
754 .withDuration(newDuration);
|
Chris@1511
|
755 m_editingCommand->add(m_editingPoint);
|
Chris@1511
|
756 }
|
Chris@1511
|
757
|
Chris@1511
|
758 void
|
Chris@1511
|
759 TimeFrequencyBoxLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *)
|
Chris@1511
|
760 {
|
Chris@1511
|
761 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
762 if (!model || !m_editing) return;
|
Chris@1511
|
763 finish(m_editingCommand);
|
Chris@1511
|
764 m_editingCommand = nullptr;
|
Chris@1511
|
765 m_editing = false;
|
Chris@1511
|
766 }
|
Chris@1511
|
767
|
Chris@1511
|
768 void
|
Chris@1511
|
769 TimeFrequencyBoxLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
770 {
|
Chris@1511
|
771 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
772 if (!model) return;
|
Chris@1511
|
773
|
Chris@1511
|
774 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
|
Chris@1511
|
775
|
Chris@1511
|
776 if (m_editingCommand) {
|
Chris@1511
|
777 finish(m_editingCommand);
|
Chris@1511
|
778 m_editingCommand = nullptr;
|
Chris@1511
|
779 }
|
Chris@1511
|
780
|
Chris@1511
|
781 m_editing = true;
|
Chris@1511
|
782 }
|
Chris@1511
|
783
|
Chris@1511
|
784 void
|
Chris@1511
|
785 TimeFrequencyBoxLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *)
|
Chris@1511
|
786 {
|
Chris@1511
|
787 }
|
Chris@1511
|
788
|
Chris@1511
|
789 void
|
Chris@1511
|
790 TimeFrequencyBoxLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
791 {
|
Chris@1511
|
792 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
793 if (!model || !m_editing) return;
|
Chris@1511
|
794
|
Chris@1511
|
795 m_editing = false;
|
Chris@1511
|
796
|
Chris@1511
|
797 Event p(0);
|
Chris@1511
|
798 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
|
Chris@1511
|
799 if (p.getFrame() != m_editingPoint.getFrame() ||
|
Chris@1511
|
800 p.getValue() != m_editingPoint.getValue()) return;
|
Chris@1511
|
801
|
Chris@1511
|
802 m_editingCommand = new ChangeEventsCommand
|
Chris@1511
|
803 (m_model.untyped, tr("Erase Time-Frequency Box"));
|
Chris@1511
|
804
|
Chris@1511
|
805 m_editingCommand->remove(m_editingPoint);
|
Chris@1511
|
806
|
Chris@1511
|
807 finish(m_editingCommand);
|
Chris@1511
|
808 m_editingCommand = nullptr;
|
Chris@1511
|
809 m_editing = false;
|
Chris@1511
|
810 }
|
Chris@1511
|
811
|
Chris@1511
|
812 void
|
Chris@1511
|
813 TimeFrequencyBoxLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
814 {
|
Chris@1511
|
815 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
816 if (!model) return;
|
Chris@1511
|
817
|
Chris@1511
|
818 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) {
|
Chris@1511
|
819 return;
|
Chris@1511
|
820 }
|
Chris@1511
|
821
|
Chris@1511
|
822 m_dragPointX = v->getXForFrame(m_editingPoint.getFrame());
|
Chris@1511
|
823 m_dragPointY = getYForValue(v, m_editingPoint.getValue());
|
Chris@1511
|
824
|
Chris@1511
|
825 m_originalPoint = m_editingPoint;
|
Chris@1511
|
826
|
Chris@1511
|
827 if (m_editingCommand) {
|
Chris@1511
|
828 finish(m_editingCommand);
|
Chris@1511
|
829 m_editingCommand = nullptr;
|
Chris@1511
|
830 }
|
Chris@1511
|
831
|
Chris@1511
|
832 m_editing = true;
|
Chris@1511
|
833 m_dragStartX = e->x();
|
Chris@1511
|
834 m_dragStartY = e->y();
|
Chris@1511
|
835 }
|
Chris@1511
|
836
|
Chris@1511
|
837 void
|
Chris@1511
|
838 TimeFrequencyBoxLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
839 {
|
Chris@1511
|
840 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
841 if (!model || !m_editing) return;
|
Chris@1511
|
842
|
Chris@1511
|
843 int xdist = e->x() - m_dragStartX;
|
Chris@1511
|
844 int ydist = e->y() - m_dragStartY;
|
Chris@1511
|
845 int newx = m_dragPointX + xdist;
|
Chris@1511
|
846 int newy = m_dragPointY + ydist;
|
Chris@1511
|
847
|
Chris@1511
|
848 sv_frame_t frame = v->getFrameForX(newx);
|
Chris@1511
|
849 if (frame < 0) frame = 0;
|
Chris@1511
|
850 frame = frame / model->getResolution() * model->getResolution();
|
Chris@1511
|
851
|
Chris@1511
|
852 double value = getValueForY(v, newy);
|
Chris@1511
|
853
|
Chris@1511
|
854 if (!m_editingCommand) {
|
Chris@1511
|
855 m_editingCommand = new ChangeEventsCommand
|
Chris@1511
|
856 (m_model.untyped,
|
Chris@1511
|
857 tr("Drag Time-Frequency Box"));
|
Chris@1511
|
858 }
|
Chris@1511
|
859
|
Chris@1511
|
860 m_editingCommand->remove(m_editingPoint);
|
Chris@1511
|
861 m_editingPoint = m_editingPoint
|
Chris@1511
|
862 .withFrame(frame)
|
Chris@1511
|
863 .withValue(float(value));
|
Chris@1511
|
864 m_editingCommand->add(m_editingPoint);
|
Chris@1511
|
865 }
|
Chris@1511
|
866
|
Chris@1511
|
867 void
|
Chris@1511
|
868 TimeFrequencyBoxLayer::editEnd(LayerGeometryProvider *, QMouseEvent *)
|
Chris@1511
|
869 {
|
Chris@1511
|
870 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
871 if (!model || !m_editing) return;
|
Chris@1511
|
872
|
Chris@1511
|
873 if (m_editingCommand) {
|
Chris@1511
|
874
|
Chris@1511
|
875 QString newName = m_editingCommand->getName();
|
Chris@1511
|
876
|
Chris@1511
|
877 if (m_editingPoint.getFrame() != m_originalPoint.getFrame()) {
|
Chris@1511
|
878 if (m_editingPoint.getValue() != m_originalPoint.getValue()) {
|
Chris@1511
|
879 newName = tr("Edit Time-Frequency Box");
|
Chris@1511
|
880 } else {
|
Chris@1511
|
881 newName = tr("Relocate Time-Frequency Box");
|
Chris@1511
|
882 }
|
Chris@1511
|
883 } else {
|
Chris@1511
|
884 newName = tr("Change Point Value");
|
Chris@1511
|
885 }
|
Chris@1511
|
886
|
Chris@1511
|
887 m_editingCommand->setName(newName);
|
Chris@1511
|
888 finish(m_editingCommand);
|
Chris@1511
|
889 }
|
Chris@1511
|
890
|
Chris@1511
|
891 m_editingCommand = nullptr;
|
Chris@1511
|
892 m_editing = false;
|
Chris@1511
|
893 }
|
Chris@1511
|
894
|
Chris@1511
|
895 bool
|
Chris@1511
|
896 TimeFrequencyBoxLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
897 {
|
Chris@1511
|
898 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
899 if (!model) return false;
|
Chris@1511
|
900
|
Chris@1511
|
901 Event region(0);
|
Chris@1511
|
902 if (!getPointToDrag(v, e->x(), e->y(), region)) return false;
|
Chris@1511
|
903
|
Chris@1511
|
904 ItemEditDialog *dialog = new ItemEditDialog
|
Chris@1511
|
905 (model->getSampleRate(),
|
Chris@1511
|
906 ItemEditDialog::ShowTime |
|
Chris@1511
|
907 ItemEditDialog::ShowDuration |
|
Chris@1511
|
908 ItemEditDialog::ShowValue |
|
Chris@1511
|
909 ItemEditDialog::ShowText,
|
Chris@1511
|
910 getScaleUnits());
|
Chris@1511
|
911
|
Chris@1511
|
912 dialog->setFrameTime(region.getFrame());
|
Chris@1511
|
913 dialog->setValue(region.getValue());
|
Chris@1511
|
914 dialog->setFrameDuration(region.getDuration());
|
Chris@1511
|
915 dialog->setText(region.getLabel());
|
Chris@1511
|
916
|
Chris@1511
|
917 if (dialog->exec() == QDialog::Accepted) {
|
Chris@1511
|
918
|
Chris@1511
|
919 Event newTimeFrequencyBox = region
|
Chris@1511
|
920 .withFrame(dialog->getFrameTime())
|
Chris@1511
|
921 .withValue(dialog->getValue())
|
Chris@1511
|
922 .withDuration(dialog->getFrameDuration())
|
Chris@1511
|
923 .withLabel(dialog->getText());
|
Chris@1511
|
924
|
Chris@1511
|
925 ChangeEventsCommand *command = new ChangeEventsCommand
|
Chris@1511
|
926 (m_model.untyped, tr("Edit Time-Frequency Box"));
|
Chris@1511
|
927 command->remove(region);
|
Chris@1511
|
928 command->add(newTimeFrequencyBox);
|
Chris@1511
|
929 finish(command);
|
Chris@1511
|
930 }
|
Chris@1511
|
931
|
Chris@1511
|
932 delete dialog;
|
Chris@1511
|
933 return true;
|
Chris@1511
|
934 }
|
Chris@1511
|
935
|
Chris@1511
|
936 void
|
Chris@1511
|
937 TimeFrequencyBoxLayer::moveSelection(Selection s, sv_frame_t newStartFrame)
|
Chris@1511
|
938 {
|
Chris@1511
|
939 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
940 if (!model) return;
|
Chris@1511
|
941
|
Chris@1511
|
942 ChangeEventsCommand *command =
|
Chris@1511
|
943 new ChangeEventsCommand(m_model.untyped, tr("Drag Selection"));
|
Chris@1511
|
944
|
Chris@1511
|
945 EventVector points =
|
Chris@1511
|
946 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
|
Chris@1511
|
947
|
Chris@1511
|
948 for (EventVector::iterator i = points.begin();
|
Chris@1511
|
949 i != points.end(); ++i) {
|
Chris@1511
|
950
|
Chris@1511
|
951 Event newPoint = (*i)
|
Chris@1511
|
952 .withFrame(i->getFrame() + newStartFrame - s.getStartFrame());
|
Chris@1511
|
953 command->remove(*i);
|
Chris@1511
|
954 command->add(newPoint);
|
Chris@1511
|
955 }
|
Chris@1511
|
956
|
Chris@1511
|
957 finish(command);
|
Chris@1511
|
958 }
|
Chris@1511
|
959
|
Chris@1511
|
960 void
|
Chris@1511
|
961 TimeFrequencyBoxLayer::resizeSelection(Selection s, Selection newSize)
|
Chris@1511
|
962 {
|
Chris@1511
|
963 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
964 if (!model || !s.getDuration()) return;
|
Chris@1511
|
965
|
Chris@1511
|
966 ChangeEventsCommand *command =
|
Chris@1511
|
967 new ChangeEventsCommand(m_model.untyped, tr("Resize Selection"));
|
Chris@1511
|
968
|
Chris@1511
|
969 EventVector points =
|
Chris@1511
|
970 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
|
Chris@1511
|
971
|
Chris@1511
|
972 double ratio = double(newSize.getDuration()) / double(s.getDuration());
|
Chris@1511
|
973 double oldStart = double(s.getStartFrame());
|
Chris@1511
|
974 double newStart = double(newSize.getStartFrame());
|
Chris@1511
|
975
|
Chris@1511
|
976 for (Event p: points) {
|
Chris@1511
|
977
|
Chris@1511
|
978 double newFrame = (double(p.getFrame()) - oldStart) * ratio + newStart;
|
Chris@1511
|
979 double newDuration = double(p.getDuration()) * ratio;
|
Chris@1511
|
980
|
Chris@1511
|
981 Event newPoint = p
|
Chris@1511
|
982 .withFrame(lrint(newFrame))
|
Chris@1511
|
983 .withDuration(lrint(newDuration));
|
Chris@1511
|
984 command->remove(p);
|
Chris@1511
|
985 command->add(newPoint);
|
Chris@1511
|
986 }
|
Chris@1511
|
987
|
Chris@1511
|
988 finish(command);
|
Chris@1511
|
989 }
|
Chris@1511
|
990
|
Chris@1511
|
991 void
|
Chris@1511
|
992 TimeFrequencyBoxLayer::deleteSelection(Selection s)
|
Chris@1511
|
993 {
|
Chris@1511
|
994 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
995 if (!model) return;
|
Chris@1511
|
996
|
Chris@1511
|
997 ChangeEventsCommand *command =
|
Chris@1511
|
998 new ChangeEventsCommand(m_model.untyped, tr("Delete Selected Points"));
|
Chris@1511
|
999
|
Chris@1511
|
1000 EventVector points =
|
Chris@1511
|
1001 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
|
Chris@1511
|
1002
|
Chris@1511
|
1003 for (EventVector::iterator i = points.begin();
|
Chris@1511
|
1004 i != points.end(); ++i) {
|
Chris@1511
|
1005
|
Chris@1511
|
1006 if (s.contains(i->getFrame())) {
|
Chris@1511
|
1007 command->remove(*i);
|
Chris@1511
|
1008 }
|
Chris@1511
|
1009 }
|
Chris@1511
|
1010
|
Chris@1511
|
1011 finish(command);
|
Chris@1511
|
1012 }
|
Chris@1511
|
1013
|
Chris@1511
|
1014 void
|
Chris@1511
|
1015 TimeFrequencyBoxLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to)
|
Chris@1511
|
1016 {
|
Chris@1511
|
1017 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
1018 if (!model) return;
|
Chris@1511
|
1019
|
Chris@1511
|
1020 EventVector points =
|
Chris@1511
|
1021 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
|
Chris@1511
|
1022
|
Chris@1511
|
1023 for (Event p: points) {
|
Chris@1511
|
1024 to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame())));
|
Chris@1511
|
1025 }
|
Chris@1511
|
1026 }
|
Chris@1511
|
1027
|
Chris@1511
|
1028 bool
|
Chris@1511
|
1029 TimeFrequencyBoxLayer::paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t /* frameOffset */, bool /* interactive */)
|
Chris@1511
|
1030 {
|
Chris@1511
|
1031 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
1032 if (!model) return false;
|
Chris@1511
|
1033
|
Chris@1511
|
1034 const EventVector &points = from.getPoints();
|
Chris@1511
|
1035
|
Chris@1511
|
1036 bool realign = false;
|
Chris@1511
|
1037
|
Chris@1511
|
1038 if (clipboardHasDifferentAlignment(v, from)) {
|
Chris@1511
|
1039
|
Chris@1511
|
1040 QMessageBox::StandardButton button =
|
Chris@1511
|
1041 QMessageBox::question(v->getView(), tr("Re-align pasted items?"),
|
Chris@1511
|
1042 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@1511
|
1043 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
|
Chris@1511
|
1044 QMessageBox::Yes);
|
Chris@1511
|
1045
|
Chris@1511
|
1046 if (button == QMessageBox::Cancel) {
|
Chris@1511
|
1047 return false;
|
Chris@1511
|
1048 }
|
Chris@1511
|
1049
|
Chris@1511
|
1050 if (button == QMessageBox::Yes) {
|
Chris@1511
|
1051 realign = true;
|
Chris@1511
|
1052 }
|
Chris@1511
|
1053 }
|
Chris@1511
|
1054
|
Chris@1511
|
1055 ChangeEventsCommand *command =
|
Chris@1511
|
1056 new ChangeEventsCommand(m_model.untyped, tr("Paste"));
|
Chris@1511
|
1057
|
Chris@1511
|
1058 for (EventVector::const_iterator i = points.begin();
|
Chris@1511
|
1059 i != points.end(); ++i) {
|
Chris@1511
|
1060
|
Chris@1511
|
1061 sv_frame_t frame = 0;
|
Chris@1511
|
1062
|
Chris@1511
|
1063 if (!realign) {
|
Chris@1511
|
1064
|
Chris@1511
|
1065 frame = i->getFrame();
|
Chris@1511
|
1066
|
Chris@1511
|
1067 } else {
|
Chris@1511
|
1068
|
Chris@1511
|
1069 if (i->hasReferenceFrame()) {
|
Chris@1511
|
1070 frame = i->getReferenceFrame();
|
Chris@1511
|
1071 frame = alignFromReference(v, frame);
|
Chris@1511
|
1072 } else {
|
Chris@1511
|
1073 frame = i->getFrame();
|
Chris@1511
|
1074 }
|
Chris@1511
|
1075 }
|
Chris@1511
|
1076
|
Chris@1511
|
1077 Event p = *i;
|
Chris@1511
|
1078 Event newPoint = p;
|
Chris@1511
|
1079 if (!p.hasValue()) {
|
Chris@1511
|
1080 newPoint = newPoint.withValue((model->getFrequencyMinimum() +
|
Chris@1511
|
1081 model->getFrequencyMaximum()) / 2);
|
Chris@1511
|
1082 }
|
Chris@1511
|
1083 if (!p.hasDuration()) {
|
Chris@1511
|
1084 sv_frame_t nextFrame = frame;
|
Chris@1511
|
1085 EventVector::const_iterator j = i;
|
Chris@1511
|
1086 for (; j != points.end(); ++j) {
|
Chris@1511
|
1087 if (j != i) break;
|
Chris@1511
|
1088 }
|
Chris@1511
|
1089 if (j != points.end()) {
|
Chris@1511
|
1090 nextFrame = j->getFrame();
|
Chris@1511
|
1091 }
|
Chris@1511
|
1092 if (nextFrame == frame) {
|
Chris@1511
|
1093 newPoint = newPoint.withDuration(model->getResolution());
|
Chris@1511
|
1094 } else {
|
Chris@1511
|
1095 newPoint = newPoint.withDuration(nextFrame - frame);
|
Chris@1511
|
1096 }
|
Chris@1511
|
1097 }
|
Chris@1511
|
1098
|
Chris@1511
|
1099 command->add(newPoint);
|
Chris@1511
|
1100 }
|
Chris@1511
|
1101
|
Chris@1511
|
1102 finish(command);
|
Chris@1511
|
1103 return true;
|
Chris@1511
|
1104 }
|
Chris@1511
|
1105
|
Chris@1511
|
1106 void
|
Chris@1511
|
1107 TimeFrequencyBoxLayer::toXml(QTextStream &stream,
|
Chris@1511
|
1108 QString indent, QString extraAttributes) const
|
Chris@1511
|
1109 {
|
Chris@1511
|
1110 QString s;
|
Chris@1511
|
1111
|
Chris@1511
|
1112 s += QString("verticalScale=\"%1\" ")
|
Chris@1511
|
1113 .arg(m_verticalScale);
|
Chris@1511
|
1114
|
Chris@1511
|
1115 SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s);
|
Chris@1511
|
1116 }
|
Chris@1511
|
1117
|
Chris@1511
|
1118 void
|
Chris@1511
|
1119 TimeFrequencyBoxLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@1511
|
1120 {
|
Chris@1511
|
1121 SingleColourLayer::setProperties(attributes);
|
Chris@1511
|
1122
|
Chris@1511
|
1123 bool ok;
|
Chris@1511
|
1124 VerticalScale scale = (VerticalScale)
|
Chris@1511
|
1125 attributes.value("verticalScale").toInt(&ok);
|
Chris@1511
|
1126 if (ok) setVerticalScale(scale);
|
Chris@1511
|
1127 }
|
Chris@1511
|
1128
|
Chris@1511
|
1129
|