From c5fb4c493b81d5b5fcf5ca65269e3c6b7dbaac74 Mon Sep 17 00:00:00 2001
From: Olivier Langella <olivier.langella@u-psud.fr>
Date: Thu, 24 Sep 2020 15:19:10 +0200
Subject: [PATCH] findMsRunFile interface has changed to fix issue #31

---
 .../identificationdatasource.cpp              |  4 +-
 src/core/msrun.cpp                            | 50 +++++++++++++------
 src/core/msrun.h                              |  5 +-
 src/gui/workerthread.cpp                      |  1 +
 src/gui/xic_view/xicwindow.cpp                |  5 +-
 5 files changed, 45 insertions(+), 20 deletions(-)

diff --git a/src/core/identification_sources/identificationdatasource.cpp b/src/core/identification_sources/identificationdatasource.cpp
index 188c58c9..49df2dfb 100644
--- a/src/core/identification_sources/identificationdatasource.cpp
+++ b/src/core/identification_sources/identificationdatasource.cpp
@@ -222,13 +222,15 @@ IdentificationDataSource::getMassSpectrumCstSPtr(unsigned int scan_number) const
 
   if(msrun_reader_sp == nullptr)
     {
-      if(_ms_run_sp.get()->findMsRunFile() == false)
+      msrun_reader_sp = _ms_run_sp.get()->findMsRunFile();
+      if(msrun_reader_sp == nullptr)
         {
           throw pappso::ExceptionNotFound(
             QObject::tr("mz data \"%1\" file not found")
               .arg(_ms_run_sp.get()->getFileName()));
         }
     }
+  qDebug();
   msrun_reader_sp = _ms_run_sp.get()->getMsRunReaderSPtr();
   if(msrun_reader_sp == nullptr)
     {
diff --git a/src/core/msrun.cpp b/src/core/msrun.cpp
index c7bb96ac..6bb3f262 100644
--- a/src/core/msrun.cpp
+++ b/src/core/msrun.cpp
@@ -132,6 +132,10 @@ MsRun::getMsRunReaderSPtr()
             }
         }
     }
+  else
+    {
+      qDebug();
+    }
 
   return _msrun_reader_sp;
 }
@@ -167,23 +171,29 @@ MsRun::getMsRunStatistics(MsRunStatistics param) const
     }
 }
 
-bool
+pappso::MsRunReaderSPtr
 MsRun::findMsRunFile()
 {
   // first look at the file path :)
-  _msrun_reader_sp = getMsRunReaderSPtr();
-  if(_msrun_reader_sp != nullptr)
+  qDebug();
+  pappso::MsRunReaderSPtr msrun_reader_sp = getMsRunReaderSPtr();
+  if(msrun_reader_sp != nullptr)
     {
       QFileInfo real_file(this->getFileName());
       if(real_file.exists())
         {
-          return true;
+
+          qDebug();
+          freeMsRunReaderSp();
+          qDebug();
+          return msrun_reader_sp;
         }
       else
         {
-          _msrun_reader_sp = nullptr;
+          msrun_reader_sp = nullptr;
         }
     }
+  qDebug();
   QFileInfo file_original(this->getFileName());
   QString basename     = file_original.baseName();
   QString onlyfilename = file_original.fileName();
@@ -207,7 +217,7 @@ MsRun::findMsRunFile()
               this->setFileName(file.absoluteFilePath());
               try
                 {
-                  if(_msrun_reader_sp == nullptr)
+                  if(msrun_reader_sp == nullptr)
                     {
 
                       pappso::MsFileAccessor accessor(getFileName(), "");
@@ -217,14 +227,15 @@ MsRun::findMsRunFile()
                       qDebug()
                         << " runid=" << getRunId() << " xmlid=" << getXmlId();
 
-                      _msrun_reader_sp = accessor.getMsRunReaderSPtrByRunId(
+                      msrun_reader_sp = accessor.getMsRunReaderSPtrByRunId(
                         getRunId(), getXmlId());
-                      return true;
+
+                      freeMsRunReaderSp();
+                      return msrun_reader_sp;
                     }
                 }
               catch(pappso::PappsoException &error)
                 {
-                  _msrun_reader_sp = nullptr;
                 }
             }
           if((file.fileName().contains(onlyfilename)) ||
@@ -235,7 +246,7 @@ MsRun::findMsRunFile()
                 {
                   this->setFileName(file.absoluteFilePath());
 
-                  if(_msrun_reader_sp == nullptr)
+                  if(msrun_reader_sp == nullptr)
                     {
 
                       pappso::MsFileAccessor accessor(getFileName(), "");
@@ -245,21 +256,24 @@ MsRun::findMsRunFile()
                         pappso::FileReaderType::tims_ms2);
                       qDebug()
                         << " runid=" << getRunId() << " xmlid=" << getXmlId();
-                      _msrun_reader_sp = accessor.getMsRunReaderSPtrByRunId(
+                      msrun_reader_sp = accessor.getMsRunReaderSPtrByRunId(
                         getRunId(), getXmlId());
-                      return true;
+
+                      freeMsRunReaderSp();
+                      return msrun_reader_sp;
                     }
                 }
               catch(pappso::PappsoException &error)
                 {
-                  _msrun_reader_sp = nullptr;
+                  msrun_reader_sp = nullptr;
                 }
             }
         }
     }
   this->setFileName(file_original.absoluteFilePath());
   freeMsRunReaderSp();
