]> Creatis software - clitk.git/commitdiff
First version that allow to open file (no button feedback yet)
authorDavid Sarrut <david.sarrut@gmail.com>
Mon, 20 Feb 2012 15:30:24 +0000 (16:30 +0100)
committerDavid Sarrut <david.sarrut@gmail.com>
Mon, 20 Feb 2012 15:30:24 +0000 (16:30 +0100)
vv/vvToolROIManager.cxx
vv/vvToolROIManager.h

index 77a65d3543c37ee3db2d4fac2e76b500ac24b4fa..b681fc79298078be55c8e392ef957576b43b3847 100644 (file)
@@ -51,10 +51,30 @@ vvToolROIManager::vvToolROIManager(vvMainWindowBase * parent, Qt::WindowFlags f)
   //  Insert the current QWidget into the tab layout (required)
   QWidget * mother = qFindChild<QWidget*>(parent->GetTab(), "ROItab");
   mother->layout()->addWidget(this);
+  mMainWindow = parent;
   
   // Build the UI
   Ui_vvToolROIManager::setupUi(this);
   setAttribute(Qt::WA_DeleteOnClose);
+  mTree->clear();
+  mTree->header()->resizeSection(0, 30);
+  parent->GetTab()->setCurrentIndex(2);
+
+  // Set default LUT
+  mDefaultLUTColor = vtkSmartPointer<vtkLookupTable>::New();
+  DD(mDefaultLUTColor->GetNumberOfTableValues());
+  for(int i=0; i<mDefaultLUTColor->GetNumberOfTableValues(); i++) {
+    double r = (rand()/(RAND_MAX+1.0));
+    double v = (rand()/(RAND_MAX+1.0));
+    double b = (rand()/(RAND_MAX+1.0));
+    mDefaultLUTColor->SetTableValue(i, r, v, b);
+    //    std::cout << "mDefaultLUTColor->SetTableValue(" << i << ", " << r << ", " << v << ", " << b << ");" << std::endl;
+  }
+#include "vvDefaultLut.h"
+
+  // Initialization
+  mNumberOfVisibleROI = 0;
+  mNumberOfVisibleContourROI = 0;
 
   // Select the current image as the target
   int i = parent->GetSlicerManagerCurrentIndex();
@@ -101,7 +121,18 @@ void vvToolROIManager::InputIsSelected(vvSlicerManager *m)
   connect(mCloseButton, SIGNAL(clicked()), this, SLOT(close()));
 
   // Initialization
+  mSlicerManager = m;
+  mCurrentImage = mSlicerManager->GetImage();
+
+  // Refuse if 4D
+  if (mCurrentImage->GetNumberOfDimensions() != 3) {
+    QMessageBox::information(this,tr("Sorry only 3D yet"), tr("Sorry only 3D yet"));
+    close();
+    return;
+  }
 
+  // Auto diusplay browser to select new contours 
+  OpenBinaryImage();
 }
 //------------------------------------------------------------------------------
 
@@ -120,3 +151,171 @@ void vvToolROIManager::SelectedImageHasChanged(vvSlicerManager * m) {
   else show();
 }
 //------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::OpenBinaryImage() 
