#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    customPlot = new QCustomPlot(this);

    QPushButton *saveButton = new QPushButton("Save Graph", this);
    connect(saveButton, &QPushButton::clicked, this, &MainWindow::saveGraph);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(customPlot);
    layout->addWidget(saveButton);
    ui->centralwidget->setLayout(layout);

    plotGraph();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::plotGraph()
{
    QVector<double> I_values, t_values;
    QVector<QCPCurveData> curve0, curve1, curve2, curve3, curve4, curve5;

    // 生成函数曲线数据
    for (int i = 1; i <= 100; ++i) {
        double I = qPow(10, 1.0 + 3.0 * i / 100.0); // 从10到10000的对数值
        double epsilon = 1e-10;
        double Tp = 1;
        double I_op = 100;
        double t = 80 * Tp / ((qPow(I / I_op, 2) - 1) + epsilon);
        if (t > 0) {
            I_values.append(I);
            t_values.append(t);
        }
    }

    // 定义其他曲线数据
    curve0 << QCPCurveData(0, 10, 1000) << QCPCurveData(1, 30, 100) << QCPCurveData(2, 100, 10) << QCPCurveData(3, 1000, 1) << QCPCurveData(4, 3000, 0.1);
    curve1 << QCPCurveData(0, 10, 1000) << QCPCurveData(1, 30, 300) << QCPCurveData(2, 100, 30) << QCPCurveData(3, 1000, 3) << QCPCurveData(4, 3000, 0.3);
    curve2 << QCPCurveData(0, 10, 1000) << QCPCurveData(1, 30, 200) << QCPCurveData(2, 100, 20) << QCPCurveData(3, 1000, 2) << QCPCurveData(4, 3000, 0.2);
    curve3 << QCPCurveData(0, 15, 500) << QCPCurveData(1, 40, 50) << QCPCurveData(2, 150, 5) << QCPCurveData(3, 1500, 0.5) << QCPCurveData(4, 5000, 0.05);
    curve4 << QCPCurveData(0, 10, 800) << QCPCurveData(1, 30, 80) << QCPCurveData(2, 100, 8) << QCPCurveData(3, 1000, 0.8) << QCPCurveData(4, 3000, 0.08);
    curve5 << QCPCurveData(0, 20, 1000) << QCPCurveData(1, 60, 150) << QCPCurveData(2, 200, 15) << QCPCurveData(3, 2000, 1.5) << QCPCurveData(4, 6000, 0.15);

    // 创建曲线并添加到图表中
    QCPCurve *curveFunction = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
    QCPCurve *curve0Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
    QCPCurve *curve1Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
    QCPCurve *curve2Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
    QCPCurve *curve3Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
    QCPCurve *curve4Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
    QCPCurve *curve5Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);

    curveFunction->setData(I_values, t_values);
    curveFunction->setPen(QPen(Qt::black, 1, Qt::DashLine));

    curve0Plot->data()->set(curve0, true);
    curve0Plot->setPen(QPen(Qt::blue));

    curve1Plot->data()->set(curve1, true);
    curve1Plot->setPen(QPen(Qt::green));

    curve2Plot->data()->set(curve2, true);
    curve2Plot->setPen(QPen(QColorConstants::Svg::purple));

    curve3Plot->data()->set(curve3, true);
    curve3Plot->setPen(QPen(QColorConstants::Svg::orange));

    curve4Plot->data()->set(curve4, true);
    curve4Plot->setPen(QPen(Qt::red));

    curve5Plot->data()->set(curve5, true);
    curve5Plot->setPen(QPen(QColorConstants::Svg::brown));

    // 设置对数坐标轴
    customPlot->xAxis->setScaleType(QCPAxis::stLogarithmic);
    customPlot->yAxis->setScaleType(QCPAxis::stLogarithmic);

    customPlot->xAxis->setLabel("Axis-X");
    customPlot->yAxis->setLabel("Axis-Y");

    customPlot->xAxis->setRange(5, 10000);
    customPlot->yAxis->setRange(0.01, 1000);

    // 显示网格
    customPlot->xAxis->grid()->setSubGridVisible(true);
    customPlot->yAxis->grid()->setSubGridVisible(true);

    // 显示图表
    customPlot->replot();
}

void MainWindow::saveGraph()
{
    QString filePath = QFileDialog::getSaveFileName(this, "Save Graph", "", "PNG Files (*.png);;All Files (*)");
    if (!filePath.isEmpty()) {
        customPlot->savePng(filePath);
    }
}
