kdganttforwardingproxymodel.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002  ** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB.  All rights reserved.
00003  **
00004  ** This file is part of the KD Gantt library.
00005  **
00006  ** This file may be distributed and/or modified under the terms of the
00007  ** GNU General Public License version 2 as published by the Free Software
00008  ** Foundation and appearing in the file LICENSE.GPL included in the
00009  ** packaging of this file.
00010  **
00011  ** Licensees holding valid commercial KD Gantt licenses may use this file in
00012  ** accordance with the KD Gantt Commercial License Agreement provided with
00013  ** the Software.
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  ** See http://www.kdab.net/kdgantt for
00019  **   information about KD Gantt Commercial License Agreements.
00020  **
00021  ** Contact info@kdab.net if any conditions of this
00022  ** licensing are not clear to you.
00023  **
00024  **********************************************************************/
00025 #include "kdganttforwardingproxymodel.h"
00026 
00027 #include <cassert>
00028 
00029 using namespace KDGantt;
00030 
00031 typedef QAbstractProxyModel BASE;
00032 
00036 ForwardingProxyModel::ForwardingProxyModel( QObject* parent )
00037     : BASE( parent )
00038 {
00039 }
00040 
00041 ForwardingProxyModel::~ForwardingProxyModel()
00042 {
00043 }
00044 
00046 QModelIndex ForwardingProxyModel::mapFromSource ( const QModelIndex & sourceIndex ) const
00047 {
00048     if ( !sourceIndex.isValid() )
00049         return QModelIndex();
00050     assert( sourceIndex.model() == sourceModel() );
00051 
00052     // Create an index that preserves the internal pointer from the source;
00053     // this way KDDataConverterProxyModel preserves the structure of the source model
00054     return createIndex( sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer() );
00055 }
00056 
00057 namespace {
00058     // Think this is ugly? Well, it's not from me, it comes from QProxyModel
00059     struct KDPrivateModelIndex {
00060         int r, c;
00061         void *p;
00062         const QAbstractItemModel *m;
00063     };
00064 }
00065 
00067 QModelIndex ForwardingProxyModel::mapToSource ( const QModelIndex & proxyIndex ) const
00068 {
00069     if ( !proxyIndex.isValid() )
00070         return QModelIndex();
00071     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     assert( sourceIndex.isValid() );
00081     return sourceIndex;
00082 }
00083 
00088 void ForwardingProxyModel::setSourceModel( QAbstractItemModel* model )
00089 {
00090     if ( sourceModel() ) sourceModel()->disconnect( this );
00091     BASE::setSourceModel( model );
00092 
00093     if(!model) return;
00094 
00095     connect( model, SIGNAL(modelAboutToBeReset()), this, SLOT(sourceModelAboutToBeReset()) );
00096     connect( model, SIGNAL(modelReset()), this, SLOT(sourceModelReset()) );
00097     connect( model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(sourceLayoutAboutToBeChanged()) );
00098     connect( model, SIGNAL(layoutChanged()), this, SLOT(sourceLayoutChanged()) );
00099 
00100     connect( model, SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)),
00101              this, SLOT(sourceDataChanged(const QModelIndex&,const QModelIndex&)) );
00102 
00103 
00104     connect( model,  SIGNAL(columnsAboutToBeInserted(const QModelIndex&, int,int)),
00105              this, SLOT(sourceColumnsAboutToBeInserted(const QModelIndex&,int,int)) );
00106     connect( model,  SIGNAL(columnsInserted(const QModelIndex&, int,int)),
00107              this, SLOT(sourceColumnsInserted(const QModelIndex&,int,int)) );
00108     connect( model,  SIGNAL(columnsAboutToBeRemoved(const QModelIndex&, int,int)),
00109              this, SLOT(sourceColumnsAboutToBeRemoved(const QModelIndex&,int,int)) );
00110     connect( model,  SIGNAL(columnsRemoved(const QModelIndex&, int,int)),
00111              this, SLOT(sourceColumnsRemoved(const QModelIndex&,int,int)) );
00112 
00113     connect( model,  SIGNAL(rowsAboutToBeInserted(const QModelIndex&, int,int)),
00114              this, SLOT(sourceRowsAboutToBeInserted(const QModelIndex&,int,int)) );
00115     connect( model,  SIGNAL(rowsInserted(const QModelIndex&, int,int)),
00116              this, SLOT(sourceRowsInserted(const QModelIndex&,int,int)) );
00117     connect( model,  SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int,int)),
00118              this, SLOT(sourceRowsAboutToBeRemoved(const QModelIndex&,int,int)) );
00119     connect( model,  SIGNAL(rowsRemoved(const QModelIndex&, int,int)),
00120              this, SLOT(sourceRowsRemoved(const QModelIndex&,int,int)) );
00121 }
00122 
00126 void ForwardingProxyModel::sourceModelAboutToBeReset()
00127 {
00128     // The matching signal is emitted be reset()
00129 }
00130 
00134 void ForwardingProxyModel::sourceModelReset()
00135 {
00136   //qDebug() << "ForwardingProxyModel::sourceModelReset()";
00137     reset();
00138 }
00139 
00144 void ForwardingProxyModel::sourceLayoutAboutToBeChanged()
00145 {
00146   //qDebug() << "ForwardingProxyModel::sourceLayoutAboutToBeChanged()";
00147     emit layoutAboutToBeChanged();
00148 }
00149 
00153 void ForwardingProxyModel::sourceLayoutChanged()
00154 {
00155   //qDebug() << "ForwardingProxyModel::sourceLayoutChanged()";
00156     reset();
00157 }
00158 
00162 void ForwardingProxyModel::sourceDataChanged( const QModelIndex& from, const QModelIndex& to )
00163 {
00164   //qDebug() << "ForwardingProxyModel::sourceDataChanged("<<from<<to<<")";
00165     emit dataChanged( mapFromSource( from ), mapFromSource( to ) );
00166 }
00167 
00171 void ForwardingProxyModel::sourceColumnsAboutToBeInserted( const QModelIndex& parentIdx,
00172                                                                     int start,
00173                                                                     int end )
00174 {
00175     beginInsertColumns( mapFromSource( parentIdx ), start, end );
00176 }
00177 
00181 void ForwardingProxyModel::sourceColumnsInserted( const QModelIndex& parentIdx, int start, int end )
00182 {
00183     Q_UNUSED( parentIdx );
00184     Q_UNUSED( start );
00185     Q_UNUSED( end );
00186     endInsertColumns();
00187 }
00188 
00192 void ForwardingProxyModel::sourceColumnsAboutToBeRemoved( const QModelIndex& parentIdx,
00193                                                                     int start,
00194                                                                     int end )
00195 {
00196     beginRemoveColumns( mapFromSource( parentIdx ), start, end );
00197 }
00198 
00202 void ForwardingProxyModel::sourceColumnsRemoved( const QModelIndex& parentIdx, int start, int end )
00203 {
00204     Q_UNUSED( parentIdx );
00205     Q_UNUSED( start );
00206     Q_UNUSED( end );
00207     endRemoveColumns();
00208 }
00209 
00213 void ForwardingProxyModel::sourceRowsAboutToBeInserted( const QModelIndex & parentIdx, int start, int end )
00214 {
00215     beginInsertRows( mapFromSource( parentIdx ), start, end );
00216 }
00217 
00221 void ForwardingProxyModel::sourceRowsInserted( const QModelIndex& parentIdx, int start, int end )
00222 {
00223     Q_UNUSED( parentIdx );
00224     Q_UNUSED( start );
00225     Q_UNUSED( end );
00226     endInsertRows();
00227 }
00228 
00232 void ForwardingProxyModel::sourceRowsAboutToBeRemoved( const QModelIndex & parentIdx, int start, int end )
00233 {
00234     beginRemoveRows( mapFromSource( parentIdx ), start, end );
00235 }
00236 
00240 void ForwardingProxyModel::sourceRowsRemoved( const QModelIndex& parentIdx, int start, int end )
00241 {
00242     Q_UNUSED( parentIdx );
00243     Q_UNUSED( start );
00244     Q_UNUSED( end );
00245     endRemoveRows();
00246 }
00247 
00249 int ForwardingProxyModel::rowCount( const QModelIndex& idx ) const
00250 {
00251     return sourceModel()->rowCount( mapToSource( idx ) );
00252 }
00253 
00255 int ForwardingProxyModel::columnCount( const QModelIndex& idx ) const
00256 {
00257     return sourceModel()->columnCount( mapToSource( idx ) );
00258 }
00259 
00261 QModelIndex ForwardingProxyModel::index( int row, int column, const QModelIndex& parent ) const
00262 {
00263     return mapFromSource( sourceModel()->index( row, column, mapToSource( parent ) ) );
00264 }
00265 
00267 QModelIndex ForwardingProxyModel::parent( const QModelIndex& idx ) const
00268 {
00269     return mapFromSource( sourceModel()->parent( mapToSource( idx ) ) );
00270 }
00271 
00273 bool ForwardingProxyModel::setData( const QModelIndex& index, const QVariant& value, int role )
00274 {
00275   //qDebug() << "ForwardingProxyModel::setData( " << index<<value<< role<<")";
00276     return sourceModel()->setData( mapToSource( index ), value, role );
00277 }
00278 
00279 #include "moc_kdganttforwardingproxymodel.cpp"
00280 

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