/***************************************************************************
 * 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 "fingeritem.h"

#include <QPainter>

#include <KGlobalSettings>
#include <KColorScheme>

#include "util/assert.h"

const uint FingerItem::msScale    = 6;
const uint FingerItem::msCircle   = 5;
const uint FingerItem::msCircBord = 1;
const uint FingerItem::msBorder   = 1;
const uint FingerItem::msSpacer   = 1;
const uint FingerItem::msFretText = 9;
const uint FingerItem::msIconChord  = 50;
const double FingerItem::msFontFactor = 0.7;

FingerItem::FingerItem(const ChordDiagram& chordDiagram)
: QWidget(),
  mNum(0),
  mCurSel(-1),
  mOldCol(0),
  mOldRow(0),
  mFrets(0),
  mStrings(0),
  mSelected(false),
  mFretNumberFont(KGlobalSettings::generalFont()),
  mChordDiagram(chordDiagram)
{
	if (mFretNumberFont.pointSize() == -1)
		mFretNumberFont.setPixelSize((int) ((double) mFretNumberFont.pixelSize() * msFontFactor));
	else
		mFretNumberFont.setPointSizeFloat(mFretNumberFont.pointSizeFloat() * msFontFactor);
	
	repaint();
}

FingerItem::~FingerItem()
{
}

void FingerItem::setSelected(bool b)
{
	mSelected = b;
}

bool FingerItem::isSelected() const
{
	return mSelected;
}

void FingerItem::setNumberOfFrets(uchar frets)
{
	mFrets = frets;
}

uchar FingerItem::numberOfFrets() const
{
	return mFrets;
}

void FingerItem::setNumberOfStrings(uchar strings)
{
	mStrings = strings;
}

uchar FingerItem::numberOfStrings() const
{
	return mStrings;
}

void FingerItem::paintEvent(QPaintEvent *)
{
	int barre, eff;
	int firstFret = mFrets;
	bool noff = true;
	QColor back = KColorScheme(QPalette::Active, KColorScheme::View, KSharedConfigPtr()).background().color();
	QColor fore = KColorScheme(QPalette::Active, KColorScheme::View, KSharedConfigPtr()).foreground().color();
	//QColor back = KGlobalSettings::baseColor();
	//QColor fore = KGlobalSettings::textColor();
	QPainter painter(this);
	
	painter.setPen(fore);
	
	// Horizontal lines
	//
	for (int i = 0; i <= sNumFrets; i++)
		painter.drawLine(msScale / 2 + msBorder + msFretText, msBorder + msScale + 2 * msSpacer + i * msScale,
					msScale / 2 + msBorder + mStrings * msScale - msScale + msFretText,
					msBorder + msScale + 2 * msSpacer + i * msScale);
	
	// Vertical lines and fingering
	//
	for (int i = 0; i < mStrings; i++) {
		painter.drawLine(i * msScale + msBorder + msScale / 2 + msFretText,
					msBorder + msScale + 2 * msSpacer,
					i * msScale + msBorder + msScale / 2 + msFretText,
					msBorder + msScale + 2 * msSpacer + sNumFrets * msScale);
	}
	
	// Beginning fret mNumber
	//
	for (int i = 0; i < mChordDiagram.chord.size(); i++) {
		if ((mChordDiagram.chord[i] < firstFret) && (mChordDiagram.chord[i] > 0))
			firstFret = mChordDiagram.chord[i];
		if (mChordDiagram.chord[i] > 5)
			noff = false;
	}

	if (noff)
		firstFret = 1;

	if (firstFret > 1) {
		QString fs;
		fs.setNum(firstFret);
		painter.setFont(mFretNumberFont);
		painter.drawText(msBorder, msBorder + msScale + 2 * msSpacer, 50, 50,
					Qt::AlignLeft | Qt::AlignTop, fs);
	}
	
	// Fingering
	//
	for (int i = 0; i < mChordDiagram.chord.size(); i++) {
		if (mChordDiagram.chord[i] == -1) {
			painter.drawLine(i*msScale+msBorder+msCircBord+msFretText,msBorder+msCircBord,
						i*msScale+msBorder+msScale-msCircBord+msFretText,
						msBorder+msScale-msCircBord);
			painter.drawLine(i*msScale+msBorder+msScale-msCircBord+msFretText,msBorder+msCircBord,
						i*msScale+msBorder+msCircBord+msFretText,msBorder+msScale-msCircBord);
		} else if (mChordDiagram.chord[i]==0) {
			painter.setBrush(back);
			painter.drawEllipse(i*msScale+msBorder+msCircBord+msFretText,msBorder+msCircBord,
							msCircle,msCircle);
		} else {
			painter.setBrush(fore);
			painter.drawEllipse(i*msScale+msBorder+msCircBord+msFretText,
							msBorder+msScale+2*msSpacer+(mChordDiagram.chord[i]-firstFret)*msScale+
							msCircBord,msCircle,msCircle);
		}
	}
	
	// Analyze & draw barre
	//
	painter.setBrush(fore);
	
	for (int i = 0; i < sNumFrets; i++) {
		barre = 0;
		while ((mChordDiagram.chord[mStrings - barre - 1] >= (i + firstFret)) ||
				(mChordDiagram.chord[mStrings - barre - 1] == -1)) {
			barre++;
			if (barre > mStrings - 1)
				break;
		}

		while ((mChordDiagram.chord[mStrings-barre]!=(i+firstFret)) && (barre>1))
			barre--;

		eff = 0;
		for (int j = mStrings-barre; j < mStrings; j++) {
			if (mChordDiagram.chord[j] != -1)
				eff++;
		}

		if (eff > 2) {
			painter.drawRect((mStrings-barre) * msScale + msScale / 2 +
						msBorder + msFretText,
						msBorder + msScale + 2 * msSpacer + i * msScale + msCircBord,
						(barre - 1) * msScale, msCircle);
		}
	}
	
	painter.setBrush(Qt::NoBrush);
	painter.setPen(Qt::SolidLine);
}

#include "fingeritem.moc"
