Skip to content

Commit d243e9a

Browse files
committed
first commit
0 parents  commit d243e9a

File tree

515 files changed

+11663
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

515 files changed

+11663
-0
lines changed

caok/drawplot.cpp

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
#include "drawplot.h"
2+
#include "ui_drawplot.h"
3+
#include <QVBoxLayout>
4+
#include <QGraphicsScene>
5+
#include <QGraphicsView>
6+
#include <QPushButton>
7+
#include <QGraphicsTextItem>
8+
#include <cmath>
9+
#include <QDebug>
10+
#include <QPen>
11+
#include <QPainterPath>
12+
13+
drawPlot::drawPlot(QSpinBox *spinBox, QSpinBox *spinBox1, QSpinBox *spinBox2, QSpinBox *spinBoxy1, QSpinBox *spinBoxy2,
14+
QComboBox *comboBox1, QComboBox *comboBox2, QComboBox *comboBox3,
15+
QLineEdit *functionInput, QLineEdit *lineEdit, QGraphicsView *view,
16+
QPushButton *pushButton1, QPushButton *pushButton2, QWidget *parent)
17+
: QWidget(parent),
18+
ui(new Ui::drawPlot),
19+
spinBox(spinBox), spinBox1(spinBox1), spinBox2(spinBox2), spinBoxy1(spinBoxy1), spinBoxy2(spinBoxy2),
20+
comboBox1(comboBox1), comboBox2(comboBox2), comboBox3(comboBox3),
21+
functionInput(functionInput), lineEdit(lineEdit), scene(nullptr), view(view),
22+
pushButton1(pushButton1), pushButton2(pushButton2), plotPathItem(nullptr), Colorplot(Qt::black)
23+
{
24+
ui->setupUi(this);
25+
26+
this->scene = new QGraphicsScene(); // Initialize scene
27+
view->setScene(this->scene);
28+
29+
QVBoxLayout *layout = new QVBoxLayout(this);
30+
view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
31+
layout->addWidget(view);
32+
setLayout(layout);
33+
34+
QList<QSpinBox*> spinBoxes = {spinBox1, spinBox2, spinBoxy1, spinBoxy2};
35+
for (QSpinBox* spinBox : spinBoxes) {
36+
spinBox->setMinimum(-500);
37+
spinBox->setMaximum(500);
38+
}
39+
40+
initializeFunctionMap();
41+
setConnection();
42+
updatePlot(); // Initial plot
43+
}
44+
45+
drawPlot::~drawPlot()
46+
{
47+
delete ui;
48+
}
49+
50+
void drawPlot::setConnection() {
51+
connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, [this]() {
52+
sizePlot(spinBox->value());
53+
});
54+
55+
connect(comboBox1, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index) {
56+
colorPlot(index);
57+
});
58+
59+
connect(spinBox1, QOverload<int>::of(&QSpinBox::valueChanged), this, [this]() {
60+
updatePlot();
61+
});
62+
63+
connect(spinBox2, QOverload<int>::of(&QSpinBox::valueChanged), this, [this]() {
64+
updatePlot();
65+
});
66+
67+
connect(spinBoxy1, QOverload<int>::of(&QSpinBox::valueChanged), this, [this]() {
68+
updatePlot();
69+
});
70+
71+
connect(spinBoxy2, QOverload<int>::of(&QSpinBox::valueChanged), this, [this]() {
72+
updatePlot();
73+
});
74+
75+
connect(functionInput, &QLineEdit::textChanged, this, [this]() {
76+
updatePlot();
77+
});
78+
79+
connect(pushButton1, &QPushButton::clicked, this, [this]() {
80+
button1Line();
81+
});
82+
}
83+
84+
void drawPlot::initializeFunctionMap() {
85+
functionMap["x^2"] = [](double x) { return x * x; };
86+
functionMap["log(x)"] = [](double x) { return x > 0 ? std::log(x) : 0; };
87+
functionMap["10*sin(x)"] = [](double x) { return 10 * std::sin(x); };
88+
functionMap["10*cos(x)"] = [](double x) { return 10 * std::cos(x); };
89+
functionMap["sqrt(x)"] = [](double x) { return x >= 0 ? std::sqrt(x) : 0; };
90+
functionMap["x"] = [](double x) { return x; };
91+
functionMap["sin(x)"] = [](double x) { return std::sin(x); };
92+
functionMap["cos(x)"] = [](double x) { return std::cos(x); };
93+
functionMap["2"] = [](double) { return 2.0; };
94+
}
95+
96+
std::function<double(double)> drawPlot::parseFunctionInput(const QString &input) {
97+
auto it = functionMap.find(input);
98+
if (it != functionMap.end()) {
99+
return it.value(); // Use it.value() to get the function associated with the key
100+
}
101+
return [](double) -> double { return 0.0; }; // Default to zero if not found
102+
}
103+
104+
void drawPlot::plotFunction(const QString &input) {
105+
if (!scene) {
106+
qDebug() << "Scene is not initialized!";
107+
return;
108+
}
109+
110+
auto func = parseFunctionInput(input);
111+
scene->clear();
112+
113+
double x1 = spinBox1->value() * 50;
114+
double x2 = spinBox2->value() * 50;
115+
double y1 = spinBoxy2->value() * 50;
116+
double y2 = spinBoxy1->value() * 50;
117+
118+
if (x1 == 0 && x2 == 0) {
119+
x1 = -500;
120+
x2 = 500;
121+
}
122+
if (y1 == 0 && y2 == 0) {
123+
y1 = 500;
124+
y2 = -500;
125+
}
126+
127+
drawGrid(scene, x1, y1, x2, y2);
128+
drawAxisLabels(scene, x1, y1, x2, y2);
129+
130+
QPen pen = Colorplot;
131+
pen.setWidth(spinBox->value()); // Line width from spinBox
132+
133+
QPainterPath path;
134+
double step = (x2 - x1) / 1000.0;
135+
double x = x1;
136+
double y = func(x) * spinBox->value();
137+
path.moveTo(x, -y);
138+
139+
for (x += step; x <= x2; x += step) {
140+
y = func(x) * spinBox->value();
141+
path.lineTo(x, -y);
142+
}
143+
144+
if (plotPathItem != nullptr) {
145+
plotPathItem->setPath(path);
146+
plotPathItem->setPen(pen);
147+
} else {
148+
plotPathItem = scene->addPath(path, pen);
149+
}
150+
151+
QGraphicsTextItem *label = new QGraphicsTextItem(QString("f(x) = %1").arg(input));
152+
label->setPos(x2, y2);
153+
scene->addItem(label);
154+
155+
qDebug() << "Function plotted successfully";
156+
}
157+
158+
void drawPlot::colorPlot(int colorIndex) {
159+
QColor color;
160+
switch (colorIndex) {
161+
case 0: color = Qt::black; break;
162+
case 1: color = Qt::yellow; break;
163+
case 2: color = Qt::blue; break;
164+
case 3: color = Qt::red; break;
165+
default: color = Qt::green; break;
166+
}
167+
Colorplot.setColor(color);
168+
169+
// Redraw the plot with the new color
170+
QString input = functionInput->text();
171+
plotFunction(input);
172+
}
173+
174+
void drawPlot::sizePlot(int size) {
175+
qDebug() << "Plot size changed to:" << size;
176+
177+
// Update pen width
178+
QPen pen = Colorplot;
179+
pen.setWidth(size);
180+
181+
if (plotPathItem != nullptr) {
182+
plotPathItem->setPen(pen);
183+
scene->update(); // Optionally, redraw the scene
184+
}
185+
}
186+
187+
void drawPlot::updatePlot() {
188+
QString input = functionInput->text();
189+
plotFunction(input);
190+
}
191+
192+
void drawPlot::button1Line() {
193+
QString input = functionInput->text();
194+
plotFunction(input);
195+
}
196+
197+
void drawPlot::drawGrid(QGraphicsScene* scene, double x1, double y1, double x2, double y2) {
198+
QPen gridPen(Qt::gray, 0.5, Qt::DotLine);
199+
double stepX = (x2 - x1) / 20.0;
200+
double stepY = (y1 - y2) / 20.0;
201+
202+
for (double x = x1; x <= x2; x += stepX) {
203+
scene->addLine(x, y1, x, y2, gridPen);
204+
}
205+
206+
for (double y = y2; y <= y1; y += stepY) {
207+
scene->addLine(x1, y, x2, y, gridPen);
208+
}
209+
210+
scene->addLine(0, y1, 0, y2, QPen(Qt::black, 1.5));
211+
}
212+
213+
void drawPlot::drawAxisLabels(QGraphicsScene* scene, double x1, double y1, double x2, double y2) {
214+
QFont font;
215+
font.setPointSize(8);
216+
217+
double stepX = (x2 - x1) / 10.0;
218+
for (double x = x1; x <= x2; x += stepX) {
219+
scene->addLine(x, -5, x, 5, QPen(Qt::black));
220+
QGraphicsTextItem* label = new QGraphicsTextItem(QString::number(x));
221+
label->setPos(x, 10);
222+
label->setFont(font);
223+
scene->addItem(label);
224+
}
225+
226+
double stepY = (y1 - y2) / 10.0;
227+
for (double y = y2; y <= y1; y += stepY) {
228+
scene->addLine(-5, y, 5, y, QPen(Qt::black));
229+
QGraphicsTextItem* label = new QGraphicsTextItem(QString::number(y));
230+
label->setPos(10, -y);
231+
label->setFont(font);
232+
scene->addItem(label);
233+
}
234+
}

