void DialogTransformVolume::RespondTextScale( int n )
{
  if ( isVisible() )
  {
    bool bOK;
    double dvalue = m_textScale[n]->text().toDouble(&bOK);
    if ( bOK && dvalue > 0 )
    {
      LayerMRI* layer = ( LayerMRI* )MainWindow::GetMainWindow()->GetActiveLayer( "MRI" );
      if ( layer )
      {
        double scale[3];
        layer->GetScale( scale );
        scale[n] = dvalue;
        layer->SetScale( scale );
        MainWindow::GetMainWindow()->RequestRedraw();

        m_scrollScale[n]->blockSignals(true);
        if ( dvalue >= 1 )
        {
          m_scrollScale[n]->setValue( 50 + (int)( (dvalue-1.0)*50 ) );
        }
        else
        {
          m_scrollScale[n]->setValue( 50 - (int)( (1.0-dvalue)*100 ) );
        }
        m_scrollScale[n]->blockSignals(false);
        UpdateUI( 0 );
      }
    }
  }
}
void DialogTransformVolume::RespondScrollScale( int n )
{
  LayerMRI* layer = ( LayerMRI* )MainWindow::GetMainWindow()->GetActiveLayer( "MRI" );
  if ( layer )
  {
    double scale[3];
    layer->GetScale( scale );
    int npos = m_scrollScale[n]->value();
    if ( npos >= 50 )
    {
      scale[n] = ( npos - 50 ) / 50.0 + 1.0;
    }
    else
    {
      scale[n] = ( npos - 50 ) / 100.0 + 1.0;
    }
    layer->SetScale( scale );
    MainWindow::GetMainWindow()->RequestRedraw();

    ChangeLineEditNumber( m_textScale[n], scale[n] );
    UpdateUI( 0 );
  }
}
// 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 );
    }
  }
}