#include "readflowpoint.h"
#include "qfile.h"
#include "qmessagebox.h"
#include "qwt_legend.h"
#include "qwt_plot_grid.h"
#include "qwt_scale_draw.h"
#include "qdatetime.h"
#include "qwt_series_data.h"
#include "qwt_plot_curve.h"
ReadFlowPoint::ReadFlowPoint(QWidget *parent, QString filepath)
: QDialog(parent)
{
ui.setupUi(this);
ui.lineEdit_Time->setText(QDateTime::currentDateTime().toString("MM/dd/yyyy"));
QList<QString> flowPoint_list = readFlowPointFile(filepath);
writeFlowPointTable(flowPoint_list);//数据写入表格
drawQwtPlotFlowPoint(flowPoint_list);//绘图
connect(ui.lineEdit_Time, SIGNAL(editingFinished()), this, SLOT(dateChanged()));
}
void ReadFlowPoint::dateChanged()
{
ui.qwtPlot_FlowPoint->detachItems(); //清除项
timeJudgeList.clear();
depthJudgeList.clear();
QString timereceive = ui.lineEdit_Time->text();
for (int i = 0; i < dateList.size(); i++)
{
if (dateList[i] == timereceive)
{
timeJudgeList.append(timeList[i]);
depthJudgeList.append(depthList[i]);
}
}
drawTidPointsPlot();
}
void ReadFlowPoint::setEnableAllAxis(bool yLeftEnable, bool yRightEnable, bool xBottomEnable, bool xTopEnable)
{
ui.qwtPlot_FlowPoint->enableAxis(QwtPlot::yLeft, yLeftEnable);
ui.qwtPlot_FlowPoint->enableAxis(QwtPlot::yRight, yRightEnable);
ui.qwtPlot_FlowPoint->enableAxis(QwtPlot::xBottom, xBottomEnable);
ui.qwtPlot_FlowPoint->enableAxis(QwtPlot::xTop, xTopEnable);
}
//将tidList数据转成点数据
QVector<QPointF> tidListConvertPoint(QStringList timeList, QStringList depthList)
{
QVector<QPointF> tidPointList;
int counts = 0;
QList<double> timeDoubleList,depthDoubleList;
for (int i = 0; i < timeList.size(); i++)
{
counts++;
QStringList xList = timeList[i].split(":");
double mm = xList[0].toDouble();
double sec = xList[1].toDouble();
double secval = sec / 60;
timeDoubleList.append(secval + mm);
}
for (int j = 0; j < depthList.size(); j++)
{
depthDoubleList.append(depthList[j].toDouble());
}
for (int k = 0; k < counts;k++)
{
QPointF tidPoint;
tidPoint.setX(timeDoubleList[k]);
tidPoint.setY(depthDoubleList[k]);
tidPointList.append(tidPoint);
}
return tidPointList;
}
void ReadFlowPoint::drawTidPointsPlot()
{
//设置坐标轴显示
QVector<QPointF> tidPointList = tidListConvertPoint(timeJudgeList, depthJudgeList);
setEnableAllAxis(true, false, true, true);
//设置坐标轴刻度
ui.qwtPlot_FlowPoint->setAxisScale(QwtPlot::xBottom, 0, 24, 1);
ui.qwtPlot_FlowPoint->setAxisScale(QwtPlot::xTop, 0, 24, 1);
//添加地图
QwtPointSeriesData* series = new QwtPointSeriesData(tidPointList);
QwtPlotCurve* curve1 = new QwtPlotCurve("时间\nCoor"); //设置line名称
//设置数据
curve1->setData(series);
//把曲线附加到qwtPlot上
curve1->attach(ui.qwtPlot_FlowPoint);
curve1->setPen(QColor(255, 0, 0), 2, Qt::SolidLine);//设置画笔
//添加格网
QwtPlotGrid* grid = new QwtPlotGrid();
grid->setPen(QColor(222, 222, 222), 1);
grid->attach(ui.qwtPlot_FlowPoint);
ui.qwtPlot_FlowPoint->insertLegend(new QwtLegend(), QwtPlot::BottomLegend);//添加图例
ui.qwtPlot_FlowPoint->replot();
ui.qwtPlot_FlowPoint->show();
}
void ReadFlowPoint::drawQwtPlotFlowPoint(QList<QString> flowPoint_list)
{
//解析数据
for (int i = 0; i < flowPoint_list.size(); i++)
{
QList<QString> flowPointDatas = flowPoint_list[i].split(" ");
if (flowPointDatas.size() >= 3)
{
dateList.append(flowPointDatas[0]);
timeList.append(flowPointDatas[1]);
depthList.append(flowPointDatas[2]);
}
}
ui.lineEdit_Time->setText(dateList[0]); //设置默认数据日期为0;
//绘图
dateChanged();
}
QList<QString> ReadFlowPoint::readFlowPointFile(QString tid_path) //return 验流点数据
{
QList<QString> flowPoint_list;
QFile file(tid_path);
if (!file.open(QIODevice::ReadWrite|QIODevice::Text))
{
QMessageBox::warning(NULL, "文件打开", "文件打开失败");
}
while (!file.atEnd())
{
QByteArray line = file.readLine();
QString str(line);
flowPoint_list.append(str);
}
return flowPoint_list;
}
void ReadFlowPoint::writeFlowPointTable(QList<QString> flowPoint_list)
{
//设置tableWidget表头内容
ui.tableWidget_FlowPoint->setColumnCount(3);
QStringList header;
header.append(QString::fromLocal8Bit("日期"));
header.append(QString::fromLocal8Bit("时间"));
header.append("Corr");
ui.tableWidget_FlowPoint->setHorizontalHeaderLabels(header);
ui.tableWidget_FlowPoint->setRowCount(flowPoint_list.size());
//设置宽度自适应
ui.tableWidget_FlowPoint->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui.tableWidget_FlowPoint->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//添加数据
for (int i = 0; i < flowPoint_list.size(); i++)
{
QList<QString> flowPointDatas = flowPoint_list[i].split(" ");
if (flowPointDatas.size() >= 3)
{
ui.tableWidget_FlowPoint->setItem(i, 0, new QTableWidgetItem(flowPointDatas[0]));
ui.tableWidget_FlowPoint->setItem(i, 1, new QTableWidgetItem(flowPointDatas[1]));
ui.tableWidget_FlowPoint->setItem(i, 2, new QTableWidgetItem(flowPointDatas[2]));
}
}
}
ReadFlowPoint::~ReadFlowPoint()
{
}
其中:::
最重要的是这句代码
ui.qwtPlot_FlowPoint->detachItems(); //清除项