changeset 687:e0b0f3e163ca by-id

Update for removal of (public) getId from Model
author Chris Cannam
date Fri, 05 Jul 2019 15:35:11 +0100
parents 610fa108fbcc
children 11336010561e
files audio/AudioCallbackPlaySource.cpp audio/AudioCallbackPlaySource.h framework/Align.cpp framework/Align.h framework/Document.cpp framework/Document.h framework/MainWindowBase.cpp framework/SVFileReader.cpp
diffstat 8 files changed, 121 insertions(+), 138 deletions(-) [+]
line wrap: on
line diff
--- a/audio/AudioCallbackPlaySource.cpp	Thu Jul 04 18:04:59 2019 +0100
+++ b/audio/AudioCallbackPlaySource.cpp	Fri Jul 05 15:35:11 2019 +0100
@@ -100,8 +100,8 @@
             m_viewManager, SLOT(playStatusChanged(bool)));
 
     connect(PlayParameterRepository::getInstance(),
-            SIGNAL(playParametersChanged(PlayParameters *)),
-            this, SLOT(playParametersChanged(PlayParameters *)));
+            SIGNAL(playParametersChanged(int)),
+            this, SLOT(playParametersChanged(int)));
 
     connect(Preferences::getInstance(),
             SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
@@ -294,8 +294,8 @@
     SVDEBUG << "AudioCallbackPlaySource::addModel: now have " << m_models.size() << " model(s)" << endl;
 #endif
 
-    connect(model.get(), SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
-            this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t)));
+    connect(model.get(), SIGNAL(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)),
+            this, SLOT(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)));
 
 #ifdef DEBUG_AUDIO_PLAY_SOURCE
     cout << "AudioCallbackPlaySource::addModel: awakening thread" << endl;
@@ -305,7 +305,7 @@
 }
 
 void
-AudioCallbackPlaySource::modelChangedWithin(sv_frame_t 
+AudioCallbackPlaySource::modelChangedWithin(ModelId, sv_frame_t 
 #ifdef DEBUG_AUDIO_PLAY_SOURCE
                                             startFrame
 #endif
@@ -332,8 +332,8 @@
     cout << "AudioCallbackPlaySource::removeModel(" << modelId << ")" << endl;
 #endif
 
-    disconnect(model.get(), SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
-               this, SLOT(modelChangedWithin(sv_frame_t, sv_frame_t)));
+    disconnect(model.get(), SIGNAL(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)),
+               this, SLOT(modelChangedWithin(ModelId, sv_frame_t, sv_frame_t)));
 
     m_models.erase(modelId);
 
@@ -576,13 +576,13 @@
 }
 
 void
-AudioCallbackPlaySource::playParametersChanged(PlayParameters *)
+AudioCallbackPlaySource::playParametersChanged(int)
 {
     clearRingBuffers();
 }
 
 void
-AudioCallbackPlaySource::preferenceChanged(PropertyContainer::PropertyName )
+AudioCallbackPlaySource::preferenceChanged(PropertyContainer::PropertyName)
 {
 }
 
--- a/audio/AudioCallbackPlaySource.h	Thu Jul 04 18:04:59 2019 +0100
+++ b/audio/AudioCallbackPlaySource.h	Fri Jul 05 15:35:11 2019 +0100
@@ -326,9 +326,9 @@
     void selectionChanged();
     void playLoopModeChanged();
     void playSelectionModeChanged();
-    void playParametersChanged(PlayParameters *);
+    void playParametersChanged(int);
     void preferenceChanged(PropertyContainer::PropertyName);
-    void modelChangedWithin(sv_frame_t startFrame, sv_frame_t endFrame);
+    void modelChangedWithin(ModelId, sv_frame_t startFrame, sv_frame_t endFrame);
 
 protected:
     ViewManagerBase                  *m_viewManager;
--- a/framework/Align.cpp	Thu Jul 04 18:04:59 2019 +0100
+++ b/framework/Align.cpp	Fri Jul 05 15:35:11 2019 +0100
@@ -89,14 +89,19 @@
 }
 
 bool
-Align::alignModelViaTransform(Document *doc, ModelId ref, ModelId other,
+Align::alignModelViaTransform(Document *doc,
+                              ModelId referenceId,
+                              ModelId otherId,
                               QString &error)
 {
     QMutexLocker locker (&m_mutex);
 
-    auto reference = ModelById::getAs<RangeSummarisableTimeValueModel>(ref);
-    auto rm = ModelById::getAs<RangeSummarisableTimeValueModel>(other);
-    if (!reference || !rm) return false;
+    auto reference =
+        ModelById::getAs<RangeSummarisableTimeValueModel>(referenceId);
+    auto other =
+        ModelById::getAs<RangeSummarisableTimeValueModel>(otherId);
+
+    if (!reference || !other) return false;
    
     // This involves creating either three or four new models:
     //
@@ -135,27 +140,27 @@
 
     AggregateWaveModel::ChannelSpecList components;
 
-    components.push_back(AggregateWaveModel::ModelChannelSpec
-                         (reference->getId(), -1));
+    components.push_back
+        (AggregateWaveModel::ModelChannelSpec(referenceId, -1));
 
-    components.push_back(AggregateWaveModel::ModelChannelSpec
-                         (rm->getId(), -1));
+    components.push_back
+        (AggregateWaveModel::ModelChannelSpec(otherId, -1));
 
     auto aggregateModel = std::make_shared<AggregateWaveModel>(components);
-    ModelById::add(aggregateModel);
-    doc->addAggregateModel(aggregateModel->getId());
+    auto aggregateModelId = ModelById::add(aggregateModel);
+    doc->addAggregateModel(aggregateModelId);
 
-    auto alignmentModel = std::make_shared<AlignmentModel>(ref, other,
-                                                           ModelId());
-    ModelById::add(alignmentModel);
+    auto alignmentModel = std::make_shared<AlignmentModel>
+        (referenceId, otherId, ModelId());
+    auto alignmentModelId = ModelById::add(alignmentModel);
 
     TransformId tdId = getTuningDifferenceTransformName();
 
     if (tdId == "") {
         
-        if (beginTransformDrivenAlignment(aggregateModel->getId(),
-                                          alignmentModel->getId())) {
-            rm->setAlignment(alignmentModel->getId());
+        if (beginTransformDrivenAlignment(aggregateModelId,
+                                          alignmentModelId)) {
+            other->setAlignment(alignmentModelId);
         } else {
             error = alignmentModel->getError();
             ModelById::release(alignmentModel);
@@ -182,7 +187,7 @@
 
         QString message;
         ModelId transformOutput = mtf->transform(transform,
-                                                 aggregateModel->getId(),
+                                                 aggregateModelId,
                                                  message);
 
         auto tdout = ModelById::getAs<SparseTimeValueModel>(transformOutput);
@@ -192,23 +197,23 @@
             return false;
         }
 
-        rm->setAlignment(alignmentModel->getId());
+        other->setAlignment(alignmentModelId);
     
-        connect(tdout.get(), SIGNAL(completionChanged()),
-                this, SLOT(tuningDifferenceCompletionChanged()));
+        connect(tdout.get(), SIGNAL(completionChanged(ModelId)),
+                this, SLOT(tuningDifferenceCompletionChanged(ModelId)));
 
         TuningDiffRec rec;
-        rec.input = aggregateModel->getId();
-        rec.alignment = alignmentModel->getId();
+        rec.input = aggregateModelId;
+        rec.alignment = alignmentModelId;
         
         // This model exists only so that the AlignmentModel can get a
         // completion value from somewhere while the tuning difference
         // calculation is going on
         auto preparatoryModel = std::make_shared<SparseTimeValueModel>
             (aggregateModel->getSampleRate(), 1);
-        ModelById::add(preparatoryModel);
+        auto preparatoryModelId = ModelById::add(preparatoryModel);
         preparatoryModel->setCompletion(0);
-        rec.preparatory = preparatoryModel->getId();
+        rec.preparatory = preparatoryModelId;
         alignmentModel->setPathFrom(rec.preparatory);
         
         m_pendingTuningDiffs[transformOutput] = rec;
@@ -218,16 +223,9 @@
 }
 
 void
-Align::tuningDifferenceCompletionChanged()
+Align::tuningDifferenceCompletionChanged(ModelId tdId)
 {
-    QMutexLocker locker (&m_mutex);
-
-    ModelId tdId;
-    if (Model *modelPtr = qobject_cast<Model *>(sender())) {
-        tdId = modelPtr->getId();
-    } else {
-        return;
-    }
+    QMutexLocker locker(&m_mutex);
 
     if (m_pendingTuningDiffs.find(tdId) == m_pendingTuningDiffs.end()) {
         SVCERR << "ERROR: Align::tuningDifferenceCompletionChanged: Model "
@@ -346,65 +344,61 @@
     path->setCompletion(0);
     alignmentModel->setPathFrom(transformOutput); //!!! who releases transformOutput?
 
-    connect(alignmentModel.get(), SIGNAL(completionChanged()),
-            this, SLOT(alignmentCompletionChanged()));
+    connect(alignmentModel.get(), SIGNAL(completionChanged(ModelId)),
+            this, SLOT(alignmentCompletionChanged(ModelId)));
 
     return true;
 }
 
 void
-Align::alignmentCompletionChanged()
+Align::alignmentCompletionChanged(ModelId modelId)
 {
     QMutexLocker locker (&m_mutex);
 
-    if (AlignmentModel *amPtr = qobject_cast<AlignmentModel *>(sender())) {
-
-        auto am = ModelById::getAs<AlignmentModel>(amPtr->getId());
-        if (am && am->isReady()) {
-            disconnect(am.get(), SIGNAL(completionChanged()),
-                       this, SLOT(alignmentCompletionChanged()));
-            emit alignmentComplete(am->getId());
-        }
+    auto am = ModelById::getAs<AlignmentModel>(modelId);
+    if (am && am->isReady()) {
+        disconnect(am.get(), SIGNAL(completionChanged(ModelId)),
+                   this, SLOT(alignmentCompletionChanged(ModelId)));
+        emit alignmentComplete(modelId);
     }
 }
 
 bool
-Align::alignModelViaProgram(Document *, ModelId ref, ModelId other,
-                            QString program, QString &error)
+Align::alignModelViaProgram(Document *,
+                            ModelId referenceId,
+                            ModelId otherId,
+                            QString program,
+                            QString &error)
 {
     QMutexLocker locker (&m_mutex);
-
-    auto reference = ModelById::getAs<RangeSummarisableTimeValueModel>(ref);
-    auto rm = ModelById::getAs<RangeSummarisableTimeValueModel>(other);
-    if (!reference || !rm) return false;
-
-    while (!reference->isReady(nullptr) || !rm->isReady(nullptr)) {
-        qApp->processEvents();
-    }
     
     // Run an external program, passing to it paths to the main
     // model's audio file and the new model's audio file. It returns
     // the path in CSV form through stdout.
 
-    auto roref = ModelById::getAs<ReadOnlyWaveFileModel>(ref);
-    auto rorm = ModelById::getAs<ReadOnlyWaveFileModel>(other);
-    if (!roref || !rorm) {
+    auto reference = ModelById::getAs<ReadOnlyWaveFileModel>(referenceId);
+    auto other = ModelById::getAs<ReadOnlyWaveFileModel>(otherId);
+    if (!reference || !other) {
         SVCERR << "ERROR: Align::alignModelViaProgram: Can't align non-read-only models via program (no local filename available)" << endl;
         return false;
     }
+
+    while (!reference->isReady(nullptr) || !other->isReady(nullptr)) {
+        qApp->processEvents();
+    }
     
-    QString refPath = roref->getLocalFilename();
-    QString otherPath = rorm->getLocalFilename();
+    QString refPath = reference->getLocalFilename();
+    QString otherPath = other->getLocalFilename();
 
     if (refPath == "" || otherPath == "") {
         error = "Failed to find local filepath for wave-file model";
         return false;
     }
 
-    auto alignmentModel = std::make_shared<AlignmentModel>(ref, other,
-                                                           ModelId());
-    ModelById::add(alignmentModel);
-    rm->setAlignment(alignmentModel->getId());
+    auto alignmentModel =
+        std::make_shared<AlignmentModel>(referenceId, otherId, ModelId());
+    auto alignmentModelId = ModelById::add(alignmentModel);
+    other->setAlignment(alignmentModelId);
 
     QProcess *process = new QProcess;
     QStringList args;
@@ -413,7 +407,7 @@
     connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),
             this, SLOT(alignmentProgramFinished(int, QProcess::ExitStatus)));
 
-    m_pendingProcesses[process] = alignmentModel->getId();
+    m_pendingProcesses[process] = alignmentModelId;
     process->start(program, args);
 
     bool success = process->waitForStarted();
@@ -424,7 +418,7 @@
         error = "Alignment program could not be started";
         m_pendingProcesses.erase(process);
         //!!! who releases alignmentModel? does this? review
-        rm->setAlignment({});
+        other->setAlignment({});
         delete process;
     }
 
@@ -482,7 +476,8 @@
         
         Model *csvOutput = reader.load();
 
-        SparseTimeValueModel *path = qobject_cast<SparseTimeValueModel *>(csvOutput);
+        SparseTimeValueModel *path =
+            qobject_cast<SparseTimeValueModel *>(csvOutput);
         if (!path) {
             SVCERR << "ERROR: Align::alignmentProgramFinished: Output did not convert to sparse time-value model"
                    << endl;
@@ -504,10 +499,13 @@
         SVCERR << "Align::alignmentProgramFinished: Setting alignment path ("
              << path->getEventCount() << " point(s))" << endl;
 
-        ModelById::add(std::shared_ptr<SparseTimeValueModel>(path));
-        alignmentModel->setPathFrom(path->getId());
+        auto pathId =
+            ModelById::add(std::shared_ptr<SparseTimeValueModel>(path));
+        alignmentModel->setPathFrom(pathId);
 
         emit alignmentComplete(alignmentModelId);
+
+        ModelById::release(pathId);
         
     } else {
         SVCERR << "ERROR: Align::alignmentProgramFinished: Aligner program "
--- a/framework/Align.h	Thu Jul 04 18:04:59 2019 +0100
+++ b/framework/Align.h	Fri Jul 05 15:35:11 2019 +0100
@@ -93,8 +93,8 @@
     void alignmentComplete(ModelId alignmentModel); // an AlignmentModel
 
 private slots:
-    void alignmentCompletionChanged();
-    void tuningDifferenceCompletionChanged();
+    void alignmentCompletionChanged(ModelId);
+    void tuningDifferenceCompletionChanged(ModelId);
     void alignmentProgramFinished(int, QProcess::ExitStatus);
     
 private:
--- a/framework/Document.cpp	Thu Jul 04 18:04:59 2019 +0100
+++ b/framework/Document.cpp	Fri Jul 05 15:35:11 2019 +0100
@@ -86,10 +86,11 @@
         ModelById::release(m);
     }
 
-    auto mainModel = m_mainModel;
+    if (!m_mainModel.isNone()) {
+        ModelById::release(m_mainModel);
+    }
     m_mainModel = {};
-    emit mainModelChanged(m_mainModel);
-    ModelById::release(mainModel);
+    emit mainModelChanged({});
 }
 
 Layer *
@@ -171,9 +172,9 @@
         return nullptr;
     }
 
-    ModelById::add(newModel);
-    addImportedModel(newModel->getId());
-    setModel(newLayer, newModel->getId());
+    auto newModelId = ModelById::add(newModel);
+    addImportedModel(newModelId);
+    setModel(newLayer, newModelId);
 
     return newLayer;
 }
@@ -554,13 +555,16 @@
 
     emit mainModelChanged(m_mainModel);
 
-    // Remove the playable explicitly - the main model's dtor will do
-    // this, but just in case something is still hanging onto a
-    // shared_ptr to the old main model so it doesn't get deleted yet
-    PlayParameterRepository::getInstance()->removePlayable
-        (oldMainModel.untyped);
-    
-    ModelById::release(oldMainModel);
+    if (!oldMainModel.isNone()) {
+
+        // Remove the playable explicitly - the main model's dtor will
+        // do this, but just in case something is still hanging onto a
+        // shared_ptr to the old main model so it doesn't get deleted
+        PlayParameterRepository::getInstance()->removePlayable
+            (oldMainModel.untyped);
+
+        ModelById::release(oldMainModel);
+    }
 }
 
 void
@@ -1122,9 +1126,9 @@
                 << "): is main model, setting alignment to itself" << endl;
         auto alignment = std::make_shared<AlignmentModel>(modelId, modelId,
                                                           ModelId());
-        ModelById::add(alignment);
+        
         //!!! hang on, who tracks alignment models?
-        rm->setAlignment(alignment->getId());
+        rm->setAlignment(ModelById::add(alignment));
         return;
     }
 
@@ -1133,8 +1137,8 @@
         SVDEBUG << "Document::alignModel(" << modelId
                 << "): model write is not complete, deferring"
                 << endl;
-        connect(w.get(), SIGNAL(writeCompleted()),
-                this, SLOT(performDeferredAlignment()));
+        connect(w.get(), SIGNAL(writeCompleted(ModelId)),
+                this, SLOT(performDeferredAlignment(ModelId)));
         return;
     }
 
@@ -1153,16 +1157,8 @@
 }
 
 void
-Document::performDeferredAlignment()
+Document::performDeferredAlignment(ModelId modelId)
 {
-    ModelId modelId;
-    if (Model *m = qobject_cast<Model *>(sender())) {
-        modelId = m->getId();
-    } else {
-        SVDEBUG << "Document::performDeferredAlignment: sender is not a Model" << endl;
-        return;
-    }
-
     SVDEBUG << "Document::performDeferredAlignment: aligning..." << endl;
     alignModel(modelId);
 }
--- a/framework/Document.h	Thu Jul 04 18:04:59 2019 +0100
+++ b/framework/Document.h	Fri Jul 05 15:35:11 2019 +0100
@@ -345,8 +345,7 @@
     void activity(QString);
 
 protected slots:
-//!!!    void aggregateModelInvalidated();
-    void performDeferredAlignment();
+    void performDeferredAlignment(ModelId);
     
 protected:
     void releaseModel(ModelId model);
--- a/framework/MainWindowBase.cpp	Thu Jul 04 18:04:59 2019 +0100
+++ b/framework/MainWindowBase.cpp	Fri Jul 05 15:35:11 2019 +0100
@@ -178,6 +178,7 @@
     
     qRegisterMetaType<sv_frame_t>("sv_frame_t");
     qRegisterMetaType<sv_samplerate_t>("sv_samplerate_t");
+    qRegisterMetaType<ModelId>("ModelId");
 
 #ifdef Q_WS_X11
     XSetErrorHandler(handle_x11_error);
@@ -1303,14 +1304,16 @@
     Layer *layer = pane->getSelectedLayer();
     if (!layer) return;
 
-    auto rm = ModelById::getAs<RegionModel>(layer->getModel());
+    ModelId modelId = layer->getModel();
+    
+    auto rm = ModelById::getAs<RegionModel>(modelId);
     if (rm) {
         Event point(alignedStart,
                     rm->getValueMaximum() + 1,
                     alignedDuration,
                     "");
         ChangeEventsCommand *command = new ChangeEventsCommand
-            (rm->getId().untyped, name);
+            (modelId.untyped, name);
         command->add(point);
         c = command->finish();
     }
