KDChartAbstractProxyModel.cpp

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*-
00002    KDChart - a multi-platform charting engine
00003    */
00004 
00005 /****************************************************************************
00006  ** Copyright (C) 2005-2007 Klarälvdalens Datakonsult AB.  All rights reserved.
00007  **
00008  ** This file is part of the KD Chart library.
00009  **
00010  ** This file may be distributed and/or modified under the terms of the
00011  ** GNU General Public License version 2 as published by the Free Software
00012  ** Foundation and appearing in the file LICENSE.GPL included in the
00013  ** packaging of this file.
00014  **
00015  ** Licensees holding valid commercial KD Chart licenses may use this file in
00016  ** accordance with the KD Chart Commercial License Agreement provided with
00017  ** the Software.
00018  **
00019  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021  **
00022  ** See http://www.kdab.net/kdchart for
00023  **   information about KD Chart Commercial License Agreements.
00024  **
00025  ** Contact info@kdab.net if any conditions of this
00026  ** licensing are not clear to you.
00027  **
00028  **********************************************************************/
00029 
00030 #include "KDChartAbstractProxyModel.h"
00031 
00032 #include <QDebug>
00033 
00034 #include <KDABLibFakes>
00035 
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 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 }

Generated on Thu Mar 4 23:19:09 2010 for KD Chart 2 by  doxygen 1.5.4