annotate data/model/SparseOneDimensionalModel.h @ 1658:5b7b01da430a single-point

Start updating SparseOneDimensionalModel
author Chris Cannam
date Wed, 20 Mar 2019 16:22:13 +0000
parents 7a23dfe65d66
children 8bf3a52a1604
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
Chris@147 8 This program is free software; you can redistribute it and/or
Chris@147 9 modify it under the terms of the GNU General Public License as
Chris@147 10 published by the Free Software Foundation; either version 2 of the
Chris@147 11 License, or (at your option) any later version. See the file
Chris@147 12 COPYING included with this distribution for more information.
Chris@147 13 */
Chris@147 14
Chris@1581 15 #ifndef SV_SPARSE_ONE_DIMENSIONAL_MODEL_H
Chris@1581 16 #define SV_SPARSE_ONE_DIMENSIONAL_MODEL_H
Chris@147 17
Chris@1658 18 #include "EventCommands.h"
Chris@1658 19 #include "TabularModel.h"
Chris@1658 20 #include "Model.h"
Chris@1658 21 #include "DeferredNotifier.h"
Chris@1658 22
Chris@1615 23 #include "base/NoteData.h"
Chris@1658 24 #include "base/EventSeries.h"
Chris@1643 25 #include "base/NoteExportable.h"
Chris@150 26 #include "base/PlayParameterRepository.h"
Chris@147 27 #include "base/RealTime.h"
Chris@147 28
Chris@1658 29 #include "system/System.h"
Chris@1658 30
Chris@387 31 #include <QStringList>
Chris@387 32
Chris@1658 33 /**
Chris@1658 34 * A model representing a series of time instants with optional labels
Chris@1658 35 * but without values.
Chris@1658 36 */
Chris@1658 37 class SparseOneDimensionalModel : public Model,
Chris@1658 38 public TabularModel,
Chris@1658 39 public EventEditable,
Chris@852 40 public NoteExportable
Chris@147 41 {
Chris@423 42 Q_OBJECT
Chris@423 43
Chris@147 44 public:
Chris@1658 45 SparseOneDimensionalModel(sv_samplerate_t sampleRate,
Chris@1658 46 int resolution,
Chris@1429 47 bool notifyOnAdd = true) :
Chris@1658 48 m_sampleRate(sampleRate),
Chris@1658 49 m_resolution(resolution),
Chris@1658 50 m_notifier(this,
Chris@1658 51 notifyOnAdd ?
Chris@1658 52 DeferredNotifier::NOTIFY_ALWAYS :
Chris@1658 53 DeferredNotifier::NOTIFY_DEFERRED),
Chris@1658 54 m_completion(100) {
Chris@1429 55 PlayParameterRepository::getInstance()->addPlayable(this);
Chris@391 56 }
Chris@391 57
Chris@1658 58 virtual ~SparseOneDimensionalModel() {
Chris@391 59 PlayParameterRepository::getInstance()->removePlayable(this);
Chris@391 60 }
Chris@391 61
Chris@1580 62 QString getTypeName() const override { return tr("Sparse 1-D"); }
Chris@420 63
Chris@1658 64 bool isOK() const override { return true; }
Chris@1658 65 sv_frame_t getStartFrame() const override { return m_events.getStartFrame(); }
Chris@1658 66 sv_frame_t getEndFrame() const override { return m_events.getEndFrame(); }
Chris@1658 67 sv_samplerate_t getSampleRate() const override { return m_sampleRate; }
Chris@1658 68 int getResolution() const { return m_resolution; }
Chris@1658 69
Chris@1658 70 bool canPlay() const override { return true; }
Chris@1658 71 QString getDefaultPlayClipId() const override { return "tap"; }
Chris@1658 72
Chris@1658 73 int getCompletion() const { return m_completion; }
Chris@1658 74
Chris@1658 75 void setCompletion(int completion, bool update = true) {
Chris@1658 76
Chris@1658 77 { QMutexLocker locker(&m_mutex);
Chris@1658 78 if (m_completion == completion) return;
Chris@1658 79 m_completion = completion;
Chris@1658 80 }
Chris@1658 81
Chris@1658 82 if (update) {
Chris@1658 83 m_notifier.makeDeferredNotifications();
Chris@1658 84 }
Chris@1658 85
Chris@1658 86 emit completionChanged();
Chris@1658 87
Chris@1658 88 if (completion == 100) {
Chris@1658 89 // henceforth:
Chris@1658 90 m_notifier.switchMode(DeferredNotifier::NOTIFY_ALWAYS);
Chris@1658 91 emit modelChanged();
Chris@1658 92 }
Chris@1658 93 }
Chris@1658 94
Chris@1658 95 /**
Chris@1658 96 * Query methods.
Chris@1658 97 */
Chris@1658 98
Chris@1658 99 int getEventCount() const {
Chris@1658 100 return m_events.count();
Chris@1658 101 }
Chris@1658 102 bool isEmpty() const {
Chris@1658 103 return m_events.isEmpty();
Chris@1658 104 }
Chris@1658 105 bool containsEvent(const Event &e) const {
Chris@1658 106 return m_events.contains(e);
Chris@1658 107 }
Chris@1658 108 EventVector getAllEvents() const {
Chris@1658 109 return m_events.getAllEvents();
Chris@1658 110 }
Chris@1658 111 EventVector getEventsSpanning(sv_frame_t f, sv_frame_t duration) const {
Chris@1658 112 return m_events.getEventsSpanning(f, duration);
Chris@1658 113 }
Chris@1658 114 EventVector getEventsCovering(sv_frame_t f) const {
Chris@1658 115 return m_events.getEventsCovering(f);
Chris@1658 116 }
Chris@1658 117 EventVector getEventsWithin(sv_frame_t f, sv_frame_t duration,
Chris@1658 118 int overspill = 0) const {
Chris@1658 119 return m_events.getEventsWithin(f, duration, overspill);
Chris@1658 120 }
Chris@1658 121 EventVector getEventsStartingWithin(sv_frame_t f, sv_frame_t duration) const {
Chris@1658 122 return m_events.getEventsStartingWithin(f, duration);
Chris@1658 123 }
Chris@1658 124 EventVector getEventsStartingAt(sv_frame_t f) const {
Chris@1658 125 return m_events.getEventsStartingAt(f);
Chris@1658 126 }
Chris@1658 127 bool getNearestEventMatching(sv_frame_t startSearchAt,
Chris@1658 128 std::function<bool(Event)> predicate,
Chris@1658 129 EventSeries::Direction direction,
Chris@1658 130 Event &found) const {
Chris@1658 131 return m_events.getNearestEventMatching
Chris@1658 132 (startSearchAt, predicate, direction, found);
Chris@1658 133 }
Chris@1658 134
Chris@1658 135 /**
Chris@1658 136 * Editing methods.
Chris@1658 137 */
Chris@1658 138 void add(Event e) override {
Chris@1658 139
Chris@1658 140 { QMutexLocker locker(&m_mutex);
Chris@1658 141 m_events.add(e.withoutValue().withoutDuration());
Chris@1658 142 }
Chris@1658 143
Chris@1658 144 m_notifier.update(e.getFrame(), m_resolution);
Chris@1658 145 }
Chris@1658 146
Chris@1658 147 void remove(Event e) override {
Chris@1658 148 { QMutexLocker locker(&m_mutex);
Chris@1658 149 m_events.remove(e);
Chris@1658 150 }
Chris@1658 151 emit modelChangedWithin(e.getFrame(), e.getFrame() + m_resolution);
Chris@1658 152 }
Chris@1658 153
Chris@420 154 /**
Chris@420 155 * TabularModel methods.
Chris@420 156 */
Chris@420 157
Chris@1658 158 int getRowCount() const override {
Chris@1658 159 return m_events.count();
Chris@1658 160 }
Chris@1658 161
Chris@1658 162 int getColumnCount() const override {
Chris@420 163 return 3;
Chris@420 164 }
Chris@420 165
Chris@1658 166 bool isColumnTimeValue(int column) const override {
Chris@1658 167 // NB duration is not a "time value" -- that's for columns
Chris@1658 168 // whose sort ordering is exactly that of the frame time
Chris@1658 169 return (column < 2);
Chris@1658 170 }
Chris@1658 171
Chris@1658 172 sv_frame_t getFrameForRow(int row) const override {
Chris@1658 173 if (row < 0 || row >= m_events.count()) {
Chris@1658 174 return 0;
Chris@1658 175 }
Chris@1658 176 Event e = m_events.getEventByIndex(row);
Chris@1658 177 return e.getFrame();
Chris@1658 178 }
Chris@1658 179
Chris@1658 180 int getRowForFrame(sv_frame_t frame) const override {
Chris@1658 181 return m_events.getIndexForEvent(Event(frame));
Chris@1658 182 }
Chris@1658 183
Chris@1658 184 QString getHeading(int column) const override {
Chris@420 185 switch (column) {
Chris@420 186 case 0: return tr("Time");
Chris@420 187 case 1: return tr("Frame");
Chris@420 188 case 2: return tr("Label");
Chris@420 189 default: return tr("Unknown");
Chris@420 190 }
Chris@420 191 }
Chris@420 192
Chris@1658 193 SortType getSortType(int column) const override {
Chris@1658 194 if (column == 2) return SortAlphabetical;
Chris@1658 195 return SortNumeric;
Chris@1658 196 }
Chris@1658 197
Chris@1658 198 QVariant getData(int row, int column, int role) const override {
Chris@1658 199
Chris@1658 200 if (row < 0 || row >= m_events.count()) {
Chris@1658 201 return QVariant();
Chris@425 202 }
Chris@425 203
Chris@1658 204 Event e = m_events.getEventByIndex(row);
Chris@420 205
Chris@420 206 switch (column) {
Chris@1658 207 case 0: return adaptFrameForRole(e.getFrame(), getSampleRate(), role);
Chris@1658 208 case 1: return int(e.getFrame());
Chris@1658 209 case 2: return e.getLabel();
Chris@420 210 default: return QVariant();
Chris@420 211 }
Chris@420 212 }
Chris@420 213
Chris@1658 214 Command *getSetDataCommand(int row, int column, const QVariant &value, int role) override {
Chris@1658 215 if (row < 0 || row >= m_events.count()) return nullptr;
Chris@1658 216 if (role != Qt::EditRole) return nullptr;
Chris@1658 217
Chris@1658 218 Event e0 = m_events.getEventByIndex(row);
Chris@1658 219 Event e1;
Chris@1658 220
Chris@1658 221 switch (column) {
Chris@1658 222 case 0: e1 = e0.withFrame(sv_frame_t(round(value.toDouble() *
Chris@1658 223 getSampleRate()))); break;
Chris@1658 224 case 1: e1 = e0.withFrame(value.toInt()); break;
Chris@1658 225 case 2: e1 = e0.withLabel(value.toString()); break;
Chris@425 226 }
Chris@425 227
Chris@1658 228 ChangeEventsCommand *command =
Chris@1658 229 new ChangeEventsCommand(this, tr("Edit Data"));
Chris@1658 230 command->remove(e0);
Chris@1658 231 command->add(e1);
Chris@424 232 return command->finish();
Chris@424 233 }
Chris@424 234
Chris@852 235 /**
Chris@852 236 * NoteExportable methods.
Chris@852 237 */
Chris@852 238
Chris@1580 239 NoteList getNotes() const override {
Chris@1643 240 return getNotesStartingWithin(getStartFrame(),
Chris@1643 241 getEndFrame() - getStartFrame());
Chris@852 242 }
Chris@852 243
Chris@1643 244 NoteList getNotesActiveAt(sv_frame_t frame) const override {
Chris@1643 245 return getNotesStartingWithin(frame, 1);
Chris@1643 246 }
Chris@1643 247
Chris@1643 248 NoteList getNotesStartingWithin(sv_frame_t startFrame,
Chris@1643 249 sv_frame_t duration) const override {
Chris@852 250
Chris@852 251 NoteList notes;
Chris@1658 252 EventVector ee = m_events.getEventsStartingWithin(startFrame, duration);
Chris@1658 253 for (const auto &e: ee) {
Chris@1658 254 notes.push_back(e.toNoteData(getSampleRate(), true));
Chris@852 255 }
Chris@852 256 return notes;
Chris@852 257 }
Chris@1658 258
Chris@1658 259 /**
Chris@1658 260 * XmlExportable methods.
Chris@1658 261 */
Chris@1658 262 void toXml(QTextStream &out,
Chris@1658 263 QString indent = "",
Chris@1658 264 QString extraAttributes = "") const override {
Chris@1658 265
Chris@1658 266 Model::toXml
Chris@1658 267 (out,
Chris@1658 268 indent,
Chris@1658 269 QString("type=\"sparse\" dimensions=\"1\" resolution=\"%1\" "
Chris@1658 270 "notifyOnAdd=\"%2\" dataset=\"%3\" %4")
Chris@1658 271 .arg(m_resolution)
Chris@1658 272 .arg("true") // always true after model reaches 100% -
Chris@1658 273 // subsequent events are always notified
Chris@1658 274 .arg(getObjectExportId(&m_events))
Chris@1658 275 .arg(extraAttributes));
Chris@1658 276
Chris@1658 277 m_events.toXml(out, indent, QString("dimensions=\"1\""));
Chris@1658 278 }
Chris@1658 279
Chris@1658 280 protected:
Chris@1658 281 sv_samplerate_t m_sampleRate;
Chris@1658 282 int m_resolution;
Chris@1658 283
Chris@1658 284 DeferredNotifier m_notifier;
Chris@1658 285 int m_completion;
Chris@1658 286
Chris@1658 287 EventSeries m_events;
Chris@1658 288
Chris@1658 289 mutable QMutex m_mutex;
Chris@147 290 };
Chris@147 291
Chris@147 292 #endif
Chris@147 293
Chris@147 294
Chris@147 295