KD Chart API Documentation  3.1
KDChartAbstractProxyModel.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 <QDebug>
14 
15 #include <KDABLibFakes>
16 
17 #ifdef __GNUC__
18 #if __GNUC__ > 3
19 #define MAY_ALIAS __attribute__((__may_alias__))
20 #endif
21 #else
22 #define MAY_ALIAS
23 #endif
24 
25 namespace KDChart {
26 
30  : QAbstractProxyModel(parent)
31 {
32 }
33 
34 // Allows access to QModelIndex's private data via type punning and a compatible data layout.
35 // Due to inlining in Qt and no d-pointer, it is safe to assume that the layout won't change except
36 // between major Qt versions. As it happens, the layout is the same in Qt4 and Qt5.
37 // The only change is void * -> quintptr.
38 struct MAY_ALIAS KDPrivateModelIndex
39 {
40  int r, c;
41  void *p;
42  const QAbstractItemModel *m;
43 };
44 
45 QModelIndex AbstractProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
46 {
47  if (!sourceIndex.isValid())
48  return QModelIndex();
49  // qDebug() << "sourceIndex.model()="<<sourceIndex.model();
50  // qDebug() << "model()="<<sourceModel();
51  Q_ASSERT(sourceIndex.model() == sourceModel());
52 
53  // Create an index that preserves the internal pointer from the source;
54  // this way AbstractProxyModel preserves the structure of the source model
55  return createIndex(sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer());
56 }
57 
58 QModelIndex AbstractProxyModel::mapToSource(const QModelIndex &proxyIndex) const
59 {
60  if (!sourceModel() || !proxyIndex.isValid())
61  return QModelIndex();
62  if (proxyIndex.model() != this)
63  qDebug() << proxyIndex.model() << this;
64  Q_ASSERT(proxyIndex.model() == this);
65  // So here we need to create a source index which holds that internal pointer.
66  // No way to pass it to sourceModel()->index... so we have to do the ugly way:
67  QModelIndex sourceIndex;
68  auto *hack = reinterpret_cast<KDPrivateModelIndex *>(&sourceIndex);
69  hack->r = proxyIndex.row();
70  hack->c = proxyIndex.column();
71  hack->p = proxyIndex.internalPointer();
72  hack->m = sourceModel();
73  Q_ASSERT(sourceIndex.isValid());
74  return sourceIndex;
75 }
76 
77 QModelIndex AbstractProxyModel::index(int row, int col, const QModelIndex &index) const
78 {
79  if (!sourceModel())
80  return QModelIndex();
81  return mapFromSource(sourceModel()->index(row, col, mapToSource(index)));
82 }
83 
84 QModelIndex AbstractProxyModel::parent(const QModelIndex &index) const
85 {
86  if (!sourceModel())
87  return QModelIndex();
88  return mapFromSource(sourceModel()->parent(mapToSource(index)));
89 }
90 }
#define MAY_ALIAS
QModelIndex index(int row, int col, const QModelIndex &index) const override
Reimplemented for internal purposes.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
Reimplemented for internal purposes.
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
Reimplemented for internal purposes.
AbstractProxyModel(QObject *parent=nullptr)
QModelIndex parent(const QModelIndex &index) const override
Reimplemented for internal purposes.

© 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