Newer
Older
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/Sample/LayerForm.cpp
//!
//! @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/LayerForm.h"
#include "GUI/Model/Material/MaterialItem.h"

Wuttke, Joachim
committed
#include "GUI/Model/Material/MaterialsSet.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/Model/Sample/SampleItem.h"
#include "GUI/Model/Type/PredefinedColors.h"
#include "GUI/View/Base/ActionFactory.h"
#include "GUI/View/Sample/HeinzFormLayout.h"
#include "GUI/View/Sample/MaterialInplaceForm.h"
#include "GUI/View/Sample/ParticleLayoutForm.h"
#include "GUI/View/Widget/WidgetMoverButton.h"
#include <QLineEdit>
#include <QMenu>
#include <QPushButton>
LayerForm::LayerForm(QWidget* parent, LayerItem* layerItem, SampleEditorController* ec)
: CollapsibleGroupBox(parent, layerItem->expandGroupbox)
, m_layout(new HeinzFormLayout(ec))
// choose color
{
auto* chooseColorAction = new QAction(this);
chooseColorAction->setText("Choose color");
chooseColorAction->setIcon(QIcon(":/images/palette.svg"));
chooseColorAction->setIconText("Choose color");
chooseColorAction->setToolTip("Choose a color for this layer");
auto* menu = new QMenu(this);
chooseColorAction->setMenu(menu);
for (const auto& col : GUI::Colors::layerDefaults()) {
QPixmap p(64, 64);
p.fill(col);
auto* ca = menu->addAction(QIcon(p), "");

Wuttke, Joachim
committed
connect(ca, &QAction::triggered, [this, layerItem, col] {
m_move_button = new WidgetMoverButton(this, this, 1);
m_move_button->setToolTip("Move layer up/down");
connect(m_move_button, &WidgetMoverButton::finishedMoving, ec,
&SampleEditorController::onStoppedToMoveLayer);
addTitleWidget(m_move_button);
m_structure_editing_widgets << m_move_button;
}
// show in real space
{
auto* showInRealspaceAction = ActionFactory::createShowInRealspaceAction(
this, "layer", [ec, layerItem] { ec->requestViewInRealspace(layerItem); });
m_duplicate_action = ActionFactory::createDuplicateAction(
this, "layer", [ec, layerItem] { ec->duplicateLayerItem(layerItem); });
m_remove_action = ActionFactory::createRemoveAction(
this, "layer", [ec, layerItem] { ec->removeLayerItem(layerItem); });
QColor bckgroundCol = m_layer->color();
setStyleSheet("LayerForm {background-color: " + bckgroundCol.name(QColor::HexRgb) + "}");
m_layout->addBoldRow("Material:", new MaterialInplaceForm(layerItem, ec));
m_layout->addValue(m_layer->thickness());
m_thickness_row = m_layout->rowCount() - 1;
m_layout->addBoldRow("Number of slices:", GUI::Util::createIntSpinBox(
[this] { return m_layer->numSlices(); },
[this](int v) {
m_layer->setNumSlices(v);
emit gDoc->sampleChanged();
},
RealLimits::lowerLimited(1),
"Number of horizontal slices.\n"
"Used for Average Layer Material calculations \n"
"when corresponding simulation option is set."));
m_roughnessForm =
new RoughnessForm(this, m_layer->roughnessSelection(), m_layer->expandRoughness, m_ec);
m_layout->addRow(m_roughnessForm);
m_roughness_row = m_layout->rowCount() - 1;
// -- layouts
m_layout->addRow(new ParticleLayoutForm(this, layout, ec));
// -- Button for adding a new layout
auto* btn = new QPushButton("Add particle layout", this);

Wuttke, Joachim
committed
connect(btn, &QPushButton::clicked, [this, ec] { ec->addLayoutItem(this); });
// listen to changes in materials to update the title (contains the material name). Necessary
// to reflect e.g. a name change done in the material editor.

Wuttke, Joachim
committed
connect(ec->materialModel(), &MaterialsSet::materialChanged, this, &LayerForm::updateTitle);
updateLayerPositionDependentElements();
}
void LayerForm::updateColor()
{
QColor bckgroundCol = m_layer->color();
setStyleSheet("LayerForm {background-color: " + bckgroundCol.name(QColor::HexRgb) + "}");
}
void LayerForm::updateTitle()
{
const SampleItem* sampleItem = m_ec->sampleItem();
ASSERT(sampleItem);
int i = Vec::indexOfPtr(m_layer, sampleItem->uniqueLayerItems());
setTitle("Layer " + QString::number(i) + " Material: " + m_layer->materialName());
void LayerForm::expand()
{
void LayerForm::updateLayerPositionDependentElements()
{
return;
updateTitle();
const auto* sample = m_ec->sampleItem();
const bool isFirstLayer = sample->uniqueLayerItems().front() == m_layer;
const bool isLastLayer = sample->uniqueLayerItems().back() == m_layer;
const bool thicknessIsSemiInfinite =
(isFirstLayer || isLastLayer) && (sample->uniqueLayerItems().size() != 1);
const bool thicknessIsInfinite = sample->uniqueLayerItems().size() == 1;
m_roughnessForm->setVisible(!isFirstLayer);
m_roughnessForm->markLayerAsBottom(isLastLayer);
QWidget* w = m_layout->itemAt(m_thickness_row, QFormLayout::FieldRole)->widget();
if (thicknessIsSemiInfinite || thicknessIsInfinite) {

Wuttke, Joachim
committed
auto* info = qobject_cast<QLineEdit*>(w);
if (info == nullptr) {
m_layout->removeRow(m_thickness_row);
info = new QLineEdit(this);
info->setEnabled(false);
info->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_layout->insertRow(m_thickness_row, m_layer->thickness().label(), info);
}
info->setText(thicknessIsSemiInfinite ? "Semi-infinite" : "Infinite");
} else if (!thicknessIsSemiInfinite && !thicknessIsInfinite
&& (qobject_cast<QLineEdit*>(w) != nullptr)) {
m_layout->removeRow(m_thickness_row);
m_layout->insertValue(m_thickness_row, m_layer->thickness());
m_move_button->setVisible(m_ec->sampleItem()->uniqueLayerItems().size() > 1);
void LayerForm::onLayoutAdded(ParticleLayoutItem* t)
int index = Vec::indexOfPtr(t, m_layer->layoutItems());
const int rowInLayout = m_layout->rowCount() - m_layer->layoutItems().size() + index;
QWidget* w = new ParticleLayoutForm(this, t, m_ec);
void LayerForm::onAboutToRemoveLayout(ParticleLayoutItem* t)
int index = Vec::indexOfPtr(t, m_layer->layoutItems());
const int rowInLayout =
m_layout->rowCount() - m_layer->layoutItems().size() - 1 + index; // -1: btn