/***************************************************************************
 * tracktuningview.h: implementation of TrackTuningView class
 *
 * This file is part of KTabEdit, a KDE tabulature editor
 *
 * copyright (C) 2003-2006 the KTabEdit 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 "tracktuningview.h"

#include "../settings.h"
#include "../global.h"
#include "../tunings.h"
#include "qcanvascm/qcanvasellipsecm.h"

TrackTuningView::TrackTuningView(const TrackProperties* trackProperties, uint dpi)
: DrawingItem(dpi),
  mTrackProperties(trackProperties),
  mFont(Settings::getInstance()->textAndTabFont()),
  mFontMetric(mFont),
  mTuningDescription(""),
  mDraw(false)
{
}

TrackTuningView::~TrackTuningView()
{
	for (uchar i = 0; i < mEllipse[0].size(); i++)
		for (uchar j = 0; j < 2; j++) {
			delete mEllipse[j][i];
			delete mCanvasText[j][i];
		}
}

void TrackTuningView::hide()
{
	mTuningDescription.hide();
	
	for (uchar i = 0; i < mEllipse[0].size(); i++)
		for (uchar j = 0; j < 2; j++) {
			mEllipse[j][i]->hide();
			mCanvasText[j][i]->hide();
		}
}

void TrackTuningView::initObjects()
{
	for (uchar i = 0; i < mTrackProperties->nbStrings(); i++)
		for (uchar j = 0; j < 2; j++) {
			mEllipse[j].push_back(new QCanvasEllipseCm());
			mCanvasText[j].push_back(new QCanvasTextCm(""));
		}
}

QString TrackTuningView::tuningName() const
{
	QString name;
	uchar i, j;
	
	i = 0;
	while (lib_tuning[i].strings != 0 && name.isEmpty()) {
		if (lib_tuning[i].strings == mTrackProperties->nbStrings()) {
			j = 0;
			while (j < mTrackProperties->nbStrings() && lib_tuning[i].shift[j] != mTrackProperties->tune(j))
				j++;
			
			if (j == mTrackProperties->nbStrings())
				name = lib_tuning[i].name;
		}
		
		i++;
	}
	
	return name;
}

QPointCm TrackTuningView::drawTuningName(const QString& name)
{
	QPointCm result;
	
	if (name.isEmpty() == false) {
		Pixel pixelHeight(mFontMetric.boundingRect(name).height(), dpi());
		pixelHeight.fromOtherDpi(75);
		Pixel pixelBoundingRect(static_cast<uint>(mTuningDescription.boundingRect().right()), dpi());
		pixelBoundingRect.fromOtherDpi(75);
		
		result = QPointCm(Cm(1.500, dpi()), Cm(0.500, dpi()) + pixelHeight.toCm());
		
		addTextItemToSceneFromPoint(&mTuningDescription, name, mFont, position() + result);
	}
	
	return result;
}

void TrackTuningView::drawTuning(const Cm& heightPos)
{
	QString text;
	Pixel pixelHeight(mFontMetric.boundingRect("8").height(), dpi());
	pixelHeight.fromOtherDpi(75);
	const Cm numHeight = pixelHeight.toCm() + Cm(0.100, dpi());
	int countRows = 0;
	int countColumn = 0;
	
	for (uchar i = 0; i < mTrackProperties->nbStrings(); i++) {
		Pixel pixelBoundingRect(mFontMetric.boundingRect(text.setNum(i + 1)).width(), dpi());
		pixelBoundingRect.fromOtherDpi(75);
		const Cm x = positionX() + Cm(1.600, dpi()) + Cm(countRows, dpi()) * Cm(1.500, dpi());
		const Cm y = positionY() + heightPos + Cm(countColumn, dpi()) * numHeight + Cm(0.300, dpi());
		const Cm width = pixelBoundingRect.toCm();
		
		addEllipseItemToScene(mEllipse[0][i], Qt::black, x, y + numHeight, numHeight, numHeight);
		addEllipseItemToScene(mEllipse[1][i], Qt::white, x, y + numHeight, numHeight - Cm(0.02, dpi()), numHeight - Cm(0.02, dpi()));
		addTextItemToScene(mCanvasText[0][i], text.setNum(i + 1), mFont, x, y + numHeight);
		addTextItemToScene(mCanvasText[0][i], " = " + Settings::getInstance()->noteName(mTrackProperties->tune(i) % 12), mFont, x + width, y + numHeight);
		
		countColumn++;
		
		if ((i + 1) % 3 == 0) {
			countColumn = 0;
			countRows++;
		}
	}
}

void TrackTuningView::show()
{
	DrawingItem::show();
	
	QPointCm result;
	
	result = drawTuningName(tuningName());
	
	initObjects();
	
	drawTuning(result.y());
}

QRectCm TrackTuningView::boundingRect() const
{
	return QRectCm(position().x(), position().y(), width(), height());
}

Cm TrackTuningView::height() const
{
	QFont fontText(Settings::getInstance()->textAndTabFont());
	QFontMetrics fm(fontText);
	Pixel pixelHeight(mFontMetric.boundingRect("8").height(), dpi());
	pixelHeight.fromOtherDpi(75);
	const Cm numHeight = pixelHeight.toCm() + Cm(0.100, dpi());
	
	return Cm(((mTrackProperties->nbStrings() / 3) + 3) * numHeight.value() + 0.300, dpi());
}

Cm TrackTuningView::width() const
{
	return Cm(1.600, dpi()) + Cm(1.500, dpi()) * 4;
}
