23 #include "leafextensionproxymodel_p.h"
25 #include <QtCore/QSet>
27 using namespace Akonadi;
29 class LeafExtensionProxyModel::Private
32 Private( LeafExtensionProxyModel *qq )
33 : q( qq ), mUniqueKeyCounter( 0 )
37 void sourceRowsInserted(
const QModelIndex&,
int,
int );
38 void sourceRowsRemoved(
const QModelIndex&,
int,
int );
40 LeafExtensionProxyModel *q;
41 QMap<qint64, QModelIndex> mParentIndexes;
42 QSet<QModelIndex> mOwnIndexes;
43 qint64 mUniqueKeyCounter;
46 void LeafExtensionProxyModel::Private::sourceRowsInserted(
const QModelIndex &parentIndex,
int start,
int end )
49 QMutableMapIterator<qint64, QModelIndex> it( mParentIndexes );
50 while ( it.hasNext() ) {
52 if ( it.value().parent() == parentIndex ) {
53 if ( it.value().row() >= start ) {
54 const QModelIndex newIndex = q->QSortFilterProxyModel::index( it.value().row() + (end-start) + 1, it.value().column(), parentIndex );
55 it.setValue( newIndex );
61 void LeafExtensionProxyModel::Private::sourceRowsRemoved(
const QModelIndex &parentIndex,
int start,
int end )
64 QMutableMapIterator<qint64, QModelIndex> it( mParentIndexes );
65 while ( it.hasNext() ) {
67 if ( it.value().parent() == parentIndex ) {
68 if ( it.value().row() >= start && it.value().row() <= end ) {
70 }
else if ( it.value().row() > end ) {
71 const QModelIndex newIndex = q->index( it.value().row() - (end-start) - 1, it.value().column(), parentIndex );
72 it.setValue( newIndex );
78 LeafExtensionProxyModel::LeafExtensionProxyModel( QObject *parent )
79 : QSortFilterProxyModel( parent ), d( new Private( this ) )
83 LeafExtensionProxyModel::~LeafExtensionProxyModel()
88 QModelIndex LeafExtensionProxyModel::index(
int row,
int column,
const QModelIndex &parent )
const
90 if ( row < 0 || column < 0 )
93 if ( parent.isValid() ) {
94 const QModelIndex sourceParent = mapToSource( parent );
95 const QModelIndex sourceIndex = sourceModel()->index( row, column, sourceParent );
96 if ( !sourceIndex.isValid() ) {
99 QMapIterator<qint64, QModelIndex> it( d->mParentIndexes );
100 while ( it.hasNext() ) {
102 if ( it.value() == parent ) {
109 key = ++(d->mUniqueKeyCounter);
110 d->mParentIndexes.insert( key, parent );
113 const QModelIndex index = createIndex( row, column, static_cast<quint32>( key ) );
114 d->mOwnIndexes.insert( index );
120 return QSortFilterProxyModel::index( row, column, parent );
123 QModelIndex LeafExtensionProxyModel::parent(
const QModelIndex &index )
const
125 if ( d->mOwnIndexes.contains( index ) )
126 return d->mParentIndexes.value( index.internalId() );
128 return QSortFilterProxyModel::parent( index );
131 int LeafExtensionProxyModel::rowCount(
const QModelIndex &index )
const
133 if ( d->mOwnIndexes.contains( index ) )
136 const QModelIndex sourceIndex = mapToSource( index );
137 if ( sourceModel()->rowCount( sourceIndex ) == 0 )
138 return leafRowCount( index );
140 return QSortFilterProxyModel::rowCount( index );
143 int LeafExtensionProxyModel::columnCount(
const QModelIndex &index )
const
145 if ( d->mOwnIndexes.contains( index ) )
148 return QSortFilterProxyModel::columnCount( index );
151 QVariant LeafExtensionProxyModel::data(
const QModelIndex &index,
int role )
const
153 if ( d->mOwnIndexes.contains( index ) )
154 return leafData( index.parent(), index.row(), index.column(), role );
156 return QSortFilterProxyModel::data( index, role );
159 Qt::ItemFlags LeafExtensionProxyModel::flags(
const QModelIndex &index )
const
161 if ( d->mOwnIndexes.contains( index ) )
162 return Qt::ItemFlags( Qt::ItemIsEnabled|Qt::ItemIsSelectable );
164 return QSortFilterProxyModel::flags( index );
167 bool LeafExtensionProxyModel::setData(
const QModelIndex &index,
const QVariant &data,
int role )
169 if ( d->mOwnIndexes.contains( index ) )
172 return QSortFilterProxyModel::setData( index, data, role );
175 bool LeafExtensionProxyModel::hasChildren(
const QModelIndex &parent )
const
177 if ( d->mOwnIndexes.contains( parent ) )
180 const QModelIndex sourceParent = mapToSource( parent );
181 if ( sourceModel() && sourceModel()->rowCount( sourceParent ) == 0 )
182 return (leafRowCount( parent ) != 0);
184 return QSortFilterProxyModel::hasChildren( parent );
187 QModelIndex LeafExtensionProxyModel::buddy(
const QModelIndex &index )
const
189 if ( d->mOwnIndexes.contains( index ) )
192 return QSortFilterProxyModel::buddy( index );
195 void LeafExtensionProxyModel::fetchMore(
const QModelIndex &index )
197 if ( d->mOwnIndexes.contains( index ) )
200 QSortFilterProxyModel::fetchMore( index );
203 void LeafExtensionProxyModel::setSourceModel( QAbstractItemModel *_sourceModel )
205 if ( _sourceModel == sourceModel() )
210 disconnect(
this, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
211 this, SLOT(sourceRowsInserted(QModelIndex,
int,
int)) );
212 disconnect(
this, SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
213 this, SLOT(sourceRowsRemoved(QModelIndex,
int,
int)) );
215 QSortFilterProxyModel::setSourceModel( _sourceModel );
217 connect(
this, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
218 this, SLOT(sourceRowsInserted(QModelIndex,
int,
int)) );
219 connect(
this, SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
220 this, SLOT(sourceRowsRemoved(QModelIndex,
int,
int)) );
225 #include "leafextensionproxymodel_p.moc"