/***************************************************************************
 * optionstimidity.cpp: implementation of OptionsTimidity class
 *
 * This file is part of KTabEdit, a KDE tabulature editor
 *
 * copyright (C) 2003-2006 the KTabEdit development team <mrgwen@gmail.com>
 * copyright (C) 2002-2003 the KGuitar development team <mrgwen@gmail.com>
 ***************************************************************************/

/***************************************************************************
 * 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.
 *
 * See the file COPYING for more information.
 ***************************************************************************/

#include "optionstimidity.h"

#include <KLocale>
#include <KFileDialog>

#include "settings.h"

OptionsTimidity::OptionsTimidity(KConfig* conf, QWidget* parent)
: OptionsPage(conf, parent),
  mTimidityPathLabel(i18n("Timidity path"), this),
  mTimidityPath(this),
  mTimidityButton("...", this),
  mBufferLabel(i18n("Buffer"), this),
  mBuffer(0, 10, 1, this),
  mFragmentLabel(i18n("Fragment"), this),
  mFragment(0, 10, 1,this),
  mOutputLabel(i18n("Output"), this),
  mOutput(this),
  mChorusLabel(i18n("Chorus"), this),
  mChorus(this),
  mDelayLabel(i18n("Delay"), this),
  mDelay(this),
  mReverbLabel(i18n("Reverb"), this),
  mReverb(this),
  mVlpfLabel(i18n("Voice LPF effect"), this),
  mVlpf(this),
  mNoiseLabel(i18n("Noise shaping"), this),
  mNoise(this),
  mResampLabel(i18n("Resample"), this),
  mResamp(this),
  mFrequencyLabel(i18n("Resampling mFrequency"), this),
  mFrequency(0, 44100, 1000, this),
  mRealtimeEnableLabel(i18n("Realtime priority enable"), this),
  mRealtimeEnable(this),
  mRealtimeLabel(i18n("Realtime priority"), this),
  mRealtime(0, 100, 10, this),
  mTimidityLayout(this)
{
	mTimidityPathLayout.addWidget(&mTimidityPathLabel);
	mTimidityPathLayout.addWidget(&mTimidityPath);
	mTimidityPathLayout.addWidget(&mTimidityButton);
	
	mBufferLayout.addWidget(&mBufferLabel);
	mBufferLayout.addWidget(&mBuffer);
	
	mFragmentLayout.addWidget(&mFragmentLabel);
	mFragmentLayout.addWidget(&mFragment);
	
	mOutputLayout.addWidget(&mOutputLabel);
	mOutputLayout.addWidget(&mOutput);
	
	mOutput.insertItem("Default");
	mOutput.insertItem("ALSA");
	mOutput.insertItem("EsounD");
	mOutput.insertItem("JACK");
	mOutput.insertItem("aRts");
	
	mChorusLayout.addWidget(&mChorusLabel);
	mChorusLayout.addWidget(&mChorus);
	
	mChorus.insertItem("Disable this effect");
	mChorus.insertItem("Enable MIDI chorus effect control.");
	mChorus.insertItem("Surround sound, chorus detuned to a lesser degree (default).");
	
	mDelayLayout.addWidget(&mDelayLabel);
	mDelayLayout.addWidget(&mDelay);
	
	mDelay.insertItem("Disable delay effect.");
	mDelay.insertItem("Left delay.");
	mDelay.insertItem("Right delay.");
	mDelay.insertItem("Swap left & right.");
	
	mReverbLayout.addWidget(&mReverbLabel);
	mReverbLayout.addWidget(&mReverb);
	
	mReverb.insertItem("Disable MIDI reverb effect control.");
	mReverb.insertItem("Enable  MIDI reverb  effect control. This effect is only available in stereo.");
	mReverb.insertItem("Global reverb effect.");
	mReverb.insertItem("Freeverb MIDI reverb effect control (default).");
	mReverb.insertItem("Global freeverb effect.");
	
	mVlpfLayout.addWidget(&mVlpfLabel);
	mVlpfLayout.addWidget(&mVlpf);
	
	mVlpf.insertItem("Disable LPF effect.");
	mVlpf.insertItem("Chamberlin resonant LPF (12dB/oct) (default).");
	mVlpf.insertItem("Moog resonant low-pass VCF (24dB/oct).");
	
	mNoiseLayout.addWidget(&mNoiseLabel);
	mNoiseLayout.addWidget(&mNoise);
	
	mNoise.insertItem("No noise shaping.");
	mNoise.insertItem("Traditional noise shaping.");
	mNoise.insertItem("Overdrive-like soft-clipping + new noise shaping.");
	mNoise.insertItem("Tube-amplifier-like soft-clipping + new noise shaping.");
	mNoise.insertItem("New noise shaping.");
	
	mResampLayout.addWidget(&mResampLabel);
	mResampLayout.addWidget(&mResamp);
	
	mResamp.insertItem("No interpolation.");
	mResamp.insertItem("Linear interpolation.");
	mResamp.insertItem("Cubic spline interpolation.");
	mResamp.insertItem("Lagrange method.");
	mResamp.insertItem("Newton polynomial interpolation.");
	mResamp.insertItem("Modified Gauss effect (default).");
	
	mFrequencyLayout.addWidget(&mFrequencyLabel);
	mFrequencyLayout.addWidget(&mFrequency);
	
	mRealtimeEnableLayout.addWidget(&mRealtimeEnableLabel);
	mRealtimeEnableLayout.addWidget(&mRealtimeEnable);
	
	mRealtimeLayout.addWidget(&mRealtimeLabel);
	mRealtimeLayout.addWidget(&mRealtime);

	mTimidityLayout.addLayout(&mTimidityPathLayout);
	mTimidityLayout.addLayout(&mBufferLayout);
	mTimidityLayout.addLayout(&mFragmentLayout);
	mTimidityLayout.addLayout(&mOutputLayout);
	mTimidityLayout.addLayout(&mChorusLayout);
	mTimidityLayout.addLayout(&mDelayLayout);
	mTimidityLayout.addLayout(&mReverbLayout);
	mTimidityLayout.addLayout(&mVlpfLayout);
	mTimidityLayout.addLayout(&mNoiseLayout);
	mTimidityLayout.addLayout(&mResampLayout);
	mTimidityLayout.addLayout(&mFrequencyLayout);
	mTimidityLayout.addLayout(&mRealtimeEnableLayout);
	mTimidityLayout.addLayout(&mRealtimeLayout);
	
	fillOptions();
	
	connect(&mTimidityButton, SIGNAL(clicked()), SLOT(search()));
}

