Chris@147: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@147: Chris@147: /* Chris@147: Sonic Visualiser Chris@147: An audio file viewer and annotation editor. Chris@147: Centre for Digital Music, Queen Mary, University of London. Chris@147: Chris@147: This program is free software; you can redistribute it and/or Chris@147: modify it under the terms of the GNU General Public License as Chris@147: published by the Free Software Foundation; either version 2 of the Chris@147: License, or (at your option) any later version. See the file Chris@147: COPYING included with this distribution for more information. Chris@147: */ Chris@147: Chris@1581: #ifndef SV_TEXT_MODEL_H Chris@1581: #define SV_TEXT_MODEL_H Chris@147: Chris@1661: #include "EventCommands.h" Chris@1661: #include "TabularModel.h" Chris@1661: #include "Model.h" Chris@1661: #include "DeferredNotifier.h" Chris@1661: Chris@1661: #include "base/EventSeries.h" Chris@302: #include "base/XmlExportable.h" Chris@147: #include "base/RealTime.h" Chris@147: Chris@1661: #include "system/System.h" Chris@1661: Chris@423: #include Chris@423: Chris@147: /** Chris@1661: * A model representing casual textual annotations. A piece of text Chris@1661: * has a given time and y-value in the [0,1) range (indicative of Chris@1661: * height on the window). Chris@147: */ Chris@1661: class TextModel : public Model, Chris@1661: public TabularModel, Chris@1661: public EventEditable Chris@147: { Chris@423: Q_OBJECT Chris@423: Chris@147: public: Chris@1661: TextModel(sv_samplerate_t sampleRate, Chris@1661: int resolution, Chris@1661: bool notifyOnAdd = true) : Chris@1661: m_sampleRate(sampleRate), Chris@1661: m_resolution(resolution), Chris@1661: m_notifier(this, Chris@1661: notifyOnAdd ? Chris@1661: DeferredNotifier::NOTIFY_ALWAYS : Chris@1661: DeferredNotifier::NOTIFY_DEFERRED), Chris@1661: m_completion(100) { Chris@147: } Chris@345: Chris@1580: QString getTypeName() const override { return tr("Text"); } Chris@1661: bool isSparse() const { return true; } Chris@1661: bool isOK() const override { return true; } Chris@1661: Chris@1661: sv_frame_t getStartFrame() const override { Chris@1661: return m_events.getStartFrame(); Chris@1661: } Chris@1661: sv_frame_t getEndFrame() const override { Chris@1661: if (m_events.isEmpty()) return 0; Chris@1661: sv_frame_t e = m_events.getEndFrame() + 1; Chris@1661: if (e % m_resolution == 0) return e; Chris@1661: else return (e / m_resolution + 1) * m_resolution; Chris@1661: } Chris@1661: Chris@1661: sv_samplerate_t getSampleRate() const override { return m_sampleRate; } Chris@1661: int getResolution() const { return m_resolution; } Chris@1661: Chris@1671: int getCompletion() const override { return m_completion; } Chris@1661: Chris@1661: void setCompletion(int completion, bool update = true) { Chris@1661: Chris@1661: { QMutexLocker locker(&m_mutex); Chris@1661: if (m_completion == completion) return; Chris@1661: m_completion = completion; Chris@1661: } Chris@1661: Chris@1661: if (update) { Chris@1661: m_notifier.makeDeferredNotifications(); Chris@1661: } Chris@1661: Chris@1661: emit completionChanged(); Chris@1661: Chris@1661: if (completion == 100) { Chris@1661: // henceforth: Chris@1661: m_notifier.switchMode(DeferredNotifier::NOTIFY_ALWAYS); Chris@1661: emit modelChanged(); Chris@1661: } Chris@1661: } Chris@1661: Chris@1661: /** Chris@1661: * Query methods. Chris@1661: */ Chris@1661: Chris@1661: int getEventCount() const { Chris@1661: return m_events.count(); Chris@1661: } Chris@1661: bool isEmpty() const { Chris@1661: return m_events.isEmpty(); Chris@1661: } Chris@1661: bool containsEvent(const Event &e) const { Chris@1661: return m_events.contains(e); Chris@1661: } Chris@1661: EventVector getAllEvents() const { Chris@1661: return m_events.getAllEvents(); Chris@1661: } Chris@1661: EventVector getEventsSpanning(sv_frame_t f, sv_frame_t duration) const { Chris@1661: return m_events.getEventsSpanning(f, duration); Chris@1661: } Chris@1661: EventVector getEventsCovering(sv_frame_t f) const { Chris@1661: return m_events.getEventsCovering(f); Chris@1661: } Chris@1661: EventVector getEventsWithin(sv_frame_t f, sv_frame_t duration, Chris@1661: int overspill = 0) const { Chris@1661: return m_events.getEventsWithin(f, duration, overspill); Chris@1661: } Chris@1661: EventVector getEventsStartingWithin(sv_frame_t f, sv_frame_t duration) const { Chris@1661: return m_events.getEventsStartingWithin(f, duration); Chris@1661: } Chris@1661: EventVector getEventsStartingAt(sv_frame_t f) const { Chris@1661: return m_events.getEventsStartingAt(f); Chris@1661: } Chris@1661: bool getNearestEventMatching(sv_frame_t startSearchAt, Chris@1661: std::function predicate, Chris@1661: EventSeries::Direction direction, Chris@1661: Event &found) const { Chris@1661: return m_events.getNearestEventMatching Chris@1661: (startSearchAt, predicate, direction, found); Chris@1661: } Chris@1661: Chris@1661: /** Chris@1661: * Editing methods. Chris@1661: */ Chris@1661: void add(Event e) override { Chris@1661: Chris@1661: { QMutexLocker locker(&m_mutex); Chris@1661: m_events.add(e.withoutDuration()); Chris@1661: } Chris@1661: Chris@1661: m_notifier.update(e.getFrame(), m_resolution); Chris@1661: } Chris@1661: Chris@1661: void remove(Event e) override { Chris@1661: { QMutexLocker locker(&m_mutex); Chris@1661: m_events.remove(e); Chris@1661: } Chris@1661: emit modelChangedWithin(e.getFrame(), e.getFrame() + m_resolution); Chris@1661: } Chris@424: Chris@424: /** Chris@424: * TabularModel methods. Chris@424: */ Chris@424: Chris@1661: int getRowCount() const override { Chris@1661: return m_events.count(); Chris@1661: } Chris@1661: Chris@1661: int getColumnCount() const override { Chris@424: return 4; Chris@424: } Chris@424: Chris@1661: bool isColumnTimeValue(int column) const override { Chris@1661: return (column < 2); Chris@1661: } Chris@1661: Chris@1661: sv_frame_t getFrameForRow(int row) const override { Chris@1661: if (row < 0 || row >= m_events.count()) { Chris@1661: return 0; Chris@1661: } Chris@1661: Event e = m_events.getEventByIndex(row); Chris@1661: return e.getFrame(); Chris@1661: } Chris@1661: Chris@1661: int getRowForFrame(sv_frame_t frame) const override { Chris@1661: return m_events.getIndexForEvent(Event(frame)); Chris@1661: } Chris@1661: Chris@1661: QString getHeading(int column) const override { Chris@424: switch (column) { Chris@424: case 0: return tr("Time"); Chris@424: case 1: return tr("Frame"); Chris@424: case 2: return tr("Height"); Chris@424: case 3: return tr("Label"); Chris@424: default: return tr("Unknown"); Chris@424: } Chris@424: } Chris@424: Chris@1661: SortType getSortType(int column) const override { Chris@1661: if (column == 3) return SortAlphabetical; Chris@1661: return SortNumeric; Chris@1661: } Chris@1661: Chris@1661: QVariant getData(int row, int column, int role) const override { Chris@1661: Chris@1661: if (row < 0 || row >= m_events.count()) { Chris@1661: return QVariant(); Chris@425: } Chris@425: Chris@1661: Event e = m_events.getEventByIndex(row); Chris@424: Chris@424: switch (column) { Chris@1661: case 0: return adaptFrameForRole(e.getFrame(), getSampleRate(), role); Chris@1661: case 1: return int(e.getFrame()); Chris@1661: case 2: return e.getValue(); Chris@1661: case 3: return e.getLabel(); Chris@424: default: return QVariant(); Chris@424: } Chris@424: } Chris@424: Chris@1661: Command *getSetDataCommand(int row, int column, const QVariant &value, int role) override { Chris@1661: Chris@1661: if (row < 0 || row >= m_events.count()) return nullptr; Chris@1661: if (role != Qt::EditRole) return nullptr; Chris@1661: Chris@1661: Event e0 = m_events.getEventByIndex(row); Chris@1661: Event e1; Chris@1661: Chris@1661: switch (column) { Chris@1661: case 0: e1 = e0.withFrame(sv_frame_t(round(value.toDouble() * Chris@1661: getSampleRate()))); break; Chris@1661: case 1: e1 = e0.withFrame(value.toInt()); break; Chris@1661: case 2: e1 = e0.withValue(float(value.toDouble())); break; Chris@1661: case 3: e1 = e0.withLabel(value.toString()); break; Chris@425: } Chris@425: Chris@1661: ChangeEventsCommand *command = Chris@1661: new ChangeEventsCommand(this, tr("Edit Data")); Chris@1661: command->remove(e0); Chris@1661: command->add(e1); Chris@424: return command->finish(); Chris@424: } Chris@1661: Chris@1661: /** Chris@1661: * XmlExportable methods. Chris@1661: */ Chris@1661: void toXml(QTextStream &out, Chris@1661: QString indent = "", Chris@1661: QString extraAttributes = "") const override { Chris@424: Chris@1661: Model::toXml Chris@1661: (out, Chris@1661: indent, Chris@1661: QString("type=\"sparse\" dimensions=\"2\" resolution=\"%1\" " Chris@1661: "notifyOnAdd=\"%2\" dataset=\"%3\" subtype=\"text\" %4") Chris@1661: .arg(m_resolution) Chris@1661: .arg("true") // always true after model reaches 100% - Chris@1661: // subsequent events are always notified Chris@1661: .arg(getObjectExportId(&m_events)) Chris@1661: .arg(extraAttributes)); Chris@1661: Chris@1661: m_events.toXml(out, indent, QString("dimensions=\"2\"")); Chris@424: } Chris@1661: Chris@1661: protected: Chris@1661: sv_samplerate_t m_sampleRate; Chris@1661: int m_resolution; Chris@424: Chris@1661: DeferredNotifier m_notifier; Chris@1661: int m_completion; Chris@1661: Chris@1661: EventSeries m_events; Chris@1661: Chris@1661: mutable QMutex m_mutex; Chris@424: Chris@147: }; Chris@147: Chris@147: Chris@147: #endif Chris@147: Chris@147: Chris@147: