Chris@45: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@45: Chris@45: /* Chris@45: Sonic Visualiser Chris@45: An audio file viewer and annotation editor. Chris@45: Centre for Digital Music, Queen Mary, University of London. Chris@45: This file copyright 2006 Chris Cannam and QMUL. Chris@45: Chris@45: This program is free software; you can redistribute it and/or Chris@45: modify it under the terms of the GNU General Public License as Chris@45: published by the Free Software Foundation; either version 2 of the Chris@45: License, or (at your option) any later version. See the file Chris@45: COPYING included with this distribution for more information. Chris@45: */ Chris@45: Chris@45: #include "Document.h" Chris@45: Chris@420: #include "Align.h" Chris@420: Chris@45: #include "data/model/WaveFileModel.h" Chris@45: #include "data/model/WritableWaveFileModel.h" Chris@45: #include "data/model/DenseThreeDimensionalModel.h" Chris@45: #include "data/model/DenseTimeValueModel.h" gyorgyf@270: #include "data/model/FlexiNoteModel.h" Chris@588: #include "data/model/AggregateWaveModel.h" gyorgyf@270: Chris@45: #include "layer/Layer.h" Chris@105: #include "widgets/CommandHistory.h" Chris@45: #include "base/Command.h" Chris@45: #include "view/View.h" Chris@45: #include "base/PlayParameterRepository.h" Chris@45: #include "base/PlayParameters.h" Chris@106: #include "transform/TransformFactory.h" Chris@106: #include "transform/ModelTransformerFactory.h" gyorgyf@270: #include "transform/FeatureExtractionModelTransformer.h" Chris@45: #include Chris@45: #include Chris@90: #include Chris@45: #include Chris@212: #include Chris@45: Chris@45: #include "data/model/AlignmentModel.h" Chris@423: #include "Align.h" Chris@45: Chris@297: using std::vector; Chris@297: Chris@138: //#define DEBUG_DOCUMENT 1 Chris@77: Chris@45: //!!! still need to handle command history, documentRestored/documentModified Chris@45: Chris@45: Document::Document() : Chris@47: m_mainModel(0), Chris@423: m_autoAlignment(false), Chris@601: m_align(new Align()), Chris@601: m_isIncomplete(false) Chris@45: { Chris@456: connect(this, Chris@456: SIGNAL(modelAboutToBeDeleted(Model *)), Chris@54: ModelTransformerFactory::getInstance(), Chris@45: SLOT(modelAboutToBeDeleted(Model *))); Chris@456: Chris@456: connect(ModelTransformerFactory::getInstance(), Chris@456: SIGNAL(transformFailed(QString, QString)), Chris@456: this, Chris@456: SIGNAL(modelGenerationFailed(QString, QString))); Chris@459: Chris@428: connect(m_align, SIGNAL(alignmentComplete(AlignmentModel *)), Chris@428: this, SIGNAL(alignmentComplete(AlignmentModel *))); Chris@45: } Chris@45: Chris@45: Document::~Document() Chris@45: { Chris@45: //!!! Document should really own the command history. atm we Chris@45: //still refer to it in various places that don't have access to Chris@45: //the document, be nice to fix that Chris@45: Chris@77: #ifdef DEBUG_DOCUMENT Chris@293: cerr << "\n\nDocument::~Document: about to clear command history" << endl; Chris@77: #endif Chris@45: CommandHistory::getInstance()->clear(); Chris@45: Chris@77: #ifdef DEBUG_DOCUMENT Chris@233: SVDEBUG << "Document::~Document: about to delete layers" << endl; Chris@77: #endif Chris@45: while (!m_layers.empty()) { Chris@595: deleteLayer(*m_layers.begin(), true); Chris@45: } Chris@45: Chris@45: if (!m_models.empty()) { Chris@595: SVDEBUG << "Document::~Document: WARNING: " Chris@595: << m_models.size() << " model(s) still remain -- " Chris@595: << "should have been garbage collected when deleting layers" Chris@595: << endl; Chris@595: while (!m_models.empty()) { Chris@45: Model *model = m_models.begin()->first; Chris@595: if (model == m_mainModel) { Chris@595: // just in case! Chris@595: SVDEBUG << "Document::~Document: WARNING: Main model is also" Chris@595: << " in models list!" << endl; Chris@595: } else if (model) { Chris@79: model->aboutToDelete(); Chris@595: emit modelAboutToBeDeleted(model); Chris@595: delete model; Chris@595: } Chris@595: m_models.erase(m_models.begin()); Chris@595: } Chris@45: } Chris@45: Chris@77: #ifdef DEBUG_DOCUMENT Chris@233: SVDEBUG << "Document::~Document: About to get rid of main model" Chris@595: << endl; Chris@77: #endif Chris@45: if (m_mainModel) { Chris@79: m_mainModel->aboutToDelete(); Chris@45: emit modelAboutToBeDeleted(m_mainModel); Chris@45: } Chris@45: Chris@45: emit mainModelChanged(0); Chris@45: delete m_mainModel; Chris@45: } Chris@45: Chris@45: Layer * Chris@45: Document::createLayer(LayerFactory::LayerType type) Chris@45: { Chris@45: Layer *newLayer = LayerFactory::getInstance()->createLayer(type); Chris@45: if (!newLayer) return 0; Chris@45: Chris@45: newLayer->setObjectName(getUniqueLayerName(newLayer->objectName())); Chris@45: Chris@45: m_layers.insert(newLayer); Chris@52: Chris@77: #ifdef DEBUG_DOCUMENT Chris@233: SVDEBUG << "Document::createLayer: Added layer of type " << type Chris@229: << ", now have " << m_layers.size() << " layers" << endl; Chris@77: #endif Chris@52: Chris@45: emit layerAdded(newLayer); Chris@45: Chris@45: return newLayer; Chris@45: } Chris@45: Chris@45: Layer * Chris@45: Document::createMainModelLayer(LayerFactory::LayerType type) Chris@45: { Chris@45: Layer *newLayer = createLayer(type); Chris@45: if (!newLayer) return 0; Chris@45: setModel(newLayer, m_mainModel); Chris@45: return newLayer; Chris@45: } Chris@45: Chris@45: Layer * Chris@45: Document::createImportedLayer(Model *model) Chris@45: { Chris@45: LayerFactory::LayerTypeSet types = Chris@595: LayerFactory::getInstance()->getValidLayerTypes(model); Chris@45: Chris@45: if (types.empty()) { Chris@595: cerr << "WARNING: Document::importLayer: no valid display layer for model" << endl; Chris@595: return 0; Chris@45: } Chris@45: Chris@45: //!!! for now, just use the first suitable layer type Chris@45: LayerFactory::LayerType type = *types.begin(); Chris@45: Chris@45: Layer *newLayer = LayerFactory::getInstance()->createLayer(type); Chris@45: if (!newLayer) return 0; Chris@45: Chris@45: newLayer->setObjectName(getUniqueLayerName(newLayer->objectName())); Chris@45: Chris@45: addImportedModel(model); Chris@45: setModel(newLayer, model); Chris@45: Chris@45: //!!! and all channels Chris@45: setChannel(newLayer, -1); Chris@45: Chris@45: m_layers.insert(newLayer); Chris@52: Chris@77: #ifdef DEBUG_DOCUMENT Chris@233: SVDEBUG << "Document::createImportedLayer: Added layer of type " << type Chris@229: << ", now have " << m_layers.size() << " layers" << endl; Chris@77: #endif Chris@52: Chris@45: emit layerAdded(newLayer); Chris@45: return newLayer; Chris@45: } Chris@45: Chris@45: Layer * Chris@45: Document::createEmptyLayer(LayerFactory::LayerType type) Chris@45: { Chris@61: if (!m_mainModel) return 0; Chris@61: Chris@45: Model *newModel = Chris@595: LayerFactory::getInstance()->createEmptyModel(type, m_mainModel); Chris@45: if (!newModel) return 0; Chris@45: Chris@45: Layer *newLayer = createLayer(type); Chris@45: if (!newLayer) { Chris@595: delete newModel; Chris@595: return 0; Chris@45: } Chris@45: Chris@45: addImportedModel(newModel); Chris@45: setModel(newLayer, newModel); Chris@45: Chris@45: return newLayer; Chris@45: } Chris@45: Chris@45: Layer * Chris@45: Document::createDerivedLayer(LayerFactory::LayerType type, Chris@595: TransformId transform) Chris@45: { Chris@45: Layer *newLayer = createLayer(type); Chris@45: if (!newLayer) return 0; Chris@45: Chris@45: newLayer->setObjectName(getUniqueLayerName Chris@54: (TransformFactory::getInstance()-> Chris@54: getTransformFriendlyName(transform))); Chris@45: Chris@45: return newLayer; Chris@45: } Chris@297: Chris@45: Layer * Chris@72: Document::createDerivedLayer(const Transform &transform, Chris@72: const ModelTransformer::Input &input) Chris@45: { Chris@297: Transforms transforms; Chris@297: transforms.push_back(transform); Chris@297: vector layers = createDerivedLayers(transforms, input); Chris@297: if (layers.empty()) return 0; Chris@297: else return layers[0]; Chris@297: } Chris@297: Chris@297: vector Chris@297: Document::createDerivedLayers(const Transforms &transforms, Chris@297: const ModelTransformer::Input &input) Chris@297: { Chris@78: QString message; Chris@329: vector newModels = addDerivedModels(transforms, input, message, 0); Chris@297: Chris@297: if (newModels.empty()) { Chris@297: //!!! This identifier may be wrong! Chris@297: emit modelGenerationFailed(transforms[0].getIdentifier(), message); Chris@297: return vector(); Chris@78: } else if (message != "") { Chris@297: //!!! This identifier may be wrong! Chris@297: emit modelGenerationWarning(transforms[0].getIdentifier(), message); Chris@45: } Chris@45: Chris@329: QStringList names; Chris@366: for (int i = 0; i < (int)newModels.size(); ++i) { Chris@329: names.push_back(getUniqueLayerName Chris@329: (TransformFactory::getInstance()-> Chris@329: getTransformFriendlyName Chris@329: (transforms[i].getIdentifier()))); Chris@329: } Chris@329: Chris@329: vector layers = createLayersForDerivedModels(newModels, names); Chris@329: return layers; Chris@329: } Chris@329: Chris@329: class AdditionalModelConverter : Chris@329: public ModelTransformerFactory::AdditionalModelHandler Chris@329: { Chris@329: public: Chris@329: AdditionalModelConverter(Document *doc, Chris@329: Document::LayerCreationHandler *handler) : Chris@329: m_doc(doc), Chris@329: m_handler(handler) { Chris@329: } Chris@329: Chris@329: virtual ~AdditionalModelConverter() { } Chris@329: Chris@329: void Chris@329: setPrimaryLayers(vector layers) { Chris@329: m_primary = layers; Chris@329: } Chris@329: Chris@329: void Chris@329: moreModelsAvailable(vector models) { Chris@329: std::cerr << "AdditionalModelConverter::moreModelsAvailable: " << models.size() << " model(s)" << std::endl; Chris@329: // We can't automatically regenerate the additional models on Chris@329: // reload -- we should delete them instead Chris@329: QStringList names; Chris@329: foreach (Model *model, models) { Chris@329: m_doc->addAdditionalModel(model); Chris@329: names.push_back(QString()); Chris@329: } Chris@329: vector layers = m_doc->createLayersForDerivedModels Chris@329: (models, names); Chris@363: m_handler->layersCreated(this, m_primary, layers); Chris@329: delete this; Chris@329: } Chris@329: Chris@329: void Chris@329: noMoreModelsAvailable() { Chris@329: std::cerr << "AdditionalModelConverter::noMoreModelsAvailable" << std::endl; Chris@363: m_handler->layersCreated(this, m_primary, vector()); Chris@329: delete this; Chris@329: } Chris@329: Chris@363: void cancel() { Chris@363: foreach (Layer *layer, m_primary) { Chris@363: Model *model = layer->getModel(); Chris@363: if (model) { Chris@363: model->abandon(); Chris@363: } Chris@363: } Chris@363: } Chris@363: Chris@329: private: Chris@329: Document *m_doc; Chris@329: vector m_primary; Chris@329: Document::LayerCreationHandler *m_handler; //!!! how to handle destruction of this? Chris@329: }; Chris@329: Chris@363: Document::LayerCreationAsyncHandle Chris@329: Document::createDerivedLayersAsync(const Transforms &transforms, Chris@329: const ModelTransformer::Input &input, Chris@329: LayerCreationHandler *handler) Chris@329: { Chris@329: QString message; Chris@329: Chris@329: AdditionalModelConverter *amc = new AdditionalModelConverter(this, handler); Chris@329: Chris@329: vector newModels = addDerivedModels Chris@329: (transforms, input, message, amc); Chris@329: Chris@329: QStringList names; Chris@366: for (int i = 0; i < (int)newModels.size(); ++i) { Chris@329: names.push_back(getUniqueLayerName Chris@329: (TransformFactory::getInstance()-> Chris@329: getTransformFriendlyName Chris@329: (transforms[i].getIdentifier()))); Chris@329: } Chris@329: Chris@329: vector layers = createLayersForDerivedModels(newModels, names); Chris@329: amc->setPrimaryLayers(layers); Chris@329: Chris@329: if (newModels.empty()) { Chris@329: //!!! This identifier may be wrong! Chris@329: emit modelGenerationFailed(transforms[0].getIdentifier(), message); Chris@363: //!!! what to do with amc? Chris@329: } else if (message != "") { Chris@329: //!!! This identifier may be wrong! Chris@329: emit modelGenerationWarning(transforms[0].getIdentifier(), message); Chris@363: //!!! what to do with amc? Chris@329: } Chris@363: Chris@363: return amc; Chris@363: } Chris@363: Chris@363: void Chris@363: Document::cancelAsyncLayerCreation(Document::LayerCreationAsyncHandle h) Chris@363: { Chris@363: AdditionalModelConverter *conv = static_cast(h); Chris@363: conv->cancel(); Chris@329: } Chris@329: Chris@329: vector Chris@329: Document::createLayersForDerivedModels(vector newModels, Chris@329: QStringList names) Chris@329: { Chris@297: vector layers; Chris@329: Chris@297: for (int i = 0; i < (int)newModels.size(); ++i) { Chris@297: Chris@297: Model *newModel = newModels[i]; Chris@297: Chris@297: LayerFactory::LayerTypeSet types = Chris@297: LayerFactory::getInstance()->getValidLayerTypes(newModel); Chris@297: Chris@297: if (types.empty()) { Chris@329: cerr << "WARNING: Document::createLayerForTransformer: no valid display layer for output of transform " << names[i] << endl; Chris@297: //!!! inadequate cleanup: Chris@297: newModel->aboutToDelete(); Chris@297: emit modelAboutToBeDeleted(newModel); Chris@297: m_models.erase(newModel); Chris@297: delete newModel; Chris@297: return vector(); Chris@297: } Chris@297: Chris@297: //!!! for now, just use the first suitable layer type Chris@297: Chris@297: Layer *newLayer = createLayer(*types.begin()); Chris@297: setModel(newLayer, newModel); Chris@297: Chris@297: //!!! We need to clone the model when adding the layer, so that it Chris@297: //can be edited without affecting other layers that are based on Chris@297: //the same model. Unfortunately we can't just clone it now, Chris@297: //because it probably hasn't been completed yet -- the transform Chris@297: //runs in the background. Maybe the transform has to handle Chris@297: //cloning and cacheing models itself. Chris@297: // Chris@297: // Once we do clone models here, of course, we'll have to avoid Chris@297: // leaking them too. Chris@297: // Chris@297: // We want the user to be able to add a model to a second layer Chris@297: // _while it's still being calculated in the first_ and have it Chris@297: // work quickly. That means we need to put the same physical Chris@297: // model pointer in both layers, so they can't actually be cloned. Chris@297: Chris@297: if (newLayer) { Chris@329: newLayer->setObjectName(names[i]); Chris@297: } Chris@297: Chris@297: emit layerAdded(newLayer); Chris@297: layers.push_back(newLayer); Chris@45: } Chris@45: Chris@297: return layers; Chris@45: } Chris@45: Chris@45: void Chris@45: Document::setMainModel(WaveFileModel *model) Chris@45: { Chris@45: Model *oldMainModel = m_mainModel; Chris@45: m_mainModel = model; Chris@160: Chris@45: emit modelAdded(m_mainModel); Chris@160: if (model) { Chris@160: emit activity(tr("Set main model to %1").arg(model->objectName())); Chris@160: } else { Chris@160: emit activity(tr("Clear main model")); Chris@160: } Chris@45: Chris@45: std::vector obsoleteLayers; Chris@53: std::set failedTransformers; Chris@45: Chris@45: // We need to ensure that no layer is left using oldMainModel or Chris@45: // any of the old derived models as its model. Either replace the Chris@45: // model, or delete the layer for each layer that is currently Chris@45: // using one of these. Carry out this replacement before we Chris@45: // delete any of the models. Chris@45: Chris@77: #ifdef DEBUG_DOCUMENT Chris@293: cerr << "Document::setMainModel: Have " Chris@293: << m_layers.size() << " layers" << endl; Chris@293: cerr << "Models now: "; Chris@137: for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@293: cerr << i->first << " "; Chris@137: } Chris@293: cerr << endl; Chris@293: cerr << "Old main model: " << oldMainModel << endl; Chris@77: #endif Chris@52: Chris@45: for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@45: Chris@595: Layer *layer = *i; Chris@595: Model *model = layer->getModel(); Chris@45: Chris@77: #ifdef DEBUG_DOCUMENT Chris@293: cerr << "Document::setMainModel: inspecting model " Chris@229: << (model ? model->objectName(): "(null)") << " in layer " Chris@293: << layer->objectName() << endl; Chris@77: #endif Chris@45: Chris@595: if (model == oldMainModel) { Chris@77: #ifdef DEBUG_DOCUMENT Chris@293: cerr << "... it uses the old main model, replacing" << endl; Chris@77: #endif Chris@595: LayerFactory::getInstance()->setModel(layer, m_mainModel); Chris@595: continue; Chris@595: } Chris@45: Chris@137: if (!model) { Chris@293: cerr << "WARNING: Document::setMainModel: Null model in layer " Chris@293: << layer << endl; Chris@595: // get rid of this hideous degenerate Chris@595: obsoleteLayers.push_back(layer); Chris@595: continue; Chris@595: } Chris@137: Chris@595: if (m_models.find(model) == m_models.end()) { Chris@595: cerr << "WARNING: Document::setMainModel: Unknown model " Chris@595: << model << " in layer " << layer << endl; Chris@595: // and this one Chris@595: obsoleteLayers.push_back(layer); Chris@595: continue; Chris@595: } Chris@595: Chris@595: if (m_models[model].source && Chris@70: (m_models[model].source == oldMainModel)) { Chris@45: Chris@77: #ifdef DEBUG_DOCUMENT Chris@293: cerr << "... it uses a model derived from the old main model, regenerating" << endl; Chris@77: #endif Chris@45: Chris@595: // This model was derived from the previous main Chris@595: // model: regenerate it. Chris@595: Chris@595: const Transform &transform = m_models[model].transform; Chris@72: QString transformId = transform.getIdentifier(); Chris@595: Chris@72: //!!! We have a problem here if the number of channels in Chris@72: //the main model has changed. Chris@72: Chris@78: QString message; Chris@595: Model *replacementModel = Chris@45: addDerivedModel(transform, Chris@72: ModelTransformer::Input Chris@78: (m_mainModel, m_models[model].channel), Chris@78: message); Chris@595: Chris@595: if (!replacementModel) { Chris@595: cerr << "WARNING: Document::setMainModel: Failed to regenerate model for transform \"" Chris@595: << transformId << "\"" << " in layer " << layer << endl; Chris@72: if (failedTransformers.find(transformId) Chris@72: == failedTransformers.end()) { Chris@45: emit modelRegenerationFailed(layer->objectName(), Chris@78: transformId, Chris@78: message); Chris@72: failedTransformers.insert(transformId); Chris@45: } Chris@595: obsoleteLayers.push_back(layer); Chris@595: } else { Chris@78: if (message != "") { Chris@78: emit modelRegenerationWarning(layer->objectName(), Chris@78: transformId, Chris@78: message); Chris@78: } Chris@77: #ifdef DEBUG_DOCUMENT Chris@293: cerr << "Replacing model " << model << " (type " Chris@77: << typeid(*model).name() << ") with model " Chris@77: << replacementModel << " (type " Chris@77: << typeid(*replacementModel).name() << ") in layer " Chris@228: << layer << " (name " << layer->objectName() << ")" Chris@293: << endl; Chris@366: Chris@45: RangeSummarisableTimeValueModel *rm = Chris@45: dynamic_cast(replacementModel); Chris@45: if (rm) { Chris@293: cerr << "new model has " << rm->getChannelCount() << " channels " << endl; Chris@45: } else { Chris@293: cerr << "new model " << replacementModel << " is not a RangeSummarisableTimeValueModel!" << endl; Chris@45: } Chris@77: #endif Chris@595: setModel(layer, replacementModel); Chris@595: } Chris@595: } Chris@45: } Chris@45: Chris@45: for (size_t k = 0; k < obsoleteLayers.size(); ++k) { Chris@595: deleteLayer(obsoleteLayers[k], true); Chris@45: } Chris@45: Chris@48: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@329: if (i->second.additional) { Chris@329: Model *m = i->first; Chris@588: m->aboutToDelete(); Chris@329: emit modelAboutToBeDeleted(m); Chris@329: delete m; Chris@329: } Chris@329: } Chris@329: Chris@329: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@86: Chris@137: Model *m = i->first; Chris@137: Chris@137: #ifdef DEBUG_DOCUMENT Chris@233: SVDEBUG << "considering alignment for model " << m << " (name \"" Chris@229: << m->objectName() << "\")" << endl; Chris@137: #endif Chris@137: Chris@86: if (m_autoAlignment) { Chris@86: Chris@137: alignModel(m); Chris@86: Chris@86: } else if (oldMainModel && Chris@137: (m->getAlignmentReference() == oldMainModel)) { Chris@86: Chris@137: alignModel(m); Chris@48: } Chris@48: } Chris@48: Chris@77: if (oldMainModel) { Chris@79: oldMainModel->aboutToDelete(); Chris@77: emit modelAboutToBeDeleted(oldMainModel); Chris@77: } Chris@77: Chris@86: if (m_autoAlignment) { Chris@387: SVDEBUG << "Document::setMainModel: auto-alignment is on, aligning model if possible" << endl; Chris@86: alignModel(m_mainModel); Chris@86: } Chris@86: Chris@45: emit mainModelChanged(m_mainModel); Chris@45: Chris@45: delete oldMainModel; Chris@45: } Chris@45: Chris@45: void Chris@329: Document::addAlreadyDerivedModel(const Transform &transform, Chris@329: const ModelTransformer::Input &input, Chris@329: Model *outputModelToAdd) Chris@45: { Chris@45: if (m_models.find(outputModelToAdd) != m_models.end()) { Chris@595: cerr << "WARNING: Document::addAlreadyDerivedModel: Model already added" Chris@595: << endl; Chris@595: return; Chris@45: } Chris@45: Chris@77: #ifdef DEBUG_DOCUMENT Chris@248: if (input.getModel()) { Chris@329: cerr << "Document::addAlreadyDerivedModel: source is " << input.getModel() << " \"" << input.getModel()->objectName() << "\"" << endl; Chris@248: } else { Chris@329: cerr << "Document::addAlreadyDerivedModel: source is " << input.getModel() << endl; Chris@248: } Chris@77: #endif Chris@45: Chris@45: ModelRecord rec; Chris@72: rec.source = input.getModel(); Chris@72: rec.channel = input.getChannel(); Chris@45: rec.transform = transform; Chris@329: rec.additional = false; Chris@45: rec.refcount = 0; Chris@45: Chris@72: outputModelToAdd->setSourceModel(input.getModel()); Chris@45: Chris@45: m_models[outputModelToAdd] = rec; Chris@45: Chris@137: #ifdef DEBUG_DOCUMENT Chris@329: cerr << "Document::addAlreadyDerivedModel: Added model " << outputModelToAdd << endl; Chris@293: cerr << "Models now: "; Chris@137: for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@293: cerr << i->first << " "; Chris@137: } Chris@293: cerr << endl; Chris@137: #endif Chris@137: Chris@45: emit modelAdded(outputModelToAdd); Chris@45: } Chris@45: Chris@45: Chris@45: void Chris@45: Document::addImportedModel(Model *model) Chris@45: { Chris@45: if (m_models.find(model) != m_models.end()) { Chris@595: cerr << "WARNING: Document::addImportedModel: Model already added" Chris@595: << endl; Chris@595: return; Chris@45: } Chris@45: Chris@45: ModelRecord rec; Chris@45: rec.source = 0; Chris@408: rec.channel = 0; Chris@45: rec.refcount = 0; Chris@329: rec.additional = false; Chris@45: Chris@45: m_models[model] = rec; Chris@45: Chris@137: #ifdef DEBUG_DOCUMENT Chris@233: SVDEBUG << "Document::addImportedModel: Added model " << model << endl; Chris@293: cerr << "Models now: "; Chris@137: for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@293: cerr << i->first << " "; Chris@137: } Chris@293: cerr << endl; Chris@137: #endif Chris@137: Chris@387: if (m_autoAlignment) { Chris@387: SVDEBUG << "Document::addImportedModel: auto-alignment is on, aligning model if possible" << endl; Chris@387: alignModel(model); Chris@387: } else { Chris@387: SVDEBUG << "Document(" << this << "): addImportedModel: auto-alignment is off" << endl; Chris@387: } Chris@47: Chris@45: emit modelAdded(model); Chris@45: } Chris@45: Chris@329: void Chris@329: Document::addAdditionalModel(Model *model) Chris@329: { Chris@329: if (m_models.find(model) != m_models.end()) { Chris@595: cerr << "WARNING: Document::addAdditionalModel: Model already added" Chris@595: << endl; Chris@595: return; Chris@329: } Chris@329: Chris@329: ModelRecord rec; Chris@329: rec.source = 0; Chris@408: rec.channel = 0; Chris@329: rec.refcount = 0; Chris@329: rec.additional = true; Chris@329: Chris@329: m_models[model] = rec; Chris@329: Chris@329: #ifdef DEBUG_DOCUMENT Chris@329: SVDEBUG << "Document::addAdditionalModel: Added model " << model << endl; Chris@329: cerr << "Models now: "; Chris@329: for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@329: cerr << i->first << " "; Chris@329: } Chris@329: cerr << endl; Chris@329: #endif Chris@329: Chris@387: if (m_autoAlignment) { Chris@387: SVDEBUG << "Document::addAdditionalModel: auto-alignment is on, aligning model if possible" << endl; Chris@387: alignModel(model); Chris@387: } Chris@329: Chris@329: emit modelAdded(model); Chris@329: } Chris@329: Chris@588: void Chris@588: Document::addAggregateModel(AggregateWaveModel *model) Chris@588: { Chris@588: connect(model, SIGNAL(modelInvalidated()), Chris@588: this, SLOT(aggregateModelInvalidated())); Chris@588: m_aggregateModels.insert(model); Chris@588: } Chris@588: Chris@588: void Chris@588: Document::aggregateModelInvalidated() Chris@588: { Chris@588: QObject *s = sender(); Chris@588: AggregateWaveModel *aggregate = qobject_cast(s); Chris@588: if (aggregate) releaseModel(aggregate); Chris@588: } Chris@588: Chris@45: Model * Chris@72: Document::addDerivedModel(const Transform &transform, Chris@78: const ModelTransformer::Input &input, Chris@296: QString &message) Chris@45: { Chris@45: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@297: if (i->second.transform == transform && Chris@297: i->second.source == input.getModel() && Chris@72: i->second.channel == input.getChannel()) { Chris@297: std::cerr << "derived model taken from map " << std::endl; Chris@297: return i->first; Chris@297: } Chris@45: } Chris@45: Chris@297: Transforms tt; Chris@297: tt.push_back(transform); Chris@329: vector mm = addDerivedModels(tt, input, message, 0); Chris@297: if (mm.empty()) return 0; Chris@297: else return mm[0]; Chris@297: } Chris@45: Chris@297: vector Chris@297: Document::addDerivedModels(const Transforms &transforms, Chris@297: const ModelTransformer::Input &input, Chris@329: QString &message, Chris@329: AdditionalModelConverter *amc) Chris@297: { Chris@297: vector mm = Chris@297: ModelTransformerFactory::getInstance()->transformMultiple Chris@329: (transforms, input, message, amc); Chris@83: Chris@297: for (int j = 0; j < (int)mm.size(); ++j) { Chris@83: Chris@297: Model *model = mm[j]; Chris@297: Chris@297: // The transform we actually used was presumably identical to Chris@297: // the one asked for, except that the version of the plugin Chris@297: // may differ. It's possible that the returned message Chris@297: // contains a warning about this; that doesn't concern us Chris@297: // here, but we do need to ensure that the transform we Chris@297: // remember is correct for what was actually applied, with the Chris@297: // current plugin version. Chris@297: Chris@536: //!!! would be nice to short-circuit this -- the version is Chris@536: //!!! static data, shouldn't have to construct a plugin for it Chris@536: //!!! (which may be expensive in Piper-world) Chris@536: Chris@297: Transform applied = transforms[j]; Chris@297: applied.setPluginVersion Chris@297: (TransformFactory::getInstance()-> Chris@297: getDefaultTransformFor(applied.getIdentifier(), Chris@436: applied.getSampleRate()) Chris@297: .getPluginVersion()); Chris@297: Chris@297: if (!model) { Chris@297: cerr << "WARNING: Document::addDerivedModel: no output model for transform " << applied.getIdentifier() << endl; Chris@297: } else { Chris@329: addAlreadyDerivedModel(applied, input, model); Chris@297: } Chris@45: } Chris@595: Chris@297: return mm; Chris@45: } Chris@45: Chris@45: void Chris@45: Document::releaseModel(Model *model) // Will _not_ release main model! Chris@45: { Chris@45: if (model == 0) { Chris@595: return; Chris@45: } Chris@45: Chris@45: if (model == m_mainModel) { Chris@595: return; Chris@45: } Chris@45: Chris@45: bool toDelete = false; Chris@45: Chris@45: if (m_models.find(model) != m_models.end()) { Chris@595: if (m_models[model].refcount == 0) { Chris@595: SVCERR << "WARNING: Document::releaseModel: model " << model Chris@588: << " reference count is zero already!" << endl; Chris@595: } else { Chris@595: if (--m_models[model].refcount == 0) { Chris@595: toDelete = true; Chris@595: } Chris@595: } Chris@588: } else if (m_aggregateModels.find(model) != m_aggregateModels.end()) { Chris@588: SVDEBUG << "Document::releaseModel: is an aggregate model" << endl; Chris@588: toDelete = true; Chris@45: } else { Chris@595: SVCERR << "WARNING: Document::releaseModel: Unfound model " Chris@588: << model << endl; Chris@595: toDelete = true; Chris@45: } Chris@45: Chris@45: if (toDelete) { Chris@45: Chris@595: int sourceCount = 0; Chris@45: Chris@595: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@595: if (i->second.source == model) { Chris@595: ++sourceCount; Chris@595: i->second.source = 0; Chris@595: } Chris@595: } Chris@45: Chris@595: if (sourceCount > 0) { Chris@595: SVDEBUG << "Document::releaseModel: Deleting model " Chris@588: << model << " even though it is source for " Chris@588: << sourceCount << " other derived model(s) -- resetting " Chris@588: << "their source fields appropriately" << endl; Chris@595: } Chris@45: Chris@79: model->aboutToDelete(); Chris@595: emit modelAboutToBeDeleted(model); Chris@595: m_models.erase(model); Chris@137: Chris@137: #ifdef DEBUG_DOCUMENT Chris@233: SVDEBUG << "Document::releaseModel: Deleted model " << model << endl; Chris@293: cerr << "Models now: "; Chris@137: for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@293: cerr << i->first << " "; Chris@137: } Chris@293: cerr << endl; Chris@137: #endif Chris@137: Chris@595: delete model; Chris@45: } Chris@45: } Chris@45: Chris@45: void Chris@45: Document::deleteLayer(Layer *layer, bool force) Chris@45: { Chris@45: if (m_layerViewMap.find(layer) != m_layerViewMap.end() && Chris@595: m_layerViewMap[layer].size() > 0) { Chris@45: Chris@595: cerr << "WARNING: Document::deleteLayer: Layer " Chris@595: << layer << " [" << layer->objectName() << "]" Chris@595: << " is still used in " << m_layerViewMap[layer].size() Chris@595: << " views!" << endl; Chris@45: Chris@595: if (force) { Chris@45: Chris@77: #ifdef DEBUG_DOCUMENT Chris@595: cerr << "(force flag set -- deleting from all views)" << endl; Chris@77: #endif Chris@45: Chris@595: for (std::set::iterator j = m_layerViewMap[layer].begin(); Chris@595: j != m_layerViewMap[layer].end(); ++j) { Chris@595: // don't use removeLayerFromView, as it issues a command Chris@595: layer->setLayerDormant(*j, true); Chris@595: (*j)->removeLayer(layer); Chris@595: } Chris@595: Chris@595: m_layerViewMap.erase(layer); Chris@45: Chris@595: } else { Chris@595: return; Chris@595: } Chris@45: } Chris@45: Chris@45: if (m_layers.find(layer) == m_layers.end()) { Chris@595: SVDEBUG << "Document::deleteLayer: Layer " Chris@212: << layer << " (" << typeid(layer).name() << Chris@212: ") does not exist, or has already been deleted " Chris@595: << "(this may not be as serious as it sounds)" << endl; Chris@595: return; Chris@45: } Chris@45: Chris@45: m_layers.erase(layer); Chris@45: Chris@132: #ifdef DEBUG_DOCUMENT Chris@233: SVDEBUG << "Document::deleteLayer: Removing, now have " Chris@229: << m_layers.size() << " layers" << endl; Chris@132: #endif Chris@52: Chris@45: releaseModel(layer->getModel()); Chris@45: emit layerRemoved(layer); Chris@45: emit layerAboutToBeDeleted(layer); Chris@45: delete layer; Chris@45: } Chris@45: Chris@45: void Chris@45: Document::setModel(Layer *layer, Model *model) Chris@45: { Chris@45: if (model && Chris@595: model != m_mainModel && Chris@595: m_models.find(model) == m_models.end()) { Chris@595: cerr << "ERROR: Document::setModel: Layer " << layer Chris@595: << " (\"" << layer->objectName() Chris@45: << "\") wants to use unregistered model " << model Chris@595: << ": register the layer's model before setting it!" Chris@595: << endl; Chris@595: return; Chris@45: } Chris@45: Chris@45: Model *previousModel = layer->getModel(); Chris@45: Chris@45: if (previousModel == model) { Chris@233: SVDEBUG << "NOTE: Document::setModel: Layer " << layer << " (\"" Chris@229: << layer->objectName() << "\") is already set to model " Chris@45: << model << " (\"" Chris@229: << (model ? model->objectName(): "(null)") Chris@229: << "\")" << endl; Chris@45: return; Chris@45: } Chris@45: Chris@45: if (model && model != m_mainModel) { Chris@595: m_models[model].refcount ++; Chris@45: } Chris@45: Chris@45: if (model && previousModel) { Chris@45: PlayParameterRepository::getInstance()->copyParameters Chris@45: (previousModel, model); Chris@45: } Chris@45: Chris@45: LayerFactory::getInstance()->setModel(layer, model); Chris@595: // std::cerr << "layer type: " << LayerFactory::getInstance()->getLayerTypeName(LayerFactory::getInstance()->getLayerType(layer)) << std::endl; Chris@45: Chris@45: if (previousModel) { Chris@45: releaseModel(previousModel); Chris@45: } Chris@45: } Chris@45: Chris@45: void Chris@45: Document::setChannel(Layer *layer, int channel) Chris@45: { Chris@45: LayerFactory::getInstance()->setChannel(layer, channel); Chris@45: } Chris@45: Chris@45: void Chris@45: Document::addLayerToView(View *view, Layer *layer) Chris@45: { Chris@45: Model *model = layer->getModel(); Chris@45: if (!model) { Chris@77: #ifdef DEBUG_DOCUMENT Chris@595: SVDEBUG << "Document::addLayerToView: Layer (\"" Chris@229: << layer->objectName() << "\") with no model being added to view: " Chris@229: << "normally you want to set the model first" << endl; Chris@77: #endif Chris@45: } else { Chris@595: if (model != m_mainModel && Chris@595: m_models.find(model) == m_models.end()) { Chris@595: cerr << "ERROR: Document::addLayerToView: Layer " << layer Chris@595: << " has unregistered model " << model Chris@595: << " -- register the layer's model before adding the layer!" << endl; Chris@595: return; Chris@595: } Chris@45: } Chris@45: Chris@45: CommandHistory::getInstance()->addCommand Chris@595: (new Document::AddLayerCommand(this, view, layer)); Chris@45: } Chris@45: Chris@45: void Chris@45: Document::removeLayerFromView(View *view, Layer *layer) Chris@45: { Chris@45: CommandHistory::getInstance()->addCommand Chris@595: (new Document::RemoveLayerCommand(this, view, layer)); Chris@45: } Chris@45: Chris@45: void Chris@45: Document::addToLayerViewMap(Layer *layer, View *view) Chris@45: { Chris@45: bool firstView = (m_layerViewMap.find(layer) == m_layerViewMap.end() || Chris@45: m_layerViewMap[layer].empty()); Chris@45: Chris@45: if (m_layerViewMap[layer].find(view) != Chris@595: m_layerViewMap[layer].end()) { Chris@595: cerr << "WARNING: Document::addToLayerViewMap:" Chris@595: << " Layer " << layer << " -> view " << view << " already in" Chris@595: << " layer view map -- internal inconsistency" << endl; Chris@45: } Chris@45: Chris@45: m_layerViewMap[layer].insert(view); Chris@45: Chris@45: if (firstView) emit layerInAView(layer, true); Chris@45: } Chris@45: Chris@45: void Chris@45: Document::removeFromLayerViewMap(Layer *layer, View *view) Chris@45: { Chris@45: if (m_layerViewMap[layer].find(view) == Chris@595: m_layerViewMap[layer].end()) { Chris@595: cerr << "WARNING: Document::removeFromLayerViewMap:" Chris@595: << " Layer " << layer << " -> view " << view << " not in" Chris@595: << " layer view map -- internal inconsistency" << endl; Chris@45: } Chris@45: Chris@45: m_layerViewMap[layer].erase(view); Chris@45: Chris@45: if (m_layerViewMap[layer].empty()) { Chris@45: m_layerViewMap.erase(layer); Chris@45: emit layerInAView(layer, false); Chris@45: } Chris@45: } Chris@45: Chris@45: QString Chris@45: Document::getUniqueLayerName(QString candidate) Chris@45: { Chris@45: for (int count = 1; ; ++count) { Chris@45: Chris@45: QString adjusted = Chris@45: (count > 1 ? QString("%1 <%2>").arg(candidate).arg(count) : Chris@45: candidate); Chris@45: Chris@45: bool duplicate = false; Chris@45: Chris@45: for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@45: if ((*i)->objectName() == adjusted) { Chris@45: duplicate = true; Chris@45: break; Chris@45: } Chris@45: } Chris@45: Chris@45: if (!duplicate) return adjusted; Chris@45: } Chris@45: } Chris@45: Chris@45: std::vector Chris@72: Document::getTransformInputModels() Chris@45: { Chris@45: std::vector models; Chris@45: Chris@45: if (!m_mainModel) return models; Chris@45: Chris@45: models.push_back(m_mainModel); Chris@45: Chris@45: //!!! This will pick up all models, including those that aren't visible... Chris@45: Chris@45: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@45: Chris@45: Model *model = i->first; Chris@45: if (!model || model == m_mainModel) continue; Chris@45: DenseTimeValueModel *dtvm = dynamic_cast(model); Chris@45: Chris@45: if (dtvm) { Chris@45: models.push_back(dtvm); Chris@45: } Chris@45: } Chris@45: Chris@45: return models; Chris@45: } Chris@45: Chris@50: bool Chris@77: Document::isKnownModel(const Model *model) const Chris@77: { Chris@77: if (model == m_mainModel) return true; Chris@77: return (m_models.find(const_cast(model)) != m_models.end()); Chris@77: } Chris@77: Chris@428: bool Chris@428: Document::canAlign() Chris@90: { Chris@428: return Align::canAlign(); Chris@50: } Chris@50: Chris@45: void Chris@45: Document::alignModel(Model *model) Chris@45: { Chris@387: SVDEBUG << "Document::alignModel(" << model << ")" << endl; Chris@387: Chris@387: if (!m_mainModel) { Chris@387: SVDEBUG << "(no main model to align to)" << endl; Chris@387: return; Chris@387: } Chris@45: Chris@45: RangeSummarisableTimeValueModel *rm = Chris@45: dynamic_cast(model); Chris@387: if (!rm) { Chris@387: SVDEBUG << "(main model is not alignable-to)" << endl; Chris@387: return; Chris@387: } Chris@48: Chris@86: if (rm->getAlignmentReference() == m_mainModel) { Chris@387: SVDEBUG << "(model " << rm << " is already aligned to main model " << m_mainModel << ")" << endl; Chris@86: return; Chris@86: } Chris@45: Chris@86: if (model == m_mainModel) { Chris@86: // The reference has an empty alignment to itself. This makes Chris@86: // it possible to distinguish between the reference and any Chris@86: // unaligned model just by looking at the model itself, Chris@86: // without also knowing what the main model is Chris@233: SVDEBUG << "Document::alignModel(" << model << "): is main model, setting appropriately" << endl; Chris@86: rm->setAlignment(new AlignmentModel(model, model, 0, 0)); Chris@86: return; Chris@86: } Chris@86: Chris@423: if (!m_align->alignModel(m_mainModel, rm)) { Chris@423: cerr << "Alignment failed: " << m_align->getError() << endl; Chris@423: emit alignmentFailed(m_align->getError()); Chris@64: } Chris@45: } Chris@45: Chris@45: void Chris@45: Document::alignModels() Chris@45: { Chris@45: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@45: alignModel(i->first); Chris@45: } Chris@86: alignModel(m_mainModel); Chris@45: } Chris@45: Chris@45: Document::AddLayerCommand::AddLayerCommand(Document *d, Chris@595: View *view, Chris@595: Layer *layer) : Chris@45: m_d(d), Chris@45: m_view(view), Chris@45: m_layer(layer), Chris@45: m_name(qApp->translate("AddLayerCommand", "Add %1 Layer").arg(layer->objectName())), Chris@45: m_added(false) Chris@45: { Chris@45: } Chris@45: Chris@45: Document::AddLayerCommand::~AddLayerCommand() Chris@45: { Chris@77: #ifdef DEBUG_DOCUMENT Chris@233: SVDEBUG << "Document::AddLayerCommand::~AddLayerCommand" << endl; Chris@77: #endif Chris@45: if (!m_added) { Chris@595: m_d->deleteLayer(m_layer); Chris@45: } Chris@45: } Chris@45: Chris@159: QString Chris@159: Document::AddLayerCommand::getName() const Chris@159: { Chris@165: #ifdef DEBUG_DOCUMENT Chris@233: SVDEBUG << "Document::AddLayerCommand::getName(): Name is " Chris@229: << m_name << endl; Chris@165: #endif Chris@159: return m_name; Chris@159: } Chris@159: Chris@45: void Chris@45: Document::AddLayerCommand::execute() Chris@45: { Chris@45: for (int i = 0; i < m_view->getLayerCount(); ++i) { Chris@595: if (m_view->getLayer(i) == m_layer) { Chris@595: // already there Chris@595: m_layer->setLayerDormant(m_view, false); Chris@595: m_added = true; Chris@595: return; Chris@595: } Chris@45: } Chris@45: Chris@45: m_view->addLayer(m_layer); Chris@45: m_layer->setLayerDormant(m_view, false); Chris@45: Chris@45: m_d->addToLayerViewMap(m_layer, m_view); Chris@45: m_added = true; Chris@45: } Chris@45: Chris@45: void Chris@45: Document::AddLayerCommand::unexecute() Chris@45: { Chris@45: m_view->removeLayer(m_layer); Chris@45: m_layer->setLayerDormant(m_view, true); Chris@45: Chris@45: m_d->removeFromLayerViewMap(m_layer, m_view); Chris@45: m_added = false; Chris@45: } Chris@45: Chris@45: Document::RemoveLayerCommand::RemoveLayerCommand(Document *d, Chris@595: View *view, Chris@595: Layer *layer) : Chris@45: m_d(d), Chris@45: m_view(view), Chris@45: m_layer(layer), Chris@339: m_wasDormant(layer->isLayerDormant(view)), Chris@45: m_name(qApp->translate("RemoveLayerCommand", "Delete %1 Layer").arg(layer->objectName())), Chris@45: m_added(true) Chris@45: { Chris@45: } Chris@45: Chris@45: Document::RemoveLayerCommand::~RemoveLayerCommand() Chris@45: { Chris@77: #ifdef DEBUG_DOCUMENT Chris@233: SVDEBUG << "Document::RemoveLayerCommand::~RemoveLayerCommand" << endl; Chris@77: #endif Chris@45: if (!m_added) { Chris@595: m_d->deleteLayer(m_layer); Chris@45: } Chris@45: } Chris@45: Chris@159: QString Chris@159: Document::RemoveLayerCommand::getName() const Chris@159: { Chris@171: #ifdef DEBUG_DOCUMENT Chris@233: SVDEBUG << "Document::RemoveLayerCommand::getName(): Name is " Chris@229: << m_name << endl; Chris@171: #endif Chris@159: return m_name; Chris@159: } Chris@159: Chris@45: void Chris@45: Document::RemoveLayerCommand::execute() Chris@45: { Chris@45: bool have = false; Chris@45: for (int i = 0; i < m_view->getLayerCount(); ++i) { Chris@595: if (m_view->getLayer(i) == m_layer) { Chris@595: have = true; Chris@595: break; Chris@595: } Chris@45: } Chris@45: Chris@45: if (!have) { // not there! Chris@595: m_layer->setLayerDormant(m_view, true); Chris@595: m_added = false; Chris@595: return; Chris@45: } Chris@45: Chris@45: m_view->removeLayer(m_layer); Chris@45: m_layer->setLayerDormant(m_view, true); Chris@45: Chris@45: m_d->removeFromLayerViewMap(m_layer, m_view); Chris@45: m_added = false; Chris@45: } Chris@45: Chris@45: void Chris@45: Document::RemoveLayerCommand::unexecute() Chris@45: { Chris@45: m_view->addLayer(m_layer); Chris@339: m_layer->setLayerDormant(m_view, m_wasDormant); Chris@45: Chris@45: m_d->addToLayerViewMap(m_layer, m_view); Chris@45: m_added = true; Chris@45: } Chris@45: Chris@45: void Chris@45: Document::toXml(QTextStream &out, QString indent, QString extraAttributes) const Chris@45: { Chris@226: toXml(out, indent, extraAttributes, false); Chris@226: } Chris@226: Chris@226: void Chris@226: Document::toXmlAsTemplate(QTextStream &out, QString indent, QString extraAttributes) const Chris@226: { Chris@226: toXml(out, indent, extraAttributes, true); Chris@226: } Chris@226: Chris@226: void Chris@226: Document::toXml(QTextStream &out, QString indent, QString extraAttributes, Chris@226: bool asTemplate) const Chris@226: { Chris@45: out << indent + QString("\n") Chris@45: .arg(extraAttributes == "" ? "" : " ").arg(extraAttributes); Chris@45: Chris@45: if (m_mainModel) { Chris@108: Chris@226: if (asTemplate) { Chris@226: writePlaceholderMainModel(out, indent + " "); Chris@226: } else { Chris@226: m_mainModel->toXml(out, indent + " ", "mainModel=\"true\""); Chris@226: } Chris@108: Chris@108: PlayParameters *playParameters = Chris@108: PlayParameterRepository::getInstance()->getPlayParameters(m_mainModel); Chris@108: if (playParameters) { Chris@108: playParameters->toXml Chris@108: (out, indent + " ", Chris@108: QString("model=\"%1\"") Chris@108: .arg(XmlExportable::getObjectExportId(m_mainModel))); Chris@108: } Chris@45: } Chris@45: Chris@45: // Models that are not used in a layer that is in a view should Chris@45: // not be written. Get our list of required models first. Chris@45: Chris@45: std::set used; Chris@45: Chris@45: for (LayerViewMap::const_iterator i = m_layerViewMap.begin(); Chris@45: i != m_layerViewMap.end(); ++i) { Chris@45: Chris@589: if (i->first && !i->second.empty()) { // Layer exists, is in views Chris@589: Model *m = i->first->getModel(); Chris@589: if (m) { Chris@589: used.insert(m); Chris@589: if (m->getSourceModel()) { Chris@589: used.insert(m->getSourceModel()); Chris@589: } Chris@589: } Chris@45: } Chris@45: } Chris@45: Chris@589: // Write aggregate models first, so that when re-reading Chris@589: // derivations we already know about their existence. But only Chris@589: // those that are actually used Chris@589: Chris@589: for (std::set::iterator i = m_aggregateModels.begin(); Chris@589: i != m_aggregateModels.end(); ++i) { Chris@589: Chris@589: SVDEBUG << "checking aggregate model " << *i << endl; Chris@589: Chris@589: AggregateWaveModel *aggregate = qobject_cast(*i); Chris@589: if (!aggregate) continue; Chris@589: if (used.find(aggregate) == used.end()) { Chris@589: SVDEBUG << "(unused, skipping)" << endl; Chris@589: continue; Chris@589: } Chris@589: Chris@589: SVDEBUG << "(used, writing)" << endl; Chris@589: Chris@589: aggregate->toXml(out, indent + " "); Chris@589: } Chris@589: Chris@111: std::set written; Chris@111: Chris@45: for (ModelMap::const_iterator i = m_models.begin(); Chris@595: i != m_models.end(); ++i) { Chris@45: Chris@111: Model *model = i->first; Chris@595: const ModelRecord &rec = i->second; Chris@45: Chris@45: if (used.find(model) == used.end()) continue; Chris@45: Chris@45: // We need an intelligent way to determine which models need Chris@45: // to be streamed (i.e. have been edited, or are small) and Chris@45: // which should not be (i.e. remain as generated by a Chris@45: // transform, and are large). Chris@45: // Chris@45: // At the moment we can get away with deciding not to stream Chris@45: // dense 3d models or writable wave file models, provided they Chris@45: // were generated from a transform, because at the moment there Chris@45: // is no way to edit those model types so it should be safe to Chris@45: // regenerate them. That won't always work in future though. Chris@45: // It would be particularly nice to be able to ask the user, Chris@45: // as well as making an intelligent guess. Chris@45: Chris@45: bool writeModel = true; Chris@45: bool haveDerivation = false; Chris@45: Chris@72: if (rec.source && rec.transform.getIdentifier() != "") { Chris@45: haveDerivation = true; Chris@45: } Chris@45: Chris@45: if (haveDerivation) { Chris@45: if (dynamic_cast(model)) { Chris@45: writeModel = false; Chris@45: } else if (dynamic_cast(model)) { Chris@45: writeModel = false; Chris@45: } Chris@45: } Chris@45: Chris@45: if (writeModel) { Chris@111: model->toXml(out, indent + " "); Chris@111: written.insert(model); Chris@45: } Chris@45: Chris@595: if (haveDerivation) { Chris@72: writeBackwardCompatibleDerivation(out, indent + " ", Chris@111: model, rec); Chris@595: } Chris@45: Chris@45: //!!! We should probably own the PlayParameterRepository Chris@45: PlayParameters *playParameters = Chris@111: PlayParameterRepository::getInstance()->getPlayParameters(model); Chris@45: if (playParameters) { Chris@45: playParameters->toXml Chris@45: (out, indent + " ", Chris@45: QString("model=\"%1\"") Chris@111: .arg(XmlExportable::getObjectExportId(model))); Chris@45: } Chris@45: } Chris@589: Chris@111: //!!! Chris@111: Chris@111: // We should write out the alignment models here. AlignmentModel Chris@111: // needs a toXml that writes out the export IDs of its reference Chris@111: // and aligned models, and then streams its path model. Note that Chris@111: // this will only work when the alignment is complete, so we Chris@111: // should probably wait for it if it isn't already by this point. Chris@111: Chris@111: for (std::set::const_iterator i = written.begin(); Chris@595: i != written.end(); ++i) { Chris@111: Chris@111: const Model *model = *i; Chris@111: const AlignmentModel *alignment = model->getAlignment(); Chris@111: if (!alignment) continue; Chris@111: Chris@111: alignment->toXml(out, indent + " "); Chris@111: } Chris@111: Chris@45: for (LayerSet::const_iterator i = m_layers.begin(); Chris@595: i != m_layers.end(); ++i) { Chris@45: Chris@595: (*i)->toXml(out, indent + " "); Chris@45: } Chris@45: Chris@45: out << indent + "\n"; Chris@45: } Chris@45: Chris@72: void Chris@226: Document::writePlaceholderMainModel(QTextStream &out, QString indent) const Chris@226: { Chris@226: out << indent; Chris@226: out << QString("\n") Chris@226: .arg(getObjectExportId(m_mainModel)) Chris@226: .arg(m_mainModel->getSampleRate()); Chris@226: } Chris@226: Chris@226: void Chris@72: Document::writeBackwardCompatibleDerivation(QTextStream &out, QString indent, Chris@72: Model *targetModel, Chris@72: const ModelRecord &rec) const Chris@72: { Chris@72: // There is a lot of redundancy in the XML we output here, because Chris@72: // we want it to work with older SV session file reading code as Chris@72: // well. Chris@72: // Chris@72: // Formerly, a transform was described using a derivation element Chris@72: // which set out the source and target models, execution context Chris@72: // (step size, input channel etc) and transform id, containing a Chris@72: // plugin element which set out the transform parameters and so Chris@72: // on. (The plugin element came from a "configurationXml" string Chris@72: // obtained from PluginXml.) Chris@72: // Chris@72: // This has been replaced by a derivation element setting out the Chris@72: // source and target models and input channel, containing a Chris@72: // transform element which sets out everything in the Transform. Chris@72: // Chris@72: // In order to retain compatibility with older SV code, however, Chris@72: // we have to write out the same stuff into the derivation as Chris@72: // before, and manufacture an appropriate plugin element as well Chris@72: // as the transform element. In order that newer code knows it's Chris@72: // dealing with a newer format, we will also write an attribute Chris@72: // 'type="transform"' in the derivation element. Chris@45: Chris@72: const Transform &transform = rec.transform; Chris@72: Chris@72: // Just for reference, this is what we would write if we didn't Chris@72: // have to be backward compatible: Chris@72: // Chris@72: // out << indent Chris@72: // << QString("\n") Chris@72: // .arg(XmlExportable::getObjectExportId(rec.source)) Chris@72: // .arg(XmlExportable::getObjectExportId(targetModel)) Chris@72: // .arg(rec.channel); Chris@72: // Chris@72: // transform.toXml(out, indent + " "); Chris@72: // Chris@72: // out << indent << "\n"; Chris@72: // Chris@72: // Unfortunately, we can't just do that. So we do this... Chris@72: Chris@72: QString extentsAttributes; Chris@72: if (transform.getStartTime() != RealTime::zeroTime || Chris@72: transform.getDuration() != RealTime::zeroTime) { Chris@72: extentsAttributes = QString("startFrame=\"%1\" duration=\"%2\" ") Chris@72: .arg(RealTime::realTime2Frame(transform.getStartTime(), Chris@72: targetModel->getSampleRate())) Chris@72: .arg(RealTime::realTime2Frame(transform.getDuration(), Chris@72: targetModel->getSampleRate())); Chris@72: } Chris@595: Chris@72: out << indent; Chris@72: out << QString("\n") Chris@72: .arg(XmlExportable::getObjectExportId(rec.source)) Chris@72: .arg(XmlExportable::getObjectExportId(targetModel)) Chris@72: .arg(rec.channel) Chris@72: .arg(TransformFactory::getInstance()->getTransformInputDomain Chris@72: (transform.getIdentifier())) Chris@72: .arg(transform.getStepSize()) Chris@72: .arg(transform.getBlockSize()) Chris@72: .arg(extentsAttributes) Chris@72: .arg(int(transform.getWindowType())) Chris@72: .arg(XmlExportable::encodeEntities(transform.getIdentifier())); Chris@72: Chris@72: transform.toXml(out, indent + " "); Chris@72: Chris@72: out << indent << " " Chris@72: << TransformFactory::getInstance()->getPluginConfigurationXml(transform); Chris@72: Chris@72: out << indent << "\n"; Chris@72: } Chris@72: