]> Creatis software - clitk.git/blob - vv/vvToolStructureSetManager.cxx
Debug RTStruct conversion with empty struc
[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_OLD");
120   SetToolMenuName("Display ROI (binary image)");
121   SetToolIconFilename(":/common/icons/tool-roi.png");
122   SetToolTip("Display ROI from a binary image.");
123   SetToolExperimental(true);
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(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
163   connect(mReloadButton, SIGNAL(clicked()), this, SLOT(ReloadCurrentROI()));
164   connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
165   connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
166
167   // Browse to load image
168   if (MustOpenDialogWhenCreated)
169     OpenBinaryImage();
170 }
171 //------------------------------------------------------------------------------
172
173
174 //------------------------------------------------------------------------------
175 void vvToolStructureSetManager::Open(int type) {
176   switch (type) {
177   case 0: OpenBinaryImage(); return; // Open binary image;
178   case 1: DD("TODO"); return; // Open DICOM RT
179   case 2: DD("TODO"); return; // Open mesh
180   default: std::cerr << "Error ????" << std::endl; exit(0);
181   }
182 }
183 //------------------------------------------------------------------------------
184
185
186 //------------------------------------------------------------------------------
187 void vvToolStructureSetManager::AddRoiInTreeWidget(clitk::DicomRT_ROI * roi, QTreeWidget * ww) {
188   mTreeWidgetList.push_back(QSharedPointer<QTreeWidgetItem>(new QTreeWidgetItem(ww)));
189   QTreeWidgetItem * w = mTreeWidgetList.back().data();
190   w->setText(0, QString("%1").arg(roi->GetROINumber()));
191   w->setText(1, QString("%1").arg(roi->GetName().c_str()));
192   vvROIActor * actor = mStructureSetActorsList[0]->GetROIActor(roi->GetROINumber());  
193   w->setText(3, QString("%1").arg(actor->GetDepth()));  
194   QBrush brush(QColor(roi->GetDisplayColor()[0]*255, roi->GetDisplayColor()[1]*255, roi->GetDisplayColor()[2]*255));
195   brush.setStyle(Qt::SolidPattern);
196   w->setBackground(2, brush);
197   mMapROIToTreeWidget[roi] = w;
198   mMapTreeWidgetToROI[w] = roi;
199   mTree->resizeColumnToContents(0);
200   mTree->resizeColumnToContents(1);
201 }
202 //------------------------------------------------------------------------------
203
204
205 //------------------------------------------------------------------------------
206 void vvToolStructureSetManager::UpdateStructureSetInTreeWidget(int index, clitk::DicomRT_StructureSet * s) {
207   // Insert ROI
208   /*
209   const std::vector<clitk::DicomRT_ROI::Pointer> & rois = s->GetListOfROI();
210   for(unsigned int i=0; i<rois.size(); i++) {
211     if (mMapROIToTreeWidget.find(rois[i]) == mMapROIToTreeWidget.end())
212       AddRoiInTreeWidget(rois[i], mTree); // replace mTree with ss if several SS
213   }
214   */
215   clitk::DicomRT_StructureSet::ROIConstIteratorType iter;
216   for(iter = s->GetROIs().begin(); iter != s->GetROIs().end(); iter++) {
217     clitk::DicomRT_ROI::Pointer roi = iter->second;
218     if (mMapROIToTreeWidget.find(roi) == mMapROIToTreeWidget.end())
219       AddRoiInTreeWidget(roi, mTree); // replace mTree with ss if several SS
220   }
221 }
222 //------------------------------------------------------------------------------
223
224
225 //------------------------------------------------------------------------------
226 int vvToolStructureSetManager::AddStructureSet(clitk::DicomRT_StructureSet * mStructureSet) {
227   // Create actor for this SS
228
229   QSharedPointer<vvStructureSetActor> mStructureSetActor(new vvStructureSetActor);
230   
231   mStructureSetActor->SetStructureSet(mStructureSet);
232   mStructureSetActor->SetSlicerManager(mCurrentSlicerManager);
233   // Insert in lists and get index
234   mStructureSetsList.push_back(mStructureSet);
235   mStructureSetActorsList.push_back(mStructureSetActor);
236   int index = mStructureSetsList.size()-1;
237   // Return index
238   return index;
239 }
240 //------------------------------------------------------------------------------
241
242
243 //------------------------------------------------------------------------------
244 void vvToolStructureSetManager::OpenBinaryImage() 
245 {
246   int index;
247   if (mCurrentStructureSet == NULL) {
248     if (mStructureSetsList.size() == 0) { // Create a default SS
249       clitk::DicomRT_StructureSet::Pointer mStructureSet = clitk::DicomRT_StructureSet::New();
250       index = AddStructureSet(mStructureSet);
251     }
252     else { // Get first SS
253       index = 0;
254     }
255   } else {
256     index = mCurrentStructureSetIndex;
257   }
258   mCurrentStructureSet = mStructureSetsList[index];
259   mCurrentStructureSetActor = mStructureSetActorsList[index].data();
260   mCurrentStructureSetIndex = index;
261   // Open images
262   QString Extensions = "Images files ( *.mha *.mhd *.hdr *.his)";
263   Extensions += ";;All Files (*)";
264   QStringList filename =
265     QFileDialog::getOpenFileNames(this,tr("Open binary image"),
266                                   mMainWindowBase->GetInputPathName(),Extensions);
267   if (filename.size() == 0) return;
268   mLoadedROIIndex.clear();
269   for(int i=0; i<filename.size(); i++) {
270     // Open Image
271     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
272     vvImageReader::Pointer reader = vvImageReader::New();
273     std::vector<std::string> filenames;
274     filenames.push_back(filename[i].toStdString());
275     reader->SetInputFilenames(filenames);
276     reader->Update(vvImageReader::IMAGE);
277     QApplication::restoreOverrideCursor();
278
279     if (reader->GetLastError().size() != 0) {
280       std::cerr << "Error while reading " << filename[i].toStdString() << std::endl;
281       QString error = "Cannot open file \n";
282       error += reader->GetLastError().c_str();
283       QMessageBox::information(this,tr("Reading problem"),error);
284       return;
285     }
286     vvImage::Pointer binaryImage = reader->GetOutput();
287     AddImage(binaryImage, filename[i].toStdString(), mBackgroundValueSpinBox->value());
288     mOpenedBinaryImage.push_back(binaryImage);
289   }
290
291   UpdateImage();
292 }
293 //------------------------------------------------------------------------------
294
295
296 //------------------------------------------------------------------------------
297 void vvToolStructureSetManager::UpdateImage() 
298 {
299   // Update the TreeWidget
300   UpdateStructureSetInTreeWidget(mCurrentStructureSetIndex, mCurrentStructureSet);
301   // Render loaded ROIs (the first is sufficient)
302   for(unsigned int i=0; i<mLoadedROIIndex.size(); i++) {
303     mCurrentStructureSetActor->GetROIActor(mLoadedROIIndex[i])->Update();
304   }
305   for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
306     mCurrentSlicerManager->GetSlicer(i)->Render();
307   }  
308 }
309 //------------------------------------------------------------------------------
310
311
312 //------------------------------------------------------------------------------
313 void vvToolStructureSetManager::AddImage(vvImage * binaryImage, std::string filename, 
314                                          double BG, bool m_modeBG) 
315 {
316   // Check current structure set
317   int index;
318   if (mCurrentStructureSet == NULL) {
319     if (mStructureSetsList.size() == 0) { // Create a default SS
320       clitk::DicomRT_StructureSet::Pointer mStructureSet = clitk::DicomRT_StructureSet::New();
321       index = AddStructureSet(mStructureSet);
322     }
323     else { // Get first SS
324       index = 0;
325     }
326   } else {
327     index = mCurrentStructureSetIndex;
328   }
329   mCurrentStructureSet = mStructureSetsList[index];
330   mCurrentStructureSetActor = mStructureSetActorsList[index].data();
331   mCurrentStructureSetIndex = index;
332
333   // Check Dimension
334   int dim = mCurrentImage->GetNumberOfDimensions();
335   int bin_dim = binaryImage->GetNumberOfDimensions();
336   if (dim < bin_dim) {  ////////// TO CHANGE FOR 3D/4D
337     std::ostringstream os;
338     os << "Error. Loaded binary image is " << bin_dim
339        << "D while selected image is " << dim << "D" << std::endl;
340     QMessageBox::information(this,tr("Reading problem"),os.str().c_str());
341     return;
342   }
343   
344   // Add a new roi to the structure
345   int n = mCurrentStructureSet->AddBinaryImageAsNewROI(binaryImage, filename);
346   mLoadedROIIndex.push_back(n);
347   if (m_modeBG) 
348     mCurrentStructureSet->GetROIFromROINumber(n)->SetBackgroundValueLabelImage(BG);
349   else 
350     mCurrentStructureSet->GetROIFromROINumber(n)->SetForegroundValueLabelImage(BG);
351   
352   // Change color
353   if (n<mDefaultLUTColor->GetNumberOfTableValues ()) {
354     double * color = mDefaultLUTColor->GetTableValue(n % mDefaultLUTColor->GetNumberOfTableValues ());
355     mCurrentStructureSet->GetROIFromROINumber(n)->SetDisplayColor(color[0], color[1], color[2]);
356   }
357   
358   // Add a new roi actor
359   mCurrentStructureSetActor->CreateNewROIActor(n, m_modeBG);
360   
361   // CheckBox for "All"
362   if (mCurrentStructureSetActor->GetROIActor(n)->IsVisible())
363     mNumberOfVisibleROI++;
364   if (mCurrentStructureSetActor->GetROIActor(n)->IsContourVisible())
365     mNumberOfVisibleContourROI++;
366   UpdateAllROIStatus();
367 }
368 //------------------------------------------------------------------------------
369
370
371 //------------------------------------------------------------------------------
372 void vvToolStructureSetManager::apply() 
373 {
374   close();
375 }
376 //------------------------------------------------------------------------------
377
378
379 //------------------------------------------------------------------------------
380 bool vvToolStructureSetManager::close()
381 {
382   //std::cout << "vvToolStructureSetManager::close()" << std::endl;
383
384   return vvToolWidgetBase::close();
385 }
386 //------------------------------------------------------------------------------
387
388
389 //------------------------------------------------------------------------------
390 void vvToolStructureSetManager::closeEvent(QCloseEvent *event) 
391 {
392   //std::cout << "vvToolStructureSetManager::closeEvent()" << std::endl;
393
394   std::vector<vvSlicerManager*>::iterator iter = std::find(mListOfInputs.begin(), mListOfInputs.end(), mCurrentSlicerManager);
395   if (iter != mListOfInputs.end()) mListOfInputs.erase(iter);
396   
397   //  DD("how delete mListOfOpenTool ???");
398   mListOfOpenTool.erase(mCurrentSlicerManager);
399
400   mCheckBoxShowAll->setCheckState(Qt::Unchecked);
401   mContourCheckBoxShowAll->setCheckState(Qt::Unchecked);
402   if (mCurrentSlicerManager != 0)
403     mCurrentSlicerManager->Render();
404
405   if (!isWindow()) {
406     if (m_NumberOfTool == 1) {
407       mMainWindow->GetTab()->removeTab(mTabNumber);
408     }
409   } 
410
411   event->accept();
412 }
413 //------------------------------------------------------------------------------
414
415
416 //------------------------------------------------------------------------------
417 // CURRENT ROI INTERACTION
418 //------------------------------------------------------------------------------
419
420
421 //------------------------------------------------------------------------------
422 void vvToolStructureSetManager::SelectedItemChangedInTree() {
423   
424   // Search which roi is selected
425   QList<QTreeWidgetItem *> l = mTree->selectedItems();
426   if (l.size() == 0) {
427     mCurrentROIActor = NULL;
428     mCurrentROI = NULL;
429     mGroupBoxROI->setEnabled(false);
430     return;
431   }
432   QTreeWidgetItem * w = l[0];
433   //std::cout << "selected item -> " << w->text(1).toStdString() << std::endl;
434   //std::cout << "m_NumberOfTool -> " << m_NumberOfTool << std::endl;
435   if (mMapTreeWidgetToROI.find(w) == mMapTreeWidgetToROI.end()) {
436     mCurrentROIActor = NULL;
437     mCurrentROI = NULL;
438     mGroupBoxROI->setEnabled(false);
439     return;
440   }
441   clitk::DicomRT_ROI * roi = mMapTreeWidgetToROI[w];
442   // Get selected roi actor
443   vvROIActor * actor = mStructureSetActorsList[mCurrentStructureSetIndex]->GetROIActor(roi->GetROINumber());
444   mCurrentROI = roi;
445   mCurrentROIActor = actor;
446
447   // Warning -> avoid unuseful Render here by disconnect slider 
448   // Update GUI
449   disconnect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
450   disconnect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
451   disconnect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
452   disconnect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
453   disconnect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));  
454   disconnect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
455   disconnect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
456   disconnect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
457
458   mGroupBoxROI->setEnabled(true);
459   mROInameLabel->setText(roi->GetName().c_str());
460   mCheckBoxShow->setChecked(actor->IsVisible());
461   mContourCheckBoxShow->setChecked(actor->IsContourVisible());
462   mContourWidthSpinBox->setValue(actor->GetContourWidth());
463   mDepthSpinBox->setValue(actor->GetDepth());
464   w->setText(3, QString("%1").arg(actor->GetDepth()));
465   mOpacitySlider->setValue((int)lrint(actor->GetOpacity()*100));
466   mOpacitySpinBox->setValue((int)lrint(actor->GetOpacity()*100));
467
468   connect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
469   connect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
470   connect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
471   connect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
472   connect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));  
473   connect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
474   connect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
475   connect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
476
477   // is this needed ?
478   //  actor->Update(); 
479   // Final rendering
480   // mCurrentSlicerManager->Render();
481 }
482 //------------------------------------------------------------------------------
483
484
485 //------------------------------------------------------------------------------
486 void vvToolStructureSetManager::UpdateAllROIStatus() {
487   int nbVisible = 0;
488   int nb = mCurrentStructureSetActor->GetROIList().size();
489   for(int i=0; i<nb; i++) {
490     if (mCurrentStructureSetActor->GetROIList()[i]->IsVisible()) {
491       nbVisible++;
492     }
493   }
494
495   disconnect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));  
496   disconnect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
497   if (nbVisible == nb) mCheckBoxShowAll->setCheckState(Qt::Checked);
498   else {
499     if (nbVisible == 0) mCheckBoxShowAll->setCheckState(Qt::Unchecked);
500     else mCheckBoxShowAll->setCheckState(Qt::PartiallyChecked);
501   }
502   connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
503   connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
504 }
505 //------------------------------------------------------------------------------
506
507 //------------------------------------------------------------------------------
508 void vvToolStructureSetManager::VisibleROIToggled(bool b) {
509   if (mCurrentROIActor == NULL) return;
510   if (b == mCurrentROIActor->IsVisible()) return; // nothing to do
511   mCurrentROIActor->SetVisible(b);
512   UpdateAllROIStatus();
513   mCurrentSlicerManager->Render(); 
514 }
515 //------------------------------------------------------------------------------
516
517
518 //------------------------------------------------------------------------------
519 void vvToolStructureSetManager::VisibleContourROIToggled(bool b) {
520   if (mCurrentROIActor == NULL) return;
521   if (mCurrentROIActor->IsContourVisible() == b) return; // nothing to do
522   mCurrentROIActor->SetContourVisible(b);
523   mCurrentROIActor->UpdateColor();
524   mCurrentSlicerManager->Render(); 
525 }
526 //------------------------------------------------------------------------------
527
528
529 //------------------------------------------------------------------------------
530 void vvToolStructureSetManager::OpacityChanged(int v) {
531   if (mCurrentROIActor == NULL) return;
532   mCurrentROIActor->SetOpacity((double)v/100.0);
533   mCurrentROIActor->UpdateColor();
534   mCurrentSlicerManager->Render(); 
535 }
536 //------------------------------------------------------------------------------
537
538
539 //------------------------------------------------------------------------------
540 void vvToolStructureSetManager::AllVisibleROIToggled(int b) {
541   bool status = false;
542   if ((mCheckBoxShowAll->checkState() == Qt::Checked) ||
543       (mCheckBoxShowAll->checkState() == Qt::PartiallyChecked))  status = true;
544
545   for(int i=0; i<mCurrentStructureSetActor->GetNumberOfROIs(); i++) {
546     mCurrentStructureSetActor->GetROIList()[i]->SetVisible(status);
547   }
548   if (status) mCheckBoxShowAll->setCheckState(Qt::Checked);
549   else  mCheckBoxShowAll->setCheckState(Qt::Unchecked);
550   mCheckBoxShow->setChecked(status);
551   mCurrentSlicerManager->Render(); 
552 }
553 //------------------------------------------------------------------------------
554
555
556 //------------------------------------------------------------------------------
557 void vvToolStructureSetManager::AllVisibleContourROIToggled(bool b) {
558   bool status = false;
559   if ((mContourCheckBoxShowAll->checkState() == Qt::Checked) ||
560       (mContourCheckBoxShowAll->checkState() == Qt::PartiallyChecked))  status = true;
561   // Update current 
562   for(int i=0; i<mCurrentStructureSetActor->GetNumberOfROIs(); i++) {
563     mCurrentStructureSetActor->GetROIList()[i]->SetContourVisible(status);
564   }
565   // Update current selection
566   if (status) mContourCheckBoxShowAll->setCheckState(Qt::Checked);
567   else  mContourCheckBoxShowAll->setCheckState(Qt::Unchecked);
568   mContourCheckBoxShow->setChecked(status);
569   mCurrentSlicerManager->Render(); 
570 }
571 //------------------------------------------------------------------------------
572
573
574 //------------------------------------------------------------------------------
575 void vvToolStructureSetManager::ChangeColor() {
576   QColor color;
577   color.setRgbF(mCurrentROIActor->GetROI()->GetDisplayColor()[0],
578                 mCurrentROIActor->GetROI()->GetDisplayColor()[1],
579                 mCurrentROIActor->GetROI()->GetDisplayColor()[2]);
580   QColor c = QColorDialog::getColor(color, this, "Choose the ROI color");
581   mCurrentROIActor->GetROI()->SetDisplayColor(c.redF(), c.greenF(), c.blueF());
582   mCurrentROIActor->UpdateColor();
583
584   QTreeWidgetItem * w = mMapROIToTreeWidget[mCurrentROI];
585   QBrush brush(QColor(mCurrentROI->GetDisplayColor()[0]*255,
586                       mCurrentROI->GetDisplayColor()[1]*255,
587                       mCurrentROI->GetDisplayColor()[2]*255));
588   brush.setStyle(Qt::SolidPattern);
589   w->setBackground(2, brush);
590   // Render
591   mCurrentSlicerManager->Render();
592 }
593 //------------------------------------------------------------------------------
594
595
596 //------------------------------------------------------------------------------
597 void vvToolStructureSetManager::ChangeContourColor() {
598   QColor color;
599   color.setRgbF(mCurrentROIActor->GetContourColor()[0], 
600                 mCurrentROIActor->GetContourColor()[1], 
601                 mCurrentROIActor->GetContourColor()[2]);
602   QColor c = QColorDialog::getColor(color, this, "Choose the contour color");
603   mCurrentROIActor->SetContourColor(c.redF(), c.greenF(), c.blueF());
604   mCurrentROIActor->UpdateColor();
605   mCurrentSlicerManager->Render();
606 }
607 //------------------------------------------------------------------------------
608
609
610 //------------------------------------------------------------------------------
611 void vvToolStructureSetManager::ChangeContourWidth(int n) {
612   mCurrentROIActor->SetContourWidth(n);
613   mCurrentROIActor->UpdateColor();
614   mCurrentSlicerManager->Render();
615 }
616 //------------------------------------------------------------------------------
617
618
619 //------------------------------------------------------------------------------
620 void vvToolStructureSetManager::ChangeDepth(int n) {
621   mCurrentROIActor->SetDepth(n);
622   mCurrentROIActor->UpdateImage();
623   mCurrentSlicerManager->Render();
624   QList<QTreeWidgetItem *> l = mTree->selectedItems();
625   QTreeWidgetItem * w = l[0];
626   w->setText(3, QString("%1").arg(mCurrentROIActor->GetDepth()));
627 }
628 //------------------------------------------------------------------------------
629
630
631 //------------------------------------------------------------------------------
632 void vvToolStructureSetManager::ReloadCurrentROI() {
633   // Reload image
634   vvImageReader::Pointer reader = vvImageReader::New();
635   reader->SetInputFilename(mCurrentROI->GetFilename());
636   reader->Update(vvImageReader::IMAGE);
637   if (reader->GetLastError() != "") {
638     QMessageBox::information(mMainWindowBase, tr("Sorry, error. Could not reload"), reader->GetLastError().c_str());
639     return;
640   }
641   mCurrentROI->GetImage()->GetFirstVTKImageData()->ReleaseData();
642   mCurrentROI->SetImage(reader->GetOutput());
643   
644   // Update visu"
645   mCurrentROIActor->UpdateImage();
646   mCurrentSlicerManager->Render();    
647 }
648 //------------------------------------------------------------------------------
649
650
651 //------------------------------------------------------------------------------
652 void vvToolStructureSetManager::CheckInputList(std::vector<vvSlicerManager*> & l, int & index) 
653 {
654   for(unsigned int i=0; i<l.size(); i++) {
655     std::vector<vvSlicerManager*>::iterator iter = std::find(mListOfInputs.begin(), mListOfInputs.end(), l[i]);
656     if (iter != mListOfInputs.end()) {
657       for(unsigned int j=i;j<l.size(); j++) l[j] = l[j+1];
658       l.pop_back();
659       if (index == (int)i) index = 0;
660       i--;
661     }
662   }
663 }
664 //------------------------------------------------------------------------------
665
666
667 //------------------------------------------------------------------------------
668 // STATIC
669 vvToolStructureSetManager * vvToolStructureSetManager::AddImage(vvSlicerManager * m, std::string name, vvImage::Pointer image, double BG, bool m_modeBG)
670 {
671   // If the tool is open for this vvSlicerManager, use it and return
672   if (mListOfOpenTool[m]) {
673     vvToolStructureSetManager * tool = mListOfOpenTool[m];
674     tool->AddImage(image, name, BG, m_modeBG);
675     tool->UpdateImage();
676     return tool;
677   }
678
679   // If the tool is not open, create it
680   vvToolStructureSetManager * tool = new vvToolStructureSetManager
681     (CREATOR(vvToolStructureSetManager)->GetMainWindow(), Qt::Dialog, m);
682   tool->AddImage(image, name, BG, m_modeBG);
683   tool->UpdateImage();
684   tool->show();
685   return tool;
686 }
687 //------------------------------------------------------------------------------