/***************************************************************************
 *   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 "homoefielditem.h"
#include "homoefieldwidget.h"
#include <QPainter>
#include <cmath>
#include <QtDebug>
#include <QRectF>
#include "simulscene.h"

HomoEFieldItem::HomoEFieldItem(QRectF sizeRect)
		: FieldItem() {
	setRectF(sizeRect);
	dockWidget = new HomoEFieldWidget(0,0,this);
	setFieldPower(10);
	setFieldLineDistance(14);
	setFlag(ItemIsMovable);
	setFlag(ItemIsSelectable);
	setFlag(ItemIsFocusable);
	setOuterPenWidth (2);
	//rotateslot(66);

	//QGraphicsRectItem * testItem = new QGraphicsRectItem(QRectF(-10,-10,6,6), this);
	//testItem->setBrush(Qt::SolidPattern);

	
	
	
}

QList<QVariant> HomoEFieldItem::getSaveDataList() {
	QList<QVariant> list;
	list << type();
	list << pos();
	list << getRectF();
	list << zValue();
	list << getRotation();
	list << getFieldPower();
	return list;
}
void HomoEFieldItem::setSaveDataList(QList<QVariant> list){
	setPos(list[1].toPointF());
	setRectF(list[2].toRectF());
	setZValue(list[3].toDouble());
	setRotation(list[4].toInt());
	setFieldPower(list[5].toDouble());
}

void HomoEFieldItem::setFieldLineDistance(int pixel) {
	fieldLineDistance = pixel;
}

QRectF HomoEFieldItem::boundingRect() const {
	return QRectF(sizeRect.x() - outerPenWidth, sizeRect.y() - outerPenWidth,
		sizeRect.width() + outerPenWidth, sizeRect.height() + outerPenWidth);
}

void HomoEFieldItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
	Qt::GlobalColor linecolor = Qt::blue;
	painter->setPen(linecolor);
	if (isSelected()) painter->setBrush(Qt::Dense6Pattern); //selection deutlich machen
	painter->drawRect(sizeRect);

	for (int i = 1; i <= floor(sizeRect.width()/(qreal)fieldLineDistance); ++i) {
		const int top_bottom_space = 10;
		const int arrow_height = 8; //pfeilhoehe
		const int arrow_width_half = 3; //Halbe pfeilbreite
		
		if ((i*fieldLineDistance)+arrow_width_half >= sizeRect.width() -2) break; //rechts ueberstehen verhindern
		if (sizeRect.height() < top_bottom_space + arrow_height) break;//nur zeichnen, wenn sizeRect hoch genug

		//Feldlinien zeichnen
		painter->drawLine(
			sizeRect.x() + (i*fieldLineDistance) ,
			sizeRect.y() +top_bottom_space ,
			sizeRect.x() + (i*fieldLineDistance) ,
			sizeRect.y() + sizeRect.height() - top_bottom_space
		);


		//Pfeile Zeichnen
		QPointF arrows[3] = {
			QPointF(sizeRect.x()+(i*fieldLineDistance), sizeRect.y()+sizeRect.height()-top_bottom_space),
			QPointF(sizeRect.x()+(i*fieldLineDistance)-arrow_width_half,sizeRect.y()+sizeRect.height()-top_bottom_space-arrow_height),
			QPointF(sizeRect.x()+(i*fieldLineDistance)+arrow_width_half,sizeRect.y()+sizeRect.height()-top_bottom_space-arrow_height),
		};
		painter->setBrush(linecolor);
		painter->drawPolygon(arrows,3);
		painter->setBrush(Qt::NoBrush);

		//qDebug() << pos();
	}
}


int HomoEFieldItem::type() const {
	return Type;
}


HomoEFieldItem::~HomoEFieldItem() {
}




/*!
    \fn HomoEFieldItem::getDockWidget()
 */
QWidget * HomoEFieldItem::getDockWidget() const
{
    return dockWidget;
}




double HomoEFieldItem::getFieldPower() const {
	return fieldPower;
}


void HomoEFieldItem::setFieldPower (double fieldPower ) {
	//if (this->fieldPower == fieldPower) return;
	this->fieldPower = fieldPower;
	emit fieldPowerChanged(fieldPower);

}





double HomoEFieldItem::getOuterPenWidth() const {
	return outerPenWidth;
}


void HomoEFieldItem::setOuterPenWidth ( double theValue ) {
	if (outerPenWidth == theValue) return;
	outerPenWidth = theValue;
}

bool HomoEFieldItem::contains(const QPointF point) {
	mapFromScene (point);
	return (
			(point.x() <= boundingRect().width()) &&
			(point.y() <= boundingRect().height()) &&
			(point.y() >=0) &&
			(point.x() >=0)
		);
	
}	