BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelPath.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/ModelPath.cpp
6 //! @brief Implements class ModelPath
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 
19 QString ModelPath::getPathFromIndex(const QModelIndex& index)
20 {
21  if (index.isValid()) {
22  QStringList namePath;
23  QModelIndex cur = index;
24  while (cur.isValid()) {
25  namePath << cur.data().toString();
26  cur = cur.parent();
27  }
28  std::reverse(namePath.begin(), namePath.end());
29  return namePath.join("/");
30  }
31  return "";
32 }
33 
34 // TODO cover with unit tests and simplify
35 
36 QModelIndex ModelPath::getIndexFromPath(const SessionModel* model, const QString& path)
37 {
38  if (model) {
39  QStringList parts = path.split("/");
40  SessionItem* t = model->rootItem();
41  for (int i = 0; i < parts.length(); i++) {
42  if (t->modelType() == "JobItem" && parts[i] == "GISASInstrument") {
44  continue;
45  }
46  for (int j = 0; j < t->numberOfChildren(); j++) {
47  if (t->childAt(j)->itemName() == parts[i]) {
48  t = t->childAt(j);
49  break;
50  }
51  }
52  }
53  return t->index();
54  }
55  return QModelIndex();
56 }
57 
58 //! returns an item from relative path wrt to given parent
59 
60 SessionItem* ModelPath::getItemFromPath(const QString& relPath, const SessionItem* parent)
61 {
62  ASSERT(parent);
63  QString fullPath = getPathFromIndex(parent->index()) + "/" + relPath;
64  return parent->model()->itemForIndex(ModelPath::getIndexFromPath(parent->model(), fullPath));
65 }
66 
67 //! Iterates through all the model and returns true if item is found. This is to
68 
69 bool ModelPath::isValidItem(SessionModel* model, SessionItem* item, const QModelIndex& parent)
70 {
71  for (int i_row = 0; i_row < model->rowCount(parent); ++i_row) {
72  QModelIndex index = model->index(i_row, 0, parent);
73  SessionItem* curr = model->itemForIndex(index);
74  if (curr == item)
75  return true;
76 
77  bool isvalid = isValidItem(model, item, index);
78  if (isvalid)
79  return isvalid;
80  }
81  return false;
82 }
83 
84 //! Returns ancestor of given modelType for given item.
85 //! For example, returns corresponding jobItem owning ParameterItem via ParameterContainer.
86 
87 const SessionItem* ModelPath::ancestor(const SessionItem* item, const QString& requiredModelType)
88 {
89  const SessionItem* cur = item;
90  while (cur && cur->modelType() != requiredModelType)
91  cur = cur->parent();
92 
93  return cur;
94 }
95 
96 //! Returns translation of item path to domain name
97 
98 QString ModelPath::itemPathTranslation(const SessionItem& item, const SessionItem* topItem)
99 {
100  QStringList pathList;
101  const SessionItem* current(&item);
102  while (current && current != topItem) {
103  pathList = current->translateList(pathList);
104  current = current->parent();
105  }
106  std::reverse(pathList.begin(), pathList.end());
107  return pathList.join("/");
108 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines class JobItem.
Defines ModelPath namespace.
Defines class SessionModel.
static const QString T_INSTRUMENT
Definition: JobItem.h:49
QString itemName() const
Get item name, return display name if no name is set.
int numberOfChildren() const
Returns total number of children.
Definition: SessionItem.cpp:94
virtual QStringList translateList(const QStringList &list) const
SessionItem * parent() const
Returns parent of this item.
Definition: SessionItem.cpp:73
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:66
SessionItem * childAt(int row) const
Returns the child at the given row.
QString modelType() const
Get model type.
QModelIndex index() const
Returns model index of this item.
Definition: SessionItem.cpp:80
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
SessionItem * itemForIndex(const QModelIndex &index) const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
virtual int rowCount(const QModelIndex &parent) const
SessionItem * rootItem() const
SessionItem * getItemFromPath(const QString &relPath, const SessionItem *parent)
returns an item from relative path wrt to given parent
Definition: ModelPath.cpp:60
QModelIndex getIndexFromPath(const SessionModel *model, const QString &path)
Definition: ModelPath.cpp:36
bool isValidItem(SessionModel *model, SessionItem *item, const QModelIndex &parent)
Iterates through all the model and returns true if item is found. This is to.
Definition: ModelPath.cpp:69
const SessionItem * ancestor(const SessionItem *item, const QString &requiredModelType)
Returns ancestor of given modelType for given item.
Definition: ModelPath.cpp:87
QString getPathFromIndex(const QModelIndex &index)
Definition: ModelPath.cpp:19
QString itemPathTranslation(const SessionItem &item, const SessionItem *topItem=0)
Returns translation of item path to domain name.
Definition: ModelPath.cpp:98