BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelView::ScientificSpinBox Class Reference
Inheritance diagram for ModelView::ScientificSpinBox:
[legend]
Collaboration diagram for ModelView::ScientificSpinBox:
[legend]

Signals

void valueChanged (double value)
 

Public Member Functions

 ScientificSpinBox (QWidget *parent=nullptr)
 
 ~ScientificSpinBox () override
 
int decimals () const
 
void fixup (QString &) const override
 
double maximum () const
 
double minimum () const
 
void setDecimals (int)
 
void setMaximum (double max)
 
void setMinimum (double min)
 
void setSingleStep (double step)
 
void setValue (double val)
 
double singleStep () const
 
void stepBy (int steps) override
 
QValidator::State validate (QString &, int &) const override
 
double value () const
 

Static Public Member Functions

static double round (double val, int decimals)
 
static double toDouble (QString text, const QDoubleValidator &validator, double min, double max, double default_value)
 
static QString toString (double val, int decimal_points)
 

Protected Member Functions

QAbstractSpinBox::StepEnabled stepEnabled () const override
 

Properties

double value
 

Private Member Functions

bool inRange (double val) const
 
void updateText ()
 
void updateValue ()
 

Private Attributes

int m_decimals
 
double m_max
 
double m_min
 
double m_step
 
QDoubleValidator m_validator
 
double m_value
 

Detailed Description

Definition at line 23 of file scientificspinbox.h.

Constructor & Destructor Documentation

◆ ScientificSpinBox()

ScientificSpinBox::ScientificSpinBox ( QWidget *  parent = nullptr)

Definition at line 32 of file scientificspinbox.cpp.

33  : QAbstractSpinBox(parent)
34  , m_value(0.0)
35  , m_min(-max_val)
36  , m_max(max_val)
37  , m_step(1.0)
39 {
40  QLocale locale;
41  locale.setNumberOptions(QLocale::RejectGroupSeparator);
42  m_validator.setLocale(locale);
43  m_validator.setNotation(QDoubleValidator::ScientificNotation);
44 
45  connect(this, &QAbstractSpinBox::editingFinished, this, &ScientificSpinBox::updateValue);
46 }
const int default_double_decimals

References m_validator, and updateValue().

Here is the call graph for this function:

◆ ~ScientificSpinBox()

ScientificSpinBox::~ScientificSpinBox ( )
overridedefault

Member Function Documentation

◆ decimals()

int ScientificSpinBox::decimals ( ) const

Definition at line 114 of file scientificspinbox.cpp.

115 {
116  return m_decimals;
117 }

References m_decimals.

Referenced by round().

◆ fixup()

void ModelView::ScientificSpinBox::fixup ( QString &  ) const
inlineoverride

Definition at line 48 of file scientificspinbox.h.

48 {}

◆ inRange()

bool ScientificSpinBox::inRange ( double  val) const
private

Definition at line 165 of file scientificspinbox.cpp.

166 {
167  return val >= m_min && val <= m_max;
168 }

References m_max, and m_min.

Referenced by stepBy().

◆ maximum()

double ScientificSpinBox::maximum ( ) const

Definition at line 94 of file scientificspinbox.cpp.

95 {
96  return m_max;
97 }

References m_max.

◆ minimum()

double ScientificSpinBox::minimum ( ) const

Definition at line 82 of file scientificspinbox.cpp.

83 {
84  return m_min;
85 }

References m_min.

◆ round()

double ScientificSpinBox::round ( double  val,
int  decimals 
)
static

Definition at line 147 of file scientificspinbox.cpp.

148 {
149  char notation = useExponentialNotation(val) ? 'e' : 'f';
150  return QString::number(val, notation, decimals).toDouble();
151 }

References decimals().

Referenced by setValue(), stepBy(), TEST_F(), and value().

Here is the call graph for this function:

◆ setDecimals()

void ScientificSpinBox::setDecimals ( int  val)

Definition at line 106 of file scientificspinbox.cpp.

107 {
108  if (val <= 0)
109  return;
110  m_decimals = val;
111  setValue(m_value);
112 }

References m_decimals, m_value, and setValue().

Referenced by ModelView::ScientificSpinBoxEditor::setDecimals().

Here is the call graph for this function:

◆ setMaximum()

void ScientificSpinBox::setMaximum ( double  max)

Definition at line 99 of file scientificspinbox.cpp.

100 {
101  m_max = max;
102  if (m_value > m_max)
103  setValue(m_max);
104 }

References m_max, m_value, and setValue().

Referenced by ModelView::ScientificSpinBoxEditor::setRange().

Here is the call graph for this function:

◆ setMinimum()

void ScientificSpinBox::setMinimum ( double  min)

Definition at line 87 of file scientificspinbox.cpp.

88 {
89  m_min = min;
90  if (m_value < m_min)
91  setValue(m_min);
92 }

References m_min, m_value, and setValue().

Referenced by ModelView::ScientificSpinBoxEditor::setRange().

Here is the call graph for this function:

◆ setSingleStep()

void ScientificSpinBox::setSingleStep ( double  step)

Definition at line 77 of file scientificspinbox.cpp.

78 {
79  m_step = step;
80 }

References m_step.

Referenced by ModelView::ScientificSpinBoxEditor::setSingleStep().

◆ setValue()

void ScientificSpinBox::setValue ( double  val)

Definition at line 57 of file scientificspinbox.cpp.

