/***************************************************************************
 * melodyeditor.cpp: implementation of MelodyEditor class
 *
 * This file is part of KTabEdit, a KDE tabulature editor
 *
 * copyright (C) 2003-2006 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 "melodyeditor.h"

#include <QLabel>
#include <QPushButton>

#include <KLocale>

#include "util/assert.h"

#include "tablature/tabbeateffects.h"
#include "tablature/tabchord.h"
#include "tablature/tabduration.h"

#include "tablature/tabbend.h"
#include "tablature/tabdynamic.h"

#include "tablature/tabgracenotetransition.h"
#include "tablature/tabgracenote.h"

#include "tablature/tabharmonic.h"
#include "tablature/tabslide.h"
#include "tablature/tabtrill.h"
#include "tablature/tabnoteeffect.h"

#include "tablature/tabnote.h"

#include "tablature/songkey.h"
#include "tablature/timesignature.h"

#include "tablature/tabmixtablechanges.h"
#include "tablature/tabtimes.h"
#include "tablature/tabbarproperties.h"

#include "tablature/tabbar.h"

#include "tablature/trackmidiproperties.h"
#include "tablature/trackproperties.h"


#include "fretboard.h"

#include "options/options.h"
#include "options/optionsmelodyeditor.h"

#include "settings.h"

MelodyEditor::MelodyEditor(const TrackProperties* trackProperties, QWidget *parent)
: QWidget(parent),
  mFretboard(0x0),
  mTonic(0x0),
  mMode(0x0),
  mOptions(0x0),
  fr(sMaxFrets + 1)
{
	QLabel* tonicLabel;
	QLabel* modeLabel;
	QBoxLayout* widgetLayout;
	QBoxLayout* settingsLayout;
	
	mFretboard = new Fretboard(trackProperties, this);
// 	connect(tv, SIGNAL(timesChanged(const TabTimes&)), fb, SLOT(refresh(const TabTimes&)));
	
	mTonic = new QComboBox(false, this);
	for (int i = 0; i < 12; i++)
		mTonic->insertItem(Settings::getInstance()->noteName(i));
	
	mMode = new QComboBox(false, this);
	mMode->insertItem(i18n("<no mode>"));
	mMode->insertItem(i18n("Pentatonic"));
	mMode->insertItem(i18n("Natural Major"));
	mMode->insertItem(i18n("Natural Minor"));
	mMode->insertItem(i18n("Harmonic Major"));
	mMode->insertItem(i18n("Harmonic Minor"));
	mMode->insertItem(i18n("Melodic Major"));
	mMode->insertItem(i18n("Melodic Minor"));
	mMode->insertItem(i18n("Mixolydian"));
	mMode->insertItem(i18n("Lydian"));
	mMode->insertItem(i18n("Dorian"));
	mMode->insertItem(i18n("Phrygian"));
	mMode->insertItem(i18n("Locrian"));
	
	mOptions = new QPushButton(i18n("Options..."), this);
	
	tonicLabel = new QLabel(mTonic, i18n("&Tonic:"), this);
	modeLabel = new QLabel(mMode, i18n("&Mode:"), this);
	
	widgetLayout = new QVBoxLayout(this);
	
	settingsLayout = new QHBoxLayout(widgetLayout, 5);
	settingsLayout->addWidget(tonicLabel);
	settingsLayout->addWidget(mTonic);
	settingsLayout->addWidget(modeLabel);
	settingsLayout->addWidget(mMode);
	settingsLayout->addStretch(1);
	settingsLayout->addWidget(mOptions);
	
	widgetLayout->addWidget(mFretboard);
	
// 	connect(fb, SIGNAL(buttonPress(int, int, ButtonState)),
// 	        tv, SLOT(melodyEditorPress(int, int, ButtonState)));
// 	connect(fb, SIGNAL(buttonRelease(ButtonState)), tv, SLOT(melodyEditorRelease(ButtonState)));
// 	connect(tv, SIGNAL(trackChanged(TabTrack *)), fb, SLOT(setTrack(TabTrack *)));
// 	connect(tv, SIGNAL(columnChanged()), fb, SLOT(update()));
	connect(mOptions, SIGNAL(clicked()), SLOT(optionsDialog()));
	
	connect(mTonic, SIGNAL(highlighted(int)), mFretboard, SLOT(setTonic(int)));
	connect(mMode, SIGNAL(highlighted(int)), mFretboard, SLOT(setMode(int)));

// 	installEventFilter(this);

	setCaption(i18n("Melody Constructor"));
}

void MelodyEditor::drawBackground()
{
	//fb->update();
}

void MelodyEditor::setTrackProperties(const TrackProperties* trackProperties)
{
	mFretboard->setTrackProperties(trackProperties);
}

void MelodyEditor::optionsDialog()
{
	KDialog* dialog = new KDialog(this);
	dialog->setCaption(i18n("Melody Constructor"));
	dialog->setButtons(KDialog::Ok | KDialog::Cancel);
	dialog->show();
	/*KDialog* dialog = new KDialog(this);
	OptionsMelodyEditor* melodyEditor = new OptionsMelodyEditor(dialog);
	
	dialog->setCaption(i18n("Melody Constructor"));
	dialog->setButtons(KDialog::Help | KDialog::Default | KDialog::Ok | KDialog::Apply | KDialog::Cancel);
	dialog->setMainWidget(melodyEditor);
	
	connect(dialog, SIGNAL(defaultClicked()), melodyEditor, SLOT(defaultBtnClicked()));
	connect(dialog, SIGNAL(okClicked()), melodyEditor, SLOT(applyBtnClicked()));
	connect(dialog, SIGNAL(applyClicked()), melodyEditor, SLOT(applyBtnClicked()));
	
	dialog->exec();
	
	delete dialog;
	
	mFretboard->update();*/
}

// Special event filter that translates all keypresses to main widget,
// i.e. TrackView
// bool MelodyEditor::eventFilter(QObject *o, QEvent *e)
// {
// 	if (e->type() == QEvent::KeyPress) {
// 		QKeyEvent *k = (QKeyEvent *) e;
// 		printf("Ate key press %d\n", k->key());
// 		QEvent ce(*e);
// 		QApplication::sendEvent(tv, &ce);
// 		return true;
// 	} else {
// 		return false;
// 	}
// }

#include "melodyeditor.moc"
