From d17ff8627aacf9a6c989bf3127b60e3d80dbdec7 Mon Sep 17 00:00:00 2001 From: Matthias Puchner <github@mpuchner.de> Date: Wed, 20 Oct 2021 18:59:45 +0200 Subject: [PATCH] introduce DoubleLineEdit, a line edit which operates on a DoubleDescriptor --- GUI/Views/CommonWidgets/DoubleLineEdit.cpp | 46 +++++++++++++++++++ GUI/Views/CommonWidgets/DoubleLineEdit.h | 51 ++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 GUI/Views/CommonWidgets/DoubleLineEdit.cpp create mode 100644 GUI/Views/CommonWidgets/DoubleLineEdit.h diff --git a/GUI/Views/CommonWidgets/DoubleLineEdit.cpp b/GUI/Views/CommonWidgets/DoubleLineEdit.cpp new file mode 100644 index 00000000000..2bff7cdcfde --- /dev/null +++ b/GUI/Views/CommonWidgets/DoubleLineEdit.cpp @@ -0,0 +1,46 @@ +// ************************************************************************************************ +// +// BornAgain: simulate and fit reflection and scattering +// +//! @file GUI/Views/CommonWidgets/DoubleLineEdit.cpp +//! @brief Implements class DoubleLineEdit +//! +//! @homepage http://www.bornagainproject.org +//! @license GNU General Public License v3 or higher (see COPYING) +//! @copyright Forschungszentrum Jülich GmbH 2021 +//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) +// +// ************************************************************************************************ + +#include "GUI/Views/CommonWidgets/DoubleLineEdit.h" +#include "GUI/Views/CommonWidgets/GUIHelpers.h" +#include <QDoubleValidator> + +DoubleLineEdit::DoubleLineEdit(QWidget* parent, const DoubleDescriptor& d) + : QLineEdit(parent), m_valueDescriptor(d) +{ + m_validator = new QDoubleValidator(0.0, 1e+200, 1000, this); + m_validator->setNotation(QDoubleValidator::ScientificNotation); + const double minimum = + d.limits.hasLowerLimit() ? std::max(d.limits.lowerLimit(), -1e+200) : -1e+200; + const double maximum = + d.limits.hasUpperLimit() ? std::min(d.limits.upperLimit(), +1e+200) : +1e+200; + m_validator->setRange(minimum, maximum, 1000); + setValidator(m_validator); + + setBaseValue(m_valueDescriptor.get()); + + connect(this, &QLineEdit::editingFinished, this, &DoubleLineEdit::onEditingFinished); +} + +void DoubleLineEdit::setBaseValue(double baseValue) +{ + setText(QString::number(baseValue, 'g')); +} + +void DoubleLineEdit::onEditingFinished() +{ + const double new_value = text().toDouble(); + if (new_value != m_valueDescriptor.get()) + emit baseValueChanged(new_value); +} diff --git a/GUI/Views/CommonWidgets/DoubleLineEdit.h b/GUI/Views/CommonWidgets/DoubleLineEdit.h new file mode 100644 index 00000000000..4aff9d7dfce --- /dev/null +++ b/GUI/Views/CommonWidgets/DoubleLineEdit.h @@ -0,0 +1,51 @@ +// ************************************************************************************************ +// +// BornAgain: simulate and fit reflection and scattering +// +//! @file GUI/Views/CommonWidgets/DoubleLineEdit.h +//! @brief Defines class DoubleLineEdit +//! +//! @homepage http://www.bornagainproject.org +//! @license GNU General Public License v3 or higher (see COPYING) +//! @copyright Forschungszentrum Jülich GmbH 2021 +//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) +// +// ************************************************************************************************ + +#ifndef BORNAGAIN_GUI_VIEWS_COMMONWIDGETS_DOUBLELINEEDIT_H +#define BORNAGAIN_GUI_VIEWS_COMMONWIDGETS_DOUBLELINEEDIT_H + +#include "GUI/Models/DoubleDescriptor.h" +#include <QLineEdit> + +class QDoubleValidator; + +//! LineEdit to edit values in a scientific notation, operating on a DoubleDescriptor. +//! +//! In the future it can be enhanced to support units. At the moment, no DoubleDescriptor with units +//! is used with a DoubleLineEdit, therefore the handling of units is not implemented yet. Only the +//! naming is prepared already (also to have a naming alike to DoubleSpinBox). +class DoubleLineEdit : public QLineEdit { + Q_OBJECT +public: + DoubleLineEdit(QWidget* parent, const DoubleDescriptor& d); + + //! Set the base value (unit is the one of the contained descriptor). + void setBaseValue(double baseValue); + +signals: + //! Emitted whenever the value changes. + //! + //! newBaseValue is in the unit of the valueDescriptor. + void baseValueChanged(double newBaseValue); + +private slots: + void onEditingFinished(); + +private: + QDoubleValidator* m_validator; + DoubleDescriptor m_valueDescriptor; +}; + + +#endif // BORNAGAIN_GUI_VIEWS_COMMONWIDGETS_DOUBLELINEEDIT_H -- GitLab