BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SpecularDataPropertyWidget.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/PropertyEditor/SpecularDataPropertyWidget.cpp
6 //! @brief Implements class SpecularDataPropertyWidget
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 
19 #include "GUI/Model/Job/JobItem.h"
20 #include "GUI/Util/ComboProperty.h"
24 #include <QAction>
25 #include <QCheckBox>
26 #include <QComboBox>
27 #include <QFormLayout>
28 #include <QGroupBox>
29 #include <QLineEdit>
30 
32  : QWidget(parent)
33  , m_parent(parent)
34  , m_item(nullptr)
35 {
36  setWindowTitle("Properties");
37  setAttribute(Qt::WA_StyledBackground, true);
38  setProperty("stylable", true); // for stylesheet addressing
39 
40  m_mainLayout = new QFormLayout(this);
41  m_mainLayout->setContentsMargins(8, 20, 8, 8);
42  m_mainLayout->setSpacing(5);
43 }
44 
46 {
47  setCurrentItem(item);
48 }
49 
51 {
52  setCurrentItem(item);
53 }
54 
56 {
57  SessionItemWidget* sessionItemWidget = dynamic_cast<SessionItemWidget*>(m_parent);
58  if (!sessionItemWidget)
59  return nullptr;
60 
61  return dynamic_cast<JobItem*>(sessionItemWidget->currentItem());
62 }
63 
65 {
66  if (m_item)
67  m_item->mapper()->unsubscribe(this);
68 
70  m_updaters.clear();
71  m_item = item;
72  if (!m_item)
73  return;
74 
75  BasicAxisItem* xAxisItem = nullptr;
76  AmplitudeAxisItem* yAxisItem = nullptr;
77  SelectionDescriptor<QString> axesUnitsDescriptor;
78 
79  if (auto* p = dynamic_cast<SpecularDataItem*>(m_item)) {
80  axesUnitsDescriptor = p->axesUnits();
81  xAxisItem = p->xAxisItem();
82  yAxisItem = p->yAxisItem();
83  } else if (auto* p = dynamic_cast<Data1DViewItem*>(m_item)) {
84  axesUnitsDescriptor = p->axesUnitsDescriptor();
85  xAxisItem = p->xAxisItem();
86  yAxisItem = p->yAxisItem();
87  } else
88  return;
89 
90  m_mainLayout->addRow("Axes units:", createComboBox(axesUnitsDescriptor));
91 
92  // -- x-axis
93  auto* xGroup = new QGroupBox("X axis", this);
94  auto* xFormLayout = new QFormLayout(xGroup);
95  xFormLayout->setContentsMargins(0, 0, 0, 0);
96  xFormLayout->setSpacing(5);
97 
98  xFormLayout->addRow("Min:", createDoubleSpinbox(xAxisItem->min()));
99  xFormLayout->addRow("Max:", createDoubleSpinbox(xAxisItem->max()));
100  xFormLayout->addRow("Title:", createTextEdit(xAxisItem->titleItem()));
101 
102  m_mainLayout->addRow(xGroup);
103 
104  // -- y-axis
105  auto* yGroup = new QGroupBox("Y axis", this);
106  auto* yFormLayout = new QFormLayout(yGroup);
107  yFormLayout->setContentsMargins(0, 0, 0, 0);
108  yFormLayout->setSpacing(5);
109 
110  yFormLayout->addRow("Min:", createDoubleSpinbox(yAxisItem->min()));
111  yFormLayout->addRow("Max:", createDoubleSpinbox(yAxisItem->max()));
112  yFormLayout->addRow("Title:", createTextEdit(yAxisItem->titleItem()));
113  yFormLayout->addRow(createCheckBox("log10", yAxisItem->logScaleItem()));
114 
115  m_mainLayout->addRow(yGroup);
116 
117  updateUIValues();
118 
119  // react on external changes (e.g. zooming in customplot shall update the axis values)
121  [=](SessionItem*, const QString&) { updateUIValues(); }, this);
122 
123  // update coordinates on axes units change
125  [this](SessionItem* item, const QString& name) {
126  if(jobItem())
127  DataItem::updateAxesUnits(item, name, jobItem()->instrumentItem());
128  },
129  this);
130 }
131 
133 {
134  auto* spinBox = new DoubleSpinBox(this, d);
135  QObject::connect(spinBox, &DoubleSpinBox::baseValueChanged,
136  [=](double newValue) { d.set(newValue); });
137 
138  m_updaters << [=]() { spinBox->updateValue(); };
139 
140  return spinBox;
141 }
142 
144 {
145  auto* edit = new QLineEdit(this);
146  connect(edit, &QLineEdit::textEdited, [=](const QString& t) { item->setValue(t); });
147 
148  m_updaters << [=]() { edit->setText(item->value().toString()); };
149 
150  return edit;
151 }
152 
153 QWidget* SpecularDataPropertyWidget::createCheckBox(const QString& title, SessionItem* item)
154 {
155  return createCheckBox(
156  title, [=]() { return item->value().toBool(); }, [=](bool b) { item->setValue(b); });
157 }
158 
159 QWidget* SpecularDataPropertyWidget::createCheckBox(const QString& title, function<bool()> getter,
160  function<void(bool)> setter)
161 {
162  auto* checkBox = new QCheckBox(title, this);
163  connect(checkBox, &QCheckBox::stateChanged, [=]() { setter(checkBox->isChecked()); });
164 
165  m_updaters << [=]() {
166  QSignalBlocker b(checkBox);
167  checkBox->setChecked(getter());
168  };
169 
170  return checkBox;
171 }
172 
174 {
175  auto* combo = new QComboBox(this);
176  combo->addItems(d.options);
177  combo->setMaxCount(d.options.size());
178 
179  connect(combo, qOverload<int>(&QComboBox::currentIndexChanged),
180  [=](int newIndex) { d.setCurrentIndex(newIndex); });
181 
182  m_updaters << [=]() {
183  QSignalBlocker b(combo);
184  combo->setCurrentIndex(d.currentIndex());
185  };
186 
187  return combo;
188 }
189 
191 {
192  for (const auto& updater : m_updaters)
193  updater();
194 }
Defines various axis items.
Defines class ComboProperty.
Defines class Data1DViewItem.
Defines class DoubleSpinBox.
Defines class JobItem.
Defines namespace GUI::Util::Layout.
Defines class ItemComboWidget.
Defines class SpecularDataItem.
Defines class SpecularDataPropertyWidget.
SessionItem * logScaleItem() const
Definition: AxesItems.cpp:188
DoubleDescriptor min(const QString &unit=QString()) const
Definition: AxesItems.cpp:41
SessionItem * titleItem() const
Definition: AxesItems.cpp:90
DoubleDescriptor max(const QString &unit=QString()) const
Definition: AxesItems.cpp:58
View model for 1D DataItem. Can represent several items at once. In current implementation the first ...
static void updateAxesUnits(SessionItem *item, const QString &name, InstrumentItem *instrumentItem)
Definition: DataItem.cpp:118
Describes properties of a double value which are necessary to allow GUI representation,...
function< void(double)> set
function to set the value
SpinBox for DoubleDescriptors, supporting units.
Definition: DoubleSpinBox.h:22
void baseValueChanged(double newBaseValue)
Emitted whenever the value changes.
void unsubscribe(const void *caller)
Cancels all subscriptions of given caller.
Definition: ModelMapper.cpp:78
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=nullptr)
Definition: ModelMapper.cpp:39
void setOnChildPropertyChange(std::function< void(SessionItem *, QString)> f, const void *caller=nullptr)
Calls back on child property change, report childItem and property name.
Definition: ModelMapper.cpp:53
Describes a selection (various possibilities and the current one).
int currentIndex() const override
Get currently selected option.
QStringList options
List of options, usually presented as combo entries.
void setCurrentIndex(int newIndex) const override
Set currently selected option.
The SessionItemWidget class is a base for all widgets representing the content of SessionItem....
SessionItem * currentItem()
Base class for a GUI data item.
Definition: SessionItem.h:204
QVariant value() const
Get value.
bool setValue(QVariant value)
Set value, ensure that variant types match.
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
QWidget * createTextEdit(SessionItem *item)
SpecularDataPropertyWidget(QWidget *parent=nullptr)
QWidget * createComboBox(SelectionDescriptor< QString > d)
QWidget * createCheckBox(const QString &title, SessionItem *item)
void setItem(SpecularDataItem *item)
QWidget * createDoubleSpinbox(DoubleDescriptor d)
QList< function< void()> > m_updaters
void setCurrentItem(SessionItem *item)
QString const & name(EShape k)
Definition: particles.cpp:20
void clearLayout(QLayout *layout, bool deleteWidgets=true)
Removes content from box layout.
Definition: LayoutUtils.cpp:68