KFileItemDelegate Class

KFileItemDelegate is intended to be used to provide a KDE file system view, when using one of the standard item views in Qt with KDirModel. More...

Header: #include <KFileItemDelegate>
CMake: find_package(KF6 REQUIRED COMPONENTS KIO)
target_link_libraries(mytarget PRIVATE KF6::KIOWidgets)
Inherits: QAbstractItemDelegate

Public Types

enum Information { NoInformation, Size, Permissions, OctalPermissions, Owner, …, Comment }
InformationList

Properties

Public Functions

KFileItemDelegate(QObject *parent = nullptr)
virtual ~KFileItemDelegate() override
QRect iconRect(const QStyleOptionViewItem &option, const QModelIndex &index) const
bool jobTransfersVisible() const
QSize maximumSize() const
void setJobTransfersVisible(bool jobTransfersVisible)
void setMaximumSize(const QSize &size)
void setShadowBlur(qreal radius)
void setShadowColor(const QColor &color)
void setShadowOffset(const QPointF &offset)
void setShowInformation(KFileItemDelegate::Information information)
void setShowInformation(const KFileItemDelegate::InformationList &list)
void setShowToolTipWhenElided(bool showToolTip)
void setWrapMode(QTextOption::WrapMode wrapMode)
qreal shadowBlur() const
QColor shadowColor() const
QPointF shadowOffset() const
KFileItemDelegate::InformationList showInformation() const
bool showToolTipWhenElided() const
QTextOption::WrapMode wrapMode() const

Reimplemented Public Functions

virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override

Public Slots

QRegion shape(const QStyleOptionViewItem &option, const QModelIndex &index)

Detailed Description

While primarily intended to be used with KDirModel, it uses Qt::DecorationRole and Qt::DisplayRole for the icons and text labels, just like QItemDelegate, and can thus be used with any standard model.

When used with KDirModel however, KFileItemDelegate can change the way the display and/or decoration roles are drawn, based on properties of the file items. For example, if the file item is a symbolic link, it will use an italic font to draw the file name.

KFileItemDelegate also supports showing additional information about the file items below the icon labels.

Which information should be shown, if any, is controlled by the information property, which is a list that can be set by calling setShowInformation(), and read by calling showInformation(). By default this list is empty.

To use KFileItemDelegate, instantiate an object from the delegate, and call setItemDelegate() in one of the standard item views in Qt:

QListView *listview = new QListView(this);
KFileItemDelegate *delegate = new KFileItemDelegate(this);
listview->setItemDelegate(delegate);

Member Type Documentation

enum KFileItemDelegate::Information

This enum defines the additional information that can be displayed below item labels in icon views.

The information will only be shown for indexes for which the model provides a valid value for KDirModel::FileItemRole, and only when there's sufficient vertical space to display at least one line of the information, along with the display label.

For the number of items to be shown for folders, the model must provide a valid value for KDirMode::ChildCountRole, in addition to KDirModel::FileItemRole.

Note that KFileItemDelegate will not call KFileItem::determineMimeType() if KFileItem::isMimeTypeKnown() returns false, so if you want to display MIME types you should use a KMimeTypeResolver with the model and the view, to ensure that MIME types are resolved. If the MIME type isn't known, "Unknown" will be displayed until the MIME type has been successfully resolved.

ConstantValueDescription
KFileItemDelegate::NoInformation0No additional information will be shown for items.
KFileItemDelegate::Size1The file size for files, and the number of items for folders.
KFileItemDelegate::Permissions2A UNIX permissions string, e.g. -rwxr-xr-x.
KFileItemDelegate::OctalPermissions3The permissions as an octal value, e.g. 0644.
KFileItemDelegate::Owner4The user name of the file owner, e.g. root
KFileItemDelegate::OwnerAndGroup5The user and group that owns the file, e.g. root:root
KFileItemDelegate::CreationTime6The date and time the file/folder was created.
KFileItemDelegate::ModificationTime7The date and time the file/folder was last modified.
KFileItemDelegate::AccessTime8The date and time the file/folder was last accessed.
KFileItemDelegate::MimeType9The MIME type for the item, e.g. text/html.
KFileItemDelegate::FriendlyMimeType10The descriptive name for the MIME type, e.g. HTML Document.
KFileItemDelegate::LinkDest (since Qt 4.5)11The destination of a symbolic link.
KFileItemDelegate::LocalPathOrUrl (since Qt 4.5)12The local path to the file or the URL in case it is not a local file.
KFileItemDelegate::Comment (since Qt 4.6)13A simple comment that can be displayed to the user as is.

