Chris@407
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@407
|
2
|
Chris@407
|
3 /*
|
Chris@407
|
4 Sonic Visualiser
|
Chris@407
|
5 An audio file viewer and annotation editor.
|
Chris@407
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@407
|
7 This file copyright 2007 QMUL.
|
Chris@407
|
8
|
Chris@407
|
9 This program is free software; you can redistribute it and/or
|
Chris@407
|
10 modify it under the terms of the GNU General Public License as
|
Chris@407
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@407
|
12 License, or (at your option) any later version. See the file
|
Chris@407
|
13 COPYING included with this distribution for more information.
|
Chris@407
|
14 */
|
Chris@407
|
15
|
Chris@1581
|
16 #ifndef SV_PATH_MODEL_H
|
Chris@1581
|
17 #define SV_PATH_MODEL_H
|
Chris@407
|
18
|
Chris@407
|
19 #include "Model.h"
|
Chris@1662
|
20 #include "DeferredNotifier.h"
|
Chris@407
|
21 #include "base/RealTime.h"
|
Chris@1040
|
22 #include "base/BaseTypes.h"
|
Chris@407
|
23
|
Chris@1662
|
24 #include "base/XmlExportable.h"
|
Chris@1662
|
25 #include "base/RealTime.h"
|
Chris@1662
|
26
|
Chris@407
|
27 #include <QStringList>
|
Chris@1662
|
28 #include <set>
|
Chris@407
|
29
|
Chris@407
|
30 struct PathPoint
|
Chris@407
|
31 {
|
Chris@1662
|
32 PathPoint(sv_frame_t _frame) :
|
Chris@1662
|
33 frame(_frame), mapframe(_frame) { }
|
Chris@1040
|
34 PathPoint(sv_frame_t _frame, sv_frame_t _mapframe) :
|
Chris@407
|
35 frame(_frame), mapframe(_mapframe) { }
|
Chris@407
|
36
|
Chris@1040
|
37 sv_frame_t frame;
|
Chris@1040
|
38 sv_frame_t mapframe;
|
Chris@407
|
39
|
Chris@407
|
40 void toXml(QTextStream &stream, QString indent = "",
|
Chris@407
|
41 QString extraAttributes = "") const {
|
Chris@407
|
42 stream << QString("%1<point frame=\"%2\" mapframe=\"%3\" %4/>\n")
|
Chris@407
|
43 .arg(indent).arg(frame).arg(mapframe).arg(extraAttributes);
|
Chris@407
|
44 }
|
Chris@407
|
45
|
Chris@1060
|
46 QString toDelimitedDataString(QString delimiter, DataExportOptions,
|
Chris@1040
|
47 sv_samplerate_t sampleRate) const {
|
Chris@407
|
48 QStringList list;
|
Chris@407
|
49 list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
|
Chris@407
|
50 list << QString("%1").arg(mapframe);
|
Chris@407
|
51 return list.join(delimiter);
|
Chris@407
|
52 }
|
Chris@407
|
53
|
Chris@1662
|
54 bool operator<(const PathPoint &p2) const {
|
Chris@1662
|
55 if (frame != p2.frame) return frame < p2.frame;
|
Chris@1662
|
56 return mapframe < p2.mapframe;
|
Chris@1662
|
57 }
|
Chris@407
|
58 };
|
Chris@407
|
59
|
Chris@1662
|
60 class PathModel : public Model
|
Chris@407
|
61 {
|
Chris@407
|
62 public:
|
Chris@1662
|
63 typedef std::set<PathPoint> PointList;
|
Chris@407
|
64
|
Chris@1662
|
65 PathModel(sv_samplerate_t sampleRate,
|
Chris@1662
|
66 int resolution,
|
Chris@1662
|
67 bool notifyOnAdd = true) :
|
Chris@1662
|
68 m_sampleRate(sampleRate),
|
Chris@1662
|
69 m_resolution(resolution),
|
Chris@1662
|
70 m_notifier(this,
|
Chris@1662
|
71 notifyOnAdd ?
|
Chris@1662
|
72 DeferredNotifier::NOTIFY_ALWAYS :
|
Chris@1662
|
73 DeferredNotifier::NOTIFY_DEFERRED),
|
Chris@1662
|
74 m_completion(100),
|
Chris@1662
|
75 m_start(0),
|
Chris@1662
|
76 m_end(0) {
|
Chris@1662
|
77 }
|
Chris@1662
|
78
|
Chris@1667
|
79 QString getTypeName() const override { return tr("Path"); }
|
Chris@1662
|
80 bool isSparse() const { return true; }
|
Chris@1662
|
81 bool isOK() const override { return true; }
|
Chris@1662
|
82
|
Chris@1662
|
83 sv_frame_t getStartFrame() const override {
|
Chris@1662
|
84 return m_start;
|
Chris@1662
|
85 }
|
Chris@1662
|
86 sv_frame_t getEndFrame() const override {
|
Chris@1662
|
87 return m_end;
|
Chris@1662
|
88 }
|
Chris@1662
|
89
|
Chris@1662
|
90 sv_samplerate_t getSampleRate() const override { return m_sampleRate; }
|
Chris@1662
|
91 int getResolution() const { return m_resolution; }
|
Chris@1662
|
92
|
Chris@1662
|
93 int getCompletion() const { return m_completion; }
|
Chris@1662
|
94
|
Chris@1662
|
95 void setCompletion(int completion, bool update = true) {
|
Chris@1662
|
96
|
Chris@1662
|
97 { QMutexLocker locker(&m_mutex);
|
Chris@1662
|
98 if (m_completion == completion) return;
|
Chris@1662
|
99 m_completion = completion;
|
Chris@1662
|
100 }
|
Chris@1662
|
101
|
Chris@1662
|
102 if (update) {
|
Chris@1662
|
103 m_notifier.makeDeferredNotifications();
|
Chris@1662
|
104 }
|
Chris@1662
|
105
|
Chris@1662
|
106 emit completionChanged();
|
Chris@1662
|
107
|
Chris@1662
|
108 if (completion == 100) {
|
Chris@1662
|
109 // henceforth:
|
Chris@1662
|
110 m_notifier.switchMode(DeferredNotifier::NOTIFY_ALWAYS);
|
Chris@1662
|
111 emit modelChanged();
|
Chris@1662
|
112 }
|
Chris@407
|
113 }
|
Chris@425
|
114
|
Chris@425
|
115 /**
|
Chris@1662
|
116 * Query methods.
|
Chris@425
|
117 */
|
Chris@1662
|
118 const PointList &getPoints() const {
|
Chris@1662
|
119 return m_points;
|
Chris@1662
|
120 }
|
Chris@425
|
121
|
Chris@1662
|
122 /**
|
Chris@1662
|
123 * Editing methods.
|
Chris@1662
|
124 */
|
Chris@1662
|
125 void add(PathPoint p) {
|
Chris@1662
|
126
|
Chris@1662
|
127 { QMutexLocker locker(&m_mutex);
|
Chris@1662
|
128 m_points.insert(p);
|
Chris@1662
|
129
|
Chris@1662
|
130 if (m_start == m_end) {
|
Chris@1662
|
131 m_start = p.frame;
|
Chris@1662
|
132 m_end = m_start + m_resolution;
|
Chris@1662
|
133 } else {
|
Chris@1662
|
134 if (p.frame < m_start) {
|
Chris@1662
|
135 m_start = p.frame;
|
Chris@1662
|
136 }
|
Chris@1662
|
137 if (p.frame + m_resolution > m_end) {
|
Chris@1662
|
138 m_end = p.frame + m_resolution;
|
Chris@1662
|
139 }
|
Chris@1662
|
140 }
|
Chris@1662
|
141 }
|
Chris@1662
|
142
|
Chris@1662
|
143 m_notifier.update(p.frame, m_resolution);
|
Chris@1662
|
144 }
|
Chris@1662
|
145
|
Chris@1662
|
146 void remove(PathPoint p) {
|
Chris@1662
|
147 { QMutexLocker locker(&m_mutex);
|
Chris@1662
|
148 m_points.erase(p);
|
Chris@1662
|
149 }
|
Chris@1662
|
150
|
Chris@1662
|
151 emit modelChangedWithin(p.frame, p.frame + m_resolution);
|
Chris@1662
|
152 }
|
Chris@1662
|
153
|
Chris@1662
|
154 void clear() {
|
Chris@1662
|
155 { QMutexLocker locker(&m_mutex);
|
Chris@1662
|
156 m_start = m_end = 0;
|
Chris@1662
|
157 m_points.clear();
|
Chris@1662
|
158 }
|
Chris@1662
|
159 }
|
Chris@1662
|
160
|
Chris@1662
|
161 /**
|
Chris@1662
|
162 * XmlExportable methods.
|
Chris@1662
|
163 */
|
Chris@1662
|
164 void toXml(QTextStream &out,
|
Chris@1662
|
165 QString indent = "",
|
Chris@1662
|
166 QString extraAttributes = "") const override {
|
Chris@1662
|
167
|
Chris@1662
|
168 Model::toXml
|
Chris@1662
|
169 (out,
|
Chris@1662
|
170 indent,
|
Chris@1662
|
171 QString("type=\"sparse\" dimensions=\"2\" resolution=\"%1\" "
|
Chris@1662
|
172 "notifyOnAdd=\"%2\" dataset=\"%3\" subtype=\"path\" %4")
|
Chris@1662
|
173 .arg(m_resolution)
|
Chris@1662
|
174 .arg("true") // always true after model reaches 100% -
|
Chris@1662
|
175 // subsequent points are always notified
|
Chris@1662
|
176 .arg(getObjectExportId(&m_points))
|
Chris@1662
|
177 .arg(extraAttributes));
|
Chris@1662
|
178
|
Chris@1662
|
179
|
Chris@1662
|
180 }
|
Chris@1662
|
181
|
Chris@1662
|
182 protected:
|
Chris@1662
|
183 sv_samplerate_t m_sampleRate;
|
Chris@1662
|
184 int m_resolution;
|
Chris@1662
|
185
|
Chris@1662
|
186 DeferredNotifier m_notifier;
|
Chris@1662
|
187 int m_completion;
|
Chris@1662
|
188
|
Chris@1662
|
189 sv_frame_t m_start;
|
Chris@1662
|
190 sv_frame_t m_end;
|
Chris@1662
|
191 PointList m_points;
|
Chris@1662
|
192
|
Chris@1662
|
193 mutable QMutex m_mutex;
|
Chris@407
|
194 };
|
Chris@407
|
195
|
Chris@407
|
196
|
Chris@407
|
197 #endif
|