annotate framework/Document.cpp @ 296:19282182e60d tonioni_multi_transform

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