annotate data/model/SparseModel.h @ 1669:f9b6e99e0520 single-point

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