-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqtfunctionplotterplot2ddialog.cpp
More file actions
152 lines (125 loc) · 3.49 KB
/
qtfunctionplotterplot2ddialog.cpp
File metadata and controls
152 lines (125 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <cassert>
#include <boost/lexical_cast.hpp>
#include <QDesktopWidget>
#include <QDoubleSpinBox>
#include <QGridLayout>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#if QWT_VERSION >= 0x060100
#include <qwt_point_data.h>
#endif
//#include "fparser.hh"
#include "functionplottermaindialog.h"
#include "qtfunctionplotterplot2ddialog.h"
#include "testtimer.h"
#include "trace.h"
#include "ui_qtfunctionplotterplot2ddialog.h"
#pragma GCC diagnostic pop
ribi::QtFunctionPlotterPlot2dDialog::QtFunctionPlotterPlot2dDialog(QWidget *parent)
: QtHideAndShowDialog(parent),
ui(new Ui::QtFunctionPlotterPlot2dDialog),
m_curve(new QwtPlotCurve),
m_plot(new QwtPlot)
{
#ifndef NDEBUG
Test();
#endif
ui->setupUi(this);
//Create plot
{
assert(!ui->plot_contents->layout());
QGridLayout * const layout = new QGridLayout;
ui->plot_contents->setLayout(layout);
layout->addWidget(m_plot);
#ifdef _WIN32
m_plot->setCanvasBackground(QBrush(QColor(255,255,255)));
#else
m_plot->setCanvasBackground(QColor(255,255,255));
#endif
m_curve->attach(m_plot);
m_curve->setStyle(QwtPlotCurve::Lines);
m_curve->setPen(QPen(QColor(128,128,128)));
}
QObject::connect(
this->ui->edit_equation,
&QLineEdit::textChanged,
this,
&ribi::QtFunctionPlotterPlot2dDialog::OnAnyChange
);
QObject::connect(
this->ui->box_minx,
static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
this,
&ribi::QtFunctionPlotterPlot2dDialog::OnAnyChange
);
QObject::connect(
this->ui->box_maxx,
static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
this,
&ribi::QtFunctionPlotterPlot2dDialog::OnAnyChange
);
ui->box_minx->setValue(-1.0);
ui->box_maxx->setValue(1.0);
ui->edit_equation->setText("cos(x)");
{
//Put the dialog in the screen center at 50% x 50% of its size
const QRect screen = QApplication::desktop()->screenGeometry();
this->setGeometry(0,0,screen.width() / 2,screen.height() /2 );
this->move( screen.center() - this->rect().center() );
}
}
ribi::QtFunctionPlotterPlot2dDialog::~QtFunctionPlotterPlot2dDialog() noexcept
{
delete ui;
}
void ribi::QtFunctionPlotterPlot2dDialog::OnAnyChange() noexcept
{
const std::string formula { ui->edit_equation->text().toStdString() };
const double x_min = ui->box_minx->value();
const double x_max = ui->box_maxx->value();
const int n_cols = ui->area_plot->width();
std::vector<double> v_x;
std::vector<double> v_y;
try
{
const FunctionPlotterMainDialog d(
formula,
x_min,
x_max,
n_cols
);
v_x = d.GetXs();
v_y = d.GetYs();
this->setWindowTitle("Function plotted successfully");
}
catch (std::runtime_error& e)
{
this->setWindowTitle(e.what());
}
//Plot
#if QWT_VERSION >= 0x060100 || !WIN32
m_curve->setData(new QwtPointArrayData(&v_x[0],&v_y[0],v_y.size()));
#else
m_curve->setData(&v_x[0],&v_y[0],v_y.size());
#endif
m_plot->replot();
}
void ribi::QtFunctionPlotterPlot2dDialog::resizeEvent(QResizeEvent *)
{
OnAnyChange();
}
#ifndef NDEBUG
void ribi::QtFunctionPlotterPlot2dDialog::Test() noexcept
{
{
static bool is_tested{false};
if (is_tested) return;
is_tested = true;
}
const TestTimer test_timer(__func__,__FILE__,1.0);
}
#endif