Files
templates/cpp/qcustomplot-renderer/main.cpp
T
Andrey e93bd3b6aa style: унифицируй форматирование C и C++
Закрепи табуляцию и размещение скобок в настройках проекта. Примени единый стиль к собственным исходникам.
2026-12-02 11:33:41 +03:00

77 lines
2.3 KiB
C++

#include "qcustomplot.h"
#include <QApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QBuffer>
#include <cstdio>
#include <iostream>
#include <QHash>
#include <fcntl.h>
#include <io.h>
int main(int argc, char **argv)
{
_setmode(_fileno(stdout), _O_BINARY);
QApplication app(argc, argv);
QCustomPlot plot;
plot.setNotAntialiasedElements(QCP::aeAll);
plot.setBackground(Qt::transparent);
plot.axisRect()->setBackground(Qt::transparent);
plot.axisRect()->setAutoMargins(QCP::msNone);
plot.axisRect()->setMargins(QMargins(0, 0, 0, 0));
plot.plotLayout()->setMargins(QMargins(0, 0, 0, 0));
for (auto axis : plot.axisRect()->axes())
{
axis->setVisible(false);
axis->grid()->setVisible(false);
}
auto graph = plot.addGraph();
graph->setAdaptiveSampling(true);
QHash<QString, QSharedPointer<QCPGraphDataContainer>> records;
std::string line;
while (std::getline(std::cin, line))
{
auto o = QJsonDocument::fromJson(QByteArray::fromStdString(line)).object();
auto path = o["file"].toString();
if (!records.contains(path))
{
QFile f(path);
if (!f.open(QIODevice::ReadOnly))
return 2;
int n = o["count"].toInt();
QByteArray bytes = f.readAll();
if (bytes.size() != qint64(n) * 16)
return 3;
QVector<double> x(n), y(n);
memcpy(x.data(), bytes.constData(), n * 8);
memcpy(y.data(), bytes.constData() + n * 8, n * 8);
auto data = QSharedPointer<QCPGraphDataContainer>::create();
QVector<QCPGraphData> points(n);
for (int i = 0; i < n; i++)
points[i] = QCPGraphData(x[i], y[i]);
data->set(points, false);
records.insert(path, data);
if (records.size() > 4)
{
auto old = records.begin();
if (old.key() == path)
++old;
records.erase(old);
}
}
graph->setData(records[path]);
graph->setPen(QPen(QColor(o["color"].toString()), 1));
plot.xAxis->setRange(o["left"].toDouble(), o["right"].toDouble());
plot.yAxis->setRange(o["bottom"].toDouble(), o["top"].toDouble());
int w = o["width"].toInt(), h = o["height"].toInt();
plot.resize(w, h);
QByteArray png;
QBuffer buf(&png);
buf.open(QIODevice::WriteOnly);
plot.toPixmap(w, h, 1.).save(&buf, "PNG");
std::cout << png.size() << "\n";
std::cout.write(png.constData(), png.size());
std::cout.flush();
}
return 0;
}