#include "realwidget.h"
extern QVector<double> g_real_temper;
extern QVector<double> g_real_dsc;

RealWidget::RealWidget(QWidget *parent) : QWidget(parent)
{
    realPlot = new QCustomPlot(this);
    realPlot->setBackground(QBrush(Qt::black));
    realPlot->xAxis->setLabelColor(QColor(255, 255, 255));
    realPlot->xAxis->setTickLabelColor(QColor(255, 255, 255));
    realPlot->yAxis->setLabelColor(QColor(255, 255, 255));
    realPlot->yAxis->setTickLabelColor(QColor(255, 255, 255));
    realPlot->xAxis->setLabel("Temp");
    realPlot->yAxis->setLabel("DSC");
    QPen pen = realPlot->xAxis->basePen();
    pen.setColor(QColor(255, 255, 255));
    realPlot->xAxis->setTickPen(pen);
    realPlot->xAxis->setBasePen(pen);
    realPlot->xAxis->setSubTickPen(pen);
    pen = realPlot->yAxis->basePen();
    pen.setColor(QColor(255, 255, 255));
    realPlot->yAxis->setTickPen(pen);
    realPlot->yAxis->setBasePen(pen);
    realPlot->yAxis->setSubTickPen(pen);
    QHBoxLayout *layout = new QHBoxLayout(this);
    ///layout->addWidget(startButton, 1);
    layout->addWidget(realPlot);
    ///this->setWindowFlags(Qt::Window);
    ///this->resize(1000,700);
    this->hide();

    QPen graphPen;
    graphPen.setColor(QColor(40, 110, 255));
    graphPen.setWidthF(3.0);
    realPlot->addGraph();
    realPlot->graph(0)->setPen(graphPen);//曲线1蓝色
    realPlot->graph(0)->setBrush(Qt::NoBrush);
    //四边安上坐标轴
    realPlot->axisRect()->setupFullAxesBox();
    realPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom| QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables | QCP::iMultiSelect | QCP::iSelectItems);

    //设置y轴范围
    realPlot->yAxis->setRange(-30, 30);
    // 使上下轴、左右轴范围同步
    connect(realPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), realPlot->xAxis2, SLOT(setRange(QCPRange)));
    connect(realPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), realPlot->yAxis2, SLOT(setRange(QCPRange)));
    //定时器连接槽函数realtimeDataSlot
    connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
    dataTimer.start(200);

}
RealWidget::~RealWidget()
{
}

void RealWidget::start_plot_real_line()
{
    mutex.lock();
    g_real_temper.clear();
    g_real_dsc.clear();
    mutex.unlock();
    dataTimer.start(200);
}

void RealWidget::stop_plot_real_line()
{
    dataTimer.stop();
    mutex.lock();
    g_real_temper.clear();
    g_real_dsc.clear();
    mutex.unlock();
}

void RealWidget::realtimeDataSlot()
{
    double key = 0.0;
    mutex.lock();
    if(g_real_temper.size()>0)
    {
        key = g_real_temper.at(0);
    }
    static double lastPointKey = 0;
    if((g_real_dsc.size()>0)&&(g_real_temper.size()>0))
    {
        // 添加数据到graph
        realPlot->graph(0)->addData(g_real_temper.at(0), g_real_dsc.at(0));
        g_real_temper.removeFirst();
        g_real_dsc.removeFirst();
    }
    mutex.unlock();

    //记录当前时刻
    lastPointKey = key;
    realPlot->xAxis->setRange(key, 16, Qt::AlignRight);
    //绘图
    realPlot->replot();
    //计算帧数
    static double lastFpsKey;
    static int frameCount;
    ++frameCount;
    if (key-lastFpsKey > 2) // 每2秒求一次平均值
    {
        lastFpsKey = key;
        frameCount = 0;
    }
}


