annotate data/model/Model.h @ 319:3ff8f571da09

* Hoist alignment model set/query up to Model, so any models can be aligned * Add Model::aboutToDelete and aboutToBeDeleted for management of models that are contained by or referred to by other models instead of only the document
author Chris Cannam
date Wed, 24 Oct 2007 15:21:38 +0000
parents 70a232b1f12a
children 1afaf98dbf11
rev   line source
Chris@150 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@150 2
Chris@150 3 /*
Chris@150 4 Sonic Visualiser
Chris@150 5 An audio file viewer and annotation editor.
Chris@150 6 Centre for Digital Music, Queen Mary, University of London.
Chris@150 7 This file copyright 2006 Chris Cannam.
Chris@150 8
Chris@150 9 This program is free software; you can redistribute it and/or
Chris@150 10 modify it under the terms of the GNU General Public License as
Chris@150 11 published by the Free Software Foundation; either version 2 of the
Chris@150 12 License, or (at your option) any later version. See the file
Chris@150 13 COPYING included with this distribution for more information.
Chris@150 14 */
Chris@150 15
Chris@150 16 #ifndef _MODEL_H_
Chris@150 17 #define _MODEL_H_
Chris@150 18
Chris@150 19 #include <vector>
Chris@150 20 #include <QObject>
Chris@150 21
Chris@150 22 #include "base/XmlExportable.h"
Chris@150 23
Chris@290 24 typedef std::vector<float> SampleBlock;
Chris@290 25
Chris@179 26 class ZoomConstraint;
Chris@319 27 class AlignmentModel;
Chris@179 28
Chris@150 29 /**
Chris@150 30 * Model is the base class for all data models that represent any sort
Chris@150 31 * of data on a time scale based on an audio frame rate.
Chris@150 32 */
Chris@150 33
Chris@179 34 class Model : public QObject,
Chris@150 35 public XmlExportable
Chris@150 36 {
Chris@150 37 Q_OBJECT
Chris@150 38
Chris@150 39 public:
Chris@150 40 virtual ~Model();
Chris@150 41
Chris@150 42 /**
Chris@150 43 * Return true if the model was constructed successfully. Classes
Chris@150 44 * that refer to the model should always test this before use.
Chris@150 45 */
Chris@150 46 virtual bool isOK() const = 0;
Chris@150 47
Chris@150 48 /**
Chris@150 49 * Return the first audio frame spanned by the model.
Chris@150 50 */
Chris@150 51 virtual size_t getStartFrame() const = 0;
Chris@150 52
Chris@150 53 /**
Chris@150 54 * Return the last audio frame spanned by the model.
Chris@150 55 */
Chris@150 56 virtual size_t getEndFrame() const = 0;
Chris@150 57
Chris@150 58 /**
Chris@150 59 * Return the frame rate in frames per second.
Chris@150 60 */
Chris@150 61 virtual size_t getSampleRate() const = 0;
Chris@150 62
Chris@150 63 /**
Chris@297 64 * Return the frame rate of the underlying material, if the model
Chris@297 65 * itself has already been resampled.
Chris@297 66 */
Chris@297 67 virtual size_t getNativeRate() const { return getSampleRate(); }
Chris@297 68
Chris@297 69 /**
Chris@150 70 * Return a copy of this model.
Chris@150 71 *
Chris@150 72 * If the model is not editable, this may be effectively a shallow
Chris@150 73 * copy. If the model is editable, however, this operation must
Chris@150 74 * properly copy all of the model's editable data.
Chris@150 75 *
Chris@150 76 * In general this operation is not useful for non-editable dense
Chris@150 77 * models such as waveforms, because there may be no efficient
Chris@150 78 * copy operation implemented -- for such models it is better not
Chris@150 79 * to copy at all.
Chris@150 80 *
Chris@150 81 * Caller owns the returned value.
Chris@150 82 */
Chris@150 83 virtual Model *clone() const = 0;
Chris@150 84
Chris@150 85 /**
Chris@150 86 * Return true if the model has finished loading or calculating
Chris@150 87 * all its data, for a model that is capable of calculating in a
Chris@150 88 * background thread. The default implementation is appropriate
Chris@150 89 * for a thread that does not background any work but carries out
Chris@150 90 * all its calculation from the constructor or accessors.
Chris@150 91 *
Chris@150 92 * If "completion" is non-NULL, this function should return
Chris@150 93 * through it an estimated percentage value showing how far
Chris@150 94 * through the background operation it thinks it is (for progress
Chris@150 95 * reporting). If it has no way to calculate progress, it may
Chris@150 96 * return the special value COMPLETION_UNKNOWN.
Chris@150 97 */
Chris@150 98 virtual bool isReady(int *completion = 0) const {
Chris@150 99 bool ok = isOK();
Chris@150 100 if (completion) *completion = (ok ? 100 : 0);
Chris@150 101 return ok;
Chris@150 102 }
Chris@150 103 static const int COMPLETION_UNKNOWN;
Chris@150 104
Chris@179 105 /**
Chris@179 106 * If this model imposes a zoom constraint, i.e. some limit to the
Chris@179 107 * set of resolutions at which its data can meaningfully be
Chris@179 108 * displayed, then return it.
Chris@179 109 */
Chris@179 110 virtual const ZoomConstraint *getZoomConstraint() const {
Chris@179 111 return 0;
Chris@179 112 }
Chris@179 113
Chris@297 114 /**
Chris@297 115 * If this model was derived from another, return the model it was
Chris@297 116 * derived from. The assumption is that the source model's
Chris@297 117 * alignment will also apply to this model, unless some other
Chris@319 118 * property (such as a specific alignment model set on this model)
Chris@319 119 * indicates otherwise.
Chris@297 120 */
Chris@297 121 virtual Model *getSourceModel() const {
Chris@297 122 return m_sourceModel;
Chris@297 123 }
Chris@297 124
Chris@297 125 /**
Chris@297 126 * Set the source model for this model.
Chris@297 127 */
Chris@319 128 virtual void setSourceModel(Model *model);
Chris@319 129
Chris@319 130 /**
Chris@319 131 * Specify an aligment between this model's timeline and that of a
Chris@319 132 * reference model. The alignment model records both the
Chris@319 133 * reference and the alignment. This model takes ownership of the
Chris@319 134 * alignment model.
Chris@319 135 */
Chris@319 136 virtual void setAlignment(AlignmentModel *alignment);
Chris@319 137
Chris@319 138 /**
Chris@319 139 * Return the reference model for the current alignment timeline,
Chris@319 140 * if any.
Chris@319 141 */
Chris@319 142 virtual const Model *getAlignmentReference() const;
Chris@319 143
Chris@319 144 /**
Chris@319 145 * Return the frame number of the reference model that corresponds
Chris@319 146 * to the given frame number in this model.
Chris@319 147 */
Chris@319 148 virtual size_t alignToReference(size_t frame) const;
Chris@319 149
Chris@319 150 /**
Chris@319 151 * Return the frame number in this model that corresponds to the
Chris@319 152 * given frame number of the reference model.
Chris@319 153 */
Chris@319 154 virtual size_t alignFromReference(size_t referenceFrame) const;
Chris@319 155
Chris@319 156 /**
Chris@319 157 * Return the completion percentage for the alignment model: 100
Chris@319 158 * if there is no alignment model or it has been entirely
Chris@319 159 * calculated, or less than 100 if it is still being calculated.
Chris@319 160 */
Chris@319 161 virtual int getAlignmentCompletion() const;
Chris@297 162
Chris@150 163 virtual void toXml(QTextStream &stream,
Chris@150 164 QString indent = "",
Chris@150 165 QString extraAttributes = "") const;
Chris@150 166
Chris@150 167 virtual QString toDelimitedDataString(QString) const { return ""; }
Chris@150 168
Chris@319 169 public slots:
Chris@319 170 void aboutToDelete();
Chris@319 171 void sourceModelAboutToBeDeleted();
Chris@319 172
Chris@150 173 signals:
Chris@150 174 /**
Chris@150 175 * Emitted when a model has been edited (or more data retrieved
Chris@150 176 * from cache, in the case of a cached model that generates slowly)
Chris@150 177 */
Chris@150 178 void modelChanged();
Chris@150 179
Chris@150 180 /**
Chris@150 181 * Emitted when a model has been edited (or more data retrieved
Chris@150 182 * from cache, in the case of a cached model that generates slowly)
Chris@150 183 */
Chris@150 184 void modelChanged(size_t startFrame, size_t endFrame);
Chris@150 185
Chris@150 186 /**
Chris@150 187 * Emitted when some internal processing has advanced a stage, but
Chris@150 188 * the model has not changed externally. Views should respond by
Chris@150 189 * updating any progress meters or other monitoring, but not
Chris@150 190 * refreshing the actual view.
Chris@150 191 */
Chris@150 192 void completionChanged();
Chris@150 193
Chris@319 194 /**
Chris@319 195 * Emitted when the completion percentage changes for the
Chris@319 196 * calculation of this model's alignment model.
Chris@319 197 */
Chris@319 198 void alignmentCompletionChanged();
Chris@319 199
Chris@319 200 /**
Chris@319 201 * Emitted when something notifies this model (through calling
Chris@319 202 * aboutToDelete() that it is about to delete it. Note that this
Chris@319 203 * depends on an external agent such as a Document object or
Chris@319 204 * owning model telling the model that it is about to delete it;
Chris@319 205 * there is nothing in the model to guarantee that this signal
Chris@319 206 * will be emitted before the actual deletion.
Chris@319 207 */
Chris@319 208 void aboutToBeDeleted();
Chris@319 209
Chris@150 210 protected:
Chris@319 211 Model() : m_sourceModel(0), m_alignment(0), m_aboutToDelete(false) { }
Chris@150 212
Chris@150 213 // Not provided.
Chris@150 214 Model(const Model &);
Chris@150 215 Model &operator=(const Model &);
Chris@297 216
Chris@297 217 Model *m_sourceModel;
Chris@319 218 AlignmentModel *m_alignment;
Chris@319 219 bool m_aboutToDelete;
Chris@150 220 };
Chris@150 221
Chris@150 222 #endif