• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

digikam

albumfiltermodel.cpp

Go to the documentation of this file.
00001 /* ============================================================
00002  *
00003  * This file is a part of digiKam project
00004  * http://www.digikam.org
00005  *
00006  * Date        : 2009-03-24
00007  * Description : Qt Model for Albums - filter model
00008  *
00009  * Copyright (C) 2008-2009 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
00010  *
00011  * This program is free software; you can redistribute it
00012  * and/or modify it under the terms of the GNU General
00013  * Public License as published by the Free Software Foundation;
00014  * either version 2, or (at your option)
00015  * any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * ============================================================ */
00023 
00024 #include "albumfiltermodel.moc"
00025 
00026 // Qt includes
00027 
00028 #include <QSortFilterProxyModel>
00029 
00030 // KDE includes
00031 
00032 #include <kstringhandler.h>
00033 
00034 // Local includes
00035 
00036 #include "albummodel.h"
00037 
00038 namespace Digikam
00039 {
00040 
00041 AlbumFilterModel::AlbumFilterModel(QObject *parent)
00042     : QSortFilterProxyModel(parent)
00043 {
00044     setDynamicSortFilter(true);
00045 }
00046 
00047 void AlbumFilterModel::setSearchTextSettings(const SearchTextSettings& settings)
00048 {
00049     m_settings = settings;
00050     invalidateFilter();
00051     emit filterChanged();
00052 }
00053 
00054 bool AlbumFilterModel::isFiltering() const
00055 {
00056     return !m_settings.text.isEmpty();
00057 }
00058 
00059 SearchTextSettings AlbumFilterModel::searchTextSettings() const
00060 {
00061     return m_settings;
00062 }
00063 
00064 void AlbumFilterModel::setSourceAlbumModel(AbstractAlbumModel *source)
00065 {
00066     setSourceModel(source);
00067 }
00068 
00069 void AlbumFilterModel::setSourceModel(QAbstractItemModel* model)
00070 {
00071     // made it protected, only setSourceAlbumModel is public
00072     QSortFilterProxyModel::setSourceModel(model);
00073 }
00074 
00075 AbstractAlbumModel *AlbumFilterModel::sourceAlbumModel() const
00076 {
00077     return static_cast<AbstractAlbumModel*>(sourceModel());
00078 }
00079 
00080 Album *AlbumFilterModel::albumForIndex(const QModelIndex& index) const
00081 {
00082     return sourceAlbumModel()->albumForIndex(mapToSource(index));
00083 }
00084 
00085 QModelIndex AlbumFilterModel::indexForAlbum(Album *album) const
00086 {
00087     return mapFromSource(sourceAlbumModel()->indexForAlbum(album));
00088 }
00089 
00090 QModelIndex AlbumFilterModel::rootAlbumIndex() const
00091 {
00092     return mapFromSource(sourceAlbumModel()->rootAlbumIndex());
00093 }
00094 
00095 AlbumFilterModel::MatchResult AlbumFilterModel::matches(const QModelIndex& index) const
00096 {
00097     return matches(sourceAlbumModel()->albumForIndex(mapToSource(index)));
00098 }
00099 
00100 AlbumFilterModel::MatchResult AlbumFilterModel::matches(Album *album) const
00101 {
00102     if (!album)
00103         return NoMatch;
00104 
00105     PAlbum *palbum = dynamic_cast<PAlbum*>(album);
00106 
00107     if (album->isRoot() || (palbum && palbum->isAlbumRoot()))
00108         return SpecialMatch;
00109 
00110     if (album->title().contains(m_settings.text, m_settings.caseSensitive))
00111         return TitleMatch;
00112 
00113     // check if any of the parents match the search
00114     Album *parent = album->parent();
00115     PAlbum* pparent = palbum ? static_cast<PAlbum*>(parent) : 0;
00116 
00117     while (parent && !(parent->isRoot() || (pparent && pparent->isAlbumRoot()) ) )
00118     {
00119         if (parent->title().contains(m_settings.text, m_settings.caseSensitive))
00120             return ParentMatch;
00121 
00122         parent = parent->parent();
00123     }
00124 
00125     // check if any of the children match the search
00126     AlbumIterator it(album);
00127     while (it.current())
00128     {
00129         if ((*it)->title().contains(m_settings.text, m_settings.caseSensitive))
00130             return ChildMatch;
00131         ++it;
00132     }
00133 
00134     return NoMatch;
00135 }
00136 
00137 bool AlbumFilterModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
00138 {
00139     QModelIndex index = sourceAlbumModel()->index(source_row, 0, source_parent);
00140     Album *album = sourceAlbumModel()->albumForIndex(index);
00141     if (!album)
00142         return false;
00143     MatchResult result = matches(album);
00144     return result;
00145 }
00146 
00147 bool AlbumFilterModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
00148 {
00149     QVariant valLeft = left.data(sortRole());
00150     QVariant valRight = right.data(sortRole());
00151 
00152     if (valLeft.type() == QVariant::String && valRight.type() == QVariant::String)
00153         return KStringHandler::naturalCompare(valLeft.toString(), valRight.toString(), sortCaseSensitivity()) < 0;
00154     else
00155         return QSortFilterProxyModel::lessThan(left, right);
00156 }
00157 
00158 } // namespace Digikam

digikam

Skip menu "digikam"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • digikam
Generated for API Reference by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal