BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
WidgetUtils.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Tool/WidgetUtils.h
6 //! @brief Defines 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 
15 #ifndef BORNAGAIN_GUI_VIEW_TOOL_WIDGETUTILS_H
16 #define BORNAGAIN_GUI_VIEW_TOOL_WIDGETUTILS_H
17 
19 #include "GUI/Support/Type/Unit.h"
21 #include <QComboBox>
22 #include <variant>
23 
24 class QSpinBox;
25 class UIntDescriptor;
26 class DoubleSpinBox;
27 class DoubleDescriptor;
28 class QFormLayout;
29 class ScientificSpinBox;
30 
31 namespace GUI::Util {
32 
33 //! Create a combo box with the information found in a selection descriptor.
34 //!
35 //! The combo will be filled with the available options and will get the found tooltip.
36 //! The current index will be set according to the current index in the selection.
37 //! Furthermore, the combo box will prohibit accidental changes by the mouse wheel. Otherwise it
38 //! would be dangerous if the combo is on a scrollable form - unintended and unnoticed changes would
39 //! take place when just scrolling through the form.
40 //!
41 //! Changes in the combobox will be notified to the SelectionDescriptor already. The additional (and
42 //! optional) slot can be used to be notified about an already executed change.
43 //!
44 template <typename T>
45 QComboBox* createSelectionCombo(QWidget* parent, const SelectionDescriptor<T> d,
46  std::function<void(int)> slot = nullptr)
47 {
48  auto* combo = new QComboBox(parent);
49  combo->addItems(d.options);
50  combo->setMaxCount(d.options.size());
51  combo->setToolTip(d.tooltip);
52  combo->setCurrentIndex(d.currentIndex());
54 
55  QObject::connect(combo, qOverload<int>(&QComboBox::currentIndexChanged),
56  [=](int newIndex) { d.setCurrentIndex(newIndex); });
57 
58  if (slot)
59  QObject::connect(combo, qOverload<int>(&QComboBox::currentIndexChanged),
60  [=](int newIndex) { slot(newIndex); });
61 
62  return combo;
63 }
64 
65 //! Create a spin box with the information found in a UIntDescriptor.
66 //!
67 //! The spin box will be fully initialized (tooltip, limits, current value, size policy).
68 //! Furthermore, the spin box will prohibit accidental changes by the mouse wheel. Otherwise it
69 //! would be dangerous if the spin box is on a scrollable form - unintended and unnoticed changes
70 //! would take place when just scrolling through the form.
71 //!
72 //! No connections to update the descriptor will be established! Therefore changes in the spin box
73 //! will *not* be notified to the descriptor. The additional (and optional) slot can be used to be
74 //! notified about a value change.
75 QSpinBox* createSpinBox(QWidget* parent, const UIntDescriptor& d,
76  std::function<void(uint)> slot = nullptr);
77 
78 //! Create a label and a spin box with the information found in a UIntDescriptor and place them in a
79 //! row in a form layout.
80 //!
81 //! The label will also contain the unit, if present.
82 //! Regarding the spin box creation see the method above.
83 QSpinBox* createSpinBox(QFormLayout* parentLayout, const UIntDescriptor& d);
84 
85 //! Create a label and a spin box with the information found in a DoubleDescriptor and place them in
86 //! a row in a form layout.
87 //!
88 //! The spin box will be fully initialized (tooltip, limits, current value, unit, size policy).
89 //! Furthermore, the spin box will prohibit accidental changes by the mouse wheel. Otherwise it
90 //! would be dangerous if the spin box is on a scrollable form - unintended and unnoticed changes
91 //! would take place when just scrolling through the form.
92 //!
93 //! No connections to update the descriptor will be established! Therefore changes in the spin box
94 //! will *not* be notified to the descriptor. The additional (and optional) slot can be used to be
95 //! notified about a value change (the notified new value is in the base unit of the
96 //! DoubleDescriptor).
97 DoubleSpinBox* createSpinBox(QFormLayout* parentLayout, const DoubleDescriptor& d,
98  std::function<void(double)> slot = nullptr);
99 
100 //! Create a label and a scientific spin box with the information found in a DoubleDescriptor and
101 //! place them in a row in a form layout.
102 //!
103 //! The spin box will be fully initialized (tooltip, limits, current value, unit, size policy).
104 //! Furthermore, the spin box will prohibit accidental changes by the mouse wheel. Otherwise it
105 //! would be dangerous if the spin box is on a scrollable form - unintended and unnoticed changes
106 //! would take place when just scrolling through the form.
107 //!
108 //! No connections to update the descriptor will be established! Therefore changes in the spin box
109 //! will *not* be notified to the descriptor. The additional (and optional) slot can be used to be
110 //! notified about a value change.
111 ScientificSpinBox* createScientificSpinBox(QFormLayout* parentLayout, const DoubleDescriptor& d,
112  std::function<void(double)> slot = nullptr);
113 
114 //! Create a label with an optional unit in brackets.
115 //!
116 //! No trailing ':' will be appended
117 QString labelWithUnit(const QString& label, std::variant<QString, Unit> unit);
118 
119 //! Create a label with an optional unit in brackets, both taken from the given descriptor.
120 //!
121 //! No trailing ':' will be appended
122 QString labelWithUnit(const DoubleDescriptor& d);
123 
124 } // namespace GUI::Util
125 
126 #endif // BORNAGAIN_GUI_VIEW_TOOL_WIDGETUTILS_H
Defines classes releted to event filtering.
Defines class SelectionDescriptor.
Defines class Unit.
Describes properties of a double value which are necessary to allow GUI representation,...
SpinBox for DoubleDescriptors, supporting units.
Definition: DoubleSpinBox.h:22
Describes a selection (various possibilities and the current one).
int currentIndex() const override
Get currently selected option.
QStringList options
List of options, usually presented as combo entries.
void setCurrentIndex(int newIndex) const override
Set currently selected option.
QString tooltip
Tooltip text.
Describes properties of a uint value which are necessary to allow GUI representation,...
static void install(QObject *obj)
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
QComboBox * createSelectionCombo(QWidget *parent, const SelectionDescriptor< T > d, std::function< void(int)> slot=nullptr)
Create a combo box with the information found in a selection descriptor.
Definition: WidgetUtils.h:45
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.