Chris@147
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@147
|
2
|
Chris@147
|
3 /*
|
Chris@147
|
4 Sonic Visualiser
|
Chris@147
|
5 An audio file viewer and annotation editor.
|
Chris@147
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@147
|
7 This file copyright 2006 Chris Cannam.
|
Chris@147
|
8
|
Chris@147
|
9 This program is free software; you can redistribute it and/or
|
Chris@147
|
10 modify it under the terms of the GNU General Public License as
|
Chris@147
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@147
|
12 License, or (at your option) any later version. See the file
|
Chris@147
|
13 COPYING included with this distribution for more information.
|
Chris@147
|
14 */
|
Chris@147
|
15
|
Chris@147
|
16 #ifndef _SPARSE_MODEL_H_
|
Chris@147
|
17 #define _SPARSE_MODEL_H_
|
Chris@147
|
18
|
Chris@150
|
19 #include "Model.h"
|
Chris@420
|
20 #include "TabularModel.h"
|
Chris@147
|
21 #include "base/Command.h"
|
Chris@425
|
22 #include "base/RealTime.h"
|
Chris@147
|
23
|
Chris@147
|
24 #include <iostream>
|
Chris@147
|
25
|
Chris@147
|
26 #include <set>
|
Chris@420
|
27 #include <vector>
|
Chris@420
|
28 #include <algorithm>
|
Chris@420
|
29
|
Chris@425
|
30 #include <cmath>
|
Chris@425
|
31
|
Chris@147
|
32 #include <QMutex>
|
Chris@147
|
33 #include <QTextStream>
|
Chris@147
|
34
|
Chris@147
|
35 /**
|
Chris@147
|
36 * Model containing sparse data (points with some properties). The
|
Chris@147
|
37 * properties depend on the point type.
|
Chris@147
|
38 */
|
Chris@147
|
39
|
Chris@147
|
40 template <typename PointType>
|
Chris@420
|
41 class SparseModel : public Model,
|
Chris@420
|
42 public TabularModel
|
Chris@147
|
43 {
|
Chris@147
|
44 public:
|
Chris@147
|
45 SparseModel(size_t sampleRate, size_t resolution,
|
Chris@147
|
46 bool notifyOnAdd = true);
|
Chris@147
|
47 virtual ~SparseModel() { }
|
Chris@147
|
48
|
Chris@147
|
49 virtual bool isOK() const { return true; }
|
Chris@147
|
50 virtual size_t getStartFrame() const;
|
Chris@147
|
51 virtual size_t getEndFrame() const;
|
Chris@147
|
52 virtual size_t getSampleRate() const { return m_sampleRate; }
|
Chris@147
|
53
|
Chris@147
|
54 virtual Model *clone() const;
|
Chris@147
|
55
|
Chris@147
|
56 // Number of frames of the underlying sample rate that this model
|
Chris@147
|
57 // is capable of resolving to. For example, if m_resolution == 10
|
Chris@147
|
58 // then every point in this model will be at a multiple of 10
|
Chris@147
|
59 // sample frames and should be considered to cover a window ending
|
Chris@147
|
60 // 10 sample frames later.
|
Chris@147
|
61 virtual size_t getResolution() const {
|
Chris@147
|
62 return m_resolution ? m_resolution : 1;
|
Chris@147
|
63 }
|
Chris@147
|
64 virtual void setResolution(size_t resolution);
|
Chris@147
|
65
|
Chris@147
|
66 typedef PointType Point;
|
Chris@147
|
67 typedef std::multiset<PointType,
|
Chris@147
|
68 typename PointType::OrderComparator> PointList;
|
Chris@147
|
69 typedef typename PointList::iterator PointListIterator;
|
Chris@606
|
70 typedef typename PointList::const_iterator PointListConstIterator;
|
Chris@147
|
71
|
Chris@147
|
72 /**
|
Chris@147
|
73 * Return whether the model is empty or not.
|
Chris@147
|
74 */
|
Chris@147
|
75 virtual bool isEmpty() const;
|
Chris@147
|
76
|
Chris@147
|
77 /**
|
Chris@147
|
78 * Get the total number of points in the model.
|
Chris@147
|
79 */
|
Chris@147
|
80 virtual size_t getPointCount() const;
|
Chris@147
|
81
|
Chris@147
|
82 /**
|
Chris@459
|
83 * Get all points.
|
Chris@459
|
84 */
|
Chris@459
|
85 virtual const PointList &getPoints() const;
|
Chris@459
|
86
|
Chris@459
|
87 /**
|
Chris@147
|
88 * Get all of the points in this model between the given
|
Chris@147
|
89 * boundaries (in frames), as well as up to two points before and
|
Chris@147
|
90 * after the boundaries. If you need exact boundaries, check the
|
Chris@147
|
91 * point coordinates in the returned list.
|
Chris@147
|
92 */
|
Chris@147
|
93 virtual PointList getPoints(long start, long end) const;
|
Chris@147
|
94
|
Chris@147
|
95 /**
|
Chris@147
|
96 * Get all points that cover the given frame number, taking the
|
Chris@147
|
97 * resolution of the model into account.
|
Chris@147
|
98 */
|
Chris@147
|
99 virtual PointList getPoints(long frame) const;
|
Chris@147
|
100
|
Chris@147
|
101 /**
|
Chris@147
|
102 * Return all points that share the nearest frame number prior to
|
Chris@147
|
103 * the given one at which there are any points.
|
Chris@147
|
104 */
|
Chris@147
|
105 virtual PointList getPreviousPoints(long frame) const;
|
Chris@147
|
106
|
Chris@147
|
107 /**
|
Chris@147
|
108 * Return all points that share the nearest frame number
|
Chris@147
|
109 * subsequent to the given one at which there are any points.
|
Chris@147
|
110 */
|
Chris@147
|
111 virtual PointList getNextPoints(long frame) const;
|
Chris@147
|
112
|
Chris@147
|
113 /**
|
Chris@147
|
114 * Remove all points.
|
Chris@147
|
115 */
|
Chris@147
|
116 virtual void clear();
|
Chris@147
|
117
|
Chris@147
|
118 /**
|
Chris@147
|
119 * Add a point.
|
Chris@147
|
120 */
|
Chris@147
|
121 virtual void addPoint(const PointType &point);
|
Chris@147
|
122
|
Chris@147
|
123 /**
|
Chris@147
|
124 * Remove a point. Points are not necessarily unique, so this
|
Chris@147
|
125 * function will remove the first point that compares equal to the
|
Chris@147
|
126 * supplied one using Point::Comparator. Other identical points
|
Chris@147
|
127 * may remain in the model.
|
Chris@147
|
128 */
|
Chris@147
|
129 virtual void deletePoint(const PointType &point);
|
Chris@147
|
130
|
Chris@297
|
131 virtual bool isReady(int *completion = 0) const {
|
Chris@297
|
132 bool ready = isOK() && (m_completion == 100);
|
Chris@297
|
133 if (completion) *completion = m_completion;
|
Chris@297
|
134 return ready;
|
Chris@297
|
135 }
|
Chris@297
|
136
|
Chris@333
|
137 virtual void setCompletion(int completion, bool update = true);
|
Chris@147
|
138 virtual int getCompletion() const { return m_completion; }
|
Chris@147
|
139
|
Chris@147
|
140 virtual bool hasTextLabels() const { return m_hasTextLabels; }
|
Chris@147
|
141
|
Chris@345
|
142 QString getTypeName() const { return tr("Sparse"); }
|
Chris@345
|
143
|
Chris@407
|
144 virtual QString getXmlOutputType() const { return "sparse"; }
|
Chris@407
|
145
|
Chris@147
|
146 virtual void toXml(QTextStream &out,
|
Chris@147
|
147 QString indent = "",
|
Chris@147
|
148 QString extraAttributes = "") const;
|
Chris@147
|
149
|
Chris@147
|
150 virtual QString toDelimitedDataString(QString delimiter) const
|
Chris@147
|
151 {
|
Chris@147
|
152 QString s;
|
Chris@606
|
153 for (PointListConstIterator i = m_points.begin(); i != m_points.end(); ++i) {
|
Chris@147
|
154 s += i->toDelimitedDataString(delimiter, m_sampleRate) + "\n";
|
Chris@147
|
155 }
|
Chris@147
|
156 return s;
|
Chris@147
|
157 }
|
Chris@147
|
158
|
Chris@147
|
159 /**
|
Chris@147
|
160 * Command to add a point, with undo.
|
Chris@147
|
161 */
|
Chris@147
|
162 class AddPointCommand : public Command
|
Chris@147
|
163 {
|
Chris@147
|
164 public:
|
Chris@147
|
165 AddPointCommand(SparseModel<PointType> *model,
|
Chris@147
|
166 const PointType &point,
|
Chris@147
|
167 QString name = "") :
|
Chris@147
|
168 m_model(model), m_point(point), m_name(name) { }
|
Chris@147
|
169
|
Chris@147
|
170 virtual QString getName() const {
|
Chris@147
|
171 return (m_name == "" ? tr("Add Point") : m_name);
|
Chris@147
|
172 }
|
Chris@147
|
173
|
Chris@147
|
174 virtual void execute() { m_model->addPoint(m_point); }
|
Chris@147
|
175 virtual void unexecute() { m_model->deletePoint(m_point); }
|
Chris@147
|
176
|
Chris@147
|
177 const PointType &getPoint() const { return m_point; }
|
Chris@147
|
178
|
Chris@147
|
179 private:
|
Chris@147
|
180 SparseModel<PointType> *m_model;
|
Chris@147
|
181 PointType m_point;
|
Chris@147
|
182 QString m_name;
|
Chris@147
|
183 };
|
Chris@147
|
184
|
Chris@147
|
185
|
Chris@147
|
186 /**
|
Chris@147
|
187 * Command to remove a point, with undo.
|
Chris@147
|
188 */
|
Chris@147
|
189 class DeletePointCommand : public Command
|
Chris@147
|
190 {
|
Chris@147
|
191 public:
|
Chris@147
|
192 DeletePointCommand(SparseModel<PointType> *model,
|
Chris@147
|
193 const PointType &point) :
|
Chris@147
|
194 m_model(model), m_point(point) { }
|
Chris@147
|
195
|
Chris@147
|
196 virtual QString getName() const { return tr("Delete Point"); }
|
Chris@147
|
197
|
Chris@147
|
198 virtual void execute() { m_model->deletePoint(m_point); }
|
Chris@147
|
199 virtual void unexecute() { m_model->addPoint(m_point); }
|
Chris@147
|
200
|
Chris@147
|
201 const PointType &getPoint() const { return m_point; }
|
Chris@147
|
202
|
Chris@147
|
203 private:
|
Chris@147
|
204 SparseModel<PointType> *m_model;
|
Chris@147
|
205 PointType m_point;
|
Chris@147
|
206 };
|
Chris@147
|
207
|
Chris@147
|
208
|
Chris@147
|
209 /**
|
Chris@147
|
210 * Command to add or remove a series of points, with undo.
|
Chris@147
|
211 * Consecutive add/remove pairs for the same point are collapsed.
|
Chris@147
|
212 */
|
Chris@147
|
213 class EditCommand : public MacroCommand
|
Chris@147
|
214 {
|
Chris@147
|
215 public:
|
Chris@147
|
216 EditCommand(SparseModel<PointType> *model, QString commandName);
|
Chris@147
|
217
|
Chris@147
|
218 virtual void addPoint(const PointType &point);
|
Chris@147
|
219 virtual void deletePoint(const PointType &point);
|
Chris@147
|
220
|
Chris@147
|
221 /**
|
Chris@147
|
222 * Stack an arbitrary other command in the same sequence.
|
Chris@147
|
223 */
|
Chris@147
|
224 virtual void addCommand(Command *command) { addCommand(command, true); }
|
Chris@147
|
225
|
Chris@147
|
226 /**
|
Chris@387
|
227 * If any points have been added or deleted, return this
|
Chris@387
|
228 * command (so the caller can add it to the command history).
|
Chris@416
|
229 * Otherwise delete the command and return NULL.
|
Chris@147
|
230 */
|
Chris@416
|
231 virtual EditCommand *finish();
|
Chris@147
|
232
|
Chris@147
|
233 protected:
|
Chris@147
|
234 virtual void addCommand(Command *command, bool executeFirst);
|
Chris@147
|
235
|
Chris@147
|
236 SparseModel<PointType> *m_model;
|
Chris@147
|
237 };
|
Chris@147
|
238
|
Chris@147
|
239
|
Chris@147
|
240 /**
|
Chris@147
|
241 * Command to relabel a point.
|
Chris@147
|
242 */
|
Chris@147
|
243 class RelabelCommand : public Command
|
Chris@147
|
244 {
|
Chris@147
|
245 public:
|
Chris@147
|
246 RelabelCommand(SparseModel<PointType> *model,
|
Chris@147
|
247 const PointType &point,
|
Chris@147
|
248 QString newLabel) :
|
Chris@147
|
249 m_model(model), m_oldPoint(point), m_newPoint(point) {
|
Chris@147
|
250 m_newPoint.label = newLabel;
|
Chris@147
|
251 }
|
Chris@147
|
252
|
Chris@147
|
253 virtual QString getName() const { return tr("Re-Label Point"); }
|
Chris@147
|
254
|
Chris@147
|
255 virtual void execute() {
|
Chris@147
|
256 m_model->deletePoint(m_oldPoint);
|
Chris@147
|
257 m_model->addPoint(m_newPoint);
|
Chris@147
|
258 std::swap(m_oldPoint, m_newPoint);
|
Chris@147
|
259 }
|
Chris@147
|
260
|
Chris@147
|
261 virtual void unexecute() { execute(); }
|
Chris@147
|
262
|
Chris@147
|
263 private:
|
Chris@147
|
264 SparseModel<PointType> *m_model;
|
Chris@147
|
265 PointType m_oldPoint;
|
Chris@147
|
266 PointType m_newPoint;
|
Chris@147
|
267 };
|
Chris@147
|
268
|
Chris@420
|
269 /**
|
Chris@420
|
270 * TabularModel methods.
|
Chris@420
|
271 */
|
Chris@420
|
272
|
Chris@420
|
273 virtual int getRowCount() const
|
Chris@420
|
274 {
|
Chris@420
|
275 return m_points.size();
|
Chris@420
|
276 }
|
Chris@420
|
277
|
Chris@420
|
278 virtual long getFrameForRow(int row) const
|
Chris@420
|
279 {
|
Chris@606
|
280 PointListConstIterator i = getPointListIteratorForRow(row);
|
Chris@420
|
281 if (i == m_points.end()) return 0;
|
Chris@420
|
282 return i->frame;
|
Chris@420
|
283 }
|
Chris@420
|
284
|
Chris@420
|
285 virtual int getRowForFrame(long frame) const
|
Chris@420
|
286 {
|
Chris@420
|
287 if (m_rows.empty()) rebuildRowVector();
|
Chris@420
|
288 std::vector<long>::iterator i =
|
Chris@420
|
289 std::lower_bound(m_rows.begin(), m_rows.end(), frame);
|
Chris@432
|
290 int row = std::distance(m_rows.begin(), i);
|
Chris@432
|
291 if (i != m_rows.begin() && (i == m_rows.end() || *i != frame)) {
|
Chris@432
|
292 --row;
|
Chris@432
|
293 }
|
Chris@432
|
294 return row;
|
Chris@420
|
295 }
|
Chris@420
|
296
|
Chris@420
|
297 virtual int getColumnCount() const { return 1; }
|
Chris@425
|
298 virtual QVariant getData(int row, int column, int role) const
|
Chris@425
|
299 {
|
Chris@606
|
300 PointListConstIterator i = getPointListIteratorForRow(row);
|
Chris@425
|
301 if (i == m_points.end()) return QVariant();
|
Chris@425
|
302
|
Chris@425
|
303 switch (column) {
|
Chris@425
|
304 case 0: {
|
Chris@425
|
305 if (role == SortRole) return int(i->frame);
|
Chris@425
|
306 RealTime rt = RealTime::frame2RealTime(i->frame, getSampleRate());
|
Chris@425
|
307 if (role == Qt::EditRole) return rt.toString().c_str();
|
Chris@425
|
308 else return rt.toText().c_str();
|
Chris@425
|
309 }
|
Chris@425
|
310 case 1: return int(i->frame);
|
Chris@425
|
311 }
|
Chris@425
|
312
|
Chris@420
|
313 return QVariant();
|
Chris@420
|
314 }
|
Chris@427
|
315
|
Chris@425
|
316 virtual Command *getSetDataCommand(int row, int column,
|
Chris@425
|
317 const QVariant &value, int role)
|
Chris@425
|
318 {
|
Chris@425
|
319 if (role != Qt::EditRole) return false;
|
Chris@425
|
320 PointListIterator i = getPointListIteratorForRow(row);
|
Chris@425
|
321 if (i == m_points.end()) return false;
|
Chris@425
|
322 EditCommand *command = new EditCommand(this, tr("Edit Data"));
|
Chris@425
|
323
|
Chris@425
|
324 Point point(*i);
|
Chris@425
|
325 command->deletePoint(point);
|
Chris@425
|
326
|
Chris@425
|
327 switch (column) {
|
Chris@425
|
328 case 0: point.frame = lrint(value.toDouble() * getSampleRate()); break;
|
Chris@425
|
329 case 1: point.frame = value.toInt(); break;
|
Chris@425
|
330 }
|
Chris@425
|
331
|
Chris@425
|
332 command->addPoint(point);
|
Chris@425
|
333 return command->finish();
|
Chris@425
|
334 }
|
Chris@425
|
335
|
Chris@427
|
336 virtual Command *getInsertRowCommand(int row)
|
Chris@427
|
337 {
|
Chris@427
|
338 EditCommand *command = new EditCommand(this, tr("Insert Data Point"));
|
Chris@427
|
339 Point point(0);
|
Chris@427
|
340 PointListIterator i = getPointListIteratorForRow(row);
|
Chris@427
|
341 if (i == m_points.end() && i != m_points.begin()) --i;
|
Chris@427
|
342 if (i != m_points.end()) point = *i;
|
Chris@427
|
343 command->addPoint(point);
|
Chris@427
|
344 return command->finish();
|
Chris@427
|
345 }
|
Chris@427
|
346
|
Chris@427
|
347 virtual Command *getRemoveRowCommand(int row)
|
Chris@427
|
348 {
|
Chris@427
|
349 EditCommand *command = new EditCommand(this, tr("Delete Data Point"));
|
Chris@427
|
350 PointListIterator i = getPointListIteratorForRow(row);
|
Chris@427
|
351 if (i == m_points.end()) return 0;
|
Chris@427
|
352 command->deletePoint(*i);
|
Chris@427
|
353 return command->finish();
|
Chris@427
|
354 }
|
Chris@427
|
355
|
Chris@147
|
356 protected:
|
Chris@147
|
357 size_t m_sampleRate;
|
Chris@147
|
358 size_t m_resolution;
|
Chris@147
|
359 bool m_notifyOnAdd;
|
Chris@147
|
360 long m_sinceLastNotifyMin;
|
Chris@147
|
361 long m_sinceLastNotifyMax;
|
Chris@147
|
362 bool m_hasTextLabels;
|
Chris@147
|
363
|
Chris@147
|
364 PointList m_points;
|
Chris@147
|
365 size_t m_pointCount;
|
Chris@147
|
366 mutable QMutex m_mutex;
|
Chris@147
|
367 int m_completion;
|
Chris@420
|
368
|
Chris@420
|
369 void getPointIterators(long frame,
|
Chris@420
|
370 PointListIterator &startItr,
|
Chris@420
|
371 PointListIterator &endItr) const;
|
Chris@420
|
372
|
Chris@420
|
373 // This is only used if the model is called on to act in
|
Chris@420
|
374 // TabularModel mode
|
Chris@420
|
375 mutable std::vector<long> m_rows; // map from row number to frame
|
Chris@420
|
376 void rebuildRowVector() const
|
Chris@420
|
377 {
|
Chris@420
|
378 m_rows.clear();
|
Chris@420
|
379 for (PointListIterator i = m_points.begin(); i != m_points.end(); ++i) {
|
Chris@420
|
380 m_rows.push_back(i->frame);
|
Chris@420
|
381 }
|
Chris@420
|
382 }
|
Chris@420
|
383
|
Chris@420
|
384 PointListIterator getPointListIteratorForRow(int row) const
|
Chris@420
|
385 {
|
Chris@420
|
386 if (m_rows.empty()) rebuildRowVector();
|
Chris@425
|
387 if (row < 0 || row + 1 > int(m_rows.size())) return m_points.end();
|
Chris@420
|
388
|
Chris@420
|
389 size_t frame = m_rows[row];
|
Chris@420
|
390 int indexAtFrame = 0;
|
Chris@420
|
391 int ri = row;
|
Chris@420
|
392 while (ri > 0 && m_rows[ri-1] == m_rows[row]) { --ri; ++indexAtFrame; }
|
Chris@420
|
393 int initialIndexAtFrame = indexAtFrame;
|
Chris@420
|
394
|
Chris@420
|
395 PointListIterator i0, i1;
|
Chris@420
|
396 getPointIterators(frame, i0, i1);
|
Chris@420
|
397 PointListIterator i = i0;
|
Chris@420
|
398
|
Chris@420
|
399 for (i = i0; i != i1; ++i) {
|
Chris@420
|
400 if (indexAtFrame > 0) { --indexAtFrame; continue; }
|
Chris@420
|
401 return i;
|
Chris@420
|
402 }
|
Chris@420
|
403
|
Chris@420
|
404 if (indexAtFrame > 0) {
|
Chris@420
|
405 std::cerr << "WARNING: SparseModel::getPointListIteratorForRow: No iterator available for row " << row << " (frame = " << frame << ", index at frame = " << initialIndexAtFrame << ", leftover index " << indexAtFrame << ")" << std::endl;
|
Chris@420
|
406 }
|
Chris@420
|
407 return i;
|
Chris@420
|
408 }
|
Chris@147
|
409 };
|
Chris@147
|
410
|
Chris@147
|
411
|
Chris@147
|
412 template <typename PointType>
|
Chris@147
|
413 SparseModel<PointType>::SparseModel(size_t sampleRate,
|
Chris@147
|
414 size_t resolution,
|
Chris@147
|
415 bool notifyOnAdd) :
|
Chris@147
|
416 m_sampleRate(sampleRate),
|
Chris@147
|
417 m_resolution(resolution),
|
Chris@147
|
418 m_notifyOnAdd(notifyOnAdd),
|
Chris@147
|
419 m_sinceLastNotifyMin(-1),
|
Chris@147
|
420 m_sinceLastNotifyMax(-1),
|
Chris@147
|
421 m_hasTextLabels(false),
|
Chris@147
|
422 m_pointCount(0),
|
Chris@147
|
423 m_completion(100)
|
Chris@147
|
424 {
|
Chris@147
|
425 }
|
Chris@147
|
426
|
Chris@147
|
427 template <typename PointType>
|
Chris@147
|
428 size_t
|
Chris@147
|
429 SparseModel<PointType>::getStartFrame() const
|
Chris@147
|
430 {
|
Chris@147
|
431 QMutexLocker locker(&m_mutex);
|
Chris@147
|
432 size_t f = 0;
|
Chris@147
|
433 if (!m_points.empty()) {
|
Chris@147
|
434 f = m_points.begin()->frame;
|
Chris@147
|
435 }
|
Chris@147
|
436 return f;
|
Chris@147
|
437 }
|
Chris@147
|
438
|
Chris@147
|
439 template <typename PointType>
|
Chris@147
|
440 size_t
|
Chris@147
|
441 SparseModel<PointType>::getEndFrame() const
|
Chris@147
|
442 {
|
Chris@147
|
443 QMutexLocker locker(&m_mutex);
|
Chris@147
|
444 size_t f = 0;
|
Chris@147
|
445 if (!m_points.empty()) {
|
Chris@147
|
446 PointListIterator i(m_points.end());
|
Chris@147
|
447 f = (--i)->frame;
|
Chris@147
|
448 }
|
Chris@147
|
449 return f;
|
Chris@147
|
450 }
|
Chris@147
|
451
|
Chris@147
|
452 template <typename PointType>
|
Chris@147
|
453 Model *
|
Chris@147
|
454 SparseModel<PointType>::clone() const
|
Chris@147
|
455 {
|
Chris@425
|
456 return 0; //!!! is this ever used?
|
Chris@425
|
457 /*
|
Chris@147
|
458 SparseModel<PointType> *model =
|
Chris@147
|
459 new SparseModel<PointType>(m_sampleRate, m_resolution, m_notifyOnAdd);
|
Chris@147
|
460 model->m_points = m_points;
|
Chris@147
|
461 model->m_pointCount = m_pointCount;
|
Chris@147
|
462 return model;
|
Chris@425
|
463 */
|
Chris@147
|
464 }
|
Chris@147
|
465
|
Chris@147
|
466 template <typename PointType>
|
Chris@147
|
467 bool
|
Chris@147
|
468 SparseModel<PointType>::isEmpty() const
|
Chris@147
|
469 {
|
Chris@147
|
470 return m_pointCount == 0;
|
Chris@147
|
471 }
|
Chris@147
|
472
|
Chris@147
|
473 template <typename PointType>
|
Chris@147
|
474 size_t
|
Chris@147
|
475 SparseModel<PointType>::getPointCount() const
|
Chris@147
|
476 {
|
Chris@147
|
477 return m_pointCount;
|
Chris@147
|
478 }
|
Chris@147
|
479
|
Chris@147
|
480 template <typename PointType>
|
Chris@459
|
481 const typename SparseModel<PointType>::PointList &
|
Chris@459
|
482 SparseModel<PointType>::getPoints() const
|
Chris@459
|
483 {
|
Chris@459
|
484 return m_points;
|
Chris@459
|
485 }
|
Chris@459
|
486
|
Chris@459
|
487 template <typename PointType>
|
Chris@147
|
488 typename SparseModel<PointType>::PointList
|
Chris@147
|
489 SparseModel<PointType>::getPoints(long start, long end) const
|
Chris@147
|
490 {
|
Chris@147
|
491 if (start > end) return PointList();
|
Chris@147
|
492 QMutexLocker locker(&m_mutex);
|
Chris@147
|
493
|
Chris@147
|
494 PointType startPoint(start), endPoint(end);
|
Chris@147
|
495
|
Chris@147
|
496 PointListIterator startItr = m_points.lower_bound(startPoint);
|
Chris@147
|
497 PointListIterator endItr = m_points.upper_bound(endPoint);
|
Chris@147
|
498
|
Chris@147
|
499 if (startItr != m_points.begin()) --startItr;
|
Chris@147
|
500 if (startItr != m_points.begin()) --startItr;
|
Chris@147
|
501 if (endItr != m_points.end()) ++endItr;
|
Chris@147
|
502 if (endItr != m_points.end()) ++endItr;
|
Chris@147
|
503
|
Chris@147
|
504 PointList rv;
|
Chris@147
|
505
|
Chris@147
|
506 for (PointListIterator i = startItr; i != endItr; ++i) {
|
Chris@147
|
507 rv.insert(*i);
|
Chris@147
|
508 }
|
Chris@147
|
509
|
Chris@147
|
510 return rv;
|
Chris@147
|
511 }
|
Chris@147
|
512
|
Chris@147
|
513 template <typename PointType>
|
Chris@147
|
514 typename SparseModel<PointType>::PointList
|
Chris@147
|
515 SparseModel<PointType>::getPoints(long frame) const
|
Chris@147
|
516 {
|
Chris@420
|
517 PointListIterator startItr, endItr;
|
Chris@420
|
518 getPointIterators(frame, startItr, endItr);
|
Chris@147
|
519
|
Chris@147
|
520 PointList rv;
|
Chris@147
|
521
|
Chris@147
|
522 for (PointListIterator i = startItr; i != endItr; ++i) {
|
Chris@147
|
523 rv.insert(*i);
|
Chris@147
|
524 }
|
Chris@147
|
525
|
Chris@147
|
526 return rv;
|
Chris@147
|
527 }
|
Chris@147
|
528
|
Chris@147
|
529 template <typename PointType>
|
Chris@420
|
530 void
|
Chris@420
|
531 SparseModel<PointType>::getPointIterators(long frame,
|
Chris@420
|
532 PointListIterator &startItr,
|
Chris@420
|
533 PointListIterator &endItr) const
|
Chris@420
|
534 {
|
Chris@420
|
535 QMutexLocker locker(&m_mutex);
|
Chris@420
|
536
|
Chris@420
|
537 if (m_resolution == 0) {
|
Chris@420
|
538 startItr = m_points.end();
|
Chris@420
|
539 endItr = m_points.end();
|
Chris@420
|
540 return;
|
Chris@420
|
541 }
|
Chris@420
|
542
|
Chris@420
|
543 long start = (frame / m_resolution) * m_resolution;
|
Chris@420
|
544 long end = start + m_resolution;
|
Chris@420
|
545
|
Chris@420
|
546 PointType startPoint(start), endPoint(end);
|
Chris@420
|
547
|
Chris@420
|
548 startItr = m_points.lower_bound(startPoint);
|
Chris@420
|
549 endItr = m_points.upper_bound(endPoint);
|
Chris@420
|
550 }
|
Chris@420
|
551
|
Chris@420
|
552 template <typename PointType>
|
Chris@147
|
553 typename SparseModel<PointType>::PointList
|
Chris@147
|
554 SparseModel<PointType>::getPreviousPoints(long originFrame) const
|
Chris@147
|
555 {
|
Chris@147
|
556 QMutexLocker locker(&m_mutex);
|
Chris@147
|
557
|
Chris@147
|
558 PointType lookupPoint(originFrame);
|
Chris@147
|
559 PointList rv;
|
Chris@147
|
560
|
Chris@147
|
561 PointListIterator i = m_points.lower_bound(lookupPoint);
|
Chris@147
|
562 if (i == m_points.begin()) return rv;
|
Chris@147
|
563
|
Chris@147
|
564 --i;
|
Chris@147
|
565 long frame = i->frame;
|
Chris@147
|
566 while (i->frame == frame) {
|
Chris@147
|
567 rv.insert(*i);
|
Chris@147
|
568 if (i == m_points.begin()) break;
|
Chris@147
|
569 --i;
|
Chris@147
|
570 }
|
Chris@147
|
571
|
Chris@147
|
572 return rv;
|
Chris@147
|
573 }
|
Chris@147
|
574
|
Chris@147
|
575 template <typename PointType>
|
Chris@147
|
576 typename SparseModel<PointType>::PointList
|
Chris@147
|
577 SparseModel<PointType>::getNextPoints(long originFrame) const
|
Chris@147
|
578 {
|
Chris@147
|
579 QMutexLocker locker(&m_mutex);
|
Chris@147
|
580
|
Chris@147
|
581 PointType lookupPoint(originFrame);
|
Chris@147
|
582 PointList rv;
|
Chris@147
|
583
|
Chris@147
|
584 PointListIterator i = m_points.upper_bound(lookupPoint);
|
Chris@147
|
585 if (i == m_points.end()) return rv;
|
Chris@147
|
586
|
Chris@147
|
587 long frame = i->frame;
|
Chris@147
|
588 while (i != m_points.end() && i->frame == frame) {
|
Chris@147
|
589 rv.insert(*i);
|
Chris@147
|
590 ++i;
|
Chris@147
|
591 }
|
Chris@147
|
592
|
Chris@147
|
593 return rv;
|
Chris@147
|
594 }
|
Chris@147
|
595
|
Chris@147
|
596 template <typename PointType>
|
Chris@147
|
597 void
|
Chris@147
|
598 SparseModel<PointType>::setResolution(size_t resolution)
|
Chris@147
|
599 {
|
Chris@147
|
600 {
|
Chris@147
|
601 QMutexLocker locker(&m_mutex);
|
Chris@147
|
602 m_resolution = resolution;
|
Chris@147
|
603 }
|
Chris@420
|
604 m_rows.clear();
|
Chris@147
|
605 emit modelChanged();
|
Chris@147
|
606 }
|
Chris@147
|
607
|
Chris@147
|
608 template <typename PointType>
|
Chris@147
|
609 void
|
Chris@147
|
610 SparseModel<PointType>::clear()
|
Chris@147
|
611 {
|
Chris@147
|
612 {
|
Chris@147
|
613 QMutexLocker locker(&m_mutex);
|
Chris@147
|
614 m_points.clear();
|
Chris@147
|
615 m_pointCount = 0;
|
Chris@147
|
616 }
|
Chris@420
|
617 m_rows.clear();
|
Chris@147
|
618 emit modelChanged();
|
Chris@147
|
619 }
|
Chris@147
|
620
|
Chris@147
|
621 template <typename PointType>
|
Chris@147
|
622 void
|
Chris@147
|
623 SparseModel<PointType>::addPoint(const PointType &point)
|
Chris@147
|
624 {
|
Chris@147
|
625 {
|
Chris@147
|
626 QMutexLocker locker(&m_mutex);
|
Chris@147
|
627 m_points.insert(point);
|
Chris@147
|
628 m_pointCount++;
|
Chris@338
|
629 if (point.getLabel() != "") m_hasTextLabels = true;
|
Chris@147
|
630 }
|
Chris@147
|
631
|
Chris@147
|
632 // Even though this model is nominally sparse, there may still be
|
Chris@147
|
633 // too many signals going on here (especially as they'll probably
|
Chris@147
|
634 // be queued from one thread to another), which is why we need the
|
Chris@147
|
635 // notifyOnAdd as an option rather than a necessity (the
|
Chris@147
|
636 // alternative is to notify on setCompletion).
|
Chris@147
|
637
|
Chris@147
|
638 if (m_notifyOnAdd) {
|
Chris@420
|
639 m_rows.clear(); //!!! inefficient
|
Chris@147
|
640 emit modelChanged(point.frame, point.frame + m_resolution);
|
Chris@147
|
641 } else {
|
Chris@147
|
642 if (m_sinceLastNotifyMin == -1 ||
|
Chris@147
|
643 point.frame < m_sinceLastNotifyMin) {
|
Chris@147
|
644 m_sinceLastNotifyMin = point.frame;
|
Chris@147
|
645 }
|
Chris@147
|
646 if (m_sinceLastNotifyMax == -1 ||
|
Chris@147
|
647 point.frame > m_sinceLastNotifyMax) {
|
Chris@147
|
648 m_sinceLastNotifyMax = point.frame;
|
Chris@147
|
649 }
|
Chris@147
|
650 }
|
Chris@147
|
651 }
|
Chris@147
|
652
|
Chris@147
|
653 template <typename PointType>
|
Chris@147
|
654 void
|
Chris@147
|
655 SparseModel<PointType>::deletePoint(const PointType &point)
|
Chris@147
|
656 {
|
Chris@147
|
657 {
|
Chris@147
|
658 QMutexLocker locker(&m_mutex);
|
Chris@147
|
659
|
Chris@147
|
660 PointListIterator i = m_points.lower_bound(point);
|
Chris@147
|
661 typename PointType::Comparator comparator;
|
Chris@147
|
662 while (i != m_points.end()) {
|
Chris@147
|
663 if (i->frame > point.frame) break;
|
Chris@147
|
664 if (!comparator(*i, point) && !comparator(point, *i)) {
|
Chris@147
|
665 m_points.erase(i);
|
Chris@147
|
666 m_pointCount--;
|
Chris@147
|
667 break;
|
Chris@147
|
668 }
|
Chris@147
|
669 ++i;
|
Chris@147
|
670 }
|
Chris@147
|
671 }
|
Chris@147
|
672 // std::cout << "SparseOneDimensionalModel: emit modelChanged("
|
Chris@147
|
673 // << point.frame << ")" << std::endl;
|
Chris@420
|
674 m_rows.clear(); //!!! inefficient
|
Chris@147
|
675 emit modelChanged(point.frame, point.frame + m_resolution);
|
Chris@147
|
676 }
|
Chris@147
|
677
|
Chris@147
|
678 template <typename PointType>
|
Chris@147
|
679 void
|
Chris@333
|
680 SparseModel<PointType>::setCompletion(int completion, bool update)
|
Chris@147
|
681 {
|
Chris@301
|
682 // std::cerr << "SparseModel::setCompletion(" << completion << ")" << std::endl;
|
Chris@191
|
683
|
Chris@147
|
684 if (m_completion != completion) {
|
Chris@147
|
685 m_completion = completion;
|
Chris@147
|
686
|
Chris@147
|
687 if (completion == 100) {
|
Chris@147
|
688
|
Chris@297
|
689 if (!m_notifyOnAdd) {
|
Chris@297
|
690 emit completionChanged();
|
Chris@297
|
691 }
|
Chris@297
|
692
|
Chris@147
|
693 m_notifyOnAdd = true; // henceforth
|
Chris@420
|
694 m_rows.clear(); //!!! inefficient
|
Chris@147
|
695 emit modelChanged();
|
Chris@147
|
696
|
Chris@147
|
697 } else if (!m_notifyOnAdd) {
|
Chris@147
|
698
|
Chris@333
|
699 if (update &&
|
Chris@333
|
700 m_sinceLastNotifyMin >= 0 &&
|
Chris@147
|
701 m_sinceLastNotifyMax >= 0) {
|
Chris@420
|
702 m_rows.clear(); //!!! inefficient
|
Chris@147
|
703 emit modelChanged(m_sinceLastNotifyMin, m_sinceLastNotifyMax);
|
Chris@147
|
704 m_sinceLastNotifyMin = m_sinceLastNotifyMax = -1;
|
Chris@147
|
705 } else {
|
Chris@147
|
706 emit completionChanged();
|
Chris@147
|
707 }
|
Chris@147
|
708 } else {
|
Chris@147
|
709 emit completionChanged();
|
Chris@147
|
710 }
|
Chris@147
|
711 }
|
Chris@147
|
712 }
|
Chris@147
|
713
|
Chris@147
|
714 template <typename PointType>
|
Chris@147
|
715 void
|
Chris@147
|
716 SparseModel<PointType>::toXml(QTextStream &out,
|
Chris@147
|
717 QString indent,
|
Chris@147
|
718 QString extraAttributes) const
|
Chris@147
|
719 {
|
Chris@318
|
720 std::cerr << "SparseModel::toXml: extraAttributes = \""
|
Chris@318
|
721 << extraAttributes.toStdString() << std::endl;
|
Chris@318
|
722
|
Chris@407
|
723 QString type = getXmlOutputType();
|
Chris@407
|
724
|
Chris@147
|
725 Model::toXml
|
Chris@147
|
726 (out,
|
Chris@147
|
727 indent,
|
Chris@407
|
728 QString("type=\"%1\" dimensions=\"%2\" resolution=\"%3\" notifyOnAdd=\"%4\" dataset=\"%5\" %6")
|
Chris@407
|
729 .arg(type)
|
Chris@147
|
730 .arg(PointType(0).getDimensions())
|
Chris@147
|
731 .arg(m_resolution)
|
Chris@147
|
732 .arg(m_notifyOnAdd ? "true" : "false")
|
Chris@147
|
733 .arg(getObjectExportId(&m_points))
|
Chris@147
|
734 .arg(extraAttributes));
|
Chris@147
|
735
|
Chris@147
|
736 out << indent;
|
Chris@147
|
737 out << QString("<dataset id=\"%1\" dimensions=\"%2\">\n")
|
Chris@147
|
738 .arg(getObjectExportId(&m_points))
|
Chris@147
|
739 .arg(PointType(0).getDimensions());
|
Chris@147
|
740
|
Chris@147
|
741 for (PointListIterator i = m_points.begin(); i != m_points.end(); ++i) {
|
Chris@314
|
742 i->toXml(out, indent + " ");
|
Chris@147
|
743 }
|
Chris@147
|
744
|
Chris@147
|
745 out << indent;
|
Chris@147
|
746 out << "</dataset>\n";
|
Chris@147
|
747 }
|
Chris@147
|
748
|
Chris@147
|
749 template <typename PointType>
|
Chris@147
|
750 SparseModel<PointType>::EditCommand::EditCommand(SparseModel *model,
|
Chris@147
|
751 QString commandName) :
|
Chris@147
|
752 MacroCommand(commandName),
|
Chris@147
|
753 m_model(model)
|
Chris@147
|
754 {
|
Chris@147
|
755 }
|
Chris@147
|
756
|
Chris@147
|
757 template <typename PointType>
|
Chris@147
|
758 void
|
Chris@147
|
759 SparseModel<PointType>::EditCommand::addPoint(const PointType &point)
|
Chris@147
|
760 {
|
Chris@147
|
761 addCommand(new AddPointCommand(m_model, point), true);
|
Chris@147
|
762 }
|
Chris@147
|
763
|
Chris@147
|
764 template <typename PointType>
|
Chris@147
|
765 void
|
Chris@147
|
766 SparseModel<PointType>::EditCommand::deletePoint(const PointType &point)
|
Chris@147
|
767 {
|
Chris@147
|
768 addCommand(new DeletePointCommand(m_model, point), true);
|
Chris@147
|
769 }
|
Chris@147
|
770
|
Chris@147
|
771 template <typename PointType>
|
Chris@416
|
772 typename SparseModel<PointType>::EditCommand *
|
Chris@147
|
773 SparseModel<PointType>::EditCommand::finish()
|
Chris@147
|
774 {
|
Chris@147
|
775 if (!m_commands.empty()) {
|
Chris@387
|
776 return this;
|
Chris@147
|
777 } else {
|
Chris@147
|
778 delete this;
|
Chris@389
|
779 return 0;
|
Chris@147
|
780 }
|
Chris@147
|
781 }
|
Chris@147
|
782
|
Chris@147
|
783 template <typename PointType>
|
Chris@147
|
784 void
|
Chris@147
|
785 SparseModel<PointType>::EditCommand::addCommand(Command *command,
|
Chris@147
|
786 bool executeFirst)
|
Chris@147
|
787 {
|
Chris@147
|
788 if (executeFirst) command->execute();
|
Chris@147
|
789
|
Chris@147
|
790 if (!m_commands.empty()) {
|
Chris@147
|
791 DeletePointCommand *dpc = dynamic_cast<DeletePointCommand *>(command);
|
Chris@147
|
792 if (dpc) {
|
Chris@147
|
793 AddPointCommand *apc = dynamic_cast<AddPointCommand *>
|
Chris@147
|
794 (m_commands[m_commands.size() - 1]);
|
Chris@147
|
795 typename PointType::Comparator comparator;
|
Chris@147
|
796 if (apc) {
|
Chris@147
|
797 if (!comparator(apc->getPoint(), dpc->getPoint()) &&
|
Chris@147
|
798 !comparator(dpc->getPoint(), apc->getPoint())) {
|
Chris@147
|
799 deleteCommand(apc);
|
Chris@147
|
800 return;
|
Chris@147
|
801 }
|
Chris@147
|
802 }
|
Chris@147
|
803 }
|
Chris@147
|
804 }
|
Chris@147
|
805
|
Chris@147
|
806 MacroCommand::addCommand(command);
|
Chris@147
|
807 }
|
Chris@147
|
808
|
Chris@147
|
809
|
Chris@147
|
810 #endif
|
Chris@147
|
811
|
Chris@147
|
812
|
Chris@147
|
813
|