BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ComponentProxyStrategy.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/ComponentProxyStrategy.cpp
6 //! @brief Implements class ComponentProxyStrategy
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 
22 
24 {
25  buildModelMap(source, proxy);
26  proxy->layoutChanged();
27 }
28 
29 bool ComponentProxyStrategy::processSourceIndex(const QModelIndex& index)
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 }
57 
58 //! Returns true if item is property related to exclude top level items (ParticleLayout, Particle
59 //! etc from the tree).
60 
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 }
71 
72 //! Returns true if item should become new root item.
73 //! This is used when we want to show single item (Layer, Particle) on top of the tree.
74 
76 {
77  return item->index() == m_sourceRootIndex;
78 }
79 
80 //! Makes SessionItem to become the only one item in a tree.
81 
83  const QPersistentModelIndex& sourceIndex)
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 }
90 
91 //! Returns true if item is a group property which in turn is inside of another group property.
92 
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 }
103 
104 //! Returns true if item is a children/grandchildrent of some group item.
105 
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 }
118 
119 //! All properties of current item of group item
120 
122  const QPersistentModelIndex& sourceIndex)
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 }
138 
139 //! Process group property which is inside of other group property.
140 
142  const QPersistentModelIndex& sourceIndex)
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 }
156 
158  const QPersistentModelIndex& sourceIndex)
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 }
175 
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 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines class ComponentProxyModel.
Defines class ComponentProxyStrategy.
Defines ComponentUtils namespace.
Defines class GroupItem.
Defines ModelPath namespace.
Defines namespace SessionItemUtils.
Defines class SessionModel.
Proxy model to adjust SessionModel for component editor (right bottom corner of SampleView and simila...
void onDataChanged(SessionModel *source, ComponentProxyModel *proxy)
bool isNewRootItem(SessionItem *item)
Returns true if item should become new root item.
bool isPropertyRelated(SessionItem *item)
Returns true if item is property related to exclude top level items (ParticleLayout,...
void processSubGroupItem(SessionItem *item, const QPersistentModelIndex &sourceIndex)
Process group property which is inside of other group property.
bool processSourceIndex(const QModelIndex &index)
Mapping of proxy model indices to indices in source model.
bool isSubGroup(SessionItem *item)
Returns true if item is a group property which in turn is inside of another group property.
int parentVisibleRow(const SessionItem &item)
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.
QMap< QPersistentModelIndex, QPersistentModelIndex > m_sourceToProxy
Mapping of proxy model indices to indices of parent in source model.
QPersistentModelIndex m_sourceRootIndex
SessionModel * m_source
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...
void buildModelMap(SessionModel *source, ComponentProxyModel *proxy)
bool isVisible() const
SessionItem * parent() const
Returns parent of this item.
Definition: SessionItem.cpp:73
QVector< SessionItem * > children() const
Returns vector of all children.
QString modelType() const
Get model type.
QString tagFromItem(const SessionItem *item) const
Returns the tag name of given item when existing.
QModelIndex index() const
Returns model index of this item.
Definition: SessionItem.cpp:80
SessionItem * itemForIndex(const QModelIndex &index) const
QStringList propertyRelatedTypes()
Returns list of strings representing modelTypes suitable for editing in component editors.
const SessionItem * ancestor(const SessionItem *item, const QString &requiredModelType)
Returns ancestor of given modelType for given item.
Definition: ModelPath.cpp:87