KIO

kdirmodel.h
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2006 David Faure <faure@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef KDIRMODEL_H
9#define KDIRMODEL_H
10
11#include "kiowidgets_export.h"
12#include <QAbstractItemModel>
13#include <kfileitem.h>
14
15#include <memory>
16
17class KDirLister;
18class KDirModelPrivate;
19class JobUrlCache;
20
21/**
22 * @class KDirModel kdirmodel.h <KDirModel>
23 *
24 * @short A model for a KIO-based directory tree.
25 *
26 * KDirModel implements the QAbstractItemModel interface (for use with Qt's model/view widgets)
27 * around the directory listing for one directory or a tree of directories.
28 *
29 * Note that there are some cases when using QPersistentModelIndexes from this model will not give
30 * expected results. QPersistentIndexes will remain valid and updated if its siblings are added or
31 * removed. However, if the QPersistentIndex or one of its ancestors is moved, the QPersistentIndex will become
32 * invalid. For example, if a file or directory is renamed after storing a QPersistentModelIndex for it,
33 * the index (along with any stored children) will become invalid even though it is still in the model. The reason
34 * for this is that moves of files and directories are treated as separate insert and remove actions.
35 *
36 * @see KDirSortFilterProxyModel
37 *
38 * @author David Faure
39 * Based on work by Hamish Rodda and Pascal Letourneau
40 */
41class KIOWIDGETS_EXPORT KDirModel : public QAbstractItemModel
42{
43 Q_OBJECT
44
45public:
46 /**
47 * @param parent parent qobject
48 */
49 explicit KDirModel(QObject *parent = nullptr);
50 ~KDirModel() override;
51
52 /**
53 * Flags for the openUrl() method
54 * @see OpenUrlFlags
55 * @since 5.69
56 */
58 NoFlags = 0x0, ///< No additional flags specified.
59 Reload = 0x1, ///< Indicates whether to use the cache or to reread
60 ///< the directory from the disk.
61 ///< Use only when opening a dir not yet listed by our dirLister()
62 ///< without using the cache. Otherwise use dirLister()->updateDirectory().
63 ShowRoot = 0x2, ///< Display a root node for the URL being opened.
64 };
65 /**
66 * Stores a combination of #OpenUrlFlag values.
67 */
68 Q_DECLARE_FLAGS(OpenUrlFlags, OpenUrlFlag)
70
71 /**
72 * Display the contents of @p url in the model.
73 * Apart from the support for the ShowRoot flag, this is equivalent to dirLister()->openUrl(url, flags)
74 * @param url the URL of the directory whose contents should be listed.
75 * Unless ShowRoot is set, the item for this directory will NOT be shown, the model starts at its children.
76 * @param flags see OpenUrlFlag
77 * @since 5.69
78 */
79 Q_INVOKABLE void openUrl(const QUrl &url, OpenUrlFlags flags = NoFlags);
80
81 /**
82 * Set the directory lister to use by this model, instead of the default KDirLister created internally.
83 * The model takes ownership.
84 */
85 void setDirLister(KDirLister *dirLister);
86
87 /**
88 * Return the directory lister used by this model.
89 */
90 KDirLister *dirLister() const;
91
92 /**
93 * Return the fileitem for a given index. This is O(1), i.e. fast.
94 */
95 KFileItem itemForIndex(const QModelIndex &index) const;
96
97 /**
98 * Return the index for a given kfileitem. This can be slow.
99 */
100 Q_INVOKABLE QModelIndex indexForItem(const KFileItem &) const;
101
102 /**
103 * Return the index for a given url. This can be slow.
104 */
105 Q_INVOKABLE QModelIndex indexForUrl(const QUrl &url) const;
106
107 /**
108 * @short Lists subdirectories using fetchMore() as needed until the given @p url exists in the model.
109 *
110 * When the model is used by a treeview, call KDirLister::openUrl with the base url of the tree,
111 * then the treeview will take care of calling fetchMore() when the user opens directories.
112 * However if you want the tree to show a given URL (i.e. open the tree recursively until that URL),
113 * call expandToUrl().
114 * Note that this is asynchronous; the necessary listing of subdirectories will take time so
115 * the model will not immediately have this url available.
116 * The model emits the signal expand() when an index has become available; this can be connected
117 * to the treeview in order to let it open that index.
118 * @param url the url of a subdirectory of the directory model (or a file in a subdirectory)
119 */
120 Q_INVOKABLE void expandToUrl(const QUrl &url);
121
122 /**
123 * Notify the model that the item at this index has changed.
124 * For instance because KMimeTypeResolver called determineMimeType on it.
125 * This makes the model emit its dataChanged signal at this index, so that views repaint.
126 * Note that for most things (renaming, changing size etc.), KDirLister's signals tell the model already.
127 */
128 Q_INVOKABLE void itemChanged(const QModelIndex &index);
129
130 /**
131 * Forget all previews (optimization for turning previews off).
132 * The items will again have their default appearance (not controlled by the model).
133 * @since 5.28
134 */
135 Q_INVOKABLE void clearAllPreviews();
136
137 /**
138 * Useful "default" columns. Views can use a proxy to have more control over this.
139 */
141 Name = 0,
142 Size,
143 ModifiedTime,
144 Permissions,
145 Owner,
146 Group,
147 Type,
148 ColumnCount,
149 };
150
151 /// Possible return value for data(ChildCountRole), meaning the item isn't a directory,
152 /// or we haven't calculated its child count yet
153 enum {
154 ChildCountUnknown = -1
155 };
156
158 // Note: use printf "0x%08X\n" $(($RANDOM*$RANDOM))
159 // to define additional roles.
160 FileItemRole = 0x07A263FF, ///< returns the KFileItem for a given index. roleName is "fileItem".
161 ChildCountRole = 0x2C4D0A40, ///< returns the number of items in a directory, or ChildCountUnknown. roleName is "childCount".
162 HasJobRole = 0x01E555A5, ///< returns whether or not there is a job on an item (file/directory). roleName is "hasJob".
163 };
164
165 /**
166 * @see DropsAllowed
167 */
169 NoDrops = 0,
170 DropOnDirectory = 1, ///< allow drops on any directory
171 DropOnAnyFile = 2, ///< allow drops on any file
172 DropOnLocalExecutable = 4, ///< allow drops on local executables, shell scripts and desktop files. Can be used with DropOnDirectory.
173 };
174 /**
175 * Stores a combination of #DropsAllowedFlag values.
176 */
179
180 /// Set whether dropping onto items should be allowed, and for which kind of item
181 /// Drops are disabled by default.
182 Q_INVOKABLE void setDropsAllowed(DropsAllowed dropsAllowed);
183
184 /// Reimplemented from QAbstractItemModel. Returns true for empty directories.
185 bool canFetchMore(const QModelIndex &parent) const override;
186 /// Reimplemented from QAbstractItemModel. Returns ColumnCount.
187 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
188 /// Reimplemented from QAbstractItemModel.
189 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
190 /// Reimplemented from QAbstractItemModel. Not implemented yet.
191 bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
192 /// Reimplemented from QAbstractItemModel. Lists the subdirectory.
193 void fetchMore(const QModelIndex &parent) override;
194 /// Reimplemented from QAbstractItemModel.
195 Qt::ItemFlags flags(const QModelIndex &index) const override;
196 /// Reimplemented from QAbstractItemModel. Returns true for directories.
197 bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
198 /// Reimplemented from QAbstractItemModel. Returns the column titles.
199 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
200 /// Reimplemented from QAbstractItemModel. O(1)
201 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
202 /// Reimplemented from QAbstractItemModel.
203 QMimeData *mimeData(const QModelIndexList &indexes) const override;
204 /// Reimplemented from QAbstractItemModel.
205 QStringList mimeTypes() const override;
206 /// Reimplemented from QAbstractItemModel.
207 QModelIndex parent(const QModelIndex &index) const override;
208 /// Reimplemented from QAbstractItemModel.
209 QModelIndex sibling(int row, int column, const QModelIndex &index) const override;
210 /// Reimplemented from QAbstractItemModel.
211 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
212 /// Reimplemented from QAbstractItemModel.
213 /// Call this to set a new icon, e.g. a preview
214 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
215 /// Reimplemented from QAbstractItemModel. Not implemented. @see KDirSortFilterProxyModel
216 void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
217 /// Reimplemented from QAbstractItemModel.
218 /// @see AdditionalRoles
219 QHash<int, QByteArray> roleNames() const override;
220
221 /**
222 * Remove urls from the list if an ancestor is present on the list. This can
223 * be used to delete only the ancestor url and skip a potential error of a non-existent url.
224 *
225 * For example, for a list of "/home/foo/a", "/home/foo/a/a.txt", "/home/foo/a/a/a.txt", "/home/foo/a/b/b.txt",
226 * "home/foo/b/b.txt", this method will return the list "/home/foo/a", "/home/foo/b/b.txt".
227 *
228 * @return the list @p urls without parented urls inside.
229 */
230 static QList<QUrl> simplifiedUrlList(const QList<QUrl> &urls);
231
232 /**
233 * This emits the needSequenceIcon signal, requesting another sequence icon
234 *
235 * If there is a KFilePreviewGenerator attached to this model, that generator will care
236 * about creating another preview.
237 *
238 * @param index Index of the item that should get another icon
239 * @param sequenceIndex Index in the sequence. If it is zero, the standard icon will be assigned.
240 * For higher indices, arbitrary different meaningful icons will be generated.
241 */
242 void requestSequenceIcon(const QModelIndex &index, int sequenceIndex);
243
244 /**
245 * Enable/Disable the displaying of an animated overlay that is shown for any destination
246 * urls (in the view). When enabled, the animations (if any) will be drawn automatically.
247 *
248 * Only the files/folders that are visible and have jobs associated with them
249 * will display the animation.
250 * You would likely not want this enabled if you perform some kind of custom painting
251 * that takes up a whole item, and will just make this(and what you paint) look funky.
252 *
253 * Default is disabled.
254 *
255 * Note: KFileItemDelegate needs to have it's method called with the same
256 * value, when you make the call to this method.
257 */
258 void setJobTransfersVisible(bool show);
259
260 /**
261 * Returns whether or not displaying job transfers has been enabled.
262 */
263 bool jobTransfersVisible() const;
264
265 Qt::DropActions supportedDropActions() const override;
266
268 /**
269 * Emitted for each subdirectory that is a parent of a url passed to expandToUrl
270 * This allows to asynchronously open a tree view down to a given directory.
271 * Also emitted for the final file, if expandToUrl is called with a file
272 * (for instance so that it can be selected).
273 */
274 void expand(const QModelIndex &index);
275 /**
276 * Emitted when another icon sequence index is requested
277 * @param index Index of the item that should get another icon
278 * @param sequenceIndex Index in the sequence. If it is zero, the standard icon should be assigned.
279 * For higher indices, arbitrary different meaningful icons should be generated.
280 * This is usually slowly counted up while the user hovers the icon.
281 * If no meaningful alternative icons can be generated, this should be ignored.
282 */
283 void needSequenceIcon(const QModelIndex &index, int sequenceIndex);
284
285private:
286 // Make those private, they shouldn't be called by applications
287 bool insertRows(int, int, const QModelIndex & = QModelIndex()) override;
288 bool insertColumns(int, int, const QModelIndex & = QModelIndex()) override;
289 bool removeRows(int, int, const QModelIndex & = QModelIndex()) override;
290 bool removeColumns(int, int, const QModelIndex & = QModelIndex()) override;
291
292private:
293 friend class KDirModelPrivate;
294 std::unique_ptr<KDirModelPrivate> const d;
295};
296
297Q_DECLARE_OPERATORS_FOR_FLAGS(KDirModel::DropsAllowed)
298Q_DECLARE_OPERATORS_FOR_FLAGS(KDirModel::OpenUrlFlags)
299
300#endif /* KDIRMODEL_H */
Subclass of KCoreDirLister which uses QWidgets to show error messages and to associate jobs with wind...
Definition kdirlister.h:25
A model for a KIO-based directory tree.
Definition kdirmodel.h:42
void expand(const QModelIndex &index)
Emitted for each subdirectory that is a parent of a url passed to expandToUrl This allows to asynchro...
void needSequenceIcon(const QModelIndex &index, int sequenceIndex)
Emitted when another icon sequence index is requested.
OpenUrlFlag
Flags for the openUrl() method.
Definition kdirmodel.h:57
ModelColumns
Useful "default" columns.
Definition kdirmodel.h:140
A KFileItem is a generic class to handle a file, local or remote.
Definition kfileitem.h:36
virtual bool canFetchMore(const QModelIndex &parent) const const
virtual int columnCount(const QModelIndex &parent) const const=0
virtual QVariant data(const QModelIndex &index, int role) const const=0
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
virtual void fetchMore(const QModelIndex &parent)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
virtual bool hasChildren(const QModelIndex &parent) const const
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual bool insertColumns(int column, int count, const QModelIndex &parent)
virtual bool insertRows(int row, int count, const QModelIndex &parent)
virtual QMimeData * mimeData(const QModelIndexList &indexes) const const
virtual QStringList mimeTypes() const const
virtual bool removeColumns(int column, int count, const QModelIndex &parent)
virtual bool removeRows(int row, int count, const QModelIndex &parent)
virtual QHash< int, QByteArray > roleNames() const const
virtual int rowCount(const QModelIndex &parent) const const=0
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
virtual QModelIndex sibling(int row, int column, const QModelIndex &index) const const
virtual void sort(int column, Qt::SortOrder order)
virtual Qt::DropActions supportedDropActions() const const
Q_FLAG(...)
Q_SIGNALSQ_SIGNALS
QObject * parent() const const
DropAction
DisplayRole
typedef ItemFlags
Orientation
SortOrder
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.