BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
DoubleSpinBox Class Reference

Description

SpinBox for DoubleDescriptors, supporting units.

Definition at line 22 of file DoubleSpinBox.h.

Inheritance diagram for DoubleSpinBox:
[legend]
Collaboration diagram for DoubleSpinBox:
[legend]

Signals

void baseValueChanged (double newBaseValue)
 Emitted whenever the value changes. More...
 

Public Member Functions

 DoubleSpinBox (QWidget *parent, const DoubleDescriptor &d)
 Create a DoubleSpinBox with the information found in a DoubleDescriptor. More...
 
Unit baseUnit () const
 Returns the unit of the contained DoubleDescriptor. More...
 
QString displayUnitAsString () const
 The display unit as human readable string. More...
 
void setBaseValue (double baseValue)
 Set the base value (unit is the one of the contained descriptor). More...
 
void setDisplayUnit (Unit displayUnit)
 Set a display unit. More...
 
void updateValue ()
 Update the shown value to the one contained in the value descriptor. More...
 
const DoubleDescriptorvalueDescriptor () const
 The descriptor on which this spinbox operates. More...
 

Protected Member Functions

void wheelEvent (QWheelEvent *event) override
 

Private Member Functions

void onDisplayValueChanged (double newDisplayValue)
 
double toBaseValue (double displayValue) const
 
double toDisplayValue (double baseValue) const
 

Private Attributes

Unit m_displayUnit = Unit::unitless
 
bool m_showUnitAsSuffix = false
 it was decided to not show the unit as a suffix. However, this may be user selectable once, therefore the code is kept and controlled by this flag More...
 
DoubleDescriptor m_valueDescriptor
 

Constructor & Destructor Documentation

◆ DoubleSpinBox()

DoubleSpinBox::DoubleSpinBox ( QWidget *  parent,
const DoubleDescriptor d 
)

Create a DoubleSpinBox with the information found in a DoubleDescriptor.

The spin box will be fully initialized (tooltip, limits, unit, current value, size policy). Furthermore, the spin box will prohibit accidental changes by the mouse wheel. Otherwise it would be dangerous if the spin box is on a scrollable form - unintended and unnoticed changes would take place when just scrolling through the form.

Definition at line 19 of file DoubleSpinBox.cpp.

