BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/ModelUtils.cpp
6 //! @brief Implements ModelUtils namespace
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 <QAbstractItemModel>
18 #include <QModelIndex>
19 
20 QStringList ModelUtils::topItemNames(SessionModel* model, const QString& modelType)
21 {
22  QStringList result;
23 
24  for (auto item : model->topItems())
25  if (modelType.isEmpty())
26  result.append(item->itemName());
27  else if (modelType == item->modelType())
28  result.append(item->itemName());
29 
30  return result;
31 }
32 
33 void ModelUtils::iterate(const QModelIndex& index, const QAbstractItemModel* model,
34  const std::function<void(const QModelIndex&)>& fun)
35 {
36  if (index.isValid())
37  fun(index);
38 
39  if (!model->hasChildren(index))
40  return;
41 
42  for (int i = 0; i < model->rowCount(index); ++i)
43  for (int j = 0; j < model->columnCount(index); ++j)
44  iterate(model->index(i, j, index), model, fun);
45 }
46 
47 void ModelUtils::iterate_if(const QModelIndex& index, const QAbstractItemModel* model,
48  const std::function<bool(const QModelIndex&)>& fun)
49 {
50  bool proceed_with_children(true);
51  if (index.isValid())
52  proceed_with_children = fun(index);
53 
54  if (!model->hasChildren(index) || !proceed_with_children)
55  return;
56 
57  for (int i = 0; i < model->rowCount(index); ++i)
58  for (int j = 0; j < model->columnCount(index); ++j)
59  iterate_if(model->index(i, j, index), model, fun);
60 }
Defines ModelUtils namespace.
Defines class SessionModel.
QVector< T * > topItems() const
Definition: SessionModel.h:147
QStringList topItemNames(SessionModel *model, const QString &modelType="")
Returns list of top iten manes.
Definition: ModelUtils.cpp:20
void iterate_if(const QModelIndex &index, const QAbstractItemModel *model, const std::function< bool(const QModelIndex &)> &fun)
Iterates through all model indices and calls user function.
Definition: ModelUtils.cpp:47
void iterate(const QModelIndex &index, const QAbstractItemModel *model, const std::function< void(const QModelIndex &)> &fun)
Iterates through all model indices and calls user function.
Definition: ModelUtils.cpp:33