BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelMapper.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/BaseItem/ModelMapper.h
6 //! @brief Defines class ModelMapper
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 
15 #ifndef BORNAGAIN_GUI_MODEL_BASEITEM_MODELMAPPER_H
16 #define BORNAGAIN_GUI_MODEL_BASEITEM_MODELMAPPER_H
17 
18 #include <QModelIndex>
19 #include <functional>
20 
21 class SessionModel;
22 class SessionItem;
23 
24 class ModelMapper : public QObject {
25  Q_OBJECT
26 
27 public:
28  ModelMapper(QObject* parent = nullptr);
29 
30  void setItem(SessionItem* item);
31 
32  void setOnValueChange(std::function<void(void)> f, const void* caller = nullptr);
33 
34  void setOnPropertyChange(std::function<void(QString)> f, const void* caller = nullptr);
35  void setOnPropertyChange(std::function<void(SessionItem*, QString)> f,
36  const void* caller = nullptr);
37 
38  void setOnChildPropertyChange(std::function<void(SessionItem*, QString)> f,
39  const void* caller = nullptr);
40 
41  void setOnChildrenChange(std::function<void(SessionItem*)> f, const void* caller = nullptr);
42 
43  void setActive(bool state) { m_active = state; }
44 
45  void setOnItemDestroy(std::function<void(SessionItem*)> f, const void* caller = nullptr);
46 
47  void setOnAboutToRemoveChild(std::function<void(SessionItem*)> f, const void* caller = nullptr);
48 
49  void unsubscribe(const void* caller);
50 
51  void callOnItemDestroy();
52 
53 public slots:
54  void onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight,
55  const QVector<int>& roles = {});
56 
57  void onRowsInserted(const QModelIndex& parent, int first, int last);
58 
59  void onBeginRemoveRows(const QModelIndex& parent, int first, int last);
60 
61  void onRowRemoved(const QModelIndex& parent, int first, int last);
62 
63 private:
64  //! removes all callbacks related to given caller
65  template <class U>
66  void clean_container(U& v, const void* caller);
67 
68  void setModel(SessionModel* model);
69  int nestlingDepth(SessionItem* item, int level = 0);
70 
71  void callOnValueChange();
72  void callOnPropertyChange(const QString& name);
73  void callOnChildPropertyChange(SessionItem* item, const QString& name);
76 
77  void clearMapper();
78 
79  bool m_active;
82 
83  using call_t = std::pair<std::function<void(void)>, const void*>;
84  using call_str_t = std::pair<std::function<void(QString)>, const void*>;
85  using call_item_t = std::pair<std::function<void(SessionItem*)>, const void*>;
86  using call_item_str_t = std::pair<std::function<void(SessionItem*, QString)>, const void*>;
87 
88  std::vector<call_t> m_onValueChange;
89  std::vector<call_item_str_t> m_onPropertyChange;
90  std::vector<call_item_str_t> m_onChildPropertyChange;
91  std::vector<call_item_t> m_onChildrenChange;
92  std::vector<call_item_t> m_onItemDestroy;
93  std::vector<call_item_t> m_onAboutToRemoveChild;
94  QModelIndex m_aboutToDelete;
95 };
96 
97 template <class U>
98 inline void ModelMapper::clean_container(U& v, const void* caller)
99 {
100  v.erase(std::remove_if(
101  v.begin(), v.end(),
102  [caller](typename U::value_type const& x) -> bool { return (x.second == caller); }),
103  v.end());
104 }
105 
106 #endif // BORNAGAIN_GUI_MODEL_BASEITEM_MODELMAPPER_H
ModelMapper(QObject *parent=nullptr)
Definition: ModelMapper.cpp:18
void unsubscribe(const void *caller)
Cancels all subscriptions of given caller.
Definition: ModelMapper.cpp:78
void setOnAboutToRemoveChild(std::function< void(SessionItem *)> f, const void *caller=nullptr)
Definition: ModelMapper.cpp:72
void onBeginRemoveRows(const QModelIndex &parent, int first, int last)
void callOnPropertyChange(const QString &name)
void onRowRemoved(const QModelIndex &parent, int first, int last)
void clearMapper()
void callOnChildPropertyChange(SessionItem *item, const QString &name)
std::pair< std::function< void(SessionItem *, QString)>, const void * > call_item_str_t
Definition: ModelMapper.h:86
void callOnItemDestroy()
Notifies subscribers if an item owning given mapper is about to be destroyed.
void setOnItemDestroy(std::function< void(SessionItem *)> f, const void *caller=nullptr)
Definition: ModelMapper.cpp:67
void setActive(bool state)
Definition: ModelMapper.h:43
void callOnValueChange()
void clean_container(U &v, const void *caller)
removes all callbacks related to given caller
Definition: ModelMapper.h:98
std::pair< std::function< void(SessionItem *)>, const void * > call_item_t
Definition: ModelMapper.h:85
void callOnChildrenChange(SessionItem *item)
void setOnChildrenChange(std::function< void(SessionItem *)> f, const void *caller=nullptr)
Calls back when number of children has changed, reports newChild. newChild == nullptr denotes the cas...
Definition: ModelMapper.cpp:62
void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles={})
std::vector< call_item_t > m_onChildrenChange
Definition: ModelMapper.h:91
std::vector< call_t > m_onValueChange
Definition: ModelMapper.h:88
QModelIndex m_aboutToDelete
Definition: ModelMapper.h:94
void setItem(SessionItem *item)
Definition: ModelMapper.cpp:26
void setModel(SessionModel *model)
Definition: ModelMapper.cpp:88
std::pair< std::function< void(QString)>, const void * > call_str_t
Definition: ModelMapper.h:84
SessionModel * m_model
Definition: ModelMapper.h:80
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=nullptr)
Definition: ModelMapper.cpp:39
std::pair< std::function< void(void)>, const void * > call_t
Definition: ModelMapper.h:83
bool m_active
Definition: ModelMapper.h:79
std::vector< call_item_str_t > m_onPropertyChange
Definition: ModelMapper.h:89
SessionItem * m_item
Definition: ModelMapper.h:81
int nestlingDepth(SessionItem *item, int level=0)
std::vector< call_item_str_t > m_onChildPropertyChange
Definition: ModelMapper.h:90
void callOnAboutToRemoveChild(SessionItem *item)
std::vector< call_item_t > m_onItemDestroy
Definition: ModelMapper.h:92
void onRowsInserted(const QModelIndex &parent, int first, int last)
void setOnValueChange(std::function< void(void)> f, const void *caller=nullptr)
Definition: ModelMapper.cpp:34
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
std::vector< call_item_t > m_onAboutToRemoveChild
Definition: ModelMapper.h:93
Base class for a GUI data item.
Definition: SessionItem.h:204
Base class for a GUI data collection. A collection is e.g. all real data (RealDataModel)....
Definition: SessionModel.h:42
QString const & name(EShape k)
Definition: particles.cpp:20