KChart

KChartAbstractProxyModel.cpp
1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "KChartAbstractProxyModel.h"
21 
22 #include "KChartMath_p.h"
23 
24 #include <QDebug>
25 
26 #ifdef __GNUC__
27 #if __GNUC__ > 3
28 #define MAY_ALIAS __attribute__((__may_alias__))
29 #endif
30 #else
31 #define MAY_ALIAS
32 #endif
33 
34 namespace KChart {
35 
37  : QAbstractProxyModel(parent) {}
38 
39 // Allows access to QModelIndex's private data via type punning and a compatible data layout.
40 // Due to inlining in Qt and no d-pointer, it is safe to assume that the layout won't change except
41 // between major Qt versions. As it happens, the layout is the same in Qt4 and Qt5.
42 // The only change is void * -> quintptr.
43 struct MAY_ALIAS KDPrivateModelIndex
44 {
45  int r, c;
46  void *p;
47  const QAbstractItemModel *m;
48 };
49 
51 {
52  if ( !sourceIndex.isValid() )
53  return QModelIndex();
54  //qDebug() << "sourceIndex.model()="<<sourceIndex.model();
55  //qDebug() << "model()="<<sourceModel();
56  Q_ASSERT( sourceIndex.model() == sourceModel() );
57 
58  // Create an index that preserves the internal pointer from the source;
59  // this way AbstractProxyModel preserves the structure of the source model
60  return createIndex( sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer() );
61 }
62 
64 {
65  if ( !proxyIndex.isValid() )
66  return QModelIndex();
67  if ( proxyIndex.model() != this )
68  qDebug() << proxyIndex.model() << this;
69  Q_ASSERT( proxyIndex.model() == this );
70  // So here we need to create a source index which holds that internal pointer.
71  // No way to pass it to sourceModel()->index... so we have to do the ugly way:
72  QModelIndex sourceIndex;
73  KDPrivateModelIndex* hack = reinterpret_cast<KDPrivateModelIndex*>(&sourceIndex);
74  hack->r = proxyIndex.row();
75  hack->c = proxyIndex.column();
76  hack->p = proxyIndex.internalPointer();
77  hack->m = sourceModel();
78  Q_ASSERT( sourceIndex.isValid() );
79  return sourceIndex;
80 }
81 
82 QModelIndex AbstractProxyModel::index( int row, int col, const QModelIndex& index ) const
83 {
84  Q_ASSERT(sourceModel());
85  return mapFromSource(sourceModel()->index( row, col, mapToSource(index) ));
86 }
87 
89 {
90  Q_ASSERT(sourceModel());
91  return mapFromSource(sourceModel()->parent( mapToSource(index) ));
92 }
93 
94 }
Class only listed here to document inheritance of some KChart classes.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
bool isValid() const const
Class only listed here to document inheritance of some KChart classes.
QModelIndex index(int row, int col, const QModelIndex &index) const override
int row() const const
void * internalPointer() const const
AbstractProxyModel(QObject *parent=nullptr)
This is basically KDAbstractProxyModel, but only the bits that we really need from it...
QModelIndex createIndex(int row, int column, void *ptr) const const
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
QAbstractItemModel * sourceModel() const const
const QAbstractItemModel * model() const const
int column() const const
Global namespace.
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Sep 19 2020 22:36:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.