/***************************************************************************
 *   Copyright (C) 2008 by Peter Dahlberg   *
 *   pdahlberg@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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "fielditem.h"
#include "homoefielditem.h"
#include "homobfielditem.h"
#include "stopperitem.h"
#include "simulscene.h"
#include <QGraphicsSceneWheelEvent>
#include <QDebug>
#include <QMenu>

FieldItem::FieldItem()
		: QGraphicsItem() {
	myRotation = 0;
	myScenePos = QPointF(0,0);	
	mySimulScene =  dynamic_cast<SimulScene*> (scene());
}


FieldItem::~FieldItem() {
}




/*!
    \fn FieldItem::isFieldItem(QGraphicsItem *item)
 */
bool FieldItem::isFieldItem(QGraphicsItem *item) {
	QList<int> fieldItemTypes;
	// ---- Definiert die FieldItem typen
	fieldItemTypes << HomoEFieldItem::Type; 
	fieldItemTypes << HomoBFieldItem::Type;
	fieldItemTypes << StopperItem::Type;
	// ----
	for (int i=0; i<fieldItemTypes.count();++i) {
		if (item->type() == fieldItemTypes.at(i)) {
			return true;
		}
	}
	return false;
	//return item->type() == HomoEFieldItem::Type;
}

void FieldItem::handleSceneChange(const QList<QRectF> & /*region*/) {
	if (myScenePos != scenePos()) {
		myScenePos = scenePos();
		emit ScenePosChanged(myScenePos);
		emit ScenePosXChanged(myScenePos.x());
		emit ScenePosYChanged(myScenePos.y());
	}

}

void FieldItem::setScenePosX(double newXPos) {
	moveBy(newXPos - scenePos().x(), 0);
	//qDebug() << mapFromScene(0 , 0);
}
void FieldItem::setScenePosY(double newYPos) {
	moveBy(0,newYPos - scenePos().y());
}

void FieldItem::setRotation(int angle) {
	if (angle == myRotation) return;
	setTransform(QTransform().translate(boundingRect().width()/2.0, boundingRect().height()/2.0).rotate(angle).translate(-boundingRect().width()/2.0, -boundingRect().height()/2.0));
	myRotation = angle;
	emit rotationChanged(angle);
	emit rotationChanged();
}

void FieldItem::setRectFHeight(double newHeight) {
	QRectF currRect = getRectF();
	if (currRect.height() == newHeight) return;
	currRect.setHeight(newHeight);
	setRectF(currRect);
}

void FieldItem::setRectFWidth(double newWidth) {
	QRectF currRect = getRectF();
	if (currRect.width() == newWidth) return;
	currRect.setWidth(newWidth);
	setRectF(currRect);
}

void FieldItem::setRectF(QRectF rect) {
	if (rect == sizeRect) return;
	double newHeight = rect.height();
	double newWidth = rect.width();
	double oldHeight = sizeRect.height();
	double oldWidth = sizeRect.width();

	prepareGeometryChange();
	sizeRect = rect;
	prepareGeometryChange();

	//breite geandert
	if (newWidth != oldWidth) emit widthChanged(newWidth);
	//hoehe geandert
	if (newHeight != oldHeight) emit heightChanged(newHeight);
}

void FieldItem::setRect(QRectF rect) {
	setRectF(rect);
}

QRectF FieldItem::getRectF() const {
	return sizeRect;
}

QRectF FieldItem::rect() const {
	return getRectF();
}

void FieldItem::wheelEvent(QGraphicsSceneWheelEvent * event) {
	if (event->modifiers() == Qt::ShiftModifier) {
		setRotation(getRotation()+( (event->delta()/abs(event->delta()))) );
	}
	else if (event->modifiers() == Qt::ControlModifier) {
		setRotation(getRotation()+( (event->delta()/abs(event->delta()))*6 ));		
	}
}
void FieldItem::contextMenuEvent ( QGraphicsSceneContextMenuEvent * event ){

	QMenu menu;
	QAction *removeAction = menu.addAction(tr("L\366schen"));
	connect(removeAction, SIGNAL(triggered()),this, SLOT(deleteMe()));

	QString selectString;
	if(isSelected()) selectString = tr("Abw\344hlen");
	else selectString = tr("Ausw\344hlen");
	QAction *selectAction = menu.addAction(selectString);
	connect(selectAction, SIGNAL(triggered()),this, SLOT(toggleSelection()));
	QAction *selectedAction = menu.exec(event->screenPos());
}

/*!
    \fn FieldItem::getRotation()
 */
int FieldItem::getRotation() {
    return myRotation;
}

void FieldItem::deleteMe() {
    delete this;
}

void FieldItem::toggleSelection() {
    setSelected(!isSelected());
}
