BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
InstrumentLibrary.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Device/InstrumentLibrary.cpp
6 //! @brief Implements class InstrumentLibrary
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 
19 #include "GUI/Util/Path.h"
20 #include <QFile>
21 #include <QXmlStreamWriter>
22 
23 namespace {
24 
25 const QString XML_ROOT_TAG = "BornAgainInstrumentLibrary";
26 const QString XML_VERSION_TAG = "Version";
27 
28 QString instrumentLibraryFilePath()
29 {
30  return GUI::Util::Path::appDataFolder() + "/BornAgainInstrumentLibrary.balib";
31 }
32 
33 } // namespace
34 
35 
37  : m_ec(&m_instrumentItems)
38  , m_modified(false)
39 {
41  [&]() { m_modified = true; });
43  [&]() { m_modified = true; });
44 }
45 
46 QString InstrumentLibrary::suggestName(const QString& name) const
47 {
49 }
50 
51 InstrumentItem* InstrumentLibrary::add(const QString& name, const InstrumentItem& itemToCopy)
52 {
53  return m_ec.addCopy(&itemToCopy, name);
54 }
55 
57 {
58  return m_instrumentItems.collectedItems().isEmpty();
59 }
60 
61 QList<InstrumentItem*> InstrumentLibrary::collectedItems() const
62 {
63  return m_instrumentItems.collectedItems().toList();
64 }
65 
67 {
68  return &m_ec;
69 }
70 
72 {
73  if (!m_modified)
74  return true;
75 
76  QFile file(instrumentLibraryFilePath());
77  if (!file.open(QFile::ReadWrite | QIODevice::Truncate | QFile::Text))
78  return false;
79 
80  QXmlStreamWriter writer(&file);
81  writer.setAutoFormatting(true);
82  writer.writeStartDocument();
83  writer.writeStartElement(XML_ROOT_TAG);
84  writer.writeAttribute(XML_VERSION_TAG, "2");
85 
86  Streamer sInstruments(&writer);
87  m_instrumentItems.serialize(sInstruments);
88 
89  writer.writeEndElement();
90  writer.writeEndDocument();
91 
92  file.close();
93  m_modified = false;
94  return true;
95 }
96 
98 {
99  m_modified = false;
101 
102  QFile file(instrumentLibraryFilePath());
103  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
104  return false;
105 
106  try {
107  QXmlStreamReader reader(&file);
108 
109  while (!reader.atEnd()) {
110  reader.readNext();
111  if (reader.isStartElement()) {
112  if (reader.name() == XML_ROOT_TAG) {
113  const int foundVersion =
114  reader.attributes().value(XML_VERSION_TAG).toString().toInt();
115  if (foundVersion != 2)
116  return false;
117 
118  Streamer sInstruments(&reader);
119  m_instrumentItems.serialize(sInstruments);
120  }
121  }
122  }
123 
124  if (reader.hasError()) {
126  return false;
127  }
128 
129  file.close();
130  return true;
131  } catch (const std::exception&) {
133  return false;
134  } catch (const DeserializationException&) {
136  return false;
137  }
138 }
139 
141 {
142  return &m_instrumentItems;
143 }
Defines class DeserializationException.
Defines class InstrumentItem and all its children.
Defines class InstrumentLibrary.
Defines class Helpers functions.
Defines class Streamer.
void serialize(Streamer &s)
QVector< InstrumentItem * > collectedItems() const
QString suggestInstrumentName(const QString &baseName) const
Abstract base class for instrument-specific item classes.
InstrumentsEditController * editController()
InstrumentItem * add(const QString &name, const InstrumentItem &itemToCopy)
Returns the new element.
QString suggestName(const QString &name) const
InstrumentCollection m_instrumentItems
InstrumentsEditController m_ec
QList< InstrumentItem * > collectedItems() const
Class to modify the instruments list or a single instrument and provide the necessary signaling withi...
InstrumentItem * addCopy(const InstrumentItem *instrument, const QString &name)
Copy an instrument, set its name and emit the respective signal.
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.
Supports serialization to or deserialization from QXmlStream.
Definition: Streamer.h:36
QString const & name(EShape k)
Definition: particles.cpp:20
QString appDataFolder()
The folder where persistent application data shall be stored. Under Windows this is the AppData/Roami...
Definition: Path.cpp:139