KDChartAbstractProxyModel.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2011 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 
00030 namespace KDChart {
00031 
00034 AbstractProxyModel::AbstractProxyModel(QObject* parent) 
00035   : QAbstractProxyModel(parent) {}
00036 
00037 // Think this is ugly? Well, it's not from me, it comes from QProxyModel
00038 struct KDPrivateModelIndex
00039 {
00040   int r, c;
00041   void *p;
00042   const QAbstractItemModel *m;
00043 };
00044 
00045 QModelIndex AbstractProxyModel::mapFromSource( const QModelIndex & sourceIndex ) const
00046 {
00047   if ( !sourceIndex.isValid() )
00048     return QModelIndex();
00049   //qDebug() << "sourceIndex.model()="<<sourceIndex.model();
00050   //qDebug() << "model()="<<sourceModel();
00051   Q_ASSERT( sourceIndex.model() == sourceModel() );
00052 
00053   // Create an index that preserves the internal pointer from the source;
00054   // this way AbstractProxyModel preserves the structure of the source model
00055   return createIndex( sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer() );
00056 }
00057 
00058 QModelIndex AbstractProxyModel::mapToSource( const QModelIndex &proxyIndex ) const
00059 {
00060   if ( !proxyIndex.isValid() )
00061     return QModelIndex();
00062   if( proxyIndex.model() != this )
00063     qDebug() << proxyIndex.model() << this;
00064   Q_ASSERT( proxyIndex.model() == this );
00065   // So here we need to create a source index which holds that internal pointer.
00066   // No way to pass it to sourceModel()->index... so we have to do the ugly way:
00067   QModelIndex sourceIndex;
00068   KDPrivateModelIndex* hack = reinterpret_cast<KDPrivateModelIndex*>(&sourceIndex);
00069   hack->r = proxyIndex.row();
00070   hack->c = proxyIndex.column();
00071   hack->p = proxyIndex.internalPointer();
00072   hack->m = sourceModel();
00073   Q_ASSERT( sourceIndex.isValid() );
00074   return sourceIndex;
00075 }
00076 
00077 QModelIndex AbstractProxyModel::index( int row, int col, const QModelIndex& index ) const
00078 {
00079     Q_ASSERT(sourceModel());
00080     return mapFromSource(sourceModel()->index( row, col, mapToSource(index) ));
00081 }
00082 
00083 QModelIndex AbstractProxyModel::parent( const QModelIndex& index ) const
00084 {
00085     Q_ASSERT(sourceModel());
00086     return mapFromSource(sourceModel()->parent( mapToSource(index) ));
00087 }
00088 
00089 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines

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