BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MaterialPropertyController Class Reference

Listens MaterialModel for changes in MaterialItems and then updates MaterialProperties in all related items in SampleModel. More...

Inheritance diagram for MaterialPropertyController:
[legend]
Collaboration diagram for MaterialPropertyController:
[legend]

Public Slots

void onMaterialModelLoad ()
 Special case when original MaterialModel was fully rebuild from MaterialEditor. More...
 

Public Member Functions

 MaterialPropertyController (QObject *parent=nullptr)
 
void setModels (MaterialModel *materialModel, SampleModel *sampleModel)
 

Private Slots

void onMaterialDataChanged (const QModelIndex &topLeft, const QModelIndex &, const QVector< int > &)
 On MaterialItem change: updates corresponding MaterialProperty in sample items. More...
 
void onMaterialRowsAboutToBeRemoved (const QModelIndex &parent, int first, int last)
 On MaterialItem removal: updates corresponding MaterialProperty in sample items. More...
 

Private Member Functions

QVector< SessionItem * > relatedSampleItems ()
 Returns vector of SessionItems having MaterialProperty on board. More...
 

Private Attributes

MaterialModelm_materialModel
 
SampleModelm_sampleModel
 

Detailed Description

Listens MaterialModel for changes in MaterialItems and then updates MaterialProperties in all related items in SampleModel.

Definition at line 27 of file MaterialPropertyController.h.

Constructor & Destructor Documentation

◆ MaterialPropertyController()

MaterialPropertyController::MaterialPropertyController ( QObject *  parent = nullptr)

Definition at line 23 of file MaterialPropertyController.cpp.

24  : QObject(parent), m_materialModel(nullptr), m_sampleModel(nullptr)
25 {
26 }

Member Function Documentation

◆ onMaterialDataChanged

void MaterialPropertyController::onMaterialDataChanged ( const QModelIndex &  topLeft,
const QModelIndex &  ,
const QVector< int > &   
)
privateslot

On MaterialItem change: updates corresponding MaterialProperty in sample items.

Definition at line 66 of file MaterialPropertyController.cpp.

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 }
#define ASSERT(condition)
Definition: Assert.h:31
The ExternalProperty class defines custom QVariant property to carry the text, color and an identifie...
QVariant variant() const
QVector< SessionItem * > relatedSampleItems()
Returns vector of SessionItems having MaterialProperty on board.
SessionItem * itemForIndex(const QModelIndex &index) const
QString materialTag(const SessionItem &item)
Returns material tag for given item. Returns empty string, if item doesn't have materials.
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

References ModelPath::ancestor(), ASSERT, SessionModel::itemForIndex(), m_materialModel, MaterialItemUtils::materialProperty(), MaterialItemUtils::materialTag(), relatedSampleItems(), and ExternalProperty::variant().

Here is the call graph for this function:

◆ onMaterialModelLoad

void MaterialPropertyController::onMaterialModelLoad ( )
slot

Special case when original MaterialModel was fully rebuild from MaterialEditor.

Full update of MaterialProperties.

Definition at line 46 of file MaterialPropertyController.cpp.

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 }
MaterialItem * materialFromIdentifier(const QString &identifier)

References ASSERT, m_materialModel, MaterialModel::materialFromIdentifier(), MaterialItemUtils::materialProperty(), MaterialItemUtils::materialTag(), relatedSampleItems(), and ExternalProperty::variant().

Referenced by ApplicationModels::readFrom(), and setModels().

Here is the call graph for this function:

◆ onMaterialRowsAboutToBeRemoved

void MaterialPropertyController::onMaterialRowsAboutToBeRemoved ( const QModelIndex &  parent,
int  first,
int  last 
)
privateslot

On MaterialItem removal: updates corresponding MaterialProperty in sample items.

Definition at line 88 of file MaterialPropertyController.cpp.

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 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const

References ASSERT, SessionModel::index(), SessionModel::itemForIndex(), m_materialModel, MaterialItemUtils::materialTag(), relatedSampleItems(), and ExternalProperty::variant().

Here is the call graph for this function:

◆ relatedSampleItems()

QVector< SessionItem * > MaterialPropertyController::relatedSampleItems ( )
private

Returns vector of SessionItems having MaterialProperty on board.

Definition at line 118 of file MaterialPropertyController.cpp.

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 }
QStringList materialRelatedModelTypes()
Returns list of model types which contains registered MaterialProperty.
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

References SessionModel::itemForIndex(), ModelUtils::iterate(), m_sampleModel, and MaterialItemUtils::materialRelatedModelTypes().

Referenced by onMaterialDataChanged(), onMaterialModelLoad(), and onMaterialRowsAboutToBeRemoved().

Here is the call graph for this function:

◆ setModels()

void MaterialPropertyController::setModels ( MaterialModel materialModel,
SampleModel sampleModel 
)

Definition at line 28 of file MaterialPropertyController.cpp.

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 }
void onMaterialModelLoad()
Special case when original MaterialModel was fully rebuild from MaterialEditor.
void modelLoaded()

References m_materialModel, m_sampleModel, SessionModel::modelLoaded(), and onMaterialModelLoad().

Referenced by ApplicationModels::ApplicationModels().

Here is the call graph for this function:

Member Data Documentation

◆ m_materialModel

MaterialModel* MaterialPropertyController::m_materialModel
private

◆ m_sampleModel

SampleModel* MaterialPropertyController::m_sampleModel
private

Definition at line 45 of file MaterialPropertyController.h.

Referenced by relatedSampleItems(), and setModels().


The documentation for this class was generated from the following files: