/***************************************************************************
                          kernsettingsdialog.cpp  -  description
                             -------------------
    begin                : Mon Aug 18 2003
    copyright            : (C) 2003 by Armando Navarro-Vazquez
    email                : qoajnv@usc.es
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#include <qlayout.h>
#include <qcheckbox.h>
#include <qlabel.h>
#include <q3groupbox.h>
#include <qradiobutton.h>
#include <q3buttongroup.h>
//Added by qt3to4:
#include <QPixmap>
#include <Q3GridLayout>
#include <Q3VBoxLayout>
#include <Q3Frame>

#include <klocale.h>
#include <kconfig.h>
#include <knuminput.h>

#include "kernsettingsdialog.h"
#include "kern.h"
#include "icons_nmr.h"
#include "defaults.h"



KryoNMRSettingsDialog::KryoNMRSettingsDialog() : KDialogBase(IconList, i18n("Settings"),
                  Help|Default|Ok|Apply|Cancel, Ok )
{
  m_config = KryoNMRFactory::instance()->config();

  setupProcessPage();
  setupViewPage();
  setupPrintPage();
}
KryoNMRSettingsDialog::~KryoNMRSettingsDialog(){
}
/** No descriptions */
void KryoNMRSettingsDialog::setupProcessPage(){

      m_config->setGroup("Process");
      QFrame *page = addPage( i18n("Process"), i18n("KryoNMR Process Preferences" ),
			    BarIcon("gear", KIconLoader::SizeMedium ) );

      Q3VBoxLayout *top = new Q3VBoxLayout( page, 0, spacingHint() );

      m_autoFTCheckBox= new QCheckBox(i18n("AutoFT"),page,"autoFTcheckbox");
      m_autoFTCheckBox->setChecked( m_config->readBoolEntry("AutoFT", defaults::autoft) );

      m_autophaseCheckBox = new QCheckBox(i18n("Auto Phase"),page,"autophasecheckbox");
      m_autophaseCheckBox->setChecked(m_config->readBoolEntry("AutoPhase",defaults::autophase));
      
      m_autobaselineCheckBox = new QCheckBox(i18n("Auto Baseline"),page,"autobaselinecheckbox");
      m_autobaselineCheckBox->setChecked(m_config->readBoolEntry("AutoBaseline",defaults::autophase));

      m_apodGroupBox= new Q3GroupBox("Apodization",page);
      Q3GridLayout* apodlayout= new Q3GridLayout( m_apodGroupBox,5,2,spacingHint()*3, spacingHint() );

      m_autoapodizeCheckBox = new QCheckBox(i18n("Auto Apodize"),m_apodGroupBox,"autoapodizecheckbox");
      m_autoapodizeCheckBox->setChecked(m_config->readBoolEntry("AutoApodize",defaults::autoapodize));

      m_apodfunction= new QComboBox(m_apodGroupBox,"apod_function");
      QStringList elist;
      elist << "Exponential";
      m_apodfunction->insertStringList(elist);
      QLabel* functionlabel= new QLabel("Window function",m_apodGroupBox);

      m_apodnucleus= new QComboBox(m_apodGroupBox,"apod_nucleus");
      QStringList nlist;
      nlist << "1H" << "13C";
      m_apodnucleus->insertStringList(nlist);
      QLabel* nucleuslabel= new QLabel("Nucleus",m_apodGroupBox);

      m_exponent= new KDoubleNumInput (0.0f,200.0f,0.5f,0.02f,2,m_apodGroupBox,"exponent_input");
      QLabel* exponentlabel= new QLabel("Parameter",m_apodGroupBox);
      top->addWidget( m_autoFTCheckBox );
      top->addWidget( m_autophaseCheckBox );
      top->addWidget( m_autobaselineCheckBox);


      apodlayout->addWidget(m_autoapodizeCheckBox,0,0);
      apodlayout->addWidget(m_apodfunction,1,0);
      apodlayout->addWidget(functionlabel,1,1);
      apodlayout->addWidget(m_apodnucleus,2,0);
      apodlayout->addWidget(nucleuslabel,2,1);
      apodlayout->addWidget(m_exponent,3,0);
      apodlayout->addWidget(exponentlabel,3,1);

      top->addWidget( m_apodGroupBox);

      top->addStretch(10);

}

