File: MaterialsQModel.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (224 lines) | stat: -rw-r--r-- 7,007 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Material/MaterialsQModel.cpp
//! @brief     Implements class MaterialsQModel.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/Material/MaterialsQModel.h"
#include "GUI/Model/Material/MaterialItem.h"
#include "GUI/Model/Material/MaterialsSet.h"
#include <QApplication>
#include <QFontMetrics>
#include <QPixmap>

MaterialsQModel::MaterialsQModel(MaterialsSet& model)
    : m_model(model)
{
}

int MaterialsQModel::rowCount(const QModelIndex& /*parent = QModelIndex()*/) const
{
    return m_model.size();
}

int MaterialsQModel::columnCount(const QModelIndex& /*parent = QModelIndex()*/) const
{
    return NUM_COLUMNS;
}

QVariant MaterialsQModel::headerData(int section, Qt::Orientation /*orientation*/,
                                     int role /* = Qt::DisplayRole */) const
{
    if (role != Qt::DisplayRole)
        return {};

    switch (section) {
    case NAME:
        return "Name";
    case TYPE:
        return "Type";
    case PARAMETERS:
        return "Material parameters";
    case MAGNETIZATION:
        return "Magnetization (A/m)";
    default:
        return {};
    }
}

QVariant MaterialsQModel::data(const QModelIndex& index, int role /*= Qt::DisplayRole*/) const
{
    if (!index.isValid())
        return {};

    const MaterialItem* material = m_model.at(index.row());

    if (role == Qt::DisplayRole) {
        switch (index.column()) {
        case NAME:
            return material->matItemName();

        case TYPE:
            return material->hasRefractiveIndex() ? "Refractive based" : "SLD based";

        case PARAMETERS:
            if (material->hasRefractiveIndex())
                return QString("delta: %1, beta: %2")
                    .arg(material->delta().dVal())
                    .arg(material->beta().dVal());
            else
                return QString("re: %1, im: %2")
                    .arg(material->sldRe().dVal())
                    .arg(material->sldIm().dVal());

        case MAGNETIZATION:
            return QString("%1/%2/%3")
                .arg(material->magnetization().r3().x())
                .arg(material->magnetization().r3().y())
                .arg(material->magnetization().r3().z());
        }
    } else if (role == Qt::DecorationRole) {
        switch (index.column()) {
        case NAME: {
            const int size = qApp->fontMetrics().height();
            QPixmap pixmap(size, size);
            pixmap.fill(materialItemFromIndex(index)->color());
            return pixmap;
        }
        }
    }

    return {};
}

void MaterialsQModel::setMaterialItemName(const QModelIndex& index, const QString& name)
{
    materialItemFromIndex(index)->setMatItemName(name);
    emit dataChanged(index, index);
}

void MaterialsQModel::setColor(const QModelIndex& index, const QColor& color)
{
    materialItemFromIndex(index)->setColor(color);
    emit dataChanged(index, index);
}

void MaterialsQModel::setX(const QModelIndex& index, double value)
{
    auto* material = materialItemFromIndex(index);
    R3 m = material->magnetization();
    m.setX(value);
    material->setMagnetization(m);
    const auto magIndex = this->index(index.row(), MAGNETIZATION);
    emit dataChanged(magIndex, magIndex);
}

void MaterialsQModel::setY(const QModelIndex& index, double value)
{
    auto* material = materialItemFromIndex(index);
    R3 m = material->magnetization();
    m.setY(value);
    material->setMagnetization(m);
    const auto magIndex = this->index(index.row(), MAGNETIZATION);
    emit dataChanged(magIndex, magIndex);
}

void MaterialsQModel::setZ(const QModelIndex& index, double value)
{
    auto* material = materialItemFromIndex(index);
    R3 m = material->magnetization();
    m.setZ(value);
    material->setMagnetization(m);
    const auto magIndex = this->index(index.row(), MAGNETIZATION);
    emit dataChanged(magIndex, magIndex);
}

void MaterialsQModel::setDelta(const QModelIndex& index, double value)
{
    auto* m = materialItemFromIndex(index);
    m->setRefractiveIndex(value, m->beta().dVal());
    const auto paramIndex = this->index(index.row(), PARAMETERS);
    emit dataChanged(paramIndex, paramIndex);
}

void MaterialsQModel::setBeta(const QModelIndex& index, double value)
{
    auto* m = materialItemFromIndex(index);
    m->setRefractiveIndex(m->delta().dVal(), value);
    const auto paramIndex = this->index(index.row(), PARAMETERS);
    emit dataChanged(paramIndex, paramIndex);
}

void MaterialsQModel::setRe(const QModelIndex& index, double value)
{
    auto* m = materialItemFromIndex(index);
    m->setScatteringLengthDensity(complex_t(value, m->sldIm().dVal()));
    auto paramIndex = this->index(index.row(), PARAMETERS);
    emit dataChanged(paramIndex, paramIndex);
}

void MaterialsQModel::setIm(const QModelIndex& index, double value)
{
    auto* m = materialItemFromIndex(index);
    m->setScatteringLengthDensity(complex_t(m->sldRe().dVal(), value));
    auto paramIndex = this->index(index.row(), PARAMETERS);
    emit dataChanged(paramIndex, paramIndex);
}

MaterialItem* MaterialsQModel::materialItemFromIndex(const QModelIndex& index) const
{
    return index.isValid() ? m_model.at(index.row()) : nullptr;
}

QModelIndex MaterialsQModel::indexFromMaterial(const QString& identifier) const
{
    for (size_t row = 0; row < m_model.size(); row++)
        if (m_model[row]->identifier() == identifier)
            return index(row, 0);
    return {};
}

QModelIndex MaterialsQModel::indexFromMaterial(const MaterialItem* m) const
{
    for (size_t row = 0; row < m_model.size(); row++)
        if (m_model[row] == m)
            return index(row, 0);
    return {};
}

QModelIndex MaterialsQModel::first() const
{
    return index(0, 0);
}

MaterialItem* MaterialsQModel::addRefractiveMaterialItem(const QString& name, double delta,
                                                         double beta)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    auto* m = m_model.addRefractiveMaterialItem(name, delta, beta);
    endInsertRows();
    return m;
}

MaterialItem* MaterialsQModel::addSLDMaterialItem(const QString& name, double sld, double abs_term)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    auto* m = m_model.addSLDMaterialItem(name, sld, abs_term);
    endInsertRows();
    return m;
}

void MaterialsQModel::removeMaterial(const QModelIndex& index)
{
    beginRemoveRows(QModelIndex(), index.row(), index.row());
    m_model.removeMaterialItem(materialItemFromIndex(index));
    endRemoveRows();
}