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;
43 qint64 mUniqueKeyCounter;
46 void LeafExtensionProxyModel::Private::sourceRowsInserted(
const QModelIndex &parentIndex,
int start,
int end )
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 )
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 )
83 LeafExtensionProxyModel::~LeafExtensionProxyModel()
90 if ( row < 0 || column < 0 ) {
95 const QModelIndex sourceParent = mapToSource( parent );
96 const QModelIndex sourceIndex = sourceModel()->index( row, column, sourceParent );
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 );
126 if ( d->mOwnIndexes.contains( index ) ) {
127 return d->mParentIndexes.value( index.
internalId() );
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 );
147 int LeafExtensionProxyModel::columnCount(
const QModelIndex &index )
const
149 if ( d->mOwnIndexes.contains( index ) ) {
158 if ( d->mOwnIndexes.contains( index ) ) {
167 if ( d->mOwnIndexes.contains( index ) ) {
168 return Qt::ItemFlags( Qt::ItemIsEnabled|Qt::ItemIsSelectable );
174 bool LeafExtensionProxyModel::setData(
const QModelIndex &index,
const QVariant &data,
int role )
176 if ( d->mOwnIndexes.contains( index ) ) {
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 );
199 if ( d->mOwnIndexes.contains( index ) ) {
206 void LeafExtensionProxyModel::fetchMore(
const QModelIndex &index )
208 if ( d->mOwnIndexes.contains( index ) ) {
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)) );
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"
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
virtual Qt::ItemFlags flags(const QModelIndex &index) const
virtual void setSourceModel(QAbstractItemModel *sourceModel)
qint64 internalId() const
virtual int rowCount(const QModelIndex &parent) const
virtual int columnCount(const QModelIndex &parent) const
QModelIndex parent() const
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
virtual bool hasChildren(const QModelIndex &parent) const
virtual QModelIndex buddy(const QModelIndex &index) const
virtual void fetchMore(const QModelIndex &parent)
virtual QVariant data(const QModelIndex &index, int role) const