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

#include "tablature/tabnote.h"
#include "tablature/tabtimes.h"
#include "qcanvascm/qcanvaslinecm.h"

const Cm TrackClassicalDurationView::queueSpace = Cm(0.1, 72);

TrackClassicalDurationView::TrackClassicalDurationView(const TabTime* tabTime, uint dpi)
: DrawingItem(dpi),
  mTabTime(tabTime),
  barHeightDur(Cm(0, dpi)),
  timeSpaceDur(Cm(0, dpi)),
  itemCollection(),
  mNextDuration(NoneDuration)
{
}

TrackClassicalDurationView::~TrackClassicalDurationView()
{
}

void TrackClassicalDurationView::setNextDuration(Duration nextDuration)
{
	mNextDuration = nextDuration;
}

Duration TrackClassicalDurationView::nextDuration() const
{
	return mNextDuration;
}

void TrackClassicalDurationView::setBarHeight(const Cm& i)
{
	barHeightDur = i;
}

Cm TrackClassicalDurationView::barHeight() const
{
	return barHeightDur;
}

void TrackClassicalDurationView::setTimeSpace(const Cm& i)
{
	timeSpaceDur = i;
}

Cm TrackClassicalDurationView::timeSpace() const
{
	return timeSpaceDur;
}

void TrackClassicalDurationView::hide()
{
	for (iterator it = itemCollection.begin(); it != itemCollection.end(); ++it)
		(*it)->hide();
}

void TrackClassicalDurationView::show()
{
	drawVerticalLine();
	
	if (mNextDuration != NoneDuration)
		drawFirstDuration();
	else
		drawLastDuration();
}

void TrackClassicalDurationView::drawVerticalLine()
{
	QCanvasLineCm* line = new QCanvasLineCm();
	itemCollection.push_front(line);
	
	line->setPoints(position().x(), position().y(), position().x(), barHeightDur);
	line->show();
}

void TrackClassicalDurationView::drawFirstDuration()
{
	uint nbLines = 0, nextNbLines = 0;
	
	switch (mTabTime->duration()) {
	case HundredTwentyEighth:
		nbLines = 5;
		break;
	case SixtyFourth:
		nbLines = 4;
		break;
	case ThirthSecond:  // 1/32 Triple croche
		nbLines = 3;
		break;
	case Sixteenth:  // 1/16 Double croche
		nbLines = 2;
		break;
	case Eighth:  // 1/8 Croche
		nbLines = 1;
		break;
	case Quarter: // 1/4 - a long vertical line, so we need to find the highest note
	case Half: // 1/2 Blanche
	case Whole: // whole Ronde
	case NoneDuration:
		break; 
	}
	
	for (uint i = 0; i < nbLines; i++) {
		QCanvasLineCm* line = new QCanvasLineCm();
		itemCollection.push_front(line);
		
		line->setPoints(position().x(), barHeightDur + (queueSpace * i),
						position().x() + timeSpaceDur / 2, barHeightDur + (queueSpace * i));
		line->show();
	}
	
	switch (mNextDuration) {
	case HundredTwentyEighth:
		nextNbLines = 5;
		break;
	case SixtyFourth:
		nextNbLines = 4;
		break;
	case ThirthSecond:  // 1/32 Triple croche
		nextNbLines = 3;
		break;
	case Sixteenth:  // 1/16 Double croche
		nextNbLines = 2;
		break;
	case Eighth:  // 1/8 Croche
		nextNbLines = 1;
		break;
	case Quarter: // 1/4 - a long vertical line, so we need to find the highest note
	case Half: // 1/2 Blanche
	case Whole: // whole Ronde
	case NoneDuration:
		break; 
	}
	
	if (nextNbLines < nbLines)
		nbLines = nextNbLines;
	
	// Draw the tablature duration
	//
	for (uint i = 0; i < nextNbLines; i++) {
		QCanvasLineCm* line = new QCanvasLineCm();
		itemCollection.push_front(line);
		
		line->setPoints(position().x() + timeSpaceDur / 2, barHeightDur + (queueSpace * i),
						position().x() + timeSpaceDur, barHeightDur + (queueSpace * i));
		line->show();
	}
}

void TrackClassicalDurationView::drawLastDuration()
{
	uint nbLines = 0;
	
	switch (mTabTime->duration()) {
	case HundredTwentyEighth:
		nbLines = 5;
		break;
	case SixtyFourth:
		nbLines = 4;
		break;
	case ThirthSecond:  // 1/32 Triple croche
		nbLines = 3;
		break;
	case Sixteenth:  // 1/16 Double croche
		nbLines = 2;
		break;
	case Eighth:  // 1/8 Croche
		nbLines = 1;
		break;
	case Quarter: // 1/4 - a long vertical line, so we need to find the highest note
	case Half: // 1/2 Blanche
	case Whole: // whole Ronde
	case NoneDuration:
		break; 
	}
	
	for (uint i = 0; i < nbLines; i++) {
		QCanvasLineCm* line = new QCanvasLineCm();
		itemCollection.push_front(line);
		
		line->setPoints(position().x() - timeSpaceDur / 2, barHeightDur + (queueSpace * i),
						position().x(), barHeightDur + (queueSpace * i));
		line->show();
	}
}

QRectCm TrackClassicalDurationView::boundingRect() const
{
}

Cm TrackClassicalDurationView::height() const
{
}

Cm TrackClassicalDurationView::width() const
{
}
