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"
29 #include <QFileDialog>
30 #include <QMessageBox>
31 #include <QColorDialog>
32 #include <QAbstractEventDispatcher>
35 #include <vtkLookupTable.h>
36 #include <vtkRenderWindow.h>
38 //------------------------------------------------------------------------------
39 // Create the tool and automagically (I like this word) insert it in
40 // the main window menu.
41 ADD_TOOL(vvToolROIManager);
42 //------------------------------------------------------------------------------
44 //------------------------------------------------------------------------------
45 vvToolROIManager::vvToolROIManager(vvMainWindowBase * parent, Qt::WindowFlags f):
46 QWidget(parent->GetTab()),
47 vvToolBase<vvToolROIManager>(parent),
48 Ui::vvToolROIManager()
53 // Assume the initial tab ROI index is 2
57 QWidget * tab = qFindChild<QWidget*>(parent->GetTab(), "ROItab");
60 parent->GetTab()->setCurrentIndex(mIndexFirstTab);
62 // Check if widget already used
63 if (tab->layout()->isEmpty()) {
64 tab->layout()->addWidget(this);
72 Ui_vvToolROIManager::setupUi(this);
73 setAttribute(Qt::WA_DeleteOnClose);
75 mTree->header()->resizeSection(0, 30);
77 // Temporary disable "Load dicom" button
81 mDefaultLUTColor = vtkSmartPointer<vtkLookupTable>::New();
82 for(int i=0; i<mDefaultLUTColor->GetNumberOfTableValues(); i++) {
83 double r = (rand()/(RAND_MAX+1.0));
84 double v = (rand()/(RAND_MAX+1.0));
85 double b = (rand()/(RAND_MAX+1.0));
86 mDefaultLUTColor->SetTableValue(i, r, v, b);
88 #include "vvDefaultLut.h"
91 mNumberOfVisibleROI = 0;
92 mNumberOfVisibleContourROI = 0;
94 // Select the current image as the target
95 int i = parent->GetSlicerManagerCurrentIndex();
96 InputIsSelected(parent->GetSlicerManagers()[i]);
98 // Connect event from mainwindow to this widget
99 connect(parent, SIGNAL(AnImageIsBeingClosed(vvSlicerManager *)),
100 this, SLOT(AnImageIsBeingClosed(vvSlicerManager *)));
101 connect(parent, SIGNAL(SelectedImageHasChanged(vvSlicerManager *)),
102 this, SLOT(SelectedImageHasChanged(vvSlicerManager *)));
103 connect(mOpenBinaryButton, SIGNAL(clicked()), this, SLOT(OpenBinaryImage()));
104 connect(mOpenDicomButton, SIGNAL(clicked()), this, SLOT(OpenDicomImage()));
105 connect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
106 connect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
107 connect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
108 connect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
109 connect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
110 connect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
111 connect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
112 connect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
113 connect(mReloadButton, SIGNAL(clicked()), this, SLOT(ReloadCurrentROI()));
114 connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
115 connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
116 connect(mCloseButton, SIGNAL(clicked()), this, SLOT(close()));
118 //------------------------------------------------------------------------------
121 //------------------------------------------------------------------------------
122 vvToolROIManager::~vvToolROIManager()
125 //------------------------------------------------------------------------------
128 //------------------------------------------------------------------------------
130 void vvToolROIManager::Initialize() {
131 SetToolName("ROIManager");
132 SetToolMenuName("Display ROI (binary image)");
133 SetToolIconFilename(":/common/icons/tool-roi.png");
134 SetToolTip("Display ROI from a binary image.");
135 SetToolExperimental(true);
137 //------------------------------------------------------------------------------
140 //------------------------------------------------------------------------------
141 void vvToolROIManager::InputIsSelected(vvSlicerManager *m)
147 mCurrentImage = mSlicerManager->GetImage();
150 if (mCurrentImage->GetNumberOfDimensions() != 3) {
151 QMessageBox::information(this,tr("Sorry only 3D yet"), tr("Sorry only 3D yet"));
157 mLabelInputInfo->setText(QString("%1").arg(m->GetFileName().c_str()));
159 // Auto display browser to select new contours
162 //------------------------------------------------------------------------------
165 //------------------------------------------------------------------------------
166 void vvToolROIManager::AnImageIsBeingClosed(vvSlicerManager * m)
168 if (m == mSlicerManager) {
173 //------------------------------------------------------------------------------
176 //------------------------------------------------------------------------------
177 void vvToolROIManager::close()
179 // Update to delete actors
183 //------------------------------------------------------------------------------
186 //------------------------------------------------------------------------------
187 void vvToolROIManager::SelectedImageHasChanged(vvSlicerManager * m) {
188 if (m != mSlicerManager) hide();
193 //------------------------------------------------------------------------------
196 //------------------------------------------------------------------------------
197 void vvToolROIManager::OpenBinaryImage()
200 QString Extensions = "Images files ( *.mha *.mhd *.hdr *.his)";
201 Extensions += ";;All Files (*)";
202 QStringList filename =
203 QFileDialog::getOpenFileNames(this,tr("Open binary image"),
204 mMainWindowBase->GetInputPathName(),Extensions);
205 if (filename.size() == 0) return;
207 // For each selected file, open the image
208 for(int i=0; i<filename.size(); i++) {
210 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
211 vvImageReader::Pointer reader = vvImageReader::New();
212 std::vector<std::string> filenames;
213 filenames.push_back(filename[i].toStdString());
214 reader->SetInputFilenames(filenames);
215 reader->Update(vvImageReader::IMAGE);
216 QApplication::restoreOverrideCursor();
218 if (reader->GetLastError().size() != 0) {
219 std::cerr << "Error while reading " << filename[i].toStdString() << std::endl;
220 QString error = "Cannot open file \n";
221 error += reader->GetLastError().c_str();
222 QMessageBox::information(this,tr("Reading problem"),error);
225 vvImage::Pointer binaryImage = reader->GetOutput();
226 AddImage(binaryImage, filename[i].toStdString(), mBackgroundValueSpinBox->value(),
227 (!mBGModeCheckBox->isChecked()));
228 mOpenedBinaryImage.push_back(binaryImage);
231 // Update the contours
234 //------------------------------------------------------------------------------
237 //------------------------------------------------------------------------------
238 void vvToolROIManager::OpenDicomImage()
240 DD("OpenDicomImage");
241 QString Extensions = "Dicom Files ( *.dcm RS*)";
242 Extensions += ";;All Files (*)";
243 QString file = QFileDialog::getOpenFileName(this,tr("Merge Images"),
244 mMainWindow->GetInputPathName(),
246 if (file.isNull()) return;
248 // AddDCStructContour(index, file);
250 reader.SetFilename(file.toStdString());
251 vvStructSelector selector;
252 selector.SetStructures(reader.GetROINames());
253 // selector.EnablePropagationCheckBox(); FIXME Disable
255 // FIXME : change text -> allow to save binary image
257 if (selector.exec()) {
258 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
259 reader.SetSelectedItems(selector.getSelectedItems());
260 reader.SetImage(mSlicerManager->GetImage());
263 // std::vector<vvMesh::Pointer> contours=reader.GetOutput();
264 // for (std::vector<vvMesh::Pointer>::iterator i=contours.begin();
265 // i!=contours.end(); i++)
266 // AddContour(index,*i,selector.PropagationEnabled());
267 QApplication::restoreOverrideCursor();
273 //------------------------------------------------------------------------------
276 //------------------------------------------------------------------------------
277 void vvToolROIManager::AddImage(vvImage * binaryImage, std::string filename,
278 double BG, bool modeBG)
281 int dim = mCurrentImage->GetNumberOfDimensions();
282 int bin_dim = binaryImage->GetNumberOfDimensions();
284 std::ostringstream os;
285 os << "Error. Loaded binary image is " << bin_dim
286 << "D while selected image is " << dim << "D" << std::endl;
287 QMessageBox::information(this,tr("Reading problem"),os.str().c_str());
292 int n = mROIList.size();
294 // Compute the name of the new ROI
295 std::ostringstream oss;
296 oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(filename));
297 std::string name = oss.str();
300 std::vector<double> color;
306 clitk::DicomRT_ROI::Pointer roi = clitk::DicomRT_ROI::New();
307 roi->SetFromBinaryImage(binaryImage, n, name, color, filename);
309 // Add a new roi to the list
310 mROIList.push_back(roi);
314 roi->SetBackgroundValueLabelImage(BG);
316 roi->SetForegroundValueLabelImage(BG);
319 if (n<mDefaultLUTColor->GetNumberOfTableValues ()) {
320 double * color = mDefaultLUTColor->GetTableValue(n % mDefaultLUTColor->GetNumberOfTableValues ());
321 roi->SetDisplayColor(color[0], color[1], color[2]);
324 // Add a new roi actor
325 QSharedPointer<vvROIActor> actor = QSharedPointer<vvROIActor>(new vvROIActor);
326 actor->SetBGMode(modeBG);
328 actor->SetSlicerManager(mSlicerManager);
329 actor->Initialize(n+1); // depth is n+1 to start at 1
330 mROIActorsList.push_back(actor);
332 // CheckBox for "All"
333 if (actor->IsVisible()) mNumberOfVisibleROI++;
334 if (actor->IsContourVisible()) mNumberOfVisibleContourROI++;
337 mTreeWidgetList.push_back(QSharedPointer<QTreeWidgetItem>(new QTreeWidgetItem(mTree)));
338 QTreeWidgetItem * w = mTreeWidgetList.back().data();
339 w->setText(0, QString("%1").arg(roi->GetROINumber()));
340 w->setText(1, QString("%1").arg(roi->GetName().c_str()));
341 w->setText(3, QString("%1").arg(actor->GetDepth()));
342 QBrush brush(QColor(roi->GetDisplayColor()[0]*255,
343 roi->GetDisplayColor()[1]*255,
344 roi->GetDisplayColor()[2]*255));
345 brush.setStyle(Qt::SolidPattern);
346 w->setBackground(2, brush);
347 mMapROIToTreeWidget[roi] = w;
348 mMapTreeWidgetToROI[w] = roi;
349 mTree->resizeColumnToContents(0);
350 mTree->resizeColumnToContents(1);
353 UpdateAllROIStatus();
355 //------------------------------------------------------------------------------
358 //------------------------------------------------------------------------------
359 void vvToolROIManager::UpdateAllContours()
361 // Render loaded ROIs (the first is sufficient)
362 for(unsigned int i=0; i<mROIList.size(); i++) {
363 mROIActorsList[i]->Update();
365 for(int i=0; i<mSlicerManager->GetNumberOfSlicers(); i++) {
366 mSlicerManager->GetSlicer(i)->Render();
369 //------------------------------------------------------------------------------
372 //------------------------------------------------------------------------------
373 void vvToolROIManager::UpdateAllROIStatus() {
375 int nb = mROIList.size();
376 for(int i=0; i<nb; i++) {
377 if (mROIActorsList[i]->IsVisible()) {
383 disconnect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
384 disconnect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
385 if (nbVisible == nb) mCheckBoxShowAll->setCheckState(Qt::Checked);
387 if (nbVisible == 0) mCheckBoxShowAll->setCheckState(Qt::Unchecked);
388 else mCheckBoxShowAll->setCheckState(Qt::PartiallyChecked);
390 connect(mContourCheckBoxShowAll, SIGNAL(toggled(bool)), this, SLOT(AllVisibleContourROIToggled(bool)));
391 connect(mCheckBoxShowAll, SIGNAL(stateChanged(int)), this, SLOT(AllVisibleROIToggled(int)));
393 //------------------------------------------------------------------------------
396 //------------------------------------------------------------------------------
397 void vvToolROIManager::SelectedItemChangedInTree() {
399 // Search which roi is selected
400 QList<QTreeWidgetItem *> l = mTree->selectedItems();
402 // mCurrentROIActor = 0;
404 mGroupBoxROI->setEnabled(false);
407 QTreeWidgetItem * w = l[0];
408 if (mMapTreeWidgetToROI.find(w) == mMapTreeWidgetToROI.end()) {
409 // mCurrentROIActor = 0;
411 mGroupBoxROI->setEnabled(false);
414 if (w == NULL) return;
415 clitk::DicomRT_ROI * roi = mMapTreeWidgetToROI[w];
416 if (roi == NULL) return; // sometimes it is called while there is no roi anymore
417 // Get selected roi actor
418 int n = roi->GetROINumber();
419 QSharedPointer<vvROIActor> actor = mROIActorsList[n];
421 mCurrentROIActor = actor;
423 // Warning -> avoid unuseful Render here by disconnect slider
425 disconnect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
426 disconnect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
427 disconnect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
428 disconnect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
429 disconnect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
430 disconnect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
431 disconnect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
432 disconnect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
434 mGroupBoxROI->setEnabled(true);
435 mROInameLabel->setText(roi->GetName().c_str());
436 mCheckBoxShow->setChecked(actor->IsVisible());
437 mContourCheckBoxShow->setChecked(actor->IsContourVisible());
438 mContourWidthSpinBox->setValue(actor->GetContourWidth());
439 mDepthSpinBox->setValue(actor->GetDepth());
440 w->setText(3, QString("%1").arg(actor->GetDepth()));
441 mOpacitySlider->setValue((int)lrint(actor->GetOpacity()*100));
442 mOpacitySpinBox->setValue((int)lrint(actor->GetOpacity()*100));
444 connect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
445 connect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
446 connect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
447 connect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
448 connect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
449 connect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
450 connect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
451 connect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
454 // Set the current color to the selected ROI name
455 mROInameLabel->setAutoFillBackground(true);// # This is important!!
456 mROInameLabel->setStyleSheet("QLabel { background-color : red; color : blue; }");
457 QColor color = QColor(mCurrentROI->GetDisplayColor()[0]*255,
458 mCurrentROI->GetDisplayColor()[1]*255,
459 mCurrentROI->GetDisplayColor()[2]*255);
460 QString values = QString("%1, %2, %3").arg(color.red()).arg(color.green()).arg(color.blue());
461 mROInameLabel->setStyleSheet("QLabel { background-color: rgb("+values+"); }");
466 // mCurrentSlicerManager->Render();
468 //------------------------------------------------------------------------------
471 //------------------------------------------------------------------------------
472 void vvToolROIManager::VisibleROIToggled(bool b) {
473 if (mCurrentROIActor == NULL) return;
474 if (b == mCurrentROIActor->IsVisible()) return; // nothing to do
475 mCurrentROIActor->SetVisible(b);
476 UpdateAllROIStatus();
477 mSlicerManager->Render();
479 //------------------------------------------------------------------------------
482 //------------------------------------------------------------------------------
483 void vvToolROIManager::VisibleContourROIToggled(bool b) {
484 if (mCurrentROIActor == NULL) return;
485 if (mCurrentROIActor->IsContourVisible() == b) return; // nothing to do
486 mCurrentROIActor->SetContourVisible(b);
487 mCurrentROIActor->UpdateColor();
488 mSlicerManager->Render();
490 //------------------------------------------------------------------------------
493 //------------------------------------------------------------------------------
494 void vvToolROIManager::OpacityChanged(int v) {
495 if (mCurrentROIActor == NULL) return;
496 mCurrentROIActor->SetOpacity((double)v/100.0);
497 mCurrentROIActor->UpdateColor();
498 mSlicerManager->Render();
500 //------------------------------------------------------------------------------
503 //------------------------------------------------------------------------------
504 void vvToolROIManager::AllVisibleROIToggled(int b) {
506 if ((mCheckBoxShowAll->checkState() == Qt::Checked) ||
507 (mCheckBoxShowAll->checkState() == Qt::PartiallyChecked)) status = true;
509 for(uint i=0; i<mROIList.size(); i++) {
510 mROIActorsList[i]->SetVisible(status);
512 if (status) mCheckBoxShowAll->setCheckState(Qt::Checked);
513 else mCheckBoxShowAll->setCheckState(Qt::Unchecked);
514 mCheckBoxShow->setChecked(status);
515 mSlicerManager->Render();
517 //------------------------------------------------------------------------------
520 //------------------------------------------------------------------------------
521 void vvToolROIManager::AllVisibleContourROIToggled(bool b) {
523 if ((mContourCheckBoxShowAll->checkState() == Qt::Checked) ||
524 (mContourCheckBoxShowAll->checkState() == Qt::PartiallyChecked)) status = true;
526 for(uint i=0; i<mROIActorsList.size(); i++) {
527 mROIActorsList[i]->SetContourVisible(status);
529 // Update current selection
530 if (status) mContourCheckBoxShowAll->setCheckState(Qt::Checked);
531 else mContourCheckBoxShowAll->setCheckState(Qt::Unchecked);
532 mContourCheckBoxShow->setChecked(status);
533 mSlicerManager->Render();
535 //------------------------------------------------------------------------------
538 //------------------------------------------------------------------------------
539 void vvToolROIManager::ChangeColor() {
541 color.setRgbF(mCurrentROIActor->GetROI()->GetDisplayColor()[0],
542 mCurrentROIActor->GetROI()->GetDisplayColor()[1],
543 mCurrentROIActor->GetROI()->GetDisplayColor()[2]);
544 QColor c = QColorDialog::getColor(color, this, "Choose the ROI color");
545 mCurrentROIActor->GetROI()->SetDisplayColor(c.redF(), c.greenF(), c.blueF());
546 mCurrentROIActor->UpdateColor();
548 QTreeWidgetItem * w = mMapROIToTreeWidget[mCurrentROI];
549 QBrush brush(QColor(mCurrentROI->GetDisplayColor()[0]*255,
550 mCurrentROI->GetDisplayColor()[1]*255,
551 mCurrentROI->GetDisplayColor()[2]*255));
552 brush.setStyle(Qt::SolidPattern);
553 w->setBackground(2, brush);
555 mSlicerManager->Render();
557 //------------------------------------------------------------------------------
560 //------------------------------------------------------------------------------
561 void vvToolROIManager::ChangeContourColor() {
563 color.setRgbF(mCurrentROIActor->GetContourColor()[0],
564 mCurrentROIActor->GetContourColor()[1],
565 mCurrentROIActor->GetContourColor()[2]);
566 QColor c = QColorDialog::getColor(color, this, "Choose the contour color");
567 mCurrentROIActor->SetContourColor(c.redF(), c.greenF(), c.blueF());
568 mCurrentROIActor->UpdateColor();
569 mSlicerManager->Render();
571 //------------------------------------------------------------------------------
574 //------------------------------------------------------------------------------
575 void vvToolROIManager::ChangeContourWidth(int n) {
576 mCurrentROIActor->SetContourWidth(n);
577 mCurrentROIActor->UpdateColor();
578 mSlicerManager->Render();
580 //------------------------------------------------------------------------------
583 //------------------------------------------------------------------------------
584 void vvToolROIManager::ChangeDepth(int n) {
585 mCurrentROIActor->SetDepth(n);
586 mCurrentROIActor->UpdateImage();
587 mSlicerManager->Render();
588 QList<QTreeWidgetItem *> l = mTree->selectedItems();
589 QTreeWidgetItem * w = l[0];
590 w->setText(3, QString("%1").arg(mCurrentROIActor->GetDepth()));
592 //------------------------------------------------------------------------------
595 //------------------------------------------------------------------------------
596 void vvToolROIManager::ReloadCurrentROI() {
598 // Remove all contours/overlay first
599 bool visible = mCurrentROIActor->IsVisible();
600 bool cvisible = mCurrentROIActor->IsContourVisible();
601 mCurrentROIActor->SetVisible(false);
602 mCurrentROIActor->SetContourVisible(false);
603 mSlicerManager->Render();
606 vvImageReader::Pointer reader = vvImageReader::New();
607 reader->SetInputFilename(mCurrentROI->GetFilename());
608 reader->Update(vvImageReader::IMAGE);
609 if (reader->GetLastError() != "") {
610 QMessageBox::information(mMainWindowBase, tr("Sorry, error. Could not reload"),
611 reader->GetLastError().c_str());
615 mCurrentROI->GetImage()->Reset();//GetFirstVTKImageData()->ReleaseData();
616 mCurrentROI->SetImage(reader->GetOutput());
619 mCurrentROIActor->UpdateImage();
620 mCurrentROIActor->SetVisible(visible);
621 mCurrentROIActor->SetContourVisible(cvisible);
622 mSlicerManager->Render();
624 //------------------------------------------------------------------------------