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

#include "qcanvascm/qcanvaslinecm.h"

TrackBarLineView::TrackBarLineView(uint strings, const Cm& height, uint dpi)
: DrawingItem(dpi),
  nbStrings(strings),
  lineSpaces(height),
  mBarLength(Cm(0, dpi)),
  mLines()
{
}

TrackBarLineView::~TrackBarLineView()
{
	for (uint i = 0; i < mLines.size(); i++) {
		mLines[i]->hide();
		
		delete mLines[i];
	}
}

void TrackBarLineView::hide()
{
	mLines[0]->hide();
	mLines[1]->hide();
	
	for (uint i = 0; i < nbStrings; i++)
		mLines[i + 2]->hide();
}

void TrackBarLineView::setBarLength(const Cm& length)
{
	PRECONDITION( length.value() >= 0 );
	
	mBarLength = length;
}

Cm TrackBarLineView::barLength() const
{
	return mBarLength;
}

void TrackBarLineView::show()
{
	DrawingItem::show();
	
	for (uint i = 0; i < nbStrings + 2; i++)
		mLines.push_back(new QCanvasLineCm());
	
	mLines[0]->setPoints(positionX(), positionY(), positionX(),
						 positionY() + lineSpaces * (nbStrings - 1));
	mLines[1]->setPoints(positionX() + mBarLength, positionY(),
						 positionX() + mBarLength, positionY() + lineSpaces * (nbStrings - 1));
	
	addItemToScene(mLines[0]);
	addItemToScene(mLines[1]);
	
	for (uint i = 0; i < nbStrings; i++) {
		QCanvasLineCm* line = new QCanvasLineCm();
		line->setPoints(positionX(), positionY() + lineSpaces * i,
						positionX() + mBarLength, positionY() + lineSpaces * i);
		addItemToScene(line);
		
		mLines.push_back(line);
	}
}

Cm TrackBarLineView::lineSpace() const
{
	return lineSpaces;
}

QRectCm TrackBarLineView::boundingRect() const
{
	return QRectCm(positionX(), positionY(), width(), height());
}

Cm TrackBarLineView::height() const
{
	return lineSpaces * (nbStrings - 1);
}

Cm TrackBarLineView::width() const
{
	if (isVisible())
		return mBarLength;
	
	return Cm(0, dpi());
}
