File: LayerItem.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 (144 lines) | stat: -rw-r--r-- 5,090 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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Sample/LayerItem.cpp
//! @brief     Implements class LayerItem.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/Model/Sample/LayerItem.h"
#include "Base/Util/Assert.h"
#include "Base/Util/Vec.h"
#include "GUI/Model/Sample/CompoundItem.h"
#include "GUI/Model/Sample/CoreAndShellItem.h"
#include "GUI/Model/Sample/MesocrystalItem.h"
#include "GUI/Model/Sample/ParticleItem.h"
#include "GUI/Model/Sample/ParticleLayoutItem.h"
#include "GUI/Model/Sample/SampleItem.h"
#include "GUI/Model/Util/UtilXML.h"

namespace {
namespace Tag {

const QString BaseLayerData("BaseLayerData");
const QString MaterialData("MaterialData");
const QString Name("Name");
const QString NumSlices("NumSlices");
const QString Thickness("Thickness");
const QString Roughness("Roughness");
const QString Layout("Layout");
const QString ExpandRoughnessGroupbox("ExpandRoughnessGroupbox");

} // namespace Tag

std::vector<ItemWithMaterial*> layoutItemsWithMaterial(ParticleLayoutItem* layout)
{
    std::vector<ItemWithMaterial*> result;

    std::vector<ItemWithParticles*> itemsWithParticles = layout->itemsWithParticles();
    while (!itemsWithParticles.empty()) {
        auto* item = itemsWithParticles.front();
        itemsWithParticles.erase(itemsWithParticles.begin());
        if (!item)
            continue;

        if (auto* p = dynamic_cast<CompoundItem*>(item))
            Vec::concat(itemsWithParticles, p->itemsWithParticles());
        else if (auto* p = dynamic_cast<MesocrystalItem*>(item))
            itemsWithParticles.push_back(p->basisItem());
        else if (auto* p = dynamic_cast<ParticleItem*>(item))
            result.push_back(p);
        else if (auto* p = dynamic_cast<CoreAndShellItem*>(item)) {
            if (p->coreItem())
                result.push_back(p->coreItem());
            if (p->shellItem())
                result.push_back(p->shellItem());
        } else
            ASSERT_NEVER;
    }
    return result;
}

} // namespace

LayerItem::LayerItem(const MaterialsSet* materials)
    : ItemWithMaterial(materials)
{
    m_thickness.init("Thickness", "nm", "Thickness of the layer", 0.0, 3,
                     RealLimits::lowerLimited(0.0), "thickness");

    m_roughness.simpleInit("Top roughness", "Roughness of top interface",
                           RoughnessCatalog::Type::SelfAffineFractal);
}

LayerItem::~LayerItem() = default;

std::vector<ItemWithMaterial*> LayerItem::itemsWithMaterial()
{
    std::vector<ItemWithMaterial*> result;
    result.push_back(this);
    for (ParticleLayoutItem* layout : layoutItems())
        Vec::concat(result, layoutItemsWithMaterial(layout));
    return result;
}

std::vector<ItemWithParticles*> LayerItem::itemsWithParticles() const
{
    std::vector<ItemWithParticles*> result;
    for (auto* layout : layoutItems())
        Vec::concat(result, layout->containedItemsWithParticles());
    return result;
}

ParticleLayoutItem* LayerItem::addLayoutItem()
{
    m_layouts.push_back(new ParticleLayoutItem(m_materials));
    return m_layouts.back();
}

void LayerItem::removeLayoutItem(ParticleLayoutItem* layout)
{
    m_layouts.delete_element(layout);
}

void LayerItem::writeTo(QXmlStreamWriter* w) const
{
    XML::writeBaseElement<ItemWithMaterial>(w, Tag::MaterialData, this);
    XML::writeBaseElement<ItemWithLayers>(w, Tag::BaseLayerData, this);
    XML::writeTaggedValue(w, Tag::NumSlices, m_num_slices);
    m_thickness.writeTo2(w, Tag::Thickness);
    XML::writeTaggedElement(w, Tag::Roughness, m_roughness);
    for (const auto* layout : m_layouts)
        XML::writeTaggedElement(w, Tag::Layout, *layout);
    XML::writeTaggedValue(w, Tag::ExpandRoughnessGroupbox, expandRoughness);
}

void LayerItem::readFrom(QXmlStreamReader* r)
{
    m_layouts.clear();
    while (r->readNextStartElement()) {
        QString tag = r->name().toString();
        if (tag == Tag::MaterialData)
            XML::readBaseElement<ItemWithMaterial>(r, tag, this);
        else if (tag == Tag::BaseLayerData)
            XML::readBaseElement<ItemWithLayers>(r, tag, this);
        else if (tag == Tag::NumSlices)
            m_num_slices = XML::readTaggedUInt(r, tag);
        else if (tag == Tag::Thickness)
            m_thickness.readFrom2(r, tag);
        else if (tag == Tag::Roughness)
            XML::readTaggedElement(r, tag, m_roughness);
        else if (tag == Tag::Layout)
            XML::readTaggedElement(r, tag, *addLayoutItem());
        else if (tag == Tag::ExpandRoughnessGroupbox)
            expandRoughness = XML::readTaggedBool(r, tag);
        else
            r->skipCurrentElement();
    }
}