BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ComponentProxyStrategy Class Reference

Strategy for ComponentProxyModel which hides extra level of GroupProperty. More...

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

Public Types

using map_t = QMap< QPersistentModelIndex, QPersistentModelIndex >
 

Public Member Functions

void buildModelMap (SessionModel *source, ComponentProxyModel *proxy)
 
void onDataChanged (SessionModel *source, ComponentProxyModel *proxy)
 
const map_tproxySourceParent ()
 
void setRootIndex (const QModelIndex &sourceRootIndex)
 
const map_tsourceToProxy ()
 

Protected Member Functions

QModelIndex createProxyIndex (int nrow, int ncol, void *adata)
 Method to ask proxy to create an index using friendship of ProxyModelStrategy and ComponentProxyModel. More...
 
bool processSourceIndex (const QModelIndex &index)
 Mapping of proxy model indices to indices in source model. More...
 

Protected Attributes

ComponentProxyModelm_proxy
 
QMap< QPersistentModelIndex, QPersistentModelIndex > m_proxySourceParent
 
SessionModelm_source
 
QPersistentModelIndex m_sourceRootIndex
 
QMap< QPersistentModelIndex, QPersistentModelIndex > m_sourceToProxy
 Mapping of proxy model indices to indices of parent in source model. More...
 

Private Member Functions

bool isGroupChildren (SessionItem *item)
 Returns true if item is a children/grandchildrent of some group item. More...
 
bool isNewRootItem (SessionItem *item)
 Returns true if item should become new root item. More...
 
bool isPropertyRelated (SessionItem *item)
 Returns true if item is property related to exclude top level items (ParticleLayout, Particle etc from the tree). More...
 
bool isSubGroup (SessionItem *item)
 Returns true if item is a group property which in turn is inside of another group property. More...
 
int parentVisibleRow (const SessionItem &item)
 
void processDefaultItem (SessionItem *item, const QPersistentModelIndex &sourceIndex)
 
void processGroupItem (SessionItem *item, const QPersistentModelIndex &sourceIndex)
 All properties of current item of group item. More...
 
void processRootItem (SessionItem *item, const QPersistentModelIndex &sourceIndex)
 Makes SessionItem to become the only one item in a tree. More...
 
void processSubGroupItem (SessionItem *item, const QPersistentModelIndex &sourceIndex)
 Process group property which is inside of other group property. More...
 

Detailed Description

Strategy for ComponentProxyModel which hides extra level of GroupProperty.

Definition at line 22 of file ComponentProxyStrategy.h.

Member Typedef Documentation

◆ map_t

using ProxyModelStrategy::map_t = QMap<QPersistentModelIndex, QPersistentModelIndex>
inherited

Definition at line 28 of file ProxyModelStrategy.h.

Member Function Documentation

◆ buildModelMap()

void ProxyModelStrategy::buildModelMap ( SessionModel source,
ComponentProxyModel proxy 
)
inherited

Definition at line 21 of file ProxyModelStrategy.cpp.

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 }
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 > m_proxySourceParent
virtual bool processSourceIndex(const QModelIndex &index)=0
Mapping of proxy model indices to indices in source model.
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

References ModelUtils::iterate_if(), ProxyModelStrategy::m_proxy, ProxyModelStrategy::m_proxySourceParent, ProxyModelStrategy::m_source, ProxyModelStrategy::m_sourceRootIndex, ProxyModelStrategy::m_sourceToProxy, and ProxyModelStrategy::processSourceIndex().

Referenced by onDataChanged().

Here is the call graph for this function:

◆ createProxyIndex()

QModelIndex ProxyModelStrategy::createProxyIndex ( int  nrow,
int  ncol,
void *  adata 
)
protectedinherited

Method to ask proxy to create an index using friendship of ProxyModelStrategy and ComponentProxyModel.

Definition at line 62 of file ProxyModelStrategy.cpp.

63 {
64  ASSERT(m_proxy);
65  return m_proxy->createIndex(nrow, ncol, adata);
66 }
#define ASSERT(condition)
Definition: Assert.h:31

References ASSERT, and ProxyModelStrategy::m_proxy.

Referenced by processDefaultItem(), processGroupItem(), processRootItem(), IndentityProxyStrategy::processSourceIndex(), and processSubGroupItem().

◆ isGroupChildren()

bool ComponentProxyStrategy::isGroupChildren ( SessionItem item)
private

Returns true if item is a children/grandchildrent of some group item.

Definition at line 106 of file ComponentProxyStrategy.cpp.

107 {
108  if (item->parent() && item->parent()->modelType() == "GroupProperty")
109  return true;
110 
111  if (const SessionItem* ancestor = ModelPath::ancestor(item, "GroupProperty")) {
112  if (ancestor != item)
113  return true;
114  }
115 
116  return false;
117 }
SessionItem * parent() const
Returns parent of this item.
Definition: SessionItem.cpp:73
QString modelType() const
Get model type.
const SessionItem * ancestor(const SessionItem *item, const QString &requiredModelType)
Returns ancestor of given modelType for given item.
Definition: ModelPath.cpp:87

References ModelPath::ancestor(), SessionItem::modelType(), and SessionItem::parent().

Referenced by processSourceIndex().

Here is the call graph for this function:

◆ isNewRootItem()

bool ComponentProxyStrategy::isNewRootItem ( SessionItem item)
private

Returns true if item should become new root item.

This is used when we want to show single item (Layer, Particle) on top of the tree.

Definition at line 75 of file ComponentProxyStrategy.cpp.

76 {
77  return item->index() == m_sourceRootIndex;
78 }
QModelIndex index() const
Returns model index of this item.
Definition: SessionItem.cpp:80

References SessionItem::index(), and ProxyModelStrategy::m_sourceRootIndex.

Referenced by processSourceIndex().

Here is the call graph for this function:

◆ isPropertyRelated()

bool ComponentProxyStrategy::isPropertyRelated ( SessionItem item)
private

Returns true if item is property related to exclude top level items (ParticleLayout, Particle etc from the tree).

Definition at line 61 of file ComponentProxyStrategy.cpp.

62 {
63  static QStringList propertyRelated = ComponentUtils::propertyRelatedTypes();
64 
65  if (m_sourceRootIndex.isValid() && item->parent()->index() == m_sourceRootIndex
66  && item->parent()->modelType() != "GroupProperty")
67  return propertyRelated.contains(item->modelType());
68 
69  return true;
70 }
QStringList propertyRelatedTypes()
Returns list of strings representing modelTypes suitable for editing in component editors.

References SessionItem::index(), ProxyModelStrategy::m_sourceRootIndex, SessionItem::modelType(), SessionItem::parent(), and ComponentUtils::propertyRelatedTypes().

Referenced by parentVisibleRow(), and processSourceIndex().

Here is the call graph for this function:

◆ isSubGroup()

bool ComponentProxyStrategy::isSubGroup ( SessionItem item)
private

Returns true if item is a group property which in turn is inside of another group property.

Definition at line 93 of file ComponentProxyStrategy.cpp.

94 {
95  const SessionItem* ancestor = ModelPath::ancestor(item->parent(), "GroupProperty");
96  if (item->modelType() == "GroupProperty" && ancestor
97  && ancestor->modelType() == "GroupProperty") {
98  return true;
99  }
100 
101  return false;
102 }

References ModelPath::ancestor(), SessionItem::modelType(), and SessionItem::parent().

Referenced by processSourceIndex().

Here is the call graph for this function:

◆ onDataChanged()

void ComponentProxyStrategy::onDataChanged ( SessionModel source,
ComponentProxyModel proxy 
)
virtual

Reimplemented from ProxyModelStrategy.

Definition at line 23 of file ComponentProxyStrategy.cpp.

24 {
25  buildModelMap(source, proxy);
26  proxy->layoutChanged();
27 }
void buildModelMap(SessionModel *source, ComponentProxyModel *proxy)

References ProxyModelStrategy::buildModelMap().

Here is the call graph for this function:

◆ parentVisibleRow()

int ComponentProxyStrategy::parentVisibleRow ( const SessionItem item)
private

Definition at line 176 of file ComponentProxyStrategy.cpp.

