BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
MaterialEditorModel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/MaterialEditor/MaterialEditorModel.cpp
6 //! @brief Implements class MaterialEditorModel
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
19 #include <QApplication>
20 #include <QFontMetrics>
21 #include <QPixmap>
22 
24  : m_model(p)
25 {
26 }
27 
28 int MaterialEditorModel::rowCount(const QModelIndex& /*parent = QModelIndex()*/) const
29 {
30  return m_model->materialItems().size();
31 }
32 
33 int MaterialEditorModel::columnCount(const QModelIndex& /*parent = QModelIndex()*/) const
34 {
35  return NUM_COLUMNS;
36 }
37 
38 QVariant MaterialEditorModel::headerData(int section, Qt::Orientation /*orientation*/,
39  int role /* = Qt::DisplayRole */) const
40 {
41  if (role != Qt::DisplayRole)
42  return {};
43 
44  switch (section) {
45  case NAME:
46  return "Name";
47  case TYPE:
48  return "Type";
49  case PARAMETERS:
50  return "Material parameters";
51  case MAGNETIZATION:
52  return "Magnetization [A/m]";
53  default:
54  return {};
55  }
56 }
57 
58 QVariant MaterialEditorModel::data(const QModelIndex& index, int role /*= Qt::DisplayRole*/) const
59 {
60  if (!index.isValid())
61  return {};
62 
63  MaterialItem* material = m_model->materialItems()[index.row()];
64 
65  if (role == Qt::DisplayRole) {
66  switch (index.column()) {
67  case NAME:
68  return material->matItemName();
69 
70  case TYPE:
71  return material->hasRefractiveIndex() ? "Refractive based" : "SLD based";
72 
73  case PARAMETERS:
74  if (material->hasRefractiveIndex())
75  return QString("delta: %1, beta: %2").arg(material->delta()).arg(material->beta());
76  else
77  return QString("re: %1, im: %2").arg(material->sldRe()).arg(material->sldIm());
78 
79  case MAGNETIZATION:
80  return QString("%1/%2/%3")
81  .arg(material->magnetization().x())
82  .arg(material->magnetization().y())
83  .arg(material->magnetization().z());
84  }
85  } else if (role == Qt::DecorationRole) {
86  switch (index.column()) {
87  case NAME: {
88  const int size = qApp->fontMetrics().height();
89  QPixmap pixmap(size, size);
90  pixmap.fill(materialFromIndex(index)->color());
91  return pixmap;
92  }
93  }
94  }
95 
96  return {};
97 }
98 
99 void MaterialEditorModel::setMaterialItemName(const QModelIndex& index, const QString& name)
100 {
102  emit dataChanged(index, index);
103 }
104 
105 void MaterialEditorModel::setColor(const QModelIndex& index, const QColor& color)
106 {
107  materialFromIndex(index)->setColor(color);
108  emit dataChanged(index, index);
109 }
110 
111 void MaterialEditorModel::setX(const QModelIndex& index, double value)
112 {
113  auto* material = materialFromIndex(index);
114  R3 m = material->magnetization();
115  m.setX(value);
116  material->setMagnetization(m);
117  const auto magIndex = this->index(index.row(), MAGNETIZATION);
118  emit dataChanged(magIndex, magIndex);
119 }
120 
121 void MaterialEditorModel::setY(const QModelIndex& index, double value)
122 {
123  auto* material = materialFromIndex(index);
124  R3 m = material->magnetization();
125  m.setY(value);
126  material->setMagnetization(m);
127  const auto magIndex = this->index(index.row(), MAGNETIZATION);
128  emit dataChanged(magIndex, magIndex);
129 }
130 
131 void MaterialEditorModel::setZ(const QModelIndex& index, double value)
132 {
133  auto* material = materialFromIndex(index);
134  R3 m = material->magnetization();
135  m.setZ(value);
136  material->setMagnetization(m);
137  const auto magIndex = this->index(index.row(), MAGNETIZATION);
138  emit dataChanged(magIndex, magIndex);
139 }
140 
141 void MaterialEditorModel::setDelta(const QModelIndex& index, double value)
142 {
143  auto* m = materialFromIndex(index);
144  m->setRefractiveIndex(value, m->beta());
145  const auto paramIndex = this->index(index.row(), PARAMETERS);
146  emit dataChanged(paramIndex, paramIndex);
147 }
148 
149 void MaterialEditorModel::setBeta(const QModelIndex& index, double value)
150 {
151  auto* m = materialFromIndex(index);
152  m->setRefractiveIndex(m->delta(), value);
153  const auto paramIndex = this->index(index.row(), PARAMETERS);
154  emit dataChanged(paramIndex, paramIndex);
155 }
156 
157 void MaterialEditorModel::setRe(const QModelIndex& index, double value)
158 {
159  auto* m = materialFromIndex(index);
160  m->setScatteringLengthDensity(complex_t(value, m->sldIm()));
161  auto paramIndex = this->index(index.row(), PARAMETERS);
162  emit dataChanged(paramIndex, paramIndex);
163 }
164 
165 void MaterialEditorModel::setIm(const QModelIndex& index, double value)
166 {
167  auto* m = materialFromIndex(index);
168  m->setScatteringLengthDensity(complex_t(m->sldRe(), value));
169  auto paramIndex = this->index(index.row(), PARAMETERS);
170  emit dataChanged(paramIndex, paramIndex);
171 }
172 
173 MaterialItem* MaterialEditorModel::materialFromIndex(const QModelIndex& index) const
174 {
175  return index.isValid() ? m_model->materialItems()[index.row()] : nullptr;
176 }
177 
178 QModelIndex MaterialEditorModel::indexFromMaterial(const QString& identifier) const
179 {
180  const auto materials = m_model->materialItems();
181  for (int row = 0; row < materials.size(); row++)
182  if (materials[row]->identifier() == identifier)
183  return index(row, 0);
184  return QModelIndex();
185 }
186 
188 {
189  const auto materials = m_model->materialItems();
190  for (int row = 0; row < materials.size(); row++)
191  if (materials[row] == m)
192  return index(row, 0);
193  return QModelIndex();
194 }
195 
196 QModelIndex MaterialEditorModel::first() const
197 {
198  return index(0, 0);
199 }
200 
202  double beta)
203 {
204  beginInsertRows(QModelIndex(), rowCount(), rowCount());
205  auto* m = m_model->addRefractiveMaterial(name, delta, beta);
206  endInsertRows();
207  return m;
208 }
209 
210 MaterialItem* MaterialEditorModel::addSLDMaterial(const QString& name, double sld, double abs_term)
211 {
212  beginInsertRows(QModelIndex(), rowCount(), rowCount());
213  auto* m = m_model->addSLDMaterial(name, sld, abs_term);
214  endInsertRows();
215  return m;
216 }
217 
219 {
220  beginInsertRows(QModelIndex(), rowCount(), rowCount());
221  auto* m = m_model->insertCopy(*materialFromIndex(index));
222  endInsertRows();
223  return m;
224 }
225 
226 void MaterialEditorModel::removeMaterial(const QModelIndex& index)
227 {
228  beginRemoveRows(QModelIndex(), index.row(), index.row());
230  endRemoveRows();
231 }
Defines class DoubleDescriptor.
Defines class MaterialEditorModel.
Defines class MaterialItem.
Defines class MaterialItems.
MaterialItem * materialFromIndex(const QModelIndex &index) const
void setDelta(const QModelIndex &index, double value)
QModelIndex indexFromMaterial(const MaterialItem *m) const
MaterialItem * addRefractiveMaterial(const QString &name, double delta, double beta)
void setY(const QModelIndex &index, double value)
void setColor(const QModelIndex &index, const QColor &color)
void setRe(const QModelIndex &index, double value)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
void setIm(const QModelIndex &index, double value)
MaterialItem * addSLDMaterial(const QString &name, double sld, double abs_term)
void setZ(const QModelIndex &index, double value)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
void setX(const QModelIndex &index, double value)
MaterialItem * cloneMaterial(const QModelIndex &index)
MaterialEditorModel(MaterialItems *p)
void removeMaterial(const QModelIndex &index)
void setMaterialItemName(const QModelIndex &index, const QString &name)
QModelIndex first() const
MaterialItems * m_model
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void setBeta(const QModelIndex &index, double value)
bool hasRefractiveIndex() const
DoubleDescriptor sldIm()
DoubleDescriptor beta()
QString matItemName() const
R3 magnetization() const
DoubleDescriptor sldRe()
void setMatItemName(const QString &name)
DoubleDescriptor delta()
void setColor(const QColor &color)
MaterialItem * addRefractiveMaterial(const QString &name, double delta, double beta)
MaterialItem * addSLDMaterial(const QString &name, double sld, double abs_term)
void removeMaterial(const QString &identifier)
MaterialItem * insertCopy(const MaterialItem &material)
Inserts a copy of the given material and returns the newly inserted item.
const QVector< MaterialItem * > & materialItems() const
QString const & name(EShape k)
Definition: particles.cpp:20