akonadi
recursivecollectionfilterproxymodel.cpp
00001 /* 00002 Copyright (c) 2009 Stephen Kelly <steveire@gmail.com> 00003 Copyright (c) 2012 Laurent Montel <montel@kde.org> 00004 00005 This library is free software; you can redistribute it and/or modify it 00006 under the terms of the GNU Library General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or (at your 00008 option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, but WITHOUT 00011 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to the 00017 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00018 02110-1301, USA. 00019 */ 00020 00021 #include "recursivecollectionfilterproxymodel.h" 00022 00023 #include "entitytreemodel.h" 00024 #include "mimetypechecker.h" 00025 00026 #include <kdebug.h> 00027 00028 using namespace Akonadi; 00029 00030 namespace Akonadi 00031 { 00032 00033 class RecursiveCollectionFilterProxyModelPrivate 00034 { 00035 Q_DECLARE_PUBLIC(RecursiveCollectionFilterProxyModel) 00036 RecursiveCollectionFilterProxyModel *q_ptr; 00037 public: 00038 RecursiveCollectionFilterProxyModelPrivate(RecursiveCollectionFilterProxyModel *model) 00039 : q_ptr(model) 00040 { 00041 00042 } 00043 00044 QSet<QString> includedMimeTypes; 00045 Akonadi::MimeTypeChecker checker; 00046 QString pattern; 00047 }; 00048 00049 } 00050 00051 RecursiveCollectionFilterProxyModel::RecursiveCollectionFilterProxyModel( QObject* parent ) 00052 : KRecursiveFilterProxyModel(parent), d_ptr(new RecursiveCollectionFilterProxyModelPrivate( this ) ) 00053 { 00054 00055 } 00056 00057 RecursiveCollectionFilterProxyModel::~RecursiveCollectionFilterProxyModel() 00058 { 00059 delete d_ptr; 00060 } 00061 00062 bool RecursiveCollectionFilterProxyModel::acceptRow( int sourceRow, const QModelIndex &sourceParent ) const 00063 { 00064 Q_D( const RecursiveCollectionFilterProxyModel ); 00065 00066 const QModelIndex rowIndex = sourceModel()->index( sourceRow, 0, sourceParent ); 00067 const Akonadi::Collection collection = rowIndex.data( Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>(); 00068 if ( !collection.isValid() ) 00069 return false; 00070 const bool collectionWanted = d->checker.isWantedCollection( collection ); 00071 if ( collectionWanted ) 00072 { 00073 if ( !d->pattern.isEmpty() ) { 00074 const QString text = rowIndex.data(Qt::DisplayRole).toString(); 00075 return text.contains( d->pattern, Qt::CaseInsensitive ); 00076 } 00077 } 00078 return collectionWanted; 00079 } 00080 00081 void RecursiveCollectionFilterProxyModel::addContentMimeTypeInclusionFilter(const QString& mimeType) 00082 { 00083 Q_D(RecursiveCollectionFilterProxyModel); 00084 d->includedMimeTypes << mimeType; 00085 d->checker.setWantedMimeTypes( d->includedMimeTypes.toList() ); 00086 invalidateFilter(); 00087 } 00088 00089 void RecursiveCollectionFilterProxyModel::addContentMimeTypeInclusionFilters(const QStringList& mimeTypes) 00090 { 00091 Q_D(RecursiveCollectionFilterProxyModel); 00092 d->includedMimeTypes.unite(mimeTypes.toSet()); 00093 d->checker.setWantedMimeTypes( d->includedMimeTypes.toList() ); 00094 invalidateFilter(); 00095 } 00096 00097 void RecursiveCollectionFilterProxyModel::clearFilters() 00098 { 00099 Q_D(RecursiveCollectionFilterProxyModel); 00100 d->includedMimeTypes.clear(); 00101 d->checker.setWantedMimeTypes( QStringList() ); 00102 invalidateFilter(); 00103 } 00104 00105 void RecursiveCollectionFilterProxyModel::setContentMimeTypeInclusionFilters(const QStringList& mimeTypes) 00106 { 00107 Q_D(RecursiveCollectionFilterProxyModel); 00108 d->includedMimeTypes = mimeTypes.toSet(); 00109 d->checker.setWantedMimeTypes( d->includedMimeTypes.toList() ); 00110 invalidateFilter(); 00111 } 00112 00113 QStringList RecursiveCollectionFilterProxyModel::contentMimeTypeInclusionFilters() const 00114 { 00115 Q_D(const RecursiveCollectionFilterProxyModel); 00116 return d->includedMimeTypes.toList(); 00117 } 00118 00119 int Akonadi::RecursiveCollectionFilterProxyModel::columnCount( const QModelIndex& index ) const 00120 { 00121 // Optimization: we know that we're not changing the number of columns, so skip QSortFilterProxyModel 00122 return sourceModel()->columnCount( mapToSource( index ) ); 00123 } 00124 00125 void Akonadi::RecursiveCollectionFilterProxyModel::setSearchPattern( const QString &pattern ) 00126 { 00127 Q_D(RecursiveCollectionFilterProxyModel); 00128 d->pattern = pattern; 00129 invalidate(); 00130 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:35 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:35 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.