Mantid
Loading...
Searching...
No Matches
DoubleSpinBox.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7/***************************************************************************
8 File : DoubleSpinBox.cpp
9 Project : QtiPlot
10 --------------------------------------------------------------------
11 * *
12 * You should have received a copy of the GNU General Public License *
13 * along with this program; if not, write to the Free Software *
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
15 * Boston, MA 02110-1301 USA *
16 * *
17 ***************************************************************************/
19
20//#include <MyParser.h>
21#include <QCloseEvent>
22#include <QHBoxLayout>
23#include <QLineEdit>
24#include <cfloat>
25#include <cmath>
26
27DoubleSpinBox::DoubleSpinBox(const char format, QWidget *parent)
28 : QAbstractSpinBox(parent), d_format(format), d_min_val(-DBL_MAX), d_max_val(DBL_MAX), d_value(0.0), d_step(0.1),
29 d_prec(14) {
30 if (format == 'f')
31 d_prec = 1;
32
33 setFocusPolicy(Qt::StrongFocus);
34 lineEdit()->setText(locale().toString(d_value, d_format, d_prec));
35 setWrapping(false);
36 connect(this, SIGNAL(editingFinished()), this, SLOT(interpretText()));
37}
38
40 if (d_step != val && val < d_max_val)
41 d_step = val;
42}
43
44void DoubleSpinBox::setMaximum(double max) {
45 if (max == d_max_val || max > DBL_MAX)
46 return;
47
48 d_max_val = max;
49}
50
52
53void DoubleSpinBox::setMinimum(double min) {
54 if (min == d_min_val || min < -DBL_MAX)
55 return;
56
57 d_min_val = min;
58}
59
61
62void DoubleSpinBox::setRange(double min, double max) {
63 setMinimum(min);
64 setMaximum(max);
65}
66
78 // RJT: Keep our version of this, which contains a bug fix (see [10521]).
79 // Also, there are lines referring to methods that don't exist in our (older)
80 // MyParser class.
81 bool ok = false;
82 double value = locale().toDouble(text(), &ok);
83 if (ok && setValue(value)) {
84 if (notify)
86 } else {
87 QString val = text().remove(",");
88 value = locale().toDouble(val, &ok);
89 if (ok && setValue(value)) {
90 if (notify)
92 } else {
93 // Check for any registered test strings that map to a given value
94 for (auto &specialTextMapping : m_specialTextMappings) {
95 if (specialTextMapping.first == text()) {
96 // Found a matching string, try to set the value
97 if (setValue(specialTextMapping.second)) {
98 lineEdit()->setText(text());
99 if (notify)
100 emit valueChanged(d_value);
101 }
102 }
103 }
104
105 lineEdit()->setText(textFromValue(d_value));
106 }
107 }
108}
109
118void DoubleSpinBox::addSpecialTextMapping(const QString &text, double value) { m_specialTextMappings[text] = value; }
119
120void DoubleSpinBox::stepBy(int steps) {
121 double val = d_value + steps * d_step;
122 if (fabs(fabs(d_value) - d_step) < 1e-14 && d_value * steps < 0) // possible zero
123 val = 0.0;
124
125 if (setValue(val))
126 emit valueChanged(d_value);
127}
128
129QAbstractSpinBox::StepEnabled DoubleSpinBox::stepEnabled() const {
130 QAbstractSpinBox::StepEnabled stepDown = QAbstractSpinBox::StepNone;
131 if (d_value > d_min_val)
132 stepDown = StepDownEnabled;
133
134 QAbstractSpinBox::StepEnabled stepUp = QAbstractSpinBox::StepNone;
135 if (d_value < d_max_val)
136 stepUp = StepUpEnabled;
137
138 return stepDown | stepUp;
139}
140
142 const bool notify(false);
143 interpretText(notify);
144 return d_value;
145}
146
147bool DoubleSpinBox::setValue(double val) {
148 if (val >= d_min_val && val <= d_max_val) {
149 d_value = val;
150 lineEdit()->setText(textFromValue(d_value));
151 return true;
152 }
153
154 lineEdit()->setText(textFromValue(d_value));
155 return false;
156}
157
158QString DoubleSpinBox::textFromValue(double value) const {
159 if (!specialValueText().isEmpty() && value == d_min_val)
160 return specialValueText();
161
162 if (d_prec <= 14)
163 return locale().toString(value, d_format, d_prec);
164
165 return locale().toString(value, d_format, 6);
166}
167
168QValidator::State DoubleSpinBox::validate(QString & /*input*/, int & /*pos*/) const { return QValidator::Acceptable; }
169
170void DoubleSpinBox::focusInEvent(QFocusEvent *e) {
171 emit activated(this);
172 return QAbstractSpinBox::focusInEvent(e);
173}
174
175/*****************************************************************************
176 *
177 * Class RangeLimitBox
178 *
179 *****************************************************************************/
180
181RangeLimitBox::RangeLimitBox(LimitType type, QWidget *parent) : QWidget(parent), d_type(type) {
182 d_checkbox = new QCheckBox();
184 d_spin_box->setSpecialValueText(" ");
185 d_spin_box->setValue(-DBL_MAX);
186 d_spin_box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
187 d_spin_box->setEnabled(false);
188
189 auto *l = new QHBoxLayout(this);
190 l->setMargin(0);
191 l->setSpacing(0);
192 l->addWidget(d_checkbox);
193 l->addWidget(d_spin_box);
194
195 setFocusPolicy(Qt::StrongFocus);
196 setFocusProxy(d_spin_box);
197 connect(d_checkbox, SIGNAL(toggled(bool)), d_spin_box, SLOT(setEnabled(bool)));
198}
199
201 if (d_checkbox->isChecked())
202 return d_spin_box->value();
203
204 double val = -DBL_MAX;
205 if (d_type == RightLimit)
206 return DBL_MAX;
207 return val;
208}
double value
The value of the point.
Definition: FitMW.cpp:51
#define fabs(x)
Definition: Matrix.cpp:22
A QDoubleSpinBox allowing to customize numbers display with respect to.
Definition: DoubleSpinBox.h:31
void setSingleStep(double val)
void setRange(double min, double max)
double d_min_val
Definition: DoubleSpinBox.h:85
QValidator::State validate(QString &input, int &pos) const override
std::map< QString, double > m_specialTextMappings
Definition: DoubleSpinBox.h:93
double getMinimum()
void setMaximum(double max)
void setMinimum(double min)
void valueChanged(double d)
void interpretText(bool notify=true)
Interpret the text and update the stored value.
void addSpecialTextMapping(const QString &text, double value)
Adds a mapping from string whihc may be entered into the edit box and a double value.
double getMaximum()
DoubleSpinBox(const char format='g', QWidget *parent=nullptr)
Constructor.
QString textFromValue(double value) const
void focusInEvent(QFocusEvent *) override
double d_max_val
Definition: DoubleSpinBox.h:86
void activated(DoubleSpinBox *)
Signal emitted when the spin box gains focus.
void stepBy(int steps) override
bool setValue(double val)
StepEnabled stepEnabled() const override
DoubleSpinBox * d_spin_box
RangeLimitBox(LimitType type, QWidget *parent=nullptr)
QCheckBox * d_checkbox
LimitType d_type