BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
GUI::Util Namespace Reference

Namespaces

 CoordName
 
 IO
 Provides utility methods for output data.
 
 Layout
 Utility functions to add/remove widgets to the layout on the fly. Taken from https://stackoverflow.com/questions/5395266/removing-widgets-from-qgridlayout.
 
 Path
 
 String
 
 Style
 
 Variant
 

Functions

template<typename T >
void copyContents (const T *source, T *dest)
 
template<typename T >
QByteArray createBackup (const T *t)
 
ScientificSpinBoxcreateScientificSpinBox (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 them in a row in a form layout. More...
 
template<typename T >
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. More...
 
DoubleSpinBoxcreateSpinBox (QFormLayout *parentLayout, const DoubleDescriptor &d, std::function< void(double)> slot=nullptr)
 Create a label and a spin box with the information found in a DoubleDescriptor and place them in a row in a form layout. More...
 
QSpinBox * createSpinBox (QFormLayout *parentLayout, const UIntDescriptor &d)
 Create a label and a spin box with the information found in a UIntDescriptor and place them in a row in a form layout. More...
 
QSpinBox * createSpinBox (QWidget *parent, const UIntDescriptor &d, std::function< void(uint)> slot=nullptr)
 Create a spin box with the information found in a UIntDescriptor. More...
 
QString labelWithUnit (const DoubleDescriptor &d)
 Create a label with an optional unit in brackets, both taken from the given descriptor. More...
 
QString labelWithUnit (const QString &label, std::variant< QString, Unit > unit)
 Create a label with an optional unit in brackets. More...
 
template<typename T >
void restoreBackup (T *t, const QByteArray &backup)
 

Function Documentation

◆ copyContents()

template<typename T >
void GUI::Util::copyContents ( const T *  source,
T *  dest 
)

Definition at line 46 of file Backup.h.

47 {
49 }
void restoreBackup(T *t, const QByteArray &backup)
Definition: Backup.h:36
QByteArray createBackup(const T *t)
Definition: Backup.h:24

References createBackup(), and restoreBackup().

Referenced by InstrumentItem::createCopy(), and MultiLayerItem::initFrom().

Here is the call graph for this function:

◆ createBackup()

template<typename T >
QByteArray GUI::Util::createBackup ( const T *  t)

Definition at line 24 of file Backup.h.

25 {
26  QByteArray backup;
27  QXmlStreamWriter w(&backup);
28  w.writeStartElement("backup");
29  Streamer s(&w);
30  const_cast<T*>(t)->serialize(s);
31  w.writeEndElement();
32  return backup;
33 }
Supports serialization to or deserialization from QXmlStream.
Definition: Streamer.h:36

Referenced by CommandRemoveLayer::CommandRemoveLayer(), copyContents(), and MaterialInplaceForm::selectMaterial().

◆ createScientificSpinBox()

ScientificSpinBox * GUI::Util::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 them in a row in a form layout.

The spin box will be fully initialized (tooltip, limits, current value, unit, 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.

No connections to update the descriptor will be established! Therefore changes in the spin box will not be notified to the descriptor. The additional (and optional) slot can be used to be notified about a value change.

Definition at line 82 of file WidgetUtils.cpp.

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 }
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
void valueChanged(double value)
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

References GUI::View::EditUtil::configSpinbox(), DoubleDescriptor::decimals, DoubleDescriptor::get, DoubleDescriptor::label, labelWithUnit(), DoubleDescriptor::limits, DoubleDescriptor::tooltip, DoubleDescriptor::unit, and ScientificSpinBox::valueChanged().

Referenced by DistributionSelector::createMeanSpinBox().

Here is the call graph for this function:

◆ createSelectionCombo()

template<typename T >
QComboBox* GUI::Util::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.

The combo will be filled with the available options and will get the found tooltip. The current index will be set according to the current index in the selection. Furthermore, the combo box will prohibit accidental changes by the mouse wheel. Otherwise it would be dangerous if the combo is on a scrollable form - unintended and unnoticed changes would take place when just scrolling through the form.

Changes in the combobox will be notified to the SelectionDescriptor already. The additional (and optional) slot can be used to be notified about an already executed change.

Definition at line 45 of file WidgetUtils.h.

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 }
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.
static void install(QObject *obj)

References SelectionDescriptor< T >::currentIndex(), WheelEventEater::install(), SelectionDescriptor< T >::options, SelectionDescriptor< T >::setCurrentIndex(), and SelectionDescriptor< T >::tooltip.

