BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
materialmodel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/model/materialmodel.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 
21 #include <QColor>
22 
23 using namespace ModelView;
24 
25 namespace {
26 
27 std::unique_ptr<ItemCatalogue> CreateItemCatalogue()
28 {
29  auto result = std::make_unique<ModelView::ItemCatalogue>();
30  result->registerItem<gui2::MaterialContainerItem>();
31  result->registerItem<gui2::SLDMaterialItem>();
32  return result;
33 }
34 
35 const double rho_si = 2.0704e-06;
36 const double mu_si = 5.96e-07;
37 
38 const double rho_default = 9.4245e-06;
39 const double mu_default = 0.0;
40 
41 const std::string air_material_name = "Air";
42 const std::string substrate_material_name = "Si";
43 const std::string default_material_name = "Default";
44 
45 //! Returns map of good looking colors for standard material names.
46 
47 std::map<std::string, QColor> name_to_color_map()
48 {
49  std::map<std::string, QColor> result = {{air_material_name, QColor(179, 242, 255)},
50  {substrate_material_name, QColor(205, 102, 0)},
51  {default_material_name, QColor(Qt::green)}};
52  return result;
53 }
54 
55 QColor suggestMaterialColor(const std::string& name)
56 {
57  static auto color_map = name_to_color_map();
58  auto it = color_map.find(name);
59  return it != color_map.end() ? it->second : Utils::RandomColor();
60 }
61 
62 } // namespace
63 
64 namespace gui2 {
65 
66 MaterialModel::MaterialModel(std::shared_ptr<ModelView::ItemPool> pool)
67  : SessionModel("MaterialModel", pool)
68 {
69  init_model();
70 }
71 
72 //! Returns vector of properties representing possible choice of materials for the given container.
73 //! Here we assume that all materials seats in top level material containers.
74 //! If no container_id was given, the very first container is examined.
75 
76 // TODO Simplify and cover with unit tests.
77 
78 std::vector<ExternalProperty> MaterialModel::material_data(std::string container_id) const
79 {
80  std::vector<ExternalProperty> result;
81  const auto containers = rootItem()->children();
82  if (!containers.empty() && container_id.empty())
83  container_id = topItem<MaterialContainerItem>()->identifier();
84 
85  for (auto container : containers) {
86  if (container->identifier() != container_id)
87  continue;
88  for (auto item : container->children()) {
89  if (auto material = dynamic_cast<MaterialBaseItem*>(item))
90  result.push_back(material->external_property());
91  }
92  }
93  return result;
94 }
95 
96 //! Returns property from given material id.
97 
99 {
100  for (const auto& prop : material_data())
101  if (prop.identifier() == id)
102  return prop;
103 
105 }
106 
107 //! Clones material and adds it at the bottom of MaterialContainerItem.
108 
110 {
111  auto tagrow = item->tagRow().next();
112  return static_cast<MaterialBaseItem*>(SessionModel::copyItem(item, item->parent(), tagrow));
113 }
114 
115 //! Adds default material.
116 
118 {
119  auto material = insertItem<SLDMaterialItem>(materialContainer(), tagrow);
120  material->set_properties("Default", QColor(Qt::green), rho_default, mu_default);
121  return material;
122 }
123 
124 //! Populates the model with some default content.
125 
127 {
128  setItemCatalogue(CreateItemCatalogue());
129 
130  auto container = insertItem<MaterialContainerItem>();
131  auto material = insertItem<SLDMaterialItem>(container);
132  material->set_properties(air_material_name, suggestMaterialColor(air_material_name), 0.0, 0.0);
133  material = insertItem<SLDMaterialItem>(container);
134  material->set_properties(default_material_name, suggestMaterialColor(default_material_name),
135  rho_default, mu_default);
136  material = insertItem<SLDMaterialItem>(container);
137  material->set_properties(substrate_material_name, suggestMaterialColor(substrate_material_name),
138  rho_si, mu_si);
139 }
140 
142 {
143  return topItem<MaterialContainerItem>();
144 }
145 
146 } // namespace gui2
MaterialModel(QObject *parent=nullptr)
Property to carry text, color and identifier.
static ExternalProperty undefined()
SessionItem * parent() const
Returns parent item. Will return nullptr if item doesn't have a parent.
std::vector< SessionItem * > children() const
Returns vector of children formed from all chidlren from all tags.
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
SessionItem * copyItem(const SessionItem *item, SessionItem *parent, const TagRow &tagrow={})
Copy item and insert it in parent's tag and row. Item could belong to any model/parent.
void setItemCatalogue(std::unique_ptr< ItemCatalogue > catalogue)
Sets brand new catalog of user-defined items.
SessionItem * rootItem() const
Returns root item of the model.
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
TagRow next() const
Constructs new tagrow representing next row in given tag.
Definition: tagrow.cpp:20
Base class with all materials with name and color defined.
Definition: materialitems.h:40
Container to hold MaterialItems.
Definition: materialitems.h:32
MaterialContainerItem * materialContainer()
MaterialBaseItem * cloneMaterial(const MaterialBaseItem *item)
Clones material and adds it at the bottom of MaterialContainerItem.
void init_model()
Populates the model with some default content.
std::vector< ModelView::ExternalProperty > material_data(std::string container_id=std::string()) const
Returns vector of properties representing possible choice of materials for the given container.
ModelView::ExternalProperty material_property(const std::string &id)
Returns property from given material id.
SLDMaterialItem * addDefaultMaterial(const ModelView::TagRow &tagrow={})
Adds default material.
Represents material based on scattering length density.
Definition: materialitems.h:57
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
QColor suggestMaterialColor(const QString &name)
MVVM_VIEW_EXPORT QColor RandomColor()
Returns random color.
Definition: widgetutils.cpp:41
materialitems.h Collection of materials to populate MaterialModel.
QString const & name(EShape k)
Definition: particles.cpp:21
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
Defines class CLASS?