Kirigami-addons

RoundedItemDelegate.qml
1// SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
2// SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
3
4import QtQuick
5import QtQuick.Layouts
6import QtQuick.Controls as QQC2
7import QtQuick.Templates as T
8import org.kde.kirigami as Kirigami
9
10/**
11 * An item delegate providing a modern look and feel. Use a combination of
12 * SubtitleContentItem, DefaultContentItem and RowLayout for the contentItem.
13 *
14 * @image html roundeditemdelegate.html
15 */
16T.ItemDelegate {
17 id: root
18
19 /**
20 * This property holds a ListView
21 *
22 * It is automatically set if the RoundedItemDelegate is the direct delegate
23 * of a ListView and must be set otherwise.
24 */
25 property var listView: ListView
26
27 /**
28 * This property holds a GridView
29 *
30 * It is automatically set if the RoundedItemDelegate is the direct delegate
31 * of a GridView and must be set otherwise.
32 */
33 property var gridView: GridView
34
35 /**
36 * This property holds whether the drop area is hovered.
37 *
38 * This allow to emulate an hover effect which can't be done with the
39 * normal hovered property as it is read only.
40 */
41 property bool dropAreaHovered: false
42
43 implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
44 implicitContentWidth + leftPadding + rightPadding,
45 implicitIndicatorWidth + leftPadding + rightPadding)
46 implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
47 implicitContentHeight + topPadding + bottomPadding,
48 implicitIndicatorHeight + topPadding + bottomPadding,
49 Kirigami.Units.gridUnit * 2)
50
51 width: if (listView.view) {
52 return listView.view.width;
53 } else if (gridView.view) {
54 return gridView.view.cellWidth;
55 } else {
56 implicitWidth
57 }
58
59 height: if (gridView.view) {
60 return gridView.view.cellHeight;
61 } else {
62 return implicitHeight;
63 }
64 highlighted: listView.isCurrentItem || gridView.isCurrentItem
65
66 spacing: Kirigami.Units.mediumSpacing
67
68 padding: Kirigami.Units.mediumSpacing
69
70 horizontalPadding: padding + Math.round(Kirigami.Units.smallSpacing / 2)
71 leftPadding: horizontalPadding
72 rightPadding: horizontalPadding
73
74 verticalPadding: padding
75 topPadding: verticalPadding
76 bottomPadding: verticalPadding
77
78 hoverEnabled: true
79
80 topInset: if (root.index !== undefined && index === 0 && listView.view && listView.view.topMargin === 0) {
81 Kirigami.Units.smallSpacing;
82 } else {
83 Math.round(Kirigami.Units.smallSpacing / 2);
84 }
85 bottomInset: if (root.index !== undefined && listView.view && index === listView.view.count - 1 && listView.view.bottomMargin === 0) {
86 Kirigami.Units.smallSpacing;
87 } else {
88 Math.round(Kirigami.Units.smallSpacing / 2)
89 }
90 rightInset: Kirigami.Units.smallSpacing
91 leftInset: Kirigami.Units.smallSpacing
92
93 icon {
94 width: if (contentItem instanceof SubtitleContentItem) {
95 Kirigami.Units.iconSizes.large
96 } else {
97 Kirigami.Units.iconSizes.sizeForLabels
98 }
99
100 height: if (contentItem instanceof SubtitleContentItem) {
101 Kirigami.Units.iconSizes.large
102 } else {
103 Kirigami.Units.iconSizes.sizeForLabels
104 }
105 }
106
107 Accessible.description: if (contentItem instanceof SubtitleContentItem) {
108 contentItem.subtitle
109 } else {
110 ""
111 }
112
113 background: Rectangle {
114 radius: Kirigami.Units.cornerRadius
115
116 color: if (root.highlighted || root.checked || (root.down && !root.checked) || root.visualFocus) {
117 const highlight = Kirigami.ColorUtils.tintWithAlpha(Kirigami.Theme.backgroundColor, Kirigami.Theme.highlightColor, 0.3);
118 if (root.hovered || root.dropAreaHovered) {
119 Kirigami.ColorUtils.tintWithAlpha(highlight, Kirigami.Theme.textColor, 0.10)
120 } else {
121 highlight
122 }
123 } else if (root.hovered || root.dropAreaHovered) {
124 Kirigami.ColorUtils.tintWithAlpha(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, 0.10)
125 } else {
126 Kirigami.Theme.backgroundColor
127 }
128
129 border {
130 color: Kirigami.Theme.highlightColor
131 width: root.visualFocus || root.activeFocus ? 1 : 0
132 }
133
134 Behavior on color {
135 ColorAnimation {
136 duration: Kirigami.Units.shortDuration
137 }
138 }
139 }
140
141 contentItem: DefaultContentItem {
142 itemDelegate: root
143 }
144}
Content item which is used by default in the RoundedItemDelegate and IndicatorItemDelegate.
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:49:11 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.