File: ParticleLayoutForm.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (122 lines) | stat: -rw-r--r-- 4,567 bytes parent folder | download
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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Sample/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)
//
//  ************************************************************************************************

#include "GUI/View/Sample/ParticleLayoutForm.h"
#include "Base/Util/Assert.h"
#include "Base/Util/Vec.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"
#include "GUI/View/Base/SlotFactory.h"
#include "GUI/View/Numeric/DSpinBox.h"
#include "GUI/View/Sample/HeinzFormLayout.h"
#include "GUI/View/Sample/InterferenceForm.h"
#include "GUI/View/Sample/LayerEditorUtil.h"
#include "GUI/View/Sample/LayerForm.h"
#include "GUI/View/Sample/SampleEditorController.h"
#include <QAction>
#include <QPushButton>

ParticleLayoutForm::ParticleLayoutForm(LayerForm* form, ParticleLayoutItem* t,
                                       SampleEditorController* ec)
    : CollapsibleGroupBox("TODO", form, t->expandParticleLayout)
    , m_layout(new HeinzFormLayout(ec))
    , m_layout_item(t)
    , m_ec(ec)
{
    body()->setLayout(m_layout);

    m_layout->addValue(m_layout_item->ownDensity());
    int rowOfTotalDensity = m_layout->rowCount() - 1;
    m_total_density_spin_box =
        m_layout->widgetAt<DSpinBox*>(rowOfTotalDensity, QFormLayout::FieldRole);
    ASSERT(m_total_density_spin_box);

    m_layout->addRow(new InterferenceForm(this, t, ec));

    for (auto* particle : m_layout_item->itemsWithParticles())
        m_layout->addRow(
            GUI::Util::Layer::createWidgetForItemWithParticles(this, particle, true, ec));

    auto* btn = GUI::Util::Layer::createAddParticleButton(
        this, [=](FormfactorCatalog::Type type) { ec->addParticleLayoutItem(t, type); },
        [=](ParticleCatalog::Type type) { ec->addParticleLayoutItem(t, type); });
    m_layout->addStructureEditingRow(btn);

    //... top right corner actions

    // show in real space
    {
        auto* showInRealspaceAction = SlotFactory::createShowInRealspaceSlot(
            this, "particle layout", [ec, t] { ec->requestViewInRealspace(t); });
        addTitleAction(showInRealspaceAction);
    }
    // duplicate
    {
        m_duplicate_action = SlotFactory::createDuplicateSlot(
            this, "particle layout", [ec, t, form] { ec->duplicateLayoutItem(form, t); });
        addTitleAction(m_duplicate_action);
    }
    // remove
    {
        m_remove_action = SlotFactory::createRemoveSlot(
            this, "particle layout", [ec, t, form] { ec->removeLayoutItem(form, t); });
        addTitleAction(m_remove_action);
    }

    updateDensityEnabling();
    updateTitle(form->layerItem());
}

void ParticleLayoutForm::onParticleAdded(ItemWithParticles* t)
{
    int index = Vec::indexOfPtr(t, m_layout_item->itemsWithParticles());
    const int rowInLayout = m_layout->rowCount() - 1
                            - (m_layout_item->itemsWithParticles().size() - 1) + index; // -1: btn

    m_layout->insertRow(rowInLayout,
                        GUI::Util::Layer::createWidgetForItemWithParticles(this, t, true, m_ec));
}

void ParticleLayoutForm::onAboutToRemoveParticle(ItemWithParticles* t)
{
    int index = Vec::indexOfPtr(t, m_layout_item->itemsWithParticles());
    const int rowInLayout =
        m_layout->rowCount() - m_layout_item->itemsWithParticles().size() - 1 + index; // -1: btn

    m_layout->removeRow(rowInLayout);
}

void ParticleLayoutForm::updateDensityEnabling()
{
    m_total_density_spin_box->setEnabled(!m_layout_item->totalDensityIsDefinedByInterference());
}

void ParticleLayoutForm::updateDensityValue()
{
    if (m_layout_item->totalDensityIsDefinedByInterference())
        m_layout_item->setOwnDensity(m_layout_item->totalDensityValue());

    m_total_density_spin_box->updateValue();
}

void ParticleLayoutForm::updateTitle(const LayerItem* layerItem)
{
    const auto& layouts = layerItem->layoutItems();
    if (layouts.size() > 1)
        setTitle("Particle layout " + QString::number(Vec::indexOfPtr(m_layout_item, layouts) + 1));
    else
        setTitle("Particle layout");
}