KD Chart API Documentation  3.1
kdganttforwardingproxymodel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Chart library.
4 **
5 ** SPDX-FileCopyrightText: 2001 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
12 
13 #include <QStringList>
14 #include <cassert>
15 
16 using namespace KDGantt;
17 
18 typedef QAbstractProxyModel BASE;
19 
24  : BASE(parent)
25 {
26 }
27 
29 {
30 }
31 
33 QModelIndex ForwardingProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
34 {
35  if (!sourceIndex.isValid())
36  return QModelIndex();
37  assert(sourceIndex.model() == sourceModel());
38 
39  // Create an index that preserves the internal pointer from the source;
40  // this way KDDataConverterProxyModel preserves the structure of the source model
41  return createIndex(sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer());
42 }
43 #ifdef __GNUC__
44 #if __GNUC__ > 3
45 #define ATTRIBUTE __attribute__((__may_alias__))
46 #endif
47 #else
48 #define ATTRIBUTE
49 #endif
50 namespace {
51 // Think this is ugly? Well, it's not from me, it comes from QProxyModel
52 struct ATTRIBUTE KDPrivateModelIndex
53 {
54  int r, c;
55  void *p;
56  const QAbstractItemModel *m;
57 };
58 }
59 
61 QModelIndex ForwardingProxyModel::mapToSource(const QModelIndex &proxyIndex) const
62 {
63  if (!proxyIndex.isValid())
64  return QModelIndex();
65  assert(proxyIndex.model() == this);
66  // So here we need to create a source index which holds that internal pointer.
67  // No way to pass it to sourceModel()->index... so we have to do the ugly way:
68  QModelIndex sourceIndex;
69  auto *hack = reinterpret_cast<KDPrivateModelIndex *>(&sourceIndex);
70  hack->r = proxyIndex.row();
71  hack->c = proxyIndex.column();
72  hack->p = proxyIndex.internalPointer();
73  hack->m = sourceModel();
74  assert(sourceIndex.isValid());
75  return sourceIndex;
76 }
77 
82 void ForwardingProxyModel::setSourceModel(QAbstractItemModel *model)
83 {
84  if (sourceModel())
85  sourceModel()->disconnect(this);
86  BASE::setSourceModel(model);
87 
88  if (!model)
89  return;
90 
91  connect(model, &QAbstractItemModel::modelAboutToBeReset, this, &ForwardingProxyModel::sourceModelAboutToBeReset);
92  connect(model, &QAbstractItemModel::modelReset, this, &ForwardingProxyModel::sourceModelReset);
93  connect(model, &QAbstractItemModel::layoutAboutToBeChanged, this, &ForwardingProxyModel::sourceLayoutAboutToBeChanged);
94  connect(model, &QAbstractItemModel::layoutChanged, this, &ForwardingProxyModel::sourceLayoutChanged);
95 
96  connect(model, &QAbstractItemModel::dataChanged,
98 
99  connect(model, &QAbstractItemModel::columnsAboutToBeInserted,
101  connect(model, &QAbstractItemModel::columnsInserted,
103  connect(model, &QAbstractItemModel::columnsAboutToBeRemoved,
105  connect(model, &QAbstractItemModel::columnsRemoved,
107 
108  connect(model, &QAbstractItemModel::rowsAboutToBeInserted,
110  connect(model, &QAbstractItemModel::rowsInserted,
112  connect(model, &QAbstractItemModel::rowsAboutToBeRemoved,
114  connect(model, &QAbstractItemModel::rowsRemoved,
116 }
117 
122 {
123  // The matching signal is emitted be reset()
124 }
125 
130 {
131  // qDebug() << "ForwardingProxyModel::sourceModelReset()";
132  beginResetModel();
133  endResetModel();
134 }
135 
141 {
142  // qDebug() << "ForwardingProxyModel::sourceLayoutAboutToBeChanged()";
143  Q_EMIT layoutAboutToBeChanged();
144 }
145 
150 {
151  // qDebug() << "ForwardingProxyModel::sourceLayoutChanged()";
152  beginResetModel();
153  endResetModel();
154 }
155 
159 void ForwardingProxyModel::sourceDataChanged(const QModelIndex &from, const QModelIndex &to)
160 {
161  // qDebug() << "ForwardingProxyModel::sourceDataChanged("<<from<<to<<")";
162  Q_EMIT dataChanged(mapFromSource(from), mapFromSource(to));
163 }
164 
169  int start,
170  int end)
171 {
172  beginInsertColumns(mapFromSource(parentIdx), start, end);
173 }
174 
178 void ForwardingProxyModel::sourceColumnsInserted(const QModelIndex &parentIdx, int start, int end)
179 {
180  Q_UNUSED(parentIdx);
181  Q_UNUSED(start);
182  Q_UNUSED(end);
183  endInsertColumns();
184 }
185 
189 void ForwardingProxyModel::sourceColumnsAboutToBeRemoved(const QModelIndex &parentIdx,
190  int start,
191  int end)
192 {
193  beginRemoveColumns(mapFromSource(parentIdx), start, end);
194 }
195 
199 void ForwardingProxyModel::sourceColumnsRemoved(const QModelIndex &parentIdx, int start, int end)
200 {
201  Q_UNUSED(parentIdx);
202  Q_UNUSED(start);
203  Q_UNUSED(end);
204  endRemoveColumns();
205 }
206 
210 void ForwardingProxyModel::sourceRowsAboutToBeInserted(const QModelIndex &parentIdx, int start, int end)
211 {
212  beginInsertRows(mapFromSource(parentIdx), start, end);
213 }
214 
218 void ForwardingProxyModel::sourceRowsInserted(const QModelIndex &parentIdx, int start, int end)
219 {
220  Q_UNUSED(parentIdx);
221  Q_UNUSED(start);
222  Q_UNUSED(end);
223  endInsertRows();
224 }
225 
229 void ForwardingProxyModel::sourceRowsAboutToBeRemoved(const QModelIndex &parentIdx, int start, int end)
230 {
231  beginRemoveRows(mapFromSource(parentIdx), start, end);
232 }
233 
237 void ForwardingProxyModel::sourceRowsRemoved(const QModelIndex &parentIdx, int start, int end)
238 {
239  Q_UNUSED(parentIdx);
240  Q_UNUSED(start);
241  Q_UNUSED(end);
242  endRemoveRows();
243 }
244 
246 int ForwardingProxyModel::rowCount(const QModelIndex &idx) const
247 {
248  return sourceModel()->rowCount(mapToSource(idx));
249 }
250 
252 int ForwardingProxyModel::columnCount(const QModelIndex &idx) const
253 {
254  return sourceModel()->columnCount(mapToSource(idx));
255 }
256 
258 QModelIndex ForwardingProxyModel::index(int row, int column, const QModelIndex &parent) const
259 {
260  return mapFromSource(sourceModel()->index(row, column, mapToSource(parent)));
261 }
262 
264 QModelIndex ForwardingProxyModel::parent(const QModelIndex &idx) const
265 {
266  return mapFromSource(sourceModel()->parent(mapToSource(idx)));
267 }
268 
270 bool ForwardingProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
271 {
272  // qDebug() << "ForwardingProxyModel::setData( " << index<<value<< role<<")";
273  return sourceModel()->setData(mapToSource(index), value, role);
274 }
275 
276 QMimeData *ForwardingProxyModel::mimeData(const QModelIndexList &indexes) const
277 {
278  QModelIndexList source_indexes;
279  for (int i = 0; i < indexes.count(); ++i)
280  source_indexes << mapToSource(indexes.at(i));
281  return sourceModel()->mimeData(source_indexes);
282 }
283 
284 bool ForwardingProxyModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
285 {
286  if ((row == -1) && (column == -1))
287  return sourceModel()->dropMimeData(data, action, -1, -1, mapToSource(parent));
288  int source_destination_row = -1;
289  int source_destination_column = -1;
290  QModelIndex source_parent;
291  if (row == rowCount(parent)) {
292  source_parent = mapToSource(parent);
293  source_destination_row = sourceModel()->rowCount(source_parent);
294  } else {
295  QModelIndex proxy_index = index(row, column, parent);
296  QModelIndex source_index = mapToSource(proxy_index);
297  source_destination_row = source_index.row();
298  source_destination_column = source_index.column();
299  source_parent = source_index.parent();
300  }
301  return sourceModel()->dropMimeData(data, action, source_destination_row, source_destination_column, source_parent);
302 }
303 
305 {
306  return sourceModel()->mimeTypes();
307 }
308 
310 {
311  return sourceModel()->supportedDropActions();
312 }
313 
314 #include "moc_kdganttforwardingproxymodel.cpp"
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
Converts indexes in the source model to indexes in the proxy model.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
Converts indexes in the proxy model to indexes in the source model.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
ForwardingProxyModel(QObject *parent=nullptr)
Constructor.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QModelIndex parent(const QModelIndex &idx) const override
QStringList mimeTypes() const override
virtual void sourceModelAboutToBeReset()
Called when the source model is about to be reset.
virtual void sourceRowsRemoved(const QModelIndex &, int start, int end)
Called after rows have been removed from the source model.
virtual void sourceColumnsAboutToBeInserted(const QModelIndex &idx, int start, int end)
Called just before columns are inserted into the source model.
virtual void sourceLayoutAboutToBeChanged()
Called just before the layout of the source model is changed.
virtual void sourceDataChanged(const QModelIndex &from, const QModelIndex &to)
Called when the data in an existing item in the source model changes.
Qt::DropActions supportedDropActions() const override
virtual void sourceColumnsAboutToBeRemoved(const QModelIndex &idx, int start, int end)
Called just before columns are removed from the source model.
virtual void sourceLayoutChanged()
Called when the layout of the source model has changed.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
int rowCount(const QModelIndex &idx=QModelIndex()) const override
virtual void sourceColumnsRemoved(const QModelIndex &idx, int start, int end)
Called after columns have been removed from the source model.
virtual void sourceModelReset()
Called when the source model is reset.
void setSourceModel(QAbstractItemModel *model) override
Sets the model to be used as the source model for this proxy.
virtual void sourceRowsInserted(const QModelIndex &idx, int start, int end)
Called after rows have been inserted into the source model.
int columnCount(const QModelIndex &idx=QModelIndex()) const override
virtual void sourceRowsAboutToBeRemoved(const QModelIndex &, int start, int end)
Called just before rows are removed from the source model.
virtual void sourceColumnsInserted(const QModelIndex &idx, int start, int end)
Called after columns have been inserted into the source model.
virtual void sourceRowsAboutToBeInserted(const QModelIndex &idx, int start, int end)
Called just before rows are inserted into the source model.
QMimeData * mimeData(const QModelIndexList &indexes) const override
#define ATTRIBUTE
QAbstractProxyModel BASE

© 2001 Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
https://www.kdab.com/development-resources/qt-tools/kd-chart/
Generated by doxygen 1.9.1