• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 API Reference
  • KDE Home
  • Contact Us
 

akonadi/contact

  • akonadi
  • contact
leafextensionproxymodel.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2010 KDAB
5  Author: Tobias Koenig <tokoe@kde.org>
6 
7  This library is free software; you can redistribute it and/or modify it
8  under the terms of the GNU Library General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but WITHOUT
13  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15  License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to the
19  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  02110-1301, USA.
21 */
22 
23 #include "leafextensionproxymodel_p.h"
24 
25 #include <QtCore/QSet>
26 
27 using namespace Akonadi;
28 
29 class LeafExtensionProxyModel::Private
30 {
31  public:
32  Private( LeafExtensionProxyModel *qq )
33  : q( qq ), mUniqueKeyCounter( 0 )
34  {
35  }
36 
37  void sourceRowsInserted( const QModelIndex&, int, int );
38  void sourceRowsRemoved( const QModelIndex&, int, int );
39 
40  LeafExtensionProxyModel *q;
41  QMap<qint64, QModelIndex> mParentIndexes;
42  QSet<QModelIndex> mOwnIndexes;
43  qint64 mUniqueKeyCounter;
44 };
45 
46 void LeafExtensionProxyModel::Private::sourceRowsInserted( const QModelIndex &parentIndex, int start, int end )
47 {
48  // iterate over all of our stored parent indexes
49  QMutableMapIterator<qint64, QModelIndex> it( mParentIndexes );
50  while ( it.hasNext() ) {
51  it.next();
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 );
56  }
57  }
58  }
59 }
60 
61 void LeafExtensionProxyModel::Private::sourceRowsRemoved( const QModelIndex &parentIndex, int start, int end )
62 {
63  // iterate over all of our stored parent indexes
64  QMutableMapIterator<qint64, QModelIndex> it( mParentIndexes );
65  while ( it.hasNext() ) {
66  it.next();
67  if ( it.value().parent() == parentIndex ) {
68  if ( it.value().row() >= start && it.value().row() <= end ) {
69  it.remove();
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 );
73  }
74  }
75  }
76 }
77 
78 LeafExtensionProxyModel::LeafExtensionProxyModel( QObject *parent )
79  : QSortFilterProxyModel( parent ), d( new Private( this ) )
80 {
81 }
82 
83 LeafExtensionProxyModel::~LeafExtensionProxyModel()
84 {
85  delete d;
86 }
87 
88 QModelIndex LeafExtensionProxyModel::index( int row, int column, const QModelIndex &parent ) const
89 {
90  if ( row < 0 || column < 0 )
91  return QModelIndex();
92 
93  if ( parent.isValid() ) {
94  const QModelIndex sourceParent = mapToSource( parent );
95  const QModelIndex sourceIndex = sourceModel()->index( row, column, sourceParent );
96  if ( !sourceIndex.isValid() ) {
97 
98  qint64 key = -1;
99  QMapIterator<qint64, QModelIndex> it( d->mParentIndexes );
100  while ( it.hasNext() ) {
101  it.next();
102  if ( it.value() == parent ) {
103  key = it.key();
104  break;
105  }
106  }
107 
108  if ( key == -1 ) {
109  key = ++(d->mUniqueKeyCounter);
110  d->mParentIndexes.insert( key, parent );
111  }
112 
113  const QModelIndex index = createIndex( row, column, static_cast<quint32>( key ) );
114  d->mOwnIndexes.insert( index );
115 
116  return index;
117  }
118  }
119 
120  return QSortFilterProxyModel::index( row, column, parent );
121 }
122 
123 QModelIndex LeafExtensionProxyModel::parent( const QModelIndex &index ) const
124 {
125  if ( d->mOwnIndexes.contains( index ) )
126  return d->mParentIndexes.value( index.internalId() );
127 
128  return QSortFilterProxyModel::parent( index );
129 }
130 
131 int LeafExtensionProxyModel::rowCount( const QModelIndex &index ) const
132 {
133  if ( d->mOwnIndexes.contains( index ) )
134  return 0;
135 
136  const QModelIndex sourceIndex = mapToSource( index );
137  if ( sourceModel()->rowCount( sourceIndex ) == 0 )
138  return leafRowCount( index );
139 
140  return QSortFilterProxyModel::rowCount( index );
141 }
142 
143 int LeafExtensionProxyModel::columnCount( const QModelIndex &index ) const
144 {
145  if ( d->mOwnIndexes.contains( index ) )
146  return 1;
147 
148  return QSortFilterProxyModel::columnCount( index );
149 }
150 
151 QVariant LeafExtensionProxyModel::data( const QModelIndex &index, int role ) const
152 {
153  if ( d->mOwnIndexes.contains( index ) )
154  return leafData( index.parent(), index.row(), index.column(), role );
155 
156  return QSortFilterProxyModel::data( index, role );
157 }
158 
159 Qt::ItemFlags LeafExtensionProxyModel::flags( const QModelIndex &index ) const
160 {
161  if ( d->mOwnIndexes.contains( index ) )
162  return Qt::ItemFlags( Qt::ItemIsEnabled|Qt::ItemIsSelectable );
163 
164  return QSortFilterProxyModel::flags( index );
165 }
166 
167 bool LeafExtensionProxyModel::setData( const QModelIndex &index, const QVariant &data, int role )
168 {
169  if ( d->mOwnIndexes.contains( index ) )
170  return false;
171 
172  return QSortFilterProxyModel::setData( index, data, role );
173 }
174 
175 bool LeafExtensionProxyModel::hasChildren( const QModelIndex &parent ) const
176 {
177  if ( d->mOwnIndexes.contains( parent ) )
178  return false; // extensible in the future?
179 
180  const QModelIndex sourceParent = mapToSource( parent );
181  if ( sourceModel() && sourceModel()->rowCount( sourceParent ) == 0 )
182  return (leafRowCount( parent ) != 0);
183 
184  return QSortFilterProxyModel::hasChildren( parent );
185 }
186 
187 QModelIndex LeafExtensionProxyModel::buddy( const QModelIndex &index ) const
188 {
189  if ( d->mOwnIndexes.contains( index ) )
190  return index;
191 
192  return QSortFilterProxyModel::buddy( index );
193 }
194 
195 void LeafExtensionProxyModel::fetchMore( const QModelIndex &index )
196 {
197  if ( d->mOwnIndexes.contains( index ) )
198  return;
199 
200  QSortFilterProxyModel::fetchMore( index );
201 }
202 
203 void LeafExtensionProxyModel::setSourceModel( QAbstractItemModel *_sourceModel )
204 {
205  if ( _sourceModel == sourceModel() )
206  return;
207 
208  beginResetModel();
209 
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)) );
214 
215  QSortFilterProxyModel::setSourceModel( _sourceModel );
216 
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)) );
221 
222  endResetModel();
223 }
224 
225 #include "leafextensionproxymodel_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 4 2012 14:36:30 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal