BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
layereditoractions.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/layereditor/layereditoractions.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
18 #include "gui2/model/sampleitems.h"
19 #include "gui2/model/samplemodel.h"
20 #include "mvvm/model/itemutils.h"
21 #include "mvvm/model/modelutils.h"
23 #include <QAction>
24 
25 using namespace ModelView;
26 
27 namespace gui2 {
28 
30  SampleModel* sample_model{nullptr};
31  LayerSelectionModel* selection_model{nullptr};
33 
34  //! Finds parent and tagrow to insert new item
35 
36  std::pair<SessionItem*, TagRow> locateInsertPlace()
37  {
38  auto all_selected = selection_model->selectedItems();
39  auto selected = all_selected.empty() ? nullptr : all_selected.back();
40  if (selected)
41  return {selected->parent(), selected->tagRow().next()};
42  return {root_item(), TagRow{}};
43  }
44 
45  //! Returns a multi layer playing the role of invisible root item.
46 
48  {
49  return selection_model->viewModel()->sessionItemFromIndex(QModelIndex());
50  }
51 };
52 
53 LayerEditorActions::LayerEditorActions(QObject* parent)
54  : QObject(parent), p_impl(std::make_unique<LayerEditorActionsImpl>())
55 {
56 }
57 
59 {
60  p_impl->sample_model = model;
61 }
62 
63 //! Adds layer after selected item. If more than one item is selected, adds after the last one.
64 
66 {
67  if (!p_impl->sample_model)
68  return;
69 
70  auto [parent, tagrow] = p_impl->locateInsertPlace();
71  auto new_item = p_impl->sample_model->insertItem<LayerItem>(parent, tagrow);
72  p_impl->selection_model->selectItem(new_item);
73 }
74 
76 {
77  if (!p_impl->sample_model)
78  return;
79 
80  auto [parent, tagrow] = p_impl->locateInsertPlace();
81  auto multilayer = p_impl->sample_model->insertItem<MultiLayerItem>(parent, tagrow);
82  p_impl->sample_model->insertItem<LayerItem>(multilayer);
83  p_impl->sample_model->insertItem<LayerItem>(multilayer);
84  p_impl->selection_model->selectItem(multilayer);
85 }
86 
88 {
89  if (!p_impl->sample_model)
90  return;
91 
92  auto items = p_impl->selection_model->selectedItems();
93  if (items.empty())
94  return;
95 
96  std::vector<ModelView::SessionItem*> new_selection;
97  for (auto to_clone : items)
98  new_selection.push_back(p_impl->sample_model->copyItem(to_clone, to_clone->parent(),
99  to_clone->tagRow().next()));
100 
101  p_impl->selection_model->selectItems(new_selection);
102 }
103 
105 {
106  auto items = p_impl->selection_model->selectedItems();
107  if (items.empty())
108  return;
109 
110  auto prev_to_select = ModelView::Utils::FindPreviousSibling(items.front());
111  auto next_to_select = ModelView::Utils::FindNextSibling(items.back());
112 
113  for (auto item : items)
115  if (next_to_select) {
116  p_impl->selection_model->selectItem(next_to_select);
117  } else if (prev_to_select) {
118  p_impl->selection_model->selectItem(prev_to_select);
119  }
120 }
121 
123 {
124  auto selected = p_impl->selection_model->selectedItems();
125 
126  for (auto item : selected)
128 
129  p_impl->selection_model->selectItems(selected);
130 }
131 
133 {
134  auto selected = p_impl->selection_model->selectedItems();
135 
136  for (auto item : selected)
138 
139  p_impl->selection_model->selectItems(selected);
140 }
141 
143 {
144  p_impl->selection_model = selection_model;
145 }
146 
148 
149 } // namespace gui2
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
void onAddLayer()
Adds layer after selected item. If more than one item is selected, adds after the last one.
void setSelectionModel(LayerSelectionModel *selection_model)
std::unique_ptr< LayerEditorActionsImpl > p_impl
void setModel(SampleModel *model)
Layer with name, thickness and reference to material.
Definition: sampleitems.h:39
Custom selection model for layer view model (AbstractViewModel).
Multi layer capable of holding layers and other multi-layers.
Definition: sampleitems.h:51
Model to hold layers and multi-layers.
Definition: samplemodel.h:24
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
MVVM_MODEL_EXPORT void MoveDown(SessionItem *item)
Moves item down (increments row of the item). Works on children belonging to single tag.
Definition: modelutils.cpp:71
MVVM_MODEL_EXPORT SessionItem * FindNextSibling(SessionItem *item)
Returns next sibling with same tag.
Definition: itemutils.cpp:131
MVVM_MODEL_EXPORT void MoveUp(SessionItem *item)
Moves item up (decrements row of the item). Works on children belonging to single tag.
Definition: modelutils.cpp:63
MVVM_MODEL_EXPORT SessionItem * FindPreviousSibling(SessionItem *item)
Returns previous sibling with same tag.
Definition: itemutils.cpp:140
MVVM_MODEL_EXPORT void DeleteItemFromModel(SessionItem *item)
Removes and deletes item from its model.
Definition: modelutils.cpp:54
materialitems.h Collection of materials to populate MaterialModel.
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
ModelView::SessionItem * root_item()
Returns a multi layer playing the role of invisible root item.
std::pair< SessionItem *, TagRow > locateInsertPlace()
Finds parent and tagrow to insert new item.
Defines class CLASS?