Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

KDChartAbstractProxyModel.cpp

Go to the documentation of this file.
00001 #include "KDChartAbstractProxyModel.h"
00002 
00003 #include <QDebug>
00004 
00005 #include <KDABLibFakes>
00006 
00007 
00008 namespace KDChart {
00009 
00012 AbstractProxyModel::AbstractProxyModel(QObject* parent) 
00013   : QAbstractProxyModel(parent) {}
00014 
00015 // Think this is ugly? Well, it's not from me, it comes from QProxyModel
00016 struct KDPrivateModelIndex
00017 {
00018   int r, c;
00019   void *p;
00020   const QAbstractItemModel *m;
00021 };
00022 
00023 QModelIndex AbstractProxyModel::mapFromSource( const QModelIndex & sourceIndex ) const
00024 {
00025   if ( !sourceIndex.isValid() )
00026     return QModelIndex();
00027   //qDebug() << "sourceIndex.model()="<<sourceIndex.model();
00028   //qDebug() << "model()="<<sourceModel();
00029   Q_ASSERT( sourceIndex.model() == sourceModel() );
00030 
00031   // Create an index that preserves the internal pointer from the source;
00032   // this way AbstractProxyModel preserves the structure of the source model
00033   return createIndex( sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer() );
00034 }
00035 
00036 QModelIndex AbstractProxyModel::mapToSource( const QModelIndex &proxyIndex ) const
00037 {
00038   if ( !proxyIndex.isValid() )
00039     return QModelIndex();
00040   Q_ASSERT( proxyIndex.model() == this );
00041   // So here we need to create a source index which holds that internal pointer.
00042   // No way to pass it to sourceModel()->index... so we have to do the ugly way:
00043   QModelIndex sourceIndex;
00044   KDPrivateModelIndex* hack = reinterpret_cast<KDPrivateModelIndex*>(&sourceIndex);
00045   hack->r = proxyIndex.row();
00046   hack->c = proxyIndex.column();
00047   hack->p = proxyIndex.internalPointer();
00048   hack->m = sourceModel();
00049   Q_ASSERT( sourceIndex.isValid() );
00050   return sourceIndex;
00051 }
00052 
00053 QModelIndex AbstractProxyModel::index( int row, int col, const QModelIndex& index ) const
00054 {
00055     Q_ASSERT(sourceModel());
00056     return mapFromSource(sourceModel()->index( row, col, mapToSource(index) ));
00057 }
00058 
00059 QModelIndex AbstractProxyModel::parent( const QModelIndex& index ) const
00060 {
00061     Q_ASSERT(sourceModel());
00062     return mapFromSource(sourceModel()->parent( mapToSource(index) ));
00063 }
00064 
00065 }

Generated on Thu May 10 11:06:24 2007 for KD Chart 2 by doxygen 1.3.6