/***************************************************************************
 * drawingitem.cpp: implementation of DrawingItem class
 *
 * This file is part of KTabEdit, a KDE tabulature editor
 *
 * copyright (C) 2003-2007 the KTabEdit development team <mrgwen@gmail.com>
 ***************************************************************************/

/***************************************************************************
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * See the file COPYING for more information.
 ***************************************************************************/

#include "drawingitem.h"

#include <QGraphicsItem>
#include <QGraphicsScene>

#include "qcanvascm/qcanvasellipsecm.h"
#include "qcanvascm/qcanvastextcm.h"
#include "util/assert.h"

DrawingItem::DrawingItem(uint dpi)
: Unit(dpi),
  mGraphicsScene(0),
  mPosition(),
  mIsVisible(false)
{
}

DrawingItem::~DrawingItem()
{
}

void DrawingItem::setGraphicsScene(QGraphicsScene* graphicsScene)
{
	mGraphicsScene = graphicsScene;
}

QGraphicsScene* DrawingItem::graphicsScene() const
{
	return mGraphicsScene;
}

void DrawingItem::setPosition(const QPointCm& position)
{
	mPosition = position;
}

QPointCm DrawingItem::position() const
{
	return mPosition;
}

Cm DrawingItem::positionX() const
{
	return mPosition.x();
}

Cm DrawingItem::positionY() const
{
	return mPosition.y();
}

void DrawingItem::addToPosition(const Cm& width,  const Cm& height)
{
	mPosition.setX(mPosition.x() + width);
	mPosition.setY(mPosition.y() + height);
}

void DrawingItem::addWidthToPosition(const Cm& width)
{
	mPosition.setX(mPosition.x() + width);
}

void DrawingItem::addHeightToPosition(const Cm& height)
{
	mPosition.setY(mPosition.y() + height);
}

void DrawingItem::show()
{
	mIsVisible = true;
}

void DrawingItem::hide()
{
	mIsVisible = false;
}

bool DrawingItem::isVisible() const
{
	return mIsVisible;
}

void DrawingItem::addItemToScene(QGraphicsItem* item)
{
	PRECONDITION(mGraphicsScene != 0);
	PRECONDITION(item != 0);
	
	mGraphicsScene->addItem(item);
}

void DrawingItem::addEllipseItemToScene(QCanvasEllipseCm* item, const QBrush& brush, const Cm& x, const Cm& y, const Cm& width, const Cm& height)
{
	PRECONDITION(item != 0);
	
	item->setBrush(brush);
	item->setPosFromCoord(x, y, width, height);
	addItemToScene(item);
}

void DrawingItem::addTextItemToScene(QCanvasTextCm* item, const QString& text, const QFont& font, const Cm& x, const Cm& y)
{
	PRECONDITION(item != 0);
	
	item->setText(text);
	item->setFont(font);
	item->setPosFromCoord(x, y);
	addItemToScene(item);
}

void DrawingItem::addTextItemToSceneFromPoint(QCanvasTextCm* item, const QString& text, const QFont& font, const QPointCm& point)
{
	PRECONDITION(item != 0);
	addTextItemToScene(item, text, font, point.x(), point.y());
}

void DrawingItem::drawCanvas(const QPointCm& position, DrawingItem* canvasItem)
{
	PRECONDITION(mGraphicsScene != 0);
	
	canvasItem->setPosition(position);
	canvasItem->show();
}
