#include "etabwidget.h"


ETabWidget::ETabWidget(QWidget * parent): QTabWidget(parent) {
	tabBar()->setMouseTracking(true);
	tabBar()->installEventFilter(this);
}


void ETabWidget::retranslate() {
	for (int i = 0; i < buttons.size(); ++i)
		buttons[i].toolTip = QApplication::translate("MainWindow", buttons[i].srcToolTip.toUtf8(), 0 /*, QCoreApplication::UnicodeUTF8*/);
	QList<QToolButton *> bl = findChildren<QToolButton *>();
	foreach(QToolButton * i, bl)
		i->setToolTip(
			QApplication::translate("MainWindow", i->property("sourceToolTip").toString().toUtf8(), 0 /*, QCoreApplication::UnicodeUTF8*/));
}


int ETabWidget::addTab(QWidget * page, const QIcon & icon, const QString & label) {
	int ret     = QTabWidget::addTab(page, icon, label);
	QWidget * w = new QWidget();
	w->setLayout(new QBoxLayout(QBoxLayout::RightToLeft));
	w->layout()->setContentsMargins(0, 0, 0, 0);
	w->layout()->setSpacing(2);
	QToolButton * b;
	foreach(const TabButton & i, buttons) {
		b = new QToolButton();
		b->setToolTip(i.toolTip);
		b->setIconSize(QSize(16, 16));
		b->setIcon(i.icon);
		// b->setFlat(true);
		b->setProperty("sourceToolTip", i.toolTip);
		b->setProperty("buttonRole", i.role);
		connect(b, SIGNAL(clicked(bool)), this, SLOT(buttonClicked()));
		w->layout()->addWidget(b);
		b->setVisible(i.visible);
	}
	tabBar()->setTabButton(ret, QTabBar::RightSide, w);
	return ret;
}


void ETabWidget::setButtonVisible(int role, bool yes) {
	QList<QToolButton *> bl = findChildren<QToolButton *>();
	foreach(QToolButton * i, bl)
		if (i->property("buttonRole").toInt() == role) i->setVisible(yes);
	QWidget * w;
	for (int i = 0; i < buttons.size(); ++i) {
		if (buttons[i].role == role) buttons[i].visible = yes;
		w = tabBar()->tabButton(i, QTabBar::RightSide);
		if (w != 0) w->adjustSize();
	}
	tabBar()->adjustSize();
}

/*
void ETabWidget::removeTab(int index) {
    tbs.removeAll(qobject_cast<QToolButton * >(tabBar()->tabButton(index, QTabBar::RightSide)->layout()->itemAt(1)->widget()));
    tbs.removeAll(qobject_cast<QToolButton * >(tabBar()->tabButton(index, QTabBar::RightSide)->layout()->itemAt(0)->widget()));
    QTabWidget::removeTab(index);
}
*/

bool ETabWidget::eventFilter(QObject * o, QEvent * e) {
	static int prev = -1;
	if (e->type() == QEvent::MouseMove) {
		QTabBar * t = qobject_cast<QTabBar *>(o);
		if (t == 0) return QTabWidget::eventFilter(o, e);
		for (int i = 0; i < count(); ++i)
			if (t->tabRect(i).contains(((QMouseEvent *)e)->pos())) {
				if (i != prev) {
					prev = i;
					emit tabHovered(i);
				}
				return QTabWidget::eventFilter(o, e);
			}
		if (-1 != prev) {
			prev = -1;
			emit tabHovered(-1);
		}
	}
	if (e->type() == QEvent::Leave) {
		if (-1 != prev) {
			prev = -1;
			emit tabHovered(-1);
		}
	}
	return QTabWidget::eventFilter(o, e);
}


void ETabWidget::changeEvent(QEvent * e) {
	QTabWidget::changeEvent(e);
	switch (e->type()) {
	case QEvent::LanguageChange: retranslate(); break;
	default: break;
	}
}


void ETabWidget::buttonClicked() {
	QToolButton * s = qobject_cast<QToolButton *>(sender());
	if (s == 0) return;
	QWidget * pw = s->parentWidget();
	if (pw == 0) return;
	for (int i = 0; i < count(); ++i)
		if (tabBar()->tabButton(i, QTabBar::RightSide) == pw) {
			emit tabButtonClicked(i, s->property("buttonRole").toInt());
			return;
		}
}
