KIO

kfileitemdelegate.h
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2006-2007, 2008 Fredrik Höglund <fredrik@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef KFILEITEMDELEGATE_H
9#define KFILEITEMDELEGATE_H
10
11#include "kiowidgets_export.h"
12#include <KFileItem>
13#include <QAbstractItemDelegate>
14#include <QTextOption>
15
16#include <memory>
17
20class QHelpEvent;
21class QModelIndex;
22class QPainter;
23
24/**
25 * @class KFileItemDelegate kfileitemdelegate.h <KFileItemDelegate>
26 *
27 * KFileItemDelegate is intended to be used to provide a KDE file system
28 * view, when using one of the standard item views in Qt with KDirModel.
29 *
30 * While primarily intended to be used with KDirModel, it uses
31 * Qt::DecorationRole and Qt::DisplayRole for the icons and text labels,
32 * just like QItemDelegate, and can thus be used with any standard model.
33 *
34 * When used with KDirModel however, KFileItemDelegate can change the way
35 * the display and/or decoration roles are drawn, based on properties
36 * of the file items. For example, if the file item is a symbolic link,
37 * it will use an italic font to draw the file name.
38 *
39 * KFileItemDelegate also supports showing additional information about
40 * the file items below the icon labels.
41 *
42 * Which information should be shown, if any, is controlled by the
43 * @ref information property, which is a list that can be set by calling
44 * setShowInformation(), and read by calling showInformation().
45 * By default this list is empty.
46 *
47 * To use KFileItemDelegate, instantiate an object from the delegate,
48 * and call setItemDelegate() in one of the standard item views in Qt:
49 *
50 * @code
51 * QListView *listview = new QListView(this);
52 * KFileItemDelegate *delegate = new KFileItemDelegate(this);
53 * listview->setItemDelegate(delegate);
54 * @endcode
55 */
56class KIOWIDGETS_EXPORT KFileItemDelegate : public QAbstractItemDelegate
57{
59
60 /**
61 * This property holds which additional information (if any) should be shown below
62 * items in icon views.
63 *
64 * Access functions:
65 * @li void setShownformation(InformationList information)
66 * @li InformationList showInformation() const
67 */
69
70 /**
71 * This property holds the color used for the text shadow.
72 *
73 * The alpha value in the color determines the opacity of the shadow.
74 * Shadows are only rendered when the alpha value is non-zero.
75 * The default value for this property is Qt::transparent.
76 *
77 * Access functions:
78 * @li void setShadowColor(const QColor &color)
79 * @li QColor shadowColor() const
80 */
82
83 /**
84 * This property holds the horizontal and vertical offset for the text shadow.
85 * The default value for this property is (1, 1).
86 *
87 * Access functions:
88 * @li void setShadowOffset(const QPointF &offset)
89 * @li QPointF shadowOffset() const
90 */
92
93 /**
94 * This property holds the blur radius for the text shadow.
95 * The default value for this property is 2.
96 *
97 * Access functions:
98 * @li void setShadowBlur(qreal radius)
99 * @li qreal shadowBlur() const
100 */
102
103 /**
104 * This property holds the maximum size that can be returned
105 * by KFileItemDelegate::sizeHint(). If the maximum size is empty,
106 * it will be ignored.
107 */
109
110 /**
111 * This property determines whether a tooltip will be shown by the delegate
112 * if the display role is elided. This tooltip will contain the full display
113 * role information. The tooltip will only be shown if the Qt::ToolTipRole differs
114 * from Qt::DisplayRole, or if they match, showToolTipWhenElided flag is set and
115 * the display role information is elided.
116 */
118
119 /**
120 * This property determines if there are KIO jobs on a destination URL visible, then
121 * they will have a small animation overlay displayed on them.
122 */
124
125public:
126 /**
127 * This enum defines the additional information that can be displayed below item
128 * labels in icon views.
129 *
130 * The information will only be shown for indexes for which the model provides
131 * a valid value for KDirModel::FileItemRole, and only when there's sufficient vertical
132 * space to display at least one line of the information, along with the display label.
133 *
134 * For the number of items to be shown for folders, the model must provide a valid
135 * value for KDirMode::ChildCountRole, in addition to KDirModel::FileItemRole.
136 *
137 * Note that KFileItemDelegate will not call KFileItem::determineMimeType() if
138 * KFileItem::isMimeTypeKnown() returns false, so if you want to display MIME types
139 * you should use a KMimeTypeResolver with the model and the view, to ensure that MIME
140 * types are resolved. If the MIME type isn't known, "Unknown" will be displayed until
141 * the MIME type has been successfully resolved.
142 *
143 * @see setShowInformation()
144 * @see showInformation()
145 * @see information
146 */
148 NoInformation, ///< No additional information will be shown for items.
149 Size, ///< The file size for files, and the number of items for folders.
150 Permissions, ///< A UNIX permissions string, e.g.\ -rwxr-xr-x.
151 OctalPermissions, ///< The permissions as an octal value, e.g.\ 0644.
152 Owner, ///< The user name of the file owner, e.g.\ root
153 OwnerAndGroup, ///< The user and group that owns the file, e.g.\ root:root
154 CreationTime, ///< The date and time the file/folder was created.
155 ModificationTime, ///< The date and time the file/folder was last modified.
156 AccessTime, ///< The date and time the file/folder was last accessed.
157 MimeType, ///< The MIME type for the item, e.g.\ text/html.
158 FriendlyMimeType, ///< The descriptive name for the MIME type, e.g.\ HTML Document.
159 LinkDest, ///< The destination of a symbolic link. @since 4.5
160 LocalPathOrUrl, ///< The local path to the file or the URL in case it is not a local file. @since 4.5
161 Comment, ///< A simple comment that can be displayed to the user as is. @since 4.6
162 };
163 Q_ENUM(Information)
164
165 typedef QList<Information> InformationList;
166
167 /**
168 * Constructs a new KFileItemDelegate.
169 *
170 * @param parent The parent object for the delegate.
171 */
172 explicit KFileItemDelegate(QObject *parent = nullptr);
173
174 /**
175 * Destroys the item delegate.
176 */
178
179 /**
180 * Returns the nominal size for the item referred to by @p index, given the
181 * provided options.
182 *
183 * If the model provides a valid Qt::FontRole and/or Qt::TextAlignmentRole for the item,
184 * those will be used instead of the ones specified in the style options.
185 *
186 * This function is reimplemented from @ref QAbstractItemDelegate.
187 *
188 * @param option The style options that should be used when painting the item.
189 * @param index The index to the item for which to return the size hint.
190 */
191 QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
192
193 /**
194 * Paints the item indicated by @p index, using @p painter.
195 *
196 * The item will be drawn in the rectangle specified by option.rect.
197 * The correct size for that rectangle can be obtained by calling
198 * @ref sizeHint().
199 *
200 * This function will use the following data values if the model provides
201 * them for the item, in place of the values in @p option:
202 *
203 * @li Qt::FontRole The font that should be used for the display role.
204 * @li Qt::TextAlignmentRole The alignment of the display role.
205 * @li Qt::ForegroundRole The text color for the display role.
206 * @li Qt::BackgroundRole The background color for the item.
207 *
208 * This function is reimplemented from @ref QAbstractItemDelegate.
209 *
210 * @param painter The painter with which to draw the item.
211 * @param option The style options that should be used when painting the item.
212 * @param index The index to the item that should be painted.
213 */
214 void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
215
216 /**
217 * Reimplemented from @ref QAbstractItemDelegate.
218 */
219 QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
220
221 /**
222 * Reimplemented from @ref QAbstractItemDelegate.
223 */
224 bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override;
225
226 /**
227 * Reimplemented from @ref QAbstractItemDelegate.
228 */
229 void setEditorData(QWidget *editor, const QModelIndex &index) const override;
230
231 /**
232 * Reimplemented from @ref QAbstractItemDelegate.
233 */
234 void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
235
236 /**
237 * Reimplemented from @ref QAbstractItemDelegate.
238 */
239 void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
240
241 /**
242 * Sets the list of information lines that are shown below the icon label in list views.
243 *
244 * You will typically construct the list like this:
245 * @code
246 * KFileItemDelegate::InformationList list;
247 * list << KFileItemDelegate::FriendlyMimeType << KFileItemDelegate::Size;
248 * delegate->setShowInformation(list);
249 * @endcode
250 *
251 * The information lines will be displayed in the list order.
252 * The delegate will first draw the item label, and then as many information
253 * lines as will fit in the available space.
254 *
255 * @param list A list of information items that should be shown
256 */
257 void setShowInformation(const InformationList &list);
258
259 /**
260 * Sets a single information line that is shown below the icon label in list views.
261 *
262 * This is a convenience function for when you only want to show a single line
263 * of information.
264 *
265 * @param information The information that should be shown
266 */
268
269 /**
270 * Returns the file item information that should be shown below item labels in list views.
271 */
272 InformationList showInformation() const;
273
274 /**
275 * Sets the color used for drawing the text shadow.
276 *
277 * To enable text shadows, set the shadow color to a non-transparent color.
278 * To disable text shadows, set the color to Qt::transparent.
279 *
280 * @see shadowColor()
281 */
282 void setShadowColor(const QColor &color);
283
284 /**
285 * Returns the color used for the text shadow.
286 *
287 * @see setShadowColor()
288 */
289 QColor shadowColor() const;
290
291 /**
292 * Sets the horizontal and vertical offset for the text shadow.
293 *
294 * @see shadowOffset()
295 */
296 void setShadowOffset(const QPointF &offset);
297
298 /**
299 * Returns the offset used for the text shadow.
300 *
301 * @see setShadowOffset()
302 */
303 QPointF shadowOffset() const;
304
305 /**
306 * Sets the blur radius for the text shadow.
307 *
308 * @see shadowBlur()
309 */
310 void setShadowBlur(qreal radius);
311
312 /**
313 * Returns the blur radius for the text shadow.
314 *
315 * @see setShadowBlur()
316 */
317 qreal shadowBlur() const;
318
319 /**
320 * Sets the maximum size for KFileItemDelegate::sizeHint().
321 *
322 * @see maximumSize()
323 */
324 void setMaximumSize(const QSize &size);
325
326 /**
327 * Returns the maximum size for KFileItemDelegate::sizeHint().
328 *
329 * @see setMaximumSize()
330 */
331 QSize maximumSize() const;
332
333 /**
334 * Sets whether a tooltip should be shown if the display role is
335 * elided containing the full display role information.
336 *
337 * @note The tooltip will only be shown if the Qt::ToolTipRole differs
338 * from Qt::DisplayRole, or if they match, showToolTipWhenElided
339 * flag is set and the display role information is elided.
340 * @see showToolTipWhenElided()
341 */
342 void setShowToolTipWhenElided(bool showToolTip);
343
344 /**
345 * Returns whether a tooltip should be shown if the display role
346 * is elided containing the full display role information.
347 *
348 * @note The tooltip will only be shown if the Qt::ToolTipRole differs
349 * from Qt::DisplayRole, or if they match, showToolTipWhenElided
350 * flag is set and the display role information is elided.
351 * @see setShowToolTipWhenElided()
352 */
353 bool showToolTipWhenElided() const;
354
355 /**
356 * Returns the rectangle of the icon that is aligned inside the decoration
357 * rectangle.
358 */
359 QRect iconRect(const QStyleOptionViewItem &option, const QModelIndex &index) const;
360
361 /**
362 * When the contents text needs to be wrapped, @p wrapMode strategy
363 * will be followed.
364 *
365 */
367
368 /**
369 * Returns the wrapping strategy followed to show text when it needs
370 * wrapping.
371 *
372 */
374
375 /**
376 * Enable/Disable the displaying of an animated overlay that is shown for any destination
377 * urls (in the view). When enabled, the animations (if any) will be drawn automatically.
378 *
379 * Only the files/folders that are visible and have jobs associated with them
380 * will display the animation.
381 * You would likely not want this enabled if you perform some kind of custom painting
382 * that takes up a whole item, and will just make this(and what you paint) look funky.
383 *
384 * Default is disabled.
385 *
386 * Note: The model (KDirModel) needs to have it's method called with the same
387 * value, when you make the call to this method.
388 */
390
391 /**
392 * Returns whether or not the displaying of job transfers is enabled.
393 * @see setJobTransfersVisible()
394 */
395 bool jobTransfersVisible() const;
396
397 /**
398 * Reimplemented from @ref QAbstractItemDelegate.
399 */
400 bool eventFilter(QObject *object, QEvent *event) override;
401
402 /**
403 * @return The rectangle where selectionEmblem is being drawn
404 */
406
407 /**
408 * Set the rectangle where selectionEmblem should be drawn in.
409 */
410 void setSelectionEmblemRect(QRect rect, int iconSize);
411
412 KFileItem fileItem(const QModelIndex &index) const;
413
414public Q_SLOTS:
415 /**
416 * Reimplemented from @ref QAbstractItemDelegate.
417 */
418 bool helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index) override;
419
420 /**
421 * Returns the shape of the item as a region.
422 * The returned region can be used for precise hit testing of the item.
423 */
424 QRegion shape(const QStyleOptionViewItem &option, const QModelIndex &index);
425
426private:
427 class Private;
428 std::unique_ptr<Private> const d; /// @internal
429 Q_DISABLE_COPY(KFileItemDelegate)
430
431 void drawSelectionEmblem(QStyleOptionViewItem option, QPainter *painter, const QModelIndex &index) const;
432};
433
434#endif // KFILEITEMDELEGATE_H
KFileItemDelegate is intended to be used to provide a KDE file system view, when using one of the sta...
void setShowInformation(const InformationList &list)
Sets the list of information lines that are shown below the icon label in list views.
void setShadowColor(const QColor &color)
Sets the color used for drawing the text shadow.
QPointF shadowOffset
This property holds the horizontal and vertical offset for the text shadow.
QColor shadowColor
This property holds the color used for the text shadow.
void setSelectionEmblemRect(QRect rect, int iconSize)
Set the rectangle where selectionEmblem should be drawn in.
InformationList information
This property holds which additional information (if any) should be shown below items in icon views.
void setShadowOffset(const QPointF &offset)
Sets the horizontal and vertical offset for the text shadow.
qreal shadowBlur
This property holds the blur radius for the text shadow.
QRect selectionEmblemRect() const
void setMaximumSize(const QSize &size)
Sets the maximum size for KFileItemDelegate::sizeHint().
void setWrapMode(QTextOption::WrapMode wrapMode)
When the contents text needs to be wrapped, wrapMode strategy will be followed.
QRect iconRect(const QStyleOptionViewItem &option, const QModelIndex &index) const
Returns the rectangle of the icon that is aligned inside the decoration rectangle.
Information
This enum defines the additional information that can be displayed below item labels in icon views.
@ Comment
A simple comment that can be displayed to the user as is.
@ OctalPermissions
The permissions as an octal value, e.g. 0644.
@ OwnerAndGroup
The user and group that owns the file, e.g. root:root.
@ LocalPathOrUrl
The local path to the file or the URL in case it is not a local file.
@ AccessTime
The date and time the file/folder was last accessed.
@ FriendlyMimeType
The descriptive name for the MIME type, e.g. HTML Document.
@ CreationTime
The date and time the file/folder was created.
@ ModificationTime
The date and time the file/folder was last modified.
@ Permissions
A UNIX permissions string, e.g. -rwxr-xr-x.
@ Size
The file size for files, and the number of items for folders.
@ MimeType
The MIME type for the item, e.g. text/html.
@ LinkDest
The destination of a symbolic link.
@ NoInformation
No additional information will be shown for items.
@ Owner
The user name of the file owner, e.g. root.
QSize maximumSize
This property holds the maximum size that can be returned by KFileItemDelegate::sizeHint().
bool showToolTipWhenElided
This property determines whether a tooltip will be shown by the delegate if the display role is elide...
KFileItemDelegate(QObject *parent=nullptr)
Constructs a new KFileItemDelegate.
void setJobTransfersVisible(bool jobTransfersVisible)
Enable/Disable the displaying of an animated overlay that is shown for any destination urls (in the v...
void setShadowBlur(qreal radius)
Sets the blur radius for the text shadow.
bool jobTransfersVisible
This property determines if there are KIO jobs on a destination URL visible, then they will have a sm...
~KFileItemDelegate() override
Destroys the item delegate.
InformationList showInformation() const
Returns the file item information that should be shown below item labels in list views.
void setShowToolTipWhenElided(bool showToolTip)
Sets whether a tooltip should be shown if the display role is elided containing the full display role...
QTextOption::WrapMode wrapMode() const
Returns the wrapping strategy followed to show text when it needs wrapping.
QRegion shape(const QStyleOptionViewItem &option, const QModelIndex &index)
Returns the shape of the item as a region.
A KFileItem is a generic class to handle a file, local or remote.
Definition kfileitem.h:36
QAbstractItemDelegate(QObject *parent)
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const const
virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
virtual bool helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const const=0
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const const
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const const
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const const=0
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const const
QObject(QObject *parent)
Q_ENUM(...)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SLOTSQ_SLOTS
virtual bool event(QEvent *e)
virtual bool eventFilter(QObject *watched, QEvent *event)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 11 2025 11:51:43 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.