Chris@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: Sonic Visualiser Chris@0: An audio file viewer and annotation editor. Chris@0: Centre for Digital Music, Queen Mary, University of London. Chris@77: This file copyright 2006 Chris Cannam and QMUL. Chris@0: Chris@0: This program is free software; you can redistribute it and/or Chris@0: modify it under the terms of the GNU General Public License as Chris@0: published by the Free Software Foundation; either version 2 of the Chris@0: License, or (at your option) any later version. See the file Chris@0: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "Document.h" Chris@0: Chris@1: #include "data/model/WaveFileModel.h" Chris@55: #include "data/model/WritableWaveFileModel.h" Chris@55: #include "data/model/DenseThreeDimensionalModel.h" Chris@66: #include "data/model/DenseTimeValueModel.h" Chris@1: #include "layer/Layer.h" Chris@0: #include "base/CommandHistory.h" Chris@0: #include "base/Command.h" Chris@1: #include "view/View.h" Chris@0: #include "base/PlayParameterRepository.h" Chris@0: #include "base/PlayParameters.h" Chris@0: #include "transform/TransformFactory.h" Chris@0: #include Chris@0: Chris@0: //!!! still need to handle command history, documentRestored/documentModified Chris@0: Chris@0: Document::Document() : Chris@0: m_mainModel(0) Chris@0: { Chris@0: } Chris@0: Chris@0: Document::~Document() Chris@0: { Chris@0: //!!! Document should really own the command history. atm we Chris@0: //still refer to it in various places that don't have access to Chris@0: //the document, be nice to fix that Chris@0: Chris@0: // std::cerr << "\n\nDocument::~Document: about to clear command history" << std::endl; Chris@0: CommandHistory::getInstance()->clear(); Chris@0: Chris@0: // std::cerr << "Document::~Document: about to delete layers" << std::endl; Chris@0: while (!m_layers.empty()) { Chris@0: deleteLayer(*m_layers.begin(), true); Chris@0: } Chris@0: Chris@0: if (!m_models.empty()) { Chris@0: std::cerr << "Document::~Document: WARNING: " Chris@0: << m_models.size() << " model(s) still remain -- " Chris@0: << "should have been garbage collected when deleting layers" Chris@0: << std::endl; Chris@0: while (!m_models.empty()) { Chris@0: if (m_models.begin()->first == m_mainModel) { Chris@0: // just in case! Chris@0: std::cerr << "Document::~Document: WARNING: Main model is also" Chris@0: << " in models list!" << std::endl; Chris@0: } else { Chris@0: emit modelAboutToBeDeleted(m_models.begin()->first); Chris@0: delete m_models.begin()->first; Chris@0: } Chris@0: m_models.erase(m_models.begin()); Chris@0: } Chris@0: } Chris@0: Chris@0: // std::cerr << "Document::~Document: About to get rid of main model" Chris@0: // << std::endl; Chris@0: emit modelAboutToBeDeleted(m_mainModel); Chris@0: emit mainModelChanged(0); Chris@0: delete m_mainModel; Chris@0: Chris@0: } Chris@0: Chris@0: Layer * Chris@0: Document::createLayer(LayerFactory::LayerType type) Chris@0: { Chris@0: Layer *newLayer = LayerFactory::getInstance()->createLayer(type); Chris@0: if (!newLayer) return 0; Chris@0: Chris@0: newLayer->setObjectName(getUniqueLayerName(newLayer->objectName())); Chris@0: Chris@0: m_layers.insert(newLayer); Chris@0: emit layerAdded(newLayer); Chris@0: Chris@0: return newLayer; Chris@0: } Chris@0: Chris@0: Layer * Chris@0: Document::createMainModelLayer(LayerFactory::LayerType type) Chris@0: { Chris@0: Layer *newLayer = createLayer(type); Chris@0: if (!newLayer) return 0; Chris@0: setModel(newLayer, m_mainModel); Chris@0: return newLayer; Chris@0: } Chris@0: Chris@0: Layer * Chris@0: Document::createImportedLayer(Model *model) Chris@0: { Chris@0: LayerFactory::LayerTypeSet types = Chris@0: LayerFactory::getInstance()->getValidLayerTypes(model); Chris@0: Chris@0: if (types.empty()) { Chris@0: std::cerr << "WARNING: Document::importLayer: no valid display layer for model" << std::endl; Chris@0: return 0; Chris@0: } Chris@0: Chris@0: //!!! for now, just use the first suitable layer type Chris@0: LayerFactory::LayerType type = *types.begin(); Chris@0: Chris@0: Layer *newLayer = LayerFactory::getInstance()->createLayer(type); Chris@0: if (!newLayer) return 0; Chris@0: Chris@0: newLayer->setObjectName(getUniqueLayerName(newLayer->objectName())); Chris@0: Chris@0: addImportedModel(model); Chris@0: setModel(newLayer, model); Chris@0: Chris@0: //!!! and all channels Chris@0: setChannel(newLayer, -1); Chris@0: Chris@0: m_layers.insert(newLayer); Chris@0: emit layerAdded(newLayer); Chris@0: return newLayer; Chris@0: } Chris@0: Chris@0: Layer * Chris@0: Document::createEmptyLayer(LayerFactory::LayerType type) Chris@0: { Chris@0: Model *newModel = Chris@0: LayerFactory::getInstance()->createEmptyModel(type, m_mainModel); Chris@0: if (!newModel) return 0; Chris@0: Chris@0: Layer *newLayer = createLayer(type); Chris@0: if (!newLayer) { Chris@0: delete newModel; Chris@0: return 0; Chris@0: } Chris@0: Chris@0: addImportedModel(newModel); Chris@0: setModel(newLayer, newModel); Chris@0: Chris@0: return newLayer; Chris@0: } Chris@0: Chris@0: Layer * Chris@0: Document::createDerivedLayer(LayerFactory::LayerType type, Chris@107: TransformId transform) Chris@0: { Chris@0: Layer *newLayer = createLayer(type); Chris@0: if (!newLayer) return 0; Chris@0: Chris@0: newLayer->setObjectName(getUniqueLayerName Chris@0: (TransformFactory::getInstance()-> Chris@0: getTransformFriendlyName(transform))); Chris@0: Chris@0: return newLayer; Chris@0: } Chris@0: Chris@0: Layer * Chris@107: Document::createDerivedLayer(TransformId transform, Chris@0: Model *inputModel, Chris@27: const PluginTransform::ExecutionContext &context, Chris@0: QString configurationXml) Chris@0: { Chris@55: Model *newModel = addDerivedModel(transform, inputModel, Chris@55: context, configurationXml); Chris@0: if (!newModel) { Chris@55: // error already printed to stderr by addDerivedModel Chris@0: emit modelGenerationFailed(transform); Chris@0: return 0; Chris@0: } Chris@0: Chris@0: LayerFactory::LayerTypeSet types = Chris@0: LayerFactory::getInstance()->getValidLayerTypes(newModel); Chris@0: Chris@0: if (types.empty()) { Chris@0: std::cerr << "WARNING: Document::createLayerForTransform: no valid display layer for output of transform " << transform.toStdString() << std::endl; Chris@0: delete newModel; Chris@0: return 0; Chris@0: } Chris@0: Chris@0: //!!! for now, just use the first suitable layer type Chris@0: Chris@0: Layer *newLayer = createLayer(*types.begin()); Chris@0: setModel(newLayer, newModel); Chris@0: Chris@0: //!!! We need to clone the model when adding the layer, so that it Chris@0: //can be edited without affecting other layers that are based on Chris@0: //the same model. Unfortunately we can't just clone it now, Chris@0: //because it probably hasn't been completed yet -- the transform Chris@0: //runs in the background. Maybe the transform has to handle Chris@0: //cloning and cacheing models itself. Chris@0: // Chris@0: // Once we do clone models here, of course, we'll have to avoid Chris@0: // leaking them too. Chris@0: // Chris@0: // We want the user to be able to add a model to a second layer Chris@0: // _while it's still being calculated in the first_ and have it Chris@0: // work quickly. That means we need to put the same physical Chris@0: // model pointer in both layers, so they can't actually be cloned. Chris@0: Chris@0: if (newLayer) { Chris@0: newLayer->setObjectName(getUniqueLayerName Chris@0: (TransformFactory::getInstance()-> Chris@0: getTransformFriendlyName(transform))); Chris@0: } Chris@0: Chris@0: emit layerAdded(newLayer); Chris@0: return newLayer; Chris@0: } Chris@0: Chris@0: void Chris@0: Document::setMainModel(WaveFileModel *model) Chris@0: { Chris@0: Model *oldMainModel = m_mainModel; Chris@0: m_mainModel = model; Chris@0: Chris@0: emit modelAdded(m_mainModel); Chris@0: Chris@0: std::vector obsoleteLayers; Chris@0: std::set failedTransforms; Chris@0: Chris@0: // We need to ensure that no layer is left using oldMainModel or Chris@0: // any of the old derived models as its model. Either replace the Chris@0: // model, or delete the layer for each layer that is currently Chris@0: // using one of these. Carry out this replacement before we Chris@0: // delete any of the models. Chris@0: Chris@0: for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@0: Chris@0: Layer *layer = *i; Chris@0: Model *model = layer->getModel(); Chris@0: 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@0: if (model == oldMainModel) { Chris@45: std::cerr << "... it uses the old main model, replacing" << std::endl; Chris@0: LayerFactory::getInstance()->setModel(layer, m_mainModel); Chris@0: continue; Chris@0: } Chris@0: Chris@0: if (m_models.find(model) == m_models.end()) { Chris@0: std::cerr << "WARNING: Document::setMainModel: Unknown model " Chris@0: << model << " in layer " << layer << std::endl; Chris@0: // get rid of this hideous degenerate Chris@0: obsoleteLayers.push_back(layer); Chris@0: continue; Chris@0: } Chris@0: Chris@0: if (m_models[model].source == oldMainModel) { Chris@0: Chris@45: std::cerr << "... it uses a model derived from the old main model, regenerating" << std::endl; Chris@45: Chris@0: // This model was derived from the previous main Chris@0: // model: regenerate it. Chris@0: Chris@107: TransformId transform = m_models[model].transform; Chris@27: PluginTransform::ExecutionContext context = m_models[model].context; Chris@0: Chris@0: Model *replacementModel = Chris@55: addDerivedModel(transform, Chris@55: m_mainModel, Chris@55: context, Chris@55: m_models[model].configurationXml); Chris@0: Chris@0: if (!replacementModel) { Chris@0: std::cerr << "WARNING: Document::setMainModel: Failed to regenerate model for transform \"" Chris@0: << transform.toStdString() << "\"" << " in layer " << layer << std::endl; Chris@0: if (failedTransforms.find(transform) == failedTransforms.end()) { Chris@0: emit modelRegenerationFailed(layer->objectName(), Chris@0: transform); Chris@0: failedTransforms.insert(transform); Chris@0: } Chris@0: obsoleteLayers.push_back(layer); Chris@0: } 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@0: setModel(layer, replacementModel); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: for (size_t k = 0; k < obsoleteLayers.size(); ++k) { Chris@0: deleteLayer(obsoleteLayers[k], true); Chris@0: } Chris@0: Chris@0: emit mainModelChanged(m_mainModel); Chris@0: Chris@0: // we already emitted modelAboutToBeDeleted for this Chris@0: delete oldMainModel; Chris@0: } Chris@0: Chris@0: void Chris@107: Document::addDerivedModel(TransformId transform, Chris@0: Model *inputModel, Chris@27: const PluginTransform::ExecutionContext &context, Chris@0: Model *outputModelToAdd, Chris@0: QString configurationXml) Chris@0: { Chris@0: if (m_models.find(outputModelToAdd) != m_models.end()) { Chris@0: std::cerr << "WARNING: Document::addDerivedModel: Model already added" Chris@0: << std::endl; Chris@0: return; Chris@0: } Chris@0: Chris@110: std::cerr << "Document::addDerivedModel: source is " << inputModel << " \"" << inputModel->objectName().toStdString() << "\"" << std::endl; Chris@54: Chris@0: ModelRecord rec; Chris@0: rec.source = inputModel; Chris@0: rec.transform = transform; Chris@27: rec.context = context; Chris@0: rec.configurationXml = configurationXml; Chris@0: rec.refcount = 0; Chris@0: Chris@0: m_models[outputModelToAdd] = rec; Chris@0: Chris@0: emit modelAdded(outputModelToAdd); Chris@0: } Chris@0: Chris@0: Chris@0: void Chris@0: Document::addImportedModel(Model *model) Chris@0: { Chris@0: if (m_models.find(model) != m_models.end()) { Chris@0: std::cerr << "WARNING: Document::addImportedModel: Model already added" Chris@0: << std::endl; Chris@0: return; Chris@0: } Chris@0: Chris@0: ModelRecord rec; Chris@0: rec.source = 0; Chris@0: rec.transform = ""; Chris@0: rec.refcount = 0; Chris@0: Chris@0: m_models[model] = rec; Chris@0: Chris@0: emit modelAdded(model); Chris@0: } Chris@0: Chris@0: Model * Chris@107: Document::addDerivedModel(TransformId transform, Chris@55: Model *inputModel, Chris@55: const PluginTransform::ExecutionContext &context, Chris@55: QString configurationXml) Chris@0: { Chris@0: Model *model = 0; Chris@0: Chris@0: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@0: if (i->second.transform == transform && Chris@0: i->second.source == inputModel && Chris@27: i->second.context == context && Chris@0: i->second.configurationXml == configurationXml) { Chris@0: return i->first; Chris@0: } Chris@0: } Chris@0: Chris@0: model = TransformFactory::getInstance()->transform Chris@27: (transform, inputModel, context, configurationXml); Chris@0: Chris@0: if (!model) { Chris@55: std::cerr << "WARNING: Document::addDerivedModel: no output model for transform " << transform.toStdString() << std::endl; Chris@0: } else { Chris@27: addDerivedModel(transform, inputModel, context, model, configurationXml); Chris@0: } Chris@0: Chris@0: return model; Chris@0: } Chris@0: Chris@0: void Chris@0: Document::releaseModel(Model *model) // Will _not_ release main model! Chris@0: { Chris@0: if (model == 0) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if (model == m_mainModel) { Chris@0: return; Chris@0: } Chris@0: Chris@0: bool toDelete = false; Chris@0: Chris@0: if (m_models.find(model) != m_models.end()) { Chris@0: Chris@0: if (m_models[model].refcount == 0) { Chris@0: std::cerr << "WARNING: Document::releaseModel: model " << model Chris@0: << " reference count is zero already!" << std::endl; Chris@0: } else { Chris@0: if (--m_models[model].refcount == 0) { Chris@0: toDelete = true; Chris@0: } Chris@0: } Chris@0: } else { Chris@0: std::cerr << "WARNING: Document::releaseModel: Unfound model " Chris@0: << model << std::endl; Chris@0: toDelete = true; Chris@0: } Chris@0: Chris@0: if (toDelete) { Chris@0: Chris@0: int sourceCount = 0; Chris@0: Chris@0: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@0: if (i->second.source == model) { Chris@0: ++sourceCount; Chris@0: i->second.source = 0; Chris@0: } Chris@0: } Chris@0: Chris@0: if (sourceCount > 0) { Chris@0: std::cerr << "Document::releaseModel: Deleting model " Chris@0: << model << " even though it is source for " Chris@0: << sourceCount << " other derived model(s) -- resetting " Chris@0: << "their source fields appropriately" << std::endl; Chris@0: } Chris@0: Chris@0: emit modelAboutToBeDeleted(model); Chris@0: m_models.erase(model); Chris@0: delete model; Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: Document::deleteLayer(Layer *layer, bool force) Chris@0: { Chris@0: if (m_layerViewMap.find(layer) != m_layerViewMap.end() && Chris@0: m_layerViewMap[layer].size() > 0) { Chris@0: Chris@0: std::cerr << "WARNING: Document::deleteLayer: Layer " Chris@0: << layer << " [" << layer->objectName().toStdString() << "]" Chris@0: << " is still used in " << m_layerViewMap[layer].size() Chris@0: << " views!" << std::endl; Chris@0: Chris@0: if (force) { Chris@0: Chris@0: std::cerr << "(force flag set -- deleting from all views)" << std::endl; Chris@0: Chris@0: for (std::set::iterator j = m_layerViewMap[layer].begin(); Chris@0: j != m_layerViewMap[layer].end(); ++j) { Chris@0: // don't use removeLayerFromView, as it issues a command Chris@0: layer->setLayerDormant(*j, true); Chris@0: (*j)->removeLayer(layer); Chris@0: } Chris@0: Chris@0: m_layerViewMap.erase(layer); Chris@0: Chris@0: } else { Chris@0: return; Chris@0: } Chris@0: } Chris@0: Chris@0: if (m_layers.find(layer) == m_layers.end()) { Chris@0: std::cerr << "Document::deleteLayer: Layer " Chris@0: << layer << " does not exist, or has already been deleted " Chris@0: << "(this may not be as serious as it sounds)" << std::endl; Chris@0: return; Chris@0: } Chris@0: Chris@0: m_layers.erase(layer); Chris@0: Chris@0: releaseModel(layer->getModel()); Chris@0: emit layerRemoved(layer); Chris@0: emit layerAboutToBeDeleted(layer); Chris@0: delete layer; Chris@0: } Chris@0: Chris@0: void Chris@0: Document::setModel(Layer *layer, Model *model) Chris@0: { Chris@0: if (model && Chris@0: model != m_mainModel && Chris@0: m_models.find(model) == m_models.end()) { Chris@0: std::cerr << "ERROR: Document::setModel: Layer " << layer Chris@95: << " (\"" << layer->objectName().toStdString() Chris@95: << "\") wants to use unregistered model " << model Chris@0: << ": register the layer's model before setting it!" Chris@0: << std::endl; Chris@0: return; Chris@0: } Chris@0: Chris@45: Model *previousModel = layer->getModel(); Chris@45: Chris@45: if (previousModel == model) { Chris@55: std::cerr << "WARNING: Document::setModel: Layer " << layer << " (\"" Chris@55: << layer->objectName().toStdString() Chris@55: << "\") is already set to model " Chris@55: << model << " (\"" Chris@55: << (model ? model->objectName().toStdString() : "(null)") Chris@55: << "\")" << std::endl; Chris@45: return; Chris@0: } Chris@0: Chris@0: if (model && model != m_mainModel) { Chris@0: m_models[model].refcount ++; Chris@0: } Chris@0: Chris@0: LayerFactory::getInstance()->setModel(layer, model); Chris@45: Chris@45: if (previousModel) { Chris@45: releaseModel(previousModel); Chris@45: } Chris@0: } Chris@0: Chris@0: void Chris@0: Document::setChannel(Layer *layer, int channel) Chris@0: { Chris@0: LayerFactory::getInstance()->setChannel(layer, channel); Chris@0: } Chris@0: Chris@0: void Chris@0: Document::addLayerToView(View *view, Layer *layer) Chris@0: { Chris@0: Model *model = layer->getModel(); Chris@0: if (!model) { Chris@55: std::cerr << "Document::addLayerToView: Layer (\"" Chris@55: << layer->objectName().toStdString() Chris@55: << "\") with no model being added to view: " Chris@55: << "normally you want to set the model first" << std::endl; Chris@0: } else { Chris@0: if (model != m_mainModel && Chris@0: m_models.find(model) == m_models.end()) { Chris@0: std::cerr << "ERROR: Document::addLayerToView: Layer " << layer Chris@0: << " has unregistered model " << model Chris@0: << " -- register the layer's model before adding the layer!" << std::endl; Chris@0: return; Chris@0: } Chris@0: } Chris@0: Chris@0: CommandHistory::getInstance()->addCommand Chris@0: (new Document::AddLayerCommand(this, view, layer)); Chris@0: } Chris@0: Chris@0: void Chris@0: Document::removeLayerFromView(View *view, Layer *layer) Chris@0: { Chris@0: CommandHistory::getInstance()->addCommand Chris@0: (new Document::RemoveLayerCommand(this, view, layer)); Chris@0: } Chris@0: Chris@0: void Chris@0: Document::addToLayerViewMap(Layer *layer, View *view) Chris@0: { Chris@0: bool firstView = (m_layerViewMap.find(layer) == m_layerViewMap.end() || Chris@0: m_layerViewMap[layer].empty()); Chris@0: Chris@0: if (m_layerViewMap[layer].find(view) != Chris@0: m_layerViewMap[layer].end()) { Chris@0: std::cerr << "WARNING: Document::addToLayerViewMap:" Chris@0: << " Layer " << layer << " -> view " << view << " already in" Chris@0: << " layer view map -- internal inconsistency" << std::endl; Chris@0: } Chris@0: Chris@0: m_layerViewMap[layer].insert(view); Chris@0: Chris@0: if (firstView) emit layerInAView(layer, true); Chris@0: } Chris@0: Chris@0: void Chris@0: Document::removeFromLayerViewMap(Layer *layer, View *view) Chris@0: { Chris@0: if (m_layerViewMap[layer].find(view) == Chris@0: m_layerViewMap[layer].end()) { Chris@0: std::cerr << "WARNING: Document::removeFromLayerViewMap:" Chris@0: << " Layer " << layer << " -> view " << view << " not in" Chris@0: << " layer view map -- internal inconsistency" << std::endl; Chris@0: } Chris@0: Chris@0: m_layerViewMap[layer].erase(view); Chris@0: Chris@0: if (m_layerViewMap[layer].empty()) { Chris@0: m_layerViewMap.erase(layer); Chris@0: emit layerInAView(layer, false); Chris@0: } Chris@0: } Chris@0: Chris@0: QString Chris@0: Document::getUniqueLayerName(QString candidate) Chris@0: { Chris@0: for (int count = 1; ; ++count) { Chris@0: Chris@0: QString adjusted = Chris@0: (count > 1 ? QString("%1 <%2>").arg(candidate).arg(count) : Chris@0: candidate); Chris@0: Chris@0: bool duplicate = false; Chris@0: Chris@0: for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@0: if ((*i)->objectName() == adjusted) { Chris@0: duplicate = true; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if (!duplicate) return adjusted; Chris@0: } Chris@0: } Chris@0: Chris@66: std::vector Chris@66: Document::getTransformInputModels() Chris@66: { Chris@66: std::vector models; Chris@66: Chris@66: if (!m_mainModel) return models; Chris@66: Chris@66: models.push_back(m_mainModel); Chris@66: Chris@66: //!!! This will pick up all models, including those that aren't visible... Chris@66: Chris@66: for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) { Chris@66: Chris@66: Model *model = i->first; Chris@66: if (!model || model == m_mainModel) continue; Chris@66: DenseTimeValueModel *dtvm = dynamic_cast(model); Chris@66: Chris@66: if (dtvm) { Chris@66: models.push_back(dtvm); Chris@66: } Chris@66: } Chris@66: Chris@66: return models; Chris@66: } Chris@66: Chris@0: Document::AddLayerCommand::AddLayerCommand(Document *d, Chris@0: View *view, Chris@0: Layer *layer) : Chris@0: m_d(d), Chris@0: m_view(view), Chris@0: m_layer(layer), Chris@0: m_name(d->tr("Add %1 Layer").arg(layer->objectName())), Chris@0: m_added(false) Chris@0: { Chris@0: } Chris@0: Chris@0: Document::AddLayerCommand::~AddLayerCommand() Chris@0: { Chris@0: // std::cerr << "Document::AddLayerCommand::~AddLayerCommand" << std::endl; Chris@0: if (!m_added) { Chris@0: m_d->deleteLayer(m_layer); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: Document::AddLayerCommand::execute() Chris@0: { Chris@0: for (int i = 0; i < m_view->getLayerCount(); ++i) { Chris@0: if (m_view->getLayer(i) == m_layer) { Chris@0: // already there Chris@0: m_layer->setLayerDormant(m_view, false); Chris@0: m_added = true; Chris@0: return; Chris@0: } Chris@0: } Chris@0: Chris@0: m_view->addLayer(m_layer); Chris@0: m_layer->setLayerDormant(m_view, false); Chris@0: Chris@0: m_d->addToLayerViewMap(m_layer, m_view); Chris@0: m_added = true; Chris@0: } Chris@0: Chris@0: void Chris@0: Document::AddLayerCommand::unexecute() Chris@0: { Chris@0: m_view->removeLayer(m_layer); Chris@0: m_layer->setLayerDormant(m_view, true); Chris@0: Chris@0: m_d->removeFromLayerViewMap(m_layer, m_view); Chris@0: m_added = false; Chris@0: } Chris@0: Chris@0: Document::RemoveLayerCommand::RemoveLayerCommand(Document *d, Chris@0: View *view, Chris@0: Layer *layer) : Chris@0: m_d(d), Chris@0: m_view(view), Chris@0: m_layer(layer), Chris@0: m_name(d->tr("Delete %1 Layer").arg(layer->objectName())), Chris@0: m_added(true) Chris@0: { Chris@0: } Chris@0: Chris@0: Document::RemoveLayerCommand::~RemoveLayerCommand() Chris@0: { Chris@0: // std::cerr << "Document::RemoveLayerCommand::~RemoveLayerCommand" << std::endl; Chris@0: if (!m_added) { Chris@0: m_d->deleteLayer(m_layer); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: Document::RemoveLayerCommand::execute() Chris@0: { Chris@0: bool have = false; Chris@0: for (int i = 0; i < m_view->getLayerCount(); ++i) { Chris@0: if (m_view->getLayer(i) == m_layer) { Chris@0: have = true; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if (!have) { // not there! Chris@0: m_layer->setLayerDormant(m_view, true); Chris@0: m_added = false; Chris@0: return; Chris@0: } Chris@0: Chris@0: m_view->removeLayer(m_layer); Chris@0: m_layer->setLayerDormant(m_view, true); Chris@0: Chris@0: m_d->removeFromLayerViewMap(m_layer, m_view); Chris@0: m_added = false; Chris@0: } Chris@0: Chris@0: void Chris@0: Document::RemoveLayerCommand::unexecute() Chris@0: { Chris@0: m_view->addLayer(m_layer); Chris@0: m_layer->setLayerDormant(m_view, false); Chris@0: Chris@0: m_d->addToLayerViewMap(m_layer, m_view); Chris@0: m_added = true; Chris@0: } Chris@0: Chris@0: void Chris@0: Document::toXml(QTextStream &out, QString indent, QString extraAttributes) const Chris@0: { Chris@0: out << indent + QString("\n") Chris@0: .arg(extraAttributes == "" ? "" : " ").arg(extraAttributes); Chris@0: Chris@0: if (m_mainModel) { Chris@0: m_mainModel->toXml(out, indent + " ", "mainModel=\"true\""); Chris@0: } Chris@0: Chris@84: // Models that are not used in a layer that is in a view should Chris@84: // not be written. Get our list of required models first. Chris@84: Chris@84: std::set used; Chris@84: Chris@84: for (LayerViewMap::const_iterator i = m_layerViewMap.begin(); Chris@84: i != m_layerViewMap.end(); ++i) { Chris@84: Chris@84: if (i->first && !i->second.empty() && i->first->getModel()) { Chris@84: used.insert(i->first->getModel()); Chris@84: } Chris@84: } Chris@84: Chris@0: for (ModelMap::const_iterator i = m_models.begin(); Chris@0: i != m_models.end(); ++i) { Chris@0: Chris@55: const Model *model = i->first; Chris@0: const ModelRecord &rec = i->second; Chris@0: Chris@84: if (used.find(model) == used.end()) continue; Chris@84: Chris@55: // We need an intelligent way to determine which models need Chris@55: // to be streamed (i.e. have been edited, or are small) and Chris@55: // which should not be (i.e. remain as generated by a Chris@55: // transform, and are large). Chris@55: // Chris@55: // At the moment we can get away with deciding not to stream Chris@55: // dense 3d models or writable wave file models, provided they Chris@55: // were generated from a transform, because at the moment there Chris@55: // is no way to edit those model types so it should be safe to Chris@55: // regenerate them. That won't always work in future though. Chris@55: // It would be particularly nice to be able to ask the user, Chris@55: // as well as making an intelligent guess. Chris@55: Chris@55: bool writeModel = true; Chris@55: bool haveDerivation = false; Chris@55: Chris@55: if (rec.source && rec.transform != "") { Chris@55: haveDerivation = true; Chris@55: } Chris@55: Chris@55: if (haveDerivation) { Chris@55: if (dynamic_cast(model)) { Chris@55: writeModel = false; Chris@55: } else if (dynamic_cast(model)) { Chris@55: writeModel = false; Chris@55: } Chris@55: } Chris@55: Chris@55: if (writeModel) { Chris@55: i->first->toXml(out, indent + " "); Chris@55: } Chris@55: Chris@55: if (haveDerivation) { Chris@0: Chris@27: //!!! stream the rest of the execution context in both directions (i.e. not just channel) Chris@27: Chris@0: out << indent; Chris@30: out << QString(" first)) Chris@27: .arg(rec.context.channel) Chris@30: .arg(rec.context.domain) Chris@30: .arg(rec.context.stepSize) Chris@30: .arg(rec.context.blockSize) Chris@30: .arg(int(rec.context.windowType)) Chris@0: .arg(XmlExportable::encodeEntities(rec.transform)); Chris@0: Chris@0: if (rec.configurationXml != "") { Chris@0: out << ">\n " + indent + rec.configurationXml Chris@0: + "\n" + indent + " \n"; Chris@0: } else { Chris@0: out << "/>\n"; Chris@0: } Chris@0: } Chris@0: Chris@0: //!!! We should probably own the PlayParameterRepository Chris@0: PlayParameters *playParameters = Chris@0: PlayParameterRepository::getInstance()->getPlayParameters(i->first); Chris@0: if (playParameters) { Chris@0: playParameters->toXml Chris@0: (out, indent + " ", Chris@0: QString("model=\"%1\"") Chris@0: .arg(XmlExportable::getObjectExportId(i->first))); Chris@0: } Chris@0: } Chris@0: Chris@0: for (LayerSet::const_iterator i = m_layers.begin(); Chris@0: i != m_layers.end(); ++i) { Chris@0: Chris@0: (*i)->toXml(out, indent + " "); Chris@0: } Chris@0: Chris@0: out << indent + "\n"; Chris@0: } Chris@0: Chris@0: QString Chris@0: Document::toXmlString(QString indent, QString extraAttributes) const Chris@0: { Chris@0: QString s; Chris@0: Chris@0: { Chris@0: QTextStream out(&s); Chris@0: toXml(out, indent, extraAttributes); Chris@0: } Chris@0: Chris@0: return s; Chris@0: } Chris@0: