annotate framework/Document.cpp @ 420:662aef012679 alignment_view

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