pulsescopewidget.cpp 3.19 KB
Newer Older
Oliver Bock's avatar
Oliver Bock committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/******************************************************************************
 *   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(),
	m_marker()
{
	m_scopeSizeH = 360;
	m_scopeSizeV = 0;

	setViewport(new QGLWidget(QGLFormat(QGL::AlphaChannel | QGL::SampleBuffers)));
	setScene(&m_scene);
	m_data.fill(0.0f, m_scopeSizeH);
	m_marker.setPen(QPen(Qt::red));

	// TODO: proof of concept only!
	for(int i = 0; i < m_scopeSizeH; ++i) {
		m_data[i] = pow(i-180,2);
	}

	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;

	// TODO: proof of concept only!

	QVector<float> checkMax(m_data);
	qSort(checkMax.begin(), checkMax.end());
	m_scopeSizeV = (int)ceil(checkMax.last()) * 2;

	m_scene.addLine(0.0f, 0.0f, 0.0f, m_scopeSizeV, QPen(Qt::white));
	m_scene.addLine(0.0f, m_scopeSizeV / 2, m_scopeSizeH, m_scopeSizeV / 2, QPen(Qt::white));

	QPainterPath pulsePath(QPoint(0.0f, m_scopeSizeV / 2));
	for(int i = 0; i < m_scopeSizeH; ++i) {
		pulsePath.lineTo(i,m_data[i]);
	}
	m_scene.addPath(pulsePath, QPen(Qt::green));

75 76 77 78 79
	if(m_marker.scene() == 0) {
		m_marker.setLine(0.0, 0.0, 0.0, m_scopeSizeV);
		m_scene.addItem(&m_marker);
	}

Oliver Bock's avatar
Oliver Bock committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	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);
}