00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "kdganttforwardingproxymodel.h"
00024
00025 #include <cassert>
00026
00027 using namespace KDGantt;
00028
00029 typedef QAbstractProxyModel BASE;
00030
00034 ForwardingProxyModel::ForwardingProxyModel( QObject* parent )
00035 : BASE( parent )
00036 {
00037 }
00038
00039 ForwardingProxyModel::~ForwardingProxyModel()
00040 {
00041 }
00042
00044 QModelIndex ForwardingProxyModel::mapFromSource ( const QModelIndex & sourceIndex ) const
00045 {
00046 if ( !sourceIndex.isValid() )
00047 return QModelIndex();
00048 assert( sourceIndex.model() == sourceModel() );
00049
00050
00051
00052 return createIndex( sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer() );
00053 }
00054
00055 namespace {
00056
00057 struct KDPrivateModelIndex {
00058 int r, c;
00059 void *p;
00060 const QAbstractItemModel *m;
00061 };
00062 }
00063
00065 QModelIndex ForwardingProxyModel::mapToSource ( const QModelIndex & proxyIndex ) const
00066 {
00067 if ( !proxyIndex.isValid() )
00068 return QModelIndex();
00069 assert( proxyIndex.model() == this );
00070
00071
00072 QModelIndex sourceIndex;
00073 KDPrivateModelIndex* hack = reinterpret_cast<KDPrivateModelIndex*>(&sourceIndex);
00074 hack->r = proxyIndex.row();
00075 hack->c = proxyIndex.column();
00076 hack->p = proxyIndex.internalPointer();
00077 hack->m = sourceModel();
00078 assert( sourceIndex.isValid() );
00079 return sourceIndex;
00080 }
00081
00086 void ForwardingProxyModel::setSourceModel( QAbstractItemModel* model )
00087 {
00088 if ( sourceModel() ) sourceModel()->disconnect( this );
00089 BASE::setSourceModel( model );
00090
00091 if(!model) return;
00092
00093 connect( model, SIGNAL(modelAboutToBeReset()), this, SLOT(sourceModelAboutToBeReset()) );
00094 connect( model, SIGNAL(modelReset()), this, SLOT(sourceModelReset()) );
00095 connect( model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(sourceLayoutAboutToBeChanged()) );
00096 connect( model, SIGNAL(layoutChanged()), this, SLOT(sourceLayoutChanged()) );
00097
00098 connect( model, SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)),
00099 this, SLOT(sourceDataChanged(const QModelIndex&,const QModelIndex&)) );
00100
00101
00102 connect( model, SIGNAL(columnsAboutToBeInserted(const QModelIndex&, int,int)),
00103 this, SLOT(sourceColumnsAboutToBeInserted(const QModelIndex&,int,int)) );
00104 connect( model, SIGNAL(columnsInserted(const QModelIndex&, int,int)),
00105 this, SLOT(sourceColumnsInserted(const QModelIndex&,int,int)) );
00106 connect( model, SIGNAL(columnsAboutToBeRemoved(const QModelIndex&, int,int)),
00107 this, SLOT(sourceColumnsAboutToBeRemoved(const QModelIndex&,int,int)) );
00108 connect( model, SIGNAL(columnsRemoved(const QModelIndex&, int,int)),
00109 this, SLOT(sourceColumnsRemoved(const QModelIndex&,int,int)) );
00110
00111 connect( model, SIGNAL(rowsAboutToBeInserted(const QModelIndex&, int,int)),
00112 this, SLOT(sourceRowsAboutToBeInserted(const QModelIndex&,int,int)) );
00113 connect( model, SIGNAL(rowsInserted(const QModelIndex&, int,int)),
00114 this, SLOT(sourceRowsInserted(const QModelIndex&,int,int)) );
00115 connect( model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int,int)),
00116 this, SLOT(sourceRowsAboutToBeRemoved(const QModelIndex&,int,int)) );
00117 connect( model, SIGNAL(rowsRemoved(const QModelIndex&, int,int)),
00118 this, SLOT(sourceRowsRemoved(const QModelIndex&,int,int)) );
00119 }
00120
00124 void ForwardingProxyModel::sourceModelAboutToBeReset()
00125 {
00126
00127 }
00128
00132 void ForwardingProxyModel::sourceModelReset()
00133 {
00134
00135 reset();
00136 }
00137
00142 void ForwardingProxyModel::sourceLayoutAboutToBeChanged()
00143 {
00144
00145 emit layoutAboutToBeChanged();
00146 }
00147
00151 void ForwardingProxyModel::sourceLayoutChanged()
00152 {
00153
00154 reset();
00155 }
00156
00160 void ForwardingProxyModel::sourceDataChanged( const QModelIndex& from, const QModelIndex& to )
00161 {
00162
00163 emit dataChanged( mapFromSource( from ), mapFromSource( to ) );
00164 }
00165
00169 void ForwardingProxyModel::sourceColumnsAboutToBeInserted( const QModelIndex& parentIdx,
00170 int start,
00171 int end )
00172 {
00173 beginInsertColumns( mapFromSource( parentIdx ), start, end );
00174 }
00175
00179 void ForwardingProxyModel::sourceColumnsInserted( const QModelIndex& parentIdx, int start, int end )
00180 {
00181 Q_UNUSED( parentIdx );
00182 Q_UNUSED( start );
00183 Q_UNUSED( end );
00184 endInsertColumns();
00185 }
00186
00190 void ForwardingProxyModel::sourceColumnsAboutToBeRemoved( const QModelIndex& parentIdx,
00191 int start,
00192 int end )
00193 {
00194 beginRemoveColumns( mapFromSource( parentIdx ), start, end );
00195 }
00196
00200 void ForwardingProxyModel::sourceColumnsRemoved( const QModelIndex& parentIdx, int start, int end )
00201 {
00202 Q_UNUSED( parentIdx );
00203 Q_UNUSED( start );
00204 Q_UNUSED( end );
00205 endRemoveColumns();
00206 }
00207
00211 void ForwardingProxyModel::sourceRowsAboutToBeInserted( const QModelIndex & parentIdx, int start, int end )
00212 {
00213 beginInsertRows( mapFromSource( parentIdx ), start, end );
00214 }
00215
00219 void ForwardingProxyModel::sourceRowsInserted( const QModelIndex& parentIdx, int start, int end )
00220 {
00221 Q_UNUSED( parentIdx );
00222 Q_UNUSED( start );
00223 Q_UNUSED( end );
00224 endInsertRows();
00225 }
00226
00230 void ForwardingProxyModel::sourceRowsAboutToBeRemoved( const QModelIndex & parentIdx, int start, int end )
00231 {
00232 beginRemoveRows( mapFromSource( parentIdx ), start, end );
00233 }
00234
00238 void ForwardingProxyModel::sourceRowsRemoved( const QModelIndex& parentIdx, int start, int end )
00239 {
00240 Q_UNUSED( parentIdx );
00241 Q_UNUSED( start );
00242 Q_UNUSED( end );
00243 endRemoveRows();
00244 }
00245
00247 int ForwardingProxyModel::rowCount( const QModelIndex& idx ) const
00248 {
00249 return sourceModel()->rowCount( mapToSource( idx ) );
00250 }
00251
00253 int ForwardingProxyModel::columnCount( const QModelIndex& idx ) const
00254 {
00255 return sourceModel()->columnCount( mapToSource( idx ) );
00256 }
00257
00259 QModelIndex ForwardingProxyModel::index( int row, int column, const QModelIndex& parent ) const
00260 {
00261 return mapFromSource( sourceModel()->index( row, column, mapToSource( parent ) ) );
00262 }
00263
00265 QModelIndex ForwardingProxyModel::parent( const QModelIndex& idx ) const
00266 {
00267 return mapFromSource( sourceModel()->parent( mapToSource( idx ) ) );
00268 }
00269
00271 bool ForwardingProxyModel::setData( const QModelIndex& index, const QVariant& value, int role )
00272 {
00273
00274 return sourceModel()->setData( mapToSource( index ), value, role );
00275 }
00276
00277 #include "moc_kdganttforwardingproxymodel.cpp"
00278