Skip to content
Snippets Groups Projects
ParticleLayoutForm.cpp 5.13 KiB
Newer Older
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/SampleDesigner/ParticleLayoutForm.cpp
//! @brief     Implements class ParticleLayoutForm
//!
//! @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)
//
//  ************************************************************************************************

Mikhail Svechnikov's avatar
Mikhail Svechnikov committed
#include "GUI/View/SampleDesigner/ParticleLayoutForm.h"
Mikhail Svechnikov's avatar
Mikhail Svechnikov committed
#include "Base/Util/Assert.h"
#include "GUI/Model/Sample/InterferenceItems.h"
#include "GUI/Model/Sample/ItemWithParticles.h"
#include "GUI/Model/Sample/LayerItem.h"
#include "GUI/Model/Sample/ParticleLayoutItem.h"
Wuttke, Joachim's avatar
Wuttke, Joachim committed
#include "GUI/Support/Util/ActionFactory.h"
#include "GUI/View/Numeric/DoubleSpinBox.h"
#include "GUI/View/SampleDesigner/InterferenceForm.h"
#include "GUI/View/SampleDesigner/LayerEditorUtil.h"
#include "GUI/View/SampleDesigner/LayerForm.h"
#include "GUI/View/SampleDesigner/SampleEditorController.h"
#include "GUI/View/Tool/GroupBoxCollapser.h"
#include <QAction>
#include <QPushButton>

ParticleLayoutForm::ParticleLayoutForm(LayerForm* form, ParticleLayoutItem* layout,
    : QGroupBox(form)
    , m_layoutItem(layout)
{
    FormLayouter layouter(this, ec);
    layouter.setContentsMargins(30, 6, 0, 0);
    int rowOfTotalDensity = layouter.addValue(m_layoutItem->ownDensity());
    m_totalDensitySpinBox =
        layouter.widgetAt<DoubleSpinBox*>(rowOfTotalDensity, QFormLayout::FieldRole);
    ASSERT(m_totalDensitySpinBox);
    layouter.addRow(new InterferenceForm(this, layout, ec));
    for (auto* particle : m_layoutItem->itemsWithParticles())
            LayerEditorUtil::createWidgetForItemWithParticles(this, particle, true, ec));
    auto* btn = LayerEditorUtil::createAddParticleButton(
        this, [=](FormFactorItemCatalog::Type type) { ec->addParticleLayoutItem(layout, type); },
        [=](ItemWithParticlesCatalog::Type type) { ec->addParticleLayoutItem(layout, type); });
    m_structureEditingWidgets << btn;
    layouter.addStructureEditingRow(btn);

    m_collapser = GroupBoxCollapser::installIntoGroupBox(this);
    m_collapser->setExpanded(layout->isExpandLayout());
    connect(m_collapser, &GroupBoxCollapser::toggled, this,
            [layout](bool b) { layout->setExpandLayout(b); });
    // top right corner actions
    // show in real space
    {
        auto* showInRealspaceAction = ActionFactory::createShowInRealspaceAction(
            this, "particle layout", [ec, layout] { ec->requestViewInRealspace(layout); });
        m_collapser->addAction(showInRealspaceAction);
    }
    // duplicate
    {
        m_duplicateAction = ActionFactory::createDuplicateAction(
            this, "particle layout", [ec, layout, form] { ec->duplicateLayoutItem(form, layout); });
        m_collapser->addAction(m_duplicateAction);
    }
        m_removeAction = ActionFactory::createRemoveAction(
            this, "particle layout", [ec, layout, form] { ec->removeLayoutItem(form, layout); });
        m_collapser->addAction(m_removeAction);
    }

    m_layout = layouter.layout();
    updateTitle(form->layerItem());
}

void ParticleLayoutForm::enableStructureEditing(bool b)
{
    m_removeAction->setVisible(b);
    m_duplicateAction->setVisible(b);

    for (auto* w : m_structureEditingWidgets)
        w->setVisible(b);
}

ParticleLayoutItem* ParticleLayoutForm::layoutItem() const
{
    return m_layoutItem;
}

void ParticleLayoutForm::onParticleAdded(ItemWithParticles* p)
    int index = m_layoutItem->itemsWithParticles().indexOf(p);
Mikhail Svechnikov's avatar
Mikhail Svechnikov committed
    const int rowInLayout = m_layout->rowCount() - 1
                            - (m_layoutItem->itemsWithParticles().size() - 1) + index; // -1: btn
    m_layout->insertRow(rowInLayout,
                        LayerEditorUtil::createWidgetForItemWithParticles(this, p, true, m_ec));
}

void ParticleLayoutForm::onAboutToRemoveParticle(ItemWithParticles* item)
{
    int index = m_layoutItem->itemsWithParticles().indexOf(item);
        m_layout->rowCount() - m_layoutItem->itemsWithParticles().size() - 1 + index; // -1: btn
    m_totalDensitySpinBox->setEnabled(!m_layoutItem->totalDensityIsDefinedByInterference());

void ParticleLayoutForm::updateDensityValue()
{
Mikhail Svechnikov's avatar
Mikhail Svechnikov committed
    if (m_layoutItem->totalDensityIsDefinedByInterference())
        m_layoutItem->setOwnDensity(m_layoutItem->totalDensityValue());
    m_totalDensitySpinBox->updateValue();

void ParticleLayoutForm::updateTitle(const LayerItem* layerItem)
{
    const auto layouts = layerItem->layoutItems();
    if (layouts.size() > 1)
        m_collapser->setTitle("Particle layout "
                              + QString::number(layouts.indexOf(m_layoutItem) + 1));
    else
        m_collapser->setTitle("Particle layout");
}