#include "readsonicpoint.h"
#include "marinebase.h"
#include "QMessageBox"
#include "qfiledialog.h"
#include "qwt_legend.h"
#include "qwt_plot_picker.h"
#include "qwt_picker.h"
#include "qwt_picker_machine.h"
#include "qwt_symbol.h"
ReadSonicPoint::ReadSonicPoint(QWidget *parent, QString filepath)
: QDialog(parent)
{
ui.setupUi(this);
QList<QString> csv_data_list;
QVector<QPointF> point_list;
QVector<QPointF> tempPoint_list;
csv_data_list = readSonicPointFile(filepath);
point_list = readStringListData(csv_data_list, 0);
tempPoint_list = readStringListData(csv_data_list, 1);
QVector<QPointF>sortPointList = sortSonicPoint(point_list);
QVector<QPointF>sortTempPointList = sortSonicPoint(tempPoint_list);
setEnableAllAxis(true, true, true, false);
setQwtPlotTitle("速度(m/s)","深度(m)","温度(℃)");
drawQwtPlotInputData(sortPointList, "声速/深度", 0);
drawQwtPlotInputData(sortTempPointList, "温度/深度", 1);
useQwtPlotPicker();
ui.qwtPlot_Sonic->insertLegend(new QwtLegend(), QwtPlot::BottomLegend); //添加图例
}
QVector<QPointF> ReadSonicPoint::sortSonicPoint(QVector<QPointF> befPointVector) //把x轴的值从小到大排序
{
QVector<QPointF> modPointVector;
QMap<double, double>sonicMap; //利用QMap自动排序
for (int i = 0; i < befPointVector.size(); i++)
{
sonicMap.insert(befPointVector[i].x(), befPointVector[i].y());
}
QMap<double, double>::Iterator it = sonicMap.begin();
while (it!=sonicMap.end())
{
QPointF sonicPoint;
sonicPoint.setX(it.key());
sonicPoint.setY(it.value());
modPointVector.append(sonicPoint);
it++;
}
return modPointVector;
}
QVector<QPointF> ReadSonicPoint::readStringListData(QList<QString> sonic_data_list, int flag)
{
QVector<QPointF> sonic_point_list;
QString sonic_data;
QList<QString> sonicpointdata;
//flag = 0:声速/深度 flag = 1:温度/深度
for (int i = 0; i < sonic_data_list.size(); i++)
{
sonic_data = sonic_data_list[i];
sonicpointdata = sonic_data.split(",");
if (flag == 0)
{
QPointF point;
point.setX(sonicpointdata[0].toDouble());
point.setY(sonicpointdata[1].toDouble());
sonic_point_list.append(point);
}
else if (flag == 1)
{
QPointF point;
point.setX(sonicpointdata[0].toDouble());
point.setY(sonicpointdata[2].toDouble());
sonic_point_list.append(point);
}
}
return sonic_point_list;
}
QList<QString> ReadSonicPoint::readSonicPointFile(QString csv_path) //读取声速文件
{
QFile csvFile(csv_path);
QList<QString> sonic_data_list;
QString sonic_data;
if (csvFile.open(QIODevice::ReadWrite))
{
QTextStream stream(&csvFile);
int i = 0;
while (!stream.atEnd())
{
i = i + 1;
sonic_data = stream.readLine();
if (i > 2)
{
sonic_data_list.append(sonic_data);
}
}
csvFile.close();
}
return sonic_data_list;
}
void ReadSonicPoint::setQwtPlotTitle(QString yLeftName, QString xBottomName, QString yRightName)
{
ui.qwtPlot_Sonic->setAxisTitle(QwtPlot::yLeft, yLeftName);
ui.qwtPlot_Sonic->setAxisTitle(QwtPlot::xBottom, xBottomName);
ui.qwtPlot_Sonic->setAxisTitle(QwtPlot::yRight, yRightName);
}
//params:vector:数据 lineTitle:底下的注记 flag :y轴以哪边为准
void ReadSonicPoint::drawQwtPlotInputData(QVector<QPointF> vector, QString lineTitle, int flag)
{
QwtPointSeriesData* series = new QwtPointSeriesData(vector);
QwtPlotCurve* curve1 = new QwtPlotCurve(lineTitle); //设置line名称
if (flag == 0) //flag:0---xBottom,yLeft;
{
//样本点
QwtSymbol *symbol = new QwtSymbol(QwtSymbol::Ellipse,
QBrush(Qt::yellow), QPen(Qt::red, 2), QSize(2, 2));//设置样本点的颜色、大小
curve1->setSymbol(symbol);//添加样本点样式
//设置数据
curve1->setAxes(QwtPlot::xBottom, QwtPlot::yLeft);//参照哪个坐标轴
curve1->setData(series);
//把曲线附加到qwtPlot上
curve1->attach(ui.qwtPlot_Sonic);
//curve1->setPen(QColor(255, 0, 0), 1, Qt::SolidLine);//设置画笔
curve1->setPen(QPen(Qt::red, 1));//设置画笔
}
else if (flag == 1)
{
//样本点
QwtSymbol *symbol = new QwtSymbol(QwtSymbol::Ellipse,
QBrush(Qt::yellow), QPen(Qt::blue, 2), QSize(2, 2));//设置样本点的颜色、大小
curve1->setSymbol(symbol);//添加样本点样式
//设置数据
curve1->setAxes(QwtPlot::xBottom, QwtPlot::yRight);//参照哪个坐标轴
curve1->setData(series);
//把曲线附加到qwtPlot上
curve1->attach(ui.qwtPlot_Sonic);
curve1->setPen(QPen(Qt::blue, 1));//设置画笔
}
//添加格网
QwtPlotGrid* grid = new QwtPlotGrid();
grid->setPen(QColor(222, 222, 222), 1);
grid->attach(ui.qwtPlot_Sonic);
ui.qwtPlot_Sonic->replot();
ui.qwtPlot_Sonic->show();
}
//用来控制是否显示坐标轴
void ReadSonicPoint::setEnableAllAxis(bool yLeftEnable, bool yRightEnable, bool xBottomEnable, bool xTopEnable)
{
ui.qwtPlot_Sonic->enableAxis(QwtPlot::yLeft, yLeftEnable);
ui.qwtPlot_Sonic->enableAxis(QwtPlot::yRight, yRightEnable);
ui.qwtPlot_Sonic->enableAxis(QwtPlot::xBottom, xBottomEnable);
ui.qwtPlot_Sonic->enableAxis(QwtPlot::xTop, xTopEnable);
}
void ReadSonicPoint::setXScale(double xMinScale,double xMaxScale,double yMinScale, double yMaxScale)
{
ui.qwtPlot_Sonic->setAxisScale(QwtPlot::xBottom, xMinScale, xMaxScale);
ui.qwtPlot_Sonic->setAxisScale(QwtPlot::yRight, yMinScale, yMaxScale);
}
//用于显示点的值
void ReadSonicPoint::useQwtPlotPicker()
{
auto picker = new QwtPlotPicker(QwtPlot::xBottom,
QwtPlot::yLeft,
QwtPlotPicker::CrossRubberBand,
QwtPicker::ActiveOnly,
ui.qwtPlot_Sonic->canvas());
picker->setStateMachine(new QwtPickerDragPointMachine());
picker->setRubberBandPen(QColor(Qt::darkMagenta));
picker->setTrackerPen(QColor(Qt::black));//显示实时的坐标
}
//void ReadSonicPoint::
ReadSonicPoint::~ReadSonicPoint()
{
}