Referenced by DetectorAlignmentEditor::DetectorAlignmentEditor(), DetectorEditor::DetectorEditor(), DistributionSelector::DistributionSelector(), EnvironmentEditor::EnvironmentEditor(), FootprintCorrectionEditor::FootprintCorrectionEditor(), ResolutionFunctionEditor::ResolutionFunctionEditor(), and MinimizerSettingsWidget::setItem().

Here is the call graph for this function:

◆ createSpinBox() [1/3]

DoubleSpinBox * GUI::Util::createSpinBox ( QFormLayout *  parentLayout,
const DoubleDescriptor d,
std::function< void(double)>  slot = nullptr 
)

Create a label and a spin box with the information found in a DoubleDescriptor and place them in a row in a form layout.

The spin box will be fully initialized (tooltip, limits, current value, unit, 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.

No connections to update the descriptor will be established! Therefore changes in the spin box will not be notified to the descriptor. The additional (and optional) slot can be used to be notified about a value change (the notified new value is in the base unit of the DoubleDescriptor).

Definition at line 47 of file WidgetUtils.cpp.

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 }
SpinBox for DoubleDescriptors, supporting units.
Definition: DoubleSpinBox.h:22
void baseValueChanged(double newBaseValue)
Emitted whenever the value changes.

References DoubleSpinBox::baseValueChanged(), DoubleDescriptor::label, labelWithUnit(), and DoubleDescriptor::unit.

Here is the call graph for this function:

◆ createSpinBox() [2/3]

QSpinBox * GUI::Util::createSpinBox ( QFormLayout *  parentLayout,
const UIntDescriptor d 
)

Create a label and a spin box with the information found in a UIntDescriptor and place them in a row in a form layout.

The label will also contain the unit, if present. Regarding the spin box creation see the method above.

Definition at line 59 of file WidgetUtils.cpp.

60 {
61  auto* sb = createSpinBox(parentLayout->parentWidget(), d);
62  parentLayout->addRow(labelWithUnit(d.label, d.unit) + ":", sb);
63  return sb;
64 }
QString label
A label text (short, no trailing colon)
variant< QString, Unit > unit
Unit of the value (internal unit only!)
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

References createSpinBox(), UIntDescriptor::label, labelWithUnit(), and UIntDescriptor::unit.

Here is the call graph for this function:

◆ createSpinBox() [3/3]

QSpinBox * GUI::Util::createSpinBox ( QWidget *  parent,
const UIntDescriptor d,
std::function< void(uint)>  slot = nullptr 
)

Create a spin box with the information found in a UIntDescriptor.

The spin box will be fully initialized (tooltip, limits, 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.

No connections to update the descriptor will be established! Therefore changes in the spin box will not be notified to the descriptor. The additional (and optional) slot can be used to be notified about a value change.

Definition at line 24 of file WidgetUtils.cpp.

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 }
QString tooltip
Tooltip text.
RealLimits limits
Limits of the value.
function< uint()> get
function to get the current value

References UIntDescriptor::get, UIntDescriptor::limits, and UIntDescriptor::tooltip.

Referenced by AxisPropertyEditor::AxisPropertyEditor(), FormLayouter::addValue(), ResolutionFunctionEditor::createResolutionWidgets(), DistributionSelector::createSpinBox(), DetectorAlignmentEditor::createSpinBox(), and createSpinBox().

◆ labelWithUnit() [1/2]

QString GUI::Util::labelWithUnit ( const DoubleDescriptor d)

Create a label with an optional unit in brackets, both taken from the given descriptor.

No trailing ':' will be appended

Definition at line 77 of file WidgetUtils.cpp.

78 {
79  return labelWithUnit(d.label, d.unit);
80 }

References DoubleDescriptor::label, labelWithUnit(), and DoubleDescriptor::unit.

Here is the call graph for this function:

◆ labelWithUnit() [2/2]

QString GUI::Util::labelWithUnit ( const QString &  label,
std::variant< QString, Unit unit 
)

◆ restoreBackup()

template<typename T >
void GUI::Util::restoreBackup ( T *  t,
const QByteArray &  backup 
)

Definition at line 36 of file Backup.h.

37 {
38  QXmlStreamReader r(backup);
39  Streamer sr(&r);
40  r.readNextStartElement();
41  ASSERT(r.name() == "backup");
42  t->serialize(sr);
43 }

Referenced by copyContents(), and CommandRemoveLayer::undo().