//
#include "CompositeView.h"

//Qt
#include <QGLContext>
#include <QGraphicsScene>
#include <QResizeEvent>
#include <QPainter>
#include <QMouseEvent>
#include <QGraphicsItem>
//VTK
#include <QVTKWidget2.h>
#include <vtkRendererCollection.h>
#include <vtkRenderer.h>
#include <vtkCollectionIterator.h>
#include <vtkSmartPointer.h>
#include <vtkGenericOpenGLRenderWindow.h>

CompositeView::CompositeView(QWidget *parent)
  : QGraphicsView(parent)
{
  double backgroundColorRGB[3] = {1., 1., 1.}; // default: white
  Initialize(backgroundColorRGB);
}

CompositeView::CompositeView(double backgroundColorRGB[])
{
  Initialize(backgroundColorRGB);
}

CompositeView::~CompositeView()
{
  vtkRendererCollection *rens = m_BackgroundVTKWidget->GetRenderWindow()->GetRenderers();
  vtkCollectionSimpleIterator rsit;
  vtkRenderer *ren;
  for (rens->InitTraversal(rsit); (ren = rens->GetNextRenderer(rsit)); )
    m_BackgroundVTKWidget->GetRenderWindow()->RemoveRenderer(ren);
  delete m_BackgroundVTKWidget;
}

vtkRenderer* CompositeView::GetFirstRenderer()
{
  return this->m_BackgroundVTKWidget->GetRenderWindow()->GetRenderers()->GetFirstRenderer();
}


void CompositeView::MakeBlankBackground(double color[])
{
  vtkRendererCollection *rens = m_BackgroundVTKWidget->GetRenderWindow()->GetRenderers();
  vtkCollectionSimpleIterator rsit;
  vtkRenderer *ren;
  for (rens->InitTraversal(rsit); (ren = rens->GetNextRenderer(rsit)); )
    m_BackgroundVTKWidget->GetRenderWindow()->RemoveRenderer(ren);

  ren = vtkRenderer::New();
  m_BackgroundVTKWidget->GetRenderWindow()->AddRenderer(ren);
  ren->ResetCamera();
  ren->SetBackground(color);
  ren->Delete();
  m_BackgroundVTKWidget->GetRenderWindow()->Render();
}

void CompositeView::drawBackground(QPainter *p, const QRectF &r)
{
#if QT_VERSION >= 0x040600
  p->beginNativePainting();
#endif
  m_BackgroundVTKWidget->GetRenderWindow()->PushState();
  m_BackgroundVTKWidget->GetRenderWindow()->OpenGLInitState();
  m_BackgroundVTKWidget->GetRenderWindow()->Render();
  m_BackgroundVTKWidget->GetRenderWindow()->PopState();
#if QT_VERSION >= 0x040600
  p->endNativePainting();
#endif
}

void CompositeView::resizeEvent(QResizeEvent *event)
{
  if (scene()) // give the same size to the scene that his widget has
    scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
  QGraphicsView::resizeEvent(event);
  m_BackgroundVTKWidget->GetRenderWindow()->SetSize(event->size().width(), event->size().height());
}

void CompositeView::mouseMoveEvent(QMouseEvent *event)
{
  QGraphicsItem *item = NULL;
  if (item = itemAt(event->pos()))
    QGraphicsView::mouseMoveEvent(event);
}

void CompositeView::Initialize(double backgroundColorRGB[])
{
  m_GLContext = new QGLContext(QGLFormat());
  m_BackgroundVTKWidget = new QVTKWidget2(m_GLContext, this);
  this->setViewport(m_BackgroundVTKWidget);
  this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  m_Scene = new QGraphicsScene(this);
  this->setScene(m_Scene);
  m_BackgroundVTKWidget->GetRenderWindow()->SetSwapBuffers(0);  // avoid flickering
  m_BackgroundVTKWidget->setAutoBufferSwap(true);

  MakeBlankBackground(backgroundColorRGB);
}
