BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ParameterTreeItems.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Job/ParameterTreeItems.cpp
6 //! @brief Implements classes for ParameterTreeItems
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 
17 #include "GUI/Model/Job/JobItem.h"
22 #include "GUI/Util/Path.h"
23 #include <QXmlStreamReader>
24 #include <QXmlStreamWriter>
25 
26 namespace {
27 
28 namespace Tags {
29 
30 const QString BackupValues("BackupValues");
31 const QString BackupValue("BackupValue");
32 const QString Link("Link");
33 const QString Value("Value");
34 
35 } // namespace Tags
36 
37 ParameterItem* findParameterItem(QObject* item, const QString& link)
38 {
39  ASSERT(item);
40  if (auto* parameter = dynamic_cast<ParameterItem*>(item))
41  if (parameter->link() == link)
42  return parameter;
43 
44  for (auto* child : item->children())
45  if (auto* p = findParameterItem(child, link))
46  return p;
47 
48  return nullptr;
49 }
50 
51 } // namespace
52 
53 
55  : QObject(parent)
56 {
57 }
58 
59 ParameterLabelItem::ParameterLabelItem(const QString& title, QObject* parent)
60  : QObject(parent)
61  , m_title(title)
62 {
63 }
64 
66 {
67  return m_title;
68 }
69 
70 void ParameterLabelItem::setTitle(const QString& title)
71 {
72  m_title = title;
73 }
74 
76  : QObject(parent)
77 {
78 }
79 
80 // ----------------------------------------------------------------------------
81 
82 
83 QString ParameterItem::title() const
84 {
85  return m_title;
86 }
87 
88 void ParameterItem::setTitle(const QString& title)
89 {
90  m_title = title;
91 }
92 
94 {
95  return m_d.get();
96 }
97 
98 //! Sets current value to the original PropertyItem of MultiLayerItem/InstrumentItem.
99 
101 {
102  m_d.set(newValue);
103 }
104 
106 {
107  m_link = d.path();
108  m_d = d;
109 }
110 
112 {
114  m_d = DoubleDescriptor(item, "");
115 }
116 
117 RealLimits ParameterItem::limitsOfLink() const
118 {
119  return m_d.limits;
120 }
121 
123 {
124  return m_d.decimals;
125 }
126 
127 QString ParameterItem::link() const
128 {
129  return m_link;
130 }
131 
133 {
134  QString result = m_title;
135 
136  auto* p = parent();
137  while (p) {
138  if (const auto* pLabel = dynamic_cast<ParameterLabelItem*>(p))
139  result.push_front(pLabel->title() + "/");
140  p = p->parent();
141  }
142 
143  return result;
144 }
145 
146 // ----------------------------------------------------------------------------
147 
149 {
150  m_parameterTreeRoot.reset(new QObject());
151 }
152 
153 void ParameterContainerItem::writeContentTo(QXmlStreamWriter* writer) const
154 {
156 
157  writer->writeStartElement(Tags::BackupValues);
158 
159  for (auto v = m_backupValues.cbegin(); v != m_backupValues.cend(); v++) {
160  writer->writeEmptyElement(Tags::BackupValue);
161  GUI::Session::XML::writeAttribute(writer, Tags::Link, v.key());
163  }
164  writer->writeEndElement();
165 }
166 
167 void ParameterContainerItem::readContentFrom(QXmlStreamReader* reader)
168 {
169  m_backupValues.clear();
170  const int version = reader->attributes().value(GUI::Session::XML::Version).toInt();
171 
172  if (version < 1)
174 
175  if (version > 1)
177 
178  while (reader->readNextStartElement()) {
179  if (reader->name().toString() == Tags::BackupValues) {
180  while (reader->readNextStartElement()) {
181  ASSERT(reader->name().toString() == Tags::BackupValue);
182  QString link;
183  double d;
184  GUI::Session::XML::readAttribute(reader, Tags::Link, &link);
186  m_backupValues[link] = d;
187  reader->skipCurrentElement();
188  }
189  }
190  }
191 }
192 
193 void ParameterContainerItem::setBackupValue(const QString& link, double d)
194 {
195  m_backupValues[link] = d;
196 }
197 
199 {
200  ASSERT(item);
201  if (auto* parameter = dynamic_cast<ParameterItem*>(item))
202  if (m_backupValues.contains(parameter->link()))
203  parameter->propagateValueToLink(m_backupValues[parameter->link()]);
204 
205  for (auto* child : item->children())
206  restoreBackupValue(child);
207 }
208 
210 {
211  for (auto* child : m_parameterTreeRoot->children())
212  restoreBackupValue(child);
213 }
214 
216 {
217  return ::findParameterItem(m_parameterTreeRoot.get(), link);
218 }
219 
221 {
222  return m_parameterTreeRoot.get();
223 }
Defines class DeserializationException.
Defines class JobItem.
Defines class MaterialItem.
Defines class MaterialItems.
Defines classes for ParameterTreeItems.
Defines class Helpers functions.
Defines.
Defines class VectorDescriptor.
static DeserializationException tooOld()
static DeserializationException tooNew()
Describes properties of a double value which are necessary to allow GUI representation,...
function< void(double)> set
function to set the value
RealLimits limits
Limits of the value.
function< double()> get
function to get the current value
int decimals
numbers of decimals to be shown in an edit control
function< QString()> path
Path describing this value. Used e.g. for undo/redo.
void setBackupValue(const QString &link, double d)
ParameterItem * findParameterItem(const QString &link) const
std::unique_ptr< QObject > m_parameterTreeRoot
QMap< QString, double > m_backupValues
void restoreBackupValue(QObject *item)
void readContentFrom(QXmlStreamReader *reader)
void writeContentTo(QXmlStreamWriter *writer) const
The ParameterItem class represent a tuning value in a parameter tuning tree.
QString titleForFitItem() const
void linkToSessionItem(SessionItem *item)
Links this item to the given session item.
double valueOfLink() const
void propagateValueToLink(double newValue)
Sets current value to the original PropertyItem of MultiLayerItem/InstrumentItem.
QString m_link
See docu of link()
int decimalsOfLink() const
ParameterItem(QObject *parent)
QString link() const
Unique string to identify this ParameterItem.
RealLimits limitsOfLink() const
QString title() const
void setTitle(const QString &title)
void linkToDescriptor(DoubleDescriptor d)
Links this item to the given value defined by a descriptor.
DoubleDescriptor m_d
The linked double value.
ParameterTreeItems is a collection of items necessary to form a tuning tree for real time widget.
ParameterLabelItem(QObject *parent)
void setTitle(const QString &title)
Base class for a GUI data item.
Definition: SessionItem.h:204
QModelIndex index() const
Returns model index of this item.
Definition: SessionItem.cpp:74
void readAttribute(QXmlStreamReader *reader, const QString &attributeName, double *d)
Definition: UtilXML.cpp:193
constexpr auto Version("Version")
void writeAttribute(QXmlStreamWriter *writer, const QString &attributeName, const QVariant &variant)
Write the variant's value as an attribute.
Definition: UtilXML.cpp:65
QString getPathFromIndex(const QModelIndex &index)
Definition: Path.cpp:144
Definition: JobItem.cpp:33
constexpr auto Value("value")