diff src/Analyser.cpp @ 341:6b57c58b88d2

Cancel async layer creation in Tony when selection is changed during it
author Chris Cannam
date Mon, 16 Jun 2014 09:28:12 +0100
parents b176ea403ee8
children f3a9e4abec1b ff00432761ce
line wrap: on
line diff
--- a/src/Analyser.cpp	Fri Jun 13 17:44:58 2014 +0100
+++ b/src/Analyser.cpp	Mon Jun 16 09:28:12 2014 +0100
@@ -35,6 +35,7 @@
 #include "layer/ShowLayerCommand.h"
 
 #include <QSettings>
+#include <QMutexLocker>
 
 using std::vector;
 
@@ -44,7 +45,8 @@
     m_paneStack(0),
     m_pane(0),
     m_currentCandidate(-1),
-    m_candidatesVisible(false)
+    m_candidatesVisible(false),
+    m_currentAsyncHandle(0)
 {
     QSettings settings;
     settings.beginGroup("LayerDefaults");
@@ -414,8 +416,14 @@
 QString
 Analyser::reAnalyseSelection(Selection sel, FrequencyRange range)
 {
+    QMutexLocker locker(&m_asyncMutex);
+
     if (sel == m_reAnalysingSelection || sel.isEmpty()) return "";
 
+    if (m_currentAsyncHandle) {
+        m_document->cancelAsyncLayerCreation(m_currentAsyncHandle);
+    }
+
     if (!m_reAnalysisCandidates.empty()) {
         CommandHistory::getInstance()->startCompoundOperation
             (tr("Discard Previous Candidates"), true);
@@ -491,8 +499,9 @@
     t.setDuration(duration);
 
     transforms.push_back(t);
-
-    m_document->createDerivedLayersAsync(transforms, m_fileModel, this);
+    
+    m_currentAsyncHandle =
+        m_document->createDerivedLayersAsync(transforms, m_fileModel, this);
 
     return "";
 }
@@ -524,18 +533,15 @@
 }
 
 void
-Analyser::layersCreated(vector<Layer *> primary,
+Analyser::layersCreated(Document::LayerCreationAsyncHandle handle,
+                        vector<Layer *> primary,
                         vector<Layer *> additional)
 {
-    //!!! how do we know these came from the right selection? user
-    //!!! might have made another one since this request was issued
+    QMutexLocker locker(&m_asyncMutex);
 
-    if (m_reAnalysingSelection == Selection()) {
-        // We don't want these (actually, as above, this should check
-        // that the selection is the same as the one requested -- but
-        // all we're doing here is checking that the selection exists
-        // at all, so hasn't been cleared or the document deleted or
-        // whatever)
+    if (handle != m_currentAsyncHandle || 
+        m_reAnalysingSelection == Selection()) {
+        // We don't want these!
         for (int i = 0; i < (int)primary.size(); ++i) {
             m_document->deleteLayer(primary[i]);
         }
@@ -544,6 +550,7 @@
         }
         return;
     }
+    m_currentAsyncHandle = 0;
 
     CommandHistory::getInstance()->startCompoundOperation
         (tr("Re-Analyse Selection"), true);