annotate framework/Document.cpp @ 428:b23db4cef02f alignment_view

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