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 ) {
94 if ( parent.isValid() ) {
95 const QModelIndex sourceParent = mapToSource( parent );
96 const QModelIndex sourceIndex = sourceModel()->index( row, column, sourceParent );
97 if ( !sourceIndex.isValid() ) {
100 QMapIterator<qint64, QModelIndex> it( d->mParentIndexes );
101 while ( it.hasNext() ) {
103 if ( it.value() == parent ) {
110 key = ++( d->mUniqueKeyCounter );
111 d->mParentIndexes.insert( key, parent );
114 const QModelIndex index = createIndex( row, column, static_cast<quint32>( key ) );
115 d->mOwnIndexes.insert( index );
121 return QSortFilterProxyModel::index( row, column, parent );
124 QModelIndex LeafExtensionProxyModel::parent(
const QModelIndex &index )
const
126 if ( d->mOwnIndexes.contains( index ) ) {
127 return d->mParentIndexes.value( index.internalId() );
130 return QSortFilterProxyModel::parent( index );
133 int LeafExtensionProxyModel::rowCount(
const QModelIndex &index )
const
135 if ( d->mOwnIndexes.contains( index ) ) {
139 const QModelIndex sourceIndex = mapToSource( index );
140 if ( sourceModel()->rowCount( sourceIndex ) == 0 ) {
141 return leafRowCount( index );
144 return QSortFilterProxyModel::rowCount( index );
147 int LeafExtensionProxyModel::columnCount(
const QModelIndex &index )
const
149 if ( d->mOwnIndexes.contains( index ) ) {
153 return QSortFilterProxyModel::columnCount( index );
156 QVariant LeafExtensionProxyModel::data(
const QModelIndex &index,
int role )
const
158 if ( d->mOwnIndexes.contains( index ) ) {
159 return leafData( index.parent(), index.row(), index.column(), role );
162 return QSortFilterProxyModel::data( index, role );
165 Qt::ItemFlags LeafExtensionProxyModel::flags(
const QModelIndex &index )
const
167 if ( d->mOwnIndexes.contains( index ) ) {
168 return Qt::ItemFlags( Qt::ItemIsEnabled|Qt::ItemIsSelectable );
171 return QSortFilterProxyModel::flags( index );
174 bool LeafExtensionProxyModel::setData(
const QModelIndex &index,
const QVariant &data,
int role )
176 if ( d->mOwnIndexes.contains( index ) ) {
180 return QSortFilterProxyModel::setData( index, data, role );
183 bool LeafExtensionProxyModel::hasChildren(
const QModelIndex &parent )
const
185 if ( d->mOwnIndexes.contains( parent ) ) {
189 const QModelIndex sourceParent = mapToSource( parent );
190 if ( sourceModel() && sourceModel()->rowCount( sourceParent ) == 0 ) {
191 return ( leafRowCount( parent ) != 0 );
194 return QSortFilterProxyModel::hasChildren( parent );
197 QModelIndex LeafExtensionProxyModel::buddy(
const QModelIndex &index )
const
199 if ( d->mOwnIndexes.contains( index ) ) {
203 return QSortFilterProxyModel::buddy( index );
206 void LeafExtensionProxyModel::fetchMore(
const QModelIndex &index )
208 if ( d->mOwnIndexes.contains( index ) ) {
212 QSortFilterProxyModel::fetchMore( index );
215 void LeafExtensionProxyModel::setSourceModel( QAbstractItemModel *_sourceModel )
217 if ( _sourceModel == sourceModel() ) {
223 disconnect(
this, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
224 this, SLOT(sourceRowsInserted(QModelIndex,
int,
int)) );
225 disconnect(
this, SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
226 this, SLOT(sourceRowsRemoved(QModelIndex,
int,
int)) );
228 QSortFilterProxyModel::setSourceModel( _sourceModel );
230 connect(
this, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
231 this, SLOT(sourceRowsInserted(QModelIndex,
int,
int)) );
232 connect(
this, SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
233 this, SLOT(sourceRowsRemoved(QModelIndex,
int,
int)) );
238 #include "moc_leafextensionproxymodel_p.cpp"