<commit_before>/*=========================================================================

Program:   Medical Imaging & Interaction Toolkit
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include "mitkToolManager.h"
#include "mitkGlobalInteraction.h"
#include "mitkCoreObjectFactory.h"

#include <itkObjectFactoryBase.h>
#include <itkCommand.h>

#include <list>

mitk::ToolManager::ToolManager(DataStorage* storage)
:m_ActiveTool(NULL),
 m_ActiveToolID(-1),
 m_RegisteredClients(0),
 m_DataStorage(storage)
{
  CoreObjectFactory::GetInstance(); // to make sure a CoreObjectFactory was instantiated (and in turn, possible tools are registered) - bug 1029

  // get a list of all known mitk::Tools
  std::list<itk::LightObject::Pointer> thingsThatClaimToBeATool = itk::ObjectFactoryBase::CreateAllInstance("mitkTool");

  // remember these tools
  for ( std::list<itk::LightObject::Pointer>::iterator iter = thingsThatClaimToBeATool.begin();
        iter != thingsThatClaimToBeATool.end();
        ++iter )
  {
    if ( Tool* tool = dynamic_cast<Tool*>( iter->GetPointer() ) )
    {
      tool->SetToolManager(this); // important to call right after instantiation
      tool->ErrorMessage += MessageDelegate1<mitk::ToolManager, std::string>( this, &ToolManager::OnToolErrorMessage );
      tool->GeneralMessage += MessageDelegate1<mitk::ToolManager, std::string>( this, &ToolManager::OnGeneralToolMessage );
      m_Tools.push_back( tool );
    }
  }

  //ActivateTool(0); // first one is default
}

mitk::ToolManager::~ToolManager()
{
  if (m_ActiveTool)
  {
    m_ActiveTool->Deactivated();
    GlobalInteraction::GetInstance()->RemoveListener( m_ActiveTool );

    m_ActiveTool = NULL;
    m_ActiveToolID = -1; // no tool active

    ActiveToolChanged.Send();
  }
  for ( NodeTagMapType::iterator observerTagMapIter = m_ReferenceDataObserverTags.begin(); observerTagMapIter != m_ReferenceDataObserverTags.end(); ++observerTagMapIter )
  {
    observerTagMapIter->first->RemoveObserver( observerTagMapIter->second );
  }
}

void mitk::ToolManager::OnToolErrorMessage(std::string s)
{
  this->ToolErrorMessage(s);
}

void mitk::ToolManager::OnGeneralToolMessage(std::string s)
{
  this->GeneralToolMessage(s);
}


const mitk::ToolManager::ToolVectorTypeConst mitk::ToolManager::GetTools()
{
  ToolVectorTypeConst resultList;

  for ( ToolVectorType::iterator iter = m_Tools.begin();
        iter != m_Tools.end();
        ++iter )
  {
    resultList.push_back( iter->GetPointer() );
  }

  return resultList;
}

mitk::Tool* mitk::ToolManager::GetToolById(int id)
{
  try
  {
    return m_Tools.at(id);
  }
  catch(std::exception&)
  {
    return NULL;
  }
}

bool mitk::ToolManager::ActivateTool(int id)
{
  //LOG_INFO << "ToolManager::ActivateTool("<<id<<")"<<std::endl;
  if( GetToolById(id) == NULL ) return false;

  if ( GetToolById( id ) == m_ActiveTool ) return true; // no change needed

  static int nextTool = -1;
  nextTool = id;
  //LOG_INFO << "ToolManager::ActivateTool("<<id<<"): nextTool = "<<nextTool<<std::endl;

  static bool inActivateTool = false;
  if (inActivateTool)
  {
    //LOG_INFO << "ToolManager::ActivateTool("<<id<<"): already inside ActivateTool somehow, returning now "<<std::endl;
    return true;
  }
  inActivateTool = true;

  while ( nextTool != m_ActiveToolID )
  {
    //LOG_INFO <<"ToolManager::ActivateTool: nextTool = " << nextTool << " (active tool = " << m_ActiveToolID<<")"<<std::endl;
    if (m_ActiveTool)
    {
      m_ActiveTool->Deactivated();
      GlobalInteraction::GetInstance()->RemoveListener( m_ActiveTool );
    }
<commit_msg>FIX (#2659): remove a check that prevented certain tool activation sequences.<commit_after>/*=========================================================================

Program:   Medical Imaging & Interaction Toolkit
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include "mitkToolManager.h"
#include "mitkGlobalInteraction.h"
#include "mitkCoreObjectFactory.h"

#include <itkObjectFactoryBase.h>
#include <itkCommand.h>

#include <list>

mitk::ToolManager::ToolManager(DataStorage* storage)
:m_ActiveTool(NULL),
 m_ActiveToolID(-1),
 m_RegisteredClients(0),
 m_DataStorage(storage)
{
  CoreObjectFactory::GetInstance(); // to make sure a CoreObjectFactory was instantiated (and in turn, possible tools are registered) - bug 1029

  // get a list of all known mitk::Tools
  std::list<itk::LightObject::Pointer> thingsThatClaimToBeATool = itk::ObjectFactoryBase::CreateAllInstance("mitkTool");

  // remember these tools
  for ( std::list<itk::LightObject::Pointer>::iterator iter = thingsThatClaimToBeATool.begin();
        iter != thingsThatClaimToBeATool.end();
        ++iter )
  {
    if ( Tool* tool = dynamic_cast<Tool*>( iter->GetPointer() ) )
    {
      tool->SetToolManager(this); // important to call right after instantiation
      tool->ErrorMessage += MessageDelegate1<mitk::ToolManager, std::string>( this, &ToolManager::OnToolErrorMessage );
      tool->GeneralMessage += MessageDelegate1<mitk::ToolManager, std::string>( this, &ToolManager::OnGeneralToolMessage );
      m_Tools.push_back( tool );
    }
  }

  //ActivateTool(0); // first one is default
}

mitk::ToolManager::~ToolManager()
{
  if (m_ActiveTool)
  {
    m_ActiveTool->Deactivated();
    GlobalInteraction::GetInstance()->RemoveListener( m_ActiveTool );

    m_ActiveTool = NULL;
    m_ActiveToolID = -1; // no tool active

    ActiveToolChanged.Send();
  }
  for ( NodeTagMapType::iterator observerTagMapIter = m_ReferenceDataObserverTags.begin(); observerTagMapIter != m_ReferenceDataObserverTags.end(); ++observerTagMapIter )
  {
    observerTagMapIter->first->RemoveObserver( observerTagMapIter->second );
  }
}

void mitk::ToolManager::OnToolErrorMessage(std::string s)
{
  this->ToolErrorMessage(s);
}

void mitk::ToolManager::OnGeneralToolMessage(std::string s)
{
  this->GeneralToolMessage(s);
}


const mitk::ToolManager::ToolVectorTypeConst mitk::ToolManager::GetTools()
{
  ToolVectorTypeConst resultList;

  for ( ToolVectorType::iterator iter = m_Tools.begin();
        iter != m_Tools.end();
        ++iter )
  {
    resultList.push_back( iter->GetPointer() );
  }

  return resultList;
}

mitk::Tool* mitk::ToolManager::GetToolById(int id)
{
  try
  {
    return m_Tools.at(id);
  }
  catch(std::exception&)
  {
    return NULL;
  }
}

bool mitk::ToolManager::ActivateTool(int id)
{
  //LOG_INFO << "ToolManager::ActivateTool("<<id<<")"<<std::endl;
  //if( GetToolById(id) == NULL ) return false; // NO, invalid IDs are actually used here. Parameter -1 or anything that does not exists will deactivate all tools!

  if ( GetToolById( id ) == m_ActiveTool ) return true; // no change needed

  static int nextTool = -1;
  nextTool = id;
  //LOG_INFO << "ToolManager::ActivateTool("<<id<<"): nextTool = "<<nextTool<<std::endl;

  static bool inActivateTool = false;
  if (inActivateTool)
  {
    //LOG_INFO << "ToolManager::ActivateTool("<<id<<"): already inside ActivateTool somehow, returning now "<<std::endl;
    return true;
  }
  inActivateTool = true;

  while ( nextTool != m_ActiveToolID )
  {
    //LOG_INFO <<"ToolManager::ActivateTool: nextTool = " << nextTool << " (active tool = " << m_ActiveToolID<<")"<<std::endl;
    if (m_ActiveTool)
    {
      m_ActiveTool->Deactivated();
      GlobalInteraction::GetInstance()->RemoveListener( m_ActiveTool );
    }

{'fraction_non_alphanumeric': 0.16501928640308583, 'fraction_numerical': 0.0032545805207328834, 'mean_word_length': 4.851198871650212, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3130149', 'n_tokens_mistral': 2550, 'n_tokens_neox': 2489, 'n_words': 557}