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 implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
36 implicitContentWidth + leftPadding + rightPadding,
37 implicitIndicatorWidth + leftPadding + rightPadding)
38 implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
39 implicitContentHeight + topPadding + bottomPadding,
40 implicitIndicatorHeight + topPadding + bottomPadding,
41 Kirigami.Units.gridUnit * 2)
42
43 width: if (listView.view) {
44 return listView.view.width;
45 } else if (gridView.view) {
46 return gridView.view.cellWidth;
47 } else {
48 implicitWidth
49 }
50
51 height: if (gridView.view) {
52 return gridView.view.cellHeight;
53 } else {
54 return implicitHeight;
55 }
56 highlighted: listView.isCurrentItem || gridView.isCurrentItem
57
58 spacing: Kirigami.Units.mediumSpacing
59
60 padding: Kirigami.Units.mediumSpacing
61
62 horizontalPadding: padding + Math.round(Kirigami.Units.smallSpacing / 2)
63 leftPadding: horizontalPadding
64 rightPadding: horizontalPadding
65
66 verticalPadding: padding
67 topPadding: verticalPadding
68 bottomPadding: verticalPadding
69
70 hoverEnabled: true
71
72 topInset: if (root.index !== undefined && index === 0 && listView.view && listView.view.topMargin === 0) {
73 Kirigami.Units.smallSpacing;
74 } else {
75 Math.round(Kirigami.Units.smallSpacing / 2);
76 }
77 bottomInset: if (root.index !== undefined && listView.view && index === listView.view.count - 1 && listView.view.bottomMargin === 0) {
78 Kirigami.Units.smallSpacing;
79 } else {
80 Math.round(Kirigami.Units.smallSpacing / 2)
81 }
82 rightInset: Kirigami.Units.smallSpacing
83 leftInset: Kirigami.Units.smallSpacing
84
85 icon {
86 width: if (contentItem instanceof SubtitleContentItem) {
87 Kirigami.Units.iconSizes.large
88 } else {
89 Kirigami.Units.iconSizes.sizeForLabels
90 }
91
92 height: if (contentItem instanceof SubtitleContentItem) {
93 Kirigami.Units.iconSizes.large
94 } else {
95 Kirigami.Units.iconSizes.sizeForLabels
96 }
97 }
98
99 Accessible.description: if (contentItem instanceof SubtitleContentItem) {
100 contentItem.subtitle
101 } else {
102 ""
103 }
104
105 background: Rectangle {
106 radius: Kirigami.Units.cornerRadius
107
108 color: if (root.highlighted || root.checked || (root.down && !root.checked) || root.visualFocus) {
109 const highlight = Kirigami.ColorUtils.tintWithAlpha(Kirigami.Theme.backgroundColor, Kirigami.Theme.highlightColor, 0.3);
110 if (root.hovered) {
111 Kirigami.ColorUtils.tintWithAlpha(highlight, Kirigami.Theme.textColor, 0.10)
112 } else {
113 highlight
114 }
115 } else if (root.hovered) {
116 Kirigami.ColorUtils.tintWithAlpha(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, 0.10)
117 } else {
118 Kirigami.Theme.backgroundColor
119 }
120
121 border {
122 color: Kirigami.Theme.highlightColor
123 width: root.visualFocus || root.activeFocus ? 1 : 0
124 }
125
126 Behavior on color {
127 ColorAnimation {
128 duration: Kirigami.Units.shortDuration
129 }
130 }
131 }
132
133 contentItem: DefaultContentItem {
134 itemDelegate: root
135 }
136}
Content item which is used by default in the RoundedItemDelegate and IndicatorItemDelegate.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.