void OptionsTimidity::defaultBtnClicked()
{
}

void OptionsTimidity::applyBtnClicked()
{
/*	config->setGroup("Timidity");
	
	config->writeEntry("Path", mTimidityPath.text());
	
	config->writeEntry("Buffer", mBuffer.value());
	
	config->writeEntry("Fragment", mFragment.value());
	
	switch(mOutput.currentItem()) {
		case 0:
			config->writeEntry("Output", "d");
			break;
		case 1:
			config->writeEntry("Output", "s");
			break;
		case 2:
			config->writeEntry("Output", "e");
			break;
		case 3:
			config->writeEntry("Output", "j");
			break;
		case 4:
			config->writeEntry("Output", "r");
			break;
	}
	
	switch(mChorus.currentItem()) {
		case 0:
			config->writeEntry("Chorus", "d");
			break;
		case 1:
			config->writeEntry("Chorus", "n");
			break;
		case 2:
			config->writeEntry("Chorus", "s");
			break;
	}
	
	switch(mDelay.currentItem()) {
		case 0:
			config->writeEntry("Delay", "d");
			break;
		case 1:
			config->writeEntry("Delay", "l");
			break;
		case 2:
			config->writeEntry("Delay", "r");
			break;
		case 3:
			config->writeEntry("Delay", "b");
			break;
	}
	
	switch(mReverb.currentItem()) {
		case 0:
			config->writeEntry("Reverb", "d");
			break;
		case 1:
			config->writeEntry("Reverb", "n");
			break;
		case 2:
			config->writeEntry("Reverb", "g");
			break;
		case 3:
			config->writeEntry("Reverb", "f");
			break;
		case 4:
			config->writeEntry("Reverb", "G");
			break;
	}
	
	switch(mVlpf.currentItem()) {
		case 0:
			config->writeEntry("Vlpf", "d");
			break;
		case 1:
			config->writeEntry("Vlpf", "c");
			break;
		case 2:
			config->writeEntry("Vlpf", "m");
			break;
	}
	
	
	config->writeEntry("Noise", mNoise.currentItem());
	
	switch(mResamp.currentItem()) {
		case 0:
			config->writeEntry("Resample", "d");
			break;
		case 1:
			config->writeEntry("Resample", "l");
			break;
		case 2:
			config->writeEntry("Resample", "c");
			break;
		case 3:
			config->writeEntry("Resample", "L");
			break;
		case 4:
			config->writeEntry("Resample", "n");
			break;
		case 5:
			config->writeEntry("Resample", "G");
			break;
	}
	
	config->writeEntry("Frequency", mFrequency.value());
	
	config->writeEntry("RealTime", mRealtime.value());
	
	config->writeEntry("RealTimeEnable", mRealtimeEnable.isChecked());*/
}

