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 +
8
9#include <QCloseEvent>
10#include <QHBoxLayout>
11#include <QLineEdit>
12#include <cfloat>
13#include <cmath>
14
15DoubleSpinBox::DoubleSpinBox(const char format, QWidget *parent)
16 : QAbstractSpinBox(parent), d_format(format), d_min_val(-DBL_MAX), d_max_val(DBL_MAX), d_value(0.0), d_step(0.1),
17 d_prec(14) {
18 if (format == 'f')
19 d_prec = 1;
20
21 setFocusPolicy(Qt::StrongFocus);
22 lineEdit()->setText(locale().toString(d_value, d_format, d_prec));
23 setWrapping(false);
24 connect(this, SIGNAL(editingFinished()), this, SLOT(interpretText()));
25}
26
28 if (d_step != val && val < d_max_val)
29 d_step = val;
30}
31
32void DoubleSpinBox::setMaximum(double max) {
33 if (max == d_max_val || max > DBL_MAX)
34 return;
35
36 d_max_val = max;
37}
38
40
41void DoubleSpinBox::setMinimum(double min) {
42 if (min == d_min_val || min < -DBL_MAX)
43 return;
44
45 d_min_val = min;
46}
47
49
50void DoubleSpinBox::setRange(double min, double max) {
51 setMinimum(min);
52 setMaximum(max);
53}
54
66 // RJT: Keep our version of this, which contains a bug fix (see [10521]).
67 // Also, there are lines referring to methods that don't exist in our (older)
68 // MyParser class.
69 bool ok = false;
70 double value = locale().toDouble(text(), &ok);
71 if (ok && setValue(value)) {
72 if (notify)
74 } else {
75 QString val = text().remove(",");
76 value = locale().toDouble(val, &ok);
77 if (ok && setValue(value)) {
78 if (notify)
80 } else {
81 // Check for any registered test strings that map to a given value
82 for (auto &specialTextMapping : m_specialTextMappings) {
83 if (specialTextMapping.first == text()) {
84 // Found a matching string, try to set the value
85 if (setValue(specialTextMapping.second)) {
86 lineEdit()->setText(text());
87 if (notify)
89 }
90 }
91 }
92
93 lineEdit()->setText(textFromValue(d_value));
94 }
95 }
96}
97
106void DoubleSpinBox::addSpecialTextMapping(const QString &text, double value) { m_specialTextMappings[text] = value; }
107
108void DoubleSpinBox::stepBy(int steps) {
109 double val = d_value + steps * d_step;
110 if (fabs(fabs(d_value) - d_step) < 1e-14 && d_value * steps < 0) // possible zero
111 val = 0.0;
112
113 if (setValue(val))
114 emit valueChanged(d_value);
115}
116
117QAbstractSpinBox::StepEnabled DoubleSpinBox::stepEnabled() const {
118 QAbstractSpinBox::StepEnabled stepDown = QAbstractSpinBox::StepNone;
119 if (d_value > d_min_val)
120 stepDown = StepDownEnabled;
121
122 QAbstractSpinBox::StepEnabled stepUp = QAbstractSpinBox::StepNone;
123 if (d_value < d_max_val)
124 stepUp = StepUpEnabled;
125
126 return stepDown | stepUp;
127}
128
130 const bool notify(false);
131 interpretText(notify);
132 return d_value;
133}
134
135bool DoubleSpinBox::setValue(double val) {
136 if (val >= d_min_val && val <= d_max_val) {
137 d_value = val;
138 lineEdit()->setText(textFromValue(d_value));
139 return true;
140 }
141
142 lineEdit()->setText(textFromValue(d_value));
143 return false;
144}
145
146QString DoubleSpinBox::textFromValue(double value) const {
147 if (!specialValueText().isEmpty() && value == d_min_val)
148 return specialValueText();
149
150 if (d_prec <= 14)
151 return locale().toString(value, d_format, d_prec);
152
153 return locale().toString(value, d_format, 6);
154}
155
156QValidator::State DoubleSpinBox::validate(QString & /*input*/, int & /*pos*/) const { return QValidator::Acceptable; }
157
158void DoubleSpinBox::focusInEvent(QFocusEvent *e) {
159 emit activated(this);
160 return QAbstractSpinBox::focusInEvent(e);
161}
162
163/*****************************************************************************
164 *
165 * Class RangeLimitBox
166 *
167 *****************************************************************************/
168
169RangeLimitBox::RangeLimitBox(LimitType type, QWidget *parent) : QWidget(parent), d_type(type) {
170 d_checkbox = new QCheckBox();
172 d_spin_box->setSpecialValueText(" ");
173 d_spin_box->setValue(-DBL_MAX);
174 d_spin_box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
175 d_spin_box->setEnabled(false);
176
177 auto *l = new QHBoxLayout(this);
178 l->setMargin(0);
179 l->setSpacing(0);
180 l->addWidget(d_checkbox);
181 l->addWidget(d_spin_box);
182
183 setFocusPolicy(Qt::StrongFocus);
184 setFocusProxy(d_spin_box);
185 connect(d_checkbox, SIGNAL(toggled(bool)), d_spin_box, SLOT(setEnabled(bool)));
186}
187
189 if (d_checkbox->isChecked())
190 return d_spin_box->value();
191
192 double val = -DBL_MAX;
193 if (d_type == RightLimit)
194 return DBL_MAX;
195 return val;
196}
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.
void setSingleStep(double val)
void setRange(double min, double max)
QValidator::State validate(QString &input, int &pos) const override
std::map< QString, double > m_specialTextMappings
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
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