From 706d7e2dc69e12b3823cfae2f3be3f903e4d3c80 Mon Sep 17 00:00:00 2001 From: tbaudier Date: Mon, 14 Jan 2019 13:21:02 +0100 Subject: [PATCH] Add a watcher to file vv reload automatically a file if it changed on the disk. The watcher is on the mhd file, and when it send a signal: we find the item in the data tree corresponding to that file (in the data tree the items are written with an extra 0 at the end of the pathname) We reload all these items stored in a vector --- vv/vvMainWindow.cxx | 30 ++++++++++++++++++++++++++++++ vv/vvMainWindow.h | 2 ++ 2 files changed, 32 insertions(+) diff --git a/vv/vvMainWindow.cxx b/vv/vvMainWindow.cxx index 989027b..2b41492 100644 --- a/vv/vvMainWindow.cxx +++ b/vv/vvMainWindow.cxx @@ -25,6 +25,7 @@ It is distributed under dual licence #include #include #include +#include // VV include #include "vvMainWindow.h" @@ -982,6 +983,12 @@ void vvMainWindow::LoadImages(std::vector files, vvImageReader::Loa linkPanel->addImage(imageManager->GetFileName(), id.toStdString()); + //Create a watcher to see if the image file is modified. In such a case, reload it automatically + QFileSystemWatcher* watcher = new QFileSystemWatcher; + watcher->addPath(files[i].c_str()); + connect(watcher, SIGNAL(fileChanged(const QString&)), this, SLOT(SlotFileChanged(const QString&))); + + connect(mSlicerManagers.back(), SIGNAL(currentImageChanged(std::string)), this,SLOT(CurrentImageChanged(std::string))); connect(mSlicerManagers.back(), SIGNAL(currentPickedImageChanged(std::string)), @@ -1441,6 +1448,29 @@ QString vvMainWindow::GetVectorIntAsString(std::vector vectorInt) } //------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ +void vvMainWindow::SlotFileChanged(const QString& pathname) +{ + std::vector items = GetItemFromPathname(pathname); + for (unsigned int i=0; i< items.size(); ++i) + ReloadImage(items[i], 0); +} +//------------------------------------------------------------------------------ + +//------------------------------------------------------------------------------ +std::vector vvMainWindow::GetItemFromPathname(const QString& pathname) +{ + std::vector items; + for (int i = 0; i < DataTree->topLevelItemCount(); ++i) { + QString tempItemPathname(DataTree->topLevelItem(i)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString()); + tempItemPathname = tempItemPathname.left(tempItemPathname.length() - 1); + if (tempItemPathname == pathname) + items.push_back(DataTree->topLevelItem(i)); + } + return items; +} +//------------------------------------------------------------------------------ + //------------------------------------------------------------------------------ //this actually returns the SlicerManager index! int vvMainWindow::GetSlicerIndexFromItem(QTreeWidgetItem* item) diff --git a/vv/vvMainWindow.h b/vv/vvMainWindow.h index 07f38e5..769865e 100644 --- a/vv/vvMainWindow.h +++ b/vv/vvMainWindow.h @@ -192,6 +192,7 @@ public slots: void UpdateRenderWindows(); void UpdateMemoryUsage(); void show(); + void SlotFileChanged(const QString& pathname); protected: @@ -230,6 +231,7 @@ private: QString GetSizeInBytes(unsigned long size); QString GetVectorDoubleAsString(std::vector vectorDouble); QString GetVectorIntAsString(std::vector vectorInt); + std::vector GetItemFromPathname(const QString& pathname); int GetSlicerIndexFromItem(QTreeWidgetItem* item); //this actually returns the SlicerManager index TODO: rename it to GetSlicerManagerIndexFromItem QTreeWidgetItem* GetItemFromSlicerManager(vvSlicerManager* sm); #if (VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION >= 1) || VTK_MAJOR_VERSION >= 9 -- 2.47.1