/***************************************************************************
 *   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 "stopperitem.h"
#include <QWidget>
#include <QPainter>
#include <cmath>
#include <QPointF>
#include <QColor>
#include "stopperwidget.h"


StopperItem::StopperItem(QPointF start, QPointF end): FieldItem() {
	setLine(QLineF(start,end));
	stopperWidget = new StopperWidget(0,0,this);
	setOuterPenWidth (0);
	setFlag(ItemIsMovable);
	setFlag(ItemIsSelectable);
	setFlag(ItemIsFocusable);
}

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

StopperItem::~StopperItem() {

}

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

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


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

void StopperItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
	QColor linecolor(172,214,255);
	painter->setPen(linecolor);
	if (isSelected()) {
		painter->setBrush(Qt::Dense6Pattern); //selection deutlich machen
		painter->drawRect(sizeRect);
	}

	linecolor.setAlpha(188);
	painter->setBrush(QBrush(linecolor));
	
	painter->drawRect(sizeRect);
}

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

QWidget* StopperItem::getDockWidget() const {
	return stopperWidget;
}

void StopperItem::setLine(QLineF line) {
	setRectF(QRectF(line.p1(), QPointF(line.p2().x(),line.p2().y()+Height)));
} 