KDeclarative

columnproxymodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "columnproxymodel.h"
8 
9 ColumnProxyModel::ColumnProxyModel(QObject* parent)
10  : QAbstractListModel(parent)
11  , m_column(0)
12  , m_sourceModel(nullptr)
13 {}
14 
15 void ColumnProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
16 {
17  if(sourceModel==m_sourceModel) {
18  return;
19  }
20 
21  beginResetModel();
22  if(m_sourceModel) {
23  disconnect(m_sourceModel, SIGNAL(destroyed(QObject*)),
24  this, SLOT(sourceDestroyed(QObject*)));
25 
26  disconnect(m_sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
27  this, SLOT(considerDataChanged(QModelIndex,QModelIndex)));
28  disconnect(m_sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
29  this, SLOT(considerRowsAboutToBeInserted(QModelIndex,int,int)));
30  disconnect(m_sourceModel, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
31  this, SLOT(considerRowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
32  disconnect(m_sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
33  this, SLOT(considerRowsAboutToBeRemoved(QModelIndex,int,int)));
34  disconnect(m_sourceModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
35  this, SLOT(considerRowsInserted(QModelIndex,int,int)));
36  disconnect(m_sourceModel, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
37  this, SLOT(considerRowsMoved(QModelIndex,int,int,QModelIndex,int)));
38  disconnect(m_sourceModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
39  this, SLOT(considerRowsRemoved(QModelIndex,int,int)));
40 
41  disconnect(m_sourceModel, SIGNAL(modelAboutToBeReset()),
42  this, SIGNAL(modelAboutToBeReset()));
43  disconnect(m_sourceModel, SIGNAL(modelReset()),
44  this, SIGNAL(modelReset()));
45  disconnect(m_sourceModel, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
46  this, SIGNAL(headerDataChanged(Qt::Orientation,int,int)));
47  disconnect(m_sourceModel, SIGNAL(layoutAboutToBeChanged()),
48  this, SIGNAL(layoutAboutToBeChanged()));
49  disconnect(m_sourceModel, SIGNAL(layoutChanged()),
50  this, SIGNAL(layoutChanged()));
51  }
52  m_sourceModel = sourceModel;
53  if(m_sourceModel) {
54  connect(m_sourceModel, SIGNAL(destroyed(QObject*)),
55  this, SLOT(sourceDestroyed(QObject*)));
56 
57  connect(m_sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
58  this, SLOT(considerDataChanged(QModelIndex,QModelIndex)));
59  connect(m_sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
60  this, SLOT(considerRowsAboutToBeInserted(QModelIndex,int,int)));
61  connect(m_sourceModel, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
62  this, SLOT(considerRowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
63  connect(m_sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
64  this, SLOT(considerRowsAboutToBeRemoved(QModelIndex,int,int)));
65  connect(m_sourceModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
66  this, SLOT(considerRowsInserted(QModelIndex,int,int)));
67  connect(m_sourceModel, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
68  this, SLOT(considerRowsMoved(QModelIndex,int,int,QModelIndex,int)));
69  connect(m_sourceModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
70  this, SLOT(considerRowsRemoved(QModelIndex,int,int)));
71 
72  connect(m_sourceModel, SIGNAL(modelAboutToBeReset()),
73  this, SIGNAL(modelAboutToBeReset()));
74  connect(m_sourceModel, SIGNAL(modelReset()),
75  this, SIGNAL(modelReset()));
76  connect(m_sourceModel, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
77  this, SIGNAL(headerDataChanged(Qt::Orientation,int,int)));
78  connect(m_sourceModel, SIGNAL(layoutAboutToBeChanged()),
79  this, SIGNAL(layoutAboutToBeChanged()));
80  connect(m_sourceModel, SIGNAL(layoutChanged()),
81  this, SIGNAL(layoutChanged()));
82  }
83  endResetModel();
84 }
85 
86 void ColumnProxyModel::setColumn(int col)
87 {
88  beginResetModel();
89  m_column = col;
90  endResetModel();
91 }
92 
93 int ColumnProxyModel::column() const
94 {
95  return m_column;
96 }
97 
98 QModelIndex ColumnProxyModel::rootIndex() const
99 {
100  return m_index;
101 }
102 
103 void ColumnProxyModel::setRootIndex(const QModelIndex& index)
104 {
105  if (index == m_index) {
106  return;
107  }
108 
109  if(index.isValid()) {
110  setSourceModel(const_cast<QAbstractItemModel*>(index.model()));
111  }
112  beginResetModel();
113  m_index = index;
114  endResetModel();
115 
116  Q_EMIT rootIndexChanged();
117 }
118 
119 QModelIndex ColumnProxyModel::indexFromModel(QAbstractItemModel* model, int row, int column, const QModelIndex& parent)
120 {
121  return model ? model->index(row, column, parent) : QModelIndex();
122 }
123 
124 QVariant ColumnProxyModel::data(const QModelIndex& index, int role) const
125 {
126  return m_sourceModel ? m_sourceModel->data(sourceIndex(index), role) : QVariant();
127 }
128 
129 QVariant ColumnProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
130 {
131  return m_sourceModel ? m_sourceModel->headerData(section, orientation, role) : QVariant();
132 }
133 
134 QModelIndex ColumnProxyModel::sourceIndex(const QModelIndex& proxyIndex) const
135 {
136  return m_sourceModel ? m_sourceModel->index(proxyIndex.row(), m_column, m_index) : QModelIndex();
137 }
138 
139 int ColumnProxyModel::rowCount(const QModelIndex& parent) const
140 {
141  return (!m_sourceModel || parent.isValid()) ? 0 : m_sourceModel->rowCount(m_index);
142 }
143 
144 QModelIndex ColumnProxyModel::proxyIndex(const QModelIndex& sourceIndex) const
145 {
146  if(sourceIndex.parent()==m_index)
147  return index(sourceIndex.row(), sourceIndex.column(), QModelIndex());
148 
149  return QModelIndex();
150 }
151 
152 void ColumnProxyModel::sourceDestroyed(QObject* source)
153 {
154  Q_ASSERT(source==m_sourceModel);
155 
156  beginResetModel();
157  m_sourceModel = nullptr;
158  endResetModel();
159 }
160 
161 QModelIndex ColumnProxyModel::indexAt(int row, const QModelIndex& parent) const
162 {
163  return m_sourceModel ? m_sourceModel->index(row, m_column, parent) : QModelIndex();
164 }
165 
167 
168 void ColumnProxyModel::considerDataChanged(const QModelIndex& idxA, const QModelIndex& idxB)
169 {
170  if(idxA.parent()==m_index && idxB.parent()==m_index) {
171  Q_EMIT dataChanged(proxyIndex(idxA), proxyIndex(idxB));
172  }
173 }
174 
175 void ColumnProxyModel::considerRowsAboutToBeInserted(const QModelIndex& parent, int rA, int rB)
176 {
177  if(parent==m_index) {
178  beginInsertRows(QModelIndex(), rA, rB);
179  }
180 }
181 
182 void ColumnProxyModel::considerRowsAboutToBeMoved(const QModelIndex &sourceParent, int rA, int rB, const QModelIndex& destParent, int rD)
183 {
184  if(sourceParent==m_index && destParent==m_index) {
185  beginMoveRows(QModelIndex(), rA, rB, QModelIndex(), rD);
186  } else if(sourceParent==m_index) {
187  beginRemoveRows(sourceParent, rA, rB);
188  } else if(destParent==m_index) {
189  beginInsertRows(destParent, rD, rD+(rB-rA));
190  }
191 }
192 
193 void ColumnProxyModel::considerRowsAboutToBeRemoved(const QModelIndex& parent, int rA, int rB)
194 {
195  if(parent==m_index) {
196  beginRemoveRows(QModelIndex(), rA, rB);
197  }
198 }
199 
200 void ColumnProxyModel::considerRowsInserted(const QModelIndex& parent, int , int )
201 {
202  if(parent==m_index) {
203  endInsertRows();
204  }
205 }
206 
207 void ColumnProxyModel::considerRowsMoved(const QModelIndex& sourceParent, int , int , const QModelIndex& destParent, int )
208 {
209  if(sourceParent==m_index && destParent==m_index) {
210  endMoveRows();
211  } else if(sourceParent==m_index) {
212  endRemoveRows();
213  } else if(destParent==m_index) {
214  endInsertRows();
215  }
216 }
217 
218 void ColumnProxyModel::considerRowsRemoved(const QModelIndex& parent, int , int )
219 {
220  if(parent==m_index) {
221  endInsertRows();
222  }
223 }
224 
225 QHash<int, QByteArray> ColumnProxyModel::roleNames() const
226 {
227  return m_sourceModel ? m_sourceModel->roleNames() : QHash<int, QByteArray>();
228 }
229 
230 bool ColumnProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
231 {
232  return m_sourceModel && m_sourceModel->setData(sourceIndex(index), value, role);
233 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const =0
bool isValid() const const
int row() const const
QModelIndex parent() const const
const QAbstractItemModel * model() const const
int column() const const
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.