1
// vim: set tabstop=4 shiftwidth=4 expandtab:
3
Gwenview: an image viewer
4
Copyright 2012 Aurélien Gâteau <agateau@kde.org>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License
8
as published by the Free Software Foundation; either version 2
9
of the License, or (at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
22
#include "recursivedirmodel.moc"
36
struct RecursiveDirModelPrivate {
37
KDirLister* mDirLister;
40
int indexForUrl(const KUrl &url) const
43
KFileItemList::ConstIterator it = mList.begin(), end = mList.end();
44
for (; it != end; ++it , ++row) {
45
if (it->url() == url) {
52
void removeAt(int row)
57
void addItem(const KFileItem& item)
63
RecursiveDirModel::RecursiveDirModel(QObject* parent)
64
: QAbstractListModel(parent)
65
, d(new RecursiveDirModelPrivate)
67
d->mDirLister = new KDirLister(this);
68
connect(d->mDirLister, SIGNAL(itemsAdded(KUrl, KFileItemList)),
69
SLOT(slotItemsAdded(KUrl, KFileItemList)));
70
connect(d->mDirLister, SIGNAL(itemsDeleted(KFileItemList)),
71
SLOT(slotItemsDeleted(KFileItemList)));
72
connect(d->mDirLister, SIGNAL(completed()),
74
connect(d->mDirLister, SIGNAL(clear()),
76
connect(d->mDirLister, SIGNAL(clear(KUrl)),
77
SLOT(slotDirCleared(KUrl)));
80
RecursiveDirModel::~RecursiveDirModel()
85
KUrl RecursiveDirModel::url() const
87
return d->mDirLister->url();
90
void RecursiveDirModel::setUrl(const KUrl& url)
95
d->mDirLister->openUrl(url);
98
int RecursiveDirModel::rowCount(const QModelIndex& parent) const
100
if (parent.isValid()) {
103
return d->mList.count();
107
QVariant RecursiveDirModel::data(const QModelIndex& index, int role) const
109
if (index.parent().isValid()) {
112
KFileItem item = d->mList.value(index.row());
114
kWarning() << "Invalid row" << index.row();
118
case Qt::DisplayRole:
120
case Qt::DecorationRole:
121
return item.iconName();
122
case KDirModel::FileItemRole:
123
return QVariant(item);
125
kWarning() << "Unhandled role" << role;
131
void RecursiveDirModel::slotItemsAdded(const KUrl&, const KFileItemList& newList)
134
Q_FOREACH(const KFileItem& item, newList) {
136
if (d->indexForUrl(item.url()) == -1) {
137
beginInsertRows(QModelIndex(), d->mList.count(), d->mList.count());
142
dirUrls << item.url();
145
Q_FOREACH(const KUrl& url, dirUrls) {
146
d->mDirLister->openUrl(url, KDirLister::Keep);
150
void RecursiveDirModel::slotItemsDeleted(const KFileItemList& list)
152
Q_FOREACH(const KFileItem& item, list) {
156
int row = d->indexForUrl(item.url());
158
kWarning() << "Received itemsDeleted for an unknown item: this should not happen!";
161
beginRemoveRows(QModelIndex(), row, row);
167
void RecursiveDirModel::slotCleared()
169
if (d->mList.isEmpty()) {
172
beginRemoveRows(QModelIndex(), 0, d->mList.count() - 1);
177
void RecursiveDirModel::slotDirCleared(const KUrl& dirUrl)
180
for (row = d->mList.count() - 1; row >= 0; --row) {
181
const KUrl url = d->mList.at(row).url();
182
if (dirUrl.isParentOf(url)) {
183
beginRemoveRows(QModelIndex(), row, row);