File: LayerContainerForm.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 (111 lines) | stat: -rw-r--r-- 3,850 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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Sample/LayerContainerForm.cpp
//! @brief     Implements class LayerContainerForm.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2024
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/Sample/LayerContainerForm.h"
#include "Base/Util/Assert.h"
#include "Base/Util/Vec.h"
#include "GUI/Model/Sample/ItemWithLayers.h"
#include "GUI/Model/Sample/LayerStackItem.h"
#include "GUI/Model/Sample/SampleItem.h"
#include "GUI/Model/Type/PredefinedColors.h"
#include "GUI/View/Base/SlotFactory.h"
#include "GUI/View/Sample/HeinzFormLayout.h"
#include "GUI/View/Sample/SampleEditorController.h"
#include "GUI/View/Widget/WidgetMoverButton.h"
#include <QMenu>

LayerContainerForm::LayerContainerForm(QWidget* parent, ItemWithLayers* item,
                                       SampleEditorController* ec, const QString& what)
    : CollapsibleGroupBox(parent, item->expandGroupbox)
    , m_layout(new HeinzFormLayout(ec))
    , m_item(item)
    , m_ec(ec)
{
    ASSERT(parent);
    setContentsMargins(5, 5, 5, 5);

    body()->setLayout(m_layout);

    //... top right corner actions

    // choose color action
    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);

    for (const auto& color : GUI::Colors::layerDefaults()) {
        QPixmap p(64, 64);
        p.fill(color);
        auto* ca = menu->addAction(QIcon(p), "");
        connect(ca, &QAction::triggered, [this, color] {
            m_item->setColor(color);
            updateColor();
            gDoc->setModified();
        });
    }

    // move component action
    m_move_button = new WidgetMoverButton(this, this, 0);
    m_move_button->setToolTip("Move " + what + " up/down");
    connect(m_move_button, &WidgetMoverButton::finishedMoving, ec,
            &SampleEditorController::onStoppedToMoveComponent);
    addTitleWidget(m_move_button);

    // show in real space action
    auto* showInRealspaceAction = SlotFactory::createShowInRealspaceSlot(
        this, what, [ec, item] { ec->requestViewInRealspace(item); });
    addTitleAction(showInRealspaceAction);

    // duplicate action
    auto* duplicate_action = SlotFactory::createDuplicateSlot(
        this, what, [ec, item] { ec->duplicateItemWithLayers(item); });
    addTitleAction(duplicate_action);

    // remove action
    auto* remove_action =
        SlotFactory::createRemoveSlot(this, what, [ec, item] { ec->removeItemWithLayers(item); });
    addTitleAction(remove_action);

    updateColor();
}

void LayerContainerForm::expand()
{
    setExpanded(true);
}

void LayerContainerForm::updatePositionDependentElements()
{
    LayerStackItem* parentStack = m_ec->sampleItem()->parentOfComponent(m_item);
    m_move_button->setVisible(parentStack && parentStack->componentItems().size() > 1);
}

void LayerContainerForm::updateColor()
{
    if (isOuterStack())
        return;

    QColor bckgrCol = m_item->color();
    setStyleSheet("LayerContainerForm {background-color: " + bckgrCol.name(QColor::HexRgb) + "}");
}

bool LayerContainerForm::isOuterStack() const
{
    return (m_item == &(m_ec->sampleItem()->outerStackItem()));
}