BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
PropertyRepeater.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/PlotComparison/PropertyRepeater.cpp
6 //! @brief Implements class PropertyRepeater
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 
18 PropertyRepeater::PropertyRepeater(QObject* parent, bool repeat_child_properties)
19  : QObject(parent)
20  , m_block_repeater(false)
21  , m_repeat_child_properties(repeat_child_properties)
22 {
23 }
24 
26 {
27  for (auto* item : m_dataItems)
28  item->mapper()->unsubscribe(this);
29 }
30 
32 {
33  if (!sessionItem || m_dataItems.contains(sessionItem))
34  return;
35 
36  sessionItem->mapper()->setOnItemDestroy(
37  [this](SessionItem* item) { m_dataItems.removeAll(item); }, this);
38 
39  sessionItem->mapper()->setOnPropertyChange(
40  [this](SessionItem* item, const QString& name) { onPropertyChanged(item, name); }, this);
41 
43  sessionItem->mapper()->setOnChildPropertyChange(
44  [this](SessionItem* item, const QString& name) {
46  },
47  this);
48  }
49  m_dataItems.push_back(sessionItem);
50 }
51 
53 {
54  for (auto* item : m_dataItems)
55  item->mapper()->unsubscribe(this);
56 
57  m_dataItems.clear();
58 }
59 
60 void PropertyRepeater::setActive(bool isActive)
61 {
62  m_block_repeater = !isActive;
63 }
64 
65 void PropertyRepeater::onPropertyChanged(SessionItem* item, const QString& propertyName)
66 {
67  if (m_block_repeater)
68  return;
69 
70  m_block_repeater = true;
71 
72  QVariant value = item->getItemValue(propertyName);
73  for (auto* target : targetItems(item))
74  target->setItemValue(propertyName, value);
75  m_block_repeater = false;
76 }
77 
78 void PropertyRepeater::setOnChildPropertyChange(SessionItem* item, const QString& propertyName)
79 {
80  if (m_block_repeater)
81  return;
82 
83  m_block_repeater = true;
84 
85  SessionItem* sourceItem = item->parentItem();
86  QString tag = sourceItem->tagFromItem(item);
87  QVariant value = item->getItemValue(propertyName);
88  for (auto* target : targetItems(sourceItem))
89  target->getItem(tag)->setItemValue(propertyName, value);
90  m_block_repeater = false;
91 }
92 
93 //! Returns list of target items to update their properties.
94 
95 QVector<SessionItem*> PropertyRepeater::targetItems(SessionItem* sourceItem)
96 {
97  QVector<SessionItem*> result = m_dataItems;
98  result.removeAll(sourceItem);
99  return result;
100 }
Defines class IntensityDataItem.
Defines class PropertyRepeater.
void setOnItemDestroy(std::function< void(SessionItem *)> f, const void *caller=nullptr)
Definition: ModelMapper.cpp:67
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=nullptr)
Definition: ModelMapper.cpp:39
void setOnChildPropertyChange(std::function< void(SessionItem *, QString)> f, const void *caller=nullptr)
Calls back on child property change, report childItem and property name.
Definition: ModelMapper.cpp:53
~PropertyRepeater() override
void setActive(bool isActive)
QVector< SessionItem * > m_dataItems
void setOnChildPropertyChange(SessionItem *item, const QString &propertyName)
QVector< SessionItem * > targetItems(SessionItem *sourceItem)
Returns list of target items to update their properties.
void onPropertyChanged(SessionItem *item, const QString &propertyName)
PropertyRepeater(QObject *parent=nullptr, bool repeat_child_properties=false)
void addItem(SessionItem *sessionItem)
Base class for a GUI data item.
Definition: SessionItem.h:204
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
SessionItem * parentItem() const
Returns parent of this item.
Definition: SessionItem.cpp:67
QString tagFromItem(const SessionItem *item) const
Returns the tag name of given item when existing.
QString const & name(EShape k)
Definition: particles.cpp:20