compile
[kdegraphics.git] / gwenview / lib / semanticinfo / sorteddirmodel.cpp
blobf6131f4e6ab5d98c73d4073e0e9ea46bf27141c4
1 /*
2 Gwenview: an image viewer
3 Copyright 2007 Aurélien Gâteau <aurelien.gateau@free.fr>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "sorteddirmodel.moc"
21 #include <config-gwenview.h>
23 // Qt
25 // KDE
26 #include <kdebug.h>
27 #include <kdatetime.h>
28 #include <kdirlister.h>
30 // Local
31 #include <lib/archiveutils.h>
32 #include <lib/timeutils.h>
33 #ifdef GWENVIEW_SEMANTICINFO_BACKEND_NONE
34 #include <kdirmodel.h>
35 #else
36 #include "abstractsemanticinfobackend.h"
37 #include "semanticinfodirmodel.h"
38 #endif
40 namespace Gwenview {
42 AbstractSortedDirModelFilter::AbstractSortedDirModelFilter(SortedDirModel* model)
43 : QObject(model)
44 , mModel(model)
46 if (mModel) {
47 mModel->addFilter(this);
51 AbstractSortedDirModelFilter::~AbstractSortedDirModelFilter()
53 if (mModel) {
54 mModel->removeFilter(this);
59 struct SortedDirModelPrivate {
60 #ifdef GWENVIEW_SEMANTICINFO_BACKEND_NONE
61 KDirModel* mSourceModel;
62 #else
63 SemanticInfoDirModel* mSourceModel;
64 #endif
65 QStringList mBlackListedExtensions;
66 QStringList mMimeExcludeFilter;
67 QList<AbstractSortedDirModelFilter*> mFilters;
68 QTimer mDelayedApplyFiltersTimer;
72 SortedDirModel::SortedDirModel(QObject* parent)
73 : KDirSortFilterProxyModel(parent)
74 , d(new SortedDirModelPrivate)
76 #ifdef GWENVIEW_SEMANTICINFO_BACKEND_NONE
77 d->mSourceModel = new KDirModel(this);
78 #else
79 d->mSourceModel = new SemanticInfoDirModel(this);
80 #endif
81 setSourceModel(d->mSourceModel);
82 d->mDelayedApplyFiltersTimer.setInterval(0);
83 d->mDelayedApplyFiltersTimer.setSingleShot(true);
84 connect(&d->mDelayedApplyFiltersTimer, SIGNAL(timeout()), SLOT(doApplyFilters()));
88 SortedDirModel::~SortedDirModel() {
89 delete d;
93 void SortedDirModel::addFilter(AbstractSortedDirModelFilter* filter) {
94 d->mFilters << filter;
95 applyFilters();
99 void SortedDirModel::removeFilter(AbstractSortedDirModelFilter* filter) {
100 d->mFilters.removeAll(filter);
101 applyFilters();
105 KDirLister* SortedDirModel::dirLister() {
106 return d->mSourceModel->dirLister();
110 void SortedDirModel::reload() {
111 #ifndef GWENVIEW_SEMANTICINFO_BACKEND_NONE
112 d->mSourceModel->clearSemanticInfoCache();
113 #endif
114 dirLister()->updateDirectory(dirLister()->url());
118 void SortedDirModel::setBlackListedExtensions(const QStringList& list) {
119 d->mBlackListedExtensions = list;
123 KFileItem SortedDirModel::itemForIndex(const QModelIndex& index) const {
124 if (!index.isValid()) {
125 return KFileItem();
128 QModelIndex sourceIndex = mapToSource(index);
129 return d->mSourceModel->itemForIndex(sourceIndex);
133 KFileItem SortedDirModel::itemForSourceIndex(const QModelIndex& sourceIndex) const {
134 if (!sourceIndex.isValid()) {
135 return KFileItem();
137 return d->mSourceModel->itemForIndex(sourceIndex);
141 QModelIndex SortedDirModel::indexForItem(const KFileItem& item) const {
142 if (item.isNull()) {
143 return QModelIndex();
146 QModelIndex sourceIndex = d->mSourceModel->indexForItem(item);
147 return mapFromSource(sourceIndex);
151 QModelIndex SortedDirModel::indexForUrl(const KUrl& url) const {
152 if (!url.isValid()) {
153 return QModelIndex();
155 QModelIndex sourceIndex = d->mSourceModel->indexForUrl(url);
156 return mapFromSource(sourceIndex);
160 void SortedDirModel::setMimeExcludeFilter(const QStringList &mimeList) {
161 if (d->mMimeExcludeFilter == mimeList) {
162 return;
164 d->mMimeExcludeFilter = mimeList;
165 applyFilters();
169 bool SortedDirModel::filterAcceptsRow(int row, const QModelIndex& parent) const {
170 QModelIndex index = d->mSourceModel->index(row, 0, parent);
171 KFileItem fileItem = d->mSourceModel->itemForIndex(index);
173 QString extension = fileItem.name().section('.', -1).toLower();
174 if (d->mBlackListedExtensions.contains(extension)) {
175 return false;
178 if (!d->mMimeExcludeFilter.isEmpty()) {
179 if (d->mMimeExcludeFilter.contains(fileItem.mimetype())) {
180 return false;
183 if (!ArchiveUtils::fileItemIsDirOrArchive(fileItem)) {
184 #ifndef GWENVIEW_SEMANTICINFO_BACKEND_NONE
185 if (!d->mSourceModel->semanticInfoAvailableForIndex(index)) {
186 Q_FOREACH(const AbstractSortedDirModelFilter* filter, d->mFilters) {
187 // Make sure we have semanticinfo, otherwise retrieve it and
188 // return false, we will be called again later when it is
189 // there.
190 if (filter->needsSemanticInfo()) {
191 d->mSourceModel->retrieveSemanticInfoForIndex(index);
192 return false;
196 #endif
198 Q_FOREACH(const AbstractSortedDirModelFilter* filter, d->mFilters) {
199 if (!filter->acceptsIndex(index)) {
200 return false;
204 return KDirSortFilterProxyModel::filterAcceptsRow(row, parent);
208 AbstractSemanticInfoBackEnd* SortedDirModel::semanticInfoBackEnd() const {
209 #ifdef GWENVIEW_SEMANTICINFO_BACKEND_NONE
210 return 0;
211 #else
212 return d->mSourceModel->semanticInfoBackEnd();
213 #endif
217 #ifndef GWENVIEW_SEMANTICINFO_BACKEND_NONE
218 SemanticInfo SortedDirModel::semanticInfoForSourceIndex(const QModelIndex& sourceIndex) const {
219 return d->mSourceModel->semanticInfoForIndex(sourceIndex);
221 #endif
224 void SortedDirModel::applyFilters() {
225 d->mDelayedApplyFiltersTimer.start();
229 void SortedDirModel::doApplyFilters() {
230 QSortFilterProxyModel::invalidateFilter();
234 bool SortedDirModel::lessThan(const QModelIndex& left, const QModelIndex& right) const {
235 if (sortRole() != KDirModel::ModifiedTime) {
236 return KDirSortFilterProxyModel::lessThan(left, right);
239 const KDateTime leftDate = TimeUtils::dateTimeForFileItem(itemForSourceIndex(left));
240 const KDateTime rightDate = TimeUtils::dateTimeForFileItem(itemForSourceIndex(right));
242 return leftDate < rightDate;
246 } //namespace