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 "vvROIActor.h"
24 #include "vvROIActor.h"
25 #include "vvMeshReader.h"
26 #include "vvStructSelector.h"
27 #include "vvToolManager.h"
30 #include <QFileDialog>
31 #include <QMessageBox>
32 #include <QColorDialog>
33 #include <QAbstractEventDispatcher>
34 #include <QXmlStreamReader>
37 #include <vtkLookupTable.h>
38 #include <vtkRenderWindow.h>
40 //------------------------------------------------------------------------------
41 // Create the tool and automagically (I like this word) insert it in
42 // the main window menu.
43 ADD_TOOL(vvToolROIManager);
44 //------------------------------------------------------------------------------
46 //------------------------------------------------------------------------------
47 vvToolROIManager::vvToolROIManager(vvMainWindowBase * parent, Qt::WindowFlags f):
48 QWidget(parent->GetTab()),
49 vvToolBase<vvToolROIManager>(parent),
50 Ui::vvToolROIManager()
55 // Assume the initial tab ROI index is 2
59 Ui_vvToolROIManager::setupUi(this);
60 setAttribute(Qt::WA_DeleteOnClose);
62 mTree->header()->resizeSection(0, 30);
63 mGroupBoxROI->setEnabled(false);
65 // Temporary disable "Load dicom" button
69 mDefaultLUTColor = vtkSmartPointer<vtkLookupTable>::New();
70 for(int i=0; i<mDefaultLUTColor->GetNumberOfTableValues(); i++) {
71 double r = (rand()/(RAND_MAX+1.0));
72 double v = (rand()/(RAND_MAX+1.0));
73 double b = (rand()/(RAND_MAX+1.0));
74 mDefaultLUTColor->SetTableValue(i, r, v, b);
76 #include "vvDefaultLut.h"
79 mCurrentSlicerManager = NULL;
80 mNumberOfVisibleROI = 0;
81 mNumberOfVisibleContourROI = 0;
82 mOpenFileBrowserFlag = true; // by default, open the file browser when the tool is launched
84 // InitializeNewTool must be called to start
86 //------------------------------------------------------------------------------
89 //------------------------------------------------------------------------------
90 vvToolROIManager::~vvToolROIManager()
92 mROIActorsList.clear();
94 //------------------------------------------------------------------------------
97 //------------------------------------------------------------------------------
99 void vvToolROIManager::Initialize() {
100 SetToolName("ROIManager");
101 SetToolMenuName("Display ROI (binary image)");
102 SetToolIconFilename(":/common/icons/tool-roi.png");
103 SetToolTip("Display ROI from a binary image.");
104 SetToolExperimental(false);
106 //------------------------------------------------------------------------------
110 //------------------------------------------------------------------------------
111 void vvToolROIManager::InitializeNewTool(bool ReadStateFlag)
113 // Check if we need to start a new tool or read in the state file to load
114 if (ReadStateFlag == false) {
115 // Select the current image as the target
116 int i = mMainWindow->GetSlicerManagerCurrentIndex();
117 mCurrentSlicerManager = mMainWindow->GetSlicerManagers()[i];
118 // Set it as current (only if not ReadStateFlag)
119 mMainWindow->GetTab()->setCurrentIndex(mIndexFirstTab);
122 // Set the first tab in front to avoid displaying two roimanager
123 // in the same tab. Because toolcreatorBase do show() and I am too
124 // lazy to find another solution now.
125 mMainWindow->GetTab()->setCurrentIndex(0);
127 // Read all information in the XML
128 ReadXMLInformation();
130 // Check that a ROI is not already present
131 mInitialImageIndex += mImageIndex;
132 if (mInitialImageIndex >= mMainWindow->GetSlicerManagers().size()) {
133 QMessageBox::warning(this, "ROIManager tool", QString("Image index %1 not found, abort.").arg(mInitialImageIndex));
138 // Set the attached image
139 mCurrentSlicerManager = mMainWindow->GetSlicerManagers()[mInitialImageIndex];
142 // Tab insertion, check that another tool does not already exist for this image
143 std::vector<vvToolBaseBase*> & tools =
144 vvToolManager::GetInstance()->GetToolCreatorFromName(GetToolName())->GetListOfTool();
145 if (tools.size() > 0) {
146 for(uint i=0; i<tools.size()-1; i++) { // current tool is last
147 vvToolROIManager * t = dynamic_cast<vvToolROIManager*>(tools[i]);
148 if (mCurrentSlicerManager == t->GetCurrentSlicerManager()) {
149 QMessageBox::warning(this, "ROIManager tool", "Already a ROI for this image, abort.");
156 // Display tool in the correct tab
157 QWidget * tab = qFindChild<QWidget*>(mMainWindow->GetTab(), "ROItab");
158 tab->layout()->addWidget(this);
160 // If not read in a file we start automatically the browser to load
161 // a roi file (binary image)
163 mOpenFileBrowserFlag = false;
164 InputIsSelected(mCurrentSlicerManager);
165 mOpenFileBrowserFlag = true;
167 else InputIsSelected(mCurrentSlicerManager);
169 // Load ROI (if read in the XML files, empty otherwise)
170 OpenBinaryImage(mROIFilenames);
172 // Set the options to the open roi
173 for(uint i=0; i<mROIActorsParamList.size(); i++) {
174 QSharedPointer<vvROIActor> roi = mROIActorsList[i];
175 QSharedPointer<vvROIActor> roi_param = mROIActorsParamList[i];
176 roi->CopyParameters(roi_param);
179 QTreeWidgetItem * w = mMapROIToTreeWidget[roi->GetROI()];
180 QBrush brush(QColor(roi->GetROI()->GetDisplayColor()[0]*255,
181 roi->GetROI()->GetDisplayColor()[1]*255,
182 roi->GetROI()->GetDisplayColor()[2]*255));
183 brush.setStyle(Qt::SolidPattern);
184 w->setBackground(2, brush);
185 w->setText(3, QString("%1").arg(roi->GetDepth()));
191 UpdateAllROIStatus();
193 // Connect event from mainwindow to this widget
194 connect(mMainWindow, SIGNAL(AnImageIsBeingClosed(vvSlicerManager *)),
195 this, SLOT(AnImageIsBeingClosed(vvSlicerManager *)));
196 connect(mMainWindow, SIGNAL(SelectedImageHasChanged(vvSlicerManager *)),
197 this, SLOT(SelectedImageHasChanged(vvSlicerManager *)));
198 connect(mOpenBinaryButton, SIGNAL(clicked()), this, SLOT(OpenBinaryImage()));
199 connect(mOpenDicomButton, SIGNAL(clicked()), this, SLOT(OpenDicomImage()));
200 connect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
201 connect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
202 connect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
203 connect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
204 connect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
205 connect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
206 connect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
207 connect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
208 connect(mReloadButton, SIGNAL(clicked()), this, SLOT(ReloadCurrentROI()));
209 connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
210 connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
211 connect(mCloseButton, SIGNAL(clicked()), this, SLOT(close()));
213 //------------------------------------------------------------------------------
216 //------------------------------------------------------------------------------
217 void vvToolROIManager::InputIsSelected(vvSlicerManager *m)
220 mCurrentSlicerManager = m;
221 mCurrentImage = mCurrentSlicerManager->GetImage();
223 // Refuse if non 3D image
224 if (mCurrentImage->GetNumberOfDimensions() != 3) {
225 QMessageBox::information(this,tr("Sorry only 3D yet"), tr("Sorry only 3D yet"));
231 mLabelInputInfo->setText(QString("%1").arg(m->GetFileName().c_str()));
233 // Auto display browser to select new contours
234 if (mOpenFileBrowserFlag) OpenBinaryImage();
236 //------------------------------------------------------------------------------
239 //------------------------------------------------------------------------------
240 void vvToolROIManager::AnImageIsBeingClosed(vvSlicerManager * m)
242 if (m == mCurrentSlicerManager) {
247 //------------------------------------------------------------------------------
250 //------------------------------------------------------------------------------
251 void vvToolROIManager::close()
253 disconnect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
254 disconnect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
255 disconnect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
256 disconnect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
257 disconnect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
258 disconnect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
259 disconnect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
260 disconnect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
263 for (unsigned int i = 0; i < mROIActorsList.size(); i++) {
264 mROIActorsList[i]->RemoveActors();
266 mROIActorsList.clear();
269 for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
270 mCurrentSlicerManager->GetSlicer(i)->Render();
273 //------------------------------------------------------------------------------
276 //------------------------------------------------------------------------------
277 void vvToolROIManager::SelectedImageHasChanged(vvSlicerManager * m) {
279 if (mCurrentSlicerManager == NULL) return;
280 if (m == NULL) return;
281 if (m != mCurrentSlicerManager) hide();
286 //------------------------------------------------------------------------------
289 //------------------------------------------------------------------------------
290 void vvToolROIManager::OpenBinaryImage()
293 QString Extensions = "Images files ( *.mha *.mhd *.hdr *.his)";
294 Extensions += ";;All Files (*)";
295 QStringList filename =
296 QFileDialog::getOpenFileNames(this,tr("Open binary image"),
297 mMainWindowBase->GetInputPathName(),Extensions);
298 OpenBinaryImage(filename);
300 //------------------------------------------------------------------------------
303 //------------------------------------------------------------------------------
304 void vvToolROIManager::OpenBinaryImage(QStringList & filename)
306 if (filename.size() == 0) return;
308 // For each selected file, open the image
309 for(int i=0; i<filename.size(); i++) {
311 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
312 vvImageReader::Pointer reader = vvImageReader::New();
313 std::vector<std::string> filenames;
314 filenames.push_back(filename[i].toStdString());
315 reader->SetInputFilenames(filenames);
316 reader->Update(vvImageReader::IMAGE);
317 QApplication::restoreOverrideCursor();
319 if (reader->GetLastError().size() != 0) {
320 std::cerr << "Error while reading " << filename[i].toStdString() << std::endl;
321 QString error = "Cannot open file \n";
322 error += reader->GetLastError().c_str();
323 QMessageBox::information(this,tr("Reading problem"),error);
326 vvImage::Pointer binaryImage = reader->GetOutput();
327 AddImage(binaryImage, filename[i].toStdString(), mBackgroundValueSpinBox->value(),
328 (!mBGModeCheckBox->isChecked()));
329 // mOpenedBinaryImage.push_back(binaryImage);
330 mOpenedBinaryImageFilenames.push_back(filename[i]);
331 //mMapImageToIndex[binaryImage]=mOpenedBinaryImageFilenames.size()-1;
334 // Update the contours
337 //------------------------------------------------------------------------------
340 //------------------------------------------------------------------------------
341 void vvToolROIManager::OpenDicomImage()
343 DD("OpenDicomImage");
344 QString Extensions = "Dicom Files ( *.dcm RS*)";
345 Extensions += ";;All Files (*)";
346 QString file = QFileDialog::getOpenFileName(this,tr("Merge Images"),
347 mMainWindow->GetInputPathName(),
349 if (file.isNull()) return;
351 // AddDCStructContour(index, file);
353 reader.SetFilename(file.toStdString());
354 vvStructSelector selector;
355 selector.SetStructures(reader.GetROINames());
356 // selector.EnablePropagationCheckBox(); FIXME Disable
358 // FIXME : change text -> allow to save binary image
360 if (selector.exec()) {
361 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
362 reader.SetSelectedItems(selector.getSelectedItems());
363 reader.SetImage(mCurrentSlicerManager->GetImage());
366 // std::vector<vvMesh::Pointer> contours=reader.GetOutput();
367 // for (std::vector<vvMesh::Pointer>::iterator i=contours.begin();
368 // i!=contours.end(); i++)
369 // AddContour(index,*i,selector.PropagationEnabled());
370 QApplication::restoreOverrideCursor();
376 //------------------------------------------------------------------------------
379 //------------------------------------------------------------------------------
380 void vvToolROIManager::AddImage(vvImage * binaryImage, std::string filename,
381 double BG, bool modeBG)
384 int dim = mCurrentImage->GetNumberOfDimensions();
385 int bin_dim = binaryImage->GetNumberOfDimensions();
387 std::ostringstream os;
388 os << "Error. Loaded binary image is " << bin_dim
389 << "D while selected image is " << dim << "D" << std::endl;
390 QMessageBox::information(this,tr("Reading problem"),os.str().c_str());
395 int n = mROIList.size();
397 // Compute the name of the new ROI
398 std::ostringstream oss;
399 oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(filename));
400 std::string name = oss.str();
403 std::vector<double> color;
409 clitk::DicomRT_ROI::Pointer roi = clitk::DicomRT_ROI::New();
410 roi->SetFromBinaryImage(binaryImage, n, name, color, filename);
412 // Add a new roi to the list
413 mROIList.push_back(roi);
417 roi->SetBackgroundValueLabelImage(BG);
419 roi->SetForegroundValueLabelImage(BG);
422 if (n<mDefaultLUTColor->GetNumberOfTableValues ()) {
423 double * color = mDefaultLUTColor->GetTableValue(n % mDefaultLUTColor->GetNumberOfTableValues ());
424 roi->SetDisplayColor(color[0], color[1], color[2]);
427 // Add a new roi actor
428 QSharedPointer<vvROIActor> actor = QSharedPointer<vvROIActor>(new vvROIActor);
429 actor->SetBGMode(modeBG);
431 actor->SetSlicerManager(mCurrentSlicerManager);
432 actor->Initialize(n+1); // depth is n+1 to start at 1
433 mROIActorsList.push_back(actor);
435 // CheckBox for "All"
436 if (actor->IsVisible()) mNumberOfVisibleROI++;
437 if (actor->IsContourVisible()) mNumberOfVisibleContourROI++;
440 mTreeWidgetList.push_back(QSharedPointer<QTreeWidgetItem>(new QTreeWidgetItem(mTree)));
441 QTreeWidgetItem * w = mTreeWidgetList.back().data();
442 w->setText(0, QString("%1").arg(roi->GetROINumber()));
443 w->setText(1, QString("%1").arg(roi->GetName().c_str()));
444 w->setText(3, QString("%1").arg(actor->GetDepth()));
445 QBrush brush(QColor(roi->GetDisplayColor()[0]*255,
446 roi->GetDisplayColor()[1]*255,
447 roi->GetDisplayColor()[2]*255));
448 brush.setStyle(Qt::SolidPattern);
449 w->setBackground(2, brush);
450 mMapROIToTreeWidget[roi] = w;
451 mMapTreeWidgetToROI[w] = roi;
452 mTree->resizeColumnToContents(0);
453 mTree->resizeColumnToContents(1);
456 UpdateAllROIStatus();
458 //------------------------------------------------------------------------------
461 //------------------------------------------------------------------------------
462 void vvToolROIManager::UpdateAllContours()
464 if (mCurrentSlicerManager == NULL) return;
465 // Render loaded ROIs (the first is sufficient)
466 for(unsigned int i=0; i<mROIList.size(); i++) {
467 mROIActorsList[i]->Update();
469 for(int i=0; i<mCurrentSlicerManager->GetNumberOfSlicers(); i++) {
470 mCurrentSlicerManager->GetSlicer(i)->Render();
473 //------------------------------------------------------------------------------
476 //------------------------------------------------------------------------------
477 void vvToolROIManager::UpdateAllROIStatus() {
479 int nb = mROIList.size();
480 for(int i=0; i<nb; i++) {
481 if (mROIActorsList[i]->IsVisible()) {
487 disconnect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
488 disconnect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
489 if (nbVisible == nb) mCheckBoxShowAll->setCheckState(Qt::Checked);
491 if (nbVisible == 0) mCheckBoxShowAll->setCheckState(Qt::Unchecked);
492 else mCheckBoxShowAll->setCheckState(Qt::PartiallyChecked);
494 connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
495 connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
497 //------------------------------------------------------------------------------
500 //------------------------------------------------------------------------------
501 void vvToolROIManager::SelectedItemChangedInTree() {
502 // Search which roi is selected
503 QList<QTreeWidgetItem *> l = mTree->selectedItems();
505 // mCurrentROIActor = 0;
507 mGroupBoxROI->setEnabled(false);
510 QTreeWidgetItem * w = l[0];
511 if (w == NULL) return;
513 if (mMapTreeWidgetToROI.find(w) == mMapTreeWidgetToROI.end()) {
514 // mCurrentROIActor = 0;
516 mGroupBoxROI->setEnabled(false);
519 if (w == NULL) return;
520 clitk::DicomRT_ROI * roi = mMapTreeWidgetToROI[w];
521 if (roi == NULL) return; // sometimes it is called while there is no roi anymore
523 // Get selected roi actor
524 int n = roi->GetROINumber();
525 QSharedPointer<vvROIActor> actor = mROIActorsList[n];
527 mCurrentROIActor = actor;
529 // Warning -> avoid unuseful Render here by disconnect slider
531 disconnect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
532 disconnect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
533 disconnect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
534 disconnect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
535 disconnect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
536 disconnect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
537 disconnect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
538 disconnect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
540 mROInameLabel->setText(roi->GetName().c_str());
541 mCheckBoxShow->setChecked(actor->IsVisible());
542 mContourCheckBoxShow->setChecked(actor->IsContourVisible());
543 mContourWidthSpinBox->setValue(actor->GetContourWidth());
544 mDepthSpinBox->setValue(actor->GetDepth());
545 w->setText(3, QString("%1").arg(actor->GetDepth()));
546 mOpacitySlider->setValue((int)lrint(actor->GetOpacity()*100));
547 mOpacitySpinBox->setValue((int)lrint(actor->GetOpacity()*100));
549 connect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
550 connect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
551 connect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
552 connect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
553 connect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
554 connect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
555 connect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
556 connect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
559 // Set the current color to the selected ROI name
560 mROInameLabel->setAutoFillBackground(true);// # This is important!!
561 // mROInameLabel->setStyleSheet("QLabel { background-color : red; color : blue; }");
562 QColor color = QColor(mCurrentROI->GetDisplayColor()[0]*255,
563 mCurrentROI->GetDisplayColor()[1]*255,
564 mCurrentROI->GetDisplayColor()[2]*255);
565 // QString values = QString("%1, %2, %3").arg(color.red()).arg(color.green()).arg(color.blue());
566 // mROInameLabel->setStyleSheet("QLabel { background-color: rgb("+values+"); }");
568 QPalette* palette = new QPalette();
569 QColor colorFG = QColor((1-mCurrentROI->GetDisplayColor()[0])*255,
570 (1-mCurrentROI->GetDisplayColor()[1])*255,
571 (1-mCurrentROI->GetDisplayColor()[2])*255);
572 palette->setColor(QPalette::WindowText,colorFG);
573 palette->setColor(QPalette::Background, color);
574 mROInameLabel->setPalette(*palette);
576 // Enable the group box (in case no selection before)
577 mGroupBoxROI->setEnabled(true);
579 //------------------------------------------------------------------------------
582 //------------------------------------------------------------------------------
583 void vvToolROIManager::VisibleROIToggled(bool b) {
584 if (mCurrentROIActor == NULL) return;
585 if (b == mCurrentROIActor->IsVisible()) return; // nothing to do
586 mCurrentROIActor->SetVisible(b);
587 UpdateAllROIStatus();
588 mCurrentSlicerManager->Render();
590 //------------------------------------------------------------------------------
593 //------------------------------------------------------------------------------
594 void vvToolROIManager::VisibleContourROIToggled(bool b) {
595 if (mCurrentROIActor == NULL) return;
596 if (mCurrentROIActor->IsContourVisible() == b) return; // nothing to do
597 mCurrentROIActor->SetContourVisible(b);
598 mCurrentROIActor->UpdateColor();
599 mCurrentSlicerManager->Render();
601 //------------------------------------------------------------------------------
604 //------------------------------------------------------------------------------
605 void vvToolROIManager::OpacityChanged(int v) {
606 if (mCurrentROIActor == NULL) return;
607 mCurrentROIActor->SetOpacity((double)v/100.0);
608 mCurrentROIActor->UpdateColor();
609 mCurrentSlicerManager->Render();
611 //------------------------------------------------------------------------------
614 //------------------------------------------------------------------------------
615 void vvToolROIManager::AllVisibleROIToggled(int b) {
617 if ((mCheckBoxShowAll->checkState() == Qt::Checked) ||
618 (mCheckBoxShowAll->checkState() == Qt::PartiallyChecked)) status = true;
620 for(uint i=0; i<mROIList.size(); i++) {
621 mROIActorsList[i]->SetVisible(status);
623 if (status) mCheckBoxShowAll->setCheckState(Qt::Checked);
624 else mCheckBoxShowAll->setCheckState(Qt::Unchecked);
625 mCheckBoxShow->setChecked(status);
626 mCurrentSlicerManager->Render();
628 //------------------------------------------------------------------------------
631 //------------------------------------------------------------------------------
632 void vvToolROIManager::AllVisibleContourROIToggled(bool b) {
634 if ((mContourCheckBoxShowAll->checkState() == Qt::Checked) ||
635 (mContourCheckBoxShowAll->checkState() == Qt::PartiallyChecked)) status = true;
637 for(uint i=0; i<mROIActorsList.size(); i++) {
638 mROIActorsList[i]->SetContourVisible(status);
640 // Update current selection
641 if (status) mContourCheckBoxShowAll->setCheckState(Qt::Checked);
642 else mContourCheckBoxShowAll->setCheckState(Qt::Unchecked);
643 mContourCheckBoxShow->setChecked(status);
644 mCurrentSlicerManager->Render();
646 //------------------------------------------------------------------------------
649 //------------------------------------------------------------------------------
650 void vvToolROIManager::ChangeColor() {
651 if (mCurrentROIActor == NULL) return;
653 color.setRgbF(mCurrentROIActor->GetROI()->GetDisplayColor()[0],
654 mCurrentROIActor->GetROI()->GetDisplayColor()[1],
655 mCurrentROIActor->GetROI()->GetDisplayColor()[2]);
656 QColor c = QColorDialog::getColor(color, this, "Choose the ROI color");
657 mCurrentROIActor->GetROI()->SetDisplayColor(c.redF(), c.greenF(), c.blueF());
658 mCurrentROIActor->UpdateColor();
660 QTreeWidgetItem * w = mMapROIToTreeWidget[mCurrentROI];
661 QBrush brush(QColor(mCurrentROI->GetDisplayColor()[0]*255,
662 mCurrentROI->GetDisplayColor()[1]*255,
663 mCurrentROI->GetDisplayColor()[2]*255));
664 brush.setStyle(Qt::SolidPattern);
665 w->setBackground(2, brush);
667 mCurrentSlicerManager->Render();
669 //------------------------------------------------------------------------------
672 //------------------------------------------------------------------------------
673 void vvToolROIManager::ChangeContourColor() {
674 if (mCurrentROIActor == NULL) return;
676 color.setRgbF(mCurrentROIActor->GetContourColor()[0],
677 mCurrentROIActor->GetContourColor()[1],
678 mCurrentROIActor->GetContourColor()[2]);
679 // QColorDialog d(color);
680 QColor c = QColorDialog::getColor(color, this, "Choose the contour color");
681 if (!c.isValid()) return; // User cancel
682 mCurrentROIActor->SetContourColor(c.redF(), c.greenF(), c.blueF());
683 mCurrentROIActor->UpdateColor();
684 mCurrentSlicerManager->Render();
686 //------------------------------------------------------------------------------
689 //------------------------------------------------------------------------------
690 void vvToolROIManager::ChangeContourWidth(int n) {
691 if (mCurrentROIActor == NULL) return;
692 mCurrentROIActor->SetContourWidth(n);
693 mCurrentROIActor->UpdateColor();
694 mCurrentSlicerManager->Render();
696 //------------------------------------------------------------------------------
699 //------------------------------------------------------------------------------
700 void vvToolROIManager::ChangeDepth(int n) {
701 if (mCurrentROIActor == NULL) return;
702 mCurrentROIActor->SetDepth(n);
703 // mCurrentROIActor->UpdateImage(); // FIXME
704 mCurrentSlicerManager->Render();
705 QList<QTreeWidgetItem *> l = mTree->selectedItems();
706 QTreeWidgetItem * w = l[0];
707 w->setText(3, QString("%1").arg(mCurrentROIActor->GetDepth()));
709 //------------------------------------------------------------------------------
712 //------------------------------------------------------------------------------
713 void vvToolROIManager::ReloadCurrentROI() {
715 // Remove all contours/overlay first
716 bool visible = mCurrentROIActor->IsVisible();
717 bool cvisible = mCurrentROIActor->IsContourVisible();
718 mCurrentROIActor->SetVisible(false);
719 mCurrentROIActor->SetContourVisible(false);
720 mCurrentSlicerManager->Render();
723 vvImageReader::Pointer reader = vvImageReader::New();
724 reader->SetInputFilename(mCurrentROI->GetFilename());
725 reader->Update(vvImageReader::IMAGE);
726 if (reader->GetLastError() != "") {
727 QMessageBox::information(mMainWindowBase, tr("Sorry, error. Could not reload"),
728 reader->GetLastError().c_str());
732 // Free the previous image
733 mCurrentROI->GetImage()->GetFirstVTKImageData()->ReleaseData(); // Needed to free
734 mCurrentROI->GetImage()->Reset();
735 mCurrentROI->SetImage(reader->GetOutput());
737 mCurrentROIActor->RemoveActors();
740 mCurrentROIActor->UpdateImage();
741 mCurrentROIActor->SetVisible(visible);
742 mCurrentROIActor->SetContourVisible(cvisible);
743 mCurrentSlicerManager->Render();
745 //------------------------------------------------------------------------------
748 //------------------------------------------------------------------------------
749 void vvToolROIManager::SaveState(std::auto_ptr<QXmlStreamWriter> & m_XmlWriter)
751 // Get index of the image
752 int n = mMainWindow->GetSlicerManagers().size();
754 for(int i=0; i<n; i++) {
755 if (mCurrentSlicerManager == mMainWindow->GetSlicerManagers()[i]) index = i;
758 std::cerr << "Error while writing state for ROIManager tool no currentimage founded." << std::endl;
761 m_XmlWriter->writeTextElement("Image_Index", QString::number(index));
765 for(uint i=0; i<mROIActorsList.size(); i++) {
766 QSharedPointer<vvROIActor> roi = mROIActorsList[i];
768 m_XmlWriter->writeStartElement("ROI");
769 m_XmlWriter->writeTextElement("Image", mOpenedBinaryImageFilenames[i]);
771 m_XmlWriter->writeStartElement("Overlay");
772 m_XmlWriter->writeAttribute("Red", QString("%1").arg(roi->GetOverlayColor()[0]));
773 m_XmlWriter->writeAttribute("Green",QString("%1").arg(roi->GetOverlayColor()[1]));
774 m_XmlWriter->writeAttribute("Blue", QString("%1").arg(roi->GetOverlayColor()[2]));
775 m_XmlWriter->writeAttribute("Visible", QString("%1").arg(roi->IsVisible()));
776 m_XmlWriter->writeAttribute("Opacity", QString("%1").arg(roi->GetOpacity()));
777 m_XmlWriter->writeAttribute("Depth", QString("%1").arg(roi->GetDepth()));
778 m_XmlWriter->writeEndElement();
780 m_XmlWriter->writeStartElement("Contour");
781 m_XmlWriter->writeAttribute("Red", QString("%1").arg(roi->GetContourColor()[0]));
782 m_XmlWriter->writeAttribute("Green",QString("%1").arg(roi->GetContourColor()[1]));
783 m_XmlWriter->writeAttribute("Blue", QString("%1").arg(roi->GetContourColor()[2]));
784 m_XmlWriter->writeAttribute("Visible", QString("%1").arg(roi->IsContourVisible()));
785 m_XmlWriter->writeAttribute("Width", QString("%1").arg(roi->GetContourWidth()));
786 m_XmlWriter->writeEndElement();
788 m_XmlWriter->writeEndElement();
791 //------------------------------------------------------------------------------
794 //------------------------------------------------------------------------------
795 void vvToolROIManager::ReadXMLInformation()
797 std::string value="";
798 mInitialImageIndex = -1;
799 while (!(m_XmlReader->isEndElement() && value == GetToolName().toStdString())) {
800 m_XmlReader->readNext();
801 value = m_XmlReader->qualifiedName().toString().toStdString();
803 if (value == "Image_Index")
804 mInitialImageIndex = m_XmlReader->readElementText().toInt();
806 if (m_XmlReader->isStartElement()) {
807 if (value == "ROI") {
808 ReadXMLInformation_ROI();
813 //------------------------------------------------------------------------------
816 //------------------------------------------------------------------------------
817 void vvToolROIManager::ReadXMLInformation_ROI()
820 std::string value="";
821 QSharedPointer<vvROIActor> param = QSharedPointer<vvROIActor>(new vvROIActor);
822 param->SetVisible(true);
823 clitk::DicomRT_ROI::Pointer roi = clitk::DicomRT_ROI::New();
824 // r->SetDisplayColor(1,1,1);
827 float r=1.0,g=1.0,b=1.0;
828 float cr=1.0,cg=1.0,cb=1.0;
831 bool cvisible = true;
835 while (!(m_XmlReader->isEndElement() && value == "ROI")) {
836 m_XmlReader->readNext();
837 value = m_XmlReader->qualifiedName().toString().toStdString();
838 if (value == "Image") {
839 s = m_XmlReader->readElementText();
842 if (value == "Overlay" && m_XmlReader->isStartElement()) {
843 QXmlStreamAttributes attributes = m_XmlReader->attributes();
844 if (!m_XmlReader->hasError())
845 r = attributes.value("Red").toString().toFloat();
846 if (!m_XmlReader->hasError())
847 g = attributes.value("Green").toString().toFloat();
848 if (!m_XmlReader->hasError())
849 b = attributes.value("Blue").toString().toFloat();
850 if (!m_XmlReader->hasError())
851 visible = attributes.value("Visible").toString().toInt();
852 if (!m_XmlReader->hasError())
853 opacity = attributes.value("Opacity").toString().toFloat();
854 if (!m_XmlReader->hasError())
855 depth = attributes.value("Depth").toString().toFloat();
859 if (value == "Contour" && m_XmlReader->isStartElement()) {
860 QXmlStreamAttributes attributes = m_XmlReader->attributes();
861 if (!m_XmlReader->hasError())
862 cr = attributes.value("Red").toString().toFloat();
863 if (!m_XmlReader->hasError())
864 cg = attributes.value("Green").toString().toFloat();
865 if (!m_XmlReader->hasError())
866 cb = attributes.value("Blue").toString().toFloat();
867 if (!m_XmlReader->hasError())
868 cvisible = attributes.value("Visible").toString().toInt();
869 if (!m_XmlReader->hasError())
870 width = attributes.value("Width").toString().toFloat();
872 param->SetOverlayColor(r,g,b);
873 param->SetVisible(visible);
874 param->SetOpacity(opacity);
875 param->SetDepth(depth);
877 param->SetContourColor(cr,cg,cb);
878 param->SetContourVisible(cvisible);
879 param->SetContourWidth(width);
881 mROIFilenames.push_back(s);
882 mROIActorsParamList.push_back(param);
884 //------------------------------------------------------------------------------