#include "mapitemtext.h"


MapItemText::MapItemText(const QString & t) {
	setBrush(Qt::NoBrush);
	setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
	setText(t);
}


void MapItemText::setAlignment(Qt::Alignment a) {
	m_alignment = a;
	updateParent();
}


void MapItemText::setText(const QString & s) {
	str = s;
	updateParent();
}


void MapItemText::setFont(const QFont & f) {
	m_font = f;
	updateParent();
}


void MapItemText::draw(QPainter * p) {
	if (str.isEmpty()) return;
	p->setPen(pen());
	p->setFont(font());
	auto br = p->fontMetrics().boundingRect(str).adjusted(0, 0, 1, 1);
	br.translate(-br.topLeft());
	if (m_alignment.testFlag(Qt::AlignHCenter)) {
		br.translate(-br.center().x(), 0.);
	}
	if (m_alignment.testFlag(Qt::AlignRight)) {
		br.translate(-br.width(), 0.);
	}
	if (m_alignment.testFlag(Qt::AlignVCenter)) {
		br.translate(0., -br.center().y());
	}
	if (m_alignment.testFlag(Qt::AlignBottom)) {
		br.translate(0., -br.height());
	}
	p->fillRect(br.adjusted(-2, 0, 2, 0), brush());
	p->drawText(br, Qt::TextSingleLine, str);
	setBoundingRect(br);
}
