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

Program:   Medical Imaging & Interaction Toolkit
Module:    $RCSfile$
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 "mitkGlobalInteraction.h"
#include "mitkInteractionConst.h"
#include "mitkEvent.h"
#include "mitkAction.h"
#include "mitkStateEvent.h"
#include <mitkStatusBar.h>
#include <mitkPositionEvent.h>
#include <vtkWorldPointPicker.h>
#include <mitkOpenGLRenderer.h>

mitk::GlobalInteraction::Pointer mitk::GlobalInteraction::s_GlobalInteraction(NULL);


mitk::GlobalInteraction::GlobalInteraction(const char * type)
: StateMachine(type),
  m_CurrentlyInInformListenersLoop(false)
{
  //build up the FocusManager
  m_FocusManager = new mitk::FocusManager();
  m_InteractionDebugger = InteractionDebugger::New();
  InteractionDebugger::Deactivate();
}

mitk::GlobalInteraction::~GlobalInteraction()
{
  //s_GlobalInteraction doesn't have to be set = NULL;
}


inline mitk::StateEvent* GenerateEmptyStateEvent(int eventId)
{
  mitk::Event *noEvent = new mitk::Event(NULL,
    mitk::Type_User,
    mitk::BS_NoButton,
    mitk::BS_NoButton,
    mitk::Key_none);
  mitk::StateEvent *stateEvent = new mitk::StateEvent(eventId, noEvent);
  return stateEvent;
}


void mitk::GlobalInteraction::AddListener(mitk::StateMachine* listener)
{
  if(listener == NULL) return;
  if(dynamic_cast<Interactor*>(listener)!=NULL)
  {
    itkWarningMacro(<<"Trying to add an Interactor ("
      << listener->GetNameOfClass() << ") as a listener. "
      << "This will probably cause problems");
  }

  if ( std::find(m_ListenerList.begin(), m_ListenerList.end(),listener) == m_ListenerList.end() )
  {
    m_ListenerList.push_back(listener);
  }
}


bool mitk::GlobalInteraction::RemoveListener(mitk::StateMachine* listener)
{
  // Defers removal to a time after the current event handling is finished. Otherwise the implementation of InformListeners would crash sometimes.
  m_ListenersFlaggedForRemoval.push_back(listener);

  StateMachineListIter position = std::find(m_ListenerList.begin(), m_ListenerList.end(),listener);
  bool removePossible = (position != m_ListenerList.end());

  RemoveFlaggedListeners();

  return removePossible;
}

void mitk::GlobalInteraction::RemoveFlaggedListeners()
{
  if (m_CurrentlyInInformListenersLoop) return;

  // iterate flagged listeners, remove them if possibleA
  if (m_ListenersFlaggedForRemoval.empty()) return;

  for (StateMachineListIter it = m_ListenersFlaggedForRemoval.begin(); it != m_ListenersFlaggedForRemoval.end(); ++it)
  {
    StateMachineListIter foundPosition = std::find( m_ListenerList.begin(), m_ListenerList.end(), it->GetPointer());
    if (foundPosition != m_ListenerList.end())
    {
      m_ListenerList.erase( foundPosition );
    }
  }
 
  m_ListenersFlaggedForRemoval.clear();
}

void mitk::GlobalInteraction::AddInteractor(mitk::Interactor* interactor)
{
  if(interactor == NULL) return;
  if ( std::find(m_InteractorList.begin(), m_InteractorList.end(),interactor) == m_InteractorList.end() )
  {
    m_InteractorList.push_back(interactor);
    
    //if Interactor already selected, then add to selected list
    if (interactor->GetMode()==Interactor::SMSELECTED)
      this->AddToSelectedInteractors(interactor);
  }
}

