<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 "mitkDisplayCoordinateOperation.h"
//has to be on top, otherwise compiler error!
#include "mitkCoordinateSupplier.h"
#include "mitkOperation.h"
#include "mitkOperationActor.h"
#include "mitkPointOperation.h"
#include "mitkPositionEvent.h"
//and not here!
#include <string>
#include "mitkInteractionConst.h"
#include "mitkAction.h"


//##ModelId=3F0189F0025B
mitk::CoordinateSupplier::CoordinateSupplier(const char * type, mitk::OperationActor* operationActor)
: mitk::StateMachine(type), m_Destination(operationActor)
{
}

//##ModelId=3F0189F00269
bool mitk::CoordinateSupplier::ExecuteAction(Action* action, mitk::StateEvent const* stateEvent)
{
    bool ok = false;
    if (m_Destination == NULL)
        return false;
	
    const PositionEvent* posEvent = dynamic_cast<const PositionEvent*>(stateEvent->GetEvent());
    
    if(posEvent!=NULL)
    {
      const Geometry2D* worldGeometry = stateEvent->GetEvent()->GetSender()->GetCurrentWorldGeometry2D();
      assert( worldGeometry != NULL );
      ScalarType timeInMS = worldGeometry->GetTimeBoundsInMS()[ 0 ];
      switch (action->GetActionId())
      {
        case AcNEWPOINT:
        {
          m_OldPoint = posEvent->GetWorldPosition();

			    PointOperation* doOp = new mitk::PointOperation(OpADD, timeInMS, m_OldPoint, 0);
			    //Undo
          if (m_UndoEnabled)
          {
				    PointOperation* undoOp = new PointOperation(OpDELETE, m_OldPoint, 0);
            OperationEvent *operationEvent = new OperationEvent( m_Destination, doOp, undoOp );
            m_UndoController->SetOperationEvent(operationEvent);
          }
          //execute the Operation
			    m_Destination->ExecuteOperation(doOp);
          ok = true;<commit_msg>FIX: avoid crashes in case of StateEvent::m_Sender==NULL<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 "mitkDisplayCoordinateOperation.h"
//has to be on top, otherwise compiler error!
#include "mitkCoordinateSupplier.h"
#include "mitkOperation.h"
#include "mitkOperationActor.h"
#include "mitkPointOperation.h"
#include "mitkPositionEvent.h"
//and not here!
#include <string>
#include "mitkInteractionConst.h"
#include "mitkAction.h"


//##ModelId=3F0189F0025B
mitk::CoordinateSupplier::CoordinateSupplier(const char * type, mitk::OperationActor* operationActor)
: mitk::StateMachine(type), m_Destination(operationActor)
{
}

//##ModelId=3F0189F00269
bool mitk::CoordinateSupplier::ExecuteAction(Action* action, mitk::StateEvent const* stateEvent)
{
    bool ok = false;
    if (m_Destination == NULL)
        return false;
	
    const PositionEvent* posEvent = dynamic_cast<const PositionEvent*>(stateEvent->GetEvent());
    
    if(posEvent!=NULL)
    {
      ScalarType timeInMS = 0;
      if(stateEvent->GetEvent()->GetSender()!=NULL)
      {
        const Geometry2D* worldGeometry = stateEvent->GetEvent()->GetSender()->GetCurrentWorldGeometry2D();
        assert( worldGeometry != NULL );
        timeInMS = worldGeometry->GetTimeBoundsInMS()[ 0 ];
      }
      else
      {
        itkWarningMacro(<<"StateEvent::GetSender()==NULL - setting timeInMS to 0");
      }

      switch (action->GetActionId())
      {
        case AcNEWPOINT:
        {
          m_OldPoint = posEvent->GetWorldPosition();

			    PointOperation* doOp = new mitk::PointOperation(OpADD, timeInMS, m_OldPoint, 0);
			    //Undo
          if (m_UndoEnabled)
          {
				    PointOperation* undoOp = new PointOperation(OpDELETE, m_OldPoint, 0);
            OperationEvent *operationEvent = new OperationEvent( m_Destination, doOp, undoOp );
            m_UndoController->SetOperationEvent(operationEvent);
          }
          //execute the Operation
			    m_Destination->ExecuteOperation(doOp);
          ok = true;
{'fraction_non_alphanumeric': 0.15804084870116994, 'fraction_numerical': 0.009914733293674401, 'mean_word_length': 4.094949494949495, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, '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': '1359195', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1380, 'n_words': 350}