BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
RealDataPropertiesWidget.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/ImportDataWidgets/RealDataPropertiesWidget.cpp
6 //! @brief Implements class RealDataPropertiesWidget
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
22 #include <QComboBox>
23 #include <QLabel>
24 #include <QVBoxLayout>
25 
27  : QWidget(parent), m_instrumentCombo(new QComboBox), m_currentDataItem(nullptr)
28 {
29  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
30  setWindowTitle("RealDataPropertiesWidget");
31 
32  QVBoxLayout* mainLayout = new QVBoxLayout;
33  mainLayout->setMargin(5);
34  mainLayout->setSpacing(2);
35 
36  m_instrumentCombo->setToolTip("Select instrument to link with real data");
37 
38  mainLayout->addWidget(new QLabel("Linked instrument"));
39  mainLayout->addWidget(m_instrumentCombo);
40 
41  mainLayout->addStretch();
42  setLayout(mainLayout);
43 
44  connect(m_instrumentCombo,
45  static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
47 
50 
51  connect(MainWindow::instance()->instrumentModel(), &InstrumentModel::instrumentNameChanged,
53 
55  setPropertiesEnabled(false);
56 }
57 
58 //! Set current RealDataItem to display in instrument selector.
59 
61 {
62  if (item == m_currentDataItem)
63  return;
64 
67 
68  m_currentDataItem = item;
69 
71 
72  if (!m_currentDataItem)
73  return;
74 
76  [this](const QString&) { onRealDataPropertyChanged(); }, this);
77 
79  [this](SessionItem*) { m_currentDataItem = nullptr; }, this);
80 
81  // Set combo selector to show linked instrument
83 }
84 
85 //! Processes user interaction with instrument selector combo. If there is realDataItem,
86 //! it will be linked with selected instrument.
87 
89 {
90  if (!m_currentDataItem)
91  return;
92 
93  const QString newSelectedInstrumentId = m_instrumentCombo->currentData().toString();
94 
95  if (newSelectedInstrumentId == m_currentDataItem->instrumentId())
96  return;
97 
98  if (MainWindow::instance()->linkInstrumentManager()->canLinkDataToInstrument(
99  m_currentDataItem, newSelectedInstrumentId)) {
100  m_currentDataItem->setInstrumentId(newSelectedInstrumentId);
101  m_currentDataItem->updateToInstrument(newSelectedInstrumentId);
102  } else
103  // Linking was impossible or denied. Set combo to previous state
105 }
106 
107 //! Updates instrument selector for new instruments and their names.
108 //! Current selection will be preserved.
109 
111 {
112  QString currentId = m_currentDataItem != nullptr ? m_currentDataItem->instrumentId() : "";
113 
114  QSignalBlocker b(m_instrumentCombo);
115 
116  m_instrumentCombo->clear();
117 
118  // fill the combo. Userdata contains instrument's uid
119  m_instrumentCombo->addItem("Undefined", ""); // undefined instrument
120  for (auto instrumentItem : MainWindow::instance()->instrumentModel()->instrumentItems())
121  m_instrumentCombo->addItem(instrumentItem->name(), instrumentItem->id());
122 
123  const int index = m_instrumentCombo->findData(currentId);
124  if (index >= 0)
125  m_instrumentCombo->setCurrentIndex(index);
126  else
127  // instrument selected in data item was deleted
128  m_instrumentCombo->setCurrentIndex(0);
129 }
130 
131 //! Updates instrument combo if instrument link of current RealDataItem changed.
132 
134 {
135  // This can be called when combo on this page was changed, but also when the linking
136  // is undone because of instrument deletion or similar
137  if (!m_currentDataItem)
138  return;
139 
140  // The notification comes for different reasons, therefore a check for "link changed" is done
141  // first
142  const bool linkChanged =
143  m_instrumentCombo->currentData().toString() != m_currentDataItem->instrumentId();
144 
145  if (linkChanged)
147 }
148 
149 //! Sets instrument combo selector to the state corresponding to given instrument identifier.
150 
151 void RealDataPropertiesWidget::setComboToIdentifier(const QString& instrumentId)
152 {
153  const int index = m_instrumentCombo->findData(instrumentId);
154  ASSERT(index >= 0);
155 
156  QSignalBlocker b(m_instrumentCombo);
157  m_instrumentCombo->setCurrentIndex(index);
158 }
159 
160 //! Sets all widget's children enabled/disabled.
161 
163 {
164  setEnabled(enabled);
165  if (!enabled)
166  m_instrumentCombo->setCurrentIndex(0);
167 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines class MainWindow.
Defines InstrumentItems classes.
Defines class InstrumentModel.
Defines class LinkInstrumentManager.
Defines class RealDataItem.
Defines class RealDataPropertiesWidget.
Defines class SessionModel.
void instrumentNameChanged(const InstrumentItem *instrument)
void instrumentAddedOrRemoved()
static MainWindow * instance()
Returns the one and only instance of this class.
Definition: mainwindow.cpp:129
void unsubscribe(const void *caller)
Cancells all subscribtion of given caller.
Definition: ModelMapper.cpp:98
void setOnItemDestroy(std::function< void(SessionItem *)> f, const void *caller=0)
Definition: ModelMapper.cpp:87
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=0)
Definition: ModelMapper.cpp:35
The RealDataItem class represents intensity data imported from file and intended for fitting.
Definition: RealDataItem.h:35
void setInstrumentId(const QString &id)
void updateToInstrument(const InstrumentItem *instrument)
QString instrumentId() const
void updateInstrumentComboEntries()
Updates instrument selector for new instruments and their names.
void setComboToIdentifier(const QString &instrumentId)
Sets instrument combo selector to the state corresponding to given instrument identifier.
void onRealDataPropertyChanged()
Updates instrument combo if instrument link of current RealDataItem changed.
void setPropertiesEnabled(bool enabled)
Sets all widget's children enabled/disabled.
void setItem(RealDataItem *item)
Set current RealDataItem to display in instrument selector.
RealDataPropertiesWidget(QWidget *parent=0)
void onInstrumentComboIndexChanged(int index)
Processes user interaction with instrument selector combo.
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.