Document.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam and QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef SV_DOCUMENT_H
17 #define SV_DOCUMENT_H
18 
19 #include "layer/LayerFactory.h"
20 #include "transform/Transform.h"
21 #include "transform/ModelTransformer.h"
22 #include "transform/FeatureExtractionModelTransformer.h"
23 #include "base/Command.h"
24 
25 #include <map>
26 #include <set>
27 
28 class Model;
29 class Layer;
30 class View;
31 class WaveFileModel;
32 class AggregateWaveModel;
33 
35 
36 class Align;
37 
71 class Document : public QObject,
72  public XmlExportable
73 {
74  Q_OBJECT
75 
76 public:
77  Document();
78  virtual ~Document();
79 
86  Layer *createLayer(LayerFactory::LayerType);
87 
92  Layer *createMainModelLayer(LayerFactory::LayerType);
93 
98  Layer *createImportedLayer(ModelId);
99 
105  Layer *createEmptyLayer(LayerFactory::LayerType);
106 
115  Layer *createDerivedLayer(LayerFactory::LayerType, TransformId);
116 
122  Layer *createDerivedLayer(const Transform &,
123  const ModelTransformer::Input &);
124 
131  std::vector<Layer *> createDerivedLayers(const Transforms &,
132  const ModelTransformer::Input &);
133 
135 
137  public:
138  virtual ~LayerCreationHandler() { }
139 
149  virtual void layersCreated(LayerCreationAsyncHandle handle,
150  std::vector<Layer *> primary,
151  std::vector<Layer *> additional) = 0;
152  };
153 
163  LayerCreationAsyncHandle createDerivedLayersAsync(const Transforms &,
164  const ModelTransformer::Input &,
165  LayerCreationHandler *handler);
166 
173  void cancelAsyncLayerCreation(LayerCreationAsyncHandle handle);
174 
181  void deleteLayer(Layer *, bool force = false);
182 
190  void setMainModel(ModelId); // a WaveFileModel
191 
195  ModelId getMainModel() { return m_mainModel; }
196 
197  std::vector<ModelId> getTransformInputModels();
198 
204  bool isKnownModel(ModelId) const;
205 
211  ModelId addDerivedModel(const Transform &transform,
212  const ModelTransformer::Input &input,
213  QString &returnedMessage);
214 
221  std::vector<ModelId> addDerivedModels(const Transforms &transforms,
222  const ModelTransformer::Input &input,
223  QString &returnedMessage,
225 
233  void addAlreadyDerivedModel(const Transform &transform,
234  const ModelTransformer::Input &input,
235  ModelId outputModelToAdd);
236 
248  void addNonDerivedModel(ModelId);
249 
255  void setModel(Layer *, ModelId);
256 
261  void setChannel(Layer *, int);
262 
268  void addLayerToView(View *, Layer *);
269 
275  void removeLayerFromView(View *, Layer *);
276 
281  static bool canAlign();
282 
287  void setAutoAlignment(bool on) { m_autoAlignment = on; }
288 
294  void alignModels();
295 
300  void realignModels();
301 
307  bool isIncomplete() const { return m_isIncomplete; }
308 
309  void setIncomplete(bool i) { m_isIncomplete = i; }
310 
311  void toXml(QTextStream &, QString indent, QString extraAttributes) const override;
312  void toXmlAsTemplate(QTextStream &, QString indent, QString extraAttributes) const;
313 
314 signals:
315  void layerAdded(Layer *);
316  void layerRemoved(Layer *);
317  void layerAboutToBeDeleted(Layer *);
318 
319  // Emitted when a layer is first added to a view, or when it is
320  // last removed from a view
321  void layerInAView(Layer *, bool);
322 
323  void modelAdded(ModelId);
324  void mainModelChanged(ModelId); // a WaveFileModel; emitted after modelAdded
325 
326  void modelGenerationFailed(QString transformName, QString message);
327  void modelGenerationWarning(QString transformName, QString message);
328  void modelRegenerationFailed(QString layerName, QString transformName,
329  QString message);
330  void modelRegenerationWarning(QString layerName, QString transformName,
331  QString message);
332 
333  void alignmentComplete(ModelId); // an AlignmentModel
334  void alignmentFailed(ModelId, QString message); // an AlignmentModel
335 
336  void activity(QString);
337 
338 protected slots:
339  void performDeferredAlignment(ModelId);
340 
341 protected:
342  void releaseModel(ModelId model);
343 
350  void alignModel(ModelId, bool forceRecalculate = false);
351 
352  /*
353  * Every model that is in use by a layer in the document must be
354  * found in either m_mainModel or m_models. We own and control
355  * the lifespan of all of these models.
356  */
357 
363  ModelId m_mainModel; // a WaveFileModel
364 
365  struct ModelRecord
366  {
367  // Information associated with a non-main model. If this
368  // model is derived from another, then source will be
369  // something other than None and the transform name will be
370  // set appropriately. If the transform is set but source is
371  // None, then there was a transform involved but the (target)
372  // model has been modified since being generated from it.
373 
374  // This does not use ModelTransformer::Input, because it would
375  // be confusing to have Input objects hanging around with None
376  // models in them.
377 
378  ModelId source; // may be None
379  int channel;
380  Transform transform;
382  };
383 
384  // These must be stored in increasing order of id (as in the
385  // ordered std::map), to ensure repeatability for automated tests
386  std::map<ModelId, ModelRecord> m_models;
387 
388  std::set<ModelId> m_aggregateModels;
389  std::set<ModelId> m_alignmentModels;
390 
395  void addAdditionalModel(ModelId);
396 
397  class AddLayerCommand : public Command
398  {
399  public:
400  AddLayerCommand(Document *d, View *view, Layer *layer);
401  virtual ~AddLayerCommand();
402 
403  void execute() override;
404  void unexecute() override;
405  QString getName() const override;
406 
407  protected:
409  View *m_view; // I don't own this
410  Layer *m_layer; // Document owns this, but I determine its lifespan
411  QString m_name;
412  bool m_added;
413  };
414 
415  class RemoveLayerCommand : public Command
416  {
417  public:
418  RemoveLayerCommand(Document *d, View *view, Layer *layer);
419  virtual ~RemoveLayerCommand();
420 
421  void execute() override;
422  void unexecute() override;
423  QString getName() const override;
424 
425  protected:
427  View *m_view; // I don't own this
428  Layer *m_layer; // Document owns this, but I determine its lifespan
430  QString m_name;
431  bool m_added;
432  };
433 
434  typedef std::map<Layer *, std::set<View *> > LayerViewMap;
435  LayerViewMap m_layerViewMap;
436 
437  void addToLayerViewMap(Layer *, View *);
438  void removeFromLayerViewMap(Layer *, View *);
439 
440  QString getUniqueLayerName(QString candidate);
441  void writeBackwardCompatibleDerivation(QTextStream &, QString, ModelId,
442  const ModelRecord &) const;
443 
444  void toXml(QTextStream &, QString, QString, bool asTemplate) const;
445  void writePlaceholderMainModel(QTextStream &, QString) const;
446 
447  std::vector<Layer *> createLayersForDerivedModels(std::vector<ModelId>,
448  QStringList names);
449 
454  typedef std::vector<Layer *> LayerList;
455  LayerList m_layers;
456 
458  Align *m_align;
459 
461 };
462 
463 #endif
void setMainModel(ModelId)
Set the main model (the source for playback sample rate, etc) to the given wave file model...
Definition: Document.cpp:421
std::vector< ModelId > getTransformInputModels()
Definition: Document.cpp:1083
void setChannel(Layer *, int)
Set the given layer to use the given channel of its model (-1 means all available channels)...
Definition: Document.cpp:987
void writeBackwardCompatibleDerivation(QTextStream &, QString, ModelId, const ModelRecord &) const
Definition: Document.cpp:1568
void writePlaceholderMainModel(QTextStream &, QString) const
Definition: Document.cpp:1557
LayerList m_layers
Definition: Document.h:455
void mainModelChanged(ModelId)
virtual ~Document()
Definition: Document.cpp:65
void cancelAsyncLayerCreation(LayerCreationAsyncHandle handle)
Indicate that the async layer creation task associated with the given handle should be cancelled...
Definition: Document.cpp:362
void activity(QString)
Transform transform
Definition: Document.h:380
void layerAboutToBeDeleted(Layer *)
void layerRemoved(Layer *)
LayerCreationAsyncHandle createDerivedLayersAsync(const Transforms &, const ModelTransformer::Input &, LayerCreationHandler *handler)
Create suitable layers for the given transforms, which must be identical apart from the output (i...
Definition: Document.cpp:324
void modelRegenerationWarning(QString layerName, QString transformName, QString message)
std::map< Layer *, std::set< View * > > LayerViewMap
Definition: Document.h:434
void * LayerCreationAsyncHandle
Definition: Document.h:134
void addNonDerivedModel(ModelId)
Add an imported model, i.e.
Definition: Document.cpp:645
Layer * createMainModelLayer(LayerFactory::LayerType)
Create and return a new layer of the given type, associated with the current main model (if appropria...
Definition: Document.cpp:140
void modelGenerationFailed(QString transformName, QString message)
Layer * createLayer(LayerFactory::LayerType)
Create and return a new layer of the given type, associated with no model.
Definition: Document.cpp:118
void performDeferredAlignment(ModelId)
Definition: Document.cpp:1194
bool m_isIncomplete
Definition: Document.h:460
void layerAdded(Layer *)
void realignModels()
Re-generate alignments for all appropriate models against the main model.
Definition: Document.cpp:1210
Document()
!! still need to handle command history, documentRestored/documentModified
Definition: Document.cpp:48
void modelAdded(ModelId)
ModelId getMainModel()
Get the main model (the source for playback sample rate, etc).
Definition: Document.h:195
void alignModels()
Generate alignments for all appropriate models against the main model.
Definition: Document.cpp:1201
void releaseModel(ModelId model)
Definition: Document.cpp:801
void addToLayerViewMap(Layer *, View *)
Definition: Document.cpp:1025
void addAdditionalModel(ModelId)
Add an extra derived model (returned at the end of processing a transform).
Definition: Document.cpp:697
void toXmlAsTemplate(QTextStream &, QString indent, QString extraAttributes) const
Definition: Document.cpp:1351
void modelRegenerationFailed(QString layerName, QString transformName, QString message)
void removeLayerFromView(View *, Layer *)
Remove the given layer from the given view.
Definition: Document.cpp:1018
void alignModel(ModelId, bool forceRecalculate=false)
If model is suitable for alignment, align it against the main model and store the alignment in the mo...
Definition: Document.cpp:1124
ModelId m_mainModel
The model that provides the underlying sample rate, etc.
Definition: Document.h:363
Layer * createEmptyLayer(LayerFactory::LayerType)
Create and return a new layer of the given type, with an appropriate empty model. ...
Definition: Document.cpp:189
std::vector< Layer * > createLayersForDerivedModels(std::vector< ModelId >, QStringList names)
Definition: Document.cpp:369
QString getUniqueLayerName(QString candidate)
Definition: Document.cpp:1061
void layerInAView(Layer *, bool)
void toXml(QTextStream &, QString indent, QString extraAttributes) const override
Definition: Document.cpp:1345
bool isKnownModel(ModelId) const
Return true if the model id is known to be the main model or one of the other existing models that ca...
Definition: Document.cpp:1108
void alignmentFailed(ModelId, QString message)
static bool canAlign()
Return true if alignment is supported (i.e.
Definition: Document.cpp:1118
std::set< ModelId > m_alignmentModels
Definition: Document.h:389
std::map< ModelId, ModelRecord > m_models
Definition: Document.h:386
Layer * createDerivedLayer(LayerFactory::LayerType, TransformId)
Create and return a new layer of the given type, associated with the given transform name...
Definition: Document.cpp:212
A Sonic Visualiser document consists of a set of data models, and also the visualisation layers used ...
Definition: Document.h:71
bool isIncomplete() const
Return true if any external files (most obviously audio) failed to be found on load, so that the document is incomplete compared to its saved description.
Definition: Document.h:307
void modelGenerationWarning(QString transformName, QString message)
bool m_autoAlignment
Definition: Document.h:457
void addLayerToView(View *, Layer *)
Add the given layer to the given view.
Definition: Document.cpp:993
void setAutoAlignment(bool on)
Specify whether models added via addImportedModel should be automatically aligned against the main mo...
Definition: Document.h:287
void setModel(Layer *, ModelId)
Associate the given model with the given layer.
Definition: Document.cpp:951
void removeFromLayerViewMap(Layer *, View *)
Definition: Document.cpp:1043
std::vector< Layer * > createDerivedLayers(const Transforms &, const ModelTransformer::Input &)
Create and return suitable layers for the given transforms, which must be identical apart from the ou...
Definition: Document.cpp:241
LayerViewMap m_layerViewMap
Definition: Document.h:435
virtual void layersCreated(LayerCreationAsyncHandle handle, std::vector< Layer * > primary, std::vector< Layer * > additional)=0
The primary layers are those corresponding 1-1 to the input models, listed in the same order as the i...
void setIncomplete(bool i)
Definition: Document.h:309
void alignmentComplete(ModelId)
void addAlreadyDerivedModel(const Transform &transform, const ModelTransformer::Input &input, ModelId outputModelToAdd)
Add a derived model associated with the given transform.
Definition: Document.cpp:604
Align * m_align
Definition: Document.h:458
void deleteLayer(Layer *, bool force=false)
Delete the given layer, and also its associated model if no longer used by any other layer...
Definition: Document.cpp:889
ModelId addDerivedModel(const Transform &transform, const ModelTransformer::Input &input, QString &returnedMessage)
Add a derived model associated with the given transform, running the transform and returning the resu...
Definition: Document.cpp:733
std::set< ModelId > m_aggregateModels
Definition: Document.h:388
Layer * createImportedLayer(ModelId)
Create and return a new layer associated with the given model, and register the model as an imported ...
Definition: Document.cpp:151
std::vector< Layer * > LayerList
And these are the layers.
Definition: Document.h:454
std::vector< ModelId > addDerivedModels(const Transforms &transforms, const ModelTransformer::Input &input, QString &returnedMessage, AdditionalModelConverter *)
Definition: Document.cpp:756