akonadi
etmviewstatesaver.cpp
00001 /* 00002 Copyright (C) 2010 Klarälvdalens Datakonsult AB, 00003 a KDAB Group company, info@kdab.net, 00004 author Stephen Kelly <stephen@kdab.com> 00005 00006 This library is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU Library General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or (at your 00009 option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, but WITHOUT 00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 02110-1301, USA. 00020 */ 00021 00022 #include "etmviewstatesaver.h" 00023 00024 #include <QtCore/QModelIndex> 00025 #include <QtGui/QItemSelection> 00026 #include <QtGui/QTreeView> 00027 00028 #include "entitytreemodel.h" 00029 00030 using namespace Akonadi; 00031 00032 ETMViewStateSaver::ETMViewStateSaver(QObject* parent) 00033 : KViewStateSaver(parent) 00034 { 00035 } 00036 00037 QModelIndex ETMViewStateSaver::indexFromConfigString(const QAbstractItemModel *model, const QString& key) const 00038 { 00039 if ( key.startsWith( QLatin1Char( 'x' ) ) ) 00040 return QModelIndex(); 00041 00042 Entity::Id id = key.mid( 1 ).toLongLong(); 00043 if ( id < 0 ) 00044 return QModelIndex(); 00045 00046 if ( key.startsWith( QLatin1Char( 'c' ) ) ) 00047 { 00048 QModelIndex idx = EntityTreeModel::modelIndexForCollection( model, Collection( id ) ); 00049 if ( !idx.isValid() ) 00050 { 00051 kDebug() << "Collection with id" << id << "is not in model yet"; 00052 return QModelIndex(); 00053 } 00054 return idx; 00055 } 00056 else if ( key.startsWith( QLatin1Char( 'i' ) ) ) 00057 { 00058 QModelIndexList list = EntityTreeModel::modelIndexesForItem( model, Item( id ) ); 00059 if ( list.isEmpty() ) 00060 { 00061 kDebug() << "Item with id" << id << "is not in model yet"; 00062 return QModelIndex(); 00063 } 00064 return list.first(); 00065 } 00066 return QModelIndex(); 00067 } 00068 00069 QString ETMViewStateSaver::indexToConfigString(const QModelIndex& index) const 00070 { 00071 if ( !index.isValid() ) 00072 return QLatin1String( "x-1" ); 00073 const Collection c = index.data( EntityTreeModel::CollectionRole ).value<Collection>(); 00074 if ( c.isValid() ) 00075 return QString::fromLatin1( "c%1" ).arg( c.id() ); 00076 Entity::Id id = index.data( EntityTreeModel::ItemIdRole ).value<Entity::Id>(); 00077 if ( id >= 0 ) 00078 return QString::fromLatin1( "i%1" ).arg( id ); 00079 return QString(); 00080 } 00081 00082 void ETMViewStateSaver::selectCollections(const Akonadi::Collection::List& list) 00083 { 00084 QStringList colStrings; 00085 foreach(const Collection &col, list) 00086 colStrings << QString::fromLatin1( "c%1" ).arg( col.id() ); 00087 restoreSelection(colStrings); 00088 } 00089 00090 void ETMViewStateSaver::selectCollections(const QList< Collection::Id >& list) 00091 { 00092 QStringList colStrings; 00093 foreach(const Collection::Id &colId, list) 00094 colStrings << QString::fromLatin1( "c%1" ).arg( colId ); 00095 restoreSelection(colStrings); 00096 } 00097 00098 void ETMViewStateSaver::selectItems(const Akonadi::Item::List& list) 00099 { 00100 QStringList itemStrings; 00101 foreach(const Item &item, list) 00102 itemStrings << QString::fromLatin1( "i%1" ).arg( item.id() ); 00103 restoreSelection(itemStrings); 00104 } 00105 00106 void ETMViewStateSaver::selectItems(const QList< Item::Id >& list) 00107 { 00108 QStringList itemStrings; 00109 foreach(const Item::Id &itemId, list) 00110 itemStrings << QString::fromLatin1( "i%1" ).arg( itemId ); 00111 restoreSelection(itemStrings); 00112 }