/***************************************************************************
 * tracklist.h: implementation of TrackList class
 *
 * This file is part of KTabEdit, a KDE tabulature editor
 *
 * copyright (C) 2003-2007 the KTabEdit development team <mrgwen@gmail.com>
 * 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 "tracklist.h"
#include "util/assert.h"

#include <KLocale>
#include <KMenu>
#include <QContextMenuEvent>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QVBoxLayout>
#include <QtDebug>

#include "sequencer/instruments.h"
#include "tablature/tabtrack.h"
#include "tablatureModel/tabsongmodel.h"

TrackList::TrackList(const TabSongModel* song)
: QWidget(),
  mTreeWidget(0x0),
  mSong(song),
  mCurrentTrack(0x0),
  mInstruments(0x0),
  mTreeList()
{
	QStringList headerLabels;
	QVBoxLayout* layout = new QVBoxLayout;
	mInstruments = new InstrumentsList();
	mTreeWidget = new QTreeWidget;
	headerLabels << i18n("Number") << i18n("Title") << i18n("Channel") << i18n("Bank") << i18n("Patch");
	
	mTreeWidget->setHeaderLabels(headerLabels);
	mTreeWidget->setFocusPolicy(Qt::StrongFocus);
	mTreeWidget->setAllColumnsShowFocus(true);
	
	layout->addWidget(mTreeWidget);
	setLayout(layout);
	
	updateTrackList();
	connect(mTreeWidget, SIGNAL(itemSelectionChanged()), SLOT(selectedTabTrack()));
}


TrackList::~TrackList()
{
	delete mInstruments;
}


void TrackList::setTabTrack(TabTrack* track)
{
	PRECONDITION( track != 0x0 );
	
	mCurrentTrack = track;
	mTreeWidget->setCurrentItem(mTreeList.value(mSong->positionOfTrack(track)));
}


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


void TrackList::trackAdded(TabTrack* track)
{
	updateTrackList();
	setTabTrack(track);
	selectedTabTrack();
}

void TrackList::trackDeleted(TabTrack* track)
{
	updateTrackList();
	if (track == mCurrentTrack)
		selectFirstTrack();
}


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


void TrackList::clearView()
{
	mTreeWidget->clear();
	mTreeList.clear();
}

/**
\brief Update the track list with the new changes
**/
void TrackList::updateTrackList()
{
	uint i = 1;
	QStringList item;
	
	clearView();	
	for (TabSongModel::const_iterator it = mSong->begin(); it != mSong->end(); ++it) {
		item.clear();
		item << QString::number(i) << (*it)->name() << QString::number((*it)->channel()) << QString::number((*it)->bank())
			<< mInstruments->getInstrument((*it)->patch());
		mTreeList.append(new QTreeWidgetItem(mTreeWidget, item));
		i = i + 1;
	}
	selectFirstTrack();
}


void TrackList::selectFirstTrack()
{
	if (mSong->size() >= 1) {
		mTreeList[0]->setSelected(true);
		mCurrentTrack = mSong->at(0);
		emit trackChanged(mCurrentTrack);
	} else
		mCurrentTrack = 0x0;
}


void TrackList::redispatchDeleteTrack()
{
	emit deleteTrack(mCurrentTrack);
}


/**
\brief Creates the right-click menu
**/
void TrackList::contextMenuEvent(QContextMenuEvent *event)
{
	KMenu menu(i18n("KTabEdit"), this);
	menu.addAction(i18n("Add track"), this, SIGNAL(addTrack()));
	menu.addAction(i18n("Delete track"), this, SLOT(redispatchDeleteTrack()));
	menu.addSeparator();
	menu.addAction(i18n("Track properties"), this, SIGNAL(trackProperties()));
	menu.exec(event->globalPos());

	updateTrackList();
}


void TrackList::selectedTabTrack()
{
	//TODO: why this? only one item can be selected anyway...
	QList<QTreeWidgetItem *> list = mTreeWidget->selectedItems();
	
	if (list.isEmpty() == false) {
		QTreeWidgetItem* item = list.first();		
		mCurrentTrack = mSong->at(item->text(0).toInt() - 1);
		emit trackChanged(mCurrentTrack);
	}
}

#include "tracklist.moc"
