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 padding: Kirigami.Settings.tabletMode ? Kirigami.Units.largeSpacing : Kirigami.Units.mediumSpacing
67 spacing: Kirigami.Settings.tabletMode ? Kirigami.Units.largeSpacing * 2 : Kirigami.Units.smallSpacing
68
69 horizontalPadding: padding + Math.round(Kirigami.Units.smallSpacing / 2)
70 leftPadding: horizontalPadding
71 rightPadding: horizontalPadding
72
73 verticalPadding: padding
74 topPadding: verticalPadding
75 bottomPadding: verticalPadding
76
77 hoverEnabled: !Kirigami.Settings.isMobile
78
79 topInset: if (root.index !== undefined && index === 0 && listView.view && listView.view.topMargin === 0) {
80 Kirigami.Units.smallSpacing;
81 } else {
82 Math.round(Kirigami.Units.smallSpacing / 2);
83 }
84 bottomInset: if (root.index !== undefined && listView.view && index === listView.view.count - 1 && listView.view.bottomMargin === 0) {
85 Kirigami.Units.smallSpacing;
86 } else {
87 Math.round(Kirigami.Units.smallSpacing / 2)
88 }
89 rightInset: Kirigami.Units.smallSpacing
90 leftInset: Kirigami.Units.smallSpacing
91
92 icon {
93 width: if (contentItem instanceof SubtitleContentItem) {
94 return Kirigami.Units.iconSizes.large
95 } else {
96 return Kirigami.Settings.tabletMode ? Kirigami.Units.iconSizes.smallMedium : Kirigami.Units.iconSizes.sizeForLabels
97 }
98
99 height: if (contentItem instanceof SubtitleContentItem) {
100 return Kirigami.Units.iconSizes.large
101 } else {
102 return Kirigami.Settings.tabletMode ? Kirigami.Units.iconSizes.smallMedium : Kirigami.Units.iconSizes.sizeForLabels
103 }
104 }
105
106 Accessible.description: if (contentItem instanceof SubtitleContentItem) {
107 contentItem.subtitle
108 } else {
109 ""
110 }
111
112 background: Rectangle {
113 radius: Kirigami.Units.cornerRadius
114
115 color: if (root.highlighted || root.checked || (root.down && !root.checked) || root.visualFocus) {
116 const highlight = Kirigami.ColorUtils.tintWithAlpha(Kirigami.Theme.backgroundColor, Kirigami.Theme.highlightColor, 0.3);
117 if (root.hovered || root.dropAreaHovered) {
118 return Kirigami.ColorUtils.tintWithAlpha(highlight, Kirigami.Theme.textColor, 0.10);
119 } else if (highlight.valid) {
120 return highlight;
121 } else {
122 return 'transparent';
123 }
124 } else if (root.hovered || root.dropAreaHovered) {
125 return Kirigami.ColorUtils.tintWithAlpha(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, 0.10)
126 } else {
127 return 'transparent'
128 }
129
130 border {
131 color: Kirigami.Theme.highlightColor
132 width: root.visualFocus || root.activeFocus ? 1 : 0
133 }
134 }
135
136 contentItem: DefaultContentItem {
137 itemDelegate: root
138 }
139}
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 Feb 21 2025 11:53:19 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.