20  : QDoubleSpinBox(parent)
22 {
23  setFocusPolicy(Qt::StrongFocus);
25  setToolTip(d.tooltip);
26  setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
27 
28  if (std::holds_alternative<QString>(m_valueDescriptor.unit))
30  else
31  setDisplayUnit(std::get<Unit>(m_valueDescriptor.unit));
32 
33  QObject::connect(this, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
35 }
@ other
The unit has no enum value defined in here (e.g. when defined as an explicit string)
variant< QString, Unit > unit
Unit of the value (internal unit only!)
RealLimits limits
Limits of the value.
QString tooltip
Tooltip text.
int decimals
numbers of decimals to be shown in an edit control
DoubleDescriptor m_valueDescriptor
Definition: DoubleSpinBox.h:79
void setDisplayUnit(Unit displayUnit)
Set a display unit.
void onDisplayValueChanged(double newDisplayValue)
void configSpinbox(QDoubleSpinBox *spinBox, int decimals, const RealLimits &limits)
Definition: EditUtil.cpp:47

References GUI::View::EditUtil::configSpinbox(), DoubleDescriptor::decimals, DoubleDescriptor::limits, m_valueDescriptor, onDisplayValueChanged(), other, setDisplayUnit(), DoubleDescriptor::tooltip, and DoubleDescriptor::unit.

Here is the call graph for this function:

Member Function Documentation

◆ baseUnit()

Unit DoubleSpinBox::baseUnit ( ) const

Returns the unit of the contained DoubleDescriptor.

If the unit is defined as a string, this method returns Unit::other. To get the string, use valueDescriptor().unit

Definition at line 94 of file DoubleSpinBox.cpp.

95 {
96  if (std::holds_alternative<QString>(m_valueDescriptor.unit))
97  return Unit::other;
98 
99  return std::get<Unit>(m_valueDescriptor.unit);
100 }

References m_valueDescriptor, other, and DoubleDescriptor::unit.

Referenced by toBaseValue(), toDisplayValue(), and MultiLayerForm::updateUnits().

◆ baseValueChanged

◆ displayUnitAsString()

QString DoubleSpinBox::displayUnitAsString ( ) const

The display unit as human readable string.

Definition at line 63 of file DoubleSpinBox.cpp.

64 {
65  if (std::holds_alternative<QString>(m_valueDescriptor.unit))
66  return std::get<QString>(m_valueDescriptor.unit);
67 
69 }
QString unitAsString(const Unit &unit)
Returns the string for the given unit.
Definition: Unit.cpp:58

References m_displayUnit, m_valueDescriptor, DoubleDescriptor::unit, and unitAsString().

Referenced by setDisplayUnit(), and LayerEditorUtils::updateLabelUnit().

Here is the call graph for this function:

◆ onDisplayValueChanged()

void DoubleSpinBox::onDisplayValueChanged ( double  newDisplayValue)
private

Definition at line 89 of file DoubleSpinBox.cpp.

90 {
91  emit baseValueChanged(toBaseValue(newDisplayValue));
92 }
double toBaseValue(double displayValue) const
void baseValueChanged(double newBaseValue)
Emitted whenever the value changes.

References baseValueChanged(), and toBaseValue().

Referenced by DoubleSpinBox().

Here is the call graph for this function:

◆ setBaseValue()

void DoubleSpinBox::setBaseValue ( double  baseValue)

Set the base value (unit is the one of the contained descriptor).

Definition at line 76 of file DoubleSpinBox.cpp.

77 {
78  setValue(toDisplayValue(baseValue));
79 }
double toDisplayValue(double baseValue) const

References toDisplayValue().

Referenced by SampleEditorController::setDoubleFromUndo(), and updateValue().

Here is the call graph for this function:

◆ setDisplayUnit()

void DoubleSpinBox::setDisplayUnit ( Unit  displayUnit)

Set a display unit.

The caller has to make sure that the new display unit has a conversion to/from the contained base value unit.

Definition at line 37 of file DoubleSpinBox.cpp.

38 {
39  m_displayUnit = displayUnit;
40 
41  if (m_showUnitAsSuffix) {
42  const QString suffix = displayUnitAsString();
43  if (suffix.isEmpty())
44  setSuffix("");
45  else
46  setSuffix(" " + suffix);
47  }
48 
49  QSignalBlocker b(this);
51 }
function< double()> get
function to get the current value
bool m_showUnitAsSuffix
it was decided to not show the unit as a suffix. However, this may be user selectable once,...
Definition: DoubleSpinBox.h:83
QString displayUnitAsString() const
The display unit as human readable string.

References displayUnitAsString(), DoubleDescriptor::get, m_displayUnit, m_showUnitAsSuffix, m_valueDescriptor, and toDisplayValue().

Referenced by DoubleSpinBox(), and MultiLayerForm::updateUnits().

Here is the call graph for this function:

◆ toBaseValue()

double DoubleSpinBox::toBaseValue ( double  displayValue) const
private

Definition at line 58 of file DoubleSpinBox.cpp.

59 {
60  return convert(displayValue, m_displayUnit, baseUnit());
61 }
double convert(double d, Unit from, Unit to)
Convert the given value d from unit "from" to unit "to" Throws an exception if no conversion possible...
Definition: Unit.cpp:20
Unit baseUnit() const
Returns the unit of the contained DoubleDescriptor.

References baseUnit(), convert(), and m_displayUnit.

Referenced by onDisplayValueChanged().

Here is the call graph for this function:

◆ toDisplayValue()

double DoubleSpinBox::toDisplayValue ( double  baseValue) const
private

Definition at line 53 of file DoubleSpinBox.cpp.

54 {
55  return convert(baseValue, baseUnit(), m_displayUnit);
56 }

References baseUnit(), convert(), and m_displayUnit.

Referenced by setBaseValue(), and setDisplayUnit().

Here is the call graph for this function:

◆ updateValue()

void DoubleSpinBox::updateValue ( )

Update the shown value to the one contained in the value descriptor.

No signal will be emitted if the new value has changed.

Definition at line 102 of file DoubleSpinBox.cpp.

103 {
104  QSignalBlocker b(this);
106 }
void setBaseValue(double baseValue)
Set the base value (unit is the one of the contained descriptor).

References DoubleDescriptor::get, m_valueDescriptor, and setBaseValue().

Referenced by AxisPropertyEditor::updateData(), and ParticleLayoutForm::updateDensityValue().

Here is the call graph for this function:

◆ valueDescriptor()

const DoubleDescriptor & DoubleSpinBox::valueDescriptor ( ) const

The descriptor on which this spinbox operates.

Definition at line 71 of file DoubleSpinBox.cpp.

72 {
73  return m_valueDescriptor;
74 }

References m_valueDescriptor.

Referenced by SampleEditorController::setDoubleFromUndo().

◆ wheelEvent()

void DoubleSpinBox::wheelEvent ( QWheelEvent *  event)
overrideprotected

Definition at line 81 of file DoubleSpinBox.cpp.

82 {
83  if (!hasFocus())
84  event->ignore();
85  else
86  QDoubleSpinBox::wheelEvent(event);
87 }

Member Data Documentation

◆ m_displayUnit

Unit DoubleSpinBox::m_displayUnit = Unit::unitless
private

Definition at line 77 of file DoubleSpinBox.h.

Referenced by displayUnitAsString(), setDisplayUnit(), toBaseValue(), and toDisplayValue().

◆ m_showUnitAsSuffix

bool DoubleSpinBox::m_showUnitAsSuffix = false
private

it was decided to not show the unit as a suffix. However, this may be user selectable once, therefore the code is kept and controlled by this flag

Definition at line 83 of file DoubleSpinBox.h.

Referenced by setDisplayUnit().

◆ m_valueDescriptor

DoubleDescriptor DoubleSpinBox::m_valueDescriptor
private

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