See also setShowInformation(), showInformation(), and information.

KFileItemDelegate::InformationList

Property Documentation

information : InformationList

This property holds which additional information (if any) should be shown below items in icon views.

Access functions:

KFileItemDelegate::InformationList showInformation() const
void setShowInformation(const KFileItemDelegate::InformationList &list)
void setShowInformation(KFileItemDelegate::Information information)

jobTransfersVisible : bool

This property determines if there are KIO jobs on a destination URL visible, then they will have a small animation overlay displayed on them.

Access functions:

bool jobTransfersVisible() const
void setJobTransfersVisible(bool jobTransfersVisible)

maximumSize : QSize

This property holds the maximum size that can be returned by KFileItemDelegate::sizeHint(). If the maximum size is empty, it will be ignored.

Access functions:

QSize maximumSize() const
void setMaximumSize(const QSize &size)

shadowBlur : qreal

This property holds the blur radius for the text shadow. The default value for this property is 2.

Access functions:

qreal shadowBlur() const
void setShadowBlur(qreal radius)

shadowColor : QColor

This property holds the color used for the text shadow.

The alpha value in the color determines the opacity of the shadow. Shadows are only rendered when the alpha value is non-zero. The default value for this property is Qt::transparent.

Access functions:

QColor shadowColor() const
void setShadowColor(const QColor &color)

shadowOffset : QPointF

This property holds the horizontal and vertical offset for the text shadow. The default value for this property is (1, 1).

Access functions:

QPointF shadowOffset() const
void setShadowOffset(const QPointF &offset)

showToolTipWhenElided : bool

This property determines whether a tooltip will be shown by the delegate if the display role is elided. This tooltip will contain the full display role information. The tooltip will only be shown if the Qt::ToolTipRole differs from Qt::DisplayRole, or if they match, showToolTipWhenElided flag is set and the display role information is elided.

Access functions:

bool showToolTipWhenElided() const
void setShowToolTipWhenElided(bool showToolTip)

Member Function Documentation

[explicit] KFileItemDelegate::KFileItemDelegate(QObject *parent = nullptr)

Constructs a new KFileItemDelegate.

parent The parent object for the delegate.

[override virtual noexcept] KFileItemDelegate::~KFileItemDelegate()

Destroys the item delegate.

QRect KFileItemDelegate::iconRect(const QStyleOptionViewItem &option, const QModelIndex &index) const

Returns the rectangle of the icon that is aligned inside the decoration rectangle.

bool KFileItemDelegate::jobTransfersVisible() const

Returns whether or not the displaying of job transfers is enabled.

Note: Getter function for property jobTransfersVisible.

See also setJobTransfersVisible().

QSize KFileItemDelegate::maximumSize() const

Returns the maximum size for KFileItemDelegate::sizeHint().

Note: Getter function for property maximumSize.

See also setMaximumSize().

[override virtual] void KFileItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

Reimplements: QAbstractItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const.

Paints the item indicated by index, using painter.

The item will be drawn in the rectangle specified by option.rect. The correct size for that rectangle can be obtained by calling sizeHint().

This function will use the following data values if the model provides them for the item, in place of the values in option:

This function is reimplemented from QAbstractItemDelegate.

painter The painter with which to draw the item.

option The style options that should be used when painting the item.

index The index to the item that should be painted.

void KFileItemDelegate::setJobTransfersVisible(bool jobTransfersVisible)

Enable/Disable the displaying of an animated overlay that is shown for any destination urls (in the view). When enabled, the animations (if any) will be drawn automatically.

Only the files/folders that are visible and have jobs associated with them will display the animation. You would likely not want this enabled if you perform some kind of custom painting that takes up a whole item, and will just make this(and what you paint) look funky.

Default is disabled.

Note: The model (KDirModel) needs to have it's method called with the same value, when you make the call to this method.

