/***************************************************************************
 * tracktimesignatureview.cpp: implementation of TrackTimeSignatureView 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 "tracktimesignatureview.h"

#include <QFontMetrics>

#include "qcanvascm/qcanvastextcm.h"

TrackTimeSignatureView::TrackTimeSignatureView(const TimeSignature* signature, const QFont& font, const Cm& height, uint dpi)
: DrawingItem(dpi),
  mTimeSignature(signature),
  mTimeFont(font),
  mTimeHeight(height)
{
	QString temp;
	
	mCanvasView[0] = new QCanvasTextCm(temp.setNum(mTimeSignature->numerator()));
	mCanvasView[1] = new QCanvasTextCm(temp.setNum(mTimeSignature->denominator()));
}

TrackTimeSignatureView::~TrackTimeSignatureView()
{
	for (uint i = 0; i < 2; i++)
		delete mCanvasView[i];
}

void TrackTimeSignatureView::hide()
{
	mCanvasView[0]->hide();
	mCanvasView[1]->hide();
}

void TrackTimeSignatureView::show()
{
	DrawingItem::show();
	
	mCanvasView[0]->moveBy(position().x(), position().y());
	mCanvasView[0]->show();
	
	mCanvasView[1]->moveBy(position().x(), position().y() + mTimeHeight / 2);
	mCanvasView[1]->show();
}

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

Cm TrackTimeSignatureView::width() const
{
	QFontMetrics fm(mTimeFont);
	QString temp;
	Pixel pixelWidth1(fm.boundingRect(temp.setNum(mTimeSignature->denominator())).width(), dpi());
	Pixel pixelWidth2(fm.boundingRect(temp.setNum(mTimeSignature->numerator())).width(), dpi());
	pixelWidth1.fromOtherDpi(75);
	pixelWidth2.fromOtherDpi(75);
	
	return (pixelWidth1 < pixelWidth2 ? pixelWidth2.toCm() : pixelWidth1.toCm());
}

Cm TrackTimeSignatureView::height() const
{
	return mTimeHeight;
}
