| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QFILESYSTEMMODEL_H |
| 5 | #define QFILESYSTEMMODEL_H |
| 6 | |
| 7 | #include <QtGui/qtguiglobal.h> |
| 8 | #include <QtCore/qabstractitemmodel.h> |
| 9 | #include <QtCore/qdir.h> |
| 10 | #include <QtGui/qicon.h> |
| 11 | |
| 12 | QT_REQUIRE_CONFIG(filesystemmodel); |
| 13 | |
| 14 | QT_BEGIN_NAMESPACE |
| 15 | |
| 16 | class ExtendedInformation; |
| 17 | class QFileSystemModelPrivate; |
| 18 | class QAbstractFileIconProvider; |
| 19 | |
| 20 | class Q_GUI_EXPORT QFileSystemModel : public QAbstractItemModel |
| 21 | { |
| 22 | Q_OBJECT |
| 23 | Q_PROPERTY(bool resolveSymlinks READ resolveSymlinks WRITE setResolveSymlinks) |
| 24 | Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly) |
| 25 | Q_PROPERTY(bool nameFilterDisables READ nameFilterDisables WRITE setNameFilterDisables) |
| 26 | Q_PROPERTY(Options options READ options WRITE setOptions) |
| 27 | |
| 28 | Q_SIGNALS: |
| 29 | void rootPathChanged(const QString &newPath); |
| 30 | void fileRenamed(const QString &path, const QString &oldName, const QString &newName); |
| 31 | void directoryLoaded(const QString &path); |
| 32 | |
| 33 | public: |
| 34 | enum Roles { |
| 35 | FileIconRole = Qt::DecorationRole, |
| 36 | |
| 37 | FileInfoRole = Qt::FileInfoRole, // New values go before, -5, -6 ..etc |
| 38 | QT7_ONLY( |
| 39 | FilePathRole = Qt::UserRole - 3, |
| 40 | FileNameRole = Qt::UserRole - 2, |
| 41 | FilePermissions = Qt::UserRole - 1, |
| 42 | ) |
| 43 | |
| 44 | QT6_ONLY( |
| 45 | FilePathRole = Qt::UserRole + 1, |
| 46 | FileNameRole = Qt::UserRole + 2, |
| 47 | FilePermissions = Qt::UserRole + 3, |
| 48 | ) |
| 49 | }; |
| 50 | |
| 51 | enum Option |
| 52 | { |
| 53 | DontWatchForChanges = 0x00000001, |
| 54 | DontResolveSymlinks = 0x00000002, |
| 55 | DontUseCustomDirectoryIcons = 0x00000004 |
| 56 | }; |
| 57 | Q_ENUM(Option) |
| 58 | Q_DECLARE_FLAGS(Options, Option) |
| 59 | |
| 60 | explicit QFileSystemModel(QObject *parent = nullptr); |
| 61 | ~QFileSystemModel(); |
| 62 | |
| 63 | QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; |
| 64 | QModelIndex index(const QString &path, int column = 0) const; |
| 65 | QModelIndex parent(const QModelIndex &child) const override; |
| 66 | using QObject::parent; |
| 67 | QModelIndex sibling(int row, int column, const QModelIndex &idx) const override; |
| 68 | bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; |
| 69 | bool canFetchMore(const QModelIndex &parent) const override; |
| 70 | void fetchMore(const QModelIndex &parent) override; |
| 71 | |
| 72 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
| 73 | int columnCount(const QModelIndex &parent = QModelIndex()) const override; |
| 74 | |
| 75 | QVariant myComputer(int role = Qt::DisplayRole) const; |
| 76 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 77 | bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; |
| 78 | |
| 79 | QVariant (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; |
| 80 | |
| 81 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 82 | |
| 83 | void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; |
| 84 | |
| 85 | QStringList mimeTypes() const override; |
| 86 | QMimeData *mimeData(const QModelIndexList &indexes) const override; |
| 87 | bool dropMimeData(const QMimeData *data, Qt::DropAction action, |
| 88 | int row, int column, const QModelIndex &parent) override; |
| 89 | Qt::DropActions supportedDropActions() const override; |
| 90 | QHash<int, QByteArray> roleNames() const override; |
| 91 | |
| 92 | // QFileSystemModel specific API |
| 93 | QModelIndex setRootPath(const QString &path); |
| 94 | QString rootPath() const; |
| 95 | QDir rootDirectory() const; |
| 96 | |
| 97 | void setIconProvider(QAbstractFileIconProvider *provider); |
| 98 | QAbstractFileIconProvider *iconProvider() const; |
| 99 | |
| 100 | void setFilter(QDir::Filters filters); |
| 101 | QDir::Filters filter() const; |
| 102 | |
| 103 | void setResolveSymlinks(bool enable); |
| 104 | bool resolveSymlinks() const; |
| 105 | |
| 106 | void setReadOnly(bool enable); |
| 107 | bool isReadOnly() const; |
| 108 | |
| 109 | void setNameFilterDisables(bool enable); |
| 110 | bool nameFilterDisables() const; |
| 111 | |
| 112 | void setNameFilters(const QStringList &filters); |
| 113 | QStringList nameFilters() const; |
| 114 | |
| 115 | void setOption(Option option, bool on = true); |
| 116 | bool testOption(Option option) const; |
| 117 | void setOptions(Options options); |
| 118 | Options options() const; |
| 119 | |
| 120 | QString filePath(const QModelIndex &index) const; |
| 121 | bool isDir(const QModelIndex &index) const; |
| 122 | qint64 size(const QModelIndex &index) const; |
| 123 | QString type(const QModelIndex &index) const; |
| 124 | |
| 125 | QDateTime lastModified(const QModelIndex &index) const; |
| 126 | QDateTime lastModified(const QModelIndex &index, const QTimeZone &tz) const; |
| 127 | |
| 128 | QModelIndex mkdir(const QModelIndex &parent, const QString &name); |
| 129 | bool rmdir(const QModelIndex &index); |
| 130 | inline QString fileName(const QModelIndex &index) const; |
| 131 | inline QIcon fileIcon(const QModelIndex &index) const; |
| 132 | QFile::Permissions permissions(const QModelIndex &index) const; |
| 133 | QFileInfo fileInfo(const QModelIndex &index) const; |
| 134 | bool remove(const QModelIndex &index); |
| 135 | |
| 136 | protected: |
| 137 | QFileSystemModel(QFileSystemModelPrivate &, QObject *parent = nullptr); |
| 138 | void timerEvent(QTimerEvent *event) override; |
| 139 | bool event(QEvent *event) override; |
| 140 | |
| 141 | private: |
| 142 | Q_DECLARE_PRIVATE(QFileSystemModel) |
| 143 | Q_DISABLE_COPY(QFileSystemModel) |
| 144 | |
| 145 | friend class QFileDialogPrivate; |
| 146 | }; |
| 147 | |
| 148 | inline QString QFileSystemModel::fileName(const QModelIndex &aindex) const |
| 149 | { |
| 150 | return aindex.data(arole: FileNameRole).toString(); |
| 151 | } |
| 152 | inline QIcon QFileSystemModel::fileIcon(const QModelIndex &aindex) const |
| 153 | { return qvariant_cast<QIcon>(v: aindex.data(arole: Qt::DecorationRole)); } |
| 154 | |
| 155 | Q_DECLARE_OPERATORS_FOR_FLAGS(QFileSystemModel::Options) |
| 156 | |
| 157 | QT_END_NAMESPACE |
| 158 | |
| 159 | #endif // QFILESYSTEMMODEL_H |
| 160 | |