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

#include <QFontMetrics>

#include "qcanvascm/qcanvastextcm.h"

const QString TrackTabClefView::tabText("TAB");

TrackTabClefView::TrackTabClefView(const QFont& clefFont, const Cm& heighTablature, uint dpi)
: DrawingItem(dpi),
  font(clefFont),
  clefHeight(heighTablature)
{
	for (uint i = 0; i < 3; i++)
		clefView[i] = new QCanvasTextCm(tabText[i]);
}

TrackTabClefView::~TrackTabClefView()
{
	for (uint i = 0; i < 3; i++)
		delete clefView[i];
}

void TrackTabClefView::hide()
{
	for (uint i = 0; i < 3; i++)
		clefView[i]->hide();
}

void TrackTabClefView::show()
{
	DrawingItem::show();
	
	for (uint i = 0; i < 3; i++) {
		clefView[i]->moveBy(position().x() + Cm(0.2, dpi()), position().y() + clefHeight * i / 3.0);
		clefView[i]->show();
	}
}

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

Cm TrackTabClefView::height() const
{
	return Cm(0, dpi());
}

Cm TrackTabClefView::width() const
{
	QFontMetrics fm(clefView[0]->font());
	Pixel larger(0, dpi()), newLarger(0, dpi());
	
	for (uint i = 0; i < 3; i++) {
		Pixel pixelWidth(fm.boundingRect(tabText[i]).width(), dpi());
		pixelWidth.fromOtherDpi(75);
		if ((newLarger = pixelWidth) > larger)
			larger = newLarger;
	}
	
	return larger.toCm() + Cm(0.2, dpi());
}
