1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
20 #include "vvToolROIManager.h"
21 #include "vvImageReader.h"
22 #include "vvImageWriter.h"
23 #include "vvROIActor.h"
25 #include "vvROIActor.h"
26 #include "vvMeshReader.h"
27 #include "vvStructSelector.h"
28 #include "vvToolManager.h"
29 #include "vvProgressDialog.h"
32 #include "clitkDicomRTStruct2ImageFilter.h"
33 #include "clitkDicomRT_StructureSet.h"
36 #include <QFileDialog>
37 #include <QMessageBox>
38 #include <QColorDialog>
39 #include <QAbstractEventDispatcher>
40 #include <QXmlStreamReader>
43 #include <vtkLookupTable.h>
44 #include <vtkRenderWindow.h>
46 //------------------------------------------------------------------------------
47 // Create the tool and automagically (I like this word) insert it in
48 // the main window menu.
49 ADD_TOOL(vvToolROIManager);
50 //------------------------------------------------------------------------------
52 //------------------------------------------------------------------------------
53 vvToolROIManager::vvToolROIManager(vvMainWindowBase * parent, Qt::WindowFlags f):
54 QWidget(parent->GetTab()),
55 vvToolBase<vvToolROIManager>(parent),
56 Ui::vvToolROIManager()
61 // Assume the initial tab ROI index is 2
65 Ui_vvToolROIManager::setupUi(this);
66 setAttribute(Qt::WA_DeleteOnClose);
68 mTree->header()->resizeSection(0, 30);
69 mGroupBoxROI->setEnabled(false);
71 // Disable "Load dicom" button -> not useful
75 mDefaultLUTColor = vtkSmartPointer<vtkLookupTable>::New();
76 for(int i=0; i<mDefaultLUTColor->GetNumberOfTableValues(); i++) {
77 double r = (rand()/(RAND_MAX+1.0));
78 double v = (rand()/(RAND_MAX+1.0));
79 double b = (rand()/(RAND_MAX+1.0));
80 mDefaultLUTColor->SetTableValue(i, r, v, b);
82 #include "vvDefaultLut.h"
85 mCurrentSlicerManager = NULL;
86 mNumberOfVisibleROI = 0;
87 mNumberOfVisibleContourROI = 0;
88 mOpenFileBrowserFlag = true; // by default, open the file browser when the tool is launched
90 // InitializeNewTool must be called to start
92 //------------------------------------------------------------------------------
95 //------------------------------------------------------------------------------
96 vvToolROIManager::~vvToolROIManager()
98 mROIActorsList.clear();
100 //------------------------------------------------------------------------------
103 //------------------------------------------------------------------------------
105 void vvToolROIManager::Initialize() {
106 SetToolName("ROIManager");
107 SetToolMenuName("Open ROI (binary image or RT-STRUCT)");
108 SetToolIconFilename(":/common/icons/tool-roi.png");
109 SetToolTip("Display ROI from a binary image or a RT-struct file.");
110 SetToolExperimental(false);
112 //------------------------------------------------------------------------------
116 //------------------------------------------------------------------------------
117 void vvToolROIManager::InitializeNewTool(bool ReadStateFlag)
119 // Check if we need to start a new tool or read in the state file to load
120 if (ReadStateFlag == false) {
121 // Select the current image as the target
122 int i = mMainWindow->GetSlicerManagerCurrentIndex();
123 mCurrentSlicerManager = mMainWindow->GetSlicerManagers()[i];
124 // Set it as current (only if not ReadStateFlag)
125 mMainWindow->GetTab()->setCurrentIndex(mIndexFirstTab);
128 // Set the first tab in front to avoid displaying two roimanager
129 // in the same tab. Because toolcreatorBase do show() and I am too
130 // lazy to find another solution now.
131 mMainWindow->GetTab()->setCurrentIndex(0);
133 // Read all information in the XML
134 ReadXMLInformation();
136 // Check that a ROI is not already present
137 mInitialImageIndex += mImageIndex;
138 if (mInitialImageIndex >= mMainWindow->GetSlicerManagers().size()) {
139 QMessageBox::warning(this, "ROIManager tool", QString("Image index %1 not found, abort.").arg(mInitialImageIndex));
144 // Set the attached image
145 mCurrentSlicerManager = mMainWindow->GetSlicerManagers()[mInitialImageIndex];
148 // Tab insertion, check that another tool does not already exist for this image
149 std::vector<vvToolBaseBase*> & tools =
150 vvToolManager::GetInstance()->GetToolCreatorFromName(GetToolName())->GetListOfTool();
151 if (tools.size() > 0) {
152 for(uint i=0; i<tools.size()-1; i++) { // current tool is last
153 vvToolROIManager * t = dynamic_cast<vvToolROIManager*>(tools[i]);
154 if (mCurrentSlicerManager == t->GetCurrentSlicerManager()) {
155 QMessageBox::warning(this, "ROIManager tool", "Already a ROI for this image, abort.");
162 // Display tool in the correct tab
163 QWidget * tab = qFindChild<QWidget*>(mMainWindow->GetTab(), "ROItab");
164 tab->layout()->addWidget(this);
166 // If not read in a file we start automatically the browser to load
167 // a roi file (binary image)
169 mOpenFileBrowserFlag = false;
170 InputIsSelected(mCurrentSlicerManager);
171 mOpenFileBrowserFlag = true;
173 else InputIsSelected(mCurrentSlicerManager);
175 // Load ROI (if read in the XML files, empty otherwise)
176 OpenBinaryImage(mROIFilenames);
178 // Set the options to the open roi
179 for(uint i=0; i<mROIActorsParamList.size(); i++) {
180 QSharedPointer<vvROIActor> roi = mROIActorsList[i];
181 QSharedPointer<vvROIActor> roi_param = mROIActorsParamList[i];
182 roi->CopyParameters(roi_param);
185 QTreeWidgetItem * w = mMapROIToTreeWidget[roi->GetROI()];
186 QBrush brush(QColor(roi->GetROI()->GetDisplayColor()[0]*255,
187 roi->GetROI()->GetDisplayColor()[1]*255,
188 roi->GetROI()->GetDisplayColor()[2]*255));
189 brush.setStyle(Qt::SolidPattern);
190 w->setBackground(2, brush);
191 w->setText(3, QString("%1").arg(roi->GetDepth()));
197 UpdateAllROIStatus();
199 // Connect event from mainwindow to this widget
200 connect(mMainWindow, SIGNAL(AnImageIsBeingClosed(vvSlicerManager *)),
201 this, SLOT(AnImageIsBeingClosed(vvSlicerManager *)));
202 connect(mMainWindow, SIGNAL(SelectedImageHasChanged(vvSlicerManager *)),
203 this, SLOT(SelectedImageHasChanged(vvSlicerManager *)));
204 connect(mOpenBinaryButton, SIGNAL(clicked()), this, SLOT(Open()));
205 // connect(mOpenDicomButton, SIGNAL(clicked()), this, SLOT(OpenDicomImage()));
206 connect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
207 connect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
208 connect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
209 connect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
210 connect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
211 connect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
212 connect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
213 connect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
214 connect(mReloadButton, SIGNAL(clicked()), this, SLOT(ReloadCurrentROI()));
215 connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
216 connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
217 connect(mCloseButton, SIGNAL(clicked()), this, SLOT(close()));
219 //------------------------------------------------------------------------------
222 //------------------------------------------------------------------------------
223 void vvToolROIManager::InputIsSelected(vvSlicerManager *m)
226 mCurrentSlicerManager = m;
227 mCurrentImage = mCurrentSlicerManager->GetImage();
229 // Refuse if non 3D image
230 if (mCurrentImage->GetNumberOfDimensions() != 3) {
231 QMessageBox::information(this,tr("Sorry only 3D yet"), tr("Sorry only 3D yet"));
237 mLabelInputInfo->setText(QString("%1").arg(m->GetFileName().c_str()));
239 // Auto display browser to select new contours
240 if (mOpenFileBrowserFlag) Open();
242 //------------------------------------------------------------------------------
245 //------------------------------------------------------------------------------
246 void vvToolROIManager::AnImageIsBeingClosed(vvSlicerManager * m)
248 if (m == mCurrentSlicerManager) {
253 //------------------------------------------------------------------------------
256 //------------------------------------------------------------------------------
257 void vvToolROIManager::close()
259 disconnect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
260 disconnect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
261 disconnect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
262 disconnect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
263 disconnect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
264 disconnect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
265 disconnect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
266 disconnect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
269 for (unsigned int i = 0; i < mROIActorsList.size(); i++) {
270 mROIActorsList[i]->RemoveActors();
272 mROIActorsList.clear();
275 for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
276 mCurrentSlicerManager->GetSlicer(i)->Render();
279 //------------------------------------------------------------------------------
282 //------------------------------------------------------------------------------
283 void vvToolROIManager::SelectedImageHasChanged(vvSlicerManager * m) {
285 if (mCurrentSlicerManager == NULL) return;
286 if (m == NULL) return;
287 if (m != mCurrentSlicerManager) hide();
292 //------------------------------------------------------------------------------
295 //------------------------------------------------------------------------------
296 void vvToolROIManager::Open()
299 QString Extensions = "Images or Dicom-Struct files ( *.mha *.mhd *.hdr *.his *.dcm RS*)";
300 Extensions += ";;All Files (*)";
301 QStringList filename =
302 QFileDialog::getOpenFileNames(this,tr("Open binary image or DICOM RT Struct"),
303 mMainWindowBase->GetInputPathName(),Extensions);
304 if (filename.size() == 0) return;
305 if (filename.size() > 1) { OpenBinaryImage(filename); return; }
307 // Try to read dicom rt ?
308 clitk::DicomRT_StructureSet::Pointer s = clitk::DicomRT_StructureSet::New();
309 if (s->IsDicomRTStruct(filename[0].toStdString())) OpenDicomImage(filename[0].toStdString());
310 else OpenBinaryImage(filename);
313 //------------------------------------------------------------------------------
316 //------------------------------------------------------------------------------
317 void vvToolROIManager::OpenBinaryImage(QStringList & filename)
319 if (filename.size() == 0) return;
321 vvProgressDialog p("Reading ROI ...", true);
322 p.SetCancelButtonEnabled(false);
323 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
325 // For each selected file, open the image
326 for(int i=0; i<filename.size(); i++) {
327 p.SetProgress(i, filename.size());
330 vvImageReader::Pointer reader = vvImageReader::New();
331 std::vector<std::string> filenames;
332 filenames.push_back(filename[i].toStdString());
333 reader->SetInputFilenames(filenames);
334 reader->Update(vvImageReader::IMAGE);
336 if (reader->GetLastError().size() != 0) {
337 std::cerr << "Error while reading " << filename[i].toStdString() << std::endl;
338 QString error = "Cannot open file \n";
339 error += reader->GetLastError().c_str();
340 QMessageBox::information(this,tr("Reading problem"),error);
343 vvImage::Pointer binaryImage = reader->GetOutput();
344 AddImage(binaryImage, filename[i].toStdString(), mBackgroundValueSpinBox->value(),
345 (!mBGModeCheckBox->isChecked()));
346 mOpenedBinaryImageFilenames.push_back(filename[i]);
348 QApplication::restoreOverrideCursor();
350 // Update the contours
353 //------------------------------------------------------------------------------
356 //------------------------------------------------------------------------------
357 void vvToolROIManager::OpenDicomImage(std::string filename)
359 // GUI selector of roi
361 reader.SetFilename(filename);
363 vvStructSelector selector;
364 selector.SetStructures(reader.GetROINames());
365 selector.SetPropagationCheckBoxFlag(false);
367 if (selector.exec()) {
368 vvProgressDialog p("Reading ROI...", true);
369 p.SetCancelButtonEnabled(false);
370 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
373 clitk::DicomRT_StructureSet::Pointer s = clitk::DicomRT_StructureSet::New();
376 // Loop on selected struct
377 std::vector<int> list = selector.getSelectedItems();
378 for (uint i=0; i<list.size(); i++) {
379 p.SetProgress(i, list.size());
381 clitk::DicomRTStruct2ImageFilter filter;
382 filter.SetCropMaskEnabled(true);
383 filter.SetImage(mCurrentImage);
384 filter.SetROI(s->GetROIFromROINumber(list[i]));
385 filter.SetWriteOutputFlag(false);
389 vvImage::Pointer binaryImage = vvImage::New();
390 binaryImage->AddVtkImage(filter.GetOutput());
393 AddImage(binaryImage, s->GetROIFromROINumber(list[i])->GetName(), 0, true);
394 mOpenedBinaryImageFilenames.push_back(filename.c_str());
397 QApplication::restoreOverrideCursor();
399 // Update the contours
402 //------------------------------------------------------------------------------
405 //------------------------------------------------------------------------------
406 void vvToolROIManager::AddImage(vvImage * binaryImage, std::string filename,
407 double BG, bool modeBG)
410 int dim = mCurrentImage->GetNumberOfDimensions();
411 int bin_dim = binaryImage->GetNumberOfDimensions();
413 std::ostringstream os;
414 os << "Error. Loaded binary image is " << bin_dim
415 << "D while selected image is " << dim << "D" << std::endl;
416 QMessageBox::information(this,tr("Reading problem"),os.str().c_str());
421 int n = mROIList.size();
423 // Compute the name of the new ROI
424 std::ostringstream oss;
425 oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(filename));
426 std::string name = oss.str();
429 std::vector<double> color;
435 clitk::DicomRT_ROI::Pointer roi = clitk::DicomRT_ROI::New();
436 roi->SetFromBinaryImage(binaryImage, n, name, color, filename);
438 // Add a new roi to the list
439 mROIList.push_back(roi);
443 roi->SetBackgroundValueLabelImage(BG);
445 roi->SetForegroundValueLabelImage(BG);
448 if (n<mDefaultLUTColor->GetNumberOfTableValues ()) {
449 double * color = mDefaultLUTColor->GetTableValue(n % mDefaultLUTColor->GetNumberOfTableValues ());
450 roi->SetDisplayColor(color[0], color[1], color[2]);
453 // Add a new roi actor
454 QSharedPointer<vvROIActor> actor = QSharedPointer<vvROIActor>(new vvROIActor);
455 actor->SetBGMode(modeBG);
457 actor->SetSlicerManager(mCurrentSlicerManager);
458 actor->Initialize(n+1); // depth is n+1 to start at 1
459 mROIActorsList.push_back(actor);
461 // CheckBox for "All"
462 if (actor->IsVisible()) mNumberOfVisibleROI++;
463 if (actor->IsContourVisible()) mNumberOfVisibleContourROI++;
466 mTreeWidgetList.push_back(QSharedPointer<QTreeWidgetItem>(new QTreeWidgetItem(mTree)));
467 QTreeWidgetItem * w = mTreeWidgetList.back().data();
468 w->setText(0, QString("%1").arg(roi->GetROINumber()));
469 w->setText(1, QString("%1").arg(roi->GetName().c_str()));
470 w->setText(3, QString("%1").arg(actor->GetDepth()));
471 QBrush brush(QColor(roi->GetDisplayColor()[0]*255,
472 roi->GetDisplayColor()[1]*255,
473 roi->GetDisplayColor()[2]*255));
474 brush.setStyle(Qt::SolidPattern);
475 w->setBackground(2, brush);
476 mMapROIToTreeWidget[roi] = w;
477 mMapTreeWidgetToROI[w] = roi;
478 mTree->resizeColumnToContents(0);
479 mTree->resizeColumnToContents(1);
482 UpdateAllROIStatus();
484 //------------------------------------------------------------------------------
487 //------------------------------------------------------------------------------
488 void vvToolROIManager::UpdateAllContours()
490 if (mCurrentSlicerManager == NULL) return;
491 // Render loaded ROIs (the first is sufficient)
492 for(unsigned int i=0; i<mROIList.size(); i++) {
493 mROIActorsList[i]->Update();
495 mCurrentSlicerManager->Render();
497 //------------------------------------------------------------------------------
500 //------------------------------------------------------------------------------
501 void vvToolROIManager::UpdateAllROIStatus() {
503 int nb = mROIList.size();
504 for(int i=0; i<nb; i++) {
505 if (mROIActorsList[i]->IsVisible()) {
511 disconnect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
512 disconnect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
513 if (nbVisible == nb) mCheckBoxShowAll->setCheckState(Qt::Checked);
515 if (nbVisible == 0) mCheckBoxShowAll->setCheckState(Qt::Unchecked);
516 else mCheckBoxShowAll->setCheckState(Qt::PartiallyChecked);
518 connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
519 connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
521 //------------------------------------------------------------------------------
524 //------------------------------------------------------------------------------
525 void vvToolROIManager::SelectedItemChangedInTree() {
526 // Search which roi is selected
527 QList<QTreeWidgetItem *> l = mTree->selectedItems();
529 // mCurrentROIActor = 0;
531 mGroupBoxROI->setEnabled(false);
534 QTreeWidgetItem * w = l[0];
535 if (w == NULL) return;
537 if (mMapTreeWidgetToROI.find(w) == mMapTreeWidgetToROI.end()) {
538 // mCurrentROIActor = 0;
540 mGroupBoxROI->setEnabled(false);
543 if (w == NULL) return;
544 clitk::DicomRT_ROI * roi = mMapTreeWidgetToROI[w];
545 if (roi == NULL) return; // sometimes it is called while there is no roi anymore
547 // Get selected roi actor
548 int n = roi->GetROINumber();
549 QSharedPointer<vvROIActor> actor = mROIActorsList[n];
551 mCurrentROIActor = actor;
553 // Warning -> avoid unuseful Render here by disconnect slider
555 disconnect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
556 disconnect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
557 disconnect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
558 disconnect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
559 disconnect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
560 disconnect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
561 disconnect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
562 disconnect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
564 mROInameLabel->setText(roi->GetName().c_str());
565 mCheckBoxShow->setChecked(actor->IsVisible());
566 mContourCheckBoxShow->setChecked(actor->IsContourVisible());
567 mContourWidthSpinBox->setValue(actor->GetContourWidth());
568 mDepthSpinBox->setValue(actor->GetDepth());
569 w->setText(3, QString("%1").arg(actor->GetDepth()));
570 mOpacitySlider->setValue((int)lrint(actor->GetOpacity()*100));
571 mOpacitySpinBox->setValue((int)lrint(actor->GetOpacity()*100));
573 connect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
574 connect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
575 connect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
576 connect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
577 connect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
578 connect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
579 connect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
580 connect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
583 // Set the current color to the selected ROI name
584 mROInameLabel->setAutoFillBackground(true);// # This is important!!
585 // mROInameLabel->setStyleSheet("QLabel { background-color : red; color : blue; }");
586 QColor color = QColor(mCurrentROI->GetDisplayColor()[0]*255,
587 mCurrentROI->GetDisplayColor()[1]*255,
588 mCurrentROI->GetDisplayColor()[2]*255);
589 // QString values = QString("%1, %2, %3").arg(color.red()).arg(color.green()).arg(color.blue());
590 // mROInameLabel->setStyleSheet("QLabel { background-color: rgb("+values+"); }");
592 QPalette* palette = new QPalette();
593 QColor colorFG = QColor((1-mCurrentROI->GetDisplayColor()[0])*255,
594 (1-mCurrentROI->GetDisplayColor()[1])*255,
595 (1-mCurrentROI->GetDisplayColor()[2])*255);
596 palette->setColor(QPalette::WindowText,colorFG);
597 palette->setColor(QPalette::Background, color);
598 mROInameLabel->setPalette(*palette);
600 // Enable the group box (in case no selection before)
601 mGroupBoxROI->setEnabled(true);
603 //------------------------------------------------------------------------------
606 //------------------------------------------------------------------------------
607 void vvToolROIManager::VisibleROIToggled(bool b) {
608 if (mCurrentROIActor == NULL) return;
609 if (b == mCurrentROIActor->IsVisible()) return; // nothing to do
610 mCurrentROIActor->SetVisible(b);
611 UpdateAllROIStatus();
612 mCurrentSlicerManager->Render();
614 //------------------------------------------------------------------------------
617 //------------------------------------------------------------------------------
618 void vvToolROIManager::VisibleContourROIToggled(bool b) {
619 if (mCurrentROIActor == NULL) return;
620 if (mCurrentROIActor->IsContourVisible() == b) return; // nothing to do
621 mCurrentROIActor->SetContourVisible(b);
622 mCurrentROIActor->UpdateColor();
623 mCurrentSlicerManager->Render();
625 //------------------------------------------------------------------------------
628 //------------------------------------------------------------------------------
629 void vvToolROIManager::OpacityChanged(int v) {
630 if (mCurrentROIActor == NULL) return;
631 mCurrentROIActor->SetOpacity((double)v/100.0);
632 mCurrentROIActor->UpdateColor();
633 mCurrentSlicerManager->Render();
635 //------------------------------------------------------------------------------
638 //------------------------------------------------------------------------------
639 void vvToolROIManager::AllVisibleROIToggled(int b) {
641 if ((mCheckBoxShowAll->checkState() == Qt::Checked) ||
642 (mCheckBoxShowAll->checkState() == Qt::PartiallyChecked)) status = true;
644 for(uint i=0; i<mROIList.size(); i++) {
645 mROIActorsList[i]->SetVisible(status);
647 if (status) mCheckBoxShowAll->setCheckState(Qt::Checked);
648 else mCheckBoxShowAll->setCheckState(Qt::Unchecked);
649 mCheckBoxShow->setChecked(status);
650 mCurrentSlicerManager->Render();
652 //------------------------------------------------------------------------------
655 //------------------------------------------------------------------------------
656 void vvToolROIManager::AllVisibleContourROIToggled(bool b) {
658 if ((mContourCheckBoxShowAll->checkState() == Qt::Checked) ||
659 (mContourCheckBoxShowAll->checkState() == Qt::PartiallyChecked)) status = true;
661 for(uint i=0; i<mROIActorsList.size(); i++) {
662 mROIActorsList[i]->SetContourVisible(status);
664 // Update current selection
665 if (status) mContourCheckBoxShowAll->setCheckState(Qt::Checked);
666 else mContourCheckBoxShowAll->setCheckState(Qt::Unchecked);
667 mContourCheckBoxShow->setChecked(status);
668 mCurrentSlicerManager->Render();
670 //------------------------------------------------------------------------------
673 //------------------------------------------------------------------------------
674 void vvToolROIManager::ChangeColor() {
675 if (mCurrentROIActor == NULL) return;
677 color.setRgbF(mCurrentROIActor->GetROI()->GetDisplayColor()[0],
678 mCurrentROIActor->GetROI()->GetDisplayColor()[1],
679 mCurrentROIActor->GetROI()->GetDisplayColor()[2]);
680 QColor c = QColorDialog::getColor(color, this, "Choose the ROI color");
681 if (!c.isValid()) return;// User cancel
683 mCurrentROIActor->GetROI()->SetDisplayColor(c.redF(), c.greenF(), c.blueF());
684 mCurrentROIActor->UpdateColor();
686 QTreeWidgetItem * w = mMapROIToTreeWidget[mCurrentROI];
687 QBrush brush(QColor(mCurrentROI->GetDisplayColor()[0]*255,
688 mCurrentROI->GetDisplayColor()[1]*255,
689 mCurrentROI->GetDisplayColor()[2]*255));
690 brush.setStyle(Qt::SolidPattern);
691 w->setBackground(2, brush);
693 mCurrentSlicerManager->Render();
695 //------------------------------------------------------------------------------
698 //------------------------------------------------------------------------------
699 void vvToolROIManager::ChangeContourColor() {
700 if (mCurrentROIActor == NULL) return;
702 color.setRgbF(mCurrentROIActor->GetContourColor()[0],
703 mCurrentROIActor->GetContourColor()[1],
704 mCurrentROIActor->GetContourColor()[2]);
705 // QColorDialog d(color);
706 QColor c = QColorDialog::getColor(color, this, "Choose the contour color");
707 if (!c.isValid()) return; // User cancel
708 mCurrentROIActor->SetContourColor(c.redF(), c.greenF(), c.blueF());
709 mCurrentROIActor->UpdateColor();
710 mCurrentSlicerManager->Render();
712 //------------------------------------------------------------------------------
715 //------------------------------------------------------------------------------
716 void vvToolROIManager::ChangeContourWidth(int n) {
717 if (mCurrentROIActor == NULL) return;
718 mCurrentROIActor->SetContourWidth(n);
719 mCurrentROIActor->UpdateColor();
720 mCurrentSlicerManager->Render();
722 //------------------------------------------------------------------------------
725 //------------------------------------------------------------------------------
726 void vvToolROIManager::ChangeDepth(int n) {
727 if (mCurrentROIActor == NULL) return;
728 mCurrentROIActor->SetDepth(n);
729 // mCurrentROIActor->UpdateImage(); // FIXME
730 mCurrentSlicerManager->Render();
731 QList<QTreeWidgetItem *> l = mTree->selectedItems();
732 QTreeWidgetItem * w = l[0];
733 w->setText(3, QString("%1").arg(mCurrentROIActor->GetDepth()));
735 //------------------------------------------------------------------------------
738 //------------------------------------------------------------------------------
739 void vvToolROIManager::ReloadCurrentROI() {
740 if (mCurrentROI->GetImage()) {
741 return; // do nothing (contour from rt struct do not reload)
744 // Remove all contours/overlay first
745 bool visible = mCurrentROIActor->IsVisible();
746 bool cvisible = mCurrentROIActor->IsContourVisible();
747 mCurrentROIActor->SetVisible(false);
748 mCurrentROIActor->SetContourVisible(false);
749 mCurrentSlicerManager->Render();
752 vvImageReader::Pointer reader = vvImageReader::New();
753 reader->SetInputFilename(mCurrentROI->GetFilename());
754 reader->Update(vvImageReader::IMAGE);
755 if (reader->GetLastError() != "") {
756 // No message just ignore (because can be from dicom)
757 // QMessageBox::information(mMainWindowBase, tr("Sorry, error. Could not reload"),
758 // reader->GetLastError().c_str());
762 // Free the previous image
763 mCurrentROI->GetImage()->GetFirstVTKImageData()->ReleaseData(); // Needed to free
764 mCurrentROI->GetImage()->Reset();
765 mCurrentROI->SetImage(reader->GetOutput());
767 mCurrentROIActor->RemoveActors();
770 mCurrentROIActor->UpdateImage();
771 mCurrentROIActor->SetVisible(visible);
772 mCurrentROIActor->SetContourVisible(cvisible);
773 mCurrentSlicerManager->Render();
775 //------------------------------------------------------------------------------
778 //------------------------------------------------------------------------------
779 void vvToolROIManager::SaveState(std::auto_ptr<QXmlStreamWriter> & m_XmlWriter)
781 // Get index of the image
782 int n = mMainWindow->GetSlicerManagers().size();
784 for(int i=0; i<n; i++) {
785 if (mCurrentSlicerManager == mMainWindow->GetSlicerManagers()[i]) index = i;
788 std::cerr << "Error while writing state for ROIManager tool no currentimage founded." << std::endl;
791 m_XmlWriter->writeTextElement("Image_Index", QString::number(index));
795 for(uint i=0; i<mROIActorsList.size(); i++) {
796 QSharedPointer<vvROIActor> roi = mROIActorsList[i];
798 m_XmlWriter->writeStartElement("ROI");
799 m_XmlWriter->writeTextElement("Image", mOpenedBinaryImageFilenames[i]);
801 m_XmlWriter->writeStartElement("Overlay");
802 m_XmlWriter->writeAttribute("Red", QString("%1").arg(roi->GetOverlayColor()[0]));
803 m_XmlWriter->writeAttribute("Green",QString("%1").arg(roi->GetOverlayColor()[1]));
804 m_XmlWriter->writeAttribute("Blue", QString("%1").arg(roi->GetOverlayColor()[2]));
805 m_XmlWriter->writeAttribute("Visible", QString("%1").arg(roi->IsVisible()));
806 m_XmlWriter->writeAttribute("Opacity", QString("%1").arg(roi->GetOpacity()));
807 m_XmlWriter->writeAttribute("Depth", QString("%1").arg(roi->GetDepth()));
808 m_XmlWriter->writeEndElement();
810 m_XmlWriter->writeStartElement("Contour");
811 m_XmlWriter->writeAttribute("Red", QString("%1").arg(roi->GetContourColor()[0]));
812 m_XmlWriter->writeAttribute("Green",QString("%1").arg(roi->GetContourColor()[1]));
813 m_XmlWriter->writeAttribute("Blue", QString("%1").arg(roi->GetContourColor()[2]));
814 m_XmlWriter->writeAttribute("Visible", QString("%1").arg(roi->IsContourVisible()));
815 m_XmlWriter->writeAttribute("Width", QString("%1").arg(roi->GetContourWidth()));
816 m_XmlWriter->writeEndElement();
818 m_XmlWriter->writeEndElement();
821 //------------------------------------------------------------------------------
824 //------------------------------------------------------------------------------
825 void vvToolROIManager::ReadXMLInformation()
827 std::string value="";
828 mInitialImageIndex = -1;
829 while (!(m_XmlReader->isEndElement() && value == GetToolName().toStdString())) {
830 m_XmlReader->readNext();
831 value = m_XmlReader->qualifiedName().toString().toStdString();
833 if (value == "Image_Index")
834 mInitialImageIndex = m_XmlReader->readElementText().toInt();
836 if (m_XmlReader->isStartElement()) {
837 if (value == "ROI") {
838 ReadXMLInformation_ROI();
843 //------------------------------------------------------------------------------
846 //------------------------------------------------------------------------------
847 void vvToolROIManager::ReadXMLInformation_ROI()
850 std::string value="";
851 QSharedPointer<vvROIActor> param = QSharedPointer<vvROIActor>(new vvROIActor);
852 param->SetVisible(true);
853 clitk::DicomRT_ROI::Pointer roi = clitk::DicomRT_ROI::New();
854 // r->SetDisplayColor(1,1,1);
857 float r=1.0,g=1.0,b=1.0;
858 float cr=1.0,cg=1.0,cb=1.0;
861 bool cvisible = true;
865 while (!(m_XmlReader->isEndElement() && value == "ROI")) {
866 m_XmlReader->readNext();
867 value = m_XmlReader->qualifiedName().toString().toStdString();
868 if (value == "Image") {
869 s = m_XmlReader->readElementText();
872 if (value == "Overlay" && m_XmlReader->isStartElement()) {
873 QXmlStreamAttributes attributes = m_XmlReader->attributes();
874 if (!m_XmlReader->hasError())
875 r = attributes.value("Red").toString().toFloat();
876 if (!m_XmlReader->hasError())
877 g = attributes.value("Green").toString().toFloat();
878 if (!m_XmlReader->hasError())
879 b = attributes.value("Blue").toString().toFloat();
880 if (!m_XmlReader->hasError())
881 visible = attributes.value("Visible").toString().toInt();
882 if (!m_XmlReader->hasError())
883 opacity = attributes.value("Opacity").toString().toFloat();
884 if (!m_XmlReader->hasError())
885 depth = attributes.value("Depth").toString().toFloat();
889 if (value == "Contour" && m_XmlReader->isStartElement()) {
890 QXmlStreamAttributes attributes = m_XmlReader->attributes();
891 if (!m_XmlReader->hasError())
892 cr = attributes.value("Red").toString().toFloat();
893 if (!m_XmlReader->hasError())
894 cg = attributes.value("Green").toString().toFloat();
895 if (!m_XmlReader->hasError())
896 cb = attributes.value("Blue").toString().toFloat();
897 if (!m_XmlReader->hasError())
898 cvisible = attributes.value("Visible").toString().toInt();
899 if (!m_XmlReader->hasError())
900 width = attributes.value("Width").toString().toFloat();
902 param->SetOverlayColor(r,g,b);
903 param->SetVisible(visible);
904 param->SetOpacity(opacity);
905 param->SetDepth(depth);
907 param->SetContourColor(cr,cg,cb);
908 param->SetContourVisible(cvisible);
909 param->SetContourWidth(width);
911 mROIFilenames.push_back(s);
912 mROIActorsParamList.push_back(param);
914 //------------------------------------------------------------------------------