BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
CustomEditors.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/PropertyEditor/CustomEditors.cpp
6 //! @brief Implements CustomEditors classes
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "Base/Util/Assert.h"
17 #include "Fit/Param/RealLimits.h"
19 #include "GUI/Util/ComboProperty.h"
22 #include <QBoxLayout>
23 #include <QComboBox>
24 #include <cmath>
25 
26 namespace {
27 
28 //! Single step for QDoubleSpinBox.
29 
30 double singleStep(int decimals)
31 {
32  // For item with decimals=3 (i.e. 0.001) single step will be 0.1
33  return 1. / std::pow(10., decimals - 1);
34 }
35 
36 } // namespace
37 
38 //! Sets the data from the model to editor.
39 
40 void CustomEditor::setData(const QVariant& data)
41 {
42  m_data = data;
43  initEditor();
44 }
45 
46 //! Inits editor widgets from m_data.
47 
49 
50 //! Saves the data from the editor and informs external delegates.
51 
52 void CustomEditor::setDataIntern(const QVariant& data)
53 {
54  m_data = data;
56 }
57 
58 // --- ComboPropertyEditor ---
59 
61  : CustomEditor(parent)
62  , m_box(new QComboBox)
63  , m_wheel_event_filter(new WheelEventEater(this))
64 {
65  setAutoFillBackground(true);
66  setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
67 
68  auto* layout = new QVBoxLayout;
69  layout->setMargin(0);
70  layout->setSpacing(0);
71  layout->addWidget(m_box);
72 
73  m_box->installEventFilter(m_wheel_event_filter);
74 
75  setLayout(layout);
76  setConnected(true);
77 }
78 
80 {
81  return m_box->sizeHint();
82 }
83 
85 {
86  return m_box->minimumSizeHint();
87 }
88 
90 {
91  auto comboProperty = m_data.value<ComboProperty>();
92 
93  if (comboProperty.currentIndex() != index) {
94  comboProperty.setCurrentIndex(index);
95  setDataIntern(QVariant::fromValue<ComboProperty>(comboProperty));
96  }
97 }
98 
100 {
101  setConnected(false);
102 
103  m_box->clear();
104  m_box->insertItems(0, internLabels());
105  m_box->setCurrentIndex(internIndex());
106 
107  setConnected(true);
108 }
109 
110 //! Returns list of labels for QComboBox
111 
113 {
114  if (!m_data.canConvert<ComboProperty>())
115  return {};
116  auto comboProperty = m_data.value<ComboProperty>();
117  return comboProperty.getValues();
118 }
119 
120 //! Returns index for QComboBox.
121 
123 {
124  if (!m_data.canConvert<ComboProperty>())
125  return 0;
126  auto comboProperty = m_data.value<ComboProperty>();
127  return comboProperty.currentIndex();
128 }
129 
130 void ComboPropertyEditor::setConnected(bool isConnected)
131 {
132  if (isConnected)
133  connect(m_box, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
134  &ComboPropertyEditor::onIndexChanged, Qt::UniqueConnection);
135  else
136  disconnect(m_box, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
138 }
139 
140 // --- ScientificSpinBoxEditor ---
141 
143  : CustomEditor(parent)
144  , m_doubleEditor(new ScientificSpinBox)
145 {
146  setAutoFillBackground(true);
147  setFocusPolicy(Qt::StrongFocus);
148  m_doubleEditor->setFocusPolicy(Qt::StrongFocus);
149  m_doubleEditor->setKeyboardTracking(false);
150 
151  auto* layout = new QVBoxLayout;
152  layout->setMargin(0);
153  layout->setSpacing(0);
154 
155  layout->addWidget(m_doubleEditor);
156 
158 
159  setLayout(layout);
160 
161  setFocusProxy(m_doubleEditor);
162 }
163 
164 void ScientificSpinBoxEditor::setLimits(const RealLimits& limits)
165 {
166  m_doubleEditor->setMinimum(limits.hasLowerLimit() ? limits.lowerLimit()
167  : std::numeric_limits<double>::lowest());
168  m_doubleEditor->setMaximum(limits.hasUpperLimit() ? limits.upperLimit()
169  : std::numeric_limits<double>::max());
170 }
171 
173 {
174  m_doubleEditor->setDecimals(decimals);
175  m_doubleEditor->setSingleStep(singleStep(decimals));
176 }
177 
179 {
181 }
182 
184 {
185  double new_value = m_doubleEditor->value();
186 
187  if (new_value != m_data.toDouble())
188  setDataIntern(QVariant::fromValue(new_value));
189 }
190 
192 {
193  ASSERT(m_data.type() == QVariant::Double);
194  m_doubleEditor->setValue(m_data.toDouble());
195 }
Defines class ComboProperty.
Defines CustomEditors classes.
Defines classes releted to event filtering.
Defines class ScientificSpinBox.
Defines struct SessionData.
virtual int internIndex()
Returns index for QComboBox.
void setConnected(bool isConnected)
virtual QStringList internLabels()
Returns list of labels for QComboBox.
virtual void onIndexChanged(int index)
void initEditor() override
Inits editor widgets from m_data.
QSize minimumSizeHint() const override
QSize sizeHint() const override
class WheelEventEater * m_wheel_event_filter
Definition: CustomEditors.h:72
ComboPropertyEditor(QWidget *parent=nullptr)
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
QStringList getValues() const
int currentIndex() const
void setCurrentIndex(int index)
Base class for all custom variants editors.
Definition: CustomEditors.h:29
QVariant m_data
Definition: CustomEditors.h:49
void dataChanged(const QVariant &data)
Signal emit then user changed the data through the editor.
virtual void initEditor()
Inits editor widgets from m_data.
void setData(const QVariant &data)
Sets the data from the model to editor.
void setDataIntern(const QVariant &data)
Saves the data from the editor and informs external delegates.
void setDecimals(int decimals)
void initEditor() override
Inits editor widgets from m_data.
void setLimits(const RealLimits &limits)
void setSingleStep(double step)
ScientificSpinBoxEditor(QWidget *parent=nullptr)
class ScientificSpinBox * m_doubleEditor
Definition: CustomEditors.h:93
void setMaximum(double max)
void setMinimum(double min)
void setValue(double val)
void setSingleStep(double step)
void valueChanged(double value)
Event filter to install on combo boxes and spin boxes to not to react on wheel events during scrollin...