Newer
Older
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/Sample/SampleEditorController.cpp
//! @brief Implements class SampleEditorController.
//!
//! @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/SampleEditorController.h"

Wuttke, Joachim
committed
#include "GUI/Model/Material/MaterialsSet.h"
#include "GUI/Model/Sample/CompoundItem.h"
#include "GUI/Model/Sample/CoreAndShellItem.h"
#include "GUI/Model/Sample/InterferenceItems.h"
#include "GUI/Model/Sample/LayerItem.h"
#include "GUI/Model/Sample/ParticleItem.h"
#include "GUI/Model/Sample/ParticleLayoutItem.h"
#include "GUI/Model/Sample/SampleItem.h"
#include "GUI/View/Sample/CompoundForm.h"
#include "GUI/View/Sample/CoreAndShellForm.h"
#include "GUI/View/Sample/InterferenceForm.h"
#include "GUI/View/Sample/LatticeTypeSelectionForm.h"
#include "GUI/View/Sample/LayerForm.h"
#include "GUI/View/Sample/MaterialInplaceForm.h"
#include "GUI/View/Sample/MesocrystalForm.h"
#include "GUI/View/Sample/ParticleLayoutForm.h"
#include "GUI/View/Sample/SampleForm.h"
SampleEditorController::SampleEditorController(SampleItem* multi)
: m_sample_item(multi)
, m_sample_form(nullptr)
{
}
void SampleEditorController::setSampleForm(SampleForm* view)
void SampleEditorController::addLayerItem(LayerItem* before)
const int i = (before != nullptr) ? m_sample_item->layerItems().indexOf(before)
: m_sample_item->layerItems().size();
QColor color = findColor(i); // before adding layer
LayerItem* layer = m_sample_item->createLayerItemAt(i);
layer->setMaterial(materialModel()->defaultMaterialItem());
layer->setColor(color);
layer->expandLayer = true; // manually added layer => expanded
QColor SampleEditorController::findColor(int atIndex)
auto unusedColors = GUI::Util::Layer::predefinedLayerColors();
for (auto* l : m_sample_item->layerItems())
unusedColors.removeAll(l->color());
if (!unusedColors.isEmpty())
else {
// search for a color which has been used the less, and which is not the same as in the
// layers above and below
QMap<QString, int> usage;
for (auto* l : m_sample_item->layerItems())
usage[l->color().name()] += 1;
auto sortedByUsage = GUI::Util::Layer::predefinedLayerColors();
std::stable_sort(
sortedByUsage.begin(), sortedByUsage.end(),
[&](const QColor& a, const QColor& b) { return usage[a.name()] < usage[b.name()]; });
(atIndex > 0) ? m_sample_item->layerItems()[atIndex - 1]->color() : QColor();
const QColor below = (atIndex < m_sample_item->layerItems().size())
? m_sample_item->layerItems()[atIndex]->color()

Wuttke, Joachim
committed
for (const auto& col : sortedByUsage)
if (col != above && col != below) {
void SampleEditorController::onLayerAdded(LayerItem* layer)
{
ASSERT(m_sample_form);
m_sample_form->onLayerAdded(layer);
// expand the new layer's form for better workflow
for (auto* c : m_sample_form->findChildren<LayerForm*>())
if (c->layerItem() == layer)
c->expand();
void SampleEditorController::duplicateLayerItem(const LayerItem* layer)
int atIndex = m_sample_item->layerItems().indexOf(layer) + 1;
QColor color = findColor(atIndex); // before adding layer
LayerItem* newLayer = m_sample_item->createLayerItemAt(atIndex);
GUI::Util::copyContents(layer, newLayer);
newLayer->setColor(color);
void SampleEditorController::removeLayerItem(LayerItem* layer)
emit aboutToRemoveItem(layer);
m_sample_form->onAboutToRemoveLayer(layer);
m_sample_item->removeLayer(layer);
m_sample_form->updateRowVisibilities();
void SampleEditorController::onLayoutAdded(LayerForm* layerForm, ParticleLayoutItem* layout)
for (auto* layoutForms : layerForm->findChildren<ParticleLayoutForm*>())
layoutForms->updateTitle(layerForm->layerItem());
emit modified();
}
void SampleEditorController::addLayoutItem(LayerForm* layerForm)
{
auto* newLayout = layerForm->layerItem()->addLayoutItem();
onLayoutAdded(layerForm, newLayout);
}
void SampleEditorController::duplicateLayoutItem(LayerForm* layerForm, ParticleLayoutItem* layout)
auto* newLayout = layerForm->layerItem()->addLayoutItem();
GUI::Util::copyContents(layout, newLayout);
onLayoutAdded(layerForm, newLayout);
void SampleEditorController::removeLayoutItem(LayerForm* layerForm, ParticleLayoutItem* layout)
emit aboutToRemoveItem(layout);
layerForm->onAboutToRemoveLayout(layout);
layerForm->layerItem()->removeLayoutItem(layout);
Matthias Puchner
committed
for (auto* layoutForm : layerForm->findChildren<ParticleLayoutForm*>())
layoutForm->updateTitle(layerForm->layerItem());
void SampleEditorController::onParticleLayoutAdded(ParticleLayoutItem* layout,
emit modified();
// search for particle layout widget for notification
ASSERT(m_sample_form);
for (auto* w : m_sample_form->findChildren<ParticleLayoutForm*>())
void SampleEditorController::addParticleLayoutItem(ParticleLayoutItem* layout,
FormFactorItemCatalog::Type formFactorType)
{
auto* newParticle = createAndInitItem(formFactorType);
layout->addItemWithParticleSelection(newParticle);
onParticleLayoutAdded(layout, newParticle);
void SampleEditorController::addParticleLayoutItem(ParticleLayoutItem* layout,
ItemWithParticlesCatalog::Type type)
layout->addItemWithParticleSelection(newItem);
onParticleLayoutAdded(layout, newItem);
}
void SampleEditorController::duplicateItemWithParticles(ItemWithParticles* item)
{
auto type = ItemWithParticlesCatalog::type(item);
auto* newItem = createAndInitItem(type);
GUI::Util::copyContents(item, newItem);
if (ParticleLayoutItem* parent_layout = parentLayoutItem(item)) {
parent_layout->addItemWithParticleSelection(newItem);
onParticleLayoutAdded(parent_layout, newItem);
} else if (CompoundItem* parent_compound = parentCompoundItem(item)) {
parent_compound->addItemWithParticleSelection(newItem);
onParticleCompoundAdded(parent_compound, newItem);
} else
void SampleEditorController::onParticleCompoundAdded(CompoundItem* composition,
// search for composition widget for notification
ASSERT(m_sample_form);
for (auto* c : m_sample_form->findChildren<CompoundForm*>())
void SampleEditorController::addCompoundItem(CompoundItem* compositionItem,
ItemWithParticlesCatalog::Type type)
compositionItem->addItemWithParticleSelection(newItem);
onParticleCompoundAdded(compositionItem, newItem);
void SampleEditorController::addCompoundItem(CompoundItem* compositionItem,
FormFactorItemCatalog::Type formFactorType)
compositionItem->addItemWithParticleSelection(newParticle);
onParticleCompoundAdded(compositionItem, newParticle);
Matthias Puchner
committed
ItemWithParticles*
SampleEditorController::createAndInitItem(FormFactorItemCatalog::Type formFactorType) const
Matthias Puchner
committed
{
Matthias Puchner
committed
newParticle->setFormFactor(FormFactorItemCatalog::create(formFactorType));
newParticle->setMaterial(materialModel()->defaultParticleMaterialItem());
Matthias Puchner
committed
return newParticle;
}
ItemWithParticles*
SampleEditorController::createAndInitItem(ItemWithParticlesCatalog::Type type) const
Matthias Puchner
committed
{
auto* newItem = ItemWithParticlesCatalog::create(type, materialModel());
Matthias Puchner
committed
if (auto* p = dynamic_cast<ItemWithMaterial*>(newItem))
Matthias Puchner
committed
if (auto* cs = dynamic_cast<CoreAndShellItem*>(newItem)) {
cs->createCoreItem(materialModel());
cs->createShellItem(materialModel());
cs->coreItem()->setFormFactor(new CylinderItem());
cs->shellItem()->setFormFactor(new CylinderItem());
Matthias Puchner
committed
}
if (auto* meso = dynamic_cast<MesocrystalItem*>(newItem); meso && meso->basisItem())
if (auto* p = dynamic_cast<ItemWithMaterial*>(meso->basisItem()))
Matthias Puchner
committed
return newItem;
}
void SampleEditorController::setCoreFormFactor(CoreAndShellForm* widget,
FormFactorItemCatalog::Type type)
{
auto* particleCoreShell = widget->coreShellItem();
particleCoreShell->coreItem()->setFormFactor(FormFactorItemCatalog::create(type));
void SampleEditorController::setShellFormFactor(CoreAndShellForm* widget,
FormFactorItemCatalog::Type type)
{
auto* particleCoreShell = widget->coreShellItem();
if (particleCoreShell->shellItem() == nullptr)
particleCoreShell->shellItem()->setFormFactor(FormFactorItemCatalog::create(type));
ParticleLayoutItem* SampleEditorController::parentLayoutItem(ItemWithParticles* item)
for (auto* layoutForm : m_sample_form->findChildren<ParticleLayoutForm*>())
if (layoutForm->layoutItem()->itemsWithParticles().contains(item))
return layoutForm->layoutItem();
return nullptr;
}
CompoundItem* SampleEditorController::parentCompoundItem(ItemWithParticles* item)
for (auto* compoundForm : m_sample_form->findChildren<CompoundForm*>())
if (compoundForm->compositionItem()->itemsWithParticles().contains(item))
return compoundForm->compositionItem();
void SampleEditorController::removeParticle(ItemWithParticles* itemToRemove)
for (auto* layoutForm : m_sample_form->findChildren<ParticleLayoutForm*>())
if (layoutForm->layoutItem()->itemsWithParticles().contains(itemToRemove)) {
layoutForm->onAboutToRemoveParticle(itemToRemove);
emit aboutToRemoveItem(itemToRemove);
layoutForm->layoutItem()->removeItemWithParticle(itemToRemove);
emit modified();
return;
}
for (auto* c : m_sample_form->findChildren<CompoundForm*>())
if (c->compositionItem()->itemsWithParticles().contains(itemToRemove)) {
c->onAboutToRemoveParticle(itemToRemove);
emit aboutToRemoveItem(itemToRemove);
c->compositionItem()->removeItemWithParticle(itemToRemove);
emit modified();
return;
}
void SampleEditorController::setDouble(double value, DoubleProperty& d)

Wuttke, Joachim
committed
MaterialsSet* SampleEditorController::materialModel() const
return &m_sample_item->materialModel();
void SampleEditorController::selectMaterial(ItemWithMaterial* item,
const QString& newMaterialIdentifier)
{
item->setMaterial(newMaterialIdentifier);
// update Layer title
ASSERT(m_sample_form);
for (auto* c : m_sample_form->findChildren<LayerForm*>())
if (c->layerItem() == item)
c->updateTitle();
// #baLayerEditor notify all material users (update link info)
void SampleEditorController::setMaterialValue(ItemWithMaterial* item, double value,
Matthias Puchner
committed
// -- notify all other users of this material (update values in the UI)
ASSERT(m_sample_form);
for (auto* c : m_sample_form->findChildren<MaterialInplaceForm*>())
Matthias Puchner
committed
if (c->itemWithMaterial() != item
&& c->itemWithMaterial()->materialIdentifier() == item->materialIdentifier())
c->updateValues();
void SampleEditorController::setDensityRelatedValue(InterferenceItem* interferenceItem,
// -- notify the containing particle layout UI about changed value
ASSERT(m_sample_form);
for (auto* c : m_sample_form->findChildren<ParticleLayoutForm*>())
if (c->layoutItem()->interferenceSelection().currentItem() == interferenceItem) {
c->updateDensityValue();
break;
}
void SampleEditorController::onStoppedToMoveLayer(QWidget* widgetToMove,
QWidget* moveAboveThisWidget)
{
auto* itemToMove = dynamic_cast<LayerForm*>(widgetToMove)->layerItem();
const auto* moveAboveThisLayerForm = m_sample_form->findNextLayerForm(moveAboveThisWidget);
moveAboveThisLayerForm != nullptr ? moveAboveThisLayerForm->layerItem() : nullptr;
m_sample_item->moveLayer(itemToMove, moveAboveThisItem);
m_sample_form->onLayerMoved(itemToMove);
// #baLayerEditor: tab order!
void SampleEditorController::setSampleName(const QString& name)
{
emit modified();
}
void SampleEditorController::setSampleDescription(const QString& description)
{
m_sample_item->setDescription(description);
emit modified();
}
void SampleEditorController::setMesocrystalBasis(MesocrystalForm* widget,
ItemWithParticlesCatalog::Type type)
widget->createBasisWidgets();
emit modified();
}
void SampleEditorController::setMesocrystalBasis(MesocrystalForm* widget,
FormFactorItemCatalog::Type type)
{
void SampleEditorController::selectInterference(InterferenceForm* widget, int newIndex)
widget->layoutItem()->interferenceSelection().setCurrentIndex(newIndex);
Matthias Puchner
committed
// Disable/enable total density property in the particle layout, depending on type of
// interference function.
QWidget* parent = widget->parentWidget();
while (parent != nullptr && dynamic_cast<ParticleLayoutForm*>(parent) == nullptr)
parent = parent->parentWidget();
if (auto* particleLayoutForm = dynamic_cast<ParticleLayoutForm*>(parent)) {
Matthias Puchner
committed
particleLayoutForm->updateDensityEnabling();
particleLayoutForm->updateDensityValue();
void SampleEditorController::setIntegrateOverXi(LatticeTypeSelectionForm* widget, bool value)
widget->onIntegrateOverXiChanged();