Newer
Older
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
#include "LowHighStepInputWidget.h"
#include <QDoubleValidator>
#include <QLineEdit>
#include <QLabel>
#include <QBoxLayout>
#include <sstream>
/**
Calculate the number of bins from low,high,step.
@param low: The low limit
@param high: The high limit
@param step: The step
@return calculated number of bins.
*/
int calculateNBins(double low, double high, double step)
{
///Note that the result is being truncated here!
return int((high-low)/step);
}
/**
Calculate the step from the high, low, number of bins
@param low: The low limit
@param high: The high limit
@param nBins: The number of bins
@return calculated number of bins.
*/
double calculateStep(double low, double high, double nBins)
{
return (high-low)/nBins;
}
/// Constructor
LowHighStepInputWidget::LowHighStepInputWidget()
{
m_step = new QLineEdit();
connect(m_step, SIGNAL(editingFinished()), this, SLOT(nBinsListener()));
QDoubleValidator* stepValidator = new QDoubleValidator(0, 10000, 5, this);
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
m_step->setValidator(stepValidator);
QHBoxLayout* layout = new QHBoxLayout;
layout->addWidget(new QLabel("Step:"));
layout->addWidget(m_step);
this->setLayout(layout);
}
/*
Getter for the entry
@min : min value
@max : max value
*/
int LowHighStepInputWidget::getEntry(double min, double max) const
{
double step = m_step->text().toDouble();
return calculateNBins(min, max, step);
}
/**
Set the entry assuming that only the step will change.
@nbins : number of bins to use.
@min : minimum (low)
@max : maximum (high)
*/
void LowHighStepInputWidget::setEntry(int nBins, double min, double max)
{
double step = calculateStep(min, max, double(nBins));
std::stringstream stream;
stream << step;
m_step->setText(stream.str().c_str());
}
/// Destructor.
LowHighStepInputWidget::~LowHighStepInputWidget()
{
}
/**
Listener for internal events, pubishing a public 'changed' call
*/
void LowHighStepInputWidget::nBinsListener()
{
emit valueChanged();
}