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@45: #include "layer/Layer.h" Chris@45: #include "base/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@54: #include "plugin/transform/TransformFactory.h" Chris@54: #include "plugin/transform/ModelTransformerFactory.h" Chris@45: #include Chris@45: #include Chris@45: #include Chris@45: Chris@45: // For alignment: Chris@45: #include "data/model/AggregateWaveModel.h" Chris@45: #include "data/model/SparseTimeValueModel.h" Chris@45: #include "data/model/AlignmentModel.h" Chris@45: Chris@45: //!!! still need to handle command history, documentRestored/documentModified Chris@45: Chris@45: Document::Document() : Chris@47: m_mainModel(0), Chris@47: m_autoAlignment(false) Chris@45: { Chris@45: connect(this, SIGNAL(modelAboutToBeDeleted(Model *)), Chris@54: ModelTransformerFactory::getInstance(), Chris@45: SLOT(modelAboutToBeDeleted(Model *))); 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@45: // std::cerr << "\n\nDocument::~Document: about to clear command history" << std::endl; Chris@45: CommandHistory::getInstance()->clear(); Chris@45: Chris@52: std::cerr << "Document::~Document: about to delete layers" << std::endl; Chris@45: while (!m_layers.empty()) { Chris@45: deleteLayer(*m_layers.begin(), true); Chris@45: } Chris@45: Chris@45: if (!m_models.empty()) { Chris@45: std::cerr << "Document::~Document: WARNING: " Chris@45: << m_models.size() << " model(s) still remain -- " Chris@45: << "should have been garbage collected when deleting layers" Chris@45: << std::endl; Chris@45: while (!m_models.empty()) { Chris@45: Model *model = m_models.begin()->first; Chris@45: if (model == m_mainModel) { Chris@45: // just in case! Chris@45: std::cerr << "Document::~Document: WARNING: Main model is also" Chris@45: << " in models list!" << std::endl; Chris@45: } else if (model) { Chris@45: emit modelAboutToBeDeleted(model); Chris@45: model->aboutToDelete(); Chris@45: delete model; Chris@45: } Chris@45: m_models.erase(m_models.begin()); Chris@45: } Chris@45: } Chris@45: Chris@45: // std::cerr << "Document::~Document: About to get rid of main model" Chris@45: // << std::endl; Chris@45: if (m_mainModel) { Chris@45: emit modelAboutToBeDeleted(m_mainModel); Chris@45: m_mainModel->aboutToDelete(); Chris@45: } Chris@45: Chris@45: emit mainModelChanged(0); Chris@45: delete m_mainModel; Chris@45: 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@52: std::cerr << "Document::createLayer: Added layer of type " << type Chris@52: << ", now have " << m_layers.size() << " layers" << std::endl; 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@45: LayerFactory::getInstance()->getValidLayerTypes(model); Chris@45: Chris@45: if (types.empty()) { Chris@45: std::cerr << "WARNING: Document::importLayer: no valid display layer for model" << std::endl; Chris@45: 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@52: std::cerr << "Document::createImportedLayer: Added layer of type " << type Chris@52: << ", now have " << m_layers.size() << " layers" << std::endl; 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@45: Model *newModel = Chris@45: 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@45: delete newModel; Chris@45: 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@54: 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@45: Chris@45: Layer * Chris@54: Document::createDerivedLayer(TransformId transform, Chris@45: Model *inputModel, Chris@53: const PluginTransformer::ExecutionContext &context, Chris@45: QString configurationXml) Chris@45: { Chris@45: Model *newModel = addDerivedModel(transform, inputModel, Chris@45: context, configurationXml); Chris@45: if (!newModel) { Chris@45: // error already printed to stderr by addDerivedModel Chris@45: emit modelGenerationFailed(transform); Chris@45: return 0; Chris@45: } Chris@45: Chris@45: LayerFactory::LayerTypeSet types = Chris@45: LayerFactory::getInstance()->getValidLayerTypes(newModel); Chris@45: Chris@45: if (types.empty()) { Chris@53: std::cerr << "WARNING: Document::createLayerForTransformer: no valid display layer for output of transform " << transform.toStdString() << std::endl; Chris@45: delete newModel; Chris@45: return 0; Chris@45: } Chris@45: Chris@45: //!!! for now, just use the first suitable layer type Chris@45: Chris@45: Layer *newLayer = createLayer(*types.begin()); Chris@45: setModel(newLayer, newModel); Chris@45: Chris@45: //!!! We need to clone the model when adding the layer, so that it Chris@45: //can be edited without affecting other layers that are based on Chris@45: //the same model. Unfortunately we can't just clone it now, Chris@45: //because it probably hasn't been completed yet -- the transform Chris@45: //runs in the background. Maybe the transform has to handle Chris@45: //cloning and cacheing models itself. Chris@45: // Chris@45: // Once we do clone models here, of course, we'll have to avoid Chris@45: // leaking them too. Chris@45: // Chris@45: // We want the user to be able to add a model to a second layer Chris@45: // _while it's still being calculated in the first_ and have it Chris@45: // work quickly. That means we need to put the same physical Chris@45: // model pointer in both layers, so they can't actually be cloned. Chris@45: Chris@45: if (newLayer) { Chris@45: newLayer->setObjectName(getUniqueLayerName Chris@54: (TransformFactory::getInstance()-> Chris@54: getTransformFriendlyName(transform))); Chris@45: } Chris@45: Chris@45: emit layerAdded(newLayer); Chris@45: return newLayer; 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@45: Chris@45: emit modelAdded(m_mainModel); 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@52: std::cerr << "Document::setMainModel: Have " Chris@52: << m_layers.size() << " layers" << std::endl; Chris@52: Chris@45: for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@45: Chris@45: Layer *layer = *i; Chris@45: Model *model = layer->getModel(); Chris@45: Chris@45: // std::cerr << "Document::setMainModel: inspecting model " Chris@45: // << (model ? model->objectName().toStdString() : "(null)") << " in layer " Chris@45: // << layer->objectName().toStdString() << std::endl; Chris@45: Chris@45: if (model == oldMainModel) { Chris@45: // std::cerr << "... it uses the old main model, replacing" << std::endl; Chris@45: LayerFactory::getInstance()->setModel(layer, m_mainModel); Chris@45: continue; Chris@45: } Chris@45: Chris@45: if (m_models.find(model) == m_models.end()) { Chris@45: std::cerr << "WARNING: Document::setMainModel: Unknown model " Chris@45: << model << " in layer " << layer << std::endl; Chris@45: // get rid of this hideous degenerate Chris@45: obsoleteLayers.push_back(layer); Chris@45: continue; Chris@45: } Chris@45: Chris@45: if (m_models[model].source == oldMainModel) { Chris@45: Chris@45: // std::cerr << "... it uses a model derived from the old main model, regenerating" << std::endl; Chris@45: Chris@45: // This model was derived from the previous main Chris@45: // model: regenerate it. Chris@45: Chris@54: TransformId transform = m_models[model].transform; Chris@53: PluginTransformer::ExecutionContext context = m_models[model].context; Chris@45: Chris@45: Model *replacementModel = Chris@45: addDerivedModel(transform, Chris@45: m_mainModel, Chris@45: context, Chris@45: m_models[model].configurationXml); Chris@45: Chris@45: if (!replacementModel) { Chris@45: std::cerr << "WARNING: Document::setMainModel: Failed to regenerate model for transform \"" Chris@45: << transform.toStdString() << "\"" << " in layer " << layer << std::endl; Chris@53: if (failedTransformers.find(transform) == failedTransformers.end()) { Chris@45: emit modelRegenerationFailed(layer->objectName(), Chris@45: transform); Chris@53: failedTransformers.insert(transform); Chris@45: } Chris@45: obsoleteLayers.push_back(layer); Chris@45: } else { Chris@45: std::cerr << "Replacing model " << model << " (type " Chris@45: << typeid(*model).name() << ") with model " Chris@45: << replacementModel << " (type " Chris@45: << typeid(*replacementModel).name() << ") in layer " Chris@45: << layer << " (name " << layer->objectName().toStdString() << ")" Chris@45: << std::endl; Chris@45: RangeSummarisableTimeValueModel *rm = Chris@45: dynamic_cast(replacementModel); Chris@45: if (rm) { Chris@45: std::cerr << "new model has " << rm->getChannelCount() << " channels " << std::endl; Chris@45: } else { Chris@45: std::cerr << "new model is not a RangeSummarisableTimeValueModel!" << std::endl; Chris@45: } Chris@45: setModel(layer, replacementModel); Chris@45: } Chris@45: } Chris@45: } Chris@45: Chris@45: for (size_t k = 0; k < obsoleteLayers.size(); ++k) { Chris@45: deleteLayer(obsoleteLayers[k], true); Chris@45: } Chris@45: Chris@48: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@48: if (i->first->getAlignmentReference() == oldMainModel) { Chris@48: alignModel(i->first); Chris@48: } Chris@48: } Chris@48: Chris@45: emit mainModelChanged(m_mainModel); Chris@45: Chris@45: // we already emitted modelAboutToBeDeleted for this Chris@45: delete oldMainModel; Chris@45: } Chris@45: Chris@45: void Chris@54: Document::addDerivedModel(TransformId transform, Chris@45: Model *inputModel, Chris@53: const PluginTransformer::ExecutionContext &context, Chris@45: Model *outputModelToAdd, Chris@45: QString configurationXml) Chris@45: { Chris@45: if (m_models.find(outputModelToAdd) != m_models.end()) { Chris@45: std::cerr << "WARNING: Document::addDerivedModel: Model already added" Chris@45: << std::endl; Chris@45: return; Chris@45: } Chris@45: Chris@45: // std::cerr << "Document::addDerivedModel: source is " << inputModel << " \"" << inputModel->objectName().toStdString() << "\"" << std::endl; Chris@45: Chris@45: ModelRecord rec; Chris@45: rec.source = inputModel; Chris@45: rec.transform = transform; Chris@45: rec.context = context; Chris@45: rec.configurationXml = configurationXml; Chris@45: rec.refcount = 0; Chris@45: Chris@45: outputModelToAdd->setSourceModel(inputModel); Chris@45: Chris@45: m_models[outputModelToAdd] = rec; Chris@45: 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@45: std::cerr << "WARNING: Document::addImportedModel: Model already added" Chris@45: << std::endl; Chris@45: return; Chris@45: } Chris@45: Chris@45: ModelRecord rec; Chris@45: rec.source = 0; Chris@45: rec.transform = ""; Chris@45: rec.refcount = 0; Chris@45: Chris@45: m_models[model] = rec; Chris@45: Chris@47: if (m_autoAlignment) alignModel(model); Chris@47: Chris@45: emit modelAdded(model); Chris@45: } Chris@45: Chris@45: Model * Chris@54: Document::addDerivedModel(TransformId transform, Chris@45: Model *inputModel, Chris@53: const PluginTransformer::ExecutionContext &context, Chris@45: QString configurationXml) Chris@45: { Chris@45: Model *model = 0; Chris@45: Chris@45: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@45: if (i->second.transform == transform && Chris@45: i->second.source == inputModel && Chris@45: i->second.context == context && Chris@45: i->second.configurationXml == configurationXml) { Chris@45: return i->first; Chris@45: } Chris@45: } Chris@45: Chris@54: model = ModelTransformerFactory::getInstance()->transform Chris@45: (transform, inputModel, context, configurationXml); Chris@45: Chris@45: if (!model) { Chris@45: std::cerr << "WARNING: Document::addDerivedModel: no output model for transform " << transform.toStdString() << std::endl; Chris@45: } else { Chris@45: addDerivedModel(transform, inputModel, context, model, configurationXml); Chris@45: } Chris@45: Chris@45: return model; 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@45: return; Chris@45: } Chris@45: Chris@45: if (model == m_mainModel) { Chris@45: return; Chris@45: } Chris@45: Chris@45: bool toDelete = false; Chris@45: Chris@45: if (m_models.find(model) != m_models.end()) { Chris@45: Chris@45: if (m_models[model].refcount == 0) { Chris@45: std::cerr << "WARNING: Document::releaseModel: model " << model Chris@45: << " reference count is zero already!" << std::endl; Chris@45: } else { Chris@45: if (--m_models[model].refcount == 0) { Chris@45: toDelete = true; Chris@45: } Chris@45: } Chris@45: } else { Chris@45: std::cerr << "WARNING: Document::releaseModel: Unfound model " Chris@45: << model << std::endl; Chris@45: toDelete = true; Chris@45: } Chris@45: Chris@45: if (toDelete) { Chris@45: Chris@45: int sourceCount = 0; Chris@45: Chris@45: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@45: if (i->second.source == model) { Chris@45: ++sourceCount; Chris@45: i->second.source = 0; Chris@45: } Chris@45: } Chris@45: Chris@45: if (sourceCount > 0) { Chris@45: std::cerr << "Document::releaseModel: Deleting model " Chris@45: << model << " even though it is source for " Chris@45: << sourceCount << " other derived model(s) -- resetting " Chris@45: << "their source fields appropriately" << std::endl; Chris@45: } Chris@45: Chris@45: emit modelAboutToBeDeleted(model); Chris@45: model->aboutToDelete(); Chris@45: m_models.erase(model); Chris@45: 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@45: m_layerViewMap[layer].size() > 0) { Chris@45: Chris@45: std::cerr << "WARNING: Document::deleteLayer: Layer " Chris@45: << layer << " [" << layer->objectName().toStdString() << "]" Chris@45: << " is still used in " << m_layerViewMap[layer].size() Chris@45: << " views!" << std::endl; Chris@45: Chris@45: if (force) { Chris@45: Chris@45: std::cerr << "(force flag set -- deleting from all views)" << std::endl; Chris@45: Chris@45: for (std::set::iterator j = m_layerViewMap[layer].begin(); Chris@45: j != m_layerViewMap[layer].end(); ++j) { Chris@45: // don't use removeLayerFromView, as it issues a command Chris@45: layer->setLayerDormant(*j, true); Chris@45: (*j)->removeLayer(layer); Chris@45: } Chris@45: Chris@45: m_layerViewMap.erase(layer); Chris@45: Chris@45: } else { Chris@45: return; Chris@45: } Chris@45: } Chris@45: Chris@45: if (m_layers.find(layer) == m_layers.end()) { Chris@45: std::cerr << "Document::deleteLayer: Layer " Chris@45: << layer << " does not exist, or has already been deleted " Chris@45: << "(this may not be as serious as it sounds)" << std::endl; Chris@45: return; Chris@45: } Chris@45: Chris@45: m_layers.erase(layer); Chris@45: Chris@52: std::cerr << "Document::deleteLayer: Removing, now have " Chris@52: << m_layers.size() << " layers" << std::endl; 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@45: model != m_mainModel && Chris@45: m_models.find(model) == m_models.end()) { Chris@45: std::cerr << "ERROR: Document::setModel: Layer " << layer Chris@45: << " (\"" << layer->objectName().toStdString() Chris@45: << "\") wants to use unregistered model " << model Chris@45: << ": register the layer's model before setting it!" Chris@45: << std::endl; Chris@45: return; Chris@45: } Chris@45: Chris@45: Model *previousModel = layer->getModel(); Chris@45: Chris@45: if (previousModel == model) { Chris@45: std::cerr << "WARNING: Document::setModel: Layer " << layer << " (\"" Chris@45: << layer->objectName().toStdString() Chris@45: << "\") is already set to model " Chris@45: << model << " (\"" Chris@45: << (model ? model->objectName().toStdString() : "(null)") Chris@45: << "\")" << std::endl; Chris@45: return; Chris@45: } Chris@45: Chris@45: if (model && model != m_mainModel) { Chris@45: 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@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@45: // std::cerr << "Document::addLayerToView: Layer (\"" Chris@45: // << layer->objectName().toStdString() Chris@45: // << "\") with no model being added to view: " Chris@45: // << "normally you want to set the model first" << std::endl; Chris@45: } else { Chris@45: if (model != m_mainModel && Chris@45: m_models.find(model) == m_models.end()) { Chris@45: std::cerr << "ERROR: Document::addLayerToView: Layer " << layer Chris@45: << " has unregistered model " << model Chris@45: << " -- register the layer's model before adding the layer!" << std::endl; Chris@45: return; Chris@45: } Chris@45: } Chris@45: Chris@45: CommandHistory::getInstance()->addCommand Chris@45: (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@45: (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@45: m_layerViewMap[layer].end()) { Chris@45: std::cerr << "WARNING: Document::addToLayerViewMap:" Chris@45: << " Layer " << layer << " -> view " << view << " already in" Chris@45: << " layer view map -- internal inconsistency" << std::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@45: m_layerViewMap[layer].end()) { Chris@45: std::cerr << "WARNING: Document::removeFromLayerViewMap:" Chris@45: << " Layer " << layer << " -> view " << view << " not in" Chris@45: << " layer view map -- internal inconsistency" << std::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@53: Document::getTransformerInputModels() 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@51: Document::canAlign() Chris@50: { Chris@54: TransformId id = "vamp:match-vamp-plugin:match:path"; Chris@54: TransformFactory *factory = TransformFactory::getInstance(); Chris@54: return factory->haveTransform(id); Chris@50: } Chris@50: Chris@45: void Chris@45: Document::alignModel(Model *model) Chris@45: { Chris@45: if (!m_mainModel || model == m_mainModel) return; Chris@45: Chris@45: RangeSummarisableTimeValueModel *rm = Chris@45: dynamic_cast(model); Chris@45: if (!rm) return; Chris@48: Chris@48: if (rm->getAlignmentReference() == m_mainModel) return; Chris@45: Chris@45: // This involves creating three new models: Chris@45: Chris@45: // 1. an AggregateWaveModel to provide the mixdowns of the main Chris@45: // model and the new model in its two channels, as input to the Chris@45: // MATCH plugin Chris@45: Chris@45: // 2. a SparseTimeValueModel, which is the model automatically Chris@53: // created by FeatureExtractionPluginTransformer when running the Chris@45: // MATCH plugin (thus containing the alignment path) Chris@45: Chris@45: // 3. an AlignmentModel, which stores the path model and carries Chris@45: // out alignment lookups on it. Chris@45: Chris@45: // The first two of these are provided as arguments to the Chris@45: // constructor for the third, which takes responsibility for Chris@45: // deleting them. The AlignmentModel, meanwhile, is passed to the Chris@45: // new model we are aligning, which also takes responsibility for Chris@45: // it. We should not have to delete any of these new models here. Chris@45: Chris@45: AggregateWaveModel::ChannelSpecList components; Chris@45: Chris@45: components.push_back(AggregateWaveModel::ModelChannelSpec Chris@45: (m_mainModel, -1)); Chris@45: Chris@45: components.push_back(AggregateWaveModel::ModelChannelSpec Chris@45: (rm, -1)); Chris@45: Chris@45: Model *aggregate = new AggregateWaveModel(components); Chris@45: Chris@54: TransformId id = "vamp:match-vamp-plugin:match:path"; Chris@45: Chris@54: ModelTransformerFactory *factory = ModelTransformerFactory::getInstance(); Chris@45: Chris@45: Model *transformOutput = factory->transform Chris@45: (id, aggregate, Chris@53: factory->getDefaultContextForTransformer(id, aggregate), Chris@45: ""); Chris@45: Chris@45: SparseTimeValueModel *path = dynamic_cast Chris@45: (transformOutput); Chris@45: Chris@45: if (!path) { Chris@45: std::cerr << "Document::alignModel: ERROR: Failed to create alignment path (no MATCH plugin?)" << std::endl; Chris@45: delete transformOutput; Chris@45: delete aggregate; Chris@45: return; Chris@45: } Chris@45: Chris@45: AlignmentModel *alignmentModel = new AlignmentModel Chris@45: (m_mainModel, model, aggregate, path); Chris@45: Chris@45: rm->setAlignment(alignmentModel); 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@45: } Chris@45: Chris@45: Document::AddLayerCommand::AddLayerCommand(Document *d, Chris@45: View *view, Chris@45: 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@45: // std::cerr << "Document::AddLayerCommand::~AddLayerCommand" << std::endl; Chris@45: if (!m_added) { Chris@45: m_d->deleteLayer(m_layer); Chris@45: } Chris@45: } Chris@45: Chris@45: void Chris@45: Document::AddLayerCommand::execute() Chris@45: { Chris@45: for (int i = 0; i < m_view->getLayerCount(); ++i) { Chris@45: if (m_view->getLayer(i) == m_layer) { Chris@45: // already there Chris@45: m_layer->setLayerDormant(m_view, false); Chris@45: m_added = true; Chris@45: return; Chris@45: } 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@45: View *view, Chris@45: Layer *layer) : Chris@45: m_d(d), Chris@45: m_view(view), Chris@45: m_layer(layer), 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@45: // std::cerr << "Document::RemoveLayerCommand::~RemoveLayerCommand" << std::endl; Chris@45: if (!m_added) { Chris@45: m_d->deleteLayer(m_layer); Chris@45: } Chris@45: } Chris@45: 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@45: if (m_view->getLayer(i) == m_layer) { Chris@45: have = true; Chris@45: break; Chris@45: } Chris@45: } Chris@45: Chris@45: if (!have) { // not there! Chris@45: m_layer->setLayerDormant(m_view, true); Chris@45: m_added = false; Chris@45: 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@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::toXml(QTextStream &out, QString indent, QString extraAttributes) const Chris@45: { Chris@45: out << indent + QString("\n") Chris@45: .arg(extraAttributes == "" ? "" : " ").arg(extraAttributes); Chris@45: Chris@45: if (m_mainModel) { Chris@45: m_mainModel->toXml(out, indent + " ", "mainModel=\"true\""); 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@45: if (i->first && !i->second.empty() && i->first->getModel()) { Chris@45: used.insert(i->first->getModel()); Chris@45: } Chris@45: } Chris@45: Chris@45: for (ModelMap::const_iterator i = m_models.begin(); Chris@45: i != m_models.end(); ++i) { Chris@45: Chris@45: const Model *model = i->first; Chris@45: 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@45: if (rec.source && rec.transform != "") { 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@45: i->first->toXml(out, indent + " "); Chris@45: } Chris@45: Chris@45: if (haveDerivation) { Chris@45: Chris@45: QString extentsAttributes; Chris@45: if (rec.context.startFrame != 0 || Chris@45: rec.context.duration != 0) { Chris@45: extentsAttributes = QString("startFrame=\"%1\" duration=\"%2\" ") Chris@45: .arg(rec.context.startFrame) Chris@45: .arg(rec.context.duration); Chris@45: } Chris@45: Chris@45: out << indent; Chris@45: out << QString(" first)) Chris@45: .arg(rec.context.channel) Chris@45: .arg(rec.context.domain) Chris@45: .arg(rec.context.stepSize) Chris@45: .arg(rec.context.blockSize) Chris@45: .arg(extentsAttributes) Chris@45: .arg(int(rec.context.windowType)) Chris@45: .arg(XmlExportable::encodeEntities(rec.transform)); Chris@45: Chris@45: if (rec.configurationXml != "") { Chris@45: out << ">\n " + indent + rec.configurationXml Chris@45: + "\n" + indent + " \n"; Chris@45: } else { Chris@45: out << "/>\n"; Chris@45: } Chris@45: } Chris@45: Chris@45: //!!! We should probably own the PlayParameterRepository Chris@45: PlayParameters *playParameters = Chris@45: PlayParameterRepository::getInstance()->getPlayParameters(i->first); Chris@45: if (playParameters) { Chris@45: playParameters->toXml Chris@45: (out, indent + " ", Chris@45: QString("model=\"%1\"") Chris@45: .arg(XmlExportable::getObjectExportId(i->first))); Chris@45: } Chris@45: } Chris@45: Chris@45: for (LayerSet::const_iterator i = m_layers.begin(); Chris@45: i != m_layers.end(); ++i) { Chris@45: Chris@45: (*i)->toXml(out, indent + " "); Chris@45: } Chris@45: Chris@45: out << indent + "\n"; Chris@45: } Chris@45: Chris@45: