BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
gui2::SLDElementController Class Reference

The controller of the sld layer visual representation. More...

Inheritance diagram for gui2::SLDElementController:
[legend]
Collaboration diagram for gui2::SLDElementController:
[legend]

Public Types

using layer_ctrl_vec = std::vector< std::unique_ptr< LayerElementController > >
 
using string_vec = std::vector< std::string >
 

Public Member Functions

 SLDElementController (MaterialModel *material_model, SampleModel *sample_model, SLDElementModel *sld_model, GraphicsScene *scene_item)
 Contructor. More...
 
 ~SLDElementController ()
 
void setScene (GraphicsScene *scene)
 Set the scene of the current controller to be passed to the LayerElementControllers. More...
 

Private Member Functions

void buildLayerControllers (string_vec &identifiers)
 Build and set up the layer controllers. More...
 
void buildSLD ()
 Updates all material properties in LayerItems to get new material colors and labels. More...
 
void clearScene ()
 Remove all LayerElementControllers and their items from scene and memory. More...
 
void connectLayerControllers ()
 Connect the layer controllers. More...
 
void connectLayerModel ()
 Connect with signals of SampleModel. More...
 
void connectMaterialModel ()
 Connect with signals of MaterialModel. More...
 
void connectSLDElementModel ()
 Connect with signals of SLDViewModel. More...
 
void disconnectLayerControllers ()
 Disconnect the layer controllers. More...
 
void disconnectLayerModel () const
 Disconnect with signals of SampleModel. More...
 
void disconnectMaterialModel () const
 Disconnect with signals of MaterialModel. More...
 
void disconnectSLDElementModel () const
 Disconnect with signals of SLDViewModel. More...
 
string_vec getIdentifierVector (ModelView::SessionItem *item)
 Get the identifiers of all layeritems in the sample model in order of appearance. More...
 
void updateRoughnessFromView (std::string identifier, double value)
 Update the material and layer models from the view items. More...
 
void updateSLDFromView (std::string identifier, double value)
 Update the material and layer models from the view items. More...
 
void updateThicknessFromView (std::string identifier, double value)
 Update the material and layer models from the view items. More...
 
void updateToView (ModelView::SessionItem *item=nullptr)
 Update the view items with the changes in the material or layer models. More...
 

Private Attributes

layer_ctrl_vec m_layer_controllers
 
MaterialModelp_material_model
 
SampleModelp_sample_model
 
GraphicsScenep_scene_item
 
SLDElementModelp_sld_model
 

Detailed Description

The controller of the sld layer visual representation.

Definition at line 33 of file sldelementcontroller.h.

Member Typedef Documentation

◆ layer_ctrl_vec

Definition at line 38 of file sldelementcontroller.h.

◆ string_vec

using gui2::SLDElementController::string_vec = std::vector<std::string>

Definition at line 37 of file sldelementcontroller.h.

Constructor & Destructor Documentation

◆ SLDElementController()

gui2::SLDElementController::SLDElementController ( MaterialModel material_model,
SampleModel sample_model,
SLDElementModel sld_model,
GraphicsScene scene_item 
)

Contructor.

Definition at line 36 of file sldelementcontroller.cpp.

38  : p_material_model(material_model)
39  , p_sample_model(sample_model)
40  , p_sld_model(sld_model)
41  , p_scene_item(scene_item)
42 {
46  buildSLD();
47 }
void connectMaterialModel()
Connect with signals of MaterialModel.
void connectLayerModel()
Connect with signals of SampleModel.
void connectSLDElementModel()
Connect with signals of SLDViewModel.
void buildSLD()
Updates all material properties in LayerItems to get new material colors and labels.

References buildSLD(), connectLayerModel(), connectMaterialModel(), and connectSLDElementModel().

Here is the call graph for this function:

◆ ~SLDElementController()

gui2::SLDElementController::~SLDElementController ( )

Definition at line 49 of file sldelementcontroller.cpp.

50 {
51  clearScene();
52 }
void clearScene()
Remove all LayerElementControllers and their items from scene and memory.

References clearScene().

Here is the call graph for this function:

Member Function Documentation

◆ buildLayerControllers()

void gui2::SLDElementController::buildLayerControllers ( string_vec identifiers)
private

Build and set up the layer controllers.

Definition at line 181 of file sldelementcontroller.cpp.

182 {
183  if (!p_scene_item)
184  return;
185  if (!p_sld_model)
186  return;
187 
188  for (auto& identifier : identifiers) {
189  auto layer_element_item = p_sld_model->addLayer();
190  auto layer_element_controller =
191  std::make_unique<LayerElementController>(layer_element_item);
192  layer_element_controller->autoPopulate();
193  layer_element_controller->setScene(p_scene_item);
194  layer_element_controller->connectToModel();
195  layer_element_controller->setSampleItemId(identifier);
196  m_layer_controllers.push_back(std::move(layer_element_controller));
197  }
198 
199  for (size_t i = 0; i < m_layer_controllers.size() - 1; ++i) {
200  m_layer_controllers.at(i)->setLayerBelow(m_layer_controllers.at(i + 1).get());
201  }
202 
203  if (m_layer_controllers.size() > 0) {
204  m_layer_controllers.at(0)->topSegment()->stretchRight(true);
205  m_layer_controllers.at(0)->topSegment()->setFlag(QGraphicsItem::ItemIsMovable, false);
206  }
207  if (m_layer_controllers.size() > 1) {
208  m_layer_controllers.at(1)->sideSegment()->setFlag(QGraphicsItem::ItemIsMovable, false);
209  m_layer_controllers.at(m_layer_controllers.size() - 1)->topSegment()->stretchLeft(true);
210  }
211 }
LayerElementItem * addLayer()
Add a layer item.

References gui2::SLDElementModel::addLayer(), m_layer_controllers, p_scene_item, and p_sld_model.

Referenced by buildSLD().

Here is the call graph for this function:

◆ buildSLD()

void gui2::SLDElementController::buildSLD ( )
private

Updates all material properties in LayerItems to get new material colors and labels.

Definition at line 116 of file sldelementcontroller.cpp.

117 {
118  if (!p_sld_model)
119  return;
120  if (!p_sample_model)
121  return;
122  if (!p_material_model)
123  return;
124  if (!p_scene_item)
125  return;
126 
128  clearScene();
129 
130  if (p_sample_model->rootItem()->childrenCount() == 0)
131  return;
133  if (identifiers.size() == 0)
134  return;
135 
136  buildLayerControllers(identifiers);
137  updateToView();
140 }
int childrenCount() const
Returns total number of children in all tags.
std::vector< SessionItem * > children() const
Returns vector of children formed from all chidlren from all tags.
SessionItem * rootItem() const
Returns root item of the model.
void disconnectSLDElementModel() const
Disconnect with signals of SLDViewModel.
string_vec getIdentifierVector(ModelView::SessionItem *item)
Get the identifiers of all layeritems in the sample model in order of appearance.
void connectLayerControllers()
Connect the layer controllers.
std::vector< std::string > string_vec
void updateToView(ModelView::SessionItem *item=nullptr)
Update the view items with the changes in the material or layer models.
void buildLayerControllers(string_vec &identifiers)
Build and set up the layer controllers.

References buildLayerControllers(), ModelView::SessionItem::children(), ModelView::SessionItem::childrenCount(), clearScene(), connectLayerControllers(), connectSLDElementModel(), disconnectSLDElementModel(), getIdentifierVector(), p_material_model, p_sample_model, p_scene_item, p_sld_model, ModelView::SessionModel::rootItem(), and updateToView().

Referenced by SLDElementController(), connectLayerModel(), setScene(), and updateToView().

Here is the call graph for this function:

◆ clearScene()

void gui2::SLDElementController::clearScene ( )
private

Remove all LayerElementControllers and their items from scene and memory.

Definition at line 143 of file sldelementcontroller.cpp.

144 {
145  if (!p_scene_item)
146  return;
147  if (!p_sld_model)
148  return;
149 
150  for (size_t i = 0; i < m_layer_controllers.size(); ++i) {
151  m_layer_controllers.at(i)->disconnectFormModel();
152  m_layer_controllers.at(i)->unsetScene();
153  m_layer_controllers.at(i)->deleteViewItems();
154  }
155  m_layer_controllers.clear();
156  p_sld_model->clear();
157 }
void clear(std::function< void(SessionItem *)> callback={})
Removes all items from the model.

References ModelView::SessionModel::clear(), m_layer_controllers, p_scene_item, and p_sld_model.

Referenced by ~SLDElementController(), and buildSLD().

Here is the call graph for this function:

◆ connectLayerControllers()

void gui2::SLDElementController::connectLayerControllers ( )
private

Connect the layer controllers.

Definition at line 214 of file sldelementcontroller.cpp.

215 {
216  for (const auto& layer_controller : m_layer_controllers) {
217  QObject::connect(layer_controller.get(), &LayerElementController::heightChanged, this,
219  QObject::connect(layer_controller.get(), &LayerElementController::widthChanged, this,
221  QObject::connect(layer_controller.get(), &LayerElementController::roughnessChanged, this,
223  }
224 }
void widthChanged(std::string id, double value) const
void roughnessChanged(std::string id, double value) const
void heightChanged(std::string id, double value) const
void updateThicknessFromView(std::string identifier, double value)
Update the material and layer models from the view items.
void updateRoughnessFromView(std::string identifier, double value)
Update the material and layer models from the view items.
void updateSLDFromView(std::string identifier, double value)
Update the material and layer models from the view items.

References gui2::LayerElementController::heightChanged(), m_layer_controllers, gui2::LayerElementController::roughnessChanged(), updateRoughnessFromView(), updateSLDFromView(), updateThicknessFromView(), and gui2::LayerElementController::widthChanged().

Referenced by buildSLD().

Here is the call graph for this function:

◆ connectLayerModel()

void gui2::SLDElementController::connectLayerModel ( )
private

Connect with signals of SampleModel.

Definition at line 68 of file sldelementcontroller.cpp.

69 {
70  auto on_sam_data_change = [this](SessionItem* item, int) { updateToView(item); };
71  p_sample_model->mapper()->setOnDataChange(on_sam_data_change, this);
72 
73  auto on_sam_item_inserted = [this](SessionItem*, TagRow) { buildSLD(); };
74  p_sample_model->mapper()->setOnItemInserted(on_sam_item_inserted, this);
75 
76  auto on_sam_item_removed = [this](SessionItem*, TagRow) { buildSLD(); };
77  p_sample_model->mapper()->setOnItemRemoved(on_sam_item_removed, this);
78 
79  auto on_sam_model_destroyed = [this](SessionModel*) { p_sample_model = nullptr; };
80  p_sample_model->mapper()->setOnModelDestroyed(on_sam_model_destroyed, this);
81 }
void setOnItemRemoved(Callbacks::item_tagrow_t f, Callbacks::slot_t client) override
Sets callback to be notified on item remove.
Definition: modelmapper.cpp:69
void setOnItemInserted(Callbacks::item_tagrow_t f, Callbacks::slot_t client) override
Sets callback to be notified on item insert.
Definition: modelmapper.cpp:61
void setOnModelDestroyed(Callbacks::model_t f, Callbacks::slot_t client) override
Sets the callback for notifications on model destruction.
Definition: modelmapper.cpp:84
void setOnDataChange(Callbacks::item_int_t f, Callbacks::slot_t client) override
Sets callback to be notified on item's data change.
Definition: modelmapper.cpp:53
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
ModelMapper * mapper()
Returns model mapper. Can be used to subscribe to various model's signal.
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25

References buildSLD(), ModelView::SessionModel::mapper(), p_sample_model, ModelView::ModelMapper::setOnDataChange(), ModelView::ModelMapper::setOnItemInserted(), ModelView::ModelMapper::setOnItemRemoved(), ModelView::ModelMapper::setOnModelDestroyed(), and updateToView().

Referenced by SLDElementController().

Here is the call graph for this function:

◆ connectMaterialModel()

void gui2::SLDElementController::connectMaterialModel ( )
private

Connect with signals of MaterialModel.

Definition at line 55 of file sldelementcontroller.cpp.

56 {
57  auto on_mat_data_change = [this](SessionItem* item, int) { updateToView(item); };
58  p_material_model->mapper()->setOnDataChange(on_mat_data_change, this);
59 
60  auto on_mat_model_destroyed = [this](SessionModel*) { p_material_model = nullptr; };
61  p_material_model->mapper()->setOnModelDestroyed(on_mat_model_destroyed, this);
62 }

References ModelView::SessionModel::mapper(), p_material_model, ModelView::ModelMapper::setOnDataChange(), ModelView::ModelMapper::setOnModelDestroyed(), and updateToView().

Referenced by SLDElementController().

Here is the call graph for this function:

◆ connectSLDElementModel()

void gui2::SLDElementController::connectSLDElementModel ( )
private

Connect with signals of SLDViewModel.

Definition at line 84 of file sldelementcontroller.cpp.

85 {
86  auto on_sld_model_destroyed = [this](SessionModel*) { p_sld_model = nullptr; };
87  p_sld_model->mapper()->setOnModelDestroyed(on_sld_model_destroyed, this);
88 }

References ModelView::SessionModel::mapper(), p_sld_model, and ModelView::ModelMapper::setOnModelDestroyed().

Referenced by SLDElementController(), and buildSLD().

Here is the call graph for this function:

◆ disconnectLayerControllers()

void gui2::SLDElementController::disconnectLayerControllers ( )
private

Disconnect the layer controllers.

Definition at line 227 of file sldelementcontroller.cpp.

228 {
229  for (const auto& layer_controller : m_layer_controllers) {
230  QObject::disconnect(layer_controller.get(), &LayerElementController::heightChanged, this,
232  QObject::disconnect(layer_controller.get(), &LayerElementController::widthChanged, this,
234  QObject::disconnect(layer_controller.get(), &LayerElementController::roughnessChanged, this,
236  }
237 }

References gui2::LayerElementController::heightChanged(), m_layer_controllers, gui2::LayerElementController::roughnessChanged(), updateRoughnessFromView(), updateSLDFromView(), updateThicknessFromView(), and gui2::LayerElementController::widthChanged().

Here is the call graph for this function:

◆ disconnectLayerModel()

void gui2::SLDElementController::disconnectLayerModel ( ) const
private

Disconnect with signals of SampleModel.

Definition at line 97 of file sldelementcontroller.cpp.

98 {
100 }
void unsubscribe(Callbacks::slot_t client) override
Removes given client from all subscriptions.

References ModelView::SessionModel::mapper(), p_sample_model, and ModelView::ModelMapper::unsubscribe().

Here is the call graph for this function:

◆ disconnectMaterialModel()

void gui2::SLDElementController::disconnectMaterialModel ( ) const
private

Disconnect with signals of MaterialModel.

Definition at line 91 of file sldelementcontroller.cpp.

92 {
94 }

References ModelView::SessionModel::mapper(), p_material_model, and ModelView::ModelMapper::unsubscribe().

Here is the call graph for this function:

◆ disconnectSLDElementModel()

void gui2::SLDElementController::disconnectSLDElementModel ( ) const
private

Disconnect with signals of SLDViewModel.

Definition at line 103 of file sldelementcontroller.cpp.

104 {
105  p_sld_model->mapper()->unsubscribe(this);
106 }

References ModelView::SessionModel::mapper(), p_sld_model, and ModelView::ModelMapper::unsubscribe().

Referenced by buildSLD().

Here is the call graph for this function:

◆ getIdentifierVector()

SLDElementController::string_vec gui2::SLDElementController::getIdentifierVector ( ModelView::SessionItem item)
private

Get the identifiers of all layeritems in the sample model in order of appearance.

Definition at line 160 of file sldelementcontroller.cpp.

161 {
162  string_vec output;
163 
164  auto children = item->children();
165  for (int i = 0; i < item->childrenCount(); ++i) {
166  if (dynamic_cast<MultiLayerItem*>(children.at(i))) {
167  auto child = dynamic_cast<MultiLayerItem*>(children.at(i));
168  for (int j = 0; j < child->property<int>(MultiLayerItem::P_NREPETITIONS); ++j) {
169  auto child_output = getIdentifierVector(child);
170  output.insert(output.end(), child_output.begin(), child_output.end());
171  }
172  } else if (dynamic_cast<LayerItem*>(children.at(i))) {
173  auto child = dynamic_cast<LayerItem*>(children.at(i));
174  output.push_back(child->identifier());
175  }
176  }
177  return output;
178 }
static const std::string P_NREPETITIONS
Definition: sampleitems.h:55

References ModelView::SessionItem::children(), ModelView::SessionItem::childrenCount(), and gui2::MultiLayerItem::P_NREPETITIONS.

Referenced by buildSLD().

Here is the call graph for this function:

◆ setScene()

void gui2::SLDElementController::setScene ( GraphicsScene scene)

Set the scene of the current controller to be passed to the LayerElementControllers.

Definition at line 109 of file sldelementcontroller.cpp.

110 {
111  p_scene_item = scene;
112  buildSLD();
113 }

References buildSLD(), and p_scene_item.

Here is the call graph for this function:

◆ updateRoughnessFromView()

void gui2::SLDElementController::updateRoughnessFromView ( std::string  identifier,
double  value 
)
private

Update the material and layer models from the view items.

Definition at line 302 of file sldelementcontroller.cpp.

303 {
304  auto layer_item = dynamic_cast<LayerItem*>(p_sample_model->findItem(identifier));
305  auto roughness_item = layer_item->item<RoughnessItem>(LayerItem::P_ROUGHNESS);
306  roughness_item->setProperty(RoughnessItem::P_SIGMA, value);
307 }
SessionItem * findItem(const identifier_type &id)
Returns SessionItem for given identifier.
T * item(const QString &tag) const
Definition: SessionItem.h:151
static const std::string P_ROUGHNESS
Definition: sampleitems.h:44
static const std::string P_SIGMA
Definition: sampleitems.h:30

References ModelView::SessionModel::findItem(), ModelView::SessionItem::item(), gui2::LayerItem::P_ROUGHNESS, p_sample_model, gui2::RoughnessItem::P_SIGMA, and ModelView::SessionItem::setProperty().

Referenced by connectLayerControllers(), and disconnectLayerControllers().

Here is the call graph for this function:

◆ updateSLDFromView()

void gui2::SLDElementController::updateSLDFromView ( std::string  identifier,
double  value 
)
private

Update the material and layer models from the view items.

Definition at line 292 of file sldelementcontroller.cpp.

293 {
294  auto layer_item = dynamic_cast<LayerItem*>(p_sample_model->findItem(identifier));
295  auto material_item = dynamic_cast<SLDMaterialItem*>(p_material_model->findItem(
296  layer_item->property<ExternalProperty>(LayerItem::P_MATERIAL).identifier()));
297  if (material_item)
298  material_item->setProperty(SLDMaterialItem::P_SLD_REAL, value);
299 }
Property to carry text, color and identifier.
std::string identifier() const
static const std::string P_MATERIAL
Definition: sampleitems.h:42
static const std::string P_SLD_REAL
Definition: materialitems.h:59

References ModelView::SessionModel::findItem(), ModelView::ExternalProperty::identifier(), gui2::LayerItem::P_MATERIAL, p_material_model, p_sample_model, gui2::SLDMaterialItem::P_SLD_REAL, and ModelView::SessionItem::setProperty().

Referenced by connectLayerControllers(), and disconnectLayerControllers().

Here is the call graph for this function:

◆ updateThicknessFromView()

void gui2::SLDElementController::updateThicknessFromView ( std::string  identifier,
double  value 
)
private

Update the material and layer models from the view items.

Definition at line 285 of file sldelementcontroller.cpp.

286 {
287  auto layer_item = dynamic_cast<LayerItem*>(p_sample_model->findItem(identifier));
288  layer_item->setProperty(LayerItem::P_THICKNESS, value);
289 }
static const std::string P_THICKNESS
Definition: sampleitems.h:43

References ModelView::SessionModel::findItem(), p_sample_model, gui2::LayerItem::P_THICKNESS, and ModelView::SessionItem::setProperty().

Referenced by connectLayerControllers(), and disconnectLayerControllers().

Here is the call graph for this function:

◆ updateToView()

void gui2::SLDElementController::updateToView ( ModelView::SessionItem item = nullptr)
private

Update the view items with the changes in the material or layer models.

Definition at line 240 of file sldelementcontroller.cpp.

241 {
242  if (item && dynamic_cast<MultiLayerItem*>(item->parent())) {
243  buildSLD();
244  return;
245  }
246 
247  for (const auto& layer_controller : m_layer_controllers) {
248  auto layer_item =
249  dynamic_cast<LayerItem*>(p_sample_model->findItem(layer_controller->sampleItemId()));
250  if (!layer_item) {
251  buildSLD();
252  return;
253  }
254  auto roughness_item = layer_item->item<RoughnessItem>(LayerItem::P_ROUGHNESS);
255  auto material_item = dynamic_cast<SLDMaterialItem*>(p_material_model->findItem(
256  layer_item->property<ExternalProperty>(LayerItem::P_MATERIAL).identifier()));
257 
258  layer_controller->layerElementItem()->setProperty(
260  roughness_item->property<double>(RoughnessItem::P_SIGMA));
261  layer_controller->layerElementItem()->setProperty(
262  LayerElementItem::P_WIDTH, layer_item->property<double>(LayerItem::P_THICKNESS));
263 
264  if (material_item) {
265  layer_controller->layerElementItem()->setProperty(
267  material_item->property<double>(SLDMaterialItem::P_SLD_REAL));
268  layer_controller->layerElementItem()->setProperty(
270  material_item->property<QColor>(SLDMaterialItem::P_COLOR));
271  layer_controller->layerElementItem()->setProperty(
273  material_item->property<QColor>(SLDMaterialItem::P_COLOR));
274  } else {
275  layer_controller->layerElementItem()->setProperty(LayerElementItem::P_HEIGHT, 1e-6);
276  layer_controller->layerElementItem()->setProperty(LayerElementItem::P_TOP_BRUSH_COLOR,
277  QColor("red"));
278  layer_controller->layerElementItem()->setProperty(LayerElementItem::P_SIDE_BRUSH_COLOR,
279  QColor("red"));
280  }
281  }
282 }
SessionItem * parent() const
Returns parent item. Will return nullptr if item doesn't have a parent.
static const std::string P_SIDE_BRUSH_COLOR
static const std::string P_TOP_BRUSH_COLOR
static const std::string P_HEIGHT
static const std::string P_WIDTH
static const std::string P_ROUGHNESS
static const std::string P_COLOR
Definition: materialitems.h:42

References buildSLD(), ModelView::SessionModel::findItem(), ModelView::ExternalProperty::identifier(), m_layer_controllers, gui2::MaterialBaseItem::P_COLOR, gui2::LayerElementItem::P_HEIGHT, gui2::LayerItem::P_MATERIAL, p_material_model, gui2::LayerItem::P_ROUGHNESS, gui2::LayerElementItem::P_ROUGHNESS, p_sample_model, gui2::LayerElementItem::P_SIDE_BRUSH_COLOR, gui2::RoughnessItem::P_SIGMA, gui2::SLDMaterialItem::P_SLD_REAL, gui2::LayerItem::P_THICKNESS, gui2::LayerElementItem::P_TOP_BRUSH_COLOR, gui2::LayerElementItem::P_WIDTH, ModelView::SessionItem::parent(), ModelView::SessionItem::property(), and ModelView::SessionItem::setProperty().

Referenced by buildSLD(), connectLayerModel(), and connectMaterialModel().

Here is the call graph for this function:

Member Data Documentation

◆ m_layer_controllers

layer_ctrl_vec gui2::SLDElementController::m_layer_controllers
private

◆ p_material_model

MaterialModel* gui2::SLDElementController::p_material_model
private

◆ p_sample_model

SampleModel* gui2::SLDElementController::p_sample_model
private

◆ p_scene_item

GraphicsScene* gui2::SLDElementController::p_scene_item
private

Definition at line 69 of file sldelementcontroller.h.

Referenced by buildLayerControllers(), buildSLD(), clearScene(), and setScene().

◆ p_sld_model

SLDElementModel* gui2::SLDElementController::p_sld_model
private

The documentation for this class was generated from the following files: