BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
WidgetUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Tool/WidgetUtils.cpp
6 //! @brief Implements GUI::Util namespace
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2022
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
19 #include "GUI/View/Tool/EditUtil.h"
20 #include <QFormLayout>
21 #include <QLabel>
22 #include <QSpinBox>
23 
24 QSpinBox* GUI::Util::createSpinBox(QWidget* parent, const UIntDescriptor& d,
25  std::function<void(uint)> slot)
26 {
27  auto* spinBox = new QSpinBox(parent);
28  spinBox->setFocusPolicy(Qt::StrongFocus);
29  spinBox->setToolTip(d.tooltip);
30  spinBox->setMaximum(std::numeric_limits<int>::max());
31  spinBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
32 
33  if (d.limits.hasLowerLimit())
34  spinBox->setMinimum(static_cast<int>(d.limits.lowerLimit()));
35  if (d.limits.hasUpperLimit())
36  spinBox->setMaximum(static_cast<int>(d.limits.upperLimit()));
37 
38  spinBox->setValue(d.get());
39 
40  if (slot)
41  QObject::connect(spinBox, qOverload<int>(&QSpinBox::valueChanged),
42  [=](int v) { slot(static_cast<uint>(v)); });
43 
44  return spinBox;
45 }
46 
47 DoubleSpinBox* GUI::Util::createSpinBox(QFormLayout* parentLayout, const DoubleDescriptor& d,
48  std::function<void(double)> slot)
49 {
50  auto* sb = new DoubleSpinBox(parentLayout->parentWidget(), d);
51  parentLayout->addRow(labelWithUnit(d.label, d.unit) + ":", sb);
52 
53  if (slot)
54  QObject::connect(sb, &DoubleSpinBox::baseValueChanged, [=](int v) { slot(v); });
55 
56  return sb;
57 }
58 
59 QSpinBox* GUI::Util::createSpinBox(QFormLayout* parentLayout, const UIntDescriptor& d)
60 {
61  auto* sb = createSpinBox(parentLayout->parentWidget(), d);
62  parentLayout->addRow(labelWithUnit(d.label, d.unit) + ":", sb);
63  return sb;
64 }
65 
66 QString GUI::Util::labelWithUnit(const QString& label, variant<QString, Unit> unit)
67 {
68  const QString s = std::holds_alternative<QString>(unit) ? std::get<QString>(unit)
69  : unitAsString(std::get<Unit>(unit));
70 
71  if (!s.isEmpty())
72  return label + " [" + s + "]";
73 
74  return label;
75 }
76 
78 {
79  return labelWithUnit(d.label, d.unit);
80 }
81 
83  const DoubleDescriptor& d,
84  std::function<void(double)> slot)
85 {
86  auto* spinBox = new ScientificSpinBox(parentLayout->parentWidget());
87  spinBox->setFocusPolicy(Qt::StrongFocus);
89  spinBox->setToolTip(d.tooltip);
90  spinBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
91  spinBox->setValue(d.get());
92  parentLayout->addRow(labelWithUnit(d.label, d.unit) + ":", spinBox);
93 
94  if (slot)
95  QObject::connect(spinBox, &ScientificSpinBox::valueChanged, [=](double v) { slot(v); });
96 
97  return spinBox;
98 }
Defines class DoubleSpinBox.
Defines class GUIHelpers functions.
Defines class ScientificSpinBox.
Defines class UIntDescriptor.
QString unitAsString(const Unit &unit)
Returns the string for the given unit.
Definition: Unit.cpp:58
Unit
Defines units, mainly to be able to convert between units.
Definition: Unit.h:26
Defines GUI::Util namespace.
Describes properties of a double value which are necessary to allow GUI representation,...
variant< QString, Unit > unit
Unit of the value (internal unit only!)
QString label
A label text (short, no trailing colon)
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
SpinBox for DoubleDescriptors, supporting units.
Definition: DoubleSpinBox.h:22
void baseValueChanged(double newBaseValue)
Emitted whenever the value changes.
void valueChanged(double value)
Describes properties of a uint value which are necessary to allow GUI representation,...
QString label
A label text (short, no trailing colon)
variant< QString, Unit > unit
Unit of the value (internal unit only!)
QString tooltip
Tooltip text.
RealLimits limits
Limits of the value.
function< uint()> get
function to get the current value
ScientificSpinBox * createScientificSpinBox(QFormLayout *parentLayout, const DoubleDescriptor &d, std::function< void(double)> slot=nullptr)
Create a label and a scientific spin box with the information found in a DoubleDescriptor and place t...
Definition: WidgetUtils.cpp:82
QSpinBox * createSpinBox(QWidget *parent, const UIntDescriptor &d, std::function< void(uint)> slot=nullptr)
Create a spin box with the information found in a UIntDescriptor.
Definition: WidgetUtils.cpp:24
QString labelWithUnit(const QString &label, std::variant< QString, Unit > unit)
Create a label with an optional unit in brackets.
void configSpinbox(QDoubleSpinBox *spinBox, int decimals, const RealLimits &limits)
Definition: EditUtil.cpp:47