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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • 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 
94  if ( parent.isValid() ) {
95  const QModelIndex sourceParent = mapToSource( parent );
96  const QModelIndex sourceIndex = sourceModel()->index( row, column, sourceParent );
97  if ( !sourceIndex.isValid() ) {
98 
99  qint64 key = -1;
100  QMapIterator<qint64, QModelIndex> it( d->mParentIndexes );
101  while ( it.hasNext() ) {
102  it.next();
103  if ( it.value() == parent ) {
104  key = it.key();
105  break;
106  }
107  }
108 
109  if ( key == -1 ) {
110  key = ++( d->mUniqueKeyCounter );
111  d->mParentIndexes.insert( key, parent );
112  }
113 
114  const QModelIndex index = createIndex( row, column, static_cast<quint32>( key ) );
115  d->mOwnIndexes.insert( index );
116 
117  return index;
118  }
119  }
120 
121  return QSortFilterProxyModel::index( row, column, parent );
122 }
123 
124 QModelIndex LeafExtensionProxyModel::parent( const QModelIndex &index ) const
125 {
126  if ( d->mOwnIndexes.contains( index ) ) {
127  return d->mParentIndexes.value( index.internalId() );
128  }
129 
130  return QSortFilterProxyModel::parent( index );
131 }
132 
133 int LeafExtensionProxyModel::rowCount( const QModelIndex &index ) const
134 {
135  if ( d->mOwnIndexes.contains( index ) ) {
136  return 0;
137  }
138 
139  const QModelIndex sourceIndex = mapToSource( index );
140  if ( sourceModel()->rowCount( sourceIndex ) == 0 ) {
141  return leafRowCount( index );
142  }
143 
144  return QSortFilterProxyModel::rowCount( index );
145 }
146 
147 int LeafExtensionProxyModel::columnCount( const QModelIndex &index ) const
148 {
149  if ( d->mOwnIndexes.contains( index ) ) {
150  return 1;
151  }
152 
153  return QSortFilterProxyModel::columnCount( index );
154 }
155 
156 QVariant LeafExtensionProxyModel::data( const QModelIndex &index, int role ) const
157 {
158  if ( d->mOwnIndexes.contains( index ) ) {
159  return leafData( index.parent(), index.row(), index.column(), role );
160  }
161 
162  return QSortFilterProxyModel::data( index, role );
163 }
164 
165 Qt::ItemFlags LeafExtensionProxyModel::flags( const QModelIndex &index ) const
166 {
167  if ( d->mOwnIndexes.contains( index ) ) {
168  return Qt::ItemFlags( Qt::ItemIsEnabled|Qt::ItemIsSelectable );
169  }
170 
171  return QSortFilterProxyModel::flags( index );
172 }
173 
174 bool LeafExtensionProxyModel::setData( const QModelIndex &index, const QVariant &data, int role )
175 {
176  if ( d->mOwnIndexes.contains( index ) ) {
177  return false;
178  }
179 
180  return QSortFilterProxyModel::setData( index, data, role );
181 }
182 
183 bool LeafExtensionProxyModel::hasChildren( const QModelIndex &parent ) const
184 {
185  if ( d->mOwnIndexes.contains( parent ) ) {
186  return false; // extensible in the future?
187  }
188 
189  const QModelIndex sourceParent = mapToSource( parent );
190  if ( sourceModel() && sourceModel()->rowCount( sourceParent ) == 0 ) {
191  return ( leafRowCount( parent ) != 0 );
192  }
193 
194  return QSortFilterProxyModel::hasChildren( parent );
195 }
196 
197 QModelIndex LeafExtensionProxyModel::buddy( const QModelIndex &index ) const
198 {
199  if ( d->mOwnIndexes.contains( index ) ) {
200  return index;
201  }
202 
203  return QSortFilterProxyModel::buddy( index );
204 }
205 
206 void LeafExtensionProxyModel::fetchMore( const QModelIndex &index )
207 {
208  if ( d->mOwnIndexes.contains( index ) ) {
209  return;
210  }
211 
212  QSortFilterProxyModel::fetchMore( index );
213 }
214 
215 void LeafExtensionProxyModel::setSourceModel( QAbstractItemModel *_sourceModel )
216 {
217  if ( _sourceModel == sourceModel() ) {
218  return;
219  }
220 
221  beginResetModel();
222 
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)) );
227 
228  QSortFilterProxyModel::setSourceModel( _sourceModel );
229 
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)) );
234 
235  endResetModel();
236 }
237 
238 #include "moc_leafextensionproxymodel_p.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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