BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
LinkInstrumentManager.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Project/LinkInstrumentManager.cpp
6 //! @brief Implements class LinkInstrumentManager
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 
20 #include <QMessageBox>
21 #include <QPushButton>
22 
23 namespace {
24 
25 bool QuestionOnInstrumentReshaping(const QString& message)
26 {
27  QMessageBox msgBox;
28  msgBox.setText("Instrument description conflicts with the experimental data.");
29 
30  QString informative;
31  informative.append(message);
32  informative.append("\n\nDo you want to adjust the instrument to the experimental data?\n\n");
33  msgBox.setInformativeText(informative);
34 
35  QPushButton* modifyInstrumentButton =
36  msgBox.addButton("Yes, please modify instrument", QMessageBox::YesRole);
37  msgBox.addButton("No, leave as it is", QMessageBox::NoRole);
38 
39  msgBox.exec();
40 
41  return msgBox.clickedButton() == modifyInstrumentButton;
42 }
43 
44 
45 QString printShapeMessage(const std::vector<int>& instrument_shape,
46  const std::vector<int>& data_shape)
47 {
48  auto to_str = [](const std::vector<int>& shape) {
49  std::string result;
50  for (size_t i = 0, size = shape.size(); i < size; ++i) {
51  result += std::to_string(shape[i]);
52  if (i + 1 != size)
53  result += ", ";
54  }
55  return result;
56  };
57 
58  std::string message_string = "instrument [";
59  message_string += to_str(instrument_shape);
60  message_string += "], data [";
61  message_string += to_str(data_shape);
62  message_string += "]";
63  return QString::fromStdString(message_string);
64 }
65 
66 } // namespace
67 
69  : m_document(document)
70 {
74 
77 }
78 
80  const QString& identifier, QWidget* parent)
81 {
82  auto* instrumentItem = m_document->collectedItems()->findInstrumentById(identifier);
83 
84  // linking to null instrument is possible, it means unlinking from currently linked
85  if (!instrumentItem)
86  return true;
87 
88  if (instrumentItem->shape().size() != realDataItem->shape().size()) {
89  if (parent)
90  QMessageBox::warning(parent, "Can't link to instrument",
91  "Can't link, data is incompatible with the instrument.");
92  return false;
93  }
94 
95  if (realDataItem->isSpecularData() && !realDataItem->hasNativeData()) {
96  if (parent)
97  QMessageBox::warning(parent, "Can't link to instrument", "Can't link, data is empty.");
98  return false;
99  }
100 
101  if (instrumentItem->alignedWith(realDataItem))
102  return true;
103 
104  QString message =
105  realDataItem->holdsDimensionalData()
106  ? "Experimental data carries information on the range/points of measurement."
107  : printShapeMessage(instrumentItem->shape(), realDataItem->shape());
108  if (!QuestionOnInstrumentReshaping(message))
109  return false;
110 
112  realDataItem);
113  return true;
114 }
115 
117 {
118  // Run through all RealDataItem and refresh linking to match possible change in detector
119  // axes definition.
120  for (auto* realDataItem : m_document->realDataModel()->realDataItems())
121  if (realDataItem->instrumentId() == instrument->id()) {
122  if (!instrument->alignedWith(realDataItem)) {
123  realDataItem->unlinkFromInstrument();
124  emit linkToInstrumentChanged(realDataItem);
125  } else
126  realDataItem->linkToInstrument(instrument); // link stays same, only data is updated
127  }
128 }
129 
131 {
132  // remove links in realDataItems (in case of a linked instrument was removed)
133  for (auto* realDataItem : m_document->realDataModel()->realDataItems())
134  if (!m_document->collectedItems()->instrumentExists(realDataItem->instrumentId()))
135  realDataItem->unlinkFromInstrument();
136 }
Defines class InstrumentItem and all its children.
Defines class LinkInstrumentManager.
Defines class ProjectDocument.
Defines class RealDataItem.
Defines class RealDataModel.
InstrumentItem * findInstrumentById(const QString &instrumentId) const
bool instrumentExists(const QString &instrumentId) const
Abstract base class for instrument-specific item classes.
QString id() const
virtual bool alignedWith(const RealDataItem *item) const
void instrumentAddedOrRemoved()
Signals a change in the list of instruments.
void instrumentChanged(const InstrumentItem *instrument)
Signals any change in the settings of the given instrument.
void updateInstrumentToRealDataItem(InstrumentItem *instrument, const RealDataItem *realDataItem)
Calls the instrument's updateToRealData and emits the respective signal.
LinkInstrumentManager(ProjectDocument *document)
ProjectDocument * m_document
void linkToInstrumentChanged(const RealDataItem *realDataItem)
void onInstrumentChanged(const InstrumentItem *instrument)
bool canLinkDataToInstrument(const RealDataItem *realDataItem, const QString &identifier, QWidget *parent)
Returns true if RealDataItem can be linked to the instrument (same number of bins)....
Project document class handles all data related to the opened project (sample, job,...
RealDataModel * realDataModel() const
InstrumentCollection * collectedItems() const
InstrumentsEditController * instrumentsEditController()
The edit controller for the instruments in this project document.
Provides access to experimental data, for display and fitting. Owns an AbstractDataLoader.
Definition: RealDataItem.h:33
bool holdsDimensionalData() const
std::vector< int > shape() const
Returns the shape of underlying data item.
bool isSpecularData() const
bool hasNativeData() const
QVector< RealDataItem * > realDataItems() const
void warning(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)
Definition: MessageBox.cpp:37