-  return false;
+  qDebug();
+  return msrun_reader_sp;
 }
 
 void
@@ -388,9 +402,12 @@ MsRun::getMsRunXicExtractorInterfaceSp()
     {
       return _xic_extractor_sp;
     }
-  if(this->findMsRunFile())
+  qDebug();
+  _msrun_reader_sp = this->findMsRunFile();
+  if(_msrun_reader_sp != nullptr)
     {
 
+      qDebug();
       pappso::MsRunXicExtractorFactory::getInstance().setTmpDir(
         QDir::tempPath());
 
@@ -410,7 +427,10 @@ MsRun::getMsRunXicExtractorInterfaceSp()
         }
       //_xic_extractor_sp = pappso::MsRunXicExtractorFactory::getInstance()
       //                      .buildMsRunXicExtractorDiskBufferSp(*this);
+
+      qDebug();
     }
+  freeMsRunReaderSp();
   return _xic_extractor_sp;
 }
 
diff --git a/src/core/msrun.h b/src/core/msrun.h
index 031c1e28..b42820b4 100644
--- a/src/core/msrun.h
+++ b/src/core/msrun.h
@@ -73,7 +73,9 @@ class MsRun : public pappso::MsRunId
   virtual const std::map<MsRunStatistics, QVariant> &
   getMsRunStatisticsMap() const;
 
-  bool findMsRunFile();
+  /** \brief find the msRun file and return an msrunreader shared pointer on it
+   */
+  pappso::MsRunReaderSPtr findMsRunFile();
 
   pappso::MsRunReaderSPtr &getMsRunReaderSPtr();
 
@@ -113,3 +115,4 @@ class MsRun : public pappso::MsRunId
 
 
 Q_DECLARE_METATYPE(MsRunSp)
+
diff --git a/src/gui/workerthread.cpp b/src/gui/workerthread.cpp
index 18174637..35c9638f 100644
--- a/src/gui/workerthread.cpp
+++ b/src/gui/workerthread.cpp
@@ -909,6 +909,7 @@ WorkerThread::doCheckMsrunFilePath(ProjectSp msp_project)
               // is_ok = false;
               break;
             }
+          msrun_sp->freeMsRunReaderSp();
           i++;
         }
     }
diff --git a/src/gui/xic_view/xicwindow.cpp b/src/gui/xic_view/xicwindow.cpp
index 6db88772..06373f04 100644
--- a/src/gui/xic_view/xicwindow.cpp
+++ b/src/gui/xic_view/xicwindow.cpp
@@ -140,10 +140,9 @@ XicWindow::getXicExtractionMethod() const
 void
 XicWindow::xicPrecisionChanged(pappso::PrecisionPtr precision [[maybe_unused]])
 {
-  qDebug() << "XicWindow::xicPrecisionChanged begin";
-  qDebug() << "XicWindow::xicPrecisionChanged emit";
+  qDebug();
   emit reExtractXicNeeded();
-  qDebug() << "XicWindow::xicPrecisionChanged end";
+  qDebug();
 }
 
 void
-- 
GitLab