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

akonadi

  • akonadi
entityorderproxymodel.cpp
1 /*
2  Copyright (C) 2010 Klarälvdalens Datakonsult AB,
3  a KDAB Group company, info@kdab.net,
4  author Stephen Kelly <stephen@kdab.com>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 
23 #include "entityorderproxymodel.h"
24 
25 #include <QMimeData>
26 
27 #include <KDE/KConfigGroup>
28 
29 #include "collection.h"
30 #include "item.h"
31 #include "entitytreemodel.h"
32 
33 namespace Akonadi
34 {
35 
36 class EntityOrderProxyModelPrivate
37 {
38 public:
39  EntityOrderProxyModelPrivate( EntityOrderProxyModel *qq )
40  : q_ptr( qq )
41  {
42 
43  }
44 
45  void saveOrder( const QModelIndex &index );
46 
47  KConfigGroup m_orderConfig;
48 
49  Q_DECLARE_PUBLIC(EntityOrderProxyModel)
50  EntityOrderProxyModel * const q_ptr;
51 
52 };
53 
54 }
55 
56 using namespace Akonadi;
57 
58 EntityOrderProxyModel::EntityOrderProxyModel( QObject* parent )
59  : QSortFilterProxyModel(parent), d_ptr( new EntityOrderProxyModelPrivate( this ) )
60 {
61  setDynamicSortFilter(true);
62  //setSortCaseSensitivity( Qt::CaseInsensitive );
63 }
64 
65 EntityOrderProxyModel::~EntityOrderProxyModel()
66 {
67  delete d_ptr;
68 }
69 
70 void EntityOrderProxyModel::setOrderConfig( KConfigGroup& configGroup )
71 {
72  Q_D( EntityOrderProxyModel );
73  layoutAboutToBeChanged();
74  d->m_orderConfig = configGroup;
75  layoutChanged();
76 }
77 
78 bool EntityOrderProxyModel::lessThan( const QModelIndex& left, const QModelIndex& right ) const
79 {
80  Q_D( const EntityOrderProxyModel );
81 
82  if ( !d->m_orderConfig.isValid() ) {
83  return QSortFilterProxyModel::lessThan( left, right );
84  }
85  Collection col = left.data( EntityTreeModel::ParentCollectionRole ).value<Collection>();
86 
87  if ( !d->m_orderConfig.hasKey( QString::number( col.id() ) ) )
88  return QSortFilterProxyModel::lessThan( left, right );
89 
90  const QStringList list = d->m_orderConfig.readEntry( QString::number( col.id() ), QStringList() );
91 
92  if ( list.isEmpty() )
93  return QSortFilterProxyModel::lessThan( left, right );
94 
95  const QString leftValue = configString( left );
96  const QString rightValue = configString( right );
97 
98  const int leftPosition = list.indexOf( leftValue );
99  const int rightPosition = list.indexOf( rightValue );
100 
101  if ( leftPosition < 0 || rightPosition < 0 )
102  return QSortFilterProxyModel::lessThan( left, right );
103 
104  return leftPosition < rightPosition;
105 }
106 
107 bool EntityOrderProxyModel::dropMimeData( const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent )
108 {
109  Q_D( EntityOrderProxyModel );
110 
111  if ( !d->m_orderConfig.isValid() )
112  return QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );
113 
114  if ( !data->hasFormat( QLatin1String( "text/uri-list" ) ) )
115  return QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );
116 
117  if ( row == -1 )
118  return QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );
119 
120  bool containsMove = false;
121 
122  const KUrl::List urls = KUrl::List::fromMimeData( data );
123 
124  Collection parentCol;
125 
126  if (parent.isValid())
127  parentCol = parent.data( EntityTreeModel::CollectionRole ).value<Collection>();
128  else
129  {
130  if (!hasChildren(parent))
131  return QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );
132 
133  const QModelIndex targetIndex = index( 0, column, parent );
134 
135  parentCol = targetIndex.data( EntityTreeModel::ParentCollectionRole ).value<Collection>();
136  }
137 
138  QStringList droppedList;
139  foreach ( const KUrl &url, urls ) {
140  Collection col = Collection::fromUrl( url );
141 
142  if ( !col.isValid() )
143  {
144  Item item = Item::fromUrl( url );
145  if ( !item.isValid() )
146  continue;
147 
148  const QModelIndexList list = EntityTreeModel::modelIndexesForItem( this, item );
149  if ( list.isEmpty() )
150  continue;
151 
152  if ( !containsMove && list.first().data( EntityTreeModel::ParentCollectionRole ).value<Collection>().id() != parentCol.id() )
153  containsMove = true;
154 
155  droppedList << configString( list.first() );
156  } else {
157  const QModelIndex idx = EntityTreeModel::modelIndexForCollection( this, col );
158  if ( !idx.isValid() )
159  continue;
160 
161  if ( !containsMove && idx.data( EntityTreeModel::ParentCollectionRole ).value<Collection>().id() != parentCol.id() )
162  containsMove = true;
163 
164  droppedList << configString( idx );
165  }
166  }
167 
168  QStringList existingList;
169  if ( d->m_orderConfig.hasKey( QString::number( parentCol.id() ) ) ) {
170  existingList = d->m_orderConfig.readEntry( QString::number( parentCol.id() ), QStringList() );
171  } else {
172  const int rowCount = this->rowCount( parent );
173  for (int row = 0; row < rowCount; ++row) {
174  static const int column = 0;
175  const QModelIndex idx = this->index( row, column, parent );
176  existingList.append(configString( idx ));
177  }
178  }
179  const int numberOfDroppedElement( droppedList.size() );
180  for ( int i = 0; i < numberOfDroppedElement; ++i )
181  {
182  const QString droppedItem = droppedList.at( i );
183  const int existingIndex = existingList.indexOf( droppedItem );
184  existingList.removeAt( existingIndex );
185  existingList.insert( row + i - (existingIndex > row ? 0 : 1), droppedList.at( i ) );
186  }
187 
188  d->m_orderConfig.writeEntry( QString::number( parentCol.id() ), existingList );
189 
190  if ( containsMove )
191  {
192  bool result = QSortFilterProxyModel::dropMimeData( data, action, row, column, parent );
193  invalidate();
194  return result;
195  }
196  invalidate();
197  return true;
198 }
199 
200 QModelIndexList EntityOrderProxyModel::match( const QModelIndex& start, int role, const QVariant& value, int hits, Qt::MatchFlags flags ) const
201 {
202  if ( role < Qt::UserRole )
203  return QSortFilterProxyModel::match( start, role, value, hits, flags );
204 
205  QModelIndexList list;
206  QModelIndex proxyIndex;
207  foreach ( const QModelIndex &idx, sourceModel()->match( mapToSource( start ), role, value, hits, flags ) ) {
208  proxyIndex = mapFromSource( idx );
209  if ( proxyIndex.isValid() )
210  list << proxyIndex;
211  }
212 
213  return list;
214 }
215 
216 void EntityOrderProxyModelPrivate::saveOrder( const QModelIndex &parent )
217 {
218  Q_Q( const EntityOrderProxyModel );
219  int rowCount = q->rowCount( parent );
220 
221  if ( rowCount == 0 )
222  return;
223 
224  static const int column = 0;
225  QModelIndex childIndex = q->index( 0, column, parent );
226 
227  QString parentKey = q->parentConfigString( childIndex );
228 
229  if ( parentKey.isEmpty() )
230  return;
231 
232  QStringList list;
233 
234  list << q->configString( childIndex );
235  saveOrder( childIndex );
236 
237  for ( int row = 1; row < rowCount; ++row )
238  {
239  childIndex = q->index( row, column, parent );
240  list << q->configString( childIndex );
241  saveOrder( childIndex );
242  }
243 
244  m_orderConfig.writeEntry( parentKey, list );
245 }
246 
247 QString EntityOrderProxyModel::parentConfigString( const QModelIndex& index ) const
248 {
249  const Collection col = index.data( EntityTreeModel::ParentCollectionRole ).value<Collection>();
250 
251  Q_ASSERT( col.isValid() );
252  if ( !col.isValid() )
253  return QString();
254 
255  return QString::number( col.id() );
256 }
257 
258 QString EntityOrderProxyModel::configString( const QModelIndex& index ) const
259 {
260  Entity::Id eId = index.data( EntityTreeModel::ItemIdRole ).toLongLong();
261  if ( eId != -1 )
262  {
263  return QString::fromLatin1( "i" ) + QString::number( eId );
264  }
265  eId = index.data( EntityTreeModel::CollectionIdRole ).toLongLong();
266  if ( eId != -1 )
267  {
268  return QString::fromLatin1( "c" ) + QString::number( eId );
269  }
270  Q_ASSERT( !"Invalid entity" );
271  return QString();
272 }
273 
274 void EntityOrderProxyModel::saveOrder()
275 {
276  Q_D( EntityOrderProxyModel );
277  d->saveOrder( QModelIndex() );
278  d->m_orderConfig.sync();
279 }
280 
281 void EntityOrderProxyModel::clearOrder( const QModelIndex& parent )
282 {
283  Q_D( EntityOrderProxyModel );
284 
285  const QString parentKey = parentConfigString( index( 0, 0, parent ) );
286 
287  if ( parentKey.isEmpty() )
288  return;
289 
290  d->m_orderConfig.deleteEntry( parentKey );
291  invalidate();
292 }
293 
294 void EntityOrderProxyModel::clearTreeOrder()
295 {
296  Q_D( EntityOrderProxyModel );
297  d->m_orderConfig.deleteGroup();
298  invalidate();
299 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jan 5 2013 19:46:03 by doxygen 1.8.1.2 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-4.9.5 API Reference

Skip menu "kdepimlibs-4.9.5 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