annotate data/model/SparseModel.h @ 1064:c9fdd9921146 tonioni

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