annotate data/model/SparseOneDimensionalModel.h @ 1659:8bf3a52a1604 single-point

Work on start/end frame stuff to match API description
author Chris Cannam
date Thu, 21 Mar 2019 11:35:30 +0000
parents 5b7b01da430a
children b234d4d011df
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@1659 63 bool isSparse() const { return true; }
Chris@1659 64 bool isOK() const override { return true; }
Chris@420 65
Chris@1659 66 sv_frame_t getStartFrame() const override {
Chris@1659 67 return m_events.getStartFrame();
Chris@1659 68 }
Chris@1659 69 sv_frame_t getEndFrame() const override {
Chris@1659 70 if (m_events.isEmpty()) return 0;
Chris@1659 71 sv_frame_t e = m_events.getEndFrame() + 1;
Chris@1659 72 if (e % m_resolution == 0) return e;
Chris@1659 73 else return (e / m_resolution + 1) * m_resolution;
Chris@1659 74 }
Chris@1659 75
Chris@1658 76 sv_samplerate_t getSampleRate() const override { return m_sampleRate; }
Chris@1658 77 int getResolution() const { return m_resolution; }
Chris@1658 78
Chris@1658 79 bool canPlay() const override { return true; }
Chris@1658 80 QString getDefaultPlayClipId() const override { return "tap"; }
Chris@1658 81
Chris@1658 82 int getCompletion() const { return m_completion; }
Chris@1658 83
Chris@1658 84 void setCompletion(int completion, bool update = true) {
Chris@1658 85
Chris@1658 86 { QMutexLocker locker(&m_mutex);
Chris@1658 87 if (m_completion == completion) return;
Chris@1658 88 m_completion = completion;
Chris@1658 89 }
Chris@1658 90
Chris@1658 91 if (update) {
Chris@1658 92 m_notifier.makeDeferredNotifications();
Chris@1658 93 }
Chris@1658 94
Chris@1658 95 emit completionChanged();
Chris@1658 96
Chris@1658 97 if (completion == 100) {
Chris@1658 98 // henceforth:
Chris@1658 99 m_notifier.switchMode(DeferredNotifier::NOTIFY_ALWAYS);
Chris@1658 100 emit modelChanged();
Chris@1658 101 }
Chris@1658 102 }
Chris@1658 103
Chris@1658 104 /**
Chris@1658 105 * Query methods.
Chris@1658 106 */
Chris@1658 107
Chris@1658 108 int getEventCount() const {
Chris@1658 109 return m_events.count();
Chris@1658 110 }
Chris@1658 111 bool isEmpty() const {
Chris@1658 112 return m_events.isEmpty();
Chris@1658 113 }
Chris@1658 114 bool containsEvent(const Event &e) const {
Chris@1658 115 return m_events.contains(e);
Chris@1658 116 }
Chris@1658 117 EventVector getAllEvents() const {
Chris@1658 118 return m_events.getAllEvents();
Chris@1658 119 }
Chris@1658 120 EventVector getEventsSpanning(sv_frame_t f, sv_frame_t duration) const {
Chris@1658 121 return m_events.getEventsSpanning(f, duration);
Chris@1658 122 }
Chris@1658 123 EventVector getEventsCovering(sv_frame_t f) const {
Chris@1658 124 return m_events.getEventsCovering(f);
Chris@1658 125 }
Chris@1658 126 EventVector getEventsWithin(sv_frame_t f, sv_frame_t duration,
Chris@1658 127 int overspill = 0) const {
Chris@1658 128 return m_events.getEventsWithin(f, duration, overspill);
Chris@1658 129 }
Chris@1658 130 EventVector getEventsStartingWithin(sv_frame_t f, sv_frame_t duration) const {
Chris@1658 131 return m_events.getEventsStartingWithin(f, duration);
Chris@1658 132 }
Chris@1658 133 EventVector getEventsStartingAt(sv_frame_t f) const {
Chris@1658 134 return m_events.getEventsStartingAt(f);
Chris@1658 135 }
Chris@1658 136 bool getNearestEventMatching(sv_frame_t startSearchAt,
Chris@1658 137 std::function<bool(Event)> predicate,
Chris@1658 138 EventSeries::Direction direction,
Chris@1658 139 Event &found) const {
Chris@1658 140 return m_events.getNearestEventMatching
Chris@1658 141 (startSearchAt, predicate, direction, found);
Chris@1658 142 }
Chris@1658 143
Chris@1658 144 /**
Chris@1658 145 * Editing methods.
Chris@1658 146 */
Chris@1658 147 void add(Event e) override {
Chris@1658 148
Chris@1658 149 { QMutexLocker locker(&m_mutex);
Chris@1658 150 m_events.add(e.withoutValue().withoutDuration());
Chris@1658 151 }
Chris@1658 152
Chris@1658 153 m_notifier.update(e.getFrame(), m_resolution);
Chris@1658 154 }
Chris@1658 155
Chris@1658 156 void remove(Event e) override {
Chris@1658 157 { QMutexLocker locker(&m_mutex);
Chris@1658 158 m_events.remove(e);
Chris@1658 159 }
Chris@1658 160 emit modelChangedWithin(e.getFrame(), e.getFrame() + m_resolution);
Chris@1658 161 }
Chris@1658 162
Chris@420 163 /**
Chris@420 164 * TabularModel methods.
Chris@420 165 */
Chris@420 166
Chris@1658 167 int getRowCount() const override {
Chris@1658 168 return m_events.count();
Chris@1658 169 }
Chris@1658 170
Chris@1658 171 int getColumnCount() const override {
Chris@420 172 return 3;
Chris@420 173 }
Chris@420 174
Chris@1658 175 bool isColumnTimeValue(int column) const override {
Chris@1658 176 // NB duration is not a "time value" -- that's for columns
Chris@1658 177 // whose sort ordering is exactly that of the frame time
Chris@1658 178 return (column < 2);
Chris@1658 179 }
Chris@1658 180
Chris@1658 181 sv_frame_t getFrameForRow(int row) const override {
Chris@1658 182 if (row < 0 || row >= m_events.count()) {
Chris@1658 183 return 0;
Chris@1658 184 }
Chris@1658 185 Event e = m_events.getEventByIndex(row);
Chris@1658 186 return e.getFrame();
Chris@1658 187 }
Chris@1658 188
Chris@1658 189 int getRowForFrame(sv_frame_t frame) const override {
Chris@1658 190 return m_events.getIndexForEvent(Event(frame));
Chris@1658 191 }
Chris@1658 192
Chris@1658 193 QString getHeading(int column) const override {
Chris@420 194 switch (column) {
Chris@420 195 case 0: return tr("Time");
Chris@420 196 case 1: return tr("Frame");
Chris@420 197 case 2: return tr("Label");
Chris@420 198 default: return tr("Unknown");
Chris@420 199 }
Chris@420 200 }
Chris@420 201
Chris@1658 202 SortType getSortType(int column) const override {
Chris@1658 203 if (column == 2) return SortAlphabetical;
Chris@1658 204 return SortNumeric;
Chris@1658 205 }
Chris@1658 206
Chris@1658 207 QVariant getData(int row, int column, int role) const override {
Chris@1658 208
Chris@1658 209 if (row < 0 || row >= m_events.count()) {
Chris@1658 210 return QVariant();
Chris@425 211 }
Chris@425 212
Chris@1658 213 Event e = m_events.getEventByIndex(row);
Chris@420 214
Chris@420 215 switch (column) {
Chris@1658 216 case 0: return adaptFrameForRole(e.getFrame(), getSampleRate(), role);
Chris@1658 217 case 1: return int(e.getFrame());
Chris@1658 218 case 2: return e.getLabel();
Chris@420 219 default: return QVariant();
Chris@420 220 }
Chris@420 221 }
Chris@420 222
Chris@1658 223 Command *getSetDataCommand(int row, int column, const QVariant &value, int role) override {
Chris@1658 224 if (row < 0 || row >= m_events.count()) return nullptr;
Chris@1658 225 if (role != Qt::EditRole) return nullptr;
Chris@1658 226
Chris@1658 227 Event e0 = m_events.getEventByIndex(row);
Chris@1658 228 Event e1;
Chris@1658 229
Chris@1658 230 switch (column) {
Chris@1658 231 case 0: e1 = e0.withFrame(sv_frame_t(round(value.toDouble() *
Chris@1658 232 getSampleRate()))); break;
Chris@1658 233 case 1: e1 = e0.withFrame(value.toInt()); break;
Chris@1658 234 case 2: e1 = e0.withLabel(value.toString()); break;
Chris@425 235 }
Chris@425 236
Chris@1658 237 ChangeEventsCommand *command =
Chris@1658 238 new ChangeEventsCommand(this, tr("Edit Data"));
Chris@1658 239 command->remove(e0);
Chris@1658 240 command->add(e1);
Chris@424 241 return command->finish();
Chris@424 242 }
Chris@424 243
Chris@852 244 /**
Chris@852 245 * NoteExportable methods.
Chris@852 246 */
Chris@852 247
Chris@1580 248 NoteList getNotes() const override {
Chris@1643 249 return getNotesStartingWithin(getStartFrame(),
Chris@1643 250 getEndFrame() - getStartFrame());
Chris@852 251 }
Chris@852 252
Chris@1643 253 NoteList getNotesActiveAt(sv_frame_t frame) const override {
Chris@1643 254 return getNotesStartingWithin(frame, 1);
Chris@1643 255 }
Chris@1643 256
Chris@1643 257 NoteList getNotesStartingWithin(sv_frame_t startFrame,
Chris@1643 258 sv_frame_t duration) const override {
Chris@852 259
Chris@852 260 NoteList notes;
Chris@1658 261 EventVector ee = m_events.getEventsStartingWithin(startFrame, duration);
Chris@1658 262 for (const auto &e: ee) {
Chris@1658 263 notes.push_back(e.toNoteData(getSampleRate(), true));
Chris@852 264 }
Chris@852 265 return notes;
Chris@852 266 }
Chris@1658 267
Chris@1658 268 /**
Chris@1658 269 * XmlExportable methods.
Chris@1658 270 */
Chris@1658 271 void toXml(QTextStream &out,
Chris@1658 272 QString indent = "",
Chris@1658 273 QString extraAttributes = "") const override {
Chris@1658 274
Chris@1658 275 Model::toXml
Chris@1658 276 (out,
Chris@1658 277 indent,
Chris@1658 278 QString("type=\"sparse\" dimensions=\"1\" resolution=\"%1\" "
Chris@1658 279 "notifyOnAdd=\"%2\" dataset=\"%3\" %4")
Chris@1658 280 .arg(m_resolution)
Chris@1658 281 .arg("true") // always true after model reaches 100% -
Chris@1658 282 // subsequent events are always notified
Chris@1658 283 .arg(getObjectExportId(&m_events))
Chris@1658 284 .arg(extraAttributes));
Chris@1658 285
Chris@1658 286 m_events.toXml(out, indent, QString("dimensions=\"1\""));
Chris@1658 287 }
Chris@1658 288
Chris@1658 289 protected:
Chris@1658 290 sv_samplerate_t m_sampleRate;
Chris@1658 291 int m_resolution;
Chris@1658 292
Chris@1658 293 DeferredNotifier m_notifier;
Chris@1658 294 int m_completion;
Chris@1658 295
Chris@1658 296 EventSeries m_events;
Chris@1658 297
Chris@1658 298 mutable QMutex m_mutex;
Chris@147 299 };
Chris@147 300
Chris@147 301 #endif
Chris@147 302
Chris@147 303
Chris@147 304