BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
experimentaldatamodel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/model/experimentaldatamodel.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 
20 #include "mvvm/model/modelutils.h"
26 
27 #include <algorithm>
28 #include <cmath>
29 
30 using namespace ModelView;
31 
32 namespace {
33 
34 std::unique_ptr<ItemCatalogue> CreateItemCatalogue()
35 {
36  auto result = std::make_unique<ModelView::ItemCatalogue>();
37  result->registerItem<gui2::CanvasItem>();
38  result->registerItem<gui2::CanvasContainerItem>();
39  result->registerItem<gui2::ExperimentalDataContainerItem>();
40  return result;
41 }
42 
43 } // namespace
44 
45 namespace gui2 {
46 
47 ExperimentalDataModel::ExperimentalDataModel(std::shared_ptr<ItemPool> pool)
48  : SessionModel("ExperimentalDataModel", pool)
49 
50 {
51  init_model();
52 }
53 
54 //! Returns the canvas container of the model.
55 
57 {
58  return topItem<CanvasContainerItem>();
59 }
60 
61 //! Returns the data container of the model.
62 
64 {
65  return topItem<ExperimentalDataContainerItem>();
66 }
67 
69 {
70  return insertItem<CanvasItem>(canvasContainer());
71 }
72 
73 //! Adds graph to 'target_canvas' and returns the result.
74 //! Internally add Data1DItem object to ExperimentalDataContainerItem,
75 //! and set it to GraphItem.
76 
78  CanvasItem& target_canvas)
79 {
80  auto result = insertItem<GraphItem>(&target_canvas);
81 
82  auto data = insertItem<Data1DItem>(dataContainer());
83  data->setAxis<PointwiseAxisItem>(graph_data.bin_centers);
84  data->setValues(graph_data.bin_values);
85  result->setDataItem(data);
86 
87  result->setData(graph_data.graph_description);
88 
89  return result;
90 }
91 
92 //! Remove graph from the model. Underlying DataItem will be removed too.
93 
95 {
96  auto dataItem = graph.dataItem();
97 
98  removeItem(graph.parent(), graph.tagRow());
99  removeItem(dataItem->parent(), dataItem->tagRow());
100 }
101 
102 //! Remove canvas with all its graphs.
103 
105 {
106  // Remove graph first. Use special method for that, since we want to remove underlying items.
107  for (auto graph : canvas.graphItems())
108  removeGraph(*graph);
109  removeItem(canvas.parent(), canvas.tagRow());
110 }
111 
112 //! Merge canvas from the vector. All graphs will be the children of the first canvas in the vector.
113 //! All other canvas will be emptied and deleted.
114 
115 void ExperimentalDataModel::mergeCanvases(const std::vector<CanvasItem*>& canvases)
116 {
117  if (canvases.size() <= 1)
118  return;
119 
120  CanvasItem* target = canvases.front();
121  for (auto it = std::next(canvases.begin()); it < canvases.end(); ++it) {
122  CanvasItem* source = (*it);
123  for (auto graph : source->graphItems())
124  moveItem(graph, target, {"", -1});
125  removeItem(source->parent(), source->tagRow());
126  }
127 }
128 
130 {
131  setItemCatalogue(CreateItemCatalogue());
132 
133  insertItem<ExperimentalDataContainerItem>(rootItem());
134  insertItem<CanvasContainerItem>(rootItem());
135 
136  setUndoRedoEnabled(true);
137 }
138 
139 } // namespace gui2
Defines class CLASS?
One-dimensional graph representation of Data1DItem.
Definition: graphitem.h:29
Data1DItem * dataItem() const
Returns data item linked to the given GraphItem.
Definition: graphitem.cpp:54
std::vector< GraphItem * > graphItems() const
Returns the selected graph items.
Item to represent pointwise axis.
Definition: axisitems.h:94
SessionItem * parent() const
Returns parent item. Will return nullptr if item doesn't have a parent.
TagRow tagRow() const
Returns TagRow of this item under which it is accessible through its parent.
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
void setUndoRedoEnabled(bool value)
Sets undo/redo either enabled or disabled. By default undo/redo is disabled.
void moveItem(SessionItem *item, SessionItem *new_parent, const TagRow &tagrow)
Move item from it's current parent to a new parent under given tag and row.
void setItemCatalogue(std::unique_ptr< ItemCatalogue > catalogue)
Sets brand new catalog of user-defined items.
SessionItem * rootItem() const
Returns root item of the model.
Variant data(SessionItem *item, int role) const
Returns the data for given item and role.
void removeItem(SessionItem *parent, const TagRow &tagrow)
Removes given row from parent.
Holds a collection of CanvasItem.
Holds a collection of GraphItem's for simultaneous plotting, as well as all information related to pl...
Holds a collection of Data1DItem's with raw data as imported by the user.
CanvasContainerItem * canvasContainer() const
Returns the canvas container of the model.
void removeCanvas(CanvasItem &canvas)
Remove canvas with all its graphs.
void mergeCanvases(const std::vector< CanvasItem * > &canvases)
Merge canvas from the vector.
void removeGraph(ModelView::GraphItem &graph)
Remove graph from the model. Underlying DataItem will be removed too.
ModelView::GraphItem * addGraph(const GraphImportData &graph_data, CanvasItem &target_canvas)
Adds graph to 'target_canvas' and returns the result.
ExperimentalDataContainerItem * dataContainer() const
Returns the data container of the model.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
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
Raw data to construct GraphItem and Data1DItem's.
std::vector< double > bin_centers
std::vector< double > bin_values
std::string graph_description