BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SampleView.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/SampleDesigner/SampleView.cpp
6 //! @brief Implements class SampleView
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
28 
29 #include <QBoxLayout>
30 #include <QDockWidget>
31 #include <QMenu>
32 #include <QToolButton>
33 
34 SampleView::SampleView(QWidget* parent, ProjectDocument* document)
35  : QMainWindow(parent)
36  , m_document(document)
37 {
38  setObjectName("SampleView");
39 
42 
43  m_docks = new DocksController(this);
44 
45  auto* editor = new LayerOrientedSampleEditor(this, document);
46 
47  auto* sampleSelectionPane = new QWidget(this);
48  auto* sampleSelectionLayout = new QVBoxLayout(sampleSelectionPane);
49  sampleSelectionLayout->setContentsMargins(0, 0, 0, 0);
50  sampleSelectionLayout->setSpacing(0);
51 
52  auto* sampleSelectionToolbar = new StyledToolbar(sampleSelectionPane);
54  sampleSelectionToolbar->addAction(m_sampleSelectionView->newSampleAction());
55 #ifdef BORNAGAIN_PYTHON
56  sampleSelectionToolbar->addAction(m_sampleSelectionView->importSampleAction());
57 #endif
58  sampleSelectionToolbar->addAction(m_sampleSelectionView->chooseFromLibraryAction());
59  if (auto* btn = dynamic_cast<QToolButton*>(sampleSelectionToolbar->widgetForAction(
61  btn->setPopupMode(QToolButton::InstantPopup);
62 
63  sampleSelectionToolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
64  sampleSelectionLayout->addWidget(sampleSelectionToolbar);
65  sampleSelectionLayout->addWidget(m_sampleSelectionView);
66  m_sampleSelectionView->setSizePolicy(QSizePolicy::MinimumExpanding,
67  QSizePolicy::MinimumExpanding);
68 
69  auto* scriptPanel = new ScriptPanel(this);
70  m_realSpacePanel = new RealSpacePanel(this);
71 
72  sampleSelectionPane->setWindowTitle("Samples");
73 
74  m_docks->addWidget(SAMPLE_LIST, sampleSelectionPane, Qt::LeftDockWidgetArea);
75  m_docks->addWidget(REALSPACEPANEL, m_realSpacePanel, Qt::BottomDockWidgetArea);
76  m_docks->addWidget(PYTHONPANEL, scriptPanel, Qt::BottomDockWidgetArea);
77 
80 
83 
86 
87  connect(editor, &LayerOrientedSampleEditor::modified, scriptPanel,
89 
92 
95 
96  connect(editor, &LayerOrientedSampleEditor::aboutToRemoveItem, this,
98 
100  m_sampleSelectionView->newSampleAction(), &QAction::trigger, Qt::QueuedConnection);
101 
104 
106  Qt::UniqueConnection);
107 
108  setCentralWidget(editor);
109  resetLayout();
110 }
111 
113 {
115 }
116 
118 {
119  RealSpaceCanvas::ItemForRealSpace itemForRealSpace;
120  if (auto* p = itemToView.get_if<MultiLayerItem*>())
121  itemForRealSpace = p;
122  else if (auto* p = itemToView.get_if<LayerItem*>())
123  itemForRealSpace = p;
124  else if (auto* p = itemToView.get_if<ParticleLayoutItem*>())
125  itemForRealSpace = p;
126  else if (auto* p = itemToView.get_if<ItemWithParticles*>())
127  itemForRealSpace = p;
128  else
129  return;
130 
133  itemForRealSpace);
134 }
135 
137 {
138  // If an item shall be deleted from the sample, check whether RealSpace is affected.
139  // Its current item could be itemToRemmove itself, or it could be a child of itemToRemove.
140 
141  // -- for convenience access later on
142  auto* canvas = m_realSpacePanel->canvas();
143  auto* currentAsLayer = canvas->currentItemAs<LayerItem*>();
144  auto* currentAsLayout = canvas->currentItemAs<ParticleLayoutItem*>();
145  auto* currentAsParticle = canvas->currentItemAs<ItemWithParticles*>();
146  const auto resetIf = [&](bool b) {
147  if (b)
148  canvas->resetScene();
149  };
150 
151  if (itemToRemove.get_if<MultiLayerItem*>()) {
152  // -- itemToRemove is a sample. In this case: always reset
153  canvas->resetScene();
154  } else if (auto* layerToRemove = itemToRemove.get_if<LayerItem*>()) {
155  // -- itemToRemove is a layer
156  resetIf(layerToRemove == currentAsLayer);
157  resetIf(layerToRemove->layouts().contains(currentAsLayout));
158  resetIf(layerToRemove->itemsWithParticles().contains(currentAsParticle));
159  } else if (auto* layoutToRemove = itemToRemove.get_if<ParticleLayoutItem*>()) {
160  // -- itemToRemove is a particle layout
161  resetIf(layoutToRemove == currentAsLayout);
162  resetIf(layoutToRemove->containedItemsWithParticles().contains(currentAsParticle));
163  } else if (auto* particleToRemove = itemToRemove.get_if<ItemWithParticles*>()) {
164  // -- itemToRemove is a particle
165  resetIf(particleToRemove == currentAsParticle);
166  resetIf(particleToRemove->containedItemsWithParticles().contains(currentAsParticle));
167  }
168 }
169 
171 {
173 }
174 
175 void SampleView::fillViewMenu(QMenu* menu)
176 {
178 
179  menu->addSeparator();
180 
181  auto* action = new QAction(menu);
182  action->setText("Reset to default layout");
183  connect(action, &QAction::triggered, this, &SampleView::resetLayout);
184  menu->addAction(action);
185 }
186 
187 void SampleView::showEvent(QShowEvent*)
188 {
191 }
192 
194 {
195  m_docks->resetLayout();
196 
198  m_docks->findDock(REALSPACEPANEL)->raise(); // makes first tab active
199 
200  m_docks->findDock(REALSPACEPANEL)->hide();
201  m_docks->findDock(PYTHONPANEL)->hide();
202 
203  if (auto* d = m_docks->findDock(SAMPLE_LIST)) {
205  d->hide();
206  else
207  d->show();
208  }
209 }
Defines class DocksController.
Defines abstract item with a material property.
Defines class LayerItem.
Defines class LayerOrientedSampleEditor.
Defines class MultiLayerItem.
Defines class ParticleLayoutItem.
Defines class ProjectDocument.
Defines class RealSpaceCanvas.
Defines class RealSpacePanel.
Defines class SampleListView.
Defines class SampleView.
Defines class SampleDesigner.
Defines class StyledToolbar.
Handles creation and appearance of docked widgets in the context of QMainWindow. It is used for Sampl...
void setDockVisible(int id, bool visible=true)
void addDockActionsToMenu(QMenu *menu)
void addWidget(int id, QWidget *widget, Qt::DockWidgetArea area)
QDockWidget * findDock(int id)
void toggleDock(int id)
Sample editor with layer oriented presentation of a sample.
void setCurrentSample(MultiLayerItem *sampleItem)
void aboutToRemoveItem(SampleItem item)
void requestViewInRealSpace(SampleItem itemToShow)
Project document class handles all data related to the opened project (sample, job,...
void singleSampleModeChanged()
Emitted when single sample mode has changed.
bool singleSampleMode() const
void setCurrentItem(MultiLayerItem *containingMultiLayerItem, ItemForRealSpace item)
std::variant< MultiLayerItem *, LayerItem *, ParticleLayoutItem *, ItemWithParticles * > ItemForRealSpace
Defines the item types which can be the "current item".
T currentItemAs() const
Panel to show 3D view of sample. Contains toolbar and RealSpaceCanvas.
RealSpaceCanvas * canvas()
This can hold any item which belongs to a sample.
Definition: SampleItem.h:33
T get_if()
Short version to get a contained pointer (the std::get_if would return pointer-to-pointer)
Definition: SampleItem.h:43
List view to select one sample (left side of layer-oriented sample editor)
MultiLayerItem * currentSample()
QAction * chooseFromLibraryAction()
QAction * importSampleAction()
void currentSampleChanged(MultiLayerItem *current)
void selectFirstSample()
Select first sample, if any sample exists.
QAction * newSampleAction()
void showEvent(QShowEvent *) override
Definition: SampleView.cpp:187
void onAboutToRemoveItem(SampleItem item)
Definition: SampleView.cpp:136
RealSpacePanel * m_realSpacePanel
Definition: SampleView.h:54
void fillViewMenu(QMenu *menu)
Definition: SampleView.cpp:175
void resetLayout()
Definition: SampleView.cpp:193
SampleListView * m_sampleSelectionView
Definition: SampleView.h:55
void updateSingleSampleMode()
Definition: SampleView.cpp:112
void toggleRealSpaceView()
Definition: SampleView.cpp:170
ProjectDocument * m_document
Definition: SampleView.h:53
SampleView(QWidget *parent, ProjectDocument *document)
Definition: SampleView.cpp:34
void onRequestViewInRealSpace(SampleItem itemToView)
Show the item in the real space view.
Definition: SampleView.cpp:117
@ REALSPACEPANEL
Definition: SampleView.h:31
DocksController * m_docks
Definition: SampleView.h:52
Resides at the bottom of SampleView and displays a Python script.
Definition: ScriptPanel.h:31
void onSampleModified()
Definition: ScriptPanel.cpp:67
void setCurrentSample(MultiLayerItem *sampleItem)
Definition: ScriptPanel.cpp:61
The StyledToolbar class represents our standard narrow toolbar with the height 24 pixels.
Definition: StyledToolbar.h:22