comparison framework/Document.cpp @ 45:9ea770d93fae

* document -> framework (will not compile, path fixes not in yet)
author Chris Cannam
date Wed, 24 Oct 2007 16:37:58 +0000
parents
children d97a7ed7aa39
comparison
equal deleted inserted replaced
44:9ebe12983f3e 45:9ea770d93fae
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam and QMUL.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "Document.h"
17
18 #include "data/model/WaveFileModel.h"
19 #include "data/model/WritableWaveFileModel.h"
20 #include "data/model/DenseThreeDimensionalModel.h"
21 #include "data/model/DenseTimeValueModel.h"
22 #include "layer/Layer.h"
23 #include "base/CommandHistory.h"
24 #include "base/Command.h"
25 #include "view/View.h"
26 #include "base/PlayParameterRepository.h"
27 #include "base/PlayParameters.h"
28 #include "plugin/transform/TransformFactory.h"
29 #include <QApplication>
30 #include <QTextStream>
31 #include <iostream>
32
33 // For alignment:
34 #include "data/model/AggregateWaveModel.h"
35 #include "data/model/SparseTimeValueModel.h"
36 #include "data/model/AlignmentModel.h"
37
38 //!!! still need to handle command history, documentRestored/documentModified
39
40 Document::Document() :
41 m_mainModel(0)
42 {
43 connect(this, SIGNAL(modelAboutToBeDeleted(Model *)),
44 TransformFactory::getInstance(),
45 SLOT(modelAboutToBeDeleted(Model *)));
46 }
47
48 Document::~Document()
49 {
50 //!!! Document should really own the command history. atm we
51 //still refer to it in various places that don't have access to
52 //the document, be nice to fix that
53
54 // std::cerr << "\n\nDocument::~Document: about to clear command history" << std::endl;
55 CommandHistory::getInstance()->clear();
56
57 // std::cerr << "Document::~Document: about to delete layers" << std::endl;
58 while (!m_layers.empty()) {
59 deleteLayer(*m_layers.begin(), true);
60 }
61
62 if (!m_models.empty()) {
63 std::cerr << "Document::~Document: WARNING: "
64 << m_models.size() << " model(s) still remain -- "
65 << "should have been garbage collected when deleting layers"
66 << std::endl;
67 while (!m_models.empty()) {
68 Model *model = m_models.begin()->first;
69 if (model == m_mainModel) {
70 // just in case!
71 std::cerr << "Document::~Document: WARNING: Main model is also"
72 << " in models list!" << std::endl;
73 } else if (model) {
74 emit modelAboutToBeDeleted(model);
75 model->aboutToDelete();
76 delete model;
77 }
78 m_models.erase(m_models.begin());
79 }
80 }
81
82 // std::cerr << "Document::~Document: About to get rid of main model"
83 // << std::endl;
84 if (m_mainModel) {
85 emit modelAboutToBeDeleted(m_mainModel);
86 m_mainModel->aboutToDelete();
87 }
88
89 emit mainModelChanged(0);
90 delete m_mainModel;
91
92 }
93
94 Layer *
95 Document::createLayer(LayerFactory::LayerType type)
96 {
97 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
98 if (!newLayer) return 0;
99
100 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
101
102 m_layers.insert(newLayer);
103 emit layerAdded(newLayer);
104
105 return newLayer;
106 }
107
108 Layer *
109 Document::createMainModelLayer(LayerFactory::LayerType type)
110 {
111 Layer *newLayer = createLayer(type);
112 if (!newLayer) return 0;
113 setModel(newLayer, m_mainModel);
114 return newLayer;
115 }
116
117 Layer *
118 Document::createImportedLayer(Model *model)
119 {
120 LayerFactory::LayerTypeSet types =
121 LayerFactory::getInstance()->getValidLayerTypes(model);
122
123 if (types.empty()) {
124 std::cerr << "WARNING: Document::importLayer: no valid display layer for model" << std::endl;
125 return 0;
126 }
127
128 //!!! for now, just use the first suitable layer type
129 LayerFactory::LayerType type = *types.begin();
130
131 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
132 if (!newLayer) return 0;
133
134 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
135
136 addImportedModel(model);
137 setModel(newLayer, model);
138
139 //!!! and all channels
140 setChannel(newLayer, -1);
141
142 m_layers.insert(newLayer);
143 emit layerAdded(newLayer);
144 return newLayer;
145 }
146
147 Layer *
148 Document::createEmptyLayer(LayerFactory::LayerType type)
149 {
150 Model *newModel =
151 LayerFactory::getInstance()->createEmptyModel(type, m_mainModel);
152 if (!newModel) return 0;
153
154 Layer *newLayer = createLayer(type);
155 if (!newLayer) {
156 delete newModel;
157 return 0;
158 }
159
160 addImportedModel(newModel);
161 setModel(newLayer, newModel);
162
163 return newLayer;
164 }
165
166 Layer *
167 Document::createDerivedLayer(LayerFactory::LayerType type,
168 TransformId transform)
169 {
170 Layer *newLayer = createLayer(type);
171 if (!newLayer) return 0;
172
173 newLayer->setObjectName(getUniqueLayerName
174 (TransformFactory::getInstance()->
175 getTransformFriendlyName(transform)));
176
177 return newLayer;
178 }
179
180 Layer *
181 Document::createDerivedLayer(TransformId transform,
182 Model *inputModel,
183 const PluginTransform::ExecutionContext &context,
184 QString configurationXml)
185 {
186 Model *newModel = addDerivedModel(transform, inputModel,
187 context, configurationXml);
188 if (!newModel) {
189 // error already printed to stderr by addDerivedModel
190 emit modelGenerationFailed(transform);
191 return 0;
192 }
193
194 LayerFactory::LayerTypeSet types =
195 LayerFactory::getInstance()->getValidLayerTypes(newModel);
196
197 if (types.empty()) {
198 std::cerr << "WARNING: Document::createLayerForTransform: no valid display layer for output of transform " << transform.toStdString() << std::endl;
199 delete newModel;
200 return 0;
201 }
202
203 //!!! for now, just use the first suitable layer type
204
205 Layer *newLayer = createLayer(*types.begin());
206 setModel(newLayer, newModel);
207
208 //!!! We need to clone the model when adding the layer, so that it
209 //can be edited without affecting other layers that are based on
210 //the same model. Unfortunately we can't just clone it now,
211 //because it probably hasn't been completed yet -- the transform
212 //runs in the background. Maybe the transform has to handle
213 //cloning and cacheing models itself.
214 //
215 // Once we do clone models here, of course, we'll have to avoid
216 // leaking them too.
217 //
218 // We want the user to be able to add a model to a second layer
219 // _while it's still being calculated in the first_ and have it
220 // work quickly. That means we need to put the same physical
221 // model pointer in both layers, so they can't actually be cloned.
222
223 if (newLayer) {
224 newLayer->setObjectName(getUniqueLayerName
225 (TransformFactory::getInstance()->
226 getTransformFriendlyName(transform)));
227 }
228
229 emit layerAdded(newLayer);
230 return newLayer;
231 }
232
233 void
234 Document::setMainModel(WaveFileModel *model)
235 {
236 Model *oldMainModel = m_mainModel;
237 m_mainModel = model;
238
239 emit modelAdded(m_mainModel);
240
241 std::vector<Layer *> obsoleteLayers;
242 std::set<QString> failedTransforms;
243
244 // We need to ensure that no layer is left using oldMainModel or
245 // any of the old derived models as its model. Either replace the
246 // model, or delete the layer for each layer that is currently
247 // using one of these. Carry out this replacement before we
248 // delete any of the models.
249
250 for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
251
252 Layer *layer = *i;
253 Model *model = layer->getModel();
254
255 // std::cerr << "Document::setMainModel: inspecting model "
256 // << (model ? model->objectName().toStdString() : "(null)") << " in layer "
257 // << layer->objectName().toStdString() << std::endl;
258
259 if (model == oldMainModel) {
260 // std::cerr << "... it uses the old main model, replacing" << std::endl;
261 LayerFactory::getInstance()->setModel(layer, m_mainModel);
262 continue;
263 }
264
265 if (m_models.find(model) == m_models.end()) {
266 std::cerr << "WARNING: Document::setMainModel: Unknown model "
267 << model << " in layer " << layer << std::endl;
268 // get rid of this hideous degenerate
269 obsoleteLayers.push_back(layer);
270 continue;
271 }
272
273 if (m_models[model].source == oldMainModel) {
274
275 // std::cerr << "... it uses a model derived from the old main model, regenerating" << std::endl;
276
277 // This model was derived from the previous main
278 // model: regenerate it.
279
280 TransformId transform = m_models[model].transform;
281 PluginTransform::ExecutionContext context = m_models[model].context;
282
283 Model *replacementModel =
284 addDerivedModel(transform,
285 m_mainModel,
286 context,
287 m_models[model].configurationXml);
288
289 if (!replacementModel) {
290 std::cerr << "WARNING: Document::setMainModel: Failed to regenerate model for transform \""
291 << transform.toStdString() << "\"" << " in layer " << layer << std::endl;
292 if (failedTransforms.find(transform) == failedTransforms.end()) {
293 emit modelRegenerationFailed(layer->objectName(),
294 transform);
295 failedTransforms.insert(transform);
296 }
297 obsoleteLayers.push_back(layer);
298 } else {
299 std::cerr << "Replacing model " << model << " (type "
300 << typeid(*model).name() << ") with model "
301 << replacementModel << " (type "
302 << typeid(*replacementModel).name() << ") in layer "
303 << layer << " (name " << layer->objectName().toStdString() << ")"
304 << std::endl;
305 RangeSummarisableTimeValueModel *rm =
306 dynamic_cast<RangeSummarisableTimeValueModel *>(replacementModel);
307 if (rm) {
308 std::cerr << "new model has " << rm->getChannelCount() << " channels " << std::endl;
309 } else {
310 std::cerr << "new model is not a RangeSummarisableTimeValueModel!" << std::endl;
311 }
312 setModel(layer, replacementModel);
313 }
314 }
315 }
316
317 for (size_t k = 0; k < obsoleteLayers.size(); ++k) {
318 deleteLayer(obsoleteLayers[k], true);
319 }
320
321 emit mainModelChanged(m_mainModel);
322
323 // we already emitted modelAboutToBeDeleted for this
324 delete oldMainModel;
325 }
326
327 void
328 Document::addDerivedModel(TransformId transform,
329 Model *inputModel,
330 const PluginTransform::ExecutionContext &context,
331 Model *outputModelToAdd,
332 QString configurationXml)
333 {
334 if (m_models.find(outputModelToAdd) != m_models.end()) {
335 std::cerr << "WARNING: Document::addDerivedModel: Model already added"
336 << std::endl;
337 return;
338 }
339
340 // std::cerr << "Document::addDerivedModel: source is " << inputModel << " \"" << inputModel->objectName().toStdString() << "\"" << std::endl;
341
342 ModelRecord rec;
343 rec.source = inputModel;
344 rec.transform = transform;
345 rec.context = context;
346 rec.configurationXml = configurationXml;
347 rec.refcount = 0;
348
349 outputModelToAdd->setSourceModel(inputModel);
350
351 m_models[outputModelToAdd] = rec;
352
353 emit modelAdded(outputModelToAdd);
354 }
355
356
357 void
358 Document::addImportedModel(Model *model)
359 {
360 if (m_models.find(model) != m_models.end()) {
361 std::cerr << "WARNING: Document::addImportedModel: Model already added"
362 << std::endl;
363 return;
364 }
365
366 ModelRecord rec;
367 rec.source = 0;
368 rec.transform = "";
369 rec.refcount = 0;
370
371 m_models[model] = rec;
372
373 emit modelAdded(model);
374 }
375
376 Model *
377 Document::addDerivedModel(TransformId transform,
378 Model *inputModel,
379 const PluginTransform::ExecutionContext &context,
380 QString configurationXml)
381 {
382 Model *model = 0;
383
384 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
385 if (i->second.transform == transform &&
386 i->second.source == inputModel &&
387 i->second.context == context &&
388 i->second.configurationXml == configurationXml) {
389 return i->first;
390 }
391 }
392
393 model = TransformFactory::getInstance()->transform
394 (transform, inputModel, context, configurationXml);
395
396 if (!model) {
397 std::cerr << "WARNING: Document::addDerivedModel: no output model for transform " << transform.toStdString() << std::endl;
398 } else {
399 addDerivedModel(transform, inputModel, context, model, configurationXml);
400 }
401
402 return model;
403 }
404
405 void
406 Document::releaseModel(Model *model) // Will _not_ release main model!
407 {
408 if (model == 0) {
409 return;
410 }
411
412 if (model == m_mainModel) {
413 return;
414 }
415
416 bool toDelete = false;
417
418 if (m_models.find(model) != m_models.end()) {
419
420 if (m_models[model].refcount == 0) {
421 std::cerr << "WARNING: Document::releaseModel: model " << model
422 << " reference count is zero already!" << std::endl;
423 } else {
424 if (--m_models[model].refcount == 0) {
425 toDelete = true;
426 }
427 }
428 } else {
429 std::cerr << "WARNING: Document::releaseModel: Unfound model "
430 << model << std::endl;
431 toDelete = true;
432 }
433
434 if (toDelete) {
435
436 int sourceCount = 0;
437
438 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
439 if (i->second.source == model) {
440 ++sourceCount;
441 i->second.source = 0;
442 }
443 }
444
445 if (sourceCount > 0) {
446 std::cerr << "Document::releaseModel: Deleting model "
447 << model << " even though it is source for "
448 << sourceCount << " other derived model(s) -- resetting "
449 << "their source fields appropriately" << std::endl;
450 }
451
452 emit modelAboutToBeDeleted(model);
453 model->aboutToDelete();
454 m_models.erase(model);
455 delete model;
456 }
457 }
458
459 void
460 Document::deleteLayer(Layer *layer, bool force)
461 {
462 if (m_layerViewMap.find(layer) != m_layerViewMap.end() &&
463 m_layerViewMap[layer].size() > 0) {
464
465 std::cerr << "WARNING: Document::deleteLayer: Layer "
466 << layer << " [" << layer->objectName().toStdString() << "]"
467 << " is still used in " << m_layerViewMap[layer].size()
468 << " views!" << std::endl;
469
470 if (force) {
471
472 std::cerr << "(force flag set -- deleting from all views)" << std::endl;
473
474 for (std::set<View *>::iterator j = m_layerViewMap[layer].begin();
475 j != m_layerViewMap[layer].end(); ++j) {
476 // don't use removeLayerFromView, as it issues a command
477 layer->setLayerDormant(*j, true);
478 (*j)->removeLayer(layer);
479 }
480
481 m_layerViewMap.erase(layer);
482
483 } else {
484 return;
485 }
486 }
487
488 if (m_layers.find(layer) == m_layers.end()) {
489 std::cerr << "Document::deleteLayer: Layer "
490 << layer << " does not exist, or has already been deleted "
491 << "(this may not be as serious as it sounds)" << std::endl;
492 return;
493 }
494
495 m_layers.erase(layer);
496
497 releaseModel(layer->getModel());
498 emit layerRemoved(layer);
499 emit layerAboutToBeDeleted(layer);
500 delete layer;
501 }
502
503 void
504 Document::setModel(Layer *layer, Model *model)
505 {
506 if (model &&
507 model != m_mainModel &&
508 m_models.find(model) == m_models.end()) {
509 std::cerr << "ERROR: Document::setModel: Layer " << layer
510 << " (\"" << layer->objectName().toStdString()
511 << "\") wants to use unregistered model " << model
512 << ": register the layer's model before setting it!"
513 << std::endl;
514 return;
515 }
516
517 Model *previousModel = layer->getModel();
518
519 if (previousModel == model) {
520 std::cerr << "WARNING: Document::setModel: Layer " << layer << " (\""
521 << layer->objectName().toStdString()
522 << "\") is already set to model "
523 << model << " (\""
524 << (model ? model->objectName().toStdString() : "(null)")
525 << "\")" << std::endl;
526 return;
527 }
528
529 if (model && model != m_mainModel) {
530 m_models[model].refcount ++;
531 }
532
533 if (model && previousModel) {
534 PlayParameterRepository::getInstance()->copyParameters
535 (previousModel, model);
536 }
537
538 LayerFactory::getInstance()->setModel(layer, model);
539
540 if (previousModel) {
541 releaseModel(previousModel);
542 }
543 }
544
545 void
546 Document::setChannel(Layer *layer, int channel)
547 {
548 LayerFactory::getInstance()->setChannel(layer, channel);
549 }
550
551 void
552 Document::addLayerToView(View *view, Layer *layer)
553 {
554 Model *model = layer->getModel();
555 if (!model) {
556 // std::cerr << "Document::addLayerToView: Layer (\""
557 // << layer->objectName().toStdString()
558 // << "\") with no model being added to view: "
559 // << "normally you want to set the model first" << std::endl;
560 } else {
561 if (model != m_mainModel &&
562 m_models.find(model) == m_models.end()) {
563 std::cerr << "ERROR: Document::addLayerToView: Layer " << layer
564 << " has unregistered model " << model
565 << " -- register the layer's model before adding the layer!" << std::endl;
566 return;
567 }
568 }
569
570 CommandHistory::getInstance()->addCommand
571 (new Document::AddLayerCommand(this, view, layer));
572 }
573
574 void
575 Document::removeLayerFromView(View *view, Layer *layer)
576 {
577 CommandHistory::getInstance()->addCommand
578 (new Document::RemoveLayerCommand(this, view, layer));
579 }
580
581 void
582 Document::addToLayerViewMap(Layer *layer, View *view)
583 {
584 bool firstView = (m_layerViewMap.find(layer) == m_layerViewMap.end() ||
585 m_layerViewMap[layer].empty());
586
587 if (m_layerViewMap[layer].find(view) !=
588 m_layerViewMap[layer].end()) {
589 std::cerr << "WARNING: Document::addToLayerViewMap:"
590 << " Layer " << layer << " -> view " << view << " already in"
591 << " layer view map -- internal inconsistency" << std::endl;
592 }
593
594 m_layerViewMap[layer].insert(view);
595
596 if (firstView) emit layerInAView(layer, true);
597 }
598
599 void
600 Document::removeFromLayerViewMap(Layer *layer, View *view)
601 {
602 if (m_layerViewMap[layer].find(view) ==
603 m_layerViewMap[layer].end()) {
604 std::cerr << "WARNING: Document::removeFromLayerViewMap:"
605 << " Layer " << layer << " -> view " << view << " not in"
606 << " layer view map -- internal inconsistency" << std::endl;
607 }
608
609 m_layerViewMap[layer].erase(view);
610
611 if (m_layerViewMap[layer].empty()) {
612 m_layerViewMap.erase(layer);
613 emit layerInAView(layer, false);
614 }
615 }
616
617 QString
618 Document::getUniqueLayerName(QString candidate)
619 {
620 for (int count = 1; ; ++count) {
621
622 QString adjusted =
623 (count > 1 ? QString("%1 <%2>").arg(candidate).arg(count) :
624 candidate);
625
626 bool duplicate = false;
627
628 for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
629 if ((*i)->objectName() == adjusted) {
630 duplicate = true;
631 break;
632 }
633 }
634
635 if (!duplicate) return adjusted;
636 }
637 }
638
639 std::vector<Model *>
640 Document::getTransformInputModels()
641 {
642 std::vector<Model *> models;
643
644 if (!m_mainModel) return models;
645
646 models.push_back(m_mainModel);
647
648 //!!! This will pick up all models, including those that aren't visible...
649
650 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
651
652 Model *model = i->first;
653 if (!model || model == m_mainModel) continue;
654 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
655
656 if (dtvm) {
657 models.push_back(dtvm);
658 }
659 }
660
661 return models;
662 }
663
664 void
665 Document::alignModel(Model *model)
666 {
667 if (!m_mainModel || model == m_mainModel) return;
668
669 RangeSummarisableTimeValueModel *rm =
670 dynamic_cast<RangeSummarisableTimeValueModel *>(model);
671 if (!rm) return;
672
673 // This involves creating three new models:
674
675 // 1. an AggregateWaveModel to provide the mixdowns of the main
676 // model and the new model in its two channels, as input to the
677 // MATCH plugin
678
679 // 2. a SparseTimeValueModel, which is the model automatically
680 // created by FeatureExtractionPluginTransform when running the
681 // MATCH plugin (thus containing the alignment path)
682
683 // 3. an AlignmentModel, which stores the path model and carries
684 // out alignment lookups on it.
685
686 // The first two of these are provided as arguments to the
687 // constructor for the third, which takes responsibility for
688 // deleting them. The AlignmentModel, meanwhile, is passed to the
689 // new model we are aligning, which also takes responsibility for
690 // it. We should not have to delete any of these new models here.
691
692 AggregateWaveModel::ChannelSpecList components;
693
694 components.push_back(AggregateWaveModel::ModelChannelSpec
695 (m_mainModel, -1));
696
697 components.push_back(AggregateWaveModel::ModelChannelSpec
698 (rm, -1));
699
700 Model *aggregate = new AggregateWaveModel(components);
701
702 TransformId id = "vamp:match-vamp-plugin:match:path";
703
704 TransformFactory *factory = TransformFactory::getInstance();
705
706 Model *transformOutput = factory->transform
707 (id, aggregate,
708 factory->getDefaultContextForTransform(id, aggregate),
709 "<plugin param-serialise=\"1\"/>");
710
711 SparseTimeValueModel *path = dynamic_cast<SparseTimeValueModel *>
712 (transformOutput);
713
714 if (!path) {
715 std::cerr << "Document::alignModel: ERROR: Failed to create alignment path (no MATCH plugin?)" << std::endl;
716 delete transformOutput;
717 delete aggregate;
718 return;
719 }
720
721 AlignmentModel *alignmentModel = new AlignmentModel
722 (m_mainModel, model, aggregate, path);
723
724 rm->setAlignment(alignmentModel);
725 }
726
727 void
728 Document::alignModels()
729 {
730 for (ModelMap::iterator i = m_models.begin(); i != m_models.end(); ++i) {
731 alignModel(i->first);
732 }
733 }
734
735 Document::AddLayerCommand::AddLayerCommand(Document *d,
736 View *view,
737 Layer *layer) :
738 m_d(d),
739 m_view(view),
740 m_layer(layer),
741 m_name(qApp->translate("AddLayerCommand", "Add %1 Layer").arg(layer->objectName())),
742 m_added(false)
743 {
744 }
745
746 Document::AddLayerCommand::~AddLayerCommand()
747 {
748 // std::cerr << "Document::AddLayerCommand::~AddLayerCommand" << std::endl;
749 if (!m_added) {
750 m_d->deleteLayer(m_layer);
751 }
752 }
753
754 void
755 Document::AddLayerCommand::execute()
756 {
757 for (int i = 0; i < m_view->getLayerCount(); ++i) {
758 if (m_view->getLayer(i) == m_layer) {
759 // already there
760 m_layer->setLayerDormant(m_view, false);
761 m_added = true;
762 return;
763 }
764 }
765
766 m_view->addLayer(m_layer);
767 m_layer->setLayerDormant(m_view, false);
768
769 m_d->addToLayerViewMap(m_layer, m_view);
770 m_added = true;
771 }
772
773 void
774 Document::AddLayerCommand::unexecute()
775 {
776 m_view->removeLayer(m_layer);
777 m_layer->setLayerDormant(m_view, true);
778
779 m_d->removeFromLayerViewMap(m_layer, m_view);
780 m_added = false;
781 }
782
783 Document::RemoveLayerCommand::RemoveLayerCommand(Document *d,
784 View *view,
785 Layer *layer) :
786 m_d(d),
787 m_view(view),
788 m_layer(layer),
789 m_name(qApp->translate("RemoveLayerCommand", "Delete %1 Layer").arg(layer->objectName())),
790 m_added(true)
791 {
792 }
793
794 Document::RemoveLayerCommand::~RemoveLayerCommand()
795 {
796 // std::cerr << "Document::RemoveLayerCommand::~RemoveLayerCommand" << std::endl;
797 if (!m_added) {
798 m_d->deleteLayer(m_layer);
799 }
800 }
801
802 void
803 Document::RemoveLayerCommand::execute()
804 {
805 bool have = false;
806 for (int i = 0; i < m_view->getLayerCount(); ++i) {
807 if (m_view->getLayer(i) == m_layer) {
808 have = true;
809 break;
810 }
811 }
812
813 if (!have) { // not there!
814 m_layer->setLayerDormant(m_view, true);
815 m_added = false;
816 return;
817 }
818
819 m_view->removeLayer(m_layer);
820 m_layer->setLayerDormant(m_view, true);
821
822 m_d->removeFromLayerViewMap(m_layer, m_view);
823 m_added = false;
824 }
825
826 void
827 Document::RemoveLayerCommand::unexecute()
828 {
829 m_view->addLayer(m_layer);
830 m_layer->setLayerDormant(m_view, false);
831
832 m_d->addToLayerViewMap(m_layer, m_view);
833 m_added = true;
834 }
835
836 void
837 Document::toXml(QTextStream &out, QString indent, QString extraAttributes) const
838 {
839 out << indent + QString("<data%1%2>\n")
840 .arg(extraAttributes == "" ? "" : " ").arg(extraAttributes);
841
842 if (m_mainModel) {
843 m_mainModel->toXml(out, indent + " ", "mainModel=\"true\"");
844 }
845
846 // Models that are not used in a layer that is in a view should
847 // not be written. Get our list of required models first.
848
849 std::set<const Model *> used;
850
851 for (LayerViewMap::const_iterator i = m_layerViewMap.begin();
852 i != m_layerViewMap.end(); ++i) {
853
854 if (i->first && !i->second.empty() && i->first->getModel()) {
855 used.insert(i->first->getModel());
856 }
857 }
858
859 for (ModelMap::const_iterator i = m_models.begin();
860 i != m_models.end(); ++i) {
861
862 const Model *model = i->first;
863 const ModelRecord &rec = i->second;
864
865 if (used.find(model) == used.end()) continue;
866
867 // We need an intelligent way to determine which models need
868 // to be streamed (i.e. have been edited, or are small) and
869 // which should not be (i.e. remain as generated by a
870 // transform, and are large).
871 //
872 // At the moment we can get away with deciding not to stream
873 // dense 3d models or writable wave file models, provided they
874 // were generated from a transform, because at the moment there
875 // is no way to edit those model types so it should be safe to
876 // regenerate them. That won't always work in future though.
877 // It would be particularly nice to be able to ask the user,
878 // as well as making an intelligent guess.
879
880 bool writeModel = true;
881 bool haveDerivation = false;
882
883 if (rec.source && rec.transform != "") {
884 haveDerivation = true;
885 }
886
887 if (haveDerivation) {
888 if (dynamic_cast<const WritableWaveFileModel *>(model)) {
889 writeModel = false;
890 } else if (dynamic_cast<const DenseThreeDimensionalModel *>(model)) {
891 writeModel = false;
892 }
893 }
894
895 if (writeModel) {
896 i->first->toXml(out, indent + " ");
897 }
898
899 if (haveDerivation) {
900
901 QString extentsAttributes;
902 if (rec.context.startFrame != 0 ||
903 rec.context.duration != 0) {
904 extentsAttributes = QString("startFrame=\"%1\" duration=\"%2\" ")
905 .arg(rec.context.startFrame)
906 .arg(rec.context.duration);
907 }
908
909 out << indent;
910 out << QString(" <derivation source=\"%1\" model=\"%2\" channel=\"%3\" domain=\"%4\" stepSize=\"%5\" blockSize=\"%6\" %7windowType=\"%8\" transform=\"%9\"")
911 .arg(XmlExportable::getObjectExportId(rec.source))
912 .arg(XmlExportable::getObjectExportId(i->first))
913 .arg(rec.context.channel)
914 .arg(rec.context.domain)
915 .arg(rec.context.stepSize)
916 .arg(rec.context.blockSize)
917 .arg(extentsAttributes)
918 .arg(int(rec.context.windowType))
919 .arg(XmlExportable::encodeEntities(rec.transform));
920
921 if (rec.configurationXml != "") {
922 out << ">\n " + indent + rec.configurationXml
923 + "\n" + indent + " </derivation>\n";
924 } else {
925 out << "/>\n";
926 }
927 }
928
929 //!!! We should probably own the PlayParameterRepository
930 PlayParameters *playParameters =
931 PlayParameterRepository::getInstance()->getPlayParameters(i->first);
932 if (playParameters) {
933 playParameters->toXml
934 (out, indent + " ",
935 QString("model=\"%1\"")
936 .arg(XmlExportable::getObjectExportId(i->first)));
937 }
938 }
939
940 for (LayerSet::const_iterator i = m_layers.begin();
941 i != m_layers.end(); ++i) {
942
943 (*i)->toXml(out, indent + " ");
944 }
945
946 out << indent + "</data>\n";
947 }
948
949