@@ -1320,7 +1323,7 @@
         return;
     }
 
-    auto nm = ModelById::getAs<NoteModel>(layer->getModel());
+    auto nm = ModelById::getAs<NoteModel>(modelId);
     if (nm) {
         Event point(alignedStart,
                     nm->getValueMinimum(),
@@ -1328,7 +1331,7 @@
                     1.f,
                     "");
         ChangeEventsCommand *command = new ChangeEventsCommand
-            (nm->getId().untyped, name);
+            (modelId.untyped, name);
         command->add(point);
         c = command->finish();
     }
@@ -1574,10 +1577,8 @@
         }
     }
 
-    ModelById::add(newModel);
-
-    return addOpenedAudioModel(source, newModel->getId(),
-                               mode, templateName, true);
+    auto newModelId = ModelById::add(newModel);
+    return addOpenedAudioModel(source, newModelId, mode, templateName, true);
 }
 
 MainWindowBase::FileOpenStatus
@@ -1953,8 +1954,8 @@
 
                 emit activity(tr("Import MIDI file \"%1\"").arg(source.getLocation()));
 
-                ModelId modelId = newModelPtr->getId();
-                ModelById::add(std::shared_ptr<Model>(newModelPtr));
+                ModelId modelId = 
+                    ModelById::add(std::shared_ptr<Model>(newModelPtr));
                 
                 Layer *newLayer = m_document->createImportedLayer(modelId);
 
