// Relict from the old times, when automous decisions were accepted
// behavior. Remains in here, because some RenderWindows do exist outside
// of StdMultiWidgets.
bool
SliceNavigationController
::ExecuteAction( Action* action, StateEvent const* stateEvent )
{
  bool ok = false;

  const PositionEvent* posEvent = dynamic_cast< const PositionEvent * >(
    stateEvent->GetEvent() );
  if ( posEvent != NULL )
  {
    if ( m_CreatedWorldGeometry.IsNull() )
    {
      return true;
    }
    switch (action->GetActionId())
    {
    case AcMOVE:
      {
        BaseRenderer *baseRenderer = posEvent->GetSender();
        if ( !baseRenderer )
        {
          baseRenderer = const_cast<BaseRenderer *>(
            GlobalInteraction::GetInstance()->GetFocus() );
        }
        if ( baseRenderer )
          if ( baseRenderer->GetMapperID() == 1 )
          {
            PointOperation* doOp = new mitk::PointOperation(OpMOVE, posEvent->GetWorldPosition());
            if (m_UndoEnabled)
            {
              m_OldPos = this->GetSlice()->GetPos();
              // m_OldPos holds the old slice position. For the undo controller this old position will be stored as index in mitk::PointOperation
              PointOperation* undoOp = new mitk::PointOperation(OpMOVE, posEvent->GetWorldPosition(), m_OldPos);
              OperationEvent *operationEvent = new mitk::OperationEvent(this, doOp, undoOp, "Move slices");
              m_UndoController->SetOperationEvent(operationEvent);
            }

            this->ExecuteOperation( doOp );

            // If click was performed in this render window than we have to update the status bar information about position and pixel value.
            if(baseRenderer == m_Renderer)
            {
              {
                std::string statusText;     
                TNodePredicateDataType<mitk::Image>::Pointer isImageData = TNodePredicateDataType<mitk::Image>::New();

                mitk::DataStorage::SetOfObjects::ConstPointer nodes = baseRenderer->GetDataStorage()->GetSubset(isImageData).GetPointer();
                mitk::Point3D worldposition = posEvent->GetWorldPosition();
                int  maxlayer = -32768;
                mitk::Image::Pointer image3D;
                // find image with largest layer, that is the image shown on top in the render window 
                for (unsigned int x = 0; x < nodes->size(); x++)
                {
                  //Just consider image data that is no helper object. E.g. do not consider nodes created for the slice interpolation
                  bool isHelper (false);
                  nodes->at(x)->GetBoolProperty("helper object", isHelper);

                  if(nodes->at(x)->GetData()->GetGeometry()->IsInside(worldposition) && isHelper == false)
                  {
                    int layer = 0;
                    if(!(nodes->at(x)->GetIntProperty("layer", layer))) continue;
                    if(layer > maxlayer)
                    {
                      if(static_cast<mitk::DataNode::Pointer>(nodes->at(x))->IsVisible(m_Renderer))
                      {
                        image3D = dynamic_cast<mitk::Image*>(nodes->at(x)->GetData());
                        maxlayer = layer;
                      }
                    }
                  }
                }

                std::stringstream stream;
                stream.imbue(std::locale::classic());

                // get the position and gray value from the image and build up status bar text                
                if(image3D.IsNotNull())
                {
                  Index3D p;
                  image3D->GetGeometry()->WorldToIndex(worldposition, p);
                  stream.precision(2);
                  stream<<"Position: <" << std::fixed <<worldposition[0] << ", " << std::fixed << worldposition[1] << ", " << std::fixed << worldposition[2] << "> mm";
                  stream<<"; Index: <"<<p[0] << ", " << p[1] << ", " << p[2] << "> ";
                  mitk::ScalarType pixelValue = image3D->GetPixelValueByIndex(p, baseRenderer->GetTimeStep());
                  if (fabs(pixelValue)>1000000)
                  {
                    stream<<"; Time: " << baseRenderer->GetTime() << " ms; Pixelvalue: "<<std::scientific<<image3D->GetPixelValueByIndex(p, baseRenderer->GetTimeStep())<<"  ";
                  }
                  else
                  {
                    stream<<"; Time: " << baseRenderer->GetTime() << " ms; Pixelvalue: "<<image3D->GetPixelValueByIndex(p, baseRenderer->GetTimeStep())<<"  ";
                  }
                }
                else
                {
                  stream << "No image information at this position!";
                }

                statusText = stream.str(); 
                mitk::StatusBar::GetInstance()->DisplayGreyValueText(statusText.c_str());
             
              }

            }
            ok = true;
            break;
          }
      }
    default:
      ok = true;
      break;
    }
    return ok;
  } 

  const DisplayPositionEvent *displPosEvent =
    dynamic_cast< const DisplayPositionEvent * >( stateEvent->GetEvent() );

  if ( displPosEvent != NULL )
  {
    return true;
  }

  return false;
}
// Relict from the old times, when automous decisions were accepted
// behavior. Remains in here, because some RenderWindows do exist outside
// of StdMultiWidgets.
bool
SliceNavigationController
::ExecuteAction( Action* action, StateEvent const* stateEvent )
{
  bool ok = false;

  const PositionEvent* posEvent = dynamic_cast< const PositionEvent * >(
    stateEvent->GetEvent() );
  if ( posEvent != NULL )
  {
    if ( m_CreatedWorldGeometry.IsNull() )
    {
      return true;
    }
    switch (action->GetActionId())
    {
    case AcMOVE:
      {
        BaseRenderer *baseRenderer = posEvent->GetSender();
        if ( !baseRenderer )
        {
          baseRenderer = const_cast<BaseRenderer *>(
            GlobalInteraction::GetInstance()->GetFocus() );
        }
        if ( baseRenderer )
          if ( baseRenderer->GetMapperID() == 1 )
          {
            PointOperation doOp(OpMOVE, posEvent->GetWorldPosition());

            this->ExecuteOperation( &doOp );

            // If click was performed in this render window than we have to update the status bar information about position and pixel value.
            if(baseRenderer == m_Renderer)
            {
              {
                std::string statusText;
                TNodePredicateDataType<mitk::Image>::Pointer isImageData = TNodePredicateDataType<mitk::Image>::New();

                mitk::DataStorage::SetOfObjects::ConstPointer nodes = baseRenderer->GetDataStorage()->GetSubset(isImageData).GetPointer();
                mitk::Point3D worldposition = posEvent->GetWorldPosition();
                //int  maxlayer = -32768;
                mitk::Image::Pointer image3D;
                mitk::DataNode::Pointer node;
                mitk::DataNode::Pointer topSourceNode;

                bool isBinary (false);

                node = this->GetTopLayerNode(nodes,worldposition);
                if(node.IsNotNull())
                {
                  node->GetBoolProperty("binary", isBinary);
                  if(isBinary)
                  {
                    mitk::DataStorage::SetOfObjects::ConstPointer sourcenodes = baseRenderer->GetDataStorage()->GetSources(node, NULL, true);
                    if(!sourcenodes->empty())
                    {
                      topSourceNode = this->GetTopLayerNode(sourcenodes,worldposition);
                    }
                    if(topSourceNode.IsNotNull())
                    {
                      image3D = dynamic_cast<mitk::Image*>(topSourceNode->GetData());
                    }
                    else
                    {
                      image3D = dynamic_cast<mitk::Image*>(node->GetData());
                    }
                  }
                  else
                  {
                    image3D = dynamic_cast<mitk::Image*>(node->GetData());
                  }
                }
                std::stringstream stream;
                stream.imbue(std::locale::classic());

                // get the position and gray value from the image and build up status bar text
                if(image3D.IsNotNull())
                {
                  Index3D p;
                  image3D->GetGeometry()->WorldToIndex(worldposition, p);
                  stream.precision(2);
                  stream<<"Position: <" << std::fixed <<worldposition[0] << ", " << std::fixed << worldposition[1] << ", " << std::fixed << worldposition[2] << "> mm";
                  stream<<"; Index: <"<<p[0] << ", " << p[1] << ", " << p[2] << "> ";
                  mitk::ScalarType pixelValue = image3D->GetPixelValueByIndex(p, baseRenderer->GetTimeStep());
                  if (fabs(pixelValue)>1000000 || fabs(pixelValue) < 0.01)
                  {
                    stream<<"; Time: " << baseRenderer->GetTime() << " ms; Pixelvalue: " << std::scientific<< pixelValue <<"  ";
                  }
                  else
                  {
                    stream<<"; Time: " << baseRenderer->GetTime() << " ms; Pixelvalue: "<< pixelValue <<"  ";
                  }
                }
                else
                {
                  stream << "No image information at this position!";
                }

                statusText = stream.str();
                mitk::StatusBar::GetInstance()->DisplayGreyValueText(statusText.c_str());

              }

            }
            ok = true;
            break;
          }
      }
    default:
      ok = true;
      break;
    }
    return ok;
  }

  const DisplayPositionEvent *displPosEvent =
    dynamic_cast< const DisplayPositionEvent * >( stateEvent->GetEvent() );

  if ( displPosEvent != NULL )
  {
    return true;
  }

  return false;
}