]> Creatis software - clitk.git/blobdiff - vv/vvToolStructureSetManager.cxx
Debug RTStruct conversion with empty struc
[clitk.git] / vv / vvToolStructureSetManager.cxx
index 8c008adc18803f3faacefad4a9b8dff9ad8a6c89..a169c087cfcea1538a77329cbb0410cb4b9c7ce4 100644 (file)
@@ -3,7 +3,7 @@
 
   Authors belong to:
   - University of LYON              http://www.universite-lyon.fr/
-  - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
 
   This software is distributed WITHOUT ANY WARRANTY; without even
@@ -14,7 +14,7 @@
 
   - BSD        See included LICENSE.txt file
   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
-  ======================================================================-====*/
+  ===========================================================================**/
 
 // vv
 #include "vvToolStructureSetManager.h"
@@ -27,7 +27,8 @@
 #include <QFileDialog>
 #include <QMessageBox>
 #include <QColorDialog>
-
+#include <QAbstractEventDispatcher>
 // vtk
 #include <vtkLookupTable.h>
 #include <vtkRenderWindow.h>
@@ -69,7 +70,7 @@ vvToolStructureSetManager::vvToolStructureSetManager(vvMainWindowBase * parent,
   mIsAllVisibleEnabled = false;
   mNumberOfVisibleROI = 0;
   mNumberOfVisibleContourROI = 0;
-  mDefaultLUTColor = vtkLookupTable::New();
+  mDefaultLUTColor = vtkSmartPointer<vtkLookupTable>::New();
   for(int i=0; i<mDefaultLUTColor->GetNumberOfTableValues(); i++) {
     double r = (rand()/(RAND_MAX+1.0));
     double v = (rand()/(RAND_MAX+1.0));
@@ -100,19 +101,14 @@ vvToolStructureSetManager::vvToolStructureSetManager(vvMainWindowBase * parent,
 vvToolStructureSetManager::~vvToolStructureSetManager()
 {
   m_NumberOfTool--;
-  mStructureSetActorsList.clear();
-  mMapROIToTreeWidget.clear();
-
-  /*
-  for(uint i=0; i<mStructureSetsList[0]->GetListOfROI().size();i++) {
-    DD(i);
-    DD(mStructureSetsList[0]->GetROI(i)->GetImage()->GetReferenceCount());
-    //    mStructureSetsList[0]->GetROI(i)->GetImage()->Delete();
-  }
-  */
+  
+  // clearing the list at this point avoids
+  // segfaulting due to events being dispatched
+  // after object destruction
+  mTreeWidgetList.clear();
+  mTree->clearSelection();
 
-  mStructureSetsList.clear();
-  mOpenedBinaryImage.clear();
+  //std::cout << "vvToolStructureSetManager::~vvToolStructureSetManager()" << std::endl;
 }
 //------------------------------------------------------------------------------
 
@@ -120,11 +116,11 @@ vvToolStructureSetManager::~vvToolStructureSetManager()
 //------------------------------------------------------------------------------
 // STATIC
 void vvToolStructureSetManager::Initialize() {
-  SetToolName("ROIManager");
+  SetToolName("ROIManager_OLD");
   SetToolMenuName("Display ROI (binary image)");
   SetToolIconFilename(":/common/icons/tool-roi.png");
   SetToolTip("Display ROI from a binary image.");
-  SetToolExperimental(false);
+  SetToolExperimental(true);
 }
 //------------------------------------------------------------------------------
 
@@ -132,6 +128,8 @@ void vvToolStructureSetManager::Initialize() {
 //------------------------------------------------------------------------------
 void vvToolStructureSetManager::InputIsSelected(vvSlicerManager *m)
 {
+  //std::cout << "vvToolStructureSetManager::InputIsSelected()" << std::endl;
+  
   //int mTabNumber = parent->GetTab()->addTab(this, "");
   //  this->setFixedWidth(120);
   //this->setPreferedHeight(441);  
@@ -139,6 +137,7 @@ void vvToolStructureSetManager::InputIsSelected(vvSlicerManager *m)
   if (mCurrentImage->GetNumberOfDimensions() != 3) {
     QMessageBox::information(this,tr("Sorry only 3D yet"), tr("Sorry only 3D yet"));
     close();
+    return;
   }
   // Hide selector
   HideInputSelector(); // splitter
@@ -160,6 +159,7 @@ void vvToolStructureSetManager::InputIsSelected(vvSlicerManager *m)
   connect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));  
   connect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
   connect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
+  connect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
   connect(mReloadButton, SIGNAL(clicked()), this, SLOT(ReloadCurrentROI()));
   connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
   connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
@@ -185,9 +185,12 @@ void vvToolStructureSetManager::Open(int type) {
 
 //------------------------------------------------------------------------------
 void vvToolStructureSetManager::AddRoiInTreeWidget(clitk::DicomRT_ROI * roi, QTreeWidget * ww) {
-  QTreeWidgetItem * w = new QTreeWidgetItem(ww);
+  mTreeWidgetList.push_back(QSharedPointer<QTreeWidgetItem>(new QTreeWidgetItem(ww)));
+  QTreeWidgetItem * w = mTreeWidgetList.back().data();
   w->setText(0, QString("%1").arg(roi->GetROINumber()));
   w->setText(1, QString("%1").arg(roi->GetName().c_str()));
+  vvROIActor * actor = mStructureSetActorsList[0]->GetROIActor(roi->GetROINumber());  
+  w->setText(3, QString("%1").arg(actor->GetDepth()));  
   QBrush brush(QColor(roi->GetDisplayColor()[0]*255, roi->GetDisplayColor()[1]*255, roi->GetDisplayColor()[2]*255));
   brush.setStyle(Qt::SolidPattern);
   w->setBackground(2, brush);
@@ -202,11 +205,19 @@ void vvToolStructureSetManager::AddRoiInTreeWidget(clitk::DicomRT_ROI * roi, QTr
 //------------------------------------------------------------------------------
 void vvToolStructureSetManager::UpdateStructureSetInTreeWidget(int index, clitk::DicomRT_StructureSet * s) {
   // Insert ROI
-  const std::vector<clitk::DicomRT_ROI*> & rois = s->GetListOfROI();
+  /*
+  const std::vector<clitk::DicomRT_ROI::Pointer> & rois = s->GetListOfROI();
   for(unsigned int i=0; i<rois.size(); i++) {
     if (mMapROIToTreeWidget.find(rois[i]) == mMapROIToTreeWidget.end())
       AddRoiInTreeWidget(rois[i], mTree); // replace mTree with ss if several SS
   }
+  */
+  clitk::DicomRT_StructureSet::ROIConstIteratorType iter;
+  for(iter = s->GetROIs().begin(); iter != s->GetROIs().end(); iter++) {
+    clitk::DicomRT_ROI::Pointer roi = iter->second;
+    if (mMapROIToTreeWidget.find(roi) == mMapROIToTreeWidget.end())
+      AddRoiInTreeWidget(roi, mTree); // replace mTree with ss if several SS
+  }
 }
 //------------------------------------------------------------------------------
 
@@ -215,7 +226,7 @@ void vvToolStructureSetManager::UpdateStructureSetInTreeWidget(int index, clitk:
 int vvToolStructureSetManager::AddStructureSet(clitk::DicomRT_StructureSet * mStructureSet) {
   // Create actor for this SS
 
-  vvStructureSetActor * mStructureSetActor =  new vvStructureSetActor;
+  QSharedPointer<vvStructureSetActor> mStructureSetActor(new vvStructureSetActor);
   
   mStructureSetActor->SetStructureSet(mStructureSet);
   mStructureSetActor->SetSlicerManager(mCurrentSlicerManager);
@@ -235,7 +246,7 @@ void vvToolStructureSetManager::OpenBinaryImage()
   int index;
   if (mCurrentStructureSet == NULL) {
     if (mStructureSetsList.size() == 0) { // Create a default SS
-      clitk::DicomRT_StructureSet * mStructureSet = new clitk::DicomRT_StructureSet;
+      clitk::DicomRT_StructureSet::Pointer mStructureSet = clitk::DicomRT_StructureSet::New();
       index = AddStructureSet(mStructureSet);
     }
     else { // Get first SS
@@ -245,10 +256,10 @@ void vvToolStructureSetManager::OpenBinaryImage()
     index = mCurrentStructureSetIndex;
   }
   mCurrentStructureSet = mStructureSetsList[index];
-  mCurrentStructureSetActor = mStructureSetActorsList[index];
+  mCurrentStructureSetActor = mStructureSetActorsList[index].data();
   mCurrentStructureSetIndex = index;
   // Open images
-  QString Extensions = "Images files ( *.mhd *.hdr *.his)";
+  QString Extensions = "Images files ( *.mha *.mhd *.hdr *.his)";
   Extensions += ";;All Files (*)";
   QStringList filename =
     QFileDialog::getOpenFileNames(this,tr("Open binary image"),
@@ -258,26 +269,25 @@ void vvToolStructureSetManager::OpenBinaryImage()
   for(int i=0; i<filename.size(); i++) {
     // Open Image
     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
-    vvImageReader * mReader = new vvImageReader;
+    vvImageReader::Pointer reader = vvImageReader::New();
     std::vector<std::string> filenames;
     filenames.push_back(filename[i].toStdString());
-    mReader->SetInputFilenames(filenames);
-    mReader->Update(IMAGE);
+    reader->SetInputFilenames(filenames);
+    reader->Update(vvImageReader::IMAGE);
     QApplication::restoreOverrideCursor();
 
-    if (mReader->GetLastError().size() != 0) {
+    if (reader->GetLastError().size() != 0) {
       std::cerr << "Error while reading " << filename[i].toStdString() << std::endl;
       QString error = "Cannot open file \n";
-      error += mReader->GetLastError().c_str();
+      error += reader->GetLastError().c_str();
       QMessageBox::information(this,tr("Reading problem"),error);
-      delete mReader;
       return;
     }
-    vvImage::Pointer binaryImage = mReader->GetOutput();
+    vvImage::Pointer binaryImage = reader->GetOutput();
     AddImage(binaryImage, filename[i].toStdString(), mBackgroundValueSpinBox->value());
     mOpenedBinaryImage.push_back(binaryImage);
-    delete mReader;
   }
+
   UpdateImage();
 }
 //------------------------------------------------------------------------------
@@ -307,7 +317,7 @@ void vvToolStructureSetManager::AddImage(vvImage * binaryImage, std::string file
   int index;
   if (mCurrentStructureSet == NULL) {
     if (mStructureSetsList.size() == 0) { // Create a default SS
-      clitk::DicomRT_StructureSet * mStructureSet = new clitk::DicomRT_StructureSet;
+      clitk::DicomRT_StructureSet::Pointer mStructureSet = clitk::DicomRT_StructureSet::New();
       index = AddStructureSet(mStructureSet);
     }
     else { // Get first SS
@@ -317,7 +327,7 @@ void vvToolStructureSetManager::AddImage(vvImage * binaryImage, std::string file
     index = mCurrentStructureSetIndex;
   }
   mCurrentStructureSet = mStructureSetsList[index];
-  mCurrentStructureSetActor = mStructureSetActorsList[index];
+  mCurrentStructureSetActor = mStructureSetActorsList[index].data();
   mCurrentStructureSetIndex = index;
 
   // Check Dimension
@@ -335,14 +345,14 @@ void vvToolStructureSetManager::AddImage(vvImage * binaryImage, std::string file
   int n = mCurrentStructureSet->AddBinaryImageAsNewROI(binaryImage, filename);
   mLoadedROIIndex.push_back(n);
   if (m_modeBG) 
-    mCurrentStructureSet->GetROI(n)->SetBackgroundValueLabelImage(BG);
+    mCurrentStructureSet->GetROIFromROINumber(n)->SetBackgroundValueLabelImage(BG);
   else 
-    mCurrentStructureSet->GetROI(n)->SetForegroundValueLabelImage(BG);
+    mCurrentStructureSet->GetROIFromROINumber(n)->SetForegroundValueLabelImage(BG);
   
   // Change color
   if (n<mDefaultLUTColor->GetNumberOfTableValues ()) {
     double * color = mDefaultLUTColor->GetTableValue(n % mDefaultLUTColor->GetNumberOfTableValues ());
-    mCurrentStructureSet->GetROI(n)->SetDisplayColor(color[0], color[1], color[2]);
+    mCurrentStructureSet->GetROIFromROINumber(n)->SetDisplayColor(color[0], color[1], color[2]);
   }
   
   // Add a new roi actor
@@ -369,6 +379,8 @@ void vvToolStructureSetManager::apply()
 //------------------------------------------------------------------------------
 bool vvToolStructureSetManager::close()
 {
+  //std::cout << "vvToolStructureSetManager::close()" << std::endl;
+
   return vvToolWidgetBase::close();
 }
 //------------------------------------------------------------------------------
@@ -377,6 +389,8 @@ bool vvToolStructureSetManager::close()
 //------------------------------------------------------------------------------
 void vvToolStructureSetManager::closeEvent(QCloseEvent *event) 
 {
+  //std::cout << "vvToolStructureSetManager::closeEvent()" << std::endl;
+
   std::vector<vvSlicerManager*>::iterator iter = std::find(mListOfInputs.begin(), mListOfInputs.end(), mCurrentSlicerManager);
   if (iter != mListOfInputs.end()) mListOfInputs.erase(iter);
   
@@ -385,14 +399,8 @@ void vvToolStructureSetManager::closeEvent(QCloseEvent *event)
 
   mCheckBoxShowAll->setCheckState(Qt::Unchecked);
   mContourCheckBoxShowAll->setCheckState(Qt::Unchecked);
-  if (mCurrentSlicerManager != 0) mCurrentSlicerManager->Render();
-  if (mCurrentStructureSetActor) {
-    for(int i=0; i<mCurrentStructureSetActor->GetNumberOfROIs(); i++) {
-      mCurrentStructureSetActor->GetROIList()[i]->SetVisible(false);
-      mCurrentStructureSetActor->GetROIList()[i]->SetContourVisible(false);
-      delete mCurrentStructureSetActor->GetROIList()[i];
-    }
-  }
+  if (mCurrentSlicerManager != 0)
+    mCurrentSlicerManager->Render();
 
   if (!isWindow()) {
     if (m_NumberOfTool == 1) {
@@ -412,6 +420,7 @@ void vvToolStructureSetManager::closeEvent(QCloseEvent *event)
 
 //------------------------------------------------------------------------------
 void vvToolStructureSetManager::SelectedItemChangedInTree() {
+  
   // Search which roi is selected
   QList<QTreeWidgetItem *> l = mTree->selectedItems();
   if (l.size() == 0) {
@@ -421,6 +430,8 @@ void vvToolStructureSetManager::SelectedItemChangedInTree() {
     return;
   }
   QTreeWidgetItem * w = l[0];
+  //std::cout << "selected item -> " << w->text(1).toStdString() << std::endl;
+  //std::cout << "m_NumberOfTool -> " << m_NumberOfTool << std::endl;
   if (mMapTreeWidgetToROI.find(w) == mMapTreeWidgetToROI.end()) {
     mCurrentROIActor = NULL;
     mCurrentROI = NULL;
@@ -433,24 +444,40 @@ void vvToolStructureSetManager::SelectedItemChangedInTree() {
   mCurrentROI = roi;
   mCurrentROIActor = actor;
 
+  // Warning -> avoid unuseful Render here by disconnect slider 
   // Update GUI
+  disconnect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
+  disconnect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
+  disconnect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
+  disconnect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
+  disconnect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));  
+  disconnect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
+  disconnect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
+  disconnect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
+
   mGroupBoxROI->setEnabled(true);
   mROInameLabel->setText(roi->GetName().c_str());
   mCheckBoxShow->setChecked(actor->IsVisible());
   mContourCheckBoxShow->setChecked(actor->IsContourVisible());
   mContourWidthSpinBox->setValue(actor->GetContourWidth());
-  
-  // Warning -> avoir unuseful Render here by disconnect slider 
-  disconnect(mOpacitySlider, SIGNAL(valueChanged(int)), 
-            this, SLOT(OpacityChanged(int)));
+  mDepthSpinBox->setValue(actor->GetDepth());
+  w->setText(3, QString("%1").arg(actor->GetDepth()));
   mOpacitySlider->setValue((int)lrint(actor->GetOpacity()*100));
   mOpacitySpinBox->setValue((int)lrint(actor->GetOpacity()*100));
-  connect(mOpacitySlider, SIGNAL(valueChanged(int)), 
-         this, SLOT(OpacityChanged(int)));
-  actor->Update(); 
 
+  connect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
+  connect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
+  connect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
+  connect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
+  connect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));  
+  connect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
+  connect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
+  connect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
+
+  // is this needed ?
+  //  actor->Update(); 
   // Final rendering
-  mCurrentSlicerManager->Render();
+  // mCurrentSlicerManager->Render();
 }
 //------------------------------------------------------------------------------
 
@@ -589,18 +616,30 @@ void vvToolStructureSetManager::ChangeContourWidth(int n) {
 //------------------------------------------------------------------------------
 
 
+//------------------------------------------------------------------------------
+void vvToolStructureSetManager::ChangeDepth(int n) {
+  mCurrentROIActor->SetDepth(n);
+  mCurrentROIActor->UpdateImage();
+  mCurrentSlicerManager->Render();
+  QList<QTreeWidgetItem *> l = mTree->selectedItems();
+  QTreeWidgetItem * w = l[0];
+  w->setText(3, QString("%1").arg(mCurrentROIActor->GetDepth()));
+}
+//------------------------------------------------------------------------------
+
+
 //------------------------------------------------------------------------------
 void vvToolStructureSetManager::ReloadCurrentROI() {
   // Reload image
-  vvImageReader * mReader = new vvImageReader;
-  mReader->SetInputFilename(mCurrentROI->GetFilename());
-  mReader->Update(IMAGE);
-  if (mReader->GetLastError() != "") {
-    QMessageBox::information(mMainWindowBase, tr("Sorry, error. Could not reload"), mReader->GetLastError().c_str());
+  vvImageReader::Pointer reader = vvImageReader::New();
+  reader->SetInputFilename(mCurrentROI->GetFilename());
+  reader->Update(vvImageReader::IMAGE);
+  if (reader->GetLastError() != "") {
+    QMessageBox::information(mMainWindowBase, tr("Sorry, error. Could not reload"), reader->GetLastError().c_str());
     return;
   }
   mCurrentROI->GetImage()->GetFirstVTKImageData()->ReleaseData();
-  mCurrentROI->SetImage(mReader->GetOutput());
+  mCurrentROI->SetImage(reader->GetOutput());
   
   // Update visu"
   mCurrentROIActor->UpdateImage();