BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
IntensityDataPropertyWidget.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/PlotUtil/IntensityDataPropertyWidget.cpp
6 //! @brief Implements class IntensityDataPropertyWidget
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 
18 #include "GUI/Model/Job/JobItem.h"
19 #include "GUI/Util/ComboProperty.h"
23 #include <QAction>
24 #include <QCheckBox>
25 #include <QComboBox>
26 #include <QFormLayout>
27 #include <QGroupBox>
28 #include <QLineEdit>
29 
31  : QWidget(parent)
32  , parent(parent)
33  , m_item(nullptr)
34 {
35  setWindowTitle("Properties");
36  setAttribute(Qt::WA_StyledBackground, true);
37  setProperty("stylable", true); // for stylesheet addressing
38 
39  m_mainLayout = new QFormLayout(this);
40  m_mainLayout->setContentsMargins(8, 8, 8, 8);
41  m_mainLayout->setSpacing(5);
42 }
43 
45 {
46  if (m_item)
47  m_item->mapper()->unsubscribe(this);
48 }
49 
51 {
52  if (m_item)
53  m_item->mapper()->unsubscribe(this);
54 
56  m_updaters.clear();
57  m_item = mainItem;
58 
59  if (!m_item)
60  return;
61 
62  m_mainLayout->addRow("Axes units:", createComboBox(m_item->axesUnits()));
63  m_mainLayout->addRow("Color scheme:", createComboBox(m_item->gradient()));
64 
66  "Interpolate", [=]() { return m_item->isInterpolated(); },
67  [=](bool b) { m_item->setInterpolated(b); }));
68 
69  // -- x-axis
70  auto* xGroup = new QGroupBox("X axis", this);
71  auto* xFormLayout = new QFormLayout(xGroup);
72  xFormLayout->setContentsMargins(0, 0, 0, 0);
73  xFormLayout->setSpacing(5);
74 
75  xFormLayout->addRow("Min:", createDoubleSpinbox(m_item->xAxisItem()->min()));
76  xFormLayout->addRow("Max:", createDoubleSpinbox(m_item->xAxisItem()->max()));
77  xFormLayout->addRow("Title:", createTextEdit(m_item->xAxisItem()->titleItem()));
78 
79  m_mainLayout->addRow(xGroup);
80 
81  // -- y-axis
82  auto* yGroup = new QGroupBox("Y axis", this);
83  auto* yFormLayout = new QFormLayout(yGroup);
84  yFormLayout->setContentsMargins(0, 0, 0, 0);
85  yFormLayout->setSpacing(5);
86 
87  yFormLayout->addRow("Min:", createDoubleSpinbox(m_item->yAxisItem()->min()));
88  yFormLayout->addRow("Max:", createDoubleSpinbox(m_item->yAxisItem()->max()));
89  yFormLayout->addRow("Title:", createTextEdit(m_item->yAxisItem()->titleItem()));
90 
91  m_mainLayout->addRow(yGroup);
92 
93  // -- color-axis
94  auto* zGroup = new QGroupBox("Color legend", this);
95  auto* zFormLayout = new QFormLayout(zGroup);
96  zFormLayout->setContentsMargins(0, 0, 0, 0);
97  zFormLayout->setSpacing(5);
98 
99  zFormLayout->addRow("Min:", createDoubleSpinbox(m_item->zAxisItem()->min()));
100  zFormLayout->addRow("Max:", createDoubleSpinbox(m_item->zAxisItem()->max()));
101  zFormLayout->addRow(createCheckBox("log10", m_item->zAxisItem()->logScaleItem()));
102  zFormLayout->addRow(createCheckBox("Visible", m_item->zAxisItem()->visibilityItem()));
103 
104  m_mainLayout->addRow(zGroup);
105 
106  updateUIValues();
107 
108  // react on external changes (e.g. zooming in customplot shall update the axis values)
110  [=](SessionItem*, const QString&) { updateUIValues(); }, this);
111 
112  // update coordinates on axes units change
114  [this](SessionItem* item, const QString& name) {
115  if(jobItem())
116  DataItem::updateAxesUnits(item, name, jobItem()->instrumentItem());
117  },
118  this);
119  if (realItem)
120  realItem->mapper()->setOnPropertyChange(
121  [this](SessionItem* item, const QString& name) {
122  if(jobItem())
123  DataItem::updateAxesUnits(item, name, jobItem()->instrumentItem());
124  },
125  this);
126 
127  m_item->mapper()->setOnItemDestroy([this](SessionItem*) { m_item = nullptr; }, this);
128 }
129 
131 {
132  SessionItemWidget* sessionItemWidget = dynamic_cast<SessionItemWidget*>(parent);
133  if (!sessionItemWidget)
134  return nullptr;
135 
136  return dynamic_cast<JobItem*>(sessionItemWidget->currentItem());
137 }
138 
140 {
141  auto* spinBox = new DoubleSpinBox(this, d);
142  spinBox->setToolTip(d.tooltip);
143  QObject::connect(spinBox, &DoubleSpinBox::baseValueChanged,
144  [=](double newValue) { d.set(newValue); });
145 
146  m_updaters << [=]() { spinBox->updateValue(); };
147 
148  return spinBox;
149 }
150 
152 {
153  auto* edit = new QLineEdit(this);
154  connect(edit, &QLineEdit::textEdited, [=](const QString& t) { item->setValue(t); });
155 
156  m_updaters << [=]() { edit->setText(item->value().toString()); };
157 
158  return edit;
159 }
160 
161 QWidget* IntensityDataPropertyWidget::createCheckBox(const QString& title, SessionItem* item)
162 {
163  return createCheckBox(
164  title, [=]() { return item->value().toBool(); }, [=](bool b) { item->setValue(b); });
165 }
166 
167 QWidget* IntensityDataPropertyWidget::createCheckBox(const QString& title, function<bool()> getter,
168  function<void(bool)> setter)
169 {
170  auto* checkBox = new QCheckBox(title, this);
171  connect(checkBox, &QCheckBox::stateChanged, [=]() { setter(checkBox->isChecked()); });
172 
173  m_updaters << [=]() {
174  QSignalBlocker b(checkBox);
175  checkBox->setChecked(getter());
176  };
177 
178  return checkBox;
179 }
180 
182 {
183  auto* combo = new QComboBox(this);
184  combo->addItems(d.options);
185  combo->setMaxCount(d.options.size());
186 
187  connect(combo, qOverload<int>(&QComboBox::currentIndexChanged),
188  [=](int newIndex) { d.setCurrentIndex(newIndex); });
189 
190  m_updaters << [=]() {
191  QSignalBlocker b(combo);
192  combo->setCurrentIndex(d.currentIndex());
193  };
194 
195  return combo;
196 }
197 
199 {
200  for (const auto& updater : m_updaters)
201  updater();
202 }
Defines various axis items.
Defines class ComboProperty.
Defines class DoubleSpinBox.
Defines class IntensityDataItem.
Defines class IntensityDataPropertyWidget.
Defines class JobItem.
Defines namespace GUI::Util::Layout.
Defines class ItemComboWidget.
SessionItem * logScaleItem() const
Definition: AxesItems.cpp:188
DoubleDescriptor min(const QString &unit=QString()) const
Definition: AxesItems.cpp:41
SessionItem * visibilityItem() const
Definition: AxesItems.cpp:116
SessionItem * titleItem() const
Definition: AxesItems.cpp:90
DoubleDescriptor max(const QString &unit=QString()) const
Definition: AxesItems.cpp:58
static void updateAxesUnits(SessionItem *item, const QString &name, InstrumentItem *instrumentItem)
Definition: DataItem.cpp:118
SelectionDescriptor< QString > axesUnits() const
Definition: DataItem.cpp:108
Describes properties of a double value which are necessary to allow GUI representation,...
function< void(double)> set
function to set the value
QString tooltip
Tooltip text.
SpinBox for DoubleDescriptors, supporting units.
Definition: DoubleSpinBox.h:22
void baseValueChanged(double newBaseValue)
Emitted whenever the value changes.
const AmplitudeAxisItem * zAxisItem() const
SelectionDescriptor< QString > gradient() const
const BasicAxisItem * yAxisItem() const
void setInterpolated(bool interp)
const BasicAxisItem * xAxisItem() const
bool isInterpolated() const
QWidget * createTextEdit(SessionItem *item)
void setItem(IntensityDataItem *mainItem, IntensityDataItem *realItem=nullptr)
QList< function< void()> > m_updaters
QWidget * createDoubleSpinbox(DoubleDescriptor d)
QWidget * createCheckBox(const QString &title, SessionItem *item)
IntensityDataPropertyWidget(QWidget *parent=nullptr)
QWidget * createComboBox(SelectionDescriptor< QString > d)
void unsubscribe(const void *caller)
Cancels all subscriptions of given caller.
Definition: ModelMapper.cpp:78
void setOnItemDestroy(std::function< void(SessionItem *)> f, const void *caller=nullptr)
Definition: ModelMapper.cpp:67
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.
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