svapp  1.9
Document.h
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam and QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #ifndef _DOCUMENT_H_
00017 #define _DOCUMENT_H_
00018 
00019 #include "layer/LayerFactory.h"
00020 #include "transform/Transform.h"
00021 #include "transform/ModelTransformer.h"
00022 #include "transform/FeatureExtractionModelTransformer.h"
00023 #include "base/Command.h"
00024 
00025 #include <map>
00026 #include <set>
00027 
00028 class Model;
00029 class Layer;
00030 class View;
00031 class WaveFileModel;
00032 
00033 class AdditionalModelConverter;
00034 
00068 class Document : public QObject,
00069                  public XmlExportable
00070 {
00071     Q_OBJECT
00072 
00073 public:
00074     Document();
00075     virtual ~Document();
00076 
00083     Layer *createLayer(LayerFactory::LayerType);
00084 
00089     Layer *createMainModelLayer(LayerFactory::LayerType);
00090 
00095     Layer *createImportedLayer(Model *);
00096 
00102     Layer *createEmptyLayer(LayerFactory::LayerType);
00103 
00112     Layer *createDerivedLayer(LayerFactory::LayerType, TransformId);
00113 
00119     Layer *createDerivedLayer(const Transform &,
00120                               const ModelTransformer::Input &);
00121 
00128     std::vector<Layer *> createDerivedLayers(const Transforms &,
00129                                              const ModelTransformer::Input &);
00130 
00131     typedef void *LayerCreationAsyncHandle;
00132 
00133     class LayerCreationHandler {
00134     public:
00135         virtual ~LayerCreationHandler() { }
00136 
00146         virtual void layersCreated(LayerCreationAsyncHandle handle,
00147                                    std::vector<Layer *> primary,
00148                                    std::vector<Layer *> additional) = 0;
00149     };
00150 
00160     LayerCreationAsyncHandle createDerivedLayersAsync(const Transforms &,
00161                                                       const ModelTransformer::Input &,
00162                                                       LayerCreationHandler *handler);
00163 
00170     void cancelAsyncLayerCreation(LayerCreationAsyncHandle handle);
00171 
00178     void deleteLayer(Layer *, bool force = false);
00179 
00185     void setMainModel(WaveFileModel *);
00186 
00190     WaveFileModel *getMainModel() { return m_mainModel; }
00191 
00195     const WaveFileModel *getMainModel() const { return m_mainModel; }
00196 
00197     std::vector<Model *> getTransformInputModels();
00198 
00199     bool isKnownModel(const Model *) const;
00200 
00205     Model *addDerivedModel(const Transform &transform,
00206                            const ModelTransformer::Input &input,
00207                            QString &returnedMessage);
00208 
00214     friend class AdditionalModelConverter;
00215     std::vector<Model *> addDerivedModels(const Transforms &transforms,
00216                                           const ModelTransformer::Input &input,
00217                                           QString &returnedMessage,
00218                                           AdditionalModelConverter *);
00219 
00225     void addAlreadyDerivedModel(const Transform &transform,
00226                                 const ModelTransformer::Input &input,
00227                                 Model *outputModelToAdd);
00228 
00234     void addImportedModel(Model *);
00235 
00241     void setModel(Layer *, Model *);
00242 
00247     void setChannel(Layer *, int);
00248 
00254     void addLayerToView(View *, Layer *);
00255 
00261     void removeLayerFromView(View *, Layer *);
00262 
00267     static bool canAlign();
00268 
00273     void setAutoAlignment(bool on) { m_autoAlignment = on; }
00274 
00280     void alignModels();
00281 
00282     void toXml(QTextStream &, QString indent, QString extraAttributes) const;
00283     void toXmlAsTemplate(QTextStream &, QString indent, QString extraAttributes) const;
00284 
00285 signals:
00286     void layerAdded(Layer *);
00287     void layerRemoved(Layer *);
00288     void layerAboutToBeDeleted(Layer *);
00289 
00290     // Emitted when a layer is first added to a view, or when it is
00291     // last removed from a view
00292     void layerInAView(Layer *, bool);
00293 
00294     void modelAdded(Model *);
00295     void mainModelChanged(WaveFileModel *); // emitted after modelAdded
00296     void modelAboutToBeDeleted(Model *);
00297 
00298     void modelGenerationFailed(QString transformName, QString message);
00299     void modelGenerationWarning(QString transformName, QString message);
00300     void modelRegenerationFailed(QString layerName, QString transformName,
00301                                  QString message);
00302     void modelRegenerationWarning(QString layerName, QString transformName,
00303                                   QString message);
00304     void alignmentFailed(QString transformName, QString message);
00305 
00306     void activity(QString);
00307 
00308 protected:
00309     void releaseModel(Model *model);
00310 
00317     void alignModel(Model *);
00318 
00319     /*
00320      * Every model that is in use by a layer in the document must be
00321      * found in either m_mainModel or m_models.  We own and control
00322      * the lifespan of all of these models.
00323      */
00324 
00330     WaveFileModel *m_mainModel;
00331 
00332     struct ModelRecord
00333     {
00334         // Information associated with a non-main model.  If this
00335         // model is derived from another, then source will be non-NULL
00336         // and the transform name will be set appropriately.  If the
00337         // transform name is set but source is NULL, then there was a
00338         // transform involved but the (target) model has been modified
00339         // since being generated from it.
00340         
00341         // This does not use ModelTransformer::Input, because it would
00342         // be confusing to have Input objects hanging around with NULL
00343         // models in them.
00344 
00345         const Model *source;
00346         int channel;
00347         Transform transform;
00348         bool additional;
00349 
00350         // Count of the number of layers using this model.
00351         int refcount;
00352     };
00353 
00354     typedef std::map<Model *, ModelRecord> ModelMap;
00355     ModelMap m_models;
00356 
00361     void addAdditionalModel(Model *);
00362 
00363     class AddLayerCommand : public Command
00364     {
00365     public:
00366         AddLayerCommand(Document *d, View *view, Layer *layer);
00367         virtual ~AddLayerCommand();
00368         
00369         virtual void execute();
00370         virtual void unexecute();
00371         virtual QString getName() const;
00372 
00373     protected:
00374         Document *m_d;
00375         View *m_view; // I don't own this
00376         Layer *m_layer; // Document owns this, but I determine its lifespan
00377         QString m_name;
00378         bool m_added;
00379     };
00380 
00381     class RemoveLayerCommand : public Command
00382     {
00383     public:
00384         RemoveLayerCommand(Document *d, View *view, Layer *layer);
00385         virtual ~RemoveLayerCommand();
00386         
00387         virtual void execute();
00388         virtual void unexecute();
00389         virtual QString getName() const;
00390 
00391     protected:
00392         Document *m_d;
00393         View *m_view; // I don't own this
00394         Layer *m_layer; // Document owns this, but I determine its lifespan
00395         bool m_wasDormant;
00396         QString m_name;
00397         bool m_added;
00398     };
00399 
00400     typedef std::map<Layer *, std::set<View *> > LayerViewMap;
00401     LayerViewMap m_layerViewMap;
00402 
00403     void addToLayerViewMap(Layer *, View *);
00404     void removeFromLayerViewMap(Layer *, View *);
00405 
00406     QString getUniqueLayerName(QString candidate);
00407     void writeBackwardCompatibleDerivation(QTextStream &, QString, Model *,
00408                                            const ModelRecord &) const;
00409 
00410     static TransformId getAlignmentTransformName();
00411     
00412     void toXml(QTextStream &, QString, QString, bool asTemplate) const;
00413     void writePlaceholderMainModel(QTextStream &, QString) const;
00414 
00415     std::vector<Layer *> createLayersForDerivedModels(std::vector<Model *>,
00416                                                       QStringList names);
00417 
00422     typedef std::set<Layer *> LayerSet;
00423     LayerSet m_layers;
00424 
00425     bool m_autoAlignment;
00426 };
00427 
00428 #endif