annotate framework/Document.cpp @ 269:40a0fa9af6b4 tonioni

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