BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
itemmapper.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/signals/itemmapper.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 Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "mvvm/model/sessionitem.h"
20 #include <stdexcept>
21 
22 using namespace ModelView;
23 
33 
34  bool m_active{true};
35  SessionItem* m_item{nullptr};
36 
37  ItemMapperImpl(ItemMapper* item_mapper) : m_itemMapper(item_mapper) {}
38 
40  {
48  }
49 
50  int nestlingDepth(SessionItem* item, int level = 0)
51  {
52  if (item == nullptr || item == m_itemMapper->model()->rootItem())
53  return -1;
54  if (item == m_item)
55  return level;
56  return nestlingDepth(item->parent(), level + 1);
57  }
58 
59  //! Processes signals from the model when item data changed.
60 
61  void processDataChange(SessionItem* item, int role)
62  {
63  int nestling = nestlingDepth(item);
64 
65  // own item data changed
66  if (nestling == 0)
67  callOnDataChange(item, role);
68 
69  // data of item's property changed
70  if (nestling == 1)
72 
73  // child property changed
74  if (nestling == 2) {
75  if (auto parent = item->parent())
76  callOnChildPropertyChange(parent, parent->tagRowOfItem(item).tag);
77  }
78  }
79 
80  void processItemInserted(SessionItem* parent, const TagRow& tagrow)
81  {
82  if (parent == m_item)
83  callOnItemInserted(m_item, tagrow);
84  }
85 
86  void processItemRemoved(SessionItem* parent, const TagRow& tagrow)
87  {
88  if (parent == m_item)
89  callOnItemRemoved(m_item, tagrow);
90  }
91 
92  void processAboutToRemoveItem(SessionItem* parent, const TagRow& tagrow)
93  {
94  if (parent == m_item)
96  }
97 
98  //! Notifies all callbacks subscribed to "item data is changed" event.
99 
100  void callOnDataChange(SessionItem* item, int role)
101  {
102  if (m_active)
103  m_on_data_change(item, role);
104  }
105 
106  //! Notifies all callbacks subscribed to "item property is changed" event.
107 
108  void callOnPropertyChange(SessionItem* item, const std::string& property_name)
109  {
110  if (m_active)
111  m_on_property_change(item, property_name);
112  }
113 
114  //! Notifies all callbacks subscribed to "child property changed" event.
115 
116  void callOnChildPropertyChange(SessionItem* item, const std::string& property_name)
117  {
118  if (m_active)
119  m_on_child_property_change(item, property_name);
120  }
121 
122  //! Notifies all callbacks subscribed to "on row inserted" event.
123 
124  void callOnItemInserted(SessionItem* parent, const TagRow& tagrow)
125  {
126  if (m_active)
127  m_on_item_inserted(parent, tagrow);
128  }
129 
130  //! Notifies all callbacks subscribed to "on row removed" event.
131 
132  void callOnItemRemoved(SessionItem* parent, const TagRow& tagrow)
133  {
134  if (m_active)
135  m_on_item_removed(parent, tagrow);
136  }
137 
138  //! Notifies all callbacks subscribed to "on row about to be removed".
139 
140  void callOnAboutToRemoveItem(SessionItem* parent, const TagRow& tagrow)
141  {
142  if (m_active)
143  m_on_about_to_remove_item(parent, tagrow);
144  }
145 };
146 
148  : ModelListener(item->model()), p_impl(std::make_unique<ItemMapperImpl>(this))
149 {
150  if (!item)
151  throw std::runtime_error("ItemMapper::ItemMapper() -> Not initialized item");
152 
153  if (!item->model())
154  throw std::runtime_error("ItemMapper::ItemMapper() -> Item doesn't have model");
155 
156  p_impl->m_item = item;
157 
158  auto on_data_change = [this](auto item, auto role) { p_impl->processDataChange(item, role); };
159  ModelListener::setOnDataChange(on_data_change);
160 
161  auto on_item_inserted = [this](auto item, auto tagrow) {
162  p_impl->processItemInserted(item, tagrow);
163  };
164  ModelListener::setOnItemInserted(on_item_inserted, this);
165 
166  auto on_item_removed = [this](auto item, auto tagrow) {
167  p_impl->processItemRemoved(item, tagrow);
168  };
169  ModelListener::setOnItemRemoved(on_item_removed, this);
170 
171  auto on_about_to_remove_item = [this](auto item, auto tagrow) {
172  p_impl->processAboutToRemoveItem(item, tagrow);
173  };
174  ModelListener::setOnAboutToRemoveItem(on_about_to_remove_item, this);
175 }
176 
177 ItemMapper::~ItemMapper() = default;
178 
180 {
181  p_impl->m_on_item_destroy.connect(std::move(f), owner);
182 }
183 
185 {
186  p_impl->m_on_data_change.connect(std::move(f), owner);
187 }
188 
190 {
191  p_impl->m_on_property_change.connect(std::move(f), owner);
192 }
193 
195 {
196  p_impl->m_on_child_property_change.connect(std::move(f), owner);
197 }
198 
200 {
201  p_impl->m_on_item_inserted.connect(std::move(f), owner);
202 }
203 
205 {
206  p_impl->m_on_item_removed.connect(std::move(f), owner);
207 }
208 
210 {
211  p_impl->m_on_about_to_remove_item.connect(std::move(f), owner);
212 }
213 
215 {
216  p_impl->unsubscribe(client);
217 }
218 
219 //! Sets activity flag to given value. Will disable all callbacks if false.
220 
221 void ItemMapper::setActive(bool value)
222 {
223  p_impl->m_active = value;
224 }
225 
226 //! Calls all callbacks subscribed to "item is destroyed" event.
227 
229 {
230  if (p_impl->m_active)
231  p_impl->m_on_item_destroy(p_impl->m_item);
232 }
Defines class CLASS?
Provides notifications on various changes for a specific item.
Definition: itemmapper.h:32
void setOnItemDestroy(Callbacks::item_t f, Callbacks::slot_t owner) override
Definition: itemmapper.cpp:179
void setActive(bool value)
Sets activity flag to given value. Will disable all callbacks if false.
Definition: itemmapper.cpp:221
void setOnItemRemoved(Callbacks::item_tagrow_t f, Callbacks::slot_t owner) override
Sets callback to be notified on child removal.
Definition: itemmapper.cpp:204
void callOnItemDestroy()
Calls all callbacks subscribed to "item is destroyed" event.
Definition: itemmapper.cpp:228
void setOnAboutToRemoveItem(Callbacks::item_tagrow_t f, Callbacks::slot_t owner) override
Sets callback to be notified when row is about to be removed.
Definition: itemmapper.cpp:209
ItemMapper(SessionItem *item)
Definition: itemmapper.cpp:147
void setOnItemInserted(Callbacks::item_tagrow_t f, Callbacks::slot_t owner) override
Sets callback to be notified on child insertion.
Definition: itemmapper.cpp:199
void unsubscribe(Callbacks::slot_t client) override
Removes given client from all subscriptions.
Definition: itemmapper.cpp:214
std::unique_ptr< ItemMapperImpl > p_impl
Definition: itemmapper.h:53
void setOnDataChange(Callbacks::item_int_t f, Callbacks::slot_t owner) override
Sets callback to be notified on item's data change.
Definition: itemmapper.cpp:184
void setOnPropertyChange(Callbacks::item_str_t f, Callbacks::slot_t owner) override
Sets callback to be notified on item's property change.
Definition: itemmapper.cpp:189
void setOnChildPropertyChange(Callbacks::item_str_t f, Callbacks::slot_t owner) override
Sets callback to be notified on item's children property change.
Definition: itemmapper.cpp:194
void setOnItemRemoved(Callbacks::item_tagrow_t f, Callbacks::slot_t client={}) override
Sets callback to be notified on item remove.
void setOnItemInserted(Callbacks::item_tagrow_t f, Callbacks::slot_t client={}) override
Sets callback to be notified on item insert.
void setOnAboutToRemoveItem(Callbacks::item_tagrow_t f, Callbacks::slot_t client={}) override
Sets callback to be notified when the item is about to be removed.
void setOnDataChange(Callbacks::item_int_t f, Callbacks::slot_t client={}) override
Sets callback to be notified on item's data change.
Templated class for all objects willing to listen for changes in concrete SessionModel.
Definition: modellistener.h:26
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
TagRow tagRowOfItem(const SessionItem *item) const
Returns pair of tag and row corresponding to given item.
SessionItem * parent() const
Returns parent item. Will return nullptr if item doesn't have a parent.
SessionModel * model() const
Returns the model to which given item belongs to.
SessionItem * rootItem() const
Returns root item of the model.
void remove_client(U client)
Remove client from the list to call back.
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
std::string tag
Definition: tagrow.h:27
Defines class CLASS?
Defines class CLASS?
std::function< void(SessionItem *, std::string)> item_str_t
std::function< void(SessionItem *, TagRow)> item_tagrow_t
std::function< void(SessionItem *, int)> item_int_t
std::function< void(SessionItem *)> item_t
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
void callOnItemInserted(SessionItem *parent, const TagRow &tagrow)
Notifies all callbacks subscribed to "on row inserted" event.
Definition: itemmapper.cpp:124
Signal< Callbacks::item_tagrow_t > m_on_about_to_remove_item
Definition: itemmapper.cpp:32
void callOnChildPropertyChange(SessionItem *item, const std::string &property_name)
Notifies all callbacks subscribed to "child property changed" event.
Definition: itemmapper.cpp:116
void processAboutToRemoveItem(SessionItem *parent, const TagRow &tagrow)
Definition: itemmapper.cpp:92
void callOnDataChange(SessionItem *item, int role)
Notifies all callbacks subscribed to "item data is changed" event.
Definition: itemmapper.cpp:100
Signal< Callbacks::item_int_t > m_on_data_change
Definition: itemmapper.cpp:27
Signal< Callbacks::item_tagrow_t > m_on_item_removed
Definition: itemmapper.cpp:31
void callOnItemRemoved(SessionItem *parent, const TagRow &tagrow)
Notifies all callbacks subscribed to "on row removed" event.
Definition: itemmapper.cpp:132
Signal< Callbacks::item_str_t > m_on_child_property_change
Definition: itemmapper.cpp:29
Signal< Callbacks::item_t > m_on_item_destroy
Definition: itemmapper.cpp:26
int nestlingDepth(SessionItem *item, int level=0)
Definition: itemmapper.cpp:50
void callOnPropertyChange(SessionItem *item, const std::string &property_name)
Notifies all callbacks subscribed to "item property is changed" event.
Definition: itemmapper.cpp:108
Signal< Callbacks::item_str_t > m_on_property_change
Definition: itemmapper.cpp:28
void processDataChange(SessionItem *item, int role)
Processes signals from the model when item data changed.
Definition: itemmapper.cpp:61
Signal< Callbacks::item_tagrow_t > m_on_item_inserted
Definition: itemmapper.cpp:30
void unsubscribe(Callbacks::slot_t client)
Definition: itemmapper.cpp:39
void callOnAboutToRemoveItem(SessionItem *parent, const TagRow &tagrow)
Notifies all callbacks subscribed to "on row about to be removed".
Definition: itemmapper.cpp:140
ItemMapperImpl(ItemMapper *item_mapper)
Definition: itemmapper.cpp:37
void processItemInserted(SessionItem *parent, const TagRow &tagrow)
Definition: itemmapper.cpp:80
void processItemRemoved(SessionItem *parent, const TagRow &tagrow)
Definition: itemmapper.cpp:86