annotate framework/Document.cpp @ 270:776e1aae635a tonioni

fixed flexi note layer and propagated model selection to analyser
author gyorgyf
date Sat, 13 Apr 2013 13:39:17 +0100
parents 40a0fa9af6b4
children 94bf7d7f2d6f
rev   line source
Chris@45 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@45 2
Chris@45 3 /*
Chris@45 4 Sonic Visualiser
Chris@45 5 An audio file viewer and annotation editor.
Chris@45 6 Centre for Digital Music, Queen Mary, University of London.
Chris@45 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@45 8
Chris@45 9 This program is free software; you can redistribute it and/or
Chris@45 10 modify it under the terms of the GNU General Public License as
Chris@45 11 published by the Free Software Foundation; either version 2 of the
Chris@45 12 License, or (at your option) any later version. See the file
Chris@45 13 COPYING included with this distribution for more information.
Chris@45 14 */
Chris@45 15
Chris@45 16 #include "Document.h"
Chris@45 17
Chris@45 18 #include "data/model/WaveFileModel.h"
Chris@45 19 #include "data/model/WritableWaveFileModel.h"
Chris@45 20 #include "data/model/DenseThreeDimensionalModel.h"
Chris@45 21 #include "data/model/DenseTimeValueModel.h"
gyorgyf@270 22 #include "data/model/FlexiNoteModel.h"
gyorgyf@270 23
Chris@45 24 #include "layer/Layer.h"
Chris@105 25 #include "widgets/CommandHistory.h"
Chris@45 26 #include "base/Command.h"
Chris@45 27 #include "view/View.h"
Chris@45 28 #include "base/PlayParameterRepository.h"
Chris@45 29 #include "base/PlayParameters.h"
Chris@106 30 #include "transform/TransformFactory.h"
Chris@106 31 #include "transform/ModelTransformerFactory.h"
gyorgyf@270 32 #include "transform/FeatureExtractionModelTransformer.h"
Chris@45 33 #include <QApplication>
Chris@45 34 #include <QTextStream>
Chris@90 35 #include <QSettings>
Chris@45 36 #include <iostream>
Chris@212 37 #include <typeinfo>
Chris@45 38
Chris@45 39 // For alignment:
Chris@45 40 #include "data/model/AggregateWaveModel.h"
Chris@45 41 #include "data/model/SparseTimeValueModel.h"
Chris@45 42 #include "data/model/AlignmentModel.h"
Chris@45 43
Chris@138 44 //#define DEBUG_DOCUMENT 1
Chris@77 45
Chris@45 46 //!!! still need to handle command history, documentRestored/documentModified
Chris@45 47
Chris@45 48 Document::Document() :
Chris@47 49 m_mainModel(0),
Chris@47 50 m_autoAlignment(false)
Chris@45 51 {
Chris@45 52 connect(this, SIGNAL(modelAboutToBeDeleted(Model *)),
Chris@54 53 ModelTransformerFactory::getInstance(),
Chris@45 54 SLOT(modelAboutToBeDeleted(Model *)));
Chris@45 55 }
Chris@45 56
Chris@45 57 Document::~Document()
Chris@45 58 {
Chris@45 59 //!!! Document should really own the command history. atm we
Chris@45 60 //still refer to it in various places that don't have access to
Chris@45 61 //the document, be nice to fix that
Chris@45 62
Chris@77 63 #ifdef DEBUG_DOCUMENT
Chris@77 64 std::cerr << "\n\nDocument::~Document: about to clear command history" << std::endl;
Chris@77 65 #endif
Chris@45 66 CommandHistory::getInstance()->clear();
Chris@45 67
Chris@77 68 #ifdef DEBUG_DOCUMENT
Chris@233 69 SVDEBUG << "Document::~Document: about to delete layers" << endl;
Chris@77 70 #endif
Chris@45 71 while (!m_layers.empty()) {
Chris@45 72 deleteLayer(*m_layers.begin(), true);
Chris@45 73 }
Chris@45 74
Chris@45 75 if (!m_models.empty()) {
Chris@233 76 SVDEBUG << "Document::~Document: WARNING: "
Chris@45 77 << m_models.size() << " model(s) still remain -- "
Chris@45 78 << "should have been garbage collected when deleting layers"
Chris@229 79 << endl;
Chris@45 80 while (!m_models.empty()) {
Chris@45 81 Model *model = m_models.begin()->first;
Chris@45 82 if (model == m_mainModel) {
Chris@45 83 // just in case!
Chris@233 84 SVDEBUG << "Document::~Document: WARNING: Main model is also"
Chris@229 85 << " in models list!" << endl;
Chris@45 86 } else if (model) {
Chris@79 87 model->aboutToDelete();
Chris@45 88 emit modelAboutToBeDeleted(model);
Chris@45 89 delete model;
Chris@45 90 }
Chris@45 91 m_models.erase(m_models.begin());
Chris@45 92 }
Chris@45 93 }
Chris@45 94
Chris@77 95 #ifdef DEBUG_DOCUMENT
Chris@233 96 SVDEBUG << "Document::~Document: About to get rid of main model"
Chris@229 97 << endl;
Chris@77 98 #endif
Chris@45 99 if (m_mainModel) {
Chris@79 100 m_mainModel->aboutToDelete();
Chris@45 101 emit modelAboutToBeDeleted(m_mainModel);
Chris@45 102 }
Chris@45 103
Chris@45 104 emit mainModelChanged(0);
Chris@45 105 delete m_mainModel;
Chris@45 106
Chris@45 107 }
Chris@45 108
Chris@45 109 Layer *
Chris@45 110 Document::createLayer(LayerFactory::LayerType type)
Chris@45 111 {
Chris@45 112 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
Chris@45 113 if (!newLayer) return 0;
Chris@45 114
Chris@45 115 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
Chris@45 116
Chris@45 117 m_layers.insert(newLayer);
Chris@52 118
Chris@77 119 #ifdef DEBUG_DOCUMENT
Chris@233 120 SVDEBUG << "Document::createLayer: Added layer of type " << type
Chris@229 121 << ", now have " << m_layers.size() << " layers" << endl;
Chris@77 122 #endif
Chris@52 123
gyorgyf@270 124 std::cerr << "Document::createLayer: Added layer of type " << type
gyorgyf@270 125 << ", now have " << m_layers.size() << " layers" << endl;
gyorgyf@270 126
Chris@45 127 emit layerAdded(newLayer);
Chris@45 128
Chris@45 129 return newLayer;
Chris@45 130 }
Chris@45 131
Chris@45 132 Layer *
Chris@45 133 Document::createMainModelLayer(LayerFactory::LayerType type)
Chris@45 134 {
Chris@45 135 Layer *newLayer = createLayer(type);
Chris@45 136 if (!newLayer) return 0;
Chris@45 137 setModel(newLayer, m_mainModel);
Chris@45 138 return newLayer;
Chris@45 139 }
Chris@45 140
Chris@45 141 Layer *
Chris@45 142 Document::createImportedLayer(Model *model)
Chris@45 143 {
Chris@45 144 LayerFactory::LayerTypeSet types =
Chris@45 145 LayerFactory::getInstance()->getValidLayerTypes(model);
Chris@45 146
Chris@45 147 if (types.empty()) {
Chris@45 148 std::cerr << "WARNING: Document::importLayer: no valid display layer for model" << std::endl;
Chris@45 149 return 0;
Chris@45 150 }
Chris@45 151
Chris@45 152 //!!! for now, just use the first suitable layer type
Chris@45 153 LayerFactory::LayerType type = *types.begin();
Chris@45 154
Chris@45 155 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
Chris@45 156 if (!newLayer) return 0;
Chris@45 157
Chris@45 158 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
Chris@45 159
Chris@45 160 addImportedModel(model);
Chris@45 161 setModel(newLayer, model);
Chris@45 162
Chris@45 163 //!!! and all channels
Chris@45 164 setChannel(newLayer, -1);
Chris@45 165
Chris@45 166 m_layers.insert(newLayer);
Chris@52 167
Chris@77 168 #ifdef DEBUG_DOCUMENT
Chris@233 169 SVDEBUG << "Document::createImportedLayer: Added layer of type " << type
Chris@229 170 << ", now have " << m_layers.size() << " layers" << endl;
Chris@77 171 #endif
Chris@52 172
Chris@45 173 emit layerAdded(newLayer);
Chris@45 174 return newLayer;
Chris@45 175 }
Chris@45 176
Chris@45 177 Layer *
Chris@45 178 Document::createEmptyLayer(LayerFactory::LayerType type)
Chris@45 179 {
Chris@61 180 if (!m_mainModel) return 0;
Chris@61 181
Chris@45 182 Model *newModel =
Chris@45 183 LayerFactory::getInstance()->createEmptyModel(type, m_mainModel);
Chris@45 184 if (!newModel) return 0;
Chris@45 185
Chris@45 186 Layer *newLayer = createLayer(type);
Chris@45 187 if (!newLayer) {
Chris@45 188 delete newModel;
Chris@45 189 return 0;
Chris@45 190 }
Chris@45 191
Chris@45 192 addImportedModel(newModel);
Chris@45 193 setModel(newLayer, newModel);
Chris@45 194
Chris@45 195 return newLayer;
Chris@45 196 }
Chris@45 197
Chris@45 198 Layer *
Chris@45 199 Document::createDerivedLayer(LayerFactory::LayerType type,
Chris@54 200 TransformId transform)
Chris@45 201 {
Chris@45 202 Layer *newLayer = createLayer(type);
Chris@45 203 if (!newLayer) return 0;
Chris@45 204
Chris@45 205 newLayer->setObjectName(getUniqueLayerName
Chris@54 206 (TransformFactory::getInstance()->
Chris@54 207 getTransformFriendlyName(transform)));
Chris@45 208
Chris@45 209 return newLayer;
Chris@45 210 }
Chris@45 211 Layer *
Chris@72 212 Document::createDerivedLayer(const Transform &transform,
Chris@72 213 const ModelTransformer::Input &input)
Chris@45 214 {
Chris@78 215 QString message;
Chris@78 216 Model *newModel = addDerivedModel(transform, input, message);
Chris@45 217 if (!newModel) {
Chris@78 218 emit modelGenerationFailed(transform.getIdentifier(), message);
Chris@45 219 return 0;
Chris@78 220 } else if (message != "") {
Chris@78 221 emit modelGenerationWarning(transform.getIdentifier(), message);
Chris@45 222 }
Chris@45 223
Chris@45 224 LayerFactory::LayerTypeSet types =
Chris@45 225 LayerFactory::getInstance()->getValidLayerTypes(newModel);
Chris@45 226
Chris@45 227 if (types.empty()) {
Chris@228 228 std::cerr << "WARNING: Document::createLayerForTransformer: no valid display layer for output of transform " << transform.getIdentifier() << std::endl;
Chris@136 229 newModel->aboutToDelete();
Chris@136 230 emit modelAboutToBeDeleted(newModel);
Chris@136 231 m_models.erase(newModel);
Chris@45 232 delete newModel;
Chris@45 233 return 0;
Chris@45 234 }
Chris@45 235
Chris@45 236 //!!! for now, just use the first suitable layer type
Chris@45 237
Chris@45 238 Layer *newLayer = createLayer(*types.begin());
Chris@45 239 setModel(newLayer, newModel);
Chris@45 240
Chris@45 241 //!!! We need to clone the model when adding the layer, so that it
Chris@45 242 //can be edited without affecting other layers that are based on
Chris@45 243 //the same model. Unfortunately we can't just clone it now,
Chris@45 244 //because it probably hasn't been completed yet -- the transform
Chris@45 245 //runs in the background. Maybe the transform has to handle
Chris@45 246 //cloning and cacheing models itself.
Chris@45 247 //
Chris@45 248 // Once we do clone models here, of course, we'll have to avoid
Chris@45 249 // leaking them too.
Chris@45 250 //
Chris@45 251 // We want the user to be able to add a model to a second layer
Chris@45 252 // _while it's still being calculated in the first_ and have it
Chris@45 253 // work quickly. That means we need to put the same physical
Chris@45 254 // model pointer in both layers, so they can't actually be cloned.
Chris@45 255
Chris@45 256 if (newLayer) {
Chris@45 257 newLayer->setObjectName(getUniqueLayerName
Chris@54 258 (TransformFactory::getInstance()->
Chris@72 259 getTransformFriendlyName
Chris@72 260 (transform.getIdentifier())));
Chris@45 261 }
Chris@45 262
Chris@45 263 emit layerAdded(newLayer);
Chris@45 264 return newLayer;
Chris@45 265 }
Chris@45 266
matthiasm@269 267 Layer *
matthiasm@269 268 Document::createDerivedLayer(const Transform &transform,
matthiasm@269 269 const ModelTransformer::Input &input,
gyorgyf@270 270 const LayerFactory::LayerType type,
gyorgyf@270 271 const FeatureExtractionModelTransformer::PreferredOutputModel outputmodel)
matthiasm@269 272 {
matthiasm@269 273 // !!! THIS IS TOTALLY REDUNDANT CODE, EXCEPT FOR THE type SETTING
matthiasm@269 274
gyorgyf@270 275 QString message;
gyorgyf@270 276 Model *newModel = addDerivedModel(transform, input, message, outputmodel);
gyorgyf@270 277 if (!newModel) {
gyorgyf@270 278 emit modelGenerationFailed(transform.getIdentifier(), message);
gyorgyf@270 279 return 0;
gyorgyf@270 280 } else if (message != "") {
gyorgyf@270 281 emit modelGenerationWarning(transform.getIdentifier(), message);
gyorgyf@270 282 }
gyorgyf@270 283
gyorgyf@270 284 LayerFactory::LayerTypeSet types =
matthiasm@269 285 LayerFactory::getInstance()->getValidLayerTypes(newModel);
gyorgyf@270 286
gyorgyf@270 287 if (types.empty()) {
matthiasm@269 288 std::cerr << "WARNING: Document::createLayerForTransformer: no valid display layer for output of transform " << transform.getIdentifier() << std::endl;
gyorgyf@270 289 newModel->aboutToDelete();
gyorgyf@270 290 emit modelAboutToBeDeleted(newModel);
gyorgyf@270 291 m_models.erase(newModel);
matthiasm@269 292 delete newModel;
matthiasm@269 293 return 0;
gyorgyf@270 294 }
matthiasm@269 295
gyorgyf@270 296 //!!! creating layer with the specified type
matthiasm@269 297
matthiasm@269 298 Layer *newLayer = createLayer(type);
gyorgyf@270 299 std::cerr << " NOTE: Created layer " << newLayer << " calling Document::setModel() " << std::endl;
matthiasm@269 300 setModel(newLayer, newModel);
matthiasm@269 301
matthiasm@269 302 //!!! We need to clone the model when adding the layer, so that it
matthiasm@269 303 //can be edited without affecting other layers that are based on
matthiasm@269 304 //the same model. Unfortunately we can't just clone it now,
matthiasm@269 305 //because it probably hasn't been completed yet -- the transform
matthiasm@269 306 //runs in the background. Maybe the transform has to handle
matthiasm@269 307 //cloning and cacheing models itself.
matthiasm@269 308 //
matthiasm@269 309 // Once we do clone models here, of course, we'll have to avoid
matthiasm@269 310 // leaking them too.
matthiasm@269 311 //
matthiasm@269 312 // We want the user to be able to add a model to a second layer
matthiasm@269 313 // _while it's still being calculated in the first_ and have it
matthiasm@269 314 // work quickly. That means we need to put the same physical
matthiasm@269 315 // model pointer in both layers, so they can't actually be cloned.
matthiasm@269 316
matthiasm@269 317 if (newLayer) {
matthiasm@269 318 newLayer->setObjectName(getUniqueLayerName
matthiasm@269 319 (TransformFactory::getInstance()->
matthiasm@269 320 getTransformFriendlyName
matthiasm@269 321 (transform.getIdentifier())));
matthiasm@269 322 }
matthiasm@269 323
matthiasm@269 324 emit layerAdded(newLayer);
matthiasm@269 325 return newLayer;
matthiasm@269 326 }
matthiasm@269 327
matthiasm@269 328
Chris@45 329 void
Chris@45 330 Document::setMainModel(WaveFileModel *model)
Chris@45 331 {
Chris@45 332 Model *oldMainModel = m_mainModel;
Chris@45 333 m_mainModel = model;
Chris@160 334
Chris@45 335 emit modelAdded(m_mainModel);
Chris@160 336 if (model) {
Chris@160 337 emit activity(tr("Set main model to %1").arg(model->objectName()));
Chris@160 338 } else {
Chris@160 339 emit activity(tr("Clear main model"));
Chris@160 340 }
Chris@45 341
Chris@45 342 std::vector<Layer *> obsoleteLayers;
Chris@53 343 std::set<QString> failedTransformers;
Chris@45 344
Chris@45 345 // We need to ensure that no layer is left using oldMainModel or
Chris@45 346 // any of the old derived models as its model. Either replace the
Chris@45 347 // model, or delete the layer for each layer that is currently
Chris@45 348 // using one of these. Carry out this replacement before we
Chris@45 349 // delete any of the models.
Chris@45 350
Chris@77 351 #ifdef DEBUG_DOCUMENT
Chris@248 352 std::cerr << "Document::setMainModel: Have "
Chris@248 353 << m_layers.size() << " layers" << std::endl;
Chris@137 354 std::cerr << "Models now: ";
Chris@137 355 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@137 356 std::cerr << i->first << " ";
Chris@137 357 }
Chris@137 358 std::cerr << std::endl;
Chris@248 359 std::cerr << "Old main model: " << oldMainModel << std::endl;
Chris@77 360 #endif
Chris@52 361
Chris@45 362 for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@45 363
Chris@45 364 Layer *layer = *i;
Chris@45 365 Model *model = layer->getModel();
Chris@45 366
Chris@77 367 #ifdef DEBUG_DOCUMENT
Chris@248 368 std::cerr << "Document::setMainModel: inspecting model "
Chris@229 369 << (model ? model->objectName(): "(null)") << " in layer "
Chris@248 370 << layer->objectName() << std::endl;
Chris@77 371 #endif
Chris@45 372
Chris@70 373 if (model == oldMainModel) {
Chris@77 374 #ifdef DEBUG_DOCUMENT
Chris@77 375 std::cerr << "... it uses the old main model, replacing" << std::endl;
Chris@77 376 #endif
Chris@45 377 LayerFactory::getInstance()->setModel(layer, m_mainModel);
Chris@45 378 continue;
Chris@45 379 }
Chris@45 380
Chris@137 381 if (!model) {
Chris@137 382 std::cerr << "WARNING: Document::setMainModel: Null model in layer "
Chris@137 383 << layer << std::endl;
Chris@137 384 // get rid of this hideous degenerate
Chris@137 385 obsoleteLayers.push_back(layer);
Chris@137 386 continue;
Chris@137 387 }
Chris@137 388
Chris@137 389 if (m_models.find(model) == m_models.end()) {
Chris@45 390 std::cerr << "WARNING: Document::setMainModel: Unknown model "
Chris@45 391 << model << " in layer " << layer << std::endl;
Chris@137 392 // and this one
Chris@45 393 obsoleteLayers.push_back(layer);
Chris@45 394 continue;
Chris@45 395 }
Chris@45 396
Chris@70 397 if (m_models[model].source &&
Chris@70 398 (m_models[model].source == oldMainModel)) {
Chris@45 399
Chris@77 400 #ifdef DEBUG_DOCUMENT
Chris@77 401 std::cerr << "... it uses a model derived from the old main model, regenerating" << std::endl;
Chris@77 402 #endif
Chris@45 403
Chris@45 404 // This model was derived from the previous main
Chris@45 405 // model: regenerate it.
Chris@45 406
Chris@72 407 const Transform &transform = m_models[model].transform;
Chris@72 408 QString transformId = transform.getIdentifier();
Chris@45 409
Chris@72 410 //!!! We have a problem here if the number of channels in
Chris@72 411 //the main model has changed.
Chris@72 412
Chris@78 413 QString message;
Chris@45 414 Model *replacementModel =
Chris@45 415 addDerivedModel(transform,
Chris@72 416 ModelTransformer::Input
Chris@78 417 (m_mainModel, m_models[model].channel),
Chris@78 418 message);
Chris@45 419
Chris@45 420 if (!replacementModel) {
Chris@45 421 std::cerr << "WARNING: Document::setMainModel: Failed to regenerate model for transform \""
Chris@228 422 << transformId << "\"" << " in layer " << layer << std::endl;
Chris@72 423 if (failedTransformers.find(transformId)
Chris@72 424 == failedTransformers.end()) {
Chris@45 425 emit modelRegenerationFailed(layer->objectName(),
Chris@78 426 transformId,
Chris@78 427 message);
Chris@72 428 failedTransformers.insert(transformId);
Chris@45 429 }
Chris@45 430 obsoleteLayers.push_back(layer);
Chris@45 431 } else {
Chris@78 432 if (message != "") {
Chris@78 433 emit modelRegenerationWarning(layer->objectName(),
Chris@78 434 transformId,
Chris@78 435 message);
Chris@78 436 }
Chris@77 437 #ifdef DEBUG_DOCUMENT
Chris@248 438 std::cerr << "Replacing model " << model << " (type "
Chris@77 439 << typeid(*model).name() << ") with model "
Chris@77 440 << replacementModel << " (type "
Chris@77 441 << typeid(*replacementModel).name() << ") in layer "
Chris@228 442 << layer << " (name " << layer->objectName() << ")"
Chris@248 443 << std::endl;
Chris@77 444 #endif
Chris@45 445 RangeSummarisableTimeValueModel *rm =
Chris@45 446 dynamic_cast<RangeSummarisableTimeValueModel *>(replacementModel);
Chris@77 447 #ifdef DEBUG_DOCUMENT
Chris@45 448 if (rm) {
Chris@45 449 std::cerr << "new model has " << rm->getChannelCount() << " channels " << std::endl;
Chris@45 450 } else {
Chris@137 451 std::cerr << "new model " << replacementModel << " is not a RangeSummarisableTimeValueModel!" << std::endl;
Chris@45 452 }
Chris@77 453 #endif
Chris@45 454 setModel(layer, replacementModel);
Chris@45 455 }
Chris@45 456 }
Chris@45 457 }
Chris@45 458
Chris@45 459 for (size_t k = 0; k < obsoleteLayers.size(); ++k) {
Chris@45 460 deleteLayer(obsoleteLayers[k], true);
Chris@45 461 }
Chris@45 462
Chris@48 463 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@86 464
Chris@137 465 Model *m = i->first;
Chris@137 466
Chris@137 467 #ifdef DEBUG_DOCUMENT
Chris@233 468 SVDEBUG << "considering alignment for model " << m << " (name \""
Chris@229 469 << m->objectName() << "\")" << endl;
Chris@137 470 #endif
Chris@137 471
Chris@86 472 if (m_autoAlignment) {
Chris@86 473
Chris@137 474 alignModel(m);
Chris@86 475
Chris@86 476 } else if (oldMainModel &&
Chris@137 477 (m->getAlignmentReference() == oldMainModel)) {
Chris@86 478
Chris@137 479 alignModel(m);
Chris@48 480 }
Chris@48 481 }
Chris@48 482
Chris@77 483 if (oldMainModel) {
Chris@79 484 oldMainModel->aboutToDelete();
Chris@77 485 emit modelAboutToBeDeleted(oldMainModel);
Chris@77 486 }
Chris@77 487
Chris@86 488 if (m_autoAlignment) {
Chris@86 489 alignModel(m_mainModel);
Chris@86 490 }
Chris@86 491
Chris@45 492 emit mainModelChanged(m_mainModel);
Chris@45 493
Chris@45 494 delete oldMainModel;
Chris@45 495 }
Chris@45 496
Chris@45 497 void
Chris@72 498 Document::addDerivedModel(const Transform &transform,
Chris@72 499 const ModelTransformer::Input &input,
Chris@72 500 Model *outputModelToAdd)
Chris@45 501 {
Chris@45 502 if (m_models.find(outputModelToAdd) != m_models.end()) {
Chris@45 503 std::cerr << "WARNING: Document::addDerivedModel: Model already added"
Chris@45 504 << std::endl;
Chris@45 505 return;
Chris@45 506 }
Chris@45 507
Chris@77 508 #ifdef DEBUG_DOCUMENT
Chris@248 509 if (input.getModel()) {
Chris@248 510 std::cerr << "Document::addDerivedModel: source is " << input.getModel() << " \"" << input.getModel()->objectName() << "\"" << std::endl;
Chris@248 511 } else {
Chris@248 512 std::cerr << "Document::addDerivedModel: source is " << input.getModel() << std::endl;
Chris@248 513 }
Chris@77 514 #endif
Chris@45 515
Chris@45 516 ModelRecord rec;
Chris@72 517 rec.source = input.getModel();
Chris@72 518 rec.channel = input.getChannel();
Chris@45 519 rec.transform = transform;
Chris@45 520 rec.refcount = 0;
Chris@45 521
Chris@72 522 outputModelToAdd->setSourceModel(input.getModel());
Chris@45 523
Chris@45 524 m_models[outputModelToAdd] = rec;
Chris@45 525
Chris@137 526 #ifdef DEBUG_DOCUMENT
Chris@233 527 SVDEBUG << "Document::addDerivedModel: Added model " << outputModelToAdd << endl;
Chris@137 528 std::cerr << "Models now: ";
Chris@137 529 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@137 530 std::cerr << i->first << " ";
Chris@137 531 }
Chris@137 532 std::cerr << std::endl;
Chris@137 533 #endif
Chris@137 534
Chris@45 535 emit modelAdded(outputModelToAdd);
Chris@45 536 }
Chris@45 537
Chris@45 538
Chris@45 539 void
Chris@45 540 Document::addImportedModel(Model *model)
Chris@45 541 {
Chris@45 542 if (m_models.find(model) != m_models.end()) {
Chris@45 543 std::cerr << "WARNING: Document::addImportedModel: Model already added"
Chris@45 544 << std::endl;
Chris@45 545 return;
Chris@45 546 }
Chris@45 547
Chris@45 548 ModelRecord rec;
Chris@45 549 rec.source = 0;
Chris@45 550 rec.refcount = 0;
Chris@45 551
Chris@45 552 m_models[model] = rec;
Chris@45 553
Chris@137 554 #ifdef DEBUG_DOCUMENT
Chris@233 555 SVDEBUG << "Document::addImportedModel: Added model " << model << endl;
Chris@137 556 std::cerr << "Models now: ";
Chris@137 557 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@137 558 std::cerr << i->first << " ";
Chris@137 559 }
Chris@137 560 std::cerr << std::endl;
Chris@137 561 #endif
Chris@137 562
Chris@47 563 if (m_autoAlignment) alignModel(model);
Chris@47 564
Chris@45 565 emit modelAdded(model);
Chris@45 566 }
Chris@45 567
Chris@45 568 Model *
Chris@72 569 Document::addDerivedModel(const Transform &transform,
Chris@78 570 const ModelTransformer::Input &input,
gyorgyf@270 571 QString &message,
gyorgyf@270 572 const FeatureExtractionModelTransformer::PreferredOutputModel outputmodel)
Chris@45 573 {
Chris@45 574 Model *model = 0;
gyorgyf@270 575 // model = (Model) new FlexiNoteModel();
gyorgyf@270 576 // return model;
Chris@45 577
Chris@45 578 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@45 579 if (i->second.transform == transform &&
Chris@72 580 i->second.source == input.getModel() &&
Chris@72 581 i->second.channel == input.getChannel()) {
gyorgyf@270 582 std::cerr << "derived model taken from map " << std::endl;
Chris@45 583 return i->first;
Chris@45 584 }
Chris@45 585 }
Chris@45 586
gyorgyf@270 587 // GF: TODO: propagate preferredOutputModelSelection (done)
Chris@78 588 model = ModelTransformerFactory::getInstance()->transform
gyorgyf@270 589 (transform, input, message, outputmodel); //e.g. FeatureExtractionModelTransformer::FlexiNoteOutputModel
Chris@45 590
Chris@83 591 // The transform we actually used was presumably identical to the
Chris@83 592 // one asked for, except that the version of the plugin may
Chris@83 593 // differ. It's possible that the returned message contains a
Chris@83 594 // warning about this; that doesn't concern us here, but we do
Chris@83 595 // need to ensure that the transform we remember is correct for
Chris@83 596 // what was actually applied, with the current plugin version.
Chris@83 597
Chris@83 598 Transform applied = transform;
Chris@83 599 applied.setPluginVersion
Chris@83 600 (TransformFactory::getInstance()->
Chris@83 601 getDefaultTransformFor(transform.getIdentifier(),
Chris@83 602 lrintf(transform.getSampleRate()))
Chris@83 603 .getPluginVersion());
Chris@83 604
Chris@45 605 if (!model) {
Chris@228 606 std::cerr << "WARNING: Document::addDerivedModel: no output model for transform " << transform.getIdentifier() << std::endl;
Chris@45 607 } else {
Chris@83 608 addDerivedModel(applied, input, model);
Chris@45 609 }
gyorgyf@270 610 std::cerr << "derived model from ModelTransformerFactory::getInstance()->transform " << std::endl;
gyorgyf@270 611 std::cerr << "derived model name: " << model->getTypeName() << std::endl;
gyorgyf@270 612
Chris@45 613 return model;
Chris@45 614 }
Chris@45 615
Chris@45 616 void
Chris@45 617 Document::releaseModel(Model *model) // Will _not_ release main model!
Chris@45 618 {
Chris@45 619 if (model == 0) {
Chris@45 620 return;
Chris@45 621 }
Chris@45 622
Chris@45 623 if (model == m_mainModel) {
Chris@45 624 return;
Chris@45 625 }
Chris@45 626
Chris@45 627 bool toDelete = false;
Chris@45 628
Chris@45 629 if (m_models.find(model) != m_models.end()) {
Chris@45 630
Chris@45 631 if (m_models[model].refcount == 0) {
Chris@45 632 std::cerr << "WARNING: Document::releaseModel: model " << model
Chris@45 633 << " reference count is zero already!" << std::endl;
Chris@45 634 } else {
Chris@45 635 if (--m_models[model].refcount == 0) {
Chris@45 636 toDelete = true;
Chris@45 637 }
Chris@45 638 }
Chris@45 639 } else {
Chris@45 640 std::cerr << "WARNING: Document::releaseModel: Unfound model "
Chris@45 641 << model << std::endl;
Chris@45 642 toDelete = true;
Chris@45 643 }
Chris@45 644
Chris@45 645 if (toDelete) {
Chris@45 646
Chris@45 647 int sourceCount = 0;
Chris@45 648
Chris@45 649 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@45 650 if (i->second.source == model) {
Chris@45 651 ++sourceCount;
Chris@45 652 i->second.source = 0;
Chris@45 653 }
Chris@45 654 }
Chris@45 655
Chris@45 656 if (sourceCount > 0) {
Chris@233 657 SVDEBUG << "Document::releaseModel: Deleting model "
Chris@45 658 << model << " even though it is source for "
Chris@45 659 << sourceCount << " other derived model(s) -- resetting "
Chris@229 660 << "their source fields appropriately" << endl;
Chris@45 661 }
Chris@45 662
Chris@79 663 model->aboutToDelete();
Chris@45 664 emit modelAboutToBeDeleted(model);
Chris@45 665 m_models.erase(model);
Chris@137 666
Chris@137 667 #ifdef DEBUG_DOCUMENT
Chris@233 668 SVDEBUG << "Document::releaseModel: Deleted model " << model << endl;
Chris@137 669 std::cerr << "Models now: ";
Chris@137 670 for (ModelMap::const_iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@137 671 std::cerr << i->first << " ";
Chris@137 672 }
Chris@137 673 std::cerr << std::endl;
Chris@137 674 #endif
Chris@137 675
Chris@45 676 delete model;
Chris@45 677 }
Chris@45 678 }
Chris@45 679
Chris@45 680 void
Chris@45 681 Document::deleteLayer(Layer *layer, bool force)
Chris@45 682 {
Chris@45 683 if (m_layerViewMap.find(layer) != m_layerViewMap.end() &&
Chris@45 684 m_layerViewMap[layer].size() > 0) {
Chris@45 685
Chris@45 686 std::cerr << "WARNING: Document::deleteLayer: Layer "
Chris@228 687 << layer << " [" << layer->objectName() << "]"
Chris@45 688 << " is still used in " << m_layerViewMap[layer].size()
Chris@45 689 << " views!" << std::endl;
Chris@45 690
Chris@45 691 if (force) {
Chris@45 692
Chris@77 693 #ifdef DEBUG_DOCUMENT
Chris@45 694 std::cerr << "(force flag set -- deleting from all views)" << std::endl;
Chris@77 695 #endif
Chris@45 696
Chris@45 697 for (std::set<View *>::iterator j = m_layerViewMap[layer].begin();
Chris@45 698 j != m_layerViewMap[layer].end(); ++j) {
Chris@45 699 // don't use removeLayerFromView, as it issues a command
Chris@45 700 layer->setLayerDormant(*j, true);
Chris@45 701 (*j)->removeLayer(layer);
Chris@45 702 }
Chris@45 703
Chris@45 704 m_layerViewMap.erase(layer);
Chris@45 705
Chris@45 706 } else {
Chris@45 707 return;
Chris@45 708 }
Chris@45 709 }
Chris@45 710
Chris@45 711 if (m_layers.find(layer) == m_layers.end()) {
Chris@233 712 SVDEBUG << "Document::deleteLayer: Layer "
Chris@212 713 << layer << " (" << typeid(layer).name() <<
Chris@212 714 ") does not exist, or has already been deleted "
Chris@229 715 << "(this may not be as serious as it sounds)" << endl;
Chris@45 716 return;
Chris@45 717 }
Chris@45 718
Chris@45 719 m_layers.erase(layer);
Chris@45 720
Chris@132 721 #ifdef DEBUG_DOCUMENT
Chris@233 722 SVDEBUG << "Document::deleteLayer: Removing, now have "
Chris@229 723 << m_layers.size() << " layers" << endl;
Chris@132 724 #endif
Chris@52 725
Chris@45 726 releaseModel(layer->getModel());
Chris@45 727 emit layerRemoved(layer);
Chris@45 728 emit layerAboutToBeDeleted(layer);
Chris@45 729 delete layer;
Chris@45 730 }
Chris@45 731
Chris@45 732 void
Chris@45 733 Document::setModel(Layer *layer, Model *model)
Chris@45 734 {
gyorgyf@270 735 std::cerr << "Document::setModel: setting model " << std::endl;
Chris@45 736 if (model &&
Chris@45 737 model != m_mainModel &&
Chris@45 738 m_models.find(model) == m_models.end()) {
Chris@45 739 std::cerr << "ERROR: Document::setModel: Layer " << layer
Chris@45 740 << " (\"" << layer->objectName().toStdString()
Chris@45 741 << "\") wants to use unregistered model " << model
Chris@45 742 << ": register the layer's model before setting it!"
Chris@45 743 << std::endl;
Chris@45 744 return;
Chris@45 745 }
Chris@45 746
Chris@45 747 Model *previousModel = layer->getModel();
Chris@45 748
Chris@45 749 if (previousModel == model) {
Chris@233 750 SVDEBUG << "NOTE: Document::setModel: Layer " << layer << " (\""
Chris@229 751 << layer->objectName() << "\") is already set to model "
Chris@45 752 << model << " (\""
Chris@229 753 << (model ? model->objectName(): "(null)")
Chris@229 754 << "\")" << endl;
Chris@45 755 return;
Chris@45 756 }
Chris@45 757
Chris@45 758 if (model && model != m_mainModel) {
Chris@45 759 m_models[model].refcount ++;
Chris@45 760 }
Chris@45 761
Chris@45 762 if (model && previousModel) {
Chris@45 763 PlayParameterRepository::getInstance()->copyParameters
Chris@45 764 (previousModel, model);
Chris@45 765 }
Chris@45 766
gyorgyf@270 767 std::cerr << "Document::setModel: calling layer->setModel() " << std::endl;
gyorgyf@270 768 std::cerr << "Document::setModel: getInstance: " << LayerFactory::getInstance() << std::endl;
gyorgyf@270 769 std::cerr << "model: " << model << std::endl;
gyorgyf@270 770 std::cerr << "layer: " << layer << std::endl;
Chris@45 771 LayerFactory::getInstance()->setModel(layer, model);
gyorgyf@270 772 std::cerr << "layer type: " << LayerFactory::getInstance()->getLayerTypeName(LayerFactory::getInstance()->getLayerType(layer)) << std::endl;
gyorgyf@270 773 std::cerr << "Document::setModel: done. " << std::endl;
Chris@45 774
Chris@45 775 if (previousModel) {
Chris@45 776 releaseModel(previousModel);
Chris@45 777 }
Chris@45 778 }
Chris@45 779
Chris@45 780 void
Chris@45 781 Document::setChannel(Layer *layer, int channel)
Chris@45 782 {
Chris@45 783 LayerFactory::getInstance()->setChannel(layer, channel);
Chris@45 784 }
Chris@45 785
Chris@45 786 void
Chris@45 787 Document::addLayerToView(View *view, Layer *layer)
Chris@45 788 {
Chris@45 789 Model *model = layer->getModel();
Chris@45 790 if (!model) {
Chris@77 791 #ifdef DEBUG_DOCUMENT
Chris@233 792 SVDEBUG << "Document::addLayerToView: Layer (\""
Chris@229 793 << layer->objectName() << "\") with no model being added to view: "
Chris@229 794 << "normally you want to set the model first" << endl;
Chris@77 795 #endif
Chris@45 796 } else {
Chris@45 797 if (model != m_mainModel &&
Chris@45 798 m_models.find(model) == m_models.end()) {
Chris@45 799 std::cerr << "ERROR: Document::addLayerToView: Layer " << layer
Chris@45 800 << " has unregistered model " << model
Chris@45 801 << " -- register the layer's model before adding the layer!" << std::endl;
Chris@45 802 return;
Chris@45 803 }
Chris@45 804 }
Chris@45 805
Chris@45 806 CommandHistory::getInstance()->addCommand
Chris@45 807 (new Document::AddLayerCommand(this, view, layer));
Chris@45 808 }
Chris@45 809
Chris@45 810 void
Chris@45 811 Document::removeLayerFromView(View *view, Layer *layer)
Chris@45 812 {
Chris@45 813 CommandHistory::getInstance()->addCommand
Chris@45 814 (new Document::RemoveLayerCommand(this, view, layer));
Chris@45 815 }
Chris@45 816
Chris@45 817 void
Chris@45 818 Document::addToLayerViewMap(Layer *layer, View *view)
Chris@45 819 {
Chris@45 820 bool firstView = (m_layerViewMap.find(layer) == m_layerViewMap.end() ||
Chris@45 821 m_layerViewMap[layer].empty());
Chris@45 822
Chris@45 823 if (m_layerViewMap[layer].find(view) !=
Chris@45 824 m_layerViewMap[layer].end()) {
Chris@45 825 std::cerr << "WARNING: Document::addToLayerViewMap:"
Chris@45 826 << " Layer " << layer << " -> view " << view << " already in"
Chris@45 827 << " layer view map -- internal inconsistency" << std::endl;
Chris@45 828 }
Chris@45 829
Chris@45 830 m_layerViewMap[layer].insert(view);
Chris@45 831
Chris@45 832 if (firstView) emit layerInAView(layer, true);
Chris@45 833 }
Chris@45 834
Chris@45 835 void
Chris@45 836 Document::removeFromLayerViewMap(Layer *layer, View *view)
Chris@45 837 {
Chris@45 838 if (m_layerViewMap[layer].find(view) ==
Chris@45 839 m_layerViewMap[layer].end()) {
Chris@45 840 std::cerr << "WARNING: Document::removeFromLayerViewMap:"
Chris@45 841 << " Layer " << layer << " -> view " << view << " not in"
Chris@45 842 << " layer view map -- internal inconsistency" << std::endl;
Chris@45 843 }
Chris@45 844
Chris@45 845 m_layerViewMap[layer].erase(view);
Chris@45 846
Chris@45 847 if (m_layerViewMap[layer].empty()) {
Chris@45 848 m_layerViewMap.erase(layer);
Chris@45 849 emit layerInAView(layer, false);
Chris@45 850 }
Chris@45 851 }
Chris@45 852
Chris@45 853 QString
Chris@45 854 Document::getUniqueLayerName(QString candidate)
Chris@45 855 {
Chris@45 856 for (int count = 1; ; ++count) {
Chris@45 857
Chris@45 858 QString adjusted =
Chris@45 859 (count > 1 ? QString("%1 <%2>").arg(candidate).arg(count) :
Chris@45 860 candidate);
Chris@45 861
Chris@45 862 bool duplicate = false;
Chris@45 863
Chris@45 864 for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@45 865 if ((*i)->objectName() == adjusted) {
Chris@45 866 duplicate = true;
Chris@45 867 break;
Chris@45 868 }
Chris@45 869 }
Chris@45 870
Chris@45 871 if (!duplicate) return adjusted;
Chris@45 872 }
Chris@45 873 }
Chris@45 874
Chris@45 875 std::vector<Model *>
Chris@72 876 Document::getTransformInputModels()
Chris@45 877 {
Chris@45 878 std::vector<Model *> models;
Chris@45 879
Chris@45 880 if (!m_mainModel) return models;
Chris@45 881
Chris@45 882 models.push_back(m_mainModel);
Chris@45 883
Chris@45 884 //!!! This will pick up all models, including those that aren't visible...
Chris@45 885
Chris@45 886 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@45 887
Chris@45 888 Model *model = i->first;
Chris@45 889 if (!model || model == m_mainModel) continue;
Chris@45 890 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
Chris@45 891
Chris@45 892 if (dtvm) {
Chris@45 893 models.push_back(dtvm);
Chris@45 894 }
Chris@45 895 }
Chris@45 896
Chris@45 897 return models;
Chris@45 898 }
Chris@45 899
Chris@50 900 bool
Chris@77 901 Document::isKnownModel(const Model *model) const
Chris@77 902 {
Chris@77 903 if (model == m_mainModel) return true;
Chris@77 904 return (m_models.find(const_cast<Model *>(model)) != m_models.end());
Chris@77 905 }
Chris@77 906
Chris@90 907 TransformId
Chris@90 908 Document::getAlignmentTransformName()
Chris@90 909 {
Chris@90 910 QSettings settings;
Chris@90 911 settings.beginGroup("Alignment");
Chris@90 912 TransformId id =
Chris@90 913 settings.value("transform-id",
Chris@90 914 "vamp:match-vamp-plugin:match:path").toString();
Chris@90 915 settings.endGroup();
Chris@90 916 return id;
Chris@90 917 }
Chris@90 918
Chris@77 919 bool
Chris@51 920 Document::canAlign()
Chris@50 921 {
Chris@90 922 TransformId id = getAlignmentTransformName();
Chris@54 923 TransformFactory *factory = TransformFactory::getInstance();
Chris@54 924 return factory->haveTransform(id);
Chris@50 925 }
Chris@50 926
Chris@45 927 void
Chris@45 928 Document::alignModel(Model *model)
Chris@45 929 {
Chris@86 930 if (!m_mainModel) return;
Chris@45 931
Chris@45 932 RangeSummarisableTimeValueModel *rm =
Chris@45 933 dynamic_cast<RangeSummarisableTimeValueModel *>(model);
Chris@45 934 if (!rm) return;
Chris@48 935
Chris@86 936 if (rm->getAlignmentReference() == m_mainModel) {
Chris@233 937 SVDEBUG << "Document::alignModel: model " << rm << " is already aligned to main model " << m_mainModel << endl;
Chris@86 938 return;
Chris@86 939 }
Chris@45 940
Chris@86 941 if (model == m_mainModel) {
Chris@86 942 // The reference has an empty alignment to itself. This makes
Chris@86 943 // it possible to distinguish between the reference and any
Chris@86 944 // unaligned model just by looking at the model itself,
Chris@86 945 // without also knowing what the main model is
Chris@233 946 SVDEBUG << "Document::alignModel(" << model << "): is main model, setting appropriately" << endl;
Chris@86 947 rm->setAlignment(new AlignmentModel(model, model, 0, 0));
Chris@86 948 return;
Chris@86 949 }
Chris@86 950
Chris@45 951 // This involves creating three new models:
Chris@45 952
Chris@45 953 // 1. an AggregateWaveModel to provide the mixdowns of the main
Chris@45 954 // model and the new model in its two channels, as input to the
Chris@45 955 // MATCH plugin
Chris@45 956
Chris@45 957 // 2. a SparseTimeValueModel, which is the model automatically
Chris@53 958 // created by FeatureExtractionPluginTransformer when running the
Chris@45 959 // MATCH plugin (thus containing the alignment path)
Chris@45 960
Chris@45 961 // 3. an AlignmentModel, which stores the path model and carries
Chris@45 962 // out alignment lookups on it.
Chris@45 963
Chris@45 964 // The first two of these are provided as arguments to the
Chris@45 965 // constructor for the third, which takes responsibility for
Chris@45 966 // deleting them. The AlignmentModel, meanwhile, is passed to the
Chris@45 967 // new model we are aligning, which also takes responsibility for
Chris@45 968 // it. We should not have to delete any of these new models here.
Chris@45 969
Chris@45 970 AggregateWaveModel::ChannelSpecList components;
Chris@45 971
Chris@45 972 components.push_back(AggregateWaveModel::ModelChannelSpec
Chris@45 973 (m_mainModel, -1));
Chris@45 974
Chris@45 975 components.push_back(AggregateWaveModel::ModelChannelSpec
Chris@45 976 (rm, -1));
Chris@45 977
Chris@112 978 Model *aggregateModel = new AggregateWaveModel(components);
Chris@112 979 ModelTransformer::Input aggregate(aggregateModel);
Chris@45 980
Chris@72 981 TransformId id = "vamp:match-vamp-plugin:match:path"; //!!! configure
Chris@45 982
Chris@72 983 TransformFactory *tf = TransformFactory::getInstance();
Chris@45 984
Chris@72 985 Transform transform = tf->getDefaultTransformFor
Chris@112 986 (id, aggregateModel->getSampleRate());
Chris@57 987
Chris@72 988 transform.setStepSize(transform.getBlockSize()/2);
Chris@72 989 transform.setParameter("serialise", 1);
Chris@64 990
Chris@233 991 SVDEBUG << "Document::alignModel: Alignment transform step size " << transform.getStepSize() << ", block size " << transform.getBlockSize() << endl;
Chris@81 992
Chris@72 993 ModelTransformerFactory *mtf = ModelTransformerFactory::getInstance();
Chris@72 994
Chris@78 995 QString message;
Chris@78 996 Model *transformOutput = mtf->transform(transform, aggregate, message);
Chris@64 997
Chris@64 998 if (!transformOutput) {
Chris@72 999 transform.setStepSize(0);
Chris@78 1000 transformOutput = mtf->transform(transform, aggregate, message);
Chris@64 1001 }
Chris@45 1002
Chris@45 1003 SparseTimeValueModel *path = dynamic_cast<SparseTimeValueModel *>
Chris@45 1004 (transformOutput);
Chris@45 1005
Chris@45 1006 if (!path) {
Chris@45 1007 std::cerr << "Document::alignModel: ERROR: Failed to create alignment path (no MATCH plugin?)" << std::endl;
Chris@78 1008 emit alignmentFailed(id, message);
Chris@45 1009 delete transformOutput;
Chris@112 1010 delete aggregateModel;
Chris@45 1011 return;
Chris@45 1012 }
Chris@45 1013
Chris@112 1014 path->setCompletion(0);
Chris@112 1015
Chris@45 1016 AlignmentModel *alignmentModel = new AlignmentModel
Chris@112 1017 (m_mainModel, model, aggregateModel, path);
Chris@45 1018
Chris@45 1019 rm->setAlignment(alignmentModel);
Chris@45 1020 }
Chris@45 1021
Chris@45 1022 void
Chris@45 1023 Document::alignModels()
Chris@45 1024 {
Chris@45 1025 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
Chris@45 1026 alignModel(i->first);
Chris@45 1027 }
Chris@86 1028 alignModel(m_mainModel);
Chris@45 1029 }
Chris@45 1030
Chris@45 1031 Document::AddLayerCommand::AddLayerCommand(Document *d,
Chris@45 1032 View *view,
Chris@45 1033 Layer *layer) :
Chris@45 1034 m_d(d),
Chris@45 1035 m_view(view),
Chris@45 1036 m_layer(layer),
Chris@45 1037 m_name(qApp->translate("AddLayerCommand", "Add %1 Layer").arg(layer->objectName())),
Chris@45 1038 m_added(false)
Chris@45 1039 {
Chris@45 1040 }
Chris@45 1041
Chris@45 1042 Document::AddLayerCommand::~AddLayerCommand()
Chris@45 1043 {
Chris@77 1044 #ifdef DEBUG_DOCUMENT
Chris@233 1045 SVDEBUG << "Document::AddLayerCommand::~AddLayerCommand" << endl;
Chris@77 1046 #endif
Chris@45 1047 if (!m_added) {
Chris@45 1048 m_d->deleteLayer(m_layer);
Chris@45 1049 }
Chris@45 1050 }
Chris@45 1051
Chris@159 1052 QString
Chris@159 1053 Document::AddLayerCommand::getName() const
Chris@159 1054 {
Chris@165 1055 #ifdef DEBUG_DOCUMENT
Chris@233 1056 SVDEBUG << "Document::AddLayerCommand::getName(): Name is "
Chris@229 1057 << m_name << endl;
Chris@165 1058 #endif
Chris@159 1059 return m_name;
Chris@159 1060 }
Chris@159 1061
Chris@45 1062 void
Chris@45 1063 Document::AddLayerCommand::execute()
Chris@45 1064 {
Chris@45 1065 for (int i = 0; i < m_view->getLayerCount(); ++i) {
Chris@45 1066 if (m_view->getLayer(i) == m_layer) {
Chris@45 1067 // already there
Chris@45 1068 m_layer->setLayerDormant(m_view, false);
Chris@45 1069 m_added = true;
Chris@45 1070 return;
Chris@45 1071 }
Chris@45 1072 }
Chris@45 1073
Chris@45 1074 m_view->addLayer(m_layer);
Chris@45 1075 m_layer->setLayerDormant(m_view, false);
Chris@45 1076
Chris@45 1077 m_d->addToLayerViewMap(m_layer, m_view);
Chris@45 1078 m_added = true;
Chris@45 1079 }
Chris@45 1080
Chris@45 1081 void
Chris@45 1082 Document::AddLayerCommand::unexecute()
Chris@45 1083 {
Chris@45 1084 m_view->removeLayer(m_layer);
Chris@45 1085 m_layer->setLayerDormant(m_view, true);
Chris@45 1086
Chris@45 1087 m_d->removeFromLayerViewMap(m_layer, m_view);
Chris@45 1088 m_added = false;
Chris@45 1089 }
Chris@45 1090
Chris@45 1091 Document::RemoveLayerCommand::RemoveLayerCommand(Document *d,
Chris@45 1092 View *view,
Chris@45 1093 Layer *layer) :
Chris@45 1094 m_d(d),
Chris@45 1095 m_view(view),
Chris@45 1096 m_layer(layer),
Chris@45 1097 m_name(qApp->translate("RemoveLayerCommand", "Delete %1 Layer").arg(layer->objectName())),
Chris@45 1098 m_added(true)
Chris@45 1099 {
Chris@45 1100 }
Chris@45 1101
Chris@45 1102 Document::RemoveLayerCommand::~RemoveLayerCommand()
Chris@45 1103 {
Chris@77 1104 #ifdef DEBUG_DOCUMENT
Chris@233 1105 SVDEBUG << "Document::RemoveLayerCommand::~RemoveLayerCommand" << endl;
Chris@77 1106 #endif
Chris@45 1107 if (!m_added) {
Chris@45 1108 m_d->deleteLayer(m_layer);
Chris@45 1109 }
Chris@45 1110 }
Chris@45 1111
Chris@159 1112 QString
Chris@159 1113 Document::RemoveLayerCommand::getName() const
Chris@159 1114 {
Chris@171 1115 #ifdef DEBUG_DOCUMENT
Chris@233 1116 SVDEBUG << "Document::RemoveLayerCommand::getName(): Name is "
Chris@229 1117 << m_name << endl;
Chris@171 1118 #endif
Chris@159 1119 return m_name;
Chris@159 1120 }
Chris@159 1121
Chris@45 1122 void
Chris@45 1123 Document::RemoveLayerCommand::execute()
Chris@45 1124 {
Chris@45 1125 bool have = false;
Chris@45 1126 for (int i = 0; i < m_view->getLayerCount(); ++i) {
Chris@45 1127 if (m_view->getLayer(i) == m_layer) {
Chris@45 1128 have = true;
Chris@45 1129 break;
Chris@45 1130 }
Chris@45 1131 }
Chris@45 1132
Chris@45 1133 if (!have) { // not there!
Chris@45 1134 m_layer->setLayerDormant(m_view, true);
Chris@45 1135 m_added = false;
Chris@45 1136 return;
Chris@45 1137 }
Chris@45 1138
Chris@45 1139 m_view->removeLayer(m_layer);
Chris@45 1140 m_layer->setLayerDormant(m_view, true);
Chris@45 1141
Chris@45 1142 m_d->removeFromLayerViewMap(m_layer, m_view);
Chris@45 1143 m_added = false;
Chris@45 1144 }
Chris@45 1145
Chris@45 1146 void
Chris@45 1147 Document::RemoveLayerCommand::unexecute()
Chris@45 1148 {
Chris@45 1149 m_view->addLayer(m_layer);
Chris@45 1150 m_layer->setLayerDormant(m_view, false);
Chris@45 1151
Chris@45 1152 m_d->addToLayerViewMap(m_layer, m_view);
Chris@45 1153 m_added = true;
Chris@45 1154 }
Chris@45 1155
Chris@45 1156 void
Chris@45 1157 Document::toXml(QTextStream &out, QString indent, QString extraAttributes) const
Chris@45 1158 {
Chris@226 1159 toXml(out, indent, extraAttributes, false);
Chris@226 1160 }
Chris@226 1161
Chris@226 1162 void
Chris@226 1163 Document::toXmlAsTemplate(QTextStream &out, QString indent, QString extraAttributes) const
Chris@226 1164 {
Chris@226 1165 toXml(out, indent, extraAttributes, true);
Chris@226 1166 }
Chris@226 1167
Chris@226 1168 void
Chris@226 1169 Document::toXml(QTextStream &out, QString indent, QString extraAttributes,
Chris@226 1170 bool asTemplate) const
Chris@226 1171 {
Chris@45 1172 out << indent + QString("<data%1%2>\n")
Chris@45 1173 .arg(extraAttributes == "" ? "" : " ").arg(extraAttributes);
Chris@45 1174
Chris@45 1175 if (m_mainModel) {
Chris@108 1176
Chris@226 1177 if (asTemplate) {
Chris@226 1178 writePlaceholderMainModel(out, indent + " ");
Chris@226 1179 } else {
Chris@226 1180 m_mainModel->toXml(out, indent + " ", "mainModel=\"true\"");
Chris@226 1181 }
Chris@108 1182
Chris@108 1183 PlayParameters *playParameters =
Chris@108 1184 PlayParameterRepository::getInstance()->getPlayParameters(m_mainModel);
Chris@108 1185 if (playParameters) {
Chris@108 1186 playParameters->toXml
Chris@108 1187 (out, indent + " ",
Chris@108 1188 QString("model=\"%1\"")
Chris@108 1189 .arg(XmlExportable::getObjectExportId(m_mainModel)));
Chris@108 1190 }
Chris@45 1191 }
Chris@45 1192
Chris@45 1193 // Models that are not used in a layer that is in a view should
Chris@45 1194 // not be written. Get our list of required models first.
Chris@45 1195
Chris@45 1196 std::set<const Model *> used;
Chris@45 1197
Chris@45 1198 for (LayerViewMap::const_iterator i = m_layerViewMap.begin();
Chris@45 1199 i != m_layerViewMap.end(); ++i) {
Chris@45 1200
Chris@45 1201 if (i->first && !i->second.empty() && i->first->getModel()) {
Chris@45 1202 used.insert(i->first->getModel());
Chris@45 1203 }
Chris@45 1204 }
Chris@45 1205
Chris@111 1206 std::set<Model *> written;
Chris@111 1207
Chris@45 1208 for (ModelMap::const_iterator i = m_models.begin();
Chris@45 1209 i != m_models.end(); ++i) {
Chris@45 1210
Chris@111 1211 Model *model = i->first;
Chris@45 1212 const ModelRecord &rec = i->second;
Chris@45 1213
Chris@45 1214 if (used.find(model) == used.end()) continue;
Chris@45 1215
Chris@45 1216 // We need an intelligent way to determine which models need
Chris@45 1217 // to be streamed (i.e. have been edited, or are small) and
Chris@45 1218 // which should not be (i.e. remain as generated by a
Chris@45 1219 // transform, and are large).
Chris@45 1220 //
Chris@45 1221 // At the moment we can get away with deciding not to stream
Chris@45 1222 // dense 3d models or writable wave file models, provided they
Chris@45 1223 // were generated from a transform, because at the moment there
Chris@45 1224 // is no way to edit those model types so it should be safe to
Chris@45 1225 // regenerate them. That won't always work in future though.
Chris@45 1226 // It would be particularly nice to be able to ask the user,
Chris@45 1227 // as well as making an intelligent guess.
Chris@45 1228
Chris@45 1229 bool writeModel = true;
Chris@45 1230 bool haveDerivation = false;
Chris@45 1231
Chris@72 1232 if (rec.source && rec.transform.getIdentifier() != "") {
Chris@45 1233 haveDerivation = true;
Chris@45 1234 }
Chris@45 1235
Chris@45 1236 if (haveDerivation) {
Chris@45 1237 if (dynamic_cast<const WritableWaveFileModel *>(model)) {
Chris@45 1238 writeModel = false;
Chris@45 1239 } else if (dynamic_cast<const DenseThreeDimensionalModel *>(model)) {
Chris@45 1240 writeModel = false;
Chris@45 1241 }
Chris@45 1242 }
Chris@45 1243
Chris@45 1244 if (writeModel) {
Chris@111 1245 model->toXml(out, indent + " ");
Chris@111 1246 written.insert(model);
Chris@45 1247 }
Chris@45 1248
Chris@45 1249 if (haveDerivation) {
Chris@72 1250 writeBackwardCompatibleDerivation(out, indent + " ",
Chris@111 1251 model, rec);
Chris@45 1252 }
Chris@45 1253
Chris@45 1254 //!!! We should probably own the PlayParameterRepository
Chris@45 1255 PlayParameters *playParameters =
Chris@111 1256 PlayParameterRepository::getInstance()->getPlayParameters(model);
Chris@45 1257 if (playParameters) {
Chris@45 1258 playParameters->toXml
Chris@45 1259 (out, indent + " ",
Chris@45 1260 QString("model=\"%1\"")
Chris@111 1261 .arg(XmlExportable::getObjectExportId(model)));
Chris@45 1262 }
Chris@45 1263 }
Chris@45 1264
Chris@111 1265 //!!!
Chris@111 1266
Chris@111 1267 // We should write out the alignment models here. AlignmentModel
Chris@111 1268 // needs a toXml that writes out the export IDs of its reference
Chris@111 1269 // and aligned models, and then streams its path model. Note that
Chris@111 1270 // this will only work when the alignment is complete, so we
Chris@111 1271 // should probably wait for it if it isn't already by this point.
Chris@111 1272
Chris@111 1273 for (std::set<Model *>::const_iterator i = written.begin();
Chris@111 1274 i != written.end(); ++i) {
Chris@111 1275
Chris@111 1276 const Model *model = *i;
Chris@111 1277 const AlignmentModel *alignment = model->getAlignment();
Chris@111 1278 if (!alignment) continue;
Chris@111 1279
Chris@111 1280 alignment->toXml(out, indent + " ");
Chris@111 1281 }
Chris@111 1282
Chris@45 1283 for (LayerSet::const_iterator i = m_layers.begin();
Chris@45 1284 i != m_layers.end(); ++i) {
Chris@45 1285
Chris@45 1286 (*i)->toXml(out, indent + " ");
Chris@45 1287 }
Chris@45 1288
Chris@45 1289 out << indent + "</data>\n";
Chris@45 1290 }
Chris@45 1291
Chris@72 1292 void
Chris@226 1293 Document::writePlaceholderMainModel(QTextStream &out, QString indent) const
Chris@226 1294 {
Chris@226 1295 out << indent;
Chris@226 1296 out << QString("<model id=\"%1\" name=\"placeholder\" sampleRate=\"%2\" type=\"wavefile\" file=\":samples/silent.wav\" mainModel=\"true\"/>\n")
Chris@226 1297 .arg(getObjectExportId(m_mainModel))
Chris@226 1298 .arg(m_mainModel->getSampleRate());
Chris@226 1299 }
Chris@226 1300
Chris@226 1301 void
Chris@72 1302 Document::writeBackwardCompatibleDerivation(QTextStream &out, QString indent,
Chris@72 1303 Model *targetModel,
Chris@72 1304 const ModelRecord &rec) const
Chris@72 1305 {
Chris@72 1306 // There is a lot of redundancy in the XML we output here, because
Chris@72 1307 // we want it to work with older SV session file reading code as
Chris@72 1308 // well.
Chris@72 1309 //
Chris@72 1310 // Formerly, a transform was described using a derivation element
Chris@72 1311 // which set out the source and target models, execution context
Chris@72 1312 // (step size, input channel etc) and transform id, containing a
Chris@72 1313 // plugin element which set out the transform parameters and so
Chris@72 1314 // on. (The plugin element came from a "configurationXml" string
Chris@72 1315 // obtained from PluginXml.)
Chris@72 1316 //
Chris@72 1317 // This has been replaced by a derivation element setting out the
Chris@72 1318 // source and target models and input channel, containing a
Chris@72 1319 // transform element which sets out everything in the Transform.
Chris@72 1320 //
Chris@72 1321 // In order to retain compatibility with older SV code, however,
Chris@72 1322 // we have to write out the same stuff into the derivation as
Chris@72 1323 // before, and manufacture an appropriate plugin element as well
Chris@72 1324 // as the transform element. In order that newer code knows it's
Chris@72 1325 // dealing with a newer format, we will also write an attribute
Chris@72 1326 // 'type="transform"' in the derivation element.
Chris@45 1327
Chris@72 1328 const Transform &transform = rec.transform;
Chris@72 1329
Chris@72 1330 // Just for reference, this is what we would write if we didn't
Chris@72 1331 // have to be backward compatible:
Chris@72 1332 //
Chris@72 1333 // out << indent
Chris@72 1334 // << QString("<derivation type=\"transform\" source=\"%1\" "
Chris@72 1335 // "model=\"%2\" channel=\"%3\">\n")
Chris@72 1336 // .arg(XmlExportable::getObjectExportId(rec.source))
Chris@72 1337 // .arg(XmlExportable::getObjectExportId(targetModel))
Chris@72 1338 // .arg(rec.channel);
Chris@72 1339 //
Chris@72 1340 // transform.toXml(out, indent + " ");
Chris@72 1341 //
Chris@72 1342 // out << indent << "</derivation>\n";
Chris@72 1343 //
Chris@72 1344 // Unfortunately, we can't just do that. So we do this...
Chris@72 1345
Chris@72 1346 QString extentsAttributes;
Chris@72 1347 if (transform.getStartTime() != RealTime::zeroTime ||
Chris@72 1348 transform.getDuration() != RealTime::zeroTime) {
Chris@72 1349 extentsAttributes = QString("startFrame=\"%1\" duration=\"%2\" ")
Chris@72 1350 .arg(RealTime::realTime2Frame(transform.getStartTime(),
Chris@72 1351 targetModel->getSampleRate()))
Chris@72 1352 .arg(RealTime::realTime2Frame(transform.getDuration(),
Chris@72 1353 targetModel->getSampleRate()));
Chris@72 1354 }
Chris@72 1355
Chris@72 1356 out << indent;
Chris@72 1357 out << QString("<derivation type=\"transform\" source=\"%1\" "
Chris@72 1358 "model=\"%2\" channel=\"%3\" domain=\"%4\" "
Chris@72 1359 "stepSize=\"%5\" blockSize=\"%6\" %7windowType=\"%8\" "
Chris@72 1360 "transform=\"%9\">\n")
Chris@72 1361 .arg(XmlExportable::getObjectExportId(rec.source))
Chris@72 1362 .arg(XmlExportable::getObjectExportId(targetModel))
Chris@72 1363 .arg(rec.channel)
Chris@72 1364 .arg(TransformFactory::getInstance()->getTransformInputDomain
Chris@72 1365 (transform.getIdentifier()))
Chris@72 1366 .arg(transform.getStepSize())
Chris@72 1367 .arg(transform.getBlockSize())
Chris@72 1368 .arg(extentsAttributes)
Chris@72 1369 .arg(int(transform.getWindowType()))
Chris@72 1370 .arg(XmlExportable::encodeEntities(transform.getIdentifier()));
Chris@72 1371
Chris@72 1372 transform.toXml(out, indent + " ");
Chris@72 1373
Chris@72 1374 out << indent << " "
Chris@72 1375 << TransformFactory::getInstance()->getPluginConfigurationXml(transform);
Chris@72 1376
Chris@72 1377 out << indent << "</derivation>\n";
Chris@72 1378 }
Chris@72 1379