comparison framework/Document.cpp @ 691:c8ba09756eff by-id

Work on management of alignment-related models
author Chris Cannam
date Fri, 12 Jul 2019 13:58:02 +0100
parents 827a522a5da4
children ad5917362158
comparison
equal deleted inserted replaced
690:827a522a5da4 691:c8ba09756eff
71 SVDEBUG << "\n\nDocument::~Document: about to clear command history" << endl; 71 SVDEBUG << "\n\nDocument::~Document: about to clear command history" << endl;
72 #endif 72 #endif
73 CommandHistory::getInstance()->clear(); 73 CommandHistory::getInstance()->clear();
74 74
75 #ifdef DEBUG_DOCUMENT 75 #ifdef DEBUG_DOCUMENT
76 SVDEBUG << "Document::~Document: about to delete layers" << endl; 76 SVCERR << "Document::~Document: about to delete layers" << endl;
77 #endif 77 #endif
78 while (!m_layers.empty()) { 78 while (!m_layers.empty()) {
79 deleteLayer(*m_layers.begin(), true); 79 deleteLayer(*m_layers.begin(), true);
80 } 80 }
81 81
82 #ifdef DEBUG_DOCUMENT
83 SVCERR << "Document::~Document: about to release normal models" << endl;
84 #endif
82 for (auto mr: m_models) { 85 for (auto mr: m_models) {
83 ModelById::release(mr.first); 86 ModelById::release(mr.first);
84 } 87 }
88
89 #ifdef DEBUG_DOCUMENT
90 SVCERR << "Document::~Document: about to release aggregate models" << endl;
91 #endif
85 for (auto m: m_aggregateModels) { 92 for (auto m: m_aggregateModels) {
86 ModelById::release(m); 93 ModelById::release(m);
87 } 94 }
88 95
96 #ifdef DEBUG_DOCUMENT
97 SVCERR << "Document::~Document: about to release alignment models" << endl;
98 #endif
99 for (auto m: m_alignmentModels) {
100 ModelById::release(m);
101 }
102
103 #ifdef DEBUG_DOCUMENT
104 SVCERR << "Document::~Document: about to release main model" << endl;
105 #endif
89 if (!m_mainModel.isNone()) { 106 if (!m_mainModel.isNone()) {
90 ModelById::release(m_mainModel); 107 ModelById::release(m_mainModel);
91 } 108 }
109
92 m_mainModel = {}; 110 m_mainModel = {};
93 emit mainModelChanged({}); 111 emit mainModelChanged({});
94 } 112 }
95 113
96 Layer * 114 Layer *
139 Layer *newLayer = LayerFactory::getInstance()->createLayer(type); 157 Layer *newLayer = LayerFactory::getInstance()->createLayer(type);
140 if (!newLayer) return nullptr; 158 if (!newLayer) return nullptr;
141 159
142 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName())); 160 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName()));
143 161
144 addImportedModel(modelId); 162 addNonDerivedModel(modelId);
145 setModel(newLayer, modelId); 163 setModel(newLayer, modelId);
146 164
147 //!!! and all channels 165 //!!! and all channels
148 setChannel(newLayer, -1); 166 setChannel(newLayer, -1);
149 167
171 if (!newLayer) { 189 if (!newLayer) {
172 return nullptr; 190 return nullptr;
173 } 191 }
174 192
175 auto newModelId = ModelById::add(newModel); 193 auto newModelId = ModelById::add(newModel);
176 addImportedModel(newModelId); 194 addNonDerivedModel(newModelId);
177 setModel(newLayer, newModelId); 195 setModel(newLayer, newModelId);
178 196
179 return newLayer; 197 return newLayer;
180 } 198 }
181 199
605 623
606 emit modelAdded(outputModelToAdd); 624 emit modelAdded(outputModelToAdd);
607 } 625 }
608 626
609 void 627 void
610 Document::addImportedModel(ModelId modelId) 628 Document::addNonDerivedModel(ModelId modelId)
611 { 629 {
630 if (ModelById::isa<AggregateWaveModel>(modelId)) {
631 #ifdef DEBUG_DOCUMENT
632 SVCERR << "Document::addNonDerivedModel: Model " << modelId << " is an aggregate model, adding it to aggregates" << endl;
633 #endif
634 m_aggregateModels.insert(modelId);
635 return;
636 }
637 if (ModelById::isa<AlignmentModel>(modelId)) {
638 #ifdef DEBUG_DOCUMENT
639 SVCERR << "Document::addNonDerivedModel: Model " << modelId << " is an alignment model, adding it to alignments" << endl;
640 #endif
641 m_alignmentModels.insert(modelId);
642 return;
643 }
644
612 if (m_models.find(modelId) != m_models.end()) { 645 if (m_models.find(modelId) != m_models.end()) {
613 SVCERR << "WARNING: Document::addImportedModel: Model already added" 646 SVCERR << "WARNING: Document::addNonDerivedModel: Model already added"
614 << endl; 647 << endl;
615 return; 648 return;
616 } 649 }
617 650
618 ModelRecord rec; 651 ModelRecord rec;
621 rec.additional = false; 654 rec.additional = false;
622 655
623 m_models[modelId] = rec; 656 m_models[modelId] = rec;
624 657
625 #ifdef DEBUG_DOCUMENT 658 #ifdef DEBUG_DOCUMENT
626 SVDEBUG << "Document::addImportedModel: Added model " << modelId << endl; 659 SVCERR << "Document::addNonDerivedModel: Added model " << modelId << endl;
627 SVDEBUG << "Models now: "; 660 SVCERR << "Models now: ";
628 for (const auto &rec : m_models) { 661 for (const auto &rec : m_models) {
629 SVDEBUG << rec.first << " "; 662 SVCERR << rec.first << " ";
630 } 663 }
631 SVDEBUG << endl; 664 SVCERR << endl;
632 #endif 665 #endif
633 666
634 if (m_autoAlignment) { 667 if (m_autoAlignment) {
635 SVDEBUG << "Document::addImportedModel: auto-alignment is on, aligning model if possible" << endl; 668 SVDEBUG << "Document::addNonDerivedModel: auto-alignment is on, aligning model if possible" << endl;
636 alignModel(modelId); 669 alignModel(modelId);
637 } else { 670 } else {
638 SVDEBUG << "Document(" << this << "): addImportedModel: auto-alignment is off" << endl; 671 SVDEBUG << "Document(" << this << "): addNonDerivedModel: auto-alignment is off" << endl;
639 } 672 }
640 673
641 emit modelAdded(modelId); 674 emit modelAdded(modelId);
642 } 675 }
643 676
664 SVDEBUG << rec.first << " "; 697 SVDEBUG << rec.first << " ";
665 } 698 }
666 SVDEBUG << endl; 699 SVDEBUG << endl;
667 #endif 700 #endif
668 701
669 if (m_autoAlignment) { 702 if (m_autoAlignment &&
670 SVDEBUG << "Document::addAdditionalModel: auto-alignment is on, aligning model if possible" << endl; 703 ModelById::isa<RangeSummarisableTimeValueModel>(modelId)) {
704 SVDEBUG << "Document::addAdditionalModel: auto-alignment is on and model is an alignable type, aligning it if possible" << endl;
671 alignModel(modelId); 705 alignModel(modelId);
672 } 706 }
673 707
674 emit modelAdded(modelId); 708 emit modelAdded(modelId);
675 }
676
677 void
678 Document::addAggregateModel(ModelId modelId)
679 {
680 m_aggregateModels.insert(modelId);
681 SVDEBUG << "Document::addAggregateModel(" << modelId << ")" << endl;
682 } 709 }
683 710
684 ModelId 711 ModelId
685 Document::addDerivedModel(const Transform &transform, 712 Document::addDerivedModel(const Transform &transform,
686 const ModelTransformer::Input &input, 713 const ModelTransformer::Input &input,
756 // we know no other layer is using one, we should release it. If 783 // we know no other layer is using one, we should release it. If
757 // we happen to release one that is being used, the ModelById 784 // we happen to release one that is being used, the ModelById
758 // borrowed-pointer mechanism will at least prevent memory errors, 785 // borrowed-pointer mechanism will at least prevent memory errors,
759 // although the other code will have to stop whatever it's doing. 786 // although the other code will have to stop whatever it's doing.
760 787
761 SVCERR << "Document::releaseModel(" << modelId << ")" << endl; 788 if (auto model = ModelById::get(modelId)) {
789 SVCERR << "Document::releaseModel(" << modelId << "), name "
790 << model->objectName() << ", type "
791 << typeid(*model.get()).name() << endl;
792 } else {
793 SVCERR << "Document::releaseModel(" << modelId << ")" << endl;
794 }
762 795
763 if (modelId.isNone()) { 796 if (modelId.isNone()) {
764 return; 797 return;
765 } 798 }
766 799
775 #endif 808 #endif
776 return; 809 return;
777 } 810 }
778 811
779 if (m_models.find(modelId) == m_models.end()) { 812 if (m_models.find(modelId) == m_models.end()) {
780 // No point in releasing aggregate models and the like, 813 // No point in releasing aggregate and alignment models,
781 // they're not large 814 // they're not large
782 #ifdef DEBUG_DOCUMENT 815 #ifdef DEBUG_DOCUMENT
783 SVCERR << "Document::releaseModel: It's not a regular layer model, ignoring" << endl; 816 SVCERR << "Document::releaseModel: It's not a regular layer model, ignoring" << endl;
784 #endif 817 #endif
785 return; 818 return;
900 << layer->objectName() 933 << layer->objectName()
901 << "\") is already set to model " 934 << "\") is already set to model "
902 << modelId << endl; 935 << modelId << endl;
903 return; 936 return;
904 } 937 }
905 /*!!! 938
906 if (model && model != m_mainModel) {
907 ModelList::iterator mitr = findModelInList(model);
908 if (mitr != m_models.end()) {
909 mitr->refcount ++;
910 }
911 }
912 */
913 if (!modelId.isNone() && !previousModel.isNone()) { 939 if (!modelId.isNone() && !previousModel.isNone()) {
914 PlayParameterRepository::getInstance()->copyParameters 940 PlayParameterRepository::getInstance()->copyParameters
915 (previousModel.untyped, modelId.untyped); 941 (previousModel.untyped, modelId.untyped);
916 } 942 }
917 943
1098 // without also knowing what the main model is 1124 // without also knowing what the main model is
1099 SVDEBUG << "Document::alignModel(" << modelId 1125 SVDEBUG << "Document::alignModel(" << modelId
1100 << "): is main model, setting alignment to itself" << endl; 1126 << "): is main model, setting alignment to itself" << endl;
1101 auto alignment = std::make_shared<AlignmentModel>(modelId, modelId, 1127 auto alignment = std::make_shared<AlignmentModel>(modelId, modelId,
1102 ModelId()); 1128 ModelId());
1103 1129
1104 //!!! hang on, who tracks alignment models? 1130 ModelId alignmentModelId = ModelById::add(alignment);
1105 rm->setAlignment(ModelById::add(alignment)); 1131 rm->setAlignment(alignmentModelId);
1132 m_alignmentModels.insert(alignmentModelId);
1106 return; 1133 return;
1107 } 1134 }
1108 1135
1109 auto w = ModelById::getAs<WritableWaveFileModel>(modelId); 1136 auto w = ModelById::getAs<WritableWaveFileModel>(modelId);
1110 if (w && w->getWriteProportion() < 100) { 1137 if (w && w->getWriteProportion() < 100) {