#include "mainwindow.h"
#include "qcustomplot.h"
#include <QWidget>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QVBoxLayout>

extern QVector<double> g_real_temper;
extern QVector<double> g_real_dsc;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("YT_ANA"));
    createMenuBar();
    createRealWindow();
    createThermalWindow();
    setWindowIcon(QIcon("://tango.ico"));

    connect(menu,SIGNAL(show_real_window()),this,SLOT(show_real_window_slot()));
    connect(menu,SIGNAL(show_thermal_window()),this,SLOT(show_thermal_window_slot()));
}

MainWindow::~MainWindow() {}

void MainWindow::closeEvent(QCloseEvent *event)
{
    //窗口关闭时询问是否退出
    QMessageBox::StandardButton result=QMessageBox::question(this, "确认", "确定要退出吗？",
                                                               QMessageBox::Yes|QMessageBox::No |QMessageBox::Cancel,
                                                               QMessageBox::No);

    if (result==QMessageBox::Yes)
        event->accept();
    else
        event->ignore();
}


void  MainWindow::createMenuBar()
{
    menu = new MenuBar(this);
    menuBar()->addMenu(menu->ApplicationMenu);
    menuBar()->addMenu(menu->testMenu);
    menuBar()->addMenu(menu->viewMenu);
    menuBar()->addMenu(menu->plotMenu);
    menuBar()->addMenu(menu->markerMenu);
    menuBar()->addMenu(menu->showMenu);
    menuBar()->addMenu(menu->helpMenu);
}

void  MainWindow::createThermalWindow()
{
    thermalWidget = new ThermalWidget(this);
    thermalWidget->setWindowFlags(Qt::Window);
    thermalWidget->resize(600,800);
    thermalWidget->setWindowTitle(tr("热焓校正"));
    thermalWidget->setWindowFlags(thermalWidget->windowFlags() &~ Qt::WindowMinMaxButtonsHint);
    thermalWidget->hide();
}

void  MainWindow::createRealWindow()
{
    realWidget = new QWidget(this);
    // startButton = new QPushButton(realWidget);
    // startButton->setText("开始绘制");
    // stopButton = new QPushButton(realWidget);
    // stopButton->setText("开始绘制");
    realPlot = new QCustomPlot(realWidget);
    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(realWidget);
    ///layout->addWidget(startButton, 1);
    layout->addWidget(realPlot);
    realWidget->setWindowFlags(Qt::Window);
    realWidget->resize(1000,700);
    realWidget->hide();

    QPen graphPen;
    graphPen.setColor(QColor(40, 110, 255));
    graphPen.setWidthF(3.0);
    realPlot->addGraph();
    realPlot->graph(0)->setPen(graphPen);//曲线1蓝色
    ///realPlot->graph(0)->setPen(QPen(QColor(40, 110, 255)));//曲线1蓝色
    ///realPlot->graph(0)->setPen(QPen(QColor(255, 255, 255)));//曲线1蓝色
    realPlot->addGraph();
    realPlot->graph(1)->setPen(QPen(QColor(255, 110, 40)));//曲线2红色
    //坐标轴使用时间刻度
    // QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
    // timeTicker->setTimeFormat("%h:%m:%s");
    // realPlot->xAxis->setTicker(timeTicker);
    //四边安上坐标轴
    realPlot->axisRect()->setupFullAxesBox();

    //设置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); // 间隔时间 0ms表示尽可能快的触发
}

void  MainWindow::show_thermal_window_slot()
{
    thermalWidget->show();
}

void  MainWindow::show_real_window_slot()
{
    realWidget->show();
}

void MainWindow::realtimeDataSlot()
{
    double key = 0.0;
    if(g_real_temper.size()>0)
    {
        key = g_real_temper.at(0);
    }
    static double lastPointKey = 0;

    mutex.lock();

    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, 8, Qt::AlignRight);
    //绘图
    realPlot->replot();
    //计算帧数
    static double lastFpsKey;
    static int frameCount;
    ++frameCount;
    if (key-lastFpsKey > 2)
    {
        lastFpsKey = key;
        frameCount = 0;
    }
}
