/***************************************************************************
 * settrack.cpp: implementation of SetTrack 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 "settrack.h"

#include <KMessageBox>

#include "notespinbox.h"
#include "radiustuner.h"
#include "sequencer/instruments.h"
#include "settabmidi.h"
#include "settabfret.h"
#include "settabdrum.h"

#include "tablature/trackmidiproperties.h"
#include "tablature/trackproperties.h"
#include "tablatureModel/tabsongmodel.h"


SetTrack::SetTrack(const TrackProperties* trackProperties, const TabSongModel* tabSong)
: QDialog(),
  Ui::TrackPropertiesDialog(),
  mTrackProperties(trackProperties),
  mTabSong(tabSong),
  mSetTabFret(0x0),
  mSetTabDrum(0x0),
  mFretVisible(false)
{
	setupUi(this);
	
	InstrumentsList instruments;
	
	mMode->insertItem(tr("Fretted instrument"));
	mMode->insertItem(tr("Drum track"));
	
	mPatch->insertStringList(instruments.getInstrumentList());
	
	mSetTabFret = new SetTabFret(mModeTab);
	mSetTabDrum = new SetTabDrum(mModeTab);
	
	fill();
	selectTrackMode(mTrackProperties->mode());
	
	connect(mMode, SIGNAL(highlighted(int)), SLOT(selectTrackMode(int)));
	
	connect(mOk, SIGNAL(clicked()), this, SLOT(accept()));
	connect(mCancel, SIGNAL(clicked()), this, SLOT(reject()));
}

void SetTrack::fillFret()
{
	mSetTabFret->setString(mTrackProperties->nbStrings());
	mSetTabFret->setFrets(mTrackProperties->nbFrets());
	
	for (int i = 0; i < mTrackProperties->nbStrings(); i++)
		mSetTabFret->setTune(i, mTrackProperties->tune(i));
}

void SetTrack::fillDrum()
{
	mSetTabDrum->setDrums(mTrackProperties->nbStrings());
	
	for (int i = 0; i < mTrackProperties->nbStrings(); i++)
		mSetTabDrum->setTune(i, mTrackProperties->tune(i));
}

void SetTrack::fillTrackMode(TrackMode mode)
{
	switch (mode) {
	case BanjoTab:
	case TwelveStringsTab:
	case FretTab: fillFret(); break;
	case DrumTab: fillDrum(); break;
	}
}

void SetTrack::fill()
{
	mTrackName->setText(mTrackProperties->name());
	mChannel->setValue(mTrackProperties->channel());
	mBank->setValue(mTrackProperties->bank());
	mPatch->setCurrentIndex(mTrackProperties->patch());
	mMode->setCurrentIndex(mTrackProperties->mode());
	mVolume->setValue(mTrackProperties->volume());
	mPan->setValue(mTrackProperties->balance());
	mChorus->setValue(mTrackProperties->chorus());
	mReverb->setValue(mTrackProperties->reverb());
	
	fillTrackMode(mTrackProperties->mode());
}

void SetTrack::showFret()
{
	mSetTabFret->show();
	mSetTabDrum->hide();
	
	mFretVisible = true;
}

void SetTrack::showDrum()
{
	if (mTabSong->isDrumTrack() == true) {
		KMessageBox::error(0, "There is a drum track, you can only have one drum track");
	} else {
		mSetTabFret->hide();
		mSetTabDrum->show();
		
		mFretVisible = false;
	}
}

void SetTrack::selectTrackMode(int sel)
{
	switch (static_cast<TrackMode>(sel)) {
	case BanjoTab:
	case TwelveStringsTab:
	case FretTab: showFret(); break;
	case DrumTab: showDrum(); break;
	}
}

QString SetTrack::title() const
{
	return mTrackName->text();
}

uint SetTrack::channel() const
{
	return mChannel->value();
}

uint SetTrack::bank() const
{
	return mBank->value();
}

uint SetTrack::patch() const
{
	return mPatch->currentItem();
}

TrackMode SetTrack::mode() const
{
	return static_cast<TrackMode>(mMode->currentItem());
}

uint SetTrack::volume() const
{
	return mVolume->value();
}

uint SetTrack::balance() const
{
	return mPan->value() + 8;
}

uint SetTrack::chorus() const
{
	return mChorus->value();
}

uint SetTrack::reverb() const
{
	return mReverb->value();
}

uint SetTrack::strings() const
{
	uint result;
	
	if (mFretVisible == true)
		result = mSetTabFret->strings();
	else
		result = mSetTabDrum->strings();
	
	return result;
}

uint SetTrack::frets() const
{
	uint result;
	
	if (mFretVisible == true)
		result = mSetTabFret->frets();
	else
		result = mSetTabDrum->frets();
	
	return result;
}

uint SetTrack::tune(uchar i) const
{
	uint result;
	
	if (mFretVisible == true)
		result = mSetTabFret->tune(i);
	else
		result = mSetTabDrum->tune(i);
	
	return result;
}

#include "settrack.moc"