58 {
59  double old_val = m_value;
60  m_value = round(val, m_decimals);
61  updateText();
62  if (std::abs(old_val - m_value) > min_val)
63  emit valueChanged(m_value);
64 }
static double round(double val, int decimals)
void valueChanged(double value)

References m_decimals, m_value, round(), updateText(), and valueChanged().

Referenced by setDecimals(), setMaximum(), setMinimum(), stepBy(), ModelView::ScientificSpinBoxEditor::update_components(), and updateValue().

Here is the call graph for this function:

◆ singleStep()

double ScientificSpinBox::singleStep ( ) const

Definition at line 72 of file scientificspinbox.cpp.

73 {
74  return m_step;
75 }

References m_step.

◆ stepBy()

void ScientificSpinBox::stepBy ( int  steps)
override

Definition at line 119 of file scientificspinbox.cpp.

120 {
121  double new_val = round(m_value + m_step * steps, m_decimals);
122  if (inRange(new_val))
123  setValue(new_val);
124 }
bool inRange(double val) const

References inRange(), m_decimals, m_step, m_value, round(), and setValue().

Here is the call graph for this function:

◆ stepEnabled()

QAbstractSpinBox::StepEnabled ScientificSpinBox::stepEnabled ( ) const
overrideprotected

Definition at line 153 of file scientificspinbox.cpp.

154 {
155  return isReadOnly() ? StepNone : StepUpEnabled | StepDownEnabled;
156 }

◆ toDouble()

double ScientificSpinBox::toDouble ( QString  text,
const QDoubleValidator &  validator,
double  min,
double  max,
double  default_value 
)
static

Definition at line 134 of file scientificspinbox.cpp.

136 {
137  int pos = 0;
138  if (validator.validate(text, pos) == QValidator::Acceptable) {
139  double new_val = validator.locale().toDouble(text);
140  if (std::abs(new_val) < min_val)
141  new_val = 0.0;
142  return new_val >= min && new_val <= max ? new_val : default_value;
143  }
144  return default_value;
145 }

Referenced by TEST_F(), updateValue(), and value().

◆ toString()

QString ScientificSpinBox::toString ( double  val,
int  decimal_points 
)
static

Definition at line 126 of file scientificspinbox.cpp.

127 {
128  QString result = useExponentialNotation(val) ? QString::number(val, 'e', decimal_points)
129  : QString::number(val, 'f', decimal_points);
130 
131  return result.replace(QRegExp("(\\.?0+)?((e{1}[\\+|-]{1})(0+)?([1-9]{1}.*))?$"), "\\3\\5");
132 }

Referenced by ModelView::DefaultCellDecorator::cellText(), TEST_F(), and updateText().

◆ updateText()

void ScientificSpinBox::updateText ( )
private

Definition at line 158 of file scientificspinbox.cpp.

159 {
160  QString new_text = toString(m_value, m_decimals);
161  if (new_text != text())
162  lineEdit()->setText(new_text);
163 }
static QString toString(double val, int decimal_points)

References m_decimals, m_value, and toString().

Referenced by setValue().

Here is the call graph for this function:

◆ updateValue()

void ScientificSpinBox::updateValue ( )
private

Definition at line 66 of file scientificspinbox.cpp.

67 {
68  double new_val = toDouble(text(), m_validator, m_min, m_max, m_value);
69  setValue(new_val);
70 }
static double toDouble(QString text, const QDoubleValidator &validator, double min, double max, double default_value)

References m_max, m_min, m_validator, m_value, setValue(), and toDouble().

Referenced by ScientificSpinBox().

Here is the call graph for this function:

◆ validate()

QValidator::State ModelView::ScientificSpinBox::validate ( QString &  ,
int &   
) const
inlineoverride

Definition at line 47 of file scientificspinbox.h.

47 { return QValidator::Acceptable; }

◆ value()

double ScientificSpinBox::value ( ) const

Definition at line 50 of file scientificspinbox.cpp.

51 {
52  // return last acceptable input (required for the proper focus-out behaviour)
53  double val = toDouble(text(), m_validator, m_min, m_max, m_value);
54  return round(val, m_decimals);
55 }

References m_decimals, m_max, m_min, m_validator, m_value, round(), and toDouble().

Here is the call graph for this function:

◆ valueChanged

void ModelView::ScientificSpinBox::valueChanged ( double  value)
signal

Member Data Documentation

◆ m_decimals

int ModelView::ScientificSpinBox::m_decimals
private

Definition at line 68 of file scientificspinbox.h.

Referenced by decimals(), setDecimals(), setValue(), stepBy(), updateText(), and value().

◆ m_max

double ModelView::ScientificSpinBox::m_max
private

Definition at line 66 of file scientificspinbox.h.

Referenced by inRange(), maximum(), setMaximum(), updateValue(), and value().

◆ m_min

double ModelView::ScientificSpinBox::m_min
private

Definition at line 66 of file scientificspinbox.h.

Referenced by inRange(), minimum(), setMinimum(), updateValue(), and value().

◆ m_step

double ModelView::ScientificSpinBox::m_step
private

Definition at line 67 of file scientificspinbox.h.

Referenced by setSingleStep(), singleStep(), and stepBy().

◆ m_validator

QDoubleValidator ModelView::ScientificSpinBox::m_validator
private

Definition at line 69 of file scientificspinbox.h.

Referenced by ScientificSpinBox(), updateValue(), and value().

◆ m_value

double ModelView::ScientificSpinBox::m_value
private

Property Documentation

◆ value

double ModelView::ScientificSpinBox::value
readwrite

The documentation for this class was generated from the following files: