2018-12-24 03:27:24 +08:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QLineEdit>
|
2018-12-24 19:29:33 +08:00
|
|
|
#include <QCheckBox>
|
2018-12-24 22:07:14 +08:00
|
|
|
#include <QFormLayout>
|
|
|
|
#include <QDoubleSpinBox>
|
2018-12-24 03:27:24 +08:00
|
|
|
#include "jkqtplotter/jkqtplotter.h"
|
2018-12-28 20:32:20 +08:00
|
|
|
#include "jkqtplotter/jkqtpgraphs.h"
|
2018-12-24 03:27:24 +08:00
|
|
|
#include "jkqtplotter/jkqtpgraphsparsedfunction.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
QApplication app(argc, argv);
|
|
|
|
|
|
|
|
// 1. create a window that conatins a line-edit to edit a function
|
|
|
|
// and a JKQtPlotter to display the function, combine everything in a layout
|
|
|
|
QWidget mainWin;
|
|
|
|
QLineEdit* edit=new QLineEdit(&mainWin);
|
|
|
|
edit->setToolTip("enter a function in dependence of the variable <tt>x</tt> and press ENTER to update the graph");
|
2018-12-24 19:29:33 +08:00
|
|
|
QCheckBox* check=new QCheckBox("display sample points");
|
2018-12-24 22:07:14 +08:00
|
|
|
QDoubleSpinBox* spinP1=new QDoubleSpinBox(&mainWin);
|
|
|
|
spinP1->setValue(8);
|
|
|
|
spinP1->setRange(-10000,10000);
|
|
|
|
spinP1->setToolTip("enter a Value for parameter <tt>p1</tt> and press ENTER to update the graph");
|
|
|
|
QDoubleSpinBox* spinP2=new QDoubleSpinBox(&mainWin);
|
|
|
|
spinP2->setValue(4);
|
|
|
|
spinP2->setRange(-10000,10000);
|
|
|
|
spinP2->setToolTip("enter a Value for parameter <tt>p1</tt> and press ENTER to update the graph");
|
2018-12-24 03:27:24 +08:00
|
|
|
JKQtPlotter* plot=new JKQtPlotter(&mainWin);
|
2018-12-24 22:07:14 +08:00
|
|
|
QFormLayout* flayout=new QFormLayout;
|
2018-12-24 03:27:24 +08:00
|
|
|
QVBoxLayout* layout=new QVBoxLayout;
|
|
|
|
mainWin.setLayout(layout);
|
2018-12-24 22:07:14 +08:00
|
|
|
flayout->addRow("equation:", edit);
|
|
|
|
flayout->addRow("p1 =", spinP1);
|
|
|
|
flayout->addRow("p2 =", spinP2);
|
|
|
|
flayout->addRow("", check);
|
|
|
|
layout->addLayout(flayout);
|
2018-12-24 03:27:24 +08:00
|
|
|
layout->addWidget(plot);
|
|
|
|
|
|
|
|
// 2. now we add a JKQTPxParsedFunctionLineGraph object, which will draw the function from
|
|
|
|
// the line edit
|
|
|
|
JKQTPxParsedFunctionLineGraph* parsedFunc=new JKQTPxParsedFunctionLineGraph(plot);
|
|
|
|
plot->addGraph(parsedFunc);
|
|
|
|
// finally we connect the line edit with the graph, whenever RETURN is pressed,
|
|
|
|
// the graph is updated:
|
|
|
|
auto updateGraphFunctor=
|
|
|
|
[=]() {
|
2018-12-24 22:07:14 +08:00
|
|
|
parsedFunc->set_title(QString("user function: \\verb{"+edit->text()+"}, p_1=%1, p_2=%2").arg(spinP1->value()).arg(spinP2->value()));
|
|
|
|
parsedFunc->set_function(edit->text());
|
|
|
|
parsedFunc->set_paramsV(spinP1->value(), spinP2->value());
|
|
|
|
parsedFunc->set_displaySamplePoints(check->isChecked());
|
|
|
|
plot->update_plot();
|
2018-12-24 03:27:24 +08:00
|
|
|
};
|
|
|
|
QObject::connect(edit, &QLineEdit::returnPressed, updateGraphFunctor);
|
|
|
|
QObject::connect(edit, &QLineEdit::editingFinished, updateGraphFunctor);
|
2018-12-24 19:29:33 +08:00
|
|
|
QObject::connect(check, &QCheckBox::toggled, updateGraphFunctor);
|
2018-12-24 22:07:14 +08:00
|
|
|
QObject::connect(spinP1, &QDoubleSpinBox::editingFinished, updateGraphFunctor);
|
|
|
|
QObject::connect(spinP2, &QDoubleSpinBox::editingFinished, updateGraphFunctor);
|
|
|
|
edit->setText("sin(x*p1)*exp(-x/p2)");
|
2018-12-24 03:27:24 +08:00
|
|
|
updateGraphFunctor();
|
|
|
|
|
|
|
|
|
|
|
|
// 3. set some axis properties (we use LaTeX for nice equation rendering)
|
2018-12-28 05:52:00 +08:00
|
|
|
plot->get_xAxis()->set_axisLabel(QObject::tr("x-axis"));
|
|
|
|
plot->get_yAxis()->set_axisLabel(QObject::tr("y-axis"));
|
2018-12-24 03:27:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
// 4. scale the plot so the graph is contained
|
|
|
|
plot->setXY(-10,10,-10,10);
|
|
|
|
|
|
|
|
// show window and make it a decent size
|
|
|
|
mainWin.show();
|
|
|
|
mainWin.resize(600,400);
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|