BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
InstrumentModel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/InstrumentModel.cpp
6 //! @brief Implements class InstrumentModel
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 
21 {
22  setObjectName(SessionXML::InstrumentModelTag);
23 
24  connect(this, &SessionModel::rowsInserted, this, &InstrumentModel::onRowsChange);
25  connect(this, &SessionModel::rowsRemoved, this, &InstrumentModel::onRowsChange);
26 }
27 
29 
31 {
32  InstrumentModel* result = new InstrumentModel();
33  result->initFrom(this, parent);
34  return result;
35 }
36 
37 QVector<SessionItem*> InstrumentModel::nonXMLItems() const
38 {
39  QVector<SessionItem*> result;
40 
41  for (auto instrument_item : topItems<SpecularInstrumentItem>()) {
42  auto axis_group = instrument_item->beamItem()
45 
46  if (auto pointwise_axis = axis_group->getChildOfType("PointwiseAxis"))
47  result.push_back(pointwise_axis);
48  }
49 
50  return result;
51 }
52 
53 void InstrumentModel::readFrom(QXmlStreamReader* reader, MessageService* messageService /*= 0*/)
54 {
55  // do not send added-notifications until completely read - otherwise partially
56  // initialized items will be notified
57  disconnect(this, &SessionModel::rowsInserted, this, &InstrumentModel::onRowsChange);
58 
59  SessionModel::readFrom(reader, messageService);
60 
61  connect(this, &SessionModel::rowsInserted, this, &InstrumentModel::onRowsChange);
62 
63  for (auto instrumentItem : instrumentItems()) {
64  instrumentItem->mapper()->setOnPropertyChange(
65  [this, instrumentItem](const QString& name) {
66  onInstrumentPropertyChange(instrumentItem, name);
67  },
68  this);
69  }
70 
71  if (!instrumentItems().isEmpty())
73 }
74 
75 QVector<InstrumentItem*> InstrumentModel::instrumentItems() const
76 {
77  return topItems<InstrumentItem>();
78 }
79 
80 InstrumentItem* InstrumentModel::findInstrumentById(const QString& instrumentId) const
81 {
82  for (auto instrument : instrumentItems())
83  if (instrument->id() == instrumentId)
84  return instrument;
85 
86  return nullptr;
87 }
88 
89 bool InstrumentModel::instrumentExists(const QString& instrumentId) const
90 {
91  return findInstrumentById(instrumentId) != nullptr;
92 }
93 
94 void InstrumentModel::onRowsChange(const QModelIndex& parent, int, int)
95 {
96  // valid parent means not an instrument (which is top level item) but something below
97  if (parent.isValid())
98  return;
99 
100  for (auto instrumentItem : instrumentItems()) {
101  instrumentItem->mapper()->unsubscribe(this);
102 
103  instrumentItem->mapper()->setOnPropertyChange(
104  [this, instrumentItem](const QString& name) {
105  onInstrumentPropertyChange(instrumentItem, name);
106  },
107  this);
108  }
109 
111 }
112 
114  const QString& propertyName)
115 {
116  if (propertyName == SessionItem::P_NAME)
117  emit instrumentNameChanged(instrument);
118 }
Defines InstrumentItems classes.
Defines class InstrumentModel.
Declares the class SpecularBeamInclinationItem.
static const QString P_INCLINATION_ANGLE
Definition: BeamItems.h:30
InstrumentModel * createCopy(SessionItem *parent=nullptr) override
void instrumentNameChanged(const InstrumentItem *instrument)
bool instrumentExists(const QString &instrumentId) const
InstrumentItem * findInstrumentById(const QString &instrumentId) const
QVector< SessionItem * > nonXMLItems() const override
InstrumentModel(QObject *parent=nullptr)
void onRowsChange(const QModelIndex &parent, int, int)
virtual void readFrom(QXmlStreamReader *reader, MessageService *messageService=0) override
void onInstrumentPropertyChange(const InstrumentItem *instrument, const QString &propertyName)
~InstrumentModel() override
QVector< InstrumentItem * > instrumentItems() const
void instrumentAddedOrRemoved()
The service to collect messages from different senders.
static const QString P_NAME
Definition: SessionItem.h:37
virtual QModelIndex parent(const QModelIndex &child) const
virtual void initFrom(SessionModel *model, SessionItem *parent)
virtual void readFrom(QXmlStreamReader *reader, MessageService *messageService=0)
QString const & name(EShape k)
Definition: particles.cpp:21
const QString InstrumentModelTag("InstrumentModel")