caok/drawplot.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#ifndef DRAWPLOT_H
2+
#define DRAWPLOT_H
3+
4+
#include "qcombobox.h"
5+
#include "qpushbutton.h"
6+
#include <QWidget>
7+
#include <QPainter>
8+
#include <QComboBox>
9+
#include <QLineEdit>
10+
#include <QSpinBox>
11+
#include <QPushButton>
12+
#include <QGraphicsView>
13+
#include <QGraphicsScene>
14+
#include <QTextEdit>
15+
16+
#include <QString>
17+
#include <QPen>
18+
#include <functional>
19+
#include <QMap>
20+
#include <functional> //函数图像库要用
21+
#include <QColor> //颜色要用
22+
#include <QPainterPath> //路径
23+
24+
25+
26+
QT_BEGIN_NAMESPACE
27+
namespace Ui {
28+
class drawPlot;
29+
}
30+
QT_END_NAMESPACE
31+
32+
class MainWindow;
33+
34+
class drawPlot : public QWidget
35+
{
36+
Q_OBJECT
37+
38+
public: //改为公共
39+
explicit drawPlot(QSpinBox *spinBox, QSpinBox *spinBox1, QSpinBox *spinBox2, QSpinBox *spinBoxy1, QSpinBox *spinBoxy2,
40+
QComboBox *comboBox1, QComboBox *comboBox2, QComboBox *comboBox3,
41+
QLineEdit *functionInput, QGraphicsView *view,
42+
QPushButton *pushButton1, QPushButton *pushButton2, QWidget *parent = nullptr);
43+
44+
45+
~drawPlot();
46+
47+
48+
public slots:
49+
// void upGraph(const QString &functionInput); //更新画面 //void LtextChanged(const QString &arg1); 废弃,用plotFunction绘画替代
50+
void colorPlot(int colorIndex); //对应MainWindow的信号参数类型int index1
51+
void backdrawPlot(const QString &found, const QString &newfound); //与显示和隐藏关联, //所以要在drawPlot实现对文本变化的反应,在这边调用MainWindow的公有槽
52+
void sizePlot(int size); //在drawPlot当中connect使用MainWindow的相关事件。设置可能的槽为公有,不然容易报错
53+
54+
void picturePlot();
55+
56+
// void exportPlot(const QString &over);
57+
void plotFunction(const QString &input); //管理所有用于绘画的函数
58+
59+
void setConnection(); //管理信号和槽的函数(包含connection式信号槽的对接)
60+
61+
62+
private slots:
63+
64+
65+
66+
private:
67+
Ui::drawPlot *ui;
68+
drawPlot *DrawPlot; //默认
69+
QTextEdit *textEditFunction;
70+
QTextEdit *textEditSize;
71+
QTextEdit *textEditColor;
72+
73+
QSpinBox *spinBox;
74+
QSpinBox *spinBox1;
75+
QSpinBox *spinBox2;
76+
QSpinBox *spinBoxy1;
77+
QSpinBox *spinBoxy2;
78+
79+
QComboBox *comboBox1,*comboBox2,*comboBox3;
80+
QLineEdit *functionInput;
81+
QGraphicsScene *scene; //管理绘画和控件 的对象
82+
QGraphicsView *view; //管理显示画面 的对象
83+
double x1, y1, x2, y2;
84+
QPushButton *pushButton1,*pushButton2;
85+
86+
QPen Colorplot;
87+
QPainterPath plotPath; //避免修改时清除图像
88+
89+
// QLineEdit *lineEditFunction,*lineEditColor,LineEditSize;
90+
//作为新界面 管理一个窗口,qwidget类,这个窗口包含view和scene,drawPlotWindow是一个实例
91+
92+
//构造函数的形式
93+
/*void drawFunction(QGraphicsScene * scene,int func,QPen pen);*/
94+
/*void drawGrid(QGraphicsScene *scene,double x,double y); */
95+
/*void drawAxesAndLables(QGraphicsScene *scene,double x,double y); */ //下面部分参数更完善
96+
97+
/*void drawAxes(QGraphicsScene* scene, double x1, double y1, double x2, double y2);*/ //坐标轴
98+
void drawGrid(QGraphicsScene* scene, double x1, double y1, double x2, double y2); //横竖线(方格)
99+
void drawAxisLabels(QGraphicsScene* scene, double x1, double y1, double x2, double y2); //画标签(不包括横竖方格)
100+
void drawFunction(QGraphicsScene* scene, std::function<double(double)> func, QPen pen, double x1, double x2, double y1, double y2); //函数图像)
101+
void initializeFunctionMap();
102+
void clearFunctionGraph(); //
103+
104+
QMap<QString, std::function<double(double)>> functionMap;
105+
std::function<double(double)> parseFunctionInput(const QString &input);
106+
107+
108+
};
109+
#endif

0 commit comments

Comments
 (0)