void DialogTransformVolume::RespondTextRotate( int n )
{
  if ( isVisible() )
  {
    bool bOK;
    double dvalue = m_textAngle[n]->text().toDouble(&bOK);
    if ( bOK )
    {
      LayerMRI* layer = ( LayerMRI* )MainWindow::GetMainWindow()->GetActiveLayer( "MRI" );
      if ( layer )
      {
        double angle[3];
        layer->GetRotate( angle );
        angle[n] = dvalue;
        layer->SetRotate( angle, ui->radioButtonAroundCenter->isChecked() );
        MainWindow::GetMainWindow()->RequestRedraw();

        while (dvalue > 180)
          dvalue -= 360;
        while (dvalue < -180)
          dvalue += 360;

        m_sliderRotate[n]->blockSignals(true);
        m_sliderRotate[n]->setValue( (int)dvalue );
        m_sliderRotate[n]->blockSignals(false);
        UpdateUI( 1 );
      }
    }
  }
}
void DialogTransformVolume::RespondSliderRotate( int n )
{
  if ( isVisible() )
  {
    LayerMRI* layer = ( LayerMRI* )MainWindow::GetMainWindow()->GetActiveLayer( "MRI" );
    if ( layer )
    {
      double angle[3];
      layer->GetRotate( angle );
      angle[n] = m_sliderRotate[n]->value();
      layer->SetRotate( angle, ui->radioButtonAroundCenter->isChecked() );
      MainWindow::GetMainWindow()->RequestRedraw();
      ChangeLineEditNumber(m_textAngle[n], angle[n] );
      UpdateUI( 1 );
    }
  }
}
// scope: 0 => translate related, 1 => scale related, 2 => both
void DialogTransformVolume::UpdateUI( int scope )
{
  LayerMRI* layer = (LayerMRI* )MainWindow::GetMainWindow()->GetActiveLayer( "MRI" );
  if ( layer )
  {
    QList<QWidget*> allwidgets = this->findChildren<QWidget*>();
    for ( int i = 0; i < allwidgets.size(); i++ )
    {
      allwidgets[i]->blockSignals( true );
    }
    if ( scope == 0 || scope == 2 )
    {
      double* vs = layer->GetWorldVoxelSize();
      double* ws = layer->GetWorldSize();
      double pos[3];
      layer->GetTranslate( pos );
      for ( int i = 0; i < 3; i++ )
      {
        int range = (int)( ws[i] / vs[i] + 0.5 ) * 2;
        int npos = (int)(pos[i] / vs[i]) + range/2;
        m_scrollTranslate[i]->setRange(0, range);
        m_scrollTranslate[i]->setValue(npos);
        ChangeLineEditNumber(m_textTranslate[i], pos[i]);
      }
    }
    if ( scope == 1 || scope == 2 )
    {
      double scale[3];
      layer->GetScale( scale );
      for ( int i = 0; i < 3; i++ )
      {
        if ( scale[i] >= 1 )
        {
          m_scrollScale[i]->setValue( 50 + (int)( (scale[i]-1.0)*50 ) );
        }
        else
        {
          m_scrollScale[i]->setValue( 50 - (int)( (1.0-scale[i])*100 ) );
        }

        ChangeLineEditNumber(m_textScale[i], scale[i]);
      }
    }

    ui->pushButtonRestore->setEnabled( layer->IsTransformed() );
    ui->pushButtonSaveReg->setEnabled( layer->IsTransformed() );
    ui->pushButtonSaveVolumeAs->setEnabled( layer->IsTransformed() );
    double angle[3];
    layer->GetRotate(angle);
    ui->groupBoxAxis->setDisabled(angle[0] != 0 || angle[1] != 0 || angle[2] != 0);
    for (int i = 0; i < 3; i++)
    {
      double val = angle[i];
      while (val > 180)
        val -= 360;
      while (angle[i] < -180)
        val += 360;
      m_sliderRotate[i]->setValue((int)val);
      ChangeLineEditNumber(m_textAngle[i], angle[i]);
    }

    for ( int i = 0; i < allwidgets.size(); i++ )
    {
      allwidgets[i]->blockSignals( false );
    }
  }
}