BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
DoubleSpinBox.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Common/DoubleSpinBox.cpp
6 //! @brief Implements class DoubleSpinBox
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "GUI/View/Tool/EditUtil.h"
17 #include <QWheelEvent>
18 
20  : QDoubleSpinBox(parent)
21  , m_valueDescriptor(d)
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 }
36 
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 }
52 
53 double DoubleSpinBox::toDisplayValue(double baseValue) const
54 {
55  return convert(baseValue, baseUnit(), m_displayUnit);
56 }
57 
58 double DoubleSpinBox::toBaseValue(double displayValue) const
59 {
60  return convert(displayValue, m_displayUnit, baseUnit());
61 }
62 
64 {
65  if (std::holds_alternative<QString>(m_valueDescriptor.unit))
66  return std::get<QString>(m_valueDescriptor.unit);
67 
69 }
70 
72 {
73  return m_valueDescriptor;
74 }
75 
76 void DoubleSpinBox::setBaseValue(double baseValue)
77 {
78  setValue(toDisplayValue(baseValue));
79 }
80 
81 void DoubleSpinBox::wheelEvent(QWheelEvent* event)
82 {
83  if (!hasFocus())
84  event->ignore();
85  else
86  QDoubleSpinBox::wheelEvent(event);
87 }
88 
89 void DoubleSpinBox::onDisplayValueChanged(double newDisplayValue)
90 {
91  emit baseValueChanged(toBaseValue(newDisplayValue));
92 }
93 
95 {
96  if (std::holds_alternative<QString>(m_valueDescriptor.unit))
97  return Unit::other;
98 
99  return std::get<Unit>(m_valueDescriptor.unit);
100 }
101 
103 {
104  QSignalBlocker b(this);
106 }
Defines class DoubleSpinBox.
Defines class GUIHelpers functions.
QString unitAsString(const Unit &unit)
Returns the string for the given unit.
Definition: Unit.cpp:58
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
Defines units, mainly to be able to convert between units.
Definition: Unit.h:26
@ other
The unit has no enum value defined in here (e.g. when defined as an explicit string)
Describes properties of a double value which are necessary to allow GUI representation,...
variant< QString, Unit > unit
Unit of the value (internal unit only!)
RealLimits limits
Limits of the value.
function< double()> get
function to get the current value
QString tooltip
Tooltip text.
int decimals
numbers of decimals to be shown in an edit control
Unit baseUnit() const
Returns the unit of the contained DoubleDescriptor.
DoubleDescriptor m_valueDescriptor
Definition: DoubleSpinBox.h:79
double toBaseValue(double displayValue) const
void updateValue()
Update the shown value to the one contained in the value descriptor.
void wheelEvent(QWheelEvent *event) override
const DoubleDescriptor & valueDescriptor() const
The descriptor on which this spinbox operates.
void setDisplayUnit(Unit displayUnit)
Set a display unit.
void setBaseValue(double baseValue)
Set the base value (unit is the one of the contained descriptor).
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
DoubleSpinBox(QWidget *parent, const DoubleDescriptor &d)
Create a DoubleSpinBox with the information found in a DoubleDescriptor.
QString displayUnitAsString() const
The display unit as human readable string.
void baseValueChanged(double newBaseValue)
Emitted whenever the value changes.
void onDisplayValueChanged(double newDisplayValue)
double toDisplayValue(double baseValue) const
void configSpinbox(QDoubleSpinBox *spinBox, int decimals, const RealLimits &limits)
Definition: EditUtil.cpp:47