annotate framework/Document.h @ 296:19282182e60d tonioni_multi_transform

Update to the multi-transform api in svcore
author Chris Cannam
date Mon, 02 Dec 2013 12:29:54 +0000
parents f3f3ca272bce
children 1e61f0c26593
rev   line source
Chris@45 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@45 2
Chris@45 3 /*
Chris@45 4 Sonic Visualiser
Chris@45 5 An audio file viewer and annotation editor.
Chris@45 6 Centre for Digital Music, Queen Mary, University of London.
Chris@45 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@45 8
Chris@45 9 This program is free software; you can redistribute it and/or
Chris@45 10 modify it under the terms of the GNU General Public License as
Chris@45 11 published by the Free Software Foundation; either version 2 of the
Chris@45 12 License, or (at your option) any later version. See the file
Chris@45 13 COPYING included with this distribution for more information.
Chris@45 14 */
Chris@45 15
Chris@45 16 #ifndef _DOCUMENT_H_
Chris@45 17 #define _DOCUMENT_H_
Chris@45 18
Chris@45 19 #include "layer/LayerFactory.h"
Chris@106 20 #include "transform/Transform.h"
Chris@106 21 #include "transform/ModelTransformer.h"
gyorgyf@270 22 #include "transform/FeatureExtractionModelTransformer.h"
Chris@45 23 #include "base/Command.h"
Chris@45 24
Chris@45 25 #include <map>
Chris@45 26 #include <set>
Chris@45 27
Chris@45 28 class Model;
Chris@45 29 class Layer;
Chris@45 30 class View;
Chris@45 31 class WaveFileModel;
Chris@45 32
Chris@45 33 /**
Chris@45 34 * A Sonic Visualiser document consists of a set of data models, and
Chris@45 35 * also the visualisation layers used to display them. Changes to the
Chris@45 36 * layers and their layout need to be stored and managed in much the
Chris@45 37 * same way as changes to the underlying data.
Chris@45 38 *
Chris@45 39 * The document manages:
Chris@45 40 *
Chris@45 41 * - A main data Model, which provides the underlying sample rate and
Chris@45 42 * such like. This must be a WaveFileModel.
Chris@45 43 *
Chris@45 44 * - Any number of imported Model objects, which contain data without any
Chris@45 45 * requirement to remember where the data came from or how to
Chris@45 46 * regenerate it.
Chris@45 47 *
Chris@53 48 * - Any number of Model objects that were generated by a Transformer
Chris@54 49 * such as FeatureExtractionModelTransformer. For these, we also
Chris@45 50 * record the source model and the name of the transform used to
Chris@45 51 * generate the model so that we can regenerate it (potentially
Chris@45 52 * from a different source) on demand.
Chris@45 53 *
Chris@45 54 * - A flat list of Layer objects. Elsewhere, the GUI may distribute these
Chris@45 55 * across any number of View widgets. A layer may be viewable on more
Chris@45 56 * than one view at once, in principle. A layer refers to one model,
Chris@45 57 * but the same model can be in use in more than one layer.
Chris@45 58 *
Chris@45 59 * The document does *not* manage the existence or structure of Pane
Chris@45 60 * and other view widgets. However, it does provide convenience
Chris@45 61 * methods for reference-counted command-based management of the
Chris@45 62 * association between layers and views (addLayerToView,
Chris@45 63 * removeLayerFromView).
Chris@45 64 */
Chris@45 65
Chris@45 66 class Document : public QObject,
Chris@45 67 public XmlExportable
Chris@45 68 {
Chris@45 69 Q_OBJECT
Chris@45 70
Chris@45 71 public:
Chris@45 72 Document();
Chris@45 73 virtual ~Document();
Chris@45 74
Chris@45 75 /**
Chris@45 76 * Create and return a new layer of the given type, associated
Chris@45 77 * with no model. The caller may set any model on this layer, but
Chris@45 78 * the model must also be registered with the document via the
Chris@45 79 * add-model methods below.
Chris@45 80 */
Chris@45 81 Layer *createLayer(LayerFactory::LayerType);
Chris@45 82
Chris@45 83 /**
Chris@45 84 * Create and return a new layer of the given type, associated
Chris@45 85 * with the current main model (if appropriate to the layer type).
Chris@45 86 */
Chris@45 87 Layer *createMainModelLayer(LayerFactory::LayerType);
Chris@45 88
Chris@45 89 /**
Chris@45 90 * Create and return a new layer associated with the given model,
Chris@45 91 * and register the model as an imported model.
Chris@45 92 */
Chris@45 93 Layer *createImportedLayer(Model *);
Chris@45 94
Chris@45 95 /**
Chris@45 96 * Create and return a new layer of the given type, with an
Chris@45 97 * appropriate empty model. If the given type is not one for
Chris@45 98 * which an empty model can meaningfully be created, return 0.
Chris@45 99 */
Chris@45 100 Layer *createEmptyLayer(LayerFactory::LayerType);
Chris@45 101
Chris@45 102 /**
Chris@45 103 * Create and return a new layer of the given type, associated
Chris@45 104 * with the given transform name. This method does not run the
Chris@45 105 * transform itself, nor create a model. The caller can safely
Chris@45 106 * add a model to the layer later, but note that all models used
Chris@45 107 * by a transform layer _must_ be registered with the document
Chris@45 108 * using addDerivedModel below.
Chris@45 109 */
Chris@54 110 Layer *createDerivedLayer(LayerFactory::LayerType, TransformId);
Chris@45 111
Chris@45 112 /**
Chris@45 113 * Create and return a suitable layer for the given transform,
Chris@45 114 * running the transform and associating the resulting model with
Chris@45 115 * the new layer.
Chris@45 116 */
Chris@72 117 Layer *createDerivedLayer(const Transform &,
Chris@72 118 const ModelTransformer::Input &);
Chris@45 119
Chris@45 120 /**
matthiasm@269 121 * Exactly the same as above, but providing exactly the layer type
matthiasm@269 122 * for cases in which the first suitable layer type for a transform
matthiasm@269 123 * is not the desired one.
matthiasm@269 124 * WARNING: highly redundant code (with the above creation funciotn)
matthiasm@269 125 */
matthiasm@269 126 Layer *createDerivedLayer(const Transform &,
matthiasm@269 127 const ModelTransformer::Input &,
Chris@296 128 const LayerFactory::LayerType type);
matthiasm@269 129
matthiasm@269 130 /**
Chris@52 131 * Delete the given layer, and also its associated model if no
Chris@52 132 * longer used by any other layer. In general, this should be the
Chris@52 133 * only method used to delete layers -- doing so directly is a bit
Chris@52 134 * of a social gaffe.
Chris@52 135 */
Chris@52 136 void deleteLayer(Layer *, bool force = false);
Chris@52 137
Chris@52 138 /**
Chris@45 139 * Set the main model (the source for playback sample rate, etc)
Chris@45 140 * to the given wave file model. This will regenerate any derived
Chris@45 141 * models that were based on the previous main model.
Chris@45 142 */
Chris@45 143 void setMainModel(WaveFileModel *);
Chris@45 144
Chris@45 145 /**
Chris@45 146 * Get the main model (the source for playback sample rate, etc).
Chris@45 147 */
Chris@45 148 WaveFileModel *getMainModel() { return m_mainModel; }
Chris@45 149
Chris@45 150 /**
Chris@45 151 * Get the main model (the source for playback sample rate, etc).
Chris@45 152 */
Chris@45 153 const WaveFileModel *getMainModel() const { return m_mainModel; }
Chris@45 154
Chris@72 155 std::vector<Model *> getTransformInputModels();
Chris@45 156
Chris@77 157 bool isKnownModel(const Model *) const;
Chris@77 158
Chris@45 159 /**
Chris@45 160 * Add a derived model associated with the given transform,
Chris@45 161 * running the transform and returning the resulting model.
Chris@45 162 */
Chris@72 163 Model *addDerivedModel(const Transform &transform,
Chris@78 164 const ModelTransformer::Input &input,
Chris@296 165 QString &returnedMessage);
Chris@45 166
Chris@45 167 /**
Chris@45 168 * Add a derived model associated with the given transform. This
Chris@45 169 * is necessary to register any derived model that was not created
Chris@45 170 * by the document using createDerivedModel or createDerivedLayer.
Chris@45 171 */
Chris@72 172 void addDerivedModel(const Transform &transform,
Chris@72 173 const ModelTransformer::Input &input,
Chris@72 174 Model *outputModelToAdd);
Chris@45 175
Chris@45 176 /**
Chris@45 177 * Add an imported (non-derived, non-main) model. This is
Chris@45 178 * necessary to register any imported model that is associated
Chris@45 179 * with a layer.
Chris@45 180 */
Chris@45 181 void addImportedModel(Model *);
Chris@45 182
Chris@45 183 /**
Chris@45 184 * Associate the given model with the given layer. The model must
Chris@45 185 * have already been registered using one of the addXXModel
Chris@45 186 * methods above.
Chris@45 187 */
Chris@45 188 void setModel(Layer *, Model *);
Chris@45 189
Chris@45 190 /**
Chris@45 191 * Set the given layer to use the given channel of its model (-1
Chris@45 192 * means all available channels).
Chris@45 193 */
Chris@45 194 void setChannel(Layer *, int);
Chris@45 195
Chris@45 196 /**
Chris@45 197 * Add the given layer to the given view. If the layer is
Chris@45 198 * intended to show a particular model, the model should normally
Chris@45 199 * be set using setModel before this method is called.
Chris@45 200 */
Chris@45 201 void addLayerToView(View *, Layer *);
Chris@45 202
Chris@45 203 /**
Chris@45 204 * Remove the given layer from the given view.
Chris@45 205 */
Chris@45 206 void removeLayerFromView(View *, Layer *);
Chris@45 207
Chris@48 208 /**
Chris@50 209 * Return true if alignment is supported (i.e. if the necessary
Chris@50 210 * plugin is found).
Chris@50 211 */
Chris@51 212 static bool canAlign();
Chris@50 213
Chris@50 214 /**
Chris@48 215 * Specify whether models added via addImportedModel should be
Chris@48 216 * automatically aligned against the main model if appropriate.
Chris@48 217 */
Chris@48 218 void setAutoAlignment(bool on) { m_autoAlignment = on; }
Chris@48 219
Chris@48 220 /**
Chris@48 221 * Generate alignments for all appropriate models against the main
Chris@48 222 * model. Existing alignments will not be re-calculated unless
Chris@48 223 * the main model has changed since they were calculated.
Chris@48 224 */
Chris@48 225 void alignModels();
Chris@48 226
Chris@45 227 void toXml(QTextStream &, QString indent, QString extraAttributes) const;
Chris@226 228 void toXmlAsTemplate(QTextStream &, QString indent, QString extraAttributes) const;
Chris@47 229
Chris@45 230 signals:
Chris@45 231 void layerAdded(Layer *);
Chris@45 232 void layerRemoved(Layer *);
Chris@45 233 void layerAboutToBeDeleted(Layer *);
Chris@45 234
Chris@45 235 // Emitted when a layer is first added to a view, or when it is
Chris@45 236 // last removed from a view
Chris@45 237 void layerInAView(Layer *, bool);
Chris@45 238
Chris@45 239 void modelAdded(Model *);
Chris@45 240 void mainModelChanged(WaveFileModel *); // emitted after modelAdded
Chris@45 241 void modelAboutToBeDeleted(Model *);
Chris@45 242
Chris@78 243 void modelGenerationFailed(QString transformName, QString message);
Chris@78 244 void modelGenerationWarning(QString transformName, QString message);
Chris@78 245 void modelRegenerationFailed(QString layerName, QString transformName,
Chris@78 246 QString message);
Chris@78 247 void modelRegenerationWarning(QString layerName, QString transformName,
Chris@78 248 QString message);
Chris@78 249 void alignmentFailed(QString transformName, QString message);
Chris@45 250
Chris@160 251 void activity(QString);
Chris@160 252
Chris@45 253 protected:
Chris@45 254 void releaseModel(Model *model);
Chris@45 255
Chris@45 256 /**
Chris@45 257 * If model is suitable for alignment, align it against the main
Chris@48 258 * model and store the alignment in the model. (If the model has
Chris@48 259 * an alignment already for the current main model, leave it
Chris@48 260 * unchanged.)
Chris@45 261 */
Chris@45 262 void alignModel(Model *);
Chris@45 263
Chris@45 264 /*
Chris@45 265 * Every model that is in use by a layer in the document must be
Chris@45 266 * found in either m_mainModel or m_models. We own and control
Chris@45 267 * the lifespan of all of these models.
Chris@45 268 */
Chris@45 269
Chris@45 270 /**
Chris@45 271 * The model that provides the underlying sample rate, etc. This
Chris@45 272 * model is not reference counted for layers, and is not freed
Chris@45 273 * unless it is replaced or the document is deleted.
Chris@45 274 */
Chris@45 275 WaveFileModel *m_mainModel;
Chris@45 276
Chris@45 277 struct ModelRecord
Chris@45 278 {
Chris@45 279 // Information associated with a non-main model. If this
Chris@45 280 // model is derived from another, then source will be non-NULL
Chris@45 281 // and the transform name will be set appropriately. If the
Chris@45 282 // transform name is set but source is NULL, then there was a
Chris@45 283 // transform involved but the (target) model has been modified
Chris@45 284 // since being generated from it.
Chris@72 285
Chris@72 286 // This does not use ModelTransformer::Input, because it would
Chris@72 287 // be confusing to have Input objects hanging around with NULL
Chris@72 288 // models in them.
Chris@72 289
Chris@45 290 const Model *source;
Chris@72 291 int channel;
Chris@72 292 Transform transform;
Chris@45 293
Chris@45 294 // Count of the number of layers using this model.
Chris@45 295 int refcount;
Chris@45 296 };
Chris@45 297
Chris@45 298 typedef std::map<Model *, ModelRecord> ModelMap;
Chris@45 299 ModelMap m_models;
Chris@45 300
Chris@45 301 class AddLayerCommand : public Command
Chris@45 302 {
Chris@45 303 public:
Chris@45 304 AddLayerCommand(Document *d, View *view, Layer *layer);
Chris@45 305 virtual ~AddLayerCommand();
Chris@45 306
Chris@45 307 virtual void execute();
Chris@45 308 virtual void unexecute();
Chris@159 309 virtual QString getName() const;
Chris@45 310
Chris@45 311 protected:
Chris@45 312 Document *m_d;
Chris@45 313 View *m_view; // I don't own this
Chris@83 314 Layer *m_layer; // Document owns this, but I determine its lifespan
Chris@45 315 QString m_name;
Chris@45 316 bool m_added;
Chris@45 317 };
Chris@45 318
Chris@45 319 class RemoveLayerCommand : public Command
Chris@45 320 {
Chris@45 321 public:
Chris@45 322 RemoveLayerCommand(Document *d, View *view, Layer *layer);
Chris@45 323 virtual ~RemoveLayerCommand();
Chris@45 324
Chris@45 325 virtual void execute();
Chris@45 326 virtual void unexecute();
Chris@159 327 virtual QString getName() const;
Chris@45 328
Chris@45 329 protected:
Chris@45 330 Document *m_d;
Chris@45 331 View *m_view; // I don't own this
Chris@45 332 Layer *m_layer; // Document owns this, but I determine its lifespan
Chris@45 333 QString m_name;
Chris@45 334 bool m_added;
Chris@45 335 };
Chris@45 336
Chris@45 337 typedef std::map<Layer *, std::set<View *> > LayerViewMap;
Chris@45 338 LayerViewMap m_layerViewMap;
Chris@45 339
Chris@45 340 void addToLayerViewMap(Layer *, View *);
Chris@45 341 void removeFromLayerViewMap(Layer *, View *);
Chris@45 342
Chris@45 343 QString getUniqueLayerName(QString candidate);
Chris@72 344 void writeBackwardCompatibleDerivation(QTextStream &, QString, Model *,
Chris@72 345 const ModelRecord &) const;
Chris@90 346
Chris@90 347 static TransformId getAlignmentTransformName();
Chris@45 348
Chris@226 349 void toXml(QTextStream &, QString, QString, bool asTemplate) const;
Chris@226 350 void writePlaceholderMainModel(QTextStream &, QString) const;
Chris@226 351
Chris@45 352 /**
Chris@45 353 * And these are the layers. We also control the lifespans of
Chris@45 354 * these (usually through the commands used to add and remove them).
Chris@45 355 */
Chris@45 356 typedef std::set<Layer *> LayerSet;
Chris@45 357 LayerSet m_layers;
Chris@47 358
Chris@47 359 bool m_autoAlignment;
Chris@45 360 };
Chris@45 361
Chris@45 362 #endif