void OptionsTimidity::search()
{
	QString temp = KFileDialog::getOpenFileName(KUrl(mTimidityPath.text()), "timidity", this, "Choose timidity" );
	
	if (!temp.isEmpty()) {
		mTimidityPath.setText(temp);
	}
}

void OptionsTimidity::fillOptions()
{
	mTimidityPath.setText(Settings::getInstance()->timidityPath());
	mBuffer.setValue(Settings::getInstance()->timidityBuffer());
	mFragment.setValue(Settings::getInstance()->timidityFragment());
	
	switch(Settings::getInstance()->timidityOutput().ascii()[0]) {
		case 'd':
			mOutput.setCurrentItem(0);
			break;
		case 's':
			mOutput.setCurrentItem(1);
			break;
		case 'e':
			mOutput.setCurrentItem(2);
			break;
		case 'j':
			mOutput.setCurrentItem(3);
			break;
		case 'r':
			mOutput.setCurrentItem(4);
			break;
		default:
			mOutput.setCurrentItem(1);
	}
	
	switch(Settings::getInstance()->timidityChorus().ascii()[0]) {
		case 'd':
			mChorus.setCurrentItem(0);
			break;
		case 'n':
			mChorus.setCurrentItem(1);
			break;
		case 's':
			mChorus.setCurrentItem(2);
			break;
		default:
			mChorus.setCurrentItem(0);
	}
	
	switch(Settings::getInstance()->timidityDelay().ascii()[0]) {
		case 'd':
			mDelay.setCurrentItem(0);
			break;
		case 'l':
			mDelay.setCurrentItem(1);
			break;
		case 'r':
			mDelay.setCurrentItem(2);
			break;
		case 'b':
			mDelay.setCurrentItem(3);
			break;
		default:
			mDelay.setCurrentItem(0);
	}
	
	switch(Settings::getInstance()->timidityReverb().ascii()[0]) {
		case 'd':
			mReverb.setCurrentItem(0);
			break;
		case 'n':
			mReverb.setCurrentItem(1);
			break;
		case 'g':
			mReverb.setCurrentItem(2);
			break;
		case 'f':
			mReverb.setCurrentItem(3);
			break;
		case 'G':
			mReverb.setCurrentItem(4);
			break;
		default:
			mReverb.setCurrentItem(0);
	}
	
	switch(Settings::getInstance()->timidityVlpf().ascii()[0]) {
		case 'd':
			mVlpf.setCurrentItem(0);
			break;
		case 'c':
			mVlpf.setCurrentItem(1);
			break;
		case 'm':
			mVlpf.setCurrentItem(2);
			break;
		default:
			mVlpf.setCurrentItem(0);
	}
	
	
	mNoise.setCurrentItem(Settings::getInstance()->timidityNoise());
	
	switch(Settings::getInstance()->timidityResamp().ascii()[0]) {
		case 'd':
			mResamp.setCurrentItem(0);
			break;
		case 'l':
			mResamp.setCurrentItem(1);
			break;
		case 'c':
			mResamp.setCurrentItem(2);
			break;
		case 'L':
			mResamp.setCurrentItem(3);
			break;
		case 'n':
			mResamp.setCurrentItem(4);
			break;
		case 'G':
			mResamp.setCurrentItem(5);
			break;
		default:
			mResamp.setCurrentItem(1);
	}
	
	mFrequency.setValue(Settings::getInstance()->timidityFrequency());
	
	mRealtime.setValue(Settings::getInstance()->timidityRealTime());
	
	mRealtimeEnable.setChecked(Settings::getInstance()->timidityRealTimeEnable());
}

#include "optionstimidity.moc"
