diff layer/SpectrumLayer.cpp @ 349:369a197737c7

* 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 8c3fb13ef11c
children 3a55cd576334
line wrap: on
line diff
--- a/layer/SpectrumLayer.cpp	Wed Jan 23 11:38:34 2008 +0000
+++ b/layer/SpectrumLayer.cpp	Wed Jan 23 15:43:27 2008 +0000
@@ -47,8 +47,11 @@
 
 SpectrumLayer::~SpectrumLayer()
 {
-    //!!! delete parent's model
-//    for (size_t i = 0; i < m_fft.size(); ++i) delete m_fft[i];
+    Model *m = const_cast<Model *>
+        (static_cast<const Model *>(m_sliceableModel));
+    m->aboutToDelete();
+    m_sliceableModel = 0;
+    delete m;
 }
 
 void
@@ -57,27 +60,43 @@
     std::cerr << "SpectrumLayer::setModel(" << model << ") from " << m_originModel << std::endl;
     
     if (m_originModel == model) return;
+
     m_originModel = model;
 
-    if (m_sliceableModel) {
-        const Model *oldModel = m_sliceableModel;
-        setSliceableModel(0);
-        // surprised I'm allowed to delete a const pointer -- may be a
-        // source of future compiler rejection?
-        delete oldModel;
-    }
-//!!!    setupFFT();
+    m_newFFTNeeded = true;
+
+    emit layerParametersChanged();
+}
+
+void
+SpectrumLayer::setChannel(int channel)
+{
+    std::cerr << "SpectrumLayer::setChannel(" << channel << ") from " << m_channel << std::endl;
+    
+    m_channelSet = true;
+    
+    if (m_channel == channel) return;
+
+    m_channel = channel;
+
+    m_newFFTNeeded = true;
+
+    emit layerParametersChanged();
 }
 
 void
 SpectrumLayer::setupFFT()
 {
-    FFTModel *oldFFT = dynamic_cast<FFTModel *>
-        (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
-    
-    if (oldFFT) {
+    if (m_sliceableModel) {
+        Model *m = const_cast<Model *>
+            (static_cast<const Model *>(m_sliceableModel));
+        m->aboutToDelete();
         setSliceableModel(0);
-        delete oldFFT;
+        delete m;
+    }
+
+    if (!m_originModel) {
+        return;
     }
 
     FFTModel *newFFT = new FFTModel(m_originModel,
@@ -99,24 +118,8 @@
     }
 
     newFFT->resume();
-}
 
-void
-SpectrumLayer::setChannel(int channel)
-{
-    m_channelSet = true;
-
-    FFTModel *fft = dynamic_cast<FFTModel *>
-        (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
-
-    if (m_channel == channel) {
-        if (fft) fft->resume();
-        return;
-    }
-
-    m_channel = channel;
-
-    emit layerParametersChanged();
+    m_newFFTNeeded = false;
 }
 
 Layer::PropertyList
@@ -642,11 +645,14 @@
 SpectrumLayer::paint(View *v, QPainter &paint, QRect rect) const
 {
     if (!m_originModel || !m_originModel->isOK() ||
-        !m_originModel->isReady()) return;
+        !m_originModel->isReady()) {
+        std::cerr << "SpectrumLayer::paint: no origin model" << std::endl;
+        return;
+    }
 
     if (m_newFFTNeeded) {
+        std::cerr << "SpectrumLayer::paint: new FFT needed, calling setupFFT" << std::endl;
         const_cast<SpectrumLayer *>(this)->setupFFT(); //ugh
-        m_newFFTNeeded = false;
     }
 
     FFTModel *fft = dynamic_cast<FFTModel *>