changeset 314:2a2fd6eb8fa8

If user asks to save before initial analysis is complete, wait for it
author Chris Cannam
date Fri, 13 Jun 2014 12:12:27 +0100
parents eef5c50e7e34
children fd7bdb74c8ec 7fa301a4f149 0862da97abc5
files src/Analyser.cpp src/Analyser.h src/MainWindow.cpp src/MainWindow.h
diffstat 4 files changed, 76 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/Analyser.cpp	Fri Jun 13 10:37:04 2014 +0100
+++ b/src/Analyser.cpp	Fri Jun 13 12:12:27 2014 +0100
@@ -138,6 +138,31 @@
     return true;
 }
 
+int
+Analyser::getInitialAnalysisCompletion()
+{
+    int completion = 0;
+
+    if (m_layers[PitchTrack]) {
+        completion = m_layers[PitchTrack]->getCompletion(m_pane);
+    }
+
+    if (m_layers[Notes]) {
+        int c = m_layers[Notes]->getCompletion(m_pane);
+        if (c < completion) completion = c;
+    }
+    
+    return completion;
+}
+
+void
+Analyser::layerCompletionChanged()
+{
+    if (getInitialAnalysisCompletion() == 100) {
+        emit initialAnalysisCompleted();
+    }
+}
+
 QString
 Analyser::addVisualisations()
 {
@@ -318,6 +343,8 @@
         PlayParameters *params = pitchLayer->getPlayParameters();
         if (params) params->setPlayPan(1);
     }
+    connect(pitchLayer, SIGNAL(modelCompletionChanged()),
+            this, SLOT(layerCompletionChanged()));
     
     FlexiNoteLayer *flexiNoteLayer = 
         qobject_cast<FlexiNoteLayer *>(m_layers[Notes]);
@@ -326,6 +353,8 @@
         PlayParameters *params = flexiNoteLayer->getPlayParameters();
         if (params) params->setPlayPan(1);
     }
+    connect(flexiNoteLayer, SIGNAL(modelCompletionChanged()),
+            this, SLOT(layerCompletionChanged()));
     
     return "";
 }
--- a/src/Analyser.h	Fri Jun 13 10:37:04 2014 +0100
+++ b/src/Analyser.h	Fri Jun 13 12:12:27 2014 +0100
@@ -54,6 +54,9 @@
     bool getDisplayFrequencyExtents(float &min, float &max);
     bool setDisplayFrequencyExtents(float min, float max);
 
+    // Return completion %age for initial analysis -- 100 means it's done
+    int getInitialAnalysisCompletion();
+
     enum Component {
         Audio = 0,
         PitchTrack = 1,
@@ -203,9 +206,11 @@
 
 signals:
     void layersChanged();
+    void initialAnalysisCompleted();
 
 protected slots:
     void layerAboutToBeDeleted(Layer *);
+    void layerCompletionChanged();
 
 protected:
     Document *m_document;
--- a/src/MainWindow.cpp	Fri Jun 13 10:37:04 2014 +0100
+++ b/src/MainWindow.cpp	Fri Jun 13 12:12:27 2014 +0100
@@ -1780,6 +1780,34 @@
     return false;
 }
 
+bool
+MainWindow::waitForInitialAnalysis()
+{
+    // Called before saving a session. We can't safely save while the
+    // initial analysis is happening, because then we end up with an
+    // incomplete session on reload. There are certainly theoretically
+    // better ways to handle this...
+    
+    if (!m_analyser || m_analyser->getInitialAnalysisCompletion() >= 100) {
+        return true;
+    }
+
+    QMessageBox mb(QMessageBox::Information,
+                   tr("Waiting for analysis"),
+                   tr("Waiting for initial analysis to complete before saving..."),
+                   QMessageBox::Cancel,
+                   this);
+
+    connect(m_analyser, SIGNAL(initialAnalysisCompleted()), 
+            &mb, SLOT(accept()));
+
+    if (mb.exec() == QDialog::Accepted) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
 void
 MainWindow::saveSession()
 {
@@ -1807,6 +1835,8 @@
 {
     if (m_audioFile == "") return;
 
+    if (!waitForInitialAnalysis()) return;
+
     // We do not want to save mid-analysis regions -- that would cause
     // confusion on reloading
     m_analyser->clearReAnalysis();
@@ -1835,6 +1865,11 @@
         }
     }
 
+    if (!waitForInitialAnalysis()) {
+        QMessageBox::warning(this, tr("File not saved"),
+                             tr("Wait cancelled: the session has not been saved."));
+    }
+
     if (!saveSessionFile(path)) {
         QMessageBox::critical(this, tr("Failed to save file"),
                               tr("Session file \"%1\" could not be saved.").arg(path));
@@ -1863,6 +1898,12 @@
         return;
     }
 
+    if (!waitForInitialAnalysis()) {
+        QMessageBox::warning(this, tr("File not saved"),
+                             tr("Wait cancelled: the session has not been saved."));
+        return;
+    }
+
     if (!saveSessionFile(path)) {
         QMessageBox::critical(this, tr("Failed to save file"),
                               tr("Session file \"%1\" could not be saved.").arg(path));
--- a/src/MainWindow.h	Fri Jun 13 10:37:04 2014 +0100
+++ b/src/MainWindow.h	Fri Jun 13 12:12:27 2014 +0100
@@ -244,6 +244,7 @@
 
     virtual void closeEvent(QCloseEvent *e);
     bool checkSaveModified();
+    bool waitForInitialAnalysis();
 
     virtual void updateVisibleRangeDisplay(Pane *p) const;
     virtual void updatePositionStatusDisplays() const;