|
| 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 | +} |
0 commit comments