]> Creatis software - clitk.git/blob - vv/vvToolStructureSetManager.cxx
Merge branch 'master' of /home/dsarrut/clitk3.server
[clitk.git] / vv / vvToolStructureSetManager.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ===========================================================================**/
18
19 // vv
20 #include "vvToolStructureSetManager.h"
21 #include "vvImageReader.h"
22 #include "vvStructureSetActor.h"
23 #include "vvSlicer.h"
24 #include "vvROIActor.h"
25
26 // Qt
27 #include <QFileDialog>
28 #include <QMessageBox>
29 #include <QColorDialog>
30 #include <QAbstractEventDispatcher>
31  
32 // vtk
33 #include <vtkLookupTable.h>
34 #include <vtkRenderWindow.h>
35
36 //------------------------------------------------------------------------------
37 // Create the tool and automagically (I like this word) insert it in
38 // the main window menu.
39 ADD_TOOL(vvToolStructureSetManager);
40 //------------------------------------------------------------------------------
41
42 int vvToolStructureSetManager::m_NumberOfTool = 0;
43 std::vector<vvSlicerManager*> vvToolStructureSetManager::mListOfInputs;
44 std::map<vvSlicerManager*, vvToolStructureSetManager*> vvToolStructureSetManager::mListOfOpenTool;
45
46 //------------------------------------------------------------------------------
47 vvToolStructureSetManager::vvToolStructureSetManager(vvMainWindowBase * parent, 
48                                                      Qt::WindowFlags f, 
49                                                      vvSlicerManager * current):
50   vvToolWidgetBase(parent, f), 
51   // if Qt::Widget -> No dialog in this case (in tab) ; PB = "invisible widget on menu" !
52   // if "f" normal widget
53   vvToolBase<vvToolStructureSetManager>(parent),
54   Ui::vvToolStructureSetManager()
55 {
56   Ui_vvToolStructureSetManager::setupUi(mToolWidget);
57
58   //  this->setFixedWidth(120);
59   m_NumberOfTool++;
60   mCurrentStructureSetActor = 0;
61   connect(mCloseButton, SIGNAL(clicked()), this, SLOT(close()));
62   mCloseButton->setVisible(false);
63   mTree->clear();
64   mTree->header()->resizeSection(0, 30);
65   mMainWindowBase->GetTab()->setTabIcon(mTabNumber, GetToolIcon());
66   mCurrentStructureSet = NULL;
67   mCurrentStructureSetIndex = -1;
68   mGroupBoxROI->setEnabled(false);
69   mCurrentROIActor = NULL;
70   mIsAllVisibleEnabled = false;
71   mNumberOfVisibleROI = 0;
72   mNumberOfVisibleContourROI = 0;
73   mDefaultLUTColor = vtkSmartPointer<vtkLookupTable>::New();
74   for(int i=0; i<mDefaultLUTColor->GetNumberOfTableValues(); i++) {
75     double r = (rand()/(RAND_MAX+1.0));
76     double v = (rand()/(RAND_MAX+1.0));
77     double b = (rand()/(RAND_MAX+1.0));
78     mDefaultLUTColor->SetTableValue(i, r, v, b);
79     //    std::cout << "mDefaultLUTColor->SetTableValue(" << i << ", " << r << ", " << v << ", " << b << ");" << std::endl;
80   }
81 #include "vvDefaultLut.h"
82
83   // Add input selector
84   if (current == NULL) {
85     MustOpenDialogWhenCreated = true;
86     AddInputSelector("Select image");
87   }
88   else {
89     MustOpenDialogWhenCreated = false;
90     mMainButtonBox->setEnabled(true);
91     mCurrentSlicerManager = current;
92     mCurrentImage = mCurrentSlicerManager->GetImage();
93     mToolWidget->setEnabled(true);
94     InputIsSelected(mCurrentSlicerManager);
95   }
96 }
97 //------------------------------------------------------------------------------
98
99
100 //------------------------------------------------------------------------------
101 vvToolStructureSetManager::~vvToolStructureSetManager()
102 {
103   m_NumberOfTool--;
104   
105   // clearing the list at this point avoids
106   // segfaulting due to events being dispatched
107   // after object destruction
108   mTreeWidgetList.clear();
109   mTree->clearSelection();
110
111   //std::cout << "vvToolStructureSetManager::~vvToolStructureSetManager()" << std::endl;
112 }
113 //------------------------------------------------------------------------------
114
115
116 //------------------------------------------------------------------------------
117 // STATIC
118 void vvToolStructureSetManager::Initialize() {
119   SetToolName("ROIManager");
120   SetToolMenuName("Display ROI (binary image)");
121   SetToolIconFilename(":/common/icons/tool-roi.png");
122   SetToolTip("Display ROI from a binary image.");
123   SetToolExperimental(false);
124 }
125 //------------------------------------------------------------------------------
126
127
128 //------------------------------------------------------------------------------
129 void vvToolStructureSetManager::InputIsSelected(vvSlicerManager *m)
130 {
131   //std::cout << "vvToolStructureSetManager::InputIsSelected()" << std::endl;
132   
133   //int mTabNumber = parent->GetTab()->addTab(this, "");
134   //  this->setFixedWidth(120);
135   //this->setPreferedHeight(441);  
136   // Refuse if 4D
137   if (mCurrentImage->GetNumberOfDimensions() != 3) {
138     QMessageBox::information(this,tr("Sorry only 3D yet"), tr("Sorry only 3D yet"));
139     close();
140     return;
141   }
142   // Hide selector
143   HideInputSelector(); // splitter
144   mToolInputSelectionWidget->hide();
145   mLabelInputInfo->setText(QString("%1").arg(m->GetFileName().c_str()));
146
147   // add to instance
148   // if (!isWindow()) {
149   mListOfInputs.push_back(mCurrentSlicerManager);
150   mListOfOpenTool[mCurrentSlicerManager] = this;
151   // }
152
153   // Connect open menus
154   connect(mOpenBinaryButton, SIGNAL(clicked()), this, SLOT(OpenBinaryImage()));
155   connect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
156   connect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
157   connect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
158   connect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
159   connect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));  
160   connect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
161   connect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
162   connect(mReloadButton, SIGNAL(clicked()), this, SLOT(ReloadCurrentROI()));
163   connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
164   connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
165
166   // Browse to load image
167   if (MustOpenDialogWhenCreated)
168     OpenBinaryImage();
169 }
170 //------------------------------------------------------------------------------
171
172
173 //------------------------------------------------------------------------------
174 void vvToolStructureSetManager::Open(int type) {
175   switch (type) {
176   case 0: OpenBinaryImage(); return; // Open binary image;
177   case 1: DD("TODO"); return; // Open DICOM RT
178   case 2: DD("TODO"); return; // Open mesh
179   default: std::cerr << "Error ????" << std::endl; exit(0);
180   }
181 }
182 //------------------------------------------------------------------------------
183
184
185 //------------------------------------------------------------------------------
186 void vvToolStructureSetManager::AddRoiInTreeWidget(clitk::DicomRT_ROI * roi, QTreeWidget * ww) {
187   mTreeWidgetList.push_back(QSharedPointer<QTreeWidgetItem>(new QTreeWidgetItem(ww)));
188   QTreeWidgetItem * w = mTreeWidgetList.back().data();
189   w->setText(0, QString("%1").arg(roi->GetROINumber()));
190   w->setText(1, QString("%1").arg(roi->GetName().c_str()));
191   QBrush brush(QColor(roi->GetDisplayColor()[0]*255, roi->GetDisplayColor()[1]*255, roi->GetDisplayColor()[2]*255));
192   brush.setStyle(Qt::SolidPattern);
193   w->setBackground(2, brush);
194   mMapROIToTreeWidget[roi] = w;
195   mMapTreeWidgetToROI[w] = roi;
196   mTree->resizeColumnToContents(0);
197   mTree->resizeColumnToContents(1);
198 }
199 //------------------------------------------------------------------------------
200
201
202 //------------------------------------------------------------------------------
203 void vvToolStructureSetManager::UpdateStructureSetInTreeWidget(int index, clitk::DicomRT_StructureSet * s) {
204   // Insert ROI
205   const std::vector<clitk::DicomRT_ROI::Pointer> & rois = s->GetListOfROI();
206   for(unsigned int i=0; i<rois.size(); i++) {
207     if (mMapROIToTreeWidget.find(rois[i]) == mMapROIToTreeWidget.end())
208       AddRoiInTreeWidget(rois[i], mTree); // replace mTree with ss if several SS
209   }
210 }
211 //------------------------------------------------------------------------------
212
213
214 //------------------------------------------------------------------------------
215 int vvToolStructureSetManager::AddStructureSet(clitk::DicomRT_StructureSet * mStructureSet) {
216   // Create actor for this SS
217
218   QSharedPointer<vvStructureSetActor> mStructureSetActor(new vvStructureSetActor);
219   
220   mStructureSetActor->SetStructureSet(mStructureSet);
221   mStructureSetActor->SetSlicerManager(mCurrentSlicerManager);
222   // Insert in lists and get index
223   mStructureSetsList.push_back(mStructureSet);
224   mStructureSetActorsList.push_back(mStructureSetActor);
225   int index = mStructureSetsList.size()-1;
226   // Return index
227   return index;
228 }
229 //------------------------------------------------------------------------------
230
231
232 //------------------------------------------------------------------------------
233 void vvToolStructureSetManager::OpenBinaryImage() 
234 {
235   int index;
236   if (mCurrentStructureSet == NULL) {
237     if (mStructureSetsList.size() == 0) { // Create a default SS
238       clitk::DicomRT_StructureSet::Pointer mStructureSet = clitk::DicomRT_StructureSet::New();
239       index = AddStructureSet(mStructureSet);
240     }
241     else { // Get first SS
242       index = 0;
243     }
244   } else {
245     index = mCurrentStructureSetIndex;
246   }
247   mCurrentStructureSet = mStructureSetsList[index];
248   mCurrentStructureSetActor = mStructureSetActorsList[index].data();
249   mCurrentStructureSetIndex = index;
250   // Open images
251   QString Extensions = "Images files ( *.mhd *.hdr *.his)";
252   Extensions += ";;All Files (*)";
253   QStringList filename =
254     QFileDialog::getOpenFileNames(this,tr("Open binary image"),
255                                   mMainWindowBase->GetInputPathName(),Extensions);
256   if (filename.size() == 0) return;
257   mLoadedROIIndex.clear();
258   for(int i=0; i<filename.size(); i++) {
259     // Open Image
260     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
261     vvImageReader::Pointer reader = vvImageReader::New();
262     std::vector<std::string> filenames;
263     filenames.push_back(filename[i].toStdString());
264     reader->SetInputFilenames(filenames);
265     reader->Update(vvImageReader::IMAGE);
266     QApplication::restoreOverrideCursor();
267
268     if (reader->GetLastError().size() != 0) {
269       std::cerr << "Error while reading " << filename[i].toStdString() << std::endl;
270       QString error = "Cannot open file \n";
271       error += reader->GetLastError().c_str();
272       QMessageBox::information(this,tr("Reading problem"),error);
273       return;
274     }
275     vvImage::Pointer binaryImage = reader->GetOutput();
276     AddImage(binaryImage, filename[i].toStdString(), mBackgroundValueSpinBox->value());
277     mOpenedBinaryImage.push_back(binaryImage);
278   }
279
280   UpdateImage();
281 }
282 //------------------------------------------------------------------------------
283
284
285 //------------------------------------------------------------------------------
286 void vvToolStructureSetManager::UpdateImage() 
287 {
288   // Update the TreeWidget
289   UpdateStructureSetInTreeWidget(mCurrentStructureSetIndex, mCurrentStructureSet);
290   // Render loaded ROIs (the first is sufficient)
291   for(unsigned int i=0; i<mLoadedROIIndex.size(); i++) {
292     mCurrentStructureSetActor->GetROIActor(mLoadedROIIndex[i])->Update();
293   }
294   for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
295     mCurrentSlicerManager->GetSlicer(i)->Render();
296   }  
297 }
298 //------------------------------------------------------------------------------
299
300
301 //------------------------------------------------------------------------------
302 void vvToolStructureSetManager::AddImage(vvImage * binaryImage, std::string filename, 
303                                          double BG, bool m_modeBG) 
304 {
305   // Check current structure set
306   int index;
307   if (mCurrentStructureSet == NULL) {
308     if (mStructureSetsList.size() == 0) { // Create a default SS
309       clitk::DicomRT_StructureSet::Pointer mStructureSet = clitk::DicomRT_StructureSet::New();
310       index = AddStructureSet(mStructureSet);
311     }
312     else { // Get first SS
313       index = 0;
314     }
315   } else {
316     index = mCurrentStructureSetIndex;
317   }
318   mCurrentStructureSet = mStructureSetsList[index];
319   mCurrentStructureSetActor = mStructureSetActorsList[index].data();
320   mCurrentStructureSetIndex = index;
321
322   // Check Dimension
323   int dim = mCurrentImage->GetNumberOfDimensions();
324   int bin_dim = binaryImage->GetNumberOfDimensions();
325   if (dim < bin_dim) {  ////////// TO CHANGE FOR 3D/4D
326     std::ostringstream os;
327     os << "Error. Loaded binary image is " << bin_dim
328        << "D while selected image is " << dim << "D" << std::endl;
329     QMessageBox::information(this,tr("Reading problem"),os.str().c_str());
330     return;
331   }
332   
333   // Add a new roi to the structure
334   int n = mCurrentStructureSet->AddBinaryImageAsNewROI(binaryImage, filename);
335   mLoadedROIIndex.push_back(n);
336   if (m_modeBG) 
337     mCurrentStructureSet->GetROI(n)->SetBackgroundValueLabelImage(BG);
338   else 
339     mCurrentStructureSet->GetROI(n)->SetForegroundValueLabelImage(BG);
340   
341   // Change color
342   if (n<mDefaultLUTColor->GetNumberOfTableValues ()) {
343     double * color = mDefaultLUTColor->GetTableValue(n % mDefaultLUTColor->GetNumberOfTableValues ());
344     mCurrentStructureSet->GetROI(n)->SetDisplayColor(color[0], color[1], color[2]);
345   }
346   
347   // Add a new roi actor
348   mCurrentStructureSetActor->CreateNewROIActor(n, m_modeBG);
349   
350   // CheckBox for "All"
351   if (mCurrentStructureSetActor->GetROIActor(n)->IsVisible())
352     mNumberOfVisibleROI++;
353   if (mCurrentStructureSetActor->GetROIActor(n)->IsContourVisible())
354     mNumberOfVisibleContourROI++;
355   UpdateAllROIStatus();
356 }
357 //------------------------------------------------------------------------------
358
359
360 //------------------------------------------------------------------------------
361 void vvToolStructureSetManager::apply() 
362 {
363   close();
364 }
365 //------------------------------------------------------------------------------
366
367
368 //------------------------------------------------------------------------------
369 bool vvToolStructureSetManager::close()
370 {
371   //std::cout << "vvToolStructureSetManager::close()" << std::endl;
372
373   return vvToolWidgetBase::close();
374 }
375 //------------------------------------------------------------------------------
376
377
378 //------------------------------------------------------------------------------
379 void vvToolStructureSetManager::closeEvent(QCloseEvent *event) 
380 {
381   //std::cout << "vvToolStructureSetManager::closeEvent()" << std::endl;
382
383   std::vector<vvSlicerManager*>::iterator iter = std::find(mListOfInputs.begin(), mListOfInputs.end(), mCurrentSlicerManager);
384   if (iter != mListOfInputs.end()) mListOfInputs.erase(iter);
385   
386   //  DD("how delete mListOfOpenTool ???");
387   mListOfOpenTool.erase(mCurrentSlicerManager);
388
389   mCheckBoxShowAll->setCheckState(Qt::Unchecked);
390   mContourCheckBoxShowAll->setCheckState(Qt::Unchecked);
391   if (mCurrentSlicerManager != 0)
392     mCurrentSlicerManager->Render();
393
394   if (!isWindow()) {
395     if (m_NumberOfTool == 1) {
396       mMainWindow->GetTab()->removeTab(mTabNumber);
397     }
398   } 
399
400   event->accept();
401 }
402 //------------------------------------------------------------------------------
403
404
405 //------------------------------------------------------------------------------
406 // CURRENT ROI INTERACTION
407 //------------------------------------------------------------------------------
408
409
410 //------------------------------------------------------------------------------
411 void vvToolStructureSetManager::SelectedItemChangedInTree() {
412   
413   // Search which roi is selected
414   QList<QTreeWidgetItem *> l = mTree->selectedItems();
415   if (l.size() == 0) {
416     mCurrentROIActor = NULL;
417     mCurrentROI = NULL;
418     mGroupBoxROI->setEnabled(false);
419     return;
420   }
421   QTreeWidgetItem * w = l[0];
422   //std::cout << "selected item -> " << w->text(1).toStdString() << std::endl;
423   //std::cout << "m_NumberOfTool -> " << m_NumberOfTool << std::endl;
424   if (mMapTreeWidgetToROI.find(w) == mMapTreeWidgetToROI.end()) {
425     mCurrentROIActor = NULL;
426     mCurrentROI = NULL;
427     mGroupBoxROI->setEnabled(false);
428     return;
429   }
430   clitk::DicomRT_ROI * roi = mMapTreeWidgetToROI[w];
431   // Get selected roi actor
432   vvROIActor * actor = mStructureSetActorsList[mCurrentStructureSetIndex]->GetROIActor(roi->GetROINumber());
433   mCurrentROI = roi;
434   mCurrentROIActor = actor;
435
436   // Update GUI
437   mGroupBoxROI->setEnabled(true);
438   mROInameLabel->setText(roi->GetName().c_str());
439   mCheckBoxShow->setChecked(actor->IsVisible());
440   mContourCheckBoxShow->setChecked(actor->IsContourVisible());
441   mContourWidthSpinBox->setValue(actor->GetContourWidth());
442   
443   // Warning -> avoir unuseful Render here by disconnect slider 
444   disconnect(mOpacitySlider, SIGNAL(valueChanged(int)), 
445              this, SLOT(OpacityChanged(int)));
446   mOpacitySlider->setValue((int)lrint(actor->GetOpacity()*100));
447   mOpacitySpinBox->setValue((int)lrint(actor->GetOpacity()*100));
448   connect(mOpacitySlider, SIGNAL(valueChanged(int)), 
449           this, SLOT(OpacityChanged(int)));
450   actor->Update(); 
451
452   // Final rendering
453   mCurrentSlicerManager->Render();
454 }
455 //------------------------------------------------------------------------------
456
457
458 //------------------------------------------------------------------------------
459 void vvToolStructureSetManager::UpdateAllROIStatus() {
460   int nbVisible = 0;
461   int nb = mCurrentStructureSetActor->GetROIList().size();
462   for(int i=0; i<nb; i++) {
463     if (mCurrentStructureSetActor->GetROIList()[i]->IsVisible()) {
464       nbVisible++;
465     }
466   }
467
468   disconnect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));  
469   disconnect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
470   if (nbVisible == nb) mCheckBoxShowAll->setCheckState(Qt::Checked);
471   else {
472     if (nbVisible == 0) mCheckBoxShowAll->setCheckState(Qt::Unchecked);
473     else mCheckBoxShowAll->setCheckState(Qt::PartiallyChecked);
474   }
475   connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
476   connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
477 }
478 //------------------------------------------------------------------------------
479
480 //------------------------------------------------------------------------------
481 void vvToolStructureSetManager::VisibleROIToggled(bool b) {
482   if (mCurrentROIActor == NULL) return;
483   if (b == mCurrentROIActor->IsVisible()) return; // nothing to do
484   mCurrentROIActor->SetVisible(b);
485   UpdateAllROIStatus();
486   mCurrentSlicerManager->Render(); 
487 }
488 //------------------------------------------------------------------------------
489
490
491 //------------------------------------------------------------------------------
492 void vvToolStructureSetManager::VisibleContourROIToggled(bool b) {
493   if (mCurrentROIActor == NULL) return;
494   if (mCurrentROIActor->IsContourVisible() == b) return; // nothing to do
495   mCurrentROIActor->SetContourVisible(b);
496   mCurrentROIActor->UpdateColor();
497   mCurrentSlicerManager->Render(); 
498 }
499 //------------------------------------------------------------------------------
500
501
502 //------------------------------------------------------------------------------
503 void vvToolStructureSetManager::OpacityChanged(int v) {
504   if (mCurrentROIActor == NULL) return;
505   mCurrentROIActor->SetOpacity((double)v/100.0);
506   mCurrentROIActor->UpdateColor();
507   mCurrentSlicerManager->Render(); 
508 }
509 //------------------------------------------------------------------------------
510
511
512 //------------------------------------------------------------------------------
513 void vvToolStructureSetManager::AllVisibleROIToggled(int b) {
514   bool status = false;
515   if ((mCheckBoxShowAll->checkState() == Qt::Checked) ||
516       (mCheckBoxShowAll->checkState() == Qt::PartiallyChecked))  status = true;
517
518   for(int i=0; i<mCurrentStructureSetActor->GetNumberOfROIs(); i++) {
519     mCurrentStructureSetActor->GetROIList()[i]->SetVisible(status);
520   }
521   if (status) mCheckBoxShowAll->setCheckState(Qt::Checked);
522   else  mCheckBoxShowAll->setCheckState(Qt::Unchecked);
523   mCheckBoxShow->setChecked(status);
524   mCurrentSlicerManager->Render(); 
525 }
526 //------------------------------------------------------------------------------
527
528
529 //------------------------------------------------------------------------------
530 void vvToolStructureSetManager::AllVisibleContourROIToggled(bool b) {
531   bool status = false;
532   if ((mContourCheckBoxShowAll->checkState() == Qt::Checked) ||
533       (mContourCheckBoxShowAll->checkState() == Qt::PartiallyChecked))  status = true;
534   // Update current 
535   for(int i=0; i<mCurrentStructureSetActor->GetNumberOfROIs(); i++) {
536     mCurrentStructureSetActor->GetROIList()[i]->SetContourVisible(status);
537   }
538   // Update current selection
539   if (status) mContourCheckBoxShowAll->setCheckState(Qt::Checked);
540   else  mContourCheckBoxShowAll->setCheckState(Qt::Unchecked);
541   mContourCheckBoxShow->setChecked(status);
542   mCurrentSlicerManager->Render(); 
543 }
544 //------------------------------------------------------------------------------
545
546
547 //------------------------------------------------------------------------------
548 void vvToolStructureSetManager::ChangeColor() {
549   QColor color;
550   color.setRgbF(mCurrentROIActor->GetROI()->GetDisplayColor()[0],
551                 mCurrentROIActor->GetROI()->GetDisplayColor()[1],
552                 mCurrentROIActor->GetROI()->GetDisplayColor()[2]);
553   QColor c = QColorDialog::getColor(color, this, "Choose the ROI color");
554   mCurrentROIActor->GetROI()->SetDisplayColor(c.redF(), c.greenF(), c.blueF());
555   mCurrentROIActor->UpdateColor();
556
557   QTreeWidgetItem * w = mMapROIToTreeWidget[mCurrentROI];
558   QBrush brush(QColor(mCurrentROI->GetDisplayColor()[0]*255,
559                       mCurrentROI->GetDisplayColor()[1]*255,
560                       mCurrentROI->GetDisplayColor()[2]*255));
561   brush.setStyle(Qt::SolidPattern);
562   w->setBackground(2, brush);
563   // Render
564   mCurrentSlicerManager->Render();
565 }
566 //------------------------------------------------------------------------------
567
568
569 //------------------------------------------------------------------------------
570 void vvToolStructureSetManager::ChangeContourColor() {
571   QColor color;
572   color.setRgbF(mCurrentROIActor->GetContourColor()[0], 
573                 mCurrentROIActor->GetContourColor()[1], 
574                 mCurrentROIActor->GetContourColor()[2]);
575   QColor c = QColorDialog::getColor(color, this, "Choose the contour color");
576   mCurrentROIActor->SetContourColor(c.redF(), c.greenF(), c.blueF());
577   mCurrentROIActor->UpdateColor();
578   mCurrentSlicerManager->Render();
579 }
580 //------------------------------------------------------------------------------
581
582
583 //------------------------------------------------------------------------------
584 void vvToolStructureSetManager::ChangeContourWidth(int n) {
585   mCurrentROIActor->SetContourWidth(n);
586   mCurrentROIActor->UpdateColor();
587   mCurrentSlicerManager->Render();
588 }
589 //------------------------------------------------------------------------------
590
591
592 //------------------------------------------------------------------------------
593 void vvToolStructureSetManager::ReloadCurrentROI() {
594   // Reload image
595   vvImageReader::Pointer reader = vvImageReader::New();
596   reader->SetInputFilename(mCurrentROI->GetFilename());
597   reader->Update(vvImageReader::IMAGE);
598   if (reader->GetLastError() != "") {
599     QMessageBox::information(mMainWindowBase, tr("Sorry, error. Could not reload"), reader->GetLastError().c_str());
600     return;
601   }
602   mCurrentROI->GetImage()->GetFirstVTKImageData()->ReleaseData();
603   mCurrentROI->SetImage(reader->GetOutput());
604   
605   // Update visu"
606   mCurrentROIActor->UpdateImage();
607   mCurrentSlicerManager->Render();    
608 }
609 //------------------------------------------------------------------------------
610
611
612 //------------------------------------------------------------------------------
613 void vvToolStructureSetManager::CheckInputList(std::vector<vvSlicerManager*> & l, int & index) 
614 {
615   for(unsigned int i=0; i<l.size(); i++) {
616     std::vector<vvSlicerManager*>::iterator iter = std::find(mListOfInputs.begin(), mListOfInputs.end(), l[i]);
617     if (iter != mListOfInputs.end()) {
618       for(unsigned int j=i;j<l.size(); j++) l[j] = l[j+1];
619       l.pop_back();
620       if (index == (int)i) index = 0;
621       i--;
622     }
623   }
624 }
625 //------------------------------------------------------------------------------
626
627
628 //------------------------------------------------------------------------------
629 // STATIC
630 vvToolStructureSetManager * vvToolStructureSetManager::AddImage(vvSlicerManager * m, std::string name, vvImage::Pointer image, double BG, bool m_modeBG)
631 {
632   // If the tool is open for this vvSlicerManager, use it and return
633   if (mListOfOpenTool[m]) {
634     vvToolStructureSetManager * tool = mListOfOpenTool[m];
635     tool->AddImage(image, name, BG, m_modeBG);
636     tool->UpdateImage();
637     return tool;
638   }
639
640   // If the tool is not open, create it
641   vvToolStructureSetManager * tool = new vvToolStructureSetManager
642     (CREATOR(vvToolStructureSetManager)->GetMainWindow(), Qt::Dialog, m);
643   tool->AddImage(image, name, BG, m_modeBG);
644   tool->UpdateImage();
645   tool->show();
646   return tool;
647 }
648 //------------------------------------------------------------------------------