From 4f40fb2863a13ab32ab97b81baafaeb8ecd7b72c Mon Sep 17 00:00:00 2001 From: Matthias Puchner <github@mpuchner.de> Date: Sun, 13 Feb 2022 09:01:06 +0100 Subject: [PATCH] simplify, add documentation --- GUI/View/Edit/DoubleSpinBox.h | 6 +++++ GUI/View/Tool/WidgetUtils.cpp | 14 +++------- GUI/View/Tool/WidgetUtils.h | 49 ++++++++++++++++++++++++++++++----- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/GUI/View/Edit/DoubleSpinBox.h b/GUI/View/Edit/DoubleSpinBox.h index f1069b4ecba..6ad1ab75936 100644 --- a/GUI/View/Edit/DoubleSpinBox.h +++ b/GUI/View/Edit/DoubleSpinBox.h @@ -22,6 +22,12 @@ class DoubleSpinBox : public QDoubleSpinBox { Q_OBJECT public: + //! 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. DoubleSpinBox(QWidget* parent, const DoubleDescriptor& d); //! Set a display unit. diff --git a/GUI/View/Tool/WidgetUtils.cpp b/GUI/View/Tool/WidgetUtils.cpp index 1170d22b7fd..d52fcb7c603 100644 --- a/GUI/View/Tool/WidgetUtils.cpp +++ b/GUI/View/Tool/WidgetUtils.cpp @@ -18,7 +18,8 @@ #include <QFormLayout> #include <QSpinBox> -QSpinBox* GUI::Util::createSpinBox(QWidget* parent, const UIntDescriptor& d) +QSpinBox* GUI::Util::createSpinBox(QWidget* parent, const UIntDescriptor& d, + std::function<void(uint)> slot) { auto* spinBox = new QSpinBox(parent); spinBox->setFocusPolicy(Qt::StrongFocus); @@ -34,18 +35,11 @@ QSpinBox* GUI::Util::createSpinBox(QWidget* parent, const UIntDescriptor& d) spinBox->setValue(d.get()); - return spinBox; -} - -QSpinBox* GUI::Util::createSpinBox(QWidget* parent, const UIntDescriptor& d, - std::function<void(uint)> slot) -{ - auto* sb = createSpinBox(parent, d); if (slot) - QObject::connect(sb, qOverload<int>(&QSpinBox::valueChanged), + QObject::connect(spinBox, qOverload<int>(&QSpinBox::valueChanged), [=](int v) { slot(static_cast<uint>(v)); }); - return sb; + return spinBox; } DoubleSpinBox* GUI::Util::createSpinBox(QFormLayout* parentLayout, const DoubleDescriptor& d) diff --git a/GUI/View/Tool/WidgetUtils.h b/GUI/View/Tool/WidgetUtils.h index 0401588fd3e..08e4c0b25cc 100644 --- a/GUI/View/Tool/WidgetUtils.h +++ b/GUI/View/Tool/WidgetUtils.h @@ -29,9 +29,17 @@ class QFormLayout; namespace GUI::Util { -// #bamigration docu -//! Changes in the combobox will be notified to the SelectionDescriptor already. The addition slot -//! exists to be notified about an already executed change. +//! 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. +//! template <typename T> QComboBox* createSelectionCombo(QWidget* parent, const SelectionDescriptor<T> d, std::function<void(int)> slot = nullptr) @@ -53,15 +61,42 @@ QComboBox* createSelectionCombo(QWidget* parent, const SelectionDescriptor<T> d, return combo; } -// #bamigration docu -QSpinBox* createSpinBox(QWidget* parent, const UIntDescriptor& d); -QSpinBox* createSpinBox(QWidget* parent, const UIntDescriptor& d, std::function<void(uint)> slot); +//! 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. +QSpinBox* createSpinBox(QWidget* parent, const UIntDescriptor& d, + std::function<void(uint)> slot = nullptr); +//! 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. QSpinBox* createSpinBox(QFormLayout* parentLayout, const UIntDescriptor& d); +//! 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. DoubleSpinBox* createSpinBox(QFormLayout* parentLayout, const DoubleDescriptor& d); -//! No trailing ':' +//! Create a label with an optional unit in brackets. +//! +//! No trailing ':' will be appended QString labelWithUnit(const QString& label, std::variant<QString, Unit> unit); } // namespace GUI::Util -- GitLab