BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ParameterTreeUtils Namespace Reference

The ParameterTreeBuilder namespace contains helper functions to create container with ParameterItems. More...

Functions

void createParameterTree (JobItem *jobItem)
 
QString domainNameToParameterName (const QString &domainName, const SessionItem *source)
 Converts domain name to parameterItem name. More...
 
QVector< QPair< QString, QString > > parameterDictionary (const SessionItem *source)
 Correspondance of parameter name to translated name for all properties found in source in its children. More...
 
QString parameterNameToDomainName (const QString &parName, const SessionItem *source)
 Converts parameter name to domain name. More...
 
SessionItemparameterNameToLinkedItem (const QString &parName, const SessionItem *source)
 Converts parameter item name to the corresponding item in the tree below the source. More...
 
QStringList parameterTreeNames (const SessionItem *source)
 Creates list with parameter names of source item. More...
 
void populateParameterContainer (SessionItem *container, const SessionItem *source)
 Populates ParameterContainer with ParameterItem's corresponding to all properties found in a source item. More...
 
QStringList translatedParameterTreeNames (const SessionItem *source)
 Creates domain translated list of parameter names for source item. More...
 
void visitParameterContainer (SessionItem *container, std::function< void(ParameterItem *)> fun)
 Visit all ParameterItem in container and execute user function. More...
 

Detailed Description

The ParameterTreeBuilder namespace contains helper functions to create container with ParameterItems.

The ParameterItem appears in RealTimeView and provides real time tuning of MultiLayerItem and InstrumentItem.

Function Documentation

◆ createParameterTree()

void ParameterTreeUtils::createParameterTree ( JobItem jobItem)

Definition at line 56 of file ParameterTreeUtils.cpp.

57 {
58  auto container = jobItem->model()->insertItem<ParameterContainerItem>(
59  jobItem, -1, JobItem::T_PARAMETER_TREE);
60 
62 
64 
66 
67 #ifndef NDEBUG
68  // Provides all items in "JobItem/Parameter Tree Container" with domain links already
69  // at the stage of ParameterTree creation. It is necessary for validation, in Release mode
70  // it will lead for unnecessary large project files.
71  populateDomainLinks(container);
72 #endif
73 }
static const QString T_SAMPLE
Definition: JobItem.h:47
static const QString T_PARAMETER_TREE
Definition: JobItem.h:53
static const QString T_INSTRUMENT
Definition: JobItem.h:49
static const QString T_MATERIAL_CONTAINER
Definition: JobItem.h:48
The ParameterContainerItem is a top item to hold all ParameterItem, represents an entry point to para...
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:66
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
T * insertItem(SessionItem *parent=nullptr, int row=-1, QString tag="")
Definition: SessionModel.h:125
void populateParameterContainer(SessionItem *container, const SessionItem *source)
Populates ParameterContainer with ParameterItem's corresponding to all properties found in a source i...

References SessionItem::getItem(), SessionModel::insertItem(), SessionItem::model(), populateParameterContainer(), JobItem::T_INSTRUMENT, JobItem::T_MATERIAL_CONTAINER, JobItem::T_PARAMETER_TREE, and JobItem::T_SAMPLE.

Referenced by JobModel::addJob().

Here is the call graph for this function:

◆ domainNameToParameterName()

QString ParameterTreeUtils::domainNameToParameterName ( const QString &  domainName,
const SessionItem source 
)

Converts domain name to parameterItem name.

Parameter name should belong to item or one of its children.

Definition at line 168 of file ParameterTreeUtils.cpp.

170 {
171  QString domain = removeLeadingSlash(domainName);
172  for (auto pair : parameterDictionary(source)) { // parName, domainName
173  if (pair.second == domain)
174  return pair.first;
175  }
176 
177  return {};
178 }
QVector< QPair< QString, QString > > parameterDictionary(const SessionItem *source)
Correspondance of parameter name to translated name for all properties found in source in its childre...

