#include "alignedtextitem.h"
#include <QPainter>
#include <QDebug>


AlignedTextItem::AlignedTextItem(QGraphicsItem * parent): QGraphicsItem(parent), text_(this) {
	align_ = Qt::AlignTop | Qt::AlignHCenter;
	text_.setData(1003, true);
	_move();
}


AlignedTextItem::AlignedTextItem(const QString & text, QGraphicsItem * parent): QGraphicsItem(parent), text_(this) {
	align_ = Qt::AlignTop | Qt::AlignHCenter;
	setText(text);
}


QPointF AlignedTextItem::_point(Qt::Alignment a) const {
	QRectF br = text_.boundingRect();
	QPointF ret;
	switch (Qt::AlignmentFlag(int(align_ & Qt::AlignHorizontal_Mask))) {
	case Qt::AlignRight: ret.rx() = br.left(); break;
	case Qt::AlignHCenter: ret.rx() = br.center().x(); break;
	case Qt::AlignLeft: ret.rx() = br.right(); break;
	default: break;
	}
	switch (Qt::AlignmentFlag(int(align_ & Qt::AlignVertical_Mask))) {
	case Qt::AlignBottom: ret.ry() = br.top(); break;
	case Qt::AlignVCenter: ret.ry() = br.center().y(); break;
	case Qt::AlignTop: ret.ry() = br.bottom(); break;
	default: break;
	}
	return ret;
}


void AlignedTextItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
	if (isSelected()) {
		painter->setPen(QPen(Qt::DashLine));
		painter->setBrush(Qt::NoBrush);
		painter->drawRect(boundingRect());
	}
}
