annotate data/model/SparseOneDimensionalModel.h @ 1649:1cc9a0d4b1b6 single-point

Update RegionModel following NoteModel, er, model. They have quite a bit in common that we should now pull out some of
author Chris Cannam
date Fri, 15 Mar 2019 14:23:50 +0000
parents 7a23dfe65d66
children 5b7b01da430a
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@1581 16 #ifndef SV_SPARSE_ONE_DIMENSIONAL_MODEL_H
Chris@1581 17 #define SV_SPARSE_ONE_DIMENSIONAL_MODEL_H
Chris@147 18
Chris@147 19 #include "SparseModel.h"
Chris@1615 20 #include "base/NoteData.h"
Chris@1643 21 #include "base/NoteExportable.h"
Chris@150 22 #include "base/PlayParameterRepository.h"
Chris@147 23 #include "base/RealTime.h"
Chris@147 24
Chris@387 25 #include <QStringList>
Chris@387 26
Chris@147 27 struct OneDimensionalPoint
Chris@147 28 {
Chris@147 29 public:
Chris@1038 30 OneDimensionalPoint(sv_frame_t _frame) : frame(_frame) { }
Chris@1038 31 OneDimensionalPoint(sv_frame_t _frame, QString _label) : frame(_frame), label(_label) { }
Chris@147 32
Chris@147 33 int getDimensions() const { return 1; }
Chris@147 34
Chris@1038 35 sv_frame_t frame;
Chris@147 36 QString label;
Chris@338 37
Chris@338 38 QString getLabel() const { return label; }
Chris@338 39
Chris@314 40 void toXml(QTextStream &stream,
Chris@314 41 QString indent = "",
Chris@314 42 QString extraAttributes = "") const
Chris@147 43 {
Chris@314 44 stream << QString("%1<point frame=\"%2\" label=\"%3\" %4/>\n")
Chris@1429 45 .arg(indent).arg(frame).arg(XmlExportable::encodeEntities(label))
Chris@603 46 .arg(extraAttributes);
Chris@147 47 }
Chris@147 48
Chris@1060 49 QString toDelimitedDataString(QString delimiter, DataExportOptions, sv_samplerate_t sampleRate) const
Chris@147 50 {
Chris@147 51 QStringList list;
Chris@147 52 list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
Chris@318 53 if (label != "") list << label;
Chris@147 54 return list.join(delimiter);
Chris@147 55 }
Chris@147 56
Chris@147 57 struct Comparator {
Chris@1429 58 bool operator()(const OneDimensionalPoint &p1,
Chris@1429 59 const OneDimensionalPoint &p2) const {
Chris@1429 60 if (p1.frame != p2.frame) return p1.frame < p2.frame;
Chris@1429 61 return p1.label < p2.label;
Chris@1429 62 }
Chris@147 63 };
Chris@147 64
Chris@147 65 struct OrderComparator {
Chris@1429 66 bool operator()(const OneDimensionalPoint &p1,
Chris@1429 67 const OneDimensionalPoint &p2) const {
Chris@1429 68 return p1.frame < p2.frame;
Chris@1429 69 }
Chris@147 70 };
Chris@1609 71
Chris@1609 72 bool operator==(const OneDimensionalPoint &p) const {
Chris@1609 73 return (frame == p.frame && label == p.label);
Chris@1609 74 }
Chris@147 75 };
Chris@147 76
Chris@147 77
Chris@852 78 class SparseOneDimensionalModel : public SparseModel<OneDimensionalPoint>,
Chris@852 79 public NoteExportable
Chris@147 80 {
Chris@423 81 Q_OBJECT
Chris@423 82
Chris@147 83 public:
Chris@1040 84 SparseOneDimensionalModel(sv_samplerate_t sampleRate, int resolution,
Chris@1429 85 bool notifyOnAdd = true) :
Chris@1429 86 SparseModel<OneDimensionalPoint>(sampleRate, resolution, notifyOnAdd)
Chris@147 87 {
Chris@1429 88 PlayParameterRepository::getInstance()->addPlayable(this);
Chris@391 89 }
Chris@391 90
Chris@391 91 virtual ~SparseOneDimensionalModel()
Chris@391 92 {
Chris@391 93 PlayParameterRepository::getInstance()->removePlayable(this);
Chris@391 94 }
Chris@391 95
Chris@1580 96 bool canPlay() const override { return true; }
Chris@391 97
Chris@1580 98 QString getDefaultPlayClipId() const override
Chris@391 99 {
Chris@864 100 return "tap";
Chris@147 101 }
Chris@147 102
Chris@420 103 int getIndexOf(const Point &point)
Chris@420 104 {
Chris@1429 105 // slow
Chris@1429 106 int i = 0;
Chris@1429 107 Point::Comparator comparator;
Chris@1429 108 for (PointList::const_iterator j = m_points.begin();
Chris@1429 109 j != m_points.end(); ++j, ++i) {
Chris@1429 110 if (!comparator(*j, point) && !comparator(point, *j)) return i;
Chris@1429 111 }
Chris@1429 112 return -1;
Chris@147 113 }
Chris@345 114
Chris@1580 115 QString getTypeName() const override { return tr("Sparse 1-D"); }
Chris@420 116
Chris@420 117 /**
Chris@420 118 * TabularModel methods.
Chris@420 119 */
Chris@420 120
Chris@1580 121 int getColumnCount() const override
Chris@420 122 {
Chris@420 123 return 3;
Chris@420 124 }
Chris@420 125
Chris@1580 126 QString getHeading(int column) const override
Chris@420 127 {
Chris@420 128 switch (column) {
Chris@420 129 case 0: return tr("Time");
Chris@420 130 case 1: return tr("Frame");
Chris@420 131 case 2: return tr("Label");
Chris@420 132 default: return tr("Unknown");
Chris@420 133 }
Chris@420 134 }
Chris@420 135
Chris@1580 136 QVariant getData(int row, int column, int role) const override
Chris@420 137 {
Chris@425 138 if (column < 2) {
Chris@425 139 return SparseModel<OneDimensionalPoint>::getData
Chris@425 140 (row, column, role);
Chris@425 141 }
Chris@425 142
Chris@606 143 PointListConstIterator i = getPointListIteratorForRow(row);
Chris@420 144 if (i == m_points.end()) return QVariant();
Chris@420 145
Chris@420 146 switch (column) {
Chris@422 147 case 2: return i->label;
Chris@420 148 default: return QVariant();
Chris@420 149 }
Chris@420 150 }
Chris@420 151
Chris@1580 152 Command *getSetDataCommand(int row, int column, const QVariant &value, int role) override
Chris@424 153 {
Chris@425 154 if (column < 2) {
Chris@425 155 return SparseModel<OneDimensionalPoint>::getSetDataCommand
Chris@425 156 (row, column, value, role);
Chris@425 157 }
Chris@425 158
Chris@740 159 if (role != Qt::EditRole) return 0;
Chris@606 160 PointListConstIterator i = getPointListIteratorForRow(row);
Chris@740 161 if (i == m_points.end()) return 0;
Chris@424 162 EditCommand *command = new EditCommand(this, tr("Edit Data"));
Chris@424 163
Chris@424 164 Point point(*i);
Chris@424 165 command->deletePoint(point);
Chris@424 166
Chris@424 167 switch (column) {
Chris@424 168 case 2: point.label = value.toString(); break;
Chris@424 169 }
Chris@424 170
Chris@424 171 command->addPoint(point);
Chris@424 172 return command->finish();
Chris@424 173 }
Chris@424 174
Chris@427 175
Chris@1580 176 bool isColumnTimeValue(int column) const override
Chris@420 177 {
Chris@420 178 return (column < 2);
Chris@420 179 }
Chris@422 180
Chris@1580 181 SortType getSortType(int column) const override
Chris@422 182 {
Chris@422 183 if (column == 2) return SortAlphabetical;
Chris@422 184 return SortNumeric;
Chris@422 185 }
Chris@852 186
Chris@852 187 /**
Chris@852 188 * NoteExportable methods.
Chris@852 189 */
Chris@852 190
Chris@1580 191 NoteList getNotes() const override {
Chris@1643 192 return getNotesStartingWithin(getStartFrame(),
Chris@1643 193 getEndFrame() - getStartFrame());
Chris@852 194 }
Chris@852 195
Chris@1643 196 NoteList getNotesActiveAt(sv_frame_t frame) const override {
Chris@1643 197 return getNotesStartingWithin(frame, 1);
Chris@1643 198 }
Chris@1643 199
Chris@1643 200 NoteList getNotesStartingWithin(sv_frame_t startFrame,
Chris@1643 201 sv_frame_t duration) const override {
Chris@852 202
Chris@1643 203 PointList points = getPoints(startFrame, startFrame + duration);
Chris@852 204 NoteList notes;
Chris@852 205
Chris@1429 206 for (PointList::iterator pli =
Chris@1429 207 points.begin(); pli != points.end(); ++pli) {
Chris@852 208
Chris@852 209 notes.push_back
Chris@852 210 (NoteData(pli->frame,
Chris@1040 211 sv_frame_t(getSampleRate() / 6), // arbitrary short duration
Chris@852 212 64, // default pitch
Chris@852 213 100)); // default velocity
Chris@852 214 }
Chris@852 215
Chris@852 216 return notes;
Chris@852 217 }
Chris@147 218 };
Chris@147 219
Chris@147 220 #endif
Chris@147 221
Chris@147 222
Chris@147 223