BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
LayerOrientedSampleEditor.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/SampleDesigner/LayerOrientedSampleEditor.cpp
6 //! @brief Implements class LayerOrientedSampleEditor
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
24 #include <QBoxLayout>
25 #include <QPushButton>
26 #include <QScrollArea>
27 
29  : QWidget(parent)
30  , m_currentMultiLayerWidget(nullptr)
31  , m_undoAction(nullptr)
32  , m_redoAction(nullptr)
33  , m_showInlineEditButtonsAction(nullptr)
34  , m_asAngstromAction(nullptr)
35  , m_currentMultiLayerItem(nullptr)
36  , m_document(document)
37 {
38  setAttribute(Qt::WA_StyledBackground, true);
39  setProperty("stylable", true); // for stylesheet addressing
40 
41  m_scrollArea = new QScrollArea(this);
42  m_scrollArea->setWidgetResizable(true);
43  m_scrollArea->setWidget(createEmptyWidget());
44 
45  m_toolbar = new StyledToolbar(this);
46  m_toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
47 
48  auto* mainLayout = new QVBoxLayout(this);
49  mainLayout->setContentsMargins(0, 0, 0, 0);
50  mainLayout->setSpacing(0);
51  mainLayout->addWidget(m_toolbar);
52  mainLayout->addWidget(m_scrollArea);
53 
54  m_showInlineEditButtonsAction = new QAction(QIcon(":/images/edit.svg"), "Edit structure", this);
55  connect(m_showInlineEditButtonsAction, &QAction::toggled, this,
57  m_showInlineEditButtonsAction->setCheckable(true);
58  m_showInlineEditButtonsAction->setChecked(true);
60 
61  m_asAngstromAction = new QAction(this);
62  m_asAngstromAction->setIcon(QIcon(":/images/unit_angstrom.svg"));
63  m_asAngstromAction->setToolTip(u8"Use \u00c5ngstrom as unit (where applicable)");
64  connect(m_asAngstromAction, &QAction::toggled, this,
66  m_asAngstromAction->setCheckable(true);
67 
68  auto* asNanometerAction = new QAction(this);
69  asNanometerAction->setIcon(QIcon(":/images/unit_nm.svg"));
70  asNanometerAction->setToolTip("Use nanometer as unit (where applicable)");
71  connect(asNanometerAction, &QAction::toggled, this,
73  asNanometerAction->setCheckable(true);
74 
75  auto* ag = new QActionGroup(this);
76  ag->addAction(asNanometerAction);
77  ag->addAction(m_asAngstromAction);
79  m_asAngstromAction->setChecked(true);
80  else
81  asNanometerAction->setChecked(true);
82 
83  m_toolbar->addActions(ag->actions());
84 }
85 
87 {
88  qDeleteAll(m_editControllers.values());
89 }
90 
92 {
93  if (m_currentMultiLayerItem != nullptr)
94  m_editControllers[m_currentMultiLayerItem]->setMultiLayerForm(nullptr);
95 
96  m_currentMultiLayerWidget = nullptr;
97  delete m_scrollArea->takeWidget();
98 
99  // use the correct toolbar buttons (from correct undo stack)
100  delete m_undoAction;
101  m_undoAction = nullptr;
102  delete m_redoAction;
103  m_redoAction = nullptr;
104 
105  m_currentMultiLayerItem = sampleItem;
106  if (m_currentMultiLayerItem == nullptr) {
107  m_scrollArea->setWidget(createEmptyWidget());
109  return;
110  }
111 
121 
122  m_undoAction = ec->undoStack()->createUndoAction(this);
123  m_redoAction = ec->undoStack()->createRedoAction(this);
124  m_undoAction->setIcon(QIcon(":/images/undo-variant.svg"));
125  m_redoAction->setIcon(QIcon(":/images/redo.svg"));
126 
127  m_toolbar->addAction(m_undoAction);
128  m_toolbar->addAction(m_redoAction);
129 
131 
133  ec->setMultiLayerForm(m_currentMultiLayerWidget);
134  m_currentMultiLayerWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
138 
140 }
141 
143 {
144  // Undo/Redo is not complete => hide the related buttons/actions
145  if (m_undoAction)
146  m_undoAction->setVisible(false);
147  if (m_redoAction)
148  m_redoAction->setVisible(false);
149 }
150 
152 {
155 }
156 
158 {
161 }
162 
163 void LayerOrientedSampleEditor::createLayerColors() // #baLayerEditor move to better place
164 {
166  return;
167 
168  int col = 0;
169  for (auto* l : m_currentMultiLayerItem->layers()) {
170  if (l->color().isValid())
171  continue;
172 
173  l->setColor(LayerEditorUtils::predefinedLayerColors()[col]);
174  col++;
175  if (col == LayerEditorUtils::predefinedLayerColors().size())
176  col = 0;
177  }
178 }
179 
181 {
182  auto* emptyWidget = new QWidget(this);
183  emptyWidget->setProperty("stylable", true); // for stylesheet addressing
184 
185  auto* btn = new QPushButton("Create sample", emptyWidget);
186  connect(btn, &QPushButton::clicked, this, &LayerOrientedSampleEditor::requestCreateNewSample);
187 
188  auto* layout = new QHBoxLayout(emptyWidget);
189  layout->setContentsMargins(10, 20, 10, 20);
190  layout->addStretch();
191  layout->addWidget(btn);
192  layout->addStretch();
193  layout->setAlignment(Qt::AlignTop);
194 
195  return emptyWidget;
196 }
ApplicationSettings * appSettings
global pointer to the instance
Defines class ApplicationSettings.
Defines class LayerEditorUtils.
Defines class LayerItem.
Defines class LayerOrientedSampleEditor.
Defines class MultiLayerForm.
Defines class MultiLayerItem.
Defines class ProjectDocument.
Defines class SampleEditorController.
Defines class StyledToolbar.
bool defaultUnitIsAngstrom() const
void setCurrentSample(MultiLayerItem *sampleItem)
void aboutToRemoveItem(SampleItem item)
QMap< MultiLayerItem *, SampleEditorController * > m_editControllers
LayerOrientedSampleEditor(QWidget *parent, ProjectDocument *document)
void onShowInlineEditButtonsToggled(bool checked)
void requestViewInRealSpace(SampleItem itemToShow)
QWidget * createEmptyWidget()
Create empty widget with a button for sample creation.
Form to present/edit a MultiLayer (sample)
void setUseAngstrom(bool angstrom)
Show values in Angstrom or nanometers.
void showInlineEditButtons(bool b)
Show or hide all buttons related to structure editing (like "add layer", "remove particle")
QVector< LayerItem * > layers() const
Project document class handles all data related to the opened project (sample, job,...
Class to modify a sample from the layer oriented sample editor.
void requestViewInRealSpace(SampleItem item)
void aboutToRemoveItem(SampleItem item)
The StyledToolbar class represents our standard narrow toolbar with the height 24 pixels.
Definition: StyledToolbar.h:22
QList< QColor > predefinedLayerColors()