References parameterDictionary().

Referenced by ParticleDistributionItem::translateParameterNameToGUI().

Here is the call graph for this function:

◆ parameterDictionary()

QVector< QPair< QString, QString > > ParameterTreeUtils::parameterDictionary ( const SessionItem source)

Correspondance of parameter name to translated name for all properties found in source in its children.

Definition at line 137 of file ParameterTreeUtils.cpp.

138 {
139  ASSERT(source);
140 
141  QVector<QPair<QString, QString>> result;
142 
143  // Create container with ParameterItem's of given source item
144  SampleModel model;
145  auto container = model.insertItem<ParameterContainerItem>();
146  populateParameterContainer(container, source);
147 
148  // Iterate through all ParameterItems and retrieve necessary data.
149  visitParameterContainer(container, [&](ParameterItem* parItem) {
150  // TODO replace with the method from ModelPath
151  QString parPath = FitParameterHelper::getParameterItemPath(parItem);
152 
153  QString relPath =
154  source->displayName() + "/" + parItem->getItemValue(ParameterItem::P_LINK).toString();
155  SessionItem* linkedItem = ModelPath::getItemFromPath(relPath, source);
156  QString translation = ModelPath::itemPathTranslation(*linkedItem, source->parent());
157 
158  result.push_back(QPair<QString, QString>(parPath, translation));
159  });
160  std::reverse(result.begin(), result.end());
161 
162  return result;
163 }
#define ASSERT(condition)
Definition: Assert.h:31
static QString getParameterItemPath(const ParameterItem *parameterItem)
return path to given item in the ParameterTreeContainer
The ParameterItem class represent a tuning value in a parameter tuning tree.
static const QString P_LINK
Main model to hold sample items.
Definition: SampleModel.h:24
QString displayName() const
Get display name of item, append index if ambigue.
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
SessionItem * parent() const
Returns parent of this item.
Definition: SessionItem.cpp:73
SessionItem * getItemFromPath(const QString &relPath, const SessionItem *parent)
returns an item from relative path wrt to given parent
Definition: ModelPath.cpp:60
QString itemPathTranslation(const SessionItem &item, const SessionItem *topItem=0)
Returns translation of item path to domain name.
Definition: ModelPath.cpp:98
void visitParameterContainer(SessionItem *container, std::function< void(ParameterItem *)> fun)
Visit all ParameterItem in container and execute user function.

References ASSERT, SessionItem::displayName(), ModelPath::getItemFromPath(), SessionItem::getItemValue(), FitParameterHelper::getParameterItemPath(), SessionModel::insertItem(), ModelPath::itemPathTranslation(), ParameterItem::P_LINK, SessionItem::parent(), populateParameterContainer(), and visitParameterContainer().

Referenced by domainNameToParameterName(), parameterNameToDomainName(), parameterTreeNames(), and translatedParameterTreeNames().

Here is the call graph for this function:

◆ parameterNameToDomainName()

QString ParameterTreeUtils::parameterNameToDomainName ( const QString &  parName,
const SessionItem source 
)

Converts parameter name to domain name.

Parameter name should belong to item or one of its children.

Definition at line 183 of file ParameterTreeUtils.cpp.

185 {
186  for (auto pair : parameterDictionary(source)) // parName, domainName
187  if (pair.first == parName)
188  return "/" + pair.second;
189 
190  return {};
191 }

References parameterDictionary().

Referenced by ParticleDistributionItem::domainLinkedParameters(), and ParticleDistributionItem::domainMainParameter().

Here is the call graph for this function:

◆ parameterNameToLinkedItem()

SessionItem * ParameterTreeUtils::parameterNameToLinkedItem ( const QString &  parName,
const SessionItem source 
)

Converts parameter item name to the corresponding item in the tree below the source.

Definition at line 195 of file ParameterTreeUtils.cpp.

