KD Chart 2  [rev.2.7]
KDChartAbstractProxyModel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2020 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
24 
25 #include <QDebug>
26 
27 #include <KDABLibFakes>
28 
29 #ifdef __GNUC__
30 #if __GNUC__ > 3
31 #define MAY_ALIAS __attribute__((__may_alias__))
32 #endif
33 #else
34 #define MAY_ALIAS
35 #endif
36 
37 namespace KDChart {
38 
42  : QAbstractProxyModel(parent) {}
43 
44 // Allows access to QModelIndex's private data via type punning and a compatible data layout.
45 // Due to inlining in Qt and no d-pointer, it is safe to assume that the layout won't change except
46 // between major Qt versions. As it happens, the layout is the same in Qt4 and Qt5.
47 // The only change is void * -> quintptr.
48 struct MAY_ALIAS KDPrivateModelIndex
49 {
50  int r, c;
51  void *p;
52  const QAbstractItemModel *m;
53 };
54 
55 QModelIndex AbstractProxyModel::mapFromSource( const QModelIndex & sourceIndex ) const
56 {
57  if ( !sourceIndex.isValid() )
58  return QModelIndex();
59  //qDebug() << "sourceIndex.model()="<<sourceIndex.model();
60  //qDebug() << "model()="<<sourceModel();
61  Q_ASSERT( sourceIndex.model() == sourceModel() );
62 
63  // Create an index that preserves the internal pointer from the source;
64  // this way AbstractProxyModel preserves the structure of the source model
65  return createIndex( sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer() );
66 }
67 
68 QModelIndex AbstractProxyModel::mapToSource( const QModelIndex &proxyIndex ) const
69 {
70  if ( !sourceModel() || !proxyIndex.isValid() )
71  return QModelIndex();
72  if ( proxyIndex.model() != this )
73  qDebug() << proxyIndex.model() << this;
74  Q_ASSERT( proxyIndex.model() == this );
75  // So here we need to create a source index which holds that internal pointer.
76  // No way to pass it to sourceModel()->index... so we have to do the ugly way:
77  QModelIndex sourceIndex;
78  KDPrivateModelIndex* hack = reinterpret_cast<KDPrivateModelIndex*>(&sourceIndex);
79  hack->r = proxyIndex.row();
80  hack->c = proxyIndex.column();
81  hack->p = proxyIndex.internalPointer();
82  hack->m = sourceModel();
83  Q_ASSERT( sourceIndex.isValid() );
84  return sourceIndex;
85 }
86 
87 QModelIndex AbstractProxyModel::index( int row, int col, const QModelIndex& index ) const
88 {
89  if (!sourceModel())
90  return QModelIndex();
91  return mapFromSource(sourceModel()->index( row, col, mapToSource(index) ));
92 }
93 
94 QModelIndex AbstractProxyModel::parent( const QModelIndex& index ) const
95 {
96  if(!sourceModel())
97  return QModelIndex();
98  return mapFromSource(sourceModel()->parent( mapToSource(index) ));
99 }
100 
101 }
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
QModelIndex index(int row, int col, const QModelIndex &index) const override
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
#define MAY_ALIAS
QModelIndex parent(const QModelIndex &index) const override
Class only listed here to document inheritance of some KDChart classes.
AbstractProxyModel(QObject *parent=0)
This is basically KDAbstractProxyModel, but only the bits that we really need from it...
Class only listed here to document inheritance of some KDChart classes.

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/