1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the examples of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
43 #include "qdeclarativefolderlistmodel.h"
46 #include <qdeclarativecontext.h>
48 #ifndef QT_NO_DIRMODEL
52 class QDeclarativeFolderListModelPrivate
55 QDeclarativeFolderListModelPrivate()
56 : sortField(QDeclarativeFolderListModel::Name), sortReversed(false), count(0) {
57 nameFilters << QLatin1String("*");
60 void updateSorting() {
61 QDir::SortFlags flags = 0;
63 case QDeclarativeFolderListModel::Unsorted:
64 flags |= QDir::Unsorted;
66 case QDeclarativeFolderListModel::Name:
69 case QDeclarativeFolderListModel::Time:
72 case QDeclarativeFolderListModel::Size:
75 case QDeclarativeFolderListModel::Type:
81 flags |= QDir::Reversed;
83 model.setSorting(flags);
88 QStringList nameFilters;
89 QModelIndex folderIndex;
90 QDeclarativeFolderListModel::SortField sortField;
96 \qmlclass FolderListModel QDeclarativeFolderListModel
97 \ingroup qml-working-with-data
98 \brief The FolderListModel provides a model of the contents of a file system folder.
100 FolderListModel provides access to information about the contents of a folder
101 in the local file system, exposing a list of files to views and other data components.
103 \note This type is made available by importing the \c Qt.labs.folderlistmodel module.
104 \e{Elements in the Qt.labs module are not guaranteed to remain compatible
107 \bold{import Qt.labs.folderlistmodel 1.0}
109 The \l folder property specifies the folder to access. Information about the
110 files and directories in the folder is supplied via the model's interface.
111 Components access names and paths via the following roles:
118 Additionally a file entry can be differentiated from a folder entry via the
123 Various properties can be set to filter the number of files and directories
124 exposed by the model.
126 The \l nameFilters property can be set to contain a list of wildcard filters
127 that are applied to names of files and directories, causing only those that
128 match the filters to be exposed.
130 Directories can be included or excluded using the \l showDirs property, and
131 navigation directories can also be excluded by setting the \l showDotAndDotDot
134 It is sometimes useful to limit the files and directories exposed to those
135 that the user can access. The \l showOnlyReadable property can be set to
138 \section1 Example Usage
140 The following example shows a FolderListModel being used to provide a list
141 of QML files in a \l ListView:
143 \snippet doc/src/snippets/declarative/folderlistmodel.qml 0
145 \section1 Path Separators
147 Qt uses "/" as a universal directory separator in the same way that "/" is
148 used as a path separator in URLs. If you always use "/" as a directory
149 separator, Qt will translate your paths to conform to the underlying
152 \sa {QML Data Models}
155 QDeclarativeFolderListModel::QDeclarativeFolderListModel(QObject *parent)
156 : QAbstractListModel(parent)
158 QHash<int, QByteArray> roles;
159 roles[FileNameRole] = "fileName";
160 roles[FilePathRole] = "filePath";
163 d = new QDeclarativeFolderListModelPrivate;
164 d->model.setFilter(QDir::AllDirs | QDir::Files | QDir::Drives | QDir::NoDotAndDotDot);
165 connect(&d->model, SIGNAL(rowsInserted(const QModelIndex&,int,int))
166 , this, SLOT(inserted(const QModelIndex&,int,int)));
167 connect(&d->model, SIGNAL(rowsRemoved(const QModelIndex&,int,int))
168 , this, SLOT(removed(const QModelIndex&,int,int)));
169 connect(&d->model, SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&))
170 , this, SLOT(handleDataChanged(const QModelIndex&,const QModelIndex&)));
171 connect(&d->model, SIGNAL(modelReset()), this, SLOT(refresh()));
172 connect(&d->model, SIGNAL(layoutChanged()), this, SLOT(refresh()));
175 QDeclarativeFolderListModel::~QDeclarativeFolderListModel()
180 QVariant QDeclarativeFolderListModel::data(const QModelIndex &index, int role) const
183 QModelIndex modelIndex = d->model.index(index.row(), 0, d->folderIndex);
184 if (modelIndex.isValid()) {
185 if (role == FileNameRole)
186 rv = d->model.data(modelIndex, QDirModel::FileNameRole).toString();
187 else if (role == FilePathRole)
188 rv = QUrl::fromLocalFile(d->model.data(modelIndex, QDirModel::FilePathRole).toString());
194 \qmlproperty int FolderListModel::count
196 Returns the number of items in the current folder that match the
199 int QDeclarativeFolderListModel::rowCount(const QModelIndex &parent) const
206 \qmlproperty string FolderListModel::folder
208 The \a folder property holds a URL for the folder that the model is
211 The value is a URL expressed as a string, and must be a \c file: or \c qrc:
212 URL, or a relative URL.
214 By default, the value is an invalid URL.
216 QUrl QDeclarativeFolderListModel::folder() const
221 void QDeclarativeFolderListModel::setFolder(const QUrl &folder)
223 if (folder == d->folder)
225 QModelIndex index = d->model.index(folder.toLocalFile());
226 if ((index.isValid() && d->model.isDir(index)) || folder.toLocalFile().isEmpty()) {
229 QMetaObject::invokeMethod(this, "refresh", Qt::QueuedConnection);
230 emit folderChanged();
235 \qmlproperty url FolderListModel::parentFolder
237 Returns the URL of the parent of of the current \l folder.
239 QUrl QDeclarativeFolderListModel::parentFolder() const
241 QString localFile = d->folder.toLocalFile();
242 if (!localFile.isEmpty()) {
244 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WIN)
250 localFile = dir.path();
252 int pos = d->folder.path().lastIndexOf(QLatin1Char('/'));
255 localFile = d->folder.path().left(pos);
257 return QUrl::fromLocalFile(localFile);
261 \qmlproperty list<string> FolderListModel::nameFilters
263 The \a nameFilters property contains a list of file name filters.
264 The filters may include the ? and * wildcards.
266 The example below filters on PNG and JPEG files:
270 nameFilters: [ "*.png", "*.jpg" ]
274 \note Directories are not excluded by filters.
276 QStringList QDeclarativeFolderListModel::nameFilters() const
278 return d->nameFilters;
281 void QDeclarativeFolderListModel::setNameFilters(const QStringList &filters)
283 d->nameFilters = filters;
284 d->model.setNameFilters(d->nameFilters);
287 void QDeclarativeFolderListModel::classBegin()
291 void QDeclarativeFolderListModel::componentComplete()
293 if (!d->folder.isValid() || d->folder.toLocalFile().isEmpty() || !QDir().exists(d->folder.toLocalFile()))
294 setFolder(QUrl(QLatin1String("file://")+QDir::currentPath()));
296 if (!d->folderIndex.isValid())
297 QMetaObject::invokeMethod(this, "refresh", Qt::QueuedConnection);
301 \qmlproperty enumeration FolderListModel::sortField
303 The \a sortField property contains field to use for sorting. sortField
306 \o Unsorted - no sorting is applied. The order is system default.
307 \o Name - sort by filename
308 \o Time - sort by time modified
309 \o Size - sort by file size
310 \o Type - sort by file type (extension)
315 QDeclarativeFolderListModel::SortField QDeclarativeFolderListModel::sortField() const
320 void QDeclarativeFolderListModel::setSortField(SortField field)
322 if (field != d->sortField) {
323 d->sortField = field;
329 \qmlproperty bool FolderListModel::sortReversed
331 If set to true, reverses the sort order. The default is false.
335 bool QDeclarativeFolderListModel::sortReversed() const
337 return d->sortReversed;
340 void QDeclarativeFolderListModel::setSortReversed(bool rev)
342 if (rev != d->sortReversed) {
343 d->sortReversed = rev;
349 \qmlmethod bool FolderListModel::isFolder(int index)
351 Returns true if the entry \a index is a folder; otherwise
354 bool QDeclarativeFolderListModel::isFolder(int index) const
357 QModelIndex idx = d->model.index(index, 0, d->folderIndex);
359 return d->model.isDir(idx);
364 void QDeclarativeFolderListModel::refresh()
366 d->folderIndex = QModelIndex();
368 emit beginRemoveRows(QModelIndex(), 0, d->count-1);
370 emit endRemoveRows();
372 d->folderIndex = d->model.index(d->folder.toLocalFile());
373 int newcount = d->model.rowCount(d->folderIndex);
375 emit beginInsertRows(QModelIndex(), 0, newcount-1);
377 emit endInsertRows();
381 void QDeclarativeFolderListModel::inserted(const QModelIndex &index, int start, int end)
383 if (index == d->folderIndex) {
384 emit beginInsertRows(QModelIndex(), start, end);
385 d->count = d->model.rowCount(d->folderIndex);
386 emit endInsertRows();
390 void QDeclarativeFolderListModel::removed(const QModelIndex &index, int start, int end)
392 if (index == d->folderIndex) {
393 emit beginRemoveRows(QModelIndex(), start, end);
394 d->count = d->model.rowCount(d->folderIndex);
395 emit endRemoveRows();
399 void QDeclarativeFolderListModel::handleDataChanged(const QModelIndex &start, const QModelIndex &end)
401 if (start.parent() == d->folderIndex)
402 emit dataChanged(index(start.row(),0), index(end.row(),0));
406 \qmlproperty bool FolderListModel::showDirs
408 If true, directories are included in the model; otherwise only files
411 By default, this property is true.
413 Note that the nameFilters are not applied to directories.
417 bool QDeclarativeFolderListModel::showDirs() const
419 return d->model.filter() & QDir::AllDirs;
422 void QDeclarativeFolderListModel::setShowDirs(bool on)
424 if (!(d->model.filter() & QDir::AllDirs) == !on)
427 d->model.setFilter(d->model.filter() | QDir::AllDirs | QDir::Drives);
429 d->model.setFilter(d->model.filter() & ~(QDir::AllDirs | QDir::Drives));
433 \qmlproperty bool FolderListModel::showDotAndDotDot
435 If true, the "." and ".." directories are included in the model; otherwise
438 By default, this property is false.
442 bool QDeclarativeFolderListModel::showDotAndDotDot() const
444 return !(d->model.filter() & QDir::NoDotAndDotDot);
447 void QDeclarativeFolderListModel::setShowDotAndDotDot(bool on)
449 if (!(d->model.filter() & QDir::NoDotAndDotDot) == on)
452 d->model.setFilter(d->model.filter() & ~QDir::NoDotAndDotDot);
454 d->model.setFilter(d->model.filter() | QDir::NoDotAndDotDot);
458 \qmlproperty bool FolderListModel::showOnlyReadable
460 If true, only readable files and directories are shown; otherwise all files
461 and directories are shown.
463 By default, this property is false.
467 bool QDeclarativeFolderListModel::showOnlyReadable() const
469 return d->model.filter() & QDir::Readable;
472 void QDeclarativeFolderListModel::setShowOnlyReadable(bool on)
474 if (!(d->model.filter() & QDir::Readable) == !on)
477 d->model.setFilter(d->model.filter() | QDir::Readable);
479 d->model.setFilter(d->model.filter() & ~QDir::Readable);
485 #endif // QT_NO_DIRMODEL