<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 "mitkVolumeDataVtkMapper3D.h"
#include "QmitkAbortEventFilter.h"
#include "QmitkRenderWindow.h"
#include "QmitkRenderingManager.h"


#include "mitkNodePredicateProperty.h"
#include "mitkDataStorage.h"
#include "mitkProperties.h"

#include <QApplication>
#include <QEventLoop>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QPointer>

QmitkAbortEventFilter*
QmitkAbortEventFilter
::GetInstance()
{
  static QmitkAbortEventFilter instance;
  return &instance;
}

QmitkAbortEventFilter
::QmitkAbortEventFilter()
: m_LODRendererAtButtonPress( NULL )
{
}
QmitkAbortEventFilter
::~QmitkAbortEventFilter()

{
}


bool 
QmitkAbortEventFilter
::eventFilter( QObject *object, QEvent *event )
{   
  typedef QPointer< QObject > GuardedObject;
  // Extract renderer (if event has been invoked on a RenderWindow)
  bool isLODRenderer = false;
  mitk::BaseRenderer *renderer = NULL;
  QVTKWidget *qVTKWidget = dynamic_cast< QVTKWidget * >( object );
  if (qVTKWidget != NULL )
  {
    renderer = mitk::BaseRenderer::GetInstance( qVTKWidget->GetRenderWindow() );
    if ( renderer && renderer->GetNumberOfVisibleLODEnabledMappers() > 0 )
    {
      isLODRenderer = true;
    }
    else
{
      // Only LOD enabled renderers are considered
      renderer = NULL;
    }

  }

  if (mitk::RenderingManager::GetInstance()->IsRendering() )
  {
    switch ( event->type() )
    {

    case QEvent::MouseButtonPress:
    {
      // Let only the first (RenderWindow specific) click event affect
      // the LOD process (Qt issues multiple events on mouse click, but the
      // RenderWindow specific one is issued first)
      if ( !m_ButtonPressed )
      {
        m_ButtonPressed = true;

        // Abort current rendering (for all rendering windows!)
        mitk::RenderingManager::GetInstance()->AbortRendering();

        // Block LOD increase until mouse button is released
        mitk::RenderingManager::GetInstance()->LODIncreaseBlockedOn();

        // Store renderer (if LOD-enabled), otherwise renderer will be NULL
        m_LODRendererAtButtonPress = renderer;
      }

      // Store renderer if it is LOD enabled for mouse release
      if ( isLODRenderer )
      {
        m_LODRendererAtButtonPress = renderer;
      }

      QMouseEvent* me = ( QMouseEvent* )( event );
      QMouseEvent* newEvent = new QMouseEvent(
        me->type(), me->pos(), me->globalPos(), me->button(),
        me->buttons(), me->modifiers());
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;
    }


    case QEvent::MouseButtonDblClick:
    { 
      QMouseEvent* me = ( QMouseEvent* )( event );
      QMouseEvent* newEvent = new QMouseEvent(
        me->type(), me->pos(), me->globalPos(), me->button(),
        me->buttons(), me->modifiers());
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;
    }

    case QEvent::MouseMove:
    {
      if ( m_ButtonPressed )
      {
        if ( isLODRenderer && mitk::RenderingManager::GetInstance()->GetNextLOD( m_LODRendererAtButtonPress ) != 0 )
        {
          mitk::RenderingManager::GetInstance()->AbortRendering();
        }
      }
      return true;
    }

    case QEvent::MouseButtonRelease:
    {
      if ( m_ButtonPressed )
      {
        m_ButtonPressed = false;
        
        mitk::RenderingManager::GetInstance()->LODIncreaseBlockedOff();
      }
      
      QMouseEvent* me = ( QMouseEvent* )( event );
      QMouseEvent* newEvent = new QMouseEvent(
        me->type(), me->pos(), me->globalPos(), me->button(),
        me->buttons(), me->modifiers());
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;
    }

    case QEvent::Wheel:
    {
      mitk::RenderingManager::GetInstance()->AbortRendering();

      QWheelEvent* we = ( QWheelEvent* )( event );
      QWheelEvent* newEvent = new QWheelEvent(
        we->pos(), we->globalPos(), we->delta(), we->buttons(),
        we->modifiers(), we->orientation()
      );
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
        return true;
      }

    case QEvent::Paint:
      { 
        QPaintEvent* pe = ( QPaintEvent* )( event );
      QPaintEvent* newEvent = new QPaintEvent( pe->region() );
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;
      }

      case QEvent::KeyPress:
      { 
        mitk::RenderingManager::GetInstance()->AbortRendering();
        QKeyEvent* ke = ( QKeyEvent* )( event );
        QKeyEvent* newEvent = new QKeyEvent(
          ke->type(), ke->key(), ke->modifiers(), ke->text(), false, ke->count()
        );
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;
    }

    case QmitkRenderingRequestEvent::RenderingRequest:
      { 
      QmitkRenderingRequestEvent *newEvent = new QmitkRenderingRequestEvent();
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
        return true;
      }

    case QEvent::ChildInserted: //change Layout (Big3D, 2D images up, etc.)	 
    {	 
      QChildEvent* ce = ( QChildEvent* )( event );	 
      QChildEvent* newEvent = new QChildEvent(	 
        QEvent::ChildInserted, ce->child() );	 
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;	 
    }	 

    case QEvent::ChildRemoved: //change Layout (Big3D, 2D images up, etc.)	 
    {	 
      QChildEvent* ce = ( QChildEvent* )( event );	 
      QChildEvent* newEvent = new QChildEvent(	 
        QEvent::ChildRemoved, ce->child() );	 
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;	 
    }	 

    case QEvent::Show:	 
    {	 
      QShowEvent* newEvent = new QShowEvent();	 
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;	 
    }	 

    case QEvent::Hide:	 
    {	 
      QHideEvent* newEvent = new QHideEvent();	 
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;
    }

    case QEvent::Close:
    {<commit_msg>FIX (#581): deprecated QEvent::ChildInserted changed to QEvent::ChildAdded<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 "mitkVolumeDataVtkMapper3D.h"
#include "QmitkAbortEventFilter.h"
#include "QmitkRenderWindow.h"
#include "QmitkRenderingManager.h"


#include "mitkNodePredicateProperty.h"
#include "mitkDataStorage.h"
#include "mitkProperties.h"

#include <QApplication>
#include <QEventLoop>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QPointer>

QmitkAbortEventFilter*
QmitkAbortEventFilter
::GetInstance()
{
  static QmitkAbortEventFilter instance;
  return &instance;
}

QmitkAbortEventFilter
::QmitkAbortEventFilter()
: m_LODRendererAtButtonPress( NULL )
{
}
QmitkAbortEventFilter
::~QmitkAbortEventFilter()

{
}


bool 
QmitkAbortEventFilter
::eventFilter( QObject *object, QEvent *event )
{   
  typedef QPointer< QObject > GuardedObject;
  // Extract renderer (if event has been invoked on a RenderWindow)
  bool isLODRenderer = false;
  mitk::BaseRenderer *renderer = NULL;
  QVTKWidget *qVTKWidget = dynamic_cast< QVTKWidget * >( object );
  if (qVTKWidget != NULL )
  {
    renderer = mitk::BaseRenderer::GetInstance( qVTKWidget->GetRenderWindow() );
    if ( renderer && renderer->GetNumberOfVisibleLODEnabledMappers() > 0 )
    {
      isLODRenderer = true;
    }
    else
{
      // Only LOD enabled renderers are considered
      renderer = NULL;
    }

  }

  if (mitk::RenderingManager::GetInstance()->IsRendering() )
  {
    switch ( event->type() )
    {

    case QEvent::MouseButtonPress:
    {
      // Let only the first (RenderWindow specific) click event affect
      // the LOD process (Qt issues multiple events on mouse click, but the
      // RenderWindow specific one is issued first)
      if ( !m_ButtonPressed )
      {
        m_ButtonPressed = true;

        // Abort current rendering (for all rendering windows!)
        mitk::RenderingManager::GetInstance()->AbortRendering();

        // Block LOD increase until mouse button is released
        mitk::RenderingManager::GetInstance()->LODIncreaseBlockedOn();

        // Store renderer (if LOD-enabled), otherwise renderer will be NULL
        m_LODRendererAtButtonPress = renderer;
      }

      // Store renderer if it is LOD enabled for mouse release
      if ( isLODRenderer )
      {
        m_LODRendererAtButtonPress = renderer;
      }

      QMouseEvent* me = ( QMouseEvent* )( event );
      QMouseEvent* newEvent = new QMouseEvent(
        me->type(), me->pos(), me->globalPos(), me->button(),
        me->buttons(), me->modifiers());
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;
    }


    case QEvent::MouseButtonDblClick:
    { 
      QMouseEvent* me = ( QMouseEvent* )( event );
      QMouseEvent* newEvent = new QMouseEvent(
        me->type(), me->pos(), me->globalPos(), me->button(),
        me->buttons(), me->modifiers());
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;
    }

    case QEvent::MouseMove:
    {
      if ( m_ButtonPressed )
      {
        if ( isLODRenderer && mitk::RenderingManager::GetInstance()->GetNextLOD( m_LODRendererAtButtonPress ) != 0 )
        {
          mitk::RenderingManager::GetInstance()->AbortRendering();
        }
      }
      return true;
    }

    case QEvent::MouseButtonRelease:
    {
      if ( m_ButtonPressed )
      {
        m_ButtonPressed = false;
        
        mitk::RenderingManager::GetInstance()->LODIncreaseBlockedOff();
      }
      
      QMouseEvent* me = ( QMouseEvent* )( event );
      QMouseEvent* newEvent = new QMouseEvent(
        me->type(), me->pos(), me->globalPos(), me->button(),
        me->buttons(), me->modifiers());
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;
    }

    case QEvent::Wheel:
    {
      mitk::RenderingManager::GetInstance()->AbortRendering();

      QWheelEvent* we = ( QWheelEvent* )( event );
      QWheelEvent* newEvent = new QWheelEvent(
        we->pos(), we->globalPos(), we->delta(), we->buttons(),
        we->modifiers(), we->orientation()
      );
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
        return true;
      }

    case QEvent::Paint:
      { 
        QPaintEvent* pe = ( QPaintEvent* )( event );
      QPaintEvent* newEvent = new QPaintEvent( pe->region() );
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;
      }

      case QEvent::KeyPress:
      { 
        mitk::RenderingManager::GetInstance()->AbortRendering();
        QKeyEvent* ke = ( QKeyEvent* )( event );
        QKeyEvent* newEvent = new QKeyEvent(
          ke->type(), ke->key(), ke->modifiers(), ke->text(), false, ke->count()
        );
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;
    }

    case QmitkRenderingRequestEvent::RenderingRequest:
      { 
      QmitkRenderingRequestEvent *newEvent = new QmitkRenderingRequestEvent();
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
        return true;
      }

	case QEvent::ChildAdded: //change Layout (Big3D, 2D images up, etc.)	 
    {	 
      QChildEvent* ce = ( QChildEvent* )( event );	 
      QChildEvent* newEvent = new QChildEvent(	 
        QEvent::ChildAdded, ce->child() );	 
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;	 
    }	 

    case QEvent::ChildRemoved: //change Layout (Big3D, 2D images up, etc.)	 
    {	 
      QChildEvent* ce = ( QChildEvent* )( event );	 
      QChildEvent* newEvent = new QChildEvent(	 
        QEvent::ChildRemoved, ce->child() );	 
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;	 
    }	 

    case QEvent::Show:	 
    {	 
      QShowEvent* newEvent = new QShowEvent();	 
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;	 
    }	 

    case QEvent::Hide:	 
    {	 
      QHideEvent* newEvent = new QHideEvent();	 
      m_EventQueue.push( ObjectEventPair(GuardedObject( object ), newEvent) );
      return true;
    }

    case QEvent::Close:
    {
{'fraction_non_alphanumeric': 0.13802693208430913, 'fraction_numerical': 0.001244145199063232, 'mean_word_length': 3.0718116805721096, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 105, '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': '7198390', 'n_tokens_mistral': 4208, 'n_tokens_neox': 4098, 'n_words': 932}