22 #include "entitymimetypefiltermodel.h"
24 #include "entitytreemodel.h"
25 #include "mimetypechecker.h"
28 #include <kmimetype.h>
30 #include <QtCore/QString>
31 #include <QtCore/QStringList>
33 using namespace Akonadi;
39 class EntityMimeTypeFilterModelPrivate
51 QStringList includedMimeTypes;
52 QStringList excludedMimeTypes;
54 QPersistentModelIndex m_rootIndex;
62 : QSortFilterProxyModel( parent ),
63 d_ptr( new EntityMimeTypeFilterModelPrivate( this ) )
75 d->includedMimeTypes << typeList;
82 d->excludedMimeTypes << typeList;
89 d->includedMimeTypes << type;
96 d->excludedMimeTypes << type;
100 bool EntityMimeTypeFilterModel::filterAcceptsColumn(
int sourceColumn,
const QModelIndex &sourceParent )
const
102 if ( sourceColumn >= columnCount( mapFromSource( sourceParent ) ) ) {
105 return QSortFilterProxyModel::filterAcceptsColumn( sourceColumn, sourceParent );
108 bool EntityMimeTypeFilterModel::filterAcceptsRow(
int sourceRow,
const QModelIndex &sourceParent)
const
111 const QModelIndex idx = sourceModel()->index( sourceRow, 0, sourceParent );
116 kDebug() <<
"Item " << item.
id() <<
" doesn't have payload";
122 if ( d->excludedMimeTypes.contains( rowMimetype ) ) {
125 if ( d->includedMimeTypes.isEmpty() ||
126 d->includedMimeTypes.contains( rowMimetype ) ) {
136 return d->includedMimeTypes;
142 return d->excludedMimeTypes;
148 d->includedMimeTypes.clear();
149 d->excludedMimeTypes.clear();
156 d->m_headerGroup = headerGroup;
159 QVariant EntityMimeTypeFilterModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
161 if ( !sourceModel() ) {
167 return sourceModel()->headerData( section, orientation, role );
170 QModelIndexList EntityMimeTypeFilterModel::match(
const QModelIndex& start,
int role,
const QVariant& value,
int hits, Qt::MatchFlags flags )
const
172 if ( !sourceModel() ) {
173 return QModelIndexList();
177 if ( role < Qt::UserRole ) {
178 return QSortFilterProxyModel::match( start, role, value, hits, flags );
181 QModelIndexList list;
182 QModelIndex proxyIndex;
183 foreach (
const QModelIndex &idx, sourceModel()->match( mapToSource( start ), role, value, hits, flags ) ) {
184 proxyIndex = mapFromSource( idx );
185 if ( proxyIndex.isValid() ) {
193 const int allHits = -1;
195 QModelIndexList proxyList;
196 QMap<int, QModelIndex> proxyMap;
197 const QModelIndexList sourceList = sourceModel()->match( mapToSource( start ), role, value, allHits, flags );
198 QModelIndexList::const_iterator it;
199 const QModelIndexList::const_iterator begin = sourceList.constBegin();
200 const QModelIndexList::const_iterator end = sourceList.constEnd();
201 QModelIndex proxyIndex;
202 for ( it = begin; it != end; ++it ) {
203 proxyIndex = mapFromSource( *it );
206 if ( !proxyIndex.isValid() ) {
211 proxyMap.insert( proxyIndex.row(), proxyIndex );
215 return proxyMap.values();
218 return proxyMap.values().mid( 0, hits );
221 int EntityMimeTypeFilterModel::columnCount(
const QModelIndex &parent)
const
225 if ( !sourceModel() ) {
230 if ( !value.isValid() ) {
234 return value.toInt();
237 bool EntityMimeTypeFilterModel::hasChildren(
const QModelIndex &parent)
const
239 if ( !sourceModel() ) {
247 return rowCount( parent ) > 0;
250 if ( !parent.isValid() ) {
251 return sourceModel()->hasChildren( parent );
260 QModelIndex childIndex = parent.child( 0, 0 );
261 while ( childIndex.isValid() ) {
266 childIndex = childIndex.sibling( childIndex.row() + 1, childIndex.column() );
273 bool EntityMimeTypeFilterModel::canFetchMore(
const QModelIndex &parent )
const
279 return QSortFilterProxyModel::canFetchMore( parent );