
#include "ETabWidget.h"
#include <QMouseEvent>
#include <QTabBar>
#include <QMenu>
#include <QInputDialog>
#include "Debugger.h"
#include "DebuggerMain.h"
#include <boost/smart_ptr.hpp>

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
ETabWidget::ETabWidget(QWidget *parent) : QTabWidget(parent) {
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
QString ETabWidget::caption(int index) const {
	QString ret;
	QTabBar *const bar = tabBar();
	if(bar != 0) {
		ret = bar->tabData(index).toString();
	}
	return ret;
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void ETabWidget::mousePressEvent(QMouseEvent *event) {
#if QT_VERSION >= 0x040300
	const int tab = tabBar()->tabAt(event->pos());
	if(tab != -1) {
		boost::scoped_ptr<QMenu> menu(new QMenu(this));
		QAction *const actionAdd	= menu->addAction(tr("&Set Label"));
		QAction *const actionClear	= menu->addAction(tr("&Clear Label"));		
		QAction *const chosen		= menu->exec(mapToGlobal(event->pos()));
		
		if(chosen == actionAdd) {
			bool ok;
			const QString text = QInputDialog::getText(
				this,
				tr("Set Caption"),
				tr("Caption:"),
				QLineEdit::Normal,
				tabBar()->tabData(tab).toString(),
				&ok);

			if (ok && !text.isEmpty()) {
				tabBar()->setTabData(tab, text);
				
			}
		} else if(chosen == actionClear) {
			tabBar()->setTabData(tab, QString());
		}
		
		// UGLY!
		// this is absolutely hidious, but i see no other way of updating
		// the GUI 
		DebuggerMain *const ui = qobject_cast<DebuggerMain *>(edb::v1::debuggerUI);
		if(ui != 0) {
			ui->updateGUI();
		}
	}
#endif
}
