EnergySpectrumAnalyer/src/MeasureAnalysisParticleCountPlotView.cpp

101 lines
3.3 KiB
C++
Raw Normal View History

#include "MeasureAnalysisParticleCountPlotView.h"
#include "CustomQwtPlot.h"
#include "csv.h"
#include <GlobalDefine.h>
#include <QHBoxLayout>
#include <QPen>
#include <QRegularExpression>
#include <QVector>
#include <QwtLegend>
#include <QwtPlotCanvas>
#include <QwtPlotCurve>
MeasureAnalysisParticleCountPlotView::MeasureAnalysisParticleCountPlotView(QWidget* parent)
: MeasureAnalysisView { parent }
{
this->setViewType(PlotFrame);
QHBoxLayout* layout = new QHBoxLayout(this);
_plot = new CustomQwtPlot(this);
layout->addWidget(_plot);
setupPlot();
}
void MeasureAnalysisParticleCountPlotView::SetAnalyzeDataFilename(const QMap<QString, QVariant>& data_files_set)
{
auto extractNumber = [](const QString& str) {
int ret_num = 0;
QRegularExpression regex("\\d+");
QRegularExpressionMatch match = regex.match(str);
if (match.hasMatch()) {
ret_num = match.captured().toInt();
}
return ret_num;
};
QStringList ch_count_data_name = data_files_set.keys();
std::sort(ch_count_data_name.begin(), ch_count_data_name.end(), [extractNumber](const QString& a, const QString& b) {
int num_a = extractNumber(a);
int num_b = extractNumber(b);
return num_a < num_b;
});
for (const QString& ch_count_data_name : ch_count_data_name) {
if (ch_count_data_name.contains(ch_count_data_name)) {
const QString ch_count_data_filename = data_files_set[ch_count_data_name].toString();
loadDataFromFile(ch_count_data_name, ch_count_data_filename);
}
}
}
void MeasureAnalysisParticleCountPlotView::setupPlot()
{
_plot->setTitle(QString(QStringLiteral(u"粒子计数谱")));
_plot->setCanvasBackground(Qt::white);
QwtPlotCanvas* canvas = qobject_cast<QwtPlotCanvas*>(_plot->canvas());
canvas->setFrameStyle(QFrame::NoFrame);
_plot->setAxisTitle(QwtPlot::xBottom, QString(QStringLiteral(u"道址")));
_plot->setAxisTitle(QwtPlot::yLeft, QString(QStringLiteral(u"计数")));
// set axis auto scale
_plot->setAxisAutoScale(QwtPlot::xBottom, true);
_plot->setAxisAutoScale(QwtPlot::yLeft, true);
// 启用网格线
_plot->enableAxis(QwtPlot::xBottom);
_plot->enableAxis(QwtPlot::yLeft);
// 设置QWT图例
QwtLegend* legend = new QwtLegend();
legend->setDefaultItemMode(QwtLegendData::Checkable);
_plot->insertLegend(legend, QwtPlot::RightLegend);
}
void MeasureAnalysisParticleCountPlotView::loadDataFromFile(const QString& data_name, const QString& filename)
{
std::string address_str = QString(QStringLiteral(u"道址")).toStdString();
std::string count_str = QString(QStringLiteral(u"计数")).toStdString();
io::CSVReader<
2,
io::trim_chars<' ', '\t'>,
io::double_quote_escape<',', '"'>,
io::throw_on_overflow,
io::empty_line_comment>
reader(QStrToSysPath(filename));
reader.read_header(io::ignore_extra_column, address_str, count_str);
int address;
int particle_count;
QVector<float> x, y;
while (reader.read_row(address, particle_count)) {
x.push_back(address);
y.push_back(particle_count);
}
// 绘制曲线
QwtPlotCurve* curve = new QwtPlotCurve(data_name);
curve->setSamples(x, y);
_plot->AddCurve(curve);
LOG_DEBUG(data_name);
}