KD Chart 2
[rev.2.5]
|
00001 /**************************************************************************** 00002 ** Copyright (C) 2001-2012 Klaralvdalens Datakonsult AB. All rights reserved. 00003 ** 00004 ** This file is part of the KD Chart library. 00005 ** 00006 ** Licensees holding valid commercial KD Chart licenses may use this file in 00007 ** accordance with the KD Chart Commercial License Agreement provided with 00008 ** the Software. 00009 ** 00010 ** 00011 ** This file may be distributed and/or modified under the terms of the 00012 ** GNU General Public License version 2 and version 3 as published by the 00013 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included. 00014 ** 00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00017 ** 00018 ** Contact info@kdab.com if any conditions of this licensing are not 00019 ** clear to you. 00020 ** 00021 **********************************************************************/ 00022 00023 #include "KDChartAbstractProxyModel.h" 00024 00025 #include <QDebug> 00026 00027 #include <KDABLibFakes> 00028 00029 #ifdef __GNUC__ 00030 #if __GNUC__ > 3 00031 #define ATTRIBUTE __attribute__((__may_alias__)) 00032 #endif 00033 #else 00034 #define ATTRIBUTE 00035 #endif 00036 00037 namespace KDChart { 00038 00041 AbstractProxyModel::AbstractProxyModel(QObject* parent) 00042 : QAbstractProxyModel(parent) {} 00043 00044 // Think this is ugly? Well, it's not from me, it comes from QProxyModel 00045 struct ATTRIBUTE KDPrivateModelIndex 00046 { 00047 int r, c; 00048 void *p; 00049 const QAbstractItemModel *m; 00050 }; 00051 00052 QModelIndex AbstractProxyModel::mapFromSource( const QModelIndex & sourceIndex ) const 00053 { 00054 if ( !sourceIndex.isValid() ) 00055 return QModelIndex(); 00056 //qDebug() << "sourceIndex.model()="<<sourceIndex.model(); 00057 //qDebug() << "model()="<<sourceModel(); 00058 Q_ASSERT( sourceIndex.model() == sourceModel() ); 00059 00060 // Create an index that preserves the internal pointer from the source; 00061 // this way AbstractProxyModel preserves the structure of the source model 00062 return createIndex( sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer() ); 00063 } 00064 00065 QModelIndex AbstractProxyModel::mapToSource( const QModelIndex &proxyIndex ) const 00066 { 00067 if ( !proxyIndex.isValid() ) 00068 return QModelIndex(); 00069 if( proxyIndex.model() != this ) 00070 qDebug() << proxyIndex.model() << this; 00071 Q_ASSERT( proxyIndex.model() == this ); 00072 // So here we need to create a source index which holds that internal pointer. 00073 // No way to pass it to sourceModel()->index... so we have to do the ugly way: 00074 QModelIndex sourceIndex; 00075 KDPrivateModelIndex* hack = reinterpret_cast<KDPrivateModelIndex*>(&sourceIndex); 00076 hack->r = proxyIndex.row(); 00077 hack->c = proxyIndex.column(); 00078 hack->p = proxyIndex.internalPointer(); 00079 hack->m = sourceModel(); 00080 Q_ASSERT( sourceIndex.isValid() ); 00081 return sourceIndex; 00082 } 00083 00084 QModelIndex AbstractProxyModel::index( int row, int col, const QModelIndex& index ) const 00085 { 00086 Q_ASSERT(sourceModel()); 00087 return mapFromSource(sourceModel()->index( row, col, mapToSource(index) )); 00088 } 00089 00090 QModelIndex AbstractProxyModel::parent( const QModelIndex& index ) const 00091 { 00092 Q_ASSERT(sourceModel()); 00093 return mapFromSource(sourceModel()->parent( mapToSource(index) )); 00094 } 00095 00096 }