KD Chart 2  [rev.2.5.1]
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
KDChartAbstractProxyModel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2013 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 ATTRIBUTE __attribute__((__may_alias__))
32 #endif
33 #else
34 #define ATTRIBUTE
35 #endif
36 
37 namespace KDChart {
38 
42  : QAbstractProxyModel(parent) {}
43 
44 // Think this is ugly? Well, it's not from me, it comes from QProxyModel
45 struct ATTRIBUTE KDPrivateModelIndex
46 {
47  int r, c;
48  void *p;
49  const QAbstractItemModel *m;
50 };
51 
52 QModelIndex AbstractProxyModel::mapFromSource( const QModelIndex & sourceIndex ) const
53 {
54  if ( !sourceIndex.isValid() )
55  return QModelIndex();
56  //qDebug() << "sourceIndex.model()="<<sourceIndex.model();
57  //qDebug() << "model()="<<sourceModel();
58  Q_ASSERT( sourceIndex.model() == sourceModel() );
59 
60  // Create an index that preserves the internal pointer from the source;
61  // this way AbstractProxyModel preserves the structure of the source model
62  return createIndex( sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer() );
63 }
64 
65 QModelIndex AbstractProxyModel::mapToSource( const QModelIndex &proxyIndex ) const
66 {
67  if ( !proxyIndex.isValid() )
68  return QModelIndex();
69  if ( proxyIndex.model() != this )
70  qDebug() << proxyIndex.model() << this;
71  Q_ASSERT( proxyIndex.model() == this );
72  // So here we need to create a source index which holds that internal pointer.
73  // No way to pass it to sourceModel()->index... so we have to do the ugly way:
74  QModelIndex sourceIndex;
75  KDPrivateModelIndex* hack = reinterpret_cast<KDPrivateModelIndex*>(&sourceIndex);
76  hack->r = proxyIndex.row();
77  hack->c = proxyIndex.column();
78  hack->p = proxyIndex.internalPointer();
79  hack->m = sourceModel();
80  Q_ASSERT( sourceIndex.isValid() );
81  return sourceIndex;
82 }
83 
84 QModelIndex AbstractProxyModel::index( int row, int col, const QModelIndex& index ) const
85 {
86  Q_ASSERT(sourceModel());
87  return mapFromSource(sourceModel()->index( row, col, mapToSource(index) ));
88 }
89 
90 QModelIndex AbstractProxyModel::parent( const QModelIndex& index ) const
91 {
92  Q_ASSERT(sourceModel());
93  return mapFromSource(sourceModel()->parent( mapToSource(index) ));
94 }
95 
96 }

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/