-
Notifications
You must be signed in to change notification settings - Fork 0
/
qttesthistogrammaindialog.cpp
103 lines (84 loc) · 2.41 KB
/
qttesthistogrammaindialog.cpp
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
#include "qttesthistogrammaindialog.h"
#include <fstream>
#include <random>
#include <QDesktopWidget>
#include <QFileDialog>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_grid.h>
#include <qwt_plot.h>
#include <qwt_plot_barchart.h>
#include <qwt_plot_curve.h>
#include "histogram.h"
#include "ui_qttesthistogrammaindialog.h"
#if QWT_VERSION >= 0x060100 || !WIN32
#include "qwt_point_data.h"
#endif
ribi::thst::QtMainDialog::QtMainDialog(QWidget *parent) :
QtHideAndShowDialog(parent),
ui(new Ui::QtTestHistogramMainDialog),
m_curve_histogram(new QwtPlotCurve)
{
#ifndef NDEBUG
Test();
#endif
ui->setupUi(this);
ui->plot_histogram->setTitle("Histogram");
//Add grid
{
QwtPlotGrid * const grid = new QwtPlotGrid;
grid->setPen(QPen(QColor(128,128,128)));
grid->attach(ui->plot_histogram);
}
//Add zoomer
{
new QwtPlotZoomer(ui->plot_histogram->canvas());
}
ui->plot_histogram->setCanvasBackground(QColor(255,255,255));
m_curve_histogram->attach(ui->plot_histogram);
m_curve_histogram->setStyle(QwtPlotCurve::Lines);
m_curve_histogram->setPen(QPen(QColor(0,0,0)));
QObject::connect(ui->box_n,SIGNAL(valueChanged(int)),this,SLOT(Run()));
QObject::connect(ui->box_mean,SIGNAL(valueChanged(double)),this,SLOT(Run()));
QObject::connect(ui->box_stddev,SIGNAL(valueChanged(double)),this,SLOT(Run()));
Run();
{
//Put the dialog in the screen center
const QRect screen = QApplication::desktop()->screenGeometry();
this->setGeometry(0,0,screen.width() * 8 / 10,screen.height() * 8 / 10);
this->move( screen.center() - this->rect().center() );
}
}
ribi::thst::QtMainDialog::~QtMainDialog()
{
delete ui;
}
void ribi::thst::QtMainDialog::Run()
{
static std::mt19937 mt(42);
std::normal_distribution<double> d(
ui->box_mean->value(),
ui->box_stddev->value()
);
const int n{ui->box_n->value()};
std::vector<double> v;
for (int i=0; i!=n; ++i)
{
const double x{d(mt)};
v.push_back(x);
}
Histogram histogram(
v,
*std::min_element(std::begin(v),std::end(v)) - 1.0,
*std::max_element(std::begin(v),std::end(v)) + 1.0,
1 + std::sqrt(n)
);
std::vector<double> xs;
std::vector<double> ys;
for (const auto& h: histogram.GetHistogram())
{
xs.push_back(h.GetMid());
ys.push_back(static_cast<double>(h.GetCnt()));
}
m_curve_histogram->setData(new QwtPointArrayData(&xs[0],&ys[0],ys.size()));
ui->plot_histogram->replot();
}