/***************************************************************************
 * chordlistitem.cpp: implementation of ChordListItem 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 "chordlistitem.h"
#include "settings.h"

const int ChordListItem::msToneShift[6] = { 3, 7, 10, 2, 5, 9 };


/**
@brief Constructs the chord list
The constructor builds the chord name with is tonic, bass, third, ... frets.
@param tonic the tonic fretted
@param s3 the third fretted
@param s5 the fifth fretted
@param s7 the seventh fretted
@param s9 the ninth fretted
@param s11 the eleventh fretted
@param s13 the thirtheenth fretted
*/
ChordListItem::ChordListItem(int tonic, int s3, int s5, int s7, int s9, int s11, int s13)
: QListWidgetItem(),
  mTonic(tonic)
{
	// MEMORIZING STEPS INFO IN THE COMBOBOX SELECTION FORM
	mStep[0] = s3;
	mStep[1] = s5;
	mStep[2] = s7;
	mStep[3] = s9;
	mStep[4] = s11;
	mStep[5] = s13;

	for (int i = 0; i < 6; i++) {
		if (mStep[i] == -1)
			mStep[i] = 0;
		else
			mStep[i] = mStep[i] - msToneShift[i] + 2;
	}

	// TEXT NAME CONSTRUCTION
	QString name = Settings::getInstance()->noteName(tonic);
	
	// Special cases
	if ((s3 == -1) && (s5 == 7) && (s7 == -1) &&
		(s9 == -1) && (s11 == -1) && (s13 == -1)) {
		setText(name + '5');
		return;
	}
	if ((s3 == 4) && (s5 == 8) && (s7 == -1) &&
		(s9 == -1) && (s11 == -1) && (s13 == -1)) {
		setText(name + "aug");
		return;
	}
	
	if ((s3 == 3) && (s5 == 6) && (s7 == 9)) {
		name = name + "dim";
	} else {
		if (s3 == 3)
			name = name + 'm';

		if (s5 == 6)
			name = name + "/5" + Settings::getInstance()->flatName();
		if (s5 == 8)
			name = name + "/5" + Settings::getInstance()->sharpName();
		if (((s5 == 6) || (s5 == 8)) && ((s7 != -1) || (s9 != -1) ||
										 (s11 != -1) || (s13 != -1)))
			name = name + '/';

		if ((s7 == 10) && (s9 == -1))
			name = name + '7';
		if (s7 == 11)
			name = name + Settings::getInstance()->maj7Name();
		if (s7 == 9)
			name = name + '6';
		if (((s7 == 11) || (s7 == 9))
			&& ((s9 != -1) || (s11 != -1) || (s13 != -1)))
			name = name + '/';
	}

	if ((s7 == -1) && (s9 != -1))
		name = name + "add";
	if ((s9 == 2) && (s11 == -1))
		name = name + '9';
	if (s9 == 1)
		name = name + '9' + Settings::getInstance()->flatName();
	if (s9 == 3)
		name = name + '9' + Settings::getInstance()->sharpName();
	if (((s9 == 1) || (s9 == 3)) && ((s11 != -1) || (s13 != -1)))
		name = name + '/';

	if ((s9 == -1) && (s11 != -1))
		name = name + "add";
	if ((s11 == 5) && (s13 == -1))
		name = name + "11";
	if (s11 == 4)
		name = name + "11" + Settings::getInstance()->flatName();
	if (s11 == 6)
		name = name + "11" + Settings::getInstance()->sharpName();
	if (((s11 == 4) || (s11 == 6)) && (s13 != -1))
		name = name + '/';

	if ((s11 == -1) && (s13 != -1))
		name = name + "add";
	if (s13 == 9)
		name = name + "13";
	if (s13 == 8)
		name = name + "13" + Settings::getInstance()->flatName();
	if (s13 == 10)
		name = name + "13" + Settings::getInstance()->sharpName();

	if (s3 == 2)
		name = name + "sus2";
	if (s3 == 5)
		name = name + "sus4";
	
	if ((s3 == -1) && (s5 == -1)) {
		name = name + " (no3no5)";
	} else {
		if (s3 == -1)
			name = name + " (no3)";
		if (s5 == -1)
			name = name + " (no5)";
	}
	
	setText(name);
}


/**
@brief Destructor
*/
ChordListItem::~ChordListItem()
{

}


/**
@brief Returns the tonic
@return the tonic
*/
int ChordListItem::tonic() const 
{
	return mTonic;
}


/**
@brief Get the step information
@return the step information
*/
int ChordListItem::step(int x) const
{
	return mStep[x];
}
