BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
InstrumentCollection.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Device/InstrumentCollection.cpp
6 //! @brief Implement class InstrumentCollection
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 "GUI/Util/String.h"
21 
23 {
24  qDeleteAll(m_instruments);
25 }
26 
28 {
29  qDeleteAll(m_instruments);
30  m_instruments.clear();
31 }
32 
34 {
35  s.assertVersion(0);
36  Serialize::rwCatalogized<InstrumentItemCatalog>(s, "InstrumentCollection", m_instruments);
37 }
38 
40 {
41  auto* copy = source.createCopy();
42  copy->setId(QUuid::createUuid().toString());
43  m_instruments << copy;
44  return copy;
45 }
46 
47 QVector<InstrumentItem*> InstrumentCollection::collectedItems() const
48 {
49  return m_instruments;
50 }
51 
53 {
54  QStringList existingNames;
55  for (const auto* item : collectedItems())
56  existingNames << item->instrumentName();
57  return existingNames;
58 }
59 
61 {
63 }
64 
65 QVector<InstrumentItem*> InstrumentCollection::instrumentItems(
66  const std::function<bool(const InstrumentItem*)>& accept) const
67 {
68  QVector<InstrumentItem*> result;
69  for (auto* p : collectedItems())
70  if (accept(p))
71  result << p;
72 
73  return result;
74 }
75 
76 QVector<Instrument2DItem*> InstrumentCollection::instrument2DItems() const
77 {
78  QVector<Instrument2DItem*> result;
79  for (auto* p : collectedItems())
80  if (auto* p2D = dynamic_cast<Instrument2DItem*>(p))
81  result << p2D;
82 
83  return result;
84 }
85 
86 InstrumentItem* InstrumentCollection::findInstrumentById(const QString& instrumentId) const
87 {
88  for (auto* instrument : collectedItems())
89  if (instrument->id() == instrumentId)
90  return instrument;
91 
92  return nullptr;
93 }
94 
95 bool InstrumentCollection::instrumentExists(const QString& instrumentId) const
96 {
97  return findInstrumentById(instrumentId) != nullptr;
98 }
99 
101 {
102  m_instruments.removeAll(instrument);
103  delete instrument;
104 }
Defines class InstrumentCollection.
Defines class InstrumentItemCatalog.
Defines class InstrumentItem and all its children.
Defines class RealDataItem.
Defines class Streamer.
Defines functions from namespace GUI::Util::String.
InstrumentItem * findInstrumentById(const QString &instrumentId) const
void serialize(Streamer &s)
QVector< InstrumentItem * > instrumentItems(const std::function< bool(const InstrumentItem *)> &accept) const
QVector< InstrumentItem * > m_instruments
void removeInstrument(InstrumentItem *instrument)
QVector< Instrument2DItem * > instrument2DItems() const
QVector< InstrumentItem * > collectedItems() const
QString suggestInstrumentName(const QString &baseName) const
InstrumentItem * insertCopy(const InstrumentItem &source)
Inserts a deep copy (also of any non xml data in a pointwise axis) The id will not be copied,...
bool instrumentExists(const QString &instrumentId) const
QStringList instrumentNames() const
Abstract base class for instrument-specific item classes.
InstrumentItem * createCopy() const
Creates an exact copy; also ID is the same!
void setId(const QString &id)
Supports serialization to or deserialization from QXmlStream.
Definition: Streamer.h:36
void assertVersion(unsigned expectedVersion) const
As reader, throws DeserializationException unless the expected version is read. As writer,...
Definition: Streamer.cpp:26
QString baseName(const QString &fileName)
Returns base name of file.
Definition: Path.cpp:133
QString suggestName(const QStringList &existingNames, const QString &name)
Returns a name suggestion based on the given name.
Definition: String.cpp:26
QString toString(const QModelIndex &index)
Provides string representation of index data.