/***************************************************************************
 * fingerlist.h: implementation of FingerList 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 "fingerlist.h"

#include <QMouseEvent>

#include "fingeritem.h"

#include "tablature/trackmidiproperties.h"
#include "tablature/timesignature.h"
#include "tablature/trackproperties.h"
#include "tablature/tabnote.h"
#include "tablature/tabtimes.h"
#include "tablature/tabbar.h"
#include "tablature/tabtrack.h"

const uint FingerList::msIconChord  = 50;
const double FingerList::msFontFactor = 0.7;

FingerList::FingerList(const TabTrack& track)
: QTableWidget(),
  mTabTrack(track),
  mNum(0),
  mPerRow(3),
  mCurSel(-1),
  mOldCol(1),
  mOldRow(1)
{
	setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	
	setRowCount(100);
	setColumnCount(100);
	
	setMinimumSize(msIconChord + 2, msIconChord + 2);
	resize(width(), 3 * msIconChord + 2);
}

FingerList::~FingerList()
{
}

// Begins new "session" for fingering list, i.e. clears it and
// prepares for adding new chords.
void FingerList::beginSession()
{
	clear();
}

// Ends adding "session" for the list, setting proper mNumber of
// columns / rows, updating it, etc.
void FingerList::endSession()
{
    // mNum is overral mNumber of chord fingerings. If it's 0 - then there are no
    // fingerings. In the appl array, indexes should be ranged from 0 to (mNum-1)
	
    //setNumRows((mNum - 1) / mPerRow + 1);
	//repaintContents();
}

void FingerList::clear()
{
	mChordDiagrams.clear();
	mNum = 0;
	mCurSel = -1;
	mOldCol = 0;
	mOldRow = 0;
}

void FingerList::addFingering(const int a[sMaxStrings])
{
	FingerItem* item;
	
	mChordDiagrams.resize((mNum + 1) * sMaxStrings);
	
	for (int i = 0; i < sMaxStrings; i++)
		mChordDiagrams[mNum].chord[i] = a[i];
	
	qDebug() << "hhhh : " << mChordDiagrams[mNum].chord.size() << "\n";
	item = new FingerItem(mChordDiagrams[mNum]);
	item->setNumberOfFrets(mTabTrack.nbFrets());
	item->setNumberOfStrings(mTabTrack.nbStrings());
	item->show();
	
	setCellWidget(mOldRow, mOldCol, item);
	setRowHeight(mOldRow, msIconChord);
	setColumnWidth(mOldCol, msIconChord);
	
	mOldCol++;
	if (mOldCol > mPerRow) {
		mOldCol = 0;
		mOldRow++;
	}
	
	//setRowCount(mPerRow);
	//setColumnCount(mOldCol);
	
	mNum++;
}

void FingerList::mousePressEvent(QMouseEvent *e)
{
	int col = columnAt(e->x());
	int row = 0;
	//int row = rowAt(e->y() + contentsY());

	int n = row * mPerRow + col;

	if ((n >= 0) && (n < mNum)) {
		mCurSel = row * mPerRow + col;
		//repaintCell(mOldRow, mOldCol);
		//repaintCell(row, col);
		mOldCol = col;
		mOldRow = row;
		emit chordSelected(mChordDiagrams[mCurSel].chord);
	}
}

void FingerList::selectFirst()
{
    emit chordSelected(mChordDiagrams[0].chord);
}

int FingerList::count() const
{
	return mChordDiagrams.size();
}

#include "fingerlist.moc"
