Chris@441
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@441
|
2
|
Chris@441
|
3 /*
|
Chris@441
|
4 Sonic Visualiser
|
Chris@441
|
5 An audio file viewer and annotation editor.
|
Chris@441
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@441
|
7 This file copyright 2006 Chris Cannam.
|
Chris@441
|
8
|
Chris@441
|
9 This program is free software; you can redistribute it and/or
|
Chris@441
|
10 modify it under the terms of the GNU General Public License as
|
Chris@441
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@441
|
12 License, or (at your option) any later version. See the file
|
Chris@441
|
13 COPYING included with this distribution for more information.
|
Chris@441
|
14 */
|
Chris@441
|
15
|
Chris@1581
|
16 #ifndef SV_REGION_MODEL_H
|
Chris@1581
|
17 #define SV_REGION_MODEL_H
|
Chris@441
|
18
|
Chris@1649
|
19 #include "EventCommands.h"
|
Chris@1649
|
20 #include "TabularModel.h"
|
Chris@1649
|
21 #include "Model.h"
|
Chris@1649
|
22
|
Chris@441
|
23 #include "base/RealTime.h"
|
Chris@1649
|
24 #include "base/EventSeries.h"
|
Chris@1649
|
25 #include "base/UnitDatabase.h"
|
Chris@1649
|
26
|
Chris@1649
|
27 #include "system/System.h"
|
Chris@1649
|
28
|
Chris@1649
|
29 #include <QMutex>
|
Chris@441
|
30
|
Chris@441
|
31 /**
|
Chris@1649
|
32 * RegionModel -- a model for intervals associated with a value, which
|
Chris@1649
|
33 * we call regions for no very compelling reason.
|
Chris@441
|
34 */
|
Chris@1649
|
35 class RegionModel : public Model,
|
Chris@1649
|
36 public TabularModel,
|
Chris@1649
|
37 public EventEditable
|
Chris@441
|
38 {
|
Chris@441
|
39 Q_OBJECT
|
Chris@441
|
40
|
Chris@441
|
41 public:
|
Chris@1649
|
42 RegionModel(sv_samplerate_t sampleRate,
|
Chris@1649
|
43 int resolution,
|
Chris@441
|
44 bool notifyOnAdd = true) :
|
Chris@1649
|
45 m_sampleRate(sampleRate),
|
Chris@1649
|
46 m_resolution(resolution),
|
Chris@1649
|
47 m_valueMinimum(0.f),
|
Chris@1649
|
48 m_valueMaximum(0.f),
|
Chris@1649
|
49 m_haveExtents(false),
|
Chris@1429
|
50 m_valueQuantization(0),
|
Chris@1649
|
51 m_haveDistinctValues(false),
|
Chris@1649
|
52 m_notifyOnAdd(notifyOnAdd),
|
Chris@1649
|
53 m_sinceLastNotifyMin(-1),
|
Chris@1649
|
54 m_sinceLastNotifyMax(-1),
|
Chris@1649
|
55 m_completion(0) {
|
Chris@441
|
56 }
|
Chris@441
|
57
|
Chris@1040
|
58 RegionModel(sv_samplerate_t sampleRate, int resolution,
|
Chris@459
|
59 float valueMinimum, float valueMaximum,
|
Chris@459
|
60 bool notifyOnAdd = true) :
|
Chris@1649
|
61 m_sampleRate(sampleRate),
|
Chris@1649
|
62 m_resolution(resolution),
|
Chris@1649
|
63 m_valueMinimum(valueMinimum),
|
Chris@1649
|
64 m_valueMaximum(valueMaximum),
|
Chris@1649
|
65 m_haveExtents(false),
|
Chris@1429
|
66 m_valueQuantization(0),
|
Chris@1649
|
67 m_haveDistinctValues(false),
|
Chris@1649
|
68 m_notifyOnAdd(notifyOnAdd),
|
Chris@1649
|
69 m_sinceLastNotifyMin(-1),
|
Chris@1649
|
70 m_sinceLastNotifyMax(-1),
|
Chris@1649
|
71 m_completion(0) {
|
Chris@441
|
72 }
|
Chris@441
|
73
|
Chris@1649
|
74 virtual ~RegionModel() {
|
Chris@1649
|
75 }
|
Chris@1649
|
76
|
Chris@1649
|
77 QString getTypeName() const override { return tr("Region"); }
|
Chris@1649
|
78
|
Chris@1649
|
79 bool isOK() const override { return true; }
|
Chris@1649
|
80 sv_frame_t getStartFrame() const override { return m_events.getStartFrame(); }
|
Chris@1649
|
81 sv_frame_t getEndFrame() const override { return m_events.getEndFrame(); }
|
Chris@1649
|
82 sv_samplerate_t getSampleRate() const override { return m_sampleRate; }
|
Chris@1649
|
83 int getResolution() const { return m_resolution; }
|
Chris@1649
|
84
|
Chris@1649
|
85 QString getScaleUnits() const { return m_units; }
|
Chris@1649
|
86 void setScaleUnits(QString units) {
|
Chris@1649
|
87 m_units = units;
|
Chris@1649
|
88 UnitDatabase::getInstance()->registerUnit(units);
|
Chris@441
|
89 }
|
Chris@441
|
90
|
Chris@441
|
91 float getValueQuantization() const { return m_valueQuantization; }
|
Chris@441
|
92 void setValueQuantization(float q) { m_valueQuantization = q; }
|
Chris@441
|
93
|
Chris@442
|
94 bool haveDistinctValues() const { return m_haveDistinctValues; }
|
Chris@442
|
95
|
Chris@1649
|
96 float getValueMinimum() const { return m_valueMinimum; }
|
Chris@1649
|
97 float getValueMaximum() const { return m_valueMaximum; }
|
Chris@1649
|
98
|
Chris@1649
|
99 int getCompletion() const { return m_completion; }
|
Chris@441
|
100
|
Chris@1649
|
101 void setCompletion(int completion, bool update = true) {
|
Chris@441
|
102
|
Chris@1649
|
103 bool emitCompletionChanged = true;
|
Chris@1649
|
104 bool emitGeneralModelChanged = false;
|
Chris@1649
|
105 bool emitRegionChanged = false;
|
Chris@1649
|
106
|
Chris@1649
|
107 {
|
Chris@1649
|
108 QMutexLocker locker(&m_mutex);
|
Chris@1649
|
109
|
Chris@1649
|
110 if (m_completion != completion) {
|
Chris@1649
|
111 m_completion = completion;
|
Chris@1649
|
112
|
Chris@1649
|
113 if (completion == 100) {
|
Chris@1649
|
114
|
Chris@1649
|
115 if (m_notifyOnAdd) {
|
Chris@1649
|
116 emitCompletionChanged = false;
|
Chris@1649
|
117 }
|
Chris@1649
|
118
|
Chris@1649
|
119 m_notifyOnAdd = true; // henceforth
|
Chris@1649
|
120 emitGeneralModelChanged = true;
|
Chris@1649
|
121
|
Chris@1649
|
122 } else if (!m_notifyOnAdd) {
|
Chris@1649
|
123
|
Chris@1649
|
124 if (update &&
|
Chris@1649
|
125 m_sinceLastNotifyMin >= 0 &&
|
Chris@1649
|
126 m_sinceLastNotifyMax >= 0) {
|
Chris@1649
|
127 emitRegionChanged = true;
|
Chris@1649
|
128 }
|
Chris@1649
|
129 }
|
Chris@1649
|
130 }
|
Chris@1649
|
131 }
|
Chris@1649
|
132
|
Chris@1649
|
133 if (emitCompletionChanged) {
|
Chris@1649
|
134 emit completionChanged();
|
Chris@1649
|
135 }
|
Chris@1649
|
136 if (emitGeneralModelChanged) {
|
Chris@1649
|
137 emit modelChanged();
|
Chris@1649
|
138 }
|
Chris@1649
|
139 if (emitRegionChanged) {
|
Chris@1649
|
140 emit modelChangedWithin(m_sinceLastNotifyMin, m_sinceLastNotifyMax);
|
Chris@1649
|
141 m_sinceLastNotifyMin = m_sinceLastNotifyMax = -1;
|
Chris@1649
|
142 }
|
Chris@441
|
143 }
|
Chris@441
|
144
|
Chris@441
|
145 /**
|
Chris@1649
|
146 * Query methods.
|
Chris@1649
|
147 */
|
Chris@1649
|
148
|
Chris@1649
|
149 int getEventCount() const {
|
Chris@1649
|
150 return m_events.count();
|
Chris@1649
|
151 }
|
Chris@1649
|
152 bool isEmpty() const {
|
Chris@1649
|
153 return m_events.isEmpty();
|
Chris@1649
|
154 }
|
Chris@1649
|
155 bool containsEvent(const Event &e) const {
|
Chris@1649
|
156 return m_events.contains(e);
|
Chris@1649
|
157 }
|
Chris@1649
|
158 EventVector getAllEvents() const {
|
Chris@1649
|
159 return m_events.getAllEvents();
|
Chris@1649
|
160 }
|
Chris@1649
|
161 EventVector getEventsSpanning(sv_frame_t f, sv_frame_t duration) const {
|
Chris@1649
|
162 return m_events.getEventsSpanning(f, duration);
|
Chris@1649
|
163 }
|
Chris@1649
|
164 EventVector getEventsWithin(sv_frame_t f, sv_frame_t duration) const {
|
Chris@1649
|
165 return m_events.getEventsWithin(f, duration);
|
Chris@1649
|
166 }
|
Chris@1649
|
167 EventVector getEventsStartingWithin(sv_frame_t f, sv_frame_t duration) const {
|
Chris@1649
|
168 return m_events.getEventsStartingWithin(f, duration);
|
Chris@1649
|
169 }
|
Chris@1649
|
170 EventVector getEventsCovering(sv_frame_t f) const {
|
Chris@1649
|
171 return m_events.getEventsCovering(f);
|
Chris@1649
|
172 }
|
Chris@1649
|
173
|
Chris@1649
|
174 /**
|
Chris@1649
|
175 * Editing methods.
|
Chris@1649
|
176 */
|
Chris@1649
|
177 void add(Event e) override {
|
Chris@1649
|
178
|
Chris@1649
|
179 bool allChange = false;
|
Chris@1649
|
180
|
Chris@1649
|
181 {
|
Chris@1649
|
182 QMutexLocker locker(&m_mutex);
|
Chris@1649
|
183 m_events.add(e);
|
Chris@1649
|
184 //!!!??? if (point.getLabel() != "") m_hasTextLabels = true;
|
Chris@1649
|
185
|
Chris@1649
|
186 float v = e.getValue();
|
Chris@1649
|
187 if (!ISNAN(v) && !ISINF(v)) {
|
Chris@1649
|
188 if (!m_haveExtents || v < m_valueMinimum) {
|
Chris@1649
|
189 m_valueMinimum = v; allChange = true;
|
Chris@1649
|
190 }
|
Chris@1649
|
191 if (!m_haveExtents || v > m_valueMaximum) {
|
Chris@1649
|
192 m_valueMaximum = v; allChange = true;
|
Chris@1649
|
193 }
|
Chris@1649
|
194 m_haveExtents = true;
|
Chris@1649
|
195 }
|
Chris@1649
|
196
|
Chris@1649
|
197 if (e.hasValue() && e.getValue() != 0.f) {
|
Chris@1649
|
198 m_haveDistinctValues = true;
|
Chris@1649
|
199 }
|
Chris@1649
|
200
|
Chris@1649
|
201 sv_frame_t f = e.getFrame();
|
Chris@1649
|
202
|
Chris@1649
|
203 if (!m_notifyOnAdd) {
|
Chris@1649
|
204 if (m_sinceLastNotifyMin == -1 || f < m_sinceLastNotifyMin) {
|
Chris@1649
|
205 m_sinceLastNotifyMin = f;
|
Chris@1649
|
206 }
|
Chris@1649
|
207 if (m_sinceLastNotifyMax == -1 || f > m_sinceLastNotifyMax) {
|
Chris@1649
|
208 m_sinceLastNotifyMax = f;
|
Chris@1649
|
209 }
|
Chris@1649
|
210 }
|
Chris@1649
|
211 }
|
Chris@1649
|
212
|
Chris@1649
|
213 if (m_notifyOnAdd) {
|
Chris@1649
|
214 emit modelChangedWithin(e.getFrame(),
|
Chris@1649
|
215 e.getFrame() + e.getDuration() + m_resolution);
|
Chris@1649
|
216 }
|
Chris@1649
|
217 if (allChange) {
|
Chris@1649
|
218 emit modelChanged();
|
Chris@1649
|
219 }
|
Chris@1649
|
220 }
|
Chris@1649
|
221
|
Chris@1649
|
222 void remove(Event e) override {
|
Chris@1649
|
223 {
|
Chris@1649
|
224 QMutexLocker locker(&m_mutex);
|
Chris@1649
|
225 m_events.remove(e);
|
Chris@1649
|
226 }
|
Chris@1649
|
227 emit modelChangedWithin(e.getFrame(),
|
Chris@1649
|
228 e.getFrame() + e.getDuration() + m_resolution);
|
Chris@1649
|
229 }
|
Chris@1649
|
230
|
Chris@1649
|
231 /**
|
Chris@441
|
232 * TabularModel methods.
|
Chris@441
|
233 */
|
Chris@1649
|
234
|
Chris@1649
|
235 int getRowCount() const override {
|
Chris@1649
|
236 return m_events.count();
|
Chris@1649
|
237 }
|
Chris@1649
|
238
|
Chris@1649
|
239 int getColumnCount() const override {
|
Chris@618
|
240 return 5;
|
Chris@441
|
241 }
|
Chris@441
|
242
|
Chris@1649
|
243 bool isColumnTimeValue(int column) const override {
|
Chris@1649
|
244 // NB duration is not a "time value" -- that's for columns
|
Chris@1649
|
245 // whose sort ordering is exactly that of the frame time
|
Chris@1649
|
246 return (column < 2);
|
Chris@1649
|
247 }
|
Chris@1649
|
248
|
Chris@1649
|
249 sv_frame_t getFrameForRow(int row) const override {
|
Chris@1649
|
250 if (row < 0 || row >= m_events.count()) {
|
Chris@1649
|
251 return 0;
|
Chris@1649
|
252 }
|
Chris@1649
|
253 Event e = m_events.getEventByIndex(row);
|
Chris@1649
|
254 return e.getFrame();
|
Chris@1649
|
255 }
|
Chris@1649
|
256
|
Chris@1649
|
257 int getRowForFrame(sv_frame_t frame) const override {
|
Chris@1649
|
258 return m_events.getIndexForEvent(Event(frame));
|
Chris@1649
|
259 }
|
Chris@1649
|
260
|
Chris@1649
|
261 QString getHeading(int column) const override {
|
Chris@441
|
262 switch (column) {
|
Chris@441
|
263 case 0: return tr("Time");
|
Chris@441
|
264 case 1: return tr("Frame");
|
Chris@441
|
265 case 2: return tr("Value");
|
Chris@441
|
266 case 3: return tr("Duration");
|
Chris@441
|
267 case 4: return tr("Label");
|
Chris@441
|
268 default: return tr("Unknown");
|
Chris@441
|
269 }
|
Chris@441
|
270 }
|
Chris@441
|
271
|
Chris@1649
|
272 QVariant getData(int row, int column, int role) const override {
|
Chris@1649
|
273
|
Chris@1649
|
274 if (row < 0 || row >= m_events.count()) {
|
Chris@1649
|
275 return QVariant();
|
Chris@441
|
276 }
|
Chris@441
|
277
|
Chris@1649
|
278 Event e = m_events.getEventByIndex(row);
|
Chris@441
|
279
|
Chris@441
|
280 switch (column) {
|
Chris@1649
|
281 case 0: return adaptFrameForRole(e.getFrame(), getSampleRate(), role);
|
Chris@1649
|
282 case 1: return int(e.getFrame());
|
Chris@1649
|
283 case 2: return adaptValueForRole(e.getValue(), getScaleUnits(), role);
|
Chris@1649
|
284 case 3: return int(e.getDuration());
|
Chris@1649
|
285 case 4: return e.getLabel();
|
Chris@441
|
286 default: return QVariant();
|
Chris@441
|
287 }
|
Chris@441
|
288 }
|
Chris@441
|
289
|
Chris@1649
|
290 Command *getSetDataCommand(int row, int column, const QVariant &value, int role) override {
|
Chris@1649
|
291
|
Chris@1649
|
292 if (row < 0 || row >= m_events.count()) return nullptr;
|
Chris@1649
|
293 if (role != Qt::EditRole) return nullptr;
|
Chris@1649
|
294
|
Chris@1649
|
295 Event e0 = m_events.getEventByIndex(row);
|
Chris@1649
|
296 Event e1;
|
Chris@1649
|
297
|
Chris@1649
|
298 switch (column) {
|
Chris@1649
|
299 case 0: e1 = e0.withFrame(sv_frame_t(round(value.toDouble() *
|
Chris@1649
|
300 getSampleRate()))); break;
|
Chris@1649
|
301 case 1: e1 = e0.withFrame(value.toInt()); break;
|
Chris@1649
|
302 case 2: e1 = e0.withValue(float(value.toDouble())); break;
|
Chris@1649
|
303 case 3: e1 = e0.withDuration(value.toInt()); break;
|
Chris@1649
|
304 case 4: e1 = e0.withLabel(value.toString()); break;
|
Chris@441
|
305 }
|
Chris@441
|
306
|
Chris@1649
|
307 ChangeEventsCommand *command =
|
Chris@1649
|
308 new ChangeEventsCommand(this, tr("Edit Data"));
|
Chris@1649
|
309 command->remove(e0);
|
Chris@1649
|
310 command->add(e1);
|
Chris@441
|
311 return command->finish();
|
Chris@441
|
312 }
|
Chris@441
|
313
|
Chris@1580
|
314 SortType getSortType(int column) const override
|
Chris@441
|
315 {
|
Chris@618
|
316 if (column == 4) return SortAlphabetical;
|
Chris@441
|
317 return SortNumeric;
|
Chris@441
|
318 }
|
Chris@441
|
319
|
Chris@1649
|
320
|
Chris@1649
|
321 /**
|
Chris@1649
|
322 * XmlExportable methods.
|
Chris@1649
|
323 */
|
Chris@1649
|
324 void toXml(QTextStream &out,
|
Chris@1649
|
325 QString indent = "",
|
Chris@1649
|
326 QString extraAttributes = "") const override {
|
Chris@1649
|
327
|
Chris@1649
|
328 Model::toXml
|
Chris@1649
|
329 (out,
|
Chris@1649
|
330 indent,
|
Chris@1649
|
331 QString("type=\"sparse\" dimensions=\"3\" resolution=\"%1\" "
|
Chris@1649
|
332 "notifyOnAdd=\"%2\" dataset=\"%3\" subtype=\"%4\" "
|
Chris@1649
|
333 "valueQuantization=\"%5\" minimum=\"%6\" maximum=\"%7\" "
|
Chris@1649
|
334 "%8")
|
Chris@1649
|
335 .arg(m_resolution)
|
Chris@1649
|
336 .arg(m_notifyOnAdd ? "true" : "false")
|
Chris@1649
|
337 .arg(getObjectExportId(&m_events))
|
Chris@1649
|
338 .arg("region")
|
Chris@1649
|
339 .arg(m_valueQuantization)
|
Chris@1649
|
340 .arg(m_valueMinimum)
|
Chris@1649
|
341 .arg(m_valueMaximum)
|
Chris@1649
|
342 .arg(extraAttributes));
|
Chris@1649
|
343
|
Chris@1649
|
344 m_events.toXml(out, indent, QString("dimensions=\"3\""));
|
Chris@442
|
345 }
|
Chris@442
|
346
|
Chris@441
|
347 protected:
|
Chris@1649
|
348 sv_samplerate_t m_sampleRate;
|
Chris@1649
|
349 int m_resolution;
|
Chris@1649
|
350
|
Chris@1649
|
351 float m_valueMinimum;
|
Chris@1649
|
352 float m_valueMaximum;
|
Chris@1649
|
353 bool m_haveExtents;
|
Chris@441
|
354 float m_valueQuantization;
|
Chris@442
|
355 bool m_haveDistinctValues;
|
Chris@1649
|
356 QString m_units;
|
Chris@1649
|
357
|
Chris@1649
|
358 bool m_notifyOnAdd;
|
Chris@1649
|
359 sv_frame_t m_sinceLastNotifyMin;
|
Chris@1649
|
360 sv_frame_t m_sinceLastNotifyMax;
|
Chris@1649
|
361
|
Chris@1649
|
362 EventSeries m_events;
|
Chris@1649
|
363
|
Chris@1649
|
364 int m_completion;
|
Chris@1649
|
365
|
Chris@1649
|
366 mutable QMutex m_mutex;
|
Chris@441
|
367 };
|
Chris@441
|
368
|
Chris@441
|
369 #endif
|