+{
+  // Open images
+  QString Extensions = "Images files ( *.mha *.mhd *.hdr *.his)";
+  Extensions += ";;All Files (*)";
+  QStringList filename =
+    QFileDialog::getOpenFileNames(this,tr("Open binary image"),
+                                 mMainWindowBase->GetInputPathName(),Extensions);
+  if (filename.size() == 0) return;
+  
+  // For each selected file, open the image
+  for(int i=0; i<filename.size(); i++) {
+    // Open Image
+    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
+    vvImageReader::Pointer reader = vvImageReader::New();
+    std::vector<std::string> filenames;
+    filenames.push_back(filename[i].toStdString());
+    reader->SetInputFilenames(filenames);
+    reader->Update(vvImageReader::IMAGE);
+    QApplication::restoreOverrideCursor();
+
+    if (reader->GetLastError().size() != 0) {
+      std::cerr << "Error while reading " << filename[i].toStdString() << std::endl;
+      QString error = "Cannot open file \n";
+      error += reader->GetLastError().c_str();
+      QMessageBox::information(this,tr("Reading problem"),error);
+      return;
+    }
+    vvImage::Pointer binaryImage = reader->GetOutput();
+    AddImage(binaryImage, filename[i].toStdString(), mBackgroundValueSpinBox->value(),
+             (!mBGModeCheckBox->isChecked()));
+    mOpenedBinaryImage.push_back(binaryImage);
+  }
+
+  // Update the contours
+  UpdateAllContours(); 
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::AddImage(vvImage * binaryImage, std::string filename, 
+                                double BG, bool modeBG) 
+{
+  DD(modeBG);
+
+  // Check Dimension
+  int dim = mCurrentImage->GetNumberOfDimensions();
+  int bin_dim = binaryImage->GetNumberOfDimensions();
+  if (dim < bin_dim) {
+    std::ostringstream os;
+    os << "Error. Loaded binary image is " << bin_dim
+       << "D while selected image is " << dim << "D" << std::endl;
+    QMessageBox::information(this,tr("Reading problem"),os.str().c_str());
+    return;
+  }
+  
+  // Compute roi index
+  int n = mROIList.size();
+  DD(n);
+  
+  // Compute the name of the new ROI
+  std::ostringstream oss;
+  oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(filename));
+  std::string name = oss.str();
+  DD(name);
+  
+  // Set color
+  std::vector<double> color;
+  color.push_back(1);
+  color.push_back(0);
+  color.push_back(0);
+
+  // Create ROI
+  clitk::DicomRT_ROI::Pointer roi = clitk::DicomRT_ROI::New();
+  roi->SetFromBinaryImage(binaryImage, n, name, color, filename);
+
+  // Add a new roi to the list
+  mROIList.push_back(roi);
+  // Set BG or FG mode
+  if (modeBG) 
+    roi->SetBackgroundValueLabelImage(BG);
+  else 
+    roi->SetForegroundValueLabelImage(BG);
+  
+  // Change color
+  DD("color");
+  if (n<mDefaultLUTColor->GetNumberOfTableValues ()) {
+    double * color = mDefaultLUTColor->GetTableValue(n % mDefaultLUTColor->GetNumberOfTableValues ());
+    roi->SetDisplayColor(color[0], color[1], color[2]);
+  }
+  
+  // Add a new roi actor
+  QSharedPointer<vvROIActor> actor = QSharedPointer<vvROIActor>(new vvROIActor);
+  actor->SetBGMode(modeBG);
+  actor->SetROI(roi);
+  actor->SetSlicerManager(mSlicerManager);
+  actor->Initialize(n+1); // depth is n+1 to start at 1
+  mROIActorsList.push_back(actor);
+  
+  // CheckBox for "All"
+  if (actor->IsVisible()) mNumberOfVisibleROI++;
+  if (actor->IsContourVisible()) mNumberOfVisibleContourROI++;
+  
+  // Add ROI in tree
+  mTreeWidgetList.push_back(QSharedPointer<QTreeWidgetItem>(new QTreeWidgetItem(mTree)));
+  QTreeWidgetItem * w = mTreeWidgetList.back().data();
+  w->setText(0, QString("%1").arg(roi->GetROINumber()));
+  w->setText(1, QString("%1").arg(roi->GetName().c_str()));
+  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);
+  mMapROIToTreeWidget[roi] = w;
+  mMapTreeWidgetToROI[w] = roi;
+  mTree->resizeColumnToContents(0);
+  mTree->resizeColumnToContents(1);
+
+  // Update 
+  UpdateAllROIStatus(); 
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::UpdateAllContours() 
+{
+  // Render loaded ROIs (the first is sufficient)
+  for(unsigned int i=0; i<mROIList.size(); i++) {
+    mROIActorsList[i]->Update();
+  }
+  for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
+    mSlicerManager->GetSlicer(i)->Render();
+  }  
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::UpdateAllROIStatus() {
+  int nbVisible = 0;
+  int nb = mROIList.size();
+  for(int i=0; i<nb; i++) {
+    if (mROIActorsList[i]->IsVisible()) {
+      nbVisible++;
+    }
+  }
+  DD(nbVisible);
+
+  // change the states
+  disconnect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));  
+  disconnect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
+  if (nbVisible == nb) mCheckBoxShowAll->setCheckState(Qt::Checked);
+  else {
+    if (nbVisible == 0) mCheckBoxShowAll->setCheckState(Qt::Unchecked);
+    else mCheckBoxShowAll->setCheckState(Qt::PartiallyChecked);
+  }
+  connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
+  connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
+}
+//------------------------------------------------------------------------------
+
index 2d91264405229913f4e8da7029c7f2bd7b5a1d96..be3b987c311bae45a503d3c72b8d01957a4a91ba 100644 (file)
@@ -43,13 +43,35 @@ class vvToolROIManager:
   static void Initialize();
   virtual void InputIsSelected(vvSlicerManager *m);
 
+  void AddImage(vvImage * binaryImage, std::string filename, double BG, bool m_modeBG=true);
+  void UpdateAllContours();
+  void UpdateAllROIStatus();
+
   public slots:
   void AnImageIsBeingClosed(vvSlicerManager *);
   void SelectedImageHasChanged(vvSlicerManager *);
+  void OpenBinaryImage();
 
 protected:
   Ui::vvToolROIManager ui;
   vvSlicerManager * mSlicerManager;
+  vvImage * mCurrentImage;
+  vvMainWindowBase * mMainWindow;
+  
+  int mNumberOfVisibleROI;
+  int mNumberOfVisibleContourROI;
+
+  vtkSmartPointer<vtkLookupTable> mDefaultLUTColor;
+
+  std::vector<vvImage::Pointer> mOpenedBinaryImage;
+  std::vector<clitk::DicomRT_ROI::Pointer> mROIList;
+  std::vector<QSharedPointer<vvROIActor> > mROIActorsList;
+
+  // Data for the widget Tree
+  std::vector< QSharedPointer<QTreeWidgetItem> > mTreeWidgetList;
+  std::map<clitk::DicomRT_ROI::Pointer, QTreeWidgetItem *> mMapROIToTreeWidget;
+  std::map<QTreeWidgetItem *, clitk::DicomRT_ROI::Pointer> mMapTreeWidgetToROI;
+  
  
 }; // end class vvToolROIManager
 //------------------------------------------------------------------------------