bool mitk::GlobalInteraction::InteractorRegistered (mitk::Interactor* interactor)
{
  if ( std::find(m_InteractorList.begin(), m_InteractorList.end(), interactor) == m_InteractorList.end() )
    return false;
  else<commit_msg>FIX bug #566: ERIS crashes when using Add, Subtract or Region Growing Tools<commit_after>/*=========================================================================

Program:   Medical Imaging & Interaction Toolkit
Module:    $RCSfile$
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 "mitkGlobalInteraction.h"
#include "mitkInteractionConst.h"
#include "mitkEvent.h"
#include "mitkAction.h"
#include "mitkStateEvent.h"
#include <mitkStatusBar.h>
#include <mitkPositionEvent.h>
#include <vtkWorldPointPicker.h>
#include <mitkOpenGLRenderer.h>

mitk::GlobalInteraction::Pointer mitk::GlobalInteraction::s_GlobalInteraction(NULL);


mitk::GlobalInteraction::GlobalInteraction(const char * type)
: StateMachine(type),
  m_CurrentlyInInformListenersLoop(false)
{
  //build up the FocusManager
  m_FocusManager = new mitk::FocusManager();
  m_InteractionDebugger = InteractionDebugger::New();
  InteractionDebugger::Deactivate();
}

mitk::GlobalInteraction::~GlobalInteraction()
{
  //s_GlobalInteraction doesn't have to be set = NULL;
}


inline mitk::StateEvent* GenerateEmptyStateEvent(int eventId)
{
  mitk::Event *noEvent = new mitk::Event(NULL,
    mitk::Type_User,
    mitk::BS_NoButton,
    mitk::BS_NoButton,
    mitk::Key_none);
  mitk::StateEvent *stateEvent = new mitk::StateEvent(eventId, noEvent);
  return stateEvent;
}


void mitk::GlobalInteraction::AddListener(mitk::StateMachine* listener)
{
  if(listener == NULL) return;
  if(dynamic_cast<Interactor*>(listener)!=NULL)
  {
    itkWarningMacro(<<"Trying to add an Interactor ("
      << listener->GetNameOfClass() << ") as a listener. "
      << "This will probably cause problems");
  }

  if ( std::find(m_ListenerList.begin(), m_ListenerList.end(),listener) == m_ListenerList.end() )
  {
    m_ListenerList.push_back(listener);
  }
}


bool mitk::GlobalInteraction::RemoveListener(mitk::StateMachine* listener)
{
  // Defers removal to a time after the current event handling is finished. Otherwise the implementation of InformListeners would crash sometimes.
  m_ListenersFlaggedForRemoval.push_back(listener);

  StateMachineListIter position = std::find(m_ListenerList.begin(), m_ListenerList.end(),listener);
  bool removePossible = (position != m_ListenerList.end());

  RemoveFlaggedListeners();

  return removePossible;
}

void mitk::GlobalInteraction::RemoveFlaggedListeners()
{
  if (m_CurrentlyInInformListenersLoop) return;

  // iterate flagged listeners, remove them if possibleA
  if (m_ListenersFlaggedForRemoval.empty()) return;

  for (StateMachineCPointerListIter it = m_ListenersFlaggedForRemoval.begin(); it != m_ListenersFlaggedForRemoval.end(); ++it)
  {
    StateMachineListIter foundPosition = std::find( m_ListenerList.begin(), m_ListenerList.end(), *it );
    if (foundPosition != m_ListenerList.end())
    {
      m_ListenerList.erase( foundPosition );
    }
  }
 
  m_ListenersFlaggedForRemoval.clear();
}

void mitk::GlobalInteraction::AddInteractor(mitk::Interactor* interactor)
{
  if(interactor == NULL) return;
  if ( std::find(m_InteractorList.begin(), m_InteractorList.end(),interactor) == m_InteractorList.end() )
  {
    m_InteractorList.push_back(interactor);
    
    //if Interactor already selected, then add to selected list
    if (interactor->GetMode()==Interactor::SMSELECTED)
      this->AddToSelectedInteractors(interactor);
  }
}

bool mitk::GlobalInteraction::InteractorRegistered (mitk::Interactor* interactor)
{
  if ( std::find(m_InteractorList.begin(), m_InteractorList.end(), interactor) == m_InteractorList.end() )
    return false;
  else
{'fraction_non_alphanumeric': 0.14976326937453277, 'fraction_numerical': 0.000373785198106155, 'mean_word_length': 5.913867355727821, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 20, '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': '2878733', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2406, 'n_words': 521}