BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MaterialPropertyController.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/MaterialPropertyController.cpp
6 //! @brief Defines MaterialPropertyController class
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 
21 #include <QVector>
22 
24  : QObject(parent), m_materialModel(nullptr), m_sampleModel(nullptr)
25 {
26 }
27 
29 {
30  m_materialModel = materialModel;
31  m_sampleModel = sampleModel;
32 
33  // connect(m_materialModel, &MaterialModel::dataChanged, this,
34  // &MaterialPropertyController::onMaterialDataChanged);
35 
36  // connect(m_materialModel, &MaterialModel::rowsAboutToBeRemoved, this,
37  // &MaterialPropertyController::onMaterialRowsAboutToBeRemoved);
38 
41 }
42 
43 //! Special case when original MaterialModel was fully rebuild from MaterialEditor.
44 //! Full update of MaterialProperties.
45 
47 {
48  for (auto sampleItem : relatedSampleItems()) {
49  QString tag = MaterialItemUtils::materialTag(*sampleItem);
50  ASSERT(!tag.isEmpty());
51 
52  ExternalProperty property = sampleItem->getItemValue(tag).value<ExternalProperty>();
53  if (MaterialItem* material =
54  m_materialModel->materialFromIdentifier(property.identifier())) {
55  ExternalProperty new_property = MaterialItemUtils::materialProperty(*material);
56  sampleItem->setItemValue(tag, new_property.variant());
57  } else {
58  ExternalProperty undefined;
59  sampleItem->setItemValue(tag, undefined.variant());
60  }
61  }
62 }
63 
64 //! On MaterialItem change: updates corresponding MaterialProperty in sample items.
65 
66 void MaterialPropertyController::onMaterialDataChanged(const QModelIndex& topLeft,
67  const QModelIndex&, const QVector<int>&)
68 {
69  auto changedItem = m_materialModel->itemForIndex(topLeft);
70  if (auto materialItem =
71  dynamic_cast<const MaterialItem*>(ModelPath::ancestor(changedItem, "Material"))) {
72 
73  for (auto sampleItem : relatedSampleItems()) {
74  QString tag = MaterialItemUtils::materialTag(*sampleItem);
75  ASSERT(!tag.isEmpty());
76 
77  ExternalProperty property = sampleItem->getItemValue(tag).value<ExternalProperty>();
78  if (property.identifier() == materialItem->identifier()) {
79  ExternalProperty new_property = MaterialItemUtils::materialProperty(*materialItem);
80  sampleItem->setItemValue(tag, new_property.variant());
81  }
82  }
83  }
84 }
85 
86 //! On MaterialItem removal: updates corresponding MaterialProperty in sample items.
87 
89  int first, int last)
90 {
91  // looking for top level items (MaterialItems)
92  if (parent.isValid())
93  return;
94 
95  // Building list of material identifiers which will be deleted
96  QStringList identifiersToDelete;
97  for (int i_row = first; i_row <= last; ++i_row) {
98  QModelIndex changed = m_materialModel->index(i_row, 0, parent);
99  if (auto material = dynamic_cast<MaterialItem*>(m_materialModel->itemForIndex(changed)))
100  identifiersToDelete.push_back(material->identifier());
101  }
102 
103  // rewriting MaterialProperty in corresponding sample items
104  for (auto sampleItem : relatedSampleItems()) {
105  QString tag = MaterialItemUtils::materialTag(*sampleItem);
106  ASSERT(!tag.isEmpty());
107 
108  ExternalProperty property = sampleItem->getItemValue(tag).value<ExternalProperty>();
109  if (identifiersToDelete.contains(property.identifier())) {
110  ExternalProperty undefined;
111  sampleItem->setItemValue(tag, undefined.variant());
112  }
113  }
114 }
115 
116 //! Returns vector of SessionItems having MaterialProperty on board.
117 
119 {
120  static QStringList materialRelated = MaterialItemUtils::materialRelatedModelTypes();
121 
122  QVector<SessionItem*> result;
123  ModelUtils::iterate(QModelIndex(), m_sampleModel, [&](const QModelIndex& index) {
124  if (index.column() != 0)
125  return;
126 
127  if (SessionItem* item = m_sampleModel->itemForIndex(index))
128  if (materialRelated.contains(item->modelType()))
129  result.push_back(item);
130  });
131 
132  return result;
133 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines class MaterialItemUtils.
Defines class MaterialModel.
Defines MaterialPropertyController class.
Defines ModelPath namespace.
Defines ModelUtils namespace.
Defines class SampleModel.
The ExternalProperty class defines custom QVariant property to carry the text, color and an identifie...
QVariant variant() const
MaterialItem * materialFromIdentifier(const QString &identifier)
void setModels(MaterialModel *materialModel, SampleModel *sampleModel)
QVector< SessionItem * > relatedSampleItems()
Returns vector of SessionItems having MaterialProperty on board.
void onMaterialDataChanged(const QModelIndex &topLeft, const QModelIndex &, const QVector< int > &)
On MaterialItem change: updates corresponding MaterialProperty in sample items.
MaterialPropertyController(QObject *parent=nullptr)
void onMaterialRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
On MaterialItem removal: updates corresponding MaterialProperty in sample items.
void onMaterialModelLoad()
Special case when original MaterialModel was fully rebuild from MaterialEditor.
Main model to hold sample items.
Definition: SampleModel.h:24
void modelLoaded()
SessionItem * itemForIndex(const QModelIndex &index) const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QString materialTag(const SessionItem &item)
Returns material tag for given item. Returns empty string, if item doesn't have materials.
QStringList materialRelatedModelTypes()
Returns list of model types which contains registered MaterialProperty.
ExternalProperty materialProperty(const SessionItem &materialItem)
Constructs material property corresponding to given material.
const SessionItem * ancestor(const SessionItem *item, const QString &requiredModelType)
Returns ancestor of given modelType for given item.
Definition: ModelPath.cpp:87
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