annotate data/model/PathModel.h @ 1735:d91ff235e69d by-id

Some messing with Model and AlignmentModel
author Chris Cannam
date Tue, 25 Jun 2019 15:29:34 +0100
parents 78fe29adfd16
children
rev   line source
Chris@407 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@407 2
Chris@407 3 /*
Chris@407 4 Sonic Visualiser
Chris@407 5 An audio file viewer and annotation editor.
Chris@407 6 Centre for Digital Music, Queen Mary, University of London.
Chris@407 7 This file copyright 2007 QMUL.
Chris@407 8
Chris@407 9 This program is free software; you can redistribute it and/or
Chris@407 10 modify it under the terms of the GNU General Public License as
Chris@407 11 published by the Free Software Foundation; either version 2 of the
Chris@407 12 License, or (at your option) any later version. See the file
Chris@407 13 COPYING included with this distribution for more information.
Chris@407 14 */
Chris@407 15
Chris@1581 16 #ifndef SV_PATH_MODEL_H
Chris@1581 17 #define SV_PATH_MODEL_H
Chris@407 18
Chris@407 19 #include "Model.h"
Chris@1662 20 #include "DeferredNotifier.h"
Chris@407 21 #include "base/RealTime.h"
Chris@1040 22 #include "base/BaseTypes.h"
Chris@407 23
Chris@1662 24 #include "base/XmlExportable.h"
Chris@1662 25 #include "base/RealTime.h"
Chris@1662 26
Chris@407 27 #include <QStringList>
Chris@1662 28 #include <set>
Chris@407 29
Chris@407 30 struct PathPoint
Chris@407 31 {
Chris@1662 32 PathPoint(sv_frame_t _frame) :
Chris@1662 33 frame(_frame), mapframe(_frame) { }
Chris@1040 34 PathPoint(sv_frame_t _frame, sv_frame_t _mapframe) :
Chris@407 35 frame(_frame), mapframe(_mapframe) { }
Chris@407 36
Chris@1040 37 sv_frame_t frame;
Chris@1040 38 sv_frame_t mapframe;
Chris@407 39
Chris@407 40 void toXml(QTextStream &stream, QString indent = "",
Chris@407 41 QString extraAttributes = "") const {
Chris@407 42 stream << QString("%1<point frame=\"%2\" mapframe=\"%3\" %4/>\n")
Chris@407 43 .arg(indent).arg(frame).arg(mapframe).arg(extraAttributes);
Chris@407 44 }
Chris@407 45
Chris@1060 46 QString toDelimitedDataString(QString delimiter, DataExportOptions,
Chris@1040 47 sv_samplerate_t sampleRate) const {
Chris@407 48 QStringList list;
Chris@407 49 list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
Chris@407 50 list << QString("%1").arg(mapframe);
Chris@407 51 return list.join(delimiter);
Chris@407 52 }
Chris@407 53
Chris@1662 54 bool operator<(const PathPoint &p2) const {
Chris@1662 55 if (frame != p2.frame) return frame < p2.frame;
Chris@1662 56 return mapframe < p2.mapframe;
Chris@1662 57 }
Chris@407 58 };
Chris@407 59
Chris@1735 60 //!!! pretty sure there is no good reason for this to be a model any
Chris@1735 61 //!!! more - it used to use implementation inheritance from
Chris@1735 62 //!!! SparseModel but that's no longer a thing. Should just be a
Chris@1735 63 //!!! simple container now, to be passed around by value/reference
Chris@1735 64 //!!! rather than on heap
Chris@1735 65
Chris@1662 66 class PathModel : public Model
Chris@407 67 {
Chris@407 68 public:
Chris@1662 69 typedef std::set<PathPoint> PointList;
Chris@407 70
Chris@1662 71 PathModel(sv_samplerate_t sampleRate,
Chris@1662 72 int resolution,
Chris@1662 73 bool notifyOnAdd = true) :
Chris@1662 74 m_sampleRate(sampleRate),
Chris@1662 75 m_resolution(resolution),
Chris@1662 76 m_notifier(this,
Chris@1662 77 notifyOnAdd ?
Chris@1662 78 DeferredNotifier::NOTIFY_ALWAYS :
Chris@1662 79 DeferredNotifier::NOTIFY_DEFERRED),
Chris@1662 80 m_completion(100),
Chris@1662 81 m_start(0),
Chris@1662 82 m_end(0) {
Chris@1662 83 }
Chris@1662 84
Chris@1667 85 QString getTypeName() const override { return tr("Path"); }
cannam@1695 86 bool isSparse() const override { return true; }
Chris@1662 87 bool isOK() const override { return true; }
Chris@1662 88
Chris@1662 89 sv_frame_t getStartFrame() const override {
Chris@1662 90 return m_start;
Chris@1662 91 }
Chris@1725 92 sv_frame_t getTrueEndFrame() const override {
Chris@1662 93 return m_end;
Chris@1662 94 }
Chris@1662 95
Chris@1662 96 sv_samplerate_t getSampleRate() const override { return m_sampleRate; }
Chris@1662 97 int getResolution() const { return m_resolution; }
Chris@1662 98
cannam@1695 99 int getCompletion() const override { return m_completion; }
Chris@1662 100
Chris@1662 101 void setCompletion(int completion, bool update = true) {
Chris@1662 102
Chris@1662 103 { QMutexLocker locker(&m_mutex);
Chris@1662 104 if (m_completion == completion) return;
Chris@1662 105 m_completion = completion;
Chris@1662 106 }
Chris@1662 107
Chris@1662 108 if (update) {
Chris@1662 109 m_notifier.makeDeferredNotifications();
Chris@1662 110 }
Chris@1662 111
Chris@1662 112 emit completionChanged();
Chris@1662 113
Chris@1662 114 if (completion == 100) {
Chris@1662 115 // henceforth:
Chris@1662 116 m_notifier.switchMode(DeferredNotifier::NOTIFY_ALWAYS);
Chris@1662 117 emit modelChanged();
Chris@1662 118 }
Chris@407 119 }
Chris@425 120
Chris@425 121 /**
Chris@1662 122 * Query methods.
Chris@425 123 */
Chris@1670 124 int getPointCount() const {
Chris@1670 125 return int(m_points.size());
Chris@1670 126 }
Chris@1662 127 const PointList &getPoints() const {
Chris@1662 128 return m_points;
Chris@1662 129 }
Chris@425 130
Chris@1662 131 /**
Chris@1662 132 * Editing methods.
Chris@1662 133 */
Chris@1662 134 void add(PathPoint p) {
Chris@1662 135
Chris@1662 136 { QMutexLocker locker(&m_mutex);
Chris@1662 137 m_points.insert(p);
Chris@1662 138
Chris@1662 139 if (m_start == m_end) {
Chris@1662 140 m_start = p.frame;
Chris@1662 141 m_end = m_start + m_resolution;
Chris@1662 142 } else {
Chris@1662 143 if (p.frame < m_start) {
Chris@1662 144 m_start = p.frame;
Chris@1662 145 }
Chris@1662 146 if (p.frame + m_resolution > m_end) {
Chris@1662 147 m_end = p.frame + m_resolution;
Chris@1662 148 }
Chris@1662 149 }
Chris@1662 150 }
Chris@1662 151
Chris@1662 152 m_notifier.update(p.frame, m_resolution);
Chris@1662 153 }
Chris@1662 154
Chris@1662 155 void remove(PathPoint p) {
Chris@1662 156 { QMutexLocker locker(&m_mutex);
Chris@1662 157 m_points.erase(p);
Chris@1662 158 }
Chris@1662 159
Chris@1662 160 emit modelChangedWithin(p.frame, p.frame + m_resolution);
Chris@1662 161 }
Chris@1662 162
Chris@1662 163 void clear() {
Chris@1662 164 { QMutexLocker locker(&m_mutex);
Chris@1662 165 m_start = m_end = 0;
Chris@1662 166 m_points.clear();
Chris@1662 167 }
Chris@1662 168 }
Chris@1662 169
Chris@1662 170 /**
Chris@1662 171 * XmlExportable methods.
Chris@1662 172 */
Chris@1662 173 void toXml(QTextStream &out,
Chris@1662 174 QString indent = "",
Chris@1662 175 QString extraAttributes = "") const override {
Chris@1677 176
Chris@1677 177 // Our dataset doesn't have its own export ID, we just use
Chris@1677 178 // ours. Actually any model could do that, since datasets
Chris@1677 179 // aren't in the same id-space as models when re-read
Chris@1662 180
Chris@1662 181 Model::toXml
Chris@1662 182 (out,
Chris@1662 183 indent,
Chris@1662 184 QString("type=\"sparse\" dimensions=\"2\" resolution=\"%1\" "
Chris@1662 185 "notifyOnAdd=\"%2\" dataset=\"%3\" subtype=\"path\" %4")
Chris@1662 186 .arg(m_resolution)
Chris@1662 187 .arg("true") // always true after model reaches 100% -
Chris@1662 188 // subsequent points are always notified
Chris@1677 189 .arg(getExportId())
Chris@1662 190 .arg(extraAttributes));
Chris@1662 191
Chris@1675 192 out << indent << QString("<dataset id=\"%1\" dimensions=\"2\">\n")
Chris@1677 193 .arg(getExportId());
Chris@1662 194
Chris@1675 195 for (PathPoint p: m_points) {
Chris@1675 196 p.toXml(out, indent + " ", "");
Chris@1675 197 }
Chris@1675 198
Chris@1675 199 out << indent << "</dataset>\n";
Chris@1662 200 }
Chris@1679 201
Chris@1679 202 QString toDelimitedDataString(QString delimiter,
Chris@1679 203 DataExportOptions,
Chris@1679 204 sv_frame_t startFrame,
Chris@1679 205 sv_frame_t duration) const override {
Chris@1679 206
Chris@1679 207 QString s;
Chris@1679 208 for (PathPoint p: m_points) {
Chris@1679 209 if (p.frame < startFrame) continue;
Chris@1679 210 if (p.frame >= startFrame + duration) break;
Chris@1679 211 s += QString("%1%2%3\n")
Chris@1679 212 .arg(p.frame)
Chris@1679 213 .arg(delimiter)
Chris@1679 214 .arg(p.mapframe);
Chris@1679 215 }
Chris@1679 216
Chris@1679 217 return s;
Chris@1679 218 }
Chris@1662 219
Chris@1662 220 protected:
Chris@1662 221 sv_samplerate_t m_sampleRate;
Chris@1662 222 int m_resolution;
Chris@1662 223
Chris@1662 224 DeferredNotifier m_notifier;
Chris@1662 225 int m_completion;
Chris@1662 226
Chris@1662 227 sv_frame_t m_start;
Chris@1662 228 sv_frame_t m_end;
Chris@1662 229 PointList m_points;
Chris@1662 230
Chris@1662 231 mutable QMutex m_mutex;
Chris@407 232 };
Chris@407 233
Chris@407 234
Chris@407 235 #endif