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 "plugin/transform/Transform.h"
00021 #include "plugin/transform/ModelTransformer.h"
00022 #include "base/Command.h"
00023 
00024 #include <map>
00025 #include <set>
00026 
00027 class Model;
00028 class Layer;
00029 class View;
00030 class WaveFileModel;
00031 
00065 class Document : public QObject,
00066                  public XmlExportable
00067 {
00068     Q_OBJECT
00069 
00070 public:
00071     Document();
00072     virtual ~Document();
00073 
00080     Layer *createLayer(LayerFactory::LayerType);
00081 
00086     Layer *createMainModelLayer(LayerFactory::LayerType);
00087 
00092     Layer *createImportedLayer(Model *);
00093 
00099     Layer *createEmptyLayer(LayerFactory::LayerType);
00100 
00109     Layer *createDerivedLayer(LayerFactory::LayerType, TransformId);
00110 
00116     Layer *createDerivedLayer(const Transform &,
00117                               const ModelTransformer::Input &);
00118 
00125     void deleteLayer(Layer *, bool force = false);
00126 
00132     void setMainModel(WaveFileModel *);
00133 
00137     WaveFileModel *getMainModel() { return m_mainModel; }
00138 
00142     const WaveFileModel *getMainModel() const { return m_mainModel; }
00143 
00144     std::vector<Model *> getTransformInputModels();
00145 
00146     bool isKnownModel(const Model *) const;
00147 
00152     Model *addDerivedModel(const Transform &transform,
00153                            const ModelTransformer::Input &input,
00154                            QString &returnedMessage);
00155 
00161     void addDerivedModel(const Transform &transform,
00162                          const ModelTransformer::Input &input,
00163                          Model *outputModelToAdd);
00164 
00170     void addImportedModel(Model *);
00171 
00177     void setModel(Layer *, Model *);
00178 
00183     void setChannel(Layer *, int);
00184 
00190     void addLayerToView(View *, Layer *);
00191 
00195     void removeLayerFromView(View *, Layer *);
00196 
00201     static bool canAlign();
00202 
00207     void setAutoAlignment(bool on) { m_autoAlignment = on; }
00208 
00214     void alignModels();
00215 
00216     void toXml(QTextStream &, QString indent, QString extraAttributes) const;
00217 
00218 signals:
00219     void layerAdded(Layer *);
00220     void layerRemoved(Layer *);
00221     void layerAboutToBeDeleted(Layer *);
00222 
00223     // Emitted when a layer is first added to a view, or when it is
00224     // last removed from a view
00225     void layerInAView(Layer *, bool);
00226 
00227     void modelAdded(Model *);
00228     void mainModelChanged(WaveFileModel *); // emitted after modelAdded
00229     void modelAboutToBeDeleted(Model *);
00230 
00231     void modelGenerationFailed(QString transformName, QString message);
00232     void modelGenerationWarning(QString transformName, QString message);
00233     void modelRegenerationFailed(QString layerName, QString transformName,
00234                                  QString message);
00235     void modelRegenerationWarning(QString layerName, QString transformName,
00236                                   QString message);
00237     void alignmentFailed(QString transformName, QString message);
00238 
00239 protected:
00240     void releaseModel(Model *model);
00241 
00248     void alignModel(Model *);
00249 
00250     /*
00251      * Every model that is in use by a layer in the document must be
00252      * found in either m_mainModel or m_models.  We own and control
00253      * the lifespan of all of these models.
00254      */
00255 
00261     WaveFileModel *m_mainModel;
00262 
00263     struct ModelRecord
00264     {
00265         // Information associated with a non-main model.  If this
00266         // model is derived from another, then source will be non-NULL
00267         // and the transform name will be set appropriately.  If the
00268         // transform name is set but source is NULL, then there was a
00269         // transform involved but the (target) model has been modified
00270         // since being generated from it.
00271         
00272         // This does not use ModelTransformer::Input, because it would
00273         // be confusing to have Input objects hanging around with NULL
00274         // models in them.
00275 
00276         const Model *source;
00277         int channel;
00278         Transform transform;
00279 
00280         // Count of the number of layers using this model.
00281         int refcount;
00282     };
00283 
00284     typedef std::map<Model *, ModelRecord> ModelMap;
00285     ModelMap m_models;
00286 
00287     class AddLayerCommand : public Command
00288     {
00289     public:
00290         AddLayerCommand(Document *d, View *view, Layer *layer);
00291         virtual ~AddLayerCommand();
00292         
00293         virtual void execute();
00294         virtual void unexecute();
00295         virtual QString getName() const { return m_name; }
00296 
00297     protected:
00298         Document *m_d;
00299         View *m_view; // I don't own this
00300         Layer *m_layer; // Document owns this, but I determine its lifespan
00301         QString m_name;
00302         bool m_added;
00303     };
00304 
00305     class RemoveLayerCommand : public Command
00306     {
00307     public:
00308         RemoveLayerCommand(Document *d, View *view, Layer *layer);
00309         virtual ~RemoveLayerCommand();
00310         
00311         virtual void execute();
00312         virtual void unexecute();
00313         virtual QString getName() const { return m_name; }
00314 
00315     protected:
00316         Document *m_d;
00317         View *m_view; // I don't own this
00318         Layer *m_layer; // Document owns this, but I determine its lifespan
00319         QString m_name;
00320         bool m_added;
00321     };
00322 
00323     typedef std::map<Layer *, std::set<View *> > LayerViewMap;
00324     LayerViewMap m_layerViewMap;
00325 
00326     void addToLayerViewMap(Layer *, View *);
00327     void removeFromLayerViewMap(Layer *, View *);
00328 
00329     QString getUniqueLayerName(QString candidate);
00330     void writeBackwardCompatibleDerivation(QTextStream &, QString, Model *,
00331                                            const ModelRecord &) const;
00332 
00333     static TransformId getAlignmentTransformName();
00334     
00339     typedef std::set<Layer *> LayerSet;
00340     LayerSet m_layers;
00341 
00342     bool m_autoAlignment;
00343 };
00344 
00345 #endif

Generated on Wed Feb 20 15:45:25 2008 for SonicVisualiser by  doxygen 1.5.1