qfilesystemmodel.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtWidgets module 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 The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QFILESYSTEMMODEL_H
  40. #define QFILESYSTEMMODEL_H
  41. #include <QtCore/qabstractitemmodel.h>
  42. #include <QtCore/qpair.h>
  43. #include <QtCore/qdir.h>
  44. #include <QtGui/qicon.h>
  45. #include <QtCore/qdiriterator.h>
  46. QT_BEGIN_NAMESPACE
  47. #ifndef QT_NO_FILESYSTEMMODEL
  48. class ExtendedInformation;
  49. class QFileSystemModelPrivate;
  50. class QFileIconProvider;
  51. class Q_WIDGETS_EXPORT QFileSystemModel : public QAbstractItemModel
  52. {
  53. Q_OBJECT
  54. Q_PROPERTY(bool resolveSymlinks READ resolveSymlinks WRITE setResolveSymlinks)
  55. Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
  56. Q_PROPERTY(bool nameFilterDisables READ nameFilterDisables WRITE setNameFilterDisables)
  57. Q_SIGNALS:
  58. void rootPathChanged(const QString &newPath);
  59. void fileRenamed(const QString &path, const QString &oldName, const QString &newName);
  60. void directoryLoaded(const QString &path);
  61. public:
  62. enum Roles {
  63. FileIconRole = Qt::DecorationRole,
  64. FilePathRole = Qt::UserRole + 1,
  65. FileNameRole = Qt::UserRole + 2,
  66. FilePermissions = Qt::UserRole + 3
  67. };
  68. explicit QFileSystemModel(QObject *parent = Q_NULLPTR);
  69. ~QFileSystemModel();
  70. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  71. QModelIndex index(const QString &path, int column = 0) const;
  72. QModelIndex parent(const QModelIndex &child) const Q_DECL_OVERRIDE;
  73. using QObject::parent;
  74. QModelIndex sibling(int row, int column, const QModelIndex &idx) const Q_DECL_OVERRIDE;
  75. bool hasChildren(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  76. bool canFetchMore(const QModelIndex &parent) const Q_DECL_OVERRIDE;
  77. void fetchMore(const QModelIndex &parent) Q_DECL_OVERRIDE;
  78. int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  79. int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  80. QVariant myComputer(int role = Qt::DisplayRole) const;
  81. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
  82. bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE;
  83. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
  84. Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
  85. void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) Q_DECL_OVERRIDE;
  86. QStringList mimeTypes() const Q_DECL_OVERRIDE;
  87. QMimeData *mimeData(const QModelIndexList &indexes) const Q_DECL_OVERRIDE;
  88. bool dropMimeData(const QMimeData *data, Qt::DropAction action,
  89. int row, int column, const QModelIndex &parent) Q_DECL_OVERRIDE;
  90. Qt::DropActions supportedDropActions() const Q_DECL_OVERRIDE;
  91. // QFileSystemModel specific API
  92. QModelIndex setRootPath(const QString &path);
  93. QString rootPath() const;
  94. QDir rootDirectory() const;
  95. void setIconProvider(QFileIconProvider *provider);
  96. QFileIconProvider *iconProvider() const;
  97. void setFilter(QDir::Filters filters);
  98. QDir::Filters filter() const;
  99. void setResolveSymlinks(bool enable);
  100. bool resolveSymlinks() const;
  101. void setReadOnly(bool enable);
  102. bool isReadOnly() const;
  103. void setNameFilterDisables(bool enable);
  104. bool nameFilterDisables() const;
  105. void setNameFilters(const QStringList &filters);
  106. QStringList nameFilters() const;
  107. QString filePath(const QModelIndex &index) const;
  108. bool isDir(const QModelIndex &index) const;
  109. qint64 size(const QModelIndex &index) const;
  110. QString type(const QModelIndex &index) const;
  111. QDateTime lastModified(const QModelIndex &index) const;
  112. QModelIndex mkdir(const QModelIndex &parent, const QString &name);
  113. bool rmdir(const QModelIndex &index);
  114. inline QString fileName(const QModelIndex &index) const;
  115. inline QIcon fileIcon(const QModelIndex &index) const;
  116. QFile::Permissions permissions(const QModelIndex &index) const;
  117. QFileInfo fileInfo(const QModelIndex &index) const;
  118. bool remove(const QModelIndex &index);
  119. protected:
  120. QFileSystemModel(QFileSystemModelPrivate &, QObject *parent = Q_NULLPTR);
  121. void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;
  122. bool event(QEvent *event) Q_DECL_OVERRIDE;
  123. private:
  124. Q_DECLARE_PRIVATE(QFileSystemModel)
  125. Q_DISABLE_COPY(QFileSystemModel)
  126. Q_PRIVATE_SLOT(d_func(), void _q_directoryChanged(const QString &directory, const QStringList &list))
  127. Q_PRIVATE_SLOT(d_func(), void _q_performDelayedSort())
  128. Q_PRIVATE_SLOT(d_func(), void _q_fileSystemChanged(const QString &path, const QVector<QPair<QString, QFileInfo> > &))
  129. Q_PRIVATE_SLOT(d_func(), void _q_resolvedName(const QString &fileName, const QString &resolvedName))
  130. friend class QFileDialogPrivate;
  131. };
  132. inline QString QFileSystemModel::fileName(const QModelIndex &aindex) const
  133. { return aindex.data(Qt::DisplayRole).toString(); }
  134. inline QIcon QFileSystemModel::fileIcon(const QModelIndex &aindex) const
  135. { return qvariant_cast<QIcon>(aindex.data(Qt::DecorationRole)); }
  136. #endif // QT_NO_FILESYSTEMMODEL
  137. QT_END_NAMESPACE
  138. #endif // QFILESYSTEMMODEL_H