BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelMapper Class Reference

Description

Definition at line 24 of file ModelMapper.h.

Inheritance diagram for ModelMapper:
[legend]
Collaboration diagram for ModelMapper:
[legend]

Public Slots

void onBeginRemoveRows (const QModelIndex &parent, int first, int last)
 
void onDataChanged (const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles={})
 
void onRowRemoved (const QModelIndex &parent, int first, int last)
 
void onRowsInserted (const QModelIndex &parent, int first, int last)
 

Public Member Functions

 ModelMapper (QObject *parent=nullptr)
 
void callOnItemDestroy ()
 Notifies subscribers if an item owning given mapper is about to be destroyed. More...
 
void setActive (bool state)
 
void setItem (SessionItem *item)
 
void setOnAboutToRemoveChild (std::function< void(SessionItem *)> f, const void *caller=nullptr)
 
void setOnChildPropertyChange (std::function< void(SessionItem *, QString)> f, const void *caller=nullptr)
 Calls back on child property change, report childItem and property name. More...
 
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 case when number of children has decreased. More...
 
void setOnItemDestroy (std::function< void(SessionItem *)> f, const void *caller=nullptr)
 
void setOnPropertyChange (std::function< void(QString)> f, const void *caller=nullptr)
 
void setOnPropertyChange (std::function< void(SessionItem *, QString)> f, const void *caller=nullptr)
 
void setOnValueChange (std::function< void(void)> f, const void *caller=nullptr)
 
void unsubscribe (const void *caller)
 Cancels all subscriptions of given caller. More...
 

Private Types

using call_item_str_t = std::pair< std::function< void(SessionItem *, QString)>, const void * >
 
using call_item_t = std::pair< std::function< void(SessionItem *)>, const void * >
 
using call_str_t = std::pair< std::function< void(QString)>, const void * >
 
using call_t = std::pair< std::function< void(void)>, const void * >
 

Private Member Functions

void callOnAboutToRemoveChild (SessionItem *item)
 
void callOnChildPropertyChange (SessionItem *item, const QString &name)
 
void callOnChildrenChange (SessionItem *item)
 
void callOnPropertyChange (const QString &name)
 
void callOnValueChange ()
 
template<class U >
void clean_container (U &v, const void *caller)
 removes all callbacks related to given caller More...
 
void clearMapper ()
 
int nestlingDepth (SessionItem *item, int level=0)
 
void setModel (SessionModel *model)
 

Private Attributes

QModelIndex m_aboutToDelete
 
bool m_active
 
SessionItemm_item
 
SessionModelm_model
 
std::vector< call_item_tm_onAboutToRemoveChild
 
std::vector< call_item_str_tm_onChildPropertyChange
 
std::vector< call_item_tm_onChildrenChange
 
std::vector< call_item_tm_onItemDestroy
 
std::vector< call_item_str_tm_onPropertyChange
 
std::vector< call_tm_onValueChange
 

Member Typedef Documentation

◆ call_item_str_t

using ModelMapper::call_item_str_t = std::pair<std::function<void(SessionItem*, QString)>, const void*>
private

Definition at line 86 of file ModelMapper.h.

◆ call_item_t

using ModelMapper::call_item_t = std::pair<std::function<void(SessionItem*)>, const void*>
private

Definition at line 85 of file ModelMapper.h.

◆ call_str_t

using ModelMapper::call_str_t = std::pair<std::function<void(QString)>, const void*>
private

Definition at line 84 of file ModelMapper.h.

◆ call_t

using ModelMapper::call_t = std::pair<std::function<void(void)>, const void*>
private

Definition at line 83 of file ModelMapper.h.

Constructor & Destructor Documentation

◆ ModelMapper()

ModelMapper::ModelMapper ( QObject *  parent = nullptr)

Definition at line 18 of file ModelMapper.cpp.

19  : QObject(parent)
20  , m_active(true)
21  , m_model(nullptr)
22  , m_item(nullptr)
23 {
24 }
SessionModel * m_model
Definition: ModelMapper.h:80
bool m_active
Definition: ModelMapper.h:79
SessionItem * m_item
Definition: ModelMapper.h:81

Member Function Documentation

◆ callOnAboutToRemoveChild()

void ModelMapper::callOnAboutToRemoveChild ( SessionItem item)
private

Definition at line 145 of file ModelMapper.cpp.

146 {
147  if (m_active)
148  for (const auto& f : m_onAboutToRemoveChild)
149  f.first(item);
150 }
std::vector< call_item_t > m_onAboutToRemoveChild
Definition: ModelMapper.h:93

References m_active, and m_onAboutToRemoveChild.

Referenced by onBeginRemoveRows().

◆ callOnChildPropertyChange()

void ModelMapper::callOnChildPropertyChange ( SessionItem item,
const QString &  name 
)
private

