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
|
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/Model/Sample/LayerStackItem.cpp
//! @brief Implements class LayerStackItem.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2024
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "GUI/Model/Sample/LayerStackItem.h"
#include "Base/Util/Vec.h"
#include "GUI/Model/Material/MaterialsSet.h"
#include "GUI/Model/Sample/LayerItem.h"
#include "GUI/Model/Util/UtilXML.h"
namespace {
namespace Tag {
const QString BaseData("BaseData");
const QString Component("Component");
const QString NumPeriods("NumPeriods");
} // namespace Tag
} // namespace
LayerStackItem::LayerStackItem(const MaterialsSet* materials, uint n_periods)
: m_n_periods(n_periods)
, m_materials(materials)
{
ASSERT(materials);
m_color = QColor(Qt::white);
}
std::vector<LayerItem*> LayerStackItem::uniqueLayerItems() const
{
std::vector<LayerItem*> result;
for (const auto& component : m_components) {
if (auto* layerItem = dynamic_cast<LayerItem*>(component->certainItem()))
result.push_back(layerItem);
else if (auto* layerStackItem = dynamic_cast<LayerStackItem*>(component->certainItem())) {
std::vector<LayerItem*> sublayers = layerStackItem->uniqueLayerItems();
result.insert(result.end(), sublayers.begin(), sublayers.end());
} else
ASSERT_NEVER;
}
return result;
}
std::vector<LayerItem*> LayerStackItem::unwrappedLayerItems() const
{
std::vector<LayerItem*> unwrapped;
for (const auto& component : m_components) {
if (auto* layerItem = dynamic_cast<LayerItem*>(component->certainItem()))
unwrapped.push_back(layerItem);
else if (auto* layerStackItem = dynamic_cast<LayerStackItem*>(component->certainItem())) {
std::vector<LayerItem*> sublayers = layerStackItem->unwrappedLayerItems();
unwrapped.insert(unwrapped.end(), sublayers.begin(), sublayers.end());
} else
ASSERT_NEVER;
}
std::vector<LayerItem*> result;
for (size_t i = 0; i < m_n_periods; i++)
result.insert(result.end(), unwrapped.begin(), unwrapped.end());
return result;
}
std::vector<ItemWithLayers*> LayerStackItem::componentItems() const
{
std::vector<ItemWithLayers*> result;
for (const auto& component : m_components)
result.push_back(component->certainItem());
return result;
}
int LayerStackItem::indexOfComponent(const ItemWithLayers* item) const
{
const std::vector<ItemWithLayers*> component_items = componentItems();
return Vec::indexOfPtr(item, component_items);
}
LayerStackItem* LayerStackItem::parentOfComponent(const ItemWithLayers* searchedItem)
{
for (auto* component : componentItems()) {
if (component == searchedItem)
return this;
if (auto* stack = dynamic_cast<LayerStackItem*>(component)) {
auto* subparent = stack->parentOfComponent(searchedItem);
if (subparent)
return subparent;
}
}
return nullptr;
}
ItemWithLayers* LayerStackItem::addNewItemAt(ItemWithLayers* item, int index)
{
ASSERT(item);
if (index < 0)
index = m_components.size();
auto new_item =
new PolyPtrWithContext<ItemWithLayers, LayeredComponentCatalog, MaterialsSet>(m_materials);
new_item->setCertainItem(item); // gets ownership
m_components.insert_at(index, new_item);
return m_components[index]->certainItem();
}
LayerItem* LayerStackItem::createLayerItemAt(int index)
{
ItemWithLayers* new_item = addNewItemAt(new LayerItem(m_materials), index);
auto* layer = dynamic_cast<LayerItem*>(new_item);
ASSERT(layer);
return layer;
}
LayerStackItem* LayerStackItem::createLayerStackItemAt(int index)
{
ItemWithLayers* new_item = addNewItemAt(new LayerStackItem(m_materials), index);
auto* stack = dynamic_cast<LayerStackItem*>(new_item);
ASSERT(stack);
return stack;
}
void LayerStackItem::removeComponent(const ItemWithLayers* component)
{
ASSERT(component);
int index = indexOfComponent(component);
ASSERT(index >= 0)
m_components.delete_at(index);
}
void LayerStackItem::moveComponent(ItemWithLayers* component, ItemWithLayers* aboveThisComponent)
{
if (component == aboveThisComponent)
return;
int currentIndex = indexOfComponent(component);
ASSERT(currentIndex >= 0);
int abothThisIndex = indexOfComponent(aboveThisComponent);
if (abothThisIndex < 0)
abothThisIndex = m_components.size();
ASSERT(currentIndex != abothThisIndex);
auto b = m_components.begin();
if (currentIndex < abothThisIndex)
std::rotate(b + currentIndex, b + currentIndex + 1, b + abothThisIndex);
else
std::rotate(b + abothThisIndex, b + currentIndex, b + currentIndex + 1);
}
void LayerStackItem::writeTo(QXmlStreamWriter* w) const
{
XML::writeBaseElement<ItemWithLayers>(w, Tag::BaseData, this);
XML::writeTaggedValue(w, Tag::NumPeriods, m_n_periods);
for (const auto* component : m_components)
XML::writeTaggedElement(w, Tag::Component, *component);
}
void LayerStackItem::readFrom(QXmlStreamReader* r)
{
m_components.clear();
while (r->readNextStartElement()) {
QString tag = r->name().toString();
if (tag == Tag::BaseData)
XML::readBaseElement<ItemWithLayers>(r, tag, this);
else if (tag == Tag::NumPeriods)
m_n_periods = XML::readTaggedUInt(r, tag);
else if (tag == Tag::Component) {
m_components.push_back(
new PolyPtrWithContext<ItemWithLayers, LayeredComponentCatalog, MaterialsSet>(
m_materials));
m_components.back()->readFrom(r, m_materials);
XML::gotoEndElementOfTag(r, tag);
} else
r->skipCurrentElement();
}
}
|