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 // SVDEBUG << "TimeFrequencyBoxLayer::paint: resolution is "
|
Chris@1511
|
528 // << model->getResolution() << " frames" << endl;
|
Chris@1511
|
529
|
Chris@1511
|
530 double min = model->getFrequencyMinimum();
|
Chris@1511
|
531 double max = model->getFrequencyMaximum();
|
Chris@1511
|
532 if (max == min) max = min + 1.0;
|
Chris@1511
|
533
|
Chris@1511
|
534 QPoint localPos;
|
Chris@1511
|
535 Event illuminatePoint(0);
|
Chris@1511
|
536 bool shouldIlluminate = false;
|
Chris@1511
|
537
|
Chris@1511
|
538 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
|
Chris@1511
|
539 shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(),
|
Chris@1511
|
540 illuminatePoint);
|
Chris@1511
|
541 }
|
Chris@1511
|
542
|
Chris@1511
|
543 paint.save();
|
Chris@1511
|
544 paint.setRenderHint(QPainter::Antialiasing, false);
|
Chris@1511
|
545
|
Chris@1514
|
546 QFontMetrics fm = paint.fontMetrics();
|
Chris@1514
|
547
|
Chris@1511
|
548 for (EventVector::const_iterator i = points.begin();
|
Chris@1511
|
549 i != points.end(); ++i) {
|
Chris@1511
|
550
|
Chris@1511
|
551 const Event &p(*i);
|
Chris@1511
|
552
|
Chris@1511
|
553 int x = v->getXForFrame(p.getFrame());
|
Chris@1511
|
554 int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x;
|
Chris@1511
|
555 int y = getYForValue(v, p.getValue());
|
Chris@1514
|
556 int h = getYForValue(v, p.getValue() + fabsf(p.getLevel())) - y;
|
Chris@1511
|
557 int ex = x + w;
|
Chris@1514
|
558 int gap = v->scalePixelSize(2);
|
Chris@1511
|
559
|
Chris@1511
|
560 EventVector::const_iterator j = i;
|
Chris@1511
|
561 ++j;
|
Chris@1511
|
562
|
Chris@1511
|
563 if (j != points.end()) {
|
Chris@1511
|
564 const Event &q(*j);
|
Chris@1511
|
565 int nx = v->getXForFrame(q.getFrame());
|
Chris@1511
|
566 if (nx < ex) ex = nx;
|
Chris@1511
|
567 }
|
Chris@1511
|
568
|
Chris@1511
|
569 if (w < 1) w = 1;
|
Chris@1511
|
570
|
Chris@1511
|
571 paint.setPen(getBaseQColor());
|
Chris@1514
|
572 paint.setBrush(Qt::NoBrush);
|
Chris@1511
|
573
|
Chris@1514
|
574 if ((shouldIlluminate && illuminatePoint == p) ||
|
Chris@1514
|
575 (m_editing && m_editingPoint == p)) {
|
Chris@1511
|
576
|
Chris@1514
|
577 paint.setPen(QPen(getBaseQColor(), v->scalePixelSize(2)));
|
Chris@1511
|
578
|
Chris@1511
|
579 // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
|
Chris@1511
|
580 // replacement (horizontalAdvance) was only added in Qt 5.11
|
Chris@1511
|
581 // which is too new for us
|
Chris@1511
|
582 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
Chris@1511
|
583
|
Chris@1514
|
584 if (abs(h) > 2 * fm.height()) {
|
Chris@1514
|
585
|
Chris@1514
|
586 QString y0label = QString("%1 %2")
|
Chris@1514
|
587 .arg(p.getValue())
|
Chris@1514
|
588 .arg(getScaleUnits());
|
Chris@1514
|
589
|
Chris@1514
|
590 QString y1label = QString("%1 %2")
|
Chris@1514
|
591 .arg(p.getValue() + fabsf(p.getLevel()))
|
Chris@1514
|
592 .arg(getScaleUnits());
|
Chris@1514
|
593
|
Chris@1514
|
594 PaintAssistant::drawVisibleText
|
Chris@1514
|
595 (v, paint,
|
Chris@1514
|
596 x - fm.width(y0label) - gap,
|
Chris@1514
|
597 y - fm.descent(),
|
Chris@1514
|
598 y0label, PaintAssistant::OutlinedText);
|
Chris@1514
|
599
|
Chris@1514
|
600 PaintAssistant::drawVisibleText
|
Chris@1514
|
601 (v, paint,
|
Chris@1514
|
602 x - fm.width(y1label) - gap,
|
Chris@1514
|
603 y + h + fm.ascent(),
|
Chris@1514
|
604 y1label, PaintAssistant::OutlinedText);
|
Chris@1514
|
605
|
Chris@1514
|
606 } else {
|
Chris@1514
|
607
|
Chris@1514
|
608 QString ylabel = QString("%1 %2 - %3 %4")
|
Chris@1514
|
609 .arg(p.getValue())
|
Chris@1514
|
610 .arg(getScaleUnits())
|
Chris@1514
|
611 .arg(p.getValue() + fabsf(p.getLevel()))
|
Chris@1514
|
612 .arg(getScaleUnits());
|
Chris@1514
|
613
|
Chris@1514
|
614 PaintAssistant::drawVisibleText
|
Chris@1514
|
615 (v, paint,
|
Chris@1514
|
616 x - fm.width(ylabel) - gap,
|
Chris@1514
|
617 y - fm.descent(),
|
Chris@1514
|
618 ylabel, PaintAssistant::OutlinedText);
|
Chris@1514
|
619 }
|
Chris@1514
|
620
|
Chris@1514
|
621 QString t0label = RealTime::frame2RealTime
|
Chris@1514
|
622 (p.getFrame(), model->getSampleRate()).toText(true).c_str();
|
Chris@1514
|
623
|
Chris@1514
|
624 QString t1label = RealTime::frame2RealTime
|
Chris@1514
|
625 (p.getFrame() + p.getDuration(), model->getSampleRate())
|
Chris@1514
|
626 .toText(true).c_str();
|
Chris@1514
|
627
|
Chris@1511
|
628 PaintAssistant::drawVisibleText
|
Chris@1514
|
629 (v, paint, x, y + fm.ascent() + gap,
|
Chris@1514
|
630 t0label, PaintAssistant::OutlinedText);
|
Chris@1514
|
631
|
Chris@1514
|
632 if (w > fm.width(t0label) + fm.width(t1label) + gap * 3) {
|
Chris@1514
|
633
|
Chris@1514
|
634 PaintAssistant::drawVisibleText
|
Chris@1514
|
635 (v, paint,
|
Chris@1514
|
636 x + w - fm.width(t1label),
|
Chris@1514
|
637 y + fm.ascent() + gap,
|
Chris@1514
|
638 t1label, PaintAssistant::OutlinedText);
|
Chris@1514
|
639
|
Chris@1514
|
640 } else {
|
Chris@1514
|
641
|
Chris@1514
|
642 PaintAssistant::drawVisibleText
|
Chris@1514
|
643 (v, paint,
|
Chris@1514
|
644 x + w - fm.width(t1label),
|
Chris@1514
|
645 y + fm.ascent() + fm.height() + gap,
|
Chris@1514
|
646 t1label, PaintAssistant::OutlinedText);
|
Chris@1514
|
647 }
|
Chris@1511
|
648 }
|
Chris@1511
|
649
|
Chris@1511
|
650 paint.drawRect(x, y, w, h);
|
Chris@1511
|
651 }
|
Chris@1511
|
652
|
Chris@1511
|
653 for (EventVector::const_iterator i = points.begin();
|
Chris@1511
|
654 i != points.end(); ++i) {
|
Chris@1511
|
655
|
Chris@1511
|
656 const Event &p(*i);
|
Chris@1511
|
657
|
Chris@1514
|
658 QString label = p.getLabel();
|
Chris@1514
|
659 if (label == "") continue;
|
Chris@1514
|
660
|
Chris@1514
|
661 if (shouldIlluminate && illuminatePoint == p) {
|
Chris@1514
|
662 continue; // already handled this in illumination special case
|
Chris@1514
|
663 }
|
Chris@1514
|
664
|
Chris@1511
|
665 int x = v->getXForFrame(p.getFrame());
|
Chris@1511
|
666 int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x;
|
Chris@1511
|
667 int y = getYForValue(v, p.getValue());
|
Chris@1511
|
668
|
Chris@1514
|
669 int labelWidth = fm.width(label);
|
Chris@1511
|
670
|
Chris@1511
|
671 int gap = v->scalePixelSize(2);
|
Chris@1511
|
672
|
Chris@1511
|
673 if (x + w < x0 || x - labelWidth - gap > x1) {
|
Chris@1511
|
674 continue;
|
Chris@1511
|
675 }
|
Chris@1511
|
676
|
Chris@1514
|
677 int labelX, labelY;
|
Chris@1511
|
678
|
Chris@1514
|
679 labelX = x - labelWidth - gap;
|
Chris@1514
|
680 labelY = y - fm.descent();
|
Chris@1511
|
681
|
Chris@1514
|
682 PaintAssistant::drawVisibleText(v, paint, labelX, labelY, label,
|
Chris@1514
|
683 PaintAssistant::OutlinedText);
|
Chris@1511
|
684 }
|
Chris@1511
|
685
|
Chris@1511
|
686 paint.restore();
|
Chris@1511
|
687 }
|
Chris@1511
|
688
|
Chris@1511
|
689 int
|
Chris@1511
|
690 TimeFrequencyBoxLayer::getVerticalScaleWidth(LayerGeometryProvider *v,
|
Chris@1511
|
691 bool, QPainter &paint) const
|
Chris@1511
|
692 {
|
Chris@1511
|
693 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
694 if (!model || m_verticalScale == AutoAlignScale) {
|
Chris@1511
|
695 return 0;
|
Chris@1511
|
696 } else {
|
Chris@1511
|
697 if (m_verticalScale == LogScale) {
|
Chris@1511
|
698 return LogNumericalScale().getWidth(v, paint);
|
Chris@1511
|
699 } else {
|
Chris@1511
|
700 return LinearNumericalScale().getWidth(v, paint);
|
Chris@1511
|
701 }
|
Chris@1511
|
702 }
|
Chris@1511
|
703 }
|
Chris@1511
|
704
|
Chris@1511
|
705 void
|
Chris@1511
|
706 TimeFrequencyBoxLayer::paintVerticalScale(LayerGeometryProvider *v,
|
Chris@1511
|
707 bool, QPainter &paint, QRect) const
|
Chris@1511
|
708 {
|
Chris@1511
|
709 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
710 if (!model || model->isEmpty()) return;
|
Chris@1511
|
711
|
Chris@1511
|
712 QString unit;
|
Chris@1511
|
713 double min, max;
|
Chris@1511
|
714 bool logarithmic;
|
Chris@1511
|
715
|
Chris@1511
|
716 int w = getVerticalScaleWidth(v, false, paint);
|
Chris@1511
|
717
|
Chris@1511
|
718 getScaleExtents(v, min, max, logarithmic);
|
Chris@1511
|
719
|
Chris@1511
|
720 if (logarithmic) {
|
Chris@1511
|
721 LogNumericalScale().paintVertical(v, this, paint, 0, min, max);
|
Chris@1511
|
722 } else {
|
Chris@1511
|
723 LinearNumericalScale().paintVertical(v, this, paint, 0, min, max);
|
Chris@1511
|
724 }
|
Chris@1511
|
725
|
Chris@1511
|
726 if (getScaleUnits() != "") {
|
Chris@1511
|
727 int mw = w - 5;
|
Chris@1511
|
728 paint.drawText(5,
|
Chris@1511
|
729 5 + paint.fontMetrics().ascent(),
|
Chris@1511
|
730 TextAbbrev::abbreviate(getScaleUnits(),
|
Chris@1511
|
731 paint.fontMetrics(),
|
Chris@1511
|
732 mw));
|
Chris@1511
|
733 }
|
Chris@1511
|
734 }
|
Chris@1511
|
735
|
Chris@1511
|
736 void
|
Chris@1511
|
737 TimeFrequencyBoxLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
738 {
|
Chris@1511
|
739 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
740 if (!model) return;
|
Chris@1511
|
741
|
Chris@1511
|
742 sv_frame_t frame = v->getFrameForX(e->x());
|
Chris@1511
|
743 if (frame < 0) frame = 0;
|
Chris@1511
|
744 frame = frame / model->getResolution() * model->getResolution();
|
Chris@1511
|
745
|
Chris@1514
|
746 double frequency = getValueForY(v, e->y());
|
Chris@1511
|
747
|
Chris@1514
|
748 m_editingPoint = Event(frame, float(frequency), 0, "");
|
Chris@1511
|
749 m_originalPoint = m_editingPoint;
|
Chris@1511
|
750
|
Chris@1511
|
751 if (m_editingCommand) finish(m_editingCommand);
|
Chris@1511
|
752 m_editingCommand = new ChangeEventsCommand(m_model.untyped,
|
Chris@1511
|
753 tr("Draw Time-Frequency Box"));
|
Chris@1511
|
754 m_editingCommand->add(m_editingPoint);
|
Chris@1511
|
755
|
Chris@1511
|
756 m_editing = true;
|
Chris@1511
|
757 }
|
Chris@1511
|
758
|
Chris@1511
|
759 void
|
Chris@1511
|
760 TimeFrequencyBoxLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
761 {
|
Chris@1511
|
762 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
763 if (!model || !m_editing) return;
|
Chris@1511
|
764
|
Chris@1514
|
765 sv_frame_t dragFrame = v->getFrameForX(e->x());
|
Chris@1514
|
766 if (dragFrame < 0) dragFrame = 0;
|
Chris@1514
|
767 dragFrame = dragFrame / model->getResolution() * model->getResolution();
|
Chris@1511
|
768
|
Chris@1514
|
769 sv_frame_t eventFrame = m_originalPoint.getFrame();
|
Chris@1514
|
770 sv_frame_t eventDuration = dragFrame - eventFrame;
|
Chris@1514
|
771 if (eventDuration < 0) {
|
Chris@1514
|
772 eventFrame = eventFrame + eventDuration;
|
Chris@1514
|
773 eventDuration = -eventDuration;
|
Chris@1514
|
774 } else if (eventDuration == 0) {
|
Chris@1514
|
775 eventDuration = model->getResolution();
|
Chris@1514
|
776 }
|
Chris@1511
|
777
|
Chris@1514
|
778 double dragFrequency = getValueForY(v, e->y());
|
Chris@1514
|
779
|
Chris@1514
|
780 double eventFrequency = m_originalPoint.getValue();
|
Chris@1514
|
781 double eventFreqDiff = dragFrequency - eventFrequency;
|
Chris@1514
|
782 if (eventFreqDiff < 0) {
|
Chris@1514
|
783 eventFrequency = eventFrequency + eventFreqDiff;
|
Chris@1514
|
784 eventFreqDiff = -eventFreqDiff;
|
Chris@1511
|
785 }
|
Chris@1511
|
786
|
Chris@1511
|
787 m_editingCommand->remove(m_editingPoint);
|
Chris@1511
|
788 m_editingPoint = m_editingPoint
|
Chris@1514
|
789 .withFrame(eventFrame)
|
Chris@1514
|
790 .withDuration(eventDuration)
|
Chris@1514
|
791 .withValue(float(eventFrequency))
|
Chris@1514
|
792 .withLevel(float(eventFreqDiff));
|
Chris@1511
|
793 m_editingCommand->add(m_editingPoint);
|
Chris@1511
|
794 }
|
Chris@1511
|
795
|
Chris@1511
|
796 void
|
Chris@1511
|
797 TimeFrequencyBoxLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *)
|
Chris@1511
|
798 {
|
Chris@1511
|
799 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
800 if (!model || !m_editing) return;
|
Chris@1511
|
801 finish(m_editingCommand);
|
Chris@1511
|
802 m_editingCommand = nullptr;
|
Chris@1511
|
803 m_editing = false;
|
Chris@1511
|
804 }
|
Chris@1511
|
805
|
Chris@1511
|
806 void
|
Chris@1511
|
807 TimeFrequencyBoxLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
808 {
|
Chris@1511
|
809 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
810 if (!model) return;
|
Chris@1511
|
811
|
Chris@1511
|
812 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return;
|
Chris@1511
|
813
|
Chris@1511
|
814 if (m_editingCommand) {
|
Chris@1511
|
815 finish(m_editingCommand);
|
Chris@1511
|
816 m_editingCommand = nullptr;
|
Chris@1511
|
817 }
|
Chris@1511
|
818
|
Chris@1511
|
819 m_editing = true;
|
Chris@1511
|
820 }
|
Chris@1511
|
821
|
Chris@1511
|
822 void
|
Chris@1511
|
823 TimeFrequencyBoxLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *)
|
Chris@1511
|
824 {
|
Chris@1511
|
825 }
|
Chris@1511
|
826
|
Chris@1511
|
827 void
|
Chris@1511
|
828 TimeFrequencyBoxLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
829 {
|
Chris@1511
|
830 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
831 if (!model || !m_editing) return;
|
Chris@1511
|
832
|
Chris@1511
|
833 m_editing = false;
|
Chris@1511
|
834
|
Chris@1511
|
835 Event p(0);
|
Chris@1511
|
836 if (!getPointToDrag(v, e->x(), e->y(), p)) return;
|
Chris@1511
|
837 if (p.getFrame() != m_editingPoint.getFrame() ||
|
Chris@1511
|
838 p.getValue() != m_editingPoint.getValue()) return;
|
Chris@1511
|
839
|
Chris@1511
|
840 m_editingCommand = new ChangeEventsCommand
|
Chris@1511
|
841 (m_model.untyped, tr("Erase Time-Frequency Box"));
|
Chris@1511
|
842
|
Chris@1511
|
843 m_editingCommand->remove(m_editingPoint);
|
Chris@1511
|
844
|
Chris@1511
|
845 finish(m_editingCommand);
|
Chris@1511
|
846 m_editingCommand = nullptr;
|
Chris@1511
|
847 m_editing = false;
|
Chris@1511
|
848 }
|
Chris@1511
|
849
|
Chris@1511
|
850 void
|
Chris@1511
|
851 TimeFrequencyBoxLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
852 {
|
Chris@1511
|
853 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
854 if (!model) return;
|
Chris@1511
|
855
|
Chris@1511
|
856 if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) {
|
Chris@1511
|
857 return;
|
Chris@1511
|
858 }
|
Chris@1511
|
859
|
Chris@1511
|
860 m_dragPointX = v->getXForFrame(m_editingPoint.getFrame());
|
Chris@1511
|
861 m_dragPointY = getYForValue(v, m_editingPoint.getValue());
|
Chris@1511
|
862
|
Chris@1511
|
863 m_originalPoint = m_editingPoint;
|
Chris@1511
|
864
|
Chris@1511
|
865 if (m_editingCommand) {
|
Chris@1511
|
866 finish(m_editingCommand);
|
Chris@1511
|
867 m_editingCommand = nullptr;
|
Chris@1511
|
868 }
|
Chris@1511
|
869
|
Chris@1511
|
870 m_editing = true;
|
Chris@1511
|
871 m_dragStartX = e->x();
|
Chris@1511
|
872 m_dragStartY = e->y();
|
Chris@1511
|
873 }
|
Chris@1511
|
874
|
Chris@1511
|
875 void
|
Chris@1511
|
876 TimeFrequencyBoxLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
877 {
|
Chris@1511
|
878 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
879 if (!model || !m_editing) return;
|
Chris@1511
|
880
|
Chris@1511
|
881 int xdist = e->x() - m_dragStartX;
|
Chris@1511
|
882 int ydist = e->y() - m_dragStartY;
|
Chris@1511
|
883 int newx = m_dragPointX + xdist;
|
Chris@1511
|
884 int newy = m_dragPointY + ydist;
|
Chris@1511
|
885
|
Chris@1511
|
886 sv_frame_t frame = v->getFrameForX(newx);
|
Chris@1511
|
887 if (frame < 0) frame = 0;
|
Chris@1511
|
888 frame = frame / model->getResolution() * model->getResolution();
|
Chris@1511
|
889
|
Chris@1511
|
890 double value = getValueForY(v, newy);
|
Chris@1511
|
891
|
Chris@1511
|
892 if (!m_editingCommand) {
|
Chris@1511
|
893 m_editingCommand = new ChangeEventsCommand
|
Chris@1511
|
894 (m_model.untyped,
|
Chris@1511
|
895 tr("Drag Time-Frequency Box"));
|
Chris@1511
|
896 }
|
Chris@1511
|
897
|
Chris@1511
|
898 m_editingCommand->remove(m_editingPoint);
|
Chris@1511
|
899 m_editingPoint = m_editingPoint
|
Chris@1511
|
900 .withFrame(frame)
|
Chris@1511
|
901 .withValue(float(value));
|
Chris@1511
|
902 m_editingCommand->add(m_editingPoint);
|
Chris@1511
|
903 }
|
Chris@1511
|
904
|
Chris@1511
|
905 void
|
Chris@1511
|
906 TimeFrequencyBoxLayer::editEnd(LayerGeometryProvider *, QMouseEvent *)
|
Chris@1511
|
907 {
|
Chris@1511
|
908 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
909 if (!model || !m_editing) return;
|
Chris@1511
|
910
|
Chris@1511
|
911 if (m_editingCommand) {
|
Chris@1511
|
912
|
Chris@1511
|
913 QString newName = m_editingCommand->getName();
|
Chris@1511
|
914
|
Chris@1511
|
915 if (m_editingPoint.getFrame() != m_originalPoint.getFrame()) {
|
Chris@1511
|
916 if (m_editingPoint.getValue() != m_originalPoint.getValue()) {
|
Chris@1511
|
917 newName = tr("Edit Time-Frequency Box");
|
Chris@1511
|
918 } else {
|
Chris@1511
|
919 newName = tr("Relocate Time-Frequency Box");
|
Chris@1511
|
920 }
|
Chris@1511
|
921 } else {
|
Chris@1511
|
922 newName = tr("Change Point Value");
|
Chris@1511
|
923 }
|
Chris@1511
|
924
|
Chris@1511
|
925 m_editingCommand->setName(newName);
|
Chris@1511
|
926 finish(m_editingCommand);
|
Chris@1511
|
927 }
|
Chris@1511
|
928
|
Chris@1511
|
929 m_editingCommand = nullptr;
|
Chris@1511
|
930 m_editing = false;
|
Chris@1511
|
931 }
|
Chris@1511
|
932
|
Chris@1511
|
933 bool
|
Chris@1511
|
934 TimeFrequencyBoxLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e)
|
Chris@1511
|
935 {
|
Chris@1511
|
936 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
937 if (!model) return false;
|
Chris@1511
|
938
|
Chris@1511
|
939 Event region(0);
|
Chris@1511
|
940 if (!getPointToDrag(v, e->x(), e->y(), region)) return false;
|
Chris@1511
|
941
|
Chris@1515
|
942 ItemEditDialog::LabelOptions labelOptions;
|
Chris@1515
|
943 labelOptions.valueLabel = tr("Minimum Frequency");
|
Chris@1515
|
944 labelOptions.levelLabel = tr("Frequency Extent");
|
Chris@1515
|
945 labelOptions.valueUnits = getScaleUnits();
|
Chris@1515
|
946 labelOptions.levelUnits = getScaleUnits();
|
Chris@1515
|
947
|
Chris@1511
|
948 ItemEditDialog *dialog = new ItemEditDialog
|
Chris@1511
|
949 (model->getSampleRate(),
|
Chris@1511
|
950 ItemEditDialog::ShowTime |
|
Chris@1511
|
951 ItemEditDialog::ShowDuration |
|
Chris@1511
|
952 ItemEditDialog::ShowValue |
|
Chris@1515
|
953 ItemEditDialog::ShowLevel |
|
Chris@1511
|
954 ItemEditDialog::ShowText,
|
Chris@1515
|
955 labelOptions);
|
Chris@1511
|
956
|
Chris@1511
|
957 dialog->setFrameTime(region.getFrame());
|
Chris@1511
|
958 dialog->setValue(region.getValue());
|
Chris@1515
|
959 dialog->setLevel(region.getLevel());
|
Chris@1511
|
960 dialog->setFrameDuration(region.getDuration());
|
Chris@1511
|
961 dialog->setText(region.getLabel());
|
Chris@1511
|
962
|
Chris@1511
|
963 if (dialog->exec() == QDialog::Accepted) {
|
Chris@1511
|
964
|
Chris@1511
|
965 Event newTimeFrequencyBox = region
|
Chris@1511
|
966 .withFrame(dialog->getFrameTime())
|
Chris@1511
|
967 .withValue(dialog->getValue())
|
Chris@1515
|
968 .withLevel(dialog->getLevel())
|
Chris@1511
|
969 .withDuration(dialog->getFrameDuration())
|
Chris@1511
|
970 .withLabel(dialog->getText());
|
Chris@1511
|
971
|
Chris@1511
|
972 ChangeEventsCommand *command = new ChangeEventsCommand
|
Chris@1511
|
973 (m_model.untyped, tr("Edit Time-Frequency Box"));
|
Chris@1511
|
974 command->remove(region);
|
Chris@1511
|
975 command->add(newTimeFrequencyBox);
|
Chris@1511
|
976 finish(command);
|
Chris@1511
|
977 }
|
Chris@1511
|
978
|
Chris@1511
|
979 delete dialog;
|
Chris@1511
|
980 return true;
|
Chris@1511
|
981 }
|
Chris@1511
|
982
|
Chris@1511
|
983 void
|
Chris@1511
|
984 TimeFrequencyBoxLayer::moveSelection(Selection s, sv_frame_t newStartFrame)
|
Chris@1511
|
985 {
|
Chris@1511
|
986 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
987 if (!model) return;
|
Chris@1511
|
988
|
Chris@1511
|
989 ChangeEventsCommand *command =
|
Chris@1511
|
990 new ChangeEventsCommand(m_model.untyped, tr("Drag Selection"));
|
Chris@1511
|
991
|
Chris@1511
|
992 EventVector points =
|
Chris@1511
|
993 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
|
Chris@1511
|
994
|
Chris@1511
|
995 for (EventVector::iterator i = points.begin();
|
Chris@1511
|
996 i != points.end(); ++i) {
|
Chris@1511
|
997
|
Chris@1511
|
998 Event newPoint = (*i)
|
Chris@1511
|
999 .withFrame(i->getFrame() + newStartFrame - s.getStartFrame());
|
Chris@1511
|
1000 command->remove(*i);
|
Chris@1511
|
1001 command->add(newPoint);
|
Chris@1511
|
1002 }
|
Chris@1511
|
1003
|
Chris@1511
|
1004 finish(command);
|
Chris@1511
|
1005 }
|
Chris@1511
|
1006
|
Chris@1511
|
1007 void
|
Chris@1511
|
1008 TimeFrequencyBoxLayer::resizeSelection(Selection s, Selection newSize)
|
Chris@1511
|
1009 {
|
Chris@1511
|
1010 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
1011 if (!model || !s.getDuration()) return;
|
Chris@1511
|
1012
|
Chris@1511
|
1013 ChangeEventsCommand *command =
|
Chris@1511
|
1014 new ChangeEventsCommand(m_model.untyped, tr("Resize Selection"));
|
Chris@1511
|
1015
|
Chris@1511
|
1016 EventVector points =
|
Chris@1511
|
1017 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
|
Chris@1511
|
1018
|
Chris@1511
|
1019 double ratio = double(newSize.getDuration()) / double(s.getDuration());
|
Chris@1511
|
1020 double oldStart = double(s.getStartFrame());
|
Chris@1511
|
1021 double newStart = double(newSize.getStartFrame());
|
Chris@1511
|
1022
|
Chris@1511
|
1023 for (Event p: points) {
|
Chris@1511
|
1024
|
Chris@1511
|
1025 double newFrame = (double(p.getFrame()) - oldStart) * ratio + newStart;
|
Chris@1511
|
1026 double newDuration = double(p.getDuration()) * ratio;
|
Chris@1511
|
1027
|
Chris@1511
|
1028 Event newPoint = p
|
Chris@1511
|
1029 .withFrame(lrint(newFrame))
|
Chris@1511
|
1030 .withDuration(lrint(newDuration));
|
Chris@1511
|
1031 command->remove(p);
|
Chris@1511
|
1032 command->add(newPoint);
|
Chris@1511
|
1033 }
|
Chris@1511
|
1034
|
Chris@1511
|
1035 finish(command);
|
Chris@1511
|
1036 }
|
Chris@1511
|
1037
|
Chris@1511
|
1038 void
|
Chris@1511
|
1039 TimeFrequencyBoxLayer::deleteSelection(Selection s)
|
Chris@1511
|
1040 {
|
Chris@1511
|
1041 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
1042 if (!model) return;
|
Chris@1511
|
1043
|
Chris@1511
|
1044 ChangeEventsCommand *command =
|
Chris@1511
|
1045 new ChangeEventsCommand(m_model.untyped, tr("Delete Selected Points"));
|
Chris@1511
|
1046
|
Chris@1511
|
1047 EventVector points =
|
Chris@1511
|
1048 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
|
Chris@1511
|
1049
|
Chris@1511
|
1050 for (EventVector::iterator i = points.begin();
|
Chris@1511
|
1051 i != points.end(); ++i) {
|
Chris@1511
|
1052
|
Chris@1511
|
1053 if (s.contains(i->getFrame())) {
|
Chris@1511
|
1054 command->remove(*i);
|
Chris@1511
|
1055 }
|
Chris@1511
|
1056 }
|
Chris@1511
|
1057
|
Chris@1511
|
1058 finish(command);
|
Chris@1511
|
1059 }
|
Chris@1511
|
1060
|
Chris@1511
|
1061 void
|
Chris@1511
|
1062 TimeFrequencyBoxLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to)
|
Chris@1511
|
1063 {
|
Chris@1511
|
1064 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
1065 if (!model) return;
|
Chris@1511
|
1066
|
Chris@1511
|
1067 EventVector points =
|
Chris@1511
|
1068 model->getEventsStartingWithin(s.getStartFrame(), s.getDuration());
|
Chris@1511
|
1069
|
Chris@1511
|
1070 for (Event p: points) {
|
Chris@1511
|
1071 to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame())));
|
Chris@1511
|
1072 }
|
Chris@1511
|
1073 }
|
Chris@1511
|
1074
|
Chris@1511
|
1075 bool
|
Chris@1511
|
1076 TimeFrequencyBoxLayer::paste(LayerGeometryProvider *v, const Clipboard &from, sv_frame_t /* frameOffset */, bool /* interactive */)
|
Chris@1511
|
1077 {
|
Chris@1511
|
1078 auto model = ModelById::getAs<TimeFrequencyBoxModel>(m_model);
|
Chris@1511
|
1079 if (!model) return false;
|
Chris@1511
|
1080
|
Chris@1511
|
1081 const EventVector &points = from.getPoints();
|
Chris@1511
|
1082
|
Chris@1511
|
1083 bool realign = false;
|
Chris@1511
|
1084
|
Chris@1511
|
1085 if (clipboardHasDifferentAlignment(v, from)) {
|
Chris@1511
|
1086
|
Chris@1511
|
1087 QMessageBox::StandardButton button =
|
Chris@1511
|
1088 QMessageBox::question(v->getView(), tr("Re-align pasted items?"),
|
Chris@1511
|
1089 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
|
1090 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
|
Chris@1511
|
1091 QMessageBox::Yes);
|
Chris@1511
|
1092
|
Chris@1511
|
1093 if (button == QMessageBox::Cancel) {
|
Chris@1511
|
1094 return false;
|
Chris@1511
|
1095 }
|
Chris@1511
|
1096
|
Chris@1511
|
1097 if (button == QMessageBox::Yes) {
|
Chris@1511
|
1098 realign = true;
|
Chris@1511
|
1099 }
|
Chris@1511
|
1100 }
|
Chris@1511
|
1101
|
Chris@1511
|
1102 ChangeEventsCommand *command =
|
Chris@1511
|
1103 new ChangeEventsCommand(m_model.untyped, tr("Paste"));
|
Chris@1511
|
1104
|
Chris@1511
|
1105 for (EventVector::const_iterator i = points.begin();
|
Chris@1511
|
1106 i != points.end(); ++i) {
|
Chris@1511
|
1107
|
Chris@1511
|
1108 sv_frame_t frame = 0;
|
Chris@1511
|
1109
|
Chris@1511
|
1110 if (!realign) {
|
Chris@1511
|
1111
|
Chris@1511
|
1112 frame = i->getFrame();
|
Chris@1511
|
1113
|
Chris@1511
|
1114 } else {
|
Chris@1511
|
1115
|
Chris@1511
|
1116 if (i->hasReferenceFrame()) {
|
Chris@1511
|
1117 frame = i->getReferenceFrame();
|
Chris@1511
|
1118 frame = alignFromReference(v, frame);
|
Chris@1511
|
1119 } else {
|
Chris@1511
|
1120 frame = i->getFrame();
|
Chris@1511
|
1121 }
|
Chris@1511
|
1122 }
|
Chris@1511
|
1123
|
Chris@1511
|
1124 Event p = *i;
|
Chris@1511
|
1125 Event newPoint = p;
|
Chris@1511
|
1126 if (!p.hasValue()) {
|
Chris@1511
|
1127 newPoint = newPoint.withValue((model->getFrequencyMinimum() +
|
Chris@1511
|
1128 model->getFrequencyMaximum()) / 2);
|
Chris@1511
|
1129 }
|
Chris@1511
|
1130 if (!p.hasDuration()) {
|
Chris@1511
|
1131 sv_frame_t nextFrame = frame;
|
Chris@1511
|
1132 EventVector::const_iterator j = i;
|
Chris@1511
|
1133 for (; j != points.end(); ++j) {
|
Chris@1511
|
1134 if (j != i) break;
|
Chris@1511
|
1135 }
|
Chris@1511
|
1136 if (j != points.end()) {
|
Chris@1511
|
1137 nextFrame = j->getFrame();
|
Chris@1511
|
1138 }
|
Chris@1511
|
1139 if (nextFrame == frame) {
|
Chris@1511
|
1140 newPoint = newPoint.withDuration(model->getResolution());
|
Chris@1511
|
1141 } else {
|
Chris@1511
|
1142 newPoint = newPoint.withDuration(nextFrame - frame);
|
Chris@1511
|
1143 }
|
Chris@1511
|
1144 }
|
Chris@1511
|
1145
|
Chris@1511
|
1146 command->add(newPoint);
|
Chris@1511
|
1147 }
|
Chris@1511
|
1148
|
Chris@1511
|
1149 finish(command);
|
Chris@1511
|
1150 return true;
|
Chris@1511
|
1151 }
|
Chris@1511
|
1152
|
Chris@1511
|
1153 void
|
Chris@1511
|
1154 TimeFrequencyBoxLayer::toXml(QTextStream &stream,
|
Chris@1515
|
1155 QString indent, QString extraAttributes) const
|
Chris@1511
|
1156 {
|
Chris@1511
|
1157 QString s;
|
Chris@1511
|
1158
|
Chris@1515
|
1159 s += QString("verticalScale=\"%1\" ").arg(m_verticalScale);
|
Chris@1511
|
1160
|
Chris@1511
|
1161 SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s);
|
Chris@1511
|
1162 }
|
Chris@1511
|
1163
|
Chris@1511
|
1164 void
|
Chris@1511
|
1165 TimeFrequencyBoxLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@1511
|
1166 {
|
Chris@1511
|
1167 SingleColourLayer::setProperties(attributes);
|
Chris@1511
|
1168
|
Chris@1511
|
1169 bool ok;
|
Chris@1511
|
1170 VerticalScale scale = (VerticalScale)
|
Chris@1511
|
1171 attributes.value("verticalScale").toInt(&ok);
|
Chris@1511
|
1172 if (ok) setVerticalScale(scale);
|
Chris@1511
|
1173 }
|
Chris@1511
|
1174
|
Chris@1511
|
1175
|