BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ProxyModelStrategy.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/ProxyModelStrategy.cpp
6 //! @brief Implements class ProxyModelStrategy
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 
18 
19 ProxyModelStrategy::ProxyModelStrategy() : m_source(nullptr), m_proxy(nullptr) {}
20 
22 {
23  m_sourceToProxy.clear();
24  m_proxySourceParent.clear();
25  m_source = source;
26  m_proxy = proxy;
27 
28  ModelUtils::iterate_if(m_sourceRootIndex, source, [=](const QModelIndex& index) -> bool {
29  return processSourceIndex(index);
30  });
31 
32  // kind of hack since we have to visit col=1 which has QModelIndex() parent
33  if (m_sourceRootIndex.isValid())
35 }
36 
38 {
39  Q_UNUSED(source);
40  Q_UNUSED(proxy);
41  // we do not expect here change of model layout
42 }
43 
45 {
46  return m_sourceToProxy;
47 }
48 
50 {
51  return m_proxySourceParent;
52 }
53 
54 void ProxyModelStrategy::setRootIndex(const QModelIndex& sourceRootIndex)
55 {
56  m_sourceRootIndex = QPersistentModelIndex(sourceRootIndex);
57 }
58 
59 //! Method to ask proxy to create an index using friendship of ProxyModelStrategy
60 //! and ComponentProxyModel.
61 
62 QModelIndex ProxyModelStrategy::createProxyIndex(int nrow, int ncol, void* adata)
63 {
64  ASSERT(m_proxy);
65  return m_proxy->createIndex(nrow, ncol, adata);
66 }
67 
68 //! Builds one-to-one mapping for source and proxy.
69 
70 bool IndentityProxyStrategy::processSourceIndex(const QModelIndex& index)
71 {
72  QPersistentModelIndex proxyIndex =
73  createProxyIndex(index.row(), index.column(), index.internalPointer());
74  m_sourceToProxy.insert(QPersistentModelIndex(index), proxyIndex);
75 
76  QPersistentModelIndex sourceParent;
77  if (index.parent().isValid())
78  sourceParent = index.parent();
79 
80  m_proxySourceParent.insert(proxyIndex, sourceParent);
81 
82  return true;
83 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines class ComponentProxyModel.
Defines ModelUtils namespace.
Defines class SessionModel.
Proxy model to adjust SessionModel for component editor (right bottom corner of SampleView and simila...
bool processSourceIndex(const QModelIndex &index)
Builds one-to-one mapping for source and proxy.
const map_t & proxySourceParent()
QMap< QPersistentModelIndex, QPersistentModelIndex > m_sourceToProxy
Mapping of proxy model indices to indices of parent in source model.
QPersistentModelIndex m_sourceRootIndex
SessionModel * m_source
ComponentProxyModel * m_proxy
QMap< QPersistentModelIndex, QPersistentModelIndex > map_t
QMap< QPersistentModelIndex, QPersistentModelIndex > m_proxySourceParent
QModelIndex createProxyIndex(int nrow, int ncol, void *adata)
Method to ask proxy to create an index using friendship of ProxyModelStrategy and ComponentProxyModel...
const map_t & sourceToProxy()
void buildModelMap(SessionModel *source, ComponentProxyModel *proxy)
void setRootIndex(const QModelIndex &sourceRootIndex)
virtual bool processSourceIndex(const QModelIndex &index)=0
Mapping of proxy model indices to indices in source model.
virtual void onDataChanged(SessionModel *source, ComponentProxyModel *proxy)
void iterate_if(const QModelIndex &index, const QAbstractItemModel *model, const std::function< bool(const QModelIndex &)> &fun)
Iterates through all model indices and calls user function.
Definition: ModelUtils.cpp:47