#include "Tag.h"

#include <QMouseEvent>
#include <QPainter>
#include <QPixmap>
#include <QDebug>

Tag::Tag(QString text, QWidget *parent)
: QLabel(text.trimmed(), parent)
{
	//textWidth = QLabel::fontMetrics().horizontalAdvance(text.trimmed());
	textWidth = QLabel::fontMetrics().width(text.trimmed());
	setStyleSheet("background:#55aaff; color:#f5f5f5; padding:2px; border-radius:8px");
	setFixedWidth(textWidth + 30);
}

void Tag::mouseReleaseEvent(QMouseEvent *ev)
{
	Q_UNUSED(ev);
	if (ev->x() > textWidth)
	{
		hide();
		emit removed(this);
	}
}

void Tag::mouseMoveEvent(QMouseEvent *ev)
{
	if (ev->x() > textWidth)
	{
		setCursor(Qt::PointingHandCursor);
	}
	else
	{
		setCursor(Qt::ArrowCursor);
	}
}

void Tag::paintEvent(QPaintEvent *ev)
{
	QLabel::paintEvent(ev);
	QPainter painter(this);

	QPixmap pixmap(":/Resources/btn_hidepanel.png");
	painter.drawPixmap(textWidth + 10, 2, pixmap);
}
