+
+//------------------------------------------------------------------------------
+void vvToolROIManager::SelectedItemChangedInTree() {
+ // Search which roi is selected
+ QList<QTreeWidgetItem *> l = mTree->selectedItems();
+ if (l.size() == 0) {
+ // mCurrentROIActor = 0;
+ mCurrentROI = NULL;
+ mGroupBoxROI->setEnabled(false);
+ return;
+ }
+ QTreeWidgetItem * w = l[0];
+ if (w == NULL) return;
+ if (w == 0) return;
+ if (mMapTreeWidgetToROI.find(w) == mMapTreeWidgetToROI.end()) {
+ // mCurrentROIActor = 0;
+ mCurrentROI = NULL;
+ mGroupBoxROI->setEnabled(false);
+ return;
+ }
+ if (w == NULL) return;
+ clitk::DicomRT_ROI * roi = mMapTreeWidgetToROI[w];
+ if (roi == NULL) return; // sometimes it is called while there is no roi anymore
+
+ // Get selected roi actor
+ int n = roi->GetROINumber();
+ QSharedPointer<vvROIActor> actor = mROIActorsList[n];
+ mCurrentROI = roi;
+ mCurrentROIActor = actor;
+
+ // Warning -> avoid unuseful Render here by disconnect slider
+ // Update GUI
+ disconnect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
+ disconnect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
+ disconnect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
+ disconnect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
+ disconnect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
+ disconnect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
+ disconnect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
+ disconnect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
+
+ mROInameLabel->setText(roi->GetName().c_str());
+ mCheckBoxShow->setChecked(actor->IsVisible());
+ mContourCheckBoxShow->setChecked(actor->IsContourVisible());
+ mContourWidthSpinBox->setValue(actor->GetContourWidth());
+ mDepthSpinBox->setValue(actor->GetDepth());
+ w->setText(3, QString("%1").arg(actor->GetDepth()));
+ mOpacitySlider->setValue((int)lrint(actor->GetOpacity()*100));
+ mOpacitySpinBox->setValue((int)lrint(actor->GetOpacity()*100));
+
+ connect(mTree, SIGNAL(itemSelectionChanged()), this, SLOT(SelectedItemChangedInTree()));
+ connect(mCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleROIToggled(bool)));
+ connect(mOpacitySlider, SIGNAL(valueChanged(int)), this, SLOT(OpacityChanged(int)));
+ connect(mChangeColorButton, SIGNAL(clicked()), this, SLOT(ChangeColor()));
+ connect(mContourCheckBoxShow, SIGNAL(toggled(bool)), this, SLOT(VisibleContourROIToggled(bool)));
+ connect(mChangeContourColorButton, SIGNAL(clicked()), this, SLOT(ChangeContourColor()));
+ connect(mContourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeContourWidth(int)));
+ connect(mDepthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ChangeDepth(int)));
+
+
+ // Set the current color to the selected ROI name
+ mROInameLabel->setAutoFillBackground(true);// # This is important!!
+ // mROInameLabel->setStyleSheet("QLabel { background-color : red; color : blue; }");
+ QColor color = QColor(mCurrentROI->GetDisplayColor()[0]*255,
+ mCurrentROI->GetDisplayColor()[1]*255,
+ mCurrentROI->GetDisplayColor()[2]*255);
+ // QString values = QString("%1, %2, %3").arg(color.red()).arg(color.green()).arg(color.blue());
+ // mROInameLabel->setStyleSheet("QLabel { background-color: rgb("+values+"); }");
+
+ QPalette* palette = new QPalette();
+ QColor colorFG = QColor((1-mCurrentROI->GetDisplayColor()[0])*255,
+ (1-mCurrentROI->GetDisplayColor()[1])*255,
+ (1-mCurrentROI->GetDisplayColor()[2])*255);
+ palette->setColor(QPalette::WindowText,colorFG);
+ palette->setColor(QPalette::Background, color);
+ mROInameLabel->setPalette(*palette);
+
+ // Enable the group box (in case no selection before)
+ mGroupBoxROI->setEnabled(true);
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::VisibleROIToggled(bool b) {
+ if (mCurrentROIActor == NULL) return;
+ if (b == mCurrentROIActor->IsVisible()) return; // nothing to do
+ mCurrentROIActor->SetVisible(b);
+ UpdateAllROIStatus();
+ mCurrentSlicerManager->Render();
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::VisibleContourROIToggled(bool b) {
+ if (mCurrentROIActor == NULL) return;
+ if (mCurrentROIActor->IsContourVisible() == b) return; // nothing to do
+ mCurrentROIActor->SetContourVisible(b);
+ mCurrentROIActor->UpdateColor();
+ mCurrentSlicerManager->Render();
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::OpacityChanged(int v) {
+ if (mCurrentROIActor == NULL) return;
+ mCurrentROIActor->SetOpacity((double)v/100.0);
+ mCurrentROIActor->UpdateColor();
+ mCurrentSlicerManager->Render();
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::AllVisibleROIToggled(int b) {
+ bool status = false;
+ if ((mCheckBoxShowAll->checkState() == Qt::Checked) ||
+ (mCheckBoxShowAll->checkState() == Qt::PartiallyChecked)) status = true;
+
+ for(uint i=0; i<mROIList.size(); i++) {
+ mROIActorsList[i]->SetVisible(status);
+ }
+ if (status) mCheckBoxShowAll->setCheckState(Qt::Checked);
+ else mCheckBoxShowAll->setCheckState(Qt::Unchecked);
+ mCheckBoxShow->setChecked(status);
+ mCurrentSlicerManager->Render();
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::AllVisibleContourROIToggled(bool b) {
+ bool status = false;
+ if ((mContourCheckBoxShowAll->checkState() == Qt::Checked) ||
+ (mContourCheckBoxShowAll->checkState() == Qt::PartiallyChecked)) status = true;
+ // Update current
+ for(uint i=0; i<mROIActorsList.size(); i++) {
+ mROIActorsList[i]->SetContourVisible(status);
+ }
+ // Update current selection
+ if (status) mContourCheckBoxShowAll->setCheckState(Qt::Checked);
+ else mContourCheckBoxShowAll->setCheckState(Qt::Unchecked);
+ mContourCheckBoxShow->setChecked(status);
+ mCurrentSlicerManager->Render();
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::ChangeColor() {
+ if (mCurrentROIActor == NULL) return;
+ QColor color;
+ color.setRgbF(mCurrentROIActor->GetROI()->GetDisplayColor()[0],
+ mCurrentROIActor->GetROI()->GetDisplayColor()[1],
+ mCurrentROIActor->GetROI()->GetDisplayColor()[2]);
+ QColor c = QColorDialog::getColor(color, this, "Choose the ROI color");
+ if (!c.isValid()) return;// User cancel
+
+ mCurrentROIActor->GetROI()->SetDisplayColor(c.redF(), c.greenF(), c.blueF());
+ mCurrentROIActor->UpdateColor();
+
+ QTreeWidgetItem * w = mMapROIToTreeWidget[mCurrentROI];
+ QBrush brush(QColor(mCurrentROI->GetDisplayColor()[0]*255,
+ mCurrentROI->GetDisplayColor()[1]*255,
+ mCurrentROI->GetDisplayColor()[2]*255));
+ brush.setStyle(Qt::SolidPattern);
+ w->setBackground(2, brush);
+ // Render
+ mCurrentSlicerManager->Render();
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::ChangeContourColor() {
+ if (mCurrentROIActor == NULL) return;
+ QColor color;
+ color.setRgbF(mCurrentROIActor->GetContourColor()[0],
+ mCurrentROIActor->GetContourColor()[1],
+ mCurrentROIActor->GetContourColor()[2]);
+ // QColorDialog d(color);
+ QColor c = QColorDialog::getColor(color, this, "Choose the contour color");
+ if (!c.isValid()) return; // User cancel
+ mCurrentROIActor->SetContourColor(c.redF(), c.greenF(), c.blueF());
+ mCurrentROIActor->UpdateColor();
+ mCurrentSlicerManager->Render();
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::ChangeContourWidth(int n) {
+ if (mCurrentROIActor == NULL) return;
+ mCurrentROIActor->SetContourWidth(n);
+ mCurrentROIActor->UpdateColor();
+ mCurrentSlicerManager->Render();
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::ChangeDepth(int n) {
+ if (mCurrentROIActor == NULL) return;
+ mCurrentROIActor->SetDepth(n);
+ // mCurrentROIActor->UpdateImage(); // FIXME
+ mCurrentSlicerManager->Render();
+ QList<QTreeWidgetItem *> l = mTree->selectedItems();
+ QTreeWidgetItem * w = l[0];
+ w->setText(3, QString("%1").arg(mCurrentROIActor->GetDepth()));
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::ReloadCurrentROI() {
+
+ // Remove all contours/overlay first
+ bool visible = mCurrentROIActor->IsVisible();
+ bool cvisible = mCurrentROIActor->IsContourVisible();
+ mCurrentROIActor->SetVisible(false);
+ mCurrentROIActor->SetContourVisible(false);
+ mCurrentSlicerManager->Render();
+
+ // Reload image
+ vvImageReader::Pointer reader = vvImageReader::New();
+ reader->SetInputFilename(mCurrentROI->GetFilename());
+ reader->Update(vvImageReader::IMAGE);
+ if (reader->GetLastError() != "") {
+ // No message just ignore (because can be from dicom)
+ // QMessageBox::information(mMainWindowBase, tr("Sorry, error. Could not reload"),
+ // reader->GetLastError().c_str());
+ return;
+ }
+
+ // Free the previous image
+ mCurrentROI->GetImage()->GetFirstVTKImageData()->ReleaseData(); // Needed to free
+ mCurrentROI->GetImage()->Reset();
+ mCurrentROI->SetImage(reader->GetOutput());
+
+ mCurrentROIActor->RemoveActors();
+
+ // Update visu
+ mCurrentROIActor->UpdateImage();
+ mCurrentROIActor->SetVisible(visible);
+ mCurrentROIActor->SetContourVisible(cvisible);
+ mCurrentSlicerManager->Render();
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::SaveState(std::auto_ptr<QXmlStreamWriter> & m_XmlWriter)
+{
+ // Get index of the image
+ int n = mMainWindow->GetSlicerManagers().size();
+ int index=-1;
+ for(int i=0; i<n; i++) {
+ if (mCurrentSlicerManager == mMainWindow->GetSlicerManagers()[i]) index = i;
+ }
+ if (index == -1) {
+ std::cerr << "Error while writing state for ROIManager tool no currentimage founded." << std::endl;
+ return;
+ }
+ m_XmlWriter->writeTextElement("Image_Index", QString::number(index));
+
+
+ // Write ROI
+ for(uint i=0; i<mROIActorsList.size(); i++) {
+ QSharedPointer<vvROIActor> roi = mROIActorsList[i];
+
+ m_XmlWriter->writeStartElement("ROI");
+ m_XmlWriter->writeTextElement("Image", mOpenedBinaryImageFilenames[i]);
+
+ m_XmlWriter->writeStartElement("Overlay");
+ m_XmlWriter->writeAttribute("Red", QString("%1").arg(roi->GetOverlayColor()[0]));
+ m_XmlWriter->writeAttribute("Green",QString("%1").arg(roi->GetOverlayColor()[1]));
+ m_XmlWriter->writeAttribute("Blue", QString("%1").arg(roi->GetOverlayColor()[2]));
+ m_XmlWriter->writeAttribute("Visible", QString("%1").arg(roi->IsVisible()));
+ m_XmlWriter->writeAttribute("Opacity", QString("%1").arg(roi->GetOpacity()));
+ m_XmlWriter->writeAttribute("Depth", QString("%1").arg(roi->GetDepth()));
+ m_XmlWriter->writeEndElement();
+
+ m_XmlWriter->writeStartElement("Contour");
+ m_XmlWriter->writeAttribute("Red", QString("%1").arg(roi->GetContourColor()[0]));
+ m_XmlWriter->writeAttribute("Green",QString("%1").arg(roi->GetContourColor()[1]));
+ m_XmlWriter->writeAttribute("Blue", QString("%1").arg(roi->GetContourColor()[2]));
+ m_XmlWriter->writeAttribute("Visible", QString("%1").arg(roi->IsContourVisible()));
+ m_XmlWriter->writeAttribute("Width", QString("%1").arg(roi->GetContourWidth()));
+ m_XmlWriter->writeEndElement();
+
+ m_XmlWriter->writeEndElement();
+ }
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::ReadXMLInformation()
+{
+ std::string value="";
+ mInitialImageIndex = -1;
+ while (!(m_XmlReader->isEndElement() && value == GetToolName().toStdString())) {
+ m_XmlReader->readNext();
+ value = m_XmlReader->qualifiedName().toString().toStdString();
+
+ if (value == "Image_Index")
+ mInitialImageIndex = m_XmlReader->readElementText().toInt();
+
+ if (m_XmlReader->isStartElement()) {
+ if (value == "ROI") {
+ ReadXMLInformation_ROI();
+ }
+ }
+ }
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvToolROIManager::ReadXMLInformation_ROI()
+{
+ QString s;
+ std::string value="";
+ QSharedPointer<vvROIActor> param = QSharedPointer<vvROIActor>(new vvROIActor);
+ param->SetVisible(true);
+ clitk::DicomRT_ROI::Pointer roi = clitk::DicomRT_ROI::New();
+ // r->SetDisplayColor(1,1,1);
+ param->SetROI(roi);
+
+ float r=1.0,g=1.0,b=1.0;
+ float cr=1.0,cg=1.0,cb=1.0;
+ float opacity = 0.7;
+ bool visible = true;
+ bool cvisible = true;
+ int width = 1;
+ int depth=1;
+
+ while (!(m_XmlReader->isEndElement() && value == "ROI")) {
+ m_XmlReader->readNext();
+ value = m_XmlReader->qualifiedName().toString().toStdString();
+ if (value == "Image") {
+ s = m_XmlReader->readElementText();
+ }
+
+ if (value == "Overlay" && m_XmlReader->isStartElement()) {
+ QXmlStreamAttributes attributes = m_XmlReader->attributes();
+ if (!m_XmlReader->hasError())
+ r = attributes.value("Red").toString().toFloat();
+ if (!m_XmlReader->hasError())
+ g = attributes.value("Green").toString().toFloat();
+ if (!m_XmlReader->hasError())
+ b = attributes.value("Blue").toString().toFloat();
+ if (!m_XmlReader->hasError())
+ visible = attributes.value("Visible").toString().toInt();
+ if (!m_XmlReader->hasError())
+ opacity = attributes.value("Opacity").toString().toFloat();
+ if (!m_XmlReader->hasError())
+ depth = attributes.value("Depth").toString().toFloat();
+ }
+
+
+ if (value == "Contour" && m_XmlReader->isStartElement()) {
+ QXmlStreamAttributes attributes = m_XmlReader->attributes();
+ if (!m_XmlReader->hasError())
+ cr = attributes.value("Red").toString().toFloat();
+ if (!m_XmlReader->hasError())
+ cg = attributes.value("Green").toString().toFloat();
+ if (!m_XmlReader->hasError())
+ cb = attributes.value("Blue").toString().toFloat();
+ if (!m_XmlReader->hasError())
+ cvisible = attributes.value("Visible").toString().toInt();
+ if (!m_XmlReader->hasError())
+ width = attributes.value("Width").toString().toFloat();
+ }
+ param->SetOverlayColor(r,g,b);
+ param->SetVisible(visible);
+ param->SetOpacity(opacity);
+ param->SetDepth(depth);
+
+ param->SetContourColor(cr,cg,cb);
+ param->SetContourVisible(cvisible);
+ param->SetContourWidth(width);
+ }
+ mROIFilenames.push_back(s);
+ mROIActorsParamList.push_back(param);
+}
+//------------------------------------------------------------------------------