void QmitkPointListView::OnPointSetSelectionChanged()
{
  const mitk::PointSet *pointSet = m_PointListModel->GetPointSet();
  if (pointSet == nullptr)
    return;

  // update this view's selection status as a result to changes in the point set data structure
  m_SelfCall = true;
  int timeStep = m_PointListModel->GetTimeStep();

  if (pointSet->GetNumberOfSelected(timeStep) > 1)
  {
    MITK_ERROR << "Point set has multiple selected points. This view is not designed for more than one selected point.";
  }

  int selectedIndex = pointSet->SearchSelectedPoint(timeStep);

  if (selectedIndex == -1) // no selected point is found
  {
    m_SelfCall = false;
    return;
  }

  QModelIndex index;

  bool modelIndexOkay = m_PointListModel->GetModelIndexForPointID(selectedIndex, index);

  if (modelIndexOkay == true)
    QListView::selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);

  emit SignalPointSelectionChanged();

  m_SelfCall = false;
}
void QmitkCorrespondingPointSetsView::OnPointSelectionChanged(const QItemSelection& selected, const QItemSelection&  /*deselected*/)
{
  if(m_SelfCall)
    return;

  std::vector<mitk::DataNode*> pointSetNodes = this->GetPointSetNodes();

  QModelIndexList selectedIndexes = selected.indexes();
  m_CorrespondingPointSetsModel->SetSelectedPointSetIndex(-1);
  if (selectedIndexes.size() > 0)
  {
    QModelIndex index = selectedIndexes.front();
    mitk::DataNode* pointSetNode = NULL;
    mitk::PointSet* pointSet = NULL;

    if (index.column() == 0)
    {
      pointSetNode = pointSetNodes.front();
    }
    else
    {
      pointSetNode = pointSetNodes.back();
    }

    if (pointSetNode)
    {
      this->m_CorrespondingPointSetsModel->UpdateSelection(pointSetNode);
      pointSet = dynamic_cast<mitk::PointSet*>(pointSetNode->GetData());

      if( pointSet->GetPointSet(m_CorrespondingPointSetsModel->GetTimeStep()))
      for (mitk::PointSet::PointsContainer::Iterator it = pointSet->GetPointSet(m_CorrespondingPointSetsModel->GetTimeStep())->GetPoints()->Begin();
        it != pointSet->GetPointSet(m_CorrespondingPointSetsModel->GetTimeStep())->GetPoints()->End(); ++it)
        {
          QModelIndex tempIndex;
          if (m_CorrespondingPointSetsModel->GetModelIndexForPointID(it->Index(), tempIndex, index.column()))
          {
            if (tempIndex == index)
            {
              pointSet->SetSelectInfo(it->Index(), true, m_CorrespondingPointSetsModel->GetTimeStep());

              m_CorrespondingPointSetsModel->SetSelectedPointSetIndex(index.column());
              if ( this->GetMultiWidget() != NULL)
              {
                this->GetMultiWidget()->MoveCrossToPosition(pointSet->GetPoint(it->Index(), m_CorrespondingPointSetsModel->GetTimeStep()));
              }
            }
            else
            {
              pointSet->SetSelectInfo(it->Index(), false, m_CorrespondingPointSetsModel->GetTimeStep());
            }
          }
        }
    }
  }
  emit(SignalPointSelectionChanged());
  mitk::RenderingManager::GetInstance()->RequestUpdateAll();
  this->UpdateSelectionHighlighting();
  m_SelfCall = false;
}
QmitkCorrespondingPointSetsWidget::QmitkCorrespondingPointSetsWidget(QWidget *parent):
  QWidget(parent),
  m_CorrespondingPointSetsView(NULL)
{
  // create new QTableView
  m_CorrespondingPointSetsView = new QmitkCorrespondingPointSetsView();
  // setup user interface
  SetupUi();
  // setup connections
  connect( this->m_CorrespondingPointSetsView, SIGNAL(SignalPointSelectionChanged()), this, SLOT(OnPointSelectionChanged()) );
  connect( this->m_CorrespondingPointSetsView, SIGNAL(SignalAddPointsModeChanged(bool)), this, SLOT(OnAddPointsModeChanged(bool)) );
}
void QmitkPointListWidget::SetupConnections()
{
  connect(this->m_LoadPointsBtn, SIGNAL(clicked()), this, SLOT(OnBtnLoadPoints()));
  connect(this->m_SavePointsBtn, SIGNAL(clicked()), this, SLOT(OnBtnSavePoints()));
  connect(this->m_MovePointUpBtn, SIGNAL(clicked()), this, SLOT(MoveSelectedPointUp()));
  connect(this->m_MovePointDownBtn, SIGNAL(clicked()), this, SLOT(MoveSelectedPointDown()));
  connect(this->m_RemovePointBtn, SIGNAL(clicked()), this, SLOT(RemoveSelectedPoint()));
  connect(this->m_ToggleAddPoint, SIGNAL(toggled(bool)), this, SLOT(OnBtnAddPoint(bool)));
  connect(this->m_AddPoint, SIGNAL(clicked()), this, SLOT(OnBtnAddPointManually()));
  connect(this->m_PointListView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(OnListDoubleClick()));
  connect(this->m_PointListView, SIGNAL(SignalPointSelectionChanged()), this, SLOT(OnPointSelectionChanged()));
}
void QmitkPointListView::OnListViewSelectionChanged(const QItemSelection &selected,
                                                    const QItemSelection & /*deselected*/)
{
  if (m_SelfCall)
    return;

  mitk::PointSet *pointSet = const_cast<mitk::PointSet *>(m_PointListModel->GetPointSet());

  if (pointSet == nullptr)
    return;

  // (take care that this widget doesn't react to self-induced changes by setting m_SelfCall)
  m_SelfCall = true;

  // update selection of all points in pointset: select the one(s) that are selected in the view, deselect all others
  QModelIndexList selectedIndexes = selected.indexes();

  for (mitk::PointSet::PointsContainer::Iterator it =
         pointSet->GetPointSet(m_PointListModel->GetTimeStep())->GetPoints()->Begin();
       it != pointSet->GetPointSet(m_PointListModel->GetTimeStep())->GetPoints()->End();
       ++it)
  {
    QModelIndex index;
    if (m_PointListModel->GetModelIndexForPointID(it->Index(), index))
    {
      if (selectedIndexes.indexOf(index) != -1) // index is found in the selected indices list
      {
        pointSet->SetSelectInfo(it->Index(), true, m_PointListModel->GetTimeStep());

        mitk::Point3D p = pointSet->GetPoint(it->Index(), m_PointListModel->GetTimeStep());

        for (auto snc : m_Sncs)
          snc->SelectSliceByPoint(p);
      }
      else
      {
        pointSet->SetSelectInfo(it->Index(), false, m_PointListModel->GetTimeStep());
      }
    }
  }

  m_SelfCall = false;

  emit SignalPointSelectionChanged();

  mitk::RenderingManager::GetInstance()->RequestUpdateAll();
}
void QmitkCorrespondingPointSetsView::ClearCurrentTimeStep()
{
  switch( QMessageBox::question( this, tr("Clear time step"),
                                 tr("Remove points from current time step of the right clicked list?"),
                                 QMessageBox::Yes | QMessageBox::No, QMessageBox::No))
  {
  case QMessageBox::Yes:
    {
      this->m_CorrespondingPointSetsModel->ClearCurrentTimeStep();
      break;
    }
  case QMessageBox::No:
    break;
  default:
    break;
  }
  emit(SignalPointSelectionChanged());
}
void QmitkCorrespondingPointSetsView::ClearSelectedPointSet()
{
  switch( QMessageBox::question( this, tr("Clear point set"),
                                 tr("Remove all points from the right clicked list?"),
                                 QMessageBox::Yes | QMessageBox::No, QMessageBox::No))
  {
  case QMessageBox::Yes:
    {
      this->m_CorrespondingPointSetsModel->ClearSelectedPointSet();
      break;
    }
  case QMessageBox::No:
    break;
  default:
    break;
  }
  emit(SignalPointSelectionChanged());
}
void QmitkCorrespondingPointSetsView::wheelEvent(QWheelEvent *event)
{
  if (!this->m_CorrespondingPointSetsModel->GetStepper())
    return;

  int whe = event->delta();
  int pos = this->m_CorrespondingPointSetsModel->GetStepper()->GetPos();

  int currentTS =  this->m_CorrespondingPointSetsModel->GetTimeStep();
  if(whe > 0)
  {
    this->m_CorrespondingPointSetsModel->SetTimeStep(++currentTS);
    this->m_CorrespondingPointSetsModel->GetStepper()->SetPos(++pos);
  }
  else if( pos > 0 )
  {
    this->m_CorrespondingPointSetsModel->SetTimeStep(--currentTS);
    this->m_CorrespondingPointSetsModel->GetStepper()->SetPos(--pos);
  }
  fadeTimeStepIn();
  emit SignalPointSelectionChanged();
}
void QmitkCorrespondingPointSetsView::RemoveSelectedPoint()
{
  this->m_CorrespondingPointSetsModel->RemoveSelectedPoint();
  emit(SignalPointSelectionChanged());
}