void QmitkTransferFunctionCanvas::keyPressEvent(QKeyEvent * e)
{
  if( m_GrabbedHandle == -1)
    return;

  switch(e->key())
  {
    case Qt::Key_Delete:
      if(this->GetFunctionSize() > 1)
      {
        this->RemoveFunctionPoint(GetFunctionX(m_GrabbedHandle));
        m_GrabbedHandle = -1;
      }
      break;

    case Qt::Key_Left:
      this->MoveFunctionPoint(m_GrabbedHandle, ValidateCoord(std::make_pair( GetFunctionX(m_GrabbedHandle)-1 , GetFunctionY(m_GrabbedHandle))));
      break;

    case Qt::Key_Right:
      this->MoveFunctionPoint(m_GrabbedHandle, ValidateCoord(std::make_pair( GetFunctionX(m_GrabbedHandle)+1 , GetFunctionY(m_GrabbedHandle))));
      break;

    case Qt::Key_Up:
      this->MoveFunctionPoint(m_GrabbedHandle, ValidateCoord(std::make_pair( GetFunctionX(m_GrabbedHandle) , GetFunctionY(m_GrabbedHandle)+0.001)));
      break;

    case Qt::Key_Down:
      this->MoveFunctionPoint(m_GrabbedHandle, ValidateCoord(std::make_pair( GetFunctionX(m_GrabbedHandle) , GetFunctionY(m_GrabbedHandle)-0.001)));
      break;
  }

  update();
  mitk::RenderingManager::GetInstance()->RequestUpdateAll();
}
void QmitkColorTransferFunctionCanvas::MoveFunctionPoint(int index,
    std::pair<double,double> pos)
{
  double color[3];
  m_ColorTransferFunction->GetColor(GetFunctionX(index), color);
  RemoveFunctionPoint( GetFunctionX(index));
  m_ColorTransferFunction->AddRGBPoint(pos.first, color[0], color[1], color[2]);
}
Exemplo n.º 3
0
void QmitkPiecewiseFunctionCanvas::MoveFunctionPoint(int index,
    std::pair<vtkFloatingPointType,vtkFloatingPointType> pos)
{
  RemoveFunctionPoint(GetFunctionX(index));
  AddFunctionPoint(pos.first, pos.second);
  //std::cout<<" AddFunctionPoint x: "<<pos.first<<" AddFunctionPoint y: "<<pos.second<<std::endl;
}
void QmitkColorTransferFunctionCanvas::DoubleClickOnHandle(int handle)
{
  double xVal = GetFunctionX(handle);
  QColor col((int) (m_ColorTransferFunction->GetRedValue(xVal) * 255),
      (int) (m_ColorTransferFunction->GetGreenValue(xVal) * 255),
      (int) (m_ColorTransferFunction->GetBlueValue(xVal) * 255));
  QColor result = QColorDialog::getColor(col);
  if (result.isValid())
  {
    m_ColorTransferFunction->AddRGBPoint(xVal, result.red() / 255.0,
        result.green() / 255.0, result.blue() / 255.0);
    this->update();
    mitk::RenderingManager::GetInstance()->RequestUpdateAll();
  }
}
int QmitkColorTransferFunctionCanvas::GetNearHandle(int x, int,
    unsigned int maxSquaredDistance)
{
  for (int i = 0; i < this->GetFunctionSize(); i++)
  {
    std::pair<int,int> point = this->FunctionToCanvas(std::make_pair(
        GetFunctionX(i), (double) 0.0));
    if ((unsigned int) ((point.first - x) * (point.first - x))
        < maxSquaredDistance)
    {
      return i;
    }
  }
  return -1;
}
void QmitkColorTransferFunctionCanvas::paintEvent(QPaintEvent*)
{
  QPainter painter(this);

  // Render gray background
  QRect contentsRect = this->contentsRect();
  painter.setPen(Qt::gray);
  painter.drawRect(0, 0, contentsRect.width()+1, contentsRect.height()+1);

  if( ! this->isEnabled() )
    return;

  if (m_ColorTransferFunction)
  {
    for (int x = contentsRect.x(); x < contentsRect.x() + contentsRect.width(); x++)
    {
      double xVal = m_Min + ((float) x) / contentsRect.width() * (m_Max
          - m_Min);
      QColor col((int) (m_ColorTransferFunction->GetRedValue(xVal) * 255),
          (int) (m_ColorTransferFunction->GetGreenValue(xVal) * 255),
          (int) (m_ColorTransferFunction->GetBlueValue(xVal) * 255));
      painter.setPen(col);
      painter.drawLine(x, 1, x, contentsRect.height());
    }
  }

 //paint title
  if (m_Title.size()>0)
  {
    painter.setPen(Qt::black);
    painter.drawText(QPoint(11,21),m_Title);
    painter.setPen(Qt::white);
    painter.drawText(QPoint(10,20),m_Title);
  }

  //paint min and max
  QString qs_min = QString::number( m_Min );
  QString qs_max = QString::number( m_Max );

  QRect qr_min = painter.fontMetrics().boundingRect( qs_min );
  QRect qr_max = painter.fontMetrics().boundingRect( qs_max );

  int y,x;

  y=this->contentsRect().height()-qr_min.height()+5;
  x=10;

  painter.setPen(Qt::black);
  painter.drawText(QPoint(x+1,y+1),qs_min);
  painter.setPen(Qt::white);
  painter.drawText(QPoint(x  ,y  ),qs_min);

  y=this->contentsRect().height()-qr_max.height()+5;
  x=this->contentsRect().width()-qr_max.width()-6;

  painter.setPen(Qt::black);
  painter.drawText(QPoint(x,y+1),qs_max);
  painter.setPen(Qt::white);
  painter.drawText(QPoint(x,y  ),qs_max);

  if (m_ColorTransferFunction)
  {
    // now paint the handles
    painter.setBrush(Qt::black);
    painter.setPen(Qt::black);
    for (int i = 0; i < this->GetFunctionSize(); i++)
    {
      int handleHeight = (i == m_GrabbedHandle) ? (int) (contentsRect.height() / 1.5)
          : contentsRect.height() / 2;
      int handleWidth = (i == m_GrabbedHandle) ? 6 : 4;
      std::pair<int,int> point = this->FunctionToCanvas(std::make_pair(
          GetFunctionX(i), 0.0f));
      int y = height() / 2;
      painter.drawRoundRect(point.first - handleWidth / 2,
          y - handleHeight / 2, handleWidth, handleHeight, 50, 50);

      if (i == m_GrabbedHandle && m_LineEditAvailable)
        m_XEdit->setText(QString::number(GetFunctionX(m_GrabbedHandle)));
    }
  }
}
void QmitkPiecewiseFunctionCanvas::paintEvent(QPaintEvent*)
{
  QPainter painter(this);

  PaintHistogram(painter);

  if (m_Title.size()>0)
  {
    painter.setPen(Qt::black);
    painter.drawText(QPoint(11,21),m_Title);
    painter.setPen(Qt::white);
    painter.drawText(QPoint(10,20),m_Title);
  }

  {
    QString qs_min = QString::number( m_Min, 'g', 4 );
    QString qs_max = QString::number( m_Max, 'g', 4 );

    QRect qr_min = painter.fontMetrics().boundingRect( qs_min );
    QRect qr_max = painter.fontMetrics().boundingRect( qs_max );

    int y,x;

    y=this->contentsRect().height()-qr_min.height()+5;
    x=10;

    // Fill the tf presets in the generator widget

    painter.setPen(Qt::black);
    painter.drawText(QPoint(x+1,y+1),qs_min);
    painter.setPen(Qt::white);
    painter.drawText(QPoint(x  ,y  ),qs_min);

    y=this->contentsRect().height()-qr_max.height()+5;
    x=this->contentsRect().width()-qr_max.width()-6;

    painter.setPen(Qt::black);
    painter.drawText(QPoint(x,y+1),qs_max);
    painter.setPen(Qt::white);
    painter.drawText(QPoint(x,y  ),qs_max);
  }

  painter.setPen(Qt::gray);

  QRect contentsRect = this->contentsRect();
  painter.drawRect(0, 0, contentsRect.width()+1, contentsRect.height()+1);
  if (m_PiecewiseFunction && this->isEnabled())
  {
    double* dp = m_PiecewiseFunction->GetDataPointer();

    // Render lines

    painter.setPen(Qt::black);

    for (int i = -1; i < m_PiecewiseFunction->GetSize(); i++)
    {
      std::pair<int,int> left;
      std::pair<int,int> right;

      if(i < 0)
        left = this->FunctionToCanvas(std::make_pair(-32768, dp[0 * 2 + 1]));
      else
        left = this->FunctionToCanvas(std::make_pair(dp[i * 2], dp[i * 2 + 1]));

      if(i+1 >= m_PiecewiseFunction->GetSize())
        right = this->FunctionToCanvas(std::make_pair(32768, dp[(i  ) * 2 + 1]));
      else
        right = this->FunctionToCanvas(std::make_pair(dp[(i+1) * 2], dp[(i+1) * 2 + 1]));

      painter.drawLine(left.first, left.second, right.first, right.second);
    }

    // Render Points

    for (int i = 0; i < m_PiecewiseFunction->GetSize(); i++)
    {
      std::pair<int,int> point = this->FunctionToCanvas(std::make_pair(
          dp[i * 2], dp[i * 2 + 1]));

      if (i == m_GrabbedHandle)
      {
        painter.setBrush(QBrush(Qt::red));
        if (m_LineEditAvailable)
        {
          int xCursor = m_XEdit->cursorPosition();
          int yCursor = m_YEdit->cursorPosition();
          m_XEdit->setText(QString::number(GetFunctionX(m_GrabbedHandle), 'g', 4));
          m_YEdit->setText(QString::number(GetFunctionY(m_GrabbedHandle), 'g', 4));
          m_XEdit->setCursorPosition( xCursor );
          m_YEdit->setCursorPosition( yCursor );
        }
      }
      else
      {
        painter.setBrush(QBrush(Qt::green));
      }
      painter.drawEllipse(point.first - 4, point.second - 4, 8, 8);
    }

    painter.setBrush(Qt::NoBrush);
  }
}
void QmitkPiecewiseFunctionCanvas::MoveFunctionPoint(int index,
    std::pair<double,double> pos)
{
  RemoveFunctionPoint(GetFunctionX(index));
  m_GrabbedHandle = AddFunctionPoint(pos.first, pos.second);
}