@@ -2370,11 +2371,11 @@
         return FileOpenFailed;
     }
 
-    std::vector<Model *> modelPtrs = importer.getDataModels(&dialog);
+    std::vector<ModelId> modelIds = importer.getDataModels(&dialog);
 
     dialog.setMessage(tr("Importing from RDF..."));
 
-    if (modelPtrs.empty()) {
+    if (modelIds.empty()) {
         QMessageBox::critical
             (this, tr("Failed to import RDF"),
              tr("<b>Failed to import RDF</b><p>No suitable data models found for import from RDF document at \"%1\"</p>").arg(source.getLocation()));
@@ -2382,13 +2383,6 @@
     }
 
     emit activity(tr("Import RDF document \"%1\"").arg(source.getLocation()));
-
-    std::set<ModelId> modelIds;
-
-    for (Model *modelPtr: modelPtrs) {
-        modelIds.insert(modelPtr->getId());
-        ModelById::add(std::shared_ptr<Model>(modelPtr));
-    }
     
     std::set<ModelId> added;
 
@@ -2688,17 +2682,15 @@
 
     connect(m_document, SIGNAL(modelAdded(ModelId )),
             this, SLOT(modelAdded(ModelId )));
-    connect(m_document, SIGNAL(mainModelChanged(WaveFileModel *)),
-            this, SLOT(mainModelChanged(WaveFileModel *)));
-    connect(m_document, SIGNAL(modelAboutToBeDeleted(ModelId )),
-            this, SLOT(modelAboutToBeDeleted(ModelId )));
+    connect(m_document, SIGNAL(mainModelChanged(ModelId)),
+            this, SLOT(mainModelChanged(ModelId)));
 
     connect(m_document, SIGNAL(modelGenerationFailed(QString, QString)),
             this, SLOT(modelGenerationFailed(QString, QString)));
     connect(m_document, SIGNAL(modelRegenerationWarning(QString, QString, QString)),
             this, SLOT(modelRegenerationWarning(QString, QString, QString)));
-    connect(m_document, SIGNAL(alignmentComplete(AlignmentModel *)),
-            this, SLOT(alignmentComplete(AlignmentModel *)));
+    connect(m_document, SIGNAL(alignmentComplete(ModelId)),
+            this, SLOT(alignmentComplete(ModelId)));
     connect(m_document, SIGNAL(alignmentFailed(QString)),
             this, SLOT(alignmentFailed(QString)));
 
@@ -3277,8 +3269,7 @@
 
     QString location = modelPtr->getLocation();
     
-    auto modelId = modelPtr->getId();
-    ModelById::add(std::shared_ptr<Model>(modelPtr));
+    auto modelId = ModelById::add(std::shared_ptr<Model>(modelPtr));
 
     if (m_audioRecordMode == RecordReplaceSession || !getMainModel()) {
 
--- a/framework/SVFileReader.cpp	Thu Jul 04 18:04:59 2019 +0100
+++ b/framework/SVFileReader.cpp	Fri Jul 05 15:35:11 2019 +0100
@@ -564,8 +564,7 @@
 
         model->setObjectName(name);
 
-        ModelId modelId = model->getId();
-        ModelById::add(std::shared_ptr<Model>(model));
+        ModelId modelId = ModelById::add(std::shared_ptr<Model>(model));
         m_models[id] = modelId;
         
         if (isMainModel) {