BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
materialeditoractions.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/materialeditor/materialeditoractions.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 
19 #include "mvvm/model/modelutils.h"
22 #include <QFile>
23 #include <QTextStream>
24 
25 using namespace ModelView;
26 
27 namespace gui2 {
28 
30  MaterialModel* material_model{nullptr};
31  MaterialSelectionModel* 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->selectedMaterials();
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 MaterialEditorActions::MaterialEditorActions(QObject* parent)
54  : QObject(parent), p_impl(std::make_unique<MaterialEditorActionsImpl>())
55 {
56 }
57 
59 {
60  p_impl->material_model = model;
61 }
62 
64 {
65  p_impl->selection_model = selection_model;
66 }
67 
69 {
70  if (!p_impl->material_model)
71  return;
72 
73  auto [parent, tagrow] = p_impl->locateInsertPlace();
74  auto material = p_impl->material_model->addDefaultMaterial(tagrow);
75  p_impl->selection_model->selectItem(material);
76 }
77 
78 //! Processes request to clone selected materials.
79 
81 {
82  if (!p_impl->material_model)
83  return;
84 
85  std::vector<ModelView::SessionItem*> new_selection;
86  for (const auto item : p_impl->selection_model->selectedMaterials())
87  new_selection.push_back(p_impl->material_model->cloneMaterial(item));
88  p_impl->selection_model->selectItems(new_selection);
89 }
90 
92 {
93  if (!p_impl->selection_model)
94  return;
95 
96  for (auto item : p_impl->selection_model->selectedMaterials())
98 }
99 
101 {
102  if (!p_impl->selection_model)
103  return;
104 
105  for (auto item : p_impl->selection_model->selectedMaterials())
107 }
108 
110 {
111  if (!p_impl->selection_model)
112  return;
113 
114  auto items = p_impl->selection_model->selectedMaterials();
115  std::reverse(items.begin(), items.end()); // to correctly move multiple selections
116  for (auto item : p_impl->selection_model->selectedMaterials())
118 }
119 
121 {
122  auto item = p_impl->root_item();
123  const auto containers = item->children();
124 
125  bool title = true; // print title only once in ascii file
126  QString tableData;
127  QString titleData;
128 
129  for (auto container : containers) {
130  auto data = container->modelType();
131  for (auto fields : dynamic_cast<MaterialBaseItem*>(container)->children()) {
132  if (title) {
133  titleData += (fields->displayName()).c_str();
134  titleData += " ";
135  }
136  auto val = fields->data<QVariant>(1); // role 1 has the values.
137  if (strcmp(val.typeName(), "std::string") == 0) {
138  tableData += val.value<std::string>().c_str();
139  tableData += " ";
140  } else if (strcmp(val.typeName(), "int") == 0) {
141  auto int_val = val.value<int>();
142  tableData += QString::number(int_val);
143  tableData += " ";
144  } else if (strcmp(val.typeName(), "double") == 0) {
145  auto double_val = val.value<double>();
146  tableData += QString::number(double_val);
147  tableData += " ";
148  } else if (strcmp(val.typeName(), "float") == 0) {
149  auto float_val = val.value<float>();
150  tableData += QString::number(float_val);
151  tableData += " ";
152  } else {
153  auto color_str = val.toString();
154  tableData += color_str;
155  tableData += " ";
156  }
157  }
158  if (title) {
159  titleData += "\n";
160  tableData = titleData + tableData;
161  }
162  title = false;
163  tableData += "\n";
164  }
165  /*for text file*/
166  QFile txtFile("materialdata");
167  if (txtFile.open(QIODevice::WriteOnly)) {
168 
169  QTextStream out(&txtFile);
170  out << tableData;
171 
172  txtFile.close();
173  }
174 }
175 
177 
179 
180 } // 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
Base class with all materials with name and color defined.
Definition: materialitems.h:40
void setMaterialSelectionModel(MaterialSelectionModel *selection_model)
void onCloneMaterial()
Processes request to clone selected materials.
std::unique_ptr< MaterialEditorActionsImpl > p_impl
void setModel(MaterialModel *model)
Model to hold MaterialItems.
Definition: materialmodel.h:35
Custom selection model for material view model (AbstractViewModel).
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 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 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?
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?