197 {
198  SampleModel model;
199  auto container = model.insertItem<ParameterContainerItem>();
200  populateParameterContainer(container, source);
201 
202  // Iterate through all ParameterItems and retrieve necessary data.
203  SessionItem* result(nullptr);
204  visitParameterContainer(container, [&](ParameterItem* parItem) {
205  // TODO replace with the method from ModelPath
206  QString parPath = FitParameterHelper::getParameterItemPath(parItem);
207  if (parPath == parName) {
208  QString relPath = source->displayName() + "/"
209  + parItem->getItemValue(ParameterItem::P_LINK).toString();
210  result = ModelPath::getItemFromPath(relPath, source);
211  }
212  });
213 
214  return result;
215 }

References SessionItem::displayName(), ModelPath::getItemFromPath(), SessionItem::getItemValue(), FitParameterHelper::getParameterItemPath(), SessionModel::insertItem(), ParameterItem::P_LINK, populateParameterContainer(), and visitParameterContainer().

Here is the call graph for this function:

◆ parameterTreeNames()

QStringList ParameterTreeUtils::parameterTreeNames ( const SessionItem source)

Creates list with parameter names of source item.

Definition at line 112 of file ParameterTreeUtils.cpp.

113 {
114  QStringList result;
115 
116  for (auto pair : parameterDictionary(source))
117  result << pair.first;
118 
119  return result;
120 }

References parameterDictionary().

Referenced by ParticleDistributionItem::childParameterNames().

Here is the call graph for this function:

◆ populateParameterContainer()

void ParameterTreeUtils::populateParameterContainer ( SessionItem container,
const SessionItem source 
)

Populates ParameterContainer with ParameterItem's corresponding to all properties found in a source item.

Definition at line 78 of file ParameterTreeUtils.cpp.

80 {
81  if (container->modelType() != "Parameter Container")
82  throw GUIHelpers::Error("ParameterTreeUtils::populateParameterContainer() -> Error. "
83  "Not a ParameterContainerType.");
84 
85  auto sourceLabel = container->model()->insertItem<ParameterLabelItem>(container);
86  handleItem(sourceLabel, source);
87 }
ParameterTreeItems is a collection of items necessary to form a tuning tree for real time widget.
QString modelType() const
Get model type.

References SessionModel::insertItem(), SessionItem::model(), and SessionItem::modelType().

Referenced by createParameterTree(), parameterDictionary(), and parameterNameToLinkedItem().

Here is the call graph for this function:

◆ translatedParameterTreeNames()

QStringList ParameterTreeUtils::translatedParameterTreeNames ( const SessionItem source)

Creates domain translated list of parameter names for source item.

Definition at line 124 of file ParameterTreeUtils.cpp.

125 {
126  QStringList result;
127 
128  for (auto pair : parameterDictionary(source))
129  result << pair.second;
130 
131  return result;
132 }

References parameterDictionary().

Here is the call graph for this function:

◆ visitParameterContainer()

void ParameterTreeUtils::visitParameterContainer ( SessionItem container,
std::function< void(ParameterItem *)>  fun 
)

Visit all ParameterItem in container and execute user function.

Definition at line 91 of file ParameterTreeUtils.cpp.

93 {
94  SessionItem* current(container);
95  QStack<SessionItem*> stack;
96  stack.push(current);
97  while (!stack.empty()) {
98  current = stack.pop();
99  if (current->modelType() == "Parameter Label"
100  || current->modelType() == "Parameter Container") {
101  for (SessionItem* child : current->getItems())
102  stack.push(child);
103  } else {
104  if (ParameterItem* parItem = dynamic_cast<ParameterItem*>(current))
105  fun(parItem);
106  }
107  }
108 }
QVector< SessionItem * > getItems(const QString &tag="") const
Returns vector of all items of given tag.

References SessionItem::getItems(), and SessionItem::modelType().

Referenced by parameterDictionary(), and parameterNameToLinkedItem().

Here is the call graph for this function: