annotate framework/Document.cpp @ 684:5e9b1956b609 by-id

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