changeset 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 1277a5dd11f6
children 6f545a46746b 6d9adf82347d
files .hgsubstate src/Analyser.cpp src/Analyser.h
diffstat 3 files changed, 26 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/.hgsubstate	Fri Jun 13 17:44:58 2014 +0100
+++ b/.hgsubstate	Mon Jun 16 09:28:12 2014 +0100
@@ -2,6 +2,6 @@
 236814e07bd07473958c1ff89103124536a0c3c8 dataquay
 27eee0f7a4f66b3dc39bc3c83d9c3f93f3845cc1 pyin
 553a5f65ef64811747a6613f759622d655db63c1 sv-dependency-builds
-5302c548adadd5e3ddb5fd2eafa45ce3033183e7 svapp
-defbe5bc47b78b19914b4091aa724f9e5d39e055 svcore
+f5c914661f6fdc7f05c2c980b0d21350d5933428 svapp
+85879408f6657f4076d73839d590c9fb80b78e73 svcore
 6eb0b20c940fe3475c3a847b10225ae720c7e958 svgui
--- 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);
--- a/src/Analyser.h	Fri Jun 13 17:44:58 2014 +0100
+++ b/src/Analyser.h	Mon Jun 16 09:28:12 2014 +0100
@@ -18,6 +18,7 @@
 
 #include <QObject>
 #include <QRect>
+#include <QMutex>
 
 #include <map>
 #include <vector>
@@ -228,6 +229,8 @@
     std::vector<Layer *> m_reAnalysisCandidates;
     int m_currentCandidate;
     bool m_candidatesVisible;
+    Document::LayerCreationAsyncHandle m_currentAsyncHandle;
+    QMutex m_asyncMutex;
 
     QString doAllAnalyses(bool withPitchTrack);
 
@@ -240,7 +243,8 @@
     void stackLayers();
     
     // Document::LayerCreationHandler method
-    void layersCreated(std::vector<Layer *>, std::vector<Layer *>);
+    void layersCreated(Document::LayerCreationAsyncHandle,
+                       std::vector<Layer *>, std::vector<Layer *>);
 
     void saveState(Component c) const;
     void loadState(Component c);