/***************************************************************************
 *   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 "homobfielditem.h"
#include "homobfieldwidget.h"
#include <cmath>
#include <QPainter>
#include <QWidget>
#include <QtDebug>
#include <QGraphicsSceneWheelEvent>

HomoBFieldItem::HomoBFieldItem(QRectF sizeRect): FieldItem() {
	setRectF(sizeRect);
	setFieldLineDistance(15);
	setFlag(ItemIsMovable);
	setFlag(ItemIsSelectable);
	setFlag(ItemIsFocusable);
	setOuterPenWidth (2);
	setIsDirectionIntoPlane(true);
	dockWidget = new HomoBFieldWidget(0,0,this);
	setFluxDensity(2e-5);
}

HomoBFieldItem::~HomoBFieldItem() {
}

QList<QVariant> HomoBFieldItem::getSaveDataList() {
	QList<QVariant> list;
	list << type();
	list << pos();
	list << getRectF();
	list << zValue();
	list << getIsDirectionIntoPlane();
	list << getFluxDensity();
	return list;
}
void HomoBFieldItem::setSaveDataList(QList<QVariant> list){
	setPos(list[1].toPointF());
	setRectF(list[2].toRectF());
	setZValue(list[3].toDouble());
	setIsDirectionIntoPlane(list[4].toBool());
	setFluxDensity(list[5].toDouble());
}


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

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

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

	const int x_arrow_width = 8; //laenge und breite des Pfeils
	const int circle_arrow_width = 12; //laenge u. breite des kreispfeiles

	int arrow_width;
	if (getIsDirectionIntoPlane()) arrow_width = x_arrow_width;
	else arrow_width = circle_arrow_width; //arrow width an das jeweilige anpassen
	for (int y = 1; y <= floor(sizeRect.height()/(qreal)(getFieldLineDistance()+arrow_width)); ++y ) {
		//unten ueberstehen verhindern
		if (-abs(sizeRect.y()) + y*getFieldLineDistance() + y*arrow_width >= sizeRect.height()-4 ) break;
		for (int x = 1; x <= floor(sizeRect.width()/(qreal)(getFieldLineDistance()+arrow_width)); ++x ) {
			//rechts ueberstehen verhindern
			if (-abs(sizeRect.x()) + x*getFieldLineDistance() + x*arrow_width >= sizeRect.width()-4 ) break; 
			if (getIsDirectionIntoPlane()) {
				painter->drawLine(
					sizeRect.x() + x*getFieldLineDistance() + (x-1)*arrow_width,
					sizeRect.y() + y*getFieldLineDistance() + (y-1)*arrow_width,
					sizeRect.x() + x*getFieldLineDistance() + arrow_width + (x-1)*arrow_width,
					sizeRect.y() + y*getFieldLineDistance() + arrow_width + (y-1)*arrow_width
				);
				painter->drawLine(
					sizeRect.x() + x*getFieldLineDistance() + arrow_width + (x-1)*arrow_width,
					sizeRect.y() + y*getFieldLineDistance() + (y-1)*arrow_width,
					sizeRect.x() + x*getFieldLineDistance() + (x-1)*arrow_width,
					sizeRect.y() + y*getFieldLineDistance() + arrow_width + (y-1)*arrow_width 
				);
			}
			else {
				painter->drawEllipse(
					sizeRect.x() + x*getFieldLineDistance() + (x-1)*arrow_width,
					sizeRect.y() + y*getFieldLineDistance() + (y-1)*arrow_width,
					arrow_width,
					arrow_width
				);
				painter->setBrush(linecolor);
				painter->drawEllipse(
					QPoint(
						sizeRect.x() + x*getFieldLineDistance() + (x-1)*arrow_width + arrow_width/2.0,
						sizeRect.y() + y*getFieldLineDistance() + (y-1)*arrow_width + arrow_width/2.0
					),
					1,
					1			
				);
				painter->setBrush(Qt::NoBrush);
			}
		}
	}


	/*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 HomoBFieldItem::type() const {
	return Type;
}

int HomoBFieldItem::getFieldLineDistance() const {
	return fieldLineDistance;
}


void HomoBFieldItem::setFieldLineDistance ( int theValue ) {
	if(fieldLineDistance == theValue) return;
	fieldLineDistance = theValue;
}


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


void HomoBFieldItem::setOuterPenWidth ( double theValue ) {
	outerPenWidth = theValue;
}

QWidget* HomoBFieldItem::getDockWidget() const {
	return dockWidget;
}


bool HomoBFieldItem::getIsDirectionIntoPlane() const {
	return isDirectionIntoPlane;
}


void HomoBFieldItem::setIsDirectionIntoPlane ( bool theValue ) {
	if(isDirectionIntoPlane == theValue) return;
	prepareGeometryChange();
	isDirectionIntoPlane = theValue;
	emit directionChanged(theValue);
}


double HomoBFieldItem::getFluxDensity() const {
	return fluxDensity;
}


void HomoBFieldItem::setFluxDensity ( double theValue ) {
	if(fluxDensity == theValue) return;
	fluxDensity = theValue;
	emit fluxDensityChanged(theValue);
}



/*!
    \fn HomoBFieldItem::wheelEvent ( QGraphicsSceneWheelEvent * event )
 */
void HomoBFieldItem::wheelEvent ( QGraphicsSceneWheelEvent * event )
{
    event->ignore();
}

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