/***************************************************************************
 * trackpane.cpp: implementation of TrackPane class
 *
 * This file is part of KGuitar, a KDE tabulature editor
 *
 * copyright (C) 2002-2003 the KGuitar 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 "trackpane.h"
#include "util/assert.h"

#include <QMouseEvent>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QVBoxLayout>

#include "tablature/tabnote.h"
#include "tablature/tabtimes.h"
#include "tablature/tabbar.h"
#include "tablature/tabtrack.h"
#include "tablatureModel/tabsongmodel.h"

TrackPane::TrackPane(const TabSongModel* tabSong, uint headerHeight, uint cellSide)
: QWidget(),
  mTabSong(tabSong),
  mCurrentTrack(0x0),
  mTableWidget(0x0),
  mHeaderHeight(headerHeight),
  mCellSide(cellSide),
  mNbCells(0)
{
	PRECONDITION( tabSong != 0x0 );
	
	QVBoxLayout* layout = new QVBoxLayout;
	
	mTableWidget = new QTableWidget(this);
	layout->addWidget(mTableWidget);
	
	setLayout(layout);
	
	updateTrackList();
}

TrackPane::~TrackPane()
{
}

void TrackPane::songUpdated()
{
	updateTrackList();
}

void TrackPane::trackAdded(TabTrack*)
{
	updateTrackList();
}

void TrackPane::trackDeleted(TabTrack*)
{
	updateTrackList();
}

void TrackPane::trackUpdated()
{
	updateTrackList();
}

void TrackPane::setTabTrack(TabTrack* track)
{
	PRECONDITION( track != 0x0 );
	
	mCurrentTrack = track;
	updateTrackList();
}

void TrackPane::updateTrackList()
{
	uint y = 0;
	uint row = 0, column = 0;
	
	mTableWidget->setRowCount(mTabSong->size());
	mTableWidget->setColumnCount(mTabSong->maximumOfBars());
	
	for (TabSongModel::const_iterator it = mTabSong->begin(); it != mTabSong->end(); ++it) {
		const TabTrack* tabTrack = *it;
		QTableWidgetItem* rowItem = new QTableWidgetItem;
		
		rowItem->setText(tabTrack->name());
		mTableWidget->setVerticalHeaderItem(row, rowItem);
		
		for (uint i = 0; i < tabTrack->size(); i++) {
			QTableWidgetItem* item = new QTableWidgetItem;
			
			if (i >= column) {
				QTableWidgetItem* columnItem = new QTableWidgetItem;
				
				mTableWidget->setHorizontalHeaderItem(column, columnItem);
				mTableWidget->setColumnWidth(column, 30);
				
				column = column + 1;
			}
			
			item->setBackground(QBrush(Qt::blue));
			item->setFlags(0);
			mTableWidget->setItem(y, i, item);
		}
		
		row = row + 1;
		y = y + 1;
	}
	
}

void TrackPane::mousePressEvent(QMouseEvent *e)
{
	if (e->button() == Qt::LeftButton) {
		int barnum = e->pos().x() / mCellSide;
		int tracknum = (e->pos().y() - mHeaderHeight) / mCellSide;
		
		if (tracknum >= mTabSong->size())
			return;
		
		if (mTabSong->at(tracknum) != mCurrentTrack) {
			mCurrentTrack = mTabSong->at(tracknum);
 			emit trackChanged(mTabSong->at(tracknum));
		}
		
		emit barSelected(barnum);
	}
}

#include "trackpane.moc"
