/*
Copyright (C) 2006 - 2009 Evan Teran
                   eteran@alum.rit.edu

Copyright (C) 2009 Arvin Schnell
                   aschnell@suse.de

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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


#include "QY2Edge.h"
#include "GraphWidget.h"
#include <QPainter>

//------------------------------------------------------------------------------
// Name:
// Desc:
//------------------------------------------------------------------------------
QY2Edge::QY2Edge(const GraphWidget *graph, const bezier &bz, const QColor &color) : QGraphicsPathItem(makeBezier(bz, graph)) {
	QPainter painter(&picture);

	if (bz.sflag) {
		drawArrow(QLineF(graph->gToQ(bz.list[0]), graph->gToQ(bz.sp)), color, &painter);
	}

	if (bz.eflag) {
		drawArrow(QLineF(graph->gToQ(bz.list[bz.size-1]), graph->gToQ(bz.ep)), color, &painter);
	}
}

//------------------------------------------------------------------------------
// Name:
// Desc:
//------------------------------------------------------------------------------
QRectF QY2Edge::boundingRect() const {
	return QGraphicsPathItem::boundingRect().united(picture.boundingRect());
}

//------------------------------------------------------------------------------
// Name:
// Desc:
//------------------------------------------------------------------------------
void QY2Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
	painter->save();
	QGraphicsPathItem::paint(painter, option, widget);
	painter->restore();
	picture.play(painter);
}

//------------------------------------------------------------------------------
// Name:
// Desc:
//------------------------------------------------------------------------------
QPainterPath QY2Edge::makeBezier(const bezier &bezier, const GraphWidget *graph) const {
	QPainterPath path;
	path.moveTo(graph->gToQ(bezier.list[0]));
	for (int i = 1; i < bezier.size - 1; i += 3)
		path.cubicTo(
			graph->gToQ(bezier.list[i+0]),
			graph->gToQ(bezier.list[i+1]),
			graph->gToQ(bezier.list[i+2]));
	
	return path;
}

//------------------------------------------------------------------------------
// Name:
// Desc:
//------------------------------------------------------------------------------
void QY2Edge::drawArrow(const QLineF &line, const QColor &color, QPainter *painter) const {
	QLineF n(line.normalVector());
	QPointF o(n.dx() / 3.0, n.dy() / 3.0);

	QPolygonF polygon;
	polygon.append(line.p1() + o);
	polygon.append(line.p2());
	polygon.append(line.p1() - o);

	QPen pen(color);
	pen.setWidthF(1.0);
	painter->setPen(pen);

	QBrush brush(color);
	painter->setBrush(brush);

	painter->drawPolygon(polygon);
}
