KChart

KChartAbstractProxyModel.cpp
1/*
2 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3 *
4 * This file is part of the KD Chart library.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "KChartAbstractProxyModel.h"
10
11#include "KChartMath_p.h"
12
13#include <QDebug>
14
15#ifdef __GNUC__
16#if __GNUC__ > 3
17#define MAY_ALIAS __attribute__((__may_alias__))
18#endif
19#else
20#define MAY_ALIAS
21#endif
22
23namespace KChart {
24
27
28// Allows access to QModelIndex's private data via type punning and a compatible data layout.
29// Due to inlining in Qt and no d-pointer, it is safe to assume that the layout won't change except
30// between major Qt versions. As it happens, the layout is the same in Qt4 and Qt5.
31// The only change is void * -> quintptr.
32struct MAY_ALIAS KDPrivateModelIndex
33{
34 int r, c;
35 void *p;
36 const QAbstractItemModel *m;
37};
38
40{
41 if ( !sourceIndex.isValid() )
42 return QModelIndex();
43 //qDebug() << "sourceIndex.model()="<<sourceIndex.model();
44 //qDebug() << "model()="<<sourceModel();
45 Q_ASSERT( sourceIndex.model() == sourceModel() );
46
47 // Create an index that preserves the internal pointer from the source;
48 // this way AbstractProxyModel preserves the structure of the source model
49 return createIndex( sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer() );
50}
51
53{
54 if ( !proxyIndex.isValid() )
55 return QModelIndex();
56 if ( proxyIndex.model() != this )
57 qDebug() << proxyIndex.model() << this;
58 Q_ASSERT( proxyIndex.model() == this );
59 // So here we need to create a source index which holds that internal pointer.
60 // No way to pass it to sourceModel()->index... so we have to do the ugly way:
61 QModelIndex sourceIndex;
62 KDPrivateModelIndex* hack = reinterpret_cast<KDPrivateModelIndex*>(&sourceIndex);
63 hack->r = proxyIndex.row();
64 hack->c = proxyIndex.column();
65 hack->p = proxyIndex.internalPointer();
66 hack->m = sourceModel();
67 Q_ASSERT( sourceIndex.isValid() );
68 return sourceIndex;
69}
70
71QModelIndex AbstractProxyModel::index( int row, int col, const QModelIndex& index ) const
72{
73 Q_ASSERT(sourceModel());
74 return mapFromSource(sourceModel()->index( row, col, mapToSource(index) ));
75}
76
78{
79 Q_ASSERT(sourceModel());
81}
82
83}
QModelIndex index(int row, int col, const QModelIndex &index) const override
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
AbstractProxyModel(QObject *parent=nullptr)
This is basically KDAbstractProxyModel, but only the bits that we really need from it.
QModelIndex createIndex(int row, int column, const void *ptr) const const
int column() const const
void * internalPointer() const const
bool isValid() const const
const QAbstractItemModel * model() const const
int row() const const
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.