]> Creatis software - clitk.git/commitdiff
Bug #650 crash with --fusion and --overlay
authorVivien Delmon <vivien.delmon@creatis.insa-lyon.fr>
Tue, 29 Nov 2011 14:23:31 +0000 (15:23 +0100)
committerVivien Delmon <vivien.delmon@creatis.insa-lyon.fr>
Tue, 29 Nov 2011 14:23:31 +0000 (15:23 +0100)
- Check if the file exists before opening it

vv/vvMainWindow.cxx

index 83fb2f4dc67476104f74ca9175d46ccb87254a7b..5bdc9780ccd73c693727a1b1dbf1872aa3d70ca7 100644 (file)
@@ -1828,70 +1828,74 @@ void vvMainWindow::SelectOverlayImage()
 //------------------------------------------------------------------------------
 void vvMainWindow::AddOverlayImage(int index, QString file)
 {
-
-  mInputPathName = itksys::SystemTools::GetFilenamePath(file.toStdString()).c_str();
-  itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(
-                                       file.toStdString().c_str(), itk::ImageIOFactory::ReadMode);
-  reader->SetFileName(file.toStdString().c_str());
-  reader->ReadImageInformation();
-  std::string component = reader->GetComponentTypeAsString(reader->GetComponentType());
-  int dimension = reader->GetNumberOfDimensions();
-  QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
-  vvProgressDialog progress("Opening " + file.toStdString());
-  qApp->processEvents();
-
-  std::string filename = itksys::SystemTools::GetFilenameWithoutExtension(file.toStdString()).c_str();
-  if (mSlicerManagers[index]->SetOverlay(file.toStdString(),dimension, component)) {
-    //create an item in the tree with good settings
-    QTreeWidgetItem *item = new QTreeWidgetItem();
-    item->setData(0,Qt::UserRole,file.toStdString().c_str());
-    item->setData(1,Qt::UserRole,tr("overlay"));
-    QFileInfo fileinfo(file); //Do not show the path
-    item->setData(COLUMN_IMAGE_NAME,Qt::DisplayRole,fileinfo.fileName());
-    item->setToolTip(COLUMN_IMAGE_NAME, mSlicerManagers[index]->GetListOfAbsoluteFilePathInOneString("overlay").c_str());
+  if (QFile::exists(file))
+  {
+    mInputPathName = itksys::SystemTools::GetFilenamePath(file.toStdString()).c_str();
+    itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(
+        file.toStdString().c_str(), itk::ImageIOFactory::ReadMode);
+    reader->SetFileName(file.toStdString().c_str());
+    reader->ReadImageInformation();
+    std::string component = reader->GetComponentTypeAsString(reader->GetComponentType());
+    int dimension = reader->GetNumberOfDimensions();
+    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
+    vvProgressDialog progress("Opening " + file.toStdString());
     qApp->processEvents();
 
-    for (int j = 1; j <= 4; j++) {
-      item->setData(j,Qt::CheckStateRole,DataTree->topLevelItem(index)->data(j,Qt::CheckStateRole));
-    }
+    std::string filename = itksys::SystemTools::GetFilenameWithoutExtension(file.toStdString()).c_str();
+    if (mSlicerManagers[index]->SetOverlay(file.toStdString(),dimension, component)) {
+      //create an item in the tree with good settings
+      QTreeWidgetItem *item = new QTreeWidgetItem();
+      item->setData(0,Qt::UserRole,file.toStdString().c_str());
+      item->setData(1,Qt::UserRole,tr("overlay"));
+      QFileInfo fileinfo(file); //Do not show the path
+      item->setData(COLUMN_IMAGE_NAME,Qt::DisplayRole,fileinfo.fileName());
+      item->setToolTip(COLUMN_IMAGE_NAME, mSlicerManagers[index]->GetListOfAbsoluteFilePathInOneString("overlay").c_str());
+      qApp->processEvents();
 
-    //Create the buttons for reload and close
-    qApp->processEvents();
-    QTreePushButton* cButton = new QTreePushButton;
-    cButton->setItem(item);
-    cButton->setColumn(COLUMN_CLOSE_IMAGE);
-    cButton->setToolTip(tr("close image"));
-    cButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/exit.png")));
-    connect(cButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
-            this,SLOT(CloseImage(QTreeWidgetItem*, int)));
+      for (int j = 1; j <= 4; j++) {
+        item->setData(j,Qt::CheckStateRole,DataTree->topLevelItem(index)->data(j,Qt::CheckStateRole));
+      }
 
-    QTreePushButton* rButton = new QTreePushButton;
-    rButton->setItem(item);
-    rButton->setColumn(COLUMN_RELOAD_IMAGE);
-    rButton->setToolTip(tr("reload image"));
-    rButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/rotateright.png")));
-    connect(rButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
-            this,SLOT(ReloadImage(QTreeWidgetItem*, int)));
+      //Create the buttons for reload and close
+      qApp->processEvents();
+      QTreePushButton* cButton = new QTreePushButton;
+      cButton->setItem(item);
+      cButton->setColumn(COLUMN_CLOSE_IMAGE);
+      cButton->setToolTip(tr("close image"));
+      cButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/exit.png")));
+      connect(cButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
+          this,SLOT(CloseImage(QTreeWidgetItem*, int)));
+
+      QTreePushButton* rButton = new QTreePushButton;
+      rButton->setItem(item);
+      rButton->setColumn(COLUMN_RELOAD_IMAGE);
+      rButton->setToolTip(tr("reload image"));
+      rButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/rotateright.png")));
+      connect(rButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
+          this,SLOT(ReloadImage(QTreeWidgetItem*, int)));
 
-    DataTree->topLevelItem(index)->setExpanded(1);
-    DataTree->topLevelItem(index)->addChild(item);
-    DataTree->setItemWidget(item, COLUMN_CLOSE_IMAGE, cButton);
-    DataTree->setItemWidget(item, COLUMN_RELOAD_IMAGE, rButton);
+      DataTree->topLevelItem(index)->setExpanded(1);
+      DataTree->topLevelItem(index)->addChild(item);
+      DataTree->setItemWidget(item, COLUMN_CLOSE_IMAGE, cButton);
+      DataTree->setItemWidget(item, COLUMN_RELOAD_IMAGE, rButton);
 
-    //set the id of the image
-    QString id = DataTree->topLevelItem(index)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString();
-    item->setData(COLUMN_IMAGE_NAME,Qt::UserRole,id.toStdString().c_str());
-    UpdateTree();
-    qApp->processEvents();
-    ImageInfoChanged();
-    QApplication::restoreOverrideCursor();
-  } else {
-    QApplication::restoreOverrideCursor();
-    QString error = "Cannot import the new image.\n";
-    error += mSlicerManagers[index]->GetLastError().c_str();
-    QMessageBox::information(this,tr("Problem reading image !"),error);
+      //set the id of the image
+      QString id = DataTree->topLevelItem(index)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString();
+      item->setData(COLUMN_IMAGE_NAME,Qt::UserRole,id.toStdString().c_str());
+      UpdateTree();
+      qApp->processEvents();
+      ImageInfoChanged();
+      QApplication::restoreOverrideCursor();
+    } else {
+      QApplication::restoreOverrideCursor();
+      QString error = "Cannot import the new image.\n";
+      error += mSlicerManagers[index]->GetLastError().c_str();
+      QMessageBox::information(this,tr("Problem reading image !"),error);
+    }
+    WindowLevelChanged();
   }
-  WindowLevelChanged();
+  else
+    QMessageBox::information(this,tr("Problem reading Overlay !"),"File doesn't exist!");
 }
 //------------------------------------------------------------------------------
 
@@ -1959,73 +1963,78 @@ void vvMainWindow::ResetTransformationToIdentity()
 //------------------------------------------------------------------------------
 void vvMainWindow::AddFusionImage(int index, QString file)
 {
-  mInputPathName = itksys::SystemTools::GetFilenamePath(file.toStdString()).c_str();
-  itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(
-                                       file.toStdString().c_str(), itk::ImageIOFactory::ReadMode);
-  reader->SetFileName(file.toStdString().c_str());
-  reader->ReadImageInformation();
-  std::string component = reader->GetComponentTypeAsString(reader->GetComponentType());
-  if (reader) {
-    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
-    vvProgressDialog progress("Opening fusion");
-    qApp->processEvents();
-
-    std::string filename = itksys::SystemTools::GetFilenameWithoutExtension(file.toStdString()).c_str();
-    if (mSlicerManagers[index]->SetFusion(file.toStdString(),
-                                          reader->GetNumberOfDimensions(), component)) {
-      //create an item in the tree with good settings
-      QTreeWidgetItem *item = new QTreeWidgetItem();
-      item->setData(0,Qt::UserRole,file.toStdString().c_str());
-      item->setData(1,Qt::UserRole,tr("fusion"));
-      QFileInfo fileinfo(filename.c_str()); //Do not show the path
-      item->setData(COLUMN_IMAGE_NAME,Qt::DisplayRole,fileinfo.fileName());
-      item->setToolTip(COLUMN_IMAGE_NAME, mSlicerManagers[index]->GetListOfAbsoluteFilePathInOneString("fusion").c_str());
+  if (QFile::exists(file))
+  {
+    mInputPathName = itksys::SystemTools::GetFilenamePath(file.toStdString()).c_str();
+    itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(
+        file.toStdString().c_str(), itk::ImageIOFactory::ReadMode);
+    reader->SetFileName(file.toStdString().c_str());
+    reader->ReadImageInformation();
+    std::string component = reader->GetComponentTypeAsString(reader->GetComponentType());
+    if (reader) {
+      QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
+      vvProgressDialog progress("Opening fusion");
       qApp->processEvents();
 
-      for (int j = 1; j <= 4; j++) {
-        item->setData(j,Qt::CheckStateRole,DataTree->topLevelItem(index)->data(j,Qt::CheckStateRole));
-      }
+      std::string filename = itksys::SystemTools::GetFilenameWithoutExtension(file.toStdString()).c_str();
+      if (mSlicerManagers[index]->SetFusion(file.toStdString(),
+            reader->GetNumberOfDimensions(), component)) {
+        //create an item in the tree with good settings
+        QTreeWidgetItem *item = new QTreeWidgetItem();
+        item->setData(0,Qt::UserRole,file.toStdString().c_str());
+        item->setData(1,Qt::UserRole,tr("fusion"));
+        QFileInfo fileinfo(filename.c_str()); //Do not show the path
+        item->setData(COLUMN_IMAGE_NAME,Qt::DisplayRole,fileinfo.fileName());
+        item->setToolTip(COLUMN_IMAGE_NAME, mSlicerManagers[index]->GetListOfAbsoluteFilePathInOneString("fusion").c_str());
+        qApp->processEvents();
 
-      //Create the buttons for reload and close
-      qApp->processEvents();
-      QTreePushButton* cButton = new QTreePushButton;
-      cButton->setItem(item);
-      cButton->setColumn(COLUMN_CLOSE_IMAGE);
-      cButton->setToolTip(tr("close image"));
-      cButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/exit.png")));
-      connect(cButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
-              this,SLOT(CloseImage(QTreeWidgetItem*, int)));
+        for (int j = 1; j <= 4; j++) {
+          item->setData(j,Qt::CheckStateRole,DataTree->topLevelItem(index)->data(j,Qt::CheckStateRole));
+        }
 
-      QTreePushButton* rButton = new QTreePushButton;
-      rButton->setItem(item);
-      rButton->setColumn(COLUMN_RELOAD_IMAGE);
-      rButton->setToolTip(tr("reload image"));
-      rButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/rotateright.png")));
-      connect(rButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
-              this,SLOT(ReloadImage(QTreeWidgetItem*, int)));
+        //Create the buttons for reload and close
+        qApp->processEvents();
+        QTreePushButton* cButton = new QTreePushButton;
+        cButton->setItem(item);
+        cButton->setColumn(COLUMN_CLOSE_IMAGE);
+        cButton->setToolTip(tr("close image"));
+        cButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/exit.png")));
+        connect(cButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
+            this,SLOT(CloseImage(QTreeWidgetItem*, int)));
 
-      DataTree->topLevelItem(index)->setExpanded(1);
-      DataTree->topLevelItem(index)->addChild(item);
-      DataTree->setItemWidget(item, COLUMN_CLOSE_IMAGE, cButton);
-      DataTree->setItemWidget(item, COLUMN_RELOAD_IMAGE, rButton);
+        QTreePushButton* rButton = new QTreePushButton;
+        rButton->setItem(item);
+        rButton->setColumn(COLUMN_RELOAD_IMAGE);
+        rButton->setToolTip(tr("reload image"));
+        rButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/rotateright.png")));
+        connect(rButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
+            this,SLOT(ReloadImage(QTreeWidgetItem*, int)));
 
-      //set the id of the image
-      QString id = DataTree->topLevelItem(index)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString();
-      item->setData(COLUMN_IMAGE_NAME,Qt::UserRole,id.toStdString().c_str());
-      UpdateTree();
-      qApp->processEvents();
-      ImageInfoChanged();
-      QApplication::restoreOverrideCursor();
+        DataTree->topLevelItem(index)->setExpanded(1);
+        DataTree->topLevelItem(index)->addChild(item);
+        DataTree->setItemWidget(item, COLUMN_CLOSE_IMAGE, cButton);
+        DataTree->setItemWidget(item, COLUMN_RELOAD_IMAGE, rButton);
+
+        //set the id of the image
+        QString id = DataTree->topLevelItem(index)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString();
+        item->setData(COLUMN_IMAGE_NAME,Qt::UserRole,id.toStdString().c_str());
+        UpdateTree();
+        qApp->processEvents();
+        ImageInfoChanged();
+        QApplication::restoreOverrideCursor();
+      } else {
+        QApplication::restoreOverrideCursor();
+        QString error = "Cannot import the new image.\n";
+        error += mSlicerManagers[index]->GetLastError().c_str();
+        QMessageBox::information(this,tr("Problem reading image !"),error);
+      }
     } else {
-      QApplication::restoreOverrideCursor();
       QString error = "Cannot import the new image.\n";
-      error += mSlicerManagers[index]->GetLastError().c_str();
       QMessageBox::information(this,tr("Problem reading image !"),error);
     }
-  } else {
-    QString error = "Cannot import the new image.\n";
-    QMessageBox::information(this,tr("Problem reading image !"),error);
   }
+  else
+    QMessageBox::information(this,tr("Problem reading Fusion !"),"File doesn't exist!");
 }
 //------------------------------------------------------------------------------