Definition at line 131 of file ModelMapper.cpp.

132 {
133  if (m_active)
134  for (const auto& f : m_onChildPropertyChange)
135  f.first(item, name);
136 }
std::vector< call_item_str_t > m_onChildPropertyChange
Definition: ModelMapper.h:90
QString const & name(EShape k)
Definition: particles.cpp:20

References m_active, m_onChildPropertyChange, and GUI::RealSpace::Particles::name().

Referenced by onDataChanged().

Here is the call graph for this function:

◆ callOnChildrenChange()

void ModelMapper::callOnChildrenChange ( SessionItem item)
private

Definition at line 138 of file ModelMapper.cpp.

139 {
140  if (m_active)
141  for (const auto& f : m_onChildrenChange)
142  f.first(item);
143 }
std::vector< call_item_t > m_onChildrenChange
Definition: ModelMapper.h:91

References m_active, and m_onChildrenChange.

Referenced by onRowRemoved(), and onRowsInserted().

◆ callOnItemDestroy()

void ModelMapper::callOnItemDestroy ( )

Notifies subscribers if an item owning given mapper is about to be destroyed.

Definition at line 153 of file ModelMapper.cpp.

154 {
155  if (m_active)
156  for (const auto& f : m_onItemDestroy)
157  f.first(m_item);
158 }
std::vector< call_item_t > m_onItemDestroy
Definition: ModelMapper.h:92

References m_active, m_item, and m_onItemDestroy.

◆ callOnPropertyChange()

void ModelMapper::callOnPropertyChange ( const QString &  name)
private

Definition at line 124 of file ModelMapper.cpp.

125 {
126  if (m_active)
127  for (const auto& f : m_onPropertyChange)
128  f.first(m_item, name);
129 }
std::vector< call_item_str_t > m_onPropertyChange
Definition: ModelMapper.h:89

References m_active, m_item, m_onPropertyChange, and GUI::RealSpace::Particles::name().

Referenced by onDataChanged().

Here is the call graph for this function:

◆ callOnValueChange()

void ModelMapper::callOnValueChange ( )
private

Definition at line 117 of file ModelMapper.cpp.

118 {
119  if (m_active && !m_onValueChange.empty())
120  for (const auto& f : m_onValueChange)
121  f.first();
122 }
std::vector< call_t > m_onValueChange
Definition: ModelMapper.h:88

References m_active, and m_onValueChange.

Referenced by onDataChanged().

◆ clean_container()

template<class U >
void ModelMapper::clean_container ( U &  v,
const void *  caller 
)
inlineprivate

removes all callbacks related to given caller

Definition at line 98 of file ModelMapper.h.

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 }

Referenced by unsubscribe().

◆ clearMapper()

void ModelMapper::clearMapper ( )
private

Definition at line 160 of file ModelMapper.cpp.

161 {
162  m_item = nullptr;
163  setModel(nullptr);
164  m_onValueChange.clear();
165  m_onPropertyChange.clear();
166  m_onChildPropertyChange.clear();
167  m_onChildrenChange.clear();
168  m_onItemDestroy.clear();
169  m_onAboutToRemoveChild.clear();
170 }
void setModel(SessionModel *model)
Definition: ModelMapper.cpp:88

References m_item, m_onAboutToRemoveChild, m_onChildPropertyChange, m_onChildrenChange, m_onItemDestroy, m_onPropertyChange, m_onValueChange, and setModel().

Referenced by onRowRemoved().

Here is the call graph for this function:

◆ nestlingDepth()

int ModelMapper::nestlingDepth ( SessionItem item,
int  level = 0 
)
private

Definition at line 107 of file ModelMapper.cpp.

108 {
109  if (item == nullptr || item == m_model->rootItem())
110  return -1;
111  if (item == m_item)
112  return level;
113 
114  return nestlingDepth(item->parentItem(), level + 1);
115 }
int nestlingDepth(SessionItem *item, int level=0)
SessionItem * parentItem() const
Returns parent of this item.
Definition: SessionItem.cpp:67
SessionItem * rootItem() const

References m_item, m_model, SessionItem::parentItem(), and SessionModel::rootItem().

Referenced by onBeginRemoveRows(), onDataChanged(), onRowRemoved(), and onRowsInserted().

Here is the call graph for this function:

◆ onBeginRemoveRows

void ModelMapper::onBeginRemoveRows ( const QModelIndex &  parent,
int  first,
int  last 
)
slot

Definition at line 210 of file ModelMapper.cpp.

211 {
212  SessionItem* oldChild = m_model->itemForIndex(m_model->index(first, 0, parent));
213 
214  int nestling = nestlingDepth(m_model->itemForIndex(parent));
215 
216  if (oldChild && nestling == 0)
217  callOnAboutToRemoveChild(oldChild);
218 
219  if (m_item == oldChild)
220  m_aboutToDelete = m_model->index(first, 0, parent);
221 }
QModelIndex m_aboutToDelete
Definition: ModelMapper.h:94
void callOnAboutToRemoveChild(SessionItem *item)
Base class for a GUI data item.
Definition: SessionItem.h:204
SessionItem * itemForIndex(const QModelIndex &index) const
QModelIndex index(int row, int column, const QModelIndex &parent) const override

References callOnAboutToRemoveChild(), SessionModel::index(), SessionModel::itemForIndex(), m_aboutToDelete, m_item, m_model, and nestlingDepth().

Referenced by setModel().

Here is the call graph for this function:

◆ onDataChanged

void ModelMapper::onDataChanged ( const QModelIndex &  topLeft,
const QModelIndex &  bottomRight,
const QVector< int > &  roles = {} 
)
slot

Definition at line 172 of file ModelMapper.cpp.

174 {
175  if (topLeft.parent() != bottomRight.parent())
176  return; // range must be from the same parent
177 
178  SessionItem* item = m_model->itemForIndex(topLeft);
179 
180  int nestling = nestlingDepth(item);
181  if (nestling == 0)
183 
184  if (nestling == 1) {
185  // something happened with our children
186  if (SessionItem* item = m_model->itemForIndex(topLeft)) {
187  // only care f
188  const QString tag = m_item->tagFromItem(item);
189  if (!tag.isEmpty())
191  }
192  }
193  if (nestling > 1) {
194  if (SessionItem* parent = item->parentItem()) {
195  const QString tag = parent->tagFromItem(item);
196  if (!tag.isEmpty())
197  callOnChildPropertyChange(parent, tag);
198  }
199  }
200 }
void callOnPropertyChange(const QString &name)
void callOnChildPropertyChange(SessionItem *item, const QString &name)
void callOnValueChange()
QString tagFromItem(const SessionItem *item) const
Returns the tag name of given item when existing.

References callOnChildPropertyChange(), callOnPropertyChange(), callOnValueChange(), SessionModel::itemForIndex(), m_item, m_model, nestlingDepth(), SessionItem::parentItem(), and SessionItem::tagFromItem().

Referenced by setModel().

Here is the call graph for this function:

◆ onRowRemoved

void ModelMapper::onRowRemoved ( const QModelIndex &  parent,
int  first,
int  last 
)
slot

Definition at line 223 of file ModelMapper.cpp.

224 {
225  if (nestlingDepth(m_model->itemForIndex(parent)) == 0)
226  callOnChildrenChange(nullptr);
227 
228  if (m_aboutToDelete.isValid() && m_aboutToDelete == m_model->index(first, 0, parent))
229  clearMapper();
230 }
void clearMapper()
void callOnChildrenChange(SessionItem *item)

References callOnChildrenChange(), clearMapper(), SessionModel::index(), SessionModel::itemForIndex(), m_aboutToDelete, m_model, and nestlingDepth().

Referenced by setModel().

Here is the call graph for this function:

◆ onRowsInserted

void ModelMapper::onRowsInserted ( const QModelIndex &  parent,
int  first,
int  last 
)
slot

Definition at line 202 of file ModelMapper.cpp.

203 {
204  SessionItem* newChild = m_model->itemForIndex(m_model->index(first, 0, parent));
205 
206  if (nestlingDepth(newChild) == 1)
207  callOnChildrenChange(newChild);
208 }

References callOnChildrenChange(), SessionModel::index(), SessionModel::itemForIndex(), m_model, and nestlingDepth().

Referenced by setModel().

Here is the call graph for this function:

◆ setActive()

void ModelMapper::setActive ( bool  state)
inline

Definition at line 43 of file ModelMapper.h.

43 { m_active = state; }

References m_active.

◆ setItem()

void ModelMapper::setItem ( SessionItem item)

Definition at line 26 of file ModelMapper.cpp.

27 {
28  if (item) {
29  m_item = item;
30  setModel(item->model());
31  }
32 }
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:60

References m_item, SessionItem::model(), and setModel().

Here is the call graph for this function:

◆ setModel()

void ModelMapper::setModel ( SessionModel model)
private

Definition at line 88 of file ModelMapper.cpp.

89 {
90  if (m_model) {
91  disconnect(m_model, &SessionModel::dataChanged, this, &ModelMapper::onDataChanged);
92  disconnect(m_model, &SessionModel::rowsInserted, this, &ModelMapper::onRowsInserted);
93  disconnect(m_model, &SessionModel::rowsAboutToBeRemoved, this,
95  disconnect(m_model, &SessionModel::rowsRemoved, this, &ModelMapper::onRowRemoved);
96  }
97  m_model = model;
98  if (m_model) {
99  connect(m_model, &SessionModel::dataChanged, this, &ModelMapper::onDataChanged);
100  connect(m_model, &SessionModel::rowsInserted, this, &ModelMapper::onRowsInserted);
101  connect(m_model, &SessionModel::rowsAboutToBeRemoved, this,
103  connect(m_model, &SessionModel::rowsRemoved, this, &ModelMapper::onRowRemoved);
104  }
105 }
void onBeginRemoveRows(const QModelIndex &parent, int first, int last)
void onRowRemoved(const QModelIndex &parent, int first, int last)
void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles={})
void onRowsInserted(const QModelIndex &parent, int first, int last)

References m_model, onBeginRemoveRows(), onDataChanged(), onRowRemoved(), and onRowsInserted().

Referenced by clearMapper(), and setItem().

Here is the call graph for this function:

◆ setOnAboutToRemoveChild()

void ModelMapper::setOnAboutToRemoveChild ( std::function< void(SessionItem *)>  f,
const void *  caller = nullptr 
)

Definition at line 72 of file ModelMapper.cpp.

73 {
74  m_onAboutToRemoveChild.emplace_back(f, caller);
75 }

References m_onAboutToRemoveChild.

Referenced by ProjectionsPlot::subscribeToItem().

◆ setOnChildPropertyChange()

void ModelMapper::setOnChildPropertyChange ( std::function< void(SessionItem *, QString)>  f,
const void *  caller = nullptr 
)

◆ setOnChildrenChange()

void ModelMapper::setOnChildrenChange ( std::function< void(SessionItem *)>  f,
const void *  caller = nullptr 
)

Calls back when number of children has changed, reports newChild. newChild == nullptr denotes the case when number of children has decreased.

Definition at line 62 of file ModelMapper.cpp.

63 {
64  m_onChildrenChange.emplace_back(f, caller);
65 }

References m_onChildrenChange.

Referenced by ProjectionsPlot::subscribeToItem().

◆ setOnItemDestroy()

void ModelMapper::setOnItemDestroy ( std::function< void(SessionItem *)>  f,
const void *  caller = nullptr 
)

◆ setOnPropertyChange() [1/2]

◆ setOnPropertyChange() [2/2]

void ModelMapper::setOnPropertyChange ( std::function< void(SessionItem *, QString)>  f,
const void *  caller = nullptr 
)

Definition at line 45 of file ModelMapper.cpp.

47 {
48  m_onPropertyChange.emplace_back(f, caller);
49 }

References m_onPropertyChange.

◆ setOnValueChange()

void ModelMapper::setOnValueChange ( std::function< void(void)>  f,
const void *  caller = nullptr 
)

Definition at line 34 of file ModelMapper.cpp.

35 {
36  m_onValueChange.emplace_back(f, caller);
37 }

References m_onValueChange.

Referenced by GroupItem::GroupItem(), SpecularPlot::subscribeToItem(), ColorMap::subscribeToItem(), and ProjectionsPlot::subscribeToItem().

◆ unsubscribe()

Member Data Documentation

◆ m_aboutToDelete

QModelIndex ModelMapper::m_aboutToDelete
private

Definition at line 94 of file ModelMapper.h.

Referenced by onBeginRemoveRows(), and onRowRemoved().

◆ m_active

◆ m_item

SessionItem* ModelMapper::m_item
private

◆ m_model

SessionModel* ModelMapper::m_model
private

◆ m_onAboutToRemoveChild

std::vector<call_item_t> ModelMapper::m_onAboutToRemoveChild
private

◆ m_onChildPropertyChange

std::vector<call_item_str_t> ModelMapper::m_onChildPropertyChange
private

◆ m_onChildrenChange

std::vector<call_item_t> ModelMapper::m_onChildrenChange
private

Definition at line 91 of file ModelMapper.h.

Referenced by callOnChildrenChange(), clearMapper(), setOnChildrenChange(), and unsubscribe().

◆ m_onItemDestroy

std::vector<call_item_t> ModelMapper::m_onItemDestroy
private

Definition at line 92 of file ModelMapper.h.

Referenced by callOnItemDestroy(), clearMapper(), setOnItemDestroy(), and unsubscribe().

◆ m_onPropertyChange

std::vector<call_item_str_t> ModelMapper::m_onPropertyChange
private

Definition at line 89 of file ModelMapper.h.

Referenced by callOnPropertyChange(), clearMapper(), setOnPropertyChange(), and unsubscribe().

◆ m_onValueChange

std::vector<call_t> ModelMapper::m_onValueChange
private

Definition at line 88 of file ModelMapper.h.

Referenced by callOnValueChange(), clearMapper(), setOnValueChange(), and unsubscribe().


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