BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
LinkInstrumentManager Class Reference

The LinkInstrumentManager class provides communication between InstrumentModel and RealDataModel. More...

Inheritance diagram for LinkInstrumentManager:
[legend]
Collaboration diagram for LinkInstrumentManager:
[legend]

Public Member Functions

 LinkInstrumentManager (QObject *parent=nullptr)
 
bool canLinkDataToInstrument (const RealDataItem *realDataItem, const QString &identifier, bool quiet=false)
 quiet defines whether a "not possible" message box is shown if link is not possible. More...
 
QList< RealDataItem * > linkedRealDataItems (InstrumentItem *instrumentItem)
 Returns list of RealDataItem's linked to given instrument. More...
 
void setModels (InstrumentModel *instrumentModel, RealDataModel *realDataModel)
 Sets models and builds initial links. More...
 

Private Slots

void onInstrumentAddedOrRemoved ()
 Updates map of instruments on insert/remove InstrumentItem event. More...
 
void onInstrumentChildChange (InstrumentItem *instrument, SessionItem *child)
 Perform actions on instrument children change. More...
 
void updateInstrumentSubscriptions ()
 Set up callbacks to all instrument items. More...
 

Private Attributes

InstrumentModelm_instrumentModel
 
RealDataModelm_realDataModel
 

Detailed Description

The LinkInstrumentManager class provides communication between InstrumentModel and RealDataModel.

Particularly, it notifies RealDataItem about changes in linked instruments to adjust axes of IntensityDataItem.

Definition at line 35 of file LinkInstrumentManager.h.

Constructor & Destructor Documentation

◆ LinkInstrumentManager()

LinkInstrumentManager::LinkInstrumentManager ( QObject *  parent = nullptr)
explicit

Definition at line 33 of file LinkInstrumentManager.cpp.

34  : QObject(parent), m_instrumentModel(nullptr), m_realDataModel(nullptr)
35 {
36 }
InstrumentModel * m_instrumentModel
RealDataModel * m_realDataModel

Member Function Documentation

◆ canLinkDataToInstrument()

bool LinkInstrumentManager::canLinkDataToInstrument ( const RealDataItem realDataItem,
const QString &  identifier,
bool  quiet = false 
)

quiet defines whether a "not possible" message box is shown if link is not possible.

Returns true if RealDataItem can be linked to the instrument (same number of bins).

Use this e.g. for unit tests. The question for adjusting the instrument is not suppressed by this flag.

Also offers dialog to adjust instrument to match shape of real data.

Definition at line 64 of file LinkInstrumentManager.cpp.

67 {
68  auto instrumentItem = m_instrumentModel->findInstrumentById(identifier);
69 
70  // linking to null instrument is possible, it means unlinking from currently linked
71  if (!instrumentItem)
72  return true;
73 
74  const bool isCompatible = ImportDataUtils::Compatible(*instrumentItem, *realDataItem);
75  if (!isCompatible) {
76  if (!quiet)
77  QMessageBox::warning(MainWindow::instance(), "Can't link to instrument",
78  "Can't link, data is incompatible with the instrument.");
79  return false;
80  }
81 
82  if (realDataItem->isSpecularData() && !realDataItem->hasNativeData()) {
83  if (!quiet)
84  QMessageBox::warning(MainWindow::instance(), "Can't link to instrument",
85  "Can't link, data is empty.");
86  return false;
87  }
88 
89  if (instrumentItem->alignedWith(realDataItem))
90  return true;
91 
92  QString message =
93  realDataItem->holdsDimensionalData()
94  ? "Experimental data carries information on the range/points of measurement."
95  : ImportDataUtils::printShapeMessage(instrumentItem->shape(), realDataItem->shape());
96  if (!QuestionOnInstrumentReshaping(message))
97  return false;
98 
99  instrumentItem->updateToRealData(realDataItem);
100  return true;
101 }
InstrumentItem * findInstrumentById(const QString &instrumentId) const
static MainWindow * instance()
Returns the one and only instance of this class.
Definition: mainwindow.cpp:129
bool holdsDimensionalData() const
std::vector< int > shape() const
Returns the shape of underlying data item.
bool isSpecularData() const
bool hasNativeData() const
void warning(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)
Definition: GUIHelpers.cpp:74
QString printShapeMessage(const std::vector< int > &instrument_shape, const std::vector< int > &data_shape)
Composes a message with the shapes of InstrumentItem and RealDataItem.
bool Compatible(const InstrumentItem &instrumentItem, const RealDataItem &realDataItem)
Check whether data item is compatible with instrument (same rank)

References ImportDataUtils::Compatible(), InstrumentModel::findInstrumentById(), RealDataItem::hasNativeData(), RealDataItem::holdsDimensionalData(), MainWindow::instance(), RealDataItem::isSpecularData(), m_instrumentModel, ImportDataUtils::printShapeMessage(), RealDataItem::shape(), and GUIHelpers::warning().

Here is the call graph for this function:

◆ linkedRealDataItems()

QList< RealDataItem * > LinkInstrumentManager::linkedRealDataItems ( InstrumentItem instrumentItem)

Returns list of RealDataItem's linked to given instrument.

Definition at line 151 of file LinkInstrumentManager.cpp.

152 {
153  ASSERT(instrumentItem != nullptr);
154 
155  QList<RealDataItem*> result;
156  for (auto realDataItem : m_realDataModel->realDataItems()) {
157  const QString linkedIdentifier = realDataItem->instrumentId();
158  const QString instrumentIdentifier = instrumentItem->id();
159 
160  if (linkedIdentifier == instrumentIdentifier)
161  result.append(realDataItem);
162  }
163  return result;
164 }
#define ASSERT(condition)
Definition: Assert.h:31
QString id() const
QVector< RealDataItem * > realDataItems() const

References ASSERT, InstrumentItem::id(), m_realDataModel, and RealDataModel::realDataItems().

Referenced by onInstrumentChildChange().

Here is the call graph for this function:

◆ onInstrumentAddedOrRemoved

void LinkInstrumentManager::onInstrumentAddedOrRemoved ( )
privateslot

Updates map of instruments on insert/remove InstrumentItem event.

Definition at line 123 of file LinkInstrumentManager.cpp.

124 {
125  // remove links in realDataItems (in case of a linked instrument was removed)
126  for (auto realDataItem : m_realDataModel->realDataItems()) {
127  if (!m_instrumentModel->instrumentExists(realDataItem->instrumentId()))
128  realDataItem->clearInstrumentId();
129  }
130 
132 }
bool instrumentExists(const QString &instrumentId) const
void updateInstrumentSubscriptions()
Set up callbacks to all instrument items.

References InstrumentModel::instrumentExists(), m_instrumentModel, m_realDataModel, RealDataModel::realDataItems(), and updateInstrumentSubscriptions().

Referenced by setModels().

Here is the call graph for this function:

◆ onInstrumentChildChange

void LinkInstrumentManager::onInstrumentChildChange ( InstrumentItem instrument,
SessionItem child 
)
privateslot

Perform actions on instrument children change.

Definition at line 105 of file LinkInstrumentManager.cpp.

106 {
107  if (child == nullptr)
108  return;
109 
110  ASSERT(instrument != nullptr);
111 
112  // Run through all RealDataItem and refresh linking to match possible change in detector
113  // axes definition.
114  for (auto realDataItem : linkedRealDataItems(instrument))
115  if (!instrument->alignedWith(realDataItem))
116  realDataItem->clearInstrumentId();
117  else
118  realDataItem->updateToInstrument(instrument);
119 }
virtual bool alignedWith(const RealDataItem *item) const
QList< RealDataItem * > linkedRealDataItems(InstrumentItem *instrumentItem)
Returns list of RealDataItem's linked to given instrument.

References InstrumentItem::alignedWith(), ASSERT, and linkedRealDataItems().

Referenced by updateInstrumentSubscriptions().

Here is the call graph for this function:

◆ setModels()

void LinkInstrumentManager::setModels ( InstrumentModel instrumentModel,
RealDataModel realDataModel 
)

Sets models and builds initial links.

Definition at line 40 of file LinkInstrumentManager.cpp.

42 {
43  ASSERT(instrumentModel != nullptr);
44  ASSERT(realDataModel != nullptr);
45 
47  m_instrumentModel->disconnect(this);
48 
49  if (m_realDataModel)
50  m_realDataModel->disconnect(this);
51 
52  m_instrumentModel = instrumentModel;
53  m_realDataModel = realDataModel;
54 
57 
59 }
void instrumentAddedOrRemoved()
void onInstrumentAddedOrRemoved()
Updates map of instruments on insert/remove InstrumentItem event.

References ASSERT, InstrumentModel::instrumentAddedOrRemoved(), m_instrumentModel, m_realDataModel, onInstrumentAddedOrRemoved(), and updateInstrumentSubscriptions().

Referenced by MainWindow::MainWindow().

Here is the call graph for this function:

◆ updateInstrumentSubscriptions

void LinkInstrumentManager::updateInstrumentSubscriptions ( )
privateslot

Set up callbacks to all instrument items.

Definition at line 136 of file LinkInstrumentManager.cpp.

137 {
138  for (auto instrumentItem : m_instrumentModel->instrumentItems()) {
139  instrumentItem->mapper()->unsubscribe(this);
140 
141  instrumentItem->mapper()->setOnAnyChildChange(
142  [this, instrumentItem](SessionItem* child) {
143  onInstrumentChildChange(instrumentItem, child);
144  },
145  this);
146  }
147 }
QVector< InstrumentItem * > instrumentItems() const
void onInstrumentChildChange(InstrumentItem *instrument, SessionItem *child)
Perform actions on instrument children change.

References InstrumentModel::instrumentItems(), m_instrumentModel, and onInstrumentChildChange().

Referenced by onInstrumentAddedOrRemoved(), and setModels().

Here is the call graph for this function:

Member Data Documentation

◆ m_instrumentModel

InstrumentModel* LinkInstrumentManager::m_instrumentModel
private

◆ m_realDataModel

RealDataModel* LinkInstrumentManager::m_realDataModel
private

The documentation for this class was generated from the following files: