void QmitkBinaryThresholdToolGUI::OnAcceptThresholdPreview()
{
  if (m_BinaryThresholdTool.IsNotNull())
  {
    QmitkNewSegmentationDialog* dialog = new QmitkNewSegmentationDialog( this ); // needs a QWidget as parent, "this" is not QWidget
    dialog->setPrompt("What did you just segment?");
    int dialogReturnValue = dialog->exec();

    std::string organName = dialog->GetSegmentationName().toStdString();
    mitk::Color color     = dialog->GetColor();

    delete dialog;

    if ( dialogReturnValue != QDialog::Rejected ) // user clicked cancel or pressed Esc or something similar
    {
      this->thresholdAccepted();
      m_BinaryThresholdTool->AcceptCurrentThresholdValue( organName, color );
    }
    else
    {
      this->thresholdCanceled();
      m_BinaryThresholdTool->CancelThresholding();
    }
  }
}
Example #2
0
void QmitkSegmentationView::CreateNewSegmentation()
{
  mitk::DataNode::Pointer node = m_Controls->m_ManualToolSelectionBox->GetToolManager()->GetReferenceData(0);
  if (node.IsNotNull())
  {
    mitk::Image::Pointer image = dynamic_cast<mitk::Image*>( node->GetData() );
    if (image.IsNotNull())
    {
      if (image->GetDimension()>1)
      {
        // ask about the name and organ type of the new segmentation
        QmitkNewSegmentationDialog* dialog = new QmitkNewSegmentationDialog( m_Parent ); // needs a QWidget as parent, "this" is not QWidget

        QString storedList = QString::fromStdString( this->GetPreferences()->GetByteArray("Organ-Color-List","") );
        QStringList organColors;
        if (storedList.isEmpty())
        {
          organColors = GetDefaultOrganColorString();
        }
        else
        {
          /*
          a couple of examples of how organ names are stored:

          a simple item is built up like 'name#AABBCC' where #AABBCC is the hexadecimal notation of a color as known from HTML

          items are stored separated by ';'
          this makes it necessary to escape occurrences of ';' in name.
          otherwise the string "hugo;ypsilon#AABBCC;eugen#AABBCC" could not be parsed as two organs
          but we would get "hugo" and "ypsilon#AABBCC" and "eugen#AABBCC"

          so the organ name "hugo;ypsilon" is stored as "hugo\;ypsilon"
          and must be unescaped after loading

          the following lines could be one split with Perl's negative lookbehind
          */

          // recover string list from BlueBerry view's preferences
          QString storedString = QString::fromStdString( this->GetPreferences()->GetByteArray("Organ-Color-List","") );
          MITK_DEBUG << "storedString: " << storedString.toStdString();
          // match a string consisting of any number of repetitions of either "anything but ;" or "\;". This matches everything until the next unescaped ';'
          QRegExp onePart("(?:[^;]|\\\\;)*");
          MITK_DEBUG << "matching " << onePart.pattern().toStdString();
          int count = 0;
          int pos = 0;
          while( (pos = onePart.indexIn( storedString, pos )) != -1 )
          {
            ++count;
            int length = onePart.matchedLength();
            if (length == 0) break;
            QString matchedString = storedString.mid(pos, length);
            MITK_DEBUG << "   Captured length " << length << ": " << matchedString.toStdString();
            pos += length + 1; // skip separating ';'

            // unescape possible occurrences of '\;' in the string
            matchedString.replace("\\;", ";");

            // add matched string part to output list
            organColors << matchedString;
          }
          MITK_DEBUG << "Captured " << count << " organ name/colors";
        }

        dialog->SetSuggestionList( organColors );

        int dialogReturnValue = dialog->exec();

        if ( dialogReturnValue == QDialog::Rejected ) return; // user clicked cancel or pressed Esc or something similar

        // ask the user about an organ type and name, add this information to the image's (!) propertylist
        // create a new image of the same dimensions and smallest possible pixel type
        mitk::ToolManager* toolManager = m_Controls->m_ManualToolSelectionBox->GetToolManager();
        mitk::Tool* firstTool = toolManager->GetToolById(0);
        if (firstTool)
        {
          try
          {
            mitk::DataNode::Pointer emptySegmentation =
              firstTool->CreateEmptySegmentationNode( image, dialog->GetSegmentationName().toStdString(), dialog->GetColor() );

            //Here we change the reslice interpolation mode for a segmentation, so that contours in rotated slice can be shown correctly
            emptySegmentation->SetProperty( "reslice interpolation", mitk::VtkResliceInterpolationProperty::New(VTK_RESLICE_NEAREST) );
            // initialize showVolume to false to prevent recalculating the volume while working on the segmentation
            emptySegmentation->SetProperty( "showVolume", mitk::BoolProperty::New( false ) );

            if (!emptySegmentation) return; // could be aborted by user

            UpdateOrganList( organColors, dialog->GetSegmentationName(), dialog->GetColor() );

            /*
            escape ';' here (replace by '\;'), see longer comment above
            */
            std::string stringForStorage = organColors.replaceInStrings(";","\\;").join(";").toStdString();
            MITK_DEBUG << "Will store: " << stringForStorage;
            this->GetPreferences()->PutByteArray("Organ-Color-List", stringForStorage );
            this->GetPreferences()->Flush();

            if(m_Controls->m_ManualToolSelectionBox->GetToolManager()->GetWorkingData(0))
            {
              m_Controls->m_ManualToolSelectionBox->GetToolManager()->GetWorkingData(0)->SetSelected(false);
            }
            emptySegmentation->SetSelected(true);
            this->GetDefaultDataStorage()->Add( emptySegmentation, node ); // add as a child, because the segmentation "derives" from the original

            this->FireNodeSelected( emptySegmentation );
            this->OnSelectionChanged( emptySegmentation );
            this->SetToolManagerSelection(node, emptySegmentation);
          }
          catch (std::bad_alloc)
          {
            QMessageBox::warning(NULL,"Create new segmentation","Could not allocate memory for new segmentation");
          }
        }
      }
      else
      {
        QMessageBox::information(NULL,"Segmentation","Segmentation is currently not supported for 2D images");
      }
    }
  }
  else
  {
    MITK_ERROR << "'Create new segmentation' button should never be clickable unless a patient image is selected...";
  }
}
void QmitkSegmentationView::CreateNewSegmentation()
{
   mitk::DataNode::Pointer node = mitk::ToolManagerProvider::GetInstance()->GetToolManager()->GetReferenceData(0);
   if (node.IsNotNull())
   {
     mitk::Image::Pointer image = dynamic_cast<mitk::Image*>(node->GetData());
     if (image.IsNotNull())
     {
       if (image->GetDimension() > 1)
       {
         // ask about the name and organ type of the new segmentation
         QmitkNewSegmentationDialog* dialog = new QmitkNewSegmentationDialog(m_Parent); // needs a QWidget as parent, "this" is not QWidget
         QStringList organColors = mitk::OrganNamesHandling::GetDefaultOrganColorString();;

         dialog->SetSuggestionList(organColors);

         int dialogReturnValue = dialog->exec();
         if (dialogReturnValue == QDialog::Rejected)
         {
           // user clicked cancel or pressed Esc or something similar
           return;
         }

         // ask the user about an organ type and name, add this information to the image's (!) propertylist
         // create a new image of the same dimensions and smallest possible pixel type
         mitk::ToolManager* toolManager = mitk::ToolManagerProvider::GetInstance()->GetToolManager();
         mitk::Tool* firstTool = toolManager->GetToolById(0);
         if (firstTool)
         {
           try
           {
             std::string newNodeName = dialog->GetSegmentationName().toStdString();
             if (newNodeName.empty())
             {
               newNodeName = "no_name";
             }

             mitk::DataNode::Pointer emptySegmentation = firstTool->CreateEmptySegmentationNode(image, newNodeName, dialog->GetColor());
             // initialize showVolume to false to prevent recalculating the volume while working on the segmentation
             emptySegmentation->SetProperty("showVolume", mitk::BoolProperty::New(false));
             if (!emptySegmentation)
             {
               return; // could be aborted by user
             }

             mitk::OrganNamesHandling::UpdateOrganList(organColors, dialog->GetSegmentationName(), dialog->GetColor());

             // escape ';' here (replace by '\;'), see longer comment above
             QString stringForStorage = organColors.replaceInStrings(";", "\\;").join(";");
             MITK_DEBUG << "Will store: " << stringForStorage;
             this->GetPreferences()->Put("Organ-Color-List", stringForStorage);
             this->GetPreferences()->Flush();

             if (mitk::ToolManagerProvider::GetInstance()->GetToolManager()->GetWorkingData(0))
             {
               mitk::ToolManagerProvider::GetInstance()->GetToolManager()->GetWorkingData(0)->SetSelected(false);
             }
             emptySegmentation->SetSelected(true);
             this->GetDataStorage()->Add(emptySegmentation, node); // add as a child, because the segmentation "derives" from the original

             this->FireNodeSelected(emptySegmentation);
             this->OnSelectionChanged(emptySegmentation);

             m_Controls->segImageSelector->SetSelectedNode(emptySegmentation);
             mitk::RenderingManager::GetInstance()->InitializeViews(emptySegmentation->GetData()->GetTimeGeometry(), mitk::RenderingManager::REQUEST_UPDATE_ALL, true);
           }
           catch (std::bad_alloc)
           {
             QMessageBox::warning(nullptr, tr("Create new segmentation"), tr("Could not allocate memory for new segmentation"));
           }
         }
       }
       else
       {
         QMessageBox::information(nullptr, tr("Segmentation"), tr("Segmentation is currently not supported for 2D images"));
       }
     }
   }
   else
   {
     MITK_ERROR << "'Create new segmentation' button should never be clickable unless a patient image is selected...";
   }
}