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@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" 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@744: #include "align/Align.h" Chris@45: Chris@297: using std::vector; Chris@297: Chris@679: #define DEBUG_DOCUMENT 1 Chris@77: Chris@45: //!!! still need to handle command history, documentRestored/documentModified Chris@45: Chris@45: Document::Document() : Chris@423: m_autoAlignment(false), Chris@601: m_align(new Align()), Chris@601: m_isIncomplete(false) Chris@45: { Chris@456: connect(ModelTransformerFactory::getInstance(), Chris@456: SIGNAL(transformFailed(QString, QString)), Chris@456: this, Chris@456: SIGNAL(modelGenerationFailed(QString, QString))); Chris@459: Chris@683: connect(m_align, SIGNAL(alignmentComplete(ModelId)), Chris@683: this, SIGNAL(alignmentComplete(ModelId))); 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@660: SVDEBUG << "\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@691: SVCERR << "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@691: #ifdef DEBUG_DOCUMENT Chris@691: SVCERR << "Document::~Document: about to release normal models" << endl; Chris@691: #endif Chris@683: for (auto mr: m_models) { Chris@683: ModelById::release(mr.first); Chris@683: } Chris@691: Chris@691: #ifdef DEBUG_DOCUMENT Chris@691: SVCERR << "Document::~Document: about to release aggregate models" << endl; Chris@691: #endif Chris@683: for (auto m: m_aggregateModels) { Chris@683: ModelById::release(m); Chris@45: } Chris@45: Chris@691: #ifdef DEBUG_DOCUMENT Chris@691: SVCERR << "Document::~Document: about to release alignment models" << endl; Chris@691: #endif Chris@691: for (auto m: m_alignmentModels) { Chris@691: ModelById::release(m); Chris@691: } Chris@691: Chris@691: #ifdef DEBUG_DOCUMENT Chris@691: SVCERR << "Document::~Document: about to release main model" << endl; Chris@691: #endif Chris@687: if (!m_mainModel.isNone()) { Chris@687: ModelById::release(m_mainModel); Chris@687: } Chris@691: Chris@683: m_mainModel = {}; Chris@687: emit mainModelChanged({}); 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@636: if (!newLayer) return nullptr; Chris@45: Chris@45: newLayer->setObjectName(getUniqueLayerName(newLayer->objectName())); Chris@45: Chris@663: m_layers.push_back(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@636: if (!newLayer) return nullptr; Chris@45: setModel(newLayer, m_mainModel); Chris@45: return newLayer; Chris@45: } Chris@45: Chris@45: Layer * Chris@683: Document::createImportedLayer(ModelId modelId) Chris@45: { Chris@45: LayerFactory::LayerTypeSet types = Chris@683: LayerFactory::getInstance()->getValidLayerTypes(modelId); Chris@45: Chris@45: if (types.empty()) { Chris@660: SVCERR << "WARNING: Document::importLayer: no valid display layer for model" << endl; Chris@636: return nullptr; 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@636: if (!newLayer) return nullptr; Chris@45: Chris@45: newLayer->setObjectName(getUniqueLayerName(newLayer->objectName())); Chris@45: Chris@691: addNonDerivedModel(modelId); Chris@683: setModel(newLayer, modelId); Chris@45: Chris@45: //!!! and all channels Chris@45: setChannel(newLayer, -1); Chris@45: Chris@663: m_layers.push_back(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@683: if (m_mainModel.isNone()) return nullptr; Chris@61: Chris@683: auto newModel = Chris@595: LayerFactory::getInstance()->createEmptyModel(type, m_mainModel); Chris@636: if (!newModel) return nullptr; Chris@683: Chris@45: Layer *newLayer = createLayer(type); Chris@45: if (!newLayer) { Chris@636: return nullptr; Chris@45: } Chris@45: Chris@687: auto newModelId = ModelById::add(newModel); Chris@691: addNonDerivedModel(newModelId); Chris@687: setModel(newLayer, newModelId); 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@636: if (!newLayer) return nullptr; 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@636: if (layers.empty()) return nullptr; 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@683: vector newModels = Chris@683: addDerivedModels(transforms, input, message, nullptr); 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@683: for (int i = 0; in_range_for(newModels, i); ++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@633: ~AdditionalModelConverter() override { } Chris@329: Chris@329: void Chris@329: setPrimaryLayers(vector layers) { Chris@329: m_primary = layers; Chris@329: } Chris@329: Chris@329: void Chris@683: moreModelsAvailable(vector models) override { Chris@660: SVDEBUG << "AdditionalModelConverter::moreModelsAvailable: " << models.size() << " model(s)" << endl; Chris@329: // We can't automatically regenerate the additional models on Chris@683: // reload - so they go in m_additionalModels instead of m_models Chris@329: QStringList names; Chris@683: foreach (ModelId modelId, models) { Chris@683: m_doc->addAdditionalModel(modelId); 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@633: noMoreModelsAvailable() override { Chris@660: SVDEBUG << "AdditionalModelConverter::noMoreModelsAvailable" << endl; Chris@363: m_handler->layersCreated(this, m_primary, vector()); Chris@329: delete this; Chris@329: } Chris@329: Chris@697: void cancel() { Chris@697: foreach (Layer *layer, m_primary) { Chris@697: m_doc->setModel(layer, {}); Chris@697: } Chris@697: } Chris@697: 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@683: vector newModels = addDerivedModels Chris@329: (transforms, input, message, amc); Chris@329: Chris@329: QStringList names; Chris@683: for (int i = 0; in_range_for(newModels, i); ++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@697: void Chris@697: Document::cancelAsyncLayerCreation(Document::LayerCreationAsyncHandle h) Chris@697: { Chris@697: AdditionalModelConverter *conv = static_cast(h); Chris@697: conv->cancel(); Chris@697: } Chris@697: Chris@329: vector Chris@683: Document::createLayersForDerivedModels(vector newModels, Chris@329: QStringList names) Chris@329: { Chris@297: vector layers; Chris@329: Chris@683: for (int i = 0; in_range_for(newModels, i); ++i) { Chris@297: Chris@683: ModelId newModelId = newModels[i]; Chris@297: Chris@297: LayerFactory::LayerTypeSet types = Chris@683: LayerFactory::getInstance()->getValidLayerTypes(newModelId); Chris@297: Chris@297: if (types.empty()) { Chris@660: SVCERR << "WARNING: Document::createLayerForTransformer: no valid display layer for output of transform " << names[i] << endl; Chris@695: releaseModel(newModelId); 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@683: setModel(newLayer, newModelId); 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@683: Document::setMainModel(ModelId modelId) Chris@45: { Chris@683: ModelId oldMainModel = m_mainModel; Chris@683: m_mainModel = modelId; Chris@160: Chris@45: emit modelAdded(m_mainModel); Chris@683: Chris@683: if (auto model = ModelById::get(modelId)) { 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@660: SVDEBUG << "Document::setMainModel: Have " Chris@293: << m_layers.size() << " layers" << endl; Chris@661: SVDEBUG << "Models now: "; Chris@661: for (const auto &r: m_models) { Chris@683: SVDEBUG << r.first << " "; Chris@664: } Chris@660: SVDEBUG << endl; Chris@660: SVDEBUG << "Old main model: " << oldMainModel << endl; Chris@77: #endif Chris@52: Chris@663: for (Layer *layer: m_layers) { Chris@45: Chris@683: ModelId modelId = layer->getModel(); Chris@45: Chris@77: #ifdef DEBUG_DOCUMENT Chris@660: SVDEBUG << "Document::setMainModel: inspecting model " Chris@683: << modelId << " in layer " << layer->objectName() << endl; Chris@77: #endif Chris@45: Chris@683: if (modelId == oldMainModel) { Chris@77: #ifdef DEBUG_DOCUMENT Chris@660: SVDEBUG << "... 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@683: if (modelId.isNone()) { Chris@660: SVCERR << "WARNING: Document::setMainModel: Null model in layer " Chris@660: << layer << endl; Chris@595: // get rid of this hideous degenerate Chris@595: obsoleteLayers.push_back(layer); Chris@595: continue; Chris@595: } Chris@137: Chris@683: if (m_models.find(modelId) == m_models.end()) { Chris@661: SVCERR << "WARNING: Document::setMainModel: Unknown model " Chris@683: << modelId << " in layer " << layer << endl; Chris@595: // and this one Chris@595: obsoleteLayers.push_back(layer); Chris@595: continue; Chris@595: } Chris@661: Chris@683: ModelRecord record = m_models[modelId]; Chris@661: Chris@683: if (!record.source.isNone() && (record.source == oldMainModel)) { Chris@45: Chris@77: #ifdef DEBUG_DOCUMENT Chris@660: SVDEBUG << "... 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@661: const Transform &transform = record.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@683: ModelId replacementModel = Chris@45: addDerivedModel(transform, Chris@72: ModelTransformer::Input Chris@661: (m_mainModel, record.channel), Chris@78: message); Chris@595: Chris@683: if (replacementModel.isNone()) { Chris@660: SVCERR << "WARNING: Document::setMainModel: Failed to regenerate model for transform \"" Chris@660: << 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@683: SVDEBUG << "Replacing model " << modelId << ") with model " Chris@683: << replacementModel << ") in layer " Chris@660: << layer << " (name " << layer->objectName() << ")" Chris@660: << endl; Chris@366: Chris@683: auto rm = ModelById::getAs(replacementModel); Chris@45: if (rm) { Chris@660: SVDEBUG << "new model has " << rm->getChannelCount() << " channels " << endl; Chris@45: } else { Chris@660: SVDEBUG << "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@683: std::set additionalModels; Chris@661: for (const auto &rec : m_models) { Chris@683: if (rec.second.additional) { Chris@683: additionalModels.insert(rec.first); Chris@329: } Chris@329: } Chris@683: for (ModelId a: additionalModels) { Chris@683: m_models.erase(a); Chris@661: } Chris@329: Chris@661: for (const auto &rec : m_models) { Chris@86: Chris@683: auto m = ModelById::get(rec.first); Chris@683: if (!m) continue; Chris@137: Chris@137: #ifdef DEBUG_DOCUMENT Chris@683: SVDEBUG << "considering alignment for model " << rec.first << endl; Chris@137: #endif Chris@137: Chris@86: if (m_autoAlignment) { Chris@86: Chris@683: alignModel(rec.first); Chris@86: Chris@683: } else if (!oldMainModel.isNone() && Chris@137: (m->getAlignmentReference() == oldMainModel)) { Chris@86: Chris@683: alignModel(rec.first); Chris@48: } Chris@48: } Chris@48: 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@667: } else { Chris@667: SVDEBUG << "Document::setMainModel: auto-alignment is off" << endl; Chris@86: } Chris@86: Chris@45: emit mainModelChanged(m_mainModel); Chris@45: Chris@687: if (!oldMainModel.isNone()) { Chris@687: Chris@687: // Remove the playable explicitly - the main model's dtor will Chris@687: // do this, but just in case something is still hanging onto a Chris@687: // shared_ptr to the old main model so it doesn't get deleted Chris@687: PlayParameterRepository::getInstance()->removePlayable Chris@687: (oldMainModel.untyped); Chris@687: Chris@687: ModelById::release(oldMainModel); Chris@687: } Chris@45: } Chris@45: Chris@45: void Chris@329: Document::addAlreadyDerivedModel(const Transform &transform, Chris@329: const ModelTransformer::Input &input, Chris@683: ModelId outputModelToAdd) Chris@45: { Chris@683: if (m_models.find(outputModelToAdd) != m_models.end()) { Chris@661: SVCERR << "WARNING: Document::addAlreadyDerivedModel: Model already added" Chris@683: << endl; Chris@595: return; Chris@45: } Chris@683: Chris@77: #ifdef DEBUG_DOCUMENT Chris@683: SVDEBUG << "Document::addAlreadyDerivedModel: source is " << input.getModel() << endl; 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: Chris@683: if (auto m = ModelById::get(outputModelToAdd)) { Chris@683: m->setSourceModel(input.getModel()); Chris@683: } Chris@45: Chris@683: m_models[outputModelToAdd] = rec; Chris@45: Chris@137: #ifdef DEBUG_DOCUMENT Chris@661: SVDEBUG << "Document::addAlreadyDerivedModel: Added model " << outputModelToAdd << endl; Chris@661: SVDEBUG << "Models now: "; Chris@661: for (const auto &rec : m_models) { Chris@683: SVDEBUG << rec.first << " "; Chris@137: } Chris@660: SVDEBUG << endl; Chris@137: #endif Chris@137: Chris@45: emit modelAdded(outputModelToAdd); Chris@45: } Chris@45: Chris@45: void Chris@691: Document::addNonDerivedModel(ModelId modelId) Chris@45: { Chris@691: if (ModelById::isa(modelId)) { Chris@691: #ifdef DEBUG_DOCUMENT Chris@691: SVCERR << "Document::addNonDerivedModel: Model " << modelId << " is an aggregate model, adding it to aggregates" << endl; Chris@691: #endif Chris@691: m_aggregateModels.insert(modelId); Chris@691: return; Chris@691: } Chris@691: if (ModelById::isa(modelId)) { Chris@691: #ifdef DEBUG_DOCUMENT Chris@691: SVCERR << "Document::addNonDerivedModel: Model " << modelId << " is an alignment model, adding it to alignments" << endl; Chris@691: #endif Chris@691: m_alignmentModels.insert(modelId); Chris@691: return; Chris@691: } Chris@691: Chris@683: if (m_models.find(modelId) != m_models.end()) { Chris@691: SVCERR << "WARNING: Document::addNonDerivedModel: Model already added" Chris@683: << endl; Chris@595: return; Chris@45: } Chris@45: Chris@45: ModelRecord rec; Chris@683: rec.source = {}; Chris@408: rec.channel = 0; Chris@329: rec.additional = false; Chris@45: Chris@683: m_models[modelId] = rec; Chris@45: Chris@137: #ifdef DEBUG_DOCUMENT Chris@691: SVCERR << "Document::addNonDerivedModel: Added model " << modelId << endl; Chris@691: SVCERR << "Models now: "; Chris@661: for (const auto &rec : m_models) { Chris@691: SVCERR << rec.first << " "; Chris@137: } Chris@691: SVCERR << endl; Chris@137: #endif Chris@137: Chris@387: if (m_autoAlignment) { Chris@691: SVDEBUG << "Document::addNonDerivedModel: auto-alignment is on, aligning model if possible" << endl; Chris@683: alignModel(modelId); Chris@387: } else { Chris@691: SVDEBUG << "Document(" << this << "): addNonDerivedModel: auto-alignment is off" << endl; Chris@387: } Chris@47: Chris@683: emit modelAdded(modelId); Chris@45: } Chris@45: Chris@329: void Chris@683: Document::addAdditionalModel(ModelId modelId) Chris@329: { Chris@683: if (m_models.find(modelId) != m_models.end()) { Chris@661: SVCERR << "WARNING: Document::addAdditionalModel: Model already added" Chris@683: << endl; Chris@595: return; Chris@329: } Chris@329: Chris@329: ModelRecord rec; Chris@683: rec.source = {}; Chris@408: rec.channel = 0; Chris@329: rec.additional = true; Chris@329: Chris@683: m_models[modelId] = rec; Chris@329: Chris@329: #ifdef DEBUG_DOCUMENT Chris@683: SVDEBUG << "Document::addAdditionalModel: Added model " << modelId << endl; Chris@661: SVDEBUG << "Models now: "; Chris@661: for (const auto &rec : m_models) { Chris@683: SVDEBUG << rec.first << " "; Chris@329: } Chris@660: SVDEBUG << endl; Chris@329: #endif Chris@329: Chris@691: if (m_autoAlignment && Chris@691: ModelById::isa(modelId)) { Chris@691: SVDEBUG << "Document::addAdditionalModel: auto-alignment is on and model is an alignable type, aligning it if possible" << endl; Chris@683: alignModel(modelId); Chris@387: } Chris@329: Chris@683: emit modelAdded(modelId); Chris@329: } Chris@329: Chris@683: ModelId Chris@72: Document::addDerivedModel(const Transform &transform, Chris@78: const ModelTransformer::Input &input, Chris@296: QString &message) Chris@45: { Chris@661: for (auto &rec : m_models) { Chris@683: if (rec.second.transform == transform && Chris@683: rec.second.source == input.getModel() && Chris@683: rec.second.channel == input.getChannel()) { Chris@661: SVDEBUG << "derived model taken from map " << endl; Chris@683: return rec.first; Chris@297: } Chris@45: } Chris@45: Chris@297: Transforms tt; Chris@297: tt.push_back(transform); Chris@683: vector mm = addDerivedModels(tt, input, message, nullptr); Chris@683: if (mm.empty()) return {}; Chris@297: else return mm[0]; Chris@297: } Chris@45: Chris@683: 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@683: vector mm = Chris@297: ModelTransformerFactory::getInstance()->transformMultiple Chris@329: (transforms, input, message, amc); Chris@83: Chris@683: for (int j = 0; in_range_for(mm, j); ++j) { Chris@83: Chris@683: ModelId modelId = mm[j]; Chris@683: Transform applied = transforms[j]; Chris@683: Chris@683: if (modelId.isNone()) { Chris@683: SVCERR << "WARNING: Document::addDerivedModel: no output model for transform " << applied.getIdentifier() << endl; Chris@683: continue; Chris@683: } 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@297: applied.setPluginVersion Chris@297: (TransformFactory::getInstance()-> Chris@297: getDefaultTransformFor(applied.getIdentifier(), Chris@436: applied.getSampleRate()) Chris@297: .getPluginVersion()); Chris@297: Chris@683: addAlreadyDerivedModel(applied, input, modelId); Chris@45: } Chris@595: Chris@297: return mm; Chris@45: } Chris@45: Chris@45: void Chris@690: Document::releaseModel(ModelId modelId) Chris@45: { Chris@690: // This is called when a layer has been deleted or has replaced Chris@690: // its model, in order to reclaim storage for the old model. It Chris@690: // could be a no-op without making any functional difference, as Chris@690: // all the models stored in the ById pool are released when the Chris@690: // document is deleted. But models can sometimes be large, so if Chris@690: // we know no other layer is using one, we should release it. If Chris@690: // we happen to release one that is being used, the ModelById Chris@690: // borrowed-pointer mechanism will at least prevent memory errors, Chris@690: // although the other code will have to stop whatever it's doing. Chris@683: Chris@719: // "warning: expression with side effects will be evaluated despite Chris@719: // being used as an operand to 'typeid'" Chris@719: #ifdef __clang__ Chris@719: #pragma clang diagnostic ignored "-Wpotentially-evaluated-expression" Chris@719: #endif Chris@719: Chris@691: if (auto model = ModelById::get(modelId)) { Chris@691: SVCERR << "Document::releaseModel(" << modelId << "), name " Chris@691: << model->objectName() << ", type " Chris@691: << typeid(*model.get()).name() << endl; Chris@691: } else { Chris@691: SVCERR << "Document::releaseModel(" << modelId << ")" << endl; Chris@691: } Chris@683: Chris@683: if (modelId.isNone()) { Chris@595: return; Chris@45: } Chris@683: Chris@665: #ifdef DEBUG_DOCUMENT Chris@690: SVCERR << "Document::releaseModel(" << modelId << ")" << endl; Chris@665: #endif Chris@690: Chris@683: if (modelId == m_mainModel) { Chris@690: #ifdef DEBUG_DOCUMENT Chris@690: SVCERR << "Document::releaseModel: It's the main model, ignoring" Chris@690: << endl; Chris@690: #endif Chris@595: return; Chris@45: } Chris@45: Chris@690: if (m_models.find(modelId) == m_models.end()) { Chris@691: // No point in releasing aggregate and alignment models, Chris@690: // they're not large Chris@664: #ifdef DEBUG_DOCUMENT Chris@690: SVCERR << "Document::releaseModel: It's not a regular layer model, ignoring" << endl; Chris@664: #endif Chris@690: return; Chris@45: } Chris@45: Chris@690: for (auto layer: m_layers) { Chris@690: if (layer->getModel() == modelId) { Chris@137: #ifdef DEBUG_DOCUMENT Chris@692: SVCERR << "Document::releaseModel: It's still in use in at least one layer (e.g. " << layer << ", \"" << layer->getLayerPresentationName() << "\"), ignoring" << endl; Chris@137: #endif Chris@690: return; Chris@676: } Chris@45: } Chris@683: Chris@690: #ifdef DEBUG_DOCUMENT Chris@690: SVCERR << "Document::releaseModel: Seems to be OK to release this one" Chris@690: << endl; Chris@683: #endif Chris@690: Chris@690: int sourceCount = 0; Chris@690: Chris@690: for (auto &m: m_models) { Chris@690: if (m.second.source == modelId) { Chris@690: ++sourceCount; Chris@690: m.second.source = {}; Chris@690: } Chris@690: } Chris@690: Chris@690: if (sourceCount > 0) { Chris@690: SVCERR << "Document::releaseModel: Request to release model " Chris@690: << modelId << " even though it was source for " Chris@690: << sourceCount << " other derived model(s) -- have cleared " Chris@690: << "their source fields" << endl; Chris@690: } Chris@690: Chris@690: m_models.erase(modelId); Chris@690: ModelById::release(modelId); 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: if (force) { Chris@45: Chris@672: SVDEBUG << "NOTE: Document::deleteLayer: Layer " Chris@672: << layer << " [" << layer->objectName() << "]" Chris@672: << " is still used in " << m_layerViewMap[layer].size() Chris@672: << " views. Force flag set, so removing from them" << endl; Chris@672: 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@672: Chris@672: SVCERR << "WARNING: Document::deleteLayer: Layer " Chris@672: << layer << " [" << layer->objectName() << "]" Chris@672: << " is still used in " << m_layerViewMap[layer].size() Chris@672: << " views! Force flag is not set, so not deleting" << endl; Chris@672: Chris@595: return; Chris@595: } Chris@45: } Chris@45: Chris@663: bool found = false; Chris@663: for (auto itr = m_layers.begin(); itr != m_layers.end(); ++itr) { Chris@663: if (*itr == layer) { Chris@663: found = true; Chris@663: m_layers.erase(itr); Chris@663: break; Chris@663: } Chris@663: } Chris@663: if (!found) { Chris@595: SVDEBUG << "Document::deleteLayer: Layer " Chris@665: << layer << " (typeid " << 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@132: #ifdef DEBUG_DOCUMENT Chris@665: SVDEBUG << "Document::deleteLayer: Removing (and about to release model), 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@683: Document::setModel(Layer *layer, ModelId modelId) Chris@45: { Chris@683: if (!modelId.isNone() && Chris@683: modelId != m_mainModel && Chris@683: m_models.find(modelId) == m_models.end()) { Chris@661: SVCERR << "ERROR: Document::setModel: Layer " << layer Chris@683: << " (\"" << layer->objectName() Chris@683: << "\") wants to use unregistered model " << modelId Chris@683: << ": register the layer's model before setting it!" Chris@683: << endl; Chris@595: return; Chris@45: } Chris@45: Chris@683: ModelId previousModel = layer->getModel(); Chris@45: Chris@683: if (previousModel == modelId) { Chris@233: SVDEBUG << "NOTE: Document::setModel: Layer " << layer << " (\"" Chris@683: << layer->objectName() Chris@683: << "\") is already set to model " Chris@683: << modelId << endl; Chris@45: return; Chris@45: } Chris@691: Chris@683: if (!modelId.isNone() && !previousModel.isNone()) { Chris@45: PlayParameterRepository::getInstance()->copyParameters Chris@683: (previousModel.untyped, modelId.untyped); Chris@45: } Chris@45: Chris@683: LayerFactory::getInstance()->setModel(layer, modelId); Chris@45: Chris@683: releaseModel(previousModel); 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@683: ModelId modelId = layer->getModel(); Chris@683: if (modelId.isNone()) { Chris@77: #ifdef DEBUG_DOCUMENT Chris@595: SVDEBUG << "Document::addLayerToView: Layer (\"" Chris@660: << layer->objectName() Chris@660: << "\") 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@683: if (modelId != m_mainModel && Chris@683: m_models.find(modelId) == m_models.end()) { Chris@661: SVCERR << "ERROR: Document::addLayerToView: Layer " << layer Chris@683: << " has unregistered model " << modelId 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@660: SVCERR << "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@660: SVCERR << "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@663: for (auto 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@683: std::vector Chris@72: Document::getTransformInputModels() Chris@45: { Chris@683: std::vector models; Chris@45: Chris@683: if (m_mainModel.isNone()) 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@683: for (auto rec: m_models) { Chris@45: Chris@683: ModelId modelId = rec.first; Chris@683: if (modelId == m_mainModel) continue; Chris@683: Chris@683: auto dtvm = ModelById::getAs(modelId); Chris@45: if (dtvm) { Chris@683: models.push_back(modelId); Chris@45: } Chris@45: } Chris@45: Chris@45: return models; Chris@45: } Chris@45: Chris@50: bool Chris@683: Document::isKnownModel(const ModelId modelId) const Chris@77: { Chris@683: if (modelId == m_mainModel) return true; Chris@683: for (auto rec: m_models) { Chris@683: if (rec.first == modelId) return true; Chris@661: } Chris@661: return false; 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@683: Document::alignModel(ModelId modelId, bool forceRecalculate) Chris@45: { Chris@683: SVDEBUG << "Document::alignModel(" << modelId << ", " << forceRecalculate Chris@672: << ") (main model is " << m_mainModel << ")" << endl; Chris@683: Chris@683: auto rm = ModelById::getAs(modelId); Chris@387: if (!rm) { Chris@683: SVDEBUG << "(model " << modelId << " is not an alignable sort)" << endl; Chris@387: return; Chris@387: } Chris@48: Chris@683: if (m_mainModel.isNone()) { Chris@672: SVDEBUG << "(no main model to align to)" << endl; Chris@683: if (forceRecalculate && !rm->getAlignment().isNone()) { Chris@672: SVDEBUG << "(but model is aligned, and forceRecalculate is true, " Chris@672: << "so resetting alignment to nil)" << endl; Chris@683: rm->setAlignment({}); Chris@672: } Chris@672: return; Chris@672: } Chris@672: Chris@86: if (rm->getAlignmentReference() == m_mainModel) { Chris@683: SVDEBUG << "(model " << modelId << " is already aligned to main model " Chris@672: << m_mainModel << ")" << endl; Chris@672: if (!forceRecalculate) { Chris@672: return; Chris@672: } else { Chris@672: SVDEBUG << "(but forceRecalculate is true, so realigning anyway)" Chris@672: << endl; Chris@672: } Chris@86: } Chris@45: Chris@683: if (modelId == 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@683: SVDEBUG << "Document::alignModel(" << modelId Chris@672: << "): is main model, setting alignment to itself" << endl; Chris@683: auto alignment = std::make_shared(modelId, modelId, Chris@683: ModelId()); Chris@691: Chris@691: ModelId alignmentModelId = ModelById::add(alignment); Chris@691: rm->setAlignment(alignmentModelId); Chris@691: m_alignmentModels.insert(alignmentModelId); Chris@86: return; Chris@86: } Chris@86: Chris@683: auto w = ModelById::getAs(modelId); Chris@679: if (w && w->getWriteProportion() < 100) { Chris@683: SVDEBUG << "Document::alignModel(" << modelId Chris@679: << "): model write is not complete, deferring" Chris@679: << endl; Chris@687: connect(w.get(), SIGNAL(writeCompleted(ModelId)), Chris@687: this, SLOT(performDeferredAlignment(ModelId))); Chris@679: return; Chris@679: } Chris@679: Chris@667: SVDEBUG << "Document::alignModel: aligning..." << endl; Chris@683: if (!rm->getAlignmentReference().isNone()) { Chris@667: SVDEBUG << "(Note: model " << rm << " is currently aligned to model " Chris@667: << rm->getAlignmentReference() << "; this will replace that)" Chris@667: << endl; Chris@667: } Chris@670: Chris@670: QString err; Chris@683: if (!m_align->alignModel(this, m_mainModel, modelId, err)) { Chris@670: SVCERR << "Alignment failed: " << err << endl; Chris@670: emit alignmentFailed(err); Chris@64: } Chris@45: } Chris@45: Chris@45: void Chris@687: Document::performDeferredAlignment(ModelId modelId) Chris@679: { Chris@683: SVDEBUG << "Document::performDeferredAlignment: aligning..." << endl; Chris@683: alignModel(modelId); Chris@679: } Chris@679: Chris@679: void Chris@45: Document::alignModels() Chris@45: { Chris@683: for (auto rec: m_models) { Chris@683: alignModel(rec.first); Chris@45: } Chris@86: alignModel(m_mainModel); Chris@45: } Chris@45: Chris@672: void Chris@672: Document::realignModels() Chris@672: { Chris@683: for (auto rec: m_models) { Chris@683: alignModel(rec.first, true); Chris@672: } Chris@672: alignModel(m_mainModel); Chris@672: } Chris@672: 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@683: auto mainModel = ModelById::getAs(m_mainModel); Chris@683: if (mainModel) { Chris@108: Chris@660: #ifdef DEBUG_DOCUMENT Chris@660: SVDEBUG << "Document::toXml: writing main model" << endl; Chris@660: #endif Chris@660: Chris@226: if (asTemplate) { Chris@226: writePlaceholderMainModel(out, indent + " "); Chris@226: } else { Chris@683: mainModel->toXml(out, indent + " ", "mainModel=\"true\""); Chris@226: } Chris@108: Chris@686: auto playParameters = Chris@683: PlayParameterRepository::getInstance()->getPlayParameters Chris@683: (m_mainModel.untyped); Chris@108: if (playParameters) { Chris@108: playParameters->toXml Chris@108: (out, indent + " ", Chris@108: QString("model=\"%1\"") Chris@683: .arg(mainModel->getExportId())); Chris@108: } Chris@660: } else { Chris@660: #ifdef DEBUG_DOCUMENT Chris@660: SVDEBUG << "Document::toXml: have no main model to write" << endl; Chris@660: #endif 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@683: 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@683: ModelId modelId = i->first->getModel(); Chris@700: ModelId sourceId = i->first->getSourceModel(); Chris@700: if (!modelId.isNone()) used.insert(modelId); Chris@700: if (!sourceId.isNone()) used.insert(sourceId); 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@629: // Chris@629: // Later note: This turns out not to be a great idea - we can't Chris@629: // use an aggregate model to drive a derivation unless its Chris@629: // component models have all also already been loaded. So we Chris@629: // really should have written non-aggregate read-only Chris@629: // (i.e. non-derived) wave-type models first, then aggregate Chris@629: // models, then models that have derivations. But we didn't do Chris@629: // that, so existing sessions will always have the aggregate Chris@629: // models first and we might as well stick with that. Chris@589: Chris@683: for (auto modelId: m_aggregateModels) { Chris@589: Chris@660: #ifdef DEBUG_DOCUMENT Chris@683: SVDEBUG << "Document::toXml: checking aggregate model " Chris@683: << modelId << endl; Chris@660: #endif Chris@683: Chris@683: auto aggregate = ModelById::getAs(modelId); Chris@589: if (!aggregate) continue; Chris@683: if (used.find(modelId) == used.end()) { Chris@660: #ifdef DEBUG_DOCUMENT Chris@589: SVDEBUG << "(unused, skipping)" << endl; Chris@660: #endif Chris@589: continue; Chris@589: } Chris@589: Chris@660: #ifdef DEBUG_DOCUMENT Chris@589: SVDEBUG << "(used, writing)" << endl; Chris@660: #endif Chris@589: Chris@589: aggregate->toXml(out, indent + " "); Chris@589: } Chris@589: Chris@683: std::set written; Chris@111: Chris@629: // Now write the other models in two passes: first the models that Chris@629: // aren't derived from anything (in case they are source Chris@629: // components for an aggregate model, in which case we need to Chris@629: // have seen them before we see any models derived from aggregates Chris@629: // that use them - see the lament above) and then the models that Chris@629: // have derivations. Chris@45: Chris@629: const int nonDerivedPass = 0, derivedPass = 1; Chris@629: for (int pass = nonDerivedPass; pass <= derivedPass; ++pass) { Chris@629: Chris@683: for (auto rec: m_models) { Chris@45: Chris@683: ModelId modelId = rec.first; Chris@629: Chris@683: if (used.find(modelId) == used.end()) continue; Chris@45: Chris@683: auto model = ModelById::get(modelId); Chris@683: if (!model) continue; Chris@683: Chris@660: #ifdef DEBUG_DOCUMENT Chris@683: SVDEBUG << "Document::toXml: looking at model " << modelId Chris@683: << " [pass = " << pass << "]" << endl; Chris@660: #endif Chris@660: Chris@629: // We need an intelligent way to determine which models Chris@629: // need to be streamed (i.e. have been edited, or are Chris@629: // small) and which should not be (i.e. remain as Chris@629: // generated by a transform, and are large). Chris@629: // Chris@629: // At the moment we can get away with deciding not to Chris@629: // stream dense 3d models or writable wave file models, Chris@629: // provided they were generated from a transform, because Chris@629: // at the moment there is no way to edit those model types Chris@629: // so it should be safe to regenerate them. That won't Chris@629: // always work in future though. It would be particularly Chris@629: // nice to be able to ask the user, as well as making an Chris@629: // intelligent guess. Chris@45: Chris@629: bool writeModel = true; Chris@629: bool haveDerivation = false; Chris@629: Chris@683: if (!rec.second.source.isNone() && Chris@683: rec.second.transform.getIdentifier() != "") { Chris@629: haveDerivation = true; Chris@629: } Chris@45: Chris@629: if (pass == nonDerivedPass) { Chris@629: if (haveDerivation) { Chris@629: SVDEBUG << "skipping derived model " << model->objectName() << " during nonDerivedPass" << endl; Chris@629: continue; Chris@629: } Chris@629: } else { Chris@629: if (!haveDerivation) { Chris@629: SVDEBUG << "skipping non-derived model " << model->objectName() << " during derivedPass" << endl; Chris@629: continue; Chris@629: } Chris@629: } Chris@45: Chris@629: if (haveDerivation) { Chris@683: if (ModelById::isa(modelId) || Chris@683: ModelById::isa(modelId)) { Chris@629: writeModel = false; Chris@629: } Chris@629: } Chris@629: Chris@629: if (writeModel) { Chris@629: model->toXml(out, indent + " "); Chris@683: written.insert(modelId); Chris@629: } Chris@629: Chris@629: if (haveDerivation) { Chris@629: writeBackwardCompatibleDerivation(out, indent + " ", Chris@683: modelId, rec.second); Chris@629: } Chris@629: Chris@686: auto playParameters = Chris@683: PlayParameterRepository::getInstance()->getPlayParameters Chris@683: (modelId.untyped); Chris@629: if (playParameters) { Chris@629: playParameters->toXml Chris@629: (out, indent + " ", Chris@629: QString("model=\"%1\"") Chris@657: .arg(model->getExportId())); Chris@45: } Chris@45: } Chris@45: } 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@683: for (auto modelId: written) { Chris@111: Chris@683: auto model = ModelById::get(modelId); Chris@683: if (!model) continue; Chris@683: Chris@683: auto alignment = ModelById::get(model->getAlignment()); Chris@111: if (!alignment) continue; Chris@111: Chris@111: alignment->toXml(out, indent + " "); Chris@111: } Chris@111: Chris@663: for (auto i = m_layers.begin(); i != m_layers.end(); ++i) { 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@683: auto mainModel = ModelById::get(m_mainModel); Chris@683: if (!mainModel) return; Chris@226: out << indent; Chris@226: out << QString("\n") Chris@683: .arg(mainModel->getExportId()) Chris@683: .arg(mainModel->getSampleRate()); Chris@226: } Chris@226: Chris@226: void Chris@72: Document::writeBackwardCompatibleDerivation(QTextStream &out, QString indent, Chris@683: ModelId targetModelId, 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@683: Chris@683: auto targetModel = ModelById::get(targetModelId); Chris@683: if (!targetModel) return; Chris@683: 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@657: // .arg(rec.source->getExportId()) Chris@657: // .arg(targetModel->getExportId()) 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@683: .arg(ModelById::getExportId(rec.source)) Chris@657: .arg(targetModel->getExportId()) 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: