annotate data/model/ImageModel.h @ 1741:9d82b164f264 by-id

Work on commands, and some other model updates
author Chris Cannam
date Thu, 27 Jun 2019 13:08:10 +0100
parents 78fe29adfd16
children 52705a328b34
rev   line source
Chris@302 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@302 2
Chris@302 3 /*
Chris@302 4 Sonic Visualiser
Chris@302 5 An audio file viewer and annotation editor.
Chris@302 6 Centre for Digital Music, Queen Mary, University of London.
Chris@302 7 This file copyright 2006-2007 Chris Cannam and QMUL.
Chris@302 8
Chris@302 9 This program is free software; you can redistribute it and/or
Chris@302 10 modify it under the terms of the GNU General Public License as
Chris@302 11 published by the Free Software Foundation; either version 2 of the
Chris@302 12 License, or (at your option) any later version. See the file
Chris@302 13 COPYING included with this distribution for more information.
Chris@302 14 */
Chris@302 15
Chris@1581 16 #ifndef SV_IMAGE_MODEL_H
Chris@1581 17 #define SV_IMAGE_MODEL_H
Chris@302 18
Chris@1663 19 #include "EventCommands.h"
Chris@1663 20 #include "TabularModel.h"
Chris@1663 21 #include "Model.h"
Chris@1663 22 #include "DeferredNotifier.h"
Chris@1663 23
Chris@1663 24 #include "base/EventSeries.h"
Chris@302 25 #include "base/XmlExportable.h"
Chris@302 26 #include "base/RealTime.h"
Chris@302 27
Chris@1663 28 #include "system/System.h"
Chris@1663 29
Chris@423 30 #include <QStringList>
Chris@423 31
Chris@302 32 /**
Chris@1663 33 * A model representing image annotations, identified by filename or
Chris@1663 34 * URI, at a given time, with an optional label. The filename can be
Chris@1663 35 * empty, in which case we instead have a space to put an image in.
Chris@302 36 */
Chris@302 37
Chris@1663 38 class ImageModel : public Model,
Chris@1663 39 public TabularModel,
Chris@1663 40 public EventEditable
Chris@302 41 {
Chris@423 42 Q_OBJECT
Chris@423 43
Chris@302 44 public:
Chris@1663 45 ImageModel(sv_samplerate_t sampleRate,
Chris@1663 46 int resolution,
Chris@1663 47 bool notifyOnAdd = true) :
Chris@1663 48 m_sampleRate(sampleRate),
Chris@1663 49 m_resolution(resolution),
Chris@1663 50 m_notifier(this,
Chris@1663 51 notifyOnAdd ?
Chris@1663 52 DeferredNotifier::NOTIFY_ALWAYS :
Chris@1663 53 DeferredNotifier::NOTIFY_DEFERRED),
Chris@1663 54 m_completion(100) {
Chris@1663 55 }
Chris@302 56
Chris@1580 57 QString getTypeName() const override { return tr("Image"); }
Chris@1692 58 bool isSparse() const override { return true; }
Chris@1663 59 bool isOK() const override { return true; }
Chris@345 60
Chris@1663 61 sv_frame_t getStartFrame() const override {
Chris@1663 62 return m_events.getStartFrame();
Chris@1663 63 }
Chris@1725 64 sv_frame_t getTrueEndFrame() const override {
Chris@1663 65 if (m_events.isEmpty()) return 0;
Chris@1663 66 sv_frame_t e = m_events.getEndFrame() + 1;
Chris@1663 67 if (e % m_resolution == 0) return e;
Chris@1663 68 else return (e / m_resolution + 1) * m_resolution;
Chris@1663 69 }
Chris@1663 70
Chris@1663 71 sv_samplerate_t getSampleRate() const override { return m_sampleRate; }
Chris@1663 72 int getResolution() const { return m_resolution; }
Chris@1663 73
Chris@1692 74 int getCompletion() const override { return m_completion; }
Chris@1663 75
Chris@1663 76 void setCompletion(int completion, bool update = true) {
Chris@1663 77
Chris@1663 78 { QMutexLocker locker(&m_mutex);
Chris@1663 79 if (m_completion == completion) return;
Chris@1663 80 m_completion = completion;
Chris@1663 81 }
Chris@1663 82
Chris@1663 83 if (update) {
Chris@1663 84 m_notifier.makeDeferredNotifications();
Chris@1663 85 }
Chris@1663 86
Chris@1663 87 emit completionChanged();
Chris@1663 88
Chris@1663 89 if (completion == 100) {
Chris@1663 90 // henceforth:
Chris@1663 91 m_notifier.switchMode(DeferredNotifier::NOTIFY_ALWAYS);
Chris@1663 92 emit modelChanged();
Chris@1663 93 }
Chris@1663 94 }
Chris@1663 95
Chris@1663 96 /**
Chris@1663 97 * Query methods.
Chris@1663 98 */
Chris@1663 99
Chris@1663 100 //!!! todo: go through all models, weeding out the methods here
Chris@1663 101 //!!! that are not used
Chris@1663 102
Chris@1663 103 int getEventCount() const {
Chris@1663 104 return m_events.count();
Chris@1663 105 }
Chris@1663 106 bool isEmpty() const {
Chris@1663 107 return m_events.isEmpty();
Chris@1663 108 }
Chris@1663 109 bool containsEvent(const Event &e) const {
Chris@1663 110 return m_events.contains(e);
Chris@1663 111 }
Chris@1663 112 EventVector getAllEvents() const {
Chris@1663 113 return m_events.getAllEvents();
Chris@1663 114 }
Chris@1663 115 EventVector getEventsSpanning(sv_frame_t f, sv_frame_t duration) const {
Chris@1663 116 return m_events.getEventsSpanning(f, duration);
Chris@1663 117 }
Chris@1663 118 EventVector getEventsCovering(sv_frame_t f) const {
Chris@1663 119 return m_events.getEventsCovering(f);
Chris@1663 120 }
Chris@1663 121 EventVector getEventsWithin(sv_frame_t f, sv_frame_t duration,
Chris@1663 122 int overspill = 0) const {
Chris@1663 123 return m_events.getEventsWithin(f, duration, overspill);
Chris@1663 124 }
Chris@1663 125 EventVector getEventsStartingWithin(sv_frame_t f, sv_frame_t duration) const {
Chris@1663 126 return m_events.getEventsStartingWithin(f, duration);
Chris@1663 127 }
Chris@1663 128 EventVector getEventsStartingAt(sv_frame_t f) const {
Chris@1663 129 return m_events.getEventsStartingAt(f);
Chris@1663 130 }
Chris@1663 131 bool getNearestEventMatching(sv_frame_t startSearchAt,
Chris@1663 132 std::function<bool(Event)> predicate,
Chris@1663 133 EventSeries::Direction direction,
Chris@1663 134 Event &found) const {
Chris@1663 135 return m_events.getNearestEventMatching
Chris@1663 136 (startSearchAt, predicate, direction, found);
Chris@302 137 }
Chris@302 138
Chris@302 139 /**
Chris@1663 140 * Editing methods.
Chris@302 141 */
Chris@1663 142 void add(Event e) override {
Chris@1663 143
Chris@1663 144 { QMutexLocker locker(&m_mutex);
Chris@1674 145 m_events.add(e.withoutDuration().withoutValue().withoutLevel());
Chris@1429 146 }
Chris@1663 147
Chris@1663 148 m_notifier.update(e.getFrame(), m_resolution);
Chris@1663 149 }
Chris@1663 150
Chris@1663 151 void remove(Event e) override {
Chris@1663 152 { QMutexLocker locker(&m_mutex);
Chris@1663 153 m_events.remove(e);
Chris@1429 154 }
Chris@1663 155 emit modelChangedWithin(e.getFrame(), e.getFrame() + m_resolution);
Chris@1663 156 }
Chris@424 157
Chris@424 158 /**
Chris@424 159 * TabularModel methods.
Chris@424 160 */
Chris@424 161
Chris@1663 162 int getRowCount() const override {
Chris@1663 163 return m_events.count();
Chris@1663 164 }
Chris@1663 165
Chris@1663 166 int getColumnCount() const override {
Chris@424 167 return 4;
Chris@424 168 }
Chris@424 169
Chris@1663 170 bool isColumnTimeValue(int column) const override {
Chris@1663 171 return (column < 2);
Chris@1663 172 }
Chris@1663 173
Chris@1663 174 sv_frame_t getFrameForRow(int row) const override {
Chris@1663 175 if (row < 0 || row >= m_events.count()) {
Chris@1663 176 return 0;
Chris@1663 177 }
Chris@1663 178 Event e = m_events.getEventByIndex(row);
Chris@1663 179 return e.getFrame();
Chris@1663 180 }
Chris@1663 181
Chris@1663 182 int getRowForFrame(sv_frame_t frame) const override {
Chris@1663 183 return m_events.getIndexForEvent(Event(frame));
Chris@1663 184 }
Chris@1663 185
Chris@1663 186 QString getHeading(int column) const override {
Chris@424 187 switch (column) {
Chris@424 188 case 0: return tr("Time");
Chris@424 189 case 1: return tr("Frame");
Chris@424 190 case 2: return tr("Image");
Chris@424 191 case 3: return tr("Label");
Chris@424 192 default: return tr("Unknown");
Chris@424 193 }
Chris@424 194 }
Chris@424 195
Chris@1663 196 SortType getSortType(int column) const override {
Chris@1663 197 if (column >= 2) return SortAlphabetical;
Chris@1663 198 return SortNumeric;
Chris@1663 199 }
Chris@1663 200
Chris@1663 201 QVariant getData(int row, int column, int role) const override {
Chris@1663 202
Chris@1663 203 if (row < 0 || row >= m_events.count()) {
Chris@1663 204 return QVariant();
Chris@425 205 }
Chris@425 206
Chris@1663 207 Event e = m_events.getEventByIndex(row);
Chris@424 208
Chris@424 209 switch (column) {
Chris@1663 210 case 0: return adaptFrameForRole(e.getFrame(), getSampleRate(), role);
Chris@1663 211 case 1: return int(e.getFrame());
Chris@1663 212 case 2: return e.getURI();
Chris@1663 213 case 3: return e.getLabel();
Chris@424 214 default: return QVariant();
Chris@424 215 }
Chris@424 216 }
Chris@424 217
Chris@1663 218 Command *getSetDataCommand(int row, int column, const QVariant &value, int role) override {
Chris@1663 219
Chris@1663 220 if (row < 0 || row >= m_events.count()) return nullptr;
Chris@1663 221 if (role != Qt::EditRole) return nullptr;
Chris@1663 222
Chris@1663 223 Event e0 = m_events.getEventByIndex(row);
Chris@1663 224 Event e1;
Chris@1663 225
Chris@1663 226 switch (column) {
Chris@1663 227 case 0: e1 = e0.withFrame(sv_frame_t(round(value.toDouble() *
Chris@1663 228 getSampleRate()))); break;
Chris@1663 229 case 1: e1 = e0.withFrame(value.toInt()); break;
Chris@1663 230 case 2: e1 = e0.withURI(value.toString()); break;
Chris@1663 231 case 3: e1 = e0.withLabel(value.toString()); break;
Chris@425 232 }
Chris@425 233
Chris@1741 234 auto command = new ChangeEventsCommand<Model>(getId(), tr("Edit Data"));
Chris@1663 235 command->remove(e0);
Chris@1663 236 command->add(e1);
Chris@424 237 return command->finish();
Chris@424 238 }
Chris@1663 239
Chris@1663 240 /**
Chris@1663 241 * XmlExportable methods.
Chris@1663 242 */
Chris@1663 243 void toXml(QTextStream &out,
Chris@1663 244 QString indent = "",
Chris@1663 245 QString extraAttributes = "") const override {
Chris@424 246
Chris@1663 247 Model::toXml
Chris@1663 248 (out,
Chris@1663 249 indent,
Chris@1663 250 QString("type=\"sparse\" dimensions=\"1\" resolution=\"%1\" "
Chris@1663 251 "notifyOnAdd=\"%2\" dataset=\"%3\" subtype=\"image\" %4")
Chris@1663 252 .arg(m_resolution)
Chris@1663 253 .arg("true") // always true after model reaches 100% -
Chris@1663 254 // subsequent events are always notified
Chris@1677 255 .arg(m_events.getExportId())
Chris@1663 256 .arg(extraAttributes));
Chris@1674 257
Chris@1674 258 Event::ExportNameOptions options;
Chris@1674 259 options.uriAttributeName = "image";
Chris@1663 260
Chris@1674 261 m_events.toXml(out, indent, QString("dimensions=\"1\""), options);
Chris@424 262 }
Chris@1679 263
Chris@1679 264 QString toDelimitedDataString(QString delimiter,
Chris@1679 265 DataExportOptions options,
Chris@1679 266 sv_frame_t startFrame,
Chris@1679 267 sv_frame_t duration) const override {
Chris@1679 268 return m_events.toDelimitedDataString(delimiter,
Chris@1679 269 options,
Chris@1679 270 startFrame,
Chris@1679 271 duration,
Chris@1679 272 m_sampleRate,
Chris@1679 273 m_resolution,
Chris@1679 274 Event().withValue(0.f));
Chris@1679 275 }
Chris@1679 276
Chris@1663 277 protected:
Chris@1663 278 sv_samplerate_t m_sampleRate;
Chris@1663 279 int m_resolution;
Chris@424 280
Chris@1663 281 DeferredNotifier m_notifier;
Chris@1663 282 int m_completion;
Chris@1663 283
Chris@1663 284 EventSeries m_events;
Chris@1663 285
Chris@1663 286 mutable QMutex m_mutex;
Chris@302 287 };
Chris@302 288
Chris@302 289
Chris@302 290 #endif
Chris@302 291