/**
 Implementes the logic, which decides, when tools are activated/deactivated.
*/
void QmitkToolSelectionBox::SetGUIEnabledAccordingToToolManagerState()
{
  mitk::DataNode *referenceNode = m_ToolManager->GetReferenceData(0);
  mitk::DataNode *workingNode = m_ToolManager->GetWorkingData(0);

  // MITK_DEBUG << this->name() << ": SetGUIEnabledAccordingToToolManagerState: referenceNode " << (void*)referenceNode
  // << " workingNode " << (void*)workingNode << " isVisible() " << isVisible();

  bool enabled = true;

  switch (m_EnabledMode)
  {
    default:
    case EnabledWithReferenceAndWorkingDataVisible:
      enabled = referenceNode && workingNode &&
                referenceNode->IsVisible(
                  mitk::BaseRenderer::GetInstance(mitk::BaseRenderer::GetRenderWindowByName("stdmulti.widget1"))) &&
                workingNode->IsVisible(
                  mitk::BaseRenderer::GetInstance(mitk::BaseRenderer::GetRenderWindowByName("stdmulti.widget1"))) &&
                isVisible();
      break;
    case EnabledWithReferenceData:
      enabled = referenceNode && isVisible();
      break;
    case EnabledWithWorkingData:
      enabled = workingNode && isVisible();
      break;
    case AlwaysEnabled:
      enabled = isVisible();
      break;
  }

  if (QWidget::isEnabled() == enabled)
    return; // nothing to change

  QWidget::setEnabled(enabled);
  if (enabled)
  {
    m_ToolManager->RegisterClient();

    int id = m_ToolManager->GetActiveToolID();
    emit ToolSelected(id);
  }
  else
  {
    m_ToolManager->ActivateTool(-1);
    m_ToolManager->UnregisterClient();

    emit ToolSelected(-1);
  }
}
void QmitkToolSelectionBox::SetOrUnsetButtonForActiveTool()
{
  // we want to emit a signal in any case, whether we selected ourselves or somebody else changes "our" tool manager. --> emit before check on m_SelfCall
  int id = m_ToolManager->GetActiveToolID();

  // don't emit signal for shape model tools
  bool emitSignal = true;
  mitk::Tool* tool = m_ToolManager->GetActiveTool();
  if(tool && std::string(tool->GetGroup()) == "organ_segmentation")
    emitSignal = false;

  if(emitSignal)
    emit ToolSelected(id);

  // delete old GUI (if any)
  if ( m_LastToolGUI && m_ToolGUIWidget )
  {
    if (m_ToolGUIWidget->layout())
    {
      m_ToolGUIWidget->layout()->removeWidget(m_LastToolGUI);
    }


    //m_LastToolGUI->reparent(NULL, QPoint(0,0));
    // TODO: reparent <-> setParent, Daniel fragen
    m_LastToolGUI->setParent(0);
    delete m_LastToolGUI; // will hopefully notify parent and layouts
    m_LastToolGUI = NULL;

    QLayout* layout = m_ToolGUIWidget->layout();
    if (layout)
    {
      layout->activate();
    }
  }

  QToolButton* toolButton(NULL);
  //mitk::Tool* tool = m_ToolManager->GetActiveTool();

  if (m_ButtonIDForToolID.find(id) != m_ButtonIDForToolID.end()) // if this tool is in our box
  {
    //toolButton = dynamic_cast<QToolButton*>( Q3ButtonGroup::find( m_ButtonIDForToolID[id] ) );
    toolButton = dynamic_cast<QToolButton*>( m_ToolButtonGroup->buttons().at( m_ButtonIDForToolID[id] ) );
  }

  if ( toolButton )
  {
    // mmueller
    // uncheck all other buttons
    QAbstractButton* tmpBtn = 0;
    QList<QAbstractButton*>::iterator it;
    for(int i=0; i < m_ToolButtonGroup->buttons().size(); ++i)
    {
      tmpBtn = m_ToolButtonGroup->buttons().at(i);
      if(tmpBtn != toolButton)
        dynamic_cast<QToolButton*>( tmpBtn )->setChecked(false);
    }

    toolButton->setChecked(true);

    if (m_ToolGUIWidget && tool)
    {
      // create and reparent new GUI (if any)
      itk::Object::Pointer possibleGUI = tool->GetGUI("Qmitk", "GUI").GetPointer(); // prefix and postfix
      QmitkToolGUI* gui = dynamic_cast<QmitkToolGUI*>( possibleGUI.GetPointer() );

      //!
      m_LastToolGUI = gui;
      if (gui)
      {
        gui->SetTool( tool );

        // mmueller
        //gui->reparent(m_ToolGUIWidget, gui->geometry().topLeft(), true );
        gui->setParent(m_ToolGUIWidget);
        gui->move(gui->geometry().topLeft());
        gui->show();

        QLayout* layout = m_ToolGUIWidget->layout();
        if (!layout)
        {
          layout = new QVBoxLayout( m_ToolGUIWidget );
        }
        if (layout)
        {
          // mmueller
          layout->addWidget( gui );
          //layout->add( gui );
          layout->activate();
        }
      }
    }
  }
  else
  {
    // disable all buttons
    QToolButton* selectedToolButton = dynamic_cast<QToolButton*>( m_ToolButtonGroup->checkedButton() );
    //QToolButton* selectedToolButton = dynamic_cast<QToolButton*>( Q3ButtonGroup::find( Q3ButtonGroup::selectedId() ) );
    if (selectedToolButton)
    {
      // mmueller
      selectedToolButton->setChecked(false);
      //selectedToolButton->setOn(false);
    }
  }
}