177 {
178  int result(-1);
179 
180  if (!item.parent() || !item.isVisible())
181  return result;
182 
183  for (auto child : item.parent()->children()) {
184  if (child->isVisible() && isPropertyRelated(child))
185  ++result;
186 
187  if (&item == child)
188  return result;
189  }
190 
191  return result;
192 }
bool isPropertyRelated(SessionItem *item)
Returns true if item is property related to exclude top level items (ParticleLayout,...
bool isVisible() const
QVector< SessionItem * > children() const
Returns vector of all children.

References SessionItem::children(), isPropertyRelated(), SessionItem::isVisible(), and SessionItem::parent().

Referenced by processDefaultItem(), processGroupItem(), and processSubGroupItem().

Here is the call graph for this function:

◆ processDefaultItem()

void ComponentProxyStrategy::processDefaultItem ( SessionItem item,
const QPersistentModelIndex &  sourceIndex 
)
private

Definition at line 157 of file ComponentProxyStrategy.cpp.

159 {
160  ASSERT(item);
161  if (!item->isVisible())
162  return;
163 
164  QPersistentModelIndex proxyIndex =
165  createProxyIndex(parentVisibleRow(*item), sourceIndex.column(), item);
166 
167  m_sourceToProxy.insert(sourceIndex, proxyIndex);
168 
169  QPersistentModelIndex sourceParent;
170  if (sourceIndex.parent().isValid())
171  sourceParent = sourceIndex.parent();
172 
173  m_proxySourceParent.insert(proxyIndex, sourceParent);
174 }
int parentVisibleRow(const SessionItem &item)
QModelIndex createProxyIndex(int nrow, int ncol, void *adata)
Method to ask proxy to create an index using friendship of ProxyModelStrategy and ComponentProxyModel...

References ASSERT, ProxyModelStrategy::createProxyIndex(), SessionItem::isVisible(), ProxyModelStrategy::m_proxySourceParent, ProxyModelStrategy::m_sourceToProxy, and parentVisibleRow().

Referenced by processSourceIndex().

Here is the call graph for this function:

◆ processGroupItem()

void ComponentProxyStrategy::processGroupItem ( SessionItem item,
const QPersistentModelIndex &  sourceIndex 
)
private

All properties of current item of group item.

Definition at line 121 of file ComponentProxyStrategy.cpp.

123 {
124  if (const SessionItem* ancestor = ModelPath::ancestor(item, "GroupProperty")) {
125  if (ancestor == item)
126  return;
127 
128  auto groupItem = dynamic_cast<const GroupItem*>(ancestor);
129  if (item->parent() == groupItem->currentItem()) {
130  QPersistentModelIndex proxyIndex = createProxyIndex(
131  parentVisibleRow(*item), sourceIndex.column(), sourceIndex.internalPointer());
132 
133  m_sourceToProxy.insert(sourceIndex, proxyIndex);
134  m_proxySourceParent.insert(proxyIndex, groupItem->index());
135  }
136  }
137 }

References ModelPath::ancestor(), ProxyModelStrategy::createProxyIndex(), ProxyModelStrategy::m_proxySourceParent, ProxyModelStrategy::m_sourceToProxy, SessionItem::parent(), and parentVisibleRow().

Referenced by processSourceIndex().

Here is the call graph for this function:

◆ processRootItem()

void ComponentProxyStrategy::processRootItem ( SessionItem item,
const QPersistentModelIndex &  sourceIndex 
)
private

Makes SessionItem to become the only one item in a tree.

Definition at line 82 of file ComponentProxyStrategy.cpp.

84 {
85  const int nrows = 0; // invisible root item will contain only single item
86  QPersistentModelIndex proxyIndex = createProxyIndex(nrows, sourceIndex.column(), item);
87  m_sourceToProxy.insert(sourceIndex, proxyIndex);
88  m_proxySourceParent.insert(proxyIndex, QModelIndex()); // new parent will be root
89 }

References ProxyModelStrategy::createProxyIndex(), ProxyModelStrategy::m_proxySourceParent, and ProxyModelStrategy::m_sourceToProxy.

Referenced by processSourceIndex().

Here is the call graph for this function:

◆ processSourceIndex()

bool ComponentProxyStrategy::processSourceIndex ( const QModelIndex &  index)
protectedvirtual

Mapping of proxy model indices to indices in source model.

Implements ProxyModelStrategy.

Definition at line 29 of file ComponentProxyStrategy.cpp.

30 {
31  QPersistentModelIndex sourceIndex = {index};
32 
33  SessionItem* item = m_source->itemForIndex(index);
34 
35  QString tag;
36  if (item->parent())
37  tag = item->parent()->tagFromItem(item);
38 
39  if (!isPropertyRelated(item))
40  return false; // not going to visit non-property items
41 
42  if (isNewRootItem(item)) {
43  processRootItem(item, sourceIndex);
44 
45  } else if (isSubGroup(item)) {
46  processSubGroupItem(item, sourceIndex);
47 
48  } else if (isGroupChildren(item)) {
49  processGroupItem(item, sourceIndex);
50 
51  } else {
52  processDefaultItem(item, sourceIndex);
53  }
54 
55  return true;
56 }
bool isNewRootItem(SessionItem *item)
Returns true if item should become new root item.
void processSubGroupItem(SessionItem *item, const QPersistentModelIndex &sourceIndex)
Process group property which is inside of other group property.
bool isSubGroup(SessionItem *item)
Returns true if item is a group property which in turn is inside of another group property.
void processRootItem(SessionItem *item, const QPersistentModelIndex &sourceIndex)
Makes SessionItem to become the only one item in a tree.
void processGroupItem(SessionItem *item, const QPersistentModelIndex &sourceIndex)
All properties of current item of group item.
void processDefaultItem(SessionItem *item, const QPersistentModelIndex &sourceIndex)
bool isGroupChildren(SessionItem *item)
Returns true if item is a children/grandchildrent of some group item.
QString tagFromItem(const SessionItem *item) const
Returns the tag name of given item when existing.
SessionItem * itemForIndex(const QModelIndex &index) const

References isGroupChildren(), isNewRootItem(), isPropertyRelated(), isSubGroup(), SessionModel::itemForIndex(), ProxyModelStrategy::m_source, SessionItem::parent(), processDefaultItem(), processGroupItem(), processRootItem(), processSubGroupItem(), and SessionItem::tagFromItem().

Here is the call graph for this function:

◆ processSubGroupItem()

void ComponentProxyStrategy::processSubGroupItem ( SessionItem item,
const QPersistentModelIndex &  sourceIndex 
)
private

Process group property which is inside of other group property.

Definition at line 141 of file ComponentProxyStrategy.cpp.

143 {
144  if (const SessionItem* ancestor = ModelPath::ancestor(item->parent(), "GroupProperty")) {
145  auto groupItem = dynamic_cast<const GroupItem*>(ancestor);
146 
147  if (item->parent() == groupItem->currentItem()) {
148  QPersistentModelIndex proxyIndex = createProxyIndex(
149  parentVisibleRow(*item), sourceIndex.column(), sourceIndex.internalPointer());
150 
151  m_sourceToProxy.insert(sourceIndex, proxyIndex);
152  m_proxySourceParent.insert(proxyIndex, groupItem->index());
153  }
154  }
155 }

References ModelPath::ancestor(), ProxyModelStrategy::createProxyIndex(), ProxyModelStrategy::m_proxySourceParent, ProxyModelStrategy::m_sourceToProxy, SessionItem::parent(), and parentVisibleRow().

Referenced by processSourceIndex().

Here is the call graph for this function:

◆ proxySourceParent()

const ProxyModelStrategy::map_t & ProxyModelStrategy::proxySourceParent ( )
inherited

Definition at line 49 of file ProxyModelStrategy.cpp.

50 {
51  return m_proxySourceParent;
52 }

References ProxyModelStrategy::m_proxySourceParent.

◆ setRootIndex()

void ProxyModelStrategy::setRootIndex ( const QModelIndex &  sourceRootIndex)
inherited

Definition at line 54 of file ProxyModelStrategy.cpp.

55 {
56  m_sourceRootIndex = QPersistentModelIndex(sourceRootIndex);
57 }

References ProxyModelStrategy::m_sourceRootIndex.

◆ sourceToProxy()

const ProxyModelStrategy::map_t & ProxyModelStrategy::sourceToProxy ( )
inherited

Definition at line 44 of file ProxyModelStrategy.cpp.

45 {
46  return m_sourceToProxy;
47 }

References ProxyModelStrategy::m_sourceToProxy.

Member Data Documentation

◆ m_proxy

ComponentProxyModel* ProxyModelStrategy::m_proxy
protectedinherited

◆ m_proxySourceParent

QMap<QPersistentModelIndex, QPersistentModelIndex> ProxyModelStrategy::m_proxySourceParent
protectedinherited

◆ m_source

SessionModel* ProxyModelStrategy::m_source
protectedinherited

Definition at line 51 of file ProxyModelStrategy.h.

Referenced by ProxyModelStrategy::buildModelMap(), and processSourceIndex().

◆ m_sourceRootIndex

QPersistentModelIndex ProxyModelStrategy::m_sourceRootIndex
protectedinherited

◆ m_sourceToProxy

QMap<QPersistentModelIndex, QPersistentModelIndex> ProxyModelStrategy::m_sourceToProxy
protectedinherited

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