Mercurial > hg > svapp
comparison framework/Document.cpp @ 77:0535c49069ba
* Various fixes to object lifetime management, particularly in the spectrum
layer and for notification of main model deletion.
The main purpose of this is to improve the behaviour of the spectrum, but I
think it may also help with #1840922 Various crashes in Layer Summary window.
author | Chris Cannam |
---|---|
date | Wed, 23 Jan 2008 15:43:27 +0000 |
parents | 4aa40182321f |
children | 58bfaaed07ed |
comparison
equal
deleted
inserted
replaced
76:2da0999dac1d | 77:0535c49069ba |
---|---|
34 // For alignment: | 34 // For alignment: |
35 #include "data/model/AggregateWaveModel.h" | 35 #include "data/model/AggregateWaveModel.h" |
36 #include "data/model/SparseTimeValueModel.h" | 36 #include "data/model/SparseTimeValueModel.h" |
37 #include "data/model/AlignmentModel.h" | 37 #include "data/model/AlignmentModel.h" |
38 | 38 |
39 #define DEBUG_DOCUMENT 1 | |
40 | |
39 //!!! still need to handle command history, documentRestored/documentModified | 41 //!!! still need to handle command history, documentRestored/documentModified |
40 | 42 |
41 Document::Document() : | 43 Document::Document() : |
42 m_mainModel(0), | 44 m_mainModel(0), |
43 m_autoAlignment(false) | 45 m_autoAlignment(false) |
51 { | 53 { |
52 //!!! Document should really own the command history. atm we | 54 //!!! Document should really own the command history. atm we |
53 //still refer to it in various places that don't have access to | 55 //still refer to it in various places that don't have access to |
54 //the document, be nice to fix that | 56 //the document, be nice to fix that |
55 | 57 |
56 // std::cerr << "\n\nDocument::~Document: about to clear command history" << std::endl; | 58 #ifdef DEBUG_DOCUMENT |
59 std::cerr << "\n\nDocument::~Document: about to clear command history" << std::endl; | |
60 #endif | |
57 CommandHistory::getInstance()->clear(); | 61 CommandHistory::getInstance()->clear(); |
58 | 62 |
63 #ifdef DEBUG_DOCUMENT | |
59 std::cerr << "Document::~Document: about to delete layers" << std::endl; | 64 std::cerr << "Document::~Document: about to delete layers" << std::endl; |
65 #endif | |
60 while (!m_layers.empty()) { | 66 while (!m_layers.empty()) { |
61 deleteLayer(*m_layers.begin(), true); | 67 deleteLayer(*m_layers.begin(), true); |
62 } | 68 } |
63 | 69 |
64 if (!m_models.empty()) { | 70 if (!m_models.empty()) { |
79 } | 85 } |
80 m_models.erase(m_models.begin()); | 86 m_models.erase(m_models.begin()); |
81 } | 87 } |
82 } | 88 } |
83 | 89 |
84 // std::cerr << "Document::~Document: About to get rid of main model" | 90 #ifdef DEBUG_DOCUMENT |
85 // << std::endl; | 91 std::cerr << "Document::~Document: About to get rid of main model" |
92 << std::endl; | |
93 #endif | |
86 if (m_mainModel) { | 94 if (m_mainModel) { |
87 emit modelAboutToBeDeleted(m_mainModel); | 95 emit modelAboutToBeDeleted(m_mainModel); |
88 m_mainModel->aboutToDelete(); | 96 m_mainModel->aboutToDelete(); |
89 } | 97 } |
90 | 98 |
101 | 109 |
102 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName())); | 110 newLayer->setObjectName(getUniqueLayerName(newLayer->objectName())); |
103 | 111 |
104 m_layers.insert(newLayer); | 112 m_layers.insert(newLayer); |
105 | 113 |
106 // std::cerr << "Document::createLayer: Added layer of type " << type | 114 #ifdef DEBUG_DOCUMENT |
107 // << ", now have " << m_layers.size() << " layers" << std::endl; | 115 std::cerr << "Document::createLayer: Added layer of type " << type |
116 << ", now have " << m_layers.size() << " layers" << std::endl; | |
117 #endif | |
108 | 118 |
109 emit layerAdded(newLayer); | 119 emit layerAdded(newLayer); |
110 | 120 |
111 return newLayer; | 121 return newLayer; |
112 } | 122 } |
145 //!!! and all channels | 155 //!!! and all channels |
146 setChannel(newLayer, -1); | 156 setChannel(newLayer, -1); |
147 | 157 |
148 m_layers.insert(newLayer); | 158 m_layers.insert(newLayer); |
149 | 159 |
160 #ifdef DEBUG_DOCUMENT | |
150 std::cerr << "Document::createImportedLayer: Added layer of type " << type | 161 std::cerr << "Document::createImportedLayer: Added layer of type " << type |
151 << ", now have " << m_layers.size() << " layers" << std::endl; | 162 << ", now have " << m_layers.size() << " layers" << std::endl; |
163 #endif | |
152 | 164 |
153 emit layerAdded(newLayer); | 165 emit layerAdded(newLayer); |
154 return newLayer; | 166 return newLayer; |
155 } | 167 } |
156 | 168 |
255 // any of the old derived models as its model. Either replace the | 267 // any of the old derived models as its model. Either replace the |
256 // model, or delete the layer for each layer that is currently | 268 // model, or delete the layer for each layer that is currently |
257 // using one of these. Carry out this replacement before we | 269 // using one of these. Carry out this replacement before we |
258 // delete any of the models. | 270 // delete any of the models. |
259 | 271 |
260 // std::cerr << "Document::setMainModel: Have " | 272 #ifdef DEBUG_DOCUMENT |
261 // << m_layers.size() << " layers" << std::endl; | 273 std::cerr << "Document::setMainModel: Have " |
274 << m_layers.size() << " layers" << std::endl; | |
275 #endif | |
262 | 276 |
263 for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { | 277 for (LayerSet::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { |
264 | 278 |
265 Layer *layer = *i; | 279 Layer *layer = *i; |
266 Model *model = layer->getModel(); | 280 Model *model = layer->getModel(); |
267 | 281 |
268 // std::cerr << "Document::setMainModel: inspecting model " | 282 #ifdef DEBUG_DOCUMENT |
269 // << (model ? model->objectName().toStdString() : "(null)") << " in layer " | 283 std::cerr << "Document::setMainModel: inspecting model " |
270 // << layer->objectName().toStdString() << std::endl; | 284 << (model ? model->objectName().toStdString() : "(null)") << " in layer " |
285 << layer->objectName().toStdString() << std::endl; | |
286 #endif | |
271 | 287 |
272 if (model == oldMainModel) { | 288 if (model == oldMainModel) { |
273 // std::cerr << "... it uses the old main model, replacing" << std::endl; | 289 #ifdef DEBUG_DOCUMENT |
290 std::cerr << "... it uses the old main model, replacing" << std::endl; | |
291 #endif | |
274 LayerFactory::getInstance()->setModel(layer, m_mainModel); | 292 LayerFactory::getInstance()->setModel(layer, m_mainModel); |
275 continue; | 293 continue; |
276 } | 294 } |
277 | 295 |
278 if (model && (m_models.find(model) == m_models.end())) { | 296 if (model && (m_models.find(model) == m_models.end())) { |
284 } | 302 } |
285 | 303 |
286 if (m_models[model].source && | 304 if (m_models[model].source && |
287 (m_models[model].source == oldMainModel)) { | 305 (m_models[model].source == oldMainModel)) { |
288 | 306 |
289 // std::cerr << "... it uses a model derived from the old main model, regenerating" << std::endl; | 307 #ifdef DEBUG_DOCUMENT |
308 std::cerr << "... it uses a model derived from the old main model, regenerating" << std::endl; | |
309 #endif | |
290 | 310 |
291 // This model was derived from the previous main | 311 // This model was derived from the previous main |
292 // model: regenerate it. | 312 // model: regenerate it. |
293 | 313 |
294 const Transform &transform = m_models[model].transform; | 314 const Transform &transform = m_models[model].transform; |
312 transformId); | 332 transformId); |
313 failedTransformers.insert(transformId); | 333 failedTransformers.insert(transformId); |
314 } | 334 } |
315 obsoleteLayers.push_back(layer); | 335 obsoleteLayers.push_back(layer); |
316 } else { | 336 } else { |
317 // std::cerr << "Replacing model " << model << " (type " | 337 #ifdef DEBUG_DOCUMENT |
318 // << typeid(*model).name() << ") with model " | 338 std::cerr << "Replacing model " << model << " (type " |
319 // << replacementModel << " (type " | 339 << typeid(*model).name() << ") with model " |
320 // << typeid(*replacementModel).name() << ") in layer " | 340 << replacementModel << " (type " |
321 // << layer << " (name " << layer->objectName().toStdString() << ")" | 341 << typeid(*replacementModel).name() << ") in layer " |
322 // << std::endl; | 342 << layer << " (name " << layer->objectName().toStdString() << ")" |
343 << std::endl; | |
344 #endif | |
323 RangeSummarisableTimeValueModel *rm = | 345 RangeSummarisableTimeValueModel *rm = |
324 dynamic_cast<RangeSummarisableTimeValueModel *>(replacementModel); | 346 dynamic_cast<RangeSummarisableTimeValueModel *>(replacementModel); |
347 #ifdef DEBUG_DOCUMENT | |
325 if (rm) { | 348 if (rm) { |
326 std::cerr << "new model has " << rm->getChannelCount() << " channels " << std::endl; | 349 std::cerr << "new model has " << rm->getChannelCount() << " channels " << std::endl; |
327 } else { | 350 } else { |
328 std::cerr << "new model is not a RangeSummarisableTimeValueModel!" << std::endl; | 351 std::cerr << "new model is not a RangeSummarisableTimeValueModel!" << std::endl; |
329 } | 352 } |
353 #endif | |
330 setModel(layer, replacementModel); | 354 setModel(layer, replacementModel); |
331 } | 355 } |
332 } | 356 } |
333 } | 357 } |
334 | 358 |
341 (i->first->getAlignmentReference() == oldMainModel)) { | 365 (i->first->getAlignmentReference() == oldMainModel)) { |
342 alignModel(i->first); | 366 alignModel(i->first); |
343 } | 367 } |
344 } | 368 } |
345 | 369 |
370 if (oldMainModel) { | |
371 emit modelAboutToBeDeleted(oldMainModel); | |
372 oldMainModel->aboutToDelete(); | |
373 } | |
374 | |
346 emit mainModelChanged(m_mainModel); | 375 emit mainModelChanged(m_mainModel); |
347 | 376 |
348 // we already emitted modelAboutToBeDeleted for this | |
349 delete oldMainModel; | 377 delete oldMainModel; |
350 } | 378 } |
351 | 379 |
352 void | 380 void |
353 Document::addDerivedModel(const Transform &transform, | 381 Document::addDerivedModel(const Transform &transform, |
358 std::cerr << "WARNING: Document::addDerivedModel: Model already added" | 386 std::cerr << "WARNING: Document::addDerivedModel: Model already added" |
359 << std::endl; | 387 << std::endl; |
360 return; | 388 return; |
361 } | 389 } |
362 | 390 |
363 // std::cerr << "Document::addDerivedModel: source is " << inputModel << " \"" << inputModel->objectName().toStdString() << "\"" << std::endl; | 391 #ifdef DEBUG_DOCUMENT |
392 std::cerr << "Document::addDerivedModel: source is " << input.getModel() << " \"" << input.getModel()->objectName().toStdString() << "\"" << std::endl; | |
393 #endif | |
364 | 394 |
365 ModelRecord rec; | 395 ModelRecord rec; |
366 rec.source = input.getModel(); | 396 rec.source = input.getModel(); |
367 rec.channel = input.getChannel(); | 397 rec.channel = input.getChannel(); |
368 rec.transform = transform; | 398 rec.transform = transform; |
486 << " is still used in " << m_layerViewMap[layer].size() | 516 << " is still used in " << m_layerViewMap[layer].size() |
487 << " views!" << std::endl; | 517 << " views!" << std::endl; |
488 | 518 |
489 if (force) { | 519 if (force) { |
490 | 520 |
521 #ifdef DEBUG_DOCUMENT | |
491 std::cerr << "(force flag set -- deleting from all views)" << std::endl; | 522 std::cerr << "(force flag set -- deleting from all views)" << std::endl; |
523 #endif | |
492 | 524 |
493 for (std::set<View *>::iterator j = m_layerViewMap[layer].begin(); | 525 for (std::set<View *>::iterator j = m_layerViewMap[layer].begin(); |
494 j != m_layerViewMap[layer].end(); ++j) { | 526 j != m_layerViewMap[layer].end(); ++j) { |
495 // don't use removeLayerFromView, as it issues a command | 527 // don't use removeLayerFromView, as it issues a command |
496 layer->setLayerDormant(*j, true); | 528 layer->setLayerDormant(*j, true); |
573 void | 605 void |
574 Document::addLayerToView(View *view, Layer *layer) | 606 Document::addLayerToView(View *view, Layer *layer) |
575 { | 607 { |
576 Model *model = layer->getModel(); | 608 Model *model = layer->getModel(); |
577 if (!model) { | 609 if (!model) { |
578 // std::cerr << "Document::addLayerToView: Layer (\"" | 610 #ifdef DEBUG_DOCUMENT |
579 // << layer->objectName().toStdString() | 611 std::cerr << "Document::addLayerToView: Layer (\"" |
580 // << "\") with no model being added to view: " | 612 << layer->objectName().toStdString() |
581 // << "normally you want to set the model first" << std::endl; | 613 << "\") with no model being added to view: " |
614 << "normally you want to set the model first" << std::endl; | |
615 #endif | |
582 } else { | 616 } else { |
583 if (model != m_mainModel && | 617 if (model != m_mainModel && |
584 m_models.find(model) == m_models.end()) { | 618 m_models.find(model) == m_models.end()) { |
585 std::cerr << "ERROR: Document::addLayerToView: Layer " << layer | 619 std::cerr << "ERROR: Document::addLayerToView: Layer " << layer |
586 << " has unregistered model " << model | 620 << " has unregistered model " << model |
679 models.push_back(dtvm); | 713 models.push_back(dtvm); |
680 } | 714 } |
681 } | 715 } |
682 | 716 |
683 return models; | 717 return models; |
718 } | |
719 | |
720 bool | |
721 Document::isKnownModel(const Model *model) const | |
722 { | |
723 if (model == m_mainModel) return true; | |
724 return (m_models.find(const_cast<Model *>(model)) != m_models.end()); | |
684 } | 725 } |
685 | 726 |
686 bool | 727 bool |
687 Document::canAlign() | 728 Document::canAlign() |
688 { | 729 { |
788 { | 829 { |
789 } | 830 } |
790 | 831 |
791 Document::AddLayerCommand::~AddLayerCommand() | 832 Document::AddLayerCommand::~AddLayerCommand() |
792 { | 833 { |
793 // std::cerr << "Document::AddLayerCommand::~AddLayerCommand" << std::endl; | 834 #ifdef DEBUG_DOCUMENT |
835 std::cerr << "Document::AddLayerCommand::~AddLayerCommand" << std::endl; | |
836 #endif | |
794 if (!m_added) { | 837 if (!m_added) { |
795 m_d->deleteLayer(m_layer); | 838 m_d->deleteLayer(m_layer); |
796 } | 839 } |
797 } | 840 } |
798 | 841 |
836 { | 879 { |
837 } | 880 } |
838 | 881 |
839 Document::RemoveLayerCommand::~RemoveLayerCommand() | 882 Document::RemoveLayerCommand::~RemoveLayerCommand() |
840 { | 883 { |
841 // std::cerr << "Document::RemoveLayerCommand::~RemoveLayerCommand" << std::endl; | 884 #ifdef DEBUG_DOCUMENT |
885 std::cerr << "Document::RemoveLayerCommand::~RemoveLayerCommand" << std::endl; | |
886 #endif | |
842 if (!m_added) { | 887 if (!m_added) { |
843 m_d->deleteLayer(m_layer); | 888 m_d->deleteLayer(m_layer); |
844 } | 889 } |
845 } | 890 } |
846 | 891 |