void KryoNMRSettingsDialog::setupViewPage()
{
  m_config->setGroup("View");
  QFrame *page = addPage( i18n("View"), i18n("KryoNMR View Preferences" ),
			    QPixmap(icons::spectrum) );
  //QVBoxLayout *top = new QVBoxLayout( page, 0, spacingHint() );
  Q3GridLayout *grid =new Q3GridLayout(page,4,2, spacingHint() );

  m_spectrumcolor= new KColorCombo(page,"spectrumcolor");
  m_spectrumcolor->setColor( m_config->readColorEntry("Spectrum_Color",&defaults::spectrumcolor) );
  QLabel* spectrumLabel= new QLabel("Spectrum Color",page);
  grid->addWidget (m_spectrumcolor,0,0);
  grid->addWidget (spectrumLabel,0,1);

  m_bkcolor= new KColorCombo(page,"bkcolor");
  m_bkcolor->setColor( m_config->readColorEntry("Background_Color",&defaults::bkcolor) );
  QLabel* bkLabel= new QLabel("Background Color",page);
  grid->addWidget (m_bkcolor,1,0);
  grid->addWidget (bkLabel,1,1);

  m_scalecolor= new KColorCombo(page,"scalecolor");
  m_scalecolor->setColor( m_config->readColorEntry("Scale_Color",&defaults::scalecolor) );
  QLabel* scaleLabel= new QLabel("Scale Color",page);
  grid->addWidget (m_scalecolor,2,0);
  grid->addWidget (scaleLabel,2,1);

  m_integralcolor= new KColorCombo(page,"integralcolor");
  m_integralcolor->setColor( m_config->readColorEntry("Integral_Color",&defaults::integralcolor) );
  QLabel* integralLabel= new QLabel("Integral Color",page);
  grid->addWidget (m_integralcolor,3,0);
  grid->addWidget (integralLabel,3,1);

  //grid->addStretch(10);
}
void KryoNMRSettingsDialog::setupPrintPage()
{
      m_config->setGroup("Printing");
      QFrame *page = addPage( i18n("Printing"), i18n("KryoNMR Printing Preferences" ),
			    BarIcon("printer1", KIconLoader::SizeMedium ) );

	Q3VBoxLayout *top = new Q3VBoxLayout( page, 0, spacingHint() );

	m_printbuttongroup= new Q3ButtonGroup(page);
	m_printbuttongroup->setExclusive(true);
	//m_printeroptions= new QRadioButton[3](m_printbuttongroup);
	for(int i=0;i<3;i++)
	{
	   QRadioButton* b= new QRadioButton(m_printbuttongroup);
	   m_printeroptions.push_back(b);
	 }
	Q3GridLayout* printlayout= new Q3GridLayout(m_printbuttongroup,3,1, spacingHint()*3, spacingHint() );

	/*for(int i=0;i<3;i++)
	{
		m_printbuttongroup->insert(&m_printeroptions[i]);

	}*/

	m_printeroptions[0]->setText("Black && White printing)");
	m_printeroptions[1]->setText("Color Printing, white background");
	m_printeroptions[2]->setText("Color Printing, color background");

	m_printbuttongroup->setButton( m_config->readNumEntry("Printing_Mode",defaults::printingmode) );

	printlayout->addWidget(m_printeroptions[0],0,0);
	printlayout->addWidget(m_printeroptions[1],1,0);
	printlayout->addWidget(m_printeroptions[2],2,0);

	top->addWidget(m_printbuttongroup);
	top->addStretch(10);
  //QVBoxLayout *top = new QVBoxLayout( page, 0, spacingHint() );

  //QGridLayout *grid =new QGridLayout(page,4,2, spacingHint() );

/*  m_spectrumcolor= new KColorCombo(page,"spectrumcolor");
  m_spectrumcolor->setColor( m_config->readColorEntry("Spectrum_Color",&defaults::spectrumcolor) );
  QLabel* spectrumLabel= new QLabel("Spectrum Color",page);
  grid->addWidget (m_spectrumcolor,0,0);
  grid->addWidget (spectrumLabel,0,1);

  m_bkcolor= new KColorCombo(page,"bkcolor");
  m_bkcolor->setColor( m_config->readColorEntry("Background_Color",&defaults::bkcolor) );
  QLabel* bkLabel= new QLabel("Background Color",page);
  grid->addWidget (m_bkcolor,1,0);
  grid->addWidget (bkLabel,1,1);

  m_scalecolor= new KColorCombo(page,"scalecolor");
  m_scalecolor->setColor( m_config->readColorEntry("Scale_Color",&defaults::scalecolor) );
  QLabel* scaleLabel= new QLabel("Scale Color",page);
  grid->addWidget (m_scalecolor,2,0);
  grid->addWidget (scaleLabel,2,1);

  m_integralcolor= new KColorCombo(page,"integralcolor");
  m_integralcolor->setColor( m_config->readColorEntry("Integral_Color",&defaults::integralcolor) );
  QLabel* integralLabel= new QLabel("Integral Color",page);
  grid->addWidget (m_integralcolor,3,0);
  grid->addWidget (integralLabel,3,1);*/

  //grid->addStretch(10);
}
void  KryoNMRSettingsDialog::slotApply()
{
  m_config->setGroup("Process");
  m_config->writeEntry("AutoFT",m_autoFTCheckBox->isChecked());
  m_config->writeEntry("AutoPhase",m_autophaseCheckBox->isChecked());
  m_config->writeEntry("AutoApodize",m_autoapodizeCheckBox->isChecked());
  m_config->writeEntry("AutoBaseline",m_autobaselineCheckBox->isChecked());
  
  m_config->setGroup("View");
  m_config->writeEntry("Spectrum_Color",m_spectrumcolor->color());
  m_config->writeEntry("Background_Color",m_bkcolor->color());
  m_config->writeEntry("Scale_Color",m_scalecolor->color());
  m_config->writeEntry("Integral_Color",m_integralcolor->color());

  m_config->setGroup("Printing");
  m_config->writeEntry("Printing_Mode",m_printbuttongroup->id(m_printbuttongroup->selected()));

  m_config->sync();


  emit settingsmodified();
  
}

void KryoNMRSettingsDialog::slotOk()
{
  slotApply();
  accept();
}

void KryoNMRSettingsDialog::slotDefault()
{
   m_autoFTCheckBox->setChecked(defaults::autoft);
   m_autophaseCheckBox->setChecked(defaults::autophase);
   m_autoapodizeCheckBox->setChecked(defaults::autoapodize);
   m_autobaselineCheckBox->setChecked(defaults::autobaseline);
   
   m_spectrumcolor->setColor(defaults::spectrumcolor);
   m_bkcolor->setColor(defaults::bkcolor);
   m_scalecolor->setColor(defaults::scalecolor);
   m_integralcolor->setColor(defaults::integralcolor);

   m_printbuttongroup->setButton(defaults::printingmode);
}
