~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/semanticinfo/sorteddirmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
ImportĀ upstreamĀ versionĀ 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include "semanticinfodirmodel.h"
38
38
#endif
39
39
 
40
 
namespace Gwenview {
 
40
namespace Gwenview
 
41
{
41
42
 
42
43
AbstractSortedDirModelFilter::AbstractSortedDirModelFilter(SortedDirModel* model)
43
44
: QObject(model)
44
45
, mModel(model)
45
46
{
46
 
        if (mModel) {
47
 
                mModel->addFilter(this);
48
 
        }
 
47
    if (mModel) {
 
48
        mModel->addFilter(this);
 
49
    }
49
50
}
50
51
 
51
52
AbstractSortedDirModelFilter::~AbstractSortedDirModelFilter()
52
53
{
53
 
        if (mModel) {
54
 
                mModel->removeFilter(this);
55
 
        }
 
54
    if (mModel) {
 
55
        mModel->removeFilter(this);
 
56
    }
56
57
}
57
58
 
58
 
 
59
59
struct SortedDirModelPrivate {
60
60
#ifdef GWENVIEW_SEMANTICINFO_BACKEND_NONE
61
 
        KDirModel* mSourceModel;
 
61
    KDirModel* mSourceModel;
62
62
#else
63
 
        SemanticInfoDirModel* mSourceModel;
 
63
    SemanticInfoDirModel* mSourceModel;
64
64
#endif
65
 
        QStringList mBlackListedExtensions;
66
 
        QList<AbstractSortedDirModelFilter*> mFilters;
67
 
        QTimer mDelayedApplyFiltersTimer;
68
 
        MimeTypeUtils::Kinds mKindFilter;
 
65
    QStringList mBlackListedExtensions;
 
66
    QList<AbstractSortedDirModelFilter*> mFilters;
 
67
    QTimer mDelayedApplyFiltersTimer;
 
68
    MimeTypeUtils::Kinds mKindFilter;
69
69
};
70
70
 
71
 
 
72
71
SortedDirModel::SortedDirModel(QObject* parent)
73
72
: KDirSortFilterProxyModel(parent)
74
73
, d(new SortedDirModelPrivate)
75
74
{
76
75
#ifdef GWENVIEW_SEMANTICINFO_BACKEND_NONE
77
 
        d->mSourceModel = new KDirModel(this);
 
76
    d->mSourceModel = new KDirModel(this);
78
77
#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()));
85
 
}
86
 
 
87
 
 
88
 
SortedDirModel::~SortedDirModel() {
89
 
        delete d;
90
 
}
91
 
 
92
 
 
93
 
MimeTypeUtils::Kinds SortedDirModel::kindFilter() const {
94
 
        return d->mKindFilter;
95
 
}
96
 
 
97
 
 
98
 
void SortedDirModel::setKindFilter(MimeTypeUtils::Kinds kindFilter) {
99
 
        if (d->mKindFilter == kindFilter) {
100
 
                return;
101
 
        }
102
 
        d->mKindFilter = kindFilter;
103
 
        applyFilters();
104
 
}
105
 
 
106
 
 
107
 
void SortedDirModel::adjustKindFilter(MimeTypeUtils::Kinds kinds, bool set) {
108
 
        MimeTypeUtils::Kinds kindFilter = d->mKindFilter;
109
 
        if (set) {
110
 
                kindFilter |= kinds;
111
 
        } else {
112
 
                kindFilter &= ~kinds;
113
 
        }
114
 
        setKindFilter(kindFilter);
115
 
}
116
 
 
117
 
 
118
 
void SortedDirModel::addFilter(AbstractSortedDirModelFilter* filter) {
119
 
        d->mFilters << filter;
120
 
        applyFilters();
121
 
}
122
 
 
123
 
 
124
 
void SortedDirModel::removeFilter(AbstractSortedDirModelFilter* filter) {
125
 
        d->mFilters.removeAll(filter);
126
 
        applyFilters();
127
 
}
128
 
 
129
 
 
130
 
KDirLister* SortedDirModel::dirLister() const {
131
 
        return d->mSourceModel->dirLister();
132
 
}
133
 
 
134
 
 
135
 
void SortedDirModel::reload() {
136
 
        #ifndef GWENVIEW_SEMANTICINFO_BACKEND_NONE
137
 
        d->mSourceModel->clearSemanticInfoCache();
138
 
        #endif
139
 
        dirLister()->updateDirectory(dirLister()->url());
140
 
}
141
 
 
142
 
 
143
 
void SortedDirModel::setBlackListedExtensions(const QStringList& list) {
144
 
        d->mBlackListedExtensions = list;
145
 
}
146
 
 
147
 
 
148
 
KFileItem SortedDirModel::itemForIndex(const QModelIndex& index) const {
149
 
        if (!index.isValid()) {
150
 
                return KFileItem();
151
 
        }
152
 
 
153
 
        QModelIndex sourceIndex = mapToSource(index);
154
 
        return d->mSourceModel->itemForIndex(sourceIndex);
155
 
}
156
 
 
157
 
 
158
 
KUrl SortedDirModel::urlForIndex(const QModelIndex& index) const {
 
78
    d->mSourceModel = new SemanticInfoDirModel(this);
 
79
#endif
 
80
    setSourceModel(d->mSourceModel);
 
81
    d->mDelayedApplyFiltersTimer.setInterval(0);
 
82
    d->mDelayedApplyFiltersTimer.setSingleShot(true);
 
83
    connect(&d->mDelayedApplyFiltersTimer, SIGNAL(timeout()), SLOT(doApplyFilters()));
 
84
}
 
85
 
 
86
SortedDirModel::~SortedDirModel()
 
87
{
 
88
    delete d;
 
89
}
 
90
 
 
91
MimeTypeUtils::Kinds SortedDirModel::kindFilter() const
 
92
{
 
93
    return d->mKindFilter;
 
94
}
 
95
 
 
96
void SortedDirModel::setKindFilter(MimeTypeUtils::Kinds kindFilter)
 
97
{
 
98
    if (d->mKindFilter == kindFilter) {
 
99
        return;
 
100
    }
 
101
    d->mKindFilter = kindFilter;
 
102
    applyFilters();
 
103
}
 
104
 
 
105
void SortedDirModel::adjustKindFilter(MimeTypeUtils::Kinds kinds, bool set)
 
106
{
 
107
    MimeTypeUtils::Kinds kindFilter = d->mKindFilter;
 
108
    if (set) {
 
109
        kindFilter |= kinds;
 
110
    } else {
 
111
        kindFilter &= ~kinds;
 
112
    }
 
113
    setKindFilter(kindFilter);
 
114
}
 
115
 
 
116
void SortedDirModel::addFilter(AbstractSortedDirModelFilter* filter)
 
117
{
 
118
    d->mFilters << filter;
 
119
    applyFilters();
 
120
}
 
121
 
 
122
void SortedDirModel::removeFilter(AbstractSortedDirModelFilter* filter)
 
123
{
 
124
    d->mFilters.removeAll(filter);
 
125
    applyFilters();
 
126
}
 
127
 
 
128
KDirLister* SortedDirModel::dirLister() const
 
129
{
 
130
    return d->mSourceModel->dirLister();
 
131
}
 
132
 
 
133
void SortedDirModel::reload()
 
134
{
 
135
#ifndef GWENVIEW_SEMANTICINFO_BACKEND_NONE
 
136
    d->mSourceModel->clearSemanticInfoCache();
 
137
#endif
 
138
    dirLister()->updateDirectory(dirLister()->url());
 
139
}
 
140
 
 
141
void SortedDirModel::setBlackListedExtensions(const QStringList& list)
 
142
{
 
143
    d->mBlackListedExtensions = list;
 
144
}
 
145
 
 
146
KFileItem SortedDirModel::itemForIndex(const QModelIndex& index) const
 
147
{
 
148
    if (!index.isValid()) {
 
149
        return KFileItem();
 
150
    }
 
151
 
 
152
    QModelIndex sourceIndex = mapToSource(index);
 
153
    return d->mSourceModel->itemForIndex(sourceIndex);
 
154
}
 
155
 
 
156
KUrl SortedDirModel::urlForIndex(const QModelIndex& index) const
 
157
{
159
158
    KFileItem item = itemForIndex(index);
160
159
    return item.isNull() ? KUrl() : item.url();
161
160
}
162
161
 
163
 
 
164
 
KFileItem SortedDirModel::itemForSourceIndex(const QModelIndex& sourceIndex) const {
165
 
        if (!sourceIndex.isValid()) {
166
 
                return KFileItem();
167
 
        }
168
 
        return d->mSourceModel->itemForIndex(sourceIndex);
169
 
}
170
 
 
171
 
 
172
 
QModelIndex SortedDirModel::indexForItem(const KFileItem& item) const {
173
 
        if (item.isNull()) {
174
 
                return QModelIndex();
175
 
        }
176
 
 
177
 
        QModelIndex sourceIndex = d->mSourceModel->indexForItem(item);
178
 
        return mapFromSource(sourceIndex);
179
 
}
180
 
 
181
 
 
182
 
QModelIndex SortedDirModel::indexForUrl(const KUrl& url) const {
183
 
        if (!url.isValid()) {
184
 
                return QModelIndex();
185
 
        }
186
 
        QModelIndex sourceIndex = d->mSourceModel->indexForUrl(url);
187
 
        return mapFromSource(sourceIndex);
188
 
}
189
 
 
190
 
 
191
 
bool SortedDirModel::filterAcceptsRow(int row, const QModelIndex& parent) const {
192
 
        QModelIndex index = d->mSourceModel->index(row, 0, parent);
193
 
        KFileItem fileItem = d->mSourceModel->itemForIndex(index);
194
 
 
195
 
        MimeTypeUtils::Kinds kind = MimeTypeUtils::fileItemKind(fileItem);
196
 
        if (d->mKindFilter != MimeTypeUtils::Kinds() && !(d->mKindFilter & kind)) {
197
 
                return false;
198
 
        }
199
 
 
200
 
        if (kind != MimeTypeUtils::KIND_DIR && kind != MimeTypeUtils::KIND_ARCHIVE) {
201
 
                int dotPos = fileItem.name().lastIndexOf('.');
202
 
                if (dotPos >= 1) {
203
 
                        QString extension = fileItem.name().mid(dotPos + 1).toLower();
204
 
                        if (d->mBlackListedExtensions.contains(extension)) {
205
 
                                return false;
206
 
                        }
207
 
                }
 
162
KFileItem SortedDirModel::itemForSourceIndex(const QModelIndex& sourceIndex) const
 
163
{
 
164
    if (!sourceIndex.isValid()) {
 
165
        return KFileItem();
 
166
    }
 
167
    return d->mSourceModel->itemForIndex(sourceIndex);
 
168
}
 
169
 
 
170
QModelIndex SortedDirModel::indexForItem(const KFileItem& item) const
 
171
{
 
172
    if (item.isNull()) {
 
173
        return QModelIndex();
 
174
    }
 
175
 
 
176
    QModelIndex sourceIndex = d->mSourceModel->indexForItem(item);
 
177
    return mapFromSource(sourceIndex);
 
178
}
 
179
 
 
180
QModelIndex SortedDirModel::indexForUrl(const KUrl& url) const
 
181
{
 
182
    if (!url.isValid()) {
 
183
        return QModelIndex();
 
184
    }
 
185
    QModelIndex sourceIndex = d->mSourceModel->indexForUrl(url);
 
186
    return mapFromSource(sourceIndex);
 
187
}
 
188
 
 
189
bool SortedDirModel::filterAcceptsRow(int row, const QModelIndex& parent) const
 
190
{
 
191
    QModelIndex index = d->mSourceModel->index(row, 0, parent);
 
192
    KFileItem fileItem = d->mSourceModel->itemForIndex(index);
 
193
 
 
194
    MimeTypeUtils::Kinds kind = MimeTypeUtils::fileItemKind(fileItem);
 
195
    if (d->mKindFilter != MimeTypeUtils::Kinds() && !(d->mKindFilter & kind)) {
 
196
        return false;
 
197
    }
 
198
 
 
199
    if (kind != MimeTypeUtils::KIND_DIR && kind != MimeTypeUtils::KIND_ARCHIVE) {
 
200
        int dotPos = fileItem.name().lastIndexOf('.');
 
201
        if (dotPos >= 1) {
 
202
            QString extension = fileItem.name().mid(dotPos + 1).toLower();
 
203
            if (d->mBlackListedExtensions.contains(extension)) {
 
204
                return false;
 
205
            }
 
206
        }
208
207
#ifndef GWENVIEW_SEMANTICINFO_BACKEND_NONE
209
 
                if (!d->mSourceModel->semanticInfoAvailableForIndex(index)) {
210
 
                        Q_FOREACH(const AbstractSortedDirModelFilter* filter, d->mFilters) {
211
 
                                // Make sure we have semanticinfo, otherwise retrieve it and
212
 
                                // return false, we will be called again later when it is
213
 
                                // there.
214
 
                                if (filter->needsSemanticInfo()) {
215
 
                                        d->mSourceModel->retrieveSemanticInfoForIndex(index);
216
 
                                        return false;
217
 
                                }
218
 
                        }
219
 
                }
 
208
        if (!d->mSourceModel->semanticInfoAvailableForIndex(index)) {
 
209
            Q_FOREACH(const AbstractSortedDirModelFilter * filter, d->mFilters) {
 
210
                // Make sure we have semanticinfo, otherwise retrieve it and
 
211
                // return false, we will be called again later when it is
 
212
                // there.
 
213
                if (filter->needsSemanticInfo()) {
 
214
                    d->mSourceModel->retrieveSemanticInfoForIndex(index);
 
215
                    return false;
 
216
                }
 
217
            }
 
218
        }
220
219
#endif
221
220
 
222
 
                Q_FOREACH(const AbstractSortedDirModelFilter* filter, d->mFilters) {
223
 
                        if (!filter->acceptsIndex(index)) {
224
 
                                return false;
225
 
                        }
226
 
                }
227
 
        }
228
 
        return KDirSortFilterProxyModel::filterAcceptsRow(row, parent);
 
221
        Q_FOREACH(const AbstractSortedDirModelFilter * filter, d->mFilters) {
 
222
            if (!filter->acceptsIndex(index)) {
 
223
                return false;
 
224
            }
 
225
        }
 
226
    }
 
227
    return KDirSortFilterProxyModel::filterAcceptsRow(row, parent);
229
228
}
230
229
 
231
 
 
232
 
AbstractSemanticInfoBackEnd* SortedDirModel::semanticInfoBackEnd() const {
 
230
AbstractSemanticInfoBackEnd* SortedDirModel::semanticInfoBackEnd() const
 
231
{
233
232
#ifdef GWENVIEW_SEMANTICINFO_BACKEND_NONE
234
 
        return 0;
 
233
    return 0;
235
234
#else
236
 
        return d->mSourceModel->semanticInfoBackEnd();
 
235
    return d->mSourceModel->semanticInfoBackEnd();
237
236
#endif
238
237
}
239
238
 
240
 
 
241
239
#ifndef GWENVIEW_SEMANTICINFO_BACKEND_NONE
242
 
SemanticInfo SortedDirModel::semanticInfoForSourceIndex(const QModelIndex& sourceIndex) const {
243
 
        return d->mSourceModel->semanticInfoForIndex(sourceIndex);
 
240
SemanticInfo SortedDirModel::semanticInfoForSourceIndex(const QModelIndex& sourceIndex) const
 
241
{
 
242
    return d->mSourceModel->semanticInfoForIndex(sourceIndex);
244
243
}
245
244
#endif
246
245
 
247
 
 
248
 
void SortedDirModel::applyFilters() {
249
 
        d->mDelayedApplyFiltersTimer.start();
250
 
}
251
 
 
252
 
 
253
 
void SortedDirModel::doApplyFilters() {
254
 
        QSortFilterProxyModel::invalidateFilter();
255
 
}
256
 
 
257
 
 
258
 
bool SortedDirModel::lessThan(const QModelIndex& left, const QModelIndex& right) const {
259
 
        const KFileItem leftItem = itemForSourceIndex(left);
260
 
        const KFileItem rightItem = itemForSourceIndex(right);
261
 
 
262
 
        const bool leftIsDirOrArchive = ArchiveUtils::fileItemIsDirOrArchive(leftItem);
263
 
        const bool rightIsDirOrArchive = ArchiveUtils::fileItemIsDirOrArchive(rightItem);
264
 
 
265
 
        if (leftIsDirOrArchive != rightIsDirOrArchive) {
266
 
                return leftIsDirOrArchive;
267
 
        }
268
 
 
269
 
        if (sortRole() != KDirModel::ModifiedTime) {
270
 
                return KDirSortFilterProxyModel::lessThan(left, right);
271
 
        }
272
 
 
273
 
        const KDateTime leftDate = TimeUtils::dateTimeForFileItem(leftItem);
274
 
        const KDateTime rightDate = TimeUtils::dateTimeForFileItem(rightItem);
275
 
 
276
 
        return leftDate < rightDate;
277
 
}
278
 
 
 
246
void SortedDirModel::applyFilters()
 
247
{
 
248
    d->mDelayedApplyFiltersTimer.start();
 
249
}
 
250
 
 
251
void SortedDirModel::doApplyFilters()
 
252
{
 
253
    QSortFilterProxyModel::invalidateFilter();
 
254
}
 
255
 
 
256
bool SortedDirModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
 
257
{
 
258
    const KFileItem leftItem = itemForSourceIndex(left);
 
259
    const KFileItem rightItem = itemForSourceIndex(right);
 
260
 
 
261
    const bool leftIsDirOrArchive = ArchiveUtils::fileItemIsDirOrArchive(leftItem);
 
262
    const bool rightIsDirOrArchive = ArchiveUtils::fileItemIsDirOrArchive(rightItem);
 
263
 
 
264
    if (leftIsDirOrArchive != rightIsDirOrArchive) {
 
265
        return leftIsDirOrArchive;
 
266
    }
 
267
 
 
268
    if (sortRole() != KDirModel::ModifiedTime) {
 
269
        return KDirSortFilterProxyModel::lessThan(left, right);
 
270
    }
 
271
 
 
272
    const KDateTime leftDate = TimeUtils::dateTimeForFileItem(leftItem);
 
273
    const KDateTime rightDate = TimeUtils::dateTimeForFileItem(rightItem);
 
274
 
 
275
    return leftDate < rightDate;
 
276
}
279
277
 
280
278
bool SortedDirModel::hasDocuments() const
281
279
{
282
 
        const int count = rowCount();
283
 
        if (count == 0) {
284
 
                return false;
285
 
        }
286
 
        for (int row = 0; row < count; ++row) {
287
 
                const QModelIndex idx = index(row, 0);
288
 
                const KFileItem item = itemForIndex(idx);
289
 
                if (!ArchiveUtils::fileItemIsDirOrArchive(item)) {
290
 
                        return true;
291
 
                }
292
 
        }
293
 
        return false;
 
280
    const int count = rowCount();
 
281
    if (count == 0) {
 
282
        return false;
 
283
    }
 
284
    for (int row = 0; row < count; ++row) {
 
285
        const QModelIndex idx = index(row, 0);
 
286
        const KFileItem item = itemForIndex(idx);
 
287
        if (!ArchiveUtils::fileItemIsDirOrArchive(item)) {
 
288
            return true;
 
289
        }
 
290
    }
 
291
    return false;
294
292
}
295
293
 
296
 
 
297
294
} //namespace