Qt 4.8
qdeclarativefolderlistmodel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
44 #include <QDirModel>
45 #include <QDebug>
46 #include <qdeclarativecontext.h>
47 
48 #ifndef QT_NO_DIRMODEL
49 
51 
53 {
54 public:
57  nameFilters << QLatin1String("*");
58  }
59 
60  void updateSorting() {
61  QDir::SortFlags flags = 0;
62  switch(sortField) {
64  flags |= QDir::Unsorted;
65  break;
67  flags |= QDir::Name;
68  break;
70  flags |= QDir::Time;
71  break;
73  flags |= QDir::Size;
74  break;
76  flags |= QDir::Type;
77  break;
78  }
79 
80  if (sortReversed)
81  flags |= QDir::Reversed;
82 
83  model.setSorting(flags);
84  }
85 
92  int count;
93 };
94 
159  : QAbstractListModel(parent)
160 {
162  roles[FileNameRole] = "fileName";
163  roles[FilePathRole] = "filePath";
164  setRoleNames(roles);
165 
168  connect(&d->model, SIGNAL(rowsInserted(const QModelIndex&,int,int))
169  , this, SLOT(inserted(const QModelIndex&,int,int)));
170  connect(&d->model, SIGNAL(rowsRemoved(const QModelIndex&,int,int))
171  , this, SLOT(removed(const QModelIndex&,int,int)));
173  , this, SLOT(handleDataChanged(const QModelIndex&,const QModelIndex&)));
174  connect(&d->model, SIGNAL(modelReset()), this, SLOT(refresh()));
175  connect(&d->model, SIGNAL(layoutChanged()), this, SLOT(refresh()));
176 }
177 
179 {
180  delete d;
181 }
182 
184 {
185  QVariant rv;
186  QModelIndex modelIndex = d->model.index(index.row(), 0, d->folderIndex);
187  if (modelIndex.isValid()) {
188  if (role == FileNameRole)
189  rv = d->model.data(modelIndex, QDirModel::FileNameRole).toString();
190  else if (role == FilePathRole)
192  }
193  return rv;
194 }
195 
206 {
207  Q_UNUSED(parent);
208  return d->count;
209 }
210 
226 {
227  return d->folder;
228 }
229 
231 {
232  if (folder == d->folder)
233  return;
234  QModelIndex index = d->model.index(folder.toLocalFile());
235  if ((index.isValid() && d->model.isDir(index)) || folder.toLocalFile().isEmpty()) {
236 
237  d->folder = folder;
240  }
241 }
242 
252 {
253  QString localFile = d->folder.toLocalFile();
254  if (!localFile.isEmpty()) {
255  QDir dir(localFile);
256 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WIN)
257  if (dir.isRoot())
258  dir.setPath("");
259  else
260 #endif
261  dir.cdUp();
262  localFile = dir.path();
263  } else {
264  int pos = d->folder.path().lastIndexOf(QLatin1Char('/'));
265  if (pos == -1)
266  return QUrl();
267  localFile = d->folder.path().left(pos);
268  }
269  return QUrl::fromLocalFile(localFile);
270 }
271 
289 {
290  return d->nameFilters;
291 }
292 
294 {
295  d->nameFilters = filters;
297 }
298 
300 {
301 }
302 
304 {
305  if (!d->folder.isValid() || d->folder.toLocalFile().isEmpty() || !QDir().exists(d->folder.toLocalFile()))
307 
308  if (!d->folderIndex.isValid())
310 }
311 
331 {
332  return d->sortField;
333 }
334 
336 {
337  if (field != d->sortField) {
338  d->sortField = field;
339  d->updateSorting();
340  }
341 }
342 
354 {
355  return d->sortReversed;
356 }
357 
359 {
360  if (rev != d->sortReversed) {
361  d->sortReversed = rev;
362  d->updateSorting();
363  }
364 }
365 
376 {
377  if (index != -1) {
378  QModelIndex idx = d->model.index(index, 0, d->folderIndex);
379  if (idx.isValid())
380  return d->model.isDir(idx);
381  }
382  return false;
383 }
384 
386 {
388  if (d->count) {
390  d->count = 0;
392  }
394  int newcount = d->model.rowCount(d->folderIndex);
395  if (newcount) {
396  emit beginInsertRows(QModelIndex(), 0, newcount-1);
397  d->count = newcount;
399  }
400 }
401 
403 {
404  if (index == d->folderIndex) {
405  emit beginInsertRows(QModelIndex(), start, end);
408  }
409 }
410 
412 {
413  if (index == d->folderIndex) {
414  emit beginRemoveRows(QModelIndex(), start, end);
417  }
418 }
419 
421 {
422  if (start.parent() == d->folderIndex)
423  emit dataChanged(index(start.row(),0), index(end.row(),0));
424 }
425 
442 {
443  return d->model.filter() & QDir::AllDirs;
444 }
445 
447 {
448  if (!(d->model.filter() & QDir::AllDirs) == !on)
449  return;
450  if (on)
452  else
454 }
455 
470 {
471  return !(d->model.filter() & QDir::NoDotAndDotDot);
472 }
473 
475 {
476  if (!(d->model.filter() & QDir::NoDotAndDotDot) == on)
477  return;
478  if (on)
479  d->model.setFilter(d->model.filter() & ~QDir::NoDotAndDotDot);
480  else
482 }
483 
498 {
499  return d->model.filter() & QDir::Readable;
500 }
501 
503 {
504  if (!(d->model.filter() & QDir::Readable) == !on)
505  return;
506  if (on)
508  else
510 }
511 
514 
515 #endif // QT_NO_DIRMODEL
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
QDir::Filters filter() const
Returns the filter specification for the directory model.
Definition: qdirmodel.cpp:712
QDeclarativeFolderListModel::SortField sortField
QDeclarativeFolderListModelPrivate * d
bool isValid() const
Returns true if the URL is valid; otherwise returns false.
Definition: qurl.cpp:4303
void handleDataChanged(const QModelIndex &start, const QModelIndex &end)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void inserted(const QModelIndex &index, int start, int end)
void setPath(const QString &path)
Sets the path of the directory to path.
Definition: qdir.cpp:590
void setNameFilters(const QStringList &filters)
Q_INVOKABLE bool isFolder(int index) const
[prop funcs]
#define SLOT(a)
Definition: qobjectdefs.h:226
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QAbstractListModel class provides an abstract model that can be subclassed to create one-dimensio...
int rowCount(const QModelIndex &parent) const
Returns the number of rows under the given parent.
void endInsertRows()
Ends a row insertion operation.
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString currentPath()
Returns the absolute path of the application&#39;s current directory.
Definition: qdir.cpp:1875
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
bool exists() const
Returns true if the directory exists; otherwise returns false.
Definition: qdir.cpp:1560
QModelIndex index(int row, int column=0, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
QString path() const
Returns the path of the URL.
Definition: qurl.cpp:4977
#define SIGNAL(a)
Definition: qobjectdefs.h:227
bool cdUp()
Changes directory by moving one directory up from the QDir&#39;s current directory.
Definition: qdir.cpp:937
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
int row() const
Returns the row this model index refers to.
#define emit
Definition: qobjectdefs.h:76
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QDeclarativeFolderListModel(QObject *parent=0)
[class props]
void setFilter(QDir::Filters filters)
Sets the directory model&#39;s filter to that specified by filters.
Definition: qdirmodel.cpp:694
void endRemoveRows()
Ends a row removal operation.
void layoutChanged()
This signal is emitted whenever the layout of items exposed by the model has changed; for example...
QString toLocalFile() const
Returns the path of this URL formatted as a local file path.
Definition: qurl.cpp:6412
void setSorting(QDir::SortFlags sort)
Sets the directory model&#39;s sorting order to that specified by sort.
Definition: qdirmodel.cpp:724
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
bool isDir(const QModelIndex &index) const
Returns true if the model item index represents a directory; otherwise returns false.
Definition: qdirmodel.cpp:986
static const char *const filters[3]
QVariant data(const QModelIndex &index, int role) const
Returns the data stored under the given role for the item referred to by the index.
void rowsInserted(const QModelIndex &parent, int first, int last)
This signal is emitted after rows have been inserted into the model.
int lastIndexOf(QChar c, int from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:3000
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void beginRemoveRows(const QModelIndex &parent, int first, int last)
Begins a row removal operation.
The QModelIndex class is used to locate data in a data model.
void modelReset()
This signal is emitted when reset() is called, after the model&#39;s internal state (e.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
This signal is emitted whenever the data in an existing item changes.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the model item index for the item in the parent with the given row and column.
Definition: qdirmodel.cpp:281
SortField sortField() const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Returns the data for the model item index with the given role.
Definition: qdirmodel.cpp:365
void setNameFilters(const QStringList &filters)
Sets the name filters for the directory model.
Definition: qdirmodel.cpp:663
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
virtual void componentComplete()
Invoked after the root component that caused this instantiation has completed construction.
static QUrl fromLocalFile(const QString &localfile)
Returns a QUrl representation of localFile, interpreted as a local file.
Definition: qurl.cpp:6374
void setRoleNames(const QHash< int, QByteArray > &roleNames)
Sets the model&#39;s role names to roleNames.
QString path() const
Returns the path.
Definition: qdir.cpp:605
static const KeyPair *const end
bool isRoot() const
Returns true if the directory is the root directory; otherwise returns false.
Definition: qdir.cpp:1577
void folderChanged()
[parserstatus]
void rowsRemoved(const QModelIndex &parent, int first, int last)
This signal is emitted after rows have been removed from the model.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
int rowCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of rows in the parent model item.
Definition: qdirmodel.cpp:331
The QDirModel class provides a data model for the local filesystem.
Definition: qdirmodel.h:59
QUrl folder() const
[count]
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void removed(const QModelIndex &index, int start, int end)
void beginInsertRows(const QModelIndex &parent, int first, int last)
Begins a row insertion operation.
QStringList nameFilters() const