BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
doubleeditor.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/doubleeditor.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 <QDoubleSpinBox>
18 #include <QVBoxLayout>
19 #include <stdexcept>
20 
21 using namespace ModelView;
22 
24  : CustomEditor(parent), m_doubleEditor(new QDoubleSpinBox)
25 {
26  setFocusPolicy(Qt::StrongFocus);
27  m_doubleEditor->setFocusPolicy(Qt::StrongFocus);
28  m_doubleEditor->setKeyboardTracking(false);
29 
30  auto layout = new QVBoxLayout;
31  layout->setMargin(0);
32  layout->setSpacing(0);
33 
34  layout->addWidget(m_doubleEditor);
35 
36  connect(m_doubleEditor,
37  static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
38  [=] { this->onEditingFinished(); });
39 
40  setLayout(layout);
41 
42  setFocusProxy(m_doubleEditor);
43 }
44 
45 void DoubleEditor::setRange(double minimum, double maximum)
46 {
47  m_doubleEditor->setRange(minimum, maximum);
48 }
49 
50 void DoubleEditor::setDecimals(int decimals)
51 {
52  m_doubleEditor->setDecimals(decimals);
53 }
54 
55 void DoubleEditor::setSingleStep(double value)
56 {
57  m_doubleEditor->setSingleStep(value);
58 }
59 
61 {
62  double new_value = m_doubleEditor->value();
63 
64  if (!Utils::AreAlmostEqual(new_value, m_data.value<double>()))
65  setDataIntern(QVariant::fromValue(new_value));
66 }
67 
69 {
70  if (m_data.type() != QVariant::Double)
71  throw std::runtime_error("DoubleEditor::update_components() -> Error. Wrong variant type");
72 
73  m_doubleEditor->setValue(m_data.value<double>());
74 }
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.
QDoubleSpinBox * m_doubleEditor
Definition: doubleeditor.h:43
void setSingleStep(double value)
void setDecimals(int decimals)
void update_components() override
Should update widget components from m_data, if necessary.
void setRange(double minimum, double maximum)
DoubleEditor(QWidget *parent=nullptr)
Defines class CLASS?
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?