annotate framework/Document.cpp @ 423:f32a64149602 alignment_view

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