File: MaterialInplaceForm.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • 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 (138 lines) | stat: -rw-r--r-- 5,455 bytes parent folder | download | duplicates (3)
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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Sample/MaterialInplaceForm.cpp
//! @brief     Implements class MaterialInplaceForm.
//!
//! @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/Sample/MaterialInplaceForm.h"
#include "Base/Util/Assert.h"
#include "GUI/Model/Material/MaterialItem.h"
#include "GUI/Model/Material/MaterialsSet.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/Model/Sample/ItemWithMaterial.h"
#include "GUI/Model/Sample/LayerItem.h"
#include "GUI/Model/Sample/SampleItem.h"
#include "GUI/Model/Util/Backup.h"
#include "GUI/View/Base/LayoutUtil.h"
#include "GUI/View/Material/MaterialEditorDialog.h"
#include "GUI/View/Numeric/DSpinBox.h"
#include "GUI/View/Sample/LayerEditorUtil.h"
#include "GUI/View/Sample/SampleEditorController.h"
#include "GUI/View/Sample/SampleForm.h"
#include <QPushButton>

MaterialInplaceForm::MaterialInplaceForm(ItemWithMaterial* item, SampleEditorController* ec)
    : m_item(item)
    , m_ec(ec)
    , m_layout(new QGridLayout(this))
{
    m_layout->setContentsMargins(0, 0, 0, 0);
    createWidgets();

    connect(itemWithMaterial()->materialItem(), &MaterialItem::dataChanged, this,
            &MaterialInplaceForm::updateValues);
}
void MaterialInplaceForm::updateValues()
{
    auto* material = m_item->materialItem();
    ASSERT(material);

    for (auto* editor : findChildren<DSpinBox*>()) {
        QSignalBlocker b(editor);
        editor->updateValue();
    }
    for (auto* w : m_magnetic_widgets)
        w->setHidden(!material->isMagnetizatioEnabled());
    m_material_label->setText(material->matItemName());
}

void MaterialInplaceForm::selectMaterial()
{
    const auto materialsBackup = GUI::Util::createBackup(&m_ec->sampleItem()->materialModel());
    const QString newMaterialIdentifier =
        GUI::chooseMaterial(m_ec->sampleItem(), m_item->materialIdentifier());

    if (!newMaterialIdentifier.isEmpty() && newMaterialIdentifier != m_item->materialIdentifier()) {
        disconnect(itemWithMaterial()->materialItem(), nullptr, this, nullptr);
        GUI::Util::Layout::clearLayout(m_layout, true);
        m_ec->selectMaterial(m_item, newMaterialIdentifier);
        createWidgets();
        connect(itemWithMaterial()->materialItem(), &MaterialItem::dataChanged, this,
                &MaterialInplaceForm::updateValues, Qt::UniqueConnection);
    } else {
        updateValues(); // necessary, since in the material editor the values could have been
                        // changed without selecting a different material

        // If the list of materials was edited (e.g. a material added), but the current was not
        // changed, no modified signal would be sent. Check now for changes and emit if necessary.
        if (GUI::Util::createBackup(&m_ec->sampleItem()->materialModel()) != materialsBackup)
            gDoc->sampleChanged();
    }
}

void MaterialInplaceForm::createWidgets()
{
    m_magnetic_widgets.clear();

    auto* material = m_item->materialItem();
    ASSERT(material);

    int col = 0;
    m_material_label = new QLabel(material->matItemName());
    QFont f = m_material_label->font();
    f.setBold(true);
    m_material_label->setFont(f);
    m_layout->addWidget(m_material_label, 1, col++);

    // -- Create UI for delta/beta resp. sldRe/sldIm
    DoubleProperties values;
    if (material->hasRefractiveIndex())
        values << &material->delta() << &material->beta();
    else
        values << &material->sldRe() << &material->sldIm();

    for (DoubleProperty* d : values) {
        auto* editor = new DSpinBox(&*d);
        auto* label = new QLabel(d->label(), this);
        label->setBuddy(editor);

        QObject::connect(editor, &DSpinBox::valueChanged,
                         [this] { m_ec->setMaterialValue(m_item); });

        m_layout->addWidget(label, 0, col);
        m_layout->addWidget(editor, 1, col++);
    }

    // -- Create UI for magnetization vector
    const auto setNewValue = [this] { m_ec->setMaterialValue(m_item); };

    // Processing z-magnetization is not implemented yet (see issue #654)
    const LayerItem* layer_item = dynamic_cast<LayerItem*>(m_item);
    VectorProperty& v = material->magnetization();
    if (layer_item && !layer_item->isAmbient())
        m_magnetic_widgets.append(GUI::Util::Layer::addMultiPropertyToGrid(
            m_layout, col, {&v.x(), &v.y()}, true, false, setNewValue));

    if (!layer_item)
        m_magnetic_widgets.append(GUI::Util::Layer::addMultiPropertyToGrid(
            m_layout, col, {&v.x(), &v.y(), &v.z()}, true, false, setNewValue));

    // -- Create UI for material selection button
    auto* btn = new QPushButton("Editor", this);
    btn->setToolTip("Select material");
    m_layout->addWidget(btn, 1, m_layout->columnCount());
    connect(btn, &QPushButton::clicked, this, &MaterialInplaceForm::selectMaterial,
            Qt::UniqueConnection);

    m_layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding), 0, m_layout->columnCount());

    updateValues();
}