#include "mapitembase.h"

#include "mapview.h"
#include "osm_math_p.h"


MapItemBase::MapItemBase() {
	m_hints = QPainter::Antialiasing | QPainter::SmoothPixmapTransform;
}


MapItemBase::~MapItemBase() {
	if (!parent) return;
	parent->removeItem(this);
}


void MapItemBase::setInteracive(bool newInteracive) {
	m_interacive = newInteracive;
	updateParent();
}


void MapItemBase::setRenderHints(QPainter::RenderHints h) {
	m_hints = h;
	updateParent();
}


void MapItemBase::setRenderHint(QPainter::RenderHint h, bool on) {
	m_hints.setFlag(h, on);
	updateParent();
}


void MapItemBase::setVisible(bool newVisible) {
	m_visible = newVisible;
	updateParent();
}


void MapItemBase::setRotation(double deg) {
	m_rotation = deg;
	updateParent();
}


void MapItemBase::rotate(double deg) {
	m_rotation += deg;
	while (deg < 0)
		deg += 360.;
	while (deg > 360.)
		deg -= 360.;
	updateParent();
}


void MapItemBase::scale(QPointF s) {
	m_scale = {m_scale.x() * s.x(), m_scale.y() * s.y()};
	updateParent();
}


void MapItemBase::setScale(QPointF s) {
	m_scale = s;
	updateParent();
}


void MapItemBase::setPosition(QPointF geo) {
	m_position = geo;
	updatePosition();
	updateParent();
}


void MapItemBase::setOffset(QPointF pixels) {
	m_offset = pixels;
	updateParent();
}


void MapItemBase::setCursor(const QCursor & newCursor) {
	m_cursor = newCursor;
	updateParent();
}


QPointF MapItemBase::mapToView(QPointF norm) const {
	if (!parent) return norm;
	return parent->mapFromNorm(norm);
}


double MapItemBase::scalePx2M(QPointF norm) const {
	if (!parent) return 1.;
	return parent->scalePx2M(norm);
}


void MapItemBase::updatePosition() {
	norm_pos = OSM::geo2xy(m_position);
	potitionChanged();
	zoomChanged();
}


void MapItemBase::updateParent() {
	if (parent) parent->update();
}


void MapItemBase::setBoundingRect(QRectF b) {
	m_bounding = b;
}
