BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
scientificdoubleeditor.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/viewmodel/mvvm/editors/scientificdoubleeditor.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include <QDoubleValidator>
18 #include <QLineEdit>
19 #include <QVBoxLayout>
20 #include <stdexcept>
21 
22 namespace {
23 const int max_digits = 1000;
24 }
25 
26 using namespace ModelView;
27 
29  : CustomEditor(parent), m_lineEdit(new QLineEdit)
30 
31 {
32  setAutoFillBackground(true);
33 
34  auto layout = new QVBoxLayout;
35  layout->setMargin(0);
36  layout->setSpacing(0);
37 
38  layout->addWidget(m_lineEdit);
39 
40  m_validator = new QDoubleValidator(0.0, std::numeric_limits<double>::max(), max_digits, this);
41  m_validator->setNotation(QDoubleValidator::ScientificNotation);
42  m_lineEdit->setValidator(m_validator);
43 
44  connect(m_lineEdit, &QLineEdit::editingFinished, this,
46 
47  setLayout(layout);
48 }
49 
50 void ScientificDoubleEditor::setRange(double minimum, double maximum)
51 {
52  m_validator->setRange(minimum, maximum, max_digits);
53 }
54 
56 {
57  double new_value = m_lineEdit->text().toDouble();
58 
59  if (!Utils::AreAlmostEqual(new_value, m_data.value<double>()))
60  setDataIntern(QVariant::fromValue(new_value));
61 }
62 
64 {
65  if (m_data.type() != QVariant::Double)
66  throw std::runtime_error(
67  "ScientificDoubleEditor::update_components() -> Error. Wrong variant type");
68 
69  m_lineEdit->setText(QString::number(m_data.value<double>(), 'g'));
70 }
Base class for all custom variant editors.
Definition: customeditor.h:26
void setDataIntern(const QVariant &data)
Saves the data as given by editor's internal components and notifies the model.
void update_components() override
Should update widget components from m_data, if necessary.
void setRange(double minimum, double maximum)
ScientificDoubleEditor(QWidget *parent=nullptr)
MVVM_MODEL_EXPORT bool AreAlmostEqual(double a, double b, double tolerance_factor=1.0)
Returns true if two doubles agree within epsilon*tolerance.
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
Defines class CLASS?