/******************************************************************************
 *   Copyright (C) 2009 by Oliver Bock                                        *
 *   oliver.bock[AT]aei.mpg.de                                                *
 *                                                                            *
 *   This file is part of PulsatingScience.                                   *
 *                                                                            *
 *   PulsatingScience 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, version 3 of the License.               *
 *                                                                            *
 *   PulsatingScience 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 PulsatingScience. If not, see <http://www.gnu.org/licenses/>. *
 *                                                                            *
 ******************************************************************************/

#include "pulsescopewidget.h"

PulseScopeWidget::PulseScopeWidget(QWidget *parent) : QGraphicsView(parent),
	m_scene(),
	m_data()
{
	setViewport(new QGLWidget(QGLFormat(QGL::AlphaChannel | QGL::SampleBuffers)));
	QGLWidget *glScope = (QGLWidget*) viewport();
	if(!glScope->format().directRendering()) {
		qWarning("Sorry, no direct rendering support for pulse scope...");
	}
	if(!glScope->format().doubleBuffer()) {
		qWarning("Sorry, no double buffering support for pulse scope...");
	}
	if(!glScope->format().rgba()) {
		qWarning("Sorry, no RGBA support for pulse scope...");
	}
	if(!glScope->format().alpha()) {
		qWarning("Sorry, no alpha channel support for pulse scope...");
	}
	if(!glScope->format().sampleBuffers()) {
		qWarning("Sorry, no multisampling support for pulse scope...");
	}

	m_scopeSizeH = 360;
	m_scopeSizeV = 1;

	setScene(&m_scene);

	m_path = NULL;
	m_marker = new QGraphicsLineItem();
	m_marker->setPen(QPen(Qt::red));
	m_marker->setZValue(1);

	m_data.fill(0.0f, m_scopeSizeH);
	drawCurve(m_data);
}

PulseScopeWidget::~PulseScopeWidget()
{

}

void PulseScopeWidget::resizeEvent(QResizeEvent *event)
{
	Q_UNUSED(event);

	fitInView(m_scene.itemsBoundingRect(), Qt::IgnoreAspectRatio);
}

void PulseScopeWidget::drawCurve(const QVector<float>& vector)
{
	m_data = vector;

	if(m_marker->scene() == 0) {
		m_marker->setLine(0.0, 0.0, 0.0, m_scopeSizeV);
		m_scene.addItem(m_marker);
	}

	QPainterPath pulsePath(QPoint(0.0f, 1.0f));
	for(int i = 0; i < m_scopeSizeH; ++i) {
		pulsePath.lineTo(i, 1-m_data[i]);
	}
	if(m_path == NULL) {
		m_path = m_scene.addPath(pulsePath, QPen(Qt::yellow));
	}
	else {
		m_path->setPath(pulsePath);
	}

	fitInView(m_scene.itemsBoundingRect(), Qt::IgnoreAspectRatio);
}

void PulseScopeWidget::markerStep(float stepSize) {
	if(m_marker->line().x1() < m_scopeSizeH) {
		QLineF line = m_marker->line();
		line.translate(stepSize, 0);
		m_marker->setLine(line);
	}
	else {
		markerReset();
	}
}

void PulseScopeWidget::markerReset() {
	m_marker->setLine(0.0, 0.0, 0.0, m_scopeSizeV);
}