2019-08-25 18:22:31 +02:00
|
|
|
#include "osciwidget.h"
|
|
|
|
|
|
2019-09-07 23:00:37 +02:00
|
|
|
// system includes
|
2019-08-25 18:22:31 +02:00
|
|
|
#include <cmath>
|
|
|
|
|
|
2019-09-07 23:00:37 +02:00
|
|
|
// Qt includes
|
2019-09-05 20:46:54 +02:00
|
|
|
#include <QLineF>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QTimerEvent>
|
2019-08-25 18:22:31 +02:00
|
|
|
|
2019-08-25 22:42:34 +02:00
|
|
|
OsciWidget::OsciWidget(QWidget *parent) :
|
2019-09-07 12:00:14 +02:00
|
|
|
QOpenGLWidget{parent},
|
2019-09-05 20:46:54 +02:00
|
|
|
m_redrawTimerId(startTimer(1000/m_fps))
|
2019-08-25 22:42:34 +02:00
|
|
|
{
|
2019-09-07 19:19:24 +02:00
|
|
|
m_statsTimer.start();
|
2019-08-25 22:42:34 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-07 20:01:43 +02:00
|
|
|
int OsciWidget::lightspeed() const
|
|
|
|
|
{
|
|
|
|
|
return std::cbrt(m_lightspeed) * 20.f;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 20:46:54 +02:00
|
|
|
void OsciWidget::setFps(int fps)
|
|
|
|
|
{
|
|
|
|
|
killTimer(m_redrawTimerId);
|
2019-09-07 12:00:14 +02:00
|
|
|
|
2019-09-05 20:46:54 +02:00
|
|
|
m_fps = fps;
|
2019-09-07 12:00:14 +02:00
|
|
|
|
2019-09-05 20:46:54 +02:00
|
|
|
m_redrawTimerId = startTimer(1000/m_fps);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-07 20:01:43 +02:00
|
|
|
void OsciWidget::setLightspeed(int lightspeed) {
|
|
|
|
|
const auto temp = (float(lightspeed)/20.f);
|
|
|
|
|
m_lightspeed = temp*temp*temp;
|
|
|
|
|
qDebug() << m_lightspeed;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 22:42:34 +02:00
|
|
|
void OsciWidget::renderSamples(const SamplePair *begin, const SamplePair *end)
|
2019-08-25 18:22:31 +02:00
|
|
|
{
|
2019-09-07 12:00:14 +02:00
|
|
|
m_callbacksCounter++;
|
2019-08-25 18:22:31 +02:00
|
|
|
|
2019-09-17 20:44:28 +02:00
|
|
|
m_samplesCounter += std::distance(begin, end);
|
|
|
|
|
|
2019-09-07 12:00:14 +02:00
|
|
|
m_buffer.insert(m_buffer.end(), begin, end);
|
2019-08-25 18:22:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OsciWidget::paintEvent(QPaintEvent *event)
|
|
|
|
|
{
|
2019-09-05 20:46:54 +02:00
|
|
|
QWidget::paintEvent(event);
|
|
|
|
|
|
|
|
|
|
m_frameCounter++;
|
2019-09-07 19:19:24 +02:00
|
|
|
if (m_statsTimer.hasExpired(1000))
|
2019-09-05 20:46:54 +02:00
|
|
|
{
|
2019-09-17 20:44:28 +02:00
|
|
|
emit statusUpdate(QString("%0FPS (%1 callbacks, %2 samples, %3 avg per callback)").arg(m_frameCounter).arg(m_callbacksCounter).arg(m_samplesCounter).arg(m_callbacksCounter>0?m_samplesCounter/m_callbacksCounter:0));
|
2019-09-05 20:46:54 +02:00
|
|
|
m_frameCounter = 0;
|
2019-09-07 12:00:14 +02:00
|
|
|
m_callbacksCounter = 0;
|
2019-09-17 20:44:28 +02:00
|
|
|
m_samplesCounter = 0;
|
2019-09-07 19:19:24 +02:00
|
|
|
m_statsTimer.restart();
|
2019-09-05 20:46:54 +02:00
|
|
|
}
|
2019-08-25 18:22:31 +02:00
|
|
|
|
2019-09-07 19:19:24 +02:00
|
|
|
if (m_pixmap.size() != size())
|
2019-09-17 20:44:28 +02:00
|
|
|
{
|
2019-09-07 19:19:24 +02:00
|
|
|
m_pixmap = QPixmap(size());
|
2019-09-17 20:44:28 +02:00
|
|
|
m_pixmap.fill(Qt::black);
|
|
|
|
|
}
|
2019-09-05 20:46:54 +02:00
|
|
|
|
2019-09-07 19:19:24 +02:00
|
|
|
QPainter painter;
|
|
|
|
|
painter.begin(&m_pixmap);
|
|
|
|
|
|
|
|
|
|
// darkening last frame
|
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_Multiply);
|
|
|
|
|
painter.setPen({});
|
2019-09-07 20:01:43 +02:00
|
|
|
painter.setBrush(QColor(m_afterglow, m_afterglow, m_afterglow));
|
2019-09-07 19:19:24 +02:00
|
|
|
painter.drawRect(m_pixmap.rect());
|
2019-09-07 12:00:14 +02:00
|
|
|
|
|
|
|
|
// drawing new lines ontop
|
|
|
|
|
QPen pen;
|
|
|
|
|
pen.setWidth(2);
|
|
|
|
|
pen.setColor(QColor(0, 255, 0));
|
|
|
|
|
painter.setPen(pen);
|
2019-09-07 19:19:24 +02:00
|
|
|
painter.translate(m_pixmap.width()/2, m_pixmap.height()/2);
|
2019-09-07 12:00:14 +02:00
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_Plus);
|
|
|
|
|
|
2019-09-07 19:19:24 +02:00
|
|
|
const auto pointToCoordinates = [width=m_pixmap.width()/2,height=m_pixmap.height()/2,factor=m_factor](const QPointF &point)
|
2019-09-07 12:00:14 +02:00
|
|
|
{
|
|
|
|
|
return QPoint{
|
|
|
|
|
int(point.x() * factor * width),
|
|
|
|
|
int(point.y() * factor * height)
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (const auto &i : m_buffer)
|
|
|
|
|
{
|
|
|
|
|
const QPointF p{
|
|
|
|
|
float(i.first) / std::numeric_limits<qint16>::max(),
|
|
|
|
|
float(-i.second) / std::numeric_limits<qint16>::max()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const QLineF line(m_lastPoint, p);
|
|
|
|
|
|
2019-09-07 20:01:43 +02:00
|
|
|
painter.setOpacity(std::min(1.0, 1. / ((line.length() * m_lightspeed) + 1)));
|
2019-09-07 12:00:14 +02:00
|
|
|
|
|
|
|
|
painter.drawLine(pointToCoordinates(m_lastPoint), pointToCoordinates(p));
|
|
|
|
|
|
|
|
|
|
m_lastPoint = p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
painter.setOpacity(1);
|
2019-09-07 19:19:24 +02:00
|
|
|
painter.resetTransform();
|
2019-09-05 20:46:54 +02:00
|
|
|
|
2019-08-25 18:22:31 +02:00
|
|
|
painter.end();
|
2019-09-07 19:19:24 +02:00
|
|
|
|
|
|
|
|
painter.begin(this);
|
|
|
|
|
painter.drawPixmap(0, 0, m_pixmap);
|
|
|
|
|
painter.end();
|
|
|
|
|
|
|
|
|
|
m_buffer.clear();
|
2019-09-05 20:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OsciWidget::timerEvent(QTimerEvent *event)
|
|
|
|
|
{
|
|
|
|
|
QWidget::timerEvent(event);
|
|
|
|
|
if (event->timerId() == m_redrawTimerId)
|
|
|
|
|
repaint();
|
2019-08-25 18:22:31 +02:00
|
|
|
}
|