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

Proxy model to adjust SessionModel for component editor (right bottom corner of SampleView and similar). More...

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

Public Member Functions

 ComponentProxyModel (QObject *parent=nullptr)
 
int columnCount (const QModelIndex &parent={}) const
 
bool hasChildren (const QModelIndex &parent) const
 
QModelIndex index (int row, int column, const QModelIndex &parent={}) const
 
QModelIndex mapFromSource (const QModelIndex &sourceIndex) const
 
QModelIndex mapToSource (const QModelIndex &proxyIndex) const
 
QModelIndex parent (const QModelIndex &child) const
 
int rowCount (const QModelIndex &parent={}) const
 
void setProxyStrategy (ProxyModelStrategy *strategy)
 
void setRootIndex (const QModelIndex &sourceRootIndex)
 
void setSessionModel (SessionModel *model)
 

Private Slots

void sourceDataChanged (const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles={})
 
void sourceRowsInserted (const QModelIndex &parent, int start, int end)
 
void sourceRowsRemoved (const QModelIndex &parent, int start, int end)
 

Private Member Functions

void buildModelMap ()
 Main method to build the map of persistent indeses. More...
 
void updateModelMap ()
 

Private Attributes

SessionModelm_model
 
std::unique_ptr< ProxyModelStrategym_proxyStrategy
 

Friends

class ProxyModelStrategy
 

Detailed Description

Proxy model to adjust SessionModel for component editor (right bottom corner of SampleView and similar).

The model hides all SessionItems which are not PropertyItems. The model hides GroupPropertyItem children and shows grand-children of currently selected item one level up.

Definition at line 33 of file ComponentProxyModel.h.

Constructor & Destructor Documentation

◆ ComponentProxyModel()

ComponentProxyModel::ComponentProxyModel ( QObject *  parent = nullptr)

Definition at line 22 of file ComponentProxyModel.cpp.

23  : QAbstractProxyModel(parent)
24  , m_model(nullptr)
25  // , m_proxyStrategy(new IndentityProxyStrategy)
27 {
28 }
std::unique_ptr< ProxyModelStrategy > m_proxyStrategy
QModelIndex parent(const QModelIndex &child) const
Strategy for ComponentProxyModel which hides extra level of GroupProperty.

Member Function Documentation

◆ buildModelMap()

void ComponentProxyModel::buildModelMap ( )
private

Main method to build the map of persistent indeses.

Definition at line 178 of file ComponentProxyModel.cpp.

179 {
180  if (!m_model)
181  return;
182  m_proxyStrategy->buildModelMap(m_model, this);
183  layoutChanged();
184 }

References m_model, and m_proxyStrategy.

Referenced by setProxyStrategy(), setRootIndex(), setSessionModel(), sourceRowsInserted(), and sourceRowsRemoved().

◆ columnCount()

int ComponentProxyModel::columnCount ( const QModelIndex &  parent = {}) const

Definition at line 130 of file ComponentProxyModel.cpp.

131 {
132  if (parent.isValid() && parent.column() != 0)
133  return 0;
135 }

References SessionFlags::MAX_COLUMNS, and parent().

Here is the call graph for this function:

◆ hasChildren()

bool ComponentProxyModel::hasChildren ( const QModelIndex &  parent) const

Definition at line 137 of file ComponentProxyModel.cpp.

138 {
139  QModelIndex source_parent = mapToSource(parent);
140  if (parent.isValid() && !source_parent.isValid())
141  return false;
142 
143  return rowCount(parent) != 0;
144 }
int rowCount(const QModelIndex &parent={}) const
QModelIndex mapToSource(const QModelIndex &proxyIndex) const

References mapToSource(), parent(), and rowCount().

Here is the call graph for this function:

◆ index()

QModelIndex ComponentProxyModel::index ( int  row,
int  column,
const QModelIndex &  parent = {} 
) const

Definition at line 88 of file ComponentProxyModel.cpp.

89 {
90  QModelIndex sourceParent;
91  if (parent.isValid())
92  sourceParent = mapToSource(parent);
93 
94  QMapIterator<QPersistentModelIndex, QPersistentModelIndex> it(
95  m_proxyStrategy->proxySourceParent());
96  while (it.hasNext()) {
97  it.next();
98  if (it.value() == sourceParent && it.key().row() == row && it.key().column() == column)
99  return it.key();
100  }
101  return QModelIndex();
102 }

References m_proxyStrategy, mapToSource(), and parent().

Here is the call graph for this function:

◆ mapFromSource()

QModelIndex ComponentProxyModel::mapFromSource ( const QModelIndex &  sourceIndex) const

Definition at line 80 of file ComponentProxyModel.cpp.

81 {
82  if (!sourceIndex.isValid())
83  return QModelIndex();
84 
85  return m_proxyStrategy->sourceToProxy().value(sourceIndex);
86 }

References m_proxyStrategy.

Referenced by parent(), ComponentTreeView::setRootIndex(), and sourceDataChanged().

◆ mapToSource()

QModelIndex ComponentProxyModel::mapToSource ( const QModelIndex &  proxyIndex) const

Definition at line 72 of file ComponentProxyModel.cpp.

73 {
74  if (!proxyIndex.isValid())
75  return QModelIndex();
76 
77  return m_proxyStrategy->sourceToProxy().key(proxyIndex);
78 }

References m_proxyStrategy.

Referenced by hasChildren(), index(), ComponentTreeView::onCustomContextMenuRequested(), and rowCount().

◆ parent()

QModelIndex ComponentProxyModel::parent ( const QModelIndex &  child) const

Definition at line 104 of file ComponentProxyModel.cpp.

105 {
106  QModelIndex sourceParent = m_proxyStrategy->proxySourceParent().value(child);
107  if (sourceParent.isValid())
108  return mapFromSource(sourceParent);
109 
110  return QModelIndex();
111 }
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const

References m_proxyStrategy, and mapFromSource().

Referenced by columnCount(), hasChildren(), index(), rowCount(), sourceRowsInserted(), and sourceRowsRemoved().

Here is the call graph for this function:

◆ rowCount()

int ComponentProxyModel::rowCount ( const QModelIndex &  parent = {}) const

Definition at line 113 of file ComponentProxyModel.cpp.

114 {
115  QModelIndex sourceParent;
116  if (parent.isValid())
117  sourceParent = mapToSource(parent);
118  QMapIterator<QPersistentModelIndex, QPersistentModelIndex> it(
119  m_proxyStrategy->proxySourceParent());
120 
121  QSet<int> rows;
122  while (it.hasNext()) {
123  it.next();
124  if (it.value() == sourceParent)
125  rows.insert(it.key().row());
126  }
127  return rows.size();
128 }

References m_proxyStrategy, mapToSource(), and parent().

Referenced by hasChildren().

Here is the call graph for this function:

◆ setProxyStrategy()

void ComponentProxyModel::setProxyStrategy ( ProxyModelStrategy strategy)

Definition at line 66 of file ComponentProxyModel.cpp.

67 {
68  m_proxyStrategy.reset(strategy);
69  buildModelMap();
70 }
void buildModelMap()
Main method to build the map of persistent indeses.

References buildModelMap(), and m_proxyStrategy.

Here is the call graph for this function:

◆ setRootIndex()

void ComponentProxyModel::setRootIndex ( const QModelIndex &  sourceRootIndex)

Definition at line 60 of file ComponentProxyModel.cpp.

61 {
62  m_proxyStrategy->setRootIndex(sourceRootIndex);
63  buildModelMap();
64 }

References buildModelMap(), and m_proxyStrategy.

Referenced by ComponentTreeView::setRootIndex().

Here is the call graph for this function:

◆ setSessionModel()

void ComponentProxyModel::setSessionModel ( SessionModel model)

Definition at line 30 of file ComponentProxyModel.cpp.

31 {
32  beginResetModel();
33 
34  if (sourceModel()) {
35  disconnect(sourceModel(), &QAbstractItemModel::dataChanged, this,
37  disconnect(sourceModel(), &QAbstractItemModel::rowsInserted, this,
39  disconnect(sourceModel(), &QAbstractItemModel::rowsRemoved, this,
41  }
42 
43  QAbstractProxyModel::setSourceModel(model);
44 
45  if (sourceModel()) {
46  connect(sourceModel(), &QAbstractItemModel::dataChanged, this,
48  connect(sourceModel(), &QAbstractItemModel::rowsInserted, this,
50  connect(sourceModel(), &QAbstractItemModel::rowsRemoved, this,
52  }
53 
54  endResetModel();
55 
56  m_model = model;
57  buildModelMap();
58 }
void sourceRowsRemoved(const QModelIndex &parent, int start, int end)
void sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles={})
void sourceRowsInserted(const QModelIndex &parent, int start, int end)

References buildModelMap(), m_model, sourceDataChanged(), sourceRowsInserted(), and sourceRowsRemoved().

Referenced by ComponentTreeView::setModel().

Here is the call graph for this function:

◆ sourceDataChanged

void ComponentProxyModel::sourceDataChanged ( const QModelIndex &  topLeft,
const QModelIndex &  bottomRight,
const QVector< int > &  roles = {} 
)
privateslot

Definition at line 146 of file ComponentProxyModel.cpp.

149 {
150  ASSERT(topLeft.isValid() ? topLeft.model() == sourceModel() : true);
151  ASSERT(bottomRight.isValid() ? bottomRight.model() == sourceModel() : true);
152 
153  if (SessionItem* item = m_model->itemForIndex(topLeft)) {
154  if (item->modelType() == "GroupProperty")
155  updateModelMap();
156  }
157  dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight), roles);
158 }
#define ASSERT(condition)
Definition: Assert.h:31
SessionItem * itemForIndex(const QModelIndex &index) const

References ASSERT, SessionModel::itemForIndex(), m_model, mapFromSource(), and updateModelMap().

Referenced by setSessionModel().

Here is the call graph for this function:

◆ sourceRowsInserted

void ComponentProxyModel::sourceRowsInserted ( const QModelIndex &  parent,
int  start,
int  end 
)
privateslot

Definition at line 160 of file ComponentProxyModel.cpp.

161 {
162  Q_UNUSED(parent);
163  Q_UNUSED(start);
164  Q_UNUSED(end);
165  buildModelMap();
166 }

References buildModelMap(), and parent().

Referenced by setSessionModel().

Here is the call graph for this function:

◆ sourceRowsRemoved

void ComponentProxyModel::sourceRowsRemoved ( const QModelIndex &  parent,
int  start,
int  end 
)
privateslot

Definition at line 168 of file ComponentProxyModel.cpp.

169 {
170  Q_UNUSED(parent);
171  Q_UNUSED(start);
172  Q_UNUSED(end);
173  buildModelMap();
174 }

References buildModelMap(), and parent().

Referenced by setSessionModel().

Here is the call graph for this function:

◆ updateModelMap()

void ComponentProxyModel::updateModelMap ( )
private

Definition at line 186 of file ComponentProxyModel.cpp.

187 {
188  m_proxyStrategy->onDataChanged(m_model, this);
189 }

References m_model, and m_proxyStrategy.

Referenced by sourceDataChanged().

Friends And Related Function Documentation

◆ ProxyModelStrategy

friend class ProxyModelStrategy
friend

Definition at line 36 of file ComponentProxyModel.h.

Member Data Documentation

◆ m_model

SessionModel* ComponentProxyModel::m_model
private

◆ m_proxyStrategy

std::unique_ptr<ProxyModelStrategy> ComponentProxyModel::m_proxyStrategy
private

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