Note: Setter function for property jobTransfersVisible.

See also jobTransfersVisible().

void KFileItemDelegate::setMaximumSize(const QSize &size)

Sets the maximum size for KFileItemDelegate::sizeHint().

Note: Setter function for property maximumSize.

See also maximumSize().

void KFileItemDelegate::setShadowBlur(qreal radius)

Sets the blur radius for the text shadow.

Note: Setter function for property shadowBlur.

See also shadowBlur().

void KFileItemDelegate::setShadowColor(const QColor &color)

Sets the color used for drawing the text shadow.

To enable text shadows, set the shadow color to a non-transparent color. To disable text shadows, set the color to Qt::transparent.

Note: Setter function for property shadowColor.

See also shadowColor().

void KFileItemDelegate::setShadowOffset(const QPointF &offset)

Sets the horizontal and vertical offset for the text shadow.

Note: Setter function for property shadowOffset.

See also shadowOffset().

void KFileItemDelegate::setShowInformation(KFileItemDelegate::Information information)

Sets a single information line that is shown below the icon label in list views.

This is a convenience function for when you only want to show a single line of information.

information The information that should be shown

Note: Setter function for property information.

See also showInformation().

void KFileItemDelegate::setShowInformation(const KFileItemDelegate::InformationList &list)

Sets the list of information lines that are shown below the icon label in list views.

You will typically construct the list like this:

KFileItemDelegate::InformationList list;
list << KFileItemDelegate::FriendlyMimeType << KFileItemDelegate::Size;
delegate->setShowInformation(list);

The information lines will be displayed in the list order. The delegate will first draw the item label, and then as many information lines as will fit in the available space.

list A list of information items that should be shown

Note: Setter function for property information.

void KFileItemDelegate::setShowToolTipWhenElided(bool showToolTip)

Sets whether a tooltip should be shown if the display role is elided containing the full display role information.

Note: The tooltip will only be shown if the Qt::ToolTipRole differs from Qt::DisplayRole, or if they match, showToolTipWhenElided flag is set and the display role information is elided.

Note: Setter function for property showToolTipWhenElided.

See also showToolTipWhenElided().

void KFileItemDelegate::setWrapMode(QTextOption::WrapMode wrapMode)

When the contents text needs to be wrapped, wrapMode strategy will be followed.

See also wrapMode().

qreal KFileItemDelegate::shadowBlur() const

Returns the blur radius for the text shadow.

Note: Getter function for property shadowBlur.

See also setShadowBlur().

QColor KFileItemDelegate::shadowColor() const

Returns the color used for the text shadow.

Note: Getter function for property shadowColor.

See also setShadowColor().

QPointF KFileItemDelegate::shadowOffset() const

Returns the offset used for the text shadow.

Note: Getter function for property shadowOffset.

See also setShadowOffset().

[slot] QRegion KFileItemDelegate::shape(const QStyleOptionViewItem &option, const QModelIndex &index)

Returns the shape of the item as a region.

The returned region can be used for precise hit testing of the item.

KFileItemDelegate::InformationList KFileItemDelegate::showInformation() const

Returns the file item information that should be shown below item labels in list views.

Note: Getter function for property information.

See also setShowInformation().

bool KFileItemDelegate::showToolTipWhenElided() const

Returns whether a tooltip should be shown if the display role is elided containing the full display role information.

Note: The tooltip will only be shown if the Qt::ToolTipRole differs from Qt::DisplayRole, or if they match, showToolTipWhenElided flag is set and the display role information is elided.

Note: Getter function for property showToolTipWhenElided.

See also setShowToolTipWhenElided().

[override virtual] QSize KFileItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const

Reimplements: QAbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const.

Returns the nominal size for the item referred to by index, given the provided options.

If the model provides a valid Qt::FontRole and/or Qt::TextAlignmentRole for the item, those will be used instead of the ones specified in the style options.

This function is reimplemented from QAbstractItemDelegate.

option The style options that should be used when painting the item.

index The index to the item for which to return the size hint.

QTextOption::WrapMode KFileItemDelegate::wrapMode() const

Returns the wrapping strategy followed to show text when it needs wrapping.

See also setWrapMode().