Commit 93e7f124 authored by Leander Schulten's avatar Leander Schulten
Browse files

FFT Graph: Only update the view when visible to the user. Cleaned up the Graph class

parent 7ee263f4
Pipeline #188355 passed with stage
in 4 minutes and 42 seconds
......@@ -3,105 +3,82 @@
#include <QSGGeometryNode>
#include <cmath>
namespace GUI{
namespace GUI {
Graph::Graph():haveNewData(false)
{
setFlag(ItemHasContents);
lastCreated = this;
Graph *Graph::lastCreated = nullptr;
Graph::Graph(QQuickItem *parent) : QQuickItem(parent), haveNewData(false) {
setFlag(ItemHasContents);
lastCreated = this;
startTimer(15);
}
Graph * Graph::lastCreated = nullptr;
Graph::~Graph() {
if (lastCreated == this) {
lastCreated = nullptr;
}
}
float mins = 1;
int counter = 1;
void Graph::showData(float *data, int size) {
setImplicitWidth(size * 2);
this->data.assign(data, data + size);
haveNewData.store(true);
}
QSGNode * Graph::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *){
void Graph::setLineColor(const QColor &c) {
lineColor = c;
update();
lineColorChanged();
}
QSGNode *Graph::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *transformNode) {
Q_UNUSED(transformNode)
QSGNode *node = nullptr;
QSGGeometryNode *lineNode = nullptr;
QSGGeometry *lineGeometry = nullptr;
if (!oldNode) {
node = new QSGNode;
{
lineNode = new QSGGeometryNode;
lineGeometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), size);
lineGeometry->setLineWidth(1);
lineGeometry->setDrawingMode(QSGGeometry::DrawLineStrip);
lineNode->setGeometry(lineGeometry);
lineNode->setFlag(QSGNode::OwnsGeometry);
QSGFlatColorMaterial *material = new QSGFlatColorMaterial;
material->setColor(lineColor);
lineNode->setMaterial(material);
lineNode->setFlag(QSGNode::OwnsMaterial);
}
/*{
pointNode = new QSGGeometryNode;
pointGeometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4);
pointGeometry->setLineWidth(1);
pointGeometry->setDrawingMode(QSGGeometry::DrawTriangleStrip);
pointNode->setGeometry(pointGeometry);
pointNode->setFlag(QSGNode::OwnsGeometry);
QSGFlatColorMaterial * material = new QSGFlatColorMaterial;
material->setColor(backgroundColor);
pointNode->setMaterial(material);
pointNode->setFlag(QSGNode::OwnsMaterial);
}
node->appendChildNode(pointNode);*/
node = new QSGNode;
lineNode = new QSGGeometryNode;
lineGeometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), static_cast<int>(data.size()));
lineGeometry->setLineWidth(1);
lineGeometry->setDrawingMode(QSGGeometry::DrawLineStrip);
lineNode->setGeometry(lineGeometry);
lineNode->setFlag(QSGNode::OwnsGeometry);
auto *material = new QSGFlatColorMaterial;
material->setColor(lineColor);
lineNode->setMaterial(material);
lineNode->setFlag(QSGNode::OwnsMaterial);
node->appendChildNode(lineNode);
} else {
node = static_cast<QSGNode *>(oldNode);
node = oldNode;
lineNode = static_cast<QSGGeometryNode *>(node->childAtIndex(0));
lineGeometry = lineNode->geometry();
lineGeometry->allocate(size);
static_cast<QSGFlatColorMaterial*>(lineNode->material())->setColor(lineColor);
/*pointNode = static_cast<QSGGeometryNode *>(node->childAtIndex(0));
pointGeometry = pointNode->geometry();
pointGeometry->allocate(4);
static_cast<QSGFlatColorMaterial*>(pointNode->material())->setColor(backgroundColor);*/
lineGeometry->allocate(static_cast<int>(data.size()));
static_cast<QSGFlatColorMaterial *>(lineNode->material())->setColor(lineColor);
}
QSGGeometry::Point2D *vertices = lineGeometry->vertexDataAsPoint2D();
/*if(haveNewData.load()){
float min = 999999;
float max = 0;
for(int i = 0 ; i< size; ++i){
data[i] = (std::log10(data[i]));
min = std::min(min,data[i]);
max = std::max(max,data[i]);
}
for(int i = 0 ; i< size; ++i){
data[i]-=min;
data[i]/=(max-min);
data[i] = std::pow(data[i],2);
}
if(min > 0)
mins += min;
++counter;
}
float scale = 0.1;
float curMin = mins/counter;
qDebug() << ' '<<mins << ' ' << curMin <<' '<<counter;
*/
for(int i = 0 ; i< size; ++i){
vertices->x = i*2;
//vertices->y = height() - 50 - 20* (std::log10(data[i]));
vertices->y = height() - 2*data[i];
for (int i = 0; i < static_cast<int>(data.size()); ++i) {
vertices->x = i * 2;
vertices->y = height() - 2 * data[i];
++vertices;
}
haveNewData = false;
lineNode->markDirty(QSGNode::DirtyGeometry);
return node;
}
void Graph::timerEvent(QTimerEvent *event) {
Q_UNUSED(event)
if (visibleForUser && haveNewData) {
update();
}
}
} // namespace GUI
......@@ -2,39 +2,40 @@
#define GRAPH_H
#include <QQuickItem>
#include <atomic>
#include <utility>
#include <vector>
namespace GUI{
namespace GUI {
class Graph : public QQuickItem
{
class Graph : public QQuickItem {
Q_OBJECT
float data[2048];
int size = 0;
QColor lineColor = QColor(0,0,0);
std::vector<float> data;
QColor lineColor = QColor(0, 0, 0);
Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor NOTIFY lineColorChanged)
static Graph * lastCreated;
Q_PROPERTY(bool visibleForUser MEMBER visibleForUser NOTIFY visibleForUserChanged)
bool visibleForUser = true;
static Graph *lastCreated;
std::atomic_bool haveNewData;
public:
Graph();
~Graph(){
if(lastCreated==this)
lastCreated = nullptr;
}
static Graph * getLast(){return lastCreated;}
void showData(float* data, int size){
setImplicitWidth(size * 2);
this->size = std::min(2048,size);
memcpy(this->data,data,this->size*sizeof (float));
haveNewData.store(true);
}
void setLineColor(QColor c){lineColor=c;update();lineColorChanged();}
QColor getLineColor(){return lineColor;}
explicit Graph(QQuickItem *parent = nullptr);
~Graph() override;
Q_DISABLE_COPY_MOVE(Graph)
static Graph *getLast() { return lastCreated; }
void showData(float *data, int size);
void setLineColor(const QColor &c);
QColor getLineColor() { return lineColor; }
protected:
virtual QSGNode * updatePaintNode(QSGNode *, UpdatePaintNodeData *)override;
QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *transformNode) override;
void timerEvent(QTimerEvent *event) override;
signals:
void lineColorChanged();
public slots:
void lineColorChanged();
void visibleForUserChanged();
};
} // namespace GUI
......
......@@ -329,11 +329,8 @@ int main(int argc, char *argv[]) {
QTimer timer;
timer.setInterval(15);
QObject::connect(&timer,&QTimer::timeout,[&](){
if(Audio::AudioCaptureManager::get().isCapturing()){
if(Graph::getLast()) {
Graph::getLast()->update();
}
QObject::connect(&timer, &QTimer::timeout, [&]() {
if (Audio::AudioCaptureManager::get().isCapturing()) {
if(Oscillogram::getLast()) {
Oscillogram::getLast()->update();
}
......
......@@ -4,6 +4,7 @@ import QtQuick.Controls 2.5
import custom.licht 1.0
Item {
property alias visibleForUser: graph.visibleForUser
Label{
id: label
anchors.top: parent.top
......
......@@ -232,7 +232,9 @@ ApplicationWindow {
onMoveToOwnWindow: ledWindow.moveToWindow(SwipeView.index);
}
FFTGraphView{}
FFTGraphView{
visibleForUser: SwipeView.isCurrentItem
}
Oscillogram{
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment