Skip to content
Snippets Groups Projects
LayerForm.cpp 7.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • //  ************************************************************************************************
    //
    //  BornAgain: simulate and fit reflection and scattering
    //
    
    //! @file      GUI/View/Sample/LayerForm.cpp
    
    //! @brief     Implements class LayerForm.
    
    //!
    //! @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"
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    #include "Base/Util/Assert.h"
    
    #include "GUI/Model/Material/MaterialItem.h"
    
    #include "GUI/Model/Sample/LayerItem.h"
    
    #include "GUI/Model/Sample/SampleItem.h"
    
    #include "GUI/View/Numeric/NumWidgetUtil.h"
    
    #include "GUI/View/Sample/HeinzFormLayout.h"
    #include "GUI/View/Sample/MaterialInplaceForm.h"
    #include "GUI/View/Sample/ParticleLayoutForm.h"
    
    #include "GUI/View/Tool/ActionFactory.h"
    
    #include "GUI/View/Tool/WidgetMoverButton.h"
    
    #include <QLineEdit>
    #include <QMenu>
    #include <QPushButton>
    
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
    LayerForm::LayerForm(QWidget* parent, LayerItem* layerItem, SampleEditorController* ec)
    
        : CollapsibleGroupBox(layerItem->layerName(), parent, layerItem->expandLayer)
    
        , m_layout(new HeinzFormLayout(ec))
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
        , m_layer(layerItem)
    
        setContentsMargins(5, 5, 5, 5);
    
        body()->setLayout(m_layout);
    
    
        //... top right corner actions
    
        // 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);
    
            addTitleAction(chooseColorAction);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
            for (const auto& col : GUI::Util::Layer::predefinedLayerColors()) {
    
                QPixmap p(64, 64);
                p.fill(col);
                auto* ca = menu->addAction(QIcon(p), "");
    
                connect(ca, &QAction::triggered, [this, layerItem, col] {
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
                    layerItem->setColor(col);
    
                    updateColor();
                });
            }
    
        // move
        {
    
            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(
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
                this, "layer", [ec, layerItem] { ec->requestViewInRealspace(layerItem); });
    
            addTitleAction(showInRealspaceAction);
    
        }
        // duplicate
        {
    
            m_duplicate_action = ActionFactory::createDuplicateAction(
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
                this, "layer", [ec, layerItem] { ec->duplicateLayerItem(layerItem); });
    
            addTitleAction(m_duplicate_action);
    
        }
        // remove
        {
    
            m_remove_action = ActionFactory::createRemoveAction(
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
                this, "layer", [ec, layerItem] { ec->removeLayerItem(layerItem); });
    
            addTitleAction(m_remove_action);
    
        QColor bckgroundCol = m_layer->color();
    
        setStyleSheet("LayerForm {background-color: " + bckgroundCol.name(QColor::HexRgb) + "}");
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        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 m_ec->modified();
                                                      },
                                                      RealLimits::lowerLimited(1),
                                                      "Number of horizontal slices.\n"
                                                      "Used for Average Layer Material calculations \n"
                                                      "when corresponding simulation option is set."));
    
        m_layout->addSelection(m_layer->roughnessSelection());
    
        m_roughness_row = m_layout->rowCount() - 1;
    
    Mikhail Svechnikov's avatar
    Mikhail Svechnikov committed
        for (auto* layout : layerItem->layoutItems())
    
            m_layout->addRow(new ParticleLayoutForm(this, layout, ec));
    
    
        // -- Button for adding a new layout
        auto* btn = new QPushButton("Add particle layout", this);
    
        connect(btn, &QPushButton::clicked, [this, ec] { ec->addLayoutItem(this); });
    
        m_structure_editing_widgets << btn;
    
        m_layout->addStructureEditingRow(btn);
    
        // 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.
    
        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) + "}");
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        const SampleItem* sampleItem = m_ec->sampleItem();
        ASSERT(sampleItem);
        int i = sampleItem->layerItems().indexOf(m_layer);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        setTitle("Layer " + QString::number(i) + "     Material: " + m_layer->materialName());
    
        setExpanded(true);
    
    void LayerForm::updateLayerPositionDependentElements()
    {
    
        if (m_roughness_row == -1)
    
        const auto* sample = m_ec->sampleItem();
    
        const bool isFirstLayer = sample->layerItems().first() == m_layer;
        const bool isLastLayer = sample->layerItems().last() == m_layer;
    
        const bool thicknessIsSemiInfinite =
    
            (isFirstLayer || isLastLayer) && (sample->layerItems().size() != 1);
        const bool thicknessIsInfinite = sample->layerItems().size() == 1;
    
        m_layout->setRowVisible(m_roughness_row, !isFirstLayer);
    
        if (m_thickness_row == -1)
    
        QWidget* w = m_layout->itemAt(m_thickness_row, QFormLayout::FieldRole)->widget();
    
        if (thicknessIsSemiInfinite || thicknessIsInfinite) {
    
                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()->layerItems().size() > 1);
    
    }
    
    void LayerForm::onLayoutAdded(ParticleLayoutItem* newLayoutItem)
    {
    
        int index = m_layer->layoutItems().indexOf(newLayoutItem);
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        const int rowInLayout = m_layout->rowCount() - m_layer->layoutItems().size() + index;
    
        QWidget* w = new ParticleLayoutForm(this, newLayoutItem, m_ec);
        m_layout->insertRow(rowInLayout, w);
    
    }
    
    void LayerForm::onAboutToRemoveLayout(ParticleLayoutItem* layoutItem)
    {
    
        int index = m_layer->layoutItems().indexOf(layoutItem);
    
            m_layout->rowCount() - m_layer->layoutItems().size() - 1 + index; // -1: btn
    
        m_layout->removeRow(rowInLayout);