179 lines
6.6 KiB
C++
179 lines
6.6 KiB
C++
#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);
|
|
plotGraph();
|
|
settingsDialog = new SettingsDialog(curves.size(), this);
|
|
QVector<QColor> curves_color = settingsDialog->getCurveColors();
|
|
for(int i=0; i< curves.size(); ++i){
|
|
curves[i]->setPen(QPen(curves_color[i]));
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
// 连接customPlot的双击信号到槽函数
|
|
connect(customPlot, &QCustomPlot::mouseDoubleClick, this, &MainWindow::openSettingsDialog);
|
|
}
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::plotGraph()
|
|
{
|
|
curves.clear();
|
|
|
|
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);
|
|
curves.append(curveFunction);
|
|
QCPCurve *curve0Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
|
|
curves.append(curve0Plot);
|
|
QCPCurve *curve1Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
|
|
curves.append(curve1Plot);
|
|
QCPCurve *curve2Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
|
|
curves.append(curve2Plot);
|
|
QCPCurve *curve3Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
|
|
curves.append(curve3Plot);
|
|
QCPCurve *curve4Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
|
|
curves.append(curve4Plot);
|
|
QCPCurve *curve5Plot = new QCPCurve(customPlot->xAxis, customPlot->yAxis);
|
|
curves.append(curve5Plot);
|
|
|
|
|
|
curveFunction->setData(I_values, t_values);
|
|
curveFunction->setPen(QPen(Qt::DashLine));
|
|
// 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->setInteraction(QCP::iRangeZoom, true); // 缩放
|
|
customPlot->setInteraction(QCP::iRangeDrag, true); // 拖动
|
|
// 设置可以通过滚轮缩放的轴
|
|
customPlot->axisRect()->setRangeZoom(Qt::Horizontal | Qt::Vertical); // 水平和垂直方向都可以缩放
|
|
customPlot->axisRect()->setRangeZoomAxes(customPlot->xAxis, customPlot->yAxis); // 指定X和Y轴
|
|
// 设置缩放的缩放速度
|
|
customPlot->axisRect()->setRangeZoomFactor(0.9);
|
|
|
|
// 显示网格
|
|
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);
|
|
}
|
|
}
|
|
|
|
void MainWindow::openSettingsDialog()
|
|
{
|
|
if (settingsDialog->exec() == QDialog::Accepted) {
|
|
// 从settingsDialog获取设置并应用到customPlot
|
|
|
|
// 获取轴范围
|
|
double xMin = settingsDialog->getXMin();
|
|
double xMax = settingsDialog->getXMax();
|
|
double yMin = settingsDialog->getYMin();
|
|
double yMax = settingsDialog->getYMax();
|
|
|
|
customPlot->xAxis->setRange(xMin, xMax);
|
|
customPlot->yAxis->setRange(yMin, yMax);
|
|
|
|
// 设置曲线颜色
|
|
// QColor curveColor1 = settingsDialog->getCurveColor(0);
|
|
// QColor curveColor2 = settingsDialog->getCurveColor(1);
|
|
// customPlot->graph(0)->setPen(QPen(curveColor1));
|
|
// customPlot->graph(1)->setPen(QPen(curveColor2));
|
|
|
|
QVector<QColor> curves_color = settingsDialog->getCurveColors();
|
|
|
|
for(int i=0; i< curves.size(); ++i){
|
|
curves[i]->setPen(QPen(curves_color[i]));
|
|
}
|
|
|
|
|
|
// 设置背景颜色
|
|
QColor backgroundColor = settingsDialog->getBackgroundColor();
|
|
customPlot->setBackground(backgroundColor);
|
|
|
|
// 设置网格可见性
|
|
bool gridVisible = settingsDialog->isGridVisible();
|
|
customPlot->xAxis->grid()->setVisible(gridVisible);
|
|
customPlot->yAxis->grid()->setVisible(gridVisible);
|
|
|
|
// 重新绘制图表
|
|
customPlot->